# Part II: Free Monoids

## String Diagrams

The utility of diagrams in formulating and proving theorems in category theory cannot be overemphasized. While working my way through the construction of free monoids, I noticed that there was a particular set of manipulations that had to be done algebraically, with little help from diagrams. These were operations involving a mix of tensor products and composition of morphisms. Tensor product is a bifunctor, so it preserves composition; which means you can often slide products through junctions of arrows—but the rules are not immediately obvious. Diagrams in which objects are nodes and morphisms are arrows have no immediate graphical representation for tensor products. A thought occurred to me that maybe a dual representation, where morphisms are nodes and objects are edges would be more accommodating. And indeed, a quick search for “string diagrams in monoidal categories” produced a paper by Joyal and Street, “The geometry of tensor calculus.”

The idea is very simple: if you represent morphisms as points on a plane, you have two additional dimensions to play with composition and tensoring. Two morphism—represented as points—can be composed if they share an object, which can be represented as a line connecting them. By convention, we read composition from the bottom of the diagram up. We follow lines as they go through points—that’s composition. Two lines ascending in parallel represent a tensor product. The geometry of the diagram just works!

Let me explain it on a simple example—the left unit law for a monoid $(m, \pi, \mu)$:

$\mu \circ (\pi \otimes m) = id$

The left hand side is a composition of two morphisms. The first morphism $\pi \otimes m$ starts from the object $I \otimes m$ (see Fig. 12).

Fig. 12. Left unit law

In principle, there should be two parallel lines at the bottom, one for $I$ and one for $m$; but $I \otimes m$ is isomorphic to $m$, so the $I$ line is redundant and can be omitted. Scanning the diagram from the bottom up, we encounter the morphism $\pi$ in parallel with the $m$ line. That’s exactly the graphical representation of $\pi \otimes m$. The output of $\pi$ is also $m$, so we now have two upward moving $m$ lines, corresponding to $m \otimes m$. That’s the input of the next morphism $\mu$. Its output is the single upward moving $m$. The unit law may be visualized as pulling the two $m$ strings in opposite direction until the whole diagram is straightened to one vertical $m$ string corresponding to $id_m$.

Here’s the right unit law:

$\mu \circ (m \otimes \pi) = id$

It works like a mirror image of the left unit law:

Fig. 13. Right unit law

The associativity law can be illustrated by the following diagram:

Fig. 14. Associativity law

The important property of a string diagram is that, because of functoriality, its value—the compound morphism it represents—doesn’t change under continuous transformations.

## Monoid

First we’d like to show that the carrier of the free $h$-algebra $(m, \sigma)$ which, as we’ve seen before, is also the initial algebra for the list functor $I + h \otimes -$, is automatically a monoid. To show that, we need to define its unit and multiplication—two morphisms that satisfy monoid laws. The obvious candidate for unit is the universal mapping $\pi \colon I \to m$. It’s the morphism in the definition of the free algebra from the previous post (see Fig 15).

Fig. 15. Free $h$-algebra $(m, \sigma)$ generated by $I$

Multiplication is a morphism:

$\mu \colon m \otimes m \to m$

which, if you think of a free monoid as a list, is the generalization of list concatenation.

The trick is to show that $m \otimes m$ is also a free $h$-algebra whose generator is $m$ itself. We could then use the universality of $m \otimes m$ to generate the unique algebra morphism from it to $m$ (which is also an $h$-algebra). That will be our $\mu$.

Proposition. {Monoid.}

The free $h$-algebra $(m, \sigma)$ generated by the unit object $I$ is a monoid whose unit is:

$\pi \colon I \to m$

and whose multiplication:

$\mu \colon m \otimes m \to m$

is the unique $h$-algebra morphism

$(m \otimes m, \sigma \otimes m) \to (m, \sigma)$

induced by the identity morphism $id_m$.

Proof.
In the previous post we’ve shown that, if $(m, \sigma)$ is a free algebra generated by the unit object $I$ with the universal map $\pi$, then $(m \otimes k, \sigma \otimes k)$ is a free algebra generated by $k$ with the universal map $\pi \otimes k$ (see Fig. 16).

Fig. 16. Free $h$-algebra generated by $k \cong I \otimes k$

We get $\mu$ by redrawing this diagram: using $m$ as both the generator and the target algebra, and replacing $f$ with $id_m$ (see Fig. 17):

Fig. 17. Monoid multiplication as an $h$-algebra morphism

Since so defined $\mu$ is an $h$-algebra morphism, it makes the following diagram, Fig. 18, commute.

Fig. 18. $\mu$ is an $h$-algebra morphism

This commuting condition can be redrawn as the identity of two string diagrams (Fig. 19) corresponding to the two paths through the original diagram.

Fig. 19. String diagram showing that $\mu$ is an algebra morphism

The universal condition in Fig. 17:

$\mu \circ (\pi \otimes m) = id_m$

gives us immediately the left unit law for the monoid.

The right unit law:

$\mu \circ (m \otimes \pi) = id_m$

requires a little more work.

There is a standard trick that we can use to show that two morphisms, whose source (in this case $m$) is a free algebra, are equal. It’s enough to prove that they are algebra morphisms, and that they are both induced by the same morphism (in this case $\pi$). Their equality then follows from the uniqueness of the universal construction.

We know that $\mu$ is an algebra morphism so, if we can show that $m \otimes \pi$ is also an algebra morphism, their composition will be an algebra morphism too. Trivially, $id_m$ is an algebra morphism so, if we can show that the two are induced by the same regular morphism $\pi$, then they must be equal.

To show that $m \otimes \pi$ is an $h$-algebra morphism, we have to show that the diagram in Fig. 20 commutes.

Fig. 20. $m \otimes \pi$ as an $h$-algebra morphism

We can redraw the two paths through Fig. 20 as two string diagrams in Fig. 21. They are equal because they can be deformed into each other.

Fig. 21. String diagram showing that $m \otimes \pi$ is an algebra morphism

Therefore the composition $\mu \circ (m \otimes \pi)$ is also an $h$-algebra morphism. The string diagram that illustrates this fact is shown in Fig. 22.

Fig. 22. String diagram showing that $\mu \circ (m \otimes \pi)$ is an algebra morphism

Since the identity $h$-algebra morphism is induced by $\pi$, we’d like to show that $\mu \circ (m \otimes \pi)$ is also induced by $\pi$ (Fig. 23).

Fig. 23. Universal property of the free $h$-algebra generated by $I$, with the algebra morphism induced by $\pi$

To do that, we have to prove the universal condition in Fig. 23:

$\mu \circ (m \otimes \pi) \circ \pi = \pi$

This is represented as a string diagram identity in Fig. 24. We can deform this diagram by sliding the left $\pi$ node up, past the right $\pi$ node, and then using the left identity.

Fig. 24. Universal condition in Fig. 23.

This concludes the proof of the right identity.

The proof of associativity is very similar, so I’ll just sketch it. We have to show that the two diagrams in Fig. 14 are equal. We’ll use the same trick as before. We’ll show that they are both algebra morphisms. Their source is a free algebra generated by $m \otimes m$ (see Fig. 25—the other diagram has $\mu \circ (\mu \otimes m)$ replaced by $\mu \circ (m \otimes \mu)$). The universal condition follows from the unit law for $m$. Associativity condition:

$\mu \circ (\mu \otimes m) = \mu \circ (m \otimes \mu)$

will then follow from the uniqueness of the universal construction.

Fig. 25. One part of associativity as an $h$-algebra morphism

You can easily convince yourself that showing that something is an $h$-algebra morphism can be done by first attaching the $h$ leg to the left of the string diagram and then sliding it to the top of the diagram, as illustrated in Fig. 26. This can be accomplished by repeatedly using the fact that $\mu$ is an $h$-algebra morphism.

Fig. 26. String diagram showing that one of the associativity diagrams is an $h$-algebra morphism

The same process can be applied to the second associativity diagram, thus completing the proof.

$\square$

For Haskell programmers, recall from the previous post our construction of the free $h$-algebra generated by $k$ and the derivation of the algebra morphism $g$ from it to the internal-hom algebra:

g :: Expr -> (k -> n)
g () = f
g a  = k -> nu (a, f k)
g (a, b) = k -> nu (a, nu (b, f k))
...


In the current proof we have replaced $k$ with $m$, which generalizes the list of $h$s, $f$ became $id$, and $\nu$ is a function that prepends an element to a list. In other words, $g$ concatenates its list-argument in front of the second list, and it does it in the correct order.

## Free Monoid

The monoid whe have just constructed from the free algebra is a free monoid. As we did with free algebras, instead of using the free/forgetful adjunction to prove it, we’ll use the free-object universal construction.

Theorem. {Free Monoid.}

The monoid $(m, \pi, \mu)$ is a free monoid generated by $h$, with a universal mapping given by $u = \sigma \circ (h \otimes \pi)$:

That is, for any monoid $(n, \eta, \nu)$ and any morphism $s \colon h \to n$, there is a unique monoid morphism $t$ from $(m, \pi, \mu)$ to $(n, \eta, \nu)$ such that the universal condition holds:

$t \circ u = s$

(see Fig. 27).

Fig. 27. Free monoid diagram

Proof. Recall that $(m, \sigma)$ is a free $h$-algebra generated by $I$. It turns out that any monoid $(n, \eta, \nu)$, for which there is a morphism $s \colon h \to n$, is automatically a carrier of an $h$-algebra. We construct its structure map $\lambda$ by combining $s$ with monoid multiplication $\nu$:

We can use $n$‘s monoidal unit $\eta$ to insert $I$ into $n$. Because $(m, \sigma)$ is a free $h$-algebra, there is a unique algebra morphism, let’s call it $t$, from it to $(n, \lambda)$, which is induced by $\eta$, such that $t \circ \pi = \eta$ (see Fig. 28). We want to show that this algebra morphism is also a monoid morphism. Furthermore, if we can show that this is the unique monoid morphism induced by $s$, we will have a proof that $m$ is a free monoid.

Fig. 28. Algebra morphism between monoids

Since $t$ is an algebra morphism, the rectangle in Fig. 29 commutes.

Fig. 29. $t$ is an $h$-algebra morphism

Let’s redraw it as an identity of string diagrams, Fig. 30. We’ll make use of it in a moment.

Fig. 30. $t$ is an $h$-algebra morphism

Going back to Fig. 27, we want to show that the universal condition holds, which means that we want the diagram in Fig. 31 to commute (I have expanded the definition of $u$).

Fig. 31. Free monoid universal condition

In other words we want show that the following two string diagrams are equal:

Fig. 32. Free monoid universal condition

Using the identity in Fig. 30, the left hand side can be rewritten as:

Fig. 33. Step 1 in transforming Fig 32

The right leg can be shrunk down to $\eta$ using the universal condition in Fig. 28:

$t \circ \pi = \eta$

which, incidentally, also expresses the fact that $t$ preserves the monoidal unit.

Finally, we can use the right unit law for the monoid $n$, Fig. 34,

Fig. 34. Right unit law for monoid $n$

to arrive at the right hand side of the identity in Fig. 32. This completes the proof of the universal condition in Fig. 27.

Now we have to show that $t$ is a full-blown monoid morphism, that is, it preserves multiplication (Fig. 35).

Fig. 35. Preservation of multiplication

The corresponding string diagrams are shown in Fig. 36.

Fig. 36. Preservation of multiplication

Let’s start with the fact that $m \otimes m$ is the free $h$-algebra generated by $m$. We will show that the two paths through the diagram in Fig. 35 are both $h$-algebra morphisms, and that they are induced by the same regular morphism $t \colon m \to n$. Therefore they must be equal.

The bottom path in Fig. 35, $t \circ \mu$, is an $h$-algebra morphism by virtue of being a composition of two $h$-algebra morphisms. This composite is induced by morphism $t$ in the diagram Fig. 37.

Fig. 37. $h$-algebra morphism $t \circ \mu$

The universal condition in Fig. 37 follows from the diagram in Fig. 38, which follows from the left unit law for the monoid $(m, \pi, \mu)$.

Fig. 38. Universal condition in Fig. 37

We want to show that the top path in Fig. 35 is also an $h$-algebra morphism, that is, the diagram in Fig. 39 commutes.

Fig. 39. $h$-algebra morphism diagram for $\nu \circ (t \otimes t)$

We can redraw this diagram as a string diagram identity in Fig. 40.

Fig. 40. $h$-algebra morphism diagram for $\nu \circ (t \otimes t)$

First, let’s use the associativity law for the monoid $n$ to transform the left hand side. We get the diagram in Fig. 41.

Fig. 41. After applying associativity, we can apply Fig. 30

We can now apply the identity in Fig. 30 to reproduce the right hand side of Fig. 40.

We have thus shown that both paths in Fig. 35 are algebra morphisms. We know that the bottom path is induced by morphism $t$. What remains is to show that the top path, which is given by $\nu \circ (t \otimes t)$ is induced by the same $t$. This will be true, if we can show the universal condition in Fig. 42.

Fig. 42. $h$-algebra morphism $\nu \circ (t \otimes t)$

This universal condition can be expanded to the diagram in Fig. 43.

Fig. 43. Universal condition in Fig. 42

Here’s the string diagram that traces the path around the square (Fig. 44).

Fig. 44. Path around Fig. 43 as a string diagram

First, let’s use the preservation of unit by $t$, Fig. 45, to shrink the left leg,

Fig. 45. Preservation of unit by $t$

and follow it with the left unit law for the monoid $(n, \eta, \nu)$ (Fig. 46). The result is that Fig. 44 shrinks to the single morphism $t$, thus making Fig. 43 commute.

Fig. 46. Left unit law for the monoid $n$

This completes the proof that $t$ is a monoid morphism.

The final step is to make sure that $t$ is the unique monoid morphism from $m$ to $n$. Suppose that there is another monoid morphism $t'$ (replacing $t$ in Fig. 28). If we can show that $t'$ is also an $h$-algebra morphism induced by the same $\eta$, it will have to, by universality, be equal to $t$. In other words, we have to show that the diagram in Fig. 28 also works for $t'$. Our assumptions are that both $t$ and $t'$ are monoid morphisms, that is, they preserve unit and multiplication; and they both satisfy the universal condition in Fig 27. In particular, $t'$ satisfies the condition in Fig. 47.

Fig. 47. Free monoid universal condition for $t'$ as a string diagram

Notice that, in the first part of the proof, we started with an $h$-algebra morphism $t$ and had to show that it’s a monoid morphism. Now we are going in the opposite direction: we know that $t'$ is a monoid morphism, and have to show that it’s an $h$-algebra morphism, and that the universal condition in Fig. 28

$t' \circ \pi = \eta$

holds. The latter simply restates our assumption that $t'$ preserves the unit.

To show that $t'$ is an algebra morphism, we have to show that the diagram in Fig 48 commutes.

Fig. 48. $t'$ as an $h$-algebra morphism

This diagram may be redrawn as a pair of string diagrams, Fig 49.

Fig. 49. $t'$ as an algebra morphism

The proof of this identity relies on redrawing string diagrams using the identities in Figs. 12, 19, 36, and 47. Before we continue, you might want to try it yourself. It’s an exercise well worth the effort.

We start by expanding the $s$ node using the diagram in Fig. 47 to get Fig. 50.

