kuniga.me > NP-Incompleteness > Monad Transformers
19 Jul 2014
In this post we’ll talk briefly about Monad Transformers. Chapter 18 from the Real World Haskell inspired this, but as usual, it was a bit hard for me to digest. The best source so far for this subject was the Haskell wiki [1].
In a high-level, monad transformers are monads generated by combining monads into a new one (thus transforming monads in monads).
Intuitively, it does so by using the analogy of wrapping, as we have for Monads, but monad transformers wraps monads inside monads. Thus, Dan Piponi makes an analogy of onion layers [3]. The idea of transformers is to avoid boilerplate in common scenarios where two monads are used in conjunction.
We’ll present some monad transformers, all follow the pattern where two monads are combined, the first one is fixed, and the other is generic. The fixed monads are Maybe
, List
and State
and their correspondent transformers are called MaybeT
, ListT
and StateT
, respectively. The Writer
and Read
monads also have corresponding transformers, but we’re not talking about them here.
As in [1], we’ll be more detailed in describing the MaybeT
which is the simplest of our examples, and for the other two, we’ll limit ourselves to the definition and a brief explanation.
Let’s start by recapping the Maybe monad, as seen in a previous post.
Review of the Maybe monad
The Maybe data type can be defined as follows:
The implementation of the monad interface is the following:
Remembering, return
wraps an element in the monad and >>=
is the bind operator, which takes an element wrapped in a monad, extracts it, applies a function f
, which returns another element wrapped in the monad.
The MaybeT data type
Monads can contain other monads and one particular useful combination is of a monad containing the Maybe monad. While we can accomplish that by regular use of monads, we can also avoid some boilerplate code by having a special type that encodes this combination, in this case the MaybeT
data type.
We can think of MaybeT
data type as a 3-layer wrapping. The inner layer being the Maybe
monad, then a generic monad and then the actual MaybeT
wrapper.
MaybeT is a Monad
MaybeT is also a monad. One possible implementation is:
Let’s break in parts:
(1) return = MaybeT . return . Just
The first part is Just
, which encapsulates the inner element in the Maybe
monad. The second return
encapsulates in the generic monad m
and finally we encapsulate in the MaybeT
monad.
(2)
The type signature is given by:
(>>=) :: MaybeT m a -> (a -> MaybeT m b) -> MaybeT m b
The bind operation has to do the opposite operation first, that is,
de-encapsulate the three layers of monads before running f
on it.
Then, we need to encapsulate into Maybe
, m
and MaybeT
again
Alternatively, we can use the chained notation:
Let’s review the list []
monad, as we saw in a previous post.
The idea is very similar to the Maybe
monad, we wrap the list in two other layers, the intermediate one being a generic monad. The implementation of the monad class type is essentially the same, except that, again, we have to do extra wraps and unwraps:
We’ve talked about the State monad before. It can be defined in the following way:
And the monad implementation is given by:
The idea of combining it with another generic monad and wrapping it, leads to a analogous idea to the List
/ListT
classes. Let’s define the StateT
class:
In this case, the generic monad wraps the result of the previous function s -> (a, s)
. The monad implementation is similar to the State
one, except that we have to take into account the extra layer:
For the return definition, we wrap (a, s)
in using the m
monad, by the use of return function of m
.
The bind operator, x
applied to s
will return m (a, s)
. We extract if from m by using the <-
operator and then run the function on a
, but since (f v)
returns the result wrapped in StateT
, we need to extract it using runStateT
, and finally wrap into m
again and then into StateT
.
All the monad transformers can implement the MonadTrans
interface, which basically defines the function lift
.
Lift is a generic version of liftM
, in a sense it allows a function that only applies to the inner element to be applicable to the top-level monad. The implementation of the MaybeT
monad is the following:
After understand better the concept of Monad transformers, Chapter 18 from Real World Haskell [2] becomes easier to digest and it’s quite interesting.
At one point, it discusses the real power of combining multiple monad transformers (for example, the generic monad in MaybeT
could be another monad transformer, say WriterT
). This “equips” a given data type with traits corresponding to the underlying monads (in the example the optional nature of Maybe
and the logging capabilities of the Writer
monad).