The Theoretical Aspect of Equality Saturation (Part II)
Post Metadata
In the last post, we showed the connections between chase and techniques developed in databases literature and tree automata literature, and how these connections help us understand equality saturation (EqSat). In this blog post, we will focus on a specific problem EqSat faces: the termination problem^{1}, something my collaborators and I have been working on recently.
The termination problem of EqSat is as follows:
 Instance: a set of rewrite rules \(R\), a term \(t\).
 Problem: does EqSat terminate over \(R\) and \(t\)?
The termination problems of the chase, term rewriting, and tree automata completion, which are defined similarly, have been extensively studied in their respective communities. There are tons of papers on when the chase terminates, when term rewriting terminates, and the problem when tree automata completion terminates is as old as tree automata completion, if not older. This makes sense as in these settings, termination is highly desirable. These algorithms rely on termination to work, and nontermination means no output can be obtained. Unfortunately, the termination problem of tree automata (TA) completion and the chase are both undecidable. Both communities came up with numerous conditions when the problems are decidable.
Compared to TA completion and the chase, the termination problem of EqSat is less disastrous. Equality saturation does not rely on termination for it to be sound: it is always possible to terminate after a fixed number of iterations and yield a partial output. The partial output is still useful in domains where equality saturation is applied, such as program optimization and program verification.
However, if we want stronger guarantees from equality saturation, it is still necessary to ensure EqSat terminates. For example, we can guarantee optimality in program optimization if EqSat saturates in a finite number of steps, at which stage all possible programs have been contained in the output Egraph. Similarly, in program verification, termination also implies decidability of program equivalence checking.
As one may expect, the termination problem of EqSat is not trivial. The undecidability of the termination problem can be shown by a reduction from the halting problem of Turing machines. Fortunately, it is possible to leverage decades of research done on the termination of the abovementioned techniques like TA completion and the chase, and adapt them to the setting of EqSat. This is an active research topic that we are working on.
One may also wonder what the relationships among the termination problems of EqSat, the chase, term rewriting, and TA completion are. It is possible to reduce the termination of EqSat to the chase via a straightforward encoding. As a result, some techniques on showing chase termination also apply to showing EqSat termination, although the specifics of EqSat are unique enough to make the study on EqSat termination worth exploring. On the other hand, there are examples where EqSat terminates, but term rewriting and TA completion do not, and vice versa.
In conclusion, the termination problem is cool, and I am very excited about the project my collaborators and I are working on. To conclude this blog post, let me present to you some fun exercises:
Do the following rulesets^{2} always terminate in equality saturation for arbitrary Egraphs? Prove your claim.
 \(\{ f(x)\rightarrow f(g(x)) \}\).
 \(\{ f(g(x)) \rightarrow f(x) \}\).
 \(\{ f(g(x)) \rightarrow g(f(x)) \}\).
 \(\{ f(x, f(y, z)) \rightarrow f(f(x, y), z) \}\).
 \(\{ f(g(x, y), g(y, z)) \rightarrow g(x, z) \}\).
 \(\{ f(g(x, y), g(y, z)) \rightarrow g(h(), z) \}\).
 \(\{ f(f(x, y), z) \rightarrow f(f(x, y), f(f(x, y), z)) \}\).
 \(\{ h(f(x), y) \rightarrow h(x, g(y)), h(x, g(y)) \rightarrow h(x, y), f(x) \rightarrow x \}\).
 \(\{ f(x) \rightarrow g(f(h(x))) \}\).
 \(\{ f(x) \rightarrow g(f(h(x))), h(x) \rightarrow b \}\).
 \(\{ f(f(x, y), z) \rightarrow g(f(x, z))\}\).
 \(\{ f(f(x, y), z) \rightarrow g(f(y, x), z) \}\).
 \(\{ g(f(x, y), z) \rightarrow h(g(x, y), z) \}\).
 \(\{ f(g(x, y), g(z, x)) \rightarrow g(y, f(x, z)) \}\).
The solutions to these problems are at the end of the blog post :)
^{1} I also wrote down some notes on two other problems on equality saturation that I am excited about: extraction and scheduling.
^{2} In this blog post, we assume \(x,y,z\) are variables, \(f,g,h\) are function symbols, and \(a,b,c\) are constants (i.e., nullary functions)
Appendix: Solutions to the Exercise
Do the following rulesets always terminate in equality saturation for arbitrary Egraphs? Prove your claim.

\(\{ f(x)\rightarrow f(g(x)) \}\).
N: Consider an Egraph representing \(f(a)\). Applying this ruleset to it would produce \(f(g(a)), f(g(g(a))),\cdots\), where \(g(a)\not\approx g(g(a))\not\approx \cdots\). If equality saturation terminates, it has to produce infinitely many equivalence classes, which is impossible.
Visualization for running three iterations:

\(\{ f(g(x)) \rightarrow f(x) \}\).
Y: This ruleset always terminates because applying the rule does not create any new Eclass. There are only finitely many Egraphs with a bounded number of Eclasses. Therefore, equality saturation would always terminate.

