Discrete Structures Winter 2016: Wonsook Lee Wslee@Uottawa - Ca Eecs, Univ. of Ottawa
Discrete Structures Winter 2016: Wonsook Lee Wslee@Uottawa - Ca Eecs, Univ. of Ottawa
DISCRETE STRUCTURES
Winter 2016
WonSook Lee
[email protected]
EECS, Univ. of Ottawa
The lecture note is collaborative work, and
large contribution is from Prof. Nejib Zaguia
3/29/18 1
Program
Correctness/Verification
We want to be able to prove that a given program
meets the intended specifications.
This can often be done manually, or even by
automated program verification tools.
• Prove p{S}q.
Proof: If x=1 in the program’s input state, then
after running y:=2 and z:=x+y, z will be 1+2=3.
5
Hoare Triple Inference
Rules
Deduction rules for Hoare Triple statements.
A simple example: The composition rule:
p{S1}q
q{S2}r
∴ p{S1; S2}r.
6
Conditional Statements
• Suppose that a program segment has the form
if condition then
S
(p ∧ condition){S1}q
(p ∧¬condition){S2}q
∴ p{if condition then S1 else S2}q 9
if-then-else rule
Example: Verify that the program segment
if x < 0 then
abs := −x
else
abs := x
is correct with respect to the initial assertion T and the final
assertion abs = |x|. i.e. Show that T {if x<0 then abs:=−x else
abs:=x} abs=|x|
10
Loop Invariants
while condition
S
11
Loop Invariants
• This leads to the inference rule:
(p ∧ condition){S}p
∴ p{while condition S}(¬ condition ∧ p)
Example: A loop invariant is needed to verify that the program
segment terminates with factorial = n! when n is a positive
integer.
i := 1
factorial := 1
while i < n
i := i + 1
factorial := factorial · I
Proof: define P, check the program segment and check the look terminates.
12
Loop Invariant Example
Proof. Let p be the assertion “factorial = i! and i ≤ n.”
Then p is a loop invariant because
Suppose that, at the beginning of one execution of the while
loop, p is true and the condition of the while loop holds; in
other words, assume that factorial = i! and that i < n. The new
values inew and factorialnew of i and factorial are
inew = i + 1 and
factorialnew = factorial · (i + 1) = (i + 1)! = inew!.
Because i < n, we also have inew = i + 1 ≤ n. Thus, p is true at
the end of the execution of the loop. This shows that p is a
loop invariant.
13
Loop Invariant Example
Proof.(conti.)
Now we consider the program segment.
Just before entering the loop, i = 1 ≤ n and factorial = 1 = 1! =
i! both hold, so p is true. Because p is a loop invariant, the
rule of inference just introduced implied that if the while
loop terminates, it terminates with p true and with i < n
false. In this case, at the end, factorial = i! and i ≤ n are true,
but i < n is false; in other words, i = n and factorial = i! = n!, as
desired.
14
Loop Invariant Example
Proof.(conti.)
Finally, we need to check that the while loop actually
terminates.
At the beginning of the program i is assigned the value 1, so
after n − 1 traversals of the loop, the new value of i will
be n, and the loop terminates at that point.
15
Loop Invariant Example
Another example: combine several rules:
procedure multiply(m, n: integers)
if n < 0 then a := −n
else a := n
k := 0
x := 0
while k < a
x := x + m
k := k + 1
if n < 0 then product := −x
else product := x
return product
{product equals mn}
17
Loop Invariant Example
Another example: combine several rules:
procedure multiply(m; n : integers)
p := "(m; n ∈ Z)"
if n < 0 then a := - n (segment S1)
else a := n
q := "(p ^ (a = |n|))"
k := 0; x := 0 (segment S2)
r := "q ^ (k = 0) ^ (x = 0)"
(x = mk ^ k≤a) ß- loop invariant
while k < a { (segment S3)
x = x + m; k = k + 1;
}
Maintain loop invariant: (x = mk ^ k≤a)
((x = mk ^ k = a) ) ∴ s := “(x = ma) ^ a = |n|)"
s ⇒ (n < 0 ^ x = - mn) ∨ (n ≥ 0 ^ x = mn)
if n < 0 then prod := - x (segment S4)
else prod := x
18
t = "(prod = mn)"
Correctness of multiply(m; n)
Correctness of de multiply(m; n)
This is the structure of the proof (using the propositions p; q; r; s; t as defined on the
previous slide):
• We must prove p{S1}q using the inference rule: if statements.
• We must prove q{S2}r by examining the trivial segment S2.
• We must prove r{S3}s using the inference rule of the loop : “while cond S”.
• We must prove s{S4}t using the inference rule : if statements.
• We use the composition rule to prove p{S1; S2; S3; S4}t;
To complete the proof of the correctness we have to verify that the segment halts
for every possible input.
The segments S1, S2 and S4 obviously halts for every possible input. For the while loop
(S4), we can easily see that this loop does exactly a iterations.
(The details of each part of this proof are left as an exercise)
19
How to prove that a loop
halts?
How to prove that a loop halts?
• For each iteration i we associate an non-negative integer ki,
such that < k0; k1; k2; ... > is a decreasing sequence.
• Because of the well ordering principle of any subset of
integers, this sequence of ki is finite.
20
3/29/18 WonSook Lee, Univ. of Ottawa 21
• Exercise:
Prove that the program segment
y := 1
z := x + y
is correct with respect to the initial assertion x = 0
and the final assertion z = 1.
Solution:
Suppose that x = 0. The program segment first
assigns the value 1 to y and then assigns the value x +
y = 0 + 1 = 1 to z.
22
• Exercise:
Verify that the program segment
x := 2
z := x + y
if y > 0 then
z := z + 1
else
z := 0
is correct with respect to the initial assertion y = 3 and
the final assertion z = 6.
Solution:
Suppose that y = 3. The program segment assigns the value 2 to
x and then assigns the value x+y = 2+3 = 5 to z. Because y = 3 >
0 it then assigns the value z+1 = 5+1 = 6 to z.
23
• Exercise: Use a loop invariant to prove that the
following program segment for computing the nth power,
where n is a positive integer, of a real number x is correct.
power := 1
i := 1
while i ≤ n
power := power ∗ x
i := i + 1
Solution:
Solution:
We will show that p : “power = xi−1 and i ≤ n + 1” is a loop invariant.
Note that p is true initially, because before the loop starts, i = 1 and power= 1 = x0 =
x1−1.
Next, we must show that if p is true and i ≤ n after an execution of the loop, then p
remains true after one more execution. The loop increments i by 1. Hence, because i ≤
n before this pass, i ≤ n+1 after this pass. Also the loop assigns power · x to power. By
the inductive hypothesis we see that power is assigned the value xi−1 · x = xi .
Hence, p remains true.
Furthermore, the loop terminates after n traversals of the loop with i
= n + 1 because i is assigned the value 1 prior to entering the loop, is
incremented by 1 on each pass, and the loop terminates when i > n.
Consequently, at termination power = xn, as desired.
25
3/29/18 WonSook Lee, Univ. of Ottawa 26