Revision as of 22:13, 29 October 2011

Monads in Haskell can be thought of as composable computation descriptions. The essence of monad is thus separation of composition timeline from the composed computation's execution timeline, as well as the ability of computation to implicitly carry extra data as pertaining to the computation itself in addition to its one (hence the name) output. This lends monads to supplementing pure calculations with features like I/O, common environment or state, and to preprocessing of computations (simplification, optimization etc.).

Each monad, or computation type, provides means, subject to Monad Laws, of (a) creating a description of computation to produce a given value (or such that will fail to produce anything at all), (b) running a computation description (CD) and returning its output to Haskell, and (c) combining a CD with a "reaction" to it, i.e. a Haskell function consuming of its output and returning another CD (using or dependent on that output, if need be), to create a combined CD. It might also define additional primitives to provide access and/or enable manipulation of data it implicitly carries, specific to its nature.

Thus in Haskell, though it is a purely-functional language, side effects that will be performed by a computation can be dealt with and combined purely at the monad's composition time. Monads thus resemble programs in a particular DSL. While programs may describe impure effects and actions outside Haskell, they can still be combined and processed ("assembled") purely, inside Haskell, creating a pure Haskell value - a CD that describes an impure calculation. That is how Monads in Haskell separate between the pure and the impure.

The computation doesn't have to be impure and can be pure itself as well. Then Monads serve to separate the pure from the pure in one big holiday celebration after the other. We still get the benefits of separation of concerns, and automatic creation of a computational "pipeline" carrying out our chained Haskell calculations one after another with computation's state threaded through behind the scenes.

Because they are very useful in practice but rather mind-twisting for the beginners, numerous tutorials that deal exclusively with monads were created (see monad tutorials).

In addition to implementing the class functions, all instances of Monad should obey the following equations, or Monad Laws:

returna>>=k=kam>>=return=mm>>=(\x->kx>>=h)=(m>>=k)>>=h

See this intuitive explanation of why they should obey the Monad laws. It basically says that monad's reactions should be associative under Kleisli composition, defined as (f >=> g) x = f x >>= g, with return its left and right identity element.

Special notation

In order to improve the look of code that uses monads Haskell provides a special syntactic sugar called do-notation. For example, following expression:

thing1>>=(\x->func1x>>=(\y->thing2>>=(\_->func2y(\z->returnz))))

which can be written more clearly by breaking it into several lines and omitting parentheses:

thing1>>=\x->func1x>>=\y->thing2>>=\_->func2y>>=\z->returnz

can be also written using the do-notation as follows:

dox<-thing1y<-func1xthing2z<-func2yreturnz

Code written using the do-notation is transformed by the compiler to ordinary expressions that use Monad class functions.

When using the do-notation and a monad like State or IO programs look very much like programs written in an imperative language as each line contains a statement that can change the simulated global state of the program and optionally binds a (local) variable that can be used by the statements later in the code block.

Commutative monads

Commutative monads are monads for which the order of actions makes no difference (they commute), that is when following code:

doa<-fxb<-gymab

is the same as:

dob<-gya<-fxmab

Examples of commutative include:

Reader monad

Maybe monad

Monad tutorials

Monads are known for being deeply confusing to lots of people, so there are plenty of tutorials specifically related to monads. Each takes a different approach to Monads, and hopefully everyone will find something useful.