HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulerebase-1.21.2Haskell2010

Rebase.Control.Monad.Writer

  • 2 types
  • 3 classes
  • 7 values
  • Packagerebase-1.21.2
  • Exports12
  • LanguageHaskell2010
  • LicenceMIT
  • SourceLazy.hs
newtypenewtype WriterT w (m :: Type -> Type) a
#

A writer monad parameterized by:

  • w - the output to accumulate.

  • m - The inner monad.

The return function produces the output mempty, while m >>= k combines the outputs of the subcomputations using mappend (also known as <>):

image: images/bind-WriterT.svg

Constructors

Instances52MonadAccum, MonadError, MonadReader, MonadState, MonadWriter, MonadSelect, …
valuerunWriter :: Writer w a -> (a, w)
#

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

classclass (forall (m :: Type -> Type). Monad m => Monad (t m)) => MonadTrans (t :: (Type -> Type) -> Type -> Type) where
#

The class of monad transformers. For any monad m, the result t m should also be a monad, and lift should be a monad transformation from m to t m, i.e. it should satisfy the following laws:

Since 0.6.0.0 and for GHC 8.6 and later, the requirement that t m be a Monad is enforced by the implication constraint forall m. Monad m => Monad (t m) enabled by the QuantifiedConstraints extension.

Ambiguity error with GHC 9.0 to 9.2.2

These versions of GHC have a bug (https://gitlab.haskell.org/ghc/ghc/-/issues/20582) which causes constraints like

(MonadTrans t, forall m. Monad m => Monad (t m)) => ...

to be reported as ambiguous. For transformers 0.6 and later, this can be fixed by removing the second constraint, which is implied by the first.

Methods

  • lift :: Monad m => m a -> t m a

    Lift a computation from the argument monad to the constructed monad.

Instances32MonadTrans, …
classclass (Monoid w, Monad m) => MonadWriter w (m :: Type -> Type) | m -> w where
#

Methods

  • writer :: (a, w) -> m a

    writer (a,w) embeds a simple writer action.

  • tell :: w -> m ()

    tell w is an action that produces the output w.

  • listen :: m a -> m (a, w)

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

  • pass :: m (a, w -> 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.

Instances25MonadWriter, …
valuecensor :: MonadWriter w m => (w -> w) -> m a -> 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.

valuelistens :: MonadWriter w m => (w -> b) -> m a -> 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.

classclass Monad m => MonadIO (m :: Type -> Type) where
#

Monads in which IO computations may be embedded. Any monad built by applying a sequence of monad transformers to the IO monad will be an instance of this class.

Instances should satisfy the following laws, which state that liftIO is a transformer of monads:

Methods

  • liftIO :: IO a -> m a

    Lift a computation from the IO monad. This allows us to run IO computations in any monadic stack, so long as it supports these kinds of operations (i.e. IO is the base monad for the stack).

    Example
    import Control.Monad.Trans.State -- from the "transformers" library
    
    printState :: Show s => StateT s IO ()
    printState = do
      state <- get
      liftIO $ print state

    Had we omitted liftIO, we would have ended up with this error:

    • Couldn't match type ‘IO’ with ‘StateT s IO’
     Expected type: StateT s IO ()
       Actual type: IO ()

    The important part here is the mismatch between StateT s IO () and IO ().

    Luckily, we know of a function that takes an IO a and returns an (m a): liftIO, enabling us to run the program and see the expected results:

    > evalStateT printState "hello"
    "hello"
    
    > evalStateT printState 3
    3
    
Instances26MonadIO, …