Looking for a dual version of ReaderT that abstracts the environment instead of a monadic result

Is the following abstraction with instances of Contravariant , Divisible , etc. already implemented in some package? I mean something in the spirit of the following compiled code:

 newtype ReaderDual amb = ReaderDual (b -> ma) instance Contravariant (ReaderDual am) where contramap ba (ReaderDual a') = ReaderDual $ a' . ba instance (Applicative m, Monoid a) => Divisible (ReaderDual am) where divide aToBC (ReaderDual b') (ReaderDual c') = ReaderDual $ \a -> aToBC a & \(b, c) -> (<>) <$> b' b <*> c' c conquer = ReaderDual $ \_ -> pure mempty run :: ReaderDual amb -> b -> ma run (ReaderDual a') b = a' b 
+8
haskell
source share
1 answer

You can create this type as an Op composition from contravariant and Ap from reducers . Op ab is just b -> a and has a Divisible instance for any Monoid a .

To get the behavior of your instance, we can use Ap ma , which provides Monoid mappend = liftA2 (<>) and mempty = pure mempty for any Applicative m and Monoid a .

 type ReaderDual amb = Op (Ap ma) b 
+7
source share

All Articles