Difference between revisions of "Monad (sans metaphors)"
(link to HaskellCafe) 
m 

(8 intermediate revisions by 5 users not shown)  
Line 1:  Line 1:  
[[Category:Glossary]] 
[[Category:Glossary]] 

+  [[Category:Monad*]] 

+  <div style="borderleft: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>[[QuotesPageDon Stewart.]]</tt> 

+  </div> 

⚫  
⚫  
⚫  
=== 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 <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 Monadic interface gives you two operations: one to throw things into a Monad thing (<hask>return</hask>), and one to chain two Monad things together <hask>(>>=)</hask>. The chaining explicitly caters for information flowing from the first to the second parameter of <hask>(>>=)</hask>. 

+  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 Monad things matters. 

−  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>). 
⚫  * State: The 

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

−  == Source == 

+  * <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 HaskellCafe: [http://www.haskell.org/pipermail/haskellcafe/2007August/029851.html Monad Description For Imperative Programmer] 
* Claus Reinke on HaskellCafe: [http://www.haskell.org/pipermail/haskellcafe/2007August/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 toMain.main
.return
puts its argument into a simpleIO
thing which, if used, merely produces that original argument.(>>=)
arrangesIO
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 byrunState
, 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 byrunST
, which obtains the result of anST
thing, through the use of private state and an extension of the regular Haskell type system.return
puts its argument into a simpleST
thing which, if used, merely produces that original argument.(>>=)
establishes the ordering ofST
things by applying its second argument to the result of executing the first.
Original source
 Claus Reinke on HaskellCafe: Monad Description For Imperative Programmer