Documentation

CslibFrontier.Computability.WordRAM.MemoryWindow.Dynamic

Dynamic virtual-width memory-window adapters #

MemoryWindow.compile translates a width-independent r-register program to run inside a caller-owned physical memory interval [b, b + 2 ^ w₀) when Reg.base holds b and Reg.modulus holds 2 ^ w₀. When a caller invokes that translated routine at an unknown physical word width W ≥ w₀, it computes the virtual modulus 2 ^ w₀ at runtime from an input-derived scalar size S (2 ≤ S) without reading W:

  1. NatClog.program computes ⌈log₂ S⌉ in 5 ⌈log₂ S⌉ + 5 instructions;
  2. modulusArgs multiplies ⌈log₂ S⌉ by the fixed constant widthCoeff to form w₀ = widthCoeff * ⌈log₂ S⌉ and loads base 2 in 3 instructions;
  3. Power.program computes 2 ^ w₀ in 4 w₀ + 5 instructions;
  4. MemoryWindow.normalizeRegisters reduces all r source registers modulo 2 ^ w₀ in r + 1 instructions without clearing the caller's dirty memory window;
  5. MemoryWindow.compile callee executes the translated callee in 3 * T instructions, clipping dirty loads modulo 2 ^ w₀ and preserving all memory outside [b, b + 2 ^ w₀).

This module provides both the in-place Data (r + 3) setup (dynamicSetup, where five low source registers serve as temporary scratch before register normalization) and the embedded-bank adapter (dynamicSetupBank and dynamicCallBank, where the window bank Fin (r + 3) → Fin R and five scratch registers Fin 5 → Fin R sit inside a larger caller register file Data R).

Main definitions #

Main results #

Virtual source width widthCoeff * ⌈log₂ S⌉ computed from a runtime scalar size S.

Virtual address-space reservation 2 ^ (widthCoeff * ⌈log₂ S⌉).

theorem Cslib.WordRAM.MemoryWindow.dynamicWindowSize_bounds {widthCoeff S : ℕ} (hc : 1 ≤ widthCoeff) (hS : 2 ≤ S) :
S ≤ dynamicWindowSize widthCoeff S ∧ dynamicWidth widthCoeff S < dynamicWindowSize widthCoeff S ∧ widthCoeff < dynamicWindowSize widthCoeff S

For 1 ≤ widthCoeff and 2 ≤ S, the dynamic window covers S, strictly exceeds its exponent dynamicWidth widthCoeff S, and strictly exceeds widthCoeff.

theorem Cslib.WordRAM.MemoryWindow.dynamicWidth_fits {W b widthCoeff S : ℕ} (hc : 1 ≤ widthCoeff) (hS : 2 ≤ S) (hfit : Fits W (b + 4 * dynamicWindowSize widthCoeff S)) :
dynamicWidth widthCoeff S + 2 ≤ W ∧ dynamicWidth widthCoeff S ≤ W ∧ Fits W (b + dynamicWindowSize widthCoeff S) ∧ S ≤ dynamicWindowSize widthCoeff S ∧ dynamicWidth widthCoeff S < dynamicWindowSize widthCoeff S ∧ widthCoeff < dynamicWindowSize widthCoeff S

When b + 4 * dynamicWindowSize widthCoeff S fits in physical width W (with 1 ≤ widthCoeff and 2 ≤ S), dynamicWidth widthCoeff S + 2 ≤ W, the window endpoint b + dynamicWindowSize widthCoeff S fits in W, and dynamicWindowSize_bounds holds.

def Cslib.WordRAM.MemoryWindow.modulusArgs {R : ℕ} (scratch : Fin 5 → Fin R) (widthCoeff : ℕ) :

Multiply ⌈log₂ S⌉ in scratch 1 by widthCoeff into scratch 3 and load base 2 into scratch 2 for Power.program.

def Cslib.WordRAM.MemoryWindow.withDynamicModulus {R : ℕ} (scratch : Fin 5 → Fin R) (widthCoeff : ℕ) (cont : Code Basis.standard R) :

From S in scratch 0, compute 2 ^ (widthCoeff * Nat.clog 2 S) into scratch 0 using NatClog.program and Power.program, then execute cont.

