Composable Word-RAM data-structure subroutines #
This file unifies standalone Word-RAM data-structure realizations (DynamicProgram,
DynamicCertificate, Stateful.Amortized, Stateful.Bound) with inline subroutine composition
inside structured outer algorithms (Code.rename, ExecWithin).
A data structure is packaged once as a SubroutineCode B r_ds k_init and certified by a
SubroutineDS bundle. From that single proof, this file derives:
- Defined-evaluation basis lifting (
SubroutineCode.mapSimulates,SubroutineDS.mapSimulates,SubroutineDS.toStandard): Non-faulting executions overBasis.exactlift instruction-for-instruction toBasis.standardviaBasis.Simulates.exactStandardfromComplexity.Computability.WordRAM.MapOp, bridging exact-basis algorithms and standard-basisDynamicProgramrealizations. - Standalone
Statefulprojections:toDynamicProgram,toCertificate,toAmortized,toBound, and adaptive-client corollaries. - Relocated initialization (
initFresh_rename,initClear_rename): BothO(1)fresh initialization on zeroedData.initmemory and genericO(space)dirty-memory re-initialization along any injective register embeddingf : Fin r_ds → Fin r_caller. - Relocated inline calls and bidirectional framing (
call_rename,frame_caller): Inline execution with amortized potential accounting, DS-to-callerModifiesframing, and caller-to-DS representation/potential framing. - Potential-retaining loop rule (
ExecWithin.loop_potential_retain): Retains exit potential+ Φ zacross inner loops so stored data-structure credit survives nested loops.
Main definitions #
Cslib.WordRAM.SubroutineCode,Cslib.WordRAM.SubroutineDS: uniform relocatable machine code and unified standalone / inline specification and resource certificate for a Word-RAM data structure.Cslib.WordRAM.SubroutineCode.initFresh,Cslib.WordRAM.SubroutineCode.initClear:O(1)fresh initialization on zeroed memory and genericO(space)dirty-memory re-initialization.Cslib.WordRAM.SubroutineDS.toDynamicProgram,Cslib.WordRAM.SubroutineDS.toCertificate: standalone.standard-basisDynamicProgramandDynamicCertificateprojections.
Main results #
Cslib.WordRAM.ExecWithin.loop_potential_retain: potential-retaining loop rule for nested loops.Cslib.WordRAM.SubroutineDS.toAmortized,Cslib.WordRAM.SubroutineDS.toBound,Cslib.WordRAM.SubroutineDS.run_client_amortized,Cslib.WordRAM.SubroutineDS.run_client_bound: standalone amortized and worst-case instruction bounds overBasis.standard.Cslib.WordRAM.SubroutineDS.initFresh_rename,Cslib.WordRAM.SubroutineDS.initClear_rename,Cslib.WordRAM.SubroutineDS.call_rename,Cslib.WordRAM.SubroutineDS.frame_caller: relocated inline initialization, inline operation calls, 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
O(1) initializer when [frontier, frontier + space) is already zeroed (e.g. Data.init).
Instances For
Generic zeroing block over B clearing [baseReg, frontierReg) using incCur.
Instances For
Generic O(space) reusable initializer on arbitrary dirty memory (zeroFillCode +
initSetup).
Instances For
Translate a SubroutineCode bundle along a basis simulation.
Instances For
Lift an exact-basis SubroutineCode to Basis.standard.
Instances For
Unified standalone and subroutine specification/resource certificate for P realizing D at
word width w, base address base, runtime parameters params, and initial state s₀.
- 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.
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.
Concrete machine data determines at most one represented abstract state.
- arguments_fit {s : D.State} (o : D.Op) : D.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 : D.Op) (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. - step_exec {s : D.State} {x : Data r_ds} (o : D.Op) : self.rep s x → D.valid s o → self.argMatch o x.regs → ∃ (y : Data r_ds) (n : ℕ), Exec w P.code x y n ∧ self.decode o y.regs = some (D.step s o).2 ∧ self.rep (D.step s o).1 y ∧ Modifies { base := base, length := self.space }.footprint x.mem y.mem ∧ n + self.potential y ≤ self.charge s o + self.potential x
Execution of
P.codefor any admissible operationowhose arguments matchx.regs.
Instances For
Total instruction bound for P.initFresh.
Instances For
Projection 1: Basis lifting and standalone DynamicProgram / Stateful theorems #
Lift a SubroutineDS certificate along any defined-evaluation basis simulation.
Instances For
Lift an exact-basis SubroutineDS certificate to Basis.standard.
Instances For
Project a .standard-basis SubroutineDS to a standalone DynamicProgram.
Instances For
Project a .standard-basis SubroutineDS to a standalone DynamicCertificate.
Instances For
Project a .standard-basis SubroutineDS to a standalone Stateful.Amortized certificate,
charging + 1 per operation for DynamicProgram's trailing halt instruction.
When C.potential is identically zero, SubroutineDS projects directly to a worst-case
Stateful.Bound certificate.
Standalone adaptive-client amortized bound for any admissible client program p.
Standalone adaptive-client worst-case bound when C.potential is identically zero.
Projection 2: Fresh O(1) initialization and register relocation #
Execute P.initFresh on zeroed [base, base + C.space) in C.initTime instructions.
Relocated O(1) fresh initialization along any injective register embedding f.
Generic execution of P.zeroFillCode clearing [base, base + C.space) in 4 * C.space + 6
instructions over any basis B.
Execute P.initClear on arbitrary dirty memory in C.initTime + 4 * C.space + 6
instructions.
Relocated O(space) dirty-memory initialization along any injective register embedding f.
Projection 4: Relocated inline subroutine calls (code_rename, call_rename) #
Execute P.code.rename f inline when arguments already match (x.comap f).regs.
Execute .seq prep (P.code.rename f) inline after a caller argument-preparation block prep
that preserves persistent DS registers and memory in [base, base + C.space).
Projection 5: Caller-to-DS bidirectional framing (frame_caller) #
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).