Fig. 50. After expanding the left leg of the diagram in Fig. 49, we can apply preservation of multiplication by $t'$.

We can now use the preservation of multiplication by $t'$ to obtain Fig 51.

Fig. 51. Applying the fact that $\mu$ is an $h$-algebra morphism

Next, we can use the fact that $\mu$ is an $h$-algebra morphism, Fig. 19, to slide the $\sigma$ node up, and obtain Fig. 52.

Fig. 52. Applying left unit law

We can now use the left unit law for the monoid $m$:

$\mu \circ (\pi \otimes m) = id$

as illustrated in Fig. 12, to arrive at the right hand side of Fig. 49.

This concludes the proof that $t'$ must be equal to $t$.

$\square$

## Conclusion

To summarize, we have shown that the free monoid can be constructed from a free algebra of the functor $h \otimes -$. This is a very general result that is valid in any monoidal closed category. Earlier we’ve seen that this free algebra is also the initial algebra of the list functor $I + h \otimes -$. The immediate consequence of this theorem is that it lets us construct free monoids in functor categories with interesting monoidal structures. In particular, we get a free monad as a free monoid in the category of endofunctors with functor composition as tensor product. We can also get a free applicative, or free lax monoidal functor, if we define the tensor product as Day convolution—the latter can be also constructed in the profunctor category.

## Preface

In my previous blog post I used, without proof, the fact that the initial algebra of the functor $I + h \otimes -$ is a free monoid. The proof of this statement is not at all trivial and, frankly, I would never have been able to work it out by myself. I was lucky enough to get help from a mathematician, Alex Campbell, who sent me the proof he extracted from the paper by G. M. Kelly (see Bibliography).

I worked my way through this proof, filling some of the steps that might have been obvious to a mathematician, but not to an outsider. I even learned how to draw diagrams using the TikZ package for LaTeX.

What I realized was that category theorists have developed a unique language to manipulate mathematical formulas: the language of 2-dimensional diagrams. For a programmer interested in languages this is a fascinating topic. We are used to working with grammars that essentially deal with string substitutions. Although diagrams can be serialized—TikZ lets you do it—you can’t really operate on diagrams without laying them out on a page. The most common operation—diagram pasting—involves combining two or more diagrams along common edges. I am amazed that, to my knowledge, there is no tool to mechanize this process.

In this post you’ll also see lots of examples of using the same diagram shape (the free-construction diagram, or the algebra-morphism diagram), substituting new expressions for some of the nodes and edges. Not all substitutions are valid and I’m sure one could develop some kind of type system to verify their correctness.

Because of proliferation of diagrams, this blog post started growing out of proportion, so I decided to split it into two parts. If you are serious about studying this proof, I strongly suggest you download (or even print out) the PDF version of this blog.

# Part I: Free Algebras

## Introduction

Here’s the broad idea: The initial algebra that defines a free monoid is a fixed point of the functor $I + h \otimes -$, which I will call the list functor. Roughly speaking, it’s the result of replacing the dash in the definition of the functor with the result of the replacement. For instance, in the first iteration we would get:

$I + h \otimes (I + h \otimes -) \cong I + h + h \otimes h \otimes -$

I used the fact that $I$ is the unit of the tensor product, the associativity of $\otimes$ (all up to isomorphism), and the distributivity of tensor product over coproduct.

Continuing this process, we would arrive at an infinite sum of powers of $h$:

$m = I + h + h \otimes h + h \otimes h \otimes h + ...$

Intuitively, a free monoid is a list of $h$s, and this representation expresses the fact that a list is either trivial (corresponding to the unit $I$), or a single $h$, or a product of two $h$s, and so on…

Let’s have a look at the structure map of the initial algebra of the list functor:

$I + h \otimes m \to m$

Mapping out of a coproduct (sum) is equivalent to defining a pair of morphisms $\langle \pi, \sigma \rangle$:

$\pi \colon I \to m$

$\sigma \colon h \otimes m \to m$

the second of which may, in itself, be considered an algebra for the product functor $h \otimes -$.

Our goal is to show that the initial algebra of the list functor is a monoid so, in particular, it supports multiplication:

$\mu \colon m \otimes m \to m$

Following our intuition about lists, this multiplication corresponds to list concatenation. One way of concatenating two lists is to keep multiplying the second list by elements taken from the first list. This operation is described by the application of our product functor $h \otimes -$. Such repetitive application of a functor is described by a free algebra.

There is just one tricky part: when concatenating two lists, we have to disassemble the left list starting from the tail (alternatively, we could disassemble the right list from the head, but then we’d have to append elements to the tail of the left list, which is the same problem). And here’s the ingenious idea: you disassemble the left list from the head, but instead of applying the elements directly to the right list, you turn them into functions that prepend an element. In other words you convert a list of elements into a (reversed) list of functions. Then you apply this list of functions to the right list one by one.

This conversion is only possible if you can trade product for function — the process we know as currying. Currying is possible if there is an adjunction between the product and the exponential, a.k.a, the internal hom, $[k, n]$ (which generalizes the set of functions from $k$ to $n$):

$C(m \otimes k, n) \cong C(m, [k, n])$

We’ll assume that the underlying category $C$ is monoidal closed, so that we can curry morphisms that map out from the tensor product:

$g \colon m \otimes k \to n$

$\bar{g} \colon m \to [k, n]$

(In what follows I’ll be using the overbar to denote the curried version of a morphism.)

The internal hom can also be defined using a universal construction, see Fig. 1. The morphism $eval$ corresponds to the counit of the adjunction (although the universal construction is more general than the adjunction).

Fig. 1. Universal construction of the internal hom $[k, n]$. For any object $m$ and a morphism $g \colon m \otimes k \to n$ there is a unique morphism $\bar{g}$ (the curried version of $g$) which makes the triangle commute.

The hard part of the proof is to show that the initial algebra produces a free monoid, which is a free object in the category of monoids. I’ll start by defining the notion of a free object.

## Free Objects

You might be familiar with the definition of a free construction as the left adjoint to the forgetful functor. Fig 2 illustrates the essence of such an adjunction.

The left hand side is in some category $D$ of structured objects: algebras, monoids, monads, etc. The right hand side is in the underlying category $C$, often the category of sets. The adjunction establishes a one-to-one correspondence between sets of morphisms, of which $g$ and $f$ are examples. If $U$ is the forgetful functor, then $F$ is the free functor, and the object $F x$ is called the free object generated by $x$. The adjunction is an isomorphism of hom-sets, natural in both $x$ and $z$:

$D(F x, z) \cong C(x, U z)$

Unfortunately, this description doesn’t quite work for some important cases, like free monads. In the case of free monads, the right category is the category of endofunctors, and the left category is the category of monads. Because of size issues, not every endofunctor on the right generates a free monad on the left.

It turns out that there is a weaker definition of a free object that doesn’t require a full blown adjunction; and which reduces to it, when the adjunction can be defined globally.

Let’s start with the object $x$ on the right, and try to define the corresponding free object $F x$ on the left (by abuse of notation I will call this object $F x$, even if there is no functor $F$). For our definition, we need a condition that would work universally for any object $z$, and any morphism $f$ from $x$ to $U z$.

We are still missing one important ingredient: something that would tell us that $x$ acts as a set of generators for $F x$. This property can be expressed by providing a morphism that inserts $x$ into $U (F x)$—the object underlying the free object. In the case of an adjunction, this morphism happens to be the component of the unit natural transformation:

$\eta \colon Id \to U \circ F$

where $Id$ is the identity functor (see Fig. 3).

The important property of the unit of adjunction $\eta$ is that it can be used to recover the mapping from the left hom-set to the right hom-set in Fig. 2. Take a morphism $g \colon F x \to z$, lift it using $U$, and compose it with $\eta_x$. You get a morphism $f \colon x \to U z$:

$f = U g \circ \eta_x$

In the absence of an adjunction, we’ll make the existence of the morphism $\eta_x$ part of the definition of the free object.

Definition. {Free object.}
A free object on $x$ consists of an object $F x$ and a morphism $\eta_x \colon x \to U (F x)$ such that, for every object $z$ and a morphism $f \colon x \to U z$, there is a unique morphism $g \colon F x \to z$ such that:

$U g \circ \eta_x = f$

The diagram in Fig. 4 illustrates this definition. It is essentially a composition of the two previous diagrams, except that we don’t require the existence of a global mapping, let alone a functor, $F$.

Fig. 4. Definition of a free object $F x$

The morphism $\eta_x$ is called the universal mapping, because it’s independent of $z$ and $f$. The equation:

$U g \circ \eta_x = f$

is called the universal condition, because it works universally for every $z$ and $f$. We say that $g$ is induced by the morphism $f$.

There is a standard trick that uses the universal condition: Suppose you have two morphisms $g$ and $g'$ from the universal object to some $z$. To prove that they are equal, it’s enough to show that they are both induced by the same $f$. Showing the equality:

$U g \circ \eta_x = U g' \circ \eta_x$

is often easier because it lets you work in the simpler, underlying category.

## Free Algebras

As I mentioned in the introduction, we are interested in algebras for the product functor: $h \otimes -$, which we’ll call $h$-algebras. Such an algebra consists of a carrier $n$ and a structure map:

$\nu \colon h \otimes n \to n$

For every $h$, $h$-algebras form a category; and there is a forgetful functor $U$ that maps an $h$-algebra to its carrier object $n$. We can therefore define a free algebra as a free object in the category of $h$-algebras, which may or may not be generalizable to a full-blown free/forgetful adjunction. Fig. 5 shows the universal condition that defines such a free algebra $(m_k, \sigma)$ generated by an object $k$.

Fig. 5. Free $h$-algebra $(m_k, \sigma)$ generated by $k$

In particular, we can define an important free $h$-algebra generated by the identity object $I$. This algebra $(m, \sigma)$ has the structure map:

$\sigma \colon h \otimes m \to m$

and is described by the diagram in Fig. 6:

Fig. 6. Free $h$-algebra $(m, \sigma)$ generated by $I$

$g \circ \pi = f$

By definition, since $g$ is an algebra morphism, it makes the diagram in Fig. 7 commute:

Fig. 7. $g$ is an $h$-algebra morphism

We use a notational convenience: $h \otimes g$ is the lifting of the morphism $g$ by the product functor $h \otimes -$. This might be confusing at first, because it looks like we are multiplying an object $h$ by a morphism $g$. One way to parse it is to consider that, if we keep the first argument to the tensor product constant, then it’s a functor in the second component, symbolically $h \otimes -$. Since it’s a functor, we can use it to lift a morphism $g$, which can be notated as $h \otimes g$.

Alternatively, we can exploit the fact that tensor product is a bifunctor, and therefore it may lift a pair of morphism, as in $id_h \otimes g$; and $h \otimes g$ is just a shorthand notation for this.

Bifunctoriality also means that the tensor product preserves composition and identity in both arguments. We’ll use these facts extensively later, especially as the basis for string diagrams.

The important property of $m$ is that it also happens to be the initial algebra for the list functor $I + h \otimes -$. Indeed, for any other algebra with the carrier $n$ and the structure map a pair $\langle f, \nu \rangle$, there exists a unique $g$ given by Fig 6, such that the diagram in Fig. 8 commutes:

Fig. 8. Initiality of the algebra $(m, \langle \pi, \sigma \rangle)$ for the functor $I + h \otimes -$.

Here, $inl$ and $inr$ are the two injections into the coproduct.

If you visualize $m$ as the sum of all powers of $h$, $\pi$ inserts the unit $I$ (zeroth power) into it, and $\sigma$ inserts the sum of non-zero powers.

$\langle \pi, \sigma \rangle \colon I + h \otimes m \to m$

The advantage of this result is that we can concentrate on studying the simpler problem of free $h$-algebras rather than the problem of initial algebras for the more complex list functor.

## Example

Here’s some useful intuition behind $h$-algebras. Think of the functor $(h \otimes -)$ as a means of forming formal expressions. These are very simple expressions: you take a variable and pair it (using the tensor product) with $h$. To define an algebra for this functor you pick a particular type $n$ for your variable (i.e., the carrier object) and a recipe for evaluating any expression of type $h \otimes n$ (i.e., the structure map).

Let’s try a simple example in Haskell. We’ll use pairs (and tuples) as our tensor product, with the empty tuple () as unit (up to isomorphism). An algebra for a functor f is defined as:

type Algebra f a = f a -> a


Consider $h$-algebras for a particular choice of $h$: the type of real numbers Double. In other words, these are algebras for the functor ((,) Double). Pick, as your carrier, the type of vectors:

data Vector = Vector { x :: Double
, y :: Double
} deriving Show


and the structure map that scales a vector by multiplying it by a number:

vecAlg :: Algebra ((,) Double) Vector
vecAlg (a, v) = Vector { x = a * x v
, y = a * y v }


Define another algebra with the carrier the set of real numbers, and the structure map multiplication by a number.

mulAlg :: Algebra ((,) Double) Double
mulAlg (a, x) = a * x


There is an algebra morphism from vecAlg to mulAlg, which takes a vector and maps it to its length.

algMorph :: Vector -> Double
algMorph v = sqrt((x v)^2 + (y v)^2)


This is an algebra morphism, because it doesn’t matter if you first calculate the length of a vector and then multiply it by a, or first multiply the vector by a and then calculate its length (modulo floating-point rounding).

A free algebra has, as its carrier, the set of all possible expressions, and an evaluator that tells you how to convert a functor-ful of such expressions to another valid expression. A good analogy is to think of the functor as defining a grammar (as in BNF grammar) and a free algebra as a language generated by this grammar.

You can generate free $h$-expressions recursively. The starting point is the set of generators $k$ as the source of variables. Applying the functor to it produces $h \otimes k$. Applying it again, produces $h \otimes h \otimes k$, and so on. The whole set of expressions is the infinite coproduct (sum):

$k + h \otimes k + h \otimes h \otimes k + ...$

An element of this coproduct is either an element of $k$, or an element of the product $h \otimes k$, and so on…

The universal mapping injects the set of generators $k$ into the set of expressions (here, it would be the leftmost injection into the coproduct).

In the special case of an algebra generated by the unit $I$, the free algebra simplifies to the power series:

$I + h + h \otimes h + ...$

and $\pi$ injects $I$ into it.

Continuing with our example, let’s consider the free algebra for the functor ((,) Double) generated by the unit (). The free algebra is, symbolically, an infinite sum (coproduct) of tuples:

data Expr =
()
| Double
| (Double, Double)
| (Double, Double, Double)
| ...


Here’s an example of an expression:

e = (1.1, 2.2, 3.3)


As you can see, free expressions are just lists of numbers. There is a function that inserts the unit into the set of expressions:

pi :: () -> Expr
pi () = ()


The free evaluator is a function (not actual Haskell):

sigma (a, ()) = a
sigma (a, x)  = (a, x)
sigma (a, (x, y)) = (a, x, y)
sigma (a, (x, y, z)) = (a, x, y, z)
...


Let’s see how the universal property works in our example. Let’s try, as the target $(n, \nu)$, our earlier vector algebra:

vecAlg :: Algebra ((,) Double) Vector
vecAlg (a, v) = Vector { x = a * x v
, y = a * y v }


The morphism f picks some concrete vector, for instance:

f :: () -> Vector
f () = Vector 3 4


