简单树monad实现错误

时间:2017-12-06 15:25:45

标签: haskell monads applicative fam-proposal

考虑下一段代码 -

module Main where

data MyTree a = MyLeaf a
              | MyNode (MyTree a) (MyTree a)
              deriving (Show)

instance Monad MyTree where
   return = MyLeaf
   (MyLeaf x) >>= f = f x
   (MyNode x y) >>= f = MyNode (x >>= f) (y >>= f)

main :: IO ()
main  =
   do

      let tree1 = MyNode (MyLeaf 3) (MyNode (MyLeaf 4) (MyLeaf 5))
      let tree2 = MyNode (MyLeaf "ABC") (MyNode (MyLeaf "DEFG") (MyLeaf "HIJKL"))

      print (tree1 >>= (\x -> MyLeaf (x+200)))
      print (tree2 >>= (\x -> MyLeaf (tail x)))

我尝试简单地实现Tree Monad,但问题是当我编译上面的代码时,我得到以下错误:

* No instance for (Applicative MyTree)
    arising from the superclasses of an instance declaration
* In the instance declaration for `Monad MyTree'

我不明白缺少什么。有Applicative的重点是什么?

0 个答案:

没有答案