3.3. Hoare Logic

In the previous chapter we gave meaning to IMP programs by means of operational semantics. With them we can, in principle, prove any property of a program: it suffices to reason about its execution derivations. In practice, however, this kind of reasoning is laborious and not very compositional: to prove something about c₁ ;; c₂ we need to open up the derivations of c₁ and c₂ and deal directly with intermediate states.

Hoare logic, proposed by C. A. R. Hoare in 1969, is a formal system for reasoning about the correctness of imperative programs compositionally, rule by rule, following the syntactic structure of the program. Its central object is the Hoare triple

\{P\}\ c\ \{Q\}

read as: "if the assertion P holds in the initial state and the execution of c terminates, then the assertion Q holds in the final state". The assertion P is the precondition and Q is the postcondition. Note the proviso "if it terminates": the Hoare logic we will study expresses partial correctness; it says nothing about programs that do not terminate.

In this chapter we proceed in four steps:

  1. we define assertions and the operation of substitution on assertions;

  2. we present the rules of Hoare logic as an inductive predicate;

  3. we define, from the big-step semantics, the semantic notion of validity of a triple;

  4. we prove that the system is sound: every derivable triple is valid.

We finish with a series of example derivations, culminating in the verification of a program that computes the factorial.

All the definitions of IMP come from the previous chapter; we make its notations available by opening the namespace Ch18.

3.3.1. Assertions

What language should we use to write pre- and postconditions? One approach is to define a syntax of first-order formulas over the program variables. In a proof assistant, there is a much simpler alternative, known as a shallow embedding: an assertion is simply a predicate on states, that is, a function from Ch18.State to Prop. In this way, we can use all of Lean's logic to write assertions.

namespace Ch19 open Ch18 abbrev Assertion := State → Prop

3.3.1.1. Substitution

The most important operation on assertions is substitution. In the classic presentation of Hoare logic, the assignment rule is

\frac{}{\{P[a/x]\}\ x := a\ \{P\}}\,(\textsf{Assign})

where P[a/x] is the formula P with the occurrences of x replaced by the expression a. In a shallow embedding, we have no access to the "occurrences of x" in P, since P is an opaque function. But we can obtain the same effect semantically: P[a/x] holds in s exactly when P holds in the state obtained by updating x with the value of a in s.

P[a/x](s) \;=\; P(s[x \mapsto [\![a]\!]_s])

def assub (P : Assertion) (x : Var) (a : AExp) : Assertion := fun s => P (s[x ↦ aeval s a])

At first sight the assignment rule seems "backwards" (why is the substitution in the precondition?), but the definition above makes it transparent: if P must hold after the assignment, then, before it, P must hold in the state that the assignment is going to produce. As we will see in the next chapter, P[a/x] is the weakest precondition of the assignment.

3.3.1.2. Connectives and notations

To write assertions more compactly, we define a few connectives on assertions (pointwise conjunction and disjunction), assertions stating that a boolean expression is true or false, and an equality assertion between an arithmetic expression and a number.

def aand (P Q : Assertion) : Assertion := fun s => P s ∧ Q s def aor (P Q : Assertion) : Assertion := fun s => P s ∨ Q s def atrue (b : BExp) : Assertion := fun s => beval s b = true def afalse (b : BExp) : Assertion := fun s => beval s b = false def aequal (a : AExp) (v : ℕ) : Assertion := fun s => aeval s a = v scoped infixl:35 " .&. " => aand scoped infixl:30 " .||. " => aor scoped notation:max "True(" b ")" => atrue b scoped notation:max "False(" b ")" => afalse b scoped notation:65 x " .==. " v => aequal (AExp.var x) v

Thus "x" .==. 5 is the assertion "the variable x has value 5" and P .&. True(b) is the assertion "P holds and the guard b is true".

3.3.2. The rules of Hoare logic

The rules of Hoare logic, one for each IMP construct plus one structural rule, are the following:

\frac{}{\{P\}\ \textsf{skip}\ \{P\}}\,(\textsf{Skip}) \qquad \frac{}{\{P[a/x]\}\ x := a\ \{P\}}\,(\textsf{Assign})

\frac{\{P\}\ c_1\ \{Q\} \quad \{Q\}\ c_2\ \{R\}}{\{P\}\ c_1 ; c_2\ \{R\}}\,(\textsf{Seq})

\frac{\{P \wedge b\}\ c_1\ \{Q\} \quad \{P \wedge \neg b\}\ c_2\ \{Q\}}{\{P\}\ \textsf{if}\ b\ \textsf{then}\ c_1\ \textsf{else}\ c_2\ \{Q\}}\,(\textsf{If})

\frac{\{I \wedge b\}\ c\ \{I\}}{\{I\}\ \textsf{while}\ b\ \textsf{do}\ c\ \{I \wedge \neg b\}}\,(\textsf{While})

\frac{P' \Rightarrow P \quad \{P\}\ c\ \{Q\} \quad Q \Rightarrow Q'}{\{P'\}\ c\ \{Q'\}}\,(\textsf{Conseq})

A few of them are worth commenting on.

  • In the Seq rule, the intermediate assertion Q describes the state between the execution of c_1 and that of c_2. It does not appear in the conclusion and must therefore be "guessed" when building a derivation.

  • In the If rule, each branch may assume, in addition to P, the outcome of the test.

  • The While rule is the heart of the logic. The assertion I is a loop invariant: if it is preserved by one iteration of the body (when the guard is true), then it holds at the end of the loop, and moreover we know that the guard is false. Finding invariants is the creative part of program verification.

  • The Conseq (consequence) rule allows us to strengthen the precondition and weaken the postcondition. It is this rule that connects Hoare logic to the logic of assertions: its side premises are ordinary implications, proved with Lean's usual tools.

In Lean, the rules become the constructors of an inductive predicate:

inductive HoareTriple : Assertion → Com → Assertion → Prop where | skip {P : Assertion} : HoareTriple P .skip P | assign {P : Assertion} {x : Var} {a : AExp} : HoareTriple (assub P x a) (.assign x a) P | seq {P Q R : Assertion} {c₁ c₂ : Com} : HoareTriple P c₁ Q → HoareTriple Q c₂ R → HoareTriple P (c₁ ;; c₂) R | ite {P Q : Assertion} {b : BExp} {c₁ c₂ : Com} : HoareTriple (P .&. True(b)) c₁ Q → HoareTriple (P .&. False(b)) c₂ Q → HoareTriple P (.ite b c₁ c₂) Q | whl {I : Assertion} {b : BExp} {c : Com} : HoareTriple (I .&. True(b)) c I → HoareTriple I (.whl b c) (I .&. False(b)) | conseq {P P' Q Q' : Assertion} {c : Com} : (∀ s, P' s → P s) → HoareTriple P c Q → (∀ s, Q s → Q' s) → HoareTriple P' c Q' scoped notation:25 "{{ " P " }} " c " {{ " Q " }}" => HoareTriple P c Q

