Safe Haskell | None |
---|

This module exports:

- The
`MonadWriter`

type class and its operations`writer`

,`tell`

,`listen`

and`pass`

. - Instances of
`MonadWriter`

for the relevant monad transformers from the`transformers`

package (lazy`WriterT`

, strict`WriterT`

, lazy`RWST`

and strict`RWST`

). - A universal pass-through instance of
`MonadWriter`

for any existing`MonadWriter`

wrapped by a`MonadLayer`

. - The utility operations
`listens`

and`censor`

.

- 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`

w`w`

.

listen :: m a -> m (a, w)Source

is an action that executes the action `listen`

m`m`

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`

m`m`

, 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 m`m`

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 m`m`

and
applies the function `f`

to its output, leaving the return value
unchanged.

censor f m = pass (liftM (\a -> (a,f)) m)