HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduletransformers-0.6.1.1Haskell2010

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.

  • 2 types
  • 20 values

The Accum monad

6 declarations
typetype Accum w = AccumT w Identity
#

An accumulation monad 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.)

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

The AccumT monad transformer

5 declarations
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.

Constructors

Instances11MonadTrans, Monad, Functor, MonadFix, MonadFail, Applicative, …
valuerunAccumT :: 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.

Accum operations

3 declarations
valuelooks :: (Monoid w, Monad m) => (w -> a) -> AccumT w m a
#

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

valueadd :: Monad m => w -> AccumT w m ()
#

add w is an action that produces the output w.

Lifting other operations

5 declarations
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.

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

Monad transformations

3 declarations