17-Correctness of Iter
17-Correctness of Iter
2
Program Correctness
Usually, we convince ourselves that our
programs are correct (debug, test, …)
condition)
3
Program Correctness: what does it take?
Proving that a program is correct requires:
Finding appropriate propositions
Using appropriate inference rules
4
Program Correctness: Relevant Inf. Rules
What are the constructs that are used in a
program
Assignment
Composition: statements in sequence
Loops: while
5
Partial Correctness Definition
Let S denote a “segment of code” to which we “input” some
values, and S “outputs” a set of values
We write p{S}q
Turing Award Winner
1. Partial Correctness
p{S}q
2. That S terminates, i.e.,
Ø condition, after a finite number of iterations
7
Easy Example
Let S be:
y := 2
z := x +
y
So, now we can let p and q be:
p: x is an integer
q: z is x + 2
However, assume q’ : “z is x + 5”
Then p{S}q’ would be false
8 What about q: z > x?; q: z < x ∨ z > x? q: true?
Not as Easy Example: Factorial
Let S be:
Procedure Factorial(n:
integer)
i := 1
f := 1
while i < n
begin
i := i +1
f := f * I
end
return f
For the factorial procedure to be correct, we
should have:
p: n ≥ 1
q: f = n!
9
Inference Rule 1 - Composition
Let S be composed of sub-statements
S = {S1 ; S2}
\ p { S 1 ; S 2} r
¾¾¾¾¾¾
We can conclude:
And therefore: \ p{ S } r
S1: x := n+5
S2: n := n-10
q{S2}r is true
p: n is an integer
q: x = n+5
\ p{S1 ; S2}r
¾¾¾¾¾¾
r: x = n + 15
\ p{ S } r
11
Understanding how we got r: x = n +
15
p: n is an integer Procedure pro1(n:
integer)
S1: x := n+5
S1 : x := n + 5
S2: n := n - 10
Then n is x – 5 (q: x = n +5)
S2 : n := n – 10
Then n is x – 5 – 10
Thus n is x – 15
and therefore r: x = n + 15
12
Inference Rule 2.1 - Simple if
Let S: if condition then S1
Given that:
(p Ù condition){S1} q
(p Ù Øcondition) ® q
13
Example – Simple if
Consider the following program segment S:
if x >y
We can set then
p: x is an integer y := x
condition: x >y
S1: y := x
q: y ³ x
Now:
(p Ù condition) {S1}: ((x is an integer ) Ù (x >y)) {y := x } q: y ³ x
14
Inference Rule 2.2- if then else
Let S : if condition then S1 else S2
Given that:
(p Ù condition){S1}q
(p Ù Øcondition){S2}q
And therefore:
\ p{ S } q
15
Example – if then else
Consider the following program segment S:
a := -x
condition: x<0
Else
S : a := -x
1 a := x
S : a := x
2
q: a = |x|
Now:
=|x|
1
16
Inference Rules 3 - while
Let S : while condition S1
Inference Rule:
(v Ù condition){S1}v i.e., v is a loop invariant
17
Finding Loop Invariants
We have p{while condition S1}q, we need to
find v such that:
pv
(v Ù condition) {S1} v
(v Ù Øcondition) q
Maintenance
If the loop invariant holds for the ith iteration, then it must hold after
the i+1th iteration;
i.e.,: (v Ù condition){S1} v
Termination
when the loop terminates, the loop invariant would still hold
i.e.,: (v Ù Øcondition) q
p : n ³ 1 integer i := 1
q : f = n! f := 1
while i < n
Loop Invariant: f := f * i
basically, after each end
iteration, f = i! return f
v : (f = i! ) ∧ ( i ≤ n )
20
Factorial – Using Hoare Logic
v : (f = i! ) ∧ ( i ≤ n ) Procedure factorial(n:
integer)
{n ³ 1 }
i := 1
Initialization: f := 1
therefore v holds
while i < n
Maintenance:
begin
{v: f=i! Ù (i <n)}
Termination:
£n)}
{v: f=i! Ù (i £n)}
Proof Completed!
{v: f=i! Ù (i £n) Ù (i ≥ n)}
{f=i! Ù (i=n)}
{f=n!}
21
return f
Algorithm: Multiply
Consider the Procedure multiply (m, n:
following code
integers)
if n < 0 then a := -n
p: m,n integers else a := n
k := 0
q: x = mn
x := 0
We need to show p{S}q
while k < a
begin
x := x + m
k := k+ 1
Loop Invariant: end
basically, after each
if n < 0 then x := -x
iteration, x = mk
else x := x
v
return x
: (x = mk) ∧ (k ≤
a)
22
Multiply – Using Hoare Logic
Procedure multiply (m, n: integers)
if n < 0 then a := -n
{m, n are integers}
else a := n
k := 0
{a = |n|} – proven before
x := 0
Initializatio
while k < a n
{a = |n| ∧ k = 0 ∧ x = 0} {a = |n| ∧ k = 0 ∧ v: x = km ∧ k ≤ a}
Maintenance at start
begin
x := x + m
{a = |n| ∧ v: x = km ∧ k < a}
k := k+ 1
Maintenance at
{a = |n| ∧ x = km + m ∧ k < a} {a = |n| ∧ x = (k+1)m ∧ k < a}
end
{a = |n| ∧ v: x = km ∧ k ≤ a}
end
n
else x := x
{n < 0 ∧ a = |n| ∧ x = -am} {n = -a ∧ x = nm}
return x {x = nm}
{n ≥ 0 ∧ a = |n| ∧ x = am} {n = a ∧ x = nm}
23
Algorithm: Array Product
Write an algorithm, which given a sequence of
integers, returns the product of all elements in
the list
Procedure array_product(a1, a2, … ,an:
integers)
product := 1;
for i := 1 to n
product = product * ai ;
return product
Loop invariant:
P(i) = “Before the ith iteration, product is equal to the
is the case
Maintenance: Assume P(i) and show that P(i+1)
holds
Termination:
After n iterations, the loop will terminate and P(n) would
hold by induction
25 *Credits to Fatima Abu Salem, American University of Beirut
Array Product – Using Hoare Logic
Procedure array_product(a1, a2, … ,an : integers)
{n ≥ 0 ∧ a1, a2, …, an are integers}
product := 1;
i := 1;
{product = 1 ∧ i = 1}
{v: (product = a1, a2, …, ai-1∧ i ≤ n+1) ∧ i = 1}
while i ≤ n
{v: (product = a1, a2, …, ai-1 ∧ i ≤ n)}
product = product*ai ;
{product = a1, a2, …, ai∧ i ≤ n}
i := i+1
{v: (product = a1, a2, …, ai-1 ∧ i ≤ n + 1)}
27
Any Questions?
28