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

Ryan Ingram ryani.spam at gmail.com
Sun Dec 21 14:02:24 EST 2008

You have a few options.

In Haskell98 (no extensions):

a () = (f,f)
g () = fst (a ())
-- alternatively
g x = fst (a ()) x

Here you make it explicit that "a" and "g" are functions; the
monomorphism restriction is there to stop things that look like values
(and therefore you expect to only get evaluated once) from being
functions (which get evaluated every time they are used).

Alternatively, you are allowed to tell the compiler "This value should
be polymorphic" with a type signature:

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

The non-haskell98 solution:

{-# LANGUAGE NoMonomorphismRestriction #-}

But beware that it is really there to protect you; code that uses g
will compile to something very similar to the first operation I
showed, re-evaluating "fst a" and doing dictionary selection at every
use of g.

  -- ryan

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