Documentation

CslibFrontier.Computability.WordRAM.Realization.Program

Flat programs from graded memory-client realizations #

Realizes.exists_program projects the existing realization certificate through Refines.executes. One emitter, scratch size and exchange constant precede every runtime argument, word width and initial memory. A statically selected coordinate of the faithful result encoding is observed by the existing Code.program; its final halt costs one instruction. The emitted body is shared by all coordinate observations.

Realizes.exists_program takes already installed arguments and frontier. Realizes.exists_init_program instead executes inputPrefix from Data.init, charging 2 * k + 5 instructions to install the first k input words and frontier. Its explicit layout, input-word and frontier-fit premises exclude free preparation and address overflow. The initial memory is retained in full. The whole-memory postcondition and register frame remain available through Realizes.exists_refines; no new execution judgment is introduced here.

Main definitions #

Main results #

References #

This semantic projection is original infrastructure over the existing flat compiler.

def Cslib.WordRAM.Realization.inputLoads {r : ℕ} (ptr : Fin r) {k : ℕ} :
(Fin k → Fin r) → ℕ → Code Basis.exact r

Install consecutive input cells in the given source registers. The terminal self-copy provides the empty case without adding a skip instruction to the native language.

def Cslib.WordRAM.Realization.inputPrefix {r k : ℕ} (frontier ptr : Fin r) (src : Fin k → Fin r) :

A native entry prefix installs the frontier and the first k input words. All data starts from the existing Data.init convention; register installation is charged.

theorem Cslib.WordRAM.Realization.inputPrefix_exec {r k w : ℕ} (frontier ptr : Fin r) (src : Fin k → Fin r) (words : List ℕ) (hi : Function.Injective src) (hp : ptr ≠ frontier) (hd : ∀ (i : Fin k), src i ≠ ptr) (hf : ∀ (i : Fin k), src i ≠ frontier) (hk : k ≤ words.length) (hw : Fits w (words.length + 1)) (hin : InputFits w words) :
∃ (y : Data r), ExecWithin w (inputPrefix frontier ptr src) (Data.init words) y (2 * k + 5) ∧ Data.Bounded w y ∧ memoryState frontier y = DataStructure.Memory.State.init words ∧ (∀ (i : Fin k), y.regs (src i) = words.getD (↑i) 0) ∧ ∀ (j : Fin r), j ≠ frontier → j ≠ ptr → (∀ (i : Fin k), src i ≠ j) → y.regs j = 0

Argument installation takes at most 2 * k + 5 native instructions. Its memory is unchanged; only the frontier, pointer and source registers can change. The frontier-fit premise also pays for the constant one and every input address.

theorem Cslib.WordRAM.Realization.Realizes.exists_program {B : Basis} {k l : ℕ} {δ α : Type} {p : δ → DataStructure.Memory.Prog α} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {work : δ → DataStructure.Memory.State → ℕ} (h : Realizes B p input output safe work) :
∃ (n : ℕ) (C : ℕ) (emit : {r : ℕ} → Fin r → (Fin k → Fin r) → (Fin (l + n + 1) → Fin r) → Code B r), ∀ {r : ℕ} (frontier : Fin r) (src : Fin k → Fin r) (tmp : Fin (l + n + 1) → Fin r), Function.Injective tmp → (∀ (i : Fin k) (j : Fin (l + n + 1)), src i ≠ tmp j) → (∀ (j : Fin (l + n + 1)), tmp j ≠ frontier) → (∀ (i : Fin k), src i ≠ frontier) → ∀ (i : Fin l) (w : ℕ) (d : δ) (x : Data r), Data.Bounded w x → x.regs ∘ src = input d → safe w d (memoryState frontier x) → (DataStructure.Memory.spec w).ValidHistory (memoryState frontier x) ((DataStructure.Memory.spec w).clientHistory (memoryState frontier x) (p d)) → ∃ (t : ℕ), Executes w ((emit frontier src tmp).program (tmp (Fin.castAdd (n + 1) i))) { pc := 0, data := x } (output ((DataStructure.Memory.spec w).clientResult (memoryState frontier x) (p d)) i) t ∧ t ≤ C * (memoryCost (p d) (memoryState frontier x) + work d (memoryState frontier x) + 1) + 1

