'''''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, that it '''''will produce''''' when run (or queried, or called upon). This lends monads to supplementing ''pure'' calculations with features like I/O, common environment or state, and to ''preprocessing'' of computations (simplification, optimization etc.).

+

'''''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, that it '''''will produce''''' when run (or queried, or called upon). This lends monads to supplementing ''pure'' calculations with features like I/O, common environment or state, etc.

Each monad, or computation type, provides means, subject to '''''Monad Laws''''', to '''''(a)''''' ''create'' a description of computation action that will produce (a.k.a. "return") a given Haskell value, '''''(b)''''' somehow ''run'' a computation action description (possibly getting its output back into Haskell should the monad choose to allow it, if computations described by the monad are pure, or causing the prescribed side effects if it's not), and '''''(c)''''' ''combine'' (a.k.a. "bind") a computation action description with a ''reaction'' to it &ndash; a regular Haskell function of one argument (that will receive computation-produced value) returning another action description (using or dependent on that value, if need be) &ndash; thus creating a combined computation action description that will feed the original action's output through the reaction while automatically taking care of the particulars of the computational process itself. A monad might also define additional primitives to provide access to and/or enable manipulation of data it implicitly carries, specific to its nature.

Each monad, or computation type, provides means, subject to '''''Monad Laws''''', to '''''(a)''''' ''create'' a description of computation action that will produce (a.k.a. "return") a given Haskell value, '''''(b)''''' somehow ''run'' a computation action description (possibly getting its output back into Haskell should the monad choose to allow it, if computations described by the monad are pure, or causing the prescribed side effects if it's not), and '''''(c)''''' ''combine'' (a.k.a. "bind") a computation action description with a ''reaction'' to it &ndash; a regular Haskell function of one argument (that will receive computation-produced value) returning another action description (using or dependent on that value, if need be) &ndash; thus creating a combined computation action description that will feed the original action's output through the reaction while automatically taking care of the particulars of the computational process itself. A monad might also define additional primitives to provide access to and/or enable manipulation of data it implicitly carries, specific to its nature.

Line 51:

Line 51:

== Special notation ==

== Special notation ==

−

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

+

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

+

<haskell>

<haskell>

thing1 >>= (\x -> func1 x >>= (\y -> thing2

thing1 >>= (\x -> func1 x >>= (\y -> thing2

−

>>= (\_ -> func2 y (\z -> return z))))

+

>>= (\_ -> func2 y >>= (\z -> return z))))

</haskell>

</haskell>

+

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

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

+

<haskell>

<haskell>

thing1 >>= \x ->

thing1 >>= \x ->

Line 64:

Line 67:

return z

return z

</haskell>

</haskell>

−

can be also written using the <hask>do</hask>-notation as follows:

+

+

This can also be written using the <hask>do</hask>-notation as follows:

+

<haskell>

<haskell>

do

do

Line 73:

Line 78:

return z

return z

</haskell>

</haskell>

−

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

−

When using the <hask>do</hask>-notation and a monad like <hask>State</hask> or <hask>IO</hask> 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.

+

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

+

+

When using <hask>do</hask>-notation and a monad like <hask>State</hask> or <hask>IO</hask> 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.

It is possible to intermix the <hask>do</hask>-notation with regular notation.

It is possible to intermix the <hask>do</hask>-notation with regular notation.

−

More on the <hask>do</hask>-notation can be found in a section of [[Monads as computation#Do notation|Monads as computation]] and in other [[Monad#Monad tutorials|tutorials]].

+

More on <hask>do</hask>-notation can be found in a section of [[Monads as computation#Do notation|Monads as computation]] and in other [[Monad#Monad tutorials|tutorials]].

* [http://hackage.haskell.org/packages/archive/monad-mersenne-random/latest/doc/html/Control-Monad-Mersenne-Random.html Random values] - run code in an environment with access to a stream of random numbers.

* [http://hackage.haskell.org/packages/archive/monad-mersenne-random/latest/doc/html/Control-Monad-Mersenne-Random.html Random values] - run code in an environment with access to a stream of random numbers.

Revision as of 15:15, 17 January 2014

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, that it will produce when run (or queried, or called upon). This lends monads to supplementing pure calculations with features like I/O, common environment or state, etc.

Each monad, or computation type, provides means, subject to Monad Laws, to (a)create a description of computation action that will produce (a.k.a. "return") a given Haskell value, (b) somehow run a computation action description (possibly getting its output back into Haskell should the monad choose to allow it, if computations described by the monad are pure, or causing the prescribed side effects if it's not), and (c)combine (a.k.a. "bind") a computation action description with a reaction to it – a regular Haskell function of one argument (that will receive computation-produced value) returning another action description (using or dependent on that value, if need be) – thus creating a combined computation action description that will feed the original action's output through the reaction while automatically taking care of the particulars of the computational process itself. A monad might also define additional primitives to provide access to 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 computation action description 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 provide the benefits of separation of concerns, and automatic creation of a computational "pipeline". 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).

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.

-notation is transformed by the compiler to ordinary expressions that use the functions from the

Monad

class.
When using

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.
It is possible to intermix the

4 Commutative monads

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

do
a <- actA
b <- actB
m a b

is the same as:

do
b <- actB
a <- actA
m a b

Examples of commutative include:

Reader

monad

Maybe

monad

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