TL;DR
Ruler is a new framework that uses equality saturation to automatically discover rewrite rules for any given programming language or mathematical domain. By using e-graphs to represent terms and rules, Ruler synthesizes rulesets that are 5.8x smaller and 25x faster to generate than state-of-the-art tools, even uncovering missing rules that fixed a long-standing bug in the Herbie numerical optimizer.
What Problem Does This Paper Address?
Rewrite rules (e.g., $x + 0 \to x$ or $x \times 2 \to x + x$) are the bread and butter of modern compilers, theorem provers, and program synthesizers. They allow tools to simplify complex expressions into more efficient or canonical forms.
However, building a good ruleset is a nightmare for developers. If you miss a rule, your compiler might miss an optimization. If you add a subtly incorrect rule, you introduce bugs. If you add too many redundant rules, your tool slows to a crawl. Traditionally, these rulesets are hand-crafted by domain experts through tedious trial and error. While automated synthesis tools exist, they often struggle with "term explosion"—the number of possible expressions grows exponentially with size—and frequently produce bloated, redundant rulesets.
Methods
The authors introduce Ruler, a domain-general framework for rewrite rule inference. Ruler follows a three-step iterative workflow:
- Enumerate terms from a grammar.
- Generate candidate rules by finding equivalent terms.
- Select a minimal, high-quality subset of rules to keep.
The "secret sauce" is the use of equality saturation and e-graphs (a data structure for compactly representing many equivalent ways to write an expression) during the inference process itself.
Ruler uses equality saturation to:
- Shrink the term space: If two terms are already proven equivalent by existing rules, Ruler treats them as the same entity. This prevents the search space from exploding.
- Filter candidates: Before testing a new candidate rule, Ruler checks if it can already be derived from the rules it has already learned. If it can, the candidate is discarded as redundant.
- Validation: Ruler supports multiple backends to verify rules, including SMT solvers (like Z3), model checking for small domains, and fuzzing (random testing) for complex ones.
To speed up finding equivalent terms, Ruler uses Characteristic Vectors (cvecs). It evaluates terms on a set of inputs to create a "fingerprint." If two terms have different fingerprints, they can't be equivalent, allowing Ruler to skip expensive formal verification for most pairs.
Datasets
The authors evaluated Ruler across several representative domains:
- Booleans: A small, finite domain.
- Bitvectors (4-bit and 32-bit): Used to test scalability and SMT-based verification.
- Rationals: An infinite domain used for a real-world case study with the Herbie tool.
Ruler was compared against the rule synthesizer built into the CVC4 theorem prover, a state-of-the-art baseline.
Key Insights and Ideas
- EqSat for Inference: The core "aha moment" is that equality saturation—usually used to apply rules—is incredibly effective at finding them. It naturally handles the redundancy that plagues other synthesis methods.
- Proving Power over Quantity: The authors emphasize that a smaller ruleset is often better if it has the same "deriving ratio" (the ability to prove the same set of equalities) as a larger one.
- "Soundiness" through Crashes: Interestingly, the authors found that if an unsound (incorrect) rule is added to an e-graph, it often causes a "collision" between distinct constants (like proving $0=1$). Ruler uses these crashes as a fast way to detect and reject bad rules during fuzzing.
- Fixing Expert Systems: In the rational number domain, Ruler found two rules involving absolute values ($|x| \times |x| \to x \times x$) that human experts had missed for six years, which directly resolved a known bug in Herbie.
Key Results
- Efficiency: Compared to CVC4, Ruler is 25x faster on average.
- Compactness: Ruler produces rulesets that are 5.8x smaller than CVC4's while maintaining or exceeding the same proving power.
- Scalability: In the
bv32(32-bit bitvector) domain, Ruler synthesized a ruleset in ~10 minutes that CVC4 took nearly 20 minutes to produce, with Ruler's set being significantly more concise (188 rules vs 1,782 rules). - Real-world Impact: Ruler-generated rules for rational arithmetic performed as well as expert-crafted rules in Herbie and improved Herbie's ability to simplify expressions without increasing its runtime.
Limitations and Future Work
- Alpha-Equivalence: While Ruler eliminates redundant rules, it doesn't yet eliminate $\alpha$-equivalent terms (terms that are the same up to variable renaming) during enumeration, which could further speed up the search.
- Side Conditions: Currently, Ruler focuses on unconditional rewrites. It cannot yet infer rules that require preconditions, such as "this rule only applies if $x$ is non-negative."
- Stochastic Search: The current prototype uses complete enumeration. The authors suggest that stochastic (randomized) enumeration could help Ruler scale to even larger expression depths.
- Heuristics: The quality of the final ruleset depends on a syntactic heuristic (e.g., preferring rules with fewer operators). Exploring more sophisticated ways to score "rule quality" is a promising direction.
Summary updated