Module

Control.Monad.Writer

Package
transformers
Repository
purescript/purescript-transformers

This module defines the Writer monad.

#Writer Source

type Writer w = WriterT w Identity

The Writer monad is a synonym for the WriterT monad transformer, applied to the Identity monad.

#writer Source

writer :: forall w a. Tuple a w -> Writer w a

Creates a Writer from a result and output pair.

#runWriter Source

runWriter :: forall w a. Writer w a -> Tuple a w

Run a computation in the Writer monad

#execWriter Source

execWriter :: forall w a. Writer w a -> w

Run a computation in the Writer monad, discarding the result

#mapWriter Source

mapWriter :: forall w1 w2 a b. (Tuple a w1 -> Tuple b w2) -> Writer w1 a -> Writer w2 b

Change the result and accumulator types in a Writer monad action

Re-exports from Control.Monad.Writer.Class

#MonadTell Source

class MonadTell :: Type -> (Type -> Type) -> Constraintclass (Semigroup w, Monad m) <= MonadTell w m | m -> w where

The MonadTell w type class represents those monads which support a monoidal accumulator of type w, where tell appends a value to the accumulator.

An implementation is provided for WriterT, and for other monad transformers defined in this library.

Law:

  • do { tell x ; tell y } = tell (x <> y)

Members

#MonadWriter Source

class MonadWriter :: Type -> (Type -> Type) -> Constraintclass (Monoid w, MonadTell w m) <= MonadWriter w m | m -> w where

An extension of the MonadTell class that introduces some operations on the accumulator:

  • listen modifies the result to include the changes to the accumulator.
  • pass applies the returned function to the accumulator.

An implementation is provided for WriterT, and for other monad transformers defined in this library.

Laws in addition to the MonadTell law:

  • do { tell x ; tell y } = tell (x <> y)
  • listen (pure a) = pure (Tuple a mempty)
  • listen (writer a x) = tell x $> Tuple a x

Members

#listens Source

listens :: forall w m a b. MonadWriter w m => (w -> b) -> m a -> m (Tuple a b)

Projects a value from modifications made to the accumulator during an action.

#censor Source

censor :: forall w m a. MonadWriter w m => (w -> w) -> m a -> m a

Modify the final accumulator value by applying a function.

Re-exports from Control.Monad.Writer.Trans

#WriterT Source

newtype WriterT :: Type -> (Type -> Type) -> Type -> Typenewtype WriterT w m a

The writer monad transformer.

This monad transformer extends the base monad with a monoidal accumulator of type w.

The MonadWriter type class describes the operations supported by this monad.

Constructors

Instances

#MonadTrans Source

class MonadTrans :: ((Type -> Type) -> Type -> Type) -> Constraintclass MonadTrans t  where

The MonadTrans type class represents monad transformers.

A monad transformer is a type constructor of kind (* -> *) -> * -> *, which takes a Monad as its first argument, and returns another Monad.

This allows us to add additional effects to an existing monad. By iterating this process, we create monad transformer stacks, which contain all of the effects required for a particular computation.

The laws state that lift is a Monad morphism.

Laws:

  • lift (pure a) = pure a
  • lift (do { x <- m ; y }) = do { x <- lift m ; lift y }

Members

#runWriterT Source

runWriterT :: forall w m a. WriterT w m a -> m (Tuple a w)

Run a computation in the WriterT monad.

#mapWriterT Source

mapWriterT :: forall w1 w2 m1 m2 a b. (m1 (Tuple a w1) -> m2 (Tuple b w2)) -> WriterT w1 m1 a -> WriterT w2 m2 b

Change the accumulator and base monad types in a WriterT monad action.

#execWriterT Source

execWriterT :: forall w m a. Functor m => WriterT w m a -> m w

Run a computation in the WriterT monad, discarding the result.