如何使用带镜头的monadic功能进​​行修改?

Pet*_*lák 12 monads haskell haskell-lens

我需要一个像镜头一样的镜头功能over,但是有了monadic操作:

overM :: (Monad m) => Lens s t a b -> (a -> m b) -> (s -> m t)
Run Code Online (Sandbox Code Playgroud)

虽然这个函数很容易定义(它实际上只是一个身份模数WrappedMonad),我想知道这些函数是在镜头中定义的吗?

{-# LANGUAGE RankNTypes #-}
import Control.Applicative
import Control.Lens

overF :: (Functor f) => Lens s t a b -> (a -> f b) -> (s -> f t)
overF l = l

overM :: (Monad m) => Lens s t a b -> (a -> m b) -> (s -> m t)
overM l = (unwrapMonad .) . l . (WrapMonad .)
Run Code Online (Sandbox Code Playgroud)

Cha*_*ham 7

在Control.Lens.Traversal中:

traverseOf :: Over p f s t a b -> p a (f b) -> s -> f t
traverseOf = id

mapMOf :: Profunctor p =>
     Over p (WrappedMonad m) s t a b -> p a (m b) -> s -> m t
mapMOf l cmd = unwrapMonad #. l (WrapMonad #. cmd)
Run Code Online (Sandbox Code Playgroud)

例:

Prelude Control.Lens> traverseOf _1 (Just . (+2)) (2,2)
Just (4,2)

Prelude Control.Lens> mapMOf _1 (Just . (+2)) (2,2)
Just (4,2)
Run Code Online (Sandbox Code Playgroud)