theorem Cslib.WordRAM.MemoryWindow.size_withDynamicModulus {R : ℕ} (scratch : Fin 5 → Fin R) (widthCoeff : ℕ) (cont : Code Basis.standard R) :
(withDynamicModulus scratch widthCoeff cont).size = 20 + cont.size

Computing the dynamic modulus prepends exactly 20 assembled instructions to cont.

theorem Cslib.WordRAM.MemoryWindow.readsWidth_withDynamicModulus {R : ℕ} (scratch : Fin 5 → Fin R) (widthCoeff : ℕ) (cont : Code Basis.standard R) :
(withDynamicModulus scratch widthCoeff cont).ReadsWidth ↔ cont.ReadsWidth

Dynamic modulus computation reads the physical width if and only if cont does.

theorem Cslib.WordRAM.MemoryWindow.preserves_withDynamicModulus {R : ℕ} (scratch : Fin 5 → Fin R) (widthCoeff : ℕ) {cont : Code Basis.standard R} {i : Fin R} (hscratch : ∀ (k : Fin 5), scratch k ≠ i) (hcont : Code.Preserves i cont) :
Code.Preserves i (withDynamicModulus scratch widthCoeff cont)

Dynamic modulus computation preserves every register outside scratch that cont preserves.

theorem Cslib.WordRAM.MemoryWindow.exec_withDynamicModulus {R W widthCoeff S tCont : ℕ} {scratch : Fin 5 → Fin R} (hscratch : Function.Injective scratch) (hc : 1 ≤ widthCoeff) (hS : 2 ≤ S) {cont : Code Basis.standard R} {x : Data R} {Q : Data R → Prop} (hxS : x.regs (scratch 0) = S) (hfit : Fits W (4 * dynamicWindowSize widthCoeff S)) (hcont : ∀ (z : Data R), z.mem = x.mem → z.regs (scratch 0) = dynamicWindowSize widthCoeff S → (∀ (i : Fin R), (∀ (k : Fin 5), scratch k ≠ i) → z.regs i = x.regs i) → ∃ (y : Data R), ExecWithin W cont z y tCont ∧ Q y) :
∃ (y : Data R), ExecWithin W (withDynamicModulus scratch widthCoeff cont) x y ((4 * widthCoeff + 5) * Nat.clog 2 S + 13 + tCont) ∧ Q y

Execute NatClog.program, multiply by widthCoeff, and run Power.program in scratch, delivering dynamicWindowSize widthCoeff S in scratch 0 with unchanged memory and external registers to cont.

def Cslib.WordRAM.MemoryWindow.dynamicSetup {r : ℕ} (scratch : Fin 5 → Fin (r + 3)) (widthCoeff : ℕ) :

Compute 2 ^ (widthCoeff * Nat.clog 2 S) from S in scratch 0, store it in Reg.modulus, and normalize all r source registers modulo 2 ^ (widthCoeff * Nat.clog 2 S).

theorem Cslib.WordRAM.MemoryWindow.size_dynamicSetup {r : ℕ} (scratch : Fin 5 → Fin (r + 3)) (widthCoeff : ℕ) :
(dynamicSetup scratch widthCoeff).size = r + 22

Code size of dynamicSetup.

theorem Cslib.WordRAM.MemoryWindow.dynamicSetup_not_readsWidth {r : ℕ} (scratch : Fin 5 → Fin (r + 3)) (widthCoeff : ℕ) :
¬(dynamicSetup scratch widthCoeff).ReadsWidth

dynamicSetup never reads the physical word width.

theorem Cslib.WordRAM.MemoryWindow.exec_dynamicSetup {r W b widthCoeff S : ℕ} {scratch : Fin 5 → Fin (r + 3)} (hscratch : Function.Injective scratch) (hbase : ∀ (k : Fin 5), scratch k ≠ Reg.base) (hc : 1 ≤ widthCoeff) (hS : 2 ≤ S) (x : Data (r + 3)) (hb : x.regs Reg.base = b) (hxS : x.regs (scratch 0) = S) (hfit : Fits W (b + 4 * dynamicWindowSize widthCoeff S)) :
∃ (y : Data (r + 3)), ExecWithin W (dynamicSetup scratch widthCoeff) x y ((4 * widthCoeff + 5) * Nat.clog 2 S + r + 15) ∧ y.mem = x.mem ∧ Matches (dynamicWidth widthCoeff S) b (view (dynamicWidth widthCoeff S) b y) y ∧ Data.Bounded (dynamicWidth widthCoeff S) (view (dynamicWidth widthCoeff S) b y)

