Documentation

CslibFrontier.Computability.WordRAM.Realization.FramedLoop

Loops, calls and a do-block driver for framed memory triples #

Memory.FramedTriple w s p c S Q says that the memory program p, run from s at word size w, issues only admissible operations, costs at most c memory units, changes no allocated cell outside S and never lowers the frontier (Memory.Modifies S), and ends in a result and state satisfying Q. This module adds the rules that a program verified for a realize certificate needs beyond the primitive ones of Cslib.DataStructure.Memory.TripleFrame:

The tactic framed_triple walks a do block with these rules. It synthesizes the cost and compares it with the stated bound at the end; it applies a primitive rule for each read, write, allocation and return and a conditional rule for each if, and it takes a sub-program call through a triple for it in the local context: by FramedTriple.call_eq when that triple fixes the result and final state, and by FramedTriple.seq otherwise. Side conditions are tried with region_omega, which proves address, region and word-size bounds (a < s.frontier, a ∈ S, a ∉ S and S ⊆ T for regions built from Set.Ico, unions, insertions and set-builder intervals, and Fits w v) by linear arithmetic over the plain arithmetic hypotheses, unfolding the @[wram_arith] layout definitions and local definitions. The goals it cannot close remain in the order of the program: postconditions at each pure, and unsolved side conditions where they arise.

Usage #

A loop copyWordsProg a := iterate copyStep a a.len 0, whose body reads a.src + i, writes it to a.dst + i and returns i + 1, is specified and proved as follows.

theorem copyWordsProg_spec (a : CopyArgs) (s₀ : Memory.State)
    (hsrc_f : a.src + a.len ≤ s₀.frontier) (hdst_f : a.dst + a.len ≤ s₀.frontier)
    (hdisj : a.src + a.len ≤ a.dst) (hfit_src : Fits w (a.src + a.len))
    (hfit_dst : Fits w (a.dst + a.len)) (hfit_vals : ∀ i < a.len, Fits w (s₀.mem (a.src + i))) :
    iterationSafe copyStep CopyStepSafe w a a.len 0 s₀ ∧
      Memory.FramedTriple w s₀ (copyWordsProg a) (2 * a.len) (Set.Ico a.dst (a.dst + a.len))
        fun _ t => ∀ i < a.len, t.mem (a.dst + i) = s₀.mem (a.src + i) := by
  refine Memory.FramedTriple.iterate
    (fun k i t => i = k ∧ ∀ j < k, t.mem (a.dst + j) = s₀.mem (a.src + j))
    ⟨rfl, by simp⟩ (fun k hk i t ⟨hi, hdone⟩ ⟨hfr, hframe⟩ => ?_) fun _ _ h _ => h.2
  subst i
  have hsrc := hframe (a.src + k) (by omega) (by region_omega)
  have hval := hsrc ▸ hfit_vals k hk
  refine ⟨⟨by region_omega, by region_omega, by region_omega⟩, ?_⟩
  unfold copyStep
  framed_triple
  refine ⟨rfl, Nat.forall_lt_succ_right.2 ⟨fun j hj => ?_, by simp [hsrc]⟩⟩
  simp [hj.ne, hdone j hj]

The first argument of FramedTriple.iterate is the invariant, the second its initial case, the third the step, which receives the invariant and the frame Memory.Modifies S s₀ t of the loop so far and returns the body's safety predicate and its triple, and the last derives the postcondition. framed_triple closes the step's address, region and word-size conditions and its cost, and leaves the postcondition of the step.

Main results #

Main definitions #

The rules and tactics are original infrastructure over the existing memory clients.

theorem Cslib.DataStructure.Memory.FramedTriple.call {w : ℕ} {α β : Type} {s : State} {c₁ c₂ : ℕ} {S S₁ : Set ℕ} {Q : α → State → Prop} {p : Prog β} {k : β → Prog α} {Q₁ : β → State → Prop} (hp : FramedTriple w s p c₁ S₁ Q₁) (hS : S₁ ⊆ S) (hk : ∀ (b : β) (t : State), Q₁ b t → Memory.Modifies S₁ s t → FramedTriple w t (k b) c₂ S Q) :
FramedTriple w s (p >>= k) (c₁ + c₂) S Q

Call a sub-program through its own framed triple, whose modifies-set lies in the caller's. The continuation runs from every result and state the sub-program may reach, with its postcondition and its finer frame. Costs add.

