Professional Documents
Culture Documents
Proving Program Correctness: The Axiomatic Approach
Proving Program Correctness: The Axiomatic Approach
Proving Program Correctness: The Axiomatic Approach
• Correctness:
– partial correctness + termination
• Partial correctness:
– Program implements its specification
Proving Partial Correctness
• Rule:
{P}
{P Bif } S {Q} , {P Bif } {Q}
{P} if Bif then S {Q} Bif
{P Bif } {P Bif}
{Q}
Conditional Stmt 1
• Rule
{P}
{P Bif } S1 {Q} , {P Bif } S2 {Q}
{P} if Bif then S1 else S2 {Q} Bif
{P Bif } {P Bif}
S1 S2
{Q}
Conditional Stmt 2 Axiom
• Example:
Then part:
1. = if|x|}
{x x y< =0 xthen
{y ={|x|}
{-x
2. = |x|} x =x =-x;-xy{x= =x|x|}
(3.?? else
x <0) -x = |x|
• Else
4. part: y = x
{x =|x|} y=x{y=|x|}
5. }
( ?? ¬(x < 0)) x = |x|
{y = |x|}
• P (-x = |x|) (x=|x|)
While Loop Axiom
• Rule
{P B} S {P} {P}
{P} while B do S {P B}
Bif
• Infinite number of paths, so we need one
predicate for that captures the effect of S
• P is called an invariant
S
{P B}
While Loop Axiom
• Example • INV y + ab = AB b 0
IN {B 0} • Bw b > 0
– a=A
• Show INV ¬ Bw OUT
– b=B
y + ab = AB b 0 ¬(b > 0)
– y=0
y + ab = AB b = 0
– while b > 0 do {
y = AB
– y=y+a
So {INV ¬ Bw} OUT
– b=b-1
– } • Establish IN INV
OUT {y = AB} {ab = AB b 0} y=0 { INV}
{aB = AB B 0} b = B {….}
{AB = AB B 0} a = A {….}
So {IN} a=A;b=B;y=0 {INV}
While Loop Axiom
• Need to show
{INV Bw} loop body {INV}
{y+a(b-1) = AB b-1 0} b = b - 1 {INV}
{y+a+a(b-1) = AB b-1 0} y = y+a {….}
{y +ab = AB b-1 0} loop body {INV}
• y + ab = AB b 0 b > 0 {y +ab = AB b-1 0},
• So
– {IN} lines 1-3} {INV},
– {INV} while loop {INV ¬ Bw }, and
– {INV ¬ Bw} OUT
• Therefore
– {IN} program {OUT}
Total correctness