pub struct TermPool<O: DenseId, V: DenseId, A: AuIds = AuIds31> { /* private fields */ }Expand description
Hash-consed term pool. Structurally equal terms get the same term id.
All fields are semi-persistent (AppendOnlyVec/SpMap); mark/restore truncates.
The id family A defaults to the 31-bit family; a Config64 session
instantiates TermPool<O, V, AuIds64> through Cfg::Au.
Every pool here is addressed by an id whose Index is A::Index, so that word —
not usize — is the index type: the five term-indexed columns are addressed by
A::Term, child_pool by A::TermChild, and by_structure’s log positions are
what A::Terms are minted from. Pinning them keeps each saved frame length and
each hash-index value at the configured width instead of 8 bytes.
Implementations§
Source§impl<O: DenseId + Hash, V: DenseId + Hash, A: AuIds> TermPool<O, V, A>
impl<O: DenseId + Hash, V: DenseId + Hash, A: AuIds> TermPool<O, V, A>
pub fn new() -> Self
Sourcepub fn len(&self) -> A::Index
pub fn len(&self) -> A::Index
Number of interned terms, in the configured index word.
A term count is a position count — the next term interns at exactly this
index — so it is reported in A::Index rather than widened to usize.
pub fn is_empty(&self) -> bool
Sourcepub fn intern(&mut self, op: TermOp<O, V>, children: &[A::Term]) -> A::Term
pub fn intern(&mut self, op: TermOp<O, V>, children: &[A::Term]) -> A::Term
Intern a term. Returns the existing id if structurally equal term exists.
Sourcepub fn intern_action_result(
&mut self,
op: TermOp<O, V>,
children_with_counts: &[(A::Term, u64)],
commutative: bool,
) -> A::Term
pub fn intern_action_result( &mut self, op: TermOp<O, V>, children_with_counts: &[(A::Term, u64)], commutative: bool, ) -> A::Term
Intern the result term of one action.
commutative MUST be true exactly for operators whose canonical node kind is
commutative (SPair, MSet, Set): their children are sorted into canonical
structural order. For ordered operators (Plain*, Seq) it MUST be false: the
pair order of the action is positional semantics and is preserved verbatim.
Counts arrive at the surface width. Two callers feed this: the structural path,
whose counts are EGraphConfig::M multiplicities, and the transport path, whose
counts are flow cells at the solver’s own narrower capacity. u64 is the one width
that holds both without a fallible conversion — MultiplicityLike::to_u64 is
total and lossless at every configured width — and the count is consumed here
rather than stored, so the width costs nothing beyond the call.
Sourcepub fn structural_cmp(&self, a: A::Term, b: A::Term) -> Ordering
pub fn structural_cmp(&self, a: A::Term, b: A::Term) -> Ordering
Total structural order on terms, independent of allocation order:
operator variant rank, then operator/value ids, then arity, then children
lexicographically. Equal ids are structurally equal (hash-consing), so this
returns Equal only for identical ids.
Iterative (explicit work stack): the first non-Equal comparison in
depth-first, left-to-right order decides, exactly like the recursive
lexicographic definition. Depth is heap-bounded, not call-stack-bounded.
Sourcepub fn size(&self, id: A::Term) -> u32
pub fn size(&self, id: A::Term) -> u32
Get the size of a term. Saturates at u32::MAX for terms whose expanded
size exceeds the counter (see intern); such a term is ranked worst, never
best.
Sourcepub fn variant_mass(&self, id: A::Term) -> u32
pub fn variant_mass(&self, id: A::Term) -> u32
Get the variant mass of a term: concrete nodes under Variants nodes.
size - variant_mass is the backbone (shared structure) size.
Sourcepub fn quality(&self, id: A::Term) -> (u32, u32)
pub fn quality(&self, id: A::Term) -> (u32, u32)
The lexicographic quality key (size, variant_mass). Lower is better:
primary objective is minimum size; at equal size the term with less
variant mass has more backbone (more factored structure) and wins.
Both components saturate (intern), so the order is total and monotone but
loses discrimination among terms too large to count — they all tie at the
bottom. That is the intended degradation: the alternative, wrapping, inverted
the comparison and made the largest term look like the best one.
pub fn mark(&mut self) -> TermPoolToken
Sourcepub fn is_valid_token(&self, token: &TermPoolToken) -> bool
pub fn is_valid_token(&self, token: &TermPoolToken) -> bool
Is this token restorable right now (same instances, live branches on every inner container)?
pub fn restore(&mut self, token: TermPoolToken)
Sourcepub fn project(&mut self, id: A::Term, side: usize) -> A::Term
pub fn project(&mut self, id: A::Term, side: usize) -> A::Term
Project one side of the anti-unifier: replace every Variants node —
at any depth — by its left (side 0) or right (side 1) child, recursively.
The result contains no Variants node (§1: variant projection must land
in the source class). New nodes may be interned for rebuilt spines.
Iterative post-order fold (explicit frame stack): each frame projects
its children left to right, then re-interns only if a child changed, so
the interning order matches the recursive definition exactly. A
Variants node is a tail step (follow the chosen arm), so it never
occupies a frame. Depth is heap-bounded, not call-stack-bounded.
Sourcepub fn has_variants(&self, id: A::Term) -> bool
pub fn has_variants(&self, id: A::Term) -> bool
Does this term contain any Variants node (at any depth)?
Iterative DFS with an explicit stack; depth is heap-bounded.