Because of defaulting. Num
is a 'defaultable' type class, meaning that if you leave it un-constrained, the compiler will make a few intelligent guesses as to which type you meant to use it as. Try putting that definition in a module, then running
:t f
in ghci
; it should tell you (IIRC) f :: Integer -> Integer -> Integer
. The compiler didn't know which a
you wanted to use, so it guessed Integer
; and since that worked, it went with that guess.
Why didn't it infer a polymorphic type for f
? Because of the dreaded[1] monomorphism restriction. When the compiler sees
f = (+)
it thinks 'f
is a value', which means it needs a single (monomorphic) type. Eta-expand the definition to
f x = (+) x
and you will get the polymorphic type
f :: Num a => a -> a -> a
and similarly if you eta-expand your first definition
f x = succ x
you don't need a type signature any more.
[1] Actual name from the GHC documentation!