``````add x y z = x + y + z
``````

``````Prelude> add 1 2 3
6
``````

``````Prelude> :t add
add :: Num a => a -> a -> a -> a
``````

``````add ::
``````

``````Num a =>
``````

``````a -> a -> a -> a
``````

Haskell就简单地使用`->`符号来标记参数和返回值，并不用别的符号来区分。这个不会引起歧义，因为只有最后一个代表返回值。

• `Num`叫做Type class
• `Num a`中的`a`叫做Type variable

``````add x y z = x + y + z
``````

``````add :: Num a => a -> a -> a -> a
``````

``````Prelude> :t (+)
(+) :: Num a => a -> a -> a
``````

``````Num a => a -> a -> a
``````

``````Prelude> cons x y z = x ++ y ++ z
``````

``````Prelude> :t cons
cons :: [a] -> [a] -> [a] -> [a]
``````

``````Prelude> :t (++)
(++) :: [a] -> [a] -> [a]
``````

``````Prelude> cons [1] [2] [3]
[1,2,3]
``````
``````Prelude> cons ['a'] ['b'] ['c']
"abc"
``````

``````Prelude> cons ['a'] ['b'] [1]

<interactive>:12:19: error:
• No instance for (Num Char) arising from the literal ‘1’
• In the expression: 1
In the third argument of ‘cons’, namely ‘[1]’
In the expression: cons ['a'] ['b'] [1]
``````

``````Prelude> cons :: Num a => [a] -> [a] -> [a] -> [a] ; cons x y z = x ++ y ++ z
``````

``````Prelude> cons [1] [2] [3]
[1,2,3]
Prelude> cons ['a'] ['b'] ['c']

<interactive>:21:1: error:
• No instance for (Num Char) arising from a use of ‘cons’
• In the expression: cons ['a'] ['b'] ['c']
In an equation for ‘it’: it = cons ['a'] ['b'] ['c']
``````

``````Prelude> :info Num
class Num a where
(+) :: a -> a -> a
(-) :: a -> a -> a
(*) :: a -> a -> a
negate :: a -> a
abs :: a -> a
signum :: a -> a
fromInteger :: Integer -> a
{-# MINIMAL (+), (*), abs, signum, fromInteger, (negate | (-)) #-}
-- Defined in ‘GHC.Num’
instance Num Word -- Defined in ‘GHC.Num’
instance Num Integer -- Defined in ‘GHC.Num’
instance Num Int -- Defined in ‘GHC.Num’
instance Num Float -- Defined in ‘GHC.Float’
instance Num Double -- Defined in ‘GHC.Float’
``````

``````class Num a ...
``````

``````(+) :: a -> a -> a
(-) :: a -> a -> a
(*) :: a -> a -> a
``````

``````instance Num Word -- Defined in ‘GHC.Num’
instance Num Integer -- Defined in ‘GHC.Num’
instance Num Int -- Defined in ‘GHC.Num’
instance Num Float -- Defined in ‘GHC.Float’
instance Num Double -- Defined in ‘GHC.Float’
``````