I'm trying to understand manually specifying Haskell function signatures better, and I don't understand the => operator in this context.
Here's my example:
Prelude> add a b = a + b
Prelude> add 1 2
3
Haskell has inferred this function signature:
Prelude> :info add
add :: Num a => a -> a -> a
This looks great, because if I were to have defined this manually, I would have done:
add :: Int -> Int -> Int
add a b = a + b
So, I understand the inferred type is better, because it allows:
Prelude> add 1.0 2.0
3.0
Prelude> add 1.0 2
3.0
But how should I read what is happening with
Num a => a -> a -> a
?
Why would this not be written, for example, as:
add :: Num -> Num -> Num
or
add :: Num a -> Num a -> Num a
?
Both of these fail:
add :: Num -> Num -> Num
[1 of 1] Compiling Main ( my.hs, interpreted )
my.hs:4:8: error:
• Expecting one more argument to ‘Num’
Expected a type, but ‘Num’ has kind ‘* -> Constraint’
• In the type signature: add :: Num -> Num -> Num
|
4 | add :: Num -> Num -> Num
| ^^^
my.hs:4:15: error:
• Expecting one more argument to ‘Num’
Expected a type, but ‘Num’ has kind ‘* -> Constraint’
• In the type signature: add :: Num -> Num -> Num
|
4 | add :: Num -> Num -> Num
| ^^^
my.hs:4:22: error:
• Expecting one more argument to ‘Num’
Expected a type, but ‘Num’ has kind ‘* -> Constraint’
• In the type signature: add :: Num -> Num -> Num
|
4 | add :: Num -> Num -> Num
| ^^^
Failed, no modules loaded.
and
add :: Num a -> Num a -> Num a
my.hs:4:8: error:
• Expected a type, but ‘Num a’ has kind ‘Constraint’
• In the type signature: add :: Num a -> Num a -> Num a
|
4 | add :: Num a -> Num a -> Num a
| ^^^^^
my.hs:4:17: error:
• Expected a type, but ‘Num a’ has kind ‘Constraint’
• In the type signature: add :: Num a -> Num a -> Num a
|
4 | add :: Num a -> Num a -> Num a
| ^^^^^
my.hs:4:26: error:
• Expected a type, but ‘Num a’ has kind ‘Constraint’
• In the type signature: add :: Num a -> Num a -> Num a
|
4 | add :: Num a -> Num a -> Num a
| ^^^^^
Failed, no modules loaded.
So I get that this is the correct syntax. But what is this operator "=>" called---that is, how would I read/pronounce the signature above in spoken English---and where can I look up its syntax in the function definition and elsewhere?