The happy outcome is that common sense programming practice has led us to create a monad without even realizing it. The <code>Maybe</code> type constructor along with the <code>Just</code> function (acts like <code>return</code>) and our combinator (acts like <code>>>=</code>) together form a simple monad for building computations which may not return a value. All that remains to make this monad truly useful is to make it conform to the monad framework built into the Haskell language. That is the subject of the next chapter.

The happy outcome is that common sense programming practice has led us to create a monad without even realizing it. The <code>Maybe</code> type constructor along with the <code>Just</code> function (acts like <code>return</code>) and our combinator (acts like <code>>>=</code>) together form a simple monad for building computations which may not return a value. All that remains to make this monad truly useful is to make it conform to the monad framework built into the Haskell language. That is the subject of the next chapter.

−

== A list is also a monad ==

+

== List is also a monad ==

We have seen that the <code>Maybe</code> type constructor is a monad for building computations which may fail to return a value. You may be surprised to know that another common Haskell type constructor, <code>[]</code> (for building lists), is also a monad. The List monad allows us to build computations which can return 0, 1, or more values.

We have seen that the <code>Maybe</code> type constructor is a monad for building computations which may fail to return a value. You may be surprised to know that another common Haskell type constructor, <code>[]</code> (for building lists), is also a monad. The List monad allows us to build computations which can return 0, 1, or more values.

Revision as of 00:21, 16 May 2014

All About Monads is a tutorial on monads and monad transformers and a walk-through of common monad instances. You can download a PDF version here or here. And here is a version of the article which includes source code.

Attempts are being made at porting the tutorial to this wiki; what you're seeing below is a preview of the result of that effort. If you wish to help out you should fork this GitHub repo rather than edit this page, for now.

1 Introduction

1.1 What is a monad?

A monad is a way to structure computations in terms of values and sequences of computations using those values. Monads allow the programmer to build up computations using sequential building blocks, which can themselves be sequences of computations. The monad determines how combined computations form a new computation and frees the programmer from having to code the combination manually each time it is required.

It is useful to think of a monad as a strategy for combining computations into more complex computations. For example, you should be familiar with the Maybe type in Haskell:

dataMaybe a = Nothing | Just a

which represents the type of computations which may fail to return a result. The Maybe type suggests a strategy for combining computations which return Maybe values: if a combined computation consists of one computation B that depends on the result of another computation A, then the combined computation should yield Nothing whenever either A or B yield Nothing and the combined computation should yield the result of B applied to the result of A when both computations succeed.

Other monads exist for building computations that perform I/O, have state, may return multiple results, etc. There are as many different type of monads as there are strategies for combining computations, but there are certain monads that are especially useful and are common enough that they are part of the standard Haskell 98 libraries. These monads are each described in Part II.

1.2 Why should I make the effort to understand monads?

The sheer number of different monad tutorials on the internet is a good indication of the difficulty many people have understanding the concept. This is due to the abstract nature of monads and to the fact that they are used in several different capacities, which can confuse the picture of exactly what a monad is and what it is good for.

In Haskell, monads play a central role in the I/O system. It is not essential to understand monads to do I/O in Haskell, but understanding the I/O monad will improve your code and extend your capabilities.

For the programmer, monads are useful tools for structuring functional programs. They have three properties that make them especially useful:

Modularity - They allow computations to be composed from simpler computations and separate the combination strategy from the actual computations being performed.

Flexibility - They allow functional programs to be much more adaptable than equivalent programs written without monads. This is because the monad distills the computational strategy into a single place instead of requiring it be distributed throughout the entire program.

Isolation - They can be used to create imperative-style computational structures which remain safely isolated from the main body of the functional program. This is useful for incorporating side-effects (such as I/O) and state (which violates referential transparency) into a pure functional language like Haskell.

Each of these features will be revisited later in the tutorial in the context of specific monads.

Meet the Monads

2 Meet the Monads

We will use the Maybe type constructor throughout this chapter, so you should familiarize yourself with the definition and usage of Maybe before continuing.

2.1 Type constructors

To understand monads in Haskell, you need to be comfortable dealing with type constructors. A type constructor is a parameterized type definition used with polymorphic types. By supplying a type constructor with one or more concrete types, you can construct a new concrete type in Haskell. In the definition of Maybe:

dataMaybe a = Nothing | Just a

Maybe is a type constructor and Nothing and Just are data constructors. You can construct a data value by applying the Just data constructor to a value:

country = Just "China"

In the same way, you can construct a type by applying the Maybe type constructor to a type:

lookupAge :: DB ->String->MaybeInt

Polymorphic types are like containers that are capable of holding values of many different types. So Maybe Int can be thought of as a Maybe container holding an Int value (or Nothing) and Maybe String would be a Maybe container holding a String value (or Nothing). In Haskell, we can also make the type of the container polymorphic, so we could write "m a" to represent a container of some type holding a value of some type!

We often use type variables with type constructors to describe abstract features of a computation. For example, the polymorphic type Maybe a is the type of all computations that may return a value or Nothing. In this way, we can talk about the properties of the container apart from any details of what the container might hold.

File:Info.png If you get messages about "kind errors" from the compiler when working with monads, it means that you are not using the type constructors correctly.

2.2 Maybe a monad

In Haskell a monad is represented as a type constructor (call it m), a function that builds values of that type (a -> m a), and a function that combines values of that type with computations that produce values of that type to produce a new computation for values of that type (m a -> (a -> m b) -> m b). Note that the container is the same, but the type of the contents of the container can change. It is customary to call the monad type constructor "m" when discussing monads in general. The function that builds values of that type is traditionally called "return" and the third function is known as "bind" but is written ">>=". The signatures of the functions are:

-- the type of monad mdata m a =...-- return is a type constructor that creates monad instances return:: a -> m a
-- bind is a function that combines a monad instance m a with a computation-- that produces another monad instance m b from a's to produce a new-- monad instance m b(>>=):: m a ->(a -> m b)-> m b

Roughly speaking, the monad type constructor defines a type of computation, the return function creates primitive values of that computation type and >>= combines computations of that type together to make more complex computations of that type. Using the container analogy, the type constructor m is a container that can hold different values. m a is a container holding a value of type a. The return function puts a value into a monad container. The >>= function takes the value from a monad container and passes it to a function to produce a monad container containing a new value, possibly of a different type. The >>= function is known as "bind" because it binds the value in a monad container to the first argument of a function. By adding logic to the binding function, a monad can implement a specific strategy for combining computations in the monad.

This will all become clearer after the example below, but if you feel particularly confused at this point you might try looking at this physical analogy of a monad before continuing.

2.3 An example

Suppose that we are writing a program to keep track of sheep cloning experiments. We would certainly want to know the genetic history of all of our sheep, so we would need mother and father functions. But since these are cloned sheep, they may not always have both a mother and a father!

We would represent the possibility of not having a mother or father using the Maybe type constructor in our Haskell code:

Aside from being ugly, unclear, and difficult to maintain, this is just too much work. It is clear that a Nothing value at any point in the computation will cause Nothing to be the final result, and it would be much nicer to implement this notion once in a single place and remove all of the explicit case testing scattered all over the code. This will make the code easier to write, easier to read and easier to change. So good programming style would have us create a combinator that captures the behavior we want:

The combinator is a huge success! The code is much cleaner and easier to write, understand and modify. Notice also that the comb function is entirely polymorphic — it is not specialized for Sheep in any way. In fact, the combinator captures a general strategy for combining computations that may fail to return a value. Thus, we can apply the same combinator to other computations that may fail to return a value, such as database queries or dictionary lookups.

The happy outcome is that common sense programming practice has led us to create a monad without even realizing it. The Maybe type constructor along with the Just function (acts like return) and our combinator (acts like >>=) together form a simple monad for building computations which may not return a value. All that remains to make this monad truly useful is to make it conform to the monad framework built into the Haskell language. That is the subject of the next chapter.

2.4 List is also a monad

We have seen that the Maybe type constructor is a monad for building computations which may fail to return a value. You may be surprised to know that another common Haskell type constructor, [] (for building lists), is also a monad. The List monad allows us to build computations which can return 0, 1, or more values.

The return function for lists simply creates a singleton list (return x = [x]). The binding operation for lists creates a new list containing the results of applying the function to all of the values in the original list (l >>= f = concatMap f l).

One use of functions which return lists is to represent ambiguous computations — that is computations which may have 0, 1, or more allowed outcomes. In a computation composed from ambigous subcomputations, the ambiguity may compound, or it may eventually resolve into a single allowed outcome or no allowed outcome at all. During this process, the set of possible computational states is represented as a list. The List monad thus embodies a strategy for performing simultaneous computations along all allowed paths of an ambiguous computation.

Examples of this use of the List monad, and contrasting examples using the Maybe monad will be presented shortly. But first, we must see how useful monads are defined in Haskell.

2.5 Summary

We have seen that a monad is a type constructor, a function called return, and a combinator function called bind or >>=. These three elements work together to encapsulate a strategy for combining computations to produce more complex computations.

