Combining monad transformers
So far, we have seen different monad transformers dedicated to specific causes. What if we would like to work with more than one transformer at the same time? In this recipe, we will be doing exactly that! We will work with Reader and Writer transformers with IO monad.
We will revisit the cursor example that we wrote earlier and then transform it to use it with multiple monad transformers.
How to do it...
- Create a new project
combine-trans
with thesimple
Stack template. - Add
mtl
to thebuild-depends
subsection of theexecutable
section:
executable combine-trans hs-source-dirs: src main-is: Main.hs default-language: Haskell2010 build-depends: base >= 4.7 && < 5 , mtl
- Open
src/Main.hs
and add the following imports after the initial module declaration. ImportPrelude
as well to avoid a clash with some names:
import Prelude hiding (Either(..)) import Control.Monad.Reader ...