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

Modulefoundation-0.0.30Haskell2010

Foundation.Monad

  • 1 type
  • 6 classes
  • 1 value
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
    
Instances8MonadIO, …
classclass Monad m => MonadFailure (m :: Type -> Type) where
#

Monad that can represent failure

Similar to MonadFail but with a parametrized Failure linked to the Monad

Associated types

  • type family Failure (m :: Type -> Type)

    The associated type with the MonadFailure, representing what failure can be encoded in this monad

Methods

Instances8MonadFailure, …
classclass Monad m => MonadThrow (m :: Type -> Type) where
#

Monad that can throw exception

Methods

Instances6MonadThrow
classclass MonadThrow m => MonadCatch (m :: Type -> Type) where
#

Monad that can catch exception

Methods

Instances6MonadCatch
classclass MonadCatch m => MonadBracket (m :: Type -> Type) where
#

Monad that can ensure cleanup actions are performed even in the case of exceptions, both synchronous and asynchronous. This usually excludes continuation-based monads.

Methods

  • generalBracket :: m a -> (a -> b -> m ignored1) -> (a -> SomeException -> m ignored2) -> (a -> m b) -> m b

    A generalized version of the standard bracket function which allows distinguishing different exit cases.

Instances3MonadBracket
classclass MonadTrans (trans :: (Type -> Type) -> Type -> Type) where
#

Basic Transformer class

Methods

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

    Lift a computation from an inner monad to the current transformer monad

Instances6MonadTrans
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

Instances37Monad, Functor, MonadFix, Applicative, Foldable, Traversable, …