Skip to main content

semi_persistent_egraph/
nodes.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3//! Concrete local DenseId types (one per node kind) and DefaultNodeIds.
4
5// ---------------------------------------------------------------------------
6// Local DenseId types — one per store
7// ---------------------------------------------------------------------------
8
9semi_persistent_containers::define_id31! {
10    pub struct Plain0Id / StoredPlain0Id, "p0";
11}
12semi_persistent_containers::define_id31! {
13    pub struct Plain1Id / StoredPlain1Id, "p1";
14}
15semi_persistent_containers::define_id31! {
16    pub struct Plain2Id / StoredPlain2Id, "p2";
17}
18semi_persistent_containers::define_id31! {
19    pub struct Plain3Id / StoredPlain3Id, "p3";
20}
21semi_persistent_containers::define_id31! {
22    pub struct PlainNId / StoredPlainNId, "pN";
23}
24semi_persistent_containers::define_id31! {
25    pub struct SPairNodeId / StoredSPairNodeId, "sp";
26}
27semi_persistent_containers::define_id31! {
28    pub struct SeqNodeId / StoredSeqNodeId, "sq";
29}
30semi_persistent_containers::define_id31! {
31    pub struct MSetNodeId / StoredMSetNodeId, "mset";
32}
33semi_persistent_containers::define_id31! {
34    pub struct SetNodeId / StoredSetNodeId, "set";
35}
36semi_persistent_containers::define_id31! {
37    pub struct LitNodeId / StoredLitNodeId, "lit";
38}
39
40/// Standard node-id configuration for the generic node store.
41pub struct DefaultNodeIds;
42impl crate::typed_routing::NodeIds for DefaultNodeIds {
43    type Index = u32;
44    type L0 = Plain0Id;
45    type L1 = Plain1Id;
46    type L2 = Plain2Id;
47    type L3 = Plain3Id;
48    type LSPair = SPairNodeId;
49    type LN = PlainNId;
50    type LSeq = SeqNodeId;
51    type LMSet = MSetNodeId;
52    type LSet = SetNodeId;
53    type LLit = LitNodeId;
54}
55
56semi_persistent_containers::define_id31! {
57    /// Interned literal value id.
58    pub struct LitValId / StoredLitValId, "lv";
59}
60
61/// Default 31-bit e-graph configuration.
62pub struct DefaultConfig;
63impl crate::config::EGraphConfig for DefaultConfig {
64    type Index = u32;
65    type G = crate::id::ENodeId;
66    type ClassKey = crate::id::EClassKey;
67    type O = crate::id::OpId;
68    type S = crate::id::SortId;
69    type V = LitValId;
70    type UL = crate::id::UseListId;
71    type UN = crate::id::UseNodeId;
72    type C = crate::node_store::MSetChild<crate::id::ENodeId>;
73    type M = crate::multiplicity::Multiplicity;
74    type Ids = DefaultNodeIds;
75    type Au = crate::au::AuIds31;
76
77    crate::impl_mset_child_pair!();
78}
79
80// ---------------------------------------------------------------------------
81// 63-bit id types
82// ---------------------------------------------------------------------------
83
84semi_persistent_containers::define_id63! { pub struct ENodeId64 / StoredENodeId64, "e64"; }
85semi_persistent_containers::define_id63! { pub struct EClassKey64 / StoredEClassKey64, "ec64"; }
86semi_persistent_containers::define_id63! { pub struct OpId64 / StoredOpId64, "op64"; }
87semi_persistent_containers::define_id63! { pub struct SortId64 / StoredSortId64, "s64"; }
88semi_persistent_containers::define_id63! { pub struct UseListId64 / StoredUseListId64, "ul64"; }
89semi_persistent_containers::define_id63! { pub struct UseNodeId64 / StoredUseNodeId64, "un64"; }
90semi_persistent_containers::define_id63! { pub struct LitValId64 / StoredLitValId64, "lv64"; }
91semi_persistent_containers::define_id63! { pub struct Plain0Id64 / StoredPlain0Id64, "p0_64"; }
92semi_persistent_containers::define_id63! { pub struct Plain1Id64 / StoredPlain1Id64, "p1_64"; }
93semi_persistent_containers::define_id63! { pub struct Plain2Id64 / StoredPlain2Id64, "p2_64"; }
94semi_persistent_containers::define_id63! { pub struct Plain3Id64 / StoredPlain3Id64, "p3_64"; }
95semi_persistent_containers::define_id63! { pub struct PlainNId64 / StoredPlainNId64, "pN_64"; }
96semi_persistent_containers::define_id63! { pub struct SPairNodeId64 / StoredSPairNodeId64, "sp64"; }
97semi_persistent_containers::define_id63! { pub struct SeqNodeId64 / StoredSeqNodeId64, "sq64"; }
98semi_persistent_containers::define_id63! { pub struct MSetNodeId64 / StoredMSetNodeId64, "mset64"; }
99semi_persistent_containers::define_id63! { pub struct SetNodeId64 / StoredSetNodeId64, "set64"; }
100semi_persistent_containers::define_id63! { pub struct LitNodeId64 / StoredLitNodeId64, "lit64"; }
101
102pub struct NodeIds64;
103impl crate::typed_routing::NodeIds for NodeIds64 {
104    type Index = u64;
105    type L0 = Plain0Id64;
106    type L1 = Plain1Id64;
107    type L2 = Plain2Id64;
108    type L3 = Plain3Id64;
109    type LSPair = SPairNodeId64;
110    type LN = PlainNId64;
111    type LSeq = SeqNodeId64;
112    type LMSet = MSetNodeId64;
113    type LSet = SetNodeId64;
114    type LLit = LitNodeId64;
115}
116
117/// 63-bit e-graph configuration (63 payload bits in a u64 word).
118pub struct Config64;
119impl crate::config::EGraphConfig for Config64 {
120    type Index = u64;
121    type G = ENodeId64;
122    type ClassKey = EClassKey64;
123    type O = OpId64;
124    type S = SortId64;
125    type V = LitValId64;
126    type UL = UseListId64;
127    type UN = UseNodeId64;
128    type C = crate::node_store::MSetChild<ENodeId64, crate::multiplicity::Multiplicity64>;
129    // A 64-bit multiplicity, because at this id width it is free: an AC child pairs an
130    // 8-byte id with the count, and the pair is 16 bytes whether the count is 2, 4, or 8
131    // bytes wide — the alignment of the id decides. Anything narrower would leave a
132    // reachable ceiling in place for nothing: a 63-bit e-graph admits up to 2^63 nodes, so
133    // `for_each_child`'s 64·N bound on a single multiplicity runs far past `u32`.
134    type M = crate::multiplicity::Multiplicity64;
135    type Ids = NodeIds64;
136    type Au = crate::au::AuIds64;
137
138    crate::impl_mset_child_pair!();
139}
140
141/// 31-bit ids with a **16-bit** multiplicity: the low-ceiling witness for the width axis.
142///
143/// This is the same 31-bit e-graph as [`DefaultConfig`] with a deliberately small
144/// multiplicity ceiling (65535 occurrences of one child in one AC node). Its purpose is to
145/// keep `EGraphConfig::M` honest, in two ways a second *wide* config cannot:
146///
147/// * it does not compile against a hardcoded `Multiplicity` anywhere on the generic paths,
148///   so the parameterization cannot silently rot back to a fixed `u32`;
149/// * its ceiling is reachable by a test. The checked narrowing and overflow paths are the
150///   whole point of the design, and at 32 bits provoking them needs four billion children.
151///
152/// It is not a space optimization: an AC child pairs a 4-byte id with the count and pads to
153/// 8 bytes at both 16 and 32 bits. Pick it when a low ceiling is *wanted* as a guard, not to
154/// save memory.
155pub struct ConfigM16;
156impl crate::config::EGraphConfig for ConfigM16 {
157    type Index = u32;
158    type G = crate::id::ENodeId;
159    type ClassKey = crate::id::EClassKey;
160    type O = crate::id::OpId;
161    type S = crate::id::SortId;
162    type V = LitValId;
163    type UL = crate::id::UseListId;
164    type UN = crate::id::UseNodeId;
165    type C = crate::node_store::MSetChild<crate::id::ENodeId, crate::multiplicity::Multiplicity16>;
166    type M = crate::multiplicity::Multiplicity16;
167    type Ids = DefaultNodeIds;
168    type Au = crate::au::AuIds31;
169
170    crate::impl_mset_child_pair!();
171}