CSC510 20242 - Chapter 5 - Program Correctness - Rai
CSC510 20242 - Chapter 5 - Program Correctness - Rai
CSC510 20242 - Chapter 5 - Program Correctness - Rai
Chapter 5
PROGRAM CORRECTNESS
1
Overview
• Program testing
• Concepts for Correctness proofs
• Hoare triple
• Strong and weak Assertions
• Program correctness proof
1. Assignment Statement/Rule
2. Concatenation
• Two Steps to Correctness Proof
3. Conditional Rule: if-then and if-then-else
4. Loop Rule: while-do
5. Loop invariant
2
PROGRAM TESTING
• It is impossible to get an error free program on the first try.
• If a program is free from any syntax error, we can test the program
with sample input.
PRE-CONDITION POST-CONDITION
- The condition that the - The final state that is
initial state must satisfy acceptable as correct
such that the program code results.
can accomplish its
objective.
4
EXAMPLE
Show that the program segment (Code) : Another example:
y := 2
{x = 1} y = 2; z = x + y {z = 3}
z := x + y
is correct with respect to the initial assertion - Precondition, P: x = 1
P: x = 1 and the final assertion Q: z = 3. - Postcondition, Q: z = 3
Prove {P} C {Q}.
- Code, C: y = 2; z = x + y
Solution:
P C Q
5
EXAMPLE
Show that the program segment (Code) : Another example:
y := 2
{x = 1} y = 2; z = x + y {z = 3}
z := x + y
is correct with respect to the initial assertion - Precondition, P: x = 1
P: x = 1 and the final assertion Q: z = 3. - Postcondition, Q: z = 3
Prove {P} C {Q}.
- Code, C: y = 2; z = x + y
Solution:
P C Q
x=1 y=2 x = 1, y = 2
//if x = 1 in the program’s //code
input state
x = 1, y = 2 z=1+2 z=3
z=3 //then after running y := 2 and
z := x + y, z will be 1 + 2 = 3.
6
Hoare triple
Axiomatic Semantics invented by Hoare is a tool/language to
specify program behaviour and proving correctness (based on
mathematical logic).
7
HOARE TRIPLE
8
Empty assertion { } is the ideal pre-condition – means true for all cases. 9
Strong & Weak Assertions
10
STRONG & WEAK ASSERTIONS
Stronger Weaker
More specific More general
x=2 vs x>2
x>2 vs x >= 2
11
STRONG & WEAK ASSERTIONS
12
FINDING WEAKEST PRE-CONDITION
13
FINDING STRONGEST POST-CONDITION
14
PROGRAM CORRECTNESS PROOF – AXIOMATIC SEMANTICS
(HOARE TRIPLE)
1. Assignment Statement/Rule
2. Concatenation Rule
3. Conditional Rule
a) if-then
b) if-then-else
4. Loop Rule
a) while-do
15
16
ASSIGNMENT RULE
19
CONCATENATION RULE:
Example 1.1: FIND THE POST-CONDITION FOR THE FOLLOWING STATEMENT
{x = 10 ∧ y = 25} x := y; y := x {?}
P START HERE C Q
PLEASE NOTE:
Therefore, the post-condition is . 1 mark for stating the conclusion. 20
CONCATENATION RULE:
Example 1.1: FIND THE POST-CONDITION FOR THE FOLLOWING STATEMENT
{x = 10 ∧ y = 25} x := y; y := x {?}
P START HERE C Q
x = 10 y = 25 x =y x = 25 y = 25
//Substitute y=25 in P into x=y. The new value of x is 25.
x = 25 //The post-condition (R) for C1 is x = 25 y = 25.
x = 25 y = 25 y=x x = 25 y = 25
//Move the final state of the 1st statement, R to //Substitute x=25 in P into y=x. The new value of y is 25.
the initial state of 2nd statement. y = 25 //Therefore the post-condition (Q) is x = 25 y = 25.
P START HERE C Q
PLEASE NOTE:
Therefore, the post-condition is . 1 mark for stating the conclusion. 22
CONCATENATION RULE:
Example 1.2: FIND THE POST-CONDITION FOR THE FOLLOWING STATEMENT
P START HERE C Q
x > 10 y > 25 x =׃y x > 25 y > 25
Let x=10, and y =25 //C1 //Substitute y=25 in P into x=y. The new value of x is 25.
//The post-condition (R) for C1 is x > 25 y > 25.
P START HERE C Q
PLEASE NOTE:
Therefore, the post-condition is . 1 mark for stating the conclusion. 24
CONCATENATION RULE:
Example 1.3: FIND THE POST-CONDITION FOR THE FOLLOWING STATEMENT
P START HERE C Q
x >= 10 y >= 25 x =׃x+2 x >= 12 y >= 25
Let x=10, and y =25 x = 10 + 2 //Substitute x=10 in P into x+2. The new value of x is 12.
//The post-condition (R) for C1 is x => 12 y >= 25.
//C1
PLEASE NOTE:
Therefore, the post-condition is x >= 12 y >= 22. 1 mark for stating the conclusion. 25
CONCATENATION RULE:
Example 1.4: FIND THE PRE-CONDITION FOR THE FOLLOWING STATEMENT
P C Q
START HERE
PLEASE NOTE:
Therefore, the pre-condition is . 1 mark for stating the conclusion. 26
CONCATENATION RULE:
Example 1.4: FIND THE PRE-CONDITION FOR THE FOLLOWING STATEMENT
P C Q
x >= 12 y >= 25 x =׃x + 2 x >= 12 y >= 25
x + 2 >= 12 y >= 25 //C1
x >= 10 y >= 25
x >= 12 y >= 22 y = y - 3 x >= 12 y >= 22
x >= 12 y – 3 >= 22 //C2 START HERE
x >= 12 y >= 25
PLEASE NOTE:
Therefore, the pre-condition is x >= 10 y >= 25. 1 mark for stating the conclusion. 27
Summary
{?} x = y + 1; { x < 10 }
P C Q P C Q
START HERE START HERE
28
Summary
{?} x = y + 1; { x < 10 }
P C Q P C Q
START HERE START HERE
x < 10
x=y+1
y + 1 < 10 y<9
x=y+1 x < 10 x=9+1 y < 9 x < 10
y < 10 – 1 Let y = 9
x = 10
y<9
29
PROVE IF THE FOLLOWING PIECES
OF CODE ARE CORRECT
30
PROVE IF THE FOLLOWING PIECES OF CODE ARE CORRECT
Find the post-condition
EXAMPLE 1.1:
{x = 1} x := x + y; x := x + y * y {x = 1 + y + y2}
SOLUTION:
P START HERE C Q
PLEASE NOTE:
Since the last statement produces the result of x = 1 + y 1 mark for stating ‘the statement is correct/incorrect’,
+ y2 which is equals to the given post-condition, hence, 1 mark for justification/explanation.
the statement is correct. 31
PROVE IF THE FOLLOWING PIECES OF CODE ARE CORRECT
Find the post-condition
EXAMPLE 1.1:
{x = 1} x := x + y; x := x + y * y {x = 1 + y + y2}
SOLUTION:
START HERE
P C Q
x=1 x := x + y x=1+y
x=1+y x := x + y * y x = (1 + y) + y * y
x = 1 + y + y2
PLEASE NOTE:
Since the last statement produces the result of x = 1 + y 1 mark for stating ‘the statement is correct/incorrect’,
+ y2 which is equals to the given post-condition, hence, 1 mark for justification/explanation.
the statement is correct. 32
PROVE IF THE FOLLOWING PIECES OF CODE ARE CORRECT
Find the pre-condition
EXAMPLE 1.2:
{x = 1} x := x + y; x := x + y * y {x = 1 + y + y2}
P C Q
START HERE
PLEASE NOTE:
Since the initial state produces the result of x = 1 which 1 mark for stating ‘the statement is correct/incorrect’,
is equals to the given pre-condition, hence, the 1 mark for justification/explanation.
statement is correct. 33
PROVE IF THE FOLLOWING PIECES OF CODE ARE CORRECT
Find the pre-condition
EXAMPLE 1.2:
{x = 1} x := x + y; x := x + y * y {x = 1 + y + y2}
P C Q
START HERE
PLEASE NOTE:
Since the initial state produces the result of x = 1 which 1 mark for stating ‘the statement is correct/incorrect’,
is equals to the given pre-condition, hence, the 1 mark for justification/explanation.
statement is correct. 34
PROVE IF THE FOLLOWING PIECES OF CODE ARE CORRECT
EXAMPLE 3:
START HERE
P C Q
PLEASE NOTE:
1 mark for stating the conclusion.
Since P and Q are same, therefore the invariant is proven true. 35
PROVE IF THE FOLLOWING PIECES OF CODE ARE CORRECT
EXAMPLE 3:
START HERE
P C Q
x = 2y y=y+1 x = 2(y + 1)
x = 2(y + 1) x = x*2 2x = 2(y + 1)
//Move all expression to RHS except x
x = 2(y + 1) / 21
x = 2(y + 1) - 1
x = 2y PLEASE NOTE:
1 mark for stating the conclusion.
Since P and Q are same, therefore the invariant is proven true. 36
PROVE IF THE FOLLOWING PIECES OF CODE ARE CORRECT
EXAMPLE 3:
P C Q
x*2 = 2y y=y+1 x*2 = 2y
2x = 2y
x = 2(y + 1) / 21
x = 2(y + 1) - 1
x = 2y
x = 2y x = x*2 x = 2y
START HERE PLEASE NOTE:
x*2 = 2y 1 mark for stating the conclusion.
Since P and Q are same, therefore the invariant is proven true. 37
Two Steps to Correctness Proof
38
39
Goal: Prove {P} C {Q}
1. Find wp(C, Q) NOTE:
If the P is weaker than the
wp P, then the code
2. Prove that P → wp(C, Q) segment is incorrect.
Code P vs wp P
segment
CORRECT Specific/ General/
Stronger Weaker
INCORRECT General/ Specific/
Weaker Stronger
40
Two steps to Correctness proof
NOTE:
• Example 1: {P} C {Q} If the P is weaker than the
• Find the weakest pre-condition. wp P, then the code
segment is incorrect.
• {y = 3} x := y + 1 {2*x + y <= 11}
Code P vs wp P
//P segment
START HERE
CORRECT Specific/ General/
P C Q Stronger Weaker
INCORRECT General/ Specific/
Weaker Stronger
P vs wp P
y=3 y <= 3
Specific/
Stronger
General/
Weaker
So, code segment is CORRECT
Conclusion: P must be as weak as possible. Since y = 3 is stronger than y <= 3, so the Proven P is correct. 41
Two steps to Correctness proof
NOTE:
• Example 1: {P} C {Q} If the P is weaker than the
• Find the weakest pre-condition. wp P, then the code
segment is incorrect.
• {y = 3} x := y + 1 {2*x + y <= 11}
Code P vs wp P
//P segment
START HERE
CORRECT Specific/ General/
P C Q Stronger Weaker
y <= 3 Specific/
Stronger
//wp P General/
Weaker
So, code segment is CORRECT
Conclusion: P must be as weak as possible. Since y = 3 is stronger than y <= 3, so the Proven P is correct. 42
CONDITIONAL RULE
D is condition
¬D is the negation of the condition 43
EXAMPLE 1: if-then-else clause
44
EXAMPLE START
1: HERE
if-then-else clause
Two parts P D C Q
If m≠1 m<0 m = -m m >= 0
//D //C1
NOTE:
If only one part is
correct, then the
code is incorrect.
Conclusion: Since both parts are correct, therefore the code is correct. 45
EXAMPLE
START HERE
1: if-then-else clause
Two parts P D C Q Analysis
If m≠1 m<0 m = -m m >= 0 Existing pre-condition {P ꓥ D}: m < 0
//D //C1
//Substitute m >= 0 in Q with m = -m
-m >= 0
New condition: m <= 0
Conclusion: Since both parts are correct, therefore the code is correct. 46
CONDITIONAL RULE
D is condition
¬D is the negation of the condition 47
EXAMPLE 2: if-then clause
NOTE:
49
If only one part is correct, then the code segment is incorrect.
EXAMPLE
START HERE
2: if-then clause
Two P D C Q Analysis
parts
If { } x<0 x = -x x >= 0 Existing pre-condition {P ꓥ D}: x < 0
//D //C1
//Substitute x >= 0 in Q with x = -x
-x >= 0
New condition: x <= 0
NOTE:
50
If only one part is correct, then the code segment is incorrect.
Example 3.1: IF ELSE How to test the code?
Refer to the next slide…
Two P D C Q
parts
If n>1 16n > 16 m = n*16; m > 16
//The weakest pre-condition, n>1
W1
for If block
Therefore, the weakest pre-condition is n > 4 (because the n > 4 is more specific than n > 1).
51
Example 3.1: IF ELSE How to test the code?
Refer to the next slide…
Two P D C Q
parts
If n>1 m > 16 m = n*16; m > 16
//The weakest pre-condition, n*16 > 16
W1
for If block 16n > 16
n>1
Therefore, the weakest pre-condition is n > 4 (because the n > 4 is more specific than n > 1).
52
Example 3.1: IF ELSE TEST THE CODE
• Based on previous example: Two parts Test code using WP n > 1 Test code using WP n > 4
• You’ve identified TWO weakest pre-conditions (WP): to each code in if and else block to each code in if and else block
1) WP for if block: n > 1 Test value n=2 n=5
2) WP for else block: n > 4 //Choose a value greater than 1 //Choose a value greater than 4
• Since both the if clause and the else clause must If //Substitute n = 2 into n //Substitute n = 5 into n
be satisfied, we need to take the stronger (more Code m = n * 16 m = n * 16
restrictive/specific) of the two pre-conditions. = 2 * 16 = 5 * 16
= 32 = 80
• Therefore, the weakest pre-condition that can
Else //Substitute n = 2 into n //Substitute n = 5 into n
guarantee the post-condition is n > 4 (because
the n > 4 is more specific than n > 1). Code m=n*n m=n*n
=2*2 =5*5
=4 = 25
Explanation Based on the results of else code, the Based on the results of if and else
value of m is less than 16. code, both values of m are greater
Therefore, the weakest pre- than 16.
condition, n > 1 cannot guarantee Therefore, the weakest pre-
the post-condition. condition, n > 4 can guarantee the
post-condition.
53
Example 3.2: IF ELSE
• Identify the strongest post-condition for the following decision
segment that can guarantee the pre-condition to be true.
Therefore, the strongest post-condition is x > 0 (because the x > 0 is more general than x = 1).
54
Example 3.3: IF ELSE POSTCONDITION
START HERE
Two parts P D C Q
If a >= 6 n < a a = 2 – (a/2)
55
Example 3.3: IF ELSE POSTCONDITION
START HERE
Two parts P D C Q
If a >= 6 n < a a = 2 – (a/2) a >= -1, Q1
Let a = 6
a = 2 – (a/2)
= 2 – (6/2)
= -1
Else a >= 6 n >= a a = 4 – a a >= -2, Q2
//¬D
Let a = 6
a = 4 – a
= 4 – 6
= -2
56
Therefore, the strongest post-condition is x >= -2 (because the x >= -2 is more general than x >= -1).
Example 3.4: IF ELSE PRECONDITION
WEAKEST PRECONDITION
START HERE
Two parts P D C Q
If a >= -1, Q1
57
Example 3.4: IF ELSE PRECONDITION
START HERE
Two parts P D C Q
If a >= 6 n < a a = 2 – (a/2) a >= -1, Q1
a >= -1
2 – (a/2) >= -1
(a/2) >= -1-2
-(a/2) >= -3
-a >= -6
a >= 6
59
FINDING LOOP INVARIANT
• Loop invariant is the formalisation of the programmer’s
objective.
• The loop invariant typically contains all the code variables that
are changed from iteration to iteration in the loop.
{?}
x = 0; z = 0;
Stop
while (z ≠ n) when
{ z==n
x = x + y;
z = z + 1;
}
{?}
61
LOOP INVARIANT – Example 1
How to find the loop invariant?
Measures the progress towards satisfying the exit condition. Often deal with summation.
63
LOOP INVARIANT – Example 1
How to find the loop invariant?
Measures the progress towards satisfying the exit condition. Often deal with summation.
{ x = z*y }
x = 0; z = 0;
Stop
while (z ≠ n) when
{ z==n
x = x + y;
z = z + 1;
}
{ x = z*y }
65
LOOP INVARIANT – Example 2
Prove/Show that x = z*y is invariant in the following loop segment.
P C Q
START HERE
Conclusion: Since both P and Q are same, therefore the invariant is proven. 66
LOOP INVARIANT – Example 2
Prove/Show that x = z*y is invariant in the following loop segment.
P C Q
x + y = yz + y x = x+y x = yz + y
x = yz + y - y
x = y*z
x = z*y
x = (z + 1)*y z=z+1 x = z*y
x = yz + y Start here
Conclusion: Since both P and Q are same, therefore the invariant is proven.
67
LOOP INVARIANT – Example 3 P = Pre-condition
I = Invariant loop
• Identify the strongest post-condition for the loop segment in previous ¬D = When the condition is false
exercise if the precondition is given as { c > 5 ꓥ d > 10 }.
Then
Let a = d Conclusion:
Therefore, the strongest post-condition, Q is b > 105.
Note: After the loop has been terminated, the
possible correct result(s) should be greater than 105. 69
Overview
• Program testing and verification
• Concepts for Correctness proofs
• Hoare triple
• Strong and weak Assertions
• Weakest Precondition and Strongest Postcondition
• Conjunction and Disjunction Rules
• Axiomatic semantics
• Program correctness proof
• Assignment Statement/Rule
• Concatenation
• Conditional Rule: if-then and if-then-else
• Loop Rule: while-do
• Loop invariant
70