Commit 1c41a12
programs: prove SwapElementsSpec (swap_elements total correctness) (#137)
* codelib: generalize NumInteger's memory framing lemmas into RustStd.Frame
Every opt-0 spec proof over linear memory needs the same read-after-write
algebra: a read at B is unaffected by a write to a disjoint range at A.
Those six lemmas lived as private copies in the NumInteger spec, so each
new memory-based target (next: swap_elements, #110) would re-derive them.
Move them into CodeLib.RustStd.Frame alongside the existing
read-after-write-same lemmas:
- Mem.write{32,64}_bytes_of_disjoint (byte-level footprints)
- Mem.read{32,64}_write{32,64}_disjoint (all four width combinations)
Statements are verbatim, only namespaced under Mem.; the .toNat
disjunction shape is load-bearing because call sites discharge the side
condition with `decide`/`omega` on concrete frame addresses. The disjoint
family is deliberately not @[simp] (each rewrite has a side condition);
only the unconditional same/pages lemmas stay global simp. NumInteger now
consumes the shared copies, shrinking its spec by ~85 lines.
Phase 1 of #68.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
* programs: prove SwapElementsSpec (swap_elements total correctness)
Discharges the statement-only `SwapElementsSpec` that #110 registered with
"No proof is attempted here". The export swaps two `u64` elements of a slice
in place; the proof follows the opt-0 call graph
func4 → func3 → func0 → func1 → func2 with a per-function `TerminatesWith`
lemma at each `call`, consuming the memory-framing family from
`CodeLib.RustStd.Frame` (#131) at the leaves.
Highlights:
- func2_swap: the exchange leaf, stated as an explicit final-memory equation
so callers frame reads themselves.
- func1_swap: drives the three nested bounds-check `block`s; `i,j < len` makes
both `panic`/`unreachable` branches dead, selecting the swap.
- func4_swap: threads the shadow-stack frame (global 0 := 1048560), the fat
pointer materialised by func3, and the teardown, then decodes the swap into
the read-based postcondition.
- elemAddr_toNat / elemAddr_disjoint: no-wrap address algebra.
Two preconditions are added to the sketch, both documented in the module
docstring as *necessary for soundness*, not convenience: the shadow-stack
pointer is pinned to its initial value (`global 0 = 1048576`) and the memory
respects the wasm32 page cap (`pages ≤ 65536`, ruling out `ptr + 8*k`
wraparound). Without either, an input satisfying the original four hypotheses
makes the postcondition false. They mirror the shadow-stack pin already used
by `total_variation` and the interpreter's own in-bounds model.
`lake build` green across the whole programs package; no `sorry`, no lint.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
---------
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>1 parent 5c40dc9 commit 1c41a12
3 files changed
Lines changed: 409 additions & 109 deletions
File tree
- codelib/CodeLib/RustStd
- programs/lean/Project
- NumInteger
- SwapElements
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
19 | 24 | | |
20 | 25 | | |
21 | 26 | | |
22 | 27 | | |
23 | 28 | | |
24 | | - | |
25 | | - | |
26 | | - | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
27 | 36 | | |
28 | 37 | | |
29 | 38 | | |
| |||
53 | 62 | | |
54 | 63 | | |
55 | 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 | + | |
56 | 154 | | |
57 | 155 | | |
58 | 156 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | | - | |
18 | | - | |
| 17 | + | |
| 18 | + | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| |||
24 | 24 | | |
25 | 25 | | |
26 | 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 | 27 | | |
111 | 28 | | |
112 | 29 | | |
| |||
232 | 149 | | |
233 | 150 | | |
234 | 151 | | |
235 | | - | |
| 152 | + | |
236 | 153 | | |
237 | 154 | | |
238 | 155 | | |
| |||
261 | 178 | | |
262 | 179 | | |
263 | 180 | | |
264 | | - | |
265 | | - | |
| 181 | + | |
| 182 | + | |
266 | 183 | | |
267 | 184 | | |
268 | 185 | | |
| |||
318 | 235 | | |
319 | 236 | | |
320 | 237 | | |
321 | | - | |
| 238 | + | |
322 | 239 | | |
323 | 240 | | |
324 | 241 | | |
| |||
395 | 312 | | |
396 | 313 | | |
397 | 314 | | |
398 | | - | |
| 315 | + | |
399 | 316 | | |
400 | 317 | | |
401 | 318 | | |
402 | 319 | | |
403 | | - | |
| 320 | + | |
404 | 321 | | |
405 | 322 | | |
406 | 323 | | |
| |||
492 | 409 | | |
493 | 410 | | |
494 | 411 | | |
495 | | - | |
| 412 | + | |
496 | 413 | | |
497 | 414 | | |
498 | 415 | | |
| |||
0 commit comments