There should be a unique morphism of algebras g that takes an arbitrary expression (a list of Doubles) to a vector, such that g . pi = f picks our special vector:

Vector 3 4


In other words, g must take the empty tuple (the result of pi) to Vector 3 4. The question is, how is g defined for an arbitrary expression? Look at the diagram in Fig. 7 and the commuting condition it implies:

$g \circ \sigma = \nu \circ (h \otimes g)$

Let’s apply it to an expression (a, ()) (the action of the functor (h, -) on ()). Applying sigma to it produces a, followed by the action of g resulting in g a. This should be the same as first applying the lifted (id, g) acting on (a, ()), which gives us (a, Vector 3 4); followed by vecAlg, which produces Vector (a * 3) (a * 4). Together, we get:

g a = Vector (a * 3) (a * 4)


Repeating this process gives us:

g :: Expr -> Vector
g () = Vector 3 4
g a  = Vector (a * 3) (a * 4)
g (a, b) = Vector (a * b * 3) (a * b * 4)
...


This is the unique g induced by our f.

## Properties of Free Algebras

Here are some interesting properties that will help us later: $h$-algebras are closed under multiplication and exponentiation. If $(n, \nu)$ is an $h$-algebra, then there are also $h$-algebras with the carriers $n \otimes k$ and $[k, n]$, for an arbitrary object $k$. Let’s find their structure maps.

The first one should be a mapping:

$h \otimes n \otimes k \to n \otimes k$

which we can simply take as the lifting of $\nu$ by the tensor product: $\nu \otimes k$.

The second one is:

$\tau_{k} \colon h \otimes [k, n] \to [k, n]$

which can be uncurried to:

$h \otimes [k, n] \otimes k \to n$

We have at our disposal the counit of the hom-adjunction:

$eval \colon [k, n] \otimes k \to n$

which we can use in combination with $\nu$:

$\nu \circ (h \otimes eval)$

to implement the (uncurried) structure map.

Here’s the same construction for Haskell programmers. Given an algebra:

nu :: Algebra ((,) h) n


we can create two algebras:

alpha :: Algebra ((,) h) (n, k)
alpha (a, (n, k)) = (nu (a, n), k)


and:

tau :: Algebra ((,) h) (k -> n)
tau (a, kton) = k -> nu (a, kton k)


These two algebras are related through an adjunction in the category of $h$-algebras:

$Alg\big((n \otimes k, \nu \otimes k), (l, \lambda)\big) \cong Alg\big((n, \nu), ([k, l], \tau_{k})\big)$

which follows directly from hom-adjunction acting on carriers.

$C(n \otimes k, l) \cong C(n, [k, l])$

Finally, we can show how to generate free algebras from arbitrary generators. Intuitively, this is obvious, since it can be described as the application of distributivity of tensor product over coproduct:

$k + h \otimes k + h \otimes h \otimes k + ... = (I + h + h \otimes h + ...) \otimes k$

More formally, we have:

Proposition.
If $(m, \sigma)$ is is the free $h$-algebra generated by $I$, then $(m \otimes k, \sigma \otimes k)$ is the free $h$-algebra generated by $k$ with the universal map given by $\pi \otimes k$.

Proof.
Let’s show the universal property of $(m \otimes k, \sigma \otimes k)$. Take any $h$-algebra $(n, \nu)$ and a morphism:

$f \colon k \to n$

We want to show that there is a unique $g \colon m \otimes k \to n$ that closes the diagram in Fig. 9.

Fig. 9. Free $h$-algebra generated by $k \cong I \otimes k$

I have rewritten $f$ (taking advantage of the isomorphism $I \otimes k \cong k$), as:

$f \colon I \otimes k \to n$

We can curry it to get:

$\bar{f} \colon I \to [k, n]$

The intuition is that the morphism $\bar{f}$ selects an element of the internal hom $[k, n]$ that corresponds to the original morphism $f \colon k \to n$.

We’ve seen earlier that there exists an $h$-algebra with the carrier $[k, n]$ and the structure map $\tau_k$. But since $m$ is the free $h$-algebra, there must be a unique algebra morphism $\bar{g}$ (see Fig. 10):

$\bar{g} \colon (m, \sigma) \to ([k, n], \tau_k)$

such that:

$\bar{g} \circ \pi = \bar{f}$

Fig. 10. The construction of the unique morphism $\bar{g}$

Uncurying this $\bar{g}$ gives us the sought after $g$.

The universal condition in Fig. 9:

$g \circ (\pi \otimes k) = f$

follows from pasting together two diagrams that define the relevant hom-adjunctions in Fig 11 (c.f., Fig. 1).

Fig. 11. The diagram defining the currying of both $g$ and $g \circ (\pi \otimes k)$. This is the pasting together of two diagrams that define the universal property of the internal hom $[k, n]$, one for the object $I$ and one for the object $m$.

$\square$

The immediate consequence of this proposition is that, in order to show that two $h$-algebra morphisms $g, g' \colon m \otimes k \to n$ are equal, it’s enough to show the equality of two regular morphisms:

$g \circ (\pi \otimes k) = g' \circ (\pi \otimes k) \colon k \to n$

(modulo isomorphism between $k$ and $I \otimes k$). We’ll use this property later.

It’s instructive to pick apart this proof in the simpler Haskell setting. We have the target algebra:

nu :: Algebra ((,) h) n


There is a related algebra $[k, n]$ of the form:

tau :: Algebra ((,) h) (k -> n)
tau (a, kton) = k -> nu (a, kton k)


We’ve analyzed, previously, a version of the universal construction of $g$, which we can now generalize to Fig. 10. We can build up the definition of $\bar{g}$, starting with the condition $\bar{g} \circ \pi = \bar{f}$. Here, $\bar{f}$ selects a function from the hom-set: this function is our $f$. That gives us the action of $g$ on the unit:

g :: Expr -> (k -> n)
g () = f


Next, we make use of the fact that $\bar{g}$ is an algebra morphism that satisfies the commuting condition:

$\bar{g} \circ \sigma = \tau \circ (h \otimes \bar{g})$

As before, we apply this equation to the expression (a, ()). The left hand side produces g a, while the right hand side produces tau (a, f). Next, we
apply the same equation to (a, (b, ())). The left hand side produces g (a, b). The right hand produces tau (a, tau (b, f)), and so on. Applying the definition of tau, we get:

g :: Expr -> (k -> n)
g () = f
g a  = k -> nu (a, f k)
g (a, b) = k -> nu (a, nu (b, f k))
...


Notice the order reversal in function application. The list that is the argument to g is converted to a series of applications of nu, with list elements in reverse order. We first apply nu (b, -) and then nu (a, -). This reversal is crucial to implementing list concatenation, where nu will prepend elements of the first list to the second list. We’ll see this in the second installment of this blog post.

## Bibliography

1. G.M. Kelly, A Unified Treatment of Transfinite Constructions for Free Algebras, Free Monoids, Colimits, Associated Sheaves, and so On. Bulletin of the Australian Mathematical Society, vol. 22, no. 01, 1980, p. 1.

Functors from a monoidal category C to Set form a monoidal category with Day convolution as product. A monoid in this category is a lax monoidal functor. We define an initial algebra using a higher order functor and show that it corresponds to a free lax monoidal functor.

Recently I’ve been obsessing over monoidal functors. I have already written two blog posts, one about free monoidal functors and one about free monoidal profunctors. I followed some ideas from category theory but, being a programmer, I leaned more towards writing code than being preoccupied with mathematical rigor. That left me longing for more elegant proofs of the kind I’ve seen in mathematical literature.

I believe that there isn’t that much difference between programming and math. There is a whole spectrum of abstractions ranging from assembly language, weakly typed languages, strongly typed languages, functional programming, set theory, type theory, category theory, and homotopy type theory. Each language comes with its own bag of tricks. Even within one language one starts with some relatively low level encodings and, with experience, progresses towards higher abstractions. I’ve seen it in Haskell, where I started by hand coding recursive functions, only to realize that I can be more productive using bulk operations on types, then building recursive data structures and applying recursive schemes, eventually diving into categories of functors and profunctors.

I’ve been collecting my own bag of mathematical tricks, mostly by reading papers and, more recently, talking to mathematicians. I’ve found that mathematicians are happy to share their knowledge even with outsiders like me. So when I got stuck trying to clean up my monoidal functor code, I reached out to Emily Riehl, who forwarded my query to Alexander Campbell from the Centre for Australian Category Theory. Alex’s answer was a very elegant proof of what I was clumsily trying to show in my previous posts. In this blog post I will explain his approach. I should also mention that most of the results presented in this post have already been covered in a comprehensive paper by Rivas and Jaskelioff, Notions of Computation as Monoids.

## Lax Monoidal Functors

To properly state the problem, I’ll have to start with a lot of preliminaries. This will require some prior knowledge of category theory, all within the scope of my blog/book.

We start with a monoidal category $C$, that is a category in which you can “multiply” objects using some kind of a tensor product $\otimes$. For any pair of objects $a$ and $b$ there is an object $a \otimes b$; and this mapping is functorial in both arguments (that is, you can also “multiply” morphisms). A monoidal category will also have a special object $I$ that is the unit of multiplication. In general, the unit and associativity laws are satisfied up to isomorphism:

$\lambda : I \otimes a \cong a$

$\rho : a \otimes I \cong a$

$\alpha : (a \otimes b) \otimes c \cong a \otimes (b \otimes c)$

These isomorphisms are called, respectively, the left and right unitors, and the associator.

The most familiar example of a monoidal category is the category of types and functions, in which the tensor product is the cartesian product (pair type) and the unit is the unit type ().

Let’s now consider functors from $C$ to the category of sets, $Set$. These functors also form a category called $[C, Set]$, in which morphisms between any two functors are natural transformations.

In Haskell, a natural transformation is approximated by a polymorphic function:

type f ~> g = forall x. f x -> g x

The category $Set$ is monoidal, with cartesian product $\times$ serving as a tensor product, and the singleton set $1$ as the unit.

We are interested in functors in $[C, Set]$ that preserve the monoidal structure. Such a functor should map the tensor product in $C$ to the cartesian product in $Set$ and the unit $I$ to the singleton set $1$. Accordingly, a strong monoidal functor $F$ comes with two isomorphisms:

$F a \times F b \cong F (a \otimes b)$

$1 \cong F I$

We are interested in a weaker version of a monoidal functor called lax monoidal functor, which is equipped with a one-way natural transformation:

$\mu : F a \times F b \to F (a \otimes b)$

and a one-way morphism:

$\eta : 1 \to F I$

A lax monoidal functor must also preserve unit and associativity laws.

Associativity law: $\alpha$ is the associator in the appropriate category (top arrow, in Set; bottom arrow, in C).

In Haskell, a lax monoidal functor can be defined as:

class Monoidal f where
eta :: () -> f ()
mu  :: (f a, f b) -> f (a, b)

It’s also known as the applicative functor.

## Day Convolution and Monoidal Functors

It turns out that our category of functors $[C, Set]$ is also equipped with monoidal structure. Two functors $F$ and $G$ can be “multiplied” using Day convolution:

$(F \star G) c = \int^{a b} C(a \otimes b, c) \times F a \times G b$

Here, $C(a \otimes b, c)$ is the hom-set, or the set of morphisms from $a \otimes b$ to $c$. The integral sign stands for a coend, which can be interpreted as a generalization of an (infinite) coproduct (modulo some identifications). An element of this coend can be constructed by injecting a triple consisting of a morphism from $C(a \otimes b, c)$, an element of the set $F a$, and an element of the set $G b$, for some $a$ and $b$.

In Haskell, a coend corresponds to an existential type, so the Day convolution can be defined as:

data Day f g c where
Day :: ((a, b) -> c, f a, g b) -> Day f g c

(The actual definition uses currying.)

The unit with respect to Day convolution is the hom-functor:

$C(I, -)$

which assigns to every object $c$ the set of morphisms $C(I, c)$ and acts on morphisms by post-composition.

The proof that this is the unit is instructive, as it uses the standard trick: the co-Yoneda lemma. In the coend form, the co-Yoneda lemma reads, for a covariant functor $F$:

$\int^x C(x, a) \times F x \cong F a$

and for a contravariant functor $H$:

$\int^x C(a, x) \times H x \cong H a$

(The mnemonics is that the integration variable must appear twice, once in the negative, and once in the positive position. An argument to a contravariant functor is in a negative position.)

Indeed, substituting $C(I, -)$ for the first functor in Day convolution produces:

$(C(I, -) \star G) c = \int^{a b} C(a \otimes b, c) \times C(I, a) \times G b$

which can be “integrated” over $a$ using the Yoneda lemma to yield:

$\int^{b} C(I \otimes b, c) \times G b$

and, since $I$ is the unit of the tensor product, this can be further “integrated” over $b$ to give $G c$. The right unit law is analogous.

To summarize, we are dealing with three monoidal categories: $C$ with the tensor product $\otimes$ and unit $I$, $Set$ with the cartesian product and singleton $1$, and a functor category $[C, Set]$ with Day convolution and unit $C(I, -)$.

## A Monoid in [C, Set]

A monoidal category can be used to define monoids. A monoid is an object $m$ equipped with two morphisms — unit and multiplication:

$\eta : I \to m$

$\mu : m \otimes m \to m$

These morphisms must satisfy unit and associativity conditions, which are best illustrated using commuting diagrams.

Unit laws. λ and ρ are the unitors.

Associativity law: α is the associator.

This definition of a monoid can be translated directly to Haskell:

class Monoid m where
eta :: () -> m
mu  :: (m, m) -> m

It so happens that a lax monoidal functor is exactly a monoid in our functor category $[C, Set]$. Since objects in this category are functors, a monoid is a functor $F$ equipped with two natural transformations:

$\eta : C(I, -) \to F$

$\mu : F \star F \to F$

At first sight, these don’t look like the morphisms in the definition of a lax monoidal functor. We need some new tricks to show the equivalence.

Let’s start with the unit. The first trick is to consider not one natural transformation but the whole hom-set:

$[C, Set](C(I, -), F)$

The set of natural transformations can be represented as an end (which, incidentally, corresponds to the forall quantifier in the Haskell definition of natural transformations):

$\int_c Set(C(I, c), F c)$

The next trick is to use the Yoneda lemma which, in the end form reads:

$\int_c Set(C(a, c), F c) \cong F a$

In more familiar terms, this formula asserts that the set of natural transformations from the hom-functor $C(a, -)$ to $F$ is isomorphic to $F a$.

There is also a version of the Yoneda lemma for contravariant functors:

$\int_c Set(C(c, a), H c) \cong H a$

The application of Yoneda to our formula produces $F I$, which is in one-to-one correspondence with morphisms $1 \to F I$.

We can use the same trick of bundling up natural transformations that define multiplication $\mu$.

$[C, Set](F \star F, F)$

and representing this set as an end over the hom-functor:

$\int_c Set((F \star F) c, F c)$

Expanding the definition of Day convolution, we get:

$\int_c Set(\int^{a b} C(a \otimes b, c) \times F a \times F b, F c)$

The next trick is to pull the coend out of the hom-set. This trick relies on the co-continuity of the hom-functor in the first argument: a hom-functor from a colimit is isomorphic to a limit of hom-functors. In programmer-speak: a function from a sum type is equivalent to a product of functions (we call it case analysis). A coend is a generalized colimit, so when we pull it out of a hom-functor, it turns into a limit, or an end. Here’s the general formula, in which $p x y$ is an arbitrary profunctor:

