Tomas Petricek, The Alan Turing Institute
tomasp.net | tomas@tomasp.net | @tomaspetricek
How monads are taught
\[\definecolor{mc}{RGB}{32,64,172}\]
A monad is a functor \({\color{mc} M} : C \rightarrow C\) together with mappings:
The mappings are required to satisfy the three monad laws:
The structure \({\color{mc}M} \alpha\) represents a data type
List int
or List string
Lazy int
or Lazy float
Lazy (List int)
Data type M a
satisfying monad laws with operations:
return
of type a -> M a
>>=
of type (a -> M b) -> (M a -> M b)
return
wraps thing in a box
>>=
applies operation on all things in a box
Plumbing for composing computations with side-effects
Plumbing for composing computations with side-effects
Plumbing for composing computations with side-effects
Plumbing for composing computations with side-effects
Neuroscientist's perspective on mathematical thinking
How and why people use monads
1: 2: 3: |
|
What if findAddress
and findNews
can fail?
1: 2: 3: 4: 5: 6: 7: |
|
1: 2: 3: |
|
Monadic notation to remove nesting & repetition:
1: 2: 3: 4: 5: |
|
Get address and compose message with city:
1: 2: 3: |
|
Does extracting getHome
function change meaning?
1: 2: 3: |
|
1: 2: 3: |
|
Write useful functions that work for any monad
Transform the thing inside the monadmapM : (a -> b) -> (M a -> M b)
Sum list and aggregate side-effectssumM : List (M int) -> M int
When monads are not the right thing
The Par
monad for modelling parallel computations
parallel : Par a -> Par b -> Par (a * b)
choose : Par a -> Par a -> Par a
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 monad-based parsers. [T]he monadic formulation [causes] the evaluation of the parser construction over and over (...).
Deterministic, Error-Correcting Combinator Parsers,
Swierstra & Duponcheel (1996)
Notation originally intended just for monads
Can be used for things that are not monadic
1: 2: 3: 4: 5: 6: 7: 8: 9: |
|
Do we need syntactic flexibility instead of monads?
What monads really are
Then came the refutationists. In their critical zeal they stretched the concept of polyhedron, to cover objects that were alien to the intended interpretation.
Proofs and refutations,
Lakatos (1979)
One of the goals of the Algol research programme was to utilize the resources of logic to increase the confidence (...) in the correctness of a program.
Science of Operations,
Priestley (2011)
This paper is about logics for reasoning about programs,
in particular for proving equivalence of programs.Notions of computations and monads,
Moggi (1991)
Reasoning about programs often appears in papers
In practice monads are just programming tool
The cost of abstraction is rarely debated
Gentlemen do not talk about syntax
Before thinking about the philosophy of experiments we should record a certain class or caste difference between the theorizer and the experimenter. It has little to do with philosophy. We find prejudices in favour of theory, as far back as there is institutionalized science.
Representing and intervening,
Hacking (1983)
Galileo replaces one natural interpretation by a very different and as yet at least partly unnatural interpretation. (...) Galileo uses propaganda; he uses psychological tricks, in addition to whatever intellectual reasons he has to offer.
Against method,
Feyerabend (1975)
Community finds monads an attractive topic
They are unnatural until you get them
Popularity means not all uses are justified
Monads are not what they seem
Metaphors guide our understanding
Concepts often change their meaning
Paradigms and propaganda matter
Applies to other concepts. Bigger picture?
The Par
monad is a joinad, which captures the more interesting aspects
Also write about dataflow (is it monad or a comonad?)
Monads were supposed to be used for reasoning about effects, but instead they are used to introduce effects. Where does the "reasoning" meme come from? How much it is actually done in practice? (Algol?)