Documentation

CslibFrontier.Computability.WordRAM.Realization.ProgFrame

Admissibility and loop-invariant combinators for Memory.Prog #

This module provides width-independent reasoning principles for Memory.Prog clients over Memory.spec:

  1. Primitive clientState, clientResult, and memoryCost equations: closed-form reductions for Memory.read, Memory.write, Memory.alloc, Memory.allocDirty, and conditional branches (memoryCost_ite).
  2. Monotone state invariants and ValidHistory rules: MonotoneMemInv (synthesized for Memory.WellFormed, frontier lower bounds F ≤ s.frontier and F < s.frontier, and conjunctions), wellFormed_clientState, and structural ValidHistory decomposition rules.
  3. Loop-invariant and memoryCost combinators: step-invariant transport across iterate and boundedWhile, read-only loop state preservation, and constant per-step memoryCost bounds.

Main definitions #

Main results #

Primitive client state, result, and cost equations #

Running Memory.allocDirty advances the frontier by k and returns the old frontier.

@[simp]
theorem Cslib.WordRAM.Realization.state_mem_ite (c : Prop) [Decidable c] (s₁ s₂ : DataStructure.Memory.State) (x : ℕ) :
(if c then s₁ else s₂).mem x = if c then s₁.mem x else s₂.mem x

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

Push memoryCost inside a conditional Memory.Prog branch.

Monotone memory invariants and ValidHistory combinators #

A predicate on Memory.State preserved across every admissible Memory.Prog step at width w. Synthesized automatically for Memory.WellFormed w, frontier lower bounds F ≤ s.frontier and F < s.frontier, and conjunctions.

