HORIZON HASKELLDocslts/ghc-10.0.x2b276322026-10-31Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 2b27632 · 2026-10-31

Moduletransformers-0.6.3.0Haskell2010

Control.Monad.Trans.Accum

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.

Contents
  1. The Accum monad
  2. The AccumT monad transformer
  3. Accum operations
  4. Lifting other operations
  5. Monad transformations
Synopsis

The Accum monad

typetype 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:

image: 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.

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

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

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

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

The AccumT monad transformer

newtypenewtype AccumT w (m :: Type -> Type) 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:

image: 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.

Constructors

  • AccumT
    • runAccumT :: 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.

Instances11MonadTrans, Monad, Functor, MonadFix, MonadFail, Applicative, …

Accum operations

Lifting other operations

valueliftCallCC :: 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.

Monad transformations