3

I'm trying to understand what the categorical definition of (co)recursion means. Here's the relevant excerpt from nLab:

Corecursion exploits the existence of a morphism from a coalgebra for an endofunctor to a terminal coalgebra for the same endofunctor to define an operation.

I have a vague idea of what corecursion is in the programming sense (in the way Wikipedia describes it).

I understand some basics of category theory at a level that alows me to understand the meaning of the sentences a monad is a monoid in the category of endofunctors and the tensor product $V\otimes U$ is an initial arrow among all bilinear maps from $V\times U$, but I still have a hard time deciphering the above.

That is, I understand all words there, but not how to interpret them.

I will rephrase the question about regular (coco) recursion, because I already have a better understanding there in the algorithmic sense, and because I hope that going to category language will be easier there, expecting that reversing the arrows afterwards will magically make me understand the dual, too.

In particular, I would like to understand:

  1. the role of the endofunctor $F$
  2. the role of the algebra $FX\to X$ (or the coalgebra $X\to FX$)
  3. the role of the initial (terminal) one among the latter
  4. how all of these corespond to my current understanding of recursion (e.g. in the sense of trees or recurrent number sequences).

The nLab article has an example, but sadly I do not find it very illuminating. It states e.g. that $\bar{\mathbb N}$ is terminal for $X\to 1+X$ and that "+" is derived from some (to me, weirdly defined) function "add", but I do not understand where these facts came from, and even if I take them for granted, I still can't easily answer the 4 questions. I also expect to be able to apply the same reasoning for regular recursion and "recover" it from the category language, but this example doesn't help me with that. I also read through the two cited references and even though helpful, they didn't lead me to a clear-cut answer to 1-4.

Actually in the dual article for recursion, some insight is found in a paragraph therein that I overlooked when I first formulated this question. Still, it is not as complete, and since then I developed a clearer picture that I'd like to share in a self answer.

Al.G.
  • 1,802
  • You might find the nlab page coalgebra for an endofunctor a better place to start than the corecursion page. I think this is a good question and might post an answer later, since I had a lot of the same questions at first and it took a me while to get it all straightened out. – N. Virgo Jan 18 '25 at 20:47
  • I quickly fell into the rabbit hole, and yes, I've been on that page. Unfortunately it starts with A coalgebra over an endofunctor is like a coalgebra over a comonad, with a link to the "coalgebra over a comonad" page which I followed - only to be faced with ...formally dual to those for an algebra over a monad and a link to "algebras over a monad". And soon I forgot where I started from, haha... in the end I just closed the site and started reading cited papers in search for a more story-like explanation. I would love to read your input, too! – Al.G. Jan 18 '25 at 23:17
  • Yeah, nlab does that a lot, though I can say that once you've been down enough rabbit holes that sort of thing does eventually start to be helpful! – N. Virgo Jan 18 '25 at 23:59

2 Answers2

2

Regular recursion over naturals

The prototype for recursion (as taught in undergrad) are the natural numbers. Natural numbers consist of a zero and a suc operation, and are most general of this kind (initial):

$$1 \overset{0}{\longrightarrow} \mathbb N \overset{+1}{\longleftarrow}\mathbb N$$

With sets in mind, initial here means that

  1. the operations 0,+1 are "free" from any constraints and are as general as possible, e.g. 0+1 can't be equal to 0+1+1+1. This is guaranteed by the existence of an outgoing arrow into any other such diagram.
  2. the object N is minimal, i.e. it has no other elements than necessary to make 0 and +1 free in the above sense. This is guaranteed by the uniqueness of the mentioned arrow.

We can look at other (non initial) such diagrams easily. Putting $\textrm{Bool}(=2=\{0,1\})$ in place of $\mathbb N$, we get diagrams $$ 1 \overset{\textrm{base}}{\longrightarrow} \textrm{Bool} \overset{\textrm{step}}{\longleftarrow} \textrm{Bool} $$ defined by a choice for base and the function step.

Now initiality of the first diagram provides a unique arrow (a triplet of arrows, technically) to the second, that makes the resulting joint diagram commutative, and this is the arrow of interest. For example, we can recover the function $$\textrm{is-even}:\mathbb N \to \textrm{Bool}$$ by defining $\textrm{base}=\textrm{true}$ and $\textrm{step} = \textrm{NOT}$.

