Community
Modules 24

Control.Monad.Trans.Accum

experimental

The lazy AccumT monad transformer, which adds accumulation capabilities (such as declarations or document patches) to a given monad. Each computation has access to the combination of the input environment and outputs added so far, and returns the outputs added.

In applications requiring only the ability to accumulate an output and to inspect the output so far, it would be considerably more efficient to use Control.Monad.Trans.State instead.

The Accum monad

type Accum w = AccumT w Identity

An accumulation monad (non-strict) parameterized by the type w of output to accumulate.

This monad is a more complex extension of both the reader and writer monads. The return function produces the output mempty, while @m >>= k uses the output of m@ both to extend the initial environment of k and to combine with the output of k:

<<images/bind-AccumT.svg>>

In applications requiring only the ability to accumulate an output and to inspect the output so far, it would be considerably more efficient to use a state monad.

accum :: (Monad m) => (w -> (a, w)) -> AccumT w m a

Construct an accumulation computation from a (result, output) pair. (The inverse of runAccum.)

runAccum :: Accum w a -> w -> (a, w)

Unwrap an accumulation computation as a (result, output) pair. (The inverse of accum.)

execAccum :: Accum w a -> w -> w

Extract the output from an accumulation computation.

  • execAccum m w = snd (runAccum m w)

evalAccum :: (Monoid w) => Accum w a -> w -> a

Evaluate an accumulation computation with the given initial output history and return the final value, discarding the final output.

  • evalAccum m w = fst (runAccum m w)

mapAccum :: ((a, w) -> (b, w)) -> Accum w a -> Accum w b

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

  • runAccum (mapAccum f m) = f . runAccum m

The AccumT monad transformer

newtype AccumT w m a

An accumulation monad parameterized by:

  • w - the output to accumulate.

  • m - The inner monad.

This monad transformer is a more complex extension of both the reader and writer monad transformers. The return function produces the output mempty, while m >>= k uses the output of m both to extend the initial environment of k and to combine with the output of k:

<<images/bind-AccumT.svg>>

In applications requiring only the ability to accumulate an output and to inspect the output so far, it would be considerably more efficient to use a state monad transformer.

AccumT w m is strict if and only if m is.

AccumT :: w -> m (a, w) -> AccumT w m a

runAccumT :: AccumT w m a -> w -> m (a, w)

Unwrap an accumulation computation. For example, in the call

(value, locals) <- runAccumT action globals

the action is fed an initial environment globals, and locals is the sum of all arguments to calls of add executed by the action.

execAccumT :: (Monad m) => AccumT w m a -> w -> m w

Extract the output from an accumulation computation.

  • execAccumT m w = liftM snd (runAccumT m w)

evalAccumT :: (Monad m, Monoid w) => AccumT w m a -> w -> m a

Evaluate an accumulation computation with the given initial output history and return the final value, discarding the final output.

  • evalAccumT m w = liftM fst (runAccumT m w)

mapAccumT :: (m (a, w) -> n (b, w)) -> AccumT w m a -> AccumT w n b

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

  • runAccumT (mapAccumT f m) = f . runAccumT m

Accum operations

look :: (Monoid w, Monad m) => AccumT w m w

look is an action that fetches all the previously accumulated output.

looks :: (Monoid w, Monad m) => (w -> a) -> AccumT w m a

look is an action that retrieves a function of the previously accumulated output.

add :: (Monad m) => w -> AccumT w m ()

add w is an action that produces the output w.

Lifting other operations

liftCallCC :: CallCC m (a, w) (b, w) -> CallCC (AccumT w m) a b

Uniform lifting of a callCC operation to the new monad. This version rolls back to the original output history on entering the continuation.

liftCallCC' :: CallCC m (a, w) (b, w) -> CallCC (AccumT w m) a b

In-situ lifting of a callCC operation to the new monad. This version uses the current output history on entering the continuation. It does not satisfy the uniformity property (see Control.Monad.Signatures).

liftCatch :: Catch e m (a, w) -> Catch e (AccumT w m) a

Lift a catchE operation to the new monad. The uniformity property (see Control.Monad.Signatures) implies that the lifted catchE discards any output from the body on entering the handler.

liftListen :: (Monad m) => Listen w m (a, s) -> Listen w (AccumT s m) a

Lift a listen operation to the new monad.

liftPass :: (Monad m) => Pass w m (a, s) -> Pass w (AccumT s m) a

Lift a pass operation to the new monad.

Monad transformations

readerToAccumT :: (Functor m, Monoid w) => ReaderT w m a -> AccumT w m a

Convert a read-only computation into an accumulation computation.

writerToAccumT :: WriterT w m a -> AccumT w m a

Convert a writer computation into an accumulation computation.

accumToStateT :: (Functor m, Monoid s) => AccumT s m a -> StateT s m a

Convert an accumulation (append-only) computation into a fully stateful computation.

Instances

10
  • instance (Generic w, Generic m, Generic a) => Generic (AccumT w m a)
  • instance (Functor m) => Functor (AccumT w m)
  • instance (Monoid w, Functor m, Monad m) => Applicative (AccumT w m)
  • instance (Monoid w, Functor m, MonadPlus m) => Alternative (AccumT w m)
  • instance (Monoid w, Functor m, Monad m) => Monad (AccumT w m)
  • instance (Monoid w, Fail.MonadFail m) => Fail.MonadFail (AccumT w m)
  • instance (Monoid w, Functor m, MonadPlus m) => MonadPlus (AccumT w m)
  • instance (Monoid w, Functor m, MonadFix m) => MonadFix (AccumT w m)
  • instance (Monoid w) => MonadTrans (AccumT w)
  • instance (Monoid w, Functor m, MonadIO m) => MonadIO (AccumT w m)
Maintainer
R.Paterson@city.ac.uk
Portability
portable
Copyright
(c) Nickolay Kudasov 2016
License
BSD-style (see the file LICENSE)
Build record and source files

Community contribution. The service has not verified this documentation.

Target: Linux on x86-64. Some inferred types and resolved links are not available.

Generator: aihc-haddock-858b1951-web-1

Metadata snapshot: ac77ab7d2b1e88487eeab41661abedb7bfb8bc846deb92e7874c08d166bff65d

Open the dependency plan · Download the documentation model

Import a package

Enter a package name and an exact version from Hackage. The site contains imported versions only.