3.4. Verification Condition Generation

At the end of the previous chapter we observed that, when building a derivation in Hoare logic, most of the work is mechanical: the rules to apply are determined by the syntax of the program, and the intermediate assertions for assignments can be computed backwards. The only genuinely creative ingredients are the loop invariants and the proofs of the logical implications left over at the end.

This observation is the basis of modern program verifiers such as Dafny, Why3 and Frama-C. In them, the programmer annotates the program with invariants, and a tool called a verification condition generator (VCG) automatically produces a set of logical formulas, the verification conditions, whose validity guarantees the correctness of the program. These formulas are then sent to an automatic prover (typically an SMT solver) or, in our case, proved in Lean.

In this chapter, we build a VCG for IMP and prove that it is sound with respect to the Hoare logic of the previous chapter. The plan is:

  1. define annotated commands, in which each loop carries its invariant;

  2. define the function wp, which computes a precondition from a postcondition;

  3. define the function vc, which collects the verification conditions;

  4. prove that, if the verification conditions hold, the triple \{\textsf{wp}(c, Q)\}\ c\ \{Q\} is derivable;

  5. apply the VCG to examples, including the factorial.

3.4.1. Annotated commands

The While rule requires an invariant, which in general cannot be computed automatically. For this reason, the VCG works on a version of IMP in which each loop comes with an assertion supplied by the programmer:

while b invariant I do c

The other commands remain unchanged.

namespace Ch20 open Ch18 Ch19 inductive ACom where | skip : ACom | assign : Var → AExp → ACom | seq : ACom → ACom → ACom | ite : BExp → ACom → ACom → ACom | whl : Assertion → BExp → ACom → ACom scoped infixl:70 " ;;ₐ " => ACom.seq

Annotations do not affect execution. To relate annotated commands to the semantics of IMP, we define the function erase, which erases the invariants and returns an ordinary IMP command:

def erase : ACom → Com | .skip => .skip | .assign x a => .assign x a | .seq c₁ c₂ => erase c₁ ;; erase c₂ | .ite b c₁ c₂ => .ite b (erase c₁) (erase c₂) | .whl _ b c => .whl b (erase c)

3.4.2. Weakest precondition

Given a command c and a postcondition Q, the weakest precondition \textsf{wp}(c, Q) is the least restrictive condition on the initial state that guarantees that Q will hold after the execution of c. The concept was introduced by E. W. Dijkstra and can be computed by recursion on the structure of the command:

\begin{array}{lcl} \textsf{wp}(\textsf{skip}, Q) & = & Q \\ \textsf{wp}(x := a, Q) & = & Q[a/x] \\ \textsf{wp}(c_1 ; c_2, Q) & = & \textsf{wp}(c_1, \textsf{wp}(c_2, Q)) \\ \textsf{wp}(\textsf{if}\ b\ \textsf{then}\ c_1\ \textsf{else}\ c_2, Q) & = & (b \Rightarrow \textsf{wp}(c_1, Q)) \wedge (\neg b \Rightarrow \textsf{wp}(c_2, Q)) \\ \textsf{wp}(\textsf{while}\ b\ \textsf{invariant}\ I\ \textsf{do}\ c, Q) & = & I \end{array}

Each equation reflects a rule of Hoare logic. For skip, nothing changes. For assignment, the Assign rule tells us exactly that Q[a/x] is the right precondition. For sequencing, we first compute the precondition of c_2, which serves as the postcondition for c_1; this is where the VCG eliminates the need to guess intermediate assertions. In the conditional, the precondition depends on the outcome of the test.

The loop case is special. The "true" weakest precondition of a loop is, in general, not computable (it involves a fixed point). In its place, we use the invariant supplied by the programmer. Strictly speaking, then, the function below computes a precondition that is "the weakest given the invariants".

def wp : ACom → Assertion → Assertion | .skip, Q => Q | .assign x a, Q => assub Q x a | .seq c₁ c₂, Q => wp c₁ (wp c₂ Q) | .ite b c₁ c₂, Q => fun s => (beval s b = true → wp c₁ Q s) ∧ (beval s b = false → wp c₂ Q s) | .whl I _ _, _ => I

3.4.3. Verification conditions

The function wp alone is not enough. In the loop case, it assumes that I is indeed an invariant and that it implies the postcondition on exit, but it checks neither of these things. These are precisely the obligations that must be proved, and the function vc collects them:

\begin{array}{lcl} \textsf{vc}(\textsf{skip}, Q) & = & \top \\ \textsf{vc}(x := a, Q) & = & \top \\ \textsf{vc}(c_1 ; c_2, Q) & = & \textsf{vc}(c_1, \textsf{wp}(c_2, Q)) \wedge \textsf{vc}(c_2, Q) \\ \textsf{vc}(\textsf{if}\ b\ \textsf{then}\ c_1\ \textsf{else}\ c_2, Q) & = & \textsf{vc}(c_1, Q) \wedge \textsf{vc}(c_2, Q) \end{array}

\textsf{vc}(\textsf{while}\ b\ \textsf{invariant}\ I\ \textsf{do}\ c, Q) = (\forall s,\ I \wedge b \Rightarrow \textsf{wp}(c, I)) \wedge (\forall s,\ I \wedge \neg b \Rightarrow Q) \wedge \textsf{vc}(c, I)

The three conditions generated by a loop are:

  • (a) preservation: if the invariant holds and the guard is true, then the precondition needed for the body to re-establish the invariant holds;

  • (b) exit: if the invariant holds and the guard is false, then the postcondition of the loop holds;

  • (c) the internal verification conditions of the body, which may contain nested loops.

