2012-08-07 29 views
0

這是關於Data.List.Class的使用問題的問題(與上一個關於Haskell Data.List.Class and syntax的問題有關)。Haskell data.list.class用法

下面列出了代碼的相關部分。根據我的理解,這個類提供了一個類型類接口或Haskell列表類型的泛化,這可能非常有用。但是,在這個課程的用法上,我找不到很多文檔。有沒有人知道一個好的教程?

此外,我有一個關於其用法和類型的技術/具體問題。從代碼中,我認爲在類型定義中,runList和joinL是相互顛倒的(從某種意義上說)。

-- other stuff omitted 
import Data.Functor.Identity (Identity(..)) 

data ListItem l a = 
    Nil | 
    Cons { headL :: a, tailL :: l a } 

Data.List.Class 
-- | A class for list types. Every list has an underlying monad. 
class (MonadPlus l, Monad (ItemM l)) => List l where 
    type ItemM l :: * -> * 
    runList :: l a -> ItemM l (ListItem l a) 
    joinL :: ItemM l (l a) -> l a 
    cons :: a -> l a -> l a 
    cons = mplus . return 

instance List [] where 
    type ItemM [] = Identity 
    runList [] = Identity Nil 
    runList (x:xs) = Identity $ Cons x xs 
    joinL = runIdentity 
    cons = (:) 

fromList :: List l => [a] -> l a 
fromList = foldr cons mzero 

首先,我在emacs模式進入joinL $ runList [1, 2, 3],但我得到了以下錯誤:

Couldn't match type `ItemM (ListItem [])' with `Identity' 
Expected type: ItemM (ListItem []) (ListItem [] Integer) 
    Actual type: ItemM [] (ListItem [] Integer) 

,因爲它說,在預期和實際類型不完全匹配。但我不明白爲什麼他們應該首先要求不同的類型。 runList :: l a -> ItemM l (ListItem l a)joinL :: ItemM l (l a) -> l a在意義或語義方面有什麼不同?

而且,我試圖在Emacs模式很簡單的功能fromlist裏如下:fromList [1,2,3],但我得到了:

Ambiguous type variable `l0' in the constraint: 
    (List l0) arising from a use of `fromList' 
Probable fix: add a type signature that fixes these type variable(s) 
In the expression: fromList [1, 2, 3] 
In an equation for `it': it = fromList [1, 2, 3] 

我很困惑在這裏爲什麼有一個模棱兩可,以及如何添加一個類型簽名作爲錯誤消息提示。任何人都可以解釋它嗎?

由於提前,

回答

1

對於最後一個問題:它可分爲多種類型實現列表類型類,你應該指定它,如果它不會被後面推斷的。例如fromList [1, 2, 3] :: [Int]工作正常。