Documentation

CslibFrontier.Computability.WordRAM.Resizable.Basic

Resizable Word RAM machine model #

This module formalizes the word-granular Resizable Word RAM machine model (RData, RInstr, RProgram, RCode, and RDemonicExec), grounding the resizable word-prefix memory model of [LLYZ23a], [LLYZ24], and [KLZ26] directly on Cslib.WordRAM instructions.

A machine configuration RData r holds r registers (regs : Fin r → ℕ), word-addressed memory (mem : ℕ → ℕ), and the 0-indexed active word frontier (frontier : ℕ, [0, frontier)):

  1. Bounds-checked access (evalResizable): Op.load d a and Op.store a s require x.regs a < x.frontier, faulting (none) otherwise.
  2. Single-word resizing primitives (RInstr.grow, RInstr.shrink): grow dst extends [0, frontier) by 1 word to [0, frontier + 1) when Fits w (frontier + 1), stores old frontier in dst, and initializes cell frontier demonically (DemonicGrow w frontier 1, matching [KLZ26], Section 5.1, PDF pp. 16–17 / printed pp. 15–16 and Footnote 10, PDF p. 18 / printed p. 17); shrink contracts [0, frontier) by 1 word when 1 ≤ frontier and zeroes cell frontier - 1. Multi-word resizing (growDirtyCode, growZeroCode, shrinkCode) is provided as derived RCode subroutines costing Θ(k + 1) instructions.
  3. Direct realize bridge (RDemonicExec.of_execBounded, RDemonicExec.of_realizesBounded): any ExecBounded w L c x.toData y T with L ≤ x.frontier lifts directly to RDemonicExec.

Main definitions #

Main results #

References #

Resizable machine data and demonic word growth #

Machine data of a word-granular Resizable Word RAM: r registers, word-addressed memory mem, and the current number of allocated w-bit words frontier (the active prefix is [0, frontier)).

  • regs : Fin r → ℕ

    Register file contents.

  • mem : ℕ → ℕ

    Word-addressed memory contents.

  • frontier : ℕ

    Number of currently allocated words L.

theorem Cslib.WordRAM.Resizable.RData.ext {r : ℕ} {x y : RData r} (regs : x.regs = y.regs) (mem : x.mem = y.mem) (frontier : x.frontier = y.frontier) :
x = y

Underlying fixed-memory WordRAM.Data r (registers and memory).

def Cslib.WordRAM.Resizable.RData.set {r : ℕ} (x : RData r) (i : Fin r) (v : ℕ) :

Set register i to v.

Write v to memory address a.

def Cslib.WordRAM.Resizable.RData.put {r : ℕ} (w : ℕ) (x : RData r) (i : Fin r) (v : ℕ) :

Set register i to v if Fits w v, and fault (none) otherwise.

@[simp]
theorem Cslib.WordRAM.Resizable.RData.set_regs {r : ℕ} (x : RData r) (i : Fin r) (v : ℕ) :
(x.set i v).regs = Function.update x.regs i v
@[simp]
theorem Cslib.WordRAM.Resizable.RData.set_mem {r : ℕ} (x : RData r) (i : Fin r) (v : ℕ) :
(x.set i v).mem = x.mem
@[simp]
theorem Cslib.WordRAM.Resizable.RData.set_frontier {r : ℕ} (x : RData r) (i : Fin r) (v : ℕ) :
@[simp]
theorem Cslib.WordRAM.Resizable.RData.set_toData {r : ℕ} (x : RData r) (i : Fin r) (v : ℕ) :
(x.set i v).toData = x.toData.set i v
@[simp]
theorem Cslib.WordRAM.Resizable.RData.write_regs {r : ℕ} (x : RData r) (a v : ℕ) :
(x.write a v).regs = x.regs
@[simp]
theorem Cslib.WordRAM.Resizable.RData.write_mem {r : ℕ} (x : RData r) (a v : ℕ) :
(x.write a v).mem = Function.update x.mem a v
@[simp]
theorem Cslib.WordRAM.Resizable.RData.write_toData {r : ℕ} (x : RData r) (a v : ℕ) :
(x.write a v).toData = x.toData.write a v
theorem Cslib.WordRAM.Resizable.RData.put_of_fits {r w : ℕ} {x : RData r} {i : Fin r} {v : ℕ} (h : Fits w v) :
put w x i v = some (x.set i v)

