我有以下功能:
f :: (Int -> Int) -> Int
f = undefined
Run Code Online (Sandbox Code Playgroud)
现在,我想打电话f与5(这是不正确的):
f 5
Run Code Online (Sandbox Code Playgroud)
显然,这不应该编译,因为5不是从功能Int到Int.所以我希望有一个错误信息Couldn't match expected type Int -> Int with Int.
但相反,我得到:
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
Run Code Online (Sandbox Code Playgroud)
为什么Num会出现在这里?
chi*_*chi 12
5是类型类中的任何类型Num.这些类型包括Int,Double,Integer,等.
Num 默认情况下,函数不在类型类中.然而,Num用户可以添加功能的实例,例如以逐点方式定义两个功能的总和.在这种情况下,文字5可以代表常数五功能.
从技术上讲,字面意思是fromInteger 5,它5是一个Integer常数.f 5因此,实际上是调用f (fromInteger 5),它试图将五个转换为Int -> Int.这需要一个实例Num (Int -> Int).
因此,GHC不会在其错误中声明5不能成为一个函数(因为它可能是,如果用户声明它,提供一个合适的fromInteger).它只是正确地指出,没有Num找到整数函数的实例.