Monad Transformers
This chapter shows how the functionality of different monads can be combined into a single monad. Creating such combinations is relatively straightforward for applicative functors. We have seen that two different constructions can be used for this purpose – products and compositions. Unfortunately, neither works for monads. The product of two monads is not a monad and the composition of two monads is not a monad.
For the first few years of Haskell’s existence, there was no other way to create custom monads with combined effects than to write them from scratch. This involved a lot of boilerplate and required substantial expertise. Hence, researchers have long sought a way to make it possible to combine existing monads. This would avoid the boilerplate and make custom monads more widely accessible.
Eventually, an out-of-the-box solution was found: monad transformers. The idea is no longer to combine monads but rather to transform an existing monad...