pub struct EGraph<Cfg: EGraphConfig, L: LitVal, const TRACK: bool = true, const PROOFS: bool = false> { /* private fields */ }Implementations§
Source§impl<Cfg: EGraphConfig, L: LitVal, const TRACK: bool, const PROOFS: bool> EGraph<Cfg, L, TRACK, PROOFS>
impl<Cfg: EGraphConfig, L: LitVal, const TRACK: bool, const PROOFS: bool> EGraph<Cfg, L, TRACK, PROOFS>
Sourcepub fn cc_basis_report(&self) -> BasisReport<Cfg::G, Cfg::O, Cfg::M>
pub fn cc_basis_report(&self) -> BasisReport<Cfg::G, Cfg::O, Cfg::M>
Compute the current active AC rule set and check the reduced-basis invariants. Pure read; safe to call any time. Diagnostic operation that rescans all nodes.
Sourcepub fn cc_min_used_nonminimal(&self) -> (usize, Vec<(Cfg::G, Cfg::O)>)
pub fn cc_min_used_nonminimal(&self) -> (usize, Vec<(Cfg::G, Cfg::O)>)
GROUND-TRUTH check 1: is the RHS each rule actually uses the true minimal monomial?
cc_basis_report reads rhs exactly as completion does (class_rhs_into: {class}
if atomic, else the stored min_monomial). Here we instead brute-force, per (class, op),
the monomial_cmp-least monomial over all active AC nodes of that op in the class,
and compare. A mismatch means the RHS completion used was not minimal at the point
of use (the §1.4 best-effort gap, or, more seriously, the single-op-slot conflating two
AC ops, §9b axis-1). Returns the count of rules whose used RHS is non-minimal, and the
worst offenders. Op-aware: the true min is taken only over same-op nodes.
Sourcepub fn cc_not_kapur_reduced(&self) -> (usize, usize)
pub fn cc_not_kapur_reduced(&self) -> (usize, usize)
Finite-state diagnostic 2: is the active rule set fully reduced in Kapur’s sense
(§3), rather than only a direct-containment antichain? Kapur-reduced means that neither
side of any rule can be rewritten by the other rules. reducible_pairs only catches
direct sub-multiset containment; this computes multi-step normalization in the current
finite rule set. It is not a theorem about every reachable state.
Returns (n_lhs_reducible, n_rhs_reducible). The .egg basis gate currently asserts
only that the LHS count is zero; callers that require full reducedness must check both.
Sourcepub fn cc_axiom_cps_nonjoinable(&self) -> (usize, Vec<(Cfg::G, Cfg::O)>)
pub fn cc_axiom_cps_nonjoinable(&self) -> (usize, Vec<(Cfg::G, Cfg::O)>)
Ground-truth check 3 (Kapur §4): every per-rule axiom critical pair of an idempotent or nilpotent op is joinable under the current state.
- Idempotent (Lemma 4.1(ii)): for a rule
f(M) → f(N)and eacha ∈ M, the pair(f(N ⊎ {a}), f(N))must join. - Nilpotent order n (Lemma 4.2(ii)/4.5): for each
awith multiplicityminM, the pair(f(N ⊎ {a: n−m}), f(M − {a: m}))must join.
“Join” is checked at the e-graph level, where Kapur’s constant rules live in the union-find: the two reducts join iff their normal forms coincide as multisets, or both resolve (lookup-only, no mutation) to the same class. A reduct that neither matches nor resolves was never materialized — the completion missed the axiom superposition. Rules are read over BOTH completion partitions (MSet and Set). Returns the non-joinable pair count and up to 20 (rule node, op) witnesses.
Sourcepub fn cc_basis_dump(&self, tag: &str)
pub fn cc_basis_dump(&self, tag: &str)
Print the basis report (one line per rule + the invariant verdicts). tag labels
the call site (e.g. a round number). This operation rescans all nodes.
Source§impl<Cfg: EGraphConfig, L: LitVal, const TRACK: bool, const PROOFS: bool> EGraph<Cfg, L, TRACK, PROOFS>
impl<Cfg: EGraphConfig, L: LitVal, const TRACK: bool, const PROOFS: bool> EGraph<Cfg, L, TRACK, PROOFS>
pub fn new() -> Self
Sourcepub fn completion_outcome(&self) -> Option<CompletionOutcome>
pub fn completion_outcome(&self) -> Option<CompletionOutcome>
Outcome of the most recent rebuild when cc is enabled. None before the first
rebuild. Callers can use this to distinguish convergence from a growth-budget abort.
Sourcepub fn set_completion_node_budget(&mut self, budget: usize)
pub fn set_completion_node_budget(&mut self, budget: usize)
Set the node-growth budget for one completion-enabled rebuild (default
DEFAULT_COMPLETION_NODE_BUDGET). Exceeding it aborts completion soundly and
records CompletionOutcome::AbortedGrowthLimit. A config knob (like
set_cc), not semi-persistent state.
Sourcepub fn set_cc(&mut self, enabled: bool)
pub fn set_cc(&mut self, enabled: bool)
Enable or disable the AC congruence-completion pass in rebuild (default off).
See the cc field docs and doc/future/ac-completion-limitations.md §1.
Sourcepub fn set_union_by(&mut self, u: UnionBy)
pub fn set_union_by(&mut self, u: UnionBy)
Select the merge survivor policy (see UnionBy).
Sourcepub fn set_cc_goal(&mut self, goal: Option<(Cfg::G, Cfg::G)>)
pub fn set_cc_goal(&mut self, goal: Option<(Cfg::G, Cfg::G)>)
Set or clear the completion goal pair (see the cc_goal field). With a
goal set, rebuild under completion returns
CompletionOutcome::GoalMet as soon as the pair joins, without
finishing the closure.
Sourcepub fn set_basis_checks(&mut self, enabled: bool)
pub fn set_basis_checks(&mut self, enabled: bool)
Enable or disable the AC reduced-basis invariant checks in rebuild (default off,
seeded from the AC_BASIS_DUMP env var). When on (and cc is on), each
completion round prints cc_basis_dump, which runs the ground-truth checkers
(min_monomial minimality, Kapur-reducedness). These are superlinear brute-force checks,
so leave this off outside diagnosis and tests. See ac_invariants.rs.
Sourcepub fn from_model(model: &impl LitModel<Value = L>) -> Self
pub fn from_model(model: &impl LitModel<Value = L>) -> Self
Create an e-graph with built-in sorts and reserved op names from a LitModel.
Sourcepub fn with_registries(
sorts: SortRegistry<Cfg::S, TRACK>,
ops: OpRegistry<Cfg::O, Cfg::S, TRACK>,
) -> Self
pub fn with_registries( sorts: SortRegistry<Cfg::S, TRACK>, ops: OpRegistry<Cfg::O, Cfg::S, TRACK>, ) -> Self
Create an e-graph with pre-built registries (from sortcheck).
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
Sourcepub fn node_ids(&self) -> impl Iterator<Item = Cfg::G> + '_
pub fn node_ids(&self) -> impl Iterator<Item = Cfg::G> + '_
Every node id in the store, ascending.
The one place a full scan should get its ids from. The obvious spelling —
for i in 0..eg.len() { Cfg::G::from_usize(i) } — is sound but not locally
justified: Cfg::G::Index spans twice the id space for a bit-stealing id, and
from_usize rejects positions outside the smaller payload range. What keeps the
routing table inside that range is that TypedRouting::reserve mints every entry
through try_new, three modules away. Stating that once here means a new scan
inherits the argument instead of re-deriving it.
pub fn sorts(&self) -> &SortRegistry<Cfg::S, TRACK>
pub fn sorts_mut(&mut self) -> &mut SortRegistry<Cfg::S, TRACK>
pub fn ops(&self) -> &OpRegistry<Cfg::O, Cfg::S, TRACK>
pub fn ops_mut(&mut self) -> &mut OpRegistry<Cfg::O, Cfg::S, TRACK>
pub fn lits(&self) -> &LitValStore<L, Cfg::V, TRACK>
pub fn lits_mut(&mut self) -> &mut LitValStore<L, Cfg::V, TRACK>
pub fn rules(&self) -> &RuleRegistry<TRACK>
pub fn register_rule(&mut self, name: &str, lhs: &str, rhs: &str) -> RuleId
pub fn axioms(&self) -> &AxiomRegistry<Cfg::G, TRACK>
pub fn register_axiom( &mut self, name: &str, lhs: Cfg::G, rhs: Cfg::G, ) -> AxiomId
pub fn intern_sort(&mut self, name: &str) -> Cfg::S
pub fn sort_name(&self, id: Cfg::S) -> &str
pub fn register_op0(&mut self, name: &str, ret: Cfg::S) -> Cfg::O
pub fn register_op1(&mut self, name: &str, a: Cfg::S, ret: Cfg::S) -> Cfg::O
pub fn register_op2( &mut self, name: &str, a: Cfg::S, b: Cfg::S, ret: Cfg::S, ) -> Cfg::O
pub fn register_op3( &mut self, name: &str, a: Cfg::S, b: Cfg::S, c: Cfg::S, ret: Cfg::S, ) -> Cfg::O
pub fn register_opn( &mut self, name: &str, args: &[Cfg::S], ret: Cfg::S, ) -> Cfg::O
pub fn register_c( &mut self, name: &str, arg_sorts: [Cfg::S; 2], ret: Cfg::S, ) -> Cfg::O
Sourcepub fn register_kind_meta(
&mut self,
name: &str,
ret: Cfg::S,
kind: OpKind<Cfg::S>,
meta: OpMeta,
) -> Cfg::O
pub fn register_kind_meta( &mut self, name: &str, ret: Cfg::S, kind: OpKind<Cfg::S>, meta: OpMeta, ) -> Cfg::O
Register an op from a resolved kind plus its non-algebraic metadata (constructor-ness,
:cost, :unextractable). The surface layer’s single registration entry point — see
crate::registry::OpRegistry::register_kind_meta.
pub fn register_a( &mut self, name: &str, arg: Cfg::S, ret: Cfg::S, dir: AssocDir, ) -> Cfg::O
pub fn register_mset(&mut self, name: &str, arg: Cfg::S, ret: Cfg::S) -> Cfg::O
pub fn register_set(&mut self, name: &str, arg: Cfg::S, ret: Cfg::S) -> Cfg::O
Sourcepub fn register_kind(
&mut self,
name: &str,
ret: Cfg::S,
kind: OpKind<Cfg::S>,
) -> Cfg::O
pub fn register_kind( &mut self, name: &str, ret: Cfg::S, kind: OpKind<Cfg::S>, ) -> Cfg::O
Register an op from a fully-resolved OpKind (the property-tag resolver in sortcheck).
Sourcepub fn set_unit_node(&mut self, op: Cfg::O, unit: Cfg::G)
pub fn set_unit_node(&mut self, op: Cfg::O, unit: Cfg::G)
Record op’s identity (unit) element node (x ∘ e = x; the unit drops from monomials).
Called by the resolver in sortcheck after it builds the :identity term to a node.
Sourcepub fn unit_node(&self, op: Cfg::O) -> Option<Cfg::G>
pub fn unit_node(&self, op: Cfg::O) -> Option<Cfg::G>
The identity (unit) element node of op, or None if op has no declared identity.
pub fn set_inverse_op(&mut self, op: Cfg::O, inv: Cfg::O)
Sourcepub fn inverse_op(&self, op: Cfg::O) -> Option<Cfg::O>
pub fn inverse_op(&self, op: Cfg::O) -> Option<Cfg::O>
The group inverse operator of op (:inverse neg), or None if none declared.
pub fn register_lit(&mut self, name: &str, ret: Cfg::S) -> Cfg::O
pub fn op(&self, name: &str) -> Option<Cfg::O>
pub fn intern_lit(&mut self, value: L) -> Cfg::V
Sourcepub fn run_query(
&self,
plan: &QueryPlan<Cfg::O, Cfg::Index, L>,
) -> Vec<Match<Cfg>>
pub fn run_query( &self, plan: &QueryPlan<Cfg::O, Cfg::Index, L>, ) -> Vec<Match<Cfg>>
Run a compiled query plan against this e-graph.
Sourcepub fn saturate<M: LitModel<Value = L>, S: DenseId + Copy>(
&mut self,
rules: &[PreparedRule<Cfg::O, S, L>],
model: &M,
limit: usize,
globals: &GlobalCtx<S, Cfg::G>,
) -> SatResult
pub fn saturate<M: LitModel<Value = L>, S: DenseId + Copy>( &mut self, rules: &[PreparedRule<Cfg::O, S, L>], model: &M, limit: usize, globals: &GlobalCtx<S, Cfg::G>, ) -> SatResult
Saturate: apply rules to fixpoint or until limit iterations.
Sourcepub fn saturate_semi<M: LitModel<Value = L>, S: DenseId + Copy>(
&mut self,
rules: &[PreparedRule<Cfg::O, S, L>],
model: &M,
limit: usize,
globals: &GlobalCtx<S, Cfg::G>,
) -> SatResult
pub fn saturate_semi<M: LitModel<Value = L>, S: DenseId + Copy>( &mut self, rules: &[PreparedRule<Cfg::O, S, L>], model: &M, limit: usize, globals: &GlobalCtx<S, Cfg::G>, ) -> SatResult
Semi-naive saturation via delta variants. The driver does not switch wholesale to naive; rules without safe delta coverage use a full-index match.
Sourcepub fn saturate_spec<M: LitModel<Value = L>, S: DenseId + Copy>(
&mut self,
rules: &[PreparedRule<Cfg::O, S, L>],
model: &M,
spec: &RunSpec<Cfg::G>,
globals: &GlobalCtx<S, Cfg::G>,
) -> SatResult
pub fn saturate_spec<M: LitModel<Value = L>, S: DenseId + Copy>( &mut self, rules: &[PreparedRule<Cfg::O, S, L>], model: &M, spec: &RunSpec<Cfg::G>, globals: &GlobalCtx<S, Cfg::G>, ) -> SatResult
Saturate under a full RunSpec — budget, ruleset, and
:until goal. The interpreter’s (run …) path.
Sourcepub fn saturate_semi_spec<M: LitModel<Value = L>, S: DenseId + Copy>(
&mut self,
rules: &[PreparedRule<Cfg::O, S, L>],
model: &M,
spec: &RunSpec<Cfg::G>,
globals: &GlobalCtx<S, Cfg::G>,
) -> SatResult
pub fn saturate_semi_spec<M: LitModel<Value = L>, S: DenseId + Copy>( &mut self, rules: &[PreparedRule<Cfg::O, S, L>], model: &M, spec: &RunSpec<Cfg::G>, globals: &GlobalCtx<S, Cfg::G>, ) -> SatResult
Semi-naive counterpart of saturate_spec.
Sourcepub fn saturate_spec_in<M: LitModel<Value = L>, S: DenseId + Copy>(
&mut self,
rules: &[PreparedRule<Cfg::O, S, L>],
model: &M,
spec: &RunSpec<Cfg::G>,
globals: &GlobalCtx<S, Cfg::G>,
scratch: &mut IndexScratch<Cfg>,
) -> SatResult
pub fn saturate_spec_in<M: LitModel<Value = L>, S: DenseId + Copy>( &mut self, rules: &[PreparedRule<Cfg::O, S, L>], model: &M, spec: &RunSpec<Cfg::G>, globals: &GlobalCtx<S, Cfg::G>, scratch: &mut IndexScratch<Cfg>, ) -> SatResult
saturate_spec over a caller-owned index scratch,
so its span arenas survive the call. See
saturate_spec_in.
Sourcepub fn saturate_semi_spec_in<M: LitModel<Value = L>, S: DenseId + Copy>(
&mut self,
rules: &[PreparedRule<Cfg::O, S, L>],
model: &M,
spec: &RunSpec<Cfg::G>,
globals: &GlobalCtx<S, Cfg::G>,
scratch: &mut IndexScratch<Cfg>,
) -> SatResult
pub fn saturate_semi_spec_in<M: LitModel<Value = L>, S: DenseId + Copy>( &mut self, rules: &[PreparedRule<Cfg::O, S, L>], model: &M, spec: &RunSpec<Cfg::G>, globals: &GlobalCtx<S, Cfg::G>, scratch: &mut IndexScratch<Cfg>, ) -> SatResult
Semi-naive counterpart of saturate_spec_in.
Sourcepub fn class_count(&self) -> usize
pub fn class_count(&self) -> usize
The number of distinct e-classes over the current node set. A scan, not a maintained
counter: (print-size) / (print-stats) ask for it a handful of times per program.
Sourcepub fn op_node_counts(&self) -> Vec<usize>
pub fn op_node_counts(&self) -> Vec<usize>
Node counts per operator, indexed by op id (ops with no nodes read 0).
pub fn find(&mut self, x: Cfg::G) -> Cfg::G
pub fn find_const(&self, x: Cfg::G) -> Cfg::G
pub fn add(&mut self, op: Cfg::O, children: &[Cfg::G]) -> Cfg::G
pub fn add_lit(&mut self, op: Cfg::O, lit: Cfg::V) -> Cfg::G
Sourcepub fn build_ground_cterm(&mut self, ct: &CTerm<Cfg::O, Cfg::S, L>) -> Cfg::G
pub fn build_ground_cterm(&mut self, ct: &CTerm<Cfg::O, Cfg::S, L>) -> Cfg::G
Build a ground checked term (a literal or a constructor applied to ground args) into a
node, returning its id. Mirrors the interpreter’s build_cterm for the ground cases; it
has no access to globals, so a CTerm::Global is unreachable here (a unit is always
ground) and panics. Used by the property-tag resolver to materialize an op’s :identity
unit at registration.
pub fn merge(&mut self, a: Cfg::G, b: Cfg::G) -> Option<(Cfg::G, Cfg::G)>
pub fn merge_justified( &mut self, a: Cfg::G, b: Cfg::G, just: Justification<Cfg::G>, ) -> Option<(Cfg::G, Cfg::G)>
pub fn explain(&self, a: Cfg::G, b: Cfg::G, buf: &mut ProofBuf<Cfg::G>) -> bool
Sourcepub fn dump_all_proofs<W: Write>(&self, out: W) -> Result<ProofDumpStats>
pub fn dump_all_proofs<W: Write>(&self, out: W) -> Result<ProofDumpStats>
Write a deterministic, line-oriented proof-path record for every e-node.
Each record names the node’s current operator, children, representative, proof-tree LCA, and every justified edge from the node to that representative. One Euler-tour table is built in O(n), then each LCA is queried in O(1); writing the proof paths costs O(number of emitted steps). Representative records have an empty reflexive proof.
The dump is available only when PROOFS = true. Any successful
justified merge or restore invalidates the table, so the complete build
and write happen under this single immutable borrow.
Sourcepub fn explain_deep(
&self,
a: Cfg::G,
b: Cfg::G,
buf: &mut ProofBuf<Cfg::G>,
) -> bool
pub fn explain_deep( &self, a: Cfg::G, b: Cfg::G, buf: &mut ProofBuf<Cfg::G>, ) -> bool
Deep explanation: expand Congruence justifications into child-pair proofs.
Appends all steps (including recursive child explanations) to buf.steps.
Uses an iterative worklist — no recursion.
For C/AC/ACI nodes, children are matched by canonical representative (group-by-find), not by position. This avoids combinatorial search.
Sourcepub fn rebuild(&mut self)
pub fn rebuild(&mut self)
Rebuild to a plain-congruence-closed state.
Plain congruence closure always runs. AC/ACI completion is opt-in through
set_cc; it is disabled by default. When enabled, two
closures are interleaved (see doc/design/ac-congruence-completeness.md
section 8, rebuild = Kapur’s Algorithm 3):
rebuild_congruence: ordinary worklist-driven congruence closure (substitutes equal atoms into recanonicalized nodes);cc_round: AC completion (substitutes equal sub-sums), which canonization alone misses.
A completion round may push new merges onto the worklist; we drain them with
another congruence pass, then complete again, until a whole completion round
adds nothing. Only CompletionOutcome::Converged reports that joint
fixpoint. CompletionOutcome::Disabled, CompletionOutcome::GoalMet,
and CompletionOutcome::AbortedGrowthLimit guarantee plain congruence
closure but not complete AC/ACI congruence closure. Inspect
completion_outcome after each call.
pub fn mark(&mut self, shrink: ShrinkPolicy) -> EGraphToken
pub fn restore(&mut self, token: EGraphToken)
Sourcepub fn touched(&self) -> &[Cfg::G]
pub fn touched(&self) -> &[Cfg::G]
Semi-naive: node ids created or recanonicalized since the last
clear_touched (or restore). May contain duplicates; the delta
index builder deduplicates. Superset of all genuinely-changed nodes.
Sourcepub fn clear_touched(&mut self)
pub fn clear_touched(&mut self)
Clear the touched log (call at a semi-naive round boundary, after the delta index for the round has been built).
pub fn node_count(&self) -> usize
pub fn class_repr(&self, id: Cfg::G) -> Cfg::G
pub fn node_ref(&self, id: Cfg::G) -> NodeRef<Cfg::Ids>
pub fn node_op(&self, id: Cfg::G) -> Cfg::O
pub fn node_op_name(&self, id: Cfg::G) -> &str
pub fn node_flags(&self, id: Cfg::G) -> u8
Sourcepub fn subsume(&mut self, id: Cfg::G)
pub fn subsume(&mut self, id: Cfg::G)
User-level subsumption: exclude id from future pattern matching (the matcher’s
indices skip FLAG_SUBSUMED). Distinct from AC-collapse — see FLAG_AC_COLLAPSED.
Sourcepub fn get_lit_val(&self, id: Cfg::G) -> Option<&L>
pub fn get_lit_val(&self, id: Cfg::G) -> Option<&L>
If id is a literal node, return its interned value. Otherwise None.
Sourcepub fn get_lit_val_id(&self, id: Cfg::G) -> Option<Cfg::V>
pub fn get_lit_val_id(&self, id: Cfg::G) -> Option<Cfg::V>
If id is a literal node, return its interned LitValId.
Sourcepub fn node_sort(&self, id: Cfg::G) -> Cfg::S
pub fn node_sort(&self, id: Cfg::G) -> Cfg::S
Return sort of a node (from its operator’s signature).
Sourcepub fn debug_check_sort_invariant(&self)
pub fn debug_check_sort_invariant(&self)
Debug: verify all nodes in each e-class have the same sort. Panics on violation. Only useful in tests / debug builds.
pub fn for_each_child(&self, id: Cfg::G, f: impl FnMut(Cfg::G, Cfg::M)) -> usize
Sourcepub fn child_at(&self, id: Cfg::G, pos: Cfg::Index) -> Cfg::G
pub fn child_at(&self, id: Cfg::G, pos: Cfg::Index) -> Cfg::G
Read the child at position pos from a fixed-arity node. O(1).
Panics if pos is out of range or node is variadic/lit.
Sourcepub fn mset_children(&self, id: Cfg::G, buf: &mut Vec<(Cfg::G, Cfg::M)>)
pub fn mset_children(&self, id: Cfg::G, buf: &mut Vec<(Cfg::G, Cfg::M)>)
Read AC children as (id, multiplicity) pairs into buf.
Sourcepub fn set_children(&self, id: Cfg::G, buf: &mut Vec<Cfg::G>)
pub fn set_children(&self, id: Cfg::G, buf: &mut Vec<Cfg::G>)
Read ACI children (ids only, no multiplicities) into buf.
Sourcepub fn seq_children(&self, id: Cfg::G, buf: &mut Vec<Cfg::G>)
pub fn seq_children(&self, id: Cfg::G, buf: &mut Vec<Cfg::G>)
Read A/PlainN children into buf in sequence order.