Documentation

CslibFrontier.Computability.WordRAM.Realization.LoopFrame

Loop framing, amortized potential inductions, and triple bridges for realization #

This module connects Memory.Triple and Memory.FramedTriple to the Realization interpreter (memoryCost, clientResult, clientState, ValidHistory), supplies the missing Memory.alloc / Memory.allocDirty step lemmas, and provides single-pass invariant and amortized-potential loop inductions (ℕ and abstract-witness ℚ potential) for both iterate and boundedWhile with explicit Memory.Modifies framing.

Main results #

Allocation step reductions #

Characterization of (Memory.spec w).runClient on Memory.alloc.

Memory.alloc k clears k cells at the frontier and advances it by k.

Split ValidHistory across Memory.alloc k >>= q in closed form.

Triple bridges to Realization #

Witness-indexed rational potential loop inductions #

theorem Cslib.WordRAM.Realization.iterate_rat_witness_spec {δ σ : Type} {W : Type u_1} (body : δ × σ → DataStructure.Memory.Prog σ) (safe : ℕ → δ × σ → DataStructure.Memory.State → Prop) (w : ℕ) (d : δ) (B : ℚ) (n : ℕ) (inv : ℕ → W → σ → DataStructure.Memory.State → Prop) (pot : ℕ → W → ℚ) (S : Set ℕ) (hstep : ∀ k < n, ∀ (wt : W) (a : σ) (s : DataStructure.Memory.State), inv k wt a s → safe w (d, a) s ∧ (DataStructure.Memory.spec w).ValidHistory s ((DataStructure.Memory.spec w).clientHistory s (body (d, a))) ∧ have a' := (DataStructure.Memory.spec 0).clientResult s (body (d, a)); have s' := (DataStructure.Memory.spec 0).clientState s (body (d, a)); ∃ (wt' : W), inv (k + 1) wt' a' s' ∧ ↑(memoryCost (body (d, a)) s) + pot (k + 1) wt' ≤ pot k wt + B ∧ DataStructure.Memory.Modifies S s s') (wt₀ : W) (a₀ : σ) (s₀ : DataStructure.Memory.State) (h₀ : inv 0 wt₀ a₀ s₀) :
have prog := iterate body d n a₀; have a' := (DataStructure.Memory.spec 0).clientResult s₀ prog; have s' := (DataStructure.Memory.spec 0).clientState s₀ prog; iterationSafe body safe w d n a₀ s₀ ∧ (DataStructure.Memory.spec w).ValidHistory s₀ ((DataStructure.Memory.spec w).clientHistory s₀ prog) ∧ DataStructure.Memory.Modifies S s₀ s' ∧ ∃ (wt' : W), inv n wt' a' s' ∧ ↑(memoryCost prog s₀) + pot n wt' ≤ pot 0 wt₀ + B * ↑n

Amortized rational potential-method induction for iterate parameterized by an abstract witness type W, rational step credit B : ℚ, and Memory.Modifies framing.