Execute dynamicSetup on Data (r + 3) when Reg.base holds b and scratch 0 holds S. All memory words survive and the resulting source view is bounded at dynamicWidth widthCoeff S.

def Cslib.WordRAM.MemoryWindow.dynamicSetupBank {r R : ℕ} (winBank : Fin (r + 3) → Fin R) (scratch : Fin 5 → Fin R) (baseReg : Fin R) (widthCoeff : ℕ) :

Compute 2 ^ (widthCoeff * Nat.clog 2 S) in scratch, copy baseReg and the computed modulus into winBank, and normalize the r embedded source registers.

theorem Cslib.WordRAM.MemoryWindow.size_dynamicSetupBank {r R : ℕ} (winBank : Fin (r + 3) → Fin R) (scratch : Fin 5 → Fin R) (baseReg : Fin R) (widthCoeff : ℕ) :
(dynamicSetupBank winBank scratch baseReg widthCoeff).size = r + 23

Code size of dynamicSetupBank.

theorem Cslib.WordRAM.MemoryWindow.dynamicSetupBank_not_readsWidth {r R : ℕ} (winBank : Fin (r + 3) → Fin R) (scratch : Fin 5 → Fin R) (baseReg : Fin R) (widthCoeff : ℕ) :
¬(dynamicSetupBank winBank scratch baseReg widthCoeff).ReadsWidth

dynamicSetupBank never reads the physical word width.

theorem Cslib.WordRAM.MemoryWindow.preserves_dynamicSetupBank {r R : ℕ} (winBank : Fin (r + 3) → Fin R) (scratch : Fin 5 → Fin R) (baseReg : Fin R) (widthCoeff : ℕ) {i : Fin R} (hscratch : ∀ (k : Fin 5), scratch k ≠ i) (hwin : ∀ (j : Fin (r + 3)), winBank j ≠ i) :
Code.Preserves i (dynamicSetupBank winBank scratch baseReg widthCoeff)

dynamicSetupBank preserves every register outside scratch and winBank.

theorem Cslib.WordRAM.MemoryWindow.exec_dynamicSetupBank {r R W b widthCoeff S : ℕ} {winBank : Fin (r + 3) → Fin R} (hwin : Function.Injective winBank) {scratch : Fin 5 → Fin R} (hscratch : Function.Injective scratch) {baseReg : Fin R} (hbase : ∀ (k : Fin 5), scratch k ≠ baseReg) (hmod : winBank Reg.base ≠ scratch 0) (hc : 1 ≤ widthCoeff) (hS : 2 ≤ S) (x : Data R) (hb : x.regs baseReg = b) (hxS : x.regs (scratch 0) = S) (hfit : Fits W (b + 4 * dynamicWindowSize widthCoeff S)) :
∃ (y : Data R), ExecWithin W (dynamicSetupBank winBank scratch baseReg widthCoeff) x y ((4 * widthCoeff + 5) * Nat.clog 2 S + r + 16) ∧ y.mem = x.mem ∧ Matches (dynamicWidth widthCoeff S) b (view (dynamicWidth widthCoeff S) b (Data.comap winBank y)) (Data.comap winBank y) ∧ Data.Bounded (dynamicWidth widthCoeff S) (view (dynamicWidth widthCoeff S) b (Data.comap winBank y)) ∧ ∀ (i : Fin R), (∀ (k : Fin 5), scratch k ≠ i) → (∀ (j : Fin (r + 3)), winBank j ≠ i) → y.regs i = x.regs i

Execute dynamicSetupBank on Data R when baseReg holds b and scratch 0 holds S. All memory words and all registers outside scratch and winBank are preserved, and the embedded window bank holds normalized source registers with modulus dynamicWindowSize widthCoeff S.

def Cslib.WordRAM.MemoryWindow.dynamicCallBank {r R : ℕ} (winBank : Fin (r + 3) → Fin R) (scratch : Fin 5 → Fin R) (baseReg : Fin R) (widthCoeff : ℕ) (callee : Code Basis.standard r) :

Complete dynamic-window call in an embedded bank: compute 2 ^ (widthCoeff * Nat.clog 2 S) in scratch, initialize winBank from baseReg and scratch 0, normalize the r source registers, and execute (MemoryWindow.compile callee).rename winBank.

