# Difference between revisions of "Monad (sans metaphors)"

(Category:Monad) |
m |
||

(6 intermediate revisions by 4 users not shown) | |||

Line 1: | Line 1: | ||

[[Category:Glossary]] | [[Category:Glossary]] | ||

− | [[Category:Monad]] | + | [[Category:Monad|*]] |

+ | <div style="border-left:1px solid lightgray; padding: 1em" alt="blockquote"> | ||

+ | Think of a monad as a spacesuit full of nuclear waste in the ocean next to a container of apples. Now, you can't put oranges in the spacesuit or the nuclear waste falls in the ocean, but the apples are carried around anyway, and you just take what you need. | ||

+ | |||

+ | <tt>[[QuotesPage|Don Stewart.]]</tt> | ||

+ | </div> | ||

=== Introduction === | === Introduction === | ||

− | + | Many discussions of Haskell [[Monad|monads]] seek to explain them through the use of a variety of metaphors. This page attempts to simply provide a more technical yet straightforward description of monads as defined by Haskell. | |

− | Many discussions of Haskell [[Monad| | ||

=== So what ''is'' a monad? === | === So what ''is'' a monad? === | ||

− | + | In Haskell, a monad is a type constructor with two operations, implementing a standard interface and following a few simple rules: | |

− | The Monad type class tells you the interface (what operations you've got, and their types), the | + | * The <code>Monad</code> type class tells you the interface (what operations you've got, and their types), |

+ | * the monad laws tell you what all types implementing that interface should have in common. | ||

− | The | + | The monadic interface gives you two operations: |

+ | * one to stuff things into a monadic thing: <code>return</code>, | ||

+ | * and one to chain two monadic things together: <code>(>>=)</code>. | ||

+ | The chaining explicitly caters for information flowing from the first to the second parameter of <code>(>>=)</code>. | ||

− | The | + | The monad laws tell you two useful facts about monadic things thrown together in that way: whatever it is the monad does, anything just thrown into it will take no part in that action, and whichever way you use that chaining operation, the structure of chaining is irrelevant - only the ordering of chained monadic things matters. |

− | structure of chaining is irrelevant - only the ordering of chained | ||

− | There are usually other ways to create | + | There are usually other ways to create "primitive" monadic things, which can be combined into complex monadic structures using the operations of the monadic interface. |

− | There is | + | There usually is an abstract way to interpret monadic structures built in this way - a <i>run</i> operation of some kind. Examples include: |

− | * | + | * <code>IO</code>: The "primitive monadic things" are basic I/O operations. The <i>run</i> operation exists outside the language, to be applied to <code>Main.main</code>. <code>return</code> puts its argument into a simple <code>IO</code> thing which, if used, merely produces that original argument. <code>(>>=)</code> arranges <code>IO</code> monadic structures sequentially, starting with the leftmost innermost I/O operation in the structure and applying the second argument of <code>(>>=)</code> to the result of executing the first. |

− | * []: The | + | * <code>[]</code>: The "primitive monadic things" are lists. The <i>run</i> operation is the identity, i.e the lists are directly exposed as data structures. <code>return</code> creates a singleton list. <code>(>>=)</code> applies its second argument to each element of its first argument and concatenates the results (<code>concatMap</code>). |

− | + | Sometimes the monadic type provides the <i>run</i> operation as part of its interface: | |

+ | * <code>State</code>: The "primitive monadic things" are operations on a state type, returning a result and a state. The <i>run</i> operation is provided by <code>runState</code>, wich applies a (possibly) complex monadic thing to an input state, returning a result and a (modified) state. <code>return</code> returns its parameter, passing its input state unchanged. <code>(>>=)</code> applies its first parameter to the input state, applies its second parameter to the result value and result state of the first. | ||

− | = | + | * <code>ST</code>: The "primitive monadic things" are operations on an abstract, <i>encapsulated</i> state type. The <i>run</i> operation is provided by <code>runST</code>, which obtains the result of an <code>ST</code> thing, through the use of private state and an extension of the regular Haskell type system. <code>return</code> puts its argument into a simple <code>ST</code> thing which, if used, merely produces that original argument. <code>(>>=)</code> establishes the ordering of <code>ST</code> things by applying its second argument to the result of executing the first. |

+ | == Original source == | ||

* Claus Reinke on Haskell-Cafe: [http://www.haskell.org/pipermail/haskell-cafe/2007-August/029851.html Monad Description For Imperative Programmer] | * Claus Reinke on Haskell-Cafe: [http://www.haskell.org/pipermail/haskell-cafe/2007-August/029851.html Monad Description For Imperative Programmer] |

## Latest revision as of 08:53, 21 September 2021

Think of a monad as a spacesuit full of nuclear waste in the ocean next to a container of apples. Now, you can't put oranges in the spacesuit or the nuclear waste falls in the ocean, but the apples are carried around anyway, and you just take what you need.

### Introduction

Many discussions of Haskell monads seek to explain them through the use of a variety of metaphors. This page attempts to simply provide a more technical yet straightforward description of monads as defined by Haskell.

### So what *is* a monad?

In Haskell, a monad is a type constructor with two operations, implementing a standard interface and following a few simple rules:

- The
`Monad`

type class tells you the interface (what operations you've got, and their types), - the monad laws tell you what all types implementing that interface should have in common.

The monadic interface gives you two operations:

- one to stuff things into a monadic thing:
`return`

, - and one to chain two monadic things together:
`(>>=)`

.

The chaining explicitly caters for information flowing from the first to the second parameter of `(>>=)`

.

The monad laws tell you two useful facts about monadic things thrown together in that way: whatever it is the monad does, anything just thrown into it will take no part in that action, and whichever way you use that chaining operation, the structure of chaining is irrelevant - only the ordering of chained monadic things matters.

There are usually other ways to create "primitive" monadic things, which can be combined into complex monadic structures using the operations of the monadic interface.

There usually is an abstract way to interpret monadic structures built in this way - a *run* operation of some kind. Examples include:

`IO`

: The "primitive monadic things" are basic I/O operations. The*run*operation exists outside the language, to be applied to`Main.main`

.`return`

puts its argument into a simple`IO`

thing which, if used, merely produces that original argument.`(>>=)`

arranges`IO`

monadic structures sequentially, starting with the leftmost innermost I/O operation in the structure and applying the second argument of`(>>=)`

to the result of executing the first.

`[]`

: The "primitive monadic things" are lists. The*run*operation is the identity, i.e the lists are directly exposed as data structures.`return`

creates a singleton list.`(>>=)`

applies its second argument to each element of its first argument and concatenates the results (`concatMap`

).

Sometimes the monadic type provides the *run* operation as part of its interface:

`State`

: The "primitive monadic things" are operations on a state type, returning a result and a state. The*run*operation is provided by`runState`

, wich applies a (possibly) complex monadic thing to an input state, returning a result and a (modified) state.`return`

returns its parameter, passing its input state unchanged.`(>>=)`

applies its first parameter to the input state, applies its second parameter to the result value and result state of the first.

`ST`

: The "primitive monadic things" are operations on an abstract,*encapsulated*state type. The*run*operation is provided by`runST`

, which obtains the result of an`ST`

thing, through the use of private state and an extension of the regular Haskell type system.`return`

puts its argument into a simple`ST`

thing which, if used, merely produces that original argument.`(>>=)`

establishes the ordering of`ST`

things by applying its second argument to the result of executing the first.

## Original source

- Claus Reinke on Haskell-Cafe: Monad Description For Imperative Programmer