theorem Cslib.WordRAM.Realization.boundedWhile_rat_witness_spec {δ σ : Type} {W : Type u_1} (guard : δ → σ → Prop) [(d : δ) → (a : σ) → Decidable (guard d a)] (body : δ × σ → DataStructure.Memory.Prog σ) (safe : ℕ → δ × σ → DataStructure.Memory.State → Prop) (w : ℕ) (d : δ) (B : ℚ) (hB : 0 ≤ B) (I : W → σ → DataStructure.Memory.State → Prop) (rank : W → ℕ) (pot : W → ℚ) (S : Set ℕ) (hzero : ∀ (wt : W) (a : σ) (s : DataStructure.Memory.State), I wt a s → rank wt = 0 → ¬guard d a) (hstep : ∀ (wt : W) (a : σ) (s : DataStructure.Memory.State), I wt a s → guard d a → safe w (d, a) s ∧ (DataStructure.Memory.spec w).ValidHistory s ((DataStructure.Memory.spec w).clientHistory s (body (d, a))) ∧ have a' := (DataStructure.Memory.spec 0).clientResult s (body (d, a)); have s' := (DataStructure.Memory.spec 0).clientState s (body (d, a)); ∃ (wt' : W), I wt' a' s' ∧ ↑(memoryCost (body (d, a)) s) + pot wt' ≤ pot wt + B ∧ DataStructure.Memory.Modifies S s s' ∧ rank wt' < rank wt) (fuel : ℕ) (wt : W) (a : σ) (s : DataStructure.Memory.State) :
I wt a s → rank wt ≤ fuel → have prog := boundedWhile guard body d fuel a; have a' := (DataStructure.Memory.spec 0).clientResult s prog; have s' := (DataStructure.Memory.spec 0).clientState s prog; whileSafe guard body safe w d fuel a s ∧ (DataStructure.Memory.spec w).ValidHistory s ((DataStructure.Memory.spec w).clientHistory s prog) ∧ ¬guard d a' ∧ DataStructure.Memory.Modifies S s s' ∧ ∃ (wt' : W), I wt' a' s' ∧ ↑(memoryCost prog s) + pot wt' ≤ pot wt + B * ↑(rank wt)

Amortized rational potential-method induction for boundedWhile parameterized by an abstract witness type W, nonnegative step credit B : ℚ, rank rank : W → ℕ, and Memory.Modifies framing.

Single-pass iterate and boundedWhile ℕ-potential inductions #

theorem Cslib.WordRAM.Realization.iterate_potential_spec {δ σ : Type} (body : δ × σ → DataStructure.Memory.Prog σ) (safe : ℕ → δ × σ → DataStructure.Memory.State → Prop) (w : ℕ) (d : δ) (B n : ℕ) (inv : ℕ → σ → DataStructure.Memory.State → Prop) (pot : ℕ → σ → DataStructure.Memory.State → ℕ) (S : Set ℕ) (hstep : ∀ k < n, ∀ (a : σ) (s : DataStructure.Memory.State), inv k a s → safe w (d, a) s ∧ (DataStructure.Memory.spec w).ValidHistory s ((DataStructure.Memory.spec w).clientHistory s (body (d, a))) ∧ have a' := (DataStructure.Memory.spec 0).clientResult s (body (d, a)); have s' := (DataStructure.Memory.spec 0).clientState s (body (d, a)); memoryCost (body (d, a)) s + pot (k + 1) a' s' ≤ pot k a s + B ∧ DataStructure.Memory.Modifies S s s' ∧ inv (k + 1) a' s') (a₀ : σ) (s₀ : DataStructure.Memory.State) (h₀ : inv 0 a₀ s₀) :
have prog := iterate body d n a₀; have a' := (DataStructure.Memory.spec 0).clientResult s₀ prog; have s' := (DataStructure.Memory.spec 0).clientState s₀ prog; iterationSafe body safe w d n a₀ s₀ ∧ (DataStructure.Memory.spec w).ValidHistory s₀ ((DataStructure.Memory.spec w).clientHistory s₀ prog) ∧ memoryCost prog s₀ + pot n a' s' ≤ pot 0 a₀ s₀ + B * n ∧ DataStructure.Memory.Modifies S s₀ s' ∧ inv n a' s'

Amortized potential-method induction for iterate with Memory.Modifies framing.

theorem Cslib.WordRAM.Realization.boundedWhile_potential_spec {δ σ : Type} (guard : δ → σ → Prop) [(d : δ) → (a : σ) → Decidable (guard d a)] (body : δ × σ → DataStructure.Memory.Prog σ) (safe : ℕ → δ × σ → DataStructure.Memory.State → Prop) (w : ℕ) (d : δ) (B : ℕ) (I : σ → DataStructure.Memory.State → Prop) (rank pot : σ → DataStructure.Memory.State → ℕ) (S : Set ℕ) (hzero : ∀ (a : σ) (s : DataStructure.Memory.State), I a s → rank a s = 0 → ¬guard d a) (hstep : ∀ (a : σ) (s : DataStructure.Memory.State), I a s → guard d a → safe w (d, a) s ∧ (DataStructure.Memory.spec w).ValidHistory s ((DataStructure.Memory.spec w).clientHistory s (body (d, a))) ∧ have a' := (DataStructure.Memory.spec 0).clientResult s (body (d, a)); have s' := (DataStructure.Memory.spec 0).clientState s (body (d, a)); memoryCost (body (d, a)) s + pot a' s' ≤ pot a s + B ∧ DataStructure.Memory.Modifies S s s' ∧ I a' s' ∧ rank a' s' < rank a s) (fuel : ℕ) (a : σ) (s : DataStructure.Memory.State) :
I a s → rank a s ≤ fuel → have prog := boundedWhile guard body d fuel a; have a' := (DataStructure.Memory.spec 0).clientResult s prog; have s' := (DataStructure.Memory.spec 0).clientState s prog; whileSafe guard body safe w d fuel a s ∧ (DataStructure.Memory.spec w).ValidHistory s ((DataStructure.Memory.spec w).clientHistory s prog) ∧ ¬guard d a' ∧ I a' s' ∧ DataStructure.Memory.Modifies S s s' ∧ memoryCost prog s + pot a' s' ≤ pot a s + B * rank a s

Amortized potential-method induction for boundedWhile with Memory.Modifies framing.

theorem Cslib.WordRAM.Realization.boundedWhile_spec {δ σ : Type} (guard : δ → σ → Prop) [(d : δ) → (a : σ) → Decidable (guard d a)] (body : δ × σ → DataStructure.Memory.Prog σ) (safe : ℕ → δ × σ → DataStructure.Memory.State → Prop) (w : ℕ) (d : δ) (B : ℕ) (I : σ → DataStructure.Memory.State → Prop) (rank : σ → DataStructure.Memory.State → ℕ) (S : Set ℕ) (hzero : ∀ (a : σ) (s : DataStructure.Memory.State), I a s → rank a s = 0 → ¬guard d a) (hstep : ∀ (a : σ) (s : DataStructure.Memory.State), I a s → guard d a → safe w (d, a) s ∧ (DataStructure.Memory.spec w).ValidHistory s ((DataStructure.Memory.spec w).clientHistory s (body (d, a))) ∧ memoryCost (body (d, a)) s ≤ B ∧ have a' := (DataStructure.Memory.spec 0).clientResult s (body (d, a)); have s' := (DataStructure.Memory.spec 0).clientState s (body (d, a)); DataStructure.Memory.Modifies S s s' ∧ I a' s' ∧ rank a' s' < rank a s) (fuel : ℕ) (a : σ) (s : DataStructure.Memory.State) :
I a s → rank a s ≤ fuel → have prog := boundedWhile guard body d fuel a; have a' := (DataStructure.Memory.spec 0).clientResult s prog; have s' := (DataStructure.Memory.spec 0).clientState s prog; whileSafe guard body safe w d fuel a s ∧ (DataStructure.Memory.spec w).ValidHistory s ((DataStructure.Memory.spec w).clientHistory s prog) ∧ ¬guard d a' ∧ I a' s' ∧ DataStructure.Memory.Modifies S s s' ∧ memoryCost prog s ≤ B * rank a s

Rank-bounded loop invariant induction for boundedWhile with worst-case cost B per iteration and Memory.Modifies framing.

Refine a Memory.FramedTriple to expose equality with (Memory.spec 0).clientResult and clientState.

theorem Cslib.WordRAM.Realization.iterate_framedTriple_of_step {δ σ : Type} (body : δ × σ → DataStructure.Memory.Prog σ) (safe : ℕ → δ × σ → DataStructure.Memory.State → Prop) (w : ℕ) (d : δ) (B n : ℕ) (inv : ℕ → σ → DataStructure.Memory.State → Prop) (S : Set ℕ) (hstep : ∀ k < n, ∀ (a : σ) (t : DataStructure.Memory.State), inv k a t → ((DataStructure.Memory.spec w).ValidHistory t ((DataStructure.Memory.spec w).clientHistory t (body (d, a))) → safe w (d, a) t) ∧ DataStructure.Memory.FramedTriple w t (body (d, a)) B S (inv (k + 1))) (a₀ : σ) (s₀ : DataStructure.Memory.State) (h₀ : inv 0 a₀ s₀) :
iterationSafe body safe w d n a₀ s₀ ∧ DataStructure.Memory.FramedTriple w s₀ (iterate body d n a₀) (B * n) S (inv n)

Lift a step FramedTriple (whose safety guard may depend on the step ValidHistory) into iterationSafe and a FramedTriple for iterate body d n a₀.