Professional Documents
Culture Documents
Manna MathematicalTheoryOfComputation Oct27 PDF
Manna MathematicalTheoryOfComputation Oct27 PDF
CHAPTER 3
VB~ifie!8tillg
llf ~~llg~8ms
Introduction
(y1 , y 2 , . . • , Yb), which is used as temporary storage during computation; 4. HALT statement
and (3) an output vector z = (z 1 , z 2 , . . . , zc), which yields the output values
when computation terminates. Correspondingly, we also distinguish among
three types of (nonempty) domains: (1) an input domain Dx, (2) a program
domain D-y, and (3) an output domain D,. Each domain is actually a cartesian
product of subdomains:
Dx = Dx1 X DX2 X X Dx,
Dy = DYI X DY2 X X Dh where h(x, ji) is a total function mapping D;; x Dy into D,
D, = DZI X DZ2 X X Dz,
A flowchart program is simply any flow-diagram constructed from these
We also distinguish among four types ofstatements:t
statements (with exactly one START statement) such that every ASSIGN-
1. START statement MENT or TEST statement is on a path from the START statement to
some HALT statement. In other words, flowchart programs are not al-
lowed to include "dead-end" TEST statements such as
T F
t ex, .Yl
wheref(x) is a total function mapping D-x into D-y. Given such a flowchart program P and an input value ~ E D-x for the
2. ASSIGNMENT statement input vector x, the program can be executed. Execution always begins at
the START statement by initializing the value of ji tof(~) and then proceeds
in the normal way, following the arcs from statement to statement. When-
ever an ASSIGNMENT statement is reached, the value of y is replaced
by the value of g(x, y) for the current values x and jl. Whenever a TEST
statement is reached, execution follows the T or F branch, depending
on whether the current value of t(x, y) is true or false; the value of y is
where g(x, ji) is a total function mapping D-x x D-y into D-y unchanged by a TEST statement. If the execution terminates (i.e., reaches a
3. TEST statement HALT statement), z is assigned the current value, say, ~' of h(x, ji) and we
say that P(e) is defined and P(~) = (; otherwise, i.e., if the execution never
terminates, we say that P(~) is undefined. In other words, the program P
should be considered as representing a partial function z = P(x) mapping
Dx into D,.
For example, the flowchart program in Fig. 3-1 performs the integer
where t(x, ji) is a total predicate over D:x x DJi division of x 1 by x 2 , where x 1 ~ 0 and x 2 > 0, yielding a quotient z 1 and
a remainder z2 ; that is, z 1 = div(x 1 ,x2 ), and z 2 = rem(x 1 ,x 2 ) . Here x =
t In addition, we clearly allow the JOI NT statement tor combining two or more arcs (arrows) into (x 1 , x 2 ), ji = (y 1 , y 2 ), z = (z 1 , z2 ), and Dx = D-y = D, = {all pairs of inte-
a single one.
gers}.
164 VERIFICATION OF PROGRAMS FLOWCHART PROGRAMS 165
a (START), p (LOOP\ andy (HALT), respectively. All terminating execu- path p shows that if p(x 1 , x 2 , y 1 , y 2 ) is true at the entrance of the loop the
tions of the program must first follow path a, then pass some number of first time, it will be true the second time; if it is true the second time, it will
times (possibly zero) around the loop p, and finally finish with path y; be true the third time; etc. Verification of path y shows that when the loop
thus all executions are "covered" by these three paths. is left with predicate p (x 1 , x 2 , y 1 , y 2 ) true, then the output predicate of the
program is true. In other words, verification of paths a and p guarantees
that p(x 1 , x 2 , y 1 , y 2 ) has the property that whenever the computation of
the program reaches the cutpoint 8, p(x 1 , x 2 , y 1 , y 2 ) is true for the current
values of x 1 , x 2 , YI> and y 2 • Therefore, verification of path y implies that
whenever the computation of the program reaches point C, the output
predicate of the program is true.
In order to complete the proof of the partial correctness of the division
2 program, we must verify the paths a, p, and y. The verification of a path
.-- - - - -- - - _ . 8 - ---- --Cut is performed in two steps: First we construct a verification condition for
3 each path in terms of the given predicates, and then we prove it.
T F Constructing a verification condition of a path is usually done by
Yz ~ Xz
moving backward through the path, considering each statement in turn.
5 4 Path a: Let's consider first path a of Fig. 3-2.
6
A
-------- cp(x 1 ,Xz)
7 1
C -------t/l(xl,Xz,ZI , zz) :
X1 = Z 1X 2 + z2
HALT A 0 ;£ Zz < Xz
Figure 3-2 Flowchart program for performing integer div ision (with the input and 2
output predicates) .
-------p(xt ,Xz,Yt ·Yz )
In order to prove the partial correctness of the program, first we must 8
find a predicate p (x 1 , x 2 , y 1 , y 2 ) describing the relationships among the
program variables at cutpoint B. An appropriate predicate for this purpose What must be true at point A, that is, before the execution of the statement
is obtained by taking p(x 1 , x 2 , y 1 , y 2 ) to be (y 1 , y 2 ) <-- (0, xr), to ensure that p(x 1 , x 2 , y 1 , y 2 ) is true after its execution?
The answer is p(x 1 , x 2 , 0, x 1 ), which is formed by substituting 0 for all
X1 = Y1X2 + Yz A Yz ~ 0 occurrences of y 1 in the predicate and x 1 for all occurrences of y 2 . Thus,
the verification condition of this path is
Having obtained this predicate, we have covered the program by three
paths, each of which begins and ends with a predicate. The partial cor-
rectness of the program is proved by verifying each one of the three paths
a, p, and y by showing that if its initial predicate is true for some values of that is,
x and y, and the path is executed then its final predicate will be true for the [x 1 ~ 0 1\ x 2 ~ OJ ~ [x 1 = 0 · x 2 + X1 1\ X1 ~ OJ
new values of x andy.
In the integer division program, verification of path a establishes that Path p: To construct the verification condition of path p, the TEST state-
p(x 1, x 2 , Y1, y 2 ) is true on the first entrance to the loop of the program ment must be handled. As we follow path p, the TEST statement finds
(assuming the input predicate of the program is satisfied). Verification of y 2 ~ x 2 , which can be shown as an annotated piece of flowchart:
168 VERIFICATION OF PROGRAMS FLOWCHART PROGRAMS 169
or, equivalently,
4 B
- - ------P (Xl, Xz • Y1, Yz )
3
5
F
-- - - - - - - - - -- P(x 1, Xz , Y 1 , Yz) Yz ~ Xz
B
5
The reader can check for himself that for all integers x 1 , x 2 , y 1 , and Yz ,
B ------ --- p(x t, Xz, Y1, Yz )
the three verification conditions are true; thus, the program is partially
correct wrt q> and t/J.
In this case the verification condition to be proved is t Throughout this chapter it should be understood that an expression of the form A 1 A A 2 :::> B
standsfor (A 1 A A 1 ) => Band.ingenerai,A 1 A A 1 A . .. A A. => Bstandsfo r (A, A A 2 A .. A A.)
p(xt , Xz,Yt>Yz) :::> [Yz ~ Xz :::> p(xi , Xz ,Yt + l ,Yz - Xz)] :::> B.
FLOWCHART PROGRAMS 171
170 VERIFICATION OF PROGRAMS
Termination Actually, so far we have only partially verified our program: Step 1. Cutpoints. The first step is to cut the loops of the program by
We have proved that whenever a computation of the program terminates choosing on the arcs of the flowchart a finite set of points, called cutpoints,
(that is, reaches a HALT statement), the output predicate is true; however, in such a way that every loop includes at least one such cutpoint. To this
we have not proved at all that the computation of the program indeed set of cutpoints we add a point on the arc leading from the START box,
terminates. Thus, in order to complete the verification of the division called the START point, and, for each HALT statement, a point on the
program, we must prove termination as well. arc leading to the HALT box, called a HALT point.
We shall now prove that the program terminates for every input x 1
and x 2 where
(Note, again, that the program does not terminate for x 2 = 0.) First we
show that the predicate -----HALT point
q(xl,Xz,yl ,Yz ): Yz ~ 0 1\ Xz > 0
HALT
has the property that whenever we reach point B during the computation,
q (x 1 , x 2 , Y!, y 2 ) is true for the current values of the variables. For this
purpose, we must prove the following two verification conditions :
We consider only paths which start and end at cutpoints and which
ql(x 1 , x 2 ) ::J q(x 1 , x 2 ,0,xd (o:) have no intermediate cutpoints. Each such path is finite because of the re-
striction that every loop includes a cutpoint, and there can be only a finite
q(xl, Xz ,YI,Yz) 1\ Yz ~ Xz ::J q(xl,Xz ,Y! + l ,y2 - x2) (/])
number of paths. Let a be a path leading from cutpoint ito j.t In the follow-
that is, ing discussion, we shall make use of the predicate RJx, .YL which indicates
(x 1 ~ 0 1\ x 2 > 0) ::J (x 1 ~ 0 1\ x 2 > 0) the condition for thi path to be traversed, and rJx, -), which describe
the ffirn.sformation of the values of y effectea while the path is traversed.
(yz ~ 0 1\ Xz > 0 1\ Yz ~ Xz) ::J (Yz - x 2 ~ 0 1\ x 2 > 0) Thus, Ra(x, y) is a predicate over D-x x Dv, and ra(x, y) is a function mapping
D-x x Dy into Dy. Both Ra and ra are expressed in terms of the functions
which are clearly true.
and tests used in o:; a simple method for deriving them is to use the back-
We observe now that since we always have x 2 > 0 at point B, whenever
ward-substitution technique.
we go around the loop (from B back to B), the value of Yz decreases. Also,
Initially, R (x, ji) is set to T and r (x, ji) is set to y, and both are attached
we know that y 2 ~ 0 whenever we reach point B. Thus, since there is no
to cutpoint j; then, in each step, the old R and r are used to construct the
infinite decreasing sequence of natural numbers, we cannot go infinitely
new Rand r, moving backward toward cutpoint i. The final Rand r obtained
many times around the loop; in other words, the computation must termi-
at cutpoint i are the desired Ra and ra. The rules for constructing the new
nate. Using this approach, we present a general technique for proving
R and r in each step are :
termination of programs in Sec. 3-1.2.
-----New { R(x, f(x)) In the special case that j is a HALT point, r is initialized to z and R
r(x, f(x)) to T; then in the first step of moving backward we obtain
_____ { R (x, y) T
r(x,y) h(x,y)
-----Old { R(x, y)
r(x,y)
- - ---New { R(x,g(x,y))
r(x, g(x, y))
- - --Old {R~x,_Y)
r(x, y) tdx,y) A~ t 2 (x,g2 (x,y))
g3(x,gz(x,Y))
~(~:~)
y+--gz(X, y)
- ---- --- -- - - -Old {
_ ~~ep _3__ { R: ~ t 2 (x, .Y)t
r : g3(x, .Y)
~-;/----New {R~x,!)
-----L-..._
t 2 (x,y)
• r(x, y)
R(x,ji)
1 - --- -- Old {r(x, y)
Consider, for example, the backward-substitution technique for path
a described in Fig. 3-3. Starting with T for R and y for r at cutpoint j, we j --~~p_l__ { R: T
proceed backward and at cutpoint i finally obtain r: y
Figure 3-3 Backward -substitution tech nique.
R~(x,y): tl(x,gdx,y)) A~ t 2 (x,g 2 (x,gdx,y)))
ra(x, y): g3 (.X, gz (x, gl (x, y))) t Note that we have used the fact that - 12 !-'i . .f) 1\ T is logically equivalent to - 1 2 (.x. y).
FLOWCHART PROGRAMS 175
174 VERIFICATION OF PROGRAMS
Step 2. Inductive assertions. The second step is to associate with each In general, all the steps are quite mechanical, except for step 2; dis-
cutpoint i ofthe program a predicate p;(x, y) (often called inductive assertion), covering the proper inductive assertion requires a deep understanding of
which purports to characterize the relation among the variables at this the program's performance. Let us illustrate the inductive-assertions
point; that is, p;(x, .Y) will have the property that whenever control reaches method with a few examples.
point i, then p;(x, ji) must be true for the current values of x and ji at this
point. The input predicate cp (x) is attached to the START point, and the EXAMPLE 3-1
output predicate t/J (x, z) is attached to the HALT points. The program P 1 over the integers (Fig. 3-4) computes z = L J for .JX
every natural number x ; that is, the final value of z is the largest integer k
This condition states simply that if P; is true for some values of x and ji,
and x and ji are such that starting with them at point i the path rx will
indeed be selected, then Pi is true for the new values of x and ji after the
path rx is traversed.
In the special case that j is a HALT point, the verification condition
IS
and in the case that i is the START point, the verification condition is
Yz <-- Yz + Y3
B
The final step is to prove that all these verification conditions for our
choice of inductive assertions are true. Proving the verification conditions F
implies that each predicate attached to a cutpoint has the property that
whenever control reaches the point, the predicate is true for the current
values of the variables; in particular, whenever control reaches a HALT
point, t/J(x, z) is true for the current values of x and z. In other words,
proving the verification conditions implies that the given program P is
partially correct wrt cp and t/J.
To summarize, we have the following theorem.
THEOREM 3-1 [INDUCTIVE-ASSERTATIONS METHOD (Floyd)] Figure 3-4 Program P 1 for computing : = L xJ
For a given flowchart program P, an input predicate cp(x), and an output
predicate t/J(x, z), apply the following steps: (1) Cut the loops; (2) find
an appropriate set of inductive assertions ; and (3) construct the verifica- We shall prove that the program is partially correct wrt the input
predicate cp (x): x ;;;; 0 and the output predicate t/1 (x, z): z ~ x <
2
tion conditions. If all the verification conditions are true, then P is partially
correct wrt qJ and t/J. (z + 1) 2 • For this purpose first we cut the only loop of the program at
176 VERIFICATION OF PROGRAMS FLOWCHART PROGRAMS 177
point B and therefore have three control paths to consider. Using the Path y (from B to C):
backward-substitution technique, we obtain the predicate R and term r
for each path as follows. B ----~~p}__ { R: y2 > x
r : Yt
A Step 3 {R : T
I . (0, 0 + I, I)
Step 2 {R: T
I · (y I • J' 2 + Y 3 ' J' 3 )
Thus, R y is y 2 > x, and ry is y 1 •
Now we are ready to verify the given program. For this purpose we
Step I {R: T attach the inductive assertion
B r : (y, , yz.J'.d p(x, yt,Y2 , Y3): yf ~ x 1\ Y2 = (Yt +W 1\ Y3 = 2yt + 1
Thus, Ra is T, and ra is (0, 1, 1). to cutpoint B, in addition to attaching the input predicate <p(x): x ~ 0
to cutpoint A and the output predicate 1/J(x, z) : z 2 ~ x < (z + 1)2 to
cutpoint C (see Fig. 3-5).
Path f3 (from B to B):
The three verification conditions to be proved (for all integers x, y 1 ,
Yz, and YJ) are:
1. For path r:t..
[yf ~ x 1\ Yz = (Yt +W 1\ Y3 = 2y t + 1 1\ Y2 ~ x]
x~O
y /2 = y 1 y /2 - 1
0 if Y2 is odd
Y/2 = (Yt . YdY2 12 if Y2 is even
START
A --------cp(x): x 2 ~0
B ---- p(x,y~>y2,Y3): YI ;£ x A Y2 = (y 1 + 1) 2
A Y3 = 2yt + I
,.-------- - - - - - . . 8 - - - ---- p(x,y): x2 ~ 0 1\ Y2 ~ 0
A Y3 . .hn = x1x2
T
C - -----l{l(x,z): z 2 ;£ x < (z + 1) 2
3. For path y
[p(x,yt,Y2•Y3) A Y2 > x]:::) l{l(x,yd
Figure 3-6 Program P2 for computing= = x 1"' (partial correctness) .
that is,
[yf ;£X A Y2 = (y 1 + 1)2 A Y3 = 2y 1 + 1 A Y2 > x]
We shall prove that the program is partially correct wrt the input
:::) yf ;£ X < (y1 + 1) 2
predicate cp(.X): x 2 ~ 0 and the output predicate l{l(x, z): z = x 1 ,.2 • For
Since the three verification conditions are true, it follows that the given this purpose we cut both loops of the program at point B and attach to it
program P 1 is partially correct wrt the input predicate x ~ 0 and the the inductive assertion
output predicate z 2 ;£ x < (z + 1)2 •
p(x,y): x2 ~ 0 A Yz ~ 0 1\ y 3 • y/2 = x 1 x 2
D The verification conditions to be proved (for all integers x 1 , x 2 , y 1 , y 2 ,
EXAMPLE 3-2
and Y3) are:
1. For path rx (from A to B)
The program P 2 over the integers (Fig. 3-6) computes z = x 1 x 2 for any
integer x 1 and any natural number x 2 (we define 0° as equal to 1). The cp(x):::) p(x,x 1 ,x2 , I)
FLOWCHART PROGRAMS 181
180 VERIFICATION OF PROGRAMS
that is, We shall prove that the program is partially correct wrt the input
predicate q> (.X): x 1 > 0 1\ x 2 > 0 and the output predicate t/1 (.X, z): z =
gcd(x 1 , x 2 ). For this purpose we cut the two loops of the program at point
2. For path /3 1 (from B to B via statement 1) B and attach to the cutpoint B the assertion
p(.X, ji): x 1 > 0 1\ x2 > 0 1\ Y1 > 0 1\ Yz > 0 1\ gcd(y1, Yz) = gcd(x 1 , Xz)
[p(x,ji) A Yz =f. 0 1\ odd(yz)J :::l p(x,y1,Yz- 1,y 1Y3)
that is,
[xz ~ 0 1\ Yz ~ 0 1\ Y3 · Y/ 2 = X1x2 1\ Yz =f. 0 odd(Yz)J
1\
2 x1 > 0 1\ x2 > 0
:::J [xz ~ 0 1\ Yz - 1 ~ 0 1\ (Y1Y3 ) · Y1y - 2 1
= x 1x J
3. For path /3 2 (from B to B via statement 2)
[p(x, ji) 1\ y 2 =f. 0 1\ even(y 2)J :::J p(x, y 1 y 1 ,y2/ 2,y 3 ) ~------•---------p(x,ji): x 1 > 0 A x 2 > 0
B 1\ y, > 0 1\ Yz > 0
that is, 1\ gcd(y 1, y 2) = gcd(x 1 , x 2 )
[xz ~ 0 1\ Yz ~ 0 1\ Y3 · Yly2 = X1x2 1\ Yz =f. 0 1\ even(Yz)J
Since the four verification conditions are true, it follows that the program
is partially correct wrt the input predicate x 2 ~ 0 and the output predicate
z = xlxz.
D
Figure 3-7 Program P 3 for computing z = gcd(x 1 • x 2 ) (partial correctness).
EXAMPLE 3-3
The program P 3 over the integers (Fig. 3-7) computes z = gcd (x 1 , x 2 ) for The verification conditions to be proved (for all integers x 1 , x 2 , y 1 ,
every pair of positive integers x 1 and x 2 ; that is, z is the greatest common and y 2 ) are:
divisor of x 1 and x 2 [for example, gcd(14, 21) = 7, and gcd(13, 21) = 1]. The 1. For path r:1. (from A to B)
computation method is based on the fact that cp(.X) :::J p(.X, x 1 , Xz)
that is,
If Y1 > Yz, then gcd(y 1 , y 2) = gcd(Yl - Yz, Yz)
If Y1 < Yz, then gcd(y 1,y2) = gcd(y1.Y2- Yd [x 1 > 0 1\ x 2 >OJ :::J [x 1 > 0 1\ x 2 > 0
1\ x 1 > 0 1\ x 2 > 0 1\ gcd(x 1 ,x 2) = gcd(x 1 ,x 2 )J
If Y 1 = y 2 , then gcd(y 1, y 2) = Y1 = Yz
182 VERIFICATION OF PROGRAMS FLOWCHART PROGRAMS 183
2. For path /3 1 (from B to B via statement 1) n we haven > · · · > 2 > 1 > 0, there are no infinite decreasing sequences
;f natural numbers. Note that we may not use the set of all integers with
[p(x,ji) A Y! =I= Yz 1\ Y1 > Yz] ~ p(x,Y!- Yz,Yz)
the same ordering because it has infinite decreasing sequences such as
that is, n > . · · > 2 > 1 > 0 > - 1 > - 2 > · · · . In general, every well-found-
[xl > 0 1\ Xz > 0 1\ Yl > 0 1\ Yz > 0
ed set can be used for the purpose of proving the termination of flowchart
1\ gcd(yl , Yz) = gcd(xl, Xz) 1\ Y1 =I= Yz 1\ y > Yz]
programs as we shall demonstrate.
1 A partially ordered set (W, -<) consists of a nonempty set Wand any
~ [xl > 0 1\ Xz > 0 1\ Y1 - Yz > 0 1\ Y2 > 0
1\ gcd(yl- Yz,Yz) = gcd(x 1 ,x2 )]
binary relation -< on elements of W which satisfies the following properties:
1. For all a, b, c E W, if a -< b and b -< c, then a -< c (transitivity).
2. For all a, bE W, if a -< b, then b -1< a (asymmetry).
3. For path /3 2 (from B to B via statement 2) 3. For all a E W, a-!< a (irreflexivity).
[p(x, ji) 1\ Y1 =/= Y2 1\ Y1 ~ Yz ] ~ p(x, Y1, Yz - Yd As usual, we write a -< b or b >- a; a -1< b means "a does not precede b."
that is, Note that the ordering need not be total, i.e., it is possible that for some
a, b E W, neither a -< b nor b -< a holds.
[x 1 > 0 1\ x 2 > 0 1\ y 1 > 0 1\ Yz > 0 A partially ordered set (W, -<) which contains no infinite decreasing
1\ gcd(yl, Yz) = gcd(xl> Xz) 1\ Y1 =I= Yz 1\ Y1 ~ Yz ] sequences, a 0 >- a 1 >- a 2 >- · · · , of elements of W is called a well-founded
~ [xl > 0 1\ Xz > 0 1\ Y! > 0 1\ Yz - Y! > 0 set. For example:
1\ gcd(yi,Yz- Yd = gcd(x 1 , x 2 )] (a) The set of all real numbers between 0 and 1, with the usual ordering
<,is partially ordered but not well-founded (consider the infinite decreasing
4. For path y (from B to C) t
sequencd > > i > · · · ).
(b) The set I of integers, with the usual ordering <, is partially
[p(x,ji) 1\ Y1 = Yz] ~ t/J(x,yl ) ordered but not well-founded (consider the infinite decreasing sequence
that is, 0 > -1 > -2 > ... ).
(c) The set N of natural numbers, with the usual ordering <, is well-
[x 1 > 0 1\ x 2 > 0 1\ y 1 > 0 1\ Yz > 0 founded.
1\ gcd(Y!, Yz) = gcd(xl> x 2 ) 1\ Y1 = Yz ] (d) If L is any alphabet, then the set L* of all words over L with the
~ Y1 = gcd(x 1 , x 2 ) substring relation -< (that is, w1 -< w2 iffw 1 is a proper substring of w2 ),
is well-founded.
Since the four verification conditions are true, it follows that the program Now, suppose we are given a flowchart program P and an input
is partially correct wrt the input predicate x 1 > 0 1\ x 2 > 0 and the output predicate <p. To show that P terminates over <p, we propose the following
predicate z = gcd(x 1 , x 2 ) . procedure:
Step 2. Choose a well-founded set (W, -<) and with every cutpoint i
associate a partial function u;(x, y) mapping Dx x Dy into W such that
u;(x, ji) are good functions. That is, for every cutpoint i, we have
Vx'v'Y[q;(x,y) => u;(x,y)E W] Figure 3-8 Program P 1 for computing == [,JX] (termination).
[q(x, ji) 1\ Y2 + Y3 ~ xJ
=> [u (x, yl , yz , YJ ) > u(x,yl + 1,yz + y3,Y3 + 2) J F
that is,
Since all three conditions a re true, it follows that the program terminates
for every natural number x.
D
t Let p8 (x,ji) be x, > 0 1\ x, > 0 "y 1 > 0 A y 2 > 0 1\ y 3 ·gcd (Jt . y 2 ) = gcd(x,,x, ) and let pc(x,ji)