The monad typeclass is best understood by looking at from many perspectives. That is why this book has no definitive section or chapter on monad. Instead, we will successively peel of the layers of this abstraction and make good use of it.
Let's begin by looking at a simple example of interpreting expressions:
The eval
function interprets expressions written in our Expr
data type:
Stripped of real-world concerns, this is very elegant. Now let's add (naive) capability to deal with errors in our interpreter. Instead of the eval
function returning integers, we'll return a Try
data type, which caters for success (Return
) and failure (Error
):
The refactored evalTry
function is now much more syntactically noisy with case statements:
The reason for the noise is that we have to explicitly propagate errors. If (evalTry a
) fails, we return Err
and bypass evaluation of the second argument.
We've used the Try
data type to make failure more explicit, but it has come at a cost. This is precisely where monads come into play. Let's make our Try
data type a monad:
Next, we refactor evalTry
by using the bind
operator (>>=
):
The bind operator enables error propagation:
Whenever we have an Err
function, the subsequent part of the bind chain will be ignored and will thereby propagate the error. While this gets rid of our case statements, it is hardly very friendly. Let's rewrite it using the do
notation:
The Try
data type helped us make failure more explicit, while making it a monad made it easier to work with. In this same way, monads can be used to make many other "effects" more explicit.
| "Being explicit about effects is extremely useful, and this is something that we believe may ultimately be seen as one of Haskell's main impacts on mainstream programming" | |
| --History of Haskell, Hudak et al. |
The IO monad is particularly interesting and played an important role in the development of Haskell. When Haskell was first conceived, there were no monads and also no clear solution to the "problem of side effects". In 1989, Eugenio Moggi used monads, from Category theory, to describe programming language features. Phillip Wadler, a then member of the Haskell Committee, recognized that it was possible to express Moggi's ideas in Haskell code:
| "Although Wadler's development of Moggi's ideas was not directed towards the question of input/output, he and others at Glasgow soon realised that monads provided an ideal framework for I/O" | |
| --History of Haskell, Hudak et al |
Because Haskell is purely functional, side effects call for special treatment. We will devote a whole chapter to exploring this topic in Chapter 2, Patterns for I/O.
Composing monads and structuring programs
As useful as monads are for capturing effects, we also need to compose them, for example, how do we use monads for failure, I/O, and logging together?
In the same way that functional composition allows us to write more focused functions that can be combined together, monad composition allows us to create more focused monads, to be recombined in different ways. In Chapter 3: Patterns for Composition, we will explore monad transformers and how to create "monad stacks" of transformers to achieve monad composition.