It’s about time I made my contribution to the zoo of monad tutorials. After explaining the concept of monads to somebody who is not a programmer or a mathematician, I was told that what I had explained sounded exactly like a game of dominoes.
However, instead of explaining monads in terms of a cute metaphor about dominoes, I’m just going to throw you into the deep end. Deep breath!
Definition: A category is given by objects and arrows between these objects. Each arrow has a unique source object and a target object . For every three objects , , and , and arrows and , there is a composite arrow
Composition is associative: if , , and , then
There exists a unique identity arrow for each object For every object , there exists an arrow such that for every arrow , we have
A category for dominoes:
The objects of the category are the natural numbers. An arrow in the category is a domino piece with two numbers on it: . A composition of arrows is two domino pieces placed together end-to-end by the following equation:
The identity arrow for each object is a domino piece with the same number on both ends.
A monad for dominoes
Definition: A monad is given by a functor (from a category to itself, i.e. an endofunctor in ), equipped with natural transformations:
where is the identity functor and x is functor composition. This must satisfy the axioms that and are identity transformations, and (i.e. is associative).
The dominoes need not be annotated with numbers, but might be annotated with any set of objects. Therefore, there exists an endofunctor on the category of sets, such that for all sets , we can construct the set of dominoes .
For every object in , we can construct a domino . This is for our monad. Then, for the set of dominoes , we can construct which is the set of dominoes annotated with other dominoes. For any pair of dominoes in , composed end-to-end, we can see it as a single domino in . The operation in our monad is then the fact that for any such pair of dominoes , it behaves as the domino with regard to composition with other dominoes in .
Exercises left to the reader:
1. What does a monoid in the category of dominoes look like?
2. How would the game of dominoes change if it allowed products and coproducts?
3. Can you come up with a coherent theory describing the behavior of “spinners” in the game of dominoes?