How to prove equations using diagrams, part 1
Initial functors
This post is cross-posted at the Topos Institute blog.
An e-graph, short for “equality graph,” is a data structure that maintains a congruence relation on expression trees: an equivalence relation stable under forming new expressions. First devised by Nelson and Oppen in 1980 (Nelson 1980; Nelson and Oppen 1980), e-graphs received a surge of new attention when Willsey et al demonstrated, via their software package egg
, that e-graphs combined with equality saturation can be a fast, powerful, and adaptable tool for equational reasoning (Willsey et al. 2021). In my own work on CatColab, I’ve begun using egg
’s successor, egglog
(Zhang et al. 2023), to reason about finitely presented categories.
Standard presentations, beginning with Nelson’s PhD thesis (Nelson 1980), emphasize the data structures and algorithms needed to implement e-graphs, such as the union-find data structure. It’s crucial that work in computer algebra ultimately touch the ground in this way. Still, I can’t help but wonder what, abstracted from the implementation details, is really going on with e-graphs. The diagrammatic and visual nature of e-graphs, so apparent in the interactive egglog
demo, will suggest to any category theorist that categorical ideas may be implicitly at work.
Two groups have recently proposed categorical semantics for e-graphs (Biondo, Castelnovo, and Gadducci 2025; Tiurin et al. 2025). Without going into specifics, both define an “e-hypergraph” to be a labeled hypergraph equipped with extra structure that, directly or indirectly, specifies an equivalence relation. Their starting point is to say what an e-graph is. Though I’ll eventually propose my own definition of an “e-diagram,” I will start by considering how e-graphs are manipulated, with the ultimate aim being to give an categorical and operational semantics for e-graph computation. The key concept will be that of an initial functor.
In this post, I’ll introduce the main ideas for categories without further structure. In comparison with e-graphs, this will be quite restrictive—it will correspond to allowing only unary function symbols—but it has the advantage of bringing us to the point with a minimum of setup. In future posts, we’ll generalize to categories with finite products and recover the standard setting of e-graphs.
Representing equations using diagrams
If you’ve encountered any abstract algebra, you’ve likely seen how category theorists represent equations between morphisms by drawing commutative diagrams. For example, saying that a square of morphisms
in some category commutes means that equation
Let’s recall the formal definitions, which are standard if not without fine distinctions.
Definition 1 A diagram in a category
- the diagram is free if its shape is a free category, i.e.,
is freely generated by a (possibly infinite) graph ; - the diagram is finitely presented if its shape is a finitely presented category;
- the diagram commutes, or is commutative, if for every pair of morphisms in
with the same domain and codomain, their images in are equal.
Note that a diagram may well be both free and commutative: freeness is a property solely of the diagram’s shape, whereas commutativity also crucially concerns the diagram’s image in the target category.
In practice, we work with diagrams that are free and finitely presented. That means that the diagram’s shape is freely generated by a finite graph, a data structure easily implemented on a computer. When doing computer algebra, the target category will itself be finitely presented by generators and relations. Initially, the only equations that we “know” are the ones explicitly given as relations. Our aim is to derive further equations in the target category by manipulating diagrams in it.
But wait: if a diagram in a finitely presented category can be described using only graph-theoretic concepts, familiar to any computer scientist, what is the point of introducing categories and functors? We’ll see that category theory is indispensable for concisely describing the valid manipulations of diagrams. These manipulations take the form of morphisms between diagrams, which are in the first instance functors.
Initial functors
Reasoning with diagrams, or “diagram chasing,”1 is the art of transforming a given diagram, say
Let’s consider the obvious moves we could make. Given a diagram
Without assuming more, neither contemplated rule of inference is valid in the sense (to be made precise) of exactly preserving solutions to the equations presented by diagrams. For that, we need
Definition
Theorem 1 (Initial functors) A functor
- Pullback along
preserves limits: if is a category and is a diagram in , then the canonical morphism between limits is an isomorphism, whenever the limits in question exist. - Pullback along
preserves limits of sets: if is a diagram of sets, then the canonical function between limits is a bijection. - Slices relative to
are connected: for any object , the comma category is non-empty and connected.2
For a proof of (the dual of) this equivalence, see (Riehl 2014, Lemma 8.3.4).
With a good intuition for limits, the limit characterization of initiality is useful because you can often see at a glance whether a given functor is initial just by imagining what will happen to a limit of sets. On the other hand, the slice characterization is useful in being concrete and syntactical. Recall that, given a functor
- as objects, an object
together with a morphism in ; - as morphisms
, morphisms in making the triangle commutes:
In the typical case that the categories involved are free and finitely generated, proving initiality—checking that each comma category
Interpretation
Why are initial functors relevant to equational reasoning? In general, a limit of a diagram can be thought of as an object representing all possible solutions to the equations postulated by the diagram. This interpretation is clear in the ur-category
are satisfied (Leinster 2014, Example 5.1.22). So, as pullback along an initial functor preserves limits, the initial functors are intuitively those that preserve the “solution set” of the system of equations presented by a diagram.
However, our interest lies in diagrams
First, we can always consider set-theoretic interpretations of the presentation, namely functors
Taking a little extra care, we can go further to deduce equations that hold in an arbitrary category
Examples and calculations
The remainder of the post will illustrate the concept of initial functor through commonly occurring examples and explicit calculations. The aim is to suggest how initial functors can be deployed for mechanical reasoning with diagrams, deferring to next time a further theoretical development.
Motifs among initial functors
Let’s begin with a few minimal examples of initial functors, acting as motifs or patterns. Each such example will give rise to a rule of inference for diagrammatic reasoning.
The target category
Operation introduction: for any morphism
in , the map of diagramsis initial.
More generally, for any path
in , the map of diagramsis initial.
Congruence reduction: for any morphism
in , the map of diagrams,4sending both
and in the domain shape to in the codomain shape, is initial.Subsuming both operation introduction and congruence reduction, for any morphism
in and number , the map of diagrams,sending all of
to , is initial.Equation introduction: suppose that the equation
holds in , as depicted earlier. Then both maps in the span of free diagramsare initial. More generally, this works for any path equation in
. As another example, we can introduce a nullary path equation, say , as:
Later, we will formalize not just the rules but how to apply them, using ideas from category-theoretic rewriting. For the moment, we take for granted that when applying the rules in a larger context, the resulting functors are still initial. Or, you can check initiality by inspection in each application.
Pasting commutative squares
We’re ready to do our first calculation, proving the pasting law for commutative squares: that two commutative squares with a common edge can be juxtaposed to produce a commutative rectangle. The notation makes this seem obviously true—that’s what makes it good notation!—but it still needs a proof. We’ll present the proof in a highly mechanical style, where each diagram in the sequence is related to the previous one by an initial functor or, when introducing an equation, by a span of initial functors.
Assume: We are given the two commutative squares
By this we mean, the target category
Construct: Start with the singleton diagram on
Recognize the path
Apply congruence reduction to the occurrences of
Now recognize the path
We’re already done, but we might as well tidy up by congruence reducing the occurrences of
Conclude: The outer rectangle commutes, since the limit of the diagram is still
A monoid presentation
Consider a monoid with an element
That is, the target category
Start with the singleton diagram
We neglect to label the arrows since they are all over the generating morphism
Next:
Congruence reducing three more times gives the sequence:
We could stop here, but we might as well reduce twice more, yielding the diagram that is the self-loop over
since the limit of the diagram is the one we started with, namely the unique object
Discussion
Category theory is often said to be abstract, even “abstract nonsense,” but it is the interplay between abstract and concrete that gives category theory its power. Initial functors, with their complementary intrinsic and syntactical descriptions, exemplify this interplay.
We’ve begun to explore how initial functors are the basis for an operational semantics for e-graphs. Rather than defining the individual steps and then chaining them together, as in the usual “small-step” operational semantics, we define the valid transformations invariantly via initial functors and then look for useful specializations. Beyond its conceptual content, this flexibility matters because practical systems tend to batch reductions together to improve efficiency. For us, that will often just amount to composing initial functors.
We haven’t yet made a direct connection with e-graphs, though there have been many hints. Another hint is that our final example is lifted directly from Nelson and Oppen’s early work (Nelson 1980; Nelson and Oppen 1980), where it is a running example. In future posts, we’ll tighten up the connection with e-graphs. We’ll also explain how the formalism generalizes to categories with finite products, the categorical counterpart to algebraic theories.
References
Footnotes
In this series, we work first with bare categories and later generalize to cartesian categories. Diagram chasing in its most traditional form takes place in additive and abelian categories, a different direction of generalization. It would be fun to try to extend our methods to the additive setting, beginning with categories enriched in commutative monoids or abelian groups.↩︎
A category is connected if any two of its objects can be connected by a zig-zag of morphisms, i.e., there is an undirected path between them.↩︎
That is not to say that exhibiting a proof implied by completeness is easy or convenient. As I understand it, proof extraction is a topic of active work in the e-graph community.↩︎
When drawing a diagram
, the “type-theoretic” notation names an object in the indexing category that is mapped to the object in the target category . Thinking of the diagram as a term graph, the object is indeed a “term of type .”↩︎