我遇到了Haskell代码的问题。这是简化的代码:
class MyClass a where myConst :: a myFunc :: a -> a instance MyClass Int where myConst = 3 myFunc = \x -> 3 data MyType a = Ctor a doit :: MyClass a => MyType a -> MyType a doit x = myFunc x -- doit x = myConst
错误消息:
Could not deduce (MyClass (MyType a)) arising from a use of `myFunc' from the context (MyClass a) bound by the type signature for doit :: MyClass a => MyType a -> MyType a at ...\Example.hs:12:9-37 In the expression: myFunc x In an equation for doit: doit x = myFunc x
我对Haskell很新,但imho doit知道该参数被约束为MyClass,因此提供了myFunc / myConst。
答案 0 :(得分:3)
myFunc
将MyClass
值作为参数,但在doit
中,您传递的值为MyType a
,但不知道它是一个实例MyClass
。您是否打算在myFunc
内的a
值上致电x
?