简体   繁体   English

在Data.Data中,为什么gmapM采用Monad而不是应用程序?

[英]In Data.Data, why does gmapM take a Monad rather than an applicative?

gmapM seems like the obvious candidate for writing a generic traversal based on Data.Data , for instance, if one wants to implement MonoTraversable . 例如,如果要实现MonoTraversablegmapM似乎是基于Data.Data编写泛型遍历的明显候选MonoTraversable The only hiccup is that it takes a Monad rather than an Applicative . 唯一的麻烦是它需要Monad而不是Applicative Why is that? 这是为什么? Also, is there a similar function to fmapM with weaker assumptions? 另外,在假设较弱的情况下,是否有与fmapM类似的功能?

It looks like a historical detail from before implementing the Monad-Applicative proposal. 它看起来像是实施Monad-Applicative建议之前的历史细节。

I can define both the gmapM default and the instance for lists in terms of Applicative . 我可以同时定义gmapM默认和方面的实例列表Applicative

gmapM :: forall m a. (Data a, Applicative m) => (forall d. Data d => d -> m d) -> a -> m a
gmapM f = gfoldl k pure
  where
    k :: Data d => m (d -> b) -> d -> m b
    k c x = c <*> f x

-- instance Data a => Data [a] where ...
gmapMlist :: forall m a. (Data a, Applicative m) => (forall d. Data d => d -> m d) -> [a] -> m [a]
gmapMlist _   []     = pure []
gmapMlist f   (x:xs) = (:) <$> f x <*> f xs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM