• Couldn't match expected type ‘Integer’ with actual type ‘Char’
• In the third argument of ‘mytriple’, namely ‘'b'’
In the expression: mytriple (1 :: Integer) 'a' 'b'
In an equation for ‘it’: it = mytriple (1 :: Integer) 'a' 'b'
This is probably what you expected. In this case, you’ve explicitly stated that 1 is a value of type Integer.
But look at the type here:
> :t 3
3 :: Num p => p
This might be a bit confusing. Haskell supports many different types of number (Int, Integer, Double, Complex, etc). Although they are different types, they are all instances of the same class (Num). When you type 1 (or some other numeric
literal) into GHCi, it doesn’t (yet) know which of these types you want, but does know it has to be a type that’s an instance of Num, which is what the type signature gives. Hence you can do:
> 3 `mod` 2
(which treats the 3 as some integral type), or
> 3 / 2
(which treats the 3 as a fractional – i.e. non-integral- type)
Now it knows more about the type of 3 needed, it will ensure it is of a more specific type.
If typeclasses are new to you, you might want to read something like
Now look at:
> :t (+)
(+) :: Num a => a -> a -> a
This says (+) can add two values of the same type, as long as that type is an instance of class Num. Hence:
> 3 + 'a'
• No instance for (Num Char) arising from a use of ‘+’
• In the expression: 3 + 'a'
In an equation for ‘it’: it = 3 + 'a'
This now makes sense: ‘a’ is of type Char, and there is no instance declaration
instance Num Char (since Char’s are not numbers).
Your example is much the same. In order for
mytriple 1 'a' 'b' to typecheck: ‘b’ must be a Char, and 1 must also be a Char, but 1 has to be a type that’s an instance of Num, so would require an
instance Num Char to exist.