Instances

    Split ValidHistory across Memory.write a v >>= q directly in closed form.

    Split ValidHistory across dependent if h : c then p₁ h else p₂ h.

    ValidHistory of iterate given a MonotoneMemInv body certificate.

    theorem Cslib.WordRAM.Realization.validHistory_boundedWhile_inv {δ σ : Type} {w : ℕ} (Inv : DataStructure.Memory.State → Prop) [MonotoneMemInv w Inv] (guard : δ → σ → Prop) [(d : δ) → (a : σ) → Decidable (guard d a)] (body : δ × σ → DataStructure.Memory.Prog σ) (d : δ) (hstep : ∀ (a : σ) (t : DataStructure.Memory.State), Inv t → guard d a → (DataStructure.Memory.spec w).ValidHistory t ((DataStructure.Memory.spec w).clientHistory t (body (d, a)))) (n : ℕ) (a : σ) {s : DataStructure.Memory.State} (hinv : Inv s) :

    ValidHistory of boundedWhile given a MonotoneMemInv body certificate.

    Generic loop-invariant, read-only, and cost lemmas #

    theorem Cslib.WordRAM.Realization.boundedWhile_invariant_of {δ σ : Type} (guard : δ → σ → Prop) [(d : δ) → (a : σ) → Decidable (guard d a)] (body : δ × σ → DataStructure.Memory.Prog σ) (d : δ) (I : σ → DataStructure.Memory.State → Prop) (hstep : ∀ (a : σ) (s : DataStructure.Memory.State), I a s → guard d a → I ((DataStructure.Memory.spec 0).clientResult s (body (d, a))) ((DataStructure.Memory.spec 0).clientState s (body (d, a)))) (n : ℕ) (a : σ) (s : DataStructure.Memory.State) (hi : I a s) :

    An invariant I a s preserved by each guarded step of boundedWhile holds on exit.

    theorem Cslib.WordRAM.Realization.iterationSafe_of_stepInv {δ σ : Type} (body : δ × σ → DataStructure.Memory.Prog σ) (safe : ℕ → δ × σ → DataStructure.Memory.State → Prop) (w : ℕ) (d : δ) (I : ℕ → σ → DataStructure.Memory.State → Prop) (hstep : ∀ (j : ℕ) (a : σ) (s : DataStructure.Memory.State), I (j + 1) a s → safe w (d, a) s ∧ I j ((DataStructure.Memory.spec 0).clientResult s (body (d, a))) ((DataStructure.Memory.spec 0).clientState s (body (d, a)))) {n : ℕ} {a : σ} {s : DataStructure.Memory.State} (hi : I n a s) :
    iterationSafe body safe w d n a s

    A step-indexed invariant I j a s preserved by body establishes iterationSafe.

    theorem Cslib.WordRAM.Realization.iterate_invariant_of {δ σ : Type} (body : δ × σ → DataStructure.Memory.Prog σ) (d : δ) (I : ℕ → σ → DataStructure.Memory.State → Prop) (hstep : ∀ (j : ℕ) (a : σ) (s : DataStructure.Memory.State), I (j + 1) a s → I j ((DataStructure.Memory.spec 0).clientResult s (body (d, a))) ((DataStructure.Memory.spec 0).clientState s (body (d, a)))) {n : ℕ} {a : σ} {s : DataStructure.Memory.State} (hi : I n a s) :

    A step-indexed invariant I j a s preserved by body reaches I 0 after n steps of iterate.

    theorem Cslib.WordRAM.Realization.clientState_iterate_eq_of {δ σ : Type} (body : δ × σ → DataStructure.Memory.Prog σ) (d : δ) (hstep : ∀ (a : σ) (t : (DataStructure.Memory.spec 0).State), (DataStructure.Memory.spec 0).clientState t (body (d, a)) = t) (n : ℕ) (a : σ) (s : DataStructure.Memory.State) :

    A read-only iterate loop leaves clientState unchanged.

    theorem Cslib.WordRAM.Realization.clientState_boundedWhile_eq_of {δ σ : Type} (guard : δ → σ → Prop) [(d : δ) → (a : σ) → Decidable (guard d a)] (body : δ × σ → DataStructure.Memory.Prog σ) (d : δ) (hstep : ∀ (a : σ) (t : (DataStructure.Memory.spec 0).State), guard d a → (DataStructure.Memory.spec 0).clientState t (body (d, a)) = t) (n : ℕ) (a : σ) (s : DataStructure.Memory.State) :

    A read-only boundedWhile loop leaves clientState unchanged.

    theorem Cslib.WordRAM.Realization.memoryCost_iterate_le_of {δ σ : Type} (body : δ × σ → DataStructure.Memory.Prog σ) (c : ℕ) (d : δ) (hstep : ∀ (a : σ) (s : DataStructure.Memory.State), memoryCost (body (d, a)) s ≤ c) (n : ℕ) (a : σ) (s : DataStructure.Memory.State) :
    memoryCost (iterate body d n a) s ≤ c * n

    Constant upper bound on memoryCost of an iterate loop with fixed environment d.

    theorem Cslib.WordRAM.Realization.memoryCost_iterate_le_potential {δ σ : Type} (body : δ × σ → DataStructure.Memory.Prog σ) (d : δ) (I : ℕ → σ → DataStructure.Memory.State → Prop) (Φ : σ → DataStructure.Memory.State → ℕ) (c : ℕ) (hstep : ∀ (j : ℕ) (a : σ) (s : DataStructure.Memory.State), I (j + 1) a s → I j ((DataStructure.Memory.spec 0).clientResult s (body (d, a))) ((DataStructure.Memory.spec 0).clientState s (body (d, a))) ∧ memoryCost (body (d, a)) s + Φ ((DataStructure.Memory.spec 0).clientResult s (body (d, a))) ((DataStructure.Memory.spec 0).clientState s (body (d, a))) ≤ c + Φ a s) (m : ℕ) (a : σ) (s : DataStructure.Memory.State) (hi : I m a s) :
    memoryCost (iterate body d m a) s ≤ c * m + Φ a s

    Amortized memoryCost bound for iterate: if every step from a state satisfying the step-indexed invariant I costs at most c plus the drop of the potential Φ, and preserves I, then m steps cost at most c * m plus the initial potential.

    Reductions through read, write, map, and cost combinators #

    Running Memory.read returns the stored word, leaves memory unchanged, and logs the read. Add this and runClient_write with attribute [local client_simps] when simplifying runClient projections directly.

    Running Memory.write updates the cell at a, preserves the frontier, and logs the write.

    theorem Cslib.WordRAM.Realization.memoryCost_bind_le {α β : Type} {p : DataStructure.Memory.Prog α} {q : α → DataStructure.Memory.Prog β} {s : DataStructure.Memory.State} {k₁ k₂ : ℕ} (h₁ : memoryCost p s ≤ k₁) (h₂ : ∀ (x : α) (t : DataStructure.Memory.State), memoryCost (q x) t ≤ k₂) :
    memoryCost (p >>= q) s ≤ k₁ + k₂

    A read followed by a continuation of cost at most k costs at most k + 1.

    A write followed by a continuation of cost at most k costs at most k + 1.

    theorem Cslib.WordRAM.Realization.memoryCost_ite_le {α : Type} {c : Prop} [Decidable c] {p₁ p₂ : DataStructure.Memory.Prog α} {s : DataStructure.Memory.State} {k : ℕ} (h₁ : memoryCost p₁ s ≤ k) (h₂ : memoryCost p₂ s ≤ k) :
    memoryCost (if c then p₁ else p₂) s ≤ k

    ValidHistory of a counting iterate loop whose step returns its index plus one.

    theorem Cslib.WordRAM.Realization.validHistory_iterate_fst {w : ℕ} {Inv : DataStructure.Memory.State → Prop} [MonotoneMemInv w Inv] {δ β : Type} {body : δ × ℕ × β → DataStructure.Memory.Prog (ℕ × β)} {d : δ} (m i₀ : ℕ) (b₀ : β) (hstep : ∀ i < i₀ + m, ∀ (b : β) (t : DataStructure.Memory.State), Inv t → (DataStructure.Memory.spec w).ValidHistory t ((DataStructure.Memory.spec w).clientHistory t (body (d, i, b))) ∧ ((DataStructure.Memory.spec 0).clientResult t (body (d, i, b))).1 = i + 1) {s : DataStructure.Memory.State} (hinv : Inv s) :

    ValidHistory of an iterate loop whose state carries a counter in its first component.

    Single-address frames #

    p leaves memory address a unchanged from every starting state.

    theorem Cslib.WordRAM.Realization.preservesAt_bind {a : ℕ} {α β : Type} {p : DataStructure.Memory.Prog α} {k : α → DataStructure.Memory.Prog β} (hp : PreservesAt a p) (hk : ∀ (x : α), PreservesAt a (k x)) :
    theorem Cslib.WordRAM.Realization.preservesAt_map {a : ℕ} {α β : Type} (f : α → β) {p : DataStructure.Memory.Prog α} (hp : PreservesAt a p) :
    theorem Cslib.WordRAM.Realization.preservesAt_ite {a : ℕ} {α : Type} (c : Prop) [Decidable c] {p₁ p₂ : DataStructure.Memory.Prog α} (h₁ : PreservesAt a p₁) (h₂ : PreservesAt a p₂) :
    PreservesAt a (if c then p₁ else p₂)
    theorem Cslib.WordRAM.Realization.preservesAt_iterate {a : ℕ} {δ σ : Type} {body : δ × σ → DataStructure.Memory.Prog σ} {d : δ} (hstep : ∀ (st : σ), PreservesAt a (body (d, st))) (m : ℕ) (st₀ : σ) :
    PreservesAt a (iterate body d m st₀)
    theorem Cslib.WordRAM.Realization.preservesAt_iterate_nat {a : ℕ} {δ : Type} {body : δ × ℕ → DataStructure.Memory.Prog ℕ} {d : δ} {m : ℕ} (hstep : ∀ i < m, PreservesAt a (body (d, i)) ∧ ∀ (s : (DataStructure.Memory.spec 0).State), (DataStructure.Memory.spec 0).clientResult s (body (d, i)) = i + 1) :
    PreservesAt a (iterate body d m 0)

    Frame of a counting iterate loop whose step returns its index plus one: only the steps i < m that actually run need to preserve a.