Notes on programming and computer science

Menu

Tag Archives: monads

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.

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.

The ListT monad

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:

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.

The MonadTrans interface

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:

instance MonadTrans MaybeT where
lift = MaybeT . (liftM Just)

Stack of monads

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).