Skip to content

feat(formal): real-lift R-mem (kernel) — linear memory + heap tuple round-trips#666

Merged
hyperpolymath merged 1 commit into
mainfrom
claude/lucid-cray-4a22dp-rmem
Jun 27, 2026
Merged

feat(formal): real-lift R-mem (kernel) — linear memory + heap tuple round-trips#666
hyperpolymath merged 1 commit into
mainfrom
claude/lucid-cray-4a22dp-rmem

Conversation

@hyperpolymath

Copy link
Copy Markdown
Owner

Fifth rung of the real lift (formal/REAL-LIFT.adoc): linear memory — the word-addressed heap and the deterministic cell layout (field i at base+i) the codegen's elaboration nodes assume, the precondition for arrays/records/strings/floats. Axiom-free.

Branch note: on claude/lucid-cray-4a22dp-rmem (per the per-rung branch convention, since the prior …/-r2loops and base branches are now protected post-merge). Diff is against current main, so it shows only the R-mem changes.

What landed

Target IR (RealWasm.v)

  • Adds the faithful lib/wasm.ml memory instructions I32Load (off) / I32Store (off) to instr (+ dead step1 arms). Zero blast radiuswexec, cexec, compile_correct, and compile_stmt_correct all still compile unchanged.

R-mem kernel (new file RealMem.v)

  • Word-addressed memory mem := list Z (each cell one i32), with mem_get / mem_set and the heap round-trip lemmas (get-after-set same/other, length) reused directly from RealWasm's set_nth_eq / set_nth_neq / set_nth_length.
  • A bump allocator alloc n m = (length m, m ++ repeat 0 n) (base = current size) with base/length lemmas.
  • The memory-aware executor mexec — because memory ops are straight-line (no nested control), mexec is a structural Fixpoint on the instruction list, no fuel needed (unlike R2's wexec / R2-loops' cexec). I32Load off pops addr a, pushes mem[⌊a⌋+off]; I32Store off pops value then addr, writes mem[⌊a⌋+off]; everything else defers to the memory-free step1. Plus the clean structural sequencing lemma mexec_app.
  • mexec_store_then_load — storing v at a fresh in-range cell then loading it back yields v (set_nth's get-after-set, lifted through mexec).
  • mexec_pair_build_proj — build a 2-cell tuple at base (store v0 at base+0, v1 at base+1) then project both fields back: the field i at base+i heap layout, proved as a get-after-set round-trip (Nat2Z.id discharges the Z.of_nat/Z.to_nat address round-trip).

Verification

coqc 8.18, whole formal/ track re-audited against current main:

  • 20 files compile, 35 Print Assumptions reports, every one "Closed under the global context"zero axioms, no Admitted.

Docs

formal/REAL-LIFT.adoc (R-mem row ⏳ + §8 status), formal/README.adoc (RealMem row + "real lift R0 → R-mem"), docs/PROOF-NEEDS.adoc (counts 19→20 / 33→35). _CoqProject / justfile / .hypatia-ignore wire RealMem.v in.

Remaining R-mem (next sub-rungs)

Runtime memory.grow/size allocator instruction; byte-granular layout; control-bearing field expressions (fuse mexec with the R2-loops cexec); and the full source tuple/array/record + indexing/field-access compile-correctness. Then R-float / R-str / R-call per the ladder.

Refs REAL-LIFT.adoc, docs/PROOF-NEEDS.adoc (K-1 row).

🤖 Generated with Claude Code

https://claude.ai/code/session_01KPG9mEQXFyA3k7NWAzMNMr


Generated by Claude Code

…ound-trips

Fifth rung of the real lift (formal/REAL-LIFT.adoc): linear memory. Establishes
the word-addressed heap and the deterministic cell layout the codegen's
elaboration nodes assume — the precondition for arrays/records/strings/floats.
Axiom-free.

Target IR (RealWasm.v):
  - adds the faithful lib/wasm.ml memory instructions `I32Load (off)` /
    `I32Store (off)` to `instr` (+ dead `step1` arms). Zero blast radius — the
    proven wexec/cexec/compile_correct/compile_stmt_correct all still compile.

R-mem kernel (new file RealMem.v):
  - word-addressed memory `mem := list Z` (cell = one i32), `mem_get`/`mem_set`
    with the heap round-trip lemmas (get-after-set same/other, length) reused
    directly from RealWasm's `set_nth_eq`/`set_nth_neq`/`set_nth_length`.
  - a bump allocator `alloc n m = (length m, m ++ repeat 0 n)` (base = current
    size) with base/length lemmas.
  - the memory-aware executor `mexec`: because memory ops are *straight-line*
    (no nested control), `mexec` is a **structural** Fixpoint on the instruction
    list — no fuel needed, unlike R2's wexec / R2-loops' cexec. `I32Load off`
    pops addr a and pushes mem[Z.to_nat a + off]; `I32Store off` pops value then
    addr and writes mem[Z.to_nat a + off]; everything else defers to the
    memory-free `step1`. Plus the clean structural sequencing lemma `mexec_app`.
  - `mexec_store_then_load` — storing v at a fresh in-range cell then loading it
    back yields v (set_nth's get-after-set lifted through mexec).
  - `mexec_pair_build_proj` — build a 2-cell tuple at `base` (store v0 at base+0,
    v1 at base+1) then project BOTH fields back: the `field i at base+i` heap
    layout, proved as a get-after-set round-trip (Nat2Z.id discharges the
    Z.of_nat/Z.to_nat address round-trip).

Whole formal/ track re-audited: **20 files, 35 `Print Assumptions` closure
reports, all "Closed under the global context" — zero axioms, no Admitted.**

Remaining R-mem (next sub-rungs): runtime `memory.grow`/`size` allocator
instruction, byte-granular layout, control-bearing field expressions (fuse
`mexec` with the R2-loops `cexec`), and the full source
`tuple`/`array`/`record` + indexing/field-access compile-correctness. Then
R-float / R-str / R-call per the ladder.

Refs REAL-LIFT.adoc, docs/PROOF-NEEDS.adoc (K-1 row).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KPG9mEQXFyA3k7NWAzMNMr
@hyperpolymath hyperpolymath marked this pull request as ready for review June 27, 2026 20:02
@hyperpolymath hyperpolymath merged commit 59dd0f7 into main Jun 27, 2026
16 of 17 checks passed
@sonarqubecloud

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants