[Haskell-cafe] Is this related to monomorphism restriction?

Reiner Pope reiner.pope at gmail.com
Sat Dec 20 18:56:18 EST 2008


On Sun, Dec 21, 2008 at 10:28 AM, Maurí­cio <briqueabraque at yahoo.com> wrote:
> Hi,
>
> Why isn't the last line of this code allowed?
>
> f :: (TestClass a) => a -> Integer
> f = const 1
> a = (f,f)
> g = fst a
>
> The only thing I can think about is monomorphism
> restriction, but it's allowed (or even the third
> line would not be accepted). Is there something
> I could read to understand that?
>
> Thanks,
> Maurício
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>

The monomorphism restriction refuses to accept the definition of a.
However, even if we turn the monomorphism restriction off there is
still a problem.

> {-# LANGUAGE NoMonomorphismRestriction #-}
>
> f :: (TestClass a) => a -> Integer
> f = const 1
> a = (f,f)
> g = fst a

In this case, the definition of a is accepted, but not the definition
of g. The reason is that a has type

a :: (TestClass a, TestClass b) => (a,b)

and then when we take 'fst' of this value (as in g) we get

g :: (TestClass a, TestClass b) => a

which is an ambiguous type, since there is no way to tell the compiler
what 'b' is when running g.

Cheers,
Reiner


More information about the Haskell-Cafe mailing list