Documentation

CslibFrontier.Computability.Program.WP

Weakest preconditions for structured programs #

WP I c x Q states that the structured program c leads from x to some data y in some number n of instructions such that Q y n. Its rules run through a program one instruction at a time and hand the rest of the program the data reached, so a correctness proof follows the program text: data instructions and conditionals are executed, a subprogram is replaced by its specification (WP.call), and a loop by its invariant (WP.loop, WP.loopUntil).

A specification ∃ y, ExecWithin I c x y T ∧ P y is WP I c x fun y n => n ≤ T ∧ P y (WP.exists_execWithin).

Main definitions #

Main results #

References #

This generalizes Cslib.WordRAM.WP.Exact to any instruction set: the count n is the exact instruction count of an execution, where Cslib.WordRAM.WP holds of any bound on it. A postcondition that bounds the count from above, as in WP.exists_execWithin, reads the same in both.

def Cslib.Program.WP (I : InstructionSet) (c : Code I.Op I.Test) (x : I.Data) (Q : I.Data → ℕ → Prop) :

WP I c x Q: the program c leads from x to data y, in n instructions, with Q y n.

theorem Cslib.Program.WP.exists_execWithin {I : InstructionSet} {c : Code I.Op I.Test} {x : I.Data} {T : ℕ} {P : I.Data → Prop} (h : WP I c x fun (y : I.Data) (n : ℕ) => n ≤ T ∧ P y) :
∃ (y : I.Data), ExecWithin I c x y T ∧ P y

A specification is a weakest precondition whose postcondition bounds the count.

theorem Cslib.Program.WP.mono {I : InstructionSet} {c : Code I.Op I.Test} {x : I.Data} {Q Q' : I.Data → ℕ → Prop} (h : WP I c x Q) (hQ : ∀ (y : I.Data) (n : ℕ), Q y n → Q' y n) :
WP I c x Q'

Weaken the postcondition.

theorem Cslib.Program.WP.skip {I : InstructionSet} {x : I.Data} {Q : I.Data → ℕ → Prop} (h : Q x 0) :

skip changes nothing and costs nothing.

theorem Cslib.Program.WP.op {I : InstructionSet} {x y : I.Data} {Q : I.Data → ℕ → Prop} {o : I.Op} (ho : I.eval o x = some y) (h : Q y 1) :
WP I (Code.op o) x Q

A data instruction.

theorem Cslib.Program.WP.seq {I : InstructionSet} {c₁ c₂ : Code I.Op I.Test} {x : I.Data} {Q : I.Data → ℕ → Prop} (h : WP I c₁ x fun (y : I.Data) (m : ℕ) => WP I c₂ y fun (z : I.Data) (n : ℕ) => Q z (n + m)) :
WP I (c₁.seq c₂) x Q

Sequencing: run c₁, then run c₂ from the data it reaches. The count of c₁ is added last, as in Cslib.WordRAM.WP.seq, so that the counts of a sequence accumulate as (n + 1) + 1 with the numerals on the right and not as 1 + (1 + n): deciding whether such a sum is a given variable takes time exponential in its length.

theorem Cslib.Program.WP.branch_pos {I : InstructionSet} {pos neg : Code I.Op I.Test} {t : I.Test} {x : I.Data} {Q : I.Data → ℕ → Prop} (ht : I.Holds t x) (h : WP I pos x fun (y : I.Data) (n : ℕ) => Q y (n + 1)) :
WP I (Code.branch t pos neg) x Q

A conditional whose test holds.

theorem Cslib.Program.WP.branch_neg {I : InstructionSet} {pos neg : Code I.Op I.Test} {t : I.Test} {x : I.Data} {Q : I.Data → ℕ → Prop} (ht : ¬I.Holds t x) (h : WP I neg x fun (y : I.Data) (n : ℕ) => Q y (n + 2)) :
WP I (Code.branch t pos neg) x Q

A conditional whose test fails.

theorem Cslib.Program.WP.branch {I : InstructionSet} {pos neg : Code I.Op I.Test} {t : I.Test} {x : I.Data} {Q : I.Data → ℕ → Prop} (hpos : I.Holds t x → WP I pos x fun (y : I.Data) (n : ℕ) => Q y (n + 1)) (hneg : ¬I.Holds t x → WP I neg x fun (y : I.Data) (n : ℕ) => Q y (n + 2)) :
WP I (Code.branch t pos neg) x Q

A conditional, by cases on its test.

