module DeepControl.Monad.Trans.Writer (
module Control.Monad.Writer,
listen2, pass2,
listen3, pass3,
) where
import DeepControl.Applicative
import DeepControl.Commutative
import DeepControl.Monad
import DeepControl.Monad.Signatures
import DeepControl.Monad.Trans
import Control.Monad.Writer
import Control.Monad.Identity
import Data.Monoid
instance (Monoid w) => Commutative (Writer w) where
commute x =
let (a, b) = runWriter x
in (WriterT . Identity) |$> (a <$|(,)|* b)
instance (Monoid w) => Monad2 (Writer w) where
mv >>== f =
mv >>= \x -> runWriterT x >- \(Identity (a, w)) ->
f a <$| (\x -> runWriterT x >- \(Identity (b, w')) ->
WriterT $ Identity (b, w <> w'))
listen2 :: (MonadWriter w m2, Applicative m1) => m1 (m2 a) -> m1 (m2 (a, w))
listen2 m = listen |$> m
pass2 :: (MonadWriter w m2, Applicative m1) => m1 (m2 (a, w -> w)) -> m1 (m2 a)
pass2 m = pass |$> m
instance (Monoid w) => Monad3 (Writer w) where
mv >>>== f =
mv >>== \x -> runWriterT x >- \(Identity (a, w)) ->
f a <<$| (\x -> runWriterT x >- \(Identity (b, w')) ->
WriterT $ Identity (b, w <> w'))
listen3 :: (MonadWriter w m3, Applicative m1, Applicative m2) => m1 (m2 (m3 a)) -> m1 (m2 (m3 (a, w)))
listen3 m = listen2 |$> m
pass3 :: (MonadWriter w m3, Applicative m1, Applicative m2) => m1 (m2 (m3 (a, w -> w))) -> m1 (m2 (m3 a))
pass3 m = pass2 |$> m
instance (Monoid w) => Monad4 (Writer w) where
mv >>>>== f =
mv >>>== \x -> runWriterT x >- \(Identity (a, w)) ->
f a <<<$| (\x -> runWriterT x >- \(Identity (b, w')) ->
WriterT $ Identity (b, w <> w'))
instance (Monoid w) => Monad5 (Writer w) where
mv >>>>>== f =
mv >>>>== \x -> runWriterT x >- \(Identity (a, w)) ->
f a <<<<$| (\x -> runWriterT x >- \(Identity (b, w')) ->
WriterT $ Identity (b, w <> w'))