i具有Effect
类型,该类型表示对堆栈的影响。Effect
可以是成功修改堆栈并选择生成输出的成功效果,也可以是一个错误,可以记住堆栈失败时的外观。
data Effect a = Failure a | Effect a [Int]
instance Monad Effect where
return s = Effect s []
(Failure debugS) >>= _ = Failure debugS
(Effect oldS oldO) >>= f =
case f oldS of
(Failure debugS) -> Failure debugS
(Effect newS newO) -> Effect newS (newO ++ oldO)
绑定应串联结果(以相反的顺序,我知道(。但是,目前GHC给我以下错误消息:
example.hs:2:10:
No instance for (Applicative Effect)
arising from the superclasses of an instance declaration
In the instance declaration for ‘Monad Effect’
example.hs:4:38:
Couldn't match expected type ‘b’ with actual type ‘a’
‘a’ is a rigid type variable bound by
the type signature for
(>>=) :: Effect a -> (a -> Effect b) -> Effect b
at example.hs:4:22
‘b’ is a rigid type variable bound by
the type signature for
(>>=) :: Effect a -> (a -> Effect b) -> Effect b
at example.hs:4:22
Relevant bindings include
debugS :: a (bound at example.hs:4:14)
(>>=) :: Effect a -> (a -> Effect b) -> Effect b
(bound at example.hs:4:5)
In the first argument of ‘Failure’, namely ‘debugS’
In the expression: Failure debugS
我是Haskell的新手,没有使用GHC的错误消息。我应该如何纠正此错误?
从GHC 7.10开始,不幸的是,您需要由于函数插入式 - 月球建议而实现Monad
,Applicative
和Functor
。
您获得类型错误的原因是因为>>=
的类型为:
(>>=) :: Monad m => m a -> (a -> m b) -> m b
这意味着您通过传递的功能给出 type m b
。但是,由于您还给Failure debugS
,这是m a
类型的类型不匹配的类型,因为它本质上迫使>>=
符合:
(>>=) :: Monad m => m a -> (a -> m a) -> m a -- Wrong!
您返回的debugS
必须不同。