theorem Cslib.Program.WP.call {I : InstructionSet} {c : Code I.Op I.Test} {x : I.Data} {Q : I.Data → ℕ → Prop} {T : ℕ} {R : I.Data → Prop} (spec : ∃ (y : I.Data), ExecWithin I c x y T ∧ R y) (h : ∀ (y : I.Data), R y → ∀ n ≤ T, Q y n) :
WP I c x Q

A subprogram, by its specification: continue from any data and count it allows.

theorem Cslib.Program.WP.loopUntil_exit {I : InstructionSet} {body : Code I.Op I.Test} {t : I.Test} {x : I.Data} {Q : I.Data → ℕ → Prop} (ht : I.Holds t x) (h : Q x 1) :
WP I (Code.loopUntil t body) x Q

Leaving a loop that runs until its test holds.

theorem Cslib.Program.WP.loopUntil_iter {I : InstructionSet} {body : Code I.Op I.Test} {t : I.Test} {x : I.Data} {Q : I.Data → ℕ → Prop} (ht : ¬I.Holds t x) (h : WP I body x fun (y : I.Data) (m : ℕ) => WP I (Code.loopUntil t body) y fun (z : I.Data) (n : ℕ) => Q z (n + m + 2)) :
WP I (Code.loopUntil t body) x Q

One iteration of a loop that runs until its test holds, then the loop again. The count of the body is added last, as in WP.seq.

theorem Cslib.Program.WP.loop_exit {I : InstructionSet} {body : Code I.Op I.Test} {t : I.Test} {x : I.Data} {Q : I.Data → ℕ → Prop} (ht : ¬I.Holds t x) (h : Q x 2) :
WP I (Code.loop t body) x Q

Leaving a loop that runs while its test holds.

theorem Cslib.Program.WP.loop_iter {I : InstructionSet} {body : Code I.Op I.Test} {t : I.Test} {x : I.Data} {Q : I.Data → ℕ → Prop} (ht : I.Holds t x) (h : WP I body x fun (y : I.Data) (m : ℕ) => WP I (Code.loop t body) y fun (z : I.Data) (n : ℕ) => Q z (n + m + 2)) :
WP I (Code.loop t body) x Q

One iteration of a loop that runs while its test holds, then the loop again. The count of the body is added last, as in WP.seq.

theorem Cslib.Program.WP.loopUntil {I : InstructionSet} {body : Code I.Op I.Test} {t : I.Test} {x : I.Data} {Q : I.Data → ℕ → Prop} (P : I.Data → Prop) (μ : I.Data → ℕ) (T : ℕ) (hbody : ∀ (x : I.Data), P x → ¬I.Holds t x → WP I body x fun (y : I.Data) (n : ℕ) => n ≤ T ∧ P y ∧ μ y < μ x) (hx : P x) (h : ∀ (y : I.Data), P y → I.Holds t y → ∀ n ≤ μ x * (T + 2) + 1, Q y n) :
WP I (Code.loopUntil t body) x Q

Until rule. A loop that runs until its test holds, by an invariant P and a measure μ that every iteration of at most T instructions decreases.

theorem Cslib.Program.WP.loopUntil_indexed {I : InstructionSet} {body : Code I.Op I.Test} {t : I.Test} {x : I.Data} {Q : I.Data → ℕ → Prop} (P : ℕ → I.Data → Prop) (T : ℕ) (hbody : ∀ (k : ℕ) (x : I.Data), P k x → ¬I.Holds t x → WP I body x fun (y : I.Data) (n : ℕ) => n ≤ T ∧ ∃ j < k, P j y) {k : ℕ} (hx : P k x) (h : ∀ (j : ℕ) (y : I.Data), j ≤ k → P j y → I.Holds t y → ∀ n ≤ k * (T + 2) + 1, Q y n) :
WP I (Code.loopUntil t body) x Q

Until rule, with a ghost variant. A loop that runs until its test holds, by an invariant P k indexed by a number that every iteration of at most T instructions decreases. The index need not be a function of the data: it may count proof data, such as the ancestors still to visit.

theorem Cslib.Program.WP.loop {I : InstructionSet} {body : Code I.Op I.Test} {t : I.Test} {x : I.Data} {Q : I.Data → ℕ → Prop} (P : I.Data → Prop) (μ : I.Data → ℕ) (T : ℕ) (hbody : ∀ (x : I.Data), P x → I.Holds t x → WP I body x fun (y : I.Data) (n : ℕ) => n ≤ T ∧ P y ∧ μ y < μ x) (hx : P x) (h : ∀ (y : I.Data), P y → ¬I.Holds t y → ∀ n ≤ μ x * (T + 2) + 2, Q y n) :
WP I (Code.loop t body) x Q

While rule. A loop that runs while its test holds, by an invariant P and a measure μ that every iteration of at most T instructions decreases.