theorem Cslib.DataStructure.Memory.FramedTriple.call_eq {w : ℕ} {α β : Type} {s : State} {c₁ c₂ : ℕ} {S S₁ : Set ℕ} {Q : α → State → Prop} {p : Prog β} {k : β → Prog α} {r : β} {t : State} (hp : FramedTriple w s p c₁ S₁ fun (r' : β) (t' : State) => r' = r ∧ t' = t) (hS : S₁ ⊆ S) (hk : FramedTriple w t (k r) c₂ S Q) :
FramedTriple w s (p >>= k) (c₁ + c₂) S Q

Call a sub-program whose framed triple fixes its result r and final state t; the continuation runs from them. framed_triple applies this rule with a triple from the local context.

theorem Cslib.DataStructure.Memory.FramedTriple.call_eq_tail {w : ℕ} {α : Type} {s : State} {c : ℕ} {S S₁ : Set ℕ} {Q : α → State → Prop} {p : Prog α} {r : α} {t : State} (hp : FramedTriple w s p c S₁ fun (r' : α) (t' : State) => r' = r ∧ t' = t) (hS : S₁ ⊆ S) (hQ : Q r t) :
FramedTriple w s p c S Q

Call a sub-program in tail position through its own framed triple, which fixes its result r and final state t; the postcondition is checked there. framed_triple applies this rule with a triple from the local context.

theorem Cslib.DataStructure.Memory.FramedTriple.seq {w : ℕ} {α β : Type} {s : State} {c₁ c₂ : ℕ} {S S₁ : Set ℕ} {Q : α → State → Prop} {p : Prog β} {k : β → Prog α} {Q₁ : β → State → Prop} (hp : FramedTriple w s p c₁ S₁ Q₁) (hS : S₁ ⊆ S) (hk : FramedTriple w ((spec 0).clientState s p) (k ((spec 0).clientResult s p)) c₂ S Q) :
FramedTriple w s (p >>= k) (c₁ + c₂) S Q

Call a sub-program through its own framed triple and continue at the concrete result and state that the realization interpreter assigns it, as a realization safety predicate that names intermediate states requires.

theorem Cslib.DataStructure.Memory.FramedTriple.ite_max {w : ℕ} {α : Type} {s : State} {c₁ c₂ : ℕ} {S : Set ℕ} {Q : α → State → Prop} {cond : Prop} [Decidable cond] {p₁ p₂ : Prog α} (h₁ : cond → FramedTriple w s p₁ c₁ S Q) (h₂ : ¬cond → FramedTriple w s p₂ c₂ S Q) :
FramedTriple w s (if cond then p₁ else p₂) (max c₁ c₂) S Q

A conditional whose branch costs combine by max.

theorem Cslib.DataStructure.Memory.FramedTriple.dite_max {w : ℕ} {α : Type} {s : State} {c₁ c₂ : ℕ} {S : Set ℕ} {Q : α → State → Prop} {cond : Prop} [Decidable cond] {p₁ : cond → Prog α} {p₂ : ¬cond → Prog α} (h₁ : ∀ (h : cond), FramedTriple w s (p₁ h) c₁ S Q) (h₂ : ∀ (h : ¬cond), FramedTriple w s (p₂ h) c₂ S Q) :
FramedTriple w s (if h : cond then p₁ h else p₂ h) (max c₁ c₂) S Q

A dependent conditional whose branch costs combine by max.

theorem Cslib.DataStructure.Memory.FramedTriple.drop_fresh {w : ℕ} {α : Type} {s : State} {c : ℕ} {S : Set ℕ} {Q : α → State → Prop} {p : Prog α} (h : FramedTriple w s p c (S ∪ Set.Ici s.frontier) Q) :
FramedTriple w s p c S Q

Cells at or above the initial frontier are not allocated there, so a frame may drop them. A sub-program that works in a block allocated by the caller is called with the block in the caller's frame, which this rule then removes.

theorem Cslib.DataStructure.Memory.FramedTriple.weaken {w : ℕ} {α : Type} {s : State} {c : ℕ} {S S₁ : Set ℕ} {Q : α → State → Prop} {p : Prog α} {Q₁ : α → State → Prop} (h : FramedTriple w s p c S₁ Q₁) (hS : S₁ ⊆ S) (hQ : ∀ (r : α) (t : State), Q₁ r t → Memory.Modifies S₁ s t → Q r t) :
FramedTriple w s p c S Q

Weaken a postcondition with the help of the triple's own frame, and enlarge the frame.

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

