This is because
Every function in Haskell takes a single parameter and returns a single value
If a function need to take multiple value, the function would have been a curried function or it have to take a single tuple.
If we add a parentheses, the function signature becomes:
sum :: (Num a) => a -> (a -> a)
In Haskell, the function signature: A -> B
means a function the "domain" of the function is A
and the "Codomain" of the function is B
; or in a programmer's language, the function takes a parameter of type A
and returns a value of type B
.
Therefore, the function definition sum :: Num -> (Num -> Num)
means sum is "a function that takes a parameter of type a
and returns a function of type Num -> Num
".
In effect, this leads to currying/partial function.
The concept of currying is essential in functional languages like Haskell, because you will want to do things like:
map (sum 5) [1, 2, 3, 5, 3, 1, 3, 4] -- note: it is usually better to use (+ 5)
In that code, (sum 5) is a function that takes a single parameter, this function (sum 5) will be called for each item in the list, e.g. ((sum 5) 1) returns 6.
If sum
had a signature of sum :: (Num, Num) -> Num
, then sum would have to receive both of its parameter at the same time because now sum is a "function that receives a tuple (Num, Num)
and returns a Num".
Now, the second question, what does Num a => a -> a
means? It's basically a shorthand for saying that each time you see a
in the signature, replace it with Num or with one of its derived class.