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

Iavor Diatchki iavor.diatchki at gmail.com
Sun Dec 21 14:25:06 EST 2008


Hello,
You can work around the monomorphism restriction with extensions but
to fix the ambiguity in your program that Reiner pointed out you'll
have to change the program to specify how you'd like to instantiate
"a".
here are all the types once again:
f :: (TestClass a) => a -> Integer
f = const 1

a :: (TestClass a, TestClass b) => (a -> Integer, b -> Integer)
a = (f,f)

g :: (TestClass a, TestClass b) => a -> Integer  -- ambiguous
g = fst a

Note that the type of 'g' to the right of '=>' does not mention 'b'.
This means that the type of 'g' is ambiguos because the type checker
does not know how to pick a type for 'b'.  To fix that, you could:
  1. Give 'a' a less general type, for example:  a :: (TestClass a) =>
(a -> Integer, a -> Integer)
  2. Write a type signature on the use of 'a':

g :: TestClass a => a -> Integer
g = fst (a :: (a -> Integer, a -> Integer))

Here we are using another GHC extension called "scoped type variables"
to associate the "a" in the type signature of "g" with the "a" in the
type annotation for the value "a".

Hope that this helps,
Iavor




On Sun, Dec 21, 2008 at 9:21 AM, Maurí­cio <briqueabraque at yahoo.com> wrote:
>>> 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 (...)
>
>> (...) 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, (...)
>
> Is there some version (i.e., set of extensions) of
> Haskell where this would be allowed?
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list