Bounded iteration. Each of the n steps, run from a state satisfying the invariant and reached from s within the frame S, meets the body's realization safety predicate and a framed triple of cost c re-establishing the invariant. The loop then meets its iteration safety predicate and a framed triple of cost c * n whose postcondition follows from the final invariant and the loop's frame. Unlike iterate_framedTriple_of_step, the invariant need not restate the frame of the loop so far.

theorem Cslib.DataStructure.Memory.FramedTriple.iterate_sum {w : ℕ} {s : State} {S : Set ℕ} {δ σ : Type} {body : δ × σ → Prog σ} {safe : ℕ → δ × σ → State → Prop} {d : δ} {n : ℕ} {a₀ : σ} {Q : σ → State → Prop} (cost : ℕ → ℕ) (inv : ℕ → σ → State → Prop) (h₀ : inv 0 a₀ s) (hstep : ∀ k < n, ∀ (a : σ) (t : State), inv k a t → Memory.Modifies S s t → safe w (d, a) t ∧ FramedTriple w t (body (d, a)) (cost k) S (inv (k + 1))) (hpost : ∀ (a : σ) (t : State), inv n a t → Memory.Modifies S s t → Q a t) :
WordRAM.Realization.iterationSafe body safe w d n a₀ s ∧ FramedTriple w s (WordRAM.Realization.iterate body d n a₀) (∑ k ∈ Finset.range n, cost k) S Q

Bounded iteration with varying step costs. As FramedTriple.iterate, but step k meets a framed triple of cost cost k, and the loop costs ∑ k ∈ Finset.range n, cost k.

