_ 17 and T O T A L P T S >=19) or (SUITLEtVGTH[spades] = 5 and S U I T P O I N T S [ s p a d e s ] >=5) or (SUITLE_NGTH[hearts] -- 5 and SUITPOI1VTS[hearts] > 5)) H . H C P > 1 7 and LO2~GEST>=5 I. H C P >=17 and L O N G E S T = 4 and S U I T P O I N T S [ l o n g e s t suit] ~SP[2] J . H C P ~ 1 8 and E V E N D I S T and S T O P = 4
Bid 1
1 no trump 1 club 1 club 1 club
1 1 club is a strong conventional bid of the bidding system. Definitions of terms used: DISTP the number of distributional points : void, 3 ; singleton, 2; doubleton, 1. E V E i V D I S T - - true if D I S T P <-1. H C P - the number of high card points: Ace, 4; King, 3; Queen, 2; Jack, 1. LONGEST length of the longest suit. SP[I] . . . . ,SP[4J - - the four S U I T P O I N T S ordered such that S P [ l J ~ S P [ 2 ] >SP[3] ->_SP[4]. S T O P - - the number of suits for which suitlength R 3 or (suitlength = 2 and Ace or King present) or singleton Ace. S U I T L E N G T H [ s u i t ] - the number of cards in the suit. S U I T P O I N T S [ s u i t ] - - t h e number of H C P s in the suit. TOTALPTS--HCP suitably modified: add to H C P for each suit with at least five cards, 2(suittength-5)+ 1; for each singleton Ace, 1; for having all four Aces, 1; for the hand, 1; and subtract from H C P for having no Aces, 1; for 4333 distribution, 1. -
-
-
-
Figure 3. Excerpt of opening bidder from reference 18.
PROVING
PROGRAMS
CORRECT:
SOME TECHNIQUES
AND EXAMPLES
173
Thus even the short, seemingly uncomplicated specifications of Figure 1 yielded a surprise: hands for which no bid is made. However, the proof of its properties is straightforward. While the cases of Figure 2 are not copied directly from the bidding algorithm, they are not difficult to produce. A complete analysis, similar to lemmas 1.1 and 1.2, has been successfully accomplished [12] on the entire opening bid procedure (some 145 lines of Algol code, excluding declarations) of a running bridge bidding program [18]. The analysis consists of over 500 cases with a maxim u m depth of 11. Those cases are definitely not copied directly from the bidding algorithm. (For comparison, the number of bridge hands is (~), approximately 6.3 × 1011. Of course, smaller a priori bounds on the number of Cases needed can be derived.) The second example consists, then, of a small portion of the analysis in [12]. The purpose is to show case analysis in a more representative use and to show the exploitation of the relations of the problem domain. Figure 3 is the excerpt of the code which is analyzed as well as definitions of the terms involved. LEMMA 2.1. I f a hand has H C P > 17, then it will meet at least one condition in Figure 3. PROOF. The proof is b y case analysis, 15 cases to a maximum depth of 5. The phrase, for example, "distribution is 4441" means "4 cards in each of three suits, 1 card in the remaining suit." There are only three distributions if L O N G E S T = 4. I. I f L O N G E S T > 5, condition H is met. II. If L O N G E S T = 4 A. If the distribution is 4441, the 1-card suit is at best Ace and then the total H C P s for the three 4-card suits is at least 13. Thus one 4-card suit has the highest ( > 5) S U I T P O I N T S . Condition I is met. B. If the distribution is 4432, then E V E N D I S T holds. 1. I f H C P > 18 a. If S T O P = 4, condition J is met. b. I f S T O P ~ 4, then the 2-card suit is at best Q J else S T O P = 4. The 3-card suit is at best A K Q . Thus the total H C P s for the two 4-card suits is at least 6, i.e., 1 8 - ( 9 + 3 ) . B u t then S U I T P O I N T S [ o n e 4-card suit] > 6/2= 3, at least the second best S U I T P O I N T S . Condition I is met. 2. If H C P = 1 7 a. If all four aces are in the hand, the only other card contriB I T 10 ~
12
174
RALPH
L. L O N D O N
buting to H C P is a Jack. Thus S U I T P O I N T S [ o n e 4-card suit] > 4, at least the second best S U I T P O I N T S . Condition I is met. b. If all four aces are not in the hand i. If S T O P = 4 , T O T A L P T S < 18, since the only addition to H C P is 1 for the hand. Since L O N G E S T = 4, condition G is met. ii. If S T O P 4 4, then proceed as in Case lb. The HCPs for the two 4-card suits total at least 5. Thus S U I T P O I N T S [one 4-card suit] > 3. Condition I is met. C. If the distribution is 4333, then S T O P =4 and E V E N D I S T holds. 1. If H C P > 18, condition J is met. 2. If H C P = 17, then T O T A L P T S < 18 since 1 is added for the hand, possibly 1 is added for all four aces, b u t 1 is subtracted for 4333 distribution. Condition G is met. Q.E.D. This proof depends heavily upon the problem domain (bridge hands) and the properties of the terms involved; only incidentally are the semantics of the code of Figure 3 used. This is natural since the programmer will take advantage of his knowledge of the problem domain and, accordingly, the prover must expect to do likewise. One should not expect, in general, that the proof will follow without the use of relations peculiar to the problem domain. Any implications of this to automatic generation of correctness proofs will not be pursued here. Of special note is the use of the so-called pigeonhole principle [17] which states, "if/on + 1 pigeons are in n pigeonholes, at least one of the holes contains/c+ 1 or more pigeons." Its use occurred at case IIA and elsewhere. While certainly applicable, this principle has nothing a priori to do with bridge or with programming. I t m a y thus be necessary to use as well in a proof a general principle seemingly unrelated to the problem domain or to programming.
The technique of proof by assertions. The third example, also taken from [18], demonstrates an extremely useful proof technique, especially in dealing with loops. I t is described, with examples, in [5], [7] and [15]. The idea is that assertions concerning the progress of the computation are made between lines of code, and the proof consists of demonstrating that each assertion is true each time control reaches that assertion, under the assumption that the previously encountered assertions are true. Using induction on the number of lines of code, it can be shown [7] once and for all that this yields a valid proof
PROVING PROGRAMS CORRECT: SOME TECHNIQUES AND EXAMPLES
175
procedure. I n effect, the technique shows there is no first false assertion. Termination of the program is shown separately. The code for this example is in the form of an Algol Boolean procedure named O U T S I D E A C E : Boolean procedure O U T S I D E A C E (SUIT); value S U I T ; integer S U I T ; begin integer I, K; O U T S I D E A C E : = false; for I : = 1 step 1 until 13 do for K := 4 step - 1 until 1 do if H A N D [ I ] = 1 3 . K - 1 2 and K # S U I T then O U T S I D E A C E :=. true; end of O U T S I D E A C E ; I t is necessary to explain the data representation of this example. O U T S I D E A C E is to return true if the hand contains an outside ace, i.e., an ace other than the ace of the suit denoted by the integer parameter S U I T , and is to return false otherwise. The hand is the array HAND[I:13]. S U I T will be either 4, 3, 2, or 1 denoting the four suits. The internal card representations are the integers 1 to 52 according to the formula, 13 • ( S U I T - 1) + R A N K V A L U E , where
RANK
tAI2IaI415t6171Sl911ol'JIQIK
RANKVA UEt 11 !,3141 16tTIS!" l t o l
n112113
.
LEM~A 3.1. O U T S I D E A C E returns the value true or false according as H A N D contains an ace other than the ace of S U I T or not. PROOF. Using Figure 4 the proof consists of verifying the six assertions t h a t have been inserted as comments in the code for O U T S I D E ACE. E x t r a begin-end pairs have been added in the bodies of the for loops to distinguish points of control, namely the end of the body of the loop from the end of the entire loop. Comment 1 is reached only from the immediately preceding assignment statement, and t h a t statement verifies comment 1. Comment 2 m a y be reached either from comment 1 or from comment 5. In the former case, I = 1 and the range of J is empty; hence comment 2 says O U T S I D E A C E is false and this is so from comment I. In the latter case comment 2 follows from comment 5 noting the appropriate change in I caused by the for statement on I. Comment 4 is reached only from comment 3 and the if statement. Since the aces are represented by 13 • ( S U I T - 1) + 1 = 13 • S U I T - 12,
176
RALPH L. LONDON
Boolean p r o c e d u r e O U T S I D E A C E ( S U I T ) ; value S U I T ; i n t e g e r S U I T ; begin integer 1, K ; O U T S I D E A C E : = false; c o m m e n t 1 : 0 U T S I D E A C E = false; for 1 : = 1 step 1 until 13 do b e g i n c o m m e n t 2: O U T S I D E A C E = t r u e i f f some H A N D [ J ] , J = 1 . . . . . 1 - 1 , is an outside ace;
for K : = 4 step - 1 until 1 do b e g i n c o m m e n t 3: O U T S I D E A C E = t r u e i f f some H A N D [ J ] , J = 1. . . . . 1 - 1 , is an outside ace or H A N D [ I ~ is the L - t h outside ace, L = K + I . . . . . 4; if H A N D [ l ] = 13,K--12 and K ~ S U I T then 0 U T S I D E A C E : = t r u e ; c o m m e n t 4: O U T S I D E A C E = t r u e i f f some H A N D [ J ] , J = 1 , . . . , I-l,
is a n outside ace or H A N D [ I ]
is the L-th outside ace, JL =
K,...,4; end K loop; c o m m e n t 5: O U T S I D E A C E
= t r u e i f f some H A N D [ J ] , J = 1 . . . . ,1, is a n outside ace; end I loop; c o m m e n t 6: O U T S I D E A C E = t r u e i f f some H A N D [ J ] , J = 1. . . . ,13,/s a n outside ace, i.e., i f f the hand contains a n outside ace; end o f 0 U T S I D E A C E ;
Figure 4. Code for OUTSIDEACE including assertions. t h e Boolean e x p r e s s i o n is t r u e iff H A N D [ I ] is t h e K t h ace a n d a n outside ace. Accordingly O U T S I D E A C E is set t r u e if H A N D [ I ] is an outside ace a n d otherwise is n o t changed. H e n c e c o m m e n t 3 holds for L = K also, i.e., c o m m e n t 4 holds. C o m m e n t s 3, 5 a n d 6 follow b y similar a r g u m e n t s which are o m i t t e d . C o m m e n t 6 is t h e l e m m a t o be p r o v e d . O U T S I D E A C E terminates since I a n d K are c h a n g e d o n l y in t h e for s t a t e m e n t s a n d hence t h e if s t a t e m e n t is e x e c u t e d precisely 52 times. Q.E.D. This e x a m p l e is also p r o v e d in [9] b u t b y t h e t e c h n i q u e s d e m o n s t r a t e d in t h e fifth e x a m p l e below.
T h e t e c h n i q u e of m a t h e m a t i c a l induction.
T h e f o u r t h e x a m p l e comes f r o m [18] a n d is a u t i l i t y p r o c e d u r e . G i v e n a c a r d in t h e f o r m of S U I T and RANK, the Boolean procedure CHECKFOR(SUIT, RANK) is t o be t r u e iff t h e c a r d is in H A N D . T h e s a m e r e p r e s e n t a t i o n s are used as in O U T S I D E A C E . T h e code is Boolean p r o c e d u r e C H E C K F O R ( S U I T , RANK); value S U I T , R A N K ; integer SUIT, RANK; begin integer I;
PROVING PROGRAMS COI~I:¢ECT: SOME TECttlqIQIJES AND EXAMPLES
177
C H E C K F O R : = false; for I : = 1 step 1 until 13 do
ff H A N D [ I ] = 13 • ( S U I T - 1) + R A N K then begin C H E C K F O R := true; go to Q U I T end; QUIT: end of C H E C K F O R ; I t is straightforward to supply and to verify the necessary assertions for proving this short program terminates correctly. Instead, an alternative recursive version of this program, C H E C K F O R 1 , will be proved to meet the above definition b y using the techniques of mathematical induction and case analysis. Thus define C H E C K F O R I ( S U I T , R A N K ) := C F ( S U I T , R A N K , l, 13) where CF(S, R, I, N ) : = ff I > N then false else ff H A N D [ I ] = 1 3 , ( S - I ) + R then true else CF(S, R, I + 1, N); CF is in the form McCarthy [13] called iterative form. LEMMA 4.1. CF(S, R, I, N) is true iff the card given by S and R is in the hand consisting of H A N D [ J ] , J = I , . . . ,N. C F terminates in all cases. P~ooF. Use backwards induction on I, i.e., induction on N - I . If I > N + 1, CF terminates with false, the correct result. Assume the lemma holds for N + 1 > I >_K + 1 and consider I = K. K < N so control reaches the second if of the definition. If H A N D [ K ] = 13. ( S - 1)+ R, H A N D [ K ] is the sought card, and CF terminates with true, the correct result. If H A N D [ K ] is not the sought card, the result is CF(S, R, I + 1, N) which, b y the induction assumption for I + I = K + I, terminates with the correct result. Q.E.D. Using lemma 4.1, the correctness of C H E C K F O R 1 follows since I = 1 and N = 13 in the call to CF. Assuming CHECK:FOR has been proved, this shows that C H E C K F O R and C H E C K F O R 1 are the same Boolean procedure. T h e t e c h n i q u e s of a p r o s e p r o o f and t w o o t h e r s .
The final example computes the quantity P O I N T S , the point-count of a hand as defined in Figure 1 except that now a doubleton is worth 1 point. The points for specific cards are called high card points; the points for low suitlengths are called distributional points. An Algol block for computing P O I N T S appears in Figure 5. It assumes that P O I N T S and the H A N D array are global to this block and further assumes the existence of two integer procedures, F I N D R A N K ( C A R D ) and F I N D S U I T ( C A R D ) , for computing the rank and the suit of a card according to the conventions used by the O U T S I D E A C E example.
178
RALPH L. LONDON
0 begin integer I, LS, RANK, 1 P O I N T S : = 0;
SUIT;
2
LENGTH[I]
3
for I : =
4
begin S U I T : -- F I N D S U I T ( H A N D [ I ] ) ;
5
6 7
: = LENGTH[2]
integer array LENGTH[1:4];
: = LENGTH[3]
: = LENGTH[4]
: = 0;
1 s t e p 1 u n t i l 13 do
LENGTH[SUIT]
: = LENGTH[SUIT]
+ 1
end; for I : = 1 s t e p 1 u n t i l 13 do
8 begin R A N K : = F I N D R A N K ( H A N D [ I ] ) ; 9 if 2 __
POINTS+I;
Figure 5. Algol block for computing :POINTS. The code n a t u r a l l y divides into four sections each of which will be p r o v e d (lemmas 5.2-5.5). T h e proofs could t a k e t h e f o r m of verified assertions, b u t instead it will be d e m o n s t r a t e d t h a t it is possible to give a convincing, y e t still rigorous, proof b y s t a n d a r d m a t h e m a t i c a l argum e n t s given in prose form. This e x a m p l e also d e m o n s t r a t e s the obvious b u t often overlooked technique of p r o v i n g a p r o g r a m a section at a time. This is useful for m a n y of t h e same reasons t h a t one debugs a p r o g r a m one section a t a time a n d t h a t one uses s u b r o u t i n i n g in p r o g r a m m i n g . The usefulness of a table such as the one in l e m m a 5.1 is also shown. T h e first l e m m a gives a n overall p r o p e r t y of t h e code. LEM~V~A 5 . 1 .
Values
of variables are changed
Variable HAND[ ] I LENGTH[ LS POINTS RANK S UIT
only as follows :
Changed at Line Number
]
3, 7, 19 2,5 12, 20 1, 11, 14, 16, 17, 21 8 4
PROVING PROGRAMS CORRECT: SOME TECHNIQUES AND EX A MP LES
179
PROOF. Inspection of the code. LEMMA 5.2. Lines 1-2 initialize P O I N T S and the four elements of the L E N G T H array to O. PROOF. Obvious. LEMMA 5.3. Lines 3-6 compute the L E N G T H altering P O I N T S .
of each suit without
PROOF. For fixed I, i.e., each card, line 4 finds the suit of the card and line 5 increments the proper L E N G T H element. Lines 3, 4 (the begin) and 6 and lemma 5.1 for I insure t h a t each of the 13 cards is counted exactly once. Thus after line 6, using lemma 5.1, P O I N T S = 0 from line 1 and L E N G T H [ J ] , J = 1. . . . ,4, is the correct length of the J t h suit. LEMMA 5.4. Lines 7-18 add the high card points to P O I N T S without altering the L E N G T H array. PROOF. For fixed I, i.e., each card, line 8 finds the rank of the card with 1 < R A N K < 13. If the rank is 2-10, P O I N T S is unchanged (lemma 5.1) and control goes to D O N E at line 18. If the rank is one (an ace), 4 points are added and control goes to D O N E . At line 12 R A N K must be 11, 12 or 13 by lines 9-11. Line 12 computes LS, the length of the suit of the card. Lines 13-17 change P O I N T S at most once according to the point-count and control goes to DONE. Lines 7, 8 and 18 insure t h a t each card is counted exactly once. Thus after line 18, using lemma 5.1, P O I N T S equals the high card points of the hand and the L E N G T H array is unchanged from line 6. LEMMA 5.5. Lines 19-22 add the distributional points to P O I N T S . PROOF. For fixed I, i.e., each suit, line 20 computes LS, the length of the suit less 3. That is,
LENGTH[I]
LS
-LS
>=3 2 1 0
>=0 -1 -2 -3
1 2 3
180
RALPH
L. L O N D O N
Hence line 21 adjusts P O I N T S by - L S if L S is negative, agreeing with the distributional points of the point-count. Lines 19, 20 and 22 insure t h a t each of the four suits is counted exactly once. LEM~L~ 5.6. The Algol block in Figure 5 computes P O I N T S quired.
as re-
PROOF. The code is executed by sections in the order covered in lemmas 5.2-5.5. Thus after line 22, P O I N T S is correct, namely the high card points plus the distributional points. Since the for variable I is not changed in the body of any for loop (lemma 5.1), it is clear t h a t the code terminates. Q.E.D. The presentation of the proof of the last example is patterned after the so-called "inside-out" strategy of programming--start with the innermost code and work out. That is, lemmas 5.2-5.5 correspond to inner code and lemma 5.6 to the outermost code. Conclusion.
The feasibility of proving the correctness of programs, at least small ones, should be clear from the preceding five examples. Techniques of proof have been presented by example. Larger, more realistic programs can be proved in roughly the same manner. This claim is supported in another paper [9] which discusses in detail techniques and strategies of program proving. That paper also summarizes the salient features of five additional proofs [4, 6, 8, 12, 14] which are more representative of realworld proofs. I t is not suggested that other techniques, including uninvented ones, are unnecessary nor t h a t an arbitrary program can be proved using this set of techniques. Instead the techniques are but reasonable starting points for the human as he employs his ingenuity and creativity in stating what needs to be proved and in constructing a proof. However, he now has existing proofs to serve as models, and he has available techniques of proof [22]. As the reader surely noted, there are several assumptions implicit in this type of proof of correctness, for example t h a t (i) a common understanding of the programming language exists between people (no explanation of the semantics of any code is here given), (ii) the problem domain is specified sufficiently and (iii) the proof is error-free. If one also considers the environment in which the program is executed, then correctness can at best be assumed for the compiler or interpreter, the operating system and the hardware.
PROVING PROGRAMS CORRECT: SOME TECHNIQUES AND EXAMPLES
181
That a proof depends upon these, and possibly other assumptions, is not reason to avoid proofs entirely. A completed proof of correctness, even with these limitations, is advantageous because (i) it gives sufficient reasons why the program must be correct, (ii) it makes explicit the assumptions on which correctness rests, certainly more so than the program alone does and (iii) it leads to increased understanding of the program. More programs can and should be proved correct.
Acknowledgements. This work is supported by NSF Grant GP 7069 and the Mathematics Research Center, United States A r m y under Contract Number DA-31124-ARO-D-462.
REFERENCES 1. Cooper, D.C., Mathematical proofs about computer programs, Machine Intelligence 1, CoUins, N . L . and Michie, D. (Eds.), American Elsevier, New York, 1967, 17-28. 2. Dijkstra, E. ~V., A constructive approach to the problem of program carrectness, BIT, Vol. 8, No. 3, 1968, 174-186. 3. Dijkstra, E. W., The structure of the "THE"-multiprogramming system, Comm. ACM, VoL II, No. 5, May, 1968, 341-346. 4. Evans, A., Jr., Syntax analysis by a production language, P h . D . thesis, CarnegieMellon University, 1965. 5. Floyd, R . W . , Assigning meanings to prograzas, Proc. of a Symposium in Applied Mathematics, Vol. 19, Mathematical Aspects of Computer Science, Schwartz, J . T . (Ed.), American Mathematical Society, Providence, R.I., 1967, 19-32. 6. Good, D . I . and London, R . L . , Interval arithmetic for the Burroughs B5500: Four Algol procedures and proofs of their correctness, Computer Sciences Technical Report No. 26, University of Wisconsin, 1968. See also [21]. 7. Knuth, D . E . , The Art of Computer Programming, Vol. 1, Fundamental Algorithms, Addison-Wesley, Reading, Mass., 1968, section 1.2.1. 8. London, R. L., Correctness of the Algol procedure A S K F O R H A N D , Computer Sciences Technical Report No. 50, University of Wisconsin, 1968. 9. London, R. L., Computer programs can be proved correct, Theoretical Approaches to Nonnumerical Problem Solving, Proc. of Systems Symposium at Case Western Reserve University, Banerji,R. B. and Mesarovic ~¢[.D. (Eds.), Springer-Verlag, 1970, 281-303. 10. London, R. L., Proof of algorithms: A new kind o/certification (Certification of Algorithm 245 T R E E S O R T 3), Comm. ACM, to appear. 11. London, R. L. and Halton, J. H., Proofs of algorithms for asymptotic series, Computer Sciences Technical Report No. 54A, University of Wisconsin, 1969. 12. London, R. L. and Wasserman, A . I . , The anatomy of an Algol procedure, Computer Sciences Technical Report No. 5, University of Wisconsin, 1967. 13. McCarthy, J., Towards a mathematical science of computation, Information Processing 1962, Proe. of I F I P Congress 62, Popplewell, C. M. (Ed.), North-Holland, Amsterdam, 1963, 21-28.
182
RALPH L. LONDON
14. McCarthy, J . a n d Painter, J . A., Correctness of a compiler for arithmetic expressions, Proc. of a Symposium in Applied Mathematics, Vol. 19, Mathematical Aspects of Computer Science, Schwartz, J . T . (Ed.), American Mathematical Society, Providence, R.I., 1967, 33-41. 15. Naur, P., Proof of algorithms by general sncrpshots, BIT, Vol. 6, No. 4, 1966, 310-316. 16. Newell, A. et. al., Information Processing Y_xtnguage-V Manual, Second Edition, Prentice-Hall, Englewood Cliffs, N.J., 1964, 48-67. 17. Niven, I., Mathematics of Choice, R a n d o m House, New York, 1965, 120. 18. Wasserman, A. I., Achievement of skill and generality in an artificial intelligence pra. gram, P h . D . thesis to be submitted to University of Wisconsin, 1970. 19. Weissman, C., Lisp 1.5 Primer, Dickenson, Belmont, Calif., 1967, 157-159. 20. Wood, D., A proof of Hamblin's algorithrafor translation of arithmetic expresslons from infix to postfix form, BIT, Vol. 9, :No. 1, 1969, 59-68. 21. Good, D. I. and London, R. L., Computer interval arithmetic: Definition and proof of correct implementation, J . ACM, t o appear. 22. London, R. L., Bibliography on proving the correctness of computer programs, Machine Intelligence 5, ~ e l t z e r , B. a n d Michie, D. (Eds.), E d i n b u r g h University Press, Edinburgh, 1970, 569-580. 23. Tobey, R. G., Rational function integration, Ph. D. thesis, H a r v a r d University, 1967.
COMPUTER SCIENCES DEPARTMENT AND
MATHEMATICS RESEARCH CENTER, UNITED STATES ARMY UNIVERSITY OF WISCONSIN MADISON, WISCONSIN 53706