[Haskell-beginners] Why is type "Integer -> Integer" and not
"(Num a) => a -> a"?
Felipe Lessa
felipe.lessa at gmail.com
Thu Nov 12 05:40:49 EST 2009
On Thu, Nov 12, 2009 at 7:06 AM, Shawn Willden
<shawn-haskell at willden.org> wrote:
> Hmm. Would that also explain this?
>
> Prelude> let f1 x = x * 2
> Prelude> :type f1
> f1 :: (Num a) => a -> a
> Prelude> let f2 = \x -> f1 x
> Prelude> :type f2
> f2 :: Integer -> Integer
Yes, that's the same monomorphism restriction. Also, note that you
are defaulting to Integer here:
Prelude> :s -Wall
Prelude> let f1 x = x * 2
Prelude> :t f1
f1 :: (Num a) => a -> a
Prelude> let f2 = \x -> f1 x
<interactive>:1:15:
Warning: Defaulting the following constraint(s) to type `Integer'
`Num a' arising from a use of `f1' at <interactive>:1:15-18
In the expression: f1 x
In the expression: \ x -> f1 x
In the definition of `f2': f2 = \ x -> f1 x
Prelude> let f3 :: Int -> Int; f3 = \x -> f1 x
Prelude> let f4 :: Num a => a -> a; f4 = \x -> f1 x
I find -Wall very useful. Relatively few times it gets annoying.
HTH,
--
Felipe.
More information about the Beginners
mailing list