Verification Mofdel
Verification Mofdel
Verification Mofdel
HOARE VERIFICATION
Albert Nymeyer
Sir Charles Antony Richard Hoare
2
! Hoare:
! discovered Hoare Logic (in 1969), quicksort, CSP, occam, …
! Hoare Logic is an inference system
! usedto prove properties of programs.
! based on proof rules, called Hoare triples:
{P} Q {R}
where
" P is a precondition
" Q is a program or a program statement
" R post-condition.
Change of state
4
! All are true, but the most useful triple is the last one because it
contains the weakest precondition
! using it allows the code to be used in the most general way
! x>0 → x≥0
! false → true … true is the weakest possible predicate, false the strongest
! P→Q means
" If x can lift 100kg then x can lift 10kg
" If x cannot lift 100kg, nothing happens
Example
! What is the wp in {wp} if x>0 then y:=z else y:=-z {y>5}
wp(if x>0 then y:=z else y:=-z; y>5)
(x>0∧wp(y:=z; y>5)) ∨ (x≤0∧wp(y:=-z; y>5))
wp is (x>0 ∧ z>5) ∨ (x≤0 ∧ z<-5)
(which you could have worked out by looking at the code)
For loops, it’s more complicated
15
Loops
! Want to prove the triple {P} while B do S {Q}
" where B is the loop condition and S is a sequence of statements
! We used ‘weakest precondition’ rules to calculate
" a single Hoare triple for a single statement
! 1 = 12
! 1+3 = 22
! 1+3+5=32
! 1+3+5+7=42
! ...
We’ll write a program that sums odd numbers, and then verify its correctness.
A program that sums odds
18
input n≥0
s:=0;
i:=0; the initialization statements must establish the invariant
while (i≠n)
we need to find an invariant
{
i:=i+1;
s:=s+(2i-1); the loop assignments must maintain the invariant
}
on conclusion, the invariant should produce the result s=n2
Sums odds: maintain the invariant
19
P Q1 R1 R1 Q2 R
{x=1} y := x {y=1} {y=1} z:=y {z=1}
{x=1} y:=x; z:=y {z=1}
P Q1 ; Q2 R
D3: Iteration
29
T T T T T
35
Examples:
! 9÷2 = 4*2 +1
! 11÷1 = 11*1+0
! 19÷5 = 3*5 +4
Code is:
// {P}
r := x;
q := 0;
while (y<=r)
{
r := r – y;
q := 1 + q;
}
// {Q}
What we actually want is: prove the triple {P} code {Q}, where
! P is (x≥0) ∧ (y>0)
! Q is (x=q*y+r) ∧ (y>r) ∧ (r≥0) ∧ (y>0)
! code is above
We let the invariant I be x=q*y+r ∧ (r≥0) ∧ (y>0)
Pre-, post-conditions, invariant for quotient
40
We’ll repeat the verification here, but show the inference rules this time
1. true → x=x+y*0 Lemma 1, see later
2. {x=x+y*0} r := x {x=r+y*0} D0
3. {x=r+y*0} q := 0 {x=r+y*q} D0
4. {true} r := x {x=r+y*0} D1, 1,2
5. {true} r := x; q :=0 {x=r+y*q} D2, 4,3
6. (x=r+y*q) ∧ (y≤r) → (x=(r-y) + y*(1+q)) Lemma 2, see later
7. {x=(r-y)+y*(1+q)} r := r-y {x=r+y*(1+q)} D0
8. {x=r+y*(1+q)} q := 1+q {x=r+y*q} D0
9. {x=(r-y)+y*(1+q)} r := r-y; q := 1+q {x=r+y*q} D2, 7,8
Repeat the verification continued
43
To prove x = x+y*0
we use the axioms from arithmetic:
! A0: x = x
! A1: x*0 = 0
! A2: x+0 = x
Proof
1. x=x by A0
2. y*0 = 0 by A1
3. x = x+y*0 by 1, 2 and A2
Lemma 2 (we needed this in the verification)
45
k := 1; S1
s := 0;
Σ
{0≤k≤n+1 ∧ s= 0..k-1} {I} the invariant
while (k<=n) { B
s := s+k; S2
k := k+1;
}
Σ
{s= 0..n} {Q} the post-condition: it’s what we want
Verifying the program
48
I’ve been lazy here: I should have computed the state after EACH
assignment statement using rule D0, and then used rule D2 to
combine them
Does the program maintain the invariant?
50
Does I∧¬B → Q?
(0≤k≤n+1 ∧ s=Σ0..k-1) ∧ ¬k≤n → s=Σ0..n
Σ
Antecedent (ie. the LHS) says k=n+1. Substituting into s= 0..k-1 generates
s = Σ0..n+1-1 → s = Σ0..n
Hence true.
1 ∀xP(x) Premise
2 ∀xS(x) Premise
3 P(a) UI (1)
4 S(a) UI (2)
5 P(a) ∧ S(a) Conjunction (3,4)
6 ∃x(P(x) ∧ S(x)) EG (5)
6. Q&A inference proofs (using contradiction)
57