5
is of any type in type class Num
. These types include Int
, Double
, Integer
, etc.
Functions are not in type class Num
by default. Yet, a Num
instance for functions might be added by the user, e.g. defining the sum of two functions in a pointwise fashion. In such case, the literal 5
can stand for the constant-five function.
Techncally, the literal stands for fromInteger 5
, where the 5
is an Integer
constant. The call f 5
is therefore actually f (fromInteger 5)
, which tries to convert five into Int -> Int
. This requires an instance of Num (Int -> Int)
.
Hence, GHC does not state in its error that 5
can not be a function (since it could be, if the user declared it such, providing a suitable fromInteger
). It just states, correctly, that no Num
instance can be found for integer functions.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…