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

Modulemtl-prelude-2.0.3.2Haskell2010

MTLPrelude

Reexports of most definitions from "mtl" and "transformers".

For details check out the source.

  • 14 types
  • 8 classes
  • 71 values
valuelabel :: MonadCont m => a -> m (a -> m b, a)
#

Introduces a recursive binding to the continuation. Due to the use of callCC, calling the continuation will interrupt execution of the current block creating an effect similar to goto/setjmp in C.

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

Methods

  • callCC :: ((a -> m b) -> m a) -> m a

    callCC (call-with-current-continuation) calls a function with the current continuation as its argument. Provides an escape continuation mechanism for use with Continuation monads. Escape continuations allow to abort the current computation and return a value immediately. They achieve a similar effect to throwError and catchError within an Except monad. Advantage of this function over calling return is that it makes the continuation explicit, allowing more flexibility and better control (see examples in Control.Monad.Cont).

    The standard idiom used with callCC is to provide a lambda-expression to name the continuation. Then calling the named continuation anywhere within its scope will escape from the computation, even if it is many layers deep within nested computations.

Instances14MonadCont, …
newtypenewtype ExceptT e (m :: Type -> Type) a
#

A monad transformer that adds exceptions to other monads.

ExceptT constructs a monad parameterized over two things:

  • e - The exception type.

  • m - The inner monad.

The return function yields a computation that produces the given value, while >>= sequences two subcomputations, exiting on the first exception.

Constructors

Instances33MonadRWS, Generic1, MonadAccum, MonadError, MonadReader, MonadState, …
typetype Except e = ExceptT e Identity
#

The parameterizable exception monad.

Computations are either exceptions or normal values.

The return function returns a normal value, while >>= exits on the first exception. For a variant that continues after an error and collects all the errors, see Errors.