$Set(\int^x p x x, y) \cong \int_x Set(p x x, y)$

Let’s apply it to our formula:

$\int_c \int_{a b} Set(C(a \otimes b, c) \times F a \times F b, F c)$

We can combine the ends under one integral sign (it’s allowed by the Fubini theorem) and move to the next trick: hom-set adjunction:

$Set(a \times b, c) \cong Set(a, b \to c)$

In programming this is known as currying. This adjunction exists because $Set$ is a cartesian closed category. We’ll use this adjunction to move $F a \times F b$ to the right:

$\int_{a b c} Set(C(a \otimes b, c), (F a \times F b) \to F c)$

Using the Yoneda lemma we can “perform the integration” over $c$  to get:

$\int_{a b} (F a \times F b) \to F (a \otimes b))$

This is exactly the set of natural transformations used in the definition of a lax monoidal functor. We have established one-to-one correspondence between monoidal multiplication and lax monoidal mapping.

Of course, a complete proof would require translating monoid laws to their lax monoidal counterparts. You can find more details in Rivas and Jaskelioff, Notions of Computation as Monoids.

We’ll use the fact that a monoid in the category $[C, Set]$ is a lax monoidal functor later.

### Alternative Derivation

Incidentally, there are shorter derivations of these formulas that use the trick borrowed from the proof of the Yoneda lemma, namely, evaluating things at the identity morphism. (Whenever mathematicians speak of Yoneda-like arguments, this is what they mean.)

Starting from $F \star F \to F$ and plugging in the Day convolution formula, we get:

$\int^{a' b'} C(a' \otimes b', c) \times F a' \times F b' \to F c$

There is a component of this natural transformation at $(a \otimes b)$ that is the morphism:

$\int^{a' b'} C(a' \otimes b', a \otimes b) \times F a' \times F b' \to F (a \otimes b)$

This morphism must be defined for all possible values of the coend. In particular, it must be defined for the triple $(id_{a \otimes b}, F a, F b)$, giving us the $\mu$ we seek.

There is also an alternative derivation for the unit: Take the component of the natural transformation $\eta$ at $I$:

$\eta_I : C(I, I) \to L I$

$C(I, I)$ is guaranteed to contain at least one element, the identity morphism $id_I$. We can use $\eta_I \, id_I$ as the (only) value of the lax monoidal constraint at the singleton $1$.

## Free Monoid

Given a monoidal category $C$, we might be able to define a whole lot of monoids in it. These monoids form a category $Mon(C)$. Morphisms in this category correspond to those morphisms in $C$ that preserve monoidal structure.

Consider, for instance, two monoids $m$ and $m'$. A monoid morphism is a morphism $f : m \to m'$ in $C$ such that the unit of $m'$ is related to the unit of $m$:

$\eta' = f \circ \eta$

and similarly for multiplication:

$\mu' \circ (f \otimes f) = f \circ \mu$

Remember, we assumed that the tensor product is functorial in both arguments, so it can be used to lift a pair of morphisms.

There is an obvious forgetful functor $U$ from $Mon(C)$ to $C$ which, for every monoid, picks its underlying object in $C$ and maps every monoid morphism to its underlying morphism in $C$.

The left adjoint to this functor, if it exists, will map an object $a$ in $C$ to a free monoid $L a$.

The intuition is that a free monoid $L a$ is a list of $a$.

In Haskell, a list is defined recursively:

data List a = Nil | Cons a (List a)

Such a recursive definition can be formalized as a fixed point of a functor. For a list of a, this functor is:

data ListF a x = NilF | ConsF a x

Notice the peculiar structure of this functor. It’s a sum type: The first part is a singleton, which is isomorphic to the unit type (). The second part is a product of a and x. Since the unit type is the unit of the product in our monoidal category of types, we can rewrite this functor symbolically as:

$\Phi a x = I + a \otimes x$

It turns out that this formula works in any monoidal category that has finite coproducts (sums) that are preserved by the tensor product. The fixed point of this functor is the free functor that generates free monoids.

I’ll define what is meant by the fixed point and prove that it defines a monoid. The proof that it’s the result of a free/forgetful adjunction is a bit involved, so I’ll leave it for a future blog post.

### Algebras

Let’s consider algebras for the functor $F$. Such an algebra is defined as an object $x$ called the carrier, and a morphism:

$f : F x \to x$

called the structure map or the evaluator.

In Haskell, an algebra is defined as:

type Algebra f x = f x -> x

There may be a lot of algebras for a given functor. In fact there is a whole category of them. We define an algebra morphism between two algebras $(x, f : F x \to x)$ and $(x', f' : F x' \to x')$ as a morphism $\nu : x \to x'$ which commutes with the two structure maps:

$\nu \circ f = f' \circ F \nu$

The initial object in the category of algebras is called the initial algebra, or the fixed point of the functor that generates these algebras. As the initial object, it has a unique algebra morphism to any other algebra. This unique morphism is called a catamorphism.

In Haskell, the fixed point of a functor f is defined recursively:

newtype Fix f = In { out :: f (Fix f) }

with, for instance:

type List a = Fix (ListF a)

A catamorphism is defined as:

cata :: Functor f => Algebra f a -> Fix f -> a
cata alg = alg . fmap (cata alg) . out

A list catamorphism is called foldr.

We want to show that the initial algebra $L a$ of the functor:

$\Phi a x = I + a \otimes x$

is a free monoid. Let’s see under what conditions it is a monoid.

### Initial Algebra is a Monoid

In this section I will show you how to concatenate lists the hard way.

We know that function type $b \to c$ (a.k.a., the exponential $c^b$) is the right adjoint to the product:

$Set(a \times b, c) \cong Set(a, b \to c)$

The function type is also called the internal hom.

In a monoidal category it’s sometimes possible to define an internal hom-object, denoted $[b, c]$, as the right adjoint to the tensor product:

$curry : C(a \otimes b, c) \cong C(a, [b, c])$

If this adjoint exists, the category is called closed monoidal.

In a closed monoidal category, the initial algebra $L a$ of the functor $\Phi a x = I + a \otimes x$ is a monoid. (In particular, a Haskell list of a, which is a fixed point of ListF a, is a monoid.)

To show that, we have to construct two morphisms corresponding to unit and multiplication (in Haskell, empty list and concatenation):

$\eta : I \to L a$

$\mu : L a \otimes L a \to L a$

What we know is that $L a$ is a carrier of the initial algebra for $\Phi a$, so it is equipped with the structure map:

$I + a \otimes L a \to L a$

which is equivalent to a pair of morphisms:

$\alpha : I \to L a$

$\beta : a \otimes L a \to L a$

Notice that, in Haskell, these correspond the two list constructors: Nil and Cons or, in terms of the fixed point:

nil :: () -> List a
nil () = In NilF

cons :: a -> List a -> List a
cons a as = In (ConsF a as)

We can immediately use $\alpha$ to implement $\eta$.

The second one, $\beta$, one can be rewritten using the hom adjuncion as:

$\bar{\beta} = curry \, \beta$

$\bar{\beta} : a \to [L a, L a]$

Notice that, if we could prove that $[L a, L a]$ is a carrier for the same algebra generated by $\Phi a$, we would know that there is a unique catamorphism from the initial algebra $L a$:

$\kappa_{[L a, L a]} : L a \to [L a, L a]$

which, by the hom adjunction, would give us the desired multiplication:

$\mu : L a \otimes L a \to L a$

Let’s establish some useful lemmas first.

Lemma 1: For any object $x$ in a closed monoidal category, $[x, x]$ is a monoid.

This is a generalization of the idea that endomorphisms form a monoid, in which identity morphism is the unit and composition is multiplication. Here, the internal hom-object $[x, x]$ generalizes the set of endomorphisms.

Proof: The unit:

$\eta : I \to [x, x]$

follows, through adjunction, from the unit law in the monoidal category:

$\lambda : I \otimes x \to x$

(In Haskell, this is a fancy way of writing mempty = id.)

Multiplication takes the form:

$\mu : [x, x] \otimes [x, x] \to [x, x]$

which is reminiscent of composition of edomorphisms. In Haskell we would say:

mappend = (.)

$curry^{-1} \, \mu : [x, x] \otimes [x, x] \otimes x \to x$

We have at our disposal the counit $eval$ of the adjunction:

$eval : [x, x] \otimes x \cong x$

We can apply it twice to get:

$\mu = curry (eval \circ (id \otimes eval))$

In Haskell, we could express this as:

mu :: ((x -> x), (x -> x)) -> (x -> x)
mu (f, g) = \x -> f (g x)

Here, the counit of the adjunction turns into simple function application.

$\square$

Lemma 2: For every morphism $f : a \to m$, where $m$ is a monoid, we can construct an algebra of the functor $\Phi a$ with $m$ as its carrier.

Proof: Since $m$ is a monoid, we have two morphisms:

$\eta : I \to m$

$\mu : m \otimes m \to m$

To show that $m$ is a carrier of our algebra, we need two morphisms:

$\alpha : I \to m$

$\beta : a \otimes m \to m$

The first one is the same as $\eta$, the second can be implemented as:

$\beta = \mu \circ (f \otimes id)$

In Haskell, we would do case analysis:

mapAlg :: Monoid m => ListF a m -> m
mapAlg NilF = mempty
mapAlg (ConsF a m) = f a mappend m

$\square$

We can now build a larger proof. By lemma 1, $[L a, L a]$ is a monoid with:

$\mu = curry (eval \circ (id \otimes eval))$

We also have a morphism $\bar{\beta} : a \to [L a, L a]$ so, by lemma 2, $[L a, L a]$ is also a carrier for the algebra:

$\alpha = \eta$

$\beta = \mu \circ (\bar{\beta} \otimes id)$

It follows that there is a unique catamorphism $\kappa_{[L a, L a]}$ from the initial algebra $L a$ to it, and we know how to use it to implement monoidal multiplication for $L a$. Therefore, $L a$ is a monoid.

Translating this to Haskell, $\bar{\beta}$ is the curried form of Cons and what we have shown is that concatenation (multiplication of lists) can be implemented as a catamorphism:

concat :: List a -> List a -> List a
conc x y = cata alg x y
where alg NilF        = id
alg (ConsF a t) = (cons a) . t


The type:

List a -> (List a -> List a)

(parentheses added for emphasis) corresponds to $L a \to [L a, L a]$.

It’s interesting that concatenation can be described in terms of the monoid of list endomorphisms. Think of turning an element a of the list into a transformation, which prepends this element to its argument (that’s what $\bar{\beta}$ does). These transformations form a monoid. We have an algebra that turns the unit $I$ into an identity transformation on lists, and a pair $a \otimes t$ (where $t$ is a list transformation) into the composite $\bar{\beta} a \circ t$. The catamorphism for this algebra takes a list $L a$ and turns it into one composite list transformation. We then apply this transformation to another list and get the final result: the concatenation of two lists. $\square$

Incidentally, lemma 2 also works in reverse: If a monoid $m$ is a carrier of the algebra of $\Phi a$, then there is a morphism $f : a \to m$. This morphism can be thought of as inserting generators represented by $a$ into the monoid $m$.

Proof: if $m$ is both a monoid and a carrier for the algebra $\Phi a$, we can construct the morphism $a \to m$ by first applying the identity law to go from $a$ to $a \otimes I$, then apply $id_a \otimes \eta$ to get $a \otimes m$. This can be right-injected into the coproduct $I + a \otimes m$ and then evaluated down to $m$ using the structure map for the algebra on $m$.

$a \to a \otimes I \to a \otimes m \to I + a \otimes m \to m$

In Haskell, this corresponds to a construction and evaluation of:

ConsF a mempty

$\square$

## Free Monoidal Functor

Let’s go back to our functor category. We started with a monoidal category $C$ and considered a functor category $[C, Set]$. We have shown that $[C, Set]$ is itself a monoidal category with Day convolution as tensor product and the hom functor $C(I, -)$ as unit. A monoid is this category is a lax monoidal functor.

The next step is to build a free monoid in $[C, Set]$, which would give us a free lax monoidal functor. We have just seen such a construction in an arbitrary closed monoidal category. We just have to translate it to $[C, Set]$. We do this by replacing objects with functors and morphisms with natural transformations.

Our construction relied on defining an initial algebra for the functor:

$I + a \otimes b$

Straightforward translation of this formula to the functor category $[C, Set]$ produces a higher order endofunctor:

$A_F G = C(I, -) + F \star G$

It defines, for any functor $F$, a mapping from a functor $G$ to a functor $A_F G$. (It also maps natural transformations.)

We can now use $A_F$ to define (higher-order) algebras. An algebra consists of a carrier — here, a functor $T$ — and a structure map — here, a natural transformation:

$A_F T \to T$

The initial algebra for this higher-order endofunctor defines a monoid, and therefore a lax monoidal functor. We have shown it for an arbitrary closed monoidal category. So the only question is whether our functor category with Day convolution is closed.

We want to define the internal hom-object in $[C, Set]$ that satisfies the adjunction:

$[C, Set](F \star G, H) \cong [C, Set](F, [G, H])$

We start with the set of natural transformations — the hom-set in $[C, Set]$:

$[C, Set](F \star G, H)$

We rewrite it as an end over $c$, and use the formula for Day convolution:

$\int_c Set(\int^{a b} C(a \otimes b, c) \times F a \times G b, H c)$

We use the co-continuity trick to pull the coend out of the hom-set and turn it into an end:

$\int_{c a b} Set(C(a \otimes b, c) \times F a \times G b, H c)$

Keeping in mind that our goal is to end up with $F a$ on the left, we use the regular hom-set adjunction to shuffle the other two terms to the right:

$\int_{c a b} Set(F a, C(a \otimes b, c) \times G b \to H c)$

The hom-functor is continuous in the second argument, so we can sneak the end over $b c$ under it:

$\int_{a} Set(F a, \int_{b c} C(a \otimes b, c) \times G b \to H c)$

We end up with a set of natural transformations from the functor $F$ to the functor we will call:

$[G, H] = \int_{b c} (C(- \otimes b, c) \times G b \to H c)$

We therefore identify this functor as the right adjoint (internal hom-object) for Day convolution. We can further simplify it by using the hom-set adjunction:

$\int_{b c} (C(- \otimes b, c) \to (G b \to H c))$

and applying the Yoneda lemma to get:

$[G, H] = \int_{b} (G b \to H (- \otimes b))$

In Haskell, we would write it as:

newtype DayHom f g a = DayHom (forall b . f b -> g (a, b))

Since Day convolution has a right adjoint, we conclude that the fixed point of our higher order functor defines a free lax monoidal functor. We can write it in a recursive form as:

$Free_F = C(I, -) + F \star Free_F$

data FreeMonR f t =
Done t
| More (Day f (FreeMonR f) t)

This blog post wouldn’t be complete without mentioning that the same construction works for monads. Famously, a monad is a monoid in the category of endofunctors. Endofunctors form a monoidal category with functor composition as tensor product and the identity functor as unit. The fact that we can construct a free monad using the formula:

$FreeM_F = Id + F \circ FreeM_F$

