arXiv:2611.05924math.CTNov 2026

Manuscript Offprint

arXiv:2611.05924

How to prove equations using diagrams: A categorical and operational semantics for e-graphs

Evan Patterson

Abstract

E-graphs are a powerful algorithmic technique for automated equational reasoning but have resisted a conceptual understanding independent of issues of presentation and implementation. We propose an operational semantics for e-graphs based on the fundamental category-theoretic concepts of diagrams, limits, and initial functors. We illustrate the formalism through numerous example calculations and establish its soundness and completeness. Technical prerequisites are kept to a minimum in the main text; less standard elements of the mathematical theory are developed in two appendices.

1 Introduction

Mathematical expressions and computer programs are most directly represented as the labeled trees known as terms. However, computer scientists have long found reasons to encode expressions in more general mathematical and data structures. Generalizing from trees to directed acyclic graphs (DAGs), a term DAG enables common subexpressions to be represented by the same vertices, making more efficient use of space [PW78]. Generalizing further to equip a term DAG with an equivalence relation, an equality graph, or e-graph for short, efficiently encodes a family of terms along with an equivalence relation on the terms [Nel80; NO80]. Computing the equivalence relation's congruence closure under term formation then leads to an algorithm to decide quantifier-free formulas in the theory of equality with uninterpreted function symbols. The ideas behind e-graphs, dating back to the late 1970s, received a surge of new attention when their combination with equality saturation [Tat+09] was shown to be a fast and flexible tool for equational reasoning [Wil+21].

Standard expositions, early and recent, emphasize the data structures and algorithms needed to implement e-graphs, such as the union-find [Tar75] and hash-cons data structures and congruence closure algorithms. Data structures and algorithms are indispensable to building practical technology based on e-graphs. Still, they do not exhaust the ways to understand e-graphs. What might be called a “theory of e-graphs”—a mathematical and conceptual understanding of e-graphs, independent of any algorithmic or implementation details—has been elusive. Besides its intrinsic interest, a good theory can be expected to eventually lead to further practical advances.

In this paper, we propose a mathematical semantics for e-graphs. The semantics is, in the first place, a categorical semantics, meaning that category-theoretic concepts—categories, functors, and so forth—are used to give mathematical content to syntactical objects like terms or, in this case, term graphs. Characteristic of categorical semantics is its invariance to presentation: not depending on how the objects in question are presented or implemented. In this spirit, we deliberately avoid proposing a single definition of “e-graph.” Rather, we describe a means of representing equations using category-theoretic diagrams that is conceptually compatible with how e-graphs encode equations, but makes no commitments about presentation.

In addition to being categorical, our semantics is primarily operational. That might seem surprising since categorical semantics are often exclusively denotational, beginning with Lawvere's reconstruction of algebraic theories as categories with finite products [Law63; Cro93; ARV11]. Indeed, we will use algebraic theories qua categories in Section 4. Yet our guiding principle throughout is to analyze not what e-graphs are as objects but what their valid transformations are as morphisms. This is an operational semantics inasmuch as it gives mathematical content to operations performed on e-graphs. The operational aspect, like the denotational one, is invariant: the set of allowed operations is defined "all at once" and without reference to a presentation, rather than being generated by a minimal set of atomic steps as in a small-step operational semantics. Our operational semantics also does not aim at reduction to a final value (or set of values). This is in contrast to many operational semantics for programming languages, but it is in keeping with software systems like egg [Wil+21] and egglog [Zha+23] that are not fixed procedures but allow flexible schedules of rules to be run against an e-graph.

The thesis of this paper is that a categorical and operational semantics for e-graphs can be found in the concept of an initial functor. Consider the term graphs below, which for the moment we take to be labeled graphs.

Figure

Both term graphs encode the same two terms, g(f(x))g(f(x)) and h(f(x))h(f(x)). The map between them, collapsing both edges labeled ff on the left to the single edge labeled ff on the right, is a homomorphism of labeled graphs. It is an example of a congruence reduction, which is, in turn, one type of valid transformation of term graphs, where "valid" means not changing the terms or equations implicit in the graph. It is then natural to ask: what are all the valid transformations? To our knowledge, an elegant answer to this question cannot be given using only the language of graphs and graph homomorphisms. If, instead, we see the map as a functor (between categories freely generated by graphs), we can recognize an example of a known class of functors, the initial functors. As we will explain, initial functors are exactly those that preserve the system of equations encoded by a diagram; in category-theoretic language, restricting the shape of a diagram along an initial functor preserves the diagram's limit, whenever it exists.

The operational semantics for e-graphs thus rests on an analysis of when a functor is guaranteed to maintain, in a suitable sense, the universal property defining a limit. Universal properties are an essential—arguably the essential—idea in category theory. It is for this reason that category theory is the natural language for a theory of e-graphs.

Outline Section 2 lays the groundwork by explaining how to represent systems of equations using category-theoretic diagrams. Section 3 justifies this representation by introducing morphisms between diagrams and singling out the initial ones as those preserving the systems of equations. Rules for, and examples of, calculating with initial morphisms are given. So Sections 2 and 3 concern representation and reasoning, respectively. They are restricted to categories with no extra structure, which in logical terms correspond to algebraic theories with only unary operations. This setting is very limited practically, but it suffices to illustrate the main ideas. Section 4 extends the formalism to categories with finite products, corresponding to the classical setting of algebraic theories (with multiary operations). Finally, Section 5 offers a brief outlook on future directions. Two technical appendices develop the background mathematics.

Background We assume familiarity with the basic concepts of category theory, particularly categories, functors, diagrams, and limits. Further concepts not covered by most textbooks, namely initial functors and comprehensive factorization, are reviewed in Appendix A. As for computer science, readers familiar with term graphs or e-graphs will find connections with those concepts, but no prior knowledge of them is needed to make sense of this paper.

Related work Two groups have recently proposed category-theoretic accounts of e-graphs [BCG25; Tiu+25]. 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. Neither representation allows taking initial functors to be morphisms of e-graphs, the main idea and novelty of this paper.

Motivated by “Tonti diagrams” in computational physics, the author and collaborators have previously used category-theoretic diagrams to represent systems of equations—here, differential equations—and initial functors to formalize weak equivalence of diagrams [Pat+23; Car+24]. The underlying mathematics dates back to the early 1970s, with key results due to Paré [Par73] and Street–Walters [SW73]. The novelty of this work is to recognize the connection between initial functors and the operational aspect of e-graphs.

References (64)