Semantic Foundations of Equality Saturation
Abstract
Equality saturation is an emerging technique for program and query optimization developed in the programming language community. It performs term rewriting over an E-graph, a data structure that compactly represents a program space. Despite its popularity, the theory of equality saturation lags behind the practice. In this paper, we define a fixpoint semantics of equality saturation based on tree automata and uncover deep connections between equality saturation and the chase. We characterize the class of chase sequences that correspond to equality saturation. We study the complexities of terminations of equality saturation in three cases: single-instance, all-term-instance, and all-E-graph-instance. Finally, we define a syntactic criterion based on acyclicity that implies equality saturation termination.
Keywords and phrases:
the chase, equality saturation, term rewriting, tree automata, query optimizationCopyright and License:
![[Uncaptioned image]](x1.png)
2012 ACM Subject Classification:
Theory of computation Equational logic and rewriting ; Theory of computation Rewrite systemsFunding:
This work is supported by NSF IIS 2314527 and NSF SHF 2312195, and is conducted while some of the authors participated in the Simons Program on Logic and Algorithms in Databases and AI.Editors:
Sudeepa Roy and Ahmet KaraSeries and Publisher:

1 Introduction
Given a set of identities between terms, the word problem asks whether the identities imply two ground terms are equivalent, i.e. . This fundamental problem has applications including automated theorem proving, program verification, and query equivalence checking. In his Ph.D. thesis, Nelson [22] introduced a data structured called E-graph for efficiently answering the word problem. At the core, an E-graph is a compact representation of an equivalence relation over a possibly infinite set of ground terms. During the 2000s, researchers applied E-graphs to program optimization [14, 28]. The compiler populates an E-graph with many equivalent programs, using axiomatic rewrites, then extracts the best program from the equivalent ones. In particular, Tate et.al. [28] coined the term equality saturation (EqSat) and gave a procedural description of the algorithm. In 2021, Willsey et al. [32] proposed egg, which introduced important algorithmic improvements and made EqSat practical. Since 2021, EqSat has been applied to a wide range of topics in domain-specific program optimization, including floating-point computation [24] computational fabrication [20], machine learning systems [33], and hardware design [29, 4]. There is also a growing interest in using EqSat for query optimization in data management. For example, EqSat is used to optimize queries in OLAP [6], linear algebra [30], tensor algebra [25], and Datalog [31].
The equality saturation procedure consists of repeatedly selecting an identity from the given set, matching the term with the E-graph, then adding the term to the E-graph, if it wasn’t already there. Equality saturation terminates when no new terms can be added. There are striking connections between equality saturation and database concepts. Zhang et al. [35] observed that the matching step is the same as conjunctive query evaluation, and described significant speedups in egg by using a Worst Case Optimal Join algorithm [23] for matching. A recent system, egglog [34], unified EqSat and Datalog to improve egg’s support for program optimization and program analysis.
In this paper we study another deep connection between equality saturation and the chase procedure for Tuple Generating Dependencies (TGDs) and Equality Generating Dependencies (EGDs) [8]. Our hope is that these results will help solve some of the open problems in equality saturation by using techniques and results for the chase procedure. Before describing our results we give a gentle introduction to EqSat and describe some of its open problems.
Example 1.
Consider a simple language with two binary operators and constant . We want to optimize the following term (the “8th power” of on ):
(1) |
We are given a single identity, , which says two terms and are equivalent, provided that are equivalent. Starting with the initial term , EqSat constructs an E-graph and grows it to represent all terms equivalent to . The literature defines an E-graph as a set of E-classes, where each E-class is a set of E-nodes, and each E-node is labeled with a function symbol and has a number of E-class children equal to the arity of the symbol. EqSat starts by constructing an E-graph representing , shown on the left in Figure 1. There are 4 E-classes, each consisting of one single E-node; the E-class represents precisely the term . Next, EqSat repeatedly applies the identity , by matching the left-hand side to the E-graph, then adding the right-hand side to the E-graph: we formalize this in Sec. 3. The resulting E-graph is on right of Figure 1. There are 4 E-classes, , each consisting of 1 or 2 E-nodes. For example, has two E-nodes, and represents two equivalent terms, , where are any terms represented by . By continuing this reasoning, we observe that , represents a total of possible terms, namely all terms of the form: where each can be either or .
Open problems about EqSat.
We still understand very little about equality saturation. Most descriptions of EqSat focus on an imperative understanding111 A notable exception is egglog [34], whose semantics is based on fixpoints instead of implementation details. Some early works also define E-graphs (under a different name like abstract congruence closure) as tree automata similar to ours [26, 2, 12]. of equality saturation and E-graphs. E-graphs are described by their individual components (e.g., a hashconsing data structure, a union-find, etc.), and EqSat is commonly defined in pseudocode as a sequence of operations. In other words, the semantics of EqSat is the output of the specific algorithm, if it terminates; if the algorithm diverges, the semantics is undefined.
We also do not know much about when EqSat terminates. The termination problem asks, given a set of rewrite rules, whether EqSat terminates on a given input E-graph, or whether it terminates on all input E-graphs. This is a fundamental problem of EqSat and has applications in program/query optimization and equivalence checking: If EqSat terminates on the symmetric closure of a set of (variable-preserving) rewrite rules , it decides the word problem of the equational theory defined by (22). With an appropriate cost model, EqSat can further pick the optimal program among all programs equivalent to the input, e.g. by using Knuth’s algorithm [15].
Our contribution.
After a review of some background material in Sec. 2, we introduce E-graphs and EqSat in Sec. 3. Our definition, in Sec. 3.1, applies even to the cases when equality saturation does not terminate and, for that purpose, we define the E-graph to be a reachable, deterministic tree automaton, with possibly infinitely many states. By explicitly allowing infinite E-graphs we can define a formal semantics even when EqSat does not terminate. We show that concepts in tree automata are in 1-1 correspondence with those in E-graphs: the automaton states correspond to E-classes, and the transitions correspond to E-nodes. A term is represented by an E-graph iff it is accepted by the E-graph viewed as a standard tree automaton. We prove that, for any two E-graphs there exists at most one homomorphism between them, and, therefore, E-graphs are rigid tree automata. Next, in Sec. 3.2, we define a few basic operations on E-graphs, such as E-matching, insertion, congruence closure, and least upper bounds, by relying on tree-automata concepts. Using these operations, we define in Sec. 3.3 an immediate consequence operator (ICO), and define EqSat formally as the least fixpoint of the ICO. The least fixpoint always exists and is unique, even if the fixpoint procedure does not terminate, in which case the least fixpoint may be infinite. Finally, we prove an important lemma, called the Finite Convergence Lemma, stating that, if the least fixpoint is finite, then equality saturation procedure converges in finitely many steps. This is not immediately obvious because, while E-matching and insertion strictly increase the size of the E-graph, congruence closure may decrease it. A similar proposition fails for TGDs and EGDs: there exists an infinite chase where all instances have bounded size, hence its “limit” is finite.
Next, in Sec. 4 we describe the connection between EqSat and the chase. After a brief review of the chase in Sec. 4.1, we start by presenting a reduction from the Skolem chase to equality saturation, denoted (Sec. 4.2), then from equality saturation to the standard chase, denoted (Sec. 4.3). For , given a set of dependencies, we show there exists a set of rewrite rules where EqSat produces an encoded result of the Skolem chase and has the same termination behavior. For , we show that, given a set of rewrite rules, there exists a set of dependencies where the standard chase produces an encoded result of EqSat (whether it terminates). Since the standard chase is a non-deterministic process, we characterize the type of chase sequences that terminate when EqSat terminates (30). We call them EGD-fair chase sequences. Intuitively, a chase sequence is called EGD-fair if it applies EGDs to a fixpoint frequently enough. We show in 30 that,
EqSat terminates | one chase sequence terminates |
---|---|
all EGD-fair chase sequences terminate. |
The notion of EGD-fair chase sequences is of independent interest.
Finally, we present our main decidability results for EqSat in Sec. 5: we show that the single-instance termination problem of EqSat, denoted as , is R.E.-complete, and the all-term-instance termination problem of EqSat, denoted as , is -complete. Our proof is based on a non-trivial reduction from the Turing machine, first presented in the undecidability proof of the finiteness of congruence classes defined by string rewriting systems [21]. While the single-instance case easily follows from the undecidability of Skolem chase termination, our approach allows us to also prove the -completeness of the all-term-instance termination case by a reduction from the universal halting problem. We also show the all-E-graph-instance termination problem of EqSat, denoted as , is undecidable, although the exact upper bound is open.
We contrast the termination problems of EqSat with those of the Skolem chase and the standard chase. The single-instance termination problems are R.E.-complete in all three cases [18, 5], and the all-instance termination of the Skolem chase () is R.E.-complete as well [18, 10]. This shows that is easier than . The case for the standard chase is more interesting. There are two all-instance termination problems of the standard chase: for all database instances, whether all chase sequences terminate in finitely many steps (), and whether there exists at least one chase sequence that terminate (). It has been shown is -complete [11], but the exact complexity of is open. Grahne and Onet showed if we allow one denial constraint, is -complete [11], although Gogacz and Marcinkowski [10] conjectured that this problem is indeed in R.E.
2 Background
2.1 Term Rewriting Systems
We review briefly the standard definition of a term rewriting system from [1]. A signature is a finite set of function symbols with given arities. If is a set of variables, then denotes the set of terms constructed inductively using symbols from and variables from . Members of are called patterns, and members of are called ground terms, or simply terms thereafter. A substitution is a function ; if is a pattern, then we denote by the term obtained by applying the substitution to . A rewrite rule has the form where lhs and rhs are patterns and the variables in rhs are a subset of those lhs, . A term rewriting system (TRS), , is a set of rewrite rules. defines a rewrite relation as follows: for any substitution and rule in , and, if then for any function symbol of arity , and any terms , . Let be the reflexive and transitive closure of . We define , , and . is a congruence relation. We define the set of reachable terms . If a term rewriting system is variable-preserving (i.e., for all rules), we define . It follows that .
2.2 Tree automata
Let be a signature. A (bottom-up) tree automaton is a tuple , where is a (potentially infinite)222 In this paper, we allow tree automata (and thus E-graphs) to have an infinite number of states and transitions. Talking about infinite E-graphs allow us to define the semantics of equality saturation even when the algorithm does not terminate. set of states, is a set of final states, and is a set of transitions of the form where , and . Denote by the signature extended with where each state is viewed as a symbol of arity 0. Then is a term rewriting system for , and we will denote by (rather than ) the rewrite relation defined by . A term is accepted by a state if , and we write for the set of terms accepted by . The language accepted by is . A tree language is called regular if it is accepted by some finite tree automaton.
Fix two tree automata . A homomorphism, , is a function that maps final states to final states, and, for every transition in there exists a transition in . An isomorphism333Notice that a bijective homomorphism is not necessarily an isomorphism. is a homomorphism for which there exists an inverse homomorphism such that and . The following holds:
Lemma 2.
Let be a homomorphism, , and be a state of . If , then . In particular, .
Proof.
We prove the statement by induction on the structure of the term . Assuming for 444 The base case is covered by the case . and , then there exists states such that and contains the transition . By induction hypothesis for , and since is a homomorphism, there exists a transition in , proving that .
We write whenever there exists a homomorphism . Observe that is a preorder relation. In the next section, we show that this preorder relation becomes a partial order when restricted to E-graphs (12).
We call an automaton deterministic if and implies for states . We call reachable if every state accepts some ground term: , .
3 E-graphs and Equality Saturation
Most papers discussing E-graphs use an operational definition not suitable for a theoretical analysis. We introduce an equivalent definition of E-graphs in terms of tree automata, similar to Kozen’s partial tree automata [17]. Throughout this section we fix the signature .
3.1 E-graphs
Definition 3.
An E-graph is a deterministic and reachable tree automaton (without a set of final state ).
Our definition maps one-to-one to the classical definition of E-graphs: An E-class is a state of the tree automaton, and an E-node is a transition . A term is represented by the E-class if is accepted by , i.e. . In the literature, the sets of E-classes and E-nodes are denoted and respectively. We will use states/E-classes and transitions/E-nodes interchangeably in this paper. E-graphs do not define a set of “final” E-classes, and for that reason we omit the final states from 3555 Alternatively, consider . , similarly to [17].
Example 4.
The E-graph in Figure 1 is the automaton , where , there are four states , and consists of seven transitions:
An example of rewritings is , showing that the term is represented by the E-class .
It is folklore that E-graphs represent equivalences of terms. We make this observation formal, by defining the semantics of an E-graph to be a certain partial congruence. A partial equivalence relation, or PER, on a set is a binary relation that is symmetric and transitive. Its support is the set . Equivalently, a PER can be described by its support and an equivalence relation on the support. A PER on the set of terms is congruent if for and implies . A PER is reachable if implies , for . A Partial Congruence Relation (PCR)666PCRs are studied in the literature as congruences on partial algebras (e.g., [17]). on is a congruent and reachable PER.
An E-graph induces a PCR defined as follows: if there exists some E-class in that accepts both and , i.e. . We check that is a PCR: is symmetric by definition, and transitivity follows from determinacy, because and implies . Suppose : then there exists states s.t. , and a transition , proving reachability; if, in addition, for , then , which implies , proving congruence, .
Definition 5.
The semantics of an E-graph is the PCR .
Theorem 6.
For any PCR over there exists a unique such that .
Proof sketch.
The states of are the equivalence classes of , denoted as for , and the transitions are for all in the support of . One can check by induction on the size of that iff , and iff , proving that .
Thus, the semantics of an E-graph is a PCR , which is a congruence on . We say that represents the set of terms .
Example 7.
Example 8.
Let . Consider the E-graph with a single state and transitions , . It represents infinitely many terms, , for , and its semantics is the PCR
Example 9.
Let and consider the infinite E-graph with states and transitions
The PCR consists of , defined by the state . No other distinct terms are in , for example because they are represented by the distinct states and respectively. Although represents a regular language, , its semantics cannot be captured by a finite E-graph. This example shows that differs from the Myhill-Nerode equivalence relation [16], under which all terms would be equivalent. It also illustrates the subtle distinction between tree automata and E-graphs. An optimizer that wants to use the identity , but not , needs this E-graph to represent all terms equivalent to , and cannot use the finite tree automaton accepting the regular language because that would incorrectly equate all terms .
Recall the definitions of tree automata homomorphisms in Sec. 2.2. When restricted to E-graphs, homomorphisms have some interesting properties:
Lemma 10.
If is a homomorphism, then .
Proof.
Assume . Then there exists some E-class where . By 2, , implying .
Lemma 11.
There exists at most one homomorphism .
Proof.
Call the weight of a state in the size of the smallest term such that . Since is reachable, every state has a finite weight. Given two homomorphisms , we prove by induction on the weight of that . Let be a term of minimal size such that , and assume , for . Then there exists states such that , , and a transition in . By induction hypothesis for . By the definition of a homomorphism, contains both transitions and , and we conclude because is deterministic.
We call a tree automaton rigid [13] if the identity mapping is the only homomorphism . It follows from 11 that every E-graph is a rigid tree automaton.
Lemma 12.
over E-graphs forms a partial order up to isomorphism.
Proof.
Obviously is reflexive and transitive. To prove anti-symmetry, assume two homomorphisms , . The composition is a homomorphism , and, by uniqueness, it must be the identity on ; similarly, is the identity on , proving that is an isomorphism, thus are isomorphic.
Next, we define models for term rewriting systems.
Definition 13.
We say that an E-graph is a model of a TRS if, for every rule in and any substitution , if then . If is another E-graph, then we say that is a model for the pair if and is a model of . is a universal model if for any other model , it holds that .
When it exists, the universal model is unique up to isomorphism, because and implies are isomorphic.
Continuing 7, let consists of the rule , and let be the E-graphs in Figure 1. is not a model of , because for the substitution we have , but . On the other hand, one can check that is a model of ; in fact it is a model of , because .
Given an E-graph and a TRS , equality saturation constructs a universal model of , by repeatedly applying some simple operations on , which we define next.
3.2 Operations over E-graphs
E-matching, Insertion, and Rebuilding are the building blocks of equality saturation. They defined in the literature operationally [19]. We provide here a formal definition, using tree automata terminology. Throughout this section we fix an E-graph .
E-matching a rule in returns the set of pairs , where is a substitution such that . For example, considering the E-graph on the left of Figure 1 and the rule , E-matching returns for . E-matching is analogous to computing the triggers for a TGD or EGD (Sec. 4.1).
The Insertion of a pair into , where and , returns an automaton such that such that . To define , we need the following:
Definition 14.
Fix a term and a state . The flattening for with root , in notation , or just FL when are clear from the context, is an E-graph that has one distinct state for each subterm of , and has a transition , for all subterms of the form and . Moreover, it is enforced that the state of the root node is (i.e., ). One can check that , and that is the identity on all subterms of . Flattening is also called normalization [9].
The result of inserting in is (i.e. we take the set-union of all states and all transitions). The result is a reachable tree automaton, but it is non-deterministic in general, thus it is not an E-graph; the next operation, rebuilding, converts it back into an E-graph. holds, because the inclusion is a homomorphism.
As an example, if we insert the pair in the E-graph in Figure 2, the result is in the center of the figure; flattening has a single transition .
Rebuilding converts into a deterministic automaton. Formally, let be any reachable tree automaton, and recall that may be infinite. The congruence closure is an E-graph (i.e. deterministic, reachable automaton) such that and, for any other E-graph , if , then . We prove the following in the full version:
Lemma 15.
For any reachable tree automaton , exists and is unique.
The procedure of computing , also known as rebuilding in EqSat literature [32], can be done efficiently in the finite case, for instance with Tarjan’s algorithm [7]. The idea is to repeatedly find violating transitions and with , and replace every occurrence of with , until fixpoint. This is similar to determinizing , but instead of constructing powerset states like , we equate states ; thus, has at most as many states as , and the procedure always terminates for finite , as merging shrinks the number of states.
Example 16.
Least upper bound of E-graphs.
Let be a (possibly infinite) family of E-graphs. Recall that their least upper bound is an E-graph such that is an upper bound for every E-graph in the set, for all , and for any other upper bound , it holds that . We prove the following in the full version:
Lemma 17 (Least upper bound).
The least upper bound exists and is given by , where is the automaton consisting of the disjoint union of the states and the disjoint union of the transitions of all E-graphs .
We will denote the least upper bound by . It is also possible to show that every family of E-graphs admits a greatest lower bound, by using a product construction [12], but we do not need it in this paper.
3.3 Equality saturation
The standard definition of equality saturation in the literature is procedural: given an E-graph and TRS , equality saturation repeatedly applies matching/insertion/rebuilding. EqSat is undefined when this process does not terminate. We provide here an alternative definition, as the least fixpoint of an immediate consequence operator (ICO), and prove that it always exists. We start by introducing the ICO:
(2) |
is the match/apply operator: it computes all E-matches then inserts the rhs’s into :
CC is the rebuilding operator of 15.
Lemma 18.
is inflationary ( for all ) and monotone.
Proof.
That is inflationary follows from . We prove that both and CC are monotone. Let , thus . Any homomorphism can be extended to a homomorphism , which proves that is monotone. Consider two automata and assume , i.e. there exists a homomorphism from to . Denote , . Then, , which implies . By the definition of , we have , proving that CC is monotone.
Theorem 19.
Fix an E-graph , and consider the class of E-graphs . Then has a least fixpoint, given by
(3) |
Furthermore, is a universal model of ; we call it equality saturation.
Given , our semantics of EqSat is the least fixpoint in (3), which is also the unique universal model of . When is finite, then this coincides with the standard procedural definition in the literature. A common case (e.g., in program and query optimization settings) is when represents a single term , more precisely with fresh state ; in that case we denote as .
Properties of equality saturation.
We establish several basic facts of EqSat.
Lemma 20 (Inflationary).
.
Corollary 21.
and .
20 follows from 18, while 21 follows from 2 and 10. Thus, represents more terms than , and identifies more pairs of terms than . Next, we examine the relationship between the PCR defined by and the relations and defined by the TRS (see Sec. 2.1). If are two PCRs, then we denote by the smallest PCR that contains both. We prove:
Lemma 22 (Representation).
Let be a term represented by some state of the E-graph . The following hold: .
Proof.
The definitions of E-matching and insertion imply that, if and is represented by some state of some E-graph , then is represented by the same state of the E-graph . Therefore, if and is represented by some state of , then is represented by the same state of (because is a fixpoint of ICO). This implies that .
For the second part, we denote by , and check by induction on that . When then and the claim is obvious. For the inductive step we observe that the only new identities introduced by are justified by .
In other words, if , then will equate with ; and if equates with then this can be derived from and . In general, does not imply . For a simple example, let , thus , and let represent only the term . Then represents only the term , thus .
Example 23.
For some TRS, the starting E-graph determines whether EqSat terminates in a finite number of steps. For a simple example, consider , . If the initial E-graph represents only the term (and its subterms), then EqSat terminates, and the resulting represents a PCR where . On the other hand, if is the E-graph with states and transitions , then already represents infinitely many terms , where and . After equality saturation, represents all terms in where the numbers of occurrences of satisfy . This is not a regular language, hence is infinite, and EqSat will not terminate in a finite number of steps.
Example 24.
We show that both inclusions in 22 can be strict. Let and let be the E-graph representing a single term with transitions . Then has transitions: . We have:777 We use as a shorthand for (as in regular expressions) and similarly for other terms. , , and . All three sets are different.
However, the three expressions in 22 are equal in an important special case:
Corollary 25.
Suppose is a variable-preserving term rewriting system. Let , and let be a term represented by some state of the E-graph . The following hold: .
Let be a finite E-graph. If is infinite, then EqSat does not terminate in a finite number of steps. Somewhat surprisingly, the converse does hold: if is finite, then EqSat terminates in a finite number of steps. This follows from the next lemma, whose proof is deferred to the full version.
Lemma 26 (Finite convergence).
Let be an ascending sequence of finite E-graphs. If is finite, then the sequence is finite.
Corollary 27 (Finite convergence of EqSat).
Let be a term rewriting system and be a finite E-graph. If is finite, EqSat converges in a finite number of steps.
4 Equality saturation and the chase
In this section, we briefly review necessary background on databases and the chase. Then, we will show the fundamental connections between equality saturation and the chase.
4.1 The Chase Procedure
Databases and conjunctive queries.
A relational database schema is a tuple of relation names with associated arities . A database instance is a tuple of relation instances , where for some domain Dom. We allow an instance to be infinite. We often view a tuple in as an atom , and view the instance as a set of atoms. The domain Dom is the disjoint union of set of constants and a set of marked nulls.
A conjunctive query is a formula with free variables of the form , where each is a tuple of variables from . The canonical database of a conjunctive query consists of all the tuples , where the variables are considered marked nulls.
Let , be two database instances. A homomorphism from to , in notation , is a a function that is the identity on the set of constants, and maps each atom to an atom . The notion of homomorphism immediately extends to conjunctive queries and/or database instances. The output of a conjunctive query on a database is defined as the set of homomorphisms from to . We say that a database instance satisfies a conjunctive query , denoted by , if there exists a homomorphism .
Dependencies.
TGDs and EGDs describe semantic constraints between relations. A TGD is a first-order formula of the form where and are conjunctive queries with free variables in and . An EGD is a first-order formula of the form where is a conjunctive query with free variables in and .
Fix a set of TGDs and EGDs . If is a database instance and , then a trigger for in is a homomorphism from (resp. ) to . An active trigger is a trigger such that, if is a TGD, then no extension to a homomorphism exists, and, if is an EGD, then . We say that is model for , and write , if it has no active triggers.
Given and we say that some database instance is a model for , if and there exists a homomorphism . is called universal model if there is a homomorphism from to every model of and . Universal models are unique up to homomorphisms.
The chase.
The chase is a fixpoint algorithm for computing universal models. We consider two variants of the chase here: the standard chase and Skolem chase. Both the standard chase and the Skolem chase produce a universal model of [8, 3, 18]. The standard chase computes answers by deriving a sequence of chase steps until all dependencies are satisfied. A chase step, denoted as , takes as inputs an instance , a homomorphism , and a dependency , where is an active trigger of in , and produces an output instance by adding some tuples (for TGDs) or collapsing some elements (for EGDs). Specifically, if is a TGD, the chase step extends with the tuple , where is an extension of that maps the variables on which is undefined to fresh marked nulls. If is an EGD, if (or ) is a marked null, a chase step replaces in every occurrence of with (or with ). If neither nor is a marked null and , then the chase fails.
A standard chase sequence starting at is a sequence of successful chase steps that is fair: for all , for each dependency and active trigger of in , some must exist such that is no longer an active trigger of in . The result of a (possibly infinite) chase sequence is [3]. A chase sequence is terminating if it ends with and , in which case is the result of the chase sequence. The standard chase is non-deterministic: depending on the order of firing, the chase sequence can be different. Different chase sequences can even differ on whether they terminate.
The Skolem chase, discussed in [18], differs from the standard chase in several ways. It first skolemizes each TGD to where each is an uninterpreted function from to Dom. The result of the Skolem chase, denoted as , is the least fixpoint of the immediate consequence operator (ICO) of the Skolemized TGDs. Note that the Skolem chase does not directly handle EGDs but uses a technique called singularization [18] to simulate EGDs with TGDs.
4.2 Reducing the Skolem chase to equality saturation
In this section, we show how to reduce the Skolem chase to EqSat. We only consider TGDs, since in the Skolem chase, EGDs are modeled as TGDs using singularization [18].
We show an encoding where there exists a simple mapping from E-graphs to database instances, defined by
such that, given a set of dependencies , running EqSat on an encoded term rewriting system from corresponds to running the Skolem chase on the set of dependencies via . Intuitively, given an E-grpah, collects every term that corresponds to a tuple from the language of . An illustration of is shown in Figure 3.
Theorem 28.
Given a database schema , a set of TGDs , and an initial database , it is possible to define a signature , a term rewriting system over , and an initial term such that
Moreover, the Skolem chase terminates if and only if equality saturation terminates.
The intuition for the construction is that we can uniformly treat relational atoms as E-nodes contained in a special E-class, and Skolem functions naturally correspond to terms in EqSat. More specifically,
-
Add symbols to the signature . Add rewrite to . Let the initial term be .
-
Add every Skolem function symbol to , and for every -ary relational symbol , add a -ary function symbol to , and add rewrite rule .
-
For every Skolemized TGD
replace the conjunctions in the head and body with nested applications of and :
and add to .
-
For each constant in the input database , add a nullary function symbol to . For each tuple in the input database , add rewrite .
Proof of 28.
See the full version.
4.3 Reducing equality saturation to the standard chase
We show how to reduce equality saturation to the standard chase. The encoding itself is straightforward. However, the standard chase is non-deterministic and can have different chase sequences, so a natural question is what kind of the chase sequence will converge finitely, given that EqSat terminates, and vice versa. We show that as long as the chase sequence applies EGDs frequent enough, the chase sequence will always converge. We capture this notion as EGD-fairness.
Definition 29.
Given a database schema , a set of dependencies over , and an initial database . We call a chase sequence of and EGD-fair if for every , either is a model of and the chase terminates, or there exists some such that is a model of the EGD subset of .
Given that EqSat terminates, what can we say about chase sequences that are not EGD-fair? In fact, such chase sequences may not terminate. Despite this, it can be shown that the result of such chase sequences, terminating or not, is isomorphic to the result of equality saturation (when encoded as a database). On the other hand, to show that equality saturation terminates, it is sufficient to show an arbitrary chase sequence terminates.
The following theorem shows the connection between EqSat and the standard chase.
Theorem 30.
Given signature , a set of rewrite rules over , and an initial E-graph , it is possible to define a relational schema , a set of dependencies over , and an initial database over . The following three statements are equivalent:
-
1.
Equality saturation terminates for and .
-
2.
There exists a terminating chase sequence of the standard chase for and .
-
3.
All EGD-fair chase sequences of the standard chase terminate for and .
Moreover, denote the result of an arbitrary chase sequence as . If equality saturation terminates, is isomorphic to the database encoding the resulting E-graph of .
The encoding consists of two steps. First, we can encode an E-graph as a database. Second, we encode the match/apply operator and congruence closure operator as a set of TGDs and EGDs. To encode an E-graph as a database:
-
Take the domain Dom to be the set of all E-classes, which are treated as marked nulls.
-
For every function symbol of arity , add relation symbol of arity to .
-
For every E-node , add a tuple to the database .
Under this encoding, each E-class is treated as a marked null, and each E-node is treated as a tuple.
The encoding of the match/apply operator and congruence closure operator is plain:
-
For every function symbol of arity , add a functional dependency to .
-
For every rewrite rule in , flatten the left- and right-hand side into conjunctions of relational atoms, unify the variable denoting the root node of lhs with that of rhs, and add existential quantifiers to the head accordingly. For example, rule is flattened into .
There are two corner cases to the above translations. First, if lhs is a single variable , we need to introduce rules of the form , one for each function symbol, to “ground” . For instance, suppose , rewrite rule is flattened into two dependencies: and . Second, in the case that the right-hand side is a single variable , we need to add an EGD instead of a TGD. For example, rule is encoded as an EGD .
Proof of 30.
See the full version.
5 The termination theorems of equality saturation
Finally, we present our main results here.
Theorem 31 (Single-instance termination).
The following problem is R.E.-complete:
-
Instance: A term rewriting system , a term .
-
Question: Does EqSat terminate with and ?
Theorem 32 (All-term-instance termination).
The following problem is -complete:
-
Instance: A term rewriting system .
-
Question: Does EqSat terminate with and for all terms ?
While 31 follows immediately from the fact the the Skolem chase is undecidable, our proof in the full version is based on Narendran et al. [21], which allows us to also show 32. We encode a Turing machine as a term rewriting system with the property that the congruence classes of initial configurations corresponds to traces of running such configurations, and that EqSat terminates if and only if congruence class is finite. For the all-term-instance case, we then show that the congruence class of an arbitrary term is infinite if and only if the congruence class of an initial configuration is. The actual proof is slightly more involved so we refer the reader to the full version for more details.
The technique above does not apply to the all-E-graph-instance case, however. The all-E-graph-instance termination can be thought of as having inputs both a term and a set of ground identities, and we have no control over the latter. Still, we are able to prove that this problem is undecidable by a reduction from the Post correspondence problem (see the full version), while the exact upper bound is unknown.
Theorem 33 (All-E-graph-instance termination).
The following problem is undecidable:
-
Instance: A term rewriting system .
-
Question: Does EqSat terminate with and for all E-graphs ?
6 Weak term acyclity for equality saturation termination
We can adapt the classic weak acyclicity criterion [8], which is used to show the termination of the chase algorithm, to equality saturation. The adapted criterion, which we call weak term acyclicity, is more powerful than simply translating EqSat rules to TGDs/EGDs and applying weak acyclicity. We demonstrate weak term acyclicity with two examples, and the full definition can be found at the full version.
Example 34.
Example 35.
Consider . This ruleset is weakly term acyclic. Its weak term dependency graph is shown in 4(b).
7 Conclusion
We have presented a semantic foundation for E-graphs and EqSat: We identified E-graphs as reachable and deterministic tree automata and defined the result of EqSat as the least fixpoint according to E-graph homomoprhisms. We defined the universal model of E-graphs and showed the fixpoint EqSat produces is the universal model (19). We showed several basic properties about E-graphs, including a finite convergence lemma (26). We then established connections between EqSat and the chase in both directions (Sec. 4) and characterize chase sequences that correspond to EqSat with EGD-fairness (29). Our main results are on the terminations of EqSat in three cases: single-instance, all-term-instance, and all-E-graph-instance. Finally, adapting ideas from weak acyclicity for the chase, we defined weak term acyclicity which implies EqSat termination.
The correspondence between EqSat and the chase established in this paper may help further port the rich results of database theory to EqSat, as the current paper only scratches the surface of the deep literatures of the chase. Another direction is to use our better understanding of EqSat to design more efficient and expressive EqSat tools and better support downstream applications of EqSat. Finally, many problems about EqSat are still open. For example, the exact upper bound of the all-E-graph-instance termination is not known. Other problems include rule scheduling, evaluation algorithm, and E-graph extraction.
References
- [1] Franz Baader and Tobias Nipkow. Term Rewriting and All That. Cambridge University Press, USA, 1999.
- [2] Leo Bachmair, Ashish Tiwari, and Laurent Vigneron. Abstract congruence closure. J. Autom. Reason., 31(2):129–168, 2003. doi:10.1023/B:JARS.0000009518.26415.49.
- [3] Michael Benedikt, George Konstantinidis, Giansalvatore Mecca, Boris Motik, Paolo Papotti, Donatello Santoro, and Efthymia Tsamoura. Benchmarking the chase. In Proceedings of the 36th ACM SIGMOD-SIGACT-SIGAI Symposium on Principles of Database Systems, PODS ’17, pages 37–52, New York, NY, USA, 2017. Association for Computing Machinery. doi:10.1145/3034786.3034796.
- [4] Samuel Coward, George A. Constantinides, and Theo Drane. Automating constraint-aware datapath optimization using e-graphs. 2023 60th ACM/IEEE Design Automation Conference (DAC), pages 1–6, 2023. URL: https://api.semanticscholar.org/CorpusID:257353847.
- [5] Alin Deutsch, Alan Nash, and Jeff Remmel. The chase revisited. In Proceedings of the Twenty-Seventh ACM SIGMOD-SIGACT-SIGART Symposium on Principles of Database Systems, PODS ’08, pages 149–158, New York, NY, USA, 2008. Association for Computing Machinery. doi:10.1145/1376916.1376938.
- [6] RisingLight developers. RisingLight: An Educational OLAP Database System, December 2022. URL: https://github.com/risinglightdb/risinglight.
- [7] Peter J. Downey, Ravi Sethi, and Robert Endre Tarjan. Variations on the common subexpression problem. Journal of the ACM, 27(4):758–771, October 1980. doi:10.1145/322217.322228.
- [8] Ronald Fagin, Phokion G. Kolaitis, Renée J. Miller, and Lucian Popa. Data exchange: semantics and query answering. Theoretical Computer Science, 336(1):89–124, 2005. Database Theory. doi:10.1016/j.tcs.2004.10.033.
- [9] Thomas Genet. Termination criteria for tree automata completion. Journal of Logical and Algebraic Methods in Programming, 85(1, Part 1):3–33, 2016. Rewriting Logic and its Applications. doi:10.1016/j.jlamp.2015.05.003.
- [10] Tomasz Gogacz and Jerzy Marcinkowski. All–instances termination of chase is undecidable. In Javier Esparza, Pierre Fraigniaud, Thore Husfeldt, and Elias Koutsoupias, editors, Automata, Languages, and Programming, pages 293–304, Berlin, Heidelberg, 2014. Springer Berlin Heidelberg. doi:10.1007/978-3-662-43951-7_25.
- [11] Gösta Grahne and Adrian Onet. Anatomy of the chase. Fundam. Informaticae, 157(3):221–270, 2018. doi:10.3233/FI-2018-1627.
- [12] Sumit Gulwani, Ashish Tiwari, and George C. Necula. Join algorithms for the theory of uninterpreted functions. In Kamal Lodaya and Meena Mahajan, editors, FSTTCS 2004: Foundations of Software Technology and Theoretical Computer Science, pages 311–323, Berlin, Heidelberg, 2005. Springer Berlin Heidelberg.
- [13] Pavol Hell and Jaroslav Nesetril. Images of rigid digraphs. Eur. J. Comb., 12(1):33–42, 1991. doi:10.1016/S0195-6698(13)80005-4.
- [14] Rajeev Joshi, Greg Nelson, and Keith Randall. Denali: A goal-directed superoptimizer. SIGPLAN Not., 37(5):304–314, May 2002. doi:10.1145/543552.512566.
- [15] Donald E. Knuth. A generalization of dijkstra’s algorithm. Inf. Process. Lett., 6(1):1–5, 1977. doi:10.1016/0020-0190(77)90002-3.
- [16] Dexter Kozen. On the myhill-nerode theorem for trees. Bulletin of the EATCS, 47:170–173, 1992.
- [17] Dexter Kozen. Partial Automata and Finitely Generated Congruences: An Extension of Nerode’s Theorem, pages 490–511. Birkhäuser Boston, Boston, MA, 1993. doi:10.1007/978-1-4612-0325-4_16.
- [18] Bruno Marnette. Generalized schema-mappings: From termination to tractability. In Proceedings of the Twenty-Eighth ACM SIGMOD-SIGACT-SIGART Symposium on Principles of Database Systems, PODS ’09, pages 13–22, New York, NY, USA, 2009. Association for Computing Machinery. doi:10.1145/1559795.1559799.
- [19] Leonardo Moura and Nikolaj Bjørner. Efficient e-matching for smt solvers. In Proceedings of the 21st International Conference on Automated Deduction: Automated Deduction, CADE-21, pages 183–198, Berlin, Heidelberg, 2007. Springer-Verlag. doi:10.1007/978-3-540-73595-3_13.
- [20] Chandrakana Nandi, Max Willsey, Adam Anderson, James R. Wilcox, Eva Darulova, Dan Grossman, and Zachary Tatlock. Synthesizing structured CAD models with equality saturation and inverse transformations. In Proceedings of the 41st ACM SIGPLAN Conference on Programming Language Design and Implementation, PLDI 2020, pages 31–44, New York, NY, USA, 2020. Association for Computing Machinery. doi:10.1145/3385412.3386012.
- [21] Paliath Narendran, Colm Ó’Dúnlaing, and Heinrich Rolletschek. Complexity of certain decision problems about congruential languages. Journal of Computer and System Sciences, 30(3):343–358, 1985. doi:10.1016/0022-0000(85)90051-0.
- [22] Charles Gregory Nelson. Techniques for Program Verification. PhD thesis, Stanford University, Stanford, CA, USA, 1980. AAI8011683.
- [23] Hung Q. Ngo, Christopher Ré, and Atri Rudra. Skew strikes back: new developments in the theory of join algorithms. SIGMOD Rec., 42(4):5–16, 2013. doi:10.1145/2590989.2590991.
- [24] Pavel Panchekha, Alex Sanchez-Stern, James R. Wilcox, and Zachary Tatlock. Automatically improving accuracy for floating point expressions. SIGPLAN Not., 50(6):1–11, June 2015. doi:10.1145/2813885.2737959.
- [25] Maximilian Schleich, Amir Shaikhha, and Dan Suciu. Optimizing tensor programs on flexible storage, 2022. doi:10.48550/arXiv.2210.06267.
- [26] Wayne Snyder. A fast algorithm for generating reduced ground rewriting systems from a set of ground equations. J. Symb. Comput., 15(4):415–450, April 1993. doi:10.1006/jsco.1993.1029.
- [27] Dan Suciu, Yisu Remy Wang, and Yihong Zhang. Semantic foundations of equality saturation, 2025. arXiv:2501.02413.
- [28] Ross Tate, Michael Stepp, Zachary Tatlock, and Sorin Lerner. Equality saturation: A new approach to optimization. In Proceedings of the 36th Annual ACM SIGPLAN-SIGACT Symposium on Principles of Programming Languages, POPL ’09, pages 264–276, New York, NY, USA, 2009. ACM. doi:10.1145/1480881.1480915.
- [29] Alexa VanHattum, Rachit Nigam, Vincent T. Lee, James Bornholt, and Adrian Sampson. Vectorization for Digital Signal Processors via Equality Saturation, pages 874–886. Association for Computing Machinery, New York, NY, USA, 2021. doi:10.1145/3445814.3446707.
- [30] Yisu Remy Wang, Shana Hutchison, Jonathan Leang, Bill Howe, and Dan Suciu. SPORES: Sum-product optimization via relational equality saturation for large scale linear algebra. Proceedings of the VLDB Endowment, 2020.
- [31] Yisu Remy Wang, Mahmoud Abo Khamis, Hung Q Ngo, Reinhard Pichler, and Dan Suciu. Optimizing recursive queries with program synthesis. arXiv preprint arXiv:2202.10390, 2022. arXiv:2202.10390.
- [32] Max Willsey, Chandrakana Nandi, Yisu Remy Wang, Oliver Flatt, Zachary Tatlock, and Pavel Panchekha. Egg: Fast and extensible equality saturation. Proc. ACM Program. Lang., 5(POPL), January 2021. doi:10.1145/3434304.
- [33] Yichen Yang, Phitchaya Mangpo Phothilimtha, Yisu Remy Wang, Max Willsey, Sudip Roy, and Jacques Pienaar. Equality saturation for tensor graph superoptimization. In Proceedings of Machine Learning and Systems, 2021. arXiv:2101.01332.
- [34] Yihong Zhang, Yisu Remy Wang, Oliver Flatt, David Cao, Philip Zucker, Eli Rosenthal, Zachary Tatlock, and Max Willsey. Better together: Unifying datalog and equality saturation. Proc. ACM Program. Lang., 7(PLDI), June 2023. doi:10.1145/3591239.
- [35] Yihong Zhang, Yisu Remy Wang, Max Willsey, and Zachary Tatlock. Relational e-matching. Proc. ACM Program. Lang., 6(POPL), January 2022. doi:10.1145/3498696.