is due to the observation that functor composition has a right adjoint, which is the right Kan extension. Unfortunately, due to size issues, this Kan extension doesn’t always exist. I’ll quote Alex Campbell here: “By making suitable size restrictions, we can give conditions for free monads to exist: for example, free monads exist for accessible endofunctors on locally presentable categories; a special case is that free monads exist for finitary endofunctors on $Set$, where finitary means the endofunctor preserves filtered colimits (more generally, an endofunctor is accessible if it preserves $\kappa$-filtered colimits for some regular cardinal number $\kappa$).”

## Conclusion

As we acquire experience in programming, we learn more tricks of trade. A seasoned programmer knows how to read a file, parse its contents, or sort an array. In category theory we use a different bag of tricks. We bunch morphisms into hom-sets, move ends and coends, use Yoneda to “integrate,” use adjunctions to shuffle things around, and use initial algebras to define recursive types.

Results derived in category theory can be translated to definitions of functions or data structures in programming languages. A lax monoidal functor becomes an Applicative. Free monoidal functor becomes:

data FreeMonR f t =
Done t
| More (Day f (FreeMonR f) t)

What’s more, since the derivation made very few assumptions about the category $C$ (other than that it’s monoidal), this result can be immediately applied to profunctors (replacing $C$ with $C^{op}\times C$) to produce:

data FreeMon p s t where
DoneFM :: t -> FreeMon p s t
MoreFM :: p a b -> FreeMon p c d ->
(b -> d -> t) ->
(s -> (a, c)) -> FreeMon p s t

Replacing Day convolution with endofunctor composition gives us a free monad:

data FreeMonadF f g a =
DoneFM a
| MoreFM (Compose f g a)

Category theory is also the source of laws (commuting diagrams) that can be used in equational reasoning to verify the correctness of programming constructs.

Writing this post has been a great learning experience. Every time I got stuck, I would ask Alex for help, and he would immediately come up with yet another algebra and yet another catamorphism. This was so different from the approach I would normally take, which would be to get bogged down in inductive proofs over recursive data structures.

I want you to perform a little experiment. Take an egg, put it in a blender, and run it for ten seconds.

Oh, I forgot to tell you to first remove the eggshell. No problem, let’s run the blender in the opposite direction for ten seconds, and we’ll get the egg back.

It doesn’t work, does it? The reason is entropy. The second law of thermodynamics states that the entropy of an isolated system can never decrease. Blending an egg increased its entropy. Unblending it would decrease entropy. But there is a workaround: feed the blended egg to a chicken, and you will get a new egg. Granted, you might have to feed it more than one egg, but still: the miracle of life! Life seems to go against the general trend of the second law of thermodynamics.

Of course, life cannot flourish in a completely isolated system, so the laws of physics are safe. A chicken can produce an egg only by increasing the entropy of its environment and, indirectly, that of the Sun.

## Entropy and the Universe

We have some kind of intuitive understanding of entropy as the degree of disorderliness. An egg is highly “ordered,” in that it has an ovoid shell, the white, the yolk and, most importantly, the genetic blueprint for a chicken. It is extremely unlikely that an egg would randomly assemble itself from the primordial soup. And yet, in a way, it did. It took about fourteen billion years, starting from the Big Bang, but it finally arrived to a supermarket near you.

Since entropy has been always copiously produced in the Universe, we are forced to deduce that the initial entropy of the Universe was much lower than it is today. The Universe has been running up the entropy bill at a tremendous pace ever since the Big Bang.

With our simplistic understanding of entropy as the opposite of order, it might be difficult to imagine what it meant for the primordial Universe to be low entropy. Were elementary particles nicely stacked according to their quantum Dewey decimal codes on separate shelves like books in a library? It turns out that, in the presence of gravity, the lowest entropy state is when matter is uniformly distributed throughout the Universe. This might be a little counter-intuitive, considering how blending an egg led to the increase of entropy. But uniform distribution of gravitational mass is a very precarious state. It’s like a needle balanced on its point. At the slightest disturbance, the parts of the volume with infinitesimally higher density will start collapsing on themselves due to gravity. The collapse will be slow in the beginning, but as it keeps increasing local density, it will attract more and more matter resulting in a positive feedback loop.

This is exactly what happened after the Big Bang (as far as we know). Low-entropy uniform soup started slowly curdling to form galaxies and stars. The more non-uniform the distribution of gravitating matter, the higher the entropy.

The ultimate fate of collapsing matter is a gravitational black hole, with all matter concentrated in a singular point. Black holes have extremely high entropy, so much so that it is believed that the current entropy of the Universe is dominated by gigantic black holes in the centers of galaxies.

So why hasn’t the whole Universe collapsed into one gigantic black hole? It’s because the breakneck race toward higher entropy has run against several obstacles. One of them works like a governor in a steam engine. Tiny fluctuations in mass density during the Big Bang were accompanied by tiny fluctuations in velocities of particles. These fluctuations resulted in random distribution of angular momentum. As a result, each collapsing region of the Universe ends up with some randomly assigned net angular momentum. In other words, it spins. And when matter is sucked up towards the center, it starts spinning faster and faster. That’s why every galaxy is spinning. The resulting centrifugal force keeps matter from falling all the way to the center and disappearing into a black hole.

The other obstacle towards reaching maximum entropy is the fact that clumps of matter of certain size turn into stars. When lots of atoms of hydrogen are squished together, they can reach a higher entropy state by fusing into helium. But this process produces excess photons, which keep pushing matter away, thus preventing total collapse. Eventually, the hydrogen burns out, the star undergoes a series of transitions and, depending on its mass, ends up as a supernova, or turns into a brown or white dwarf. What’s left after a supernova explosion can be a neutron star or a black hole.

In a neutron star, further collapse is stalled by another property of matter: Fermi statistics. Neutrons are fermions, and no two fermions may occupy the same quantum state. In particular, you can’t squeeze them all into a very small volume — they repel each other.

Are neutron stars and black holes the end products of the evolution of the Universe? Probably not. There is a strong suspicion that neutrons will eventually decay into leptons — mostly neutrinos, electrons, and positrons. Black holes will evaporate through Hawking radiation. The Universe will eventually reach its thermal death: an ever expanding volume filled with photons and leptons.

## What’s Life Got to Do with It?

So far we’ve seen that matter has properties that tend to slow down the ratchet of entropy. Our Sun, for instance, could increase its entropy tremendously by turning all its hydrogen into helium in one fell swoop while collapsing to form a black hole. It can’t do that because of the heat and radiation pressure generated in the process. And even if all the heat were siphoned out, the leftover neutrons would congeal into a solid neutron star, preventing further collapse.

So the Sun is doing its best, under the circumstances, trying to dissipate the excess of energy. It does it mostly by radiating high energy photons. These are the photons of visible and ultraviolet light that warm up the Earth. The Earth, in turn, re-radiates this heat in the form of low energy infrared photons.

It turns out that turning high energy photons to low energy photons increases overall entropy. So, in its small way, the Earth speeds up the rise of entropy. In fact, it does it better than, for instance, Mercury; because the Earth has the atmosphere and the oceans, which are good heat sinks, and because it spins on its axis, transporting the accumulated heat from the sunny side to the shaded side, where it’s radiated into space in the form of infrared photons.

But Earth has another secret weapon that speeds up the advent of the heat death of the Universe: life. To begin with, living organisms consume energy during the day. They also need energy to survive at night, so they came up with clever ways to store energy in chemical compounds. They can then cash their savings at night, all the while radiating heat. At higher latitudes, they also store energy during summer and expend it during winter.

A steppe is better at entropy production than barren land; a forest or a jungle is still better. But human civilization is the best. Our cars, factories, cities, air conditioners, all produce entropy at a much faster pace than bare nature. We’re good at it!

## The Self-Organizing Principle

The advent of life on Earth is often attributed to something called the self-organizing principle. It’s just a name for what happens in systems that are away from thermodynamic equilibrium. In those systems it is often possible to speed up the increase of entropy by organizing things a little better.

The simplest example of this is when you heat a layer of liquid in a pan. The liquid can transport energy by thermal conduction, which leads to overall raise in entropy. But there is a faster way: the heated liquid at the bottom of the pan is lighter than the cooler liquid at the top, so it can float to the top. The heavier liquid at the top can then sink to the bottom. This is called convection, and it’s faster than conduction. The only problem is that the two streams of liquid have to negotiate the flow, because they can’t both pass through the same point simultaneously. In fact, in the ideal case, they would be deadlocked. What happens in reality is an amazing feat of self-organization: regularly spaced hexagonal convection cells called Bénard cells emerge in the heated liquid.

A honeycomb pattern of Bénard cells suggests that order may be spontaneously generated in situations when it can speed up the production of entropy. If you have rich enough environment and wait long enough, more and more complex patterns that ease the production of entropy may emerge — such as life itself.

But life doesn’t emerge everywhere. As far as we know there’s no life on the Moon and no (visible) life on Mars. What’s different about Earth is that it is, and has always been, very turbulent. For starters, we have water that is constantly changing state. It’s boiling in hydrothermal vents, liquid in the oceans, solid in the ice caps; it’s precipitating from the atmosphere and evaporating from pools. It dissolves lots of chemical compounds and makes colloids with others. Continental plates keep shifting resulting in constant volcanic activity. New minerals are brought up from the depths and exposed to erosion. We also have a large Moon that’s causing regular tides, and the Earth’s axis of rotation is tilted resulting in changing seasons. On top of that, we have occasional comets causing impact winters. We can’t complain about lack of entertainment on Earth.

Here’s what I think: Life can only emerge and thrive on the edge. Our planet has been on the edge for a few billion years. Conditions on Earth have always been barely short of wiping the life out and, paradoxically, this is exactly what makes life possible. The Earth is a living proof that what doesn’t kill you, makes you stronger. There have been uncountable attempts on the life on Earth and they all resulted in accelerating the evolution towards higher life forms. I know that it might be controversial to call one form of life higher than another, but there is an objective measure that we can use, and that’s the efficiency of turning energy into entropy. In this respect, humans are indeed the highest form of life. We were able to tap into sources of energy that have been forgotten by nature for hundreds of millions of years in the form of coal, oil, and gas. We use all this to speed up the increase of entropy.

## Why Are We Alone?

You might be familiar with the Fermi paradox. In essence, the question is: if life is inevitable, why haven’t we seen it all over the Universe. And judging by how quickly life emerged on Earth– essentially as soon as the water condensed into oceans– life seems to be inevitable, at least on Earth-like planets, which are very common in the Universe. And life — civilized life in particular — being so good at producing vast amounts of entropy, should eventually make itself conspicuous on the cosmic scale.

On the other hand, we don’t know how many planets are “on the edge,” and how narrow the edge is. It’s possible that for an Earth-like planet to enter the life-creating period is a relatively common occurrence — possibly right after the water gathers into oceans. Finding remnants of life on Mars would give support to this idea. But the Earth has been walking this narrow path between stagnation and destruction for more than four billion years. There have been long periods of stagnation: there was the snowball Earth when the oceans froze over, and the “boring billion,” when the air was filled with the smell of rotten eggs. There have been major extinction events, like the asteroid impact that wiped out the dinosaurs.

Being on edge means that you are likely to fall off. You either die of boredom (that’s what might have happened on Mars), or you get wiped out by a cataclysm (if the Chicxulub asteroid were a tad larger, the Earth could have been sterilized). It might be extremely unlikely to stay for a few billion years on the narrow path that leads from Bénard cells to a space-faring civilization. We might actually be the first to reach this level in our cosmic neighborhood. Life on Earth could be more like a professional Russian-roulette player than a nine-to-five worker.

There is also something we don’t quite get about cosmic timescales. For the last few hundred of years the powers of humanity have been growing exponentially. From the cosmic perspective, humanity looks like a sudden bloom that took over a stagnant pool on the outskirts of the Galaxy. We foolishly imagine that we can sustain this level of progress and in short time colonize the Solar system and reach for the stars. But one thing we know for sure about exponential growth is that it’s not sustainable in the long run. We are not only going to bump our heads against unbreakable laws of physics, but we’ll also have to deal with the limitations of human mind. And all other civilizations that might be out there will have to deal with the same problems. This might explain why we are not seeing them.

In fact, we could reverse this reasoning and argue that the fact that we don’t detect any signs of alien civilizations suggests that the obstacles that we see in front of us are not easily overcome. In particular:

• The speed of light limits our ability to travel and exchange information at large distances. This is one of the hardest limits, because special relativity is the foundation of all physics.
• The coupling of gravity to other forces is extremely weak, so the prospects of controlling gravity and counter-balancing acceleration are virtually non-existent. This means that there is no easy way to shrink the enormous distances between stars — no warp drive.
• The size of the atom and the speed of light limit our ability to store and process information. This prevents us from extending the capabilities of our brains to discover and explore the laws of the Universe.

These three limits can also be related to three fundamental theories: special relativity, general relativity, and quantum mechanics, respectively.

So what does the future have in stock for humanity? It looks like we are reaching the end of exponential expansion. There hasn’t been any major breakthrough in fundamental physics for almost half a century, we are seeing the tail end of the Moore’s law, and the population of Earth is finally stabilizing. If we don’t wipe ourselves out from the face of the Earth, we might be facing a boring millennium, if not a boring million. And it’s entirely possible that we are surrounded by other civilizations that have already entered their boring periods. If they eventually graduate to the next stage, they will be ready to help the Universe increase its entropy on a vastly larger scale. Hopefully humanity will still be around to see the Galaxy blooming with sentient activity.

Abstract: I derive free monoidal profunctors as fixed points of a higher order functor acting on profunctors. Monoidal profunctors play an important role in defining traversals.

The beauty of category theory is that it lets us reuse concepts at all levels. In my previous post I have derived a free monoidal functor that goes from a monoidal category $C$ to $Set$. The current post may then be shortened to: Since profunctors are just functors from $C^{op} \times C$ to $Set$, with the obvious monoidal structure induced by the tensor product in $C$, we automatically get free monoidal profunctors.

Let me fill in the details.

Here’s the definition of a profunctor from Data.Profunctor:

class Profunctor p where
dimap :: (s -> a) -> (b -> t) -> p a b -> p s t

The idea is that, just like a functor acts on objects, a profunctor p acts on pairs of objects $\langle a, b \rangle$. In other words, it’s a type constructor that takes two types as arguments. And just like a functor acts on morphisms, a profunctor acts on pairs of morphisms. The only tricky part is that the first morphism of the pair is reversed: instead of going from $a$ to $s$, as one would expect, it goes from $s$ to $a$. This is why we say that the first argument comes from the opposite category $C^{op}$, where all morphisms are reversed with respect to $C$. Thus a morphism from $\langle a, b \rangle$ to $\langle s, t \rangle$ in $C^{op} \times C$ is a pair of morphisms $\langle s \to a, b \to t \rangle$.

Just like functors form a category, profunctors form a category too. In this category profunctors are objects, and natural transformations are morphisms. A natural transformation between two profunctors $p$ and $q$ is a family of functions which, in Haskell, can be approximated by a polymorphic function:

type p ::~> q = forall a b. p a b -> q a b

If the category $C$ is monoidal (has a tensor product $\otimes$ and a unit object $1$), then the category $C^{op} \times C$ has a trivially induced tensor product:

