liveBook · Manning

Lift

As explained in Learn Haskell by Example

lift is a monad-transformer operation that embeds an action from an underlying monad into a transformed monad.

lift embeds an action from an underlying monad into a monad-transformer context. For example, ReaderT can lift actions from its underlying monad, while liftIO specifically lifts IO actions. section 15.1.1

Lifting an IO action in ReaderT

With type App a = ReaderT Config IO a, an App action can read its Config environment and use liftIO to print a path mapping. Running runReaderT example cfg produces src -> dst. section 15.1.1

ghci> import Control.Monad.Trans.Class
ghci> :i MonadTrans
type MonadTrans :: ((* -> *) -> * -> *) -> Constraint
class MonadTrans t where
  lift :: Monad m => m a -> t m a
  {-# MINIMAL lift #-}

The MonadTrans definition gives lift its general type: Monad m => m a -> t m a. section 15.1.1

How Learn Haskell by Example covers it

How deep it goes
Defines the MonadTrans operation and contrasts general lift with the IO-specific liftIO helper.
Examples
Uses a ReaderT Config IO application that lifts putStrLn to print configured source and destination paths.
What you can do after
Apply lift to reuse underlying-monad actions inside a transformer context.
How it connects
Connects monad transformers with ReaderT, MonadTrans, IO, MonadIO, and liftIO.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Haskell by Example. The section links point to the parts of the book it draws on.