HA

MTL Effects

MTL-style monad transformer effects for structuring Haskell application layers with ReaderT, StateT, and WriterT

Details

Language / Topic
haskellHaskell
Category
Architecture

Rules

balanced
- Use `ReaderT env IO a` as the primary application monad — it threads read-only config and service handles through all layers without explicit parameter passing.
- Define a `newtype App a = App { unApp :: ReaderT AppEnv IO a }` and derive `MonadReader AppEnv`, `MonadIO`, and `MonadLogger`.
- Constrain functions with type class constraints (`MonadReader`, `MonadError`, `MonadIO`) rather than `App` directly to keep them testable.
- Use `MonadReader AppEnv m` to access configuration and service dependencies — call `asks appDbPool` to extract a specific field from the environment.
- Use `MonadError AppError m` via `ExceptT` in the transformer stack for typed error propagation across all layers.
- Avoid `WriterT` for logging in production code — it buffers all log output until the computation completes; use `MonadLogger` with immediate IO instead.
- Use `runReaderT (unApp action) env` at the top-level `main` to supply the concrete environment and execute the `App` monad.