Consider the following Haskell function:
sign a
| a < 0 = (-1)
| a > 0 = 1
| otherwise = 0
When I load this into ghci I expected :t sign
to be:
sign :: (Num a, Ord a) => a -> Integer
Instead it inferred it as:
*Main> :t sign
sign :: (Num a1, Num a, Ord a1) => a1 -> a
Similarly, if I ask for the type of the integer 5
, I expected Integer
, but instead I got
*Main> :t 5
5 :: Num a => a
There's something I am not understanding about Haskell's types. The thing is, if all I know about the return type of sign
is that it is an instance of the Num
typeclass, then I should not be able to pass its return value into this function:
double :: Integer -> Integer
double x = x * 2
That is, my double
function requires an Integer
, not just any instance of Num
.
Yet, the following works just fine:
*Main> double (sign 5.5)
2
What is it that I am mis-understanding about Haskell's type system?
signum :: Num a => a -> a
, i.e you pass a number into it, and get back a number of the same type. – mruegdouble
gives the type inference engine more knowledge--it knows it works withdouble
, and figures out how to make that possible. – jpaugh