valuewithExcept :: (e -> e') -> Except e a -> Except e' a
#

Transform any exceptions thrown by the computation using the given function (a specialization of withExceptT).

valuerunReader
  1. :: Reader r a

    A Reader to run.

  2. -> r

    An initial environment.

  3. -> a
#

Runs a Reader and extracts the final value from it. (The inverse of reader.)

newtypenewtype ReaderT r (m :: Type -> Type) a
#

The reader monad transformer, which adds a read-only environment to the given monad.

The return function ignores the environment, while m >>= k passes the inherited environment to both subcomputations:

image: images/bind-ReaderT.svg

Constructors

Instances22Generic1, MonadAccum, MonadError, MonadReader, MonadState, MonadWriter, …
typetype RWS r w s = RWST r w s Identity
#

A monad containing an environment of type r, output of type w and an updatable state of type s.

valuerws :: (r -> s -> (a, s, w)) -> RWS r w s a
#

Construct an RWS computation from a function. (The inverse of runRWS.)

valuerunRWS :: RWS r w s a -> r -> s -> (a, s, w)
#

Unwrap an RWS computation as a function. (The inverse of rws.)

valueevalRWS
  1. :: RWS r w s a

    RWS computation to execute

  2. -> r

    initial environment

  3. -> s

    initial value

  4. -> (a, w)

    final value and output

#

Evaluate a computation with the given initial state and environment, returning the final value and output, discarding the final state.

valueexecRWS
  1. :: RWS r w s a

    RWS computation to execute

  2. -> r

    initial environment

  3. -> s

    initial value

  4. -> (s, w)

    final state and output

#

Evaluate a computation with the given initial state and environment, returning the final state and output, discarding the final value.

valuemapRWS :: ((a, s, w) -> (b, s, w')) -> RWS r w s a -> RWS r w' s b
#

Map the return value, final state and output of a computation using the given function.

newtypenewtype RWST r w s (m :: Type -> Type) a
#

A monad transformer adding reading an environment of type r, collecting an output of type w and updating a state of type s to an inner monad m.

Constructors

Instances20MonadRWS, MonadAccum, MonadError, MonadReader, MonadState, MonadWriter, …
valueevalRWST
  1. :: Monad m
  2. => RWST r w s m a

    computation to execute

  3. -> r

    initial environment

  4. -> s

    initial value

  5. -> m (a, w)

    computation yielding final value and output

#

Evaluate a computation with the given initial state and environment, returning the final value and output, discarding the final state.

valueexecRWST
  1. :: Monad m
  2. => RWST r w s m a

    computation to execute

  3. -> r

    initial environment

  4. -> s

    initial value

  5. -> m (s, w)

    computation yielding final state and output

#

Evaluate a computation with the given initial state and environment, returning the final state and output, discarding the final value.

typetype State s = StateT s Identity
#

A state monad parameterized by the type s of the state to carry.

The return function leaves the state unchanged, while >>= uses the final state of the first computation as the initial state of the second.

valuerunState
  1. :: State s a

    state-passing computation to execute

  2. -> s

    initial state

  3. -> (a, s)

    return value and final state

#

Unwrap a state monad computation as a function. (The inverse of state.)

valueevalState
  1. :: State s a

    state-passing computation to execute

  2. -> s

    initial value

  3. -> a

    return value of the state computation

#

Evaluate a state computation with the given initial state and return the final value, discarding the final state.

valueexecState
  1. :: State s a

    state-passing computation to execute

  2. -> s

    initial value

  3. -> s

    final state

#

Evaluate a state computation with the given initial state and return the final state, discarding the final value.

newtypenewtype StateT s (m :: Type -> Type) a
#

A state transformer monad parameterized by:

  • s - The state.

  • m - The inner monad.

The return function leaves the state unchanged, while >>= uses the final state of the first computation as the initial state of the second.

Constructors

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

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

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

Instances30MonadAccum, MonadError, MonadReader, MonadState, MonadWriter, MonadSelect, …
typetype Cont r = ContT r Identity
#

Continuation monad. Cont r a is a CPS ("continuation-passing style") computation that produces an intermediate result of type a within a CPS computation whose final result type is r.

The return function simply creates a continuation which passes the value on.

The >>= operator adds the bound function into the continuation chain.

newtypenewtype Identity a
#

Identity functor and monad. (a non-strict monad)

Examples
Example1 expression
fmap (+1) (Identity 0)Identity 1
Example1 expression
Identity [1, 2, 3] <> Identity [4, 5, 6]Identity [1,2,3,4,5,6]
>>> do
      x <- Identity 10
      y <- Identity (x + 5)
      pure (x + y)
Identity 25

Constructors

Instances39Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …
newtypenewtype ContT (r :: k) (m :: k -> Type) a
#

The continuation monad transformer. Can be used to add continuation handling to any type constructor: the Monad instance and most of the operations do not require m to be a monad.

ContT is not a functor on the category of monads, and many operations cannot be lifted through it.

Constructors

Instances13MonadAccum, MonadReader, MonadState, MonadSelect, MonadTrans, Monad, …
newtypenewtype MaybeT (m :: Type -> Type) a
#

The parameterizable maybe monad, obtained by composing an arbitrary monad with the Maybe monad.

Computations are actions that may produce a value or exit.

The return function yields a computation that produces that value, while >>= sequences two subcomputations, exiting if either computation does.

Constructors

Instances33MonadTrans, MonadRWS, Generic1, MonadAccum, MonadError, MonadReader, …
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.

Instances15MonadTrans, …
classclass Monad m => MonadReader r (m :: Type -> Type) | m -> r where
#

See examples in Control.Monad.Reader. Note, the partially applied function type (->) r is a simple reader monad. See the instance declaration below.

Methods

  • ask :: m r

    Retrieves the monad environment.

  • local :: (r -> r) -> m a -> m a

    Executes a computation in a modified environment.

  • reader :: (r -> a) -> m a

    Retrieves a function of the current environment.

Instances16MonadReader, …
valuecont :: ((a -> r) -> r) -> Cont r a
#

Construct a continuation-passing computation from a function. (The inverse of runCont)

valueevalCont :: Cont r r -> r
#

The result of running a CPS computation with the identity as the final continuation.

valuemapContT :: (m r -> m r) -> ContT r m a -> ContT r m a
#

Apply a function to transform the result of a continuation-passing computation. This has a more restricted type than the map operations for other monad transformers, because ContT does not define a functor in the category of monads.

valuerunCont
  1. :: Cont r a

    continuation computation (Cont).

  2. -> (a -> r)

    the final continuation, which produces the final result (often id).

  3. -> r
#

The result of running a CPS computation with a given final continuation. (The inverse of cont)

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
    
Instances16MonadIO, …
classclass Monad m => MonadError e (m :: Type -> Type) | m -> e where
#

The strategy of combining computations that can throw exceptions by bypassing bound functions from the point an exception is thrown to the point that it is handled.

Is parameterized over the type of error information and the monad type constructor. It is common to use Either String as the monad type constructor for an error monad in which error descriptions take the form of strings. In that case and many other common cases the resulting monad is already defined as an instance of the MonadError class. You can also define your own error type and/or use a monad type constructor other than Either String or Either IOError. In these cases you will have to explicitly define instances of the MonadError class. (If you are using the deprecated Control.Monad.Error or Control.Monad.Trans.Error, you may also have to define an Error instance.)

Methods

  • throwError :: e -> m a

    Is used within a monadic computation to begin exception processing.

  • catchError :: m a -> (e -> m a) -> m a

    A handler function to handle previous errors and return to normal execution. A common idiom is:

    do { action1; action2; action3 } `catchError` handler

    where the action functions can call throwError. Note that handler and the do-block must have the same return type.

Instances16MonadError, …
valuemodifyError :: MonadError e' m => (e -> e') -> ExceptT e m a -> m a
#

A different MonadError analogue to the withExceptT function. Modify the value (and possibly the type) of an error in an ExceptT-transformed monad, while stripping the ExceptT layer.

This is useful for adapting the MonadError constraint of a computation.

For example:

data DatabaseError = ...

performDatabaseQuery :: (MonadError DatabaseError m, ...) => m PersistedValue

data AppError
  = MkDatabaseError DatabaseError
  | ...

app :: (MonadError AppError m, ...) => m ()

Given these types, performDatabaseQuery cannot be used directly inside app, because the error types don't match. Using modifyError, an equivalent function with a different error type can be constructed:

performDatabaseQuery' :: (MonadError AppError m, ...) => m PersistedValue
performDatabaseQuery' = modifyError MkDatabaseError performDatabaseQuery

Since the error types do match, performDatabaseQuery' _can_ be used in app, assuming all other constraints carry over.

This works by instantiating the m in the type of performDatabaseQuery to ExceptT DatabaseError m', which satisfies the MonadError DatabaseError constraint. Immediately, the ExceptT DatabaseError layer is unwrapped, producing Either a DatabaseError or a PersistedValue. If it's the former, the error is wrapped in MkDatabaseError and re-thrown in the inner monad, otherwise the result value is returned.

valuewithError :: MonadError e m => (e -> e) -> m a -> m a
#

MonadError analogue to the withExceptT function. Modify the value (but not the type) of an error. The type is fixed because of the functional dependency m -> e. If you need to change the type of e use mapError or modifyError.

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.

Instances14MonadWriter, …
valueasks
  1. :: MonadReader r m
  2. => (r -> a)

    The selector function to apply to the environment.

  3. -> m a
#

Retrieves a function of the current environment.

classclass Monad m => MonadState s (m :: Type -> Type) | m -> s where
#

Minimal definition is either both of get and put or just state

Methods

  • get :: m s

    Return the state from the internals of the monad.

  • put :: s -> m ()

    Replace the state inside the monad.

  • state :: (s -> (a, s)) -> m a

    Embed a simple state action into the monad.

Instances15MonadState, …
valuegets :: MonadState s m => (s -> a) -> m a
#

Gets specific component of the state, using a projection function supplied.

valuemodify :: MonadState s m => (s -> s) -> m ()
#

Monadic state transformer.

Maps an old state to a new state inside a state monad. The old state is thrown away.

     Main> :t modify ((+1) :: Int -> Int)
     modify (...) :: (MonadState Int a) => a ()

This says that modify (+1) acts over any Monad that is a member of the MonadState class, with an Int state.

valuemodify' :: MonadState s m => (s -> s) -> m ()
#

A variant of modify in which the computation is strict in the new state.

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 (Monoid w, MonadReader r m, MonadWriter w m, MonadState s m) => MonadRWS r w s (m :: Type -> Type) | m -> r, m -> w, m -> s
#
Instances6MonadRWS