Unlike wp, which returns an assertion (a predicate on the state), vc returns a proposition: the verification conditions are universally quantified over all states.

def vc : ACom → Assertion → Prop | .skip, _ => True | .assign _ _, _ => True | .seq c₁ c₂, Q => vc c₁ (wp c₂ Q) ∧ vc c₂ Q | .ite _ c₁ c₂, Q => vc c₁ Q ∧ vc c₂ Q | .whl I b c, Q => (∀ s, I s ∧ beval s b = true → wp c I s) ∧ (∀ s, I s ∧ beval s b = false → Q s) ∧ vc c I

3.4.4. Soundness of the VCG

The central theorem of this chapter states that the VCG is sound: if the verification conditions of c with respect to Q hold, then the triple \{\textsf{wp}(c, Q)\}\ \textsf{erase}(c)\ \{Q\} is derivable in Hoare logic.

\textsf{vc}(c, Q) \implies\ \vdash \{\textsf{wp}(c, Q)\}\ \textsf{erase}(c)\ \{Q\}

The proof is by induction on the annotated command c, with the postcondition Q universally quantified (since it changes in the recursive calls of wp and vc). Each case builds the derivation using the corresponding Hoare rule:

  • skip and assignment: the Skip and Assign rules apply directly, since wp was defined precisely to coincide with their preconditions;

  • sequencing: the Seq rule with intermediate assertion \textsf{wp}(c_2, Q), followed by the two induction hypotheses;

  • conditional: the If rule, and in each branch the Pre rule, noting that \textsf{wp}(\textsf{if}\ldots, Q) \wedge b implies \textsf{wp}(c_1, Q) (and similarly for the other branch);

  • loop: a combination of the Post, While and Pre rules. The precondition of the body, I \wedge b, is strengthened to \textsf{wp}(c, I) by condition (a); the induction hypothesis, applied with condition (c), gives the triple for the body; and the postcondition I \wedge \neg b is weakened to Q by condition (b).

