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

我的梦境 提交于 2019-12-29 01:39:11

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/27671594/no-instance-for-num-int-int-arising-from-the-literal-5

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!