Each static coordinate of a faithfully encoded result has one flat program, independent of the runtime input, width and memory. The selected halt observes an output register of the same emitted body; the clock includes that halt. Arguments and frontier are explicit entry premises. For an empty result register vector this makes no scalar observation.

theorem Cslib.WordRAM.Realization.Realizes.exists_init_program {k l : ℕ} {δ α : Type} {p : δ → DataStructure.Memory.Prog α} {input : δ ↪ Fin k → ℕ} {output : α ↪ Fin l → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {work : δ → DataStructure.Memory.State → ℕ} (h : Realizes Basis.exact p input output safe work) (raw : δ → List ℕ) (hlayout : ∀ (d : δ) (i : Fin k), (raw d).getD (↑i) 0 = input d i) :
∃ (n : ℕ) (C : ℕ) (emit : {r : ℕ} → Fin r → (Fin k → Fin r) → (Fin (l + n + 1) → Fin r) → Code Basis.exact r), ∀ {r : ℕ} (frontier : Fin r) (src : Fin k → Fin r) (tmp : Fin (l + n + 1) → Fin r), Function.Injective src → Function.Injective tmp → (∀ (i : Fin k) (j : Fin (l + n + 1)), src i ≠ tmp j) → (∀ (j : Fin (l + n + 1)), tmp j ≠ frontier) → (∀ (i : Fin k), src i ≠ frontier) → ∀ (i : Fin l) (w : ℕ) (d : δ), k ≤ (raw d).length → Fits w ((raw d).length + 1) → InputFits w (raw d) → safe w d (DataStructure.Memory.State.init (raw d)) → (DataStructure.Memory.spec w).ValidHistory (DataStructure.Memory.State.init (raw d)) ((DataStructure.Memory.spec w).clientHistory (DataStructure.Memory.State.init (raw d)) (p d)) → ∃ (t : ℕ), Executes w (((inputPrefix frontier (tmp (Fin.last (l + n))) src).seq (emit frontier src tmp)).program (tmp (Fin.castAdd (n + 1) i))) (State.init (raw d)) (output ((DataStructure.Memory.spec w).clientResult (DataStructure.Memory.State.init (raw d)) (p d)) i) t ∧ t ≤ C * (memoryCost (p d) (DataStructure.Memory.State.init (raw d)) + work d (DataStructure.Memory.State.init (raw d)) + 1) + 1

A flat exact-basis program starts from the native list-input convention and charges both argument installation and its trailing halt. The first k cells encode the argument; the whole input memory and its frontier are passed to the client. The fixed installation cost is absorbed into one exchange constant before every width and runtime argument.

theorem Cslib.WordRAM.Realization.Realizes.exists_unit_refines {B : Basis} {k : ℕ} {δ : Type} {input : δ ↪ Fin k → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {work : δ → DataStructure.Memory.State → ℕ} {p : δ → DataStructure.Memory.Prog Unit} {output : Unit ↪ Fin 0 → ℕ} (h : Realizes B p input output safe work) :
∃ (n : ℕ) (C : ℕ) (emit : {r : ℕ} → Fin r → (Fin k → Fin r) → (Fin (0 + n + 1) → Fin r) → Code B r), ∀ {r : ℕ} (frontier : Fin r) (src : Fin k → Fin r) (tmp : Fin (0 + n + 1) → Fin r), Function.Injective tmp → (∀ (i : Fin k) (j : Fin (0 + n + 1)), src i ≠ tmp j) → (∀ (j : Fin (0 + n + 1)), tmp j ≠ frontier) → (∀ (i : Fin k), src i ≠ frontier) → ∀ (w : ℕ) (d : δ), Refines w (fun (s : (DataStructure.Memory.spec w).State) (x : Data r) => s = memoryState frontier x ∧ Data.Bounded w x ∧ x.regs ∘ src = input d ∧ safe w d s ∧ (DataStructure.Memory.spec w).ValidHistory s ((DataStructure.Memory.spec w).clientHistory s (p d))) (p d) (fun (x : Unit) (s : (DataStructure.Memory.spec w).State) (y : Data r) => memoryState frontier y = s ∧ y.regs (tmp 0) = 0) (fun (s : (DataStructure.Memory.spec w).State) (x : Data r) => C * (memoryCost (p d) s + work d s + 1) + 1) ((emit frontier src tmp).seq (Code.op (Op.const (tmp 0) 0)))

A Unit-returning client needs no output coordinate. Execute an actual zero constant after its body to obtain a scalar observation, preserving the entire represented client state. The extra instruction fits every width, including zero.

theorem Cslib.WordRAM.Realization.Realizes.exists_unit_program {B : Basis} {k : ℕ} {δ : Type} {input : δ ↪ Fin k → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {work : δ → DataStructure.Memory.State → ℕ} {p : δ → DataStructure.Memory.Prog Unit} {output : Unit ↪ Fin 0 → ℕ} (h : Realizes B p input output safe work) :
∃ (n : ℕ) (C : ℕ) (emit : {r : ℕ} → Fin r → (Fin k → Fin r) → (Fin (0 + n + 1) → Fin r) → Code B r), ∀ {r : ℕ} (frontier : Fin r) (src : Fin k → Fin r) (tmp : Fin (0 + n + 1) → Fin r), Function.Injective tmp → (∀ (i : Fin k) (j : Fin (0 + n + 1)), src i ≠ tmp j) → (∀ (j : Fin (0 + n + 1)), tmp j ≠ frontier) → (∀ (i : Fin k), src i ≠ frontier) → ∀ (w : ℕ) (d : δ) (x : Data r), Data.Bounded w x → x.regs ∘ src = input d → safe w d (memoryState frontier x) → (DataStructure.Memory.spec w).ValidHistory (memoryState frontier x) ((DataStructure.Memory.spec w).clientHistory (memoryState frontier x) (p d)) → ∃ (t : ℕ), Executes w (((emit frontier src tmp).seq (Code.op (Op.const (tmp 0) 0))).program (tmp 0)) { pc := 0, data := x } 0 t ∧ t ≤ C * (memoryCost (p d) (memoryState frontier x) + work d (memoryState frontier x) + 1) + 2

A zero-output Unit client has a uniform flat scalar observation after a charged zero write and halt. Both instructions are included, without a positive-width premise.

theorem Cslib.WordRAM.Realization.Realizes.exists_init_unit_program {k : ℕ} {δ : Type} {input : δ ↪ Fin k → ℕ} {safe : ℕ → δ → DataStructure.Memory.State → Prop} {work : δ → DataStructure.Memory.State → ℕ} {p : δ → DataStructure.Memory.Prog Unit} {output : Unit ↪ Fin 0 → ℕ} (h : Realizes Basis.exact p input output safe work) (raw : δ → List ℕ) (hlayout : ∀ (d : δ) (i : Fin k), (raw d).getD (↑i) 0 = input d i) :
∃ (n : ℕ) (C : ℕ) (emit : {r : ℕ} → Fin r → (Fin k → Fin r) → (Fin (0 + n + 1) → Fin r) → Code Basis.exact r), ∀ {r : ℕ} (frontier : Fin r) (src : Fin k → Fin r) (tmp : Fin (0 + n + 1) → Fin r), Function.Injective src → Function.Injective tmp → (∀ (i : Fin k) (j : Fin (0 + n + 1)), src i ≠ tmp j) → (∀ (j : Fin (0 + n + 1)), tmp j ≠ frontier) → (∀ (i : Fin k), src i ≠ frontier) → ∀ (w : ℕ) (d : δ), k ≤ (raw d).length → Fits w ((raw d).length + 1) → InputFits w (raw d) → safe w d (DataStructure.Memory.State.init (raw d)) → (DataStructure.Memory.spec w).ValidHistory (DataStructure.Memory.State.init (raw d)) ((DataStructure.Memory.spec w).clientHistory (DataStructure.Memory.State.init (raw d)) (p d)) → ∃ (t : ℕ), Executes w (((inputPrefix frontier (tmp (Fin.last (0 + n))) src).seq ((emit frontier src tmp).seq (Code.op (Op.const (tmp 0) 0)))).program (tmp 0)) (State.init (raw d)) 0 t ∧ t ≤ C * (memoryCost (p d) (DataStructure.Memory.State.init (raw d)) + work d (DataStructure.Memory.State.init (raw d)) + 1) + 2

Unit-returning clients can start from Data.init too. Argument installation is charged and absorbed into the uniform exchange constant; the real zero write and trailing halt cost two further instructions.