1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
//! Semi-persistent union-find with optional proof tracking.
use crate::containers::Tagged;
/// Why two e-nodes were unified.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default)]
pub enum Justification<G: Copy> {
/// No-op filler used to default-initialize slots (e.g. as the
/// `resize_default` filler during restore, and the initial entry from
/// `make_set`). Never carries proof information; never observed as a real
/// justification.
#[default]
Filler,
Rewrite {
rule_id: crate::id::RuleId,
},
Congruence {
node_a: G,
node_b: G,
},
Axiom {
axiom_id: crate::id::AxiomId,
},
/// AC completion: a critical-pair superposition between two rules sharing a child class.
/// The merge equates the normalized reducts. (Kapur Def 3.2.)
ACSuperposition {
node_a: G,
node_b: G,
},
/// A completion inter-reduction merge (Kapur Algo 1 step 4): an AC
/// monomial's sub-multiset substituted by a class's minimal monomial, or
/// an A-only sequence's contiguous subsequence substituted by the
/// shortlex-least spelling (`a_round`). `node_a` is the rewritten node,
/// `node_b` its normal form.
ACInterReduction {
node_a: G,
node_b: G,
},
/// Semantic-axiom critical pair (Kapur §4 per-rule): idempotent/nilpotent axiom critical
/// pair, or an identity-drop / degeneracy merge derived during completion normalization.
ACAxiomCP {
node_a: G,
node_b: G,
},
/// Cancellative closure: `x∘z = y∘z ⟹ x = y` (Kapur §5.2).
Cancellative {
node_a: G,
node_b: G,
},
/// Inverse-pair cancellation: `x ∘ inv(x) = e` recognized and merged.
InverseCancel {
node_a: G,
node_b: G,
},
}
impl<G: Copy + Clone + core::fmt::Debug + PartialEq + Eq> Tagged for Justification<G> {
type Repr = crate::containers::BoolTagged<Justification<G>>;
fn into_repr(self) -> Self::Repr {
crate::containers::BoolTagged::new(self)
}
fn from_repr(stored: &Self::Repr) -> Self {
stored.value
}
fn tag(stored: &Self::Repr) -> bool {
stored.tagged
}
fn set_tag(stored: &mut Self::Repr) {
stored.tagged = true;
}
fn clear_tag(stored: &mut Self::Repr) {
stored.tagged = false;
}
}
/// Semi-persistent union-find: the VERIFIED kernel, with this crate's
/// [`Justification`] as the proof payload. The implementation lives in
/// `containers-verus/src/union_find.rs`: dual fast/proof forests under
/// `PROOFS`, by-rank and directed unions, two-pass path-compressing `find`,
/// re-rooting proof edges, LCA `explain`, with W1 (the partition invariant)
/// machine-checked (`containers-verus/doc/design/egraph-class-layer.md`).
pub type UnionFind<T, const TRACK: bool = true, const PROOFS: bool = false> =
crate::containers::union_find::UnionFind<T, Justification<T>, TRACK, PROOFS>;
/// Opaque token for [`UnionFind::mark`] / [`UnionFind::restore`].
pub type UnionFindToken = crate::containers::union_find::UnionFindToken;
/// Reusable scratch buffers for proof extraction (the kernel's, with this
/// crate's justification payload).
pub type ProofBuf<T> = crate::containers::union_find::ProofBuf<T, Justification<T>>;
#[cfg(test)]
mod tests {
use super::*;
use crate::id::ENodeId;
type UF = UnionFind<ENodeId, false, false>;
fn uf(n: u32) -> UF {
let mut uf = UF::new();
for i in 0..n {
uf.make_set(ENodeId::new(i));
}
uf
}
#[test]
fn union_directed_forces_survivor() {
let mut uf = uf(2);
let a = ENodeId::new(0);
let b = ENodeId::new(1);
// prefer_a = true keeps a's root as the survivor.
let (survivor, absorbed) = uf.union_directed(a, b, true).unwrap();
assert_eq!(survivor, a);
assert_eq!(absorbed, b);
assert_eq!(uf.find(a), a);
assert_eq!(uf.find(b), a);
}
#[test]
fn union_directed_other_survivor() {
let mut uf = uf(2);
let a = ENodeId::new(0);
let b = ENodeId::new(1);
// prefer_a = false keeps b's root as the survivor.
let (survivor, absorbed) = uf.union_directed(a, b, false).unwrap();
assert_eq!(survivor, b);
assert_eq!(absorbed, a);
assert_eq!(uf.find(a), b);
assert_eq!(uf.find(b), b);
}
#[test]
fn justification_named_repr_preserves_value_when_tagged() {
let just = Justification::<ENodeId>::Congruence {
node_a: ENodeId::new(1),
node_b: ENodeId::new(2),
};
let mut repr = just.into_repr();
assert!(!Justification::<ENodeId>::tag(&repr));
Justification::<ENodeId>::set_tag(&mut repr);
assert!(Justification::<ENodeId>::tag(&repr));
assert_eq!(Justification::<ENodeId>::from_repr(&repr), just);
Justification::<ENodeId>::clear_tag(&mut repr);
assert!(!Justification::<ENodeId>::tag(&repr));
}
#[test]
fn union_directed_can_force_against_rank() {
// Build a taller tree rooted at `tall`, then a singleton `small`, and force `small`
// to survive even though it is the shorter tree. `find` must still resolve correctly
// (the rank stays a valid height upper bound, see `union_inner`).
let mut uf = uf(4);
let (n0, n1, n2, small) = (
ENodeId::new(0),
ENodeId::new(1),
ENodeId::new(2),
ENodeId::new(3),
);
// n0,n1 then merge in n2 to bump rank at n0's root.
uf.union(n0, n1);
uf.union(n0, n2);
let tall = uf.find(n0);
// Force the singleton `small` to be the survivor of (tall ∪ small).
let (survivor, absorbed) = uf.union_directed(tall, small, false).unwrap();
assert_eq!(survivor, small);
assert_eq!(absorbed, tall);
// Every original element now resolves to `small`.
for n in [n0, n1, n2, small] {
assert_eq!(uf.find(n), small);
}
}
#[test]
fn union_directed_idempotent_when_same_class() {
let mut uf = uf(2);
let a = ENodeId::new(0);
let b = ENodeId::new(1);
uf.union(a, b);
// Already merged: a directed union returns None regardless of preference.
assert!(uf.union_directed(a, b, true).is_none());
assert!(uf.union_directed(a, b, false).is_none());
}
}