theorem vcg_sound : ∀ (c : ACom) (Q : Assertion), vc c Q → {{ wp c Q }} (erase c) {{ Q }} := ⊢ ∀ (c : ACom) (Q : Assertion), vc c Q → {{ wp c Q }} erase c {{ Q }} c:ACom⊢ ∀ (Q : Assertion), vc c Q → {{ wp c Q }} erase c {{ Q }} induction c with ⊢ ∀ (Q : Assertion), vc ACom.skip Q → {{ wp ACom.skip Q }} erase ACom.skip {{ Q }} intro Q Q:Assertiona:vc ACom.skip Q⊢ {{ wp ACom.skip Q }} erase ACom.skip {{ Q }} All goals completed! 🐙 x:Vara:AExp⊢ ∀ (Q : Assertion), vc (ACom.assign x a) Q → {{ wp (ACom.assign x a) Q }} erase (ACom.assign x a) {{ Q }} intro Q x:Vara:AExpQ:Assertiona_1:vc (ACom.assign x a) Q⊢ {{ wp (ACom.assign x a) Q }} erase (ACom.assign x a) {{ Q }} All goals completed! 🐙 c₁:AComc₂:AComIH₁:∀ (Q : Assertion), vc c₁ Q → {{ wp c₁ Q }} erase c₁ {{ Q }}IH₂:∀ (Q : Assertion), vc c₂ Q → {{ wp c₂ Q }} erase c₂ {{ Q }}⊢ ∀ (Q : Assertion), vc (c₁ ;;ₐ c₂) Q → {{ wp (c₁ ;;ₐ c₂) Q }} erase (c₁ ;;ₐ c₂) {{ Q }} intro Q c₁:AComc₂:AComIH₁:∀ (Q : Assertion), vc c₁ Q → {{ wp c₁ Q }} erase c₁ {{ Q }}IH₂:∀ (Q : Assertion), vc c₂ Q → {{ wp c₂ Q }} erase c₂ {{ Q }}Q:Assertionh₁:vc c₁ (wp c₂ Q)h₂:vc c₂ Q⊢ {{ wp (c₁ ;;ₐ c₂) Q }} erase (c₁ ;;ₐ c₂) {{ Q }} c₁:AComc₂:AComIH₁:∀ (Q : Assertion), vc c₁ Q → {{ wp c₁ Q }} erase c₁ {{ Q }}IH₂:∀ (Q : Assertion), vc c₂ Q → {{ wp c₂ Q }} erase c₂ {{ Q }}Q:Assertionh₁:vc c₁ (wp c₂ Q)h₂:vc c₂ Q⊢ {{ wp (c₁ ;;ₐ c₂) Q }} erase c₁ {{ ?seq.Q }}c₁:AComc₂:AComIH₁:∀ (Q : Assertion), vc c₁ Q → {{ wp c₁ Q }} erase c₁ {{ Q }}IH₂:∀ (Q : Assertion), vc c₂ Q → {{ wp c₂ Q }} erase c₂ {{ Q }}Q:Assertionh₁:vc c₁ (wp c₂ Q)h₂:vc c₂ Q⊢ {{ ?seq.Q }} erase c₂ {{ Q }}c₁:AComc₂:AComIH₁:∀ (Q : Assertion), vc c₁ Q → {{ wp c₁ Q }} erase c₁ {{ Q }}IH₂:∀ (Q : Assertion), vc c₂ Q → {{ wp c₂ Q }} erase c₂ {{ Q }}Q:Assertionh₁:vc c₁ (wp c₂ Q)h₂:vc c₂ Q⊢ Assertion c₁:AComc₂:AComIH₁:∀ (Q : Assertion), vc c₁ Q → {{ wp c₁ Q }} erase c₁ {{ Q }}IH₂:∀ (Q : Assertion), vc c₂ Q → {{ wp c₂ Q }} erase c₂ {{ Q }}Q:Assertionh₁:vc c₁ (wp c₂ Q)h₂:vc c₂ Q⊢ {{ wp (c₁ ;;ₐ c₂) Q }} erase c₁ {{ ?seq.Q }} c₁:AComc₂:AComIH₁:∀ (Q : Assertion), vc c₁ Q → {{ wp c₁ Q }} erase c₁ {{ Q }}IH₂:∀ (Q : Assertion), vc c₂ Q → {{ wp c₂ Q }} erase c₂ {{ Q }}Q:Assertionh₁:vc c₁ (wp c₂ Q)h₂:vc c₂ Q⊢ vc c₁ (wp c₂ Q) ; All goals completed! 🐙 c₁:AComc₂:AComIH₁:∀ (Q : Assertion), vc c₁ Q → {{ wp c₁ Q }} erase c₁ {{ Q }}IH₂:∀ (Q : Assertion), vc c₂ Q → {{ wp c₂ Q }} erase c₂ {{ Q }}Q:Assertionh₁:vc c₁ (wp c₂ Q)h₂:vc c₂ Q⊢ {{ wp c₂ Q }} erase c₂ {{ Q }} c₁:AComc₂:AComIH₁:∀ (Q : Assertion), vc c₁ Q → {{ wp c₁ Q }} erase c₁ {{ Q }}IH₂:∀ (Q : Assertion), vc c₂ Q → {{ wp c₂ Q }} erase c₂ {{ Q }}Q:Assertionh₁:vc c₁ (wp c₂ Q)h₂:vc c₂ Q⊢ vc c₂ Q ; All goals completed! 🐙 b:BExpc₁:AComc₂:AComIH₁:∀ (Q : Assertion), vc c₁ Q → {{ wp c₁ Q }} erase c₁ {{ Q }}IH₂:∀ (Q : Assertion), vc c₂ Q → {{ wp c₂ Q }} erase c₂ {{ Q }}⊢ ∀ (Q : Assertion), vc (ACom.ite b c₁ c₂) Q → {{ wp (ACom.ite b c₁ c₂) Q }} erase (ACom.ite b c₁ c₂) {{ Q }} intro Q b:BExpc₁:AComc₂:AComIH₁:∀ (Q : Assertion), vc c₁ Q → {{ wp c₁ Q }} erase c₁ {{ Q }}IH₂:∀ (Q : Assertion), vc c₂ Q → {{ wp c₂ Q }} erase c₂ {{ Q }}Q:Assertionh₁:vc c₁ Qh₂:vc c₂ Q⊢ {{ wp (ACom.ite b c₁ c₂) Q }} erase (ACom.ite b c₁ c₂) {{ Q }} b:BExpc₁:AComc₂:AComIH₁:∀ (Q : Assertion), vc c₁ Q → {{ wp c₁ Q }} erase c₁ {{ Q }}IH₂:∀ (Q : Assertion), vc c₂ Q → {{ wp c₂ Q }} erase c₂ {{ Q }}Q:Assertionh₁:vc c₁ Qh₂:vc c₂ Q⊢ {{ wp (ACom.ite b c₁ c₂) Q .&. True(b) }} erase c₁ {{ Q }}b:BExpc₁:AComc₂:AComIH₁:∀ (Q : Assertion), vc c₁ Q → {{ wp c₁ Q }} erase c₁ {{ Q }}IH₂:∀ (Q : Assertion), vc c₂ Q → {{ wp c₂ Q }} erase c₂ {{ Q }}Q:Assertionh₁:vc c₁ Qh₂:vc c₂ Q⊢ {{ wp (ACom.ite b c₁ c₂) Q .&. False(b) }} erase c₂ {{ Q }} b:BExpc₁:AComc₂:AComIH₁:∀ (Q : Assertion), vc c₁ Q → {{ wp c₁ Q }} erase c₁ {{ Q }}IH₂:∀ (Q : Assertion), vc c₂ Q → {{ wp c₂ Q }} erase c₂ {{ Q }}Q:Assertionh₁:vc c₁ Qh₂:vc c₂ Q⊢ {{ wp (ACom.ite b c₁ c₂) Q .&. True(b) }} erase c₁ {{ Q }} b:BExpc₁:AComc₂:AComIH₁:∀ (Q : Assertion), vc c₁ Q → {{ wp c₁ Q }} erase c₁ {{ Q }}IH₂:∀ (Q : Assertion), vc c₂ Q → {{ wp c₂ Q }} erase c₂ {{ Q }}Q:Assertionh₁:vc c₁ Qh₂:vc c₂ Q⊢ ∀ (s : State), (wp (ACom.ite b c₁ c₂) Q .&. True(b)) s → wp c₁ Q s intro s b:BExpc₁:AComc₂:AComIH₁:∀ (Q : Assertion), vc c₁ Q → {{ wp c₁ Q }} erase c₁ {{ Q }}IH₂:∀ (Q : Assertion), vc c₂ Q → {{ wp c₂ Q }} erase c₂ {{ Q }}Q:Assertionh₁:vc c₁ Qh₂:vc c₂ Qs:Statehwp:wp (ACom.ite b c₁ c₂) Q shbT:True(b) s⊢ wp c₁ Q s b:BExpc₁:AComc₂:AComIH₁:∀ (Q : Assertion), vc c₁ Q → {{ wp c₁ Q }} erase c₁ {{ Q }}IH₂:∀ (Q : Assertion), vc c₂ Q → {{ wp c₂ Q }} erase c₂ {{ Q }}Q:Assertionh₁:vc c₁ Qh₂:vc c₂ Qs:Statehwp:(beval s b = true → wp c₁ Q s) ∧ (beval s b = false → wp c₂ Q s)hbT:True(b) s⊢ wp c₁ Q s All goals completed! 🐙 b:BExpc₁:AComc₂:AComIH₁:∀ (Q : Assertion), vc c₁ Q → {{ wp c₁ Q }} erase c₁ {{ Q }}IH₂:∀ (Q : Assertion), vc c₂ Q → {{ wp c₂ Q }} erase c₂ {{ Q }}Q:Assertionh₁:vc c₁ Qh₂:vc c₂ Q⊢ {{ wp (ACom.ite b c₁ c₂) Q .&. False(b) }} erase c₂ {{ Q }} b:BExpc₁:AComc₂:AComIH₁:∀ (Q : Assertion), vc c₁ Q → {{ wp c₁ Q }} erase c₁ {{ Q }}IH₂:∀ (Q : Assertion), vc c₂ Q → {{ wp c₂ Q }} erase c₂ {{ Q }}Q:Assertionh₁:vc c₁ Qh₂:vc c₂ Q⊢ ∀ (s : State), (wp (ACom.ite b c₁ c₂) Q .&. False(b)) s → wp c₂ Q s intro s b:BExpc₁:AComc₂:AComIH₁:∀ (Q : Assertion), vc c₁ Q → {{ wp c₁ Q }} erase c₁ {{ Q }}IH₂:∀ (Q : Assertion), vc c₂ Q → {{ wp c₂ Q }} erase c₂ {{ Q }}Q:Assertionh₁:vc c₁ Qh₂:vc c₂ Qs:Statehwp:wp (ACom.ite b c₁ c₂) Q shbF:False(b) s⊢ wp c₂ Q s b:BExpc₁:AComc₂:AComIH₁:∀ (Q : Assertion), vc c₁ Q → {{ wp c₁ Q }} erase c₁ {{ Q }}IH₂:∀ (Q : Assertion), vc c₂ Q → {{ wp c₂ Q }} erase c₂ {{ Q }}Q:Assertionh₁:vc c₁ Qh₂:vc c₂ Qs:Statehwp:(beval s b = true → wp c₁ Q s) ∧ (beval s b = false → wp c₂ Q s)hbF:False(b) s⊢ wp c₂ Q s All goals completed! 🐙 I:Assertionb:BExpc:AComIH:∀ (Q : Assertion), vc c Q → {{ wp c Q }} erase c {{ Q }}⊢ ∀ (Q : Assertion), vc (ACom.whl I b c) Q → {{ wp (ACom.whl I b c) Q }} erase (ACom.whl I b c) {{ Q }} intro Q I:Assertionb:BExpc:AComIH:∀ (Q : Assertion), vc c Q → {{ wp c Q }} erase c {{ Q }}Q:Assertionhbody:∀ (s : State), I s ∧ beval s b = true → wp c I shpost:∀ (s : State), I s ∧ beval s b = false → Q shvc:vc c I⊢ {{ wp (ACom.whl I b c) Q }} erase (ACom.whl I b c) {{ Q }} I:Assertionb:BExpc:AComIH:∀ (Q : Assertion), vc c Q → {{ wp c Q }} erase c {{ Q }}Q:Assertionhbody:∀ (s : State), I s ∧ beval s b = true → wp c I shpost:∀ (s : State), I s ∧ beval s b = false → Q shvc:vc c I⊢ {{ wp (ACom.whl I b c) Q }} erase (ACom.whl I b c) {{ ?whl.Q }}I:Assertionb:BExpc:AComIH:∀ (Q : Assertion), vc c Q → {{ wp c Q }} erase c {{ Q }}Q:Assertionhbody:∀ (s : State), I s ∧ beval s b = true → wp c I shpost:∀ (s : State), I s ∧ beval s b = false → Q shvc:vc c I⊢ ∀ (s : State), ?whl.Q s → Q sI:Assertionb:BExpc:AComIH:∀ (Q : Assertion), vc c Q → {{ wp c Q }} erase c {{ Q }}Q:Assertionhbody:∀ (s : State), I s ∧ beval s b = true → wp c I shpost:∀ (s : State), I s ∧ beval s b = false → Q shvc:vc c I⊢ Assertion I:Assertionb:BExpc:AComIH:∀ (Q : Assertion), vc c Q → {{ wp c Q }} erase c {{ Q }}Q:Assertionhbody:∀ (s : State), I s ∧ beval s b = true → wp c I shpost:∀ (s : State), I s ∧ beval s b = false → Q shvc:vc c I⊢ {{ wp (ACom.whl I b c) Q }} erase (ACom.whl I b c) {{ ?whl.Q }} I:Assertionb:BExpc:AComIH:∀ (Q : Assertion), vc c Q → {{ wp c Q }} erase c {{ Q }}Q:Assertionhbody:∀ (s : State), I s ∧ beval s b = true → wp c I shpost:∀ (s : State), I s ∧ beval s b = false → Q shvc:vc c I⊢ {{ wp (ACom.whl I b c) Q .&. True(b) }} erase c {{ wp (ACom.whl I b c) Q }} I:Assertionb:BExpc:AComIH:∀ (Q : Assertion), vc c Q → {{ wp c Q }} erase c {{ Q }}Q:Assertionhbody:∀ (s : State), I s ∧ beval s b = true → wp c I shpost:∀ (s : State), I s ∧ beval s b = false → Q shvc:vc c I⊢ ∀ (s : State), (wp (ACom.whl I b c) Q .&. True(b)) s → wp c I s intro s I:Assertionb:BExpc:AComIH:∀ (Q : Assertion), vc c Q → {{ wp c Q }} erase c {{ Q }}Q:Assertionhbody:∀ (s : State), I s ∧ beval s b = true → wp c I shpost:∀ (s : State), I s ∧ beval s b = false → Q shvc:vc c Is:StatehI:wp (ACom.whl I b c) Q shbT:True(b) s⊢ wp c I s All goals completed! 🐙 I:Assertionb:BExpc:AComIH:∀ (Q : Assertion), vc c Q → {{ wp c Q }} erase c {{ Q }}Q:Assertionhbody:∀ (s : State), I s ∧ beval s b = true → wp c I shpost:∀ (s : State), I s ∧ beval s b = false → Q shvc:vc c I⊢ ∀ (s : State), (wp (ACom.whl I b c) Q .&. False(b)) s → Q s intro s I:Assertionb:BExpc:AComIH:∀ (Q : Assertion), vc c Q → {{ wp c Q }} erase c {{ Q }}Q:Assertionhbody:∀ (s : State), I s ∧ beval s b = true → wp c I shpost:∀ (s : State), I s ∧ beval s b = false → Q shvc:vc c Is:StatehI:wp (ACom.whl I b c) Q shbF:False(b) s⊢ Q s All goals completed! 🐙

