[Haskell-beginners] Why is type "Integer -> Integer" and not
"(Num a) => a -> a"?
jfredett at gmail.com
Thu Nov 12 03:45:08 EST 2009
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
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.
On Nov 12, 2009, at 3:37 AM, Dag Hovland wrote:
> 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
> the polymorphic (Num a) => a -> a. I believed that f1, f2 and the
> expression should all have the same type. Similar output to that above
> is given by Hugs.
> Dag Hovland
> Beginners mailing list
> Beginners at haskell.org
More information about the Beginners