$\langle a, b \rangle \otimes \langle c, d \rangle = \langle a \otimes c, b \otimes d \rangle$

and unit $\langle 1, 1 \rangle$

In Haskell, we’ll use cartesian product (pair type) as the underlying tensor product, and () type as the unit.

Notice that the induced product does not have the usual exponential as the right adjoint. Indeed, the hom-set:

$(C^{op} \times C) \, ( \langle a, b \rangle \otimes \langle c, d \rangle, \langle s, t \rangle )$

is a set of pairs of morphisms:

$\langle s \to a \otimes c, b \otimes d \to t \rangle$

If the right adjoint existed, it would be a pair of objects $\langle X, Y \rangle$, such that the following hom-set would be isomorphic to the previous one:

$\langle X \to a, b \to Y \rangle$

While $Y$ could be the internal hom, there is no candidate for $X$ that would produce the isomorphism:

$s \to a \otimes c \cong X \to a$

(Consider, for instance, unit $()$ for $a$.) This lack of the right adjoint is the reason why we can’t define an analog of Applicative for profunctors. We can, however, define a monoidal profunctor:

class Monoidal p where
punit :: p () ()
(>**<) :: p a b -> p c d -> p (a, c) (b, d)

This profunctor is a map between two monoidal structures. For instance, punit can be seen as mapping the unit in $Set$ to the unit in $C^{op} \times C$:

punit :: () -> p <1, 1>

Operator >**< maps the product in $Set$ to the induced product in $C^{op} \times C$:

(>**<) :: (p <a, b>, p <c, d>) -> p (<a, b> × <c, d>)

Day convolution, which works with monoidal structures, generalizes naturally to the profunctor category:

data PDay p q s t = forall a b c d.
PDay (p a b) (q c d) ((b, d) -> t) (s -> (a, c))

## Higher Order Functors

Since profunctors form a category, we can define endofunctors in that category. This is a no-brainer in category theory, but it requires some new definitions in Haskell. Here’s a higher-order functor that maps a profunctor to another profunctor:

class HPFunctor pp where
hpmap :: (p ::~> q) -> (pp p ::~> pp q)
ddimap :: (s -> a) -> (b -> t) -> pp p a b -> pp p s t

The function hpmap lifts a natural transformation, and ddimap shows that the result of the mapping is also a profunctor.

An endofunctor in the profunctor category may have a fixed point:

newtype FixH pp a b = InH { outH :: pp (FixH pp) a b }

which is also a profunctor:

instance HPFunctor pp => Profunctor (FixH pp) where
dimap f g (InH pp) = InH (ddimap f g pp)

Finally, our Day convolution is a higher-order endofunctor in the category of profunctors:

instance HPFunctor (PDay p) where
hpmap nat (PDay p q from to) = PDay p (nat q) from to
ddimap f g (PDay p q from to) = PDay p q (g . from) (to . f)

We’ll use this fact to construct a free monoidal profunctor next.

## Free Monoidal Profunctor

In the previous post, I defined the free monoidal functor as a fixed point of the following endofunctor:

data FreeF f g t =
DoneF t
| MoreF (Day f g t)

Replacing the functors f and g with profunctors is straightforward:

data FreeP p q s t =
DoneP (s -> ()) (() -> t)
| MoreP (PDay p q s t)

The only tricky part is realizing that the first term in the sum comes from the unit of Day convolution, which is the type () -> t, and it generalizes to an appropriate pair of functions (we’ll simplify this definition later).

FreeP is a higher order endofunctor acting on profunctors:

instance HPFunctor (FreeP p) where
hpmap _ (DoneP su ut) = DoneP su ut
hpmap nat (MoreP day) = MoreP (hpmap nat day)
ddimap f g (DoneP au ub) = DoneP (au . f) (g . ub)
ddimap f g (MoreP day) = MoreP (ddimap f g day)

We can, therefore, define its fixed point:

type FreeMon p = FixH (FreeP p)

and show that it is indeed a monoidal profunctor. As before, the trick is to fist show the following property of Day convolution:

cons :: Monoidal q => PDay p q a b -> q c d -> PDay p q (a, c) (b, d)
cons (PDay pxy quv yva bxu) qcd =
PDay pxy (quv >**< qcd) (bimap yva id . reassoc)
(assoc . bimap bxu id)

where

assoc ((a,b),c) = (a,(b,c))
reassoc (a, (b, c)) = ((a, b), c)

Using this function, we can show that FreeMon p is monoidal for any p:

instance Profunctor p => Monoidal (FreeMon p) where
punit = InH (DoneP id id)
(InH (DoneP au ub)) >**< frcd = dimap snd (\d -> (ub (), d)) frcd
(InH (MoreP dayab)) >**< frcd = InH (MoreP (cons dayab frcd))

FreeMon can also be rewritten as a recursive data type:

data FreeMon p s t where
DoneFM :: t -> FreeMon p s t
MoreFM :: p a b -> FreeMon p c d ->
(b -> d -> t) ->
(s -> (a, c)) -> FreeMon p s t

## Categorical Picture

As I mentioned before, from the categorical point of view there isn’t much to talk about. We define a functor in the category of profunctors:

$A_p q = (C^{op} \times C) (1, -) + \int^{ a b c d } p a b \times q c d \times (C^{op} \times C) (\langle a, b \rangle \otimes \langle c, d \rangle, -)$

As previously shown in the general case, its initial algebra defines a free monoidal profunctor.

## Acknowledgments

I’m grateful to Eugenia Cheng not only for talking to me about monoidal profunctors, but also for getting me interested in category theory in the first place through her Catsters video series. Thanks also go to Edward Kmett for numerous discussions on this topic.

Abstract: I derive a free monoidal (applicative) functor as an initial algebra of a higher-order functor using Day convolution.

I thought I was done with monoids for a while, after writing my Monoids on Steroids post, but I keep bumping into them. This time I read a paper by Capriotti and Kaposi about Free Applicative Functors and it got me thinking about the relationship between applicative and monoidal functors. In a monoidal closed category, the two are equivalent, but monoidal structure seems to be more fundamental. It’s possible to have a monoidal category, which is not closed. Not to mention that monoidal structures look cleaner and more symmetrical than closed structures.

One particular statement in the paper caught my attention: the authors said that the free applicative functors are initial algebras for a very simple higher-order functor:

$A G = Id + F \star G$

which, in their own words, “makes precise the intuition that free applicative functors are in some sense lists (i.e. free monoids).” In this post I will decode this statement and then expand it by showing how to implement a free monoidal functor as a higher order fixed point using some properties of Day convolution.

Let me start with some refresher on lists. To define a list all we need is a monoidal category. If we pretend that Hask is a category, then our product is a pair type (a, b), which is associative up to isomorphism; with () as its unit, also up to isomorphism. We can then define a functor that generates lists:

$A_a b = () + (a, b)$

Notice the similarity with the Capriotti-Kaposi formula. You might recognize this functor in its Haskell form:

data ListF a b = Nil | Cons a b

The fixed point of this functor is the familiar list, a.k.a., the free monoid. So that’s the general idea.

## Day Convolution

There are lots of interesting monoidal structures. Famously (because of the monad quip) the category of endofunctors is monoidal; with functor composition as product and identity functor as identity. What is less known is that functors from a monoidal category $\mathscr{C}$ to $\mathscr{S}et$ also form a monoidal category. A product of two such functors is defined through Day convolution. I have talked about Day convolution in the context of applicative functors, but here I’d like to give you some more intuition.

What did it for me was Alexander Campbell comment on Stack Exchange. You know how, in a vector space, you can have a set of basis vectors, say $\vec{e}_i$, and represent any vector as a linear combination:

$\vec{v} = \sum \limits_{i = 1}^n v_i \vec{e}_i$

It turns out that, under some conditions, there is a basis in the category of functors from $\mathscr{C}$ to $\mathscr{S}et$. The basis is formed by representable functors of the form $\mathscr{C}(x, -)$, and the decomposition of a functor $F$ is given by the co-Yoneda lemma:

$F a = \int^x F x \times \mathscr{C}(x, a)$

The coend in this formula roughly corresponds to (possibly infinite) categorical sum (coproduct). The product under the integral sign is the cartesian product in $\mathscr{S}et$.

In pseudo-Haskell, we would write this formula as:

f a ~ exists x . (f x, x -> a)

because a coend corresponds to an existential type. The intuition is that, because the type x is hidden, the only thing the user can do with this data type is to fmap the function over the value f x, and that is equivalent to the value f a.

data Coyoneda f a where
Coyoneda :: f x -> (x -> a) -> Coyoneda f a

Now suppose that we want to define “multiplication” of two functors that are represented using the coend formula.

$(F \star G) a \cong \int^x F x \times \mathscr{C}(x, a) \star \int^y G y \times \mathscr{C}(y, a)$

Let’s assume that our multiplication interacts nicely with coends. We get:

$\int^{x y} F x \times G y \times (\mathscr{C}(x, a) \star \mathscr{C}(y, a))$

All that remains is to define the multiplication of our “basis vectors,” the hom-functors. Since we have a tensor product $\otimes$ in $\mathscr{C}$, the obvious choice is:

$\mathscr{C}(x, -) \star \mathscr{C}(y, -) \cong \mathscr{C}(x \otimes y, -)$

This gives us the formula for Day convolution:

$(F \star G) a = \int^{x y} F x \times G y \times \mathscr{C}(x \otimes y, a)$

We can translate it directly to Haskell:

data Day f g a = forall x y. Day (f x) (g y) ((x, y) -> a)

The actual Haskell library implementation uses GADTs, and also curries the product, but here I’m opting for encoding an existential type using forall in front of the constructor.

For those who like the analogy between functors and containers, Day convolution may be understood as containing a box of $x$s and a bag of $y$s, plus a binary combinator for turning every possible pair $(x, y)$ into an $a$.

Day convolution lifts the monoidal structure of $\mathscr{C}$ (the tensor product $\otimes$) to the functor category $[\mathscr{C}, \mathscr{S}et]$. In particular, it lifts the unit object $1$ to the hom-functor $\mathscr{C}(1, -)$. We have, for instance:

$(F \star \mathscr{C}(1, -)) a = \int^{x y} F x \times \mathscr{C}(1, y) \times \mathscr{C}(x \otimes y, a)$

which, by co-Yoneda, is isomorphic to:

$\int^{x} F x \times \mathscr{C}(x \otimes 1, a)$

Considering that $x \otimes 1$ is isomorphic to $x$, we can apply co-Yoneda again, to indeed get $F a$.

In Haskell, the unit object with respect to product is the unit type (), and the hom-functor $\mathscr{C}(1, -)$ is isomorphic to the identity functor $Id$ (a set of functions from unit to $x$ is isomorphic to $x$).

We now have all the tools to understand the formula:

$A G = Id + F \star G$

or, more generally:

$A_F G = \mathscr{C}(1, -) + F \star G$

It’s a sum (coproduct) of the unit under Day convolution and the Day convolution of two functors $F$ and $G$.

## Lax Monoidal Functors

Whenever we have a functor between two monoidal categories $\mathscr{C}$ and $\mathscr{D}$, it makes sense to ask how this functor interacts with the monoidal structure. For instance, does it map the unit object in one category to the unit object in another? Does it map the result of a tensor product to a tensor product of mapped arguments?

A lax monoidal functor doesn’t do that, but it does the next best thing. It doesn’t map unit to unit, but it provides a morphism that connects the unit in the target category to the image of the unit of the source category.

$\epsilon : 1_\mathscr{D} \to F 1_\mathscr{C}$

It also provides a family of morphisms from the product of images to the image of a product:

$\mu_{a b} : F a \otimes_\mathscr{D} F b \to F (a \otimes_\mathscr{C} b)$

which is natural in both arguments. These morphisms must satisfy additional conditions related to associativity and unitality of respective tensor products. If $\epsilon$ and $\mu$ are isomorphisms then we call $F$ a strong monoidal functor.

If you look at the way Day convolution was defined, you can see now that we insisted that the hom-functor be strong monoidal:

$\mathscr{C}(x, -) \star \mathscr{C}(y, -) \cong \mathscr{C}(x \otimes y, -)$

Since hom-functors define the Yoneda embedding $\mathscr{C} \to [\mathscr{C}, \mathscr{S}et]$, we can say that Day convolution makes Yoneda embedding strong monoidal.

The translation of the definition of a lax monoidal functor to Haskell is straightforward:

class Monoidal f where
unit  :: f ()
(>*<) :: f x -> f y -> f (x, y)

Because Hask is a closed monoidal category, Monoidal is equivalent to Applicative (see my post on Applicative Functors).

## Fixed Points

Recursive data structures can be formally defined as fixed points of F-algebras. Lists, in particular, are fixed points of functors of the form:

$F_a b = 1 + a \otimes b$

defined in a monoidal category ($\otimes$ and $1$) with coproducts (the plus sign).

In general, a fixed point is defined as a point that is fixed under some particular mapping. For instance, a fixed point of a function $f(x)$ is some value $x_0$ such that:

$f(x_0) = x_0$

Obviously, the function’s codomain has to be the same as its domain, for this equation to make sense.

By analogy, we can define a fixed point of an endofunctor $F$ as an object that is isomorphic to its image under $F$:

$F x \cong x$

The isomorphism here is just a pair of morphisms, one the inverse of the other. One of these morphisms can be seen as part of the F-algebra $(x, f)$ whose carrier is $x$ and whose action is:

$f : F x \to x$

Lambek’s lemma states that the action of the initial (or terminal) F-algebra is an isomorphism. This explains why a fixed point of a functor is often referred to as an initial (terminal) algebra.

In Haskell, a fixed point of a functor f is called Fix f. It is defined by the property that f acting on Fix f must be isomorphic to Fix f:

$Fix f \cong f (Fix f)$

which can be expressed as:

newtype Fix f = In { out :: f (Fix f) }

Notice that the pair $(Fix f, In)$ is the (initial) algebra for the functor $f$, with the carrier $Fix f$ and the action $In$; and that out is the inverse of In, as prescribed by the Lambek’s lemma.

Here’s another useful intuition about fixed points: they can often be calculated iteratively as a limit of a sequence. For functions, if the following sequence converges to $x$:

$x_{n+1} = f (x_n)$

then $f(x) = x$ (at least for continuous functions).

We can apply the same idea to our list functor, iteratively replacing $b$ with the definition of $F_a b = 1 + a \otimes b$:

$1 + a \otimes b\\ 1 + a \otimes (1 + a \otimes b)\\ 1 + a + a \otimes a \otimes (1 + a \otimes b)\\ 1 + a + a \otimes a + a \otimes a \otimes a + ...$

where we assumed associativity and unit laws (up to isomorphism). This formal expansion is in agreement with our intuition that a list is either empty, contains one element, a product of two elements, a product of three elements, and so on…

## Higher Order Functors

Category theory has achieved something we can only dream of in programming languages: reusability of concepts. For instance, functors between two categories $C$ and $D$ form a category, with natural transformations as morphisms. Therefore everything we said about fixed points and algebras can be immediately applied to the functor category. In Haskell, however, we have to start almost from scratch. For instance, a higher order functor, which takes a functor as argument and returns another functor has to be defined as:

class HFunctor ff where
ffmap :: Functor g => (a -> b) -> ff g a -> ff g b
hfmap :: (g :~> h) -> (ff g :~> ff h)

