In computer science, corecursion is a type of operation that is dual to (structural) recursion. Whereas recursion consumes a data structure by first handling the topmost layer before descending into its inner parts, corecursion produces a data structure by first defining the topmost layer before defining its inner parts. Corecursion is a particularly important in total languages, as it allows encoding potentially non-terminating computations in a context where every function must terminate. It is supported by theorem provers Agda and Rocq. Both corecursion and recursion can be thought of as operating on trees, which include data structures like lists and streams as special cases. Since recursion must terminate, it only works on trees that are well-founded, i.e. not infinitely deep, which are called data or initial data types; on the other hand, corecursion produces codata or final data types, which includes infinitely deep trees. Codata cannot be represented in memory directly, so is often implemented using self-referential data structures or lazy evaluation.
Data and codata In total programming languages, the natural numbers may be defined as follows (using Haskell syntax):
This states that every natural number is either zero or the successor of an existing natural number. For example, the number one is represented as Succ Zero, two as Succ (Succ Zero), three as Succ (Succ (Succ Zero)) and so on. If we interpret the above declaration in an inductive way, then all natural numbers are generated like this, and we get our familiar set of natural numbers. Importantly, we can do recursion on this set: for example, we can use it to make a list that repeats a value n times:
Note that the use of repeat x n is crucial—we could not have written repeat x (Succ n), because that is the function we are trying to define, and therefore it would loop forever. More specifically, the input must get smaller—or rather deeper, if seen as a data structure—each time we recurse. The declaration may also be interpreted in a coinductive way, which may be denoted as:
CoNat has all the natural numbers that Nat has automatically. But because codata can be infinitely deep, it has an extra term Succ (Succ (Succ (Succ …))) that continues on indefinitely—often denoted ∞. This is unique to the conatural numbers, so it can only be constructed using corecursion:
While recursion requires that the input of the recursive call must get smaller each time, corecursion requires that the output of the recursive call must get larger each time. This is why we must write Succ infinity; just infinity = infinity would be disallowed as it loops forever. To illustrate the duality between recursion and corecursion, we can encapsulate them in two functions, rec and corec. These functions' existence, together with the regular two constructors of Nat and CoNat, uniquely identify their respective types, and therefore allow rewriting all forms of recursion and corecursion in terms of them.
Conceptually, CoNat can be thought of as a state machine, where c is a type encapsulating the "current state". The function c -> Maybe c is a state transition function that either results in termination with Zero or continuation with Succ. We can rewrite the examples above using the functions:
A more complex example of codata is that of binary trees, where each node is either a leaf node, holding some data, or is a branch node that has exactly two children:
This example has many more infinitely deep terms that can only be constructed by corecursion. For example, it can be infinitely deep only on the left-hand side, or on both sides, or alternating left and right:
Again, corecursion on binary trees has a more general form based on a "state machine"-like constructor:
M-types In a dependently-typed setting, codata can be encoded using M-types, which are dual to W-types. Given a type A and an A-indexed family of types B, one can form the M-type 𝖬a:AB(a), representing the type of trees whose nodes are labelled with elements of A and who child nodes are indexed by the set B(a). In pseudocode, M-types (and their corecursion principle) may be defined as:As an example, the natural and conatural numbers may be constructed as W- and M-types of the same function:M-types can be proven to exist in many elementary topoi, and their existence follows from the existence of W-types.
Mathematical description The above section showed that there is an intimate link between natural and conatural numbers and Maybe c, and similarly between binary trees and Either a (c, c). This link is made precise by showing that Nat is in bijection with Maybe Nat, and CoNat with Maybe CoNat; explicitly, this bijection associates Zero with Nothing and Succ n with Just n. In other words, Nat and CoNat are fixed points (up to isomorphism) of the map X↦X+1. The difference between data and codata is that data is the least such fixed point, while codata is the greatest. In this way, recursion can be summarized as the statement that "every element of the type was generated only by the given constructors (Zero and Succ in the case of Nat/CoNat)", while corecursion states that "every value that can be analysed using the constructors as cases exists". From a category-theoretic perspective, CoNat can be precisely defined as the final coalgebra of the endofunctorX↦X+1. Unfolding this definition, we have that:
… excerpt ends here. Continue reading the full article.