Combining vcg_sound with the soundness theorem of Hoare logic (Ch19.hoare_sound), we obtain a semantic guarantee. To verify that a program satisfies \{P\}\ c\ \{Q\}, it suffices to prove two purely logical facts: that P implies \textsf{wp}(c, Q) and that \textsf{vc}(c, Q) holds.

\frac{\forall s,\ P(s) \Rightarrow \textsf{wp}(c, Q)(s) \quad \textsf{vc}(c, Q)}{\models \{P\}\ \textsf{erase}(c)\ \{Q\}}

theorem vcg_correct (P : Assertion) (c : ACom) (Q : Assertion) (hpre : ∀ s, P s → wp c Q s) (hvc : vc c Q) : {{* P *}} (erase c) {{* Q *}} := hoare_sound (conseq_pre hpre (vcg_sound c Q hvc))

This theorem is the user interface of the VCG. Note that, when applying it, we no longer need to mention any rule of Hoare logic: the whole structure of the derivation is built by the proof of vcg_sound, once and for all.

3.4.5. Examples

In all the examples, the pattern is the same: we apply vcg_correct and discharge the two resulting goals, unfolding the definitions of wp and vc with simp.

3.4.5.1. Assignments

For the assignment x := 5 with postcondition x = 5, the verification condition is trivial and the obligation \top \Rightarrow \textsf{wp} reduces to 5 = 5.

