Commit e9eade4
committed
feat(formal): real-lift R2-loops — backward jumps + while simulation (settles #601)
Fourth rung of the real lift (formal/REAL-LIFT.adoc): structured control with
**backward jumps**, and the source `while` whose lowering is proved correct —
settling the #601 value-returning-tail question concretely. Axiom-free.
Target IR (RealWasm.v):
- adds the faithful lib/wasm.ml structured-control instructions `Block`,
`Loop`, `Br k`, `BrIf k` to `instr` (+ dead `step1` arms). Zero blast radius:
the proven `wexec` and `compile_correct` still compile unchanged.
R2-loops (new file RealLoop.v):
- the R2 executor `wexec : … → option (locals×stack)` cannot express "this code
branched to label k", so it cannot run `Br`. R2-loops introduces a
**branch-aware** executor `cexec` returning an `outcome`
(`ONormal`/`OBranch k`/`OTrap`). It reuses the very same fuel device R2 used
for forward control: a `Loop` consumed by `Br 0` re-enters by recursing with
decremented fuel, so `cexec` stays a structural `Fixpoint` (definitional —
`cbn`/`reflexivity`/demos still compute; a real countdown `while` evaluates
by `reflexivity`).
- `cexec_le_S`/`cexec_mono` (more fuel never changes a `Some`) +
`cexec_app_normal` (additive sequencing when the prefix ends `Normal`).
- mutual `instr`/`list instr` recursion is rejected by Coq's guard checker
(list recursion only decreases on the spine), so instead of a `branchfree`
predicate the R2 expression compiler is re-proved directly into `cexec`:
`cexec_compile` (the `cexec` analogue of `compile_correct`).
- source statements `SSkip`/`SSeq`/`SSet`/`SWhile` with a fuel-indexed
reference `run_stmt` (+ `run_stmt_mono`, `run_stmt_length`) and a lowering
`compile_stmt` taking `while` to `Block[Loop[<cond>;I32Eqz;BrIf 1;<body>;
Br 0]]`.
- `cexec_loop` — the loop-simulation lemma (induction on the while's fuel:
cond-false unwinds with `OBranch 1`, caught by the Block; cond-true runs the
body and re-enters via `OBranch 0`).
- `compile_stmt_correct` — a terminating statement's lowering runs to the same
locals, leaving the value stack **empty** (`cexec … = Some (ONormal lo' [])`).
A `while` is a statement (unit tail); this is #601 settled concretely — the
expression tail instead keeps its value on the stack.
Whole formal/ track re-audited: **19 files, 33 `Print Assumptions` closure
reports, all "Closed under the global context" — zero axioms, no Admitted.**
Next rung **R-mem** (linear memory: load/store, tuples/arrays/records).
(`for`/`break`/`continue` + a general source-divergence model reuse this cexec.)
Refs REAL-LIFT.adoc, docs/PROOF-NEEDS.adoc (K-1 row), #601.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KPG9mEQXFyA3k7NWAzMNMr1 parent bfa3c70 commit e9eade4
8 files changed
Lines changed: 802 additions & 37 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
83 | 83 | | |
84 | 84 | | |
85 | 85 | | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
15 | | - | |
| 15 | + | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| |||
70 | 70 | | |
71 | 71 | | |
72 | 72 | | |
73 | | - | |
| 73 | + | |
74 | 74 | | |
75 | 75 | | |
76 | 76 | | |
| |||
84 | 84 | | |
85 | 85 | | |
86 | 86 | | |
87 | | - | |
88 | | - | |
89 | | - | |
90 | | - | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
91 | 96 | | |
92 | 97 | | |
93 | 98 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
107 | 107 | | |
108 | 108 | | |
109 | 109 | | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
110 | 117 | | |
111 | 118 | | |
112 | 119 | | |
| |||
232 | 239 | | |
233 | 240 | | |
234 | 241 | | |
235 | | - | |
| 242 | + | |
236 | 243 | | |
237 | 244 | | |
238 | 245 | | |
239 | | - | |
| 246 | + | |
240 | 247 | | |
241 | 248 | | |
242 | 249 | | |
| |||
254 | 261 | | |
255 | 262 | | |
256 | 263 | | |
257 | | - | |
258 | | - | |
259 | | - | |
260 | | - | |
261 | | - | |
| 264 | + | |
| 265 | + | |
| 266 | + | |
| 267 | + | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
262 | 279 | | |
263 | 280 | | |
264 | 281 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
169 | 169 | | |
170 | 170 | | |
171 | 171 | | |
172 | | - | |
173 | | - | |
174 | | - | |
175 | | - | |
176 | | - | |
177 | | - | |
178 | | - | |
179 | | - | |
180 | | - | |
181 | | - | |
182 | | - | |
183 | | - | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
184 | 190 | | |
185 | 191 | | |
186 | 192 | | |
| |||
222 | 228 | | |
223 | 229 | | |
224 | 230 | | |
225 | | - | |
226 | | - | |
227 | | - | |
228 | | - | |
229 | | - | |
230 | | - | |
231 | | - | |
232 | | - | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
233 | 250 | | |
234 | 251 | | |
235 | 252 | | |
| |||
297 | 314 | | |
298 | 315 | | |
299 | 316 | | |
300 | | - | |
301 | | - | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
302 | 330 | | |
303 | 331 | | |
0 commit comments