liveBook · Manning

MonadTrans

As explained in Learn Haskell by Example

MonadTrans is a Haskell type class for monad transformers that lift computations from an underlying monad.

MonadTrans is a Haskell type class for monad transformers. Its lift function moves a computation from an underlying monad into the transformer context, allowing both effects to participate in one action. section 15.1.1

Lifting an IO action into ReaderT

With type App a = ReaderT Config IO a, an App action cannot use putStrLn directly because putStrLn belongs to IO. Writing liftIO $ putStrLn (...) lifts that IO action into App. MonadTrans generalizes this idea with lift for underlying monads beyond IO. 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 class signature makes the abstraction concrete: lift accepts a Monad action and returns the corresponding action in transformer t. section 15.1.1

How Learn Haskell by Example covers it

How deep it goes
Defines lift through its class signature but stops short of transformer implementation details.
Examples
Uses liftIO to print configured source and destination paths from a ReaderT Config IO action.
What you can do after
Apply lift to reuse underlying-monad actions inside transformer computations.
How it connects
Connects monad transformers with ReaderT, MonadIO, and IO actions.

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.