The squiggly arrows are natural transformations:

infixr 0 :~>
type f :~> g = forall a. f a -> g a

Notice that the definition of HFunctor not only requires a higher-order version of fmap called hfmap, which lifts natural transformations, but also the lower-order ffmap that attests to the fact that the result of HFunctor is again a functor. (Quantified class constraints will soon make this redundant.)

The definition of a fixed point also has to be painstakingly rewritten:

newtype FixH ff a = InH { outH :: ff (FixH ff) a }

Functoriality of a higher order fixed point is easily established:

instance HFunctor f => Functor (FixH f) where
fmap h (InH x) = InH (ffmap h x)

Finally, Day convolution is a higher order functor:

instance HFunctor (Day f) where
hfmap nat (Day fx gy xyt) = Day fx (nat gy) xyt
ffmap h   (Day fx gy xyt) = Day fx gy (h . xyt)

## Free Monoidal Functor

With all the preliminaries out of the way, we are now ready to derive the main result.

We start with the higher-order functor whose initial algebra defines the free monoidal functor:

$A_F G = Id + F \star G$

We can translate it to Haskell as:

data FreeF f g t =
DoneF t
| MoreF (Day f g t)

It is a higher order functor, in that it takes a functor g and produces a new functor FreeF f g:

instance HFunctor (FreeF f) where
hfmap _ (DoneF x) = DoneF x
hfmap nat (MoreF day) = MoreF (hfmap nat day)
ffmap f (DoneF x) = DoneF (f x)
ffmap f (MoreF day) = MoreF (ffmap f day)

The claim is that, for any functor f, the (higher order) fixed point of FreeF f:

type FreeMon f = FixH (FreeF f)

is monoidal.

The usual approach to solving such a problem is to express FreeMon as a recursive data structure:

data FreeMonR f t =
Done t
| More (Day f (FreeMonR f) t)

and proceed from there. This is fine, but it doesn’t give us any insight about what property of the original higher-order functor makes its fixed point monoidal. So instead, I will concentrate on properties of Day convolution.

To begin with, let’s establish the functoriality of FreeMon using the fact that Day convolution is a functor:

instance Functor f => Functor (FreeMon f) where
fmap h (InH (DoneF s)) = InH (DoneF (h s))
fmap h (InH (MoreF day)) = InH (MoreF (ffmap h day))

The next step is based on the list analogy. The free monoidal functor is analogous to a list in which the product is replaced by Day convolution. The proof that it’s monoidal amounts to showing that one can “concatenate” two such lists. Concatenation is a recursive process in which we detach an element from one list and attach it to the other.

When building recursion, the functor $g$ in Day convolution will play the role of the tail of the list. We’ll prove monoidality of the fixed point inductively by assuming that the tail is already monoidal. Here’s the crucial step expressed in terms of Day convolution:

cons :: Monoidal g => Day f g s -> g t -> Day f g (s, t)
cons (Day fx gy xys) gt = Day fx (gy >*< gt) (bimap xys id) . reassoc)

reassoc :: (a, (b, c)) -> ((a, b), c)
reassoc (a, (b, c)) = ((a, b), c)

and functoriality (bimap) of the underlying product.

The intuition here is that we have a Day product of the head of the list, which is a box of $x$s; and the tail, which is a container of $y$s. We are appending to it another container of $t$s. We do it by concatenating the two containers $(gy >*< gt)$ into one container of pairs $(y \otimes t)$. The new combinator reassociates the nested pairs $(x \otimes (y \otimes t))$ and applies the old combinator to $(x \otimes y)$.

The final step is to show that FreeMon defined through Day convolution is indeed monoidal. Here’s the proof:

instance Functor f => Monoidal (FreeMon f) where
unit = InH (DoneF ())
(InH (DoneF s)) >*< frt = fmap (s, ) frt
(InH (MoreF day)) >*< frt = InH (MoreF (day cons frt))

A lax monoidal functor must also preserve associativity and unit laws. Unlike the corresponding laws for applicative functors, these are pretty straightforward to formulate.

The unit laws are:

fmap lunit (unit () >*< frx) = frx
fmap runit (frx >*< unit ()) = frx

where I used the left and right unitors:

lunit :: ((), a) -> a
lunit ((), a) = a
runit :: (a, ()) -> a
runit (a, ()) = a

The associativity law is:

fmap assoc ((frx >*< fry) >*< frz) = (frx >*< (fry >*< frz))

where I used the associator:

assoc :: ((a,b),c) -> (a,(b,c))
assoc ((a,b),c) = (a,(b,c))

Except for the left unit law, I wasn’t able to find simple derivations of these laws.

## Categorical Picture

Translating this construction to category theory, we start with a monoidal category $(\mathscr{C}, \otimes, 1, \alpha, \rho, \lambda)$, where $\alpha$ is the associator, and $\rho$ and $\lambda$ are right and left unitors, respectively. We will be constructing a lax monoidal functors from $\mathscr{C}$ to $\mathscr{S}et$, the latter equipped with the usual cartesian product and coproduct.

I will sketch some of the constructions without going into too much detail.

The analogue of cons is a family of natural transformations:

$\beta_{s t} = \int^{x y} f x \times g y \times \mathscr{C}(x \otimes y, s) \times g t \to \int^{u v} f u \times g v \times \mathscr{C}(u \otimes v, s \otimes t)$

We will assume that $g$ is lax monoidal, so the left hand side can be mapped to:

$\int^{x y} f x \times g (y \otimes t) \times \mathscr{C}(x \otimes y, s)$

The set of natural transformations can be represented as an end:

$\int_{s t} Set(\int^{x y} f x \times g (y \otimes t) \times \mathscr{C}(x \otimes y, s), \int^{u v} f u \times g v \times \mathscr{C}(u \otimes v, s \otimes t))$

A hom-set from a coend is isomorphic to an end of a hom-set:

$\int_{s t x y} Set(f x \times g (y \otimes t) \times \mathscr{C}(x \otimes y, s), \int^{u v} f u \times g v \times \mathscr{C}(u \otimes v, s \otimes t))$

There is an injection that is a member of this hom-set:

$i_{x, y \otimes t} : f x \times g (y \otimes t) \times \mathscr{C}(x \otimes (y \otimes t), -) \to \int^{u v} f u \times g v \times \mathscr{C}(u \otimes v, -)$

Given a morphism $h$ that is a member of $\mathscr{C}(x \otimes y, s)$, we can construct the morphism $(h \otimes id) \circ \alpha^{-1}$, which is a member of $\mathscr{C}(x \otimes (y \otimes t), s \otimes t)$.

The free monoidal functor $Free_f$ is given as the initial algebra of the (higher-order) endofunctor acting on a functor $g$ from $[\mathscr{C}, \mathscr{S}et]$:

$A_f g = \mathscr{C}(1, -) + f \star g$

By Lambek’s lemma, the action of this functor on the fixed point is naturally isomorphic to the fixed point itself:

$\mathscr{C}(1, -) + (f \star Free_f) \cong Free_f$

We want to show that $Free_f$ is lax monoidal, that is that there’s a mapping:

$\epsilon : 1 \to Free_f \, 1$

and a family of natural transformations:

$\mu_{s t} : Free_f\, s \times Free_f\, t \to Free_f\, (s \otimes t)$

The first one can be simply chosen as the identity $id_1$ of the singleton set.

Let’s rewrite the type of natural transformations in the second one as an end:

$\int_{s t} \mathscr{S}et(Free_f\, s \times Free_f\, t, Free_f\, (s \otimes t))$

We can expand the first factor using the Lambek’s lemma:

$\int_{s t} \mathscr{S}et((\mathscr{C}(1,s) + (f \star Free_f) s) \times Free_f\, t, Free_f\, (s \otimes t))$

distribute the product over the sum:

$\int_{s t} \mathscr{S}et(\mathscr{C}(1,s)\times Free_f\, t + (f \star Free_f) s \times Free_f\, t, Free_f\, (s \otimes t))$

and replace functions from coproducts with products of functions:

$\int_{s t} \mathscr{S}et(\mathscr{C}(1,s)\times Free_f\, t, Free_f\, (s \otimes t)) \times$

$\int_{s t} \mathscr{S}et((f \star Free_f) s \times Free_f\, t, Free_f\, (s \otimes t))$

The first hom-set forms the base of induction and the second is the inductive step. If we call a member of $\mathscr{C}(1,s)$ $h$ then we can implement the first function as the lifting of $(h 1 \otimes -)$ acting on $Free_f\, t$, and for the second, we can use $\beta_{s t}$.

## Conclusion

The purpose of this post was to explore the formulation of a free lax monoidal functor without recourse to closed structure. I have to admit to a hidden agenda: The profunctor formulation of traversables involves monoidal profunctors, so that’s what I’m hoping to explore in the next post.

While reviewing the draft of this post, Oleg Grenrus suggested that I derive free monad as a fixed point of a higher order functor. The monoidal product in this case is endofunctor composition:

newtype Compose f g a = Compose (f (g a))

The higher-order functor in question can be written as:

$A_f g = Id + f \circ g$

data FreeMonadF f g a =
DoneFM a
| MoreFM (Compose f g a)
instance Functor f => HFunctor (FreeMonadF f) where
hfmap _ (DoneFM a) = DoneFM a
hfmap nat (MoreFM (Compose fg)) = MoreFM $Compose$ fmap nat fg
ffmap h (DoneFM a) = DoneFM (h a)
ffmap h (MoreFM (Compose fg)) = MoreFM $Compose$ fmap (fmap h) fg

The free monad is given by the fixed point:

type FreeMonad f = FixH (FreeMonadF f)

as witnessed by the following instance definition:

instance Functor f => Monad (FreeMonad f) where
return = InH . DoneFM
(InH (DoneFM a)) >>= k = k a
fma >>= k = join (fmap k fma)
join :: Functor f => FreeMonad f (FreeMonad f a) -> FreeMonad f a
join (InH (DoneFM x)) = x
join (InH (MoreFM (Compose ffr))) =
InH $MoreFM$ Compose \$ fmap join ffr

In my category theory blog posts, I stated many theorems, but I didn’t provide many proofs. In most cases, it’s enough to know that the proof exists. We trust mathematicians to do their job. Granted, when you’re a professional mathematician, you have to study proofs, because one day you’ll have to prove something, and it helps to know the tricks that other people used before you. But programmers are engineers, and are therefore used to taking advantage of existing solutions, trusting that somebody else made sure they were correct. So it would seem that mathematical proofs are irrelevant to programming. Or so it may seem, until you learn about the Curry-Howard isomorphism–or propositions as types, as it is sometimes called–which says that there is a one to one correspondence between logic and programs, and that every function can be seen as a proof of a theorem. And indeed, I found that a lot of proofs in category theory turn out to be recipes for implementing functions. In most cases the problem can be reduced to this: Given some morphisms, implement another morphism, usually using simple composition. This is very much like using point-free notation to define functions in Haskell. The other ingredient in categorical proofs is diagram chasing, which is very much like equational resoning in Haskell. Of course, mathematicians use different notation, and they use lots of different categories, but the principles are the same.

I want to illustrate these points with the example from Emily Riehl’s excellent book Category Theory in Context. It’s a book for mathematicians, so it’s not an easy read. I’m going to concentrate on theorem 6.2.1, which derives a formula for left Kan extensions using colimits. I picked this theorem because it has calculational content: it tells you how to calculate a particular functor.

It’s not a short proof, and I have made it even longer by unpacking every single step. These steps are not too hard, it’s mostly a matter of understanding and using definitions of functoriality, naturality, and universality.

There is a bonus at the end of this post for Haskell programmers.

## Kan Extensions

I wrote about Kan extensions before, so here I’ll only recap the definition using the notation from Emily’s book. Here’s the setup: We want to extend a functor $F$, which goes from category $C$ to $E$, along another functor $K$, which goes from $C$ to $D$. This extension is a new functor from $D$ to $E$.

To give you some intuition, imagine that the functor $F$ is the Rosetta Stone. It’s a functor that maps the Ancient Egyptian text of a royal decree to the same text written in Ancient Greek. The functor $K$ embeds the Rosetta Stone hieroglyphics into the know corpus of Egyptian texts from various papyri and inscriptions on the walls of temples. We want to extend the functor $F$ to the whole corpus. In other words, we want to translate new texts from Egyptian to Greek (or whatever other language that’s isomorphic to it).

In the ideal case, we would just want $F$ to be isomorphic to the composition of the new functor after $K$. That’s usually not possible, so we’ll settle for less. A Kan extension is a functor which, when composed with $K$ produces something that is related to $F$ through a natural transformation. In particular, the left Kan extension, $Lan_K F$, is equipped with a natural transformation $\eta$ from $F$ to $Lan_K F \circ K$.

(The right Kan extension has this natural transformation reversed.)

There are usually many such functors, so there is the standard trick of universal construction to pick the best one.

In our analogy, we would ideally like the new functor, when applied to the hieroglyphs from the Rosetta stone, to exactly reproduce the original translation, but we’ll settle for something that has the same meaning. We’ll try to translate new hieroglyphs by looking at their relationship with known hieroglyphs. That means we should look closely at morphism in $D$.

## Comma Category

The key to understanding how Kan extensions work is to realize that, in general, the functor $K$ embeds $C$ in $D$ in a lossy way.

There may be objects (and morphisms) in $D$ that are not in the image of $K$. We have to somehow define the action of $Lan_K F$ on those objects. What do we know about such objects?

We know from the Yoneda lemma that all the information about an object is encoded in the totality of morphisms incoming to or outgoing from that object. We can summarize this information in the form of a functor, the hom-functor. Or we can define a category based on this information. This category is called the slice category. Its objects are morphisms from the original category. Notice that this is different from Yoneda, where we talked about sets of morphisms — the hom-sets. Here we treat individual morphisms as objects.

This is the definition: Given a category $C$ and a fixed object $c$ in it, the slice category $C/c$ has as objects pairs $(x, f)$, where $x$ is an object of $C$ and $f$ is a morphism from $x$ to $c$. In other words, all the arrows whose codomain is $c$ become objects in $C/c$.

A morphism in $C/c$ between two objects, $(x, f)$ and $(y, g)$ is a morphism $h : x \to y$ in $C$ that makes the following triangle commute:

In our case, we are interested in an object $d$ in $D$, and the slice category $D/d$ describes it in terms of morphisms. Think of this category as a holographic picture of $d$.

But what we are really interested in, is how to transfer this information about $d$ to $E$. We do have a functor $F$, which goes from $C$ to $E$. We need to somehow back-propagate the information about $d$ to $C$ along $K$, and then use $F$ to move it to $E$.

So let’s try again. Instead of using all morphisms impinging on $d$, let’s only pick the ones that originate in the image of $K$, because only those can be back-propagated to $C$.

This gives us limited information about $d$, but it’ll have to do. We’ll just use a partial hologram of $d$. Instead of the slice category, we’ll use the comma category $K \downarrow d$.

Here’s the definition: Given a functor $K : C \to D$ and an object $d$ of $D$, the comma category $K \downarrow d$ has as objects pairs $(c, f)$, where $c$ is an object of $C$ and $f$ is a morphism from $K c$ to $d$. So, indeed, this category describes the totality of morphisms coming to $d$ from the image of $K$.

