Recall the substitution notation we have been using:

denotes the term , with each simultaneously replaced by the term . For an equational presentation , inducing a monad , consider a pair:

where denotes an equivalence class with representative term . We can then write the action of the first double strength map in terms of representatives and substitutions as:

and the second:

Therefore, the resulting monad is commutative if and only if for all , the following equality is provable in equational logic:

We shall call this **the commutativity condition**.

The substitutions can be made a bit easier to read with a change of notation. For two terms , let and be a choice of enumeration of the variables appearing in the two terms. We can then write them as

and writing substitution in the natural way, the commutativity condition becomes:

That’s an awful lot of formal notation and brackets to deal with. Let’s consider some implications of this condition to build intuition for what it means in practice.

**Example**: Consider a presentation with binary operations and . The action of the double strengths on and are:

and

If the monad is commutative, the following equality must be provable in equational logic:

As a special case of this observation, any monad presented by a binary operation must satisfy the following equation:

If is associative, this boils down to:

The only difference between the two terms is the order of the middle two constants, so this equality will certainly hold for any associative commutative binary operation. This condition is satisfied in many natural algebraic structures, and does exhibit a very weak relationship between commutative operations and commutative monads.

An instructive boundary case is the following.

**Example**: Consider two terms in which no variables appear. These are constants in our equational theory. The commutativity condition implies that:

as all the variable substitutions become trivial. **Therefore any equational presentation of a commutative monad can have at most one distinct constant term**. We have already encountered this phenomenon. The exception monad is only commutative when there is at most one exception constant.

“Constant counting” can be a quick way to discount the possibility that a monad for an equational presentation is commutative. For example the monads corresponding to commutative rings or rigs (semirings) cannot be commutative as they have distinct constant symbols for the additive and multiplicative structures. These are further natural examples of monads with all the binary operations in the signature commutative, but the resulting monads are not commutative.

Another simple case is worth considering.

**Example**: Let and be terms in which only one variable appears, say and respectively. Then the left hand side of the commutativity condition is:

and the right hand side is:

So for to be commutative, each pair of unary terms must satisfy .

As an application of this special case, we introduce another commonly considered monad. For a fixed monoid the **writer monad** has:

**Endofunctor**: The endofunctor is the product .**Unit**: .**Multiplication**: .

Computationally, this monad can be seen as encoding computations that also record some additional output such as logging. The monoid structure combines the outputs from successive computations. Algebraically, it corresponds to actions of the monoid .

The writer monad has an equational presentation with one unary operational symbol for each element of the monoid, and equations:

- .
- if and only if in the monoid.

Using our observation above, the writer monad is commutative if and only if the monoid is commutative. So in this case, we do see a strong connection between the monadic and algebraic notions of commutativity.

Another useful boundary case is to consider what the commutativity condition means for variables.

**Example**: Consider a variable and an an arbitrary term . The left hand side of the commutativity condition is:

and the right hand side is:

So variables always satisfy the commutativity condition with respect to any other term. With hindsight, maybe we should not find this too surprising.

We will now consider an important example, which will point the way to getting a better handle on the unpleasant looking general commutativity condition we deduced above.

**Example**: We now consider an equational presentation with a constant term and a binary term and a unary term . The equations yielded by the commutativity condition for the pairs and are:

we can simplify the second condition by renaming variables, and we arrive at the conditions:

These conditions should hopefully look familiar, they are exactly the equations we require for to be a homomorphism with respect to and .

We now aim to make the connection with homomorphisms from the previous example precise by making two observations:

- For positive natural and set , the term induces an -ary operation on with action .
- Similarly, the term induces an -ary function with action

The homomorphism condition is equivalent to saying that the -ary function induced by is a homomorphism with respect to the -ary operation induced by . In this sense, the commutativity conditions can be rephrased as **all the terms are homomorphisms with respect to each other**.

From the algebraic perspective **a monad is commutative in the sense that all terms can be commuted past each other as homomorphisms**.

## 2 thoughts on “Commutativity Algebraically”