layers-0.1: Modular type class machinery for monad transformer stacks.

Safe HaskellNone

Control.Monad.Interface.Writer

Description

This module exports:

  1. The MonadWriter type class and its operations writer, tell, listen and pass.
  2. Instances of MonadWriter for the relevant monad transformers from the transformers package (lazy WriterT, strict WriterT, lazy RWST and strict RWST).
  3. A universal pass-through instance of MonadWriter for any existing MonadWriter wrapped by a MonadLayer.
  4. The utility operations listens and censor.

Synopsis

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.

Methods

writer :: (a, w) -> m aSource

writer (a,w) embeds a simple writer action.

tell :: w -> m ()Source

tell w is an action that produces the output w.

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

listen m is an action that executes the action m and adds its output to the value of the computation.

pass :: m (a, w -> w) -> m aSource

pass m is an action that executes the action m, which returns a value and a function, and returns the value, applying the function to the output.

Instances

(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

listens f m is an action that executes the action 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

censor f m is an action that executes the action m and applies the function f to its output, leaving the return value unchanged.

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