The dreaded M-R

Andrew Pimlott andrew at pimlott.net
Fri Jan 27 17:05:00 EST 2006


On Fri, Jan 27, 2006 at 07:04:43PM +0100, Benjamin Franksen wrote:
> All normal bindings (i.e. using '=') should be as polymorphic and 
> general as possible.

I agree with the position in the Ben's email.  Also, especially since
much of the discussion has considered the impact on beginners, I want to
recall that the monomorphism restriction can cause highly confusing
error messages.  They do not necessarily mention "probably cause:
monomorphism restriction", and one cannot always find the problem by
looking at the error location and the definition.  I have a feeling
there should be a better example of this issue floating around, but not
finding one I'll submit my own:

    bar :: IO Char
    bar = foo

    foo = return 'a'

    baz = "hello " ++ foo

ghc 6.4.1 gives the error

    try.hs:2:6:
        Couldn't match `IO' against `[]'
          Expected type: IO Char
          Inferred type: [Char]
        In the definition of `bar': bar = foo

If you imagine that baz is buried at the bottom of the module in some
test code, you might see how this could be mystifying.  After all,
there's no list mentioned in foo or bar.  (But perhaps the error message
could be improved to mention the monomorphism restriction?)

My feeling is that when a variable definition should be monomorphic (and
thus shared), the programmer ought to give the monomorphic type
explicitly, rather than count on some use of the variable (possibly far
away in the code) to decide it implicitly.

Andrew


More information about the Haskell-prime mailing list