Difference between revisions of "Monad (sans metaphors)"
m (Reverted edits by Tomjaguarpaw (talk) to last revision by Lemming) 
(Various formatting and other changes) 

Line 3:  Line 3:  
=== Introduction === 
=== Introduction === 

−  
⚫  
⚫  
=== 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 == 
== 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] 
Revision as of 08:11, 21 September 2021
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.
Source
 Claus Reinke on HaskellCafe: Monad Description For Imperative Programmer