Safe Haskell | None |
---|
This module exports:
- The
MonadWriter
type class and its operationswriter
,tell
,listen
andpass
. - Instances of
MonadWriter
for the relevant monad transformers from thetransformers
package (lazyWriterT
, strictWriterT
, lazyRWST
and strictRWST
). - A universal pass-through instance of
MonadWriter
for any existingMonadWriter
wrapped by aMonadLayer
. - The utility operations
listens
andcensor
.
- class (Monad m, Monoid w) => MonadWriter w m | m -> w where
- listens :: MonadWriter w m => (w -> b) -> m a -> m (a, b)
- censor :: MonadWriter w m => (w -> w) -> m a -> m a
Documentation
class (Monad m, Monoid w) => MonadWriter w m | m -> w whereSource
It is often desirable for a computation to generate output "on the side". Logging and tracing are the most common examples in which data is generated during a computation that we want to retain but is not the primary result of the computation.
Explicitly managing the logging or tracing data can clutter up the code and
invite subtle bugs such as missed log entries. The MonadWriter
interface
provides a cleaner way to manage the output without cluttering the main
computation.
Minimal complete definition: listen
, pass
and one of either writer
or
tell
.
embeds a simple writer action.
writer
(a,w)
is an action that produces the output tell
ww
.
listen :: m a -> m (a, w)Source
is an action that executes the action listen
mm
and adds its
output to the value of the computation.
pass :: m (a, w -> w) -> m aSource
is an action that executes the action pass
mm
, which returns a
value and a function, and returns the value, applying the function to
the output.
(MonadLayer m, MonadWriter w (Inner m)) => MonadWriter w m | |
(MonadWriter w f, MonadWriter w g) => MonadWriter w (Product f g) | |
(Monad m, Monoid w) => MonadWriter w (WriterT w m) | |
(Monad m, Monoid w) => MonadWriter w (WriterT w m) | |
(Monad m, Monoid w) => MonadWriter w (RWST r w s m) | |
(Monad m, Monoid w) => MonadWriter w (RWST r w s m) |
listens :: MonadWriter w m => (w -> b) -> m a -> m (a, b)Source
is an action that executes the action listens
f mm
and adds the
result of applying f
to the output to the value of the computation.
listens f m = liftM (\(~(a, w)) -> (a, f w)) (listen m)
censor :: MonadWriter w m => (w -> w) -> m a -> m aSource
is an action that executes the action censor
f mm
and
applies the function f
to its output, leaving the return value
unchanged.
censor f m = pass (liftM (\a -> (a,f)) m)