Haskell,(应用 M)没有实例

Haskell, No instance for (Applicative M)

看看我的代码。 ghci 告诉

(应用 M)没有实例 源自实例声明的超类 在“Monad M”的实例声明中

我不明白这个错误,也不知道如何解决。你能帮帮我吗?

newtype M a = StOut (Stack -> (a, Stack, String))

unStOut (StOut f) = f
--unStout is used to extract the emeded function from monadic capsule

instance Monad M where
    return x = StOut (\n -> (x, n, ""))
    e >>= f = StOut (\n ->  let     (a, n1, s1) = (unStOut e) n
                                    (b, n2, s2) = (unStOut (f a)) n1
                            in      (b, n2, s1++s2))

签出 Monad. Long story short - in order to become Monad, M must be Applicative. Which in turns require M to be Functor 的定义。

class Applicative m => Monad m where
class Functor f => Applicative f where
class Functor f where

这就是错误所说的。

错误如其所言:您忽略了 M 一个应用实例。

每个单子都是一个应用函子。由于历史的偶然,过去不需要明确说明这一事实,但这导致编写泛型代码时的各种不便。 The omission has since been fixed, so now to define a Monad instance you must first also define Functor and Applicative 个实例。