example : {{* (fun _ => True) *}} (erase (.assign "x" (.num 5))) {{* "x" .==. 5 *}} := ⊢ {{* fun x => True *}} erase (ACom.assign "x" (AExp.num 5)) {{* "x" .==. 5 *}} ⊢ ∀ (s : State), True → wp (ACom.assign "x" (AExp.num 5)) ("x" .==. 5) s⊢ vc (ACom.assign "x" (AExp.num 5)) ("x" .==. 5) ⊢ ∀ (s : State), True → wp (ACom.assign "x" (AExp.num 5)) ("x" .==. 5) s intro s s:Statea:True⊢ wp (ACom.assign "x" (AExp.num 5)) ("x" .==. 5) s All goals completed! 🐙 ⊢ vc (ACom.assign "x" (AExp.num 5)) ("x" .==. 5) All goals completed! 🐙

In the sequence x := 0 ;; x := x + 1, the VCG computes the precondition (x + 1 = 1)[0/x], that is, 0 + 1 = 1, without our having to give the intermediate assertion.

example : {{* (fun _ => True) *}} (erase (.assign "x" (.num 0) ;;ₐ .assign "x" (.add (.var "x") (.num 1)))) {{* "x" .==. 1 *}} := ⊢ {{* fun x => True *}} erase (ACom.assign "x" (AExp.num 0) ;;ₐ ACom.assign "x" ((AExp.var "x").add (AExp.num 1))) {{* "x" .==. 1 *}} ⊢ ∀ (s : State), True → wp (ACom.assign "x" (AExp.num 0) ;;ₐ ACom.assign "x" ((AExp.var "x").add (AExp.num 1))) ("x" .==. 1) s⊢ vc (ACom.assign "x" (AExp.num 0) ;;ₐ ACom.assign "x" ((AExp.var "x").add (AExp.num 1))) ("x" .==. 1) ⊢ ∀ (s : State), True → wp (ACom.assign "x" (AExp.num 0) ;;ₐ ACom.assign "x" ((AExp.var "x").add (AExp.num 1))) ("x" .==. 1) s intro s s:Statea:True⊢ wp (ACom.assign "x" (AExp.num 0) ;;ₐ ACom.assign "x" ((AExp.var "x").add (AExp.num 1))) ("x" .==. 1) s All goals completed! 🐙 ⊢ vc (ACom.assign "x" (AExp.num 0) ;;ₐ ACom.assign "x" ((AExp.var "x").add (AExp.num 1))) ("x" .==. 1) All goals completed! 🐙

Compare the verification of the swap below with the one in the previous chapter: all the manipulation of the Seq and Assign rules disappears.

