Tomas Petricek, The Alan Turing Institute
tomasp.net | tomas@tomasp.net | @tomaspetricek
\[\definecolor{mc}{RGB}{137,64,96} \newcommand{\mbnd}{>\!\hspace{-0.25em}>\!\hspace{-0.27em}=}\]
A monad is just a monoid in the category
of endofunctors. What is the problem?
From category theory to programming
A monad over a category \(\mathcal{C}\) is a triple \((T, \eta, \mu)\) where
\(T : \mathcal{C} \rightarrow \mathcal{C}~\) is a functor, \(\eta : {Id}_{\mathcal{C}} \rightarrow T\) and
\(\mu : T^2 \rightarrow T\) are natural transformations such that:\[\begin{array}{l} \mu_{A} \circ T \mu_A = \mu_{A} \circ \mu_{T A} \\ \mu_{A} \circ \eta_{T A} = \mathit{id}_{T A} = \mu_{A} \circ T \eta_{A} \end{array}\]
A monad is a triple \((M, \mathit{unit}, \mbnd)\) consisting of a type
constructor \(M\) and two operations of the following types:\[\begin{array}{lcl} \mbnd &::& M x \rightarrow (x\rightarrow M y) \rightarrow M y\\ \mathit{unit} &::& x\rightarrow M x\\ \end{array}\]
These operations must satisfy the following laws:
\[\begin{array}{l} \mathit{unit}~a~\mbnd~f ~=~ f a\\ m~\mbnd~\mathit{unit} ~=~ m\\ (m~\mbnd~f)~\mbnd~g ~=~ m~\mbnd~(\lambda x.f x~\mbnd~g) \end{array}\]
Reasoning about effects vs. Introducing effects
A priori knowledge vs. A posteriori knowledge
Understanding monads using metaphors
Data type
M a
with the two operations satisfying monad laws:Operation
return
has a typea -> M a
Operation>>=
has a type(a -> M b) -> (M a -> M b)
One of the principal results in cognitive science is that abstract concepts are typically understood, via metaphor, in terms of more concrete concepts.
(Lakoff & Núñez, 2000)
Metaphors link abstract concepts with bodily experience
Reasoning about programs with monads
This paper is about logics for reasoning about programs,
in particular for proving equivalence of programs.Moggi (1991)
\({\color{mc} \mathit{if}}~\,b~{\color{mc} \mathit{then}}~\,p~{\color{mc} \mathit{else}}~p = p\)
\((f^\ast \circ g^\ast) \circ h = f^\ast \circ (g^\ast \circ h)\)
\(\mathit{unit}^\ast \circ f = f = f^\ast \circ \mathit{unit}\)
One of the goals of the Algol programme was to utilize the resources of logic to increase the confidence (...) in the correctness of a program.
(Priestley, 2011)
From abstractions to syntactic sugar
1:
|
|
1: 2: 3: |
|
1:
|
|
1: 2: 3: 4: |
|
1: 2: 3: 4: 5: 6: |
|
1:
|
|
1: 2: 3: |
|
Mathematics does not grow through increase of the number of established theorems, but through improvement by speculation and criticism, by the method of proofs and refutations.
Lakatos (1979)
The nature of programming entities
Intuitively understanding concepts
Implementing things in programs
Reasoning and proving about programs
Motivation at formal level
Monads are logic for reasoning about effects
Used differently for implementation
Language abstraction for encoding effects
Shift at implementation level
Abstraction and notation for effects
Causes adaptation at metaphorical level
Think of monads as railway tracks
Monads as religious objects
A case for wider understanding
Monad is a resource of logic
Monads are cool and exciting
Monads are discovered, not invented!
The Par
monad for modelling parallel computations
spawn : Par a -> Par (IVar a)
get : IVar a -> Par a
and put : IVar a -> a -> Par ()
Also supports monadic return : a -> Par a
and >>= : (a -> Par b) -> (Par a -> Par b)
Parser a
reads input string and produces value a
Parse one thing and then another thing
Parser a -> Parser b -> Parser (a * b)
Try parsing in two ways, use the first success
Parser a -> Parser a -> Parser a
Parsers can be extended to support monadic >>=
and return
.
The normal disadvantages of conventional [monadic] parsers,
such as their lack of speed and their poor error reporting are remedied.The techniques [do not] extend to monadic parsers. [T]he monadic formu-lation [causes] the evaluation of the parser construction over and over (...).
Swierstra & Duponcheel (1996)
Wadge proposed that the semantics of the dataflow language Lucid (...), could be structured by a monad.
Ten years later, Uustalu and Vene gave a semantics for Lucid in terms of a comonad, and stated that "dataflow cannot be structured with monads".
Orchard (2012)
1:
|
|
What we talk about when we talk about monads
Strong roots in the Algol paradigm
Meaning evolves at three levels
Metaphors are a fundamental part
Tomas Petricek, The Alan Turing Institute
tomasp.net | tomas@tomasp.net | @tomaspetricek