Monads and Algebras/Algebras for an Endofunctor and Recursion

Lesson 7.42,211 words

Algebras for an Endofunctor and Recursion

Dropping the monad laws leaves algebras for a bare endofunctor, whose initial objects are the least fixed points of the functor by Lambek's lemma. The natural numbers, lists, and trees are initial algebras; the unique map out of an initial algebra is the fold of functional programming; and the Smyth–Plotkin fixed-point technique builds Scott domains the same way.

╌╌╌╌

A monad algebra is a structure map constrained by two laws tied to the monad's unit and multiplication. Delete the laws (and the unit and multiplication with them), and what remains is an algebra for an endofunctor: any object with any map . The definition looks weak, but its initial objects are the least fixed points of , and the least fixed points of suitable functors are the natural numbers, lists, and trees of programming. Recursion over a datatype is one universal property applied repeatedly.1

Fixed points of a functor

For a monotone function on a poset, a fixed point is an element with , and among the fixed points there may be a least one. Barr & Wells open Chapter 14 by asking what the analogue is for a functor , and the obvious candidate fails.2

Consider with (disjoint union with a one-point set), the functor whose fixed point ought to be the natural numbers. Demanding a set with on the nose is hopeless: the equality would depend on the arbitrary choice of how disjoint unions are implemented. Weakening to isomorphism fixes that but breaks leastness: every infinite set satisfies , and infinite sets have proper infinite subsets also satisfying it, so nothing is least in any containment sense. The resolution is to change the structure carrying the question.

Least reads a category as a generalized poset: in a poset-as-category, an initial object is a bottom element. A lemma justifies the definition by guaranteeing that initial algebras are fixed points.

The proof of Lambek's lemma. The top row is the composite , an endomorphism of the initial algebra, so it is the identity; the bottom row is its image . The right-hand square is the homomorphism square for , giving .

Initial algebras also inherit the two other virtues of initial objects: they have no proper subobjects (the categorical residue of least), and they are unique up to unique isomorphism.5

The natural numbers as an initial algebra

Return to . The natural numbers carry the -algebra structure

where picks out zero on the left summand and is the successor on the right. This algebra is initial: given any algebra , decompose into a chosen element and a map ; the unique homomorphism is defined by induction as

Commuting with the structure maps means this pair of equations, so is a homomorphism and is the only one.6 Definition by recursion — start at , keep applying — is the universal property of an initial algebra, stated in .

Initiality of the naturals ( in the figure stands for ): any set with a point and an endomap is an algebra for , and defined by recursion is the unique homomorphism from .

Folds

In functional programming the unique map out of an initial algebra is called a fold (or catamorphism). The data of an arbitrary algebra (one clause per summand of ) is the body of a structural recursion, and initiality says the recursion has exactly one solution. For the naturals, fold x0 t is the function iterating from ; for lists and trees (below), it is the usual fold over the datatype. Uniqueness is as useful as existence: two functions agreeing on the algebra clauses are equal, which is the fusion-style reasoning used to prove program transformations correct.

Algorithm:FoldF(f)\textsc{Fold}_F(f) — the unique homomorphism from the initial FF-algebra (A,a)(A, a) to an algebra f:F(S)Sf : F(S) \to S
  1. 1
    given an element of AA, expose its structure via a1:AF(A)a^{-1} : A \to F(A)
  2. 2
    for each constructor cell in the exposed structure do
  3. 3
    recursively apply FoldF(f)\textsc{Fold}_F(f) to every child of type AA
  4. 4
    replace the constructor by the corresponding clause of ff
  5. 5
    return the resulting element of SS

The step expose via is licensed precisely by Lambek's lemma: the structure map of the initial algebra is invertible, so every element decomposes uniquely into a constructor applied to children.

Constructing initial algebras: the colimit chain

Existence of initial algebras needs a hypothesis on the functor. In , the right condition is that the functor is finitary: every element of comes from for some finite subset — the functor's behavior is determined by what it does on finite sets.7 The functor is finitary; the powerset functor is not, since an infinite subset of is not contained in any finite subset.

