liftIO -package:LambdaHack

base Control.Monad.IO.Class, conduit Conduit, unliftio-core Control.Monad.IO.Unlift, yesod-core Yesod.Core, protolude Protolude, cairo Graphics.Rendering.Cairo, scotty Web.Scotty Web.Scotty.Trans, haskell-gi-base Data.GI.Base.ShortPrelude, streaming Streaming, base-prelude BasePrelude, shake Development.Shake, turtle Turtle Turtle.Shell, shelly Shelly Shelly.Lifted Shelly.Pipe, cgi Network.CGI, classy-prelude ClassyPrelude, distributed-process Control.Distributed.Process, hosc Sound.Osc, streamly Streamly.Data.Stream.MkType Streamly.Internal.Data.Stream.MkType, effectful-core Effectful, universum Universum.Monad.Reexport, managed Control.Monad.Managed Control.Monad.Managed.Safe, zeromq4-haskell System.ZMQ4.Monadic, ihaskell IHaskell.Eval.Evaluate IHaskellPrelude, hslua-core HsLua.Core HsLua.Core.Types, massiv Data.Massiv.Core, beam-core Database.Beam, HaTeX Text.LaTeX.Base.Writer, foundation Foundation.Monad, ghc-lib-parser GHC.Core.Opt.Monad GHC.Data.Stream GHC.Driver.Monad GHC.Utils.Monad, xmonad XMonad, annotated-exception Control.Exception.Annotated.UnliftIO, ghc-internal GHC.Internal.Control.Monad.IO.Class, rebase Rebase.Prelude, core-program Core.System.Base, hackage-security Hackage.Security.Client.Verify, xmonad-contrib XMonad.Config.Prime, apecs Apecs, mtl-prelude MTLPrelude, nvim-hs Neovim, rhine FRP.Rhine, list-transformer List.Transformer, gi-cairo-render GI.Cairo.Render, BNFC BNFC.Backend.Base, breakpoint Debug.Breakpoint.GhcFacade, can-i-haz Control.Monad.Except.CoHas Control.Monad.Reader.Has, heftia Control.Monad.Hefty, mandrill Network.API.Mandrill, verset Verset, distribution-opensuse OpenSuse.Prelude, hledger-web Hledger.Web.Import, pandoc-plot Text.Pandoc.Filter.Plot.Internal, termonad Termonad.Prelude
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
Lifting of IO actions. Not exported, as we want to encapsulate IO.
Lift a computation from the IO monad.
Lift a computation from the IO monad.
Lift an IO action directly into GenProcess, liftIO = lift . Process.LiftIO.
Lift an IO action. Note that this can only happen with ConcIO.
Lift an IO operation into CoreM while consuming its SimplCount
A helper function for lifting IO a -> IO b functions into any MonadUnliftIO.

Example

liftedTry :: (Exception e, MonadUnliftIO m) => m a -> m (Either e a)
liftedTry m = liftIOOp Control.Exception.try m
Lift an IO operation with 1 argument into another monad
Lift an IO operation with 2 arguments into another monad
Lift an IO action into the Moment monad, but defer its execution until compilation time. This can be useful for recursive definitions using MonadFix.
Schedule an IO action to be run later.
liftIOOp is a particular application of peelIO that allows lifting control operations of type (a -> IO b) -> IO b (e.g. alloca, withMVar v) to MonadPeelIO m => (a -> m b) -> m b.
liftIOOp f g = do
k <- peelIO
join $ liftIO $ f (k . g)
liftIOOp_ is a particular application of peelIO that allows lifting control operations of type IO a -> IO a (e.g. block) to MonadPeelIO m => m a -> m a.
liftIOOp_ f m = do
k <- peelIO
join $ liftIO $ f (k m)
Lift with an unlifting function that accounts for the effects over IO.
Catch IOException of IO, modify it with a (IOException -> e) function and then throwError the new error in m (lifted IO).
Monads which allow their actions to be run in IO. While MonadIO allows an IO action to be lifted into another monad, this class captures the opposite concept: allowing you to capture the monadic context. Note that, in order to meet the laws given below, the intuition is that a monad must have no monadic state, but may have monadic context. This essentially limits MonadUnliftIO to ReaderT and IdentityT transformers on top of IO. Laws. For any function run provided by withRunInIO, it must meet the monad transformer laws as reformulated for MonadUnliftIO:
  • run . return = return
  • run (m >>= f) = run m >>= run . f
Instances of MonadUnliftIO must also satisfy the following laws:
  • Identity law withRunInIO (\run -> run m) = m
  • Inverse law withRunInIO (\_ -> m) = liftIO m
As an example of an invalid instance, a naive implementation of MonadUnliftIO (StateT s m) might be
withRunInIO inner =
StateT $ \s ->
withRunInIO $ \run ->
inner (run . flip evalStateT s)
This breaks the identity law because the inner run m would throw away any state changes in m.