Prelude> -- I have 2 functions: f and g
Prelude> f x y = x + y
Prelude> g x = 2*x
Prelude> f 2 3
5要用x=2和y=3表示x=2和y=3,可以很好地完成以下工作:
Prelude> f 2 (g 3)
8为什么会出现以下返回错误?
Prelude>
Prelude> f 2 g 3
<interactive>:19:1: error:
• Non type-variable argument in the constraint: Num (a -> a)
(Use FlexibleContexts to permit this)
• When checking the inferred type
it :: forall a. (Num a, Num (a -> a)) => a
Prelude> 发布于 2021-05-22 16:25:05
f 2 g 3是(因为函数应用程序left-associative):)
f 2 g 3 = ((f 2) g) 3这就是为什么您会得到这个错误&它期望g有一个Num (因为它是f x y = x+y和+ :: Num a -> a -> a -> a中的参数y )。
2作为一个文本在每个Num a中都可以是一个值,但是GHC不知道Num的一个实例,它是一个函数a -> a。
现在,错误本身讨论了上下文-- basic不可能有Num ((->) a a)形式的约束--但是您可以很容易(并且安全地)使用给定的扩展来规避这个问题。然后,您应该得到类型类的错误。
https://stackoverflow.com/questions/67651692
复制相似问题