(under construction)
Part 4 of a short course on monads.
We spent one lecture on the Eilenberg-Moore definition of monads. In a second lecture, we will look at the Kleisli definition.
For self study, I augmented the lecture notes with some videos. First, I made a video summarizing the previous lectures, retaining only the minimum needed to motiviate the mathematical definition of a monad. After that, one should study the definition of a monad and show that List
is a monad. [1] Try yourself, but I also made a video.
(Programs: I should add some new ones …)
Going back to the three notations for monoids we notice the following. In Notation 3, the operations and equations change with each signature. In Notation 1 a 2, the operations and equations are independent of any reference to monoids.
This opens up the possibility of treating all algebraic theories in a common abstract framework and has been equally important for mathematics and software engineering.'
This section assumes that the reader remembers the section on the free monoid and/or its Haskell implementation monoid.hs.
Definition: A monad
Exercise: Instantiate the definition with the list/free monoid monad. In other words, verify that in (List, eta, mu)
as defined in monoids
is a monad.
Answer. [2]
Remark: The definition of a monad can be equivalently formulated by saying that a monad is a monoid in the category of endofunctors. To make this precise, we need to explain what a monoid in a monoidal category is. This is a bit of a detour, so I'll skip it for now. But if you squeeze your eyes, you will be able to see the two triangles as the left and right identity laws of a monoid and the square as the associativity law.
I advertised the notion of monad as an axiomatisation of the idea of algebraic structure. So far, we defined a monad
Optional maths exercise: Show that our observation above on monoids generalises to all algebras specified by operations and equations. To get started, define
Definition: A monad in Kleisli form
Exercise: Let
Exercise: (For Haskell programmers). Explain in which sense a monad in Kleisli form and a monad in Haskell are the same concept.
We can use this opportunity to illustrate that category theory is an abstrac theory of structure by instantiating the notion of a monad in different "base" categories.
In sets, a monad can be identified with class of algebras defined by operations and equations. [3]
It is quite interesting that many important functors on
Which structure turns
We have seen previously that the operation
This idea also extends to probability distributions (Giry monad).
But in general, to carry the structure of a monad is quite a special property for a functor. For example, functors do always compose, but monads do not. [4]
Is Haskell a category? There is some debate on this. My take is that full Haskell is clearly not a category (as demonstrated in the blog linked above), but that there are various fragments of Haskell that come close enough. In any case, when implementing category theory in Haskell we need to keep an eye on possible mismatches. For example, Haskell is clearly not the category
For example List a
is a functor in Haskell. List a
is a monad in Haskell. Moreover, they are functors/monads in
Conversely, many monads known in Haskell such as the error monad or the state monad also correspond to monads in
Monads in
Let
Exercise: Show that such a relation
There is a lot to choose from. We could prove the claims about the examples above. We could prove that the Eilenberg-Moore and the Kleisli definitions are equivalent. Maybe a sketch of this would be helpful. And explain why monads in mathematics and monads in programming look quite different.
… too many diagrams for markdown latex but I will draw them by hand in the lecture … trying to tie everything we learned together …
Definition: Let
…
The category of
Example: Let
Example: Every
Definition:
…
I denote category of Eilenberg-Moore algebars for the monad
Example: We have seen that lists, errors and a certain kind of binary trees are monads. It may be a good exercise to write down the respective
Definition: Kleisli category …
Theorem: (Lawvere 1963, Linton 1969) Let
The previous theorem suggests that algebras for a monad are more general than algebras for a functor. Intuitively, while the functor encodes a signature (=operations), a monad also allows us take into account equations. Given a functor
The answer is that the equation holds if
In fact, in Haskell we can describe the free monad over a functor in more familiar terms. Let us first assume that we have defined
data functor x = ...
Then the free monad over functor
is
data free_monad a = Var a | Node (functor (free_monad a))
In fact, we have already seen an example in the file magma
.
Theorem: The embedding of
We have seen that monads unify such different areas as universal algebra and effects in programming languages. While monads provide an important perspective, these areas can be studied without monads. The reason is that monads provide a common abstraction (a typeclass in Haskell), but as long as one is interested only in one particular instance, the more abstract perspective can always be "compiled away".
Thus, mondads are more likely to show their true power in situations where one wants to study several (or all) instances simultaneously.
One example is the study of the category of general effects in programming languages. From a theoretical point of view, monads allow us to define a category of effects and study it. From a software engineering perspective, monads provide a common interface for a wide range of different applications with all the common benefits such as code reuse, maintainability, etc.
Many examples in both mathematics and theoretical computer science arise from the study of algebras over different base categories. Monads over the category
In fact, some of the examples above are of this nature. I mention a few more.
In algebraic topology one wants to compute so-called homology groups in order to classify different spaces according to the holes they have (make the donut/cup example). This requires counting how often one goes around a loop and thus requires some kind of group structure. So we end up with monads on categories enriched over abelian groups.
In programming languages, we may be interested in replacing
In fact, these two applications are typical. Monads were invented for applications to homology and then later introduced to computer science for their use in denotational semantics of programming languages.
What would come next?
If this was a
category theory course we would do limits, colimits, adjoints, completion of categories.
semantics of programming languages course, we would do typed lambda calculus, cartesian closed categories, PCF, domain theory.
a mathematics course we would do abelian categories, 2-categories, enriched categories, higher categories.
type theory course, we would do dependent types and some theorem proving in a language such as Lean, Idris, Agda, Coq, Isabelle.
foundations of mathematics/categorical logic course, we would build up from equational logic to higher-order intuitionistic predicate calculus through a sequence of richer and richer categories, from Lawvere theories, essentially algebraic theories, geometric theories to toposes.
The definition of List
is available in Haskell in any of the two files monoids
and monoidshs
. ↩︎
I made a video on why List is a monad. The main insight is condensed in the following picture (I write
Just think of
This is a theorem due to Lawvere and then Linton. See below for more details. ↩︎
It is actually an interesting exercise to start out with two monads
The deeper point here is that monads abstract the structure that allows us to deal with "effects". This applies to all programming languages, it is just that haskellers found this perspective more useful than others. (It could be interesting to discuss: Why?) ↩︎
Actually, to be more precise, we need something like a 2-category or bicategory here, but that would lead us too far astray for now. ↩︎
This really needs a bit more explanation. I may put more details later. ↩︎
To make this statement precise we should ask the monad to be "finitary" or allow the signature to have a proper class of operations of infinitary arity. ↩︎