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

Daniel Fischer daniel.is.fischer at googlemail.com
Sat Jan 22 23:26:05 CET 2011


On Saturday 22 January 2011 23:11:07, aditya siram wrote:
> > The value of `null res' for a given String obviously depends on the
> > type at which res is used.
>
> Why does it matter what the type of res is as long as it is some kind
> of list? Doesn't 'reads' always gives [(a1,String)]? So why does it
> matter what type 'a1' is?

Because

reads "True, that" :: [(Bool,String)]
~> [(True,", that")]

and

reads "True, that" :: [(Int,String)]
~> []

So to evaluate `null res', the type of res must be known/fixed.
If it isn't, the compiler has two choices.
It can refuse to compile or it can compile and let the runtime throw an 
error "Can't evaluate because I don't know the type" (I think the second 
option isn't available for GHC).
In my opinion, the first option is preferable.

>
> Thanks for the clear explanation of the monomorphism restriction.
> -deech




More information about the Beginners mailing list