Consider the following piece of Haskell code:
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"
When trying to run the above code in hugs (Version September 2006), I get the following error message:
Type error in explicitly typed binding
*** Term : pseq
*** Type : [Char] -> [(([(Char,[Char])],[(Char,[Char])]),[Char])]
*** Does not match : Parser (Char,Char)
And, when I remove my type declaration for “pseq”, I get the following error message:
Unresolved top-level overloading
*** Binding : pseq
*** Outstanding context : Monad ((->) [Char])
Making Parser a Monad is easy, and I think resorting to ListT or StateT is probably overkill here.
Lastly, you use the return type of
[(a, String)], so that you can indicate things which can’t parse. But the list only ever has zero or one items in it. You should look into the typesMaybeandEitherwhich would likely be clearer in this case.