Lysxia / profunctor-monad

Bidirectional programming in Haskell with monadic profunctors
MIT License
44 stars 4 forks source link

Monadic `lmapM` #3

Open BebeSparkelSparkel opened 1 month ago

BebeSparkelSparkel commented 1 month ago

I found in biparsing that a monadic lmap was very useful.

Is there an lmapM :: (a -> m b) -> t m b c -> t m a c?

BebeSparkelSparkel commented 1 month ago

It seems like Cofunctor may work here.

Lysxia commented 1 month ago

Indeed that's why I added Cofunctor, though I wonder if I really need the generality as opposed to just a mapping from a -> Maybe b

BebeSparkelSparkel commented 1 month ago

Actually, I have not been able to get Cofunctor to work correctly with this. It seems like I need

class Cotrans p m | p -> m where colift :: (a -> m b) -> p b c -> p a c

newtype Fwd m u v = Fwd {unFwd :: m v}
instance Cotrans (Fwd m) m where
  colift = const coerce

newtype Bwd m u v = Bwd {unBwd :: u -> m v}
instance Monad m => Cotrans (Bwd m) m where
  colift f = Bwd . (>=>) f . unBwd

I'm not sure if this exists somewhere. Have you seen this before?