theorem Cslib.WordRAM.MemoryWindow.size_dynamicCallBank {r R : ℕ} (winBank : Fin (r + 3) → Fin R) (scratch : Fin 5 → Fin R) (baseReg : Fin R) (widthCoeff : ℕ) (callee : Code Basis.standard r) :
(dynamicCallBank winBank scratch baseReg widthCoeff callee).size ≤ r + 23 + 3 * callee.size

Code-size bound for dynamicCallBank.

theorem Cslib.WordRAM.MemoryWindow.dynamicCallBank_not_readsWidth {r R : ℕ} (winBank : Fin (r + 3) → Fin R) (scratch : Fin 5 → Fin R) (baseReg : Fin R) (widthCoeff : ℕ) {callee : Code Basis.standard r} (hcallee : ¬callee.ReadsWidth) :
¬(dynamicCallBank winBank scratch baseReg widthCoeff callee).ReadsWidth

dynamicCallBank never reads the physical word width when callee does not.

theorem Cslib.WordRAM.MemoryWindow.preserves_dynamicCallBank {r R : ℕ} (winBank : Fin (r + 3) → Fin R) (scratch : Fin 5 → Fin R) (baseReg : Fin R) (widthCoeff : ℕ) (callee : Code Basis.standard r) {i : Fin R} (hscratch : ∀ (k : Fin 5), scratch k ≠ i) (hwin : ∀ (j : Fin (r + 3)), winBank j ≠ i) :
Code.Preserves i (dynamicCallBank winBank scratch baseReg widthCoeff callee)

dynamicCallBank preserves every register outside scratch and winBank.

theorem Cslib.WordRAM.MemoryWindow.exec_dynamicCallBank {r R W b widthCoeff S T : ℕ} {winBank : Fin (r + 3) → Fin R} (hwin : Function.Injective winBank) {scratch : Fin 5 → Fin R} (hscratch : Function.Injective scratch) {baseReg : Fin R} (hbase : ∀ (k : Fin 5), scratch k ≠ baseReg) (hmod : winBank Reg.base ≠ scratch 0) (hc : 1 ≤ widthCoeff) (hS : 2 ≤ S) {callee : Code Basis.standard r} (hcallee : ¬callee.ReadsWidth) (x : Data R) (hb : x.regs baseReg = b) (hxS : x.regs (scratch 0) = S) (hfit : Fits W (b + 4 * dynamicWindowSize widthCoeff S)) {Post : Data r → Prop} (hsrc : ∀ (v : Data r), Data.Bounded (dynamicWidth widthCoeff S) v → (∀ (a : ℕ), v.mem a = x.mem (b + a) % dynamicWindowSize widthCoeff S) → ∃ (z : Data r), ExecWithin (dynamicWidth widthCoeff S) callee v z T ∧ Post z) :
∃ (z : Data r) (y : Data R), ExecWithin W (dynamicCallBank winBank scratch baseReg widthCoeff callee) x y ((4 * widthCoeff + 5) * Nat.clog 2 S + r + 16 + 3 * T) ∧ Matches (dynamicWidth widthCoeff S) b z (Data.comap winBank y) ∧ Post z ∧ (∀ (a : ℕ), a < b ∨ b + dynamicWindowSize widthCoeff S ≤ a → y.mem a = x.mem a) ∧ ∀ (i : Fin R), (∀ (k : Fin 5), scratch k ≠ i) → (∀ (j : Fin (r + 3)), winBank j ≠ i) → y.regs i = x.regs i

Execute a width-independent callee in a dynamic virtual-width window [b, b + 2 ^ w₀) (with w₀ = dynamicWidth widthCoeff S), preserving all memory outside [b, b + 2 ^ w₀) and all registers outside scratch and winBank.

theorem Cslib.WordRAM.MemoryWindow.view_mem_init_of_inputFits {r r₀ w b L : ℕ} {inp : List ℕ} (hi : InputFits w inp) (x : Data (r + 3)) (hinput : ∀ q < L, x.mem (b + q) = (Data.init inp).mem q) (q : ℕ) :
q < L → (view w b x).mem q = (Data.init inp).mem q

Whenever inp fits at width w and physical memory [b, b + L) equals (Data.init inp).mem, the clipped source view view w b x agrees on [0, L) with Data.init inp.