{-# LANGUAGE Safe #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FunctionalDependencies #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE UndecidableInstances #-}
module Control.Monad.Writer.Class (
MonadWriter(..),
listens,
censor,
) where
import Control.Monad.Trans.Except (ExceptT)
import qualified Control.Monad.Trans.Except as Except
import Control.Monad.Trans.Identity (IdentityT)
import qualified Control.Monad.Trans.Identity as Identity
import Control.Monad.Trans.Maybe (MaybeT)
import qualified Control.Monad.Trans.Maybe as Maybe
import Control.Monad.Trans.Reader (ReaderT, mapReaderT)
import qualified Control.Monad.Trans.RWS.Lazy as LazyRWS
import qualified Control.Monad.Trans.RWS.Strict as StrictRWS
import qualified Control.Monad.Trans.State.Lazy as Lazy
import qualified Control.Monad.Trans.State.Strict as Strict
import qualified Control.Monad.Trans.Writer.Lazy as Lazy
import qualified Control.Monad.Trans.Writer.Strict as Strict
import Control.Monad.Trans.Accum (AccumT)
import qualified Control.Monad.Trans.Accum as Accum
import qualified Control.Monad.Trans.RWS.CPS as CPSRWS
import qualified Control.Monad.Trans.Writer.CPS as CPS
import Control.Monad.Trans.Class (lift)
class (Monoid w, Monad m) => MonadWriter w m | m -> w where
{-# MINIMAL (writer | tell), listen, pass #-}
writer :: (a,w) -> m a
writer ~(a
a, w
w) = do
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell w
w
forall (m :: * -> *) a. Monad m => a -> m a
return a
a
tell :: w -> m ()
tell w
w = forall w (m :: * -> *) a. MonadWriter w m => (a, w) -> m a
writer ((),w
w)
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 :: forall w (m :: * -> *) b a.
MonadWriter w m =>
(w -> b) -> m a -> m (a, b)
listens w -> b
f m a
m = do
~(a
a, w
w) <- forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen m a
m
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, w -> b
f w
w)
censor :: MonadWriter w m => (w -> w) -> m a -> m a
censor :: forall w (m :: * -> *) a. MonadWriter w m => (w -> w) -> m a -> m a
censor w -> w
f m a
m = forall w (m :: * -> *) a. MonadWriter w m => m (a, w -> w) -> m a
pass forall a b. (a -> b) -> a -> b
$ do
a
a <- m a
m
forall (m :: * -> *) a. Monad m => a -> m a
return (a
a, w -> w
f)
instance (Monoid w) => MonadWriter w ((,) w) where
writer :: forall a. (a, w) -> (w, a)
writer ~(a
a, w
w) = (w
w, a
a)
tell :: w -> (w, ())
tell w
w = (w
w, ())
listen :: forall a. (w, a) -> (w, (a, w))
listen ~(w
w, a
a) = (w
w, (a
a, w
w))
pass :: forall a. (w, (a, w -> w)) -> (w, a)
pass ~(w
w, (a
a, w -> w
f)) = (w -> w
f w
w, a
a)
instance (Monoid w, Monad m) => MonadWriter w (CPS.WriterT w m) where
writer :: forall a. (a, w) -> WriterT w m a
writer = forall w (m :: * -> *) a.
(Monoid w, Monad m) =>
(a, w) -> WriterT w m a
CPS.writer
tell :: w -> WriterT w m ()
tell = forall w (m :: * -> *). (Monoid w, Monad m) => w -> WriterT w m ()
CPS.tell
listen :: forall a. WriterT w m a -> WriterT w m (a, w)
listen = forall w (m :: * -> *) a.
(Monoid w, Monad m) =>
WriterT w m a -> WriterT w m (a, w)
CPS.listen
pass :: forall a. WriterT w m (a, w -> w) -> WriterT w m a
pass = forall w w' (m :: * -> *) a.
(Monoid w, Monoid w', Monad m) =>
WriterT w m (a, w -> w') -> WriterT w' m a
CPS.pass
instance (Monoid w, Monad m) => MonadWriter w (Lazy.WriterT w m) where
writer :: forall a. (a, w) -> WriterT w m a
writer = forall (m :: * -> *) a w. Monad m => (a, w) -> WriterT w m a
Lazy.writer
tell :: w -> WriterT w m ()
tell = forall (m :: * -> *) w. Monad m => w -> WriterT w m ()
Lazy.tell
listen :: forall a. WriterT w m a -> WriterT w m (a, w)
listen = forall (m :: * -> *) w a.
Monad m =>
WriterT w m a -> WriterT w m (a, w)
Lazy.listen
pass :: forall a. WriterT w m (a, w -> w) -> WriterT w m a
pass = forall (m :: * -> *) w a.
Monad m =>
WriterT w m (a, w -> w) -> WriterT w m a
Lazy.pass
instance (Monoid w, Monad m) => MonadWriter w (Strict.WriterT w m) where
writer :: forall a. (a, w) -> WriterT w m a
writer = forall (m :: * -> *) a w. Monad m => (a, w) -> WriterT w m a
Strict.writer
tell :: w -> WriterT w m ()
tell = forall (m :: * -> *) w. Monad m => w -> WriterT w m ()
Strict.tell
listen :: forall a. WriterT w m a -> WriterT w m (a, w)
listen = forall (m :: * -> *) w a.
Monad m =>
WriterT w m a -> WriterT w m (a, w)
Strict.listen
pass :: forall a. WriterT w m (a, w -> w) -> WriterT w m a
pass = forall (m :: * -> *) w a.
Monad m =>
WriterT w m (a, w -> w) -> WriterT w m a
Strict.pass
instance (Monoid w, Monad m) => MonadWriter w (CPSRWS.RWST r w s m) where
writer :: forall a. (a, w) -> RWST r w s m a
writer = forall w (m :: * -> *) a r s.
(Monoid w, Monad m) =>
(a, w) -> RWST r w s m a
CPSRWS.writer
tell :: w -> RWST r w s m ()
tell = forall w (m :: * -> *) r s.
(Monoid w, Monad m) =>
w -> RWST r w s m ()
CPSRWS.tell
listen :: forall a. RWST r w s m a -> RWST r w s m (a, w)
listen = forall w (m :: * -> *) r s a.
(Monoid w, Monad m) =>
RWST r w s m a -> RWST r w s m (a, w)
CPSRWS.listen
pass :: forall a. RWST r w s m (a, w -> w) -> RWST r w s m a
pass = forall w w' (m :: * -> *) r s a.
(Monoid w, Monoid w', Monad m) =>
RWST r w s m (a, w -> w') -> RWST r w' s m a
CPSRWS.pass
instance (Monoid w, Monad m) => MonadWriter w (LazyRWS.RWST r w s m) where
writer :: forall a. (a, w) -> RWST r w s m a
writer = forall (m :: * -> *) a w r s. Monad m => (a, w) -> RWST r w s m a
LazyRWS.writer
tell :: w -> RWST r w s m ()
tell = forall (m :: * -> *) w r s. Monad m => w -> RWST r w s m ()
LazyRWS.tell
listen :: forall a. RWST r w s m a -> RWST r w s m (a, w)
listen = forall (m :: * -> *) r w s a.
Monad m =>
RWST r w s m a -> RWST r w s m (a, w)
LazyRWS.listen
pass :: forall a. RWST r w s m (a, w -> w) -> RWST r w s m a
pass = forall (m :: * -> *) r w s a.
Monad m =>
RWST r w s m (a, w -> w) -> RWST r w s m a
LazyRWS.pass
instance (Monoid w, Monad m) => MonadWriter w (StrictRWS.RWST r w s m) where
writer :: forall a. (a, w) -> RWST r w s m a
writer = forall (m :: * -> *) a w r s. Monad m => (a, w) -> RWST r w s m a
StrictRWS.writer
tell :: w -> RWST r w s m ()
tell = forall (m :: * -> *) w r s. Monad m => w -> RWST r w s m ()
StrictRWS.tell
listen :: forall a. RWST r w s m a -> RWST r w s m (a, w)
listen = forall (m :: * -> *) r w s a.
Monad m =>
RWST r w s m a -> RWST r w s m (a, w)
StrictRWS.listen
pass :: forall a. RWST r w s m (a, w -> w) -> RWST r w s m a
pass = forall (m :: * -> *) r w s a.
Monad m =>
RWST r w s m (a, w -> w) -> RWST r w s m a
StrictRWS.pass
instance MonadWriter w m => MonadWriter w (ExceptT e m) where
writer :: forall a. (a, w) -> ExceptT e m a
writer = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *) a. MonadWriter w m => (a, w) -> m a
writer
tell :: w -> ExceptT e m ()
tell = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell
listen :: forall a. ExceptT e m a -> ExceptT e m (a, w)
listen = forall (m :: * -> *) w e a.
Monad m =>
Listen w m (Either e a) -> Listen w (ExceptT e m) a
Except.liftListen forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen
pass :: forall a. ExceptT e m (a, w -> w) -> ExceptT e m a
pass = forall (m :: * -> *) w e a.
Monad m =>
Pass w m (Either e a) -> Pass w (ExceptT e m) a
Except.liftPass forall w (m :: * -> *) a. MonadWriter w m => m (a, w -> w) -> m a
pass
instance MonadWriter w m => MonadWriter w (IdentityT m) where
writer :: forall a. (a, w) -> IdentityT m a
writer = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *) a. MonadWriter w m => (a, w) -> m a
writer
tell :: w -> IdentityT m ()
tell = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell
listen :: forall a. IdentityT m a -> IdentityT m (a, w)
listen = forall {k1} {k2} (m :: k1 -> *) (a :: k1) (n :: k2 -> *) (b :: k2).
(m a -> n b) -> IdentityT m a -> IdentityT n b
Identity.mapIdentityT forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen
pass :: forall a. IdentityT m (a, w -> w) -> IdentityT m a
pass = forall {k1} {k2} (m :: k1 -> *) (a :: k1) (n :: k2 -> *) (b :: k2).
(m a -> n b) -> IdentityT m a -> IdentityT n b
Identity.mapIdentityT forall w (m :: * -> *) a. MonadWriter w m => m (a, w -> w) -> m a
pass
instance MonadWriter w m => MonadWriter w (MaybeT m) where
writer :: forall a. (a, w) -> MaybeT m a
writer = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *) a. MonadWriter w m => (a, w) -> m a
writer
tell :: w -> MaybeT m ()
tell = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell
listen :: forall a. MaybeT m a -> MaybeT m (a, w)
listen = forall (m :: * -> *) w a.
Monad m =>
Listen w m (Maybe a) -> Listen w (MaybeT m) a
Maybe.liftListen forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen
pass :: forall a. MaybeT m (a, w -> w) -> MaybeT m a
pass = forall (m :: * -> *) w a.
Monad m =>
Pass w m (Maybe a) -> Pass w (MaybeT m) a
Maybe.liftPass forall w (m :: * -> *) a. MonadWriter w m => m (a, w -> w) -> m a
pass
instance MonadWriter w m => MonadWriter w (ReaderT r m) where
writer :: forall a. (a, w) -> ReaderT r m a
writer = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *) a. MonadWriter w m => (a, w) -> m a
writer
tell :: w -> ReaderT r m ()
tell = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell
listen :: forall a. ReaderT r m a -> ReaderT r m (a, w)
listen = forall (m :: * -> *) a (n :: * -> *) b r.
(m a -> n b) -> ReaderT r m a -> ReaderT r n b
mapReaderT forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen
pass :: forall a. ReaderT r m (a, w -> w) -> ReaderT r m a
pass = forall (m :: * -> *) a (n :: * -> *) b r.
(m a -> n b) -> ReaderT r m a -> ReaderT r n b
mapReaderT forall w (m :: * -> *) a. MonadWriter w m => m (a, w -> w) -> m a
pass
instance MonadWriter w m => MonadWriter w (Lazy.StateT s m) where
writer :: forall a. (a, w) -> StateT s m a
writer = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *) a. MonadWriter w m => (a, w) -> m a
writer
tell :: w -> StateT s m ()
tell = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell
listen :: forall a. StateT s m a -> StateT s m (a, w)
listen = forall (m :: * -> *) w a s.
Monad m =>
Listen w m (a, s) -> Listen w (StateT s m) a
Lazy.liftListen forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen
pass :: forall a. StateT s m (a, w -> w) -> StateT s m a
pass = forall (m :: * -> *) w a s.
Monad m =>
Pass w m (a, s) -> Pass w (StateT s m) a
Lazy.liftPass forall w (m :: * -> *) a. MonadWriter w m => m (a, w -> w) -> m a
pass
instance MonadWriter w m => MonadWriter w (Strict.StateT s m) where
writer :: forall a. (a, w) -> StateT s m a
writer = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *) a. MonadWriter w m => (a, w) -> m a
writer
tell :: w -> StateT s m ()
tell = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell
listen :: forall a. StateT s m a -> StateT s m (a, w)
listen = forall (m :: * -> *) w a s.
Monad m =>
Listen w m (a, s) -> Listen w (StateT s m) a
Strict.liftListen forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen
pass :: forall a. StateT s m (a, w -> w) -> StateT s m a
pass = forall (m :: * -> *) w a s.
Monad m =>
Pass w m (a, s) -> Pass w (StateT s m) a
Strict.liftPass forall w (m :: * -> *) a. MonadWriter w m => m (a, w -> w) -> m a
pass
instance
( Monoid w'
, MonadWriter w m
) => MonadWriter w (AccumT w' m) where
writer :: forall a. (a, w) -> AccumT w' m a
writer = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *) a. MonadWriter w m => (a, w) -> m a
writer
tell :: w -> AccumT w' m ()
tell = forall (t :: (* -> *) -> * -> *) (m :: * -> *) a.
(MonadTrans t, Monad m) =>
m a -> t m a
lift forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell
listen :: forall a. AccumT w' m a -> AccumT w' m (a, w)
listen = forall (m :: * -> *) w a s.
Monad m =>
Listen w m (a, s) -> Listen w (AccumT s m) a
Accum.liftListen forall w (m :: * -> *) a. MonadWriter w m => m a -> m (a, w)
listen
pass :: forall a. AccumT w' m (a, w -> w) -> AccumT w' m a
pass = forall (m :: * -> *) w a s.
Monad m =>
Pass w m (a, s) -> Pass w (AccumT s m) a
Accum.liftPass forall w (m :: * -> *) a. MonadWriter w m => m (a, w -> w) -> m a
pass