Using the Maybe type constructor, we saw how good programming practice led us to define a simple monad that could be used to build complex computations out of sequences of computations that could each fail to return a value. The resulting Maybe monad encapsulates a strategy for combining computations that may not return values. By codifying the strategy in a monad, we have achieved a degree of modularity and flexibility that is not present when the computations are combined in an ad hoc manner.

We have also seen that another common Haskell type constructor, [], is a monad. The List monad encapsulates a strategy for combining computations that can return 0, 1, or multiple values.

Doing it with class

3 Doing it with class

3.1 Haskell type classes

The discussion in this chapter involves the Haskell type class system. If you are not familiar with type classes in Haskell, you should review them before continuing.

3.2 The Monad class

In Haskell, there is a standard Monad class that defines the names and signatures of the two monad functions return and >>=. It is not strictly necessary to make your monads instances of the Monad class, but it is a good idea. Haskell has special support for Monad instances built into the language and making your monads instances of the Monad class will allow you to use these features to write cleaner and more elegant code. Also, making your monads instances of the Monad class communicates important information to others who read the code and failing to do so can cause you to use confusing and non-standard function names. It's easy to do and it has many benefits, so just do it!

3.3 Example continued

Continuing the previous example, we will now see how the Maybe type constructor fits into the Haskell monad framework as an instance of the Monad class.

Recall that our Maybe monad used the Just data constructor to fill the role of the monad return function and we built a simple combinator to fill the role of the monad >>= binding function. We can make its role as a monad explicit by declaring Maybe as an instance of the Monad class:

In Haskell, Maybe is defined as an instance of the Monad class in the standard prelude, so you don't need to do it yourself. The other monad we have seen so far, the list constructor, is also defined as an instance of the Monad class in the standard prelude.

File:Info.png When writing functions that work with monads, try to make use of the Monad class instead of using a specific monad instance. A function of the type

doSomething ::(Monad m)=> a -> m b

is much more flexible than one of the type

doSomething :: a ->Maybe b

The former function can be used with many types of monads to get different behavior depending on the strategy embodied in the monad, whereas the latter function is restricted to the strategy of the Maybe monad.

3.4 Do notation

Using the standard monadic function names is good, but another advantage of membership in the Monad class is the Haskell support for "do" notation. Do notation is an expressive shorthand for building up monadic computations, similar to the way that list comprehensions are an expressive shorthand for building computations on lists. Any instance of the Monad class can be used in a do-block in Haskell.

In short, the do notation allows you to write monadic computations using a pseudo-imperative style with named variables. The result of a monadic computation can be "assigned" to a variable using a left arrow <- operator. Then using that variable in a subsequent monadic computation automatically performs the binding. The type of the expression to the right of the arrow is a monadic type m a. The expression to the left of the arrow is a pattern to be matched against the value inside the monad. (x:xs) would match against Maybe [1,2,3], for example.

Notice that do notation resembles an imperative programming language, in which a computation is built up from an explicit sequence of simpler computations. In this respect, monads offer the possibility to create imperative-style computations within a larger functional program. This theme will be expanded upon when we deal with side-effects and the I/O monad later.

Do notation is simply syntactic sugar. There is nothing that can be done using do notation that cannot be done using only the standard monadic operators. But do notation is cleaner and more convenient in some cases, especially when the sequence of monadic computations is long. You should understand both the standard monadic binding notation and do notation and be able to apply each where they are appropriate.

The actual translation from do notation to standard monadic operators is roughly that every expression matched to a pattern, x <- expr1, becomes

expr1 >>= \x ->

and every expression without a variable assignment, expr2 becomes

expr2 >>= \_->

All do blocks must end with a monadic expression, and a let clause is allowed at the beginning of a do block (but let clauses in do blocks do not use the "in" keyword). The definition of mothersPaternalGrandfather above would be translated to:

It now becomes clear why the binding operator is so named. It is literally used to bind the value in the monad to the argument in the following lambda expression.

3.5 Summary

Haskell provides built-in support for monads. To take advantage of Haskell's monad support, you must declare the monad type constructor to be an instance of the Monad class and supply definitions of the return and >>= (pronounced "bind") functions for the monad.

A monad that is an instance of the Monad class can be used with do-notation, which is syntactic sugar that provides a simple, imperative-style notation for describing computations with monads.

The monad laws

4 The monad laws

The tutorial up to now has avoided technical discussions, but there are a few technical points that must be made concerning monads. Monadic operations must obey a set of laws, known as "the monad axioms". These laws aren't enforced by the Haskell compiler, so it is up to the programmer to ensure that any Monad instances they declare obey the laws. Haskell's Monad class also includes some functions beyond the minimal complete definition that we have not seen yet. Finally, many monads obey additional laws beyond the standard monad laws, and there is an additional Haskell class to support these extended monads.

4.1 The three fundamental laws

The concept of a monad comes from a branch of mathematics called category theory. While it is not necessary to know category theory to create and use monads, we do need to obey a small bit of mathematical formalism. To create a monad, it is not enough just to declare a Haskell instance of the Monad class with the correct type signatures. To be a proper monad, the return and >>= functions must work together according to three laws:

(return x) >>= f == f x

m >>= return == m

(m >>= f) >>= g == m >>= (\x -> f x >>= g)

The first law requires that return is a left-identity with respect to >>=. The second law requires that return is a right-identity with respect to >>=. The third law is a kind of associativity law for >>=. Obeying the three laws ensures that the semantics of the do-notation using the monad will be consistent.

Any type constructor with return and bind operators that satisfy the three monad laws is a monad. In Haskell, the compiler does not check that the laws hold for every instance of the Monad class. It is up to the programmer to ensure that any Monad instance they create satisfies the monad laws.

4.2 Failure IS an option

The definition of the Monad class given earlier showed only the minimal complete definition. The full definition of the Monad class actually includes two additional functions: fail and >>.

The default implementation of the fail function is:

fail s =error s

You do not need to change this for your monad unless you want to provide different behavior for failure or to incorporate failure into the computational strategy of your monad. The Maybe monad, for instance, defines fail as:

fail_= Nothing

so that fail returns an instance of the Maybe monad with meaningful behavior when it is bound with other functions in the Maybe monad.

The fail function is not a required part of the mathematical definition of a monad, but it is included in the standard Monad class definition because of the role it plays in Haskell's do notation. The fail function is called whenever a pattern matching failure occurs in a do block:

So in the code above, fn 0 has the value Just [2,3], but fn 1 and fn 2 both have the value Nothing.

The >> function is a convenience operator that is used to bind a monadic computation that does not require input from the previous computation in the sequence. It is defined in terms of >>=:

(>>):: m a -> m b -> m b
m >> k = m >>=(\_-> k)

4.3 No way out

You might have noticed that there is no way to get values out of a monad as defined in the standard Monad class. That is not an accident. Nothing prevents the monad author from allowing it using functions specific to the monad. For instance, values can be extracted from the Maybe monad by pattern matching on Just x or using the fromJust function.

By not requiring such a function, the Haskell Monad class allows the creation of one-way monads. One-way monads allow values to enter the monad through the return function (and sometimes the fail function) and they allow computations to be performed within the monad using the bind functions >>= and >>, but they do not allow values back out of the monad.

The IO monad is a familiar example of a one-way monad in Haskell. Because you can't escape from the IO monad, it is impossible to write a function that does a computation in the IO monad but whose result type does not include the IO type constructor. This means that any function whose result type does not contain the IO type constructor is guaranteed not to use the IO monad. Other monads, such as List and Maybe, do allow values out of the monad. So it is possible to write functions which use these monads internally but return non-monadic values.

The wonderful feature of a one-way monad is that it can support side-effects in its monadic operations but prevent them from destroying the functional properties of the non-monadic portions of the program.

Consider the simple issue of reading a character from the user. We cannot simply have a function readChar :: Char, because it needs to return a different character each time it is called, depending on the input from the user. It is an essential property of Haskell as a pure functional language that all functions return the same value when called twice with the same arguments. But it is ok to have an I/O function getChar :: IO Char in the IO monad, because it can only be used in a sequence within the one-way monad. There is no way to get rid of the IO type constructor in the signature of any function that uses it, so the IO type constructor acts as a kind of tag that identifies all functions that do I/O. Furthermore, such functions are only useful within the IO monad. So a one-way monad effectively creates an isolated computational domain in which the rules of a pure functional language can be relaxed. Functional computations can move into the domain, but dangerous side-effects and non-referentially-transparent functions cannot escape from it.

Another common pattern when defining monads is to represent monadic values as functions. Then when the value of a monadic computation is required, the resulting monad is "run" to provide the answer.

4.4 Zero and Plus

Beyond the three monad laws stated above, some monads obey additional laws. The monads have a special value mzero and an operator mplus that obey four additional laws:

mzero >>= f == mzero

m >>= (\x -> mzero) == mzero

mzero `mplus` m == m

m `mplus` mzero == m

It is easy to remember the laws for mzero and mplus if you associate mzero with 0, mplus with +, and >>= with × in ordinary arithmetic.

