Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No instance for (Num (Int -> Int)) arising from the literal `5'

Tags:

haskell

I have the following function:

f :: (Int -> Int) -> Int
f = undefined

Now I want to call f with 5 (which is incorrect):

f 5

Obviously, this should not compile, because 5 is not a function from Int to Int. So I would expect an error message like Couldn't match expected type Int -> Int with Int.

But instead I get:

No instance for (Num (Int -> Int)) arising from the literal `5'
In the first argument of `f', namely `5'
In the expression: f 5
In an equation for `it': it = f 5

Why did Num appear here?

like image 665
ZhekaKozlov Avatar asked Dec 14 '22 17:12

ZhekaKozlov


1 Answers

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.

like image 100
chi Avatar answered Jan 04 '23 17:01

chi