The chain starts from nothing and applies the functor repeatedly; the colimit is the union if the maps are inclusions. Finitarity lets commute with this colimit, so that is the colimit of the shifted chain, which is canonically again. For the chain is

(with the -fold sum and the maps the sum injections), and the colimit is with successor: the initial algebra built floor by floor, the -th stage containing the numbers writable with at most constructor applications.

The initial-algebra chain for . Start from the empty set, apply at each step (the sum injections), and take the colimit: stage holds the numbers needing at most constructor applications, and the colimit is (drawn for ) with successor.

Inductive datatypes

The same construction interprets datatype declarations. A polynomial endofunctor is one built from constant functors and the identity by finite products and sums9; each summand is a constructor, each factor a field.

DeclarationEndofunctor Initial algebra
natural numbers with zero and successor
lists over with nil and cons
binary trees, data at nodesfinite binary trees
nonempty trees over binary leaf trees

Barr & Wells work in the setting of a locos (a coherent, locally recursive category), where every polynomial endofunctor admits free algebras: the underlying functor from back to has a left adjoint.10 The free algebra on the terminal object is the initial algebra, and for the tree functor its structure map

reads as the two constructors: a leaf datum, or a datum with two subtrees. This is initial-algebra semantics: the meaning of a recursive datatype is the initial algebra of its signature functor.

Recursive categories and list objects

Recursion can also be internalized: made available inside a category rather than stated about it. For an object of a category with finite products, the endofunctor has as algebras the -actions, forming a category .

Write , the free -action on . Its universal property: given any and , there is a unique compatible with both: a seed and a step determine a unique recursive map. In , : a list of 's waiting to be consumed, together with a seed . Two specializations matter:

  • is the list object on , available in any recursive category, with well-defined head and tail operations.
  • is a natural numbers object: lists over a one-point alphabet are tallies.

The definition strengthens in stages: a locally recursive category has uniformly and all slices recursive, and a locos is additionally coherent. The stronger axioms yield a computability theorem: in a locos generated by decidable objects, every object is decidable, so the constructions of a locos never manufacture undecidable equality from decidable ingredients.12

State machines as algebras

An algebra for is also exactly a deterministic transition system: takes an input letter and a state to a next state. Barr & Wells package this as categorical dynamics: an endofunctor whose algebra category has free objects is an input process, an -algebra is an -dynamic, and a machine is a dynamic with an initial state arrow and an output arrow .13 The free-monoid action extends to strings, recovering the classical automaton; reachability becomes factors through no proper subalgebra. Choosing a different functor (say ) gives machine notions with no classical counterpart.

Terminal coalgebras

