Transformers and deriving

Contents
  • Setup
  • Type alias
  • ReaderT
  • Newtype
  • Generalized newtype deriving
  • Deriving via

This article introduces two topics:

  1. Monad transformers
  2. GHC’s extensions for typeclass deriving

We successively refactor an example, first introducing a very typical application of ReaderT, then cleaning up the code with two approaches for deriving Monad.

The final code uses the via deriving strategy to elegantly extract a monad instance from the transformers library without introducing any extra complication to our example’s type definitions.

Sign up for access to the full page, plus the complete archive and all the latest content.