Monads which have a zero and a plus can be declared as instances of the MonadPlus class in Haskell:

This identifies Nothing as the zero value and says that adding two Maybe values together gives the first value that is not Nothing. If both input values are Nothing, then the result of mplus is also Nothing.

The List monad also has a zero and a plus. mzero is the empty list and mplus is the ++ operator.

The mplus operator is used to combine monadic values from separate computations into a single monadic value. Within the context of our sheep-cloning example, we could use Maybe's mplus to define a function, parent s = (mother s) `mplus` (father s), which would return a parent if there is one, and Nothing is the sheep has no parents at all. For a sheep with both parents, the function would return one or the other, depending on the exact definition of mplus in the Maybe monad.

4.5 Summary

Instances of the Monad class should conform to the so-called monad laws, which describe algabraic properties of monads. There are three of these laws which state that the return function is both a left and a right identity and that the binding operator is associative. Failure to satisfy these laws will result in monads that do not behave properly and may cause subtle problems when using do-notation.

In addition to the return and >>= functions, the Monad class defines another function, fail. The fail function is not a technical requirement for inclusion as a monad, but it is often useful in practice and it is included in the Monad class because it is used in Haskell's do-notation.

Some monads obey laws beyond the three basic monad laws. An important class of such monads are ones which have a notion of a zero element and a plus operator. Haskell provides a MonadPlus class for such monads which define the mzero value and the mplus operator.

Exercises

5 Exercises

This section contains a few simple exercises to hone the reader's monadic reasoning skills and to provide a solid comprehension of the function and use of the Maybe and List monads before looking at monadic programming in more depth. The exercises will build on the previous sheep-cloning [[../examples/example2.hs|example]], with which the reader should already be familiar.

5.1 Exercise 1: Do notation

Rewrite the maternalGrandfather, fathersMaternalGrandmother, and mothersPaternalGrandfather functions in [[../examples/example2.hs|Example 2]] using the monadic operators return and >>=, without using any do-notation syntactic sugar.

5.2 Exercise 2: Combining monadic values

Write functions parent and grandparent with signature Sheep -> Maybe Sheep. They should return one sheep selected from all sheep matching the description, or Nothing if there is no such sheep. Hint: the mplus operator is useful here.

5.3 Exercise 3: Using the List monad

Write functions parent and grandparent with signature Sheep -> [Sheep]. They should return all sheep matching the description, or the empty list if there is no such sheep. Hint: the mplus operator in the List monad is useful here. Also the maybeToList function in the Maybe module can be used to convert a value from the Maybe monad to the List monad.

5.4 Exercise 4: Using the Monad class constraint

Monads promote modularity and code reuse by encapsulating often-used computational strategies into single blocks of code that can be used to construct many different computations. Less obviously, monads also promote modularity by allowing you to vary the monad in which a computation is done to achieve different variations of the computation. This is achieved by writing functions which are polymorphic in the monad type constructor, using the (Monad m) =>, (MonadPlus m) =>, etc. class constraints.

Write functions parent and grandparent with signature (MonadPlus m) => Sheep -> m Sheep. They should be useful in both the Maybe and List monads. How does the functions' behavior differ when used with the List monad versus the Maybe monad? If you need to review the use of type classes and class constraints in Haskell, look here.

6 Monad support in Haskell

Haskell's built in support for monads is split among the standard prelude, which exports the most common monad functions, and the Monad module, which contains less-commonly used monad functions. The individual monad types are each in their own libraries and are the subject of Part II of this tutorial.

6.1 In the standard prelude

The Haskell 98 standard prelude includes the definition of the Monad class as well as a few auxilliary functions for working with monadic data types.

The sequence_ function (notice the underscore) has the same behavior as sequence but does not return a list of results. It is useful when only the side-effects of the monadic computations are important.

sequence_::Monad m =>[m a]-> m ()sequence_=foldr(>>)(return())

6.1.3 The mapping functions

The mapM function maps a monadic computation over a list of values and returns a list of the results. It is defined in terms of the list map function and the sequence function above:

mapM::Monad m =>(a -> m b)->[a]-> m [b]mapM f as=sequence(map f as)

There is also a version with an underscore, mapM_ which is defined using sequence_. mapM_ operates the same as mapM, but it doesn't return the list of values. It is useful when only the side-effects of the monadic computation are important.

mapM_::Monad m =>(a -> m b)->[a]-> m ()mapM_ f as=sequence_(map f as)

As a simple example of the use the mapping functions, a putString function for the IO monad could be defined as:

putString ::[Char]->IO()
putString s =mapM_putChar s

mapM can be used within a do block in a manner similar to the way the map function is normally used on lists. This is a common pattern with monads — a version of a function for use within a monad (i.e., intended for binding) will have a signature similar to the non-monadic version but the function outputs will be within the monad:

6.1.4 The reverse binder function (=<<)

The prelude also defines a binding function that takes it arguments in the opposite order to the standard binding function. Since the standard binding function is called ">>=", the reverse binding function is called "=<<". It is useful in circumstances where the binding operator is used as a higher-order term and it is more convenient to have the arguments in the reversed order. Its definition is simply:

(=<<)::Monad m =>(a -> m b)-> m a -> m b
f =<< x = x >>= f

6.2 In the Monad module

The Monad module in the standard Haskell 98 libraries exports a number of facilities for more advanced monadic operations. To access these facilities, simply import Monad in your Haskell program.

Not all of the function in the Monad module are discussed here, but you are encouraged to explore the module for yourself when you feel you are ready to see some of the more esoteric monad functions.

6.2.1 The MonadPlus class

The Monad module defines the MonadPlus class for monads with a zero element and a plus operator:

The traceFamily function uses foldM to create a simple way to trace back in the family tree to any depth and in any pattern. In fact, it is probably clearer to write "traceFamily s [father, mother]" than it is to use the paternalGrandmother function!

The filterM function works like the list filter function inside of a monad. It takes a predicate function which returns a Boolean value in the monad and a list of values. It returns, inside the monad, a list of those values for which the predicate was True.

Here is an example showing how filterM can be used within the IO monad to select only the directories from a list:

Code available in [[../examples/example5.hs|example5.hs]]

importMonadimport Directory
import System
-- NOTE: doesDirectoryExist has type FilePath -> IO Bool-- this program prints only the directories named on the command line
main ::IO()
main =do names <- getArgs
dirs <- filterM doesDirectoryExist names
mapM_putStrLn dirs

zipWithM is a monadic version of the zipWith function on lists. zipWithM_ behaves the same but discards the output of the function. It is useful when only the side-effects of the monadic computation matter.

6.2.3 Conditional monadic computations

There are two functions provided for conditionally executing monadic computations. The when function takes a boolean argument and a monadic computation with unit "()" type and performs the computation only when the boolean argument is True. The unless function does the same, except that it performs the computation unless the boolean argument is True.

6.2.4 ap and the lifting functions

Lifting is a monadic operation that converts a non-monadic function into an equivalent function that operates on monadic values. We say that a function is "lifted into the monad" by the lifting operators. A lifted function is useful for operating on monad values outside of a do block and can also allow for cleaner code within a do block.

The simplest lifting operator is liftM, which lifts a function of a single argument into a monad.

The difference is even greater when lifting functions with more arguments.

The lifting functions also enable very concise constructions using higher-order functions. To understand this example code, you might need to review the definition of the monad functions for the List monad (particularly >>=). Imagine how you might implement this function without lifting the operator:

Code available in [[../examples/example7.hs|example7.hs]]

-- allCombinations returns a list containing the result of-- folding the binary operator through all combinations-- of elements of the given lists-- For example, allCombinations (+) [[0,1],[1,2,3]] would be-- [0+1,0+2,0+3,1+1,1+2,1+3], or [1,2,3,2,3,4]-- and allCombinations (*) [[0,1],[1,2],[3,5]] would be-- [0*1*3,0*1*5,0*2*3,0*2*5,1*1*3,1*1*5,1*2*3,1*2*5], or [0,0,0,0,3,5,6,10]
allCombinations ::(a -> a -> a)->[[a]]->[a]
allCombinations fn []=[]
allCombinations fn (l:ls)=foldl(liftM2 fn) l ls

There is a related function called ap that is sometimes more convenient to use than the lifting functions. ap is simply the function application operator ($) lifted into the monad:

ap ::(Monad m)=> m (a -> b)-> m a -> m b
ap = liftM2 ($)

Note that liftM2 f x y is equivalent to return f `ap` x `ap` y, and so on for functions of more arguments. ap is useful when working with higher-order functions and monads.

The effect of ap depends on the strategy of the monad in which it is used. So for example [(*2),(+3)] `ap` [0,1,2] is equal to [0,2,4,3,4,5] and (Just (*2)) `ap` (Just 3) is Just 6. Here is a simple example that shows how ap can be useful when doing higher-order computations:

6.2.5 Functions for use with MonadPlus