It is convenient to have versions of the consequence rule that change only one side of the triple. They are derived rules: special cases of Conseq in which the other implication is the identity.

\frac{P' \Rightarrow P \quad \{P\}\ c\ \{Q\}}{\{P'\}\ c\ \{Q\}}\,(\textsf{Pre}) \qquad \frac{\{P\}\ c\ \{Q\} \quad Q \Rightarrow Q'}{\{P\}\ c\ \{Q'\}}\,(\textsf{Post})

lemma conseq_pre {P P' Q : Assertion} {c : Com} (h : ∀ s, P' s → P s) (ht : {{ P }} c {{ Q }}) : {{ P' }} c {{ Q }} := .conseq h ht (fun _ hq => hq) lemma conseq_post {P Q Q' : Assertion} {c : Com} (ht : {{ P }} c {{ Q }}) (h : ∀ s, Q s → Q' s) : {{ P }} c {{ Q' }} := .conseq (fun _ hp => hp) ht h

3.3.3. Validity of triples

The predicate HoareTriple is purely syntactic: it says which triples can be derived by the rules, but it does not say what a triple means. We need an independent definition, based on the semantics of IMP, that captures the informal reading "if P holds before and c terminates, Q holds afterwards". We use the big-step semantics from the previous chapter:

\models \{P\}\ c\ \{Q\} \;\iff\; \forall s\, t,\ P(s) \wedge \langle c, s\rangle \Downarrow t \implies Q(t)

def ValidTriple (P : Assertion) (c : Com) (Q : Assertion) : Prop := ∀ s t, P s → ⟪c, s⟫ ⟹ t → Q t scoped notation:25 "{{* " P " *}} " c " {{* " Q " *}}" => ValidTriple P c Q

The phrase "if it terminates" is encoded in the hypothesis \langle c, s\rangle \Downarrow t: if c does not terminate from s, there is no t with this property, and the condition is vacuously true.

3.3.4. Soundness of Hoare logic

The relationship between the two notions is given by the soundness theorem: every derivable triple is valid.

\vdash \{P\}\ c\ \{Q\} \implies\ \models \{P\}\ c\ \{Q\}

This theorem justifies the use of Hoare logic: when we build a derivation, we are guaranteed that the program really satisfies its specification. (The converse, relative completeness, also holds, but we will not study it here.)

3.3.4.1. The loop case

The proof of the soundness theorem is by induction on the derivation of the triple. All cases are straightforward, except for the While rule. In that case, the induction hypothesis says that the body preserves the invariant, but an execution of the loop may involve an arbitrary number of iterations. We therefore need a second induction, this time on the big-step derivation of the loop's execution.

