module Control.Monad.Writer.Class (
MonadWriter(..),
listens,
censor,
) where
import Control.Monad.Trans.Error as Error
import Control.Monad.Trans.Identity as Identity
import Control.Monad.Trans.Maybe as Maybe
import Control.Monad.Trans.Reader
import qualified Control.Monad.Trans.RWS.Lazy as LazyRWS (
RWST, tell, listen, pass)
import qualified Control.Monad.Trans.RWS.Strict as StrictRWS (
RWST, tell, listen, pass)
import Control.Monad.Trans.State.Lazy as Lazy
import Control.Monad.Trans.State.Strict as Strict
import qualified Control.Monad.Trans.Writer.Lazy as Lazy (
WriterT, tell, listen, pass)
import qualified Control.Monad.Trans.Writer.Strict as Strict (
WriterT, tell, listen, pass)
import Control.Monad.Trans.Class (lift)
import Control.Monad
import Data.Monoid
class (Monoid w, Monad m) => MonadWriter w m | m -> w where
tell :: w -> m ()
listen :: m a -> m (a, w)
pass :: m (a, w -> w) -> m a
listens :: (MonadWriter w m) => (w -> b) -> m a -> m (a, b)
listens f m = do
~(a, w) <- listen m
return (a, f w)
censor :: (MonadWriter w m) => (w -> w) -> m a -> m a
censor f m = pass $ do
a <- m
return (a, f)
instance (Monoid w, Monad m) => MonadWriter w (Lazy.WriterT w m) where
tell = Lazy.tell
listen = Lazy.listen
pass = Lazy.pass
instance (Monoid w, Monad m) => MonadWriter w (Strict.WriterT w m) where
tell = Strict.tell
listen = Strict.listen
pass = Strict.pass
instance (Monoid w, Monad m) => MonadWriter w (LazyRWS.RWST r w s m) where
tell = LazyRWS.tell
listen = LazyRWS.listen
pass = LazyRWS.pass
instance (Monoid w, Monad m) => MonadWriter w (StrictRWS.RWST r w s m) where
tell = StrictRWS.tell
listen = StrictRWS.listen
pass = StrictRWS.pass
instance (Error e, MonadWriter w m) => MonadWriter w (ErrorT e m) where
tell = lift . tell
listen = Error.liftListen listen
pass = Error.liftPass pass
instance (MonadWriter w m) => MonadWriter w (IdentityT m) where
tell = lift . tell
listen = Identity.mapIdentityT listen
pass = Identity.mapIdentityT pass
instance (MonadWriter w m) => MonadWriter w (MaybeT m) where
tell = lift . tell
listen = Maybe.liftListen listen
pass = Maybe.liftPass pass
instance (MonadWriter w m) => MonadWriter w (ReaderT r m) where
tell = lift . tell
listen = mapReaderT listen
pass = mapReaderT pass
instance (MonadWriter w m) => MonadWriter w (Lazy.StateT s m) where
tell = lift . tell
listen = Lazy.liftListen listen
pass = Lazy.liftPass pass
instance (MonadWriter w m) => MonadWriter w (Strict.StateT s m) where
tell = lift . tell
listen = Strict.liftListen listen
pass = Strict.liftPass pass