[Haskell-beginners] Question about List Type Constraint and Null

Jürgen Doser jurgen.doser at gmail.com
Sat Jan 22 19:45:40 CET 2011


El sáb, 22-01-2011 a las 11:06 -0600, aditya siram escribió:
> Ok I've figured out why I can't compile it. But now I'm more confused
> than ever. The problem was that I have a -XNoMonomorphismRestriction
> flag on Ghci. Removing this allowed me to compile.

This is indeed a bit tricky, it seems. res has a type-class polymorphic
type Read a => ... (the ... is complicated by the fact that you have
given a strange signature to test, see below)

With the monomorphism restriction on, ghc takes the type-signature for
test, inferes a type for res in the line Just $ fst $ head res, and uses
this type for the null res test. Interestingly, it doesn't infer a
monomorphic type for res, but insists that the instantiation for the
type-class parameter is the same in both occurences.

With no monomorphism restriction, ghc doesn't do this inference, because
the type for res in null res may be different to the type of res in Just
$ fst $ head res. (i.e., the instantion for the type-class parameter may
be different)

Finally, are you sure that the given type signature is what you want?
Don't you want

test ::  Read a => String -> Maybe a


	Jürgen





More information about the Beginners mailing list