theorem Cslib.DataStructure.Memory.FramedTriple.iterate_potential {w : ℕ} {s : State} {S : Set ℕ} {δ σ : Type} {body : δ × σ → Prog σ} {safe : ℕ → δ × σ → State → Prop} {d : δ} {n B : ℕ} {a₀ : σ} {Q : σ → State → Prop} (Φ : ℕ → ℕ) (inv : ℕ → σ → State → Prop) (h₀ : inv 0 a₀ s) (hstep : ∀ k < n, ∀ (a : σ) (t : State), inv k a t → Memory.Modifies S s t → safe w (d, a) t ∧ ∃ (c' : ℕ), c' + Φ (k + 1) ≤ Φ k + B ∧ FramedTriple w t (body (d, a)) c' S (inv (k + 1))) (hpost : ∀ (a : σ) (t : State), inv n a t → Memory.Modifies S s t → Q a t) :
WordRAM.Realization.iterationSafe body safe w d n a₀ s ∧ FramedTriple w s (WordRAM.Realization.iterate body d n a₀) (Φ 0 + B * n) S Q

Bounded iteration amortized by a potential of the step index. As FramedTriple.iterate, but step k meets a framed triple of some cost c' with c' + Φ (k + 1) ≤ Φ k + B, and the loop costs Φ 0 + B * n.

theorem Cslib.DataStructure.Memory.FramedTriple.allocBlockDirty_bind_at {w : ℕ} {α : Type} {s : State} {c : ℕ} {S : Set ℕ} {Q : α → State → Prop} {k fr : ℕ} {next : WordRAM.Block → Prog α} (hfr : s.frontier = fr) (hk : WordRAM.Fits w (fr + k)) (h : FramedTriple w { mem := s.mem, frontier := fr + k } (next { base := fr, length := k }) c S Q) :
FramedTriple w s (allocBlockDirty k >>= next) (c + 1) S Q

Allocate a dirty block of k cells at a frontier fr known by an equation, then continue from the enlarged state with the block ⟨fr, k⟩. Stating the frontier as fr avoids rewriting a frontier expression inside the program.

theorem Cslib.DataStructure.Memory.FramedTriple.realization_of_frontier_eq {w : ℕ} {α : Type} {s : State} {c : ℕ} {S : Set ℕ} {Q : α → State → Prop} {p : Prog α} (h : FramedTriple w s p c S fun (r : α) (s' : State) => s'.frontier = s.frontier ∧ Q r s') :
(spec w).ValidHistory s ((spec w).clientHistory s p) ∧ WordRAM.Realization.memoryCost p s ≤ c ∧ have s' := (spec 0).clientState s p; s'.frontier = s.frontier ∧ (∀ (a : ℕ), s'.mem a ≠ s.mem a → a ∈ S) ∧ Q ((spec 0).clientResult s p) s'

A framed triple whose postcondition keeps the frontier gives the older form of a memory specification: an admissible history, the cost bound, the unchanged frontier, every changed cell in S, and the postcondition, read through the realization interpreter.

theorem Cslib.DataStructure.Memory.FramedTriple.of_realization {w : ℕ} {α : Type} {s : State} {S : Set ℕ} {p : Prog α} (hv : (spec w).ValidHistory s ((spec w).clientHistory s p)) (hfr : ((spec 0).clientState s p).frontier = s.frontier) (hS : ∀ (a : ℕ), ((spec 0).clientState s p).mem a ≠ s.mem a → a ∈ S) :
FramedTriple w s p (WordRAM.Realization.memoryCost p s) S fun (r : α) (t : State) => r = (spec 0).clientResult s p ∧ t = (spec 0).clientState s p

A program specified in the older form (admissible history, unchanged frontier, every changed cell in S) meets a framed triple at its own cost that fixes its result and final state, so that FramedTriple.call_eq can call it.

theorem Cslib.DataStructure.Memory.FramedTriple.of_readOnly {w : ℕ} {α : Type} {s : State} {c : ℕ} {p : Prog α} {r : α} (hv : (spec w).ValidHistory s ((spec w).clientHistory s p)) (hst : (spec 0).clientState s p = s) (hr : (spec 0).clientResult s p = r) (hc : WordRAM.Realization.memoryCost p s ≤ c) :
FramedTriple w s p c ∅ fun (r' : α) (t : State) => r' = r ∧ t = s

A read-only program specified in the older form (admissible history, unchanged state, result r, cost at most c) meets a framed triple with an empty frame that fixes its result and leaves the state unchanged, so that FramedTriple.call_eq and framed_triple can call it.

theorem Cslib.DataStructure.Memory.FramedTriple.univ_of_realization {w : ℕ} {α : Type} {s : State} {c : ℕ} {p : Prog α} {r : α} {t : State} (hv : (spec w).ValidHistory s ((spec w).clientHistory s p)) (hc : WordRAM.Realization.memoryCost p s ≤ c) (hr : (spec 0).clientResult s p = r) (ht : (spec 0).clientState s p = t) :
FramedTriple w s p c Set.univ fun (r' : α) (t' : State) => r' = r ∧ t' = t

A program specified in the older form (admissible history, cost at most c, result r, final state t) meets a framed triple over all of memory that fixes its result and final state; no frame or frontier fact is needed, since no program lowers the frontier.

Clear every hypothesis that is a proposition other than plain arithmetic (comparisons of natural numbers or integers, word-size bounds Fits w v, and their quantifier-free combinations), so that the arithmetic closers of region_omega see only the facts they can use.

Close an address, region or word-size side condition: a < s.frontier, a ∈ S, a ∉ S, S ⊆ T for regions built from Set.Ico, ∪, insert, singletons and set-builder intervals, or Fits w v. It keeps only the plain arithmetic hypotheses (region_prune), unfolds interval membership, structure projections and local let definitions (so a region may be named by a let), and closes the goal by omega, by fits, or by wram_omega over the @[wram_arith] layout definitions and the local definitions in context.

A primitive step of framed_triple_step: a read, write, allocation or return, with its side conditions tried by region_omega.

One structural step of framed_triple on the main goal. A read, write, allocation or return that appears literally is taken by its primitive rule; a call of a sub-program goes through a framed triple for it in the local context (FramedTriple.call_eq when the triple fixes the result and final state, FramedTriple.seq otherwise, and FramedTriple.call_eq_tail for a call in tail position); then conditionals; and only then a primitive rule up to unfolding of definitions, which enters a sub-program that has no triple in context. Side conditions are tried with region_omega and left open otherwise.

Close a goal left by framed_triple, all or nothing: by reducible rfl, by omega (after dsimp only if needed, which reduces structure projections), by True.intro up to unfolding, by an assumption, by rfl, or by splitting a conjunction and closing each part the same way. Unlike trivial it never tries decide or contradiction, which are slow to fail on large invariants.

Walk the do block of a Memory.FramedTriple goal with framed_triple_step. The cost is synthesized and compared with the stated bound at the end by omega; side conditions are tried with region_omega; postconditions are tried with framed_close, and the remaining goals are left to the author in the order of the program. A sub-program call is taken through a triple for it in the local context; a call whose continuation needs the sub-program's postcondition at an arbitrary result is composed with FramedTriple.call by hand.

framed_triple without its attempts on the postconditions: walk the do block with framed_triple_step, compare the synthesized cost with the stated bound by omega, and leave every postcondition to the author. Use it when the postconditions are large invariants, on which even failing closers cost time.