transformers-0.3.0.0: Concrete functor and monad transformers

Copyright | (c) Andy Gill 2001, (c) Oregon Graduate Institute of Science and Technology, 2001
License | BSD-style (see the file LICENSE)
Maintainer | ross@soi.city.ac.uk
Stability | experimental
Portability | portable
Safe Haskell | Safe-Inferred
Language | Haskell98

Control.Monad.Trans.Writer.Strict

Contents

- The Writer monad
- The WriterT monad transformer
- Writer operations
- Lifting other operations

Description

The strict `WriterT` monad transformer, which adds collection of outputs (such as a count or string output) to a given monad.

This version builds its output strictly; for a lazy version, see Control.Monad.Trans.Writer.Lazy, which has the same interface.

This monad transformer provides only limited access to the output during the computation. For more general access, use Control.Monad.Trans.State instead.

Synopsis

- type Writer w = WriterT w Identity
- writer :: Monad m => (a, w) -> WriterT w m a
- runWriter :: Writer w a -> (a, w)
- execWriter :: Writer w a -> w
- mapWriter :: ((a, w) -> (b, w')) -> Writer w a -> Writer w' b
- newtype WriterT w m a = WriterT {
  - runWriterT :: m (a, w)
  }
- execWriterT :: Monad m => WriterT w m a -> m w
- mapWriterT :: (m (a, w) -> n (b, w')) -> WriterT w m a -> WriterT w' n b
- tell :: (Monoid w, Monad m) => w -> WriterT w m ()
- listen :: (Monoid w, Monad m) => WriterT w m a -> WriterT w m (a, w)
- listens :: (Monoid w, Monad m) => (w -> b) -> WriterT w m a -> WriterT w m (a, b)
- pass :: (Monoid w, Monad m) => WriterT w m (a, w -> w) -> WriterT w m a
- censor :: (Monoid w, Monad m) => (w -> w) -> WriterT w m a -> WriterT w m a
- liftCallCC :: Monoid w => ((((a, w) -> m (b, w)) -> m (a, w)) -> m (a, w)) -> ((a -> WriterT w m b) -> WriterT w m a) -> WriterT w m a
- liftCatch :: (m (a, w) -> (e -> m (a, w)) -> m (a, w)) -> WriterT w m a -> (e -> WriterT w m a) -> WriterT w m a

# The Writer monad

type Writer w = WriterT w Identity

A writer monad parameterized by the type `w` of output to accumulate.

The `return` function produces the output `mempty`, while `>>=` combines the outputs of the subcomputations using `mappend`.

writer :: Monad m => (a, w) -> WriterT w m a

Construct a writer computation from a (result, output) pair. (The inverse of `runWriter`.)

runWriter :: Writer w a -> (a, w)

Unwrap a writer computation as a (result, output) pair. (The inverse of `writer`.)

execWriter :: Writer w a -> w

Extract the output from a writer computation.

-     execWriter m = snd (runWriter m)

mapWriter :: ((a, w) -> (b, w')) -> Writer w a -> Writer w' b

Map both the return value and output of a computation using the given function.

-     runWriter (mapWriter f m) = f (runWriter m)

# The WriterT monad transformer

newtype WriterT w m a

A writer monad parameterized by:

- `w` - the output to accumulate.
- `m` - The inner monad.

The `return` function produces the output `mempty`, while `>>=` combines the outputs of the subcomputations using `mappend`.

Constructors

WriterT |

Fields

runWriterT :: m (a, w)

Instances

Monoid w => MonadTrans (WriterT w) |
(Monoid w, Alternative m) => Alternative (WriterT w m) |
(Monoid w, Monad m) => Monad (WriterT w m) |
Functor m => Functor (WriterT w m) |
(Monoid w, MonadFix m) => MonadFix (WriterT w m) |
(Monoid w, MonadPlus m) => MonadPlus (WriterT w m) |
(Monoid w, Applicative m) => Applicative (WriterT w m) |
Foldable f => Foldable (WriterT w f) |
Traversable f => Traversable (WriterT w f) |
(Monoid w, MonadIO m) => MonadIO (WriterT w m) |

execWriterT :: Monad m => WriterT w m a -> m w

Extract the output from a writer computation.

-     execWriterT m = liftM snd (runWriterT m)

mapWriterT :: (m (a, w) -> n (b, w')) -> WriterT w m a -> WriterT w' n b

Map both the return value and output of a computation using the given function.

-     runWriterT (mapWriterT f m) = f (runWriterT m)

# Writer operations

tell :: (Monoid w, Monad m) => w -> WriterT w m ()

``tell` w` is an action that produces the output `w`.

listen :: (Monoid w, Monad m) => WriterT w m a -> WriterT w m (a, w)

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

-     runWriterT (listen m) = liftM (\(a, w) -> ((a, w), w)) (runWriterT m)

listens :: (Monoid w, Monad m) => (w -> b) -> WriterT w m a -> WriterT w m (a, b)

``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 (id *** f) (listen m)

-     runWriterT (listens f m) = liftM (\(a, w) -> ((a, f w), w)) (runWriterT m)

pass :: (Monoid w, Monad m) => WriterT w m (a, w -> w) -> WriterT w m a

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

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

censor :: (Monoid w, Monad m) => (w -> w) -> WriterT w m a -> WriterT w m a

``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 (\x -> (x,f)) m)

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

# Lifting other operations

liftCallCC :: Monoid w => ((((a, w) -> m (b, w)) -> m (a, w)) -> m (a, w)) -> ((a -> WriterT w m b) -> WriterT w m a) -> WriterT w m a

Lift a `callCC` operation to the new monad.

liftCatch :: (m (a, w) -> (e -> m (a, w)) -> m (a, w)) -> WriterT w m a -> (e -> WriterT w m a) -> WriterT w m a

Lift a `catchError` operation to the new monad.