[Haskell-beginners] Parsec Monad Type Confusion

Stephen Tetley stephen.tetley at gmail.com
Sat Jul 10 07:22:49 EDT 2010


On 10 July 2010 10:54, Magnus Therning <magnus at therning.org> wrote:
>
> AFAICS that change should make no difference at all.  In fact the line
> could also be written as
>
>    return . Number . read $ x
>


Yeah - it was a rather poor answer especially as on a proper reading
the original poster's question was even asking about that...

Here's a better answer:

Starting from the line:

x <- many1 digit

Without a type decl, at this point x :: [a]. /a/ is constrained, as
the parsers answer type as the whole computation is with the ParsecT
monad transformer. But other than that /a/ is polymorphic.

The next line with the read obliges x to be a String (for read to type check):

return $ Number . read $ x

But this is not enough to give a full type to the whole computation,
with the lifting to Number of the "x" the type of the whole function
is going to be :: ParsecT s m Number.

The stream type /s/ and the monad type /m/ are not resolved. Adding a
type signature with the Parser type in Parsec 3 resolves the types of
the stream and monad via 2 type synonyms:

type Parser  = Parsec  String  ()

type Parsec s u = ParsecT  s u Identity

where u is a further type parameter for user state.

The base monad is resolved to Identity, the stream is resolved to String.


More information about the Beginners mailing list