---
title: Equality Saturation Optimization
url: https://www.emergentmind.com/topics/equality-saturation
type: topic
---

# Equality Saturation Optimization

Equality saturation is a non-destructive program transformation and optimization technique based on e-graphs, a data structure that compactly represents a large set of equivalent terms under an equational theory. Equality saturation iteratively, and in parallel, applies all possible rewrite rules to terms encoded in the e-graph, merging their equivalence classes, until a fixpoint or resource limit is reached. This approach is widely adopted in compilers, theorem provers, and formal reasoning systems because it eliminates classical phase-ordering problems and enables the systematic discovery of optimized or canonical representations from a vast equivalence space [1012.1802][2004.03082][2501.02413][2101.01332][2312.17682]. The recent expansion of e-graph and equality saturation techniques includes categorical generalizations, formal semantic models, advanced extraction algorithms, and support for richer logical features such as case splitting and variable binding.

## 1. Formal Framework of Equality Saturation

At the core of equality saturation is the e-graph, which maintains a congruence closure over terms built from a signature Σ of operators. An e-graph consists of:

- **E-classes**: Sets of e-nodes representing terms known to be semantically equal.
- **E-nodes**: Records of the form $(f, [c_1, ..., c_r])$, with $f\in\Sigma$ and each $c_i$ an e-class ID [2004.03082][2101.01332].
- **Hash-consing**: A mapping from canonicalized e-nodes to e-class IDs to support fast congruence detection.
- **Union-find structure**: Maintains the partitioning of e-class IDs and underpins the congruence closure invariant.

The saturation process repeatedly applies all rewrite rules $\ell \to r$ by finding every possible e-match μ (substitution identifying $\ell$ in G), inserting any necessary new e-nodes, and merging their e-classes, followed by a rebuild step to re-establish congruence closure [2004.03082][2305.19203].

Formally, for rewrite system $R$, and initial e-graph $G_0$, the saturation process forms an ascending chain $G_0 \sqsubseteq ICO_R(G_0) \sqsubseteq ICO_R^2(G_0) \ldots$ (where $ICO_R$ is the inflationary immediate consequence operator), until a fixpoint representing all terms equivalent under $R$ is reached [2501.02413]. Extraction from this saturated e-graph yields a single representative (often via an ILP or dynamic programming pass) that minimizes a user-supplied cost function.

## 2. Advantages over Sequential Term Rewriting and Phase Ordering

Traditional optimization pipelines often destructively apply transformation passes in sequence, introducing *phase-ordering problems* in which the application of one optimization may preclude subsequent beneficial rewrites [1012.1802]. In contrast, equality saturation accumulates all reachable rewrites in a shared, non-destructive e-graph. This ensures:

- **Exploration of an exponential equivalence space**: All forms generated by rewrite rules are represented simultaneously.
- **Invariance to pass ordering**: No transformation disables another; all applicable rewrites fire in parallel.
- **Global extraction**: The best program (or query plan, tensor computation, etc.) is selected post-saturation based on holistic cost modeling.
- **Translation validation**: Semantic equivalence of two representations can be checked by their classes after saturation [1012.1802].

These properties are central in robust superoptimization [2101.01332], functional array idiom recognition [2312.17682], and advanced relational query optimization [2407.12794].

## 3. Data Structures and Algorithms Enabling Scalable Equality Saturation

E-graph performance hinges on amortized, scalable congruence closure and efficient e-matching.

- **Amortized rebuilding**: Modern systems defer congruence-closure “repairs” between batches of rewrites, coalescing multiple upward merges and avoiding superlinear work [2004.03082].
- **E-class analyses**: E-classes are extended with lattice-based analyses (for types, value ranges, shapes, etc.) to support domain-specific rewrites and analysis-triggered rule firing [2004.03082][2304.04332].
- **Batch and worklist loops**: Rule application is managed via worklists to apply all eligible rewrites per batch, avoiding repeated exploration of stable subgraphs [2312.17682].

Recent advances address further scalability bottlenecks:

- **Guided saturation**: Integration with reinforcement learning [2407.12794], sketch guides [2111.13040], or probabilistic models [2511.00403] focus the search and control e-graph blowup in complex domains.
- **Colored E-graphs**: Support for hundreds of simultaneous assumptions ("colors") via a layered union-find, sharing a base e-graph and maintaining coarsened congruences for case splitting, solving exponential duplication problems in conditional reasoning [2305.19203].

## 4. Extraction Methods: From E-Graphs to Optimized Terms

The extraction phase seeks a minimal-cost representative from the exponential set encoded in the saturated e-graph [1012.1802][2101.01332][2002.07951].

- **ILP-based extraction**: Introduces binary selection variables for e-nodes, with “one-per-class” and child-coverage constraints. Extensions incorporate dominance relations (for SSA/CFG reuse) [2502.17075].
- **Greedy/Dynamic Programming Extraction**: Linear-time, bottom-up passes compute per-class minimums for local cost models, but must correct for shared subexpression overcounts in general graphs [2410.05534].
- **Sketch-constrained extraction**: For domain-constrained optimization, only terms matching a user-supplied sketch are eligible for extraction [2111.13040].

Advanced variants address side effects, control flow, or multi-objective cost models [2502.17075][2505.09363].

## 5. Recent Extensions and Theoretical Developments

### 5.1. Generalizations: Monoidal and Categorical Semantics

