'''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'', which lends monads to supplementing ''pure'' calculations with features like state, common environment or I/O, and also 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. This lends monads to supplementing ''pure'' calculations with features like I/O, common environment or state, and to ''preprocessing'' of computations (simplification, optimization etc.).

−

Even though Haskell is a purely-functional language, side effects that ''would be'' performed by a computation (when and if ''run'') can be dealt with and combined ''purely'' at the monad's composition time. The combined computation doesn't have to be impure and can be pure itself as well. 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 (''"compiled"'') purely, ''inside'' Haskell.

+

Each monad, or computation type, provides a means of (a) creating a description of computation to produce a ''given value'', (b) ''running'' a computation description (CD) and returning its output to Haskell, and (c) combining a CD with a Haskell function consuming of its output and returning another CD, to create a ''combined'' one. 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 (''"compiled"'') purely ''inside'' Haskell, creating a pure Haskell value - a CD that describes an impure calculation. The combined computations don't have to be impure and can be pure themselves as well.

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#Monad tutorials|monad tutorials]]).

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#Monad tutorials|monad tutorials]]).

Revision as of 10:30, 11 June 2010

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 a means of (a) creating a description of computation to produce a given value, (b) running a computation description (CD) and returning its output to Haskell, and (c) combining a CD with a Haskell function consuming of its output and returning another CD, to create a combined one. 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 ("compiled") purely inside Haskell, creating a pure Haskell value - a CD that describes an impure calculation. The combined computations don't have to be impure and can be pure themselves as well.

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

-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.
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 <- f x
b <- g y
m a b

is the same as:

do
b <- g y
a <- f x
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.