A morphism in the comma category from $(c, f)$ to $(c', g)$ is a morphism $h : c \to c'$ such that the following triangle commutes:

So how does back-propagation work? There is a projection functor $\Pi_d : K \downarrow d \to C$ that maps an object $(c, f)$ to $c$ (with obvious action on morphisms). This functor loses a lot of information about objects (completely forgets the $f$ part), but it keeps a lot of the information in morphisms — it only picks those morphisms in $C$ that preserve the structure of the comma category. And it lets us “upload” the hologram of $d$ into $C$

Next, we transport all this information to $E$ using $F$. We get a mapping

$F \circ \Pi_d : K \downarrow d \to E$

Here’s the main observation: We can look at this functor $F \circ \Pi_d$ as a diagram in $E$ (remember, when constructing limits, diagrams are defined through functors). It’s just a bunch of objects and morphisms in $E$ that somehow approximate the image of $d$. This holographic information was extracted by looking at morphisms impinging on $d$. In our search for $(Lan_K F) d$ we should therefore look for an object in $E$ together with morphisms impinging on it from the diagram we’ve just constructed. In particular, we could look at cones under that diagram (or co-cones, as they are sometimes called). The best such cone defines a colimit. If that colimit exists, it is indeed the left Kan extension $(Lan_K F) d$. That’s the theorem we are going to prove.

To prove it, we’ll have to go through several steps:

1. Show that the mapping we have just defined on objects is indeed a functor, that is, we’ll have to define its action on morphisms.
2. Construct the transformation $\eta$ from $F$ to $Lan_K F \circ K$ and show the naturality condition.
3. Prove universality: For any other functor $G$ together with a natural transformation $\gamma$, show that $\gamma$ uniquely factorizes through $\eta$.

All of these things can be shown using clever manipulations of cones and the universality of the colimit. Let’s get to work.

## Functoriality

We have defined the action of $Lan_K F$ on objects of $D$. Let’s pick a morphism $g : d \to d'$. Just like $d$, the object $d'$ defines its own comma category $K \downarrow d'$, its own projection $\Pi_{d'}$, its own diagram $F \circ \Pi_{d'}$, and its own limiting cone. Parts of this new cone, however, can be reinterpreted as a cone for the old object $d$. That’s because, surprisingly, the diagram $F \circ \Pi_{d'}$ contains the diagram $F \circ \Pi_{d}$.

Take, for instance, an object $(c, f) : K \downarrow d$, with $f: K c \to d$. There is a corresponding object $(c, g \circ f)$ in $K \downarrow d'$. Both get projected down to the same $c$. That shows that every object in the diagram for the $d$ cone is automatically an object in the diagram for the $d'$ cone.

Now take a morphism $h$ from $(c, f)$ to $(c', f')$ in $K \downarrow d$. It is also a morphism in $K \downarrow d'$ between $(c, g \circ f)$ and $(c', g \circ f')$. The commuting triangle condition in $K \downarrow d$

$f = f' \circ K h$

ensures the commuting condition in $K \downarrow d'$

$g \circ f = g \circ f' \circ K h$

All this shows that the new cone that defines the colimit of $F \circ \Pi_{d'}$ contains a cone under $F \circ \Pi_{d}$.

But that diagram has its own colimit $(Lan_K F) d$. Because that colimit is universal, there must be a unique morphism from $(Lan_K F) d$ to $(Lan_K F) d'$, which makes all triangles between the two cones commute. We pick this morphism as the lifting of our $g$, which ensures the functoriality of $Lan_K F$.

The commuting condition will come in handy later, so let’s spell it out. For every object $(c, f:Kc \to d)$ we have a leg of the cone, a morphism $V_d^{(c, f)}$ from $F c$ to $(Lan_K F) d$; and another leg of the cone $V_{d'}^{(c, g \circ f)}$ from $F c$ to $(Lan_K F) d'$. If we denote the lifting of $g$ as $(Lan_K F) g$ then the commuting triangle is:

$(Lan_K F) g \circ V_{d}^{(c, f)} = V_{d'}^{(c, g \circ f)}$

In principle, we should also check that this newly defined functor preserves composition and identity, but this pretty much follows automatically whenever lifting is defined using composition of morphisms, which is indeed the case here.

## Natural Transformation

We want to show that there is a natural transformation $\eta$ from $F$ to $Lan_K F \circ K$. As usual, we’ll define the component of this natural transformation at some arbitrary object $c$. It’s a morphism between $F c$ and $(Lan_K F) (K c)$. We have lots of morphisms at our disposal, with all those cones lying around, so it shouldn’t be a problem.

First, observe that, because of the pre-composition with $K$, we are only looking at the action of $Lan_K F$ on objects which are inside the image of $K$.

The objects of the corresponding comma category $K \downarrow K c'$ have the form $(c, f)$, where $f : K c \to K c'$. In particular, we can pick $c' = c$, and $f = id_{K c}$, which will give us one particular vertex of the diagram $F \circ \Pi_{K c}$. The object at this vertex is $F c$ — exactly what we need as a starting point for our natural transformation. The leg of the colimiting cone we are interested in is:

$V_{K c}^{(c, id)} : F c \to (Lan_K F) (K c)$

We’ll pick this leg as the component of our natural transformation $\eta_c$.

What remains is to show the naturality condition. We pick a morphism $h : c \to c'$. We know how to lift this morphism using $F$ and using $Lan_K F \circ K$. The other two sides of the naturality square are $\eta_c$ and $\eta_{c'}$.

The bottom left composition is $(Lan_K F) (K h) \circ V_{K c}^{(c, id)}$. Let’s take the commuting triangle that we used to show the functoriality of $Lan_K F$:

$(Lan_K F) g \circ V_{d}^{(c, f)} = V_{d'}^{(c, g \circ f)}$

and replace $f$ by $id_{K c}$, $g$ by $K h$, $d$ by $K c$, and $d'$ by $K c'$, to get:

$(Lan_K F) (K h) \circ V_{K c}^{(c, id)} = V_{K c'}^{(c, K h)}$

Let’s draw this as the diagonal in the naturality square, and examine the upper right composition:

$V_{K c'}^{(c', id)} \circ F h$.

This is also equal to the diagonal $V_{K c'}^{(c, K h)}$. That’s because these are two legs of the same colimiting cone corresponding to $(c', id_{K c'})$ and $(c, K h)$. These vertices are connected by $h$ in $K \downarrow K c'$.

But how do we know that $h$ is a morphism in $K \downarrow K c'$? Not every morphism in $C$ is a morphism in the comma category. In this case, however, the triangle identity is automatic, because one of its sides is an identity $id_{K c'}$.

We have shown that our naturality square is composed of two commuting triangles, with $V_{K c'}^{(c, K h)}$ as its diagonal, and therefore commutes as a whole.

## Universality

Kan extension is defined using a universal construction: it’s the best of all functors that extend $F$ along $K$. It means that any other extension will factor through ours. More precisely, if there is another functor $G$ and another natural transformation:

$\gamma : F \to G \circ K$

then there is a unique natural transformation $\alpha$, such that

$(\alpha \cdot K) \circ \eta = \gamma$

(where we have a horizontal composition of a natural transformation $\alpha$ with the functor $K$)

As always, we start by clearly stating the goals. The proof proceeds in these steps:

1. Implement $\alpha$.
2. Prove that it’s natural.
3. Show that it factorizes $\gamma$ through $\eta$.
4. Show that it’s unique.

We are defining a natural transformation $\alpha$ between two functors: $Lan_K F$, which we have defined as a colimit, and $G$. Both are functors from $D$ to $E$. Let’s implement the component of $\alpha$ at some $d$. It must be a morphism:

$\alpha_d : (Lan_K F) d \to G d$

Notice that $d$ varies over all of $D$, not just the image of $K$. We are comparing the two extensions where it really matters.

We have at our disposal the natural transformation:

$\gamma : F \to G \circ K$

or, in components:

$\gamma_c : F c \to G (K c)$

More importantly, though, we have the universal property of the colimit. If we can construct a cone with the nadir at $G d$ then we can use its factorizing morphism to define $\alpha_d$.

This cone has to be built under the same diagram as $(Lan_K F) d$. So let’s start with some $(c, f : K c \to d)$. We want to construct a leg of our new cone going from $F c$ to $G d$. We can use $\gamma_c$ to get to $G (K c)$ and then hop to $G d$ using $G f$. Thus we can define the new cone’s leg as:

$W_c = G f \circ \gamma_c$

Let’s make sure that this is really a cone, meaning, its sides must be commuting triangles.

Let’s pick a morphism $h$ in $K \downarrow d$ from $(c, f)$ to $(c', f')$. A morphism in $K \downarrow d$ must satisfy the triangle condition, $f = f' \circ K h$:

We can lift this triangle using $G$:

$G f = G f' \circ G (K h)$

Naturality condition for $\gamma$ tells us that:

$\gamma_{c'} \circ F h = G (K h) \circ \gamma_c$

By combining the two, we get the pentagon:

whose outline forms a commuting triangle:

$G f \circ \gamma_c = G f' \circ \gamma_{c'} \circ F h$

Now that we have a cone with the nadir at $G d$, universality of the colimit tells us that there is a unique morphism from the colimiting cone to it that factorizes all triangles between the two cones. We make this morphism our $\alpha_d$. The commuting triangles are between the legs of the colimiting cone $V_d^{(c, f)}$ and the legs of our new cone $W_c$:

$\alpha_d \circ V_d^{(c, f)} = G f \circ \gamma_c$

Now we have to show that so defined $\alpha$ is a natural transformation. Let’s pick a morphism $g : d \to d'$. We can lift it using $Lan_K F$ or using $G$ in order to construct the naturality square:

$G g \circ \alpha_d = \alpha_{d'} \circ (Lan_K F) g$

Remember that the lifting of a morphism by $Lan_K F$ satisfies the following commuting condition:

$(Lan_K F) g \circ V_{d}^{(c, f)} = V_{d'}^{(c, g \circ f)}$

We can combine the two diagrams:

The two arms of the large triangle can be replaced using the diagram that defines $\alpha$, and we get:

$G (g \circ f) \circ \gamma_c = G g \circ G f \circ \gamma_c$

which commutes due to functoriality of $G$.

Now we have to show that $\alpha$ factorizes $\gamma$ through $\eta$. Both $\eta$ and $\gamma$ are natural transformations between functors going from $C$ to $E$, whereas $\alpha$ connects functors from $D$ to $E$. To extend $\alpha$, we horizontally compose it with the identity natural transformation from $K$ to $K$. This is called whiskering and is written as $\alpha \cdot K$. This becomes clear when expressed in components. Let’s pick an object $c$ in $C$. We want to show that:

$\gamma_c = \alpha_{K c} \circ \eta_c$

Let’s go back all the way to the definition of $\eta$:

$\eta_c = V_{K c}^{(c, id)}$

where $id$ is the identity morphism at $K c$. Compare this with the definition of $\alpha$:

$\alpha_d \circ V_d^{(c, f)} = G f \circ \gamma_c$

If we replace $f$ with $id$ and $d$ with $K c$, we get:

$\alpha_{K c} \circ V_{K c}^{(c, id)} = G id \circ \gamma_c$

which, due to functoriality of $G$ is exactly what we need:

$\alpha_{K c} \circ \eta_c = \gamma_c$

Finally, we have to prove the uniqueness of $\alpha$. Here’s the trick: assume that there is another natural transformation $\alpha'$ that factorizes $\gamma$. Let’s rewrite the naturality condition for $\alpha'$:

$G g \circ \alpha'_d = \alpha'_{d'} \circ (Lan_K F) g$

Replacing $g : d \to d'$ with $f : K c \to d$, we get:

$G f \circ \alpha'_{K c} = \alpha'_d \circ (Lan_K F) f$

The lifiting of $f$ by $Lan_K F$ satisfies the triangle identity:

$V_d^{(c, f)} = (Lan_K F) f \circ V_{K c}^{(c, id)}$

where we recognize $V_{K c}^{(c, id)}$ as $\eta_c$.

We said that $\alpha'$ factorizes $\gamma$ through $\eta$:

$\gamma_c = \alpha'_{K c} \circ \eta_c$

which let us straighten the left side of the pentagon.

This shows that $\alpha'$ is another factorization of the cone with the nadir at $G d$ through the colimit cone with the nadir $(Lan_K F) d$. But that would contradict the universality of the colimit, therefore $\alpha'$ must be the same as $\alpha$.

This completes the proof.

This post would not be complete if I didn’t mention a Haskell implementation of Kan extensions by Edward Kmett, which you can find in the library Data.Functor.Kan.Lan. At first look you might not recognize the definition given there:

data Lan g h a where
Lan :: (g b -> a) -> h b -> Lan g h a

To make it more in line with the previous discussion, let’s rename some variables:

data Lan k f d where
Lan :: (k c -> d) -> f c -> Lan k f d

This is an example of GADT syntax, which is a Haskell way of implementing existential types. It’s equivalent to the following pseudo-Haskell:

Lan k f d = exists c. (k c -> d, f c)

This is more like it: you may recognize (k c -> d) as an object of the comma category $K \downarrow d$, and f c as the mapping of $c$ (which is the projection of this object back to $C$) under the functor $F$. In fact, the Haskell representation is based on the encoding of the colimit using a coend:

$(Lan_K F) d = \int^{c \in C} C(K c, d) \times F c$

The Haskell library also contains the proof that Kan extension is a functor:

instance Functor (Lan k f) where
fmap g (Lan kcd fc) = Lan (g . kcd) fc

The natural transformation $\eta$ that is part of the definition of the Kan extension can be extracted from the Haskell definition:

eta :: f c -> Lan k f (k c)
eta = Lan id

In Haskell, we don’t have to prove naturality, as it is a consequence of parametricity.

The universality of the Kan extension is witnessed by the following function:

toLan :: Functor g => (forall c. f c -> g (k c)) -> Lan k f d -> g d
toLan gamma (Lan kcd fc) = fmap kcd (gamma fc)

It takes a natural transformation $\gamma$ from $F$ to $G \circ K$, and produces a natural transformation we called $\alpha$ from $Lan_K F$ to $G$.

This is $\gamma$ expressed as a composition of $\alpha$ and $\eta$:

fromLan :: (forall d. Lan k f d -> g d) -> f c -> g (k c)
fromLan alpha = alpha . eta

As an example of equational reasoning, let’s prove that $\alpha$ defined by toLan indeed factorizes $\gamma$. In other words, let’s prove that:

fromLan (toLan gamma) = gamma

Let’s plug the definition of toLan into the left hand side:

fromLan (\(Lan kcd fc) -> fmap kcd (gamma fc))

then use the definition of fromLan:

(\(Lan kcd fc) -> fmap kcd (gamma fc)) . eta

Let’s apply this to some arbitrary function g and expand eta:

(\(Lan kcd fc) -> fmap kcd (gamma fc)) (Lan id g)

Beta-reduction gives us:

fmap id (gamma g)

which is indeed equal to the right hand side acting on g:

gamma g

The proof of toLan . fromLan = id is left as an exercise to the reader (hint: you’ll have to use naturality).

## Acknowledgments

I’m grateful to Emily Riehl for reviewing the draft of this post and for writing the book from which I borrowed this proof.