Recent work axiomatizes e-graphs as morphisms in the free semilattice-enriched symmetric monoidal category (SLatt-enriched SMC), generalizing classical equality saturation to arbitrary monoidal settings (including quantum circuits, dataflow, and algebraic structures) [2406.15882]. Equivalence is managed by DPOI (double-pushout with interfaces) rewriting on e-hypergraphs: combinatorial structures whose nodes and edges capture both operator application and semilattice joins, and whose isomorphism class absorbs all structural monoidal equalities.

### 5.2. Handling Binding and Logical Cuts

Native support for variable binding—essential for λ-calculus and higher-order reasoning—is realized by modeling e-graphs as morphisms in the free SLatt-enriched closed symmetric monoidal category. This absorbs $\alpha$-equivalence and $\beta$-reduction structurally, avoiding explicit substitutions and De Bruijn indices [2505.00807]. Hierarchical hypergraphs encode the binding and equivalence hierarchy, and DPOI rewriting manipulates these structures soundly and completely with respect to term rewriting modulo SMC, semilattice, and binding laws.

### 5.3. Context-Sensitive and Conditional Reasoning

Conditional and context-sensitive rewrites (where rule acts only under certain assumptions or contexts) pose significant challenges. Colored E-graphs encode all color-indexed congruences as layered union-finds, enabling hundreds of simultaneous assumptions with only O(N + ∑U_c) space and computation [2305.19203].

## 6. Applications and Experimental Highlights

Equality saturation is central to many optimization and synthesis domains:

- **Compiler and IR optimization**: Classical [1012.1802], high-level Julia IR [2502.17075], eqsat dialects in MLIR [2505.09363], and compositional floating-point optimization [2004.03082].
- **Tensor graph superoptimization**: Neural network acceleration via comprehensive rule application and cost-based DAG extraction, with order-of-magnitude speed and quality gains [2101.01332][2410.05534].
- **Linear algebra and relational query plans**: SystemML/SPORES optimizes linear algebra by relational encoding and saturation, discovering all known rewrites and novel optimizations with 1.2×–5× speedups [2002.07951].
- **Boolean and hardware reasoning**: Symbolic reasoning over gate-level netlists via domain-specific Boolean rule sets, extracting circuits with maximal high-level structure (full adders, etc.) and enormous verification speedups [2504.05577].
- **Automated idiom detection**: Minimalist array languages with only a handful of core rewrite rules, latent idiom rules, and equality saturation automatically recognize patterns and map them to highly optimized library calls, delivering up to 20× speedup on matrix multiplications [2312.17682].
- **Learning and inference**: RL-guided rule selection and hybrid e-graph/LLM workflow for combinatorial optimization [2407.12794][2511.00403].
- **Rule inference**: Using equality saturation itself to synthesize concise, high-coverage rewrite rule sets [2108.10436].

## 7. Theoretical Foundations, Complexity, and Termination

Equality saturation’s fixpoint semantics is formalized via deterministic tree automata, providing a clean universal model for the set of equalities and justifying the correctness and convergence of saturation [2501.02413]. There are deep connections to the database chase; termination of equality saturation is RE-complete (single instance), Π₂-complete (for all terms), and undecidable for arbitrary e-graph instances. Syntactic acyclicity of rules (i.e., weak term acyclicity) gives a polynomial-time guarantee of convergence, providing a practical criterion for safe use in optimizers, theorem provers, and program analyzers.

Space complexity is bounded by O(N + ∑U_c) for colored e-graphs versus O(C·N) for naively forking per assumption. Batched congruence-closure and hash-consing ensure near-linear time per operation in practice [2305.19203][2004.03082]. Greedy extraction is linear in the number of e-nodes if subexpression sharing is correctly handled; ILP extraction is NP-hard but tractable for moderate sized e-graphs.

---

**References:**
- [1012.1802] Equality Saturation: A New Approach to Optimization
- [2004.03082] egg: Fast and Extensible Equality Saturation
- [2501.02413] Semantic foundations of equality saturation
- [2312.17682] Latent Idiom Recognition for a Minimalist Functional Array Language using Equality Saturation
- [2101.01332] Equality Saturation for Tensor Graph Superoptimization
- [2410.05534] Optimizing Tensor Computation Graphs with Equality Saturation and Monte Carlo Tree Search
- [2502.17075] Equality Saturation for Optimizing High-Level Julia IR
- [2305.19203] Colored E-Graph: Equality Reasoning with Conditions
- [2406.15882] Equivalence Hypergraphs: DPO Rewriting for Monoidal E-Graphs
- [2505.00807] E-Graphs With Bindings
- [2505.09363] eqsat: An Equality Saturation Dialect for Non-destructive Rewriting
- [2002.07951] SPORES: Sum-Product Optimization via Relational Equality Saturation for Large Scale Linear Algebra
- [2108.10436] Rewrite Rule Inference Using Equality Saturation
- [2111.13040] Sketch-Guided Equality Saturation: Scaling Equality Saturation to Complex Optimizations of Functional Programs
- [2407.12794] Learned Graph Rewriting with Equality Saturation: A New Paradigm in Relational Query Rewrite and Beyond
- [2504.05577] BoolE: Exact Symbolic Reasoning via Boolean Equality Saturation
- [2511.00403] Equality Saturation Guided by Large Language Models

Source: https://www.emergentmind.com/topics/equality-saturation