将折叠转换为monadic折叠haskell

时间:2018-03-11 01:45:13

标签: haskell monads fold

我在将自定义数据类型的折叠转换为monadic折叠时遇到了一些困难。我的想法是,我应该可以使用IOMaybe>>=折叠。但是,我遇到的错误我并不完全明白。

这是我的代码:

data RTree a = Val a | Question String [RTree a] deriving (Show)

foldRT :: (a -> b) -> (String -> [b] -> b) -> RTree a -> b
foldRT v q (Val x) = v x 
foldRT v q (Question s xs) = q s $ map (foldRT v q) xs



foldRTM :: (Monad m) => (a -> m b) -> (String -> [b] -> m b) -> RTree a -> m b 
foldRTM v q t = foldRT (>>= v) q' (mon t)
   where q' s ls = mapM id ls >>= (\xs -> q s xs) 
         mon (Val x) = Val $ return x
         mon (Question s xs) = Question s (map return xs)

以下是错误消息:

main.hs:14:36: error:
    • Couldn't match type ‘m’ with ‘RTree’
      ‘m’ is a rigid type variable bound by
        the type signature for:
          foldRTM :: forall (m :: * -> *) a b.
                     Monad m =>
                     (a -> m b) -> (String -> [b] -> m b) -> RTree a -> m b
        at main.hs:13:1-78
      Expected type: RTree (m a)
        Actual type: RTree (RTree a)
    • In the third argument of ‘foldRT’, namely ‘(mon t)’
      In the expression: foldRT (>>= v) q' (mon t)
      In an equation for ‘foldRTM’:
          foldRTM v q t
            = foldRT (>>= v) q' (mon t)
            where
                q' s ls = mapM id ls >>= (\ xs -> q s xs)
                mon (Val x) = Val $ return x
                mon (Question s xs) = Question s (map return xs)
    • Relevant bindings include
        q' :: String -> [m b] -> m b (bound at main.hs:15:10)
        q :: String -> [b] -> m b (bound at main.hs:14:11)
        v :: a -> m b (bound at main.hs:14:9)
        foldRTM :: (a -> m b) -> (String -> [b] -> m b) -> RTree a -> m b
          (bound at main.hs:14:1)
   |
14 | foldRTM v q t = foldRT (>>= v) q' (mon t)
   |                                    ^^^^^

我的想法是>>= vq'作为foldRT的参数构成其类型......

(Monad m) :: (m a -> m b) -> (String -> [m b] -> m b) -> RTree m a -> m b

...但是将树的初始输入转换为此形式的映射似乎不起作用。我认为这是我的一个愚蠢的误解。

谢谢!

1 个答案:

答案 0 :(得分:3)

问题出在这里:

     mon (Question s xs) = Question s (map return xs)

return的类型为RTree a -> RTree (RTree a)

您可能打算:

     mon (Question s xs) = Question s (map (fmap return) xs)

此外,foldRTM可以简化为:

foldRTM :: (Monad m) => (a -> m b) -> (String -> [b] -> m b) -> RTree a -> m b 
foldRTM v q t = foldRT (>>= v) q' (return <$> t)
   where q' s ls = sequence ls >>= q s

如果您制作了Functor RTree的正确实例:

instance Functor RTree where
    fmap f (Val x) = Val (f x)
    fmap f (Question s xs) = Question s (map (fmap f) xs)