Context not part of a function definition?

Bernard James POPE bjpop@cs.mu.OZ.AU
Mon, 17 Mar 2003 16:50:22 +1100 (EST)


> I'm trying to use the following idiom to selectively import functions from 
> the List module:
> 
>    import qualified List
>    nub    = List.nub
> 
> but I'm finding that HUGS complains about "unresolved top level 
> overloading" with "outstanding context:  "Eq b".
> 
> If I duplicate the type signature thus:
> 
>    import qualified List
>    nub    :: Eq a => [a] -> [a]
>    nub    = List.nub
> 
> All seems to be well.  I find it counter-intuitive that I cannot simply define
> 
>    a = b
> 
> such that any occurrence of a is equivalent to an occurrence of b.  I 
> thought that was the point of referential transparency in functional 
> languages.  I don't know where to look in the Haskell documents to decide 
> whether the above is a bug or truly according to the language spec.

You have struck the monomorphism restriction.

See:
http://research.microsoft.com/Users/simonpj/haskell98-revised/haskell98-report-html/decls.html#sect4.5.5

This is an often tripped over snag in Haskell 98's type system.

Cheers,
Bernie.