We saw the two double strength natural transformations in the previous post:
A commutative monad is a strong monad for which . We saw last time that the list monad is not commutative, but the powerset monad is. In this post we will restrict ourselves to examining some more instructive examples. This will help build our intuitions, and the examples lay the groundwork for discussions in later posts.
Example: For a set , there is a monad with:
- Endofunctor:
.
- Unit: The unit maps an element into the left component of the coproduct
.
- Multiplication: The multiplication:
does the “obvious thing”,
,
and
.
The monad is sometimes referred to as the exception monad. Computationally, we can interpret a Kleisli morphism as a function that transforms elements of
to elements of
, but may return error or exception values captured by
. The first double strength for this monad is defined by the following cases:
.
.
.
.
Notice that exception values are preferred, but there is rather arbitrary choice that has to be made in the fourth case. The second double strength agrees with the first, except for the final case, where it makes the other choice of exception to prefer:
.
So we see that the exception monad is only commutative if there’s exactly one exception. This special case is sometimes referred to as the maybe monad, as computationally it encodes functions that may fail.
Example: The multiset monad is commutative. To describe this, we shall introduce the notation
for a multiset where element appears with multiplicity
. The action of both double strength maps sends the pair of multisets:
to the multiset:
.
The multiset monad is (isomorphic to) the Abelian monoid monad, so this monad is also commutative.
Example: Another monad that occurs commonly in practice is the finite probability monad on . This has:
- Endofunctor:
has elements finitely supported formal convex sums
. These are weighted sum of elements of
such that for each weight
,
,
and only finitely many
are non-zero. Alternatively, these can be thought of as functions
satisfying the previous conditions on the weights.
- Unit:
. That is, the unit maps an element of
to the corresponding trivial sum.
- Multiplication:
. This is simply flattening out a sum of sums.
This monad is commutative, with the action of both double strengths being:
For example:
In the next post we will explore a slightly misleading intuition that is commonly hinted at in the literature.
5 thoughts on “Commutative Monads”