Skip to main content

TermPool

Struct TermPool 

Source
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>

Source

pub fn new() -> Self

Source

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.

Source

pub fn is_empty(&self) -> bool

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn op(&self, id: A::Term) -> &TermOp<O, V>

Get the operator of a term.

Source

pub fn children(&self, id: A::Term) -> &[A::Term]

Get the children of a term.

Source

pub fn mark(&mut self) -> TermPoolToken

Source

pub fn is_valid_token(&self, token: &TermPoolToken) -> bool

Is this token restorable right now (same instances, live branches on every inner container)?

Source

pub fn restore(&mut self, token: TermPoolToken)

Source

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.

Source

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.

Trait Implementations§

Source§

impl<O: DenseId, V: DenseId> Debug for TermPool<O, V>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<O: DenseId + Hash, V: DenseId + Hash> Default for TermPool<O, V>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<O, V, A> Freeze for TermPool<O, V, A>
where AppendOnlyVec<TermOp<O, V>, <A as AuIds>::Index>: Freeze, AppendOnlyVec<Span<<A as AuIds>::TermChild>, <A as AuIds>::Index>: Freeze, AppendOnlyVec<<A as AuIds>::Term, <A as AuIds>::Index>: Freeze, AppendOnlyVec<u32, <A as AuIds>::Index>: Freeze, SpMap<(TermOp<O, V>, Vec<<A as AuIds>::Term>), <A as AuIds>::Term, <A as AuIds>::Index>: Freeze, SpMap<<A as AuIds>::Class, <A as AuIds>::Term, <A as AuIds>::Index>: Freeze,

§

impl<O, V, A> RefUnwindSafe for TermPool<O, V, A>

§

impl<O, V, A> Send for TermPool<O, V, A>
where AppendOnlyVec<TermOp<O, V>, <A as AuIds>::Index>: Send, AppendOnlyVec<Span<<A as AuIds>::TermChild>, <A as AuIds>::Index>: Send, AppendOnlyVec<<A as AuIds>::Term, <A as AuIds>::Index>: Send, AppendOnlyVec<u32, <A as AuIds>::Index>: Send, SpMap<(TermOp<O, V>, Vec<<A as AuIds>::Term>), <A as AuIds>::Term, <A as AuIds>::Index>: Send, SpMap<<A as AuIds>::Class, <A as AuIds>::Term, <A as AuIds>::Index>: Send,

§

impl<O, V, A> Sync for TermPool<O, V, A>
where AppendOnlyVec<TermOp<O, V>, <A as AuIds>::Index>: Sync, AppendOnlyVec<Span<<A as AuIds>::TermChild>, <A as AuIds>::Index>: Sync, AppendOnlyVec<<A as AuIds>::Term, <A as AuIds>::Index>: Sync, AppendOnlyVec<u32, <A as AuIds>::Index>: Sync, SpMap<(TermOp<O, V>, Vec<<A as AuIds>::Term>), <A as AuIds>::Term, <A as AuIds>::Index>: Sync, SpMap<<A as AuIds>::Class, <A as AuIds>::Term, <A as AuIds>::Index>: Sync,

§

impl<O, V, A> Unpin for TermPool<O, V, A>
where AppendOnlyVec<TermOp<O, V>, <A as AuIds>::Index>: Unpin, AppendOnlyVec<Span<<A as AuIds>::TermChild>, <A as AuIds>::Index>: Unpin, AppendOnlyVec<<A as AuIds>::Term, <A as AuIds>::Index>: Unpin, AppendOnlyVec<u32, <A as AuIds>::Index>: Unpin, SpMap<(TermOp<O, V>, Vec<<A as AuIds>::Term>), <A as AuIds>::Term, <A as AuIds>::Index>: Unpin, SpMap<<A as AuIds>::Class, <A as AuIds>::Term, <A as AuIds>::Index>: Unpin,

§

impl<O, V, A> UnsafeUnpin for TermPool<O, V, A>
where AppendOnlyVec<TermOp<O, V>, <A as AuIds>::Index>: UnsafeUnpin, AppendOnlyVec<Span<<A as AuIds>::TermChild>, <A as AuIds>::Index>: UnsafeUnpin, AppendOnlyVec<<A as AuIds>::Term, <A as AuIds>::Index>: UnsafeUnpin, AppendOnlyVec<u32, <A as AuIds>::Index>: UnsafeUnpin, SpMap<(TermOp<O, V>, Vec<<A as AuIds>::Term>), <A as AuIds>::Term, <A as AuIds>::Index>: UnsafeUnpin, SpMap<<A as AuIds>::Class, <A as AuIds>::Term, <A as AuIds>::Index>: UnsafeUnpin,

§

impl<O, V, A> UnwindSafe for TermPool<O, V, A>
where AppendOnlyVec<TermOp<O, V>, <A as AuIds>::Index>: UnwindSafe, AppendOnlyVec<Span<<A as AuIds>::TermChild>, <A as AuIds>::Index>: UnwindSafe, AppendOnlyVec<<A as AuIds>::Term, <A as AuIds>::Index>: UnwindSafe, AppendOnlyVec<u32, <A as AuIds>::Index>: UnwindSafe, SpMap<(TermOp<O, V>, Vec<<A as AuIds>::Term>), <A as AuIds>::Term, <A as AuIds>::Index>: UnwindSafe, SpMap<<A as AuIds>::Class, <A as AuIds>::Term, <A as AuIds>::Index>: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.