Unit 11 Program Correctness
Unit 11 Program Correctness
1
Objectives
• Define program correctness
• Identify the techniques used to prove program correctness
• Analyse the rules of Inference and Program Correctness
2
Introduction to Program Correctness
• A correct program is a program that always produces correct
output
• It is easy to prove that a program is incorrect:
• test program with sample input
• if it produces incorrect results, it is incorrect
• The opposite is not so simple, however:
• can’t prove correctness by testing unless all possible inputs can be tested
3
Proving Program Correctness
• Also known as program verification
• How can we be sure that a program/algorithm always produces the
correct result?
• Test it on sample input
• Test boundary conditions
• Test it on all possible inputs
• Prove it correct
• can we automate this?
• Difficult, perhaps impossible to automate the process
• Use rules of inference, mathematical induction
4
Proving Program Correctness
• Two major steps:
• Prove partial correctness: that is, prove that the program produces correct
output if it terminates
• Prove that the program always terminates
• Two propositions involved:
• initial assertion: gives properties input values must have
• final assertion: gives properties output should have, if program performed
correctly
5
Proving Program Correctness
• A program or program segment S is said to be partially correct with respect
to initial assertion p and final assertion q if:
• whenever p is true for the input values of S and S terminates,
• q is true for output values of S
• The notation p{S}q indicates such partial correctness
• p{S}q is called a Hoare triple
Note:
partial correctness only states that the program produces the correct results if
it terminates.
It does not prove that the program terminates
partial correctness is only proven within the context of the initial and final
assertions
6
Example 1
•Program segment S is as follows
• y=2;
• z = x + y;
• Initial assertion
• p: x = 1
• Final assertion
• q: z = 3
• Prove p{S}q
• assume p
• x initially has the value 1
• y is assigned the value 2
• z is then assigned the value x + y
• that is equal to 1 + 2 which is 3
• Therefore S is correct with respect to p and q
7
Rules of Inference and Program Correctness
• Several rules of inference are useful in proving program correctness
• The first of these is called the composition rule, which in essence states
that a program is correct if each of its subprograms is correct
8
Composition Rule
We can split our program into parts (subprograms) and prove that each of these parts (subprograms) is
correct
• It follows that
• if p is true and S1 executes and terminates then q is true
• if q is true and S2 executes and terminates then r is true
• Therefore if p is true and S executes and terminates r is true
9
Composition Rule Cont.
• Stated mathematically, the composition rule is:
p{S1}q
q{S2}r
---------------
p{S1;S2}r
10
Conditional Statements
• Conditional statements are program segments of the form: if condition
then S (where S is a block of statements)
• When condition is true, S is executed
• When condition is untrue, S is not executed
11
Conditional Statements Cont.
•Assume program segment is as follows
• if cond then S
• Show that
• when p is true, and cond is true and S executes, q is true
• when p is true and cond is false and S does not execute, q is true
12
Rule of Inference for Conditional Statements
(p condition){S}q
(p condition) q
-------------------------------------
p{if condition then S}q
13
Conditional Statements : Example 1
• Program segment S is as follows
• if x > y then
•x = y
• Initial assertion
• p: is True
• Final assertion
• q: y x (y is greater than or equal to x)
15
Verifying more complex conditionals
• To verify segment correct with respect to initial assertion p and final
assertion q:
• show that when p is true and condition is true, q is true after S1 terminates;
• show that when p is true and condition is false, q is true after S2 terminates
16
Rule of Inference
(p condition){S1}q
(p condition){S2}q
----------------------------------------------
p{if condition then S1 else S2}q
17
Complex Conditionals: Example
•Program segment S is as follows
• Initial assertion
• p: is True
• Final assertion
• q: abs = |x|
• Consider the cases when cond = true and when cond = false
18
Loops and loop invariants
• A loop is a statement of the form:
• while condition S
• where S is executed repeatedly until condition becomes false
• A loop invariant is an assertion that remains true each time S is
executed
19
Loops and loop invariants cont.
• Assume program segment is as follows
• while cond do S
• Show that
• p is true before S is executed
• p is true and cond is false on termination of the loop
• if it terminates
20
Rule of Inference for Loops
(p condition){S}p
----------------------------------------------
p{while condition S}(condition p)
21
Loops and loop invariants: Example
i := 1;
• Prove segment terminates with fact = n! fact = 1;
• a loop invariant is required while i < n
do begin
• let p be proposition p: fact = i! and i <= n i = i + 1;
• let S be the segment: i = i+1; fact = fact * i; fact = fact * i;
end
• Prove that p is a loop invariant, using mathematical induction
• Basis Step: initially i = fact = 1 = i! and 1 <= n
• Inductive Step
• assume p is true and 1 < i < n and fact = i!
• after executing loop
• i was incremented by 1, i.e. i + 1
• therefore i n
• fact = i!(i + 1)
• therefore fact = (i+1)! … and i has been incremented
• Therefore p is a loop invariant
22
Loops and loop invariants: Example cont.
•Therefore p is a loop invariant
23
Proving correctness of entire programs
• Split the program into segments: the rule of composition can be used to
build the correctness proof
• Prove the correctness of each segment: for example, if there are 4
segments, prove p{S1}q, q{S2}r, r{S3}s, and s{S4}t
• If all are true, then p{S}t is true
• If all 4 segments terminate, then S terminates, and correctness is proven
24
Exercise
• Prove the correctness of the following
1. if (x < y)
min = x;
else
min = y;
2. if (x > y)
{
x = x + y;
y = x - y;
x = x - y;
}
25