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 #
WP: the weakest-precondition form ofExecWithin.
Main results #
WP.seq,WP.branch,WP.call: sequencing, conditionals and subprograms.WP.loop,WP.loopUntil: loops, by an invariant and a measure that decreases.WP.loopUntil_indexed: the same with a ghost variant, which need not be a function of the data.WP.loop_iter,WP.loopUntil_iter: one iteration, for proofs by induction on the data a loop descends.
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.
skip changes nothing and costs nothing.
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.
A conditional, by cases on its test.
A subprogram, by its specification: continue from any data and count it allows.
Leaving a loop that runs until its test holds.
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.
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.
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.
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.
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.