哈斯克尔:"无法演绎"

时间:2015-12-10 20:02:22

标签: haskell

我遇到了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。

1 个答案:

答案 0 :(得分:3)

myFuncMyClass值作为参数,但在doit中,您传递的值为MyType a,但不知道它是一个实例MyClass。您是否打算在myFunc内的a值上致电x