There are two functions in the Monad module that are used with monads that have a zero and a plus. The first function is msum, which is analogous to the sum function on lists of integers. msum operates on lists of monadic values and folds the mplus operator into the list using the mzero element as the initial value:

msum :: MonadPlus m =>[m a]-> m a
msum xs =foldr mplus mzero xs

In the List monad, msum is equivalent to concat. In the Maybe monad, msum returns the first non-Nothing value from a list. Likewise, the behavior in other monads will depend on the exact nature of their mzero and mplus definitions.

msum allows many recursive functions and folds to be expressed more concisely. In the Maybe monad, for example, we can write:

The trick to understanding this function is to recall the law for monads with zero and plus that states mzero >>= f == mzero. So, placing a guard function in a sequence of monadic operations will force any execution in which the guard is False to be mzero. This is similar to the way that guard predicates in a list comprehension cause values that fail the predicate to become [].

Here is an example demonstrating the use of the guard function in the Maybe monad.

Code available in [[../examples/example10.hs|example10.hs]]

data Record = Rec {name::String, age::Int}derivingShowtype DB =[Record]-- getYoungerThan returns all records for people younger than a specified age.-- It uses the guard function to eliminate records for ages at or over the limit.-- This is just for demonstration purposes. In real life, it would be-- clearer to simply use filter. When the filter criteria are more complex,-- guard becomes more useful.
getYoungerThan ::Int-> DB ->[Record]
getYoungerThan limit db = mapMaybe (\r ->do{ guard (age r < limit); return r }) db

6.3 Summary

Haskell provides a number of functions which are useful for working with monads in the standard libraries. The Monad class and most common monad functions are in the standard prelude. The MonadPlus class and less commonly-used (but still very useful!) functions are defined in the Monad module. Many other types in the Haskell libraries are declared as instances of Monad and MonadPlus in their respective modules.

Part II - Introduction

7 Introduction

The monads covered in Part II include a mixture of standard Haskell types that are monads as well as monad classes from Andy Gill's Monad Template Library. The Monad Template Library is included in the Glasgow Haskell Compiler's hierarchical libraries under Control.Monad

Some of the documentation for these monads comes from the excellent Haskell Wiki. In addition to the monads covered here, monads appear many other places in Haskell, such as the Parsec monadic combinator parsing library. These monads are beyond the scope of this reference, but they are thoroughly documented on their own. You can get a taste of the Parsec library by looking in the [[../examples/example16.hs|source code]] for example 16.

8.2 Motivation

The Identity monad is a monad that does not embody any computational strategy. It simply applies the bound function to its input without any modification. Computationally, there is no reason to use the Identity monad instead of the much simpler act of simply applying functions to their arguments. The purpose of the Identity monad is its fundamental role in the theory of monad transformers (covered in Part III). Any monad transformer applied to the Identity monad yields a non-transformer version of that monad.

8.3 Definition

The runIdentity label is used in the type definition because it follows a style of monad definition that explicitly represents monad values as computations. In this style, a monadic computation is built up using the monadic operators and then the value of the computation is extracted using the run****** function. Because the Identity monad does not do any computation, its definition is trivial. For a better example of this style of monad, see the State monad.

8.4 Example

A typical use of the Identity monad is to derive a monad from a monad transformer.

-- derive the State monad using the StateT monad transformertype State s a = StateT s Identity a

The Maybe monad

9 The Maybe monad

9.1 Overview

Computation type:

Computations which may return Nothing

Binding strategy:

Nothing values bypass the bound function, other values are used as inputs to the bound function.

Useful for:

Building computations from sequences of functions that may return Nothing. Complex database queries or dictionary lookups are good examples.

Zero and plus:

Nothing is the zero. The plus operation returns the first non-Nothing value or Nothing if both inputs are Nothing.

9.2 Motivation

The Maybe monad embodies the strategy of combining a chain of computations that may each return Nothing by ending the chain early if any step produces Nothing as output. It is useful when a computation entails a sequence of steps that depend on one another, and in which some steps may fail to return a value.

9.4 Example

A common example is in combining dictionary lookups. Given a dictionary that maps full names to email addresses, another that maps nicknames to email addresses, and a third that maps email addresses to email preferences, you could create a function that finds a person's email preferences based on either a full name or a nickname.

10.2 Motivation

The Error monad (also called the Exception monad) embodies the strategy of combining computations that can throw exceptions by bypassing bound functions from the point an exception is thrown to the point that it is handled.

The MonadError class is parameterized over the type of error information and the monad type constructor. It is common to use Either String as the monad type constructor for an error monad in which error descriptions take the form of strings. In that case and many other common cases the resulting monad is already defined as an instance of the MonadError class. You can also define your own error type and/or use a monad type constructor other than Either String or Either IOError. In these cases you will have to explicitly define instances of the Error and/or MonadError classes.

10.3 Definition

The definition of the MonadError class below uses multi-parameter type classes and funDeps, which are language extensions not found in standard Haskell 98. You don't need to understand them to take advantage of the MonadError class.

throwError is used within a monadic computation to begin exception processing. catchError provides a handler function to handle previous errors and return to normal execution. A common idiom is:

do{ action1; action2; action3 } `catchError` handler

where the action functions can call throwError. Note that handler and the do-block must have the same return type.

The definition of the Either e type constructor as an instance of the MonadError class is straightforward. Following convention, Left is used for error values and Right is used for non-error (right) values.

10.4 Example

Here is an example that demonstrates the use of a custom Error data type with the ErrorMonad's throwError and catchError exception mechanism. The example attempts to parse hexadecimal numbers and throws an exception if an invalid character is encountered. We use a custom Error data type to record the location of the parse error. The exception is caught by a calling function and handled by printing an informative error message.

11 The List monad

11.1 Overview

The bound function is applied to all possible values in the input list and the resulting lists are concatenated to produce a list of all possible results.

Useful for:

Building computations from sequences of non-deterministic operations. Parsing ambiguous grammars is a common example.

Zero and plus:

[] is the zero and ++ is the plus operation.

Example type:

[a]

11.2 Motivation

The List monad embodies the strategy of combining a chain of non-deterministic computations by applying the operations to all possible values at each step. It is useful when computations must deal with ambiguity. In that case it allows all possibilities to be explored until the ambiguity is resolved.

11.3 Definition

11.4 Example

The canonical example of using the List monad is for parsing ambiguous grammars. The example below shows just a small example of parsing data into hex values, decimal values and words containing only alpha-numeric characters. Note that hexadecimal digits overlap both decimal digits and alphanumeric characters, leading to an ambiguous grammar. "dead" is both a valid hex value and a word, for example, and "10" is both a decimal value of 10 and a hex value of 16.

12.2 Motivation

Input/Output is incompatible with a pure functional language because it is not referentially transparent and side-effect free. The IO monad solves this problem by confining computations that perform I/O within the IO monad.

12.3 Definition

The definition of the IO monad is platform-specific. No data constructors are exported and no functions are provided to remove data from the IO monad. This makes the IO monad a one-way monad and is essential to ensuring safety of functional programs by isolating side-effects and non-referentially transparent actions within the imperative-style computations of the IO monad.

Throughout this tutorial, we have referred to monadic values as computations. However, values in the IO monad are often called I/O actions and we will use that terminology here.

In Haskell, the top-level main function must have type IO (), so that programs are typically structured at the top level as an imperative-style sequence of I/O actions and calls to functional-style code. The functions exported from the IO module do not perform I/O themselves. They return I/O actions, which describe an I/O operation to be performed. The I/O actions are combined within the IO monad (in a purely functional manner) to create more complex I/O actions, resulting in the final I/O action that is the main value of the program.

The standard prelude and the IO module define many functions that can be used within the IO monad and any Haskell programmer will undoubtedly be familiar with some of them. This tutorial will only discuss the monadic aspects of the IO monad, not the full range of functions available to perform I/O.

The IO type constructor is a member of the Monad class and the MonadError class, where errors are of the type IOError. fail is defined to throw an error built from the string argument. Within the IO monad you can use the exception mechanisms from the Control.Monad.Error module in the Monad Template Library if you import the module. The same mechanisms have alternative names exported by the IO module: ioError and catch.

The IO module exports a convenience function called try that executes an I/O action and returns Right result if the action succeeded or Left IOError if an I/O error was caught.

12.4 Example

This example shows a partial implementation of the "tr" command that copies the standard input stream to the standard output stream with character translations controlled by command-line arguments. It demonstrates the use of the exception handling mechanisms of the MonadError class with the IO monad.

13.2 Motivation

A pure functional language cannot update values in place because it violates referential transparency. A common idiom to simulate such stateful computations is to "thread" a state parameter through a sequence of functions:

This approach works, but such code can be error-prone, messy and difficult to maintain. The State monad hides the threading of the state parameter inside the binding operation, simultaneously making the code easier to write, easier to read and easier to modify.

13.3 Definition

The definition shown here uses multi-parameter type classes and funDeps, which are not standard Haskell 98. It is not necessary to fully understand these details to make use of the State monad.

