[Haskell-cafe] RLE in Haskell: why does the type variable get
instantiated?
Pekka Karjalainen
p3k at iki.fi
Tue Jul 31 10:48:07 EDT 2007
On 7/31/07, Chris Eidhof <chris at eidhof.nl> wrote:
> Hey Haskell-Cafe,
>
> I was trying out the code in Dons's article [1], and I noticed a
> weird thing when doing it in GHCi. When binding the function
> composition to a variable, the type suddenly changes. I'm not
> completely sure why this happens. Is this because GHCi is in a monad
> and wants to find an instance for the type variable? Here's my GHCi
> session: [ ... ]
Apfelmus already explained why it happens. I'd like to add one thing.
If you are experimenting with GHCi, you can turn the restriction off
with the option -fno-monomorphism-restriction. In GHCi itself this is
given as follows:
Prelude> :set -fno-monomorphism-restriction
After giving this and entering the same things as in your original
message, the type of encode came out to be as follows:
Prelude Control.Arrow Data.List> let encode = map (length &&& head) . group
Prelude Control.Arrow Data.List> :t encode
encode :: (Eq a) => [a] -> [(Int, a)]
This option also saves some typing (of the variety you do on the
keyboard!) when you just want to use GHCi as a calculator:
Prelude Control.Arrow Data.List> let x = 5
Prelude Control.Arrow Data.List> let y = 6.3
Prelude Control.Arrow Data.List> x*y
31.5
Instead of Integer, the type of x is now x :: (Num t) => t without the
restriction, and I don't need to add fromInteger to the
multiplication.
I don't recommend you to use this option all the time, of course. It's
just a convenience.
Pekka
More information about the Haskell-Cafe
mailing list