[Haskell-beginners] Parsec Monad Type Confusion

Aleksandar Dimitrov aleks.dimitrov at googlemail.com
Fri Jul 9 19:31:11 EDT 2010


Hello list,

I have a rather trivial question, but one that I can't seem to get my
head around. In the following example function (using
Text.ParserCombinators.Parsec) I'm getting an error without the type
declaration, but it compiles just fine with it:

> parseNumber' :: Parser LispVal
> parseNumber'  = do x <- many1 digit
>                    return $ Number . read $ x

The error message when compiling without the type declaration is the
following:

    No instance for (Text.Parsec.Prim.Stream s m Char)
      arising from a use of `digit' at Scheme.hs:33:30-34
    Possible fix:
      add an instance declaration for (Text.Parsec.Prim.Stream s m Char)
    In the first argument of `many1', namely `digit'
    In a stmt of a 'do' expression: x <- many1 digit
    In the expression:
        do { x <- many1 digit;
               return $ Number . read $ x }

The function is an example in "Write yourself a Scheme in 48 hours." I'd
consider myself fairly accustomed to programming with monads; and still,
I can't seem to find out *why* exactly GHC ( 6.12.1 ) cannot compile
this without the type signature. What does it try to build here? Why
does the inferencing fail?

My best guess is that it somehow doesn't get that 'x' is supposed to be
a String value ([Char], not Char.) What really confuses me, though, is
that adding the instance declaration makes the whole thing work! The
type declaration for LispVal looks like this:

> data LispVal = Number Integer

So it should be pretty clear to GHC that, due to `Number . read`
demanding a [Char], I actually want x to be a [Char]. I'm assuming here
that I'm just in the wrong Monad, starting with `do`. Somehow, GHC
assumes it's not Parser LispVal, but something more complex, possibly
Text.Parsec.Prim.Stream s m LispVal or so?

Thank you everybody for you help :-) This list is a truly amazing place
with a lot of helpful people!
Aleks



More information about the Beginners mailing list