Other monad transformers
With the notable exception of the IO
monad, which is a special case because it is built into the language, most other monads have a corresponding monad transformer. As they mostly follow the same approach as ReaderT
, we’ll only review them briefly.
The StateT and WriterT transformers
The transformers for the state and writer effects have the closest resemblance to ReaderT
:
Control.Monad.Trans.State newtype StateT s m a = StateT { runStateT :: s -> m (a, s) } Control.Monad.Trans.Writer newtype WriterT w m a = WriterT { runWriterT :: m (a, w) }
As monad transformers, they have appropriate Monad
instances:
Control.Monad.Trans.State instance Monad m => Monad (StateT s m) Control.Monad.Trans.Writer instance (Monad m, Monoid w) => Monad (WriterT w m)
We haven’t shown the implementation because it is very similar to the ones for State s
and Writer w
from the previous chapter. Instead, we will show the full MonadTrans
instances...