Values in the State monad are represented as transition functions from an initial state to a (value,newState) pair and a new type definition is provided to describe this construct: State s a is the type of a value of type a inside the State monad with state of type s.

The type constructor State s is an instance of the Monad class. The return function simply creates a state transition function which sets the value but leaves the state unchanged. The binding operator creates a state transition function that applies its right-hand argument to the value and new state from its left-hand argument.

The MonadState class provides a standard but very simple interface for State monads. The get function retrieves the state by copying it as the value. The put function sets the state of the monad and does not yield a value.

There are many additional functions provide which perform more complex computations built on top of get and put. The most useful one is gets which retrieves a function of the state. The others are listed in the documentation for the State monad library.

13.4 Example

A simple application of the State monad is to thread the random generator state through multiple calls to the generation function.

14.2 Motivation

Some programming problems require computations within a shared environment (such as a set of variable bindings). These computations typically read values from the environment and sometimes execute sub-computations in a modified environment (with new or shadowing bindings, for example), but they do not require the full generality of the State monad.

The Reader monad is specifically designed for these types of computations and is often a clearer and easier mechanism than using the State monad.

14.3 Definition

The definition shown here uses multi-parameter type classes and funDeps, which are not standard Haskell 98. It is not necessary to fully understand these details to make use of the Reader monad.

Values in the Reader monad are functions from an environment to a value. To extract the final value from a computation in the Reader monad, you simply apply (runReader reader) to an environment value.

The return function creates a Reader that ignores the environment and produces the given value. The binding operator produces a Reader that uses the environment to extract the value its left-hand side and then applies the bound function to that value in the same environment.

The MonadReader class provides a number of convenience functions that are very useful when working with a Reader monad. The ask function retrieves the environment and the local function executes a computation in a modified environment. The asks function is a convenience function that retrieves a function of the current environment, and is typically used with a selector or lookup function.

14.4 Example

Consider the problem of instantiating templates which contain variable substitutions and included templates. Using the Reader monad, we can maintain an environment of all known templates and all known variable bindings. Then, when a variable substitution is encountered, we can use the asks function to lookup the value of the variable. When a template is included with new variable definitions, we can use the local function to resolve the template in a modified environment that contains the additional variable bindings.

To use the Reader monad to resolve a template t into a String, you simply need to do runReader (resolve t) env.

The Writer monad

15 The Writer monad

15.1 Overview

Computation type:

Computations which produce a stream of data in addition to the computed values.

Binding strategy:

A Writer monad value is a (computation value, log value) pair. Binding replaces the computation value with the result of applying the bound function to the previous value and appends any log data from the computation to the existing log data.

15.2 Motivation

It is often desirable for a computation to generate output "on the side". Logging and tracing are the most common examples in which data is generated during a computation that we want to retain but is not the primary result of the computation.

Explicitly managing the logging or tracing data can clutter up the code and invite subtle bugs such as missed log entries. The Writer monad provides a cleaner way to manage the output without cluttering the main computation.

15.3 Definition

The definition shown here uses multi-parameter type classes and funDeps, which are not standard Haskell 98. It is not necessary to fully understand these details to make use of the Writer monad.

File:Info.png To fully understand this definition, you need to know about Haskell's Monoid class, which represents a mathematical structure called a monoid in the same way that the Monad class represents the monad structure.

The good news is that monoids are simpler than monads. A monoid is a set of objects, a single identity element, and an associative binary operator over the set of objects. A monoid must obey some mathematical laws, such that applying the operator to any values from the set gives another value in the set, and whenever one operand of the operator is the identity element the result is equal to the other operand. You may notice that these laws are the same as the laws governing mzero and mplus for instances of MonadPlus. That is because monads with a zero and plus are monads that are also monoids!

Some examples of mathematical monoids are the natural numbers with identity element 0 and binary operator for addition, and also the natural numbers with identity element 1 and binary operator for multiplication.

In Haskell, a monoid consists of a type, an identity element, and a binary operator. Haskell defines the Monoid class (in Data.Monoid) to provide a standard convention for working with monoids: the identity element is named mempty and the operator is named mappend.

The most commonly used standard monoid in Haskell is the list, but functions of type (a -> a) also form a monoid.

File:Warn.png Care should be taken when using a list as the monoid for a Writer, as there may be a performance penalty associated with the mappend operation as the output grows. In that case, a data structure that supports fast append operations would be a more appropriate choice.

The Writer monad maintains a (value,log) pair, where the log type must be a monoid. The return function simply returns the value along with an empty log. Binding executes the bound function using the current value as input, and appends any log output to the existing log.

The MonadWriter class provides a number of convenience functions for working with Writer monads. The simplest and most useful is tell, which adds one or more entries to the log. The listen function turns a Writer that returns a value a and produces output w into a Writer that produces a value (a,w) and still produces output w. This allows the computation to "listen" to the log output generated by a Writer.

The pass function is slightly more complicated. It converts a Writer that produces a value (a,f) and output w into a Writer that produces a value a and output f w. This is somewhat cumbersome, so the helper function censor is normally used. The censor function takes a function and a Writer and produces a new Writer whose output is the same but whose log entry has been modified by the function.

The listens function operates just like listen except that the log part of the value is modified by the supplied function.

15.4 Example

In this example, we imagine a very simple firewall that filters packets based on a rulebase of rules matching the source and destination hosts and the payload of the packet. The firewall's primary job is packet filtering, but we would also like it to produce a log of its activity.

That was pretty simple, but what if we want to merge duplicate consecutive log entries? None of the existing functions allow us to modify the output from previous stages of the computation, but we can use a "delayed logging" trick to only add a log entry only after we get a new entry that doesn't match the ones before it.

Code available in [[../examples/example17.hs|example17.hs]]

