Composable Word-RAM subroutine infrastructure #
This file provides the step-independent Word-RAM machine infrastructure for relocatable bounded-span
subroutines (SubroutineCode, SubroutineBase), along with general structured-code helpers
(appendCode, execWithin_append, ExecWithin.loop_potential_retain, Data.*).
State-transition data-structure implementations built on SubroutineBase live in
CslibFrontier/DataStructure/WordRAM/StateTransition.lean
(Cslib.DataStructure.WordRAM.StateTransitionImpl), and oblivious sequence implementations live in
CslibFrontier/DataStructure/WordRAM/Randomized/ObliviousSequence.lean
(Cslib.DataStructure.WordRAM.Randomized.ObliviousSequenceImpl).
Main definitions #
Cslib.WordRAM.SubroutineCode,Cslib.WordRAM.SubroutineBase: uniform relocatable machine code and step-independent initialization/framing bundle for a Word-RAM subroutine.Cslib.WordRAM.SubroutineCode.initFresh,Cslib.WordRAM.SubroutineCode.initClear: fresh initialization on zeroed memory, with no zero-fill pass, and generic dirty-memory re-initialization, which zero-fills the block before setup.Cslib.WordRAM.SubroutineCode.toDynamicProgram: standaloneDynamicProgramprojection.Cslib.WordRAM.SubroutineCode.initFreshProg,Cslib.WordRAM.SubroutineCode.initClearProg,Cslib.WordRAM.SubroutineCode.opProg: compiled flat programs.
Main results #
Cslib.WordRAM.ExecWithin.loop_potential_retain: potential-retaining loop rule for nested loops.Cslib.WordRAM.SubroutineCode.zeroFillCode_exec: generic4 * space + 6zero-fill execution.Cslib.WordRAM.SubroutineBase.initFresh_exec,Cslib.WordRAM.SubroutineBase.initFresh_rename,Cslib.WordRAM.SubroutineBase.initClear_exec,Cslib.WordRAM.SubroutineBase.initClear_rename,Cslib.WordRAM.SubroutineBase.frame_caller: relocated inline initialization and bidirectional register/memory framing.
References #
Potential-retaining loop rule and general helpers #
Bounded-step execution rule for appendCode.
Potential-retaining loop rule. Like ExecWithin.loop_potential, but retains the remaining
exit potential Φ z on the left of T + Φ z ≤ Φ x + 2 so that amortized data-structure credit
survives inner loops inside nested loops.
Data.comap preserves Bounded w.
Data.extend preserves Bounded w when both y and x are bounded.
Uniform machine code for a relocatable bounded-footprint data-structure subroutine over B,
independent of runtime problem size, base address, and word size w.
- allocSpan : Code B r_ds
Computes
baseReg ← frontierRegandfrontierReg ← frontierReg + spacewithout writes. - initSetup : Code B r_ds
Scalar/register initialization assuming
[base, base + space)already holds zeros. - code : Code B r_ds
Unified operation dispatcher serving all ADT operations.
- frontierReg : Fin r_ds
Bump-pointer frontier register passed in and updated on initialization.
- baseReg : Fin r_ds
Persistent base-address register of the allocated block.
Registers carrying runtime dimension parameters (
params) into initialization.- curReg : Fin r_ds
Scratch pointer register used by generic
initClearto zero[base, base + space). - zeroReg : Fin r_ds
Constant-
0scratch register used by genericinitClear. - oneReg : Fin r_ds
Constant-
1scratch register used by genericinitClear. - incCur : Op B r_ds
trueon caller-overwritable argument/scratch registers;falseon persistent registers.- output : Fin r_ds
Primary output register returned on
haltinDynamicProgram.
Instances For
Initializer when [frontier, frontier + space) is already zeroed (e.g. Data.init): runs
allocSpan then initSetup, with no zero-fill pass. SubroutineBase.initFresh_exec bounds it by
SubroutineBase.initTime instructions (spanTime + setupTime).
Instances For
Generic zeroing block over B clearing [baseReg, frontierReg) using incCur.
Instances For
Generic reusable initializer on arbitrary dirty memory (allocSpan, then zeroFillCode, then
initSetup). SubroutineBase.initClear_exec bounds it by initTime + 4 * space + 6
instructions.
Instances For
Translate a SubroutineCode bundle along a basis simulation.
Instances For
Lift an exact-basis SubroutineCode to Basis.standard.
Instances For
Project subroutine code and its calling convention to the same operation signature. The basis is retained, so this projection does not translate instructions or resource claims.
Instances For
Generic execution of P.zeroFillCode clearing [base, base + space) in 4 * space + 6
instructions over any basis B.
Step-independent specification, representation, calling-convention, and initialization bundle
for P : SubroutineCode B r_ds k_init over a state type State, operation type Op, observation
family Result, and admissibility predicate valid at word width w, base address base,
runtime parameters params, and initial state s₀.
Extended by StateTransitionImpl so that initialization (initFresh, zeroFillCode, initClear),
register relocation, standalone toDynamicProgram, and bidirectional framing (frame_caller) are
stated and proved once.
- space : ℕ
Contiguous memory footprint length
[base, base + space). - spanTime : ℕ
Instruction bound for
P.allocSpan. - setupTime : ℕ
Instruction bound for
P.initSetup. - initPotential : ℕ
Upper bound on initial concrete potential after initialization.
- charge : State → O → ℕ
Amortized instruction charge per operation (excluding trailing standalone
halt). Representation invariant linking mathematical state
sto concreteData r_ds.Concrete integer potential depending only on
Data r_ds.Standalone register encoding for
DynamicProgram.prepare.Partial argument-matching predicate for inline callers (refined by
encode).Output decoder reading typed operation observations from final registers.
Represented configurations are word-bounded.
- arguments_fit {s : State} (o : O) : valid s o → ∀ (i : Fin r_ds), P.input i = true → Fits w (self.encode o i)
Standalone encoded arguments fit in
w-bit words on admissible calls. - encode_matches (o : O) (regs : Fin r_ds → ℕ) : self.argMatch o fun (i : Fin r_ds) => if P.input i = true then self.encode o i else regs i
Full standalone
preparedelivery satisfiesargMatch. Distinctness of registers used by
zeroFillCoderelative tobaseRegandfrontierReg.Initialization parameter registers are not overwritten by
zeroFillCode.- span_exec {x : Data r_ds} : Data.Bounded w x → x.regs P.frontierReg = base → (∀ (j : Fin k_init), x.regs (P.initParams j) = params j) → ∃ (x₁ : Data r_ds), ExecWithin w P.allocSpan x x₁ self.spanTime ∧ x₁.mem = x.mem ∧ x₁.regs P.baseReg = base ∧ x₁.regs P.frontierReg = base + self.space ∧ ∀ (j : Fin k_init), x₁.regs (P.initParams j) = params j
Execution of
P.allocSpan: recordsbaseReg = baseand advancesfrontierReg. - setup_exec {x₁ : Data r_ds} : Data.Bounded w x₁ → x₁.regs P.baseReg = base → x₁.regs P.frontierReg = base + self.space → (∀ (j : Fin k_init), x₁.regs (P.initParams j) = params j) → (∀ a ∈ { base := base, length := self.space }.footprint, x₁.mem a = 0) → ∃ (y : Data r_ds), ExecWithin w P.initSetup x₁ y self.setupTime ∧ y.regs P.frontierReg = base + self.space ∧ self.rep s₀ y ∧ self.potential y ≤ self.initPotential ∧ Modifies { base := base, length := self.space }.footprint x₁.mem y.mem
Execution of
P.initSetuponce[base, base + space)holds zeros.
Instances For
Total instruction bound for P.initFresh.
Instances For
Default register-preparation function overwriting P.input registers with C.encode o.
Instances For
Lift a SubroutineBase bundle along any defined-evaluation basis simulation.
Instances For
Project a subroutine bundle to its signature-level program, retaining the instruction basis.
Instances For
Execute P.initFresh on zeroed [base, base + C.space) in C.initTime instructions.
Relocated fresh initialization along any injective register embedding f, in C.initTime
instructions.
Execute P.initClear on arbitrary dirty memory in C.initTime + 4 * C.space + 6
instructions.
Relocated dirty-memory initialization along any injective register embedding f, in
C.initTime + 4 * C.space + 6 instructions.
Any caller state transition that preserves (x'.comap f).Bounded w, leaves persistent DS
registers (P.input i = false) unchanged, and writes only to a set F disjoint from
[base, base + C.space) preserves both C.rep s (x'.comap f) and C.potential (x'.comap f).
Compiled flat Program B r_ds for fresh zeroed-memory initialization (initFresh).
Instances For
Compiled flat Program B r_ds for dirty-memory reusable initialization (initClear).
Instances For
Compiled flat Program B r_ds serving all operations of the data structure.