\(\{ f(g(x)) \rightarrow g(f(x)) \}\).
N: To prove this does not terminate, let us first introduce a lemma: given a set of rewrite rules \(R\) and an initial Egraph \(G\), \(\rightarrow^*_R(L(G))\subseteq L(\textsf{EqSat}(G, R))\subseteq \leftrightarrow^*_R(L(G))\), where \(\rightarrow^*_R(L(G))\) is the set of terms reachable by terms in \(L(G)\) via \(R\) and \(\leftrightarrow^*_R(L(G))\) is the set of terms equivalent to terms in \(L(G)\) via \(R\).
Now back to this ruleset. Since we are only working with unary functions in this ruleset, it is more convenient to view it as a string rewriting system \(\{fg\rightarrow gf\}\). Now, consider an initial regular string language \(L=(fg)^*\). Both the reachable terms and equivalent terms of \(L\) under this rewriting system are the set of strings in \((fg)^*\) with the same number of character \(f\) and character \(g\). This set is not regular (not even contextfree), and therefore cannot be represented by a finite Egraph.

\(\{ f(f(x, y), z) \rightarrow f(x, f(y, z))\}\).
N: Consider an Egraph representing identity \(+(a, 0)\approx 0\). This Egraph represents terms \(a, a+0, (a+0)+0, \cdots\). Applying the ruleset to this Egraph would produce subterms \(0+0, (0+0)+0, \cdots\). Moreover, each term is in its own equivalence class, thus an infinite number of Eclasses, which is impossible.

\(\{ f(g(x, y), g(y, z)) \rightarrow g(x, z) \}\).
Y: Same as 2, since this ruleset does not produce any new Eclass.

\(\{ f(g(x, y), g(y, z)) \rightarrow g(h(), z) \}\).
Y: Applying this ruleset does not produce any new Eclass besides \(h()\), so equality saturation always terminates.

\(\{ f(f(x, y), z) \rightarrow f(f(x, y), f(f(x, y), z)) \}\).
Y: This ruleset does not produce any new Eclass: any subterm on the righthand side besides the top level also occurs on the lefthand side.

\(\{ h(f(x), y) \rightarrow h(x, g(y)), h(x, g(y)) \rightarrow h(x, y), f(x) \rightarrow x \}\).
N: It is tempting to think equality saturation would terminate on this ruleset, since this ruleset is convergent. However, equality saturation does not. Consider the following egraph containing term \(\{ g(a, b)\}\) and identity \(f(a)\approx a\).
Applying the rule \(h(f(x), y)\rightarrow h(x, g(y))\) would “unfold” the leftchild Eclass that contains \(\{a, f(a), \ldots\}\) and populate \(\{g(b), g^2(b),\ldots\}\) that are pairwise inequivalent to each other. Since other rules in this rewriting system would not union any of the \(g^n(b)\) with \(g^m(b)\), the Egraph would contain an infinite number of Eclasses, which is impossible.
Not that this ruleset can be “repaired” by adding the rule \(g(b)\rightarrow b\). In other words, we can force termination by contriving additional identities

\(\{ f(x) \rightarrow g(f(h(x))) \}\).
N: Consider an initial Egraph representing \(f(a)\). Equality saturation would populate \(\{g^n(f(h^n(x)))\mid n\in \mathbb{N} \}\), which is not a regular language.
Visualization for running equality saturation three times:

\(\{ f(x) \rightarrow g(f(h(x))), h(x) \rightarrow b \}\).
Y: We can assume the rule \(h(x)\rightarrow b\) is always first applied to the input Egraph and reduce the problem to whether \(\{f(x)\rightarrow g(f(b))\}\) always terminates in equality saturation. It is since the righthand side is a ground term (term without a variable).
Note that, in term rewriting, starting with term \(f(b)\), the set of reachable terms form the language \(\{ g^n(f(h^m(x)))\}\mid n,m\in\mathcal{N}, n\geq m\), which is not regular, so no finite Egraph can represent this language. Despite that, equality saturation indeed terminates for this ruleset, because it overapproximates the set of reachable terms.

\(\{ f(f(x, y), z) \rightarrow g(f(x, z))\}\).
Y: This (and the following rulesets) are the tricky ones. This ruleset is “weakly acyclic”. Weak acyclicity is a criterion developed for when the chase terminates, and we can adapt it to decide whether equality saturation terminates.
Without referring to weak acyclicity, the intuition for why this ruleset terminates is that, in this ruleset, the first child of \(f\) (i.e., \(x\)) stays as the first child and the second child of \(f\) (i.e., \(y, z\)) stays as the second child, so there are only finitely many \(f\) nodes. As a result, there are only finitely many \(g\) nodes, so the Egraph is finite.

\(\{ f(f(x, y), z) \rightarrow g(f(y, x), z) \}\).
Y: This ruleset is also weakly acyclic. To see the intuition, let us first consider rule \(f(f(x, y), z) \Rightarrow f(y, x)\), where \(\Rightarrow\) means “populate the righthand side, but do not union it with the lefthand side”. This process always terminates, because \(f(y,x)\), the new node created, would not participate in this rule again as \(x\) or \(y\). In other words, there are only a finite number of candidates for \(x\) and \(y\).
Once all possible \(f(y,x)\)’s are populated with the above process, we can then apply the original rule, which would not create any new Eclass, so it would terminate.

\(\{ g(f(x, y), z) \rightarrow h(g(x, y), z) \}\).
Y: This ruleset is also weakly acyclic. The idea is that possible instantiation of \(g(x, y)\) on the righthand side is bound by \(f(x, y)\), and after all \(g(x,y)\) have been populated, possible instantiations of the righthand side is bound by the number of \(g\) nodes.

\(\{ f(g(x, y), g(z, x)) \rightarrow g(y, f(x, z)) \}\).
N: Consider an Egraph containing term \(f(g(a, f(a, a)), g(a, a))\). Denote this term as \(u\), and add identity \(f(u, u) = u\) to the Egraph. This egglog program shows that equality saturation does not terminate on this egraph.