Dualizing once more: a coalgebra for is a map , and the terminal coalgebra is the greatest fixed point of (Lambek's lemma dualizes, so its structure map is again an isomorphism). Where the initial algebra of is finite lists, the terminal coalgebra of is infinite streams over : a stream is observed by its head and tail, , and the unique map into the terminal coalgebra assigns to every state of any transition system the stream of outputs it generates. Folds consume finite structure; unfolds produce possibly-infinite structure. The pairing — datatypes as initial algebras, codatatypes as terminal coalgebras — organizes recursion and corecursion into one picture.

Two fixed points of the structure functor: the initial algebra (a map , the least solution) builds data from constructors; the terminal coalgebra (a map , the greatest solution) observes possibly-unbounded behavior via destructors.

Scott domains by the same technique

Fixed points of functors also produce a model of the untyped lambda calculus, an object in a cartesian closed category with

so that every element of is simultaneously a function and can be applied to itself. In this is impossible for cardinality reasons (except for ). Worse, the assignment is not even a functor: an arrow induces maps and , but nothing between and — the exponential is contravariant in one variable and covariant in the other.14

The Smyth–Plotkin technique repairs functoriality by enriching. A Smyth–Plotkin category is a cartesian closed category whose hom-sets are posets, with composition and the exponential monotone, and with limits and colimits along countable chains that are order-isomorphisms on homs.15 From such a build the category : same objects, but an arrow is an adjoint pair with and — each arrow carries its own approximate inverse, and either component determines the other. A pair with is a retract.

On the exponential becomes an honest covariant functor: sends the pair to the pair , using in the contravariant slot and in the covariant one. The main theorem then supplies the fixed point.

The construction of the model runs the initial-algebra chain in this enriched setting. Start with an object with and a retract pair ; iterate :

Let be the colimit. Because converts the colimit chain into a limit chain and back, preserves the colimit, so — that is, , and a short calculation upgrades this to

The concrete instance takes to be the category of -complete partial orders and countable-sup-preserving maps, which is cartesian closed. For , take a countable power of any nontrivial CPO with a bottom element (a countable power satisfies by splitting a sequence into its even- and odd-indexed halves); the retract pair sends an element to the constant function at it, and a function to its value at . The result is a Scott domain with : a genuine model of self-application, constructed as a fixed point of a functor by the same chain that built the natural numbers.17

The same fixed-point chain, enriched over posets, builds the Scott domains on which the cartesian closed categories interpret the untyped lambda calculus.

Footnotes

  1. Barr & Wells, Category Theory for Computing Science, Ch. 14 introduction — the chapter's topics centered around the idea of the algebra for a functor: fixed points, triples, and the Smyth–Plotkin construction of Scott domains.
  2. Barr & Wells, §14.1.2 — why the naive definitions fail for : strict fixedness depends on the implementation of sums, and fixedness up to isomorphism admits every infinite set with no least one.
  3. Barr & Wells, §14.1.3–14.1.5 — -algebras, the category , fixed points as algebras whose structure map is an isomorphism, and least fixed points as initial objects.
  4. Barr & Wells, §14.1.7, Theorem (Lambek 1970) — if is initial in then is an isomorphism; the displayed two-square proof.
  5. Barr & Wells, §14.1.8 — initial algebras have no proper subobjects, and are determined uniquely up to unique isomorphism.
  6. Barr & Wells, §14.1.8 and Exercise 1 — is an initial algebra, with the unique homomorphism defined by , .
  7. Barr & Wells, §14.1.9 — conditions FF-1 and FF-2 defining finitary functors on ; everything is determined by what happens on finite subsets.
  8. Barr & Wells, §14.1.11, Theorem — the colimit of the chain carries an initial -algebra structure when is finitary; §14.1.12 computes the chain for .
  9. Barr & Wells, §14.2.7 — polynomial endofunctors: the least class containing constants and the identity, closed under finite products and sums.
  10. Barr & Wells, §14.2.6–14.2.8 — locally recursive categories and locoses; Proposition: in a locos every polynomial functor's algebra category has free objects; the binary-tree example .
  11. Barr & Wells, §14.2.2–14.2.4 — -actions, recursive categories, the universal property of , and in ; Exercise 2: is a natural numbers object.
  12. Barr & Wells, §14.2.9, Theorem (Cockett) — in a locos with a generating collection of decidable objects, every object is decidable.
  13. Barr & Wells, §14.2.10–14.2.11 — input processes, -dynamics, dynamorphisms, machines, and the recovery of classical state-transition machines from the functor on .
  14. Barr & Wells, §14.5.1 — the difficulty: is not a functor, and the Smyth–Plotkin trick resolves it.
  15. Barr & Wells, §14.5.2–14.5.4 — Smyth–Plotkin categories SP-1 through SP-7, the categories and of adjoint pairs, retracts and coretracts.
  16. Barr & Wells, §14.5.5, Theorem — colimits of countable retract chains in are computed in , with the dual statement for .
  17. Barr & Wells, §14.5.6–14.5.7 — the iteration with , its colimit satisfying , realized in the cartesian closed category of -CPOs.

╌╌ END ╌╌