example (a b : ℕ) : {{* ("x" .==. a) .&. ("y" .==. b) *}} (erase (.assign "tmp" (.var "x") ;;ₐ .assign "x" (.var "y") ;;ₐ .assign "y" (.var "tmp"))) {{* ("x" .==. b) .&. ("y" .==. a) *}} := a:ℕb:ℕ⊢ {{* ("x" .==. a) .&. "y" .==. b *}} erase (ACom.assign "tmp" (AExp.var "x") ;;ₐ ACom.assign "x" (AExp.var "y") ;;ₐ ACom.assign "y" (AExp.var "tmp")) {{* ("x" .==. b) .&. "y" .==. a *}} a:ℕb:ℕ⊢ ∀ (s : State), (("x" .==. a) .&. "y" .==. b) s → wp (ACom.assign "tmp" (AExp.var "x") ;;ₐ ACom.assign "x" (AExp.var "y") ;;ₐ ACom.assign "y" (AExp.var "tmp")) (("x" .==. b) .&. "y" .==. a) sa:ℕb:ℕ⊢ vc (ACom.assign "tmp" (AExp.var "x") ;;ₐ ACom.assign "x" (AExp.var "y") ;;ₐ ACom.assign "y" (AExp.var "tmp")) (("x" .==. b) .&. "y" .==. a) a:ℕb:ℕ⊢ ∀ (s : State), (("x" .==. a) .&. "y" .==. b) s → wp (ACom.assign "tmp" (AExp.var "x") ;;ₐ ACom.assign "x" (AExp.var "y") ;;ₐ ACom.assign "y" (AExp.var "tmp")) (("x" .==. b) .&. "y" .==. a) s intro s a:ℕb:ℕs:StateH1:(("x" .==. a) .&. "y" .==. b) s⊢ wp (ACom.assign "tmp" (AExp.var "x") ;;ₐ ACom.assign "x" (AExp.var "y") ;;ₐ ACom.assign "y" (AExp.var "tmp")) (("x" .==. b) .&. "y" .==. a) s a:ℕb:ℕs:StateH1:s "x" = a ∧ s "y" = b⊢ s "y" = b ∧ s "x" = a a:ℕb:ℕs:StateH1:s "x" = a ∧ s "y" = b⊢ b = b ∧ a = a All goals completed! 🐙 a:ℕb:ℕ⊢ vc (ACom.assign "tmp" (AExp.var "x") ;;ₐ ACom.assign "x" (AExp.var "y") ;;ₐ ACom.assign "y" (AExp.var "tmp")) (("x" .==. b) .&. "y" .==. a) All goals completed! 🐙

3.4.5.2. Loops

In the loop while false invariant True do skip, both loop verification conditions are trivial: preservation holds vacuously (the guard is never true) and exit holds because the postcondition is \top.

example : {{* (fun _ => True) *}} (erase (.whl (fun _ => True) .ff .skip)) {{* (fun _ => True) *}} := ⊢ {{* fun x => True *}} erase (ACom.whl (fun x => True) BExp.ff ACom.skip) {{* fun x => True *}} ⊢ ∀ (s : State), True → wp (ACom.whl (fun x => True) BExp.ff ACom.skip) (fun x => True) s⊢ vc (ACom.whl (fun x => True) BExp.ff ACom.skip) fun x => True ⊢ ∀ (s : State), True → wp (ACom.whl (fun x => True) BExp.ff ACom.skip) (fun x => True) s intro s s:Statea:True⊢ wp (ACom.whl (fun x => True) BExp.ff ACom.skip) (fun x => True) s All goals completed! 🐙 ⊢ vc (ACom.whl (fun x => True) BExp.ff ACom.skip) fun x => True ⊢ ∀ (s : State), beval s BExp.ff = true → wp ACom.skip (fun x => True) s intro s s:Statea:beval s BExp.ff = true⊢ wp ACom.skip (fun x => True) s All goals completed! 🐙

A slightly more interesting example is the countdown while x ≠ 0 do x := x - 1. To prove that x = 0 at the end, the invariant \top is enough! The exit condition guarantees the postcondition directly: if the guard x \neq 0 is false, then x = 0. This nicely illustrates the nature of partial correctness: we do not need to argue that the loop terminates.

