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

Modulemanaged-1.0.10Haskell2010

Control.Monad.Managed.Safe

This module is a safer subset of Control.Monad.Managed that only lets you unwrap the Managed type using runManaged. This enforces that you never leak acquired resources from a Managed computation.

In general, you should strive to propagate the Managed type as much as possible and use runManaged when you are done with acquired resources. However, there are legitimate circumstances where you want to return a value other than acquired resource from the bracketed computation, which requires using with.

This module is not the default because you can also use the Managed type for callback-based code that is completely unrelated to resources.

  • 1 type
  • 2 classes
  • 4 values
  • Packagemanaged-1.0.10
  • Exports7
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceSafe.hs

Managed

6 declarations
newtypenewtype Managed a
#

A managed resource that you acquire using with

Instances11Monad, Functor, MonadFail, Applicative, MonadIO, MonadManaged, …
classclass MonadIO m => MonadManaged (m :: Type -> Type) where
#

You can embed a Managed action within any Monad that implements MonadManaged by using the using function

All instances must obey the following two laws:

using (return x) = return x

using (m >>= f) = using m >>= \x -> using (f x)

Methods

Instances12MonadManaged, …
valuedefer :: MonadManaged m => IO r -> m ()
#

Defer running an action until exit (via runManaged).

For example, the following code will print "Hello" followed by "Goodbye":

runManaged $ do
  defer $ liftIO $ putStrLn "Goodbye"
  liftIO $ putStrLn "Hello"

Re-exports

1 declaration
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
    
Instances17MonadIO, …