Word-boundedness and canonical frontier invariant at word width w: every register, memory cell, and frontier fits in w bits, and all unallocated cells a ≥ frontier hold 0.

theorem Cslib.WordRAM.Resizable.RData.Bounded.regs_fits {r w : ℕ} {x : RData r} (hx : Bounded w x) (i : Fin r) :
Fits w (x.regs i)
theorem Cslib.WordRAM.Resizable.RData.Bounded.mem_fits {r w : ℕ} {x : RData r} (hx : Bounded w x) (a : ℕ) :
Fits w (x.mem a)
theorem Cslib.WordRAM.Resizable.RData.Bounded.zero_above {r w : ℕ} {x : RData r} (hx : Bounded w x) {a : ℕ} (ha : x.frontier ≤ a) :
x.mem a = 0
theorem Cslib.WordRAM.Resizable.RData.Bounded.set {r w : ℕ} {x : RData r} (hx : Bounded w x) {i : Fin r} {v : ℕ} (hv : Fits w v) :
Bounded w (x.set i v)
theorem Cslib.WordRAM.Resizable.RData.Bounded.write {r w : ℕ} {x : RData r} (hx : Bounded w x) {a v : ℕ} (ha : a < x.frontier) (hv : Fits w v) :
Bounded w (x.write a v)
def Cslib.WordRAM.Resizable.DemonicGrow (w L k : ℕ) (mem mem' : ℕ → ℕ) :

Demonic word-granular growth of [0, L) by k words at word width w ([KuszmaulLiangZhou2026], Section 5.1): cells a < L retain their values, cells a ∈ [L, L + k) receive arbitrary w-bit words (Fits w (mem' a)), and cells a ≥ L + k hold 0.

theorem Cslib.WordRAM.Resizable.DemonicGrow.zero (w L : ℕ) {mem : ℕ → ℕ} (hz : ∀ (a : ℕ), L ≤ a → mem a = 0) :
DemonicGrow w L 0 mem mem

Zero-word growth is the identity transition on any memory zeroed above L.

theorem Cslib.WordRAM.Resizable.DemonicGrow.trans_one {w L k : ℕ} {mem₀ mem₁ mem₂ : ℕ → ℕ} (h₁ : DemonicGrow w L k mem₀ mem₁) (h₂ : DemonicGrow w (L + k) 1 mem₁ mem₂) :
DemonicGrow w L (k + 1) mem₀ mem₂

Composing a k-word demonic growth from L with a 1-word demonic growth from L + k.

theorem Cslib.WordRAM.Resizable.demonicGrow_clear (w L k : ℕ) (mem : ℕ → ℕ) (hz : ∀ (a : ℕ), L ≤ a → mem a = 0) :
DemonicGrow w L k mem (clear mem L k)

Zero-initializing [L, L + k) via clear mem L k satisfies DemonicGrow when zero above.

theorem Cslib.WordRAM.Resizable.RData.Bounded.grow {r w : ℕ} {x : RData r} (hx : Bounded w x) {dst : Fin r} {k : ℕ} {mem' : ℕ → ℕ} (hfit : Fits w (x.frontier + k)) (hgrow : DemonicGrow w x.frontier k x.mem mem') :
Bounded w { regs := Function.update x.regs dst x.frontier, mem := mem', frontier := x.frontier + k }

DemonicGrow w x.frontier k preserves RData.Bounded w when Fits w (x.frontier + k).

theorem Cslib.WordRAM.Resizable.RData.Bounded.shrink {r w : ℕ} {x : RData r} (hx : Bounded w x) {k : ℕ} (hk : k ≤ x.frontier) :
Bounded w { regs := x.regs, mem := clear x.mem (x.frontier - k) k, frontier := x.frontier - k }

Shrinking by k ≤ x.frontier words and clearing [frontier - k, frontier) keeps Bounded.

theorem Cslib.WordRAM.Resizable.clear_demonicGrow_eq {r w k : ℕ} {x : RData r} {mem' : ℕ → ℕ} (hx : RData.Bounded w x) (hdem : DemonicGrow w x.frontier k x.mem mem') :
clear mem' x.frontier k = x.mem

Clearing [frontier, frontier + k) after DemonicGrow recovers x.mem when x.Bounded w.

Bounds-checked data instruction evaluation on RData r #

def Cslib.WordRAM.Resizable.evalResizable {B : Basis} {r : ℕ} (w : ℕ) :
Op B r → RData r → Option (RData r)

Evaluate a data instruction on RData r at word size w, faulting when load/store accesses an address x.regs a ≥ x.frontier.

theorem Cslib.WordRAM.Resizable.evalResizable_of_boundedAccess {B : Basis} {r w L : ℕ} {o : Op B r} {x : RData r} {y : Data r} (hb : Op.BoundedAccess L o x.toData) (hL : L ≤ x.frontier) (he : Op.eval w o x.toData = some y) :
evalResizable w o x = some { regs := y.regs, mem := y.mem, frontier := x.frontier }

Any data instruction whose fixed-memory Op.eval succeeds with Op.BoundedAccess L x.toData for L ≤ x.frontier evaluates identically under evalResizable and preserves x.frontier.

theorem Cslib.WordRAM.Resizable.RData.Bounded.evalResizable {B : Basis} {r w : ℕ} {o : Op B r} {x y : RData r} (hx : Bounded w x) (he : Resizable.evalResizable w o x = some y) :

evalResizable preserves RData.Bounded w.

theorem Cslib.WordRAM.Resizable.evalResizable_mapSimulates {B B' : Basis} {r : ℕ} (φ : B.Simulates B') {w : ℕ} {o : Op B r} {x y : RData r} (he : evalResizable w o x = some y) :

Basis simulation transports evalResizable.

Flat and structured Resizable Word RAM programs #

Flat instructions of a word-granular Resizable Word RAM over basis B and r registers.

@[instance_reducible]
def Cslib.WordRAM.Resizable.instDecidableEqRInstr.decEq {B✝ : Basis} {r✝ : ℕ} (x✝ x✝¹ : RInstr B✝ r✝) :
Decidable (x✝ = x✝¹)
@[reducible, inline]

A flat Resizable Word RAM program is a list of RInstr B r indexed by the program counter.

Machine state of a flat Resizable Word RAM program: program counter and RData r.

  • pc : ℕ

    Program counter.

  • data : RData r

    Registers, memory, and active word frontier.

theorem Cslib.WordRAM.Resizable.RState.ext {r : ℕ} {x y : RState r} (pc : x.pc = y.pc) (data : x.data = y.data) :
x = y

Absorbing configuration for clocked execution of a flat Resizable Word RAM program.

  • running {r : ℕ} (s : RState r) : RCfg r

    Active execution at state s.

  • halted {r : ℕ} (v : ℕ) (data : RData r) : RCfg r

    Halted with scalar output v and final resizable machine data data.

  • faulted {r : ℕ} : RCfg r

    Faulted on an out-of-bounds memory access, overflow, invalid shrink, or invalid pc.

def Cslib.WordRAM.Resizable.RProgram.Contains {B : Basis} {r : ℕ} (p : RProgram B r) (base : ℕ) (q : RProgram B r) :

p contains the subprogram q starting at instruction index base.

theorem Cslib.WordRAM.Resizable.RProgram.Contains.append_left {B : Basis} {r : ℕ} {p q₁ q₂ : RProgram B r} {base : ℕ} (h : p.Contains base (q₁ ++ q₂)) :
p.Contains base q₁
theorem Cslib.WordRAM.Resizable.RProgram.Contains.append_right {B : Basis} {r : ℕ} {p q₁ q₂ : RProgram B r} {base : ℕ} (h : p.Contains base (q₁ ++ q₂)) :
p.Contains (base + List.length q₁) q₂
theorem Cslib.WordRAM.Resizable.RProgram.Contains.cons_tail {B : Basis} {r : ℕ} {p q : RProgram B r} {base : ℕ} {i : RInstr B r} (h : p.Contains base (i :: q)) :
p.Contains (base + 1) q
theorem Cslib.WordRAM.Resizable.RProgram.Contains.getElem? {B : Basis} {r : ℕ} {p q : RProgram B r} {base : ℕ} (h : p.Contains base q) {k : ℕ} (hk : k < List.length q) :
p[base + k]? = q[k]?
theorem Cslib.WordRAM.Resizable.RProgram.Contains.head {B : Basis} {r : ℕ} {p q : RProgram B r} {base : ℕ} {i : RInstr B r} (h : p.Contains base (i :: q)) :
p[base]? = some i

Whether a flat resizable instruction inspects the ambient word width w.

def Cslib.WordRAM.Resizable.RInstr.mapSimulates {B B' : Basis} {r : ℕ} (φ : B.Simulates B') :
RInstr B r → RInstr B' r

Map a basis simulation across a flat resizable instruction.

Structured programs of a Resizable Word RAM: bounds-checked data instructions, sequencing, conditionals, while loops, uniform random sampling (rand), single-word demonic grow, and single-word shrink.

def Cslib.WordRAM.Resizable.instDecidableEqRCode.decEq {B✝ : Basis} {r✝ : ℕ} (x✝ x✝¹ : RCode B✝ r✝) :
Decidable (x✝ = x✝¹)
@[instance_reducible]

Embed a standard structured WordRAM.Code B r into RCode B r.

Compiled flat instruction count of a structured RCode B r.

Assemble a structured RCode B r at instruction index pc.

@[simp]

Assembling c : RCode B r at any base index produces c.size flat instructions.

def Cslib.WordRAM.Resizable.RCode.program {B : Basis} {r : ℕ} (c : RCode B r) (out : Fin r) :

Complete flat Resizable Word RAM program: c.assemble 0 followed by halt out.

Whether a structured RCode B r inspects the ambient word width w.

def Cslib.WordRAM.Resizable.RCode.mapSimulates {B B' : Basis} {r : ℕ} (φ : B.Simulates B') :
RCode B r → RCode B' r

Map a basis simulation across RCode B r.

@[simp]
theorem Cslib.WordRAM.Resizable.RCode.size_mapSimulates {B B' : Basis} {r : ℕ} (φ : B.Simulates B') (c : RCode B r) :

Locating the parts of an assembled structured program #

theorem Cslib.WordRAM.Resizable.RProgram.Contains.seq_right {B : Basis} {r : ℕ} {p : RProgram B r} {pc : ℕ} {c₁ c₂ : RCode B r} (hp : p.Contains pc ((c₁.seq c₂).assemble pc)) :
p.Contains (pc + c₁.size) (c₂.assemble (pc + c₁.size))

The second part of an assembled sequence starts after the first.

theorem Cslib.WordRAM.Resizable.RProgram.Contains.branch_head {B : Basis} {r : ℕ} {p : RProgram B r} {pc : ℕ} {c : Cond r} {pos neg : RCode B r} (hp : p.Contains pc ((RCode.branch c pos neg).assemble pc)) :
p[pc]? = some (RInstr.branch c (pc + neg.size + 2))

An assembled branch starts with its test, which jumps over the negative arm.

theorem Cslib.WordRAM.Resizable.RProgram.Contains.branch_neg {B : Basis} {r : ℕ} {p : RProgram B r} {pc : ℕ} {c : Cond r} {pos neg : RCode B r} (hp : p.Contains pc ((RCode.branch c pos neg).assemble pc)) :
p.Contains (pc + 1) (neg.assemble (pc + 1))

The negative arm of an assembled branch follows its test.

theorem Cslib.WordRAM.Resizable.RProgram.Contains.branch_jump {B : Basis} {r : ℕ} {p : RProgram B r} {pc : ℕ} {c : Cond r} {pos neg : RCode B r} (hp : p.Contains pc ((RCode.branch c pos neg).assemble pc)) :
p[pc + 1 + neg.size]? = some (RInstr.jump (pc + neg.size + pos.size + 2))

The negative arm of an assembled branch ends with a jump over the positive arm.

theorem Cslib.WordRAM.Resizable.RProgram.Contains.branch_pos {B : Basis} {r : ℕ} {p : RProgram B r} {pc : ℕ} {c : Cond r} {pos neg : RCode B r} (hp : p.Contains pc ((RCode.branch c pos neg).assemble pc)) :
p.Contains (pc + neg.size + 2) (pos.assemble (pc + neg.size + 2))

The positive arm of an assembled branch follows the jump that ends the negative arm.

theorem Cslib.WordRAM.Resizable.RProgram.Contains.loop_exit {B : Basis} {r : ℕ} {p : RProgram B r} {pc : ℕ} {c : Cond r} {body : RCode B r} (hp : p.Contains pc ((RCode.loop c body).assemble pc)) :
p[pc + 1]? = some (RInstr.jump (pc + body.size + 3))

The test of an assembled loop is followed by its exit jump.

theorem Cslib.WordRAM.Resizable.RProgram.Contains.loop_body {B : Basis} {r : ℕ} {p : RProgram B r} {pc : ℕ} {c : Cond r} {body : RCode B r} (hp : p.Contains pc ((RCode.loop c body).assemble pc)) :
p.Contains (pc + 2) (body.assemble (pc + 2))

The body of an assembled loop follows its test and exit jump.

theorem Cslib.WordRAM.Resizable.RProgram.Contains.loop_back {B : Basis} {r : ℕ} {p : RProgram B r} {pc : ℕ} {c : Cond r} {body : RCode B r} (hp : p.Contains pc ((RCode.loop c body).assemble pc)) :
p[pc + 2 + body.size]? = some (RInstr.jump pc)

The body of an assembled loop is followed by a jump back to the test.

Big-step demonic execution (RDemonicExec) #

inductive Cslib.WordRAM.Resizable.RDemonicExec {B : Basis} {r : ℕ} (w L_peak : ℕ) :
RCode B r → RData r → ℕ → (RData r → Prop) → Prop

Big-step demonic execution judgment RDemonicExec w L_peak c x T P: at word width w, starting from x : RData r, the structured program c : RCode B r executes without faulting in at most T instructions, keeps the active hardware word frontier ≤ L_peak at every step, and establishes postcondition P y on the resulting state y : RData r for every demonic choice of newly grown memory words (DemonicGrow) on grow instructions.

theorem Cslib.WordRAM.Resizable.RDemonicExec.frontier_le {B : Basis} {r w L_peak : ℕ} {x : RData r} {c : RCode B r} {T : ℕ} {P : RData r → Prop} (h : RDemonicExec w L_peak c x T P) :
x.frontier ≤ L_peak

Every RDemonicExec requires the entry hardware frontier to satisfy x.frontier ≤ L_peak.

theorem Cslib.WordRAM.Resizable.RDemonicExec.mono_peak {B : Basis} {r w : ℕ} {x : RData r} {L₁ L₂ : ℕ} {c : RCode B r} {T : ℕ} {P : RData r → Prop} (hL : L₁ ≤ L₂) (h : RDemonicExec w L₁ c x T P) :
RDemonicExec w L₂ c x T P

Enlarge the peak hardware frontier bound L₁ ≤ L₂.

theorem Cslib.WordRAM.Resizable.RDemonicExec.seq_eq {B : Basis} {r w L_peak : ℕ} {x : RData r} {c₁ c₂ : RCode B r} {y : RData r} {T₁ T₂ : ℕ} {P : RData r → Prop} (h₁ : RDemonicExec w L_peak c₁ x T₁ fun (x : RData r) => x = y) (h₂ : RDemonicExec w L_peak c₂ y T₂ P) :
RDemonicExec w L_peak (c₁.seq c₂) x (T₁ + T₂) P

Deterministic sequential composition specialized to equality postcondition · = y.

theorem Cslib.WordRAM.Resizable.RDemonicExec.grow_seq {B : Basis} {r w L_peak : ℕ} {x : RData r} {dst : Fin r} {c : RCode B r} {T : ℕ} {P : RData r → Prop} (hL : x.frontier + 1 ≤ L_peak) (hf : Fits w (x.frontier + 1)) (hc : ∀ (m : ℕ → ℕ), DemonicGrow w x.frontier 1 x.mem m → RDemonicExec w L_peak c { regs := Function.update x.regs dst x.frontier, mem := m, frontier := x.frontier + 1 } T P) :
RDemonicExec w L_peak ((RCode.grow dst).seq c) x (1 + T) P
theorem Cslib.WordRAM.Resizable.RDemonicExec.const {B : Basis} {r w L_peak : ℕ} {x : RData r} {d : Fin r} {v : ℕ} (hL : x.frontier ≤ L_peak) (hv : Fits w v) :
RDemonicExec w L_peak (RCode.op (Op.const d v)) x 1 fun (x_1 : RData r) => x_1 = x.set d v
theorem Cslib.WordRAM.Resizable.RDemonicExec.copy {B : Basis} {r w L_peak : ℕ} {x : RData r} {d s : Fin r} (hL : x.frontier ≤ L_peak) :
RDemonicExec w L_peak (RCode.op (Op.copy d s)) x 1 fun (x_1 : RData r) => x_1 = x.set d (x.regs s)
theorem Cslib.WordRAM.Resizable.RDemonicExec.load {B : Basis} {r w L_peak : ℕ} {x : RData r} {d a : Fin r} (hL : x.frontier ≤ L_peak) (ha : x.regs a < x.frontier) :
RDemonicExec w L_peak (RCode.op (Op.load d a)) x 1 fun (x_1 : RData r) => x_1 = x.set d (x.mem (x.regs a))
theorem Cslib.WordRAM.Resizable.RDemonicExec.store {B : Basis} {r w L_peak : ℕ} {x : RData r} {a s : Fin r} (hL : x.frontier ≤ L_peak) (ha : x.regs a < x.frontier) :
RDemonicExec w L_peak (RCode.op (Op.store a s)) x 1 fun (x_1 : RData r) => x_1 = x.write (x.regs a) (x.regs s)
theorem Cslib.WordRAM.Resizable.RDemonicExec.exactBin {r w L_peak : ℕ} {x : RData r} {f : BinOp} {d i j : Fin r} (hL : x.frontier ≤ L_peak) {v : ℕ} (hev : f.evalExact (x.regs i) (x.regs j) = some v) (hfit : Fits w v) :
RDemonicExec w L_peak (RCode.op (Op.exactBin f d i j)) x 1 fun (x_1 : RData r) => x_1 = x.set d v
theorem Cslib.WordRAM.Resizable.RDemonicExec.loop_rel {B : Basis} {r w L_peak : ℕ} {cond : Cond r} {body : RCode B r} (P : ℕ → RData r → Prop) (k T : ℕ) (hc : ∀ i < k, ∀ (z : RData r), P i z → cond.Holds z.toData) (hk : ∀ (z : RData r), P k z → z.frontier ≤ L_peak ∧ ¬cond.Holds z.toData) (hbody : ∀ i < k, ∀ (z : RData r), P i z → RDemonicExec w L_peak body z T (P (i + 1))) {x₀ : RData r} (hx₀ : P 0 x₀) :
RDemonicExec w L_peak (RCode.loop cond body) x₀ (k * (T + 2) + 2) (P k)

Indexed relational loop rule for RDemonicExec.

theorem Cslib.WordRAM.Resizable.RDemonicExec.loop {B : Basis} {r w L_peak : ℕ} {cond : Cond r} {body : RCode B r} (xs : ℕ → RData r) (k T : ℕ) (hL_end : (xs k).frontier ≤ L_peak) (hc : ∀ i < k, cond.Holds (xs i).toData) (hk : ¬cond.Holds (xs k).toData) (hbody : ∀ i < k, RDemonicExec w L_peak body (xs i) T fun (x : RData r) => x = xs (i + 1)) :
RDemonicExec w L_peak (RCode.loop cond body) (xs 0) (k * (T + 2) + 2) fun (x : RData r) => x = xs k

Indexed deterministic loop rule for RDemonicExec.

theorem Cslib.WordRAM.Resizable.RDemonicExec.mapSimulates {B B' : Basis} {r w L_peak : ℕ} {x : RData r} (φ : B.Simulates B') {c : RCode B r} {T : ℕ} {P : RData r → Prop} (h : RDemonicExec w L_peak c x T P) :
RDemonicExec w L_peak (RCode.mapSimulates φ c) x T P

Transport RDemonicExec along any defined-evaluation basis simulation φ : B.Simulates B'.

theorem Cslib.WordRAM.Resizable.RDemonicExec.of_execBoundedExact {B : Basis} {r w L_peak : ℕ} {x : RData r} {L n : ℕ} {c : Code B r} {y : Data r} (hL_peak : x.frontier ≤ L_peak) (hL : L ≤ x.frontier) (h : ExecBoundedExact w L c x.toData y n) :
RDemonicExec w L_peak (RCode.ofCode c) x n fun (x_1 : RData r) => x_1 = { regs := y.regs, mem := y.mem, frontier := x.frontier }

Lift any frontier-bounded structured execution ExecBoundedExact w L c x.toData y n with L ≤ x.frontier ≤ L_peak directly to RDemonicExec on RCode.ofCode c.

theorem Cslib.WordRAM.Resizable.RDemonicExec.of_execBounded {B : Basis} {r w L_peak : ℕ} {x : RData r} {L T : ℕ} {c : Code B r} {y : Data r} (hL_peak : x.frontier ≤ L_peak) (hL : L ≤ x.frontier) (h : ExecBounded w L c x.toData y T) :
RDemonicExec w L_peak (RCode.ofCode c) x T fun (x_1 : RData r) => x_1 = { regs := y.regs, mem := y.mem, frontier := x.frontier }

Main ExecBounded → RDemonicExec bridge (RDemonicExec.of_execBounded). Every ExecBounded w L c x.toData y T whose address bound L lies at or below x.frontier lifts directly to RDemonicExec w L_peak (RCode.ofCode c) x T with unchanged hardware frontier.

theorem Cslib.WordRAM.Resizable.RDemonicExec.of_execWithin_regOnly {B : Basis} {r w L_peak : ℕ} {x : RData r} {T : ℕ} {c : Code B r} {y : Data r} (hL_peak : x.frontier ≤ L_peak) (hc : IsRegOnlyCode c) (h : ExecWithin w c x.toData y T) :
RDemonicExec w L_peak (RCode.ofCode c) x T fun (x_1 : RData r) => x_1 = { regs := y.regs, mem := y.mem, frontier := x.frontier }

Lift any register-only ExecWithin execution directly to RDemonicExec.

theorem Cslib.WordRAM.Resizable.RDemonicExec.of_realizesBounded {B : Basis} {r w L_peak : ℕ} {x : RData r} {k l n C : ℕ} {δ α : Type} {p : δ → DataStructure.Memory.Prog α} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {work : δ → DataStructure.Memory.State → ℕ} {emit : {r : ℕ} → Fin r → (Fin k → Fin r) → (Fin (l + n + 1) → Fin r) → Code B r} (hspec : Realization.Realizes.SpecBounded B p input output safe work n C fun {r : ℕ} => emit) (frontier : Fin r) (src : Fin k → Fin r) (tmp : Fin (l + n + 1) → Fin r) (hi : Function.Injective tmp) (hd : ∀ (i : Fin k) (j : Fin (l + n + 1)), src i ≠ tmp j) (hf : ∀ (j : Fin (l + n + 1)), tmp j ≠ frontier) (hs : ∀ (i : Fin k), src i ≠ frontier) (d : δ) (hx : RData.Bounded w x) (hL_peak : x.frontier ≤ L_peak) (hin : x.regs ∘ src = input d) (hsafe : safe w d (Realization.memoryState frontier x.toData)) (hv : (DataStructure.Memory.spec w).ValidHistory (Realization.memoryState frontier x.toData) ((DataStructure.Memory.spec w).clientHistory (Realization.memoryState frontier x.toData) (p d))) (hnoalloc : ((DataStructure.Memory.spec w).clientState (Realization.memoryState frontier x.toData) (p d)).frontier ≤ x.frontier) :
∃ (y : Data r), (RDemonicExec w L_peak (RCode.ofCode (emit frontier src tmp)) x (C * (Realization.memoryCost (p d) (Realization.memoryState frontier x.toData) + work d (Realization.memoryState frontier x.toData) + 1)) fun (x_1 : RData r) => x_1 = { regs := y.regs, mem := y.mem, frontier := x.frontier }) ∧ Realization.memoryState frontier y = (DataStructure.Memory.spec w).clientState (Realization.memoryState frontier x.toData) (p d) ∧ (∀ (i : Fin l), y.regs (tmp (Fin.castAdd (n + 1) i)) = output ((DataStructure.Memory.spec w).clientResult (Realization.memoryState frontier x.toData) (p d)) i) ∧ ∀ (i : Fin r), i ≠ frontier → (∀ (j : Fin (l + n + 1)), tmp j ≠ i) → y.regs i = x.regs i

Direct RealizesBounded → RDemonicExec bridge (RDemonicExec.of_realizesBounded). Any Memory.Prog client certified by RealizesBounded whose post-state frontier stays ≤ x.frontier (for example, any non-allocating segment using only Memory.read, Memory.write, arithmetic glue, conditionals, iterate, and boundedWhile when x.regs frontier ≤ x.frontier) executes directly on the Resizable Word RAM via RCode.ofCode.

Derived Θ(k + 1) bulk resize subroutines (growDirtyCode, growZeroCode, shrinkCode) #

def Cslib.WordRAM.Resizable.growDirtyCode {r : ℕ} (count result cur idx zero one : Fin r) :

Derived exact-basis (Basis.exact) subroutine looping single-word .grow regs count times in 4 * count + 5 instructions, storing the old frontier in result when 0 < regs count.

theorem Cslib.WordRAM.Resizable.growDirtyCode_not_readsWidth {r : ℕ} (count result cur idx zero one : Fin r) :
¬(growDirtyCode count result cur idx zero one).ReadsWidth
theorem Cslib.WordRAM.Resizable.growDirtyCode_exec {r w L_peak : ℕ} {count result cur idx zero one : Fin r} {x : RData r} (hx : RData.Bounded w x) (hd : [count, result, cur, idx, zero, one].Nodup) (h1 : Fits w 1) (hL_peak : x.frontier + x.regs count ≤ L_peak) (hw : Fits w (x.frontier + x.regs count)) :
have f := x.frontier; have k := x.regs count; RDemonicExec w L_peak (growDirtyCode count result cur idx zero one) x (4 * k + 5) fun (y : RData r) => RData.Bounded w y ∧ DemonicGrow w f k x.mem y.mem ∧ y.frontier = f + k ∧ (y.regs result = if k = 0 then x.regs result else f) ∧ ∀ (j : Fin r), j ≠ result → j ≠ cur → j ≠ idx → j ≠ zero → j ≠ one → y.regs j = x.regs j

Derived Θ(k + 1) demonic growth (growDirtyCode_exec).

def Cslib.WordRAM.Resizable.growZeroPost {r : ℕ} (count result cur idx zero one : Fin r) (x : RData r) :

Deterministic post-state of growZeroCode.

def Cslib.WordRAM.Resizable.growZeroCode {r : ℕ} (count result cur idx zero one : Fin r) :

Derived exact-basis (Basis.exact) subroutine growing by regs count words via 1-word .grow and immediately zeroing each newly grown word in 5 * count + 6 instructions.

theorem Cslib.WordRAM.Resizable.growZeroCode_not_readsWidth {r : ℕ} (count result cur idx zero one : Fin r) :
¬(growZeroCode count result cur idx zero one).ReadsWidth
theorem Cslib.WordRAM.Resizable.growZeroCode_exec {r w L_peak : ℕ} {count result cur idx zero one : Fin r} {x : RData r} (hx : RData.Bounded w x) (hd : [count, result, cur, idx, zero, one].Nodup) (h1 : Fits w 1) (hL_peak : x.frontier + x.regs count ≤ L_peak) (hw : Fits w (x.frontier + x.regs count)) :
RDemonicExec w L_peak (growZeroCode count result cur idx zero one) x (5 * x.regs count + 6) fun (x_1 : RData r) => x_1 = growZeroPost count result cur idx zero one x

Derived Θ(k + 1) zero-initialized growth (growZeroCode_exec).

def Cslib.WordRAM.Resizable.shrinkPost {r : ℕ} (count idx zero one : Fin r) (x : RData r) :

Deterministic post-state of shrinkCode.

def Cslib.WordRAM.Resizable.shrinkCode {r : ℕ} (count idx zero one : Fin r) :

Derived exact-basis (Basis.exact) subroutine looping single-word .shrink regs count times in 4 * count + 5 instructions (regs count ≤ frontier).

theorem Cslib.WordRAM.Resizable.shrinkCode_not_readsWidth {r : ℕ} (count idx zero one : Fin r) :
¬(shrinkCode count idx zero one).ReadsWidth
theorem Cslib.WordRAM.Resizable.shrinkCode_exec {r w L_peak : ℕ} {count idx zero one : Fin r} {x : RData r} (hx : RData.Bounded w x) (hd : [count, idx, zero, one].Nodup) (h1 : Fits w 1) (hL_peak : x.frontier ≤ L_peak) (hk_le : x.regs count ≤ x.frontier) :
RDemonicExec w L_peak (shrinkCode count idx zero one) x (4 * x.regs count + 5) fun (x_1 : RData r) => x_1 = shrinkPost count idx zero one x

Derived Θ(k + 1) multi-word shrink (shrinkCode_exec).