example : {{* (fun _ => True) *}} (erase (.whl (fun _ => True) (.not (.eq (.var "x") (.num 0))) (.assign "x" (.sub (.var "x") (.num 1))))) {{* "x" .==. 0 *}} := ⊢ {{* fun x => True *}} erase (ACom.whl (fun x => True) (BExp.eq (AExp.var "x") (AExp.num 0)).not (ACom.assign "x" ((AExp.var "x").sub (AExp.num 1)))) {{* "x" .==. 0 *}} ⊢ ∀ (s : State), True → wp (ACom.whl (fun x => True) (BExp.eq (AExp.var "x") (AExp.num 0)).not (ACom.assign "x" ((AExp.var "x").sub (AExp.num 1)))) ("x" .==. 0) s⊢ vc (ACom.whl (fun x => True) (BExp.eq (AExp.var "x") (AExp.num 0)).not (ACom.assign "x" ((AExp.var "x").sub (AExp.num 1)))) ("x" .==. 0) ⊢ ∀ (s : State), True → wp (ACom.whl (fun x => True) (BExp.eq (AExp.var "x") (AExp.num 0)).not (ACom.assign "x" ((AExp.var "x").sub (AExp.num 1)))) ("x" .==. 0) s intro s s:Statea:True⊢ wp (ACom.whl (fun x => True) (BExp.eq (AExp.var "x") (AExp.num 0)).not (ACom.assign "x" ((AExp.var "x").sub (AExp.num 1)))) ("x" .==. 0) s All goals completed! 🐙 ⊢ vc (ACom.whl (fun x => True) (BExp.eq (AExp.var "x") (AExp.num 0)).not (ACom.assign "x" ((AExp.var "x").sub (AExp.num 1)))) ("x" .==. 0) ⊢ (∀ (s : State), beval s (BExp.eq (AExp.var "x") (AExp.num 0)).not = true → wp (ACom.assign "x" ((AExp.var "x").sub (AExp.num 1))) (fun x => True) s) ∧ ∀ (s : State), beval s (BExp.eq (AExp.var "x") (AExp.num 0)).not = false → ("x" .==. 0) s ⊢ ∀ (s : State), beval s (BExp.eq (AExp.var "x") (AExp.num 0)).not = true → wp (ACom.assign "x" ((AExp.var "x").sub (AExp.num 1))) (fun x => True) s⊢ ∀ (s : State), beval s (BExp.eq (AExp.var "x") (AExp.num 0)).not = false → ("x" .==. 0) s ⊢ ∀ (s : State), beval s (BExp.eq (AExp.var "x") (AExp.num 0)).not = true → wp (ACom.assign "x" ((AExp.var "x").sub (AExp.num 1))) (fun x => True) s intro s s:Statea:beval s (BExp.eq (AExp.var "x") (AExp.num 0)).not = true⊢ wp (ACom.assign "x" ((AExp.var "x").sub (AExp.num 1))) (fun x => True) s All goals completed! 🐙 ⊢ ∀ (s : State), beval s (BExp.eq (AExp.var "x") (AExp.num 0)).not = false → ("x" .==. 0) s intro s s:StateH1:beval s (BExp.eq (AExp.var "x") (AExp.num 0)).not = false⊢ ("x" .==. 0) s s:StateH1:s "x" = 0⊢ s "x" = 0 All goals completed! 🐙

3.4.5.3. Factorial

Finally, we revisit the factorial. Now the invariant r \cdot n! = n_0! appears inside the program, as an annotation on the loop.

def factA (n₀ : ℕ) : ACom := .assign "r" (.num 1) ;;ₐ .whl (fun s => s "r" * (s "n").factorial = n₀.factorial) (.not (.eq (.var "n") (.num 0))) (.assign "r" (.mul (.var "r") (.var "n")) ;;ₐ .assign "n" (.sub (.var "n") (.num 1)))

The VCG generates exactly the two nontrivial conditions that we identified informally in the previous chapter:

  • preservation: if r \cdot n! = n_0! and n \neq 0, then (r \cdot n) \cdot (n - 1)! = n_0!;

  • exit: if r \cdot n! = n_0! and n = 0, then r = n_0!.

In addition, there is the obligation n = n_0 \Rightarrow 1 \cdot n! = n_0!, which comes from the precondition. In the preservation proof, we do case analysis on n: the zero case contradicts the guard, and in the successor case we unfold (k+1)! = (k+1) \cdot k! and conclude with linear arithmetic.

