semi_persistent_egraph/config.rs
1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3//! E-graph configuration trait — bundles all id types for a concrete e-graph.
4
5use crate::containers::{DenseId, IndexLike, Tagged};
6use crate::typed_routing::NodeIds;
7use core::hash::Hash;
8
9/// Configuration for an e-graph instance.
10/// Bundles all id types so the EGraph struct has a single type parameter.
11pub trait EGraphConfig: 'static {
12 /// Unsigned machine word backing all capacity-coupled DenseIds.
13 ///
14 /// DenseIds reserve the most-significant bit for inline tagging, so the
15 /// payload capacity is one bit less than the word:
16 /// * `u32` gives 31 payload bits (`define_id31!` types),
17 /// * `u64` gives 63 payload bits (`define_id63!` types).
18 ///
19 /// Every id whose capacity must scale with the configured e-graph (node
20 /// ids, operator ids, per-kind local ids, AU search/pool ids) is
21 /// constrained to this word. Intentionally bounded semantic registries
22 /// (e.g. rule ids) may remain narrower; each such exception is documented
23 /// at its definition.
24 type Index: IndexLike + Tagged;
25
26 /// Global e-node id (e.g. 31-bit `ENodeId`).
27 type G: DenseId<Index = Self::Index> + Hash;
28 /// Recycled key into the packed e-class data store. It has the same
29 /// payload width as `G`, because the worst case has one class per node.
30 type ClassKey: DenseId<Index = Self::Index>;
31 /// Operator id.
32 type O: DenseId<Index = Self::Index> + Hash;
33 /// Sort id.
34 type S: DenseId<Index = Self::Index>;
35 /// Interned literal value id.
36 type V: DenseId<Index = Self::Index> + Hash;
37 /// Use-list id.
38 type UL: DenseId<Index = Self::Index>;
39 /// Use-list node id.
40 type UN: DenseId<Index = Self::Index>;
41 /// AC child type (e.g. `(G, Multiplicity)`).
42 type C: Tagged + Clone + Copy + Hash + Eq + core::fmt::Debug;
43 /// Multiplicity width for AC multiset nodes: `Multiplicity16`,
44 /// `Multiplicity`, or `Multiplicity64`.
45 ///
46 /// Independent of `Index`. A multiplicity counts occurrences of one child
47 /// within one AC node, so its domain is that node's child count (bounded by
48 /// `for_each_child`'s `1 + 64 * node_count()` cap, i.e. 64·N) — a different
49 /// quantity from the id population, and one a caller may want to size
50 /// separately. Conversions to and from the `u64` surface width are checked;
51 /// see `MultiplicityLike`.
52 type M: crate::multiplicity::MultiplicityLike;
53 /// Extract the global id from an AC child.
54 fn mset_child_id(c: &Self::C) -> Self::G;
55 /// Extract the multiplicity from an AC child.
56 fn mset_child_mult(c: &Self::C) -> Self::M;
57 /// Create an AC child with multiplicity 1.
58 fn mset_child_single(g: Self::G) -> Self::C {
59 Self::mset_child_with_mult(g, <Self::M as crate::multiplicity::MultiplicityLike>::ONE)
60 }
61 /// Create an AC child with a given multiplicity.
62 fn mset_child_with_mult(g: Self::G, mult: Self::M) -> Self::C;
63 /// Increment the multiplicity of an AC child. Returns true if same group.
64 ///
65 /// Implementations must use [`MultiplicityLike::checked_add`] — the build path
66 /// has no error channel here, and a wrapped increment would silently produce a
67 /// multiplicity of zero, which the canonical form says cannot exist.
68 ///
69 /// [`MultiplicityLike::checked_add`]: crate::multiplicity::MultiplicityLike::checked_add
70 fn mset_child_merge(existing: &mut Self::C, new_g: Self::G) -> bool;
71 /// Local id bundle for the node store.
72 type Ids: NodeIds<Index = Self::Index>;
73 /// AU search/snapshot/pool id bundle.
74 type Au: AuIds<Index = Self::Index>;
75}
76
77/// The five `mset_child_*` bodies for a config whose `C` is
78/// [`MSetChild<G, M>`](crate::node_store::MSetChild) — i.e. any config that stores AC
79/// children as an id/multiplicity pair. Emitted rather than hand-written per config so the
80/// checked increment in `mset_child_merge` cannot drift between configs: a duplicated
81/// hand-written impl is where an unchecked `+ 1` can hide.
82#[macro_export]
83macro_rules! impl_mset_child_pair {
84 () => {
85 fn mset_child_id(c: &Self::C) -> Self::G {
86 c.a
87 }
88 fn mset_child_mult(c: &Self::C) -> Self::M {
89 c.b
90 }
91 fn mset_child_with_mult(g: Self::G, mult: Self::M) -> Self::C {
92 $crate::containers::Pair { a: g, b: mult }
93 }
94 fn mset_child_merge(existing: &mut Self::C, new_g: Self::G) -> bool {
95 use $crate::multiplicity::MultiplicityLike;
96 if existing.a == new_g {
97 // Checked, not `+ 1`: the build path has no error channel, and a wrapped
98 // increment would produce a multiplicity of zero — an entry the canonical
99 // form says cannot exist, which the clamps would then silently delete.
100 existing.b = existing.b.checked_add(Self::M::ONE).expect(
101 "AC child multiplicity overflowed EGraphConfig::M; \
102 the configured multiplicity width is too narrow for this e-graph",
103 );
104 true
105 } else {
106 false
107 }
108 }
109 };
110}
111
112/// Id bundle for the anti-unification subsystem: search-graph identities,
113/// snapshot identities, and typed positions into flattened persistent pools.
114/// Selected by `EGraphConfig::Au` so a wide e-graph gets wide AU arenas.
115pub trait AuIds: 'static {
116 /// Backing word; equals the owning config's `Index`.
117 type Index: IndexLike + Tagged;
118
119 // --- Direct identities ---
120 /// Dense live-class index inside one AU snapshot.
121 type Class: DenseId<Index = Self::Index> + Hash;
122 /// Strongly connected component index in the snapshot's class graph.
123 type Scc: DenseId<Index = Self::Index>;
124 /// OR node (subproblem) in the search space.
125 type Or: DenseId<Index = Self::Index> + Hash;
126 /// Cached action list in the action cache.
127 type Action: DenseId<Index = Self::Index>;
128 /// Interned cycle context.
129 type Context: DenseId<Index = Self::Index> + Hash;
130 /// Hash-consed result term.
131 type Term: DenseId<Index = Self::Index> + Hash;
132 /// MCGS OR-statistics entry.
133 type OrStats: DenseId<Index = Self::Index>;
134 /// MCGS AND-statistics entry.
135 type AndStats: DenseId<Index = Self::Index>;
136
137 // --- Typed positions into flattened persistent pools ---
138 /// Position in the snapshot's flattened member pool.
139 type SnapshotMember: DenseId<Index = Self::Index>;
140 /// Position in the context interner's class pool.
141 type ContextElem: DenseId<Index = Self::Index>;
142 /// Position in the term pool's child pool.
143 type TermChild: DenseId<Index = Self::Index>;
144 /// Position in the reachability bit-block pool.
145 type ReachBlock: DenseId<Index = Self::Index>;
146 /// Position in the flattened MCGS OR-edge statistics pools.
147 type OrEdgeStat: DenseId<Index = Self::Index>;
148 /// Position in the flattened MCGS AND-child statistics pools.
149 type AndChildStat: DenseId<Index = Self::Index>;
150}