Slotted e-graphs¶
Laws that cannot be oriented are decided by equality saturation over a slotted e-graph, whose classes abstract free variables into slots, followed by least-cost extraction (Equations and normalization §6).
egraph
¶
Equality saturation with slotted e-graphs for laws that cannot be oriented.
Associativity and commutativity have no terminating orientation, so a theory sends such laws to equality saturation [@willsey2021egg]: an e-graph holds a set of terms closed under congruence, each law is applied in both directions until nothing new is learned or a limit is reached, and a term of least cost is extracted from the class of the input.
Terms with binders are stored in a slotted e-graph [@schneider2025slotted]. An e-class abstracts the free variables of its terms into slots, so terms that differ only by a renaming of their free variables, such as the body of one abstraction placed at two depths, share one class; a reference to a class instantiates its slots with variables. A class also records the slot permutations it is invariant under, so commutativity is a symmetry of one class rather than two classes. De Bruijn indices make alpha-equivalent terms identical before any of this.
The backend applies a law l = r when it is regular and renaming:
both sides are in Miller's pattern fragment, with every metavariable
applied to bound variables only, and they use the same metavariables.
Instantiating such a law renames and never substitutes a term, which is
exactly what slots express; the theory refuses other laws for this
backend and names the property they lack. A data metavariable binds the
data leaf at its position, and the other side puts that leaf back.
VARIABLE_CLASS
module-attribute
¶
The class a variable node's single child points at: the variable itself.
A variable node BoundVar has one child whose one slot image is the
variable's index, so a variable's free variable is computed like any other
node's and the variable class has one slot.
MetaBinding
¶
MetaBinding = tuple[Ref, int] | str
What a law's metavariable is bound to while it is applied.
A metavariable at a term position is bound to a class, its slot images in the redex context extended by the metavariable's arity, with the arity; a data metavariable is bound to the key of its data leaf.
SaturationError
¶
Bases: ValueError
A law is outside the saturation backend, or saturation hit a limit.
Ref
didactic-model
¶
Bases: Model
A class instantiated at variables.
| PARAMETER | DESCRIPTION |
|---|---|
eclass
|
The class.
TYPE:
|
slots
|
The variable each slot of the class stands for, as an index in the context of the reference.
TYPE:
|
Child
didactic-model
¶
Child(*, ref: Ref | None = None, bound: int = 0, leaf: str = '')
Bases: Model
One argument of an e-node.
| PARAMETER | DESCRIPTION |
|---|---|
ref
|
The argument's class, with slot images in the argument's context
(the node's context extended by
TYPE:
|
bound
|
The variables the argument binds.
TYPE:
|
leaf
|
The data leaf, rendered with its type, when
TYPE:
|
ENode
didactic-model
¶
ENode(*, op: str, children: tuple[Child, ...] = ())
Bases: Model
An operator applied to class references: one way to build a class.
| PARAMETER | DESCRIPTION |
|---|---|
op
|
The constructor, or
TYPE:
|
children
|
Its arguments.
TYPE:
|
Leaf
didactic-model
¶
Leaf(*, value: DataLeaf = field(opaque=True, extras=OPAQUE_BY_VALUE))
Bases: ValueModel
A data leaf kept with its value for extraction.
| PARAMETER | DESCRIPTION |
|---|---|
value
|
The value.
TYPE:
|
EGraph
¶
EGraph(signature: Signature)
A slotted e-graph over the terms of a signature.
| PARAMETER | DESCRIPTION |
|---|---|
signature
|
The signature.
TYPE:
|
Source code in src/quivers/structural/egraph.py
find
¶
The canonical reference equal to a reference.
| PARAMETER | DESCRIPTION |
|---|---|
ref
|
The reference.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Ref
|
A reference to the root class, slots composed along the path. |
Source code in src/quivers/structural/egraph.py
same
¶
Whether two references denote equal terms.
| PARAMETER | DESCRIPTION |
|---|---|
left
|
The references.
TYPE:
|
right
|
The references.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
Source code in src/quivers/structural/egraph.py
add_node
¶
Add a node and return the reference that denotes it.
| PARAMETER | DESCRIPTION |
|---|---|
node
|
The node, with slot images in its context.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Ref
|
Its class instantiated at the node's free variables. |
Source code in src/quivers/structural/egraph.py
add
¶
Add a term and return its reference.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Ref
|
The term's class, its slots at the term's free variables. |
Source code in src/quivers/structural/egraph.py
variable
¶
variable(index: int) -> Ref
The reference of a variable.
| PARAMETER | DESCRIPTION |
|---|---|
index
|
The de Bruijn index.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Ref
|
The one-slot variable class at that index. |
Source code in src/quivers/structural/egraph.py
union
¶
Record that two references denote equal terms.
| PARAMETER | DESCRIPTION |
|---|---|
left
|
References in one context, with the same free variables.
TYPE:
|
right
|
References in one context, with the same free variables.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
Whether anything new was learned. |
| RAISES | DESCRIPTION |
|---|---|
SaturationError
|
If the two have different free variables, which a regular law never produces. |
Source code in src/quivers/structural/egraph.py
rebuild
¶
Restore congruence: equal children make equal parents.
Every node is re-canonicalized against the current classes, and two nodes with one canonical form are unified, until nothing changes; the node tables and the memo are then rebuilt.
Source code in src/quivers/structural/egraph.py
nodes
¶
The nodes of a reference's class, instantiated at its slots.
Every symmetric orientation of a node is produced, so matching is complete modulo the class's symmetries.
| PARAMETER | DESCRIPTION |
|---|---|
ref
|
The reference.
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
ENode
|
Each node, its children's slot images in the reference's context. |
Source code in src/quivers/structural/egraph.py
ematch
¶
ematch(pattern: PatternArgument, ref: Ref, depth: int, binding: dict[str, MetaBinding]) -> Iterator[dict[str, MetaBinding]]
Match a renaming pattern against a class.
| PARAMETER | DESCRIPTION |
|---|---|
pattern
|
The pattern.
TYPE:
|
ref
|
The class, its slot images at the pattern's depth.
TYPE:
|
depth
|
The number of pattern binders around it.
TYPE:
|
binding
|
Each metavariable's class, its slot images in the redex
context extended by the metavariable's arity, with the arity;
a data metavariable's leaf key (
TYPE:
|
| YIELDS | DESCRIPTION |
|---|---|
dict
|
Each extended binding. |
Source code in src/quivers/structural/egraph.py
build
¶
build(pattern: PatternArgument, binding: Mapping[str, MetaBinding], depth: int) -> Ref
Add the instance of a renaming pattern.
| PARAMETER | DESCRIPTION |
|---|---|
pattern
|
The pattern.
TYPE:
|
binding
|
The metavariables' classes and data metavariables' leaf keys.
TYPE:
|
depth
|
The pattern binders around it.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Ref
|
The instance, its slots in the redex context at |
| RAISES | DESCRIPTION |
|---|---|
SaturationError
|
If a metavariable is unbound or applied to a non-variable. |
Source code in src/quivers/structural/egraph.py
roots
¶
roots() -> list[Ref]
A reference to every root class at its own slots.
| RETURNS | DESCRIPTION |
|---|---|
list of Ref
|
The classes. |
Source code in src/quivers/structural/egraph.py
size
¶
The number of nodes.
| RETURNS | DESCRIPTION |
|---|---|
int
|
The node count over root classes. |
CostModel
didactic-model
¶
Bases: Model
The cost of a term for extraction: a weight per constructor.
| PARAMETER | DESCRIPTION |
|---|---|
kind
|
TYPE:
|
weights
|
Per-constructor weights; absent constructors weigh one.
TYPE:
|
combine
¶
The cost of a node from its children's costs.
| PARAMETER | DESCRIPTION |
|---|---|
op
|
The constructor.
TYPE:
|
children
|
The children's costs.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
float
|
The node's cost. |
| RAISES | DESCRIPTION |
|---|---|
SaturationError
|
If the kind is unknown. |
Source code in src/quivers/structural/egraph.py
SaturationResult
didactic-model
¶
Bases: ValueModel
What a saturation run reached.
| PARAMETER | DESCRIPTION |
|---|---|
saturated
|
Whether a pass found nothing new, so the e-graph is closed under the laws.
TYPE:
|
iterations
|
The passes run.
TYPE:
|
nodes
|
The final node count.
TYPE:
|
Saturation
¶
Saturation(signature: Signature, laws: Sequence[Equation], cost: CostModel, max_iterations: int = 30, max_nodes: int = 20000)
Equality saturation for a signature's renaming laws.
| PARAMETER | DESCRIPTION |
|---|---|
signature
|
The signature.
TYPE:
|
laws
|
The laws, each checked by
TYPE:
|
cost
|
The extraction cost.
TYPE:
|
max_iterations
|
The passes allowed.
TYPE:
|
max_nodes
|
The node budget.
TYPE:
|
Source code in src/quivers/structural/egraph.py
saturate
¶
saturate(graph: EGraph) -> SaturationResult
Apply every law in both directions until nothing changes.
| PARAMETER | DESCRIPTION |
|---|---|
graph
|
The e-graph, extended in place.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
SaturationResult
|
Whether it saturated within the limits. |
Source code in src/quivers/structural/egraph.py
extract
¶
The least-cost term of a class.
| PARAMETER | DESCRIPTION |
|---|---|
graph
|
The e-graph.
TYPE:
|
ref
|
The class, instantiated.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Term
|
The cheapest term under the cost model. |
| RAISES | DESCRIPTION |
|---|---|
SaturationError
|
If the class has no finite term. |
Source code in src/quivers/structural/egraph.py
normalize
¶
normalize(term: Term) -> tuple[Term, SaturationResult]
The least-cost term equal to a term under the laws.
| PARAMETER | DESCRIPTION |
|---|---|
term
|
The term.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tuple of Term and SaturationResult
|
The extracted term and how saturation went. |
Source code in src/quivers/structural/egraph.py
equal_modulo
¶
Whether two terms are equal under the laws.
| PARAMETER | DESCRIPTION |
|---|---|
left
|
The terms.
TYPE:
|
right
|
The terms.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
|
| RAISES | DESCRIPTION |
|---|---|
SaturationError
|
If saturation stops at a limit before deciding. |
Source code in src/quivers/structural/egraph.py
check_renaming_law
¶
Require a law the saturation backend can apply.
Both sides use the same metavariables (the law is regular), so equal terms have the same free variables. Each side is either in Miller's pattern fragment, with every metavariable applied to distinct bound variables, or a bare metavariable; a law is applied only from a side that is a constructor application, and at least one side is.
| PARAMETER | DESCRIPTION |
|---|---|
law
|
The law.
TYPE:
|
signature
|
The signature.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
SaturationError
|
If the sides use different metavariables, both sides are bare metavariables, or a side is outside the pattern fragment; the message names the law. |