Monad subclasses
The six monad transformers, together with an appropriate base monad such as Identity
or IO
, allow us to put together a large number of different monads. Given the many choices for the monad that’s used by an application, we often do not want to fix the monad up front:
- We may not have a full overview of the effects that are required by the application before we start writing parts of it
- We may want to reuse different programs or parts of programs with different monads
- We may want to quickly adapt an existing program to additional requirements, which may entail incorporating additional effects
To support these and similar scenarios, Haskell programmers can make use of type classes to abstract over the particular monad being used while still imposing requirements on it. These type classes are known as Monad
subclasses, and there is one for each effect.
The MonadReader type class
Our first Monad
subclass is for the reader effect: