functionhaskelltypestype-signaturehaskell-prelude

What is the way to describe the type signature of Haskell functions that are not type-specific?


Given a function like negate, it has the type signature:

negate :: Num a => a -> a

which I would describe as a being the type in the context of Num (correct me if you think I am wrong).

But I not fully sure how to describe something like last, which has the type signature:

last :: [a] -> a

My guess would be to say it isn't type-specific, and that it takes a list and produces a single value of the same type as the list. Is this the correct way to think about it?


Solution

  • First, a is not the type in the context of Num, but a type that has a Num instance.

    Num a => a -> a is a constrained polymorphic type, while [a] -> a is an unconstrained polymorphic type, or just polymorphic type for short. In the unconstrained case, a can be any type; in the constrained case, it must be a type that obeys the given constraints.