example (n₀ : ℕ) : {{* (fun s => s "n" = n₀) *}} (erase (factA n₀)) {{* "r" .==. n₀.factorial *}} := n₀:ℕ⊢ {{* fun s => s "n" = n₀ *}} erase (factA n₀) {{* "r" .==. n₀.factorial *}} n₀:ℕ⊢ ∀ (s : State), s "n" = n₀ → wp (factA n₀) ("r" .==. n₀.factorial) sn₀:ℕ⊢ vc (factA n₀) ("r" .==. n₀.factorial) n₀:ℕ⊢ ∀ (s : State), s "n" = n₀ → wp (factA n₀) ("r" .==. n₀.factorial) s intro s n₀:ℕs:Statehs:s "n" = n₀⊢ wp (factA n₀) ("r" .==. n₀.factorial) s All goals completed! 🐙 n₀:ℕ⊢ vc (factA n₀) ("r" .==. n₀.factorial) n₀:ℕ⊢ (∀ (s : State), s "r" * (s "n").factorial = n₀.factorial → beval s (BExp.eq (AExp.var "n") (AExp.num 0)).not = true → wp (ACom.assign "r" ((AExp.var "r").mul (AExp.var "n")) ;;ₐ ACom.assign "n" ((AExp.var "n").sub (AExp.num 1))) (fun s => s "r" * (s "n").factorial = n₀.factorial) s) ∧ ∀ (s : State), s "r" * (s "n").factorial = n₀.factorial → beval s (BExp.eq (AExp.var "n") (AExp.num 0)).not = false → ("r" .==. n₀.factorial) s n₀:ℕ⊢ ∀ (s : State), s "r" * (s "n").factorial = n₀.factorial → beval s (BExp.eq (AExp.var "n") (AExp.num 0)).not = true → wp (ACom.assign "r" ((AExp.var "r").mul (AExp.var "n")) ;;ₐ ACom.assign "n" ((AExp.var "n").sub (AExp.num 1))) (fun s => s "r" * (s "n").factorial = n₀.factorial) sn₀:ℕ⊢ ∀ (s : State), s "r" * (s "n").factorial = n₀.factorial → beval s (BExp.eq (AExp.var "n") (AExp.num 0)).not = false → ("r" .==. n₀.factorial) s n₀:ℕ⊢ ∀ (s : State), s "r" * (s "n").factorial = n₀.factorial → beval s (BExp.eq (AExp.var "n") (AExp.num 0)).not = true → wp (ACom.assign "r" ((AExp.var "r").mul (AExp.var "n")) ;;ₐ ACom.assign "n" ((AExp.var "n").sub (AExp.num 1))) (fun s => s "r" * (s "n").factorial = n₀.factorial) s -- preservation of the invariant intro s n₀:ℕs:StateH1:s "r" * (s "n").factorial = n₀.factorial⊢ beval s (BExp.eq (AExp.var "n") (AExp.num 0)).not = true → wp (ACom.assign "r" ((AExp.var "r").mul (AExp.var "n")) ;;ₐ ACom.assign "n" ((AExp.var "n").sub (AExp.num 1))) (fun s => s "r" * (s "n").factorial = n₀.factorial) s n₀:ℕs:StateH1:s "r" * (s "n").factorial = n₀.factorialH2:beval s (BExp.eq (AExp.var "n") (AExp.num 0)).not = true⊢ wp (ACom.assign "r" ((AExp.var "r").mul (AExp.var "n")) ;;ₐ ACom.assign "n" ((AExp.var "n").sub (AExp.num 1))) (fun s => s "r" * (s "n").factorial = n₀.factorial) s n₀:ℕs:StateH1:s "r" * (s "n").factorial = n₀.factorialH2:¬s "n" = 0⊢ s "r" * s "n" * (s "n" - 1).factorial = n₀.factorial n₀:ℕs:StateH1:s "r" * (s "n").factorial = n₀.factorialH2:¬s "n" = 0⊢ s "r" * s "n" * (s "n" - 1).factorial = s "r" * (s "n").factorial n₀:ℕs:StateH1:s "r" * (s "n").factorial = n₀.factorialH2:¬s "n" = 0H3:s "n" = 0⊢ s "r" * 0 * (0 - 1).factorial = s "r" * Nat.factorial 0n₀:ℕs:StateH1:s "r" * (s "n").factorial = n₀.factorialH2:¬s "n" = 0n:ℕH3:s "n" = n + 1⊢ s "r" * (n + 1) * (n + 1 - 1).factorial = s "r" * (n + 1).factorial n₀:ℕs:StateH1:s "r" * (s "n").factorial = n₀.factorialH2:¬s "n" = 0H3:s "n" = 0⊢ s "r" * 0 * (0 - 1).factorial = s "r" * Nat.factorial 0 All goals completed! 🐙 n₀:ℕs:StateH1:s "r" * (s "n").factorial = n₀.factorialH2:¬s "n" = 0n:ℕH3:s "n" = n + 1⊢ s "r" * (n + 1) * (n + 1 - 1).factorial = s "r" * (n + 1).factorial n₀:ℕs:StateH1:s "r" * (s "n").factorial = n₀.factorialH2:¬s "n" = 0n:ℕH3:s "n" = n + 1⊢ s "r" * (n + 1) * n.factorial = s "r" * (n + 1).factorial n₀:ℕs:Staten:ℕH1:s "r" * ((n + 1) * n.factorial) = n₀.factorialH2:TrueH3:True⊢ s "r" * (n + 1) * n.factorial = s "r" * ((n + 1) * n.factorial) All goals completed! 🐙 n₀:ℕ⊢ ∀ (s : State), s "r" * (s "n").factorial = n₀.factorial → beval s (BExp.eq (AExp.var "n") (AExp.num 0)).not = false → ("r" .==. n₀.factorial) s -- loop exit intro s n₀:ℕs:StateH1:s "r" * (s "n").factorial = n₀.factorial⊢ beval s (BExp.eq (AExp.var "n") (AExp.num 0)).not = false → ("r" .==. n₀.factorial) s n₀:ℕs:StateH1:s "r" * (s "n").factorial = n₀.factorialH2:beval s (BExp.eq (AExp.var "n") (AExp.num 0)).not = false⊢ ("r" .==. n₀.factorial) s n₀:ℕs:StateH1:s "r" * (s "n").factorial = n₀.factorialH2:s "n" = 0⊢ s "r" = n₀.factorial n₀:ℕs:StateH1:s "r" = n₀.factorialH2:True⊢ s "r" = n₀.factorial All goals completed! 🐙 end Ch20

Compare this proof with the one in the previous chapter. There, half of the effort was spent applying Hoare rules in the right order and supplying intermediate assertions; here, all of that has been absorbed by Ch20.vcg_correct, and what remains are just the verification conditions, arithmetic formulas about the values of the variables. In industrial tools, these formulas would be sent to an SMT solver; in Lean, we can prove them with the available automation tactics, such as simp, omega and linarith.

3.4.6. Exercises

1. Prove that wp and vc are monotone in the postcondition: if
   ∀ s, Q s → Q' s, then ∀ s, wp c Q s → wp c Q' s, and
   vc c Q → vc c Q'. Why is the loop case trivial for wp
   but not for vc?

2. Show that, for loop-free commands, wp c Q really is the
   weakest precondition: if {{* P *}} erase c {{* Q *}}, then
   ∀ s, P s → wp c Q s. (Hint: define a "loop-free" predicate and
   use the determinism of the big-step semantics.)

3. Use the VCG to verify the summation program
     r := 0; i := 0;
     while (i ≠ n) invariant (2 * r = i * (i + 1) ∧ i ≤ n)
       do (i := i + 1; r := r + i)
   with precondition n = n₀ and postcondition 2 * r = n₀ * (n₀ + 1).

4. Choose an inadequate invariant for the factorial (for example,
   fun _ => True) and observe which verification condition is no
   longer provable.

5. Extend ACom with a construct  assert P , which does not change
   the state. Define wp and vc for it and update vcg_sound.
   (Hint: the semantics of IMP does not change, since
   erase (assert P) = skip.)

6. An alternative to wp is the calculus of strongest postconditions
   (sp). Define sp for loop-free commands and state the
   corresponding soundness theorem.