And this is how recursion on the naturals works from the categorical perspective.

  • The initial algebra $1+\mathbb N \to \mathbb N$ is the inductively defined data structure.
  • The "any other" algebra like $1+\textrm{Bool}\to \textrm{Bool}$ provides the individual steps for the recursion.
  • The unique morphism from the former to the latter is the resulting magically auto-defined recursively defined function.

So the initial natural number diagram shoots recursively defined functions to any other diagram shaped like it, at the moment they are defined.

Corecursion on extended naturals

In order to talk about the dual concept we can try to reverse the arrows above, but that gives an arrow $\mathbb N \to 1$, which is just the constant 1 and is useless. Instead, we first combine the two functions $0, (+1)$ into a single one (denoted $0+(+1)$) and rewrite the diagram into the equivalent form $$1+\mathbb N \xrightarrow{0+(+1)} \mathbb N.$$ This allows to reverse the arrow in a meaningful way now.

Now we abstract away the $\mathbb N$ and the $\textrm{Bool}$ from above and get a general $ 1+X \to X $ morphism. Reversing all arrows, we get

  • a general coalgebra $ X \to 1+X $ (interpreted as a partial function on X).
  • a terminal one among these, $ T\to T+1 $, where $T$ happens to be the extended natural numbers $\mathbb N \cup \{\infty\}$.
  • a unique arrow from any corecursive "step" $ X \to 1+X $ to the terminal one that makes the joint diagram commute.

In exactly the same way, this resulting incoming (to $\bar {\mathbb N}$) morphism is the corecursively defined function, built from the steps in the source coalgebra.

And this is how the categorical perspective captures the programmer's viewpoint that recursion deconstructs an inductive structure to produce something else, while corecursion constructs a (coinductive) structure from a given "generator" step (above we had an outgoing arrow to Bool vs an incoming arrow to $\bar { \mathbb N }$).
Remark: actually the duality is not as nicely illustrated by (co)naturals as it is by lists/streams. N. Virgo's answer below/above gives an awesome interpretation of streams that makes the construction nature of corecursion much more obvious.

General recursion and corecursion.

There is nothing too special about the (1+) in the expression $1+X$, so we can abstract it away to any functor $F:X\mapsto FX$.

In the natural numbers case, the functor $X\mapsto X+1$ meant that there is

  • a zero number (a base case) and a unary successor operation (a recursive step), or
  • a predecessor operation (a partial function) as a step, in the dual case.

In the general case, a functor $X \mapsto F X$ can describe any (co)recursion scheme one could define.

  • $X\mapsto 1 + X^2$ gives rise to (unlabeled) finite binary trees recursively, and to possibly infinite ones corecursively: see [FM] and [AMM]
  • [AMM] shows that $X\mapsto 1 + A\times X$ recursively gives rise to finite lists and corecursively - to possibly infinite streams over $A$.
  • [JA] shows that $X\mapsto X^\Sigma \times \textrm{Bool}$ gives (corecursively) rise to deterministic automata over an alphabet $\Sigma$.

So the role of the functor F is to encode the way in which future values depend on previous ones. It's the particular recursion's signature.

References

I got most of my insight in this answer while pondering over [FM] and playing with the is-even example I made up. I still believe it's worth to share my thought process here and spare someone a week (in my case) of rediscovering this themselves. With this understanding, papers become much easier to read, even ones about haskell recursion schemes (like this one).

nLab also has an article on the natural numbers object that proves some facts and puts the matters in an even more general setting.

Al.G.
  • 1,802
2

Because this answer will end up long, I'll restrict the scope to your questions 1-3. I wanted to explain more. (In particular I wanted to explain the '+' example from nlab, which should somewhat address question 4 - without that I'm only really talking about coinduction and not corecursion.) But I realised I'm covering too much for it to really be practical for the Stack Exchange format. I hope it's helpful even if it's not really complete.

A good example goes a long way, and with this topic it's helpful to choose one that's not quite the simplest possible. So let us consider the functor $F(X) = A\times X + 1$, for some given set $A$. (It will be helpful later on to think of $A$ as some alphabet of symbols.)

