[Haskell-beginners] Why is type "Integer -> Integer" and not
"(Num a) => a -> a"?
Joe Fredette
jfredett at gmail.com
Thu Nov 12 03:45:08 EST 2009
Hi!
That's very weird, I don't have a good answer (and fortunately there
are far smarter people on this list who will have a better answer...)
but my instincts say it has to do with defaulting.
When GHC sees a literal digit, it tries to guess what it's type should
be, first via inference, but if that returns a polymorphic type (like
`Num a => a` or something) it will "default" to a particular type, for
literal whole positive/negative numbers, it defaults to `Integer`. My
guess is that, defining in GHCi
> let f x = x * 2
> let g = \x -> x * 2
the former doesn't default to anything (it just does inference) since
it's a function definition, and the latter defaults the '2' to an
Integer because it's a value -- or some suitable analog of that
situation.
What will really blow your mind, try having GHCi inspect the type of
> :t \x -> x * 2
(the defn. of `g` w/o the let...)
Short answer, I have no idea, but I think it has to do with defaulting.
/Joe
On Nov 12, 2009, at 3:37 AM, Dag Hovland wrote:
> Hi!
>
> I have a problem with understanding some types given by ghc and hugs.
> The file loaded is:
>
> f1 = \x -> x * 2
> f2 x = x * 2
>
> After they are loaded I get the following from ghci
>
> *Main> :t f1
> f1 :: Integer -> Integer
> *Main> :t f2
> f2 :: (Num a) => a -> a
> *Main> :t \x -> x * 2
> \x -> x * 2 :: (Num a) => a -> a
>
>
> I do not understand why f1 is given Integer -> Integer as a type and
> not
> the polymorphic (Num a) => a -> a. I believed that f1, f2 and the
> lambda
> expression should all have the same type. Similar output to that above
> is given by Hugs.
>
> Thanks,
>
> Dag Hovland
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
More information about the Beginners
mailing list