-- merge identical entries at the end of the log-- This function uses [Entry] as both the log type and the result type.-- When two identical messages are merged, the result is just the message-- with an incremented count. When two different messages are merged,-- the first message is logged and the second is returned as the result.
mergeEntries ::[Entry]->[Entry]-> Writer [Entry][Entry]
mergeEntries [] x =return x
mergeEntries x []=return x
mergeEntries [e1][e2]=let(Log n msg)= e1
(Log n' msg')= e2
inif msg == msg' then
return [(Log (n+n') msg)]elsedo tell [e1]return[e2]-- This is a complex-looking function but it is actually pretty simple.-- It maps a function over a list of values to get a list of Writers,-- then runs each writer and combines the results. The result of the function-- is a writer whose value is a list of all the values from the writers and whose-- log output is the result of folding the merge operator into the individual-- log entries (using 'initial' as the initial log value).
groupSame ::(Monoid a)=> a ->(a -> a -> Writer a a)->[b]->(b -> Writer a c)-> Writer a [c]
groupSame initial merge []_=do tell initial
return[]
groupSame initial merge (x:xs) fn =do(result,output)<-return(runWriter (fn x))
new <- merge initial output
rest <- groupSame new merge xs fn
return(result:rest)-- this filters a list of packets, producing a filtered packet list and a log of-- the activity in which consecutive messages are merged
filterAll ::[Rule]->[Packet]-> Writer [Entry][Packet]
filterAll rules packets =do tell [Log 1"STARTING PACKET FILTER"]
out <- groupSame [] mergeEntries packets (filterOne rules)
tell [Log 1"STOPPING PACKET FILTER"]return(catMaybes out)

The Continuation monad

16 The Continuation monad

16.1 Overview

Computation type:

Computations which can be interrupted and resumed.

Binding strategy:

Binding a function to a monadic value creates a new continuation which uses the function as the continuation of the monadic computation.

16.2 Motivation

File:Warn.png Abuse of the Continuation monad can produce code that is impossible to understand and maintain.

Before using the Continuation monad, be sure that you have a firm understanding of continuation-passing style (CPS) and that continuations represent the best solution to your particular design problem. Many algorithms which require continuations in other languages do not require them in Haskell, due to Haskell's lazy semantics.

Continuations represent the future of a computation, as a function from an intermediate result to the final result. In continuation-passing style, computations are built up from sequences of nested continuations, terminated by a final continuation (often id) which produces the final result. Since continuations are functions which represent the future of a computation, manipulation of the continuation functions can achieve complex manipulations of the future of the computation, such as interrupting a computation in the middle, aborting a portion of a computation, restarting a computation and interleaving execution of computations. The Continuation monad adapts CPS to the structure of a monad.

The Continuation monad represents computations in continuation-passing style. Cont r a is a CPS computation that produces an intermediate result of type a within a CPS computation whose final result type is r.

The return function simply creates a continuation which passes the value on. The >>= operator adds the bound function into the continuation chain.

The MonadCont class provides the callCC function, which provides an escape continuation mechanism for use with Continuation monads. Escape continuations allow you to abort the current computation and return a value immediately. They achieve a similar effect to throwError and catchError within an Error monad.

callCC calls a function with the current continuation as its argument (hence the name). The standard idiom used with callCC is to provide a lambda-expression to name the continuation. Then calling the named continuation anywhere within its scope will escape from the computation, even if it is many layers deep within nested computations.

In addition to the escape mechanism provided by callCC, the Continuation monad can be used to implement other, more powerful continuation manipulations. These other mechanisms have fairly specialized uses, however — and abuse of them can easily create fiendishly obfuscated code — so they will not be covered here.

16.4 Example

This example gives a taste of how escape continuations work. The example function uses escape continuations to perform a complicated transformation on an integer.

17 Introduction

Part I has introduced the monad concept and Part II has provided an understanding of a number of common, useful monads in the standard Haskell libraries. This is not enough to put monads into heavy practice, however, because in the real world you often want computations which combine aspects of more than one monad at the same time, such as stateful, non-determistic computations or computations which make use of continuations and perform I/O. When one computation is a strict subset of the other, it is possible to perform the monad computations separately, unless the sub-computation is performed in a one-way monad.

Often, the computations can't be performed in isolation. In this case, what is needed is a monad that combines the features of the two monads into a single computation. It is inefficient and poor practice to write a new monad instance with the required characteristics each time a new combination is desired. Instead, we would prefer to develop a way to combine the standard monads to produce the needed hybrids. The technique that lets us do exactly that is called monad transformers.

Monad transformers are the topic of Part III, and they are explained by revisiting earlier examples to see how monad transformers can be used to add more realistic capabilities to them. It may be helpful to review the earlier examples as they are re-examined.

Combining monads the hard way

18 Combining monads the hard way

Before we investigate the use of monad transformers, we will see how monads can be combined without using transformers. This is a useful excercise to develop insights into the issues that arise when combining monads and provides a baseline from which the advantages of the transformer approach can be measured. We use the code from example 18 (the Continuation monad) to illustrate these issues, so you may want to review it before continuing.

18.1 Nested Monads

Some computations have a simple enough structure that the monadic computations can be nested, avoiding the need for a combined monad altogether. In Haskell, all computations occur in the IO monad at the top level, so the monad examples we have seen so far all actually use the technique of nested monadic computations. To do this, the computations perform all of their input at the beginning — usually by reading arguments from the command line — then pass the values on to the monadic computations to produce results, and finally perform their output at the end. This structure avoids the issues of combining monads but makes the examples seem contrived at times.

The code introduced in example 18 followed the nesting pattern: reading a number from the command line in the IO monad, passing that number to a computation in the Continuation monad to produce a string, and then writing the string back in the IO monad. The computations in the IO monad aren't restricted to reading from the command line and writing strings; they can be arbitrarily complex. Likewise, the inner computation can be arbitrarily complex as well. As long as the inner computation does not depend on the functionality of the outer monad, it can be safely nested within the outer monad, as illustrated in this variation on example 18 which reads the value from stdin instead of using a command line argument:

18.2 Combined Monads

What about computations with more complicated structure? If the nesting pattern cannot be used, we need a way to combine the attributes of two or more monads in a single computation. This is accomplished by doing computations within a monad in which the values are themselves monadic values in another monad. For example, we might perform computations in the Continuation monad of type Cont (IO String) a if we need to perform I/O within the computation in the Continuation monad. We could use a monad of type State (Either Err a) a to combine the features of the State and Error monads in a single computation.

Consider a slight modification to our example in which we perform the same I/O at the beginning, but we may require additional input in the middle of the computation in the Continuation monad. In this case, we will allow the user to specify part of the output value when the input value is within a certain range. Because the I/O depends on part of the computation in the Continuation monad and part of the computation in the Continuation monad depends on the result of the I/O, we cannot use the nested monad pattern.

Instead, we make the computation in the Continuation monad use values from the IO monad. What used to be Int and String values are now of type IO Int and IO String. We can't extract values from the IO monad — it's a one-way monad — so we may need to nest little do-blocks of the IO monad within the Continuation monad to manipulate the values. We use a helper function toIO to make it clearer when we are creating values in the IO monad nested within the Continuation monad.

Even this trivial example has gotten confusing and ugly when we tried to combine different monads in the same computation. It works, but it isn't pretty. Comparing the code side-by-side shows the degree to which the manual monad combination strategy pollutes the code.

19 Monad transformers

Monad transformers are special variants of standard monads that facilitate the combining of monads. Their type constructors are parameterized over a monad type constructor, and they produce combined monadic types.

19.1 Transformer type constructors

Type constructors play a fundamental role in Haskell's monad support. Recall that Reader r a is the type of values of type a within a Reader monad with environment of type r. The type constructor Reader r is an instance of the Monad class, and the runReader::(r->a) function performs a computation in the Reader monad and returns the result of type a.

A transformer version of the Reader monad, called ReaderT, exists which adds a monad type constructor as an addition parameter. ReaderT r m a is the type of values of the combined monad in which Reader is the base monad and m is the inner monad. ReaderT r m is an instance of the monad class, and the runReaderT::(r -> m a) function performs a computation in the combined monad and returns a result of type m a.

Using the transformer versions of the monads, we can produce combined monads very simply. ReaderT r IO is a combined Reader+IO monad. We can also generate the non-transformer version of a monad from the transformer version by applying it to the Identity monad. So ReaderT r Identity is the same monad as Reader r.

File:Info.png If your code produces kind errors during compilation, it means that you are not using the type cosntructors properly. Make sure that you have supplied the correct number of parameters to the type constructors and that you have not left out any parenthesis in complex type expressions.

19.2 Lifting

When using combined monads created by the monad transformers, we avoid having to explicitly manage the inner monad types, resulting in clearer, simpler code. Instead of creating additional do-blocks within the computation to manipulate values in the inner monad type, we can use lifting operations to bring functions from the inner monad into the combined monad.

Recall the liftM family of functions which are used to lift non-monadic functions into a monad. Each monad transformer provides a lift function that is used to lift a monadic computation into a combined monad. Many transformers also provide a liftIO function, which is a version of lift that is optimized for lifting computations in the IO monad. To see this in action, we will continue to develop our previous example in the Continuation monad.

The impact of adding the I/O in the middle of the computation is narrowly confined when using the monad transformer. Contrast this with the changes required to achieve the same result using a manually combined monad.

Standard monad transformers

20 Standard monad transformers

Haskell's base libraries provide support for monad transformers in the form of classes which represent monad transformers and special transformer versions of standard monads.

20.1 The MonadTrans and MonadIO classes

The MonadTrans class is defined in Control.Monad.Trans and provides the single function lift. The lift function lifts a monadic computation in the inner monad into the combined monad.

class MonadTrans t where
lift ::(Monad m)=> m a -> t m a

Monads which provide optimized support for lifting IO operations are defined as members of the MonadIO class, which defines the liftIO function.

class(Monad m)=> MonadIO m where
liftIO ::IO a -> m a

20.2 Transformer versions of standard monads

The standard monads of the monad template library all have transformer versions which are defined consistently with their non-transformer versions. However, it is not the case the all monad transformers apply the same transformation. We have seen that the ContT transformer turns continuations of the form (a->r)->r into continuations of the form (a->m r)->m r. The StateT transformer is different. It turns state transformer functions of the form s->(a,s) into state transformer functions of the form s->m (a,s). In general, there is no magic formula to create a transformer version of a monad — the form of each transformer depends on what makes sense in the context of its non-transformer type.

File:Info.png Order is important when combining monads. StateT s (Error e) is different than ErrorT e (State s). The first produces a combined type of s -> Error e (a,s), in which the computation can either return a new state or generate an error. The second combination produces a combined type of s -> (Error e a,s), in which the computation always returns a new state, and the value can be an error or a normal value.

Anatomy of a monad transformer

21 Anatomy of a monad transformer

In this section, we will take a detailed look at the implementation of one of the more interesting transformers in the standard library, StateT. Studying this transformer will build insight into the transformer mechanism that you can call upon when using monad transformers in your code. You might want to review the section on the State monad before continuing.

21.1 Combined monad definition

Just as the State monad was built upon the definition

newtype State s a = State { runState ::(s ->(a,s))}

the StateT transformer is built upon the definition

newtype StateT s m a = StateT { runStateT ::(s -> m (a,s))}

State s is an instance of both the Monad class and the MonadState s class, so StateT s m should also be members of the Monad and MonadState s classes. Furthermore, if m is an instance of MonadPlus, StateT s m should also be a member of MonadPlus.

Compare this to the definition for State s. Our definition of return makes use of the return function of the inner monad, and the binding operator uses a do-block to perform a computation in the inner monad.

We also want to declare all combined monads that use the StateT transformer to be instaces of the MonadState class, so we will have to give definitions for get and put:

The lift function creates a StateT state transformation function that binds the computation in the inner monad to a function that packages the result with the input state. The result is that a function that returns a list (i.e., a computation in the List monad) can be lifted into StateT s [], where it becomes a function that returns a StateT (s -> [(a,s)]). That is, the lifted computation produces multiple (value,state) pairs from its input state. The effect of this is to "fork" the computation in StateT, creating a different branch of the computation for each value in the list returned by the lifted function. Of course, applying StateT to a different monad will produce different semantics for the lift function.

21.3 Functors

We have examined the implementation of one monad transformer above, and it was stated earlier that there was no magic formula to produce transformer versions of monads. Each transformer's implementation will depend on the nature of the computational effects it is adding to the inner monad.

Despite this, there is some theoretical foundation to the theory of monad transformers. Certain transformers can be grouped according to how they use the inner monad, and the transformers within each group can be derived using monadic functions and functors. Functors, roughly, are types which support a mapping operation fmap :: (a->b) -> f a -> f b. To learn more about it, check out Mark Jones' influential paper that inspired the Haskell monad template library.

More examples with monad transformers

22 More examples with monad transformers

At this point, you should know everything you need to begin using monads and monad transformers in your programs. The best way to build proficiency is to work on actual code. As your monadic programs become more abitious, you may find it awkward to mix additional transformers into your combined monads. This will be addressed in the next section, but first you should master the basic process of applying a single transformer to a base monad.

22.1 WriterT with IO

Try adapting the firewall simulator of example 17 to include a timestamp on each log entry (don't worry about merging entries). The necessary changes should look something like this:

22.2 ReaderT with IO

If you found that one too easy, move on to a slightly more complex example: convert the template system in example 16 from using a single template file with named templates to treating individual files as templates. One possible solution is shown in [[../examples/example23.hs|example 23]], but try to do it without looking first.

22.3 StateT with List

The previous examples have all been using the IO monad as the inner monad. Here is a more interesting example: combining StateT with the List monad to produce a monad for stateful nondeterministic computations.

We will apply this powerful monad combination to the task of solving constraint satisfaction problems (in this case, a logic problem). The idea behind it is to have a number of variables that can take on different values and a number of predicates involving those variables that must be satisfied. The current variable assignments and the predicates make up the state of the computation, and the non-deterministic nature of the List monad allows us to easily test all combinations of variable assignments.

We start by laying the groundwork we will need to represent the logic problem, a simple predicate language:

The next thing we will need is some code for representing and solving constraint satisfaction problems. This is where we will define our combined monad.

Code available in [[../examples/example24.hs|example24.hs]]

-- this is the type of our logic problemdata ProblemState = PS {vars::Variables, constraints::[Predicate]}-- this is our monad type for non-determinstic computations with statetype NDS a = StateT ProblemState [] a
-- lookup a variable
getVar :: Var -> NDS (Maybe Value)
getVar v =do vs <- gets vars
return$lookup v vs
-- set a variable
setVar :: Var -> Value -> NDS ()
setVar v x =do st <- get
vs' <- return $ filter ((v/=).fst) (vars st)
put $ st {vars=(v,x):vs'}-- Check if the variable assignments satisfy all of the predicates.-- The partial argument determines the value used when a predicate returns-- Nothing because some variable it uses is not set. Setting this to True-- allows us to accept partial solutions, then we can use a value of-- False at the end to signify that all solutions should be complete.
isConsistent ::Bool-> NDS Bool
isConsistent partial =do cs <- gets constraints
vs <- gets vars
let results =map(\p->check p vs) cs
return$and(map(maybe partial id) results)-- Return only the variable bindings that are complete consistent solutions.
getFinalVars :: NDS Variables
getFinalVars =do c <- isConsistent False
guard c
gets vars
-- Get the first solution to the problem, by evaluating the solver computation with-- an initial problem state and then returning the first solution in the result list,-- or Nothing if there was no solution.
getSolution :: NDS a -> ProblemState ->Maybe a
getSolution c i = listToMaybe (evalStateT c i)-- Get a list of all possible solutions to the problem by evaluating the solver-- computation with an initial problem state.
getAllSolutions :: NDS a -> ProblemState ->[a]
getAllSolutions c i = evalStateT c i

We are ready to apply the predicate language and stateful nondeterministic monad to solving a logic problem. For this example, we will use the well-known Kalotan puzzle which appeared in Mathematical Brain-Teasers, Dover Publications (1976), by J. A. H. Hunter.

The Kalotans are a tribe with a peculiar quirk: their males always tell the truth. Their females never make two consecutive true statements, or two consecutive untrue statements. An anthropologist (let's call him Worf) has begun to study them. Worf does not yet know the Kalotan language. One day, he meets a Kalotan (heterosexual) couple and their child Kibi. Worf asks Kibi: ``Are you a boy? The kid answers in Kalotan, which of course Worf doesn't understand. Worf turns to the parents (who know English) for explanation. One of them says: "Kibi said: `I am a boy.'" The other adds: "Kibi is a girl. Kibi lied." Solve for the sex of Kibi and the sex of each parent.

We will need some additional predicates specific to this puzzle, and to define the universe of allowed variables values:

Each call to tryAllValues will fork the solution space, assigning the named variable to be "male" in one fork and "female" in the other. The forks which produce inconsistent variable assignments are eliminated (using the guard function). The call to getFinalVars applies guard again to eliminate inconsistent variable assignments and returns the remaining assignments as the value of the computation.

Managing the transformer stack

23 Managing the transformer stack

As the number of monads combined together increases, it becomes increasingly important to manage the stack of monad transformers well.

23.1 Selecting the correct order

Once you have decided on the monad features you need, you must choose the correct order in which to apply the monad transformers to achieve the results you want. For instance you may know that you want a combined monad that is an instance of MonadError and MonadState, but should you apply StateT to the Error monad or ErrorT to the State monad?

The decision depends on the exact semantics you want for your combined monad. Applying StateT to the Error monad gives a state transformer function of type s -> Error e (a,s). Applying ErrorT to the State monad gives a state transformer function of type s -> (Error e a,s). Which order to choose depends on the role of errors in your computation. If an error means no state could be produced, you would apply StateT to Error. If an error means no value could be produced, but the state remains valid, then you would apply ErrorT to State.

Choosing the correct order requires understanding the transformation carried out by each monad transformer, and how that transformation affects the semantics of the combined monad.

23.2 An example with multiple transformers

The following example demonstrates the use of multiple monad transformers. The code uses the StateT monad transformer along with the List monad to produce a combined monad for doing stateful nondeterministic computations. In this case, however, we have added the WriterT monad transformer to perform logging during the computation. The problem we will apply this monad to is the famous N-queens problem: to place N queens on a chess board so that no queen can attack another.

The first decision is in what order to apply the monad transformers. StateT s (WriterT w []) yields a type like: s -> [((a,s),w)]. WriterT w (StateT s []) yields a type like: s -> [((a,w),s)]. In this case, there is little difference between the two orders, so we will choose the second arbitrarily.

Our combined monad is an instance of both MonadState and MonadWriter, so we can freely mix use of get, put, and tell in our monadic computations.

The program operates in a similar manner to the previous example, which solved the kalotan puzzle. In this example, however, we do not test for consistency using the guard function. Instead, we only create branches that correspond to allowed queen positions. We use the added logging facility to log the number of possible choices at each step and the position in which the queen was placed.

23.3 Heavy lifting

There is one subtle problem remaining with our use of multiple monad transformers. Did you notice that all of the computations in the previous example are done in the combined monad, even if they only used features of one monad? The code for these functions in tied unneccessarily to the definition of the combined monad, which decreases their reusability.

This is where the lift function from the MonadTrans class comes into its own. The lift function gives us the ability to write our code in a clear, modular, reusable manner and then lift the computations into the combined monad as needed.

and then lift the logString computation into the combined monad when we use it.

File:Info.png You may need to use the compiler flags -fglasgow-exts with GHC or the equivalent flags with your Haskell compiler to use this technique. The issue is that m in the constraint above is a type constructor, not a type, and this is not supported in standard Haskell 98.

When using lifting with complex transformer stacks, you may find yourself composing multiple lifts, like lift . lift . lift $ f x. This can become hard to follow, and if the transformer stack changes (perhaps you add ErrorT into the mix) the lifting may need to be changed all over the code. A good practice to prevent this is to declare helper functions with informative names to do the lifting:

liftListToState = lift . lift . lift

Then, the code is more informative and if the transformer stack changes, the impact on the lifting code is confined to a small number of these helper functions.

The hardest part about lifting is understanding the semantics of lifting computations, since this depends on the details of the inner monad and the transformers in the stack. As a final task, try to understand the different roles that lifting plays in the following example code. Can you predict what the output of the program will be?

Code available in [[../examples/example26.hs|example26.hs]]

-- this is our combined monad type for this problemtype NDS a = StateT Int(WriterT [String][]) a
{- Here is a computation on lists -}-- return the digits of a number as a list
getDigits ::Int->[Int]
getDigits n =let s =(show n)inmap digitToInt s
{- Here are some computations in MonadWriter -}-- write a value to a log and return that value
logVal ::(MonadWriter [String] m)=>Int-> m Int
logVal n =do tell ["logVal: "++(show n)]return n
-- do a logging computation and return the length of the log it wrote
getLogLength ::(MonadWriter [[a]] m)=> m b -> m Int
getLogLength c =do(_,l)<- listen $ c
return(length(concat l))-- log a string value and return 0
logString ::(MonadWriter [String] m)=>String-> m Int
logString s =do tell ["logString: "++ s]return0{- Here is a computation that requires a WriterT [String] [] -}-- "Fork" the computation and log each list item in a different branch.
logEach ::(Show a)=>[a]-> WriterT [String][] a
logEach xs =do x <- lift xs
tell ["logEach: "++(show x)]return x
{- Here is a computation in MonadState -}-- increment the state by a specified value
addVal ::(MonadState Int m)=>Int-> m ()
addVal n =do x <- get
put (x+n){- Here are some computations in the combined monad -}-- set the state to a given value, and log that value
setVal ::Int-> NDS ()
setVal n =do x <- lift $ logVal n
put x
-- "Fork" the computation, adding a different digit to the state in each branch.-- Because setVal is used, the new values are logged as well.
addDigits ::Int-> NDS ()
addDigits n =do x <- get
y <- lift . lift $ getDigits n
setVal (x+y){- an equivalent construction is:
addDigits :: Int -> NDS ()
addDigits n = do x <- get
msum (map (\i->setVal (x+i)) (getDigits n))
-}{- This is an example of a helper function that can be used to put all of the lifting logic
in one location and provide more informative names. This has the advantage that if the
transformer stack changes in the future (say, to add ErrorT) the changes to the existing
lifting logic are confined to a small number of functions.
-}
liftListToNDS ::[a]-> NDS a
liftListToNDS = lift . lift
-- perform a series of computations in the combined monad, lifting computations from other-- monads as necessary.
main ::IO()
main =domapM_print$ runWriterT $(`evalStateT` 0)$do x <- lift $ getLogLength $ logString "hello"
addDigits x
x <- lift $ logEach [1,3,5]
lift $ logVal x
liftListToNDS $ getDigits 287

Once you fully understand how the various lifts in the example work and how lifting promotes code reuse, you are ready for real-world monadic programming. All that is left to do is to hone your skills writing real software. Happy hacking!

Continuing Exploration

24 Continuing Exploration

This brings us to the end of this tutorial. If you want to continue learning about the mathematical foundations of monads, there are numerous category theory resources on the internet. For more examples of monads and their applications in the real world, you might want to explore the design of the Parsec monadic parser combinator library and/or the QuickCheck testing tool. You may also be interested in arrows, which are similar to monads but more general.

If you discover any errors — no matter how small — in this document, or if you have suggestions for how it can be improved, please write to the author at jnewbern@yahoo.com.

A physical analogy for monads

25 A physical analogy for monads

Because monads are such abstract entities, it is sometimes useful to think about a physical system that is analogous to a monad instead of thinking about monads directly. In this way, we can use our physical intuition and experiences to gain insights that we can relate back to the abstract world of computational monads.

The particular physical analogy developed here is that of a mechanized assembly line. It is not a perfect fit for monads — especially with some of the higher-order aspects of monadic computation — but I believe it could be helpful to gain the initial understanding of how a monad works.

Begin by thinking about a Haskell program as a conveyor belt. Input goes on end of the conveyor belt and is carried to a succession of work areas. At each work area, some operation is performed on the item on the conveyor belt and the result is carried by the conveyor belt to the next work area. Finally, the conveyor belt carries the final product to the end of the assembly line to be output.

In this assembly line model, our physical monad is a system of machines that controls how successive work areas on the assembly line combine their functionality to create the overall product.

Our monad consists of three parts:

Trays that hold work products as they move along the conveyor belt.

Loader machines that can put any object into a tray.

Combiner machines that can take a tray with an object and produce a tray with a new object. These combiner machines are attached to worker machines that actualy produce the new objects.

We use the monad by setting up our assembly line as a loader machine which puts materials into trays at the beginning of the assembly line. The conveyor belt then carries these trays to each work area, where a combiner machine takes the tray and may decide based on its contents whether to run them through a worker machine, as shown in Figure A-1.

The important thing to notice about the monadic assembly line is that it separates out the work of combining the output of the worker machines from the actual work done by the worker machines. Once they are separated, we can vary them independently. So the same combiner machines could be used on an assembly line to make airplanes and an assembly line to make chopsticks. Likewise, the same worker machines could be used with different combiners to alter the way the final product is produced.

Lets take the example of an assembly line to make chopsticks, and see how it is handled in our physical analogy and how me might represent it as a program in Haskell. We will have three worker machines. The first takes small pieces of wood as input and outputs a tray containing a pair of roughly shaped chopsticks. The second takes a pair of roughly shaped chopsticks and outputs a tray containing a pair of smooth, polished chopsticks with the name of the restaurant printed on them. The third takes a pair of polished chopsticks and outputs a tray containing a finished pair of chopsticks in a printed paper wrapper. We could represent this in Haskell as:

It is clear that the worker machines contain all of the functionality needed to produce chopsticks. What is missing is the specification of the trays, loader, and combiner machines that collectively make up the Tray monad. Our trays should either be empty or contain a single item. Our loader machine would simply take an item and place it in a tray on the conveyor belt. The combiner machine would take each input tray and pass along empty trays while feeding the contents of non-empty trays to its worker machine. In Haskell, we would define the Tray monad as:

So far, you have seen how monads are like a framework for building assembly lines, but you probably haven't been overawed by their utility. To see why we might want to build our assembly line using the monadic approach, consider what would happen if we wanted to change the manufacturing process.

Right now, when a worker machine malfunctions, it uses the fail routine to produce an empty tray. The fail routine takes an argument describing the failure, but our Tray type ignores this and simply produces an empty tray. This empty tray travels down the assembly line and the combiner machines allow it to bypass the remaining worker machines. It eventually reaches the end of the assembly line, where it is brought to you, the quality control engineer. It is your job to figure out which machine failed, but all you have to go on is an empty tray.

You realize that your job would be much easier if you took advantage of the failure messages that are currently ignored by the fail routine in your Tray monad. Because your assembly line is organized around a monadic approach, it is easy for you to add this functionality to your assembly line without changing your worker machines.

To make the change, you simply create a new tray type that can never be empty. It will always either contain an item or it will contain a failure report describing the exact reason there is no item in the tray.

File:Info.png You may recognize the Tray2 monad as a disguised version of the Error monad that is a standard part of the Haskell 98 libraries.

Replacing the Tray monad with the Tray2 monad instantly upgrades your assembly line. Now when a failure occurs, the tray that is brought to the quality control engineer contains a failure report detailing the exact cause of the failure!

Haskell code examples

26 Haskell code examples

This appendix contains a list of all of the code [[../examples/|examples]] supplied with the tutorial. The source code can be found here.

26.1 [[../examples/example1.hs|Example 1]]

The example code introduces the monad concept without using Haskell typeclasses. It shows how a monadic combinator can be used to simplify the construction of computations from sequences of computations which may not return a result.

26.4 [[../examples/example4.hs|Example 4]]

The example code shows a more typical use of the foldM function within a do-block. It combines dictionary values read from different files into a single dictionary using the foldM function within the IO monad.

26.11 [[../examples/example11.hs|Example 11]]

The example code shows how to use the Maybe monad to build complex queries from simpler queries that may fail to return a result. The specific example used is looking up mail preferences for someone based on either their full name or a nickname.

26.12 [[../examples/example12.hs|Example 12]]

The example code demonstrates the use of the Either type constructor as an Error monad with a custom error type. The example parses hexadecimal digits and uses the exception handling mechanism of the Error monad to provide informative error messages in the event of a parse failure.

26.14 [[../examples/example14.hs|Example 14]]

The example code implements a simple version of the standard Unix command "tr". The example demonstrates use of the IO monad including implicit fail calls due to pattern matching failures and the use of catcherror.

26.15 [[../examples/example15.hs|Example 15]]

The example code shows how the State monad can be used instead of explicitly passing state. The example uses the State monad to manage the random number generator state while building a compound data value requiring multiple calls to the random number generator.

26.16 [[../examples/example16.hs|Example 16]]

The example code shows how the Reader monad can be used to simplify computations involving a shared environment. The example uses the Reader monad to implement a simple template substitution system. The example code demonstrates the use of the Parsec monadic parser combinator library.

26.22 [[../examples/example22.hs|Example 22]]

The example code shows how the transformer version of the Writer monad can be used to create a combined monad for logging and doing I/O. The example adds timestamps to the log entries of the firewall simulator from example 17.

26.23 [[../examples/example23.hs|Example 23]]

The example code shows how the transformer version of the Reader monad can be used to create a monad that combines a shared environment with I/O. The example converts the template system of example 16 to use files as templates.

26.24 [[../examples/example24.hs|Example 24]]

The example code uses the StateT transformer on the List monad to create a combined monad for doing non-deterministic stateful computations. The example uses the combined monad to solve a logic problem.

26.25 [[../examples/example25.hs|Example 25]]

The example code uses the StateT and WriterT transformers on the List monad to create a combined monad for doing non-deterministic stateful computations with logging. The example uses the combined monad to solve the N-queens problem.