Just in case it's not clear what that means, the symbols $\times$ and $+$ refer to the product and coproduct in Set, which are the cartesian product and the disjoint union. So this functor takes as input a set $X$ and gives as output the set $\{(a,x) \mid a\in A, x\in X\}\cup\{\bot\}$. It's a functor, so it has to act on morphisms as well as objects, but I won't discuss that - its behaviour on morphisms comes from the definition of product and coproduct.

Now, a coalgebra of $F$ consists of two things: a given set $S$ and a function $f:S\to F(S)$, or equivalently $f:S\to A\times S + 1$. A helpful way to think about this is as a kind of machine. The set $S$ is its state space, and the function $f$ determines its dynamics. On each time step it starts in some state $s$. If $f(s) = (a,s')$ then we think of the machine as outputting the symbol $a$ and changing to a new state $s'$. Otherwise, if the machine's output is $\bot$, we say the machine has halted, in which case there is no output and no next state.

So if we're given some initial state $s_0$ then the machine will output a sequence of symbols from $A$, which might at some point halt, but it's also possible for it never to halt and just keep outputting symbols for ever. Thus we can say that each state of the machine has a corresponding behaviour, which is the sequence of $A$'s (possibly finite, possibly infinite) that it outputs. This notion of behaviour will be made formal shortly.

It's worth stopping at this point to answer your questions 1 and 2. The endofunctor $F$ determines the kind of thing a behaviour can be. For example, if we changed $F$ to just $F(X) = A\times X$ instead of $A\times X + 1$ then we would take away the possibility that the machine could halt, so that then the behaviour of a state would always be an infinite stream of $A$'s and not a possibly-finite one. By an appropriate choice of $F$ you can make machines that take inputs or behave stochastically among other possibilities. On the other hand, the coalgebra $f:S\to F(S)$ determines the behaviour of a specific machine - it specifies what this specific machine will do, as a function of its state.

Now let's address question 3, the role of the terminal coalgebra. I mentioned that in our example each state has a "behaviour" consisting of the possibly-infinite string of elements of $A$ that it outputs. Let us then define a coalgebra whose state space is $A^*\cup A^\mathbb{N}$, i.e. the set of all finite and infinite strings of elements of $A$. To define a coalgebra we need a function of type $A^*\cup A^\mathbb{N} \to A \times (A^*\cup A^\mathbb{N}) + 1$. I will define this function as follows:

  • if the input is the empty list, return $\bot$
  • otherwise the input $l$ is a possibly-infinite list; in this case return the pair $(\text{head}(l),\text{tail}(l))$.

I claim that this is a coalgebra of $F$, and moreover that it is the final one. It is very worthwhile to go through the exercise of showing this. (First figure out how $F$ acts on morphisms, then figure out what a coalgebra homomorphism is in this example, and then show that for each coalgebra $(S,f:S\to F(S))$ there is a unique homomorphism into the machine I just described, which takes the form of mapping each state to its 'behaviour'.)

So in this example, the final coalgebra is a machine whose states are 'behaviours,' and the unique map into the final coalgebra sends each state of a machine to its behaviour. Although the terms "machine" and "behaviour" won't always be the right ones, every example will have this general flavour.

There is more to say, but it's at this point I realised I'm writing more of an introduction to the topic than anything else. For more I would highly recommend the introduction to Bart Jacobs' book Introduction to coalgebra.

N. Virgo
  • 7,904
  • I really liked your answer because it made me realize something that was not as obvious to me so far from the (co)naturals. By interpreting streams as machines producing state and result (akin to the State monad, by the way), you make very clear the duality between the "downward" deconstructing recursion over Lists vs the "upwards" construction process in streams. Thanks! – Al.G. Jan 19 '25 at 20:03
  • @Al.G. exactly, an element of an initial algebra is defined in terms of what you can "add to it" whereas an element of a final coalgebra is defined in terms of what you can "take from it". The initial algebra of $A\times({-})+1$ is the set of all finite strings, because you have to build them up starting from the base case, whereas the final coalgebra is the set of all possibly-infinite strings as in the answer. – N. Virgo Jan 19 '25 at 20:37