ReaderT is a Haskell monad transformer that adds read-only environment access to an underlying monad. ReaderT r m a stores an environment of type r, uses m for effects, and produces a result of type a. section 15.1.1
Use ask to query the environment without passing it from action to action. Define an application type such as App a = ReaderT Config IO a, then lift IO actions with liftIO. section 15.1.1
The main design choices are:
- Choose the environment type, such as Config. section 15.1.1
- Choose the underlying monad, such as IO, to retain its capabilities. section 15.1.1
- Lift actions from the underlying monad into ReaderT with lift or liftIO. section 15.1.1
Configuration-driven application
type App a = ReaderT Config IO a. An action can read configFromPath and configToPath with ask, print the paths using liftIO, and run with runReaderT example cfg. section 15.1.1
ghci> import Control.Monad.Trans.Reader
ghci> type App a = ReaderT Config IO a
ghci> import Control.Monad.IO.Class (liftIO)
ghci> :{
ghci| example :: App ()
ghci| example = do
ghci| env <- ask
ghci| let toPath = configToPath env
ghci| fromPath = configFromPath env
ghci| in liftIO $ putStrLn (fromPath <> " -> " <> toPath)
ghci| :}
ghci> cfg = Config "src" "dst" Nothing Nothing Nothing
ghci> runReaderT example cfg
src -> dst
Browse the complete ReaderT Config IO example, including the application type, environment query, lifted output, configuration value, and execution. section 15.1.1

