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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
use crate::{
Term, TermId,
ast::ResolvedNCommand,
proofs::{
proof_checker::{ProofCheckError, gather_globals},
proof_format::{Justification, Proof, ProofId, ProofStore, Proposition},
},
util::HashMap,
};
impl ProofStore {
/// Remove globals from a proof by replacing all global variable references
/// with their computed values.
/// This constructs a map of global names to their terms (without globals),
/// then replaces all occurrences of those globals in the proof's term dag.
pub fn remove_globals(&mut self, prog: &[ResolvedNCommand]) -> Result<(), ProofCheckError> {
// Gather all globals and their values as terms
let globals = gather_globals(prog, &mut self.term_dag)?;
// Replace all global function calls (nullary functions) in the term dag
// with their computed values
self.replace_global_terms(&globals);
Ok(())
}
/// Replace all global function applications in the term dag with their values.
fn replace_global_terms(&mut self, globals: &HashMap<String, TermId>) {
// We need to rebuild the term dag, replacing global calls as we go
// Build a map from old term IDs to new term IDs
let mut term_mapping: HashMap<TermId, TermId> = HashMap::default();
// Process all terms in order (since term dag is built bottom-up)
for term_id in 0..self.term_dag.size() {
let term = self.term_dag.get(term_id).clone();
let new_term_id = match term {
Term::Lit(_) | Term::Var(_) => term_id, // Literals and vars don't change
Term::App(ref head, ref args) => {
// Check if this is a nullary global function call
if args.is_empty() && globals.contains_key(head) {
// Replace with the global's value, applying the term mapping
let global_term_id = globals[head];
*term_mapping.get(&global_term_id).unwrap_or(&global_term_id)
} else {
// Map the children and reconstruct the term if any changed
let mapped_args: Vec<TermId> = args
.iter()
.map(|&child_id| *term_mapping.get(&child_id).unwrap_or(&child_id))
.collect();
self.term_dag.app(head.clone(), mapped_args)
}
}
};
term_mapping.insert(term_id, new_term_id);
}
// Now update all proofs to use the new term IDs
for (_id, proof) in self.id_to_proof.iter_mut() {
proof.map_terms_mut(|term_id| *term_mapping.get(&term_id).unwrap_or(&term_id));
}
}
/// Add a new proof to the store and return its ID.
fn add_proof(&mut self, proof: Proof) -> ProofId {
self.id_to_proof.push(proof)
}
/// A simple simplification pass removing unnecessary steps.
/// Applies optimizations until a fixed point is reached.
///
/// Simplifications performed:
/// - Remove reflexive congruence: Congr(p, refl) -> p
/// - Remove reflexive transitivity: Trans(refl, p) -> p and Trans(p, refl) -> p
/// - Remove reflexive symmetry: Sym(p) -> p when p proves t = t
/// - Collapse double symmetry: Sym(Sym(p)) -> p
/// - Push symmetry through transitivity: Sym(Trans(p1, p2)) -> Trans(Sym(p2), Sym(p1))
/// This enables further simplifications by exposing the inner proofs
/// - Remove redundant container normalization: ContainerNormalize(p) -> p when the
/// normalization left the term unchanged (always the case for
/// order/arity-preserving containers like Vec/Pair, which mint the normalization
/// unconditionally, and for already-canonical sets/maps/multisets)
pub fn simplify(&mut self, proof_id: ProofId) -> ProofId {
// First, recursively simplify all child proofs
let proof_id = self.map_child_proofs(proof_id, |store, pid| store.simplify(pid));
// Apply local optimizations until fixed point
self.simplify_local(proof_id)
}
fn simplify_local(&mut self, proof_id: ProofId) -> ProofId {
let mut current_id = proof_id;
loop {
let new_id = self.apply_local_optimizations(current_id);
if new_id == current_id {
break;
}
current_id = new_id;
}
current_id
}
/// Apply local optimizations to a single proof node.
/// Returns a potentially different proof ID if an optimization was applied.
fn apply_local_optimizations(&mut self, proof_id: ProofId) -> ProofId {
// List of optimization functions to try
let optimizations: &[fn(&mut ProofStore, ProofId) -> Option<ProofId>] = &[
Self::opt_reflexive_congr,
Self::opt_reflexive_trans,
Self::opt_reflexive_sym,
Self::opt_double_sym,
Self::opt_sym_trans,
Self::opt_redundant_container_normalize,
];
for opt in optimizations {
if let Some(new_id) = opt(self, proof_id) {
return new_id;
}
}
proof_id
}
/// Optimization: Remove reflexive congruence
/// Congr(p, refl) -> p where refl proves t = t
fn opt_reflexive_congr(&mut self, proof_id: ProofId) -> Option<ProofId> {
let proof = self.get(proof_id);
if let Justification::Congr {
child_proof,
proof: base_proof,
..
} = proof.justification()
{
let child = self.get(*child_proof);
if child.lhs() == child.rhs() {
return Some(*base_proof);
}
}
None
}
/// Optimization: Remove a redundant container normalization.
/// ContainerNormalize(p) -> p when normalization left the term unchanged
/// (its rhs equals the inner proof's rhs).
fn opt_redundant_container_normalize(&mut self, proof_id: ProofId) -> Option<ProofId> {
let proof = self.get(proof_id);
if let Justification::ContainerNormalize { proof: inner } = proof.justification()
&& proof.rhs() == self.get(*inner).rhs()
{
return Some(*inner);
}
None
}
/// Optimization: Remove reflexive transitivity
/// Trans(refl, p) -> p and Trans(p, refl) -> p
fn opt_reflexive_trans(&mut self, proof_id: ProofId) -> Option<ProofId> {
let proof = self.get(proof_id);
if let Justification::Trans(p1, p2) = proof.justification() {
let p1_proof = self.get(*p1);
let p2_proof = self.get(*p2);
if p1_proof.lhs() == p1_proof.rhs() {
return Some(*p2);
} else if p2_proof.lhs() == p2_proof.rhs() {
return Some(*p1);
}
}
None
}
/// Optimization: Remove reflexive symmetry
/// Sym(p) -> p when p proves t = t (identity)
fn opt_reflexive_sym(&mut self, proof_id: ProofId) -> Option<ProofId> {
let proof = self.get(proof_id);
if let Justification::Sym(inner) = proof.justification() {
let inner_proof = self.get(*inner);
// If inner proof is t = t, then Sym(t = t) is just t = t
if inner_proof.lhs() == inner_proof.rhs() {
return Some(*inner);
}
}
None
}
/// Optimization: Collapse double symmetry
/// Sym(Sym(p)) -> p
fn opt_double_sym(&mut self, proof_id: ProofId) -> Option<ProofId> {
let proof = self.get(proof_id);
if let Justification::Sym(inner) = proof.justification() {
let inner_proof = self.get(*inner);
if let Justification::Sym(inner_inner) = inner_proof.justification() {
return Some(*inner_inner);
}
}
None
}
/// Optimization: Push symmetry through transitivity
/// Sym(Trans(p1, p2)) -> Trans(Sym(p2), Sym(p1))
fn opt_sym_trans(&mut self, proof_id: ProofId) -> Option<ProofId> {
let proof = self.get(proof_id);
if let Justification::Sym(inner) = proof.justification() {
let inner_id = *inner;
let inner_proof = self.get(inner_id);
if let Justification::Trans(left, right) = inner_proof.justification() {
let left_id = *left;
let right_id = *right;
// Get the lhs/rhs values we need before any mutations
let left_proof = self.get(left_id);
let left_lhs = left_proof.lhs();
let left_rhs = left_proof.rhs();
let right_proof = self.get(right_id);
let right_lhs = right_proof.lhs();
let right_rhs = right_proof.rhs();
// Create Sym(p2): c = b
let sym_right = Proof {
proposition: Proposition::new(right_rhs, right_lhs),
justification: Justification::Sym(right_id),
};
let sym_right_id = self.add_proof(sym_right);
let sym_right_id = self.simplify_local(sym_right_id);
// Create Sym(p1): b = a
let sym_left = Proof {
proposition: Proposition::new(left_rhs, left_lhs),
justification: Justification::Sym(left_id),
};
let sym_left_id = self.add_proof(sym_left);
let sym_left_id = self.simplify_local(sym_left_id);
// Create Trans(Sym(p2), Sym(p1)): c = a
let new_trans = Proof {
proposition: Proposition::new(right_rhs, left_lhs),
justification: Justification::Trans(sym_right_id, sym_left_id),
};
// Replace current proof, mutating in place.
// Other optimizations may not need to do this if they return existing proofs.
self.id_to_proof[proof_id] = new_trans;
return Some(proof_id);
}
}
None
}
/// Map over the child proofs of this proof, producing a new proof with the same justification but updated child proofs.
pub fn map_child_proofs<F>(&mut self, proof_id: ProofId, mut f: F) -> ProofId
where
F: FnMut(&mut ProofStore, ProofId) -> ProofId,
{
let mut proof = self.id_to_proof[proof_id].clone();
let mut changed = false;
match &mut proof.justification {
Justification::Fiat => return proof_id,
Justification::Rule { premise_proofs, .. } => {
for pid in premise_proofs.iter_mut() {
let mapped = f(self, *pid);
if mapped != *pid {
*pid = mapped;
changed = true;
}
}
}
Justification::MergeFn {
old_proof,
new_proof,
..
} => {
let mapped_old = f(self, *old_proof);
let mapped_new = f(self, *new_proof);
if mapped_old != *old_proof || mapped_new != *new_proof {
*old_proof = mapped_old;
*new_proof = mapped_new;
let old = self.get(*old_proof);
let new = self.get(*new_proof);
proof.proposition.lhs = old.lhs();
proof.proposition.rhs = new.rhs();
changed = true;
}
}
Justification::Trans(left, right) => {
let mapped_left = f(self, *left);
let mapped_right = f(self, *right);
if mapped_left != *left || mapped_right != *right {
*left = mapped_left;
*right = mapped_right;
let left_proof = self.get(*left);
let right_proof = self.get(*right);
debug_assert_eq!(
left_proof.rhs(),
right_proof.lhs(),
"transitivity requires matching middle terms"
);
proof.proposition.lhs = left_proof.lhs();
proof.proposition.rhs = right_proof.rhs();
changed = true;
}
}
Justification::Sym(inner) => {
let mapped_inner = f(self, *inner);
if mapped_inner != *inner {
*inner = mapped_inner;
let inner_proof = self.get(*inner);
proof.proposition.lhs = inner_proof.rhs();
proof.proposition.rhs = inner_proof.lhs();
changed = true;
}
}
Justification::Congr {
proof: base,
child_index,
child_proof,
} => {
let mapped_base = f(self, *base);
let mapped_child = f(self, *child_proof);
if mapped_base != *base || mapped_child != *child_proof {
*base = mapped_base;
*child_proof = mapped_child;
let base_proof = self.get(*base);
let child = self.get(*child_proof);
proof.proposition.lhs = base_proof.lhs();
proof.proposition.rhs =
self.replace_term_child(base_proof.rhs(), *child_index, child.rhs());
changed = true;
}
}
Justification::ContainerNormalize { proof: inner } => {
let mapped_inner = f(self, *inner);
if mapped_inner != *inner {
*inner = mapped_inner;
let (inner_lhs, inner_rhs) = {
let p = self.get(*inner);
(p.lhs(), p.rhs())
};
proof.proposition.lhs = inner_lhs;
proof.proposition.rhs = self.normalize_container(inner_rhs);
changed = true;
}
}
// No sub-proofs to remap; the result lives in the proposition.
Justification::Eval => return proof_id,
}
if !changed {
return proof_id;
}
self.id_to_proof[proof_id] = proof;
proof_id
}
}
impl Proof {
fn map_terms_mut<F>(&mut self, mut f: F)
where
F: FnMut(TermId) -> TermId,
{
self.proposition.lhs = f(self.proposition.lhs);
self.proposition.rhs = f(self.proposition.rhs);
match &mut self.justification {
Justification::Fiat => {}
Justification::Rule {
name: _,
premise_proofs: _,
substitution,
} => {
for term_id in substitution.values_mut() {
*term_id = f(*term_id);
}
}
Justification::MergeFn {
old_proof: _,
new_proof: _,
function: _,
} => {}
Justification::Congr {
proof: _,
child_index: _,
child_proof: _,
} => {}
Justification::Trans(_, _) => {}
Justification::Sym(_) => {}
Justification::ContainerNormalize { proof: _ } => {}
// The only term (the result) lives in the proposition, already mapped.
Justification::Eval => {}
}
}
}