考慮下面的一段Haskell代碼:哈斯克爾類型錯誤
type Parser a = String -> [(a, String)]
item :: Parser Char
item = \ inp -> case inp of
[] -> []
(x:xs) -> [(x, xs)]
ret :: a -> Parser a
ret v = \ inp -> [(v, inp)]
parse :: Parser a -> String -> [(a, String)]
parse p inp = p inp
pseq :: Parser (Char, Char)
pseq = do x <- item
item
y <- item
ret (x, y)
ac = parse pseq "abcdef"
當試圖運行擁抱(版本2006年9月)上面的代碼中,我得到了以下錯誤消息:
Type error in explicitly typed binding
*** Term : pseq
*** Type : [Char] -> [(([(Char,[Char])],[(Char,[Char])]),[Char])]
*** Does not match : Parser (Char,Char)
並且,當我刪除「pseq」的類型聲明時,出現以下錯誤消息:
Unresolved top-level overloading
*** Binding : pseq
*** Outstanding context : Monad ((->) [Char])