Documentation

CslibFrontier.Computability.WordRAM.SymbolicExec.ListLoop

Loops that consume a list #

Many loops of word-RAM programs take one element of a list per iteration: the words of an array between a pointer and its end, the clauses of a formula, the indices below a bound. Their proofs by induction on the list repeat the same steps: leave the loop on the empty list, enter it on a nonempty one, run the body, and carry every fact the rest of the loop needs to the induction hypothesis.

Cslib.WordRAM.WP.loop_list is that induction, once. Its invariant I rest y holds at the head of an iteration with the elements rest left. The data there is the data x at the entry of the loop with the registers the body writes, and memory, replaced (Cslib.WordRAM.Data.havoc), so the invariant states those and nothing about the registers the body leaves alone: reads of them reduce to reads in x. The elements may take different numbers of instructions; the loop takes their sum, and two more for each test.

An invariant that says what is left to do needs no ghost state for what was done: for a loop computing f s l from registers holding s, take I rest y := ∃ s', (the registers of y hold s') ∧ f s' rest = f s l.

wram_fold I over l cost T applies the rule at a goal WP w (.loop cond body) x Q or ∃ y, ExecWithin w (.loop cond body) x y n ∧ P y, and simplifies the reads of the data at the head of an iteration in the goals exit, step and post with wram_simp.

Main definitions #

Main results #

theorem Cslib.WordRAM.WP.loop_list {B : Basis} {r w : ℕ} {cond : Cond r} {body : Code B r} {x : Data r} {Q : Data r → ℕ → Prop} {α : Type u_1} (I : List α → Data r → Prop) (T : α → ℕ) (l : List α) (init : I l x) (exit : ∀ (v : Fin r → ℕ) (m : ℕ → ℕ), I [] (Data.havoc body x v m) → ¬cond.Holds (Data.havoc body x v m)) (step : ∀ (a : α) (as : List α) (v : Fin r → ℕ) (m : ℕ → ℕ), I (a :: as) (Data.havoc body x v m) → cond.Holds (Data.havoc body x v m) ∧ WP w body (Data.havoc body x v m) fun (z : Data r) (t : ℕ) => t ≤ T a ∧ I as z) (post : ∀ (v : Fin r → ℕ) (m : ℕ → ℕ), I [] (Data.havoc body x v m) → Q (Data.havoc body x v m) ((List.map (fun (a : α) => T a + 2) l).sum + 2)) :
WP w (Code.loop cond body) x Q

A loop consuming a list l, one element per iteration. I rest y holds at the head of an iteration with rest left: the loop is left when rest is empty (exit), and otherwise its body leads, within T a instructions for the first element a, to data satisfying I for the other elements (step). The data at the head of an iteration is the entry data x with the registers the body writes, and memory, replaced. The loop takes the instructions of its bodies and two for each test.

wram_fold I over l cost T proves a loop that consumes the list l, one element per iteration, from the invariant I rest y on the elements rest left and the data y at the head of an iteration (Cslib.WordRAM.WP.loop_list), on a goal WP w (.loop cond body) x Q or ∃ y, ExecWithin w (.loop cond body) x y n ∧ P y. The body takes at most T a instructions on the element a. It leaves four goals:

  • init: the invariant holds at the entry, I l x;
  • exit: ∀ v m, I [] y → ¬cond.Holds y;
  • step: ∀ a as v m, I (a :: as) y → cond.Holds y ∧ WP w body y fun z t => t ≤ T a ∧ I as z;
  • post: ∀ v m, I [] y → Q y n, where n is the sum of T a + 2 over l, plus two.

Here y is the entry data x with the registers the body writes replaced by v and memory by m. In the last three goals the reads of y are simplified with wram_simp: a register the body does not write is read in x, and the others are v i. Those of the three that this closes are not left. The goals after the loop's goal are not touched and stay after these.