There is a technical difficulty. The derivation has the form \langle \textsf{while}\ b\ \textsf{do}\ c, s\rangle \Downarrow t, in which the command is not a variable, and the induction tactic requires indices that are free variables. The solution, already seen in the previous chapters, is to generalize: we prove a statement for an arbitrary command cmd, with the additional hypothesis cmd = .whl b c. In the induction, the cases for rules that are not loop rules are discarded because the equality is impossible (simp detects that different constructors are distinct). In the two loop cases, the injection tactic extracts from the equality that the guard and the body are the expected ones:

  • WhileF: the final state is the initial one, where I holds, and the guard is false;

  • WhileT: the hypothesis about the body guarantees that I holds after one iteration, and the induction hypothesis (about the rest of the loop's execution) concludes.

lemma while_valid {b : BExp} {c : Com} (I : Assertion) (hbody : {{* I .&. True(b) *}} c {{* I *}}) : {{* I *}} (.whl b c) {{* I .&. False(b) *}} := b:BExpc:ComI:Assertionhbody:{{* I .&. True(b) *}} c {{* I *}}⊢ {{* I *}} Com.whl b c {{* I .&. False(b) *}} b:BExpc:ComI:Assertionhbody:{{* I .&. True(b) *}} c {{* I *}}⊢ ∀ (cmd : Com) (s t : State), ⟪cmd, s⟫ ⟹ t → cmd = Com.whl b c → I s → I t ∧ beval t b = false intro cmd b:BExpc:ComI:Assertionhbody:{{* I .&. True(b) *}} c {{* I *}}cmd:Coms:State⊢ ∀ (t : State), ⟪cmd, s⟫ ⟹ t → cmd = Com.whl b c → I s → I t ∧ beval t b = false b:BExpc:ComI:Assertionhbody:{{* I .&. True(b) *}} c {{* I *}}cmd:Coms:Statet:State⊢ ⟪cmd, s⟫ ⟹ t → cmd = Com.whl b c → I s → I t ∧ beval t b = false b:BExpc:ComI:Assertionhbody:{{* I .&. True(b) *}} c {{* I *}}cmd:Coms:Statet:StatehBig:⟪cmd, s⟫ ⟹ t⊢ cmd = Com.whl b c → I s → I t ∧ beval t b = false b:BExpc:ComI:Assertionhbody:{{* I .&. True(b) *}} c {{* I *}}cmd:Coms:Statet:StatehBig:⟪cmd, s⟫ ⟹ thEq:cmd = Com.whl b c⊢ I s → I t ∧ beval t b = false b:BExpc:ComI:Assertionhbody:{{* I .&. True(b) *}} c {{* I *}}cmd:Coms:Statet:StatehBig:⟪cmd, s⟫ ⟹ thEq:cmd = Com.whl b chIs:I s⊢ I t ∧ beval t b = false induction hBig with b:BExpc:ComI:Assertionhbody:{{* I .&. True(b) *}} c {{* I *}}cmd:Coms:Statet:States_1:StatehEq:Com.skip = Com.whl b chIs:I s_1⊢ I s_1 ∧ beval s_1 b = false All goals completed! 🐙 b:BExpc:ComI:Assertionhbody:{{* I .&. True(b) *}} c {{* I *}}cmd:Coms:Statet:Statex:Vara:AExps_1:StatehEq:(x ::= a) = Com.whl b chIs:I s_1⊢ I (s_1[x ↦ aeval s_1 a]) ∧ beval (s_1[x ↦ aeval s_1 a]) b = false All goals completed! 🐙 b:BExpc:ComI:Assertionhbody:{{* I .&. True(b) *}} c {{* I *}}cmd:Coms:Statet:Statec₁:Comc₂:Coms_1:States':States'':Statea:⟪c₁, s_1⟫ ⟹ s'a_1:⟪c₂, s'⟫ ⟹ s''a_ih:c₁ = Com.whl b c → I s_1 → I s' ∧ beval s' b = falsea_ih_1:c₂ = Com.whl b c → I s' → I s'' ∧ beval s'' b = falsehEq:c₁ ;; c₂ = Com.whl b chIs:I s_1⊢ I s'' ∧ beval s'' b = false All goals completed! 🐙 b:BExpc:ComI:Assertionhbody:{{* I .&. True(b) *}} c {{* I *}}cmd:Coms:Statet:Stateb_1:BExpc₁:Comc₂:Coms_1:States':Statea:beval s_1 b_1 = truea_1:⟪c₁, s_1⟫ ⟹ s'a_ih:c₁ = Com.whl b c → I s_1 → I s' ∧ beval s' b = falsehEq:Com.ite b_1 c₁ c₂ = Com.whl b chIs:I s_1⊢ I s' ∧ beval s' b = false All goals completed! 🐙 b:BExpc:ComI:Assertionhbody:{{* I .&. True(b) *}} c {{* I *}}cmd:Coms:Statet:Stateb_1:BExpc₁:Comc₂:Coms_1:States':Statea:beval s_1 b_1 = falsea_1:⟪c₂, s_1⟫ ⟹ s'a_ih:c₂ = Com.whl b c → I s_1 → I s' ∧ beval s' b = falsehEq:Com.ite b_1 c₁ c₂ = Com.whl b chIs:I s_1⊢ I s' ∧ beval s' b = false All goals completed! 🐙 b:BExpc:ComI:Assertionhbody:{{* I .&. True(b) *}} c {{* I *}}cmd:Coms:Statet:Stateb_1:BExpc_1:Coms':StatehbF:beval s' b_1 = falsehEq:Com.whl b_1 c_1 = Com.whl b chIs:I s'⊢ I s' ∧ beval s' b = false b:BExpc:ComI:Assertionhbody:{{* I .&. True(b) *}} c {{* I *}}cmd:Coms:Statet:Stateb_1:BExpc_1:Coms':StatehbF:beval s' b_1 = falsehIs:I s'hb:b_1 = bhc_eq:c_1 = c⊢ I s' ∧ beval s' b = false All goals completed! 🐙 b:BExpc:ComI:Assertionhbody:{{* I .&. True(b) *}} c {{* I *}}cmd:Coms:Statet:Stateb_1:BExpc_1:Coms_1:States':States'':StatehbT:beval s_1 b_1 = truehc:⟪c_1, s_1⟫ ⟹ s'hw:⟪Com.whl b_1 c_1, s'⟫ ⟹ s''a_ih:c_1 = Com.whl b c → I s_1 → I s' ∧ beval s' b = falseIHw:Com.whl b_1 c_1 = Com.whl b c → I s' → I s'' ∧ beval s'' b = falsehEq:Com.whl b_1 c_1 = Com.whl b chIs:I s_1⊢ I s'' ∧ beval s'' b = false b:BExpc:ComI:Assertionhbody:{{* I .&. True(b) *}} c {{* I *}}cmd:Coms:Statet:Stateb_1:BExpc_1:Coms_1:States':States'':StatehbT:beval s_1 b_1 = truehc:⟪c_1, s_1⟫ ⟹ s'hw:⟪Com.whl b_1 c_1, s'⟫ ⟹ s''a_ih:c_1 = Com.whl b c → I s_1 → I s' ∧ beval s' b = falseIHw:Com.whl b_1 c_1 = Com.whl b c → I s' → I s'' ∧ beval s'' b = falsehEq:Com.whl b_1 c_1 = Com.whl b chIs:I s_1hEq':Com.whl b_1 c_1 = Com.whl b c⊢ I s'' ∧ beval s'' b = false b:BExpc:ComI:Assertionhbody:{{* I .&. True(b) *}} c {{* I *}}cmd:Coms:Statet:Stateb_1:BExpc_1:Coms_1:States':States'':StatehbT:beval s_1 b_1 = truehc:⟪c_1, s_1⟫ ⟹ s'hw:⟪Com.whl b_1 c_1, s'⟫ ⟹ s''a_ih:c_1 = Com.whl b c → I s_1 → I s' ∧ beval s' b = falseIHw:Com.whl b_1 c_1 = Com.whl b c → I s' → I s'' ∧ beval s'' b = falsehIs:I s_1hEq':Com.whl b_1 c_1 = Com.whl b chb:b_1 = bhc_eq:c_1 = c⊢ I s'' ∧ beval s'' b = false All goals completed! 🐙

3.3.4.2. The soundness theorem

With the loop lemma in hand, the soundness theorem is an induction on the derivation of the triple. In each case, we assume the precondition in s and an execution \langle c, s\rangle \Downarrow t, which we invert with cases:

  • Skip and Assign: inverting the execution determines t, and the precondition is, literally, the postcondition in the final state (in the case of assignment, thanks to the semantic definition of assub);

  • Seq: the execution splits into two, and we chain the induction hypotheses through the intermediate state;

  • If: depending on the rule used in the execution, we use the induction hypothesis for one branch or the other, supplying the information about the guard;

  • While: we apply while_valid;

  • Conseq: we use the side implications before and after the induction hypothesis.

theorem hoare_sound {P : Assertion} {c : Com} {Q : Assertion} (h : {{ P }} c {{ Q }}) : {{* P *}} c {{* Q *}} := P:Assertionc:ComQ:Assertionh:{{ P }} c {{ Q }}⊢ {{* P *}} c {{* Q *}} induction h with P:Assertionc:ComQ:AssertionP_1:Assertion⊢ {{* P_1 *}} Com.skip {{* P_1 *}} intro s P:Assertionc:ComQ:AssertionP_1:Assertions:Statet:State⊢ P_1 s → ⟪Com.skip, s⟫ ⟹ t → P_1 t P:Assertionc:ComQ:AssertionP_1:Assertions:Statet:StatehPs:P_1 s⊢ ⟪Com.skip, s⟫ ⟹ t → P_1 t P:Assertionc:ComQ:AssertionP_1:Assertions:Statet:StatehPs:P_1 shBig:⟪Com.skip, s⟫ ⟹ t⊢ P_1 t; P:Assertionc:ComQ:AssertionP_1:Assertions:StatehPs:P_1 s⊢ P_1 s; All goals completed! 🐙 P:Assertionc:ComQ:AssertionP_1:Assertionx:Vara:AExp⊢ {{* assub P_1 x a *}} x ::= a {{* P_1 *}} intro s P:Assertionc:ComQ:AssertionP_1:Assertionx:Vara:AExps:Statet:State⊢ assub P_1 x a s → ⟪x ::= a, s⟫ ⟹ t → P_1 t P:Assertionc:ComQ:AssertionP_1:Assertionx:Vara:AExps:Statet:StatehPs:assub P_1 x a s⊢ ⟪x ::= a, s⟫ ⟹ t → P_1 t P:Assertionc:ComQ:AssertionP_1:Assertionx:Vara:AExps:Statet:StatehPs:assub P_1 x a shBig:⟪x ::= a, s⟫ ⟹ t⊢ P_1 t; P:Assertionc:ComQ:AssertionP_1:Assertionx:Vara:AExps:StatehPs:assub P_1 x a s⊢ P_1 (s[x ↦ aeval s a]); All goals completed! 🐙 P:Assertionc:ComQ:AssertionP_1:AssertionQ_1:AssertionR:Assertionc₁:Comc₂:Coma:{{ P_1 }} c₁ {{ Q_1 }}a_1:{{ Q_1 }} c₂ {{ R }}IH1:{{* P_1 *}} c₁ {{* Q_1 *}}IH2:{{* Q_1 *}} c₂ {{* R *}}⊢ {{* P_1 *}} c₁ ;; c₂ {{* R *}} intro s P:Assertionc:ComQ:AssertionP_1:AssertionQ_1:AssertionR:Assertionc₁:Comc₂:Coma:{{ P_1 }} c₁ {{ Q_1 }}a_1:{{ Q_1 }} c₂ {{ R }}IH1:{{* P_1 *}} c₁ {{* Q_1 *}}IH2:{{* Q_1 *}} c₂ {{* R *}}s:Statet:State⊢ P_1 s → ⟪c₁ ;; c₂, s⟫ ⟹ t → R t P:Assertionc:ComQ:AssertionP_1:AssertionQ_1:AssertionR:Assertionc₁:Comc₂:Coma:{{ P_1 }} c₁ {{ Q_1 }}a_1:{{ Q_1 }} c₂ {{ R }}IH1:{{* P_1 *}} c₁ {{* Q_1 *}}IH2:{{* Q_1 *}} c₂ {{* R *}}s:Statet:StatehPs:P_1 s⊢ ⟪c₁ ;; c₂, s⟫ ⟹ t → R t P:Assertionc:ComQ:AssertionP_1:AssertionQ_1:AssertionR:Assertionc₁:Comc₂:Coma:{{ P_1 }} c₁ {{ Q_1 }}a_1:{{ Q_1 }} c₂ {{ R }}IH1:{{* P_1 *}} c₁ {{* Q_1 *}}IH2:{{* Q_1 *}} c₂ {{* R *}}s:Statet:StatehPs:P_1 shBig:⟪c₁ ;; c₂, s⟫ ⟹ t⊢ R t cases hBig with P:Assertionc:ComQ:AssertionP_1:AssertionQ_1:AssertionR:Assertionc₁:Comc₂:Coma:{{ P_1 }} c₁ {{ Q_1 }}a_1:{{ Q_1 }} c₂ {{ R }}IH1:{{* P_1 *}} c₁ {{* Q_1 *}}IH2:{{* Q_1 *}} c₂ {{* R *}}s:Statet:StatehPs:P_1 ss':Stateh1:⟪c₁, s⟫ ⟹ s'h2:⟪c₂, s'⟫ ⟹ t⊢ R t All goals completed! 🐙 P:Assertionc:ComQ:AssertionP_1:AssertionQ_1:Assertionb:BExpc₁:Comc₂:Coma:{{ P_1 .&. True(b) }} c₁ {{ Q_1 }}a_1:{{ P_1 .&. False(b) }} c₂ {{ Q_1 }}IH1:{{* P_1 .&. True(b) *}} c₁ {{* Q_1 *}}IH2:{{* P_1 .&. False(b) *}} c₂ {{* Q_1 *}}⊢ {{* P_1 *}} Com.ite b c₁ c₂ {{* Q_1 *}} intro s P:Assertionc:ComQ:AssertionP_1:AssertionQ_1:Assertionb:BExpc₁:Comc₂:Coma:{{ P_1 .&. True(b) }} c₁ {{ Q_1 }}a_1:{{ P_1 .&. False(b) }} c₂ {{ Q_1 }}IH1:{{* P_1 .&. True(b) *}} c₁ {{* Q_1 *}}IH2:{{* P_1 .&. False(b) *}} c₂ {{* Q_1 *}}s:Statet:State⊢ P_1 s → ⟪Com.ite b c₁ c₂, s⟫ ⟹ t → Q_1 t P:Assertionc:ComQ:AssertionP_1:AssertionQ_1:Assertionb:BExpc₁:Comc₂:Coma:{{ P_1 .&. True(b) }} c₁ {{ Q_1 }}a_1:{{ P_1 .&. False(b) }} c₂ {{ Q_1 }}IH1:{{* P_1 .&. True(b) *}} c₁ {{* Q_1 *}}IH2:{{* P_1 .&. False(b) *}} c₂ {{* Q_1 *}}s:Statet:StatehPs:P_1 s⊢ ⟪Com.ite b c₁ c₂, s⟫ ⟹ t → Q_1 t P:Assertionc:ComQ:AssertionP_1:AssertionQ_1:Assertionb:BExpc₁:Comc₂:Coma:{{ P_1 .&. True(b) }} c₁ {{ Q_1 }}a_1:{{ P_1 .&. False(b) }} c₂ {{ Q_1 }}IH1:{{* P_1 .&. True(b) *}} c₁ {{* Q_1 *}}IH2:{{* P_1 .&. False(b) *}} c₂ {{* Q_1 *}}s:Statet:StatehPs:P_1 shBig:⟪Com.ite b c₁ c₂, s⟫ ⟹ t⊢ Q_1 t cases hBig with P:Assertionc:ComQ:AssertionP_1:AssertionQ_1:Assertionb:BExpc₁:Comc₂:Coma:{{ P_1 .&. True(b) }} c₁ {{ Q_1 }}a_1:{{ P_1 .&. False(b) }} c₂ {{ Q_1 }}IH1:{{* P_1 .&. True(b) *}} c₁ {{* Q_1 *}}IH2:{{* P_1 .&. False(b) *}} c₂ {{* Q_1 *}}s:Statet:StatehPs:P_1 shbT:beval s b = truehc:⟪c₁, s⟫ ⟹ t⊢ Q_1 t All goals completed! 🐙 P:Assertionc:ComQ:AssertionP_1:AssertionQ_1:Assertionb:BExpc₁:Comc₂:Coma:{{ P_1 .&. True(b) }} c₁ {{ Q_1 }}a_1:{{ P_1 .&. False(b) }} c₂ {{ Q_1 }}IH1:{{* P_1 .&. True(b) *}} c₁ {{* Q_1 *}}IH2:{{* P_1 .&. False(b) *}} c₂ {{* Q_1 *}}s:Statet:StatehPs:P_1 shbF:beval s b = falsehc:⟪c₂, s⟫ ⟹ t⊢ Q_1 t All goals completed! 🐙 P:Assertionc:ComQ:AssertionI:Assertionb:BExpc_1:Coma:{{ I .&. True(b) }} c_1 {{ I }}IH:{{* I .&. True(b) *}} c_1 {{* I *}}⊢ {{* I *}} Com.whl b c_1 {{* I .&. False(b) *}} All goals completed! 🐙 P:Assertionc:ComQ:AssertionP_1:AssertionP':AssertionQ_1:AssertionQ':Assertionc_1:ComhPP':∀ (s : State), P' s → P_1 sa:{{ P_1 }} c_1 {{ Q_1 }}hQQ':∀ (s : State), Q_1 s → Q' sIH:{{* P_1 *}} c_1 {{* Q_1 *}}⊢ {{* P' *}} c_1 {{* Q' *}} intro s P:Assertionc:ComQ:AssertionP_1:AssertionP':AssertionQ_1:AssertionQ':Assertionc_1:ComhPP':∀ (s : State), P' s → P_1 sa:{{ P_1 }} c_1 {{ Q_1 }}hQQ':∀ (s : State), Q_1 s → Q' sIH:{{* P_1 *}} c_1 {{* Q_1 *}}s:Statet:State⊢ P' s → ⟪c_1, s⟫ ⟹ t → Q' t P:Assertionc:ComQ:AssertionP_1:AssertionP':AssertionQ_1:AssertionQ':Assertionc_1:ComhPP':∀ (s : State), P' s → P_1 sa:{{ P_1 }} c_1 {{ Q_1 }}hQQ':∀ (s : State), Q_1 s → Q' sIH:{{* P_1 *}} c_1 {{* Q_1 *}}s:Statet:StatehP's:P' s⊢ ⟪c_1, s⟫ ⟹ t → Q' t P:Assertionc:ComQ:AssertionP_1:AssertionP':AssertionQ_1:AssertionQ':Assertionc_1:ComhPP':∀ (s : State), P' s → P_1 sa:{{ P_1 }} c_1 {{ Q_1 }}hQQ':∀ (s : State), Q_1 s → Q' sIH:{{* P_1 *}} c_1 {{* Q_1 *}}s:Statet:StatehP's:P' shBig:⟪c_1, s⟫ ⟹ t⊢ Q' t All goals completed! 🐙

3.3.5. Example derivations

Let us now see how to use the system to verify programs. A common pattern is to apply the appropriate structural rule and leave for the end the proof obligations generated by the consequence rule, which are facts about states discharged with simp and arithmetic. The swap tactic is useful for handling the triple goal first and the side implication afterwards, since only after applying the assignment rule does Lean find out what the precondition P is.

3.3.5.1. Assignments

The assignment rule can be used directly when the precondition already has the form of a substitution:

example : {{ assub (fun s => s "x" = 5) "x" (.num 5) }} ("x" ::= .num 5) {{ "x" .==. 5 }} := ⊢ {{ assub (fun s => s "x" = 5) "x" (AExp.num 5) }} "x" ::= AExp.num 5 {{ "x" .==. 5 }} All goals completed! 🐙

More often, the desired precondition is a different one, and we combine the assignment rule with the Pre rule. To prove \{\top\}\ x := 0\ \{x = 0\}, we must show that \top \Rightarrow (x = 0)[0/x], that is, that 0 = 0:

example : {{ (fun _ => True) }} ("x" ::= .num 0) {{ (fun s => s "x" = 0) }} := ⊢ {{ fun x => True }} "x" ::= AExp.num 0 {{ fun s => s "x" = 0 }} ⊢ ∀ (s : State), True → ?P s⊢ {{ ?P }} "x" ::= AExp.num 0 {{ fun s => s "x" = 0 }}⊢ Assertion ⊢ {{ ?P }} "x" ::= AExp.num 0 {{ fun s => s "x" = 0 }}⊢ ∀ (s : State), True → ?P s⊢ Assertion ⊢ {{ ?P }} "x" ::= AExp.num 0 {{ fun s => s "x" = 0 }} All goals completed! 🐙 ⊢ ∀ (s : State), True → assub (fun s => s "x" = 0) "x" (AExp.num 0) s intro s s:Statea:True⊢ assub (fun s => s "x" = 0) "x" (AExp.num 0) s All goals completed! 🐙

Incrementing a variable follows the same pattern: the proof obligation is x = n \Rightarrow x + 1 = n + 1.

example (n : ℕ) : {{ "x" .==. n }} ("x" ::= .add (.var "x") (.num 1)) {{ "x" .==. n + 1 }} := n:ℕ⊢ {{ "x" .==. n }} "x" ::= (AExp.var "x").add (AExp.num 1) {{ "x" .==. n + 1 }} n:ℕ⊢ ∀ (s : State), ("x" .==. n) s → ?P sn:ℕ⊢ {{ ?P }} "x" ::= (AExp.var "x").add (AExp.num 1) {{ "x" .==. n + 1 }}n:ℕ⊢ Assertion n:ℕ⊢ {{ ?P }} "x" ::= (AExp.var "x").add (AExp.num 1) {{ "x" .==. n + 1 }}n:ℕ⊢ ∀ (s : State), ("x" .==. n) s → ?P sn:ℕ⊢ Assertion n:ℕ⊢ {{ ?P }} "x" ::= (AExp.var "x").add (AExp.num 1) {{ "x" .==. n + 1 }} All goals completed! 🐙 n:ℕ⊢ ∀ (s : State), ("x" .==. n) s → assub ("x" .==. n + 1) "x" ((AExp.var "x").add (AExp.num 1)) s intro s n:ℕs:Statehs:("x" .==. n) s⊢ assub ("x" .==. n + 1) "x" ((AExp.var "x").add (AExp.num 1)) s n:ℕs:Statehs:s "x" = n⊢ s "x" = n All goals completed! 🐙

3.3.5.2. A trivial loop

For a loop, we need an invariant. In the program while false do skip, the invariant \top is enough: the While rule produces the postcondition \top \wedge \neg\textsf{false}, which we weaken to \top with the Post rule.

example : {{ (fun _ => True) }} (.whl .ff .skip) {{ (fun _ => True) }} := ⊢ {{ fun x => True }} Com.whl BExp.ff Com.skip {{ fun x => True }} ⊢ {{ fun x => True }} Com.whl BExp.ff Com.skip {{ ?Q }}⊢ ∀ (s : State), ?Q s → True⊢ Assertion ⊢ {{ fun x => True }} Com.whl BExp.ff Com.skip {{ ?Q }} ⊢ {{ (fun x => True) .&. True(BExp.ff) }} Com.skip {{ fun x => True }} ⊢ ∀ (s : State), ((fun x => True) .&. True(BExp.ff)) s → ?ht.a.P s⊢ {{ ?ht.a.P }} Com.skip {{ fun x => True }}⊢ Assertion ⊢ {{ ?ht.a.P }} Com.skip {{ fun x => True }}⊢ ∀ (s : State), ((fun x => True) .&. True(BExp.ff)) s → ?ht.a.P s⊢ Assertion ⊢ {{ ?ht.a.P }} Com.skip {{ fun x => True }} All goals completed! 🐙 ⊢ ∀ (s : State), ((fun x => True) .&. True(BExp.ff)) s → True intro s s:Statea:((fun x => True) .&. True(BExp.ff)) s⊢ True All goals completed! 🐙 ⊢ ∀ (s : State), ((fun x => True) .&. False(BExp.ff)) s → True intro _ s:Stateh:((fun x => True) .&. False(BExp.ff)) s⊢ True All goals completed! 🐙

3.3.5.3. Sequences

In the Seq rule, we must supply the intermediate assertion, which we do with the named argument (Q := ...). For the program x := 1 ;; y := 2, the natural intermediate assertion is x = 1. The second proof obligation requires showing that the assignment to y does not change x, which depends on "x" ≠ "y".

example : {{ (fun _ => True) }} (("x" ::= .num 1) ;; ("y" ::= .num 2)) {{ ("x" .==. 1) .&. ("y" .==. 2) }} := ⊢ {{ fun x => True }} ("x" ::= AExp.num 1) ;; ("y" ::= AExp.num 2) {{ ("x" .==. 1) .&. "y" .==. 2 }} ⊢ {{ fun x => True }} "x" ::= AExp.num 1 {{ "x" .==. 1 }}⊢ {{ "x" .==. 1 }} "y" ::= AExp.num 2 {{ ("x" .==. 1) .&. "y" .==. 2 }} ⊢ {{ fun x => True }} "x" ::= AExp.num 1 {{ "x" .==. 1 }} ⊢ ∀ (s : State), True → ?a.P✝ s⊢ {{ ?a.P✝ }} "x" ::= AExp.num 1 {{ "x" .==. 1 }}⊢ Assertion ⊢ {{ ?a.P✝ }} "x" ::= AExp.num 1 {{ "x" .==. 1 }}⊢ ∀ (s : State), True → ?a.P✝ s⊢ Assertion ⊢ {{ ?a.P✝ }} "x" ::= AExp.num 1 {{ "x" .==. 1 }} All goals completed! 🐙 ⊢ ∀ (s : State), True → assub ("x" .==. 1) "x" (AExp.num 1) s intro s s:Statea:True⊢ assub ("x" .==. 1) "x" (AExp.num 1) s All goals completed! 🐙 ⊢ {{ "x" .==. 1 }} "y" ::= AExp.num 2 {{ ("x" .==. 1) .&. "y" .==. 2 }} ⊢ ∀ (s : State), ("x" .==. 1) s → ?a.P✝ s⊢ {{ ?a.P✝ }} "y" ::= AExp.num 2 {{ ("x" .==. 1) .&. "y" .==. 2 }}⊢ Assertion ⊢ {{ ?a.P✝ }} "y" ::= AExp.num 2 {{ ("x" .==. 1) .&. "y" .==. 2 }}⊢ ∀ (s : State), ("x" .==. 1) s → ?a.P✝ s⊢ Assertion ⊢ {{ ?a.P✝ }} "y" ::= AExp.num 2 {{ ("x" .==. 1) .&. "y" .==. 2 }} All goals completed! 🐙 ⊢ ∀ (s : State), ("x" .==. 1) s → assub (("x" .==. 1) .&. "y" .==. 2) "y" (AExp.num 2) s intro s s:Statehs:("x" .==. 1) s⊢ assub (("x" .==. 1) .&. "y" .==. 2) "y" (AExp.num 2) s s:Statehs:s "x" = 1⊢ (if "x" = "y" then 2 else s "x") = 1 ∧ (if True then 2 else s "y") = 2 have hne : ("x" : Var) ≠ "y" := ⊢ {{ fun x => True }} ("x" ::= AExp.num 1) ;; ("y" ::= AExp.num 2) {{ ("x" .==. 1) .&. "y" .==. 2 }} All goals completed! 🐙 s:Statehs:s "x" = 1hne:"x" ≠ "y"h✝:"x" = "y"⊢ 2 = 1 ∧ 2 = 2s:Statehs:s "x" = 1hne:"x" ≠ "y"h✝:¬"x" = "y"⊢ s "x" = 1 ∧ 2 = 2 s:Statehs:s "x" = 1hne:"x" ≠ "y"h✝:"x" = "y"⊢ 2 = 1 ∧ 2 = 2 All goals completed! 🐙 s:Statehs:s "x" = 1hne:"x" ≠ "y"h✝:¬"x" = "y"⊢ s "x" = 1 ∧ 2 = 2 All goals completed! 🐙

When the sequence consists only of assignments, we do not need to guess intermediate assertions: if we let Lean infer them from the Assign rule, working backwards, it automatically computes the chained substitutions. This is what we do in the verification of the program Ch18.swap, which swaps the values of x and y. The only remaining proof obligation relates the given precondition to the computed one.

example (a b : ℕ) : {{ ("x" .==. a) .&. ("y" .==. b) }} swap {{ ("x" .==. b) .&. ("y" .==. a) }} := a:ℕb:ℕ⊢ {{ ("x" .==. a) .&. "y" .==. b }} swap {{ ("x" .==. b) .&. "y" .==. a }} a:ℕb:ℕ⊢ ∀ (s : State), (("x" .==. a) .&. "y" .==. b) s → ?P sa:ℕb:ℕ⊢ {{ ?P }} swap {{ ("x" .==. b) .&. "y" .==. a }}a:ℕb:ℕ⊢ Assertion a:ℕb:ℕ⊢ {{ ?P }} swap {{ ("x" .==. b) .&. "y" .==. a }}a:ℕb:ℕ⊢ ∀ (s : State), (("x" .==. a) .&. "y" .==. b) s → ?P sa:ℕb:ℕ⊢ Assertion a:ℕb:ℕ⊢ {{ ?P }} swap {{ ("x" .==. b) .&. "y" .==. a }} a:ℕb:ℕ⊢ {{ ?P }} ("tmp" ::= AExp.var "x") ;; ("x" ::= AExp.var "y") {{ ?ht.Q }}a:ℕb:ℕ⊢ {{ ?ht.Q }} "y" ::= AExp.var "tmp" {{ ("x" .==. b) .&. "y" .==. a }}a:ℕb:ℕ⊢ Assertion a:ℕb:ℕ⊢ {{ ?P }} ("tmp" ::= AExp.var "x") ;; ("x" ::= AExp.var "y") {{ ?ht.Q }} a:ℕb:ℕ⊢ {{ ?P }} "tmp" ::= AExp.var "x" {{ ?ht.a.Q }}a:ℕb:ℕ⊢ {{ ?ht.a.Q }} "x" ::= AExp.var "y" {{ ?ht.Q }}a:ℕb:ℕ⊢ Assertion a:ℕb:ℕ⊢ {{ ?P }} "tmp" ::= AExp.var "x" {{ ?ht.a.Q }} All goals completed! 🐙 a:ℕb:ℕ⊢ {{ ?ht.a.Q }} "x" ::= AExp.var "y" {{ ?ht.Q }} All goals completed! 🐙 a:ℕb:ℕ⊢ {{ ?ht.Q }} "y" ::= AExp.var "tmp" {{ ("x" .==. b) .&. "y" .==. a }} All goals completed! 🐙 a:ℕb:ℕ⊢ ∀ (s : State), (("x" .==. a) .&. "y" .==. b) s → assub (assub (assub (("x" .==. b) .&. "y" .==. a) "y" (AExp.var "tmp")) "x" (AExp.var "y")) "tmp" (AExp.var "x") s intro s a:ℕb:ℕs:StateH:(("x" .==. a) .&. "y" .==. b) s⊢ assub (assub (assub (("x" .==. b) .&. "y" .==. a) "y" (AExp.var "tmp")) "x" (AExp.var "y")) "tmp" (AExp.var "x") s a:ℕb:ℕs:StateH:s "x" = a ∧ s "y" = b⊢ s "y" = b ∧ s "x" = a All goals completed! 🐙

This phenomenon, the automatic computation of preconditions from postconditions, is the seed of the next chapter.

3.3.5.4. Factorial

We finish with a more substantial example: the iterative computation of the factorial.

r := 1;
while (n ≠ 0) do
  r := r * n;
  n := n - 1

We want to prove that, if initially n = n_0, then at the end r = n_0!. The crucial step is finding the loop invariant. Throughout the execution, r accumulates the product of the values of n already "consumed", while n! represents the product that remains to be computed. This suggests the invariant

I \;\equiv\; r \cdot n! = n_0!

It holds on entry to the loop (since r = 1 and n = n_0); it is preserved by the body, since r \cdot n \cdot (n-1)! = r \cdot n! when n \neq 0; and, on exit, when n = 0, we have r \cdot 0! = r = n_0!.

The derivation follows exactly this plan. The first proof obligation is the preservation of the invariant by the body. After simplifying the substitutions, we need the fact n \cdot (n-1)! = n! for n \neq 0, which we obtain by case analysis on n, and a calculation with ring. The second obligation, the loop exit, extracts n = 0 from the false guard and simplifies the invariant.

def factProg : Com := ("r" ::= .num 1) ;; .whl (.not (.eq (.var "n") (.num 0))) (("r" ::= .mul (.var "r") (.var "n")) ;; ("n" ::= .sub (.var "n") (.num 1))) example (n₀ : ℕ) : {{ "n" .==. n₀ }} factProg {{ "r" .==. n₀.factorial }} := n₀:ℕ⊢ {{ "n" .==. n₀ }} factProg {{ "r" .==. n₀.factorial }} n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ {{ "n" .==. n₀ }} factProg {{ "r" .==. n₀.factorial }} n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ {{ "n" .==. n₀ }} "r" ::= AExp.num 1 {{ I }}n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ {{ I }} Com.whl (BExp.eq (AExp.var "n") (AExp.num 0)).not (("r" ::= (AExp.var "r").mul (AExp.var "n")) ;; ("n" ::= (AExp.var "n").sub (AExp.num 1))) {{ "r" .==. n₀.factorial }} n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ {{ "n" .==. n₀ }} "r" ::= AExp.num 1 {{ I }} -- { n = n₀ } r := 1 { I } n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ ∀ (s : State), ("n" .==. n₀) s → ?a.P✝ sn₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ {{ ?a.P✝ }} "r" ::= AExp.num 1 {{ I }}n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ Assertion n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ {{ ?a.P✝ }} "r" ::= AExp.num 1 {{ I }}n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ ∀ (s : State), ("n" .==. n₀) s → ?a.P✝ sn₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ Assertion n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ {{ ?a.P✝ }} "r" ::= AExp.num 1 {{ I }} All goals completed! 🐙 n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ ∀ (s : State), ("n" .==. n₀) s → assub I "r" (AExp.num 1) s intro s n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorials:Statehs:("n" .==. n₀) s⊢ assub I "r" (AExp.num 1) s n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorials:Statehs:s "n" = n₀⊢ (s "n").factorial = n₀.factorial All goals completed! 🐙 n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ {{ I }} Com.whl (BExp.eq (AExp.var "n") (AExp.num 0)).not (("r" ::= (AExp.var "r").mul (AExp.var "n")) ;; ("n" ::= (AExp.var "n").sub (AExp.num 1))) {{ "r" .==. n₀.factorial }} -- { I } while ... { r = n₀! } n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ {{ I }} Com.whl (BExp.eq (AExp.var "n") (AExp.num 0)).not (("r" ::= (AExp.var "r").mul (AExp.var "n")) ;; ("n" ::= (AExp.var "n").sub (AExp.num 1))) {{ ?a.Q✝ }}n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ ∀ (s : State), ?a.Q✝ s → ("r" .==. n₀.factorial) sn₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ Assertion n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ {{ I }} Com.whl (BExp.eq (AExp.var "n") (AExp.num 0)).not (("r" ::= (AExp.var "r").mul (AExp.var "n")) ;; ("n" ::= (AExp.var "n").sub (AExp.num 1))) {{ ?a.Q✝ }} n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ {{ I .&. True((BExp.eq (AExp.var "n") (AExp.num 0)).not) }} ("r" ::= (AExp.var "r").mul (AExp.var "n")) ;; ("n" ::= (AExp.var "n").sub (AExp.num 1)) {{ I }} n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ ∀ (s : State), (I .&. True((BExp.eq (AExp.var "n") (AExp.num 0)).not)) s → ?a.ht.a.P✝ sn₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ {{ ?a.ht.a.P✝ }} ("r" ::= (AExp.var "r").mul (AExp.var "n")) ;; ("n" ::= (AExp.var "n").sub (AExp.num 1)) {{ I }}n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ Assertion n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ {{ ?a.ht.a.P✝ }} ("r" ::= (AExp.var "r").mul (AExp.var "n")) ;; ("n" ::= (AExp.var "n").sub (AExp.num 1)) {{ I }}n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ ∀ (s : State), (I .&. True((BExp.eq (AExp.var "n") (AExp.num 0)).not)) s → ?a.ht.a.P✝ sn₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ Assertion n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ {{ ?a.ht.a.P✝ }} ("r" ::= (AExp.var "r").mul (AExp.var "n")) ;; ("n" ::= (AExp.var "n").sub (AExp.num 1)) {{ I }} n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ {{ ?a.ht.a.P✝ }} "r" ::= (AExp.var "r").mul (AExp.var "n") {{ ?a.ht.a.ht.Q✝ }}n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ {{ ?a.ht.a.ht.Q✝ }} "n" ::= (AExp.var "n").sub (AExp.num 1) {{ I }}n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ Assertion n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ {{ ?a.ht.a.P✝ }} "r" ::= (AExp.var "r").mul (AExp.var "n") {{ ?a.ht.a.ht.Q✝ }} All goals completed! 🐙 n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ {{ ?a.ht.a.ht.Q✝ }} "n" ::= (AExp.var "n").sub (AExp.num 1) {{ I }} All goals completed! 🐙 n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ ∀ (s : State), (I .&. True((BExp.eq (AExp.var "n") (AExp.num 0)).not)) s → assub (assub I "n" ((AExp.var "n").sub (AExp.num 1))) "r" ((AExp.var "r").mul (AExp.var "n")) s -- the body preserves the invariant intro s n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorials:StatehI:I shbT:True((BExp.eq (AExp.var "n") (AExp.num 0)).not) s⊢ assub (assub I "n" ((AExp.var "n").sub (AExp.num 1))) "r" ((AExp.var "r").mul (AExp.var "n")) s replace hbT : s "n" ≠ 0 := n₀:ℕ⊢ {{ "n" .==. n₀ }} factProg {{ "r" .==. n₀.factorial }} n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorials:StatehI:I shbT:True((BExp.eq (AExp.var "n") (AExp.num 0)).not) sh:s "n" = 0⊢ False n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorials:StatehI:I shbT:True((BExp.eq (AExp.var "n") (AExp.num 0)).not) sh:s "n" = 0this:(!s "n" == 0) = true⊢ False n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorials:StatehI:I shbT:True((BExp.eq (AExp.var "n") (AExp.num 0)).not) sh:s "n" = 0this:(!0 == 0) = true⊢ False; All goals completed! 🐙 have hrn : ("r" : Var) ≠ "n" := n₀:ℕ⊢ {{ "n" .==. n₀ }} factProg {{ "r" .==. n₀.factorial }} All goals completed! 🐙 have hnr : ("n" : Var) ≠ "r" := n₀:ℕ⊢ {{ "n" .==. n₀ }} factProg {{ "r" .==. n₀.factorial }} All goals completed! 🐙 n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorials:StatehI:I shbT:s "n" ≠ 0hrn:"r" ≠ "n"hnr:"n" ≠ "r"⊢ s "r" * s "n" * (s "n" - 1).factorial = n₀.factorial have hfact : s "n" * (s "n" - 1).factorial = (s "n").factorial := match s "n", hbT with n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorials:StatehI:I shbT:s "n" ≠ 0hrn:"r" ≠ "n"hnr:"n" ≠ "r"k:ℕx✝:k.succ ≠ 0⊢ k.succ * (k.succ - 1).factorial = k.succ.factorial n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorials:StatehI:I shbT:s "n" ≠ 0hrn:"r" ≠ "n"hnr:"n" ≠ "r"k:ℕx✝:k.succ ≠ 0⊢ k.succ * (k.succ - 1).factorial = k.succ.factorial All goals completed! 🐙 calc s "r" * s "n" * (s "n" - 1).factorial = s "r" * (s "n" * (s "n" - 1).factorial) := n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorials:StatehI:I shbT:s "n" ≠ 0hrn:"r" ≠ "n"hnr:"n" ≠ "r"hfact:s "n" * (s "n" - 1).factorial = (s "n").factorial⊢ s "r" * s "n" * (s "n" - 1).factorial = s "r" * (s "n" * (s "n" - 1).factorial) All goals completed! 🐙 _ = s "r" * (s "n").factorial := n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorials:StatehI:I shbT:s "n" ≠ 0hrn:"r" ≠ "n"hnr:"n" ≠ "r"hfact:s "n" * (s "n" - 1).factorial = (s "n").factorial⊢ s "r" * (s "n" * (s "n" - 1).factorial) = s "r" * (s "n").factorial All goals completed! 🐙 _ = n₀.factorial := hI n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorial⊢ ∀ (s : State), (I .&. False((BExp.eq (AExp.var "n") (AExp.num 0)).not)) s → ("r" .==. n₀.factorial) s -- loop exit: I ∧ n = 0 → r = n₀! intro s n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorials:StatehI:I shbF:False((BExp.eq (AExp.var "n") (AExp.num 0)).not) s⊢ ("r" .==. n₀.factorial) s replace hbF : s "n" = 0 := n₀:ℕ⊢ {{ "n" .==. n₀ }} factProg {{ "r" .==. n₀.factorial }} cases hn : s "n" with n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorials:StatehI:I shbF:False((BExp.eq (AExp.var "n") (AExp.num 0)).not) shn:s "n" = 0⊢ 0 = 0 All goals completed! 🐙 n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorials:StatehI:I shbF:False((BExp.eq (AExp.var "n") (AExp.num 0)).not) sk:ℕhn:s "n" = k + 1⊢ k + 1 = 0 n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorials:StatehI:I shbF:False((BExp.eq (AExp.var "n") (AExp.num 0)).not) sk:ℕhn:s "n" = k + 1h1:(!s "n" == 0) = false⊢ k + 1 = 0 All goals completed! 🐙 n₀:ℕI:Assertion := fun s => s "r" * (s "n").factorial = n₀.factorials:StatehbF:s "n" = 0hI:s "r" = n₀.factorial⊢ ("r" .==. n₀.factorial) s All goals completed! 🐙 end Ch19

Note the division of labor in this example. Applying the Hoare rules is completely mechanical, guided by the syntax of the program; the only creative step is the choice of the invariant. All the remaining effort lies in the logical proof obligations, which no longer mention the program. This observation motivates the next chapter, in which we will automate the mechanical part by means of a verification condition generator.

3.3.6. Exercises

1. Prove the semantic version of the conjunction rule:
     {{* P *}} c {{* Q₁ *}}  →  {{* P *}} c {{* Q₂ *}}
       →  {{* P *}} c {{* Q₁ .&. Q₂ *}}.
   Discuss why proving the syntactic version (with HoareTriple)
   directly, by induction, is harder.

2. Prove that {{ fun _ => True }} .whl .tt .skip {{ fun _ => False }}.
   Why does this not contradict the soundness theorem?

3. Verify the program
     r := 0; i := 0;
     while (i ≠ n) do (i := i + 1; r := r + i)
   with precondition n = n₀ and postcondition 2 * r = n₀ * (n₀ + 1).
   (Hint: the invariant should relate r and i and state i ≤ n.)

4. Verify the program that computes multiplication by repeated
   addition:
     r := 0; i := y;
     while (i ≠ 0) do (r := r + x; i := i - 1)
   with postcondition r = x * y (under the precondition x = x₀ ∧ y = y₀).

5. Define the "forward" assignment rule
     {P} x := a {∃ v, P[v/x] ∧ x = a[v/x]}
   in the shallow embedding and prove that it is valid.

6. Prove directly, without using hoare_sound, that the consequence
   rule is valid for semantic triples: if {{* P *}} c {{* Q *}},
   P' implies P and Q implies Q', then {{* P' *}} c {{* Q' *}}.