We encountered algebras for endofunctors in the previous post. In this post, shall introduce the dual notion, endofunctor coalgebras, and shall look at some special endofunctor algebras and coalgebras. These constructions have wide applications, but we shall be particularly interested in their usefulness for systematically constructing certain classes of monads.

## Initial Algebras

For an endofunctor , an initial -algebra is simply an initial object in . Explicitly, this is an -algebra such that for every -algebra there is a unique morphism such that

The first crucial fact to know about initial algebras, known as Lambek’s Lemma, is that is always an isomorphism. This highlights that initial algebras generalise the notion of least fixed points for monotone maps.

Lambek’s lemma immediately tells us that certain initial algebras cannot exist.

**Example**: The powerset functor does not have an initial algebra. as there is no set such that .

As usual, we will motivate constructions by considering algebraic examples of initial algebras.

**Example**: Consider a signature . We inductively define sets , with , and:

- If is a constant symbol in , .
- If is an -ary operation symbol, and then .

It is hopefully clear that if the signature contains no constant symbols, all of the will be empty. To see what happens with a signature with constants, let , with a constant, and a binary operation. We then have:

- .
- .
- .
- And so on …

The least fixed point of this process is the set of *ground terms* over the signature, and is for the corresponding signature functor. The algebra structure map maps the left component to the term , and on the second component:

The general case for an arbitrary signature is formally identical.

So the initial algebra of a signature functor is the set of ground terms, equipped with the obvious purely syntactic operations that just form new terms. As usual, we are actually interested in monads, but these don’t involve just ground terms, we need to get the variables involved. So we’re really interested in free algebras over some set, not just the initial algebra.

**Example**: Let be some signature. To build the free algebra over the set , we construct sets , with , and:

- If is a constant symbol then .
- If then .
- If is an -ary operation symbol, and then .

If we take as in the previous example, and for simplicity, we have:

- .
- .
- .
- And so on…

The least fixed point of this process is the set of all such terms over , which is , where is the corresponding signature functor. We can think of this as forming the initial algebra for a signature extended with a constant symbol for each element of . Recalling the notation for the free monad, we have deduced that, at least at the level of objects .

In fact, this approach works in great generality. For an endofunctor on a category with binary coproducts, if the functor has an initial algebra, then is the free -algebra over .

From the above, it follows that if all the functors have initial algebras, the free monad on is given by:

If we restrict our attention to signature functors, this simply yields another perspective on the description of free monads we saw in the previous post. For other functors, it does yield new monads.

**Example**: Although there is no initial algebra for the full powerset functor, life is much better for the finite powerset functor . Each of the functors has an initial algebra. The elements of are the hereditarily finite sets. These are finite sets, with all elements also hereditarily finite. Similarly the elements of are hereditarily finite sets with atoms from . That is, finite sets, with all elements either elements of , or hereditarily finite sets with atoms from .

## Final Coalgebras

For an endofunctor , a coalgebra is a pair consisting of a -object , and a -morphism . This is the dual notion to that of an -algebra. These form a category , with morphisms being -morphisms such that

Coalgebras are interesting objects, to which we could devote a great deal of discussion. If algebras are about composing things, coalgebras are about taking them apart again. In compute science, they are often used to model objects with some sort of observable behaviour. We consider one very simple example to illustrate the idea.

**Example**: A coalgebra for the endofunctor is simply a function of the form . We could interpret this as a device, for which when we press the “go button” either jumps to a new state, ready to go again, or moves to a failure state.

We will resist the temptation to discuss coalgebra theory more generally at this point, and move rapidly on to the construction we are interested in.

A final coalgebra (also sometime termed a terminal coalgebra) for an endofunctor is a final object in . Explicitly, this is an object such that for every coalgebra there is a unique map such that

Final coalgebras generalise the notion of greatest fixed point for a monotone map, and satisfy a dual form of Lambek’s lemma.

**Example**: There is no final coalgebra for the powerset functor, as Lambek’s lemma for coalgebras would lead to a contradiction about cardinalities of powersets.

For our purposes in this post, we are not going to delve deeply into questions about the terminal coalgebra, although it does have great conceptual and mathematical significance. Informally, it can be seen as abstracting all possible behaviours that a coalgebra can exhibit, but we won’t pursue this remark now.

Instead, we’re going to ask a very naive question. Assuming they exist, the objects yielded a monad, the free monad . Do the objects of the form also carry the structure of a monad as well?

This wouldn’t make a very satisfying end to this blog post if the answer were no, so unsurprisingly, the switch to considering terminal coalgebras does yield a monad. For an endofunctor , if all the required final coalgebras exist, we construct a monad in Kleisli form as follows:

- The object map is .
- By Lambek’s lemma is an isomorphism. We take as our monad unit the composite .
- For a morphism , we can define its Kleisli extension as the unique -algebra morphism such that . That there is such an requires some more theory, that hopefully we can discuss another time. There is a more explicit description of this morphism, but unfortunately it is cumbersome to describe in this format.

Obviously we need to check the appropriate axioms hold for a Kleisli triple, but this works out. The resulting monad is known as the **free completely iterative monad** on . Furthermore, it is hopefully not too hard to see that, similarly to the free monad construction, this monad “keeps variables separate”. That is, the resulting monad is another example of the ideal monads that we discussed in the previous post. There is a lot more to say about completely iterative monads, and what the various adjectives mean, but we shall defer that to a later post. Instead, we shall conclude with an example.

**Example**: Let be an algebraic signature. For the corresponding signature functor, the final coalgebra for all of the functors of the form exists. Therefore the free completely iterative monad on exists. The elements of are -trees. That is, trees such that:

- Every internal node of arity is labelled by an -ary operation symbol.
- Every leaf node is labelled by either a constant symbol, or an element of .

Note, these trees can be either finite or infinite, unlike for any of our previously examples, where we work with -terms, which are equivalently finite -trees. Intuitively, we should think of the extra elements in as “infinite -terms”.

**Remark**: In this post, we have simply introduced various initial algebras and final coalgebras. This sort of “guess and check” approach to finding the right construction is ok, but there are a lot of more systematic strategies for establishing both the existence and a description of these objects. This is a large subject, and involves more technical background that we have introduced so far. A later post may return to this topic.

## One thought on “Initial Ideas and Final Thoughts”