試驗存在類型。似乎是獲得某種類型靈活性的好方法。如何解開haskell存在類型?
我在將它包裝起來後拆開存在類型時遇到了問題。我的代碼如下:
{-# LANGUAGE ExistentialQuantification #-}
class Eq a => Blurb a
data BlurbBox = forall a . Blurb a => BlurbBox a
data Greek = Alpha | Beta deriving Eq
instance Blurb Greek
data English = Ay | Bee deriving Eq
instance Blurb English
box1 :: BlurbBox
box1 = BlurbBox Alpha
box2 :: BlurbBox
box2 = BlurbBox Ay
main = do
case box1 of
BlurbBox Alpha -> putStrLn "Alpha"
BlurbBox Beta -> putStrLn "Beta"
BlurbBox Ay -> putStrLn "Ay"
BlurbBox Bee -> putStrLn "Bee"
此代碼編譯爲main,然後抱怨BlurbBox Alpha的類型。我如何去拆箱/拆包存在類型?
你有鏈接瞭解這種翻譯如何/在哪裏發生? BlurbBox(構造函數)類型爲'forall a。 Blurb a => a - > BlurbBox',但類型本身(通常)與isomorphic to ..? – nicolas 2015-12-11 13:50:41
,可以幫助。 https://mail.haskell.org/pipermail/haskell-cafe/2010-May/078254.html TAPL的練習23.4.8和章節24.3也是有用的 – nicolas 2015-12-11 14:53:38