Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

  • Upload
    sydfi

  • View
    218

  • Download
    0

Embed Size (px)

Citation preview

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    1/234

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    2/234

    . , v ' ~ . 1 c :

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    3/234

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    4/234

    STRUCTUREDPROGRAMMING

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    5/234

    A.P.I.C. Studies in Data ProcessingGeneral Editor: C. A. R. Hoare

    1. Some Commercial Autocodes. A Comparative StudyE. L. WiUey, A. d'Agapeyeff, Marion Tribe, B. J. Gibbens andMichelle Clarke.

    2. A Primer of ALGO L 60 ProgrammingE. W. Dijkstra

    3. Input Language for Automatic ProgrammingA. P. Yershov, G. I. Kozhukhin and U. Voloshin

    4. Introduction to System ProgrammingEdited by Peter Wegner

    5. ALGOL 60 Implementation. The translation and use of Algol 60Programs on a Computer

    B. RandeU and L. J. Russell6. Dictionary for Computer Languages

    Hans Breuer7. The Alpha Automatic Programming System

    Edited by A. P. Yershov8. Structured Programming

    O.-J. Dahl, E. W. Dijkstra and C. A. R. HoareIn preparation

    Operating Systems TechniquesEdited by C. A. R. Hoare and R. H. Perrott

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    6/234

    A . P . I . C . S t u d ie s in D a t a P r o c e s s i n gNo. 8

    S T R U C T U R E DP R O G R A MM I N G

    O . - J . DAH LUniversitet i Oslo,Ma temat i sk Ins t itu t ,Blindern, Oslo, Norway

    E . W . DIJK S T RADepartment o f Mathemat ics ,Technological University,Eindhoven, The Netherlands

    C . A . R . H O A R EDepartment of Computer Science,The Queen 's U niversi ty o f Belfast,Belfast , Northern Ireland

    1 9 7 2A C A D E M I C P R E S S

    L O N D O N A N D N E W Y O R K

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    7/234

    ACADEMIC PRESS INC. (LONDON) LTD.24]28 Oval Road,

    London NW1United States E dition published by

    ACADEMIC PRESS INC.111 Fifth Avenue

    New York, New Yo rk 10003

    Copyr ight 1972 byACADEMIC PRESS INC. (LONDON) LTD.

    S econd p r in t ing 1973

    All Rights ReservedNo part of this book may be reprod uced in any form by photostat, microfilm, or any othermeans, without written permission from the publishers

    Library of Congress Catalog Card Number: 72-84452ISBN: 0--12-200550-3

    PRINTED IN GREAT BRITAIN BYWHITSTABLE LITHO~ STRAKER BROTHERS LTD.

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    8/234

    P R E F A C EIn recent years there has been an increasing interest in the ar t of computerprogram ming , t he concep tua l t oo l s ava i l ab le fo r t he des ign of p rograms,and the p reven t ion o f p rogram ming ove rs igh t s and e r ro r . The in it i al ou t -s t and ing con t r ibu t ion to our un de rs t and ing of th i s sub jec t was made byE . W. D i jks t ra , whose Notes on St ruc tured Programming fo rm the f i r s t andmajor sect ion of this book. They c lear ly expound the ref lec t ions of a br i l l iantp rogrammer on the me thods which he has h i the r to unconsc ious ly app l i ed ;the re can be no p rogram mer o f t he p re sen t day who cou ld no t i nc rease hi sski l l s by a study and conscious appl icat ion of these pr inciples.

    In the second monograph I have t r ied to describe how similar pr inciplescan be appl ied in the design of data st ructures. I have suggested th at inana lys ing a p rob lem and grop ing towards a so lu t ion , a p rogrammer shouldtake advantage of abst ract concepts such as se ts , sequences, and mappings;and judic iously postpone decisions on representa t ion unt i l he is const ruct ingthe more de t a i led code o f t he p rogram. Th e mon ograp h a l so desc r ibes arange o f useful ideas for data rep resenta t ion, and suggests the cr i ter iare levant for thei r se lect ion.

    The th i rd m onog raph prov ides a syn thes i s o f the p rev ious two , andexpounds the c lose theoret ica l and pract ica l connect ions between the designof da t a and the des ign of p rograms. I t i n t roduces use fu l add i ti ona l m e thodsfor p rogram and da ta s t ruc tu r ing which may be unfami l i a r t o many pro-grammers . The examples show tha t s t ruc tu red p rogramming pr inc ip l e scan be equa l ly app l i ed in "bo t tom-up" a s i n " top-down" program des ign .The original inspira t ion, insight , and a l l the examples were contr ibuted byO. - J . Dahl ; I have on ly a ssembled the ma te r i a l , and added some add i t iona lexplanat ions where I found i t di ff icul t to understand.June 1972 C . A . R . H O A R E

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    9/234

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    10/234

    C O N T E N T SPage

    P r e f a c e . . . . . . . . . . . . . . . . . . v

    I . N o t e s o n S t r u c t u r e d P r o g r a m m i n g . EDSGER W. DIJKSTRA . 1I . T o M y R e a d e r . . . . . . . . . . . . . 12 . O n O u r I n a b i l i t y T o D o M u c h . . . . . . . . 13. O n T h e R e l i a b i l i t y o f M e c h a n i s m s . . . . . . . 34 . O n O u r M e n t a l A i d s . . . . . . . . . . . 65. A n E x a m p l e o f a C o r r e c t n e s s P r o o f . . . . . . . 126 . O n t h e V a l i d i t y o f P r o o f s V e r s u s t h e V a l i d i t y o f I m p l e m e n t -

    a t i o n s . . . . . . . . . . . . . . . . 147 . O n U n d e r s t a n d i n g P r o g r a m s . . . . . . . . . 168. O n C o m p a r i n g P r o g r a m s . . . . . . . . . . 239 . A F i r s t E x a m p l e o f S t e p- w i s e P r o g r a m C o m p o s i t i o n . . 2 6

    I0 . O n P r o g r a m F a m i l i e s . . . . . . . . . . . 391 1. O n T r a d i n g S t o r a g e S p a c e f o r C o m p u t a t i o n S p e e d . . . 4 212. O n a P r o g r a m M o d e l . . . . . . . . . . . 4 41 3. A S e c o n d E x a m p l e o f S t e p -w i s e P r o g r a m C o m p o s i t i o n . 5 014. O n W h a t W e H a v e A c h i e v e d . . . . . . . . . 5915. O n G r o u p i n g a n d S e q u e n c i n g . . . . . . . . . 6316 . D es i g n C on s i d e r a t i o n s i n M o r e D et a i l . . . . . . 6 717. T h e P r o b l e m o f t h e E i g h t Q u e e n s . . . . . . . . 7 2

    I I . N o t e s o n D a t a S t r u c t u r i n g . c . A. R . H O A R E . . . . . . . 831. I n t r o d u c t i o n . . . . . . . . . . . . . . 8 32 . T h e C o n c e p t o f T y p e . . . . . . . . . . . 913. U n s t r u c t u r e d D a t a T y p e s . . . . . . . . . . 9 64 . T h e C a r t e s i a n P r o d u c t . . . . . . . . . . . 1035. T h e D i s c r i m i n a t e d U n i o n . . . . . . . . . . 1096. T h e A r r a y . . . . . . . . . . . . . . 1157. T h e P o w e r s e t . . . . . . . . . . . . . 1228. T h e S e q u e n c e . . . . . . . . . . . . . 13 09 . R e c u r s i v e D a t a S t r u c t u r e s . . . . . . . . . . 142

    10. S p a r s e D a t a S t r u c t u r e s . . . . . . . . . . . 14811. E x a m p l e : E x a m i n a t i o n T i m e t a b l e s . . . . . . . 155

    VI I

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    11/234

    VIII CONTENTSPage

    12. A x i o m a t i s a t i o n . . . . . . . . . . . . . 166Refe rences . . . . . . . . . . . . . . 174

    III . Hi er ar ch ic al P ro gr am Str uct ur es. OLE-JOHAN DAHL AND C. A. R. HOARE 1751. I n t r o d u c t i o n . . . . . . . . . . . . . . 1752. Pr el im ina r ie s . . . . . . . . . . . . . . 1753. Objec t CIasses . . . . . . . . . . . . . 1794. C o r o u t i n e s . . . . . . . . . . . . . . 18 45. Lis t S t ruc tu r es . . . . . . . . . . . . . 1936. P r o g r a m C o n c a t e n a t i o n . . . . . . . . . . . 2027. C o n c e p t H ie ra r ch ie s . . . . . . . . . . . 208

    Refe rences . . . . . . . . . . . . . . 220

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    12/234

    I . N o t e s o n S t r u c t u r e d P r o g r a m m i n gEDSGER W. DIJKSTRA

    1. To MY R EADERThese no tes have the s ta tu s o f "Le t te r s wr i t ten to myse l f " : I wro te them downbecause , w i thou t do ing so , I f ound myse l f r epea t ing the same a rgum en tsover and over aga in . When r ead ing what I had wr i t ten , I was no t a lways toosatisfied.

    Fo r one th ing , I f e l t tha t they su f f e r ed f rom a marked verbos i ty . Yet I dono t t r y to condense them (now) , f i r s t ly because tha t wou ld in t roduce ano therde lay and I wou ld l ike to " th ink on" , second ly because ear l ie r exper iencesh a v e m a d e m e a f r a i d o f be i n g m i s u n d e rs t o o d : m a n y a p r o g r a m m e r t e n d s t osee h is (sometim es ra the r specific) d if f icult ies as the core of the subject a ndas a r esu l t there a r e w ide ly d ivergen t op in ions as to what p rog ramming i sr ea l ly abou t .

    I hope that , despite i ts defects , you will en joy at least par ts of i t . I f theseno tes p rove to be a sou rce o f in sp i ra t ion o r to g ive you a new a pp rec ia t iono f the p rog rammer ' s t r ade , some o f my goa ls w i l l have been r eached .

    P r io r to the i r pub l ica t ion in book fo rm, the "No tes on S t ruc tu red P ro -g ramming" have been d is t r ibu ted p r iva te ly . The in te r es t then shown inthem, fo r wh ich I wou ld l ike to exp ress my g ra t i tude here , has been one o fthe main incen t ives to supp lemen t them wi th some add i t iona l mate r ia l andto make them ava i lab le to a w ider pub l ic . I n par t icu la r I wou ld l ike to thankB o b F l o y d , R a l p h L o n d o n a n d M i k e W o o d g e r f o r t h e i r e n c o u r a g i n gcommen ts and Pe te r Nau r fo r the c r i t ic i sm he exp ressed . F ina l ly I wou ldl ike to exp ress my g ra t i tude to Mrs . E . L . D i jk s t r a -Tucker fo r her k indass i s tance in m y s t rugg les w i th the Eng l i sh language .

    2 . ON OUR INABILITY TO D o MUC HI am faced wi th a bas ic p rob lem o f p resen ta t ion . W hat I am rea l ly concernedabou t i s the comp os i t ion o f la rge p rog rams , the tex t o f wh ich m ay be , say,of the same s ize as the whole text o f th is chapte r . Also I have to include

    1

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    13/234

    E. W. DIJKSTRA

    e x a m p le s t o i l l u s t r a t e t h e v a r io u s t e c h n iq u e s . Fo r p ra c t i c a l r e a so n s , t h ed e m o n s t r a t i o n p r o g r a m s m u s t b e s m a l l , m a n y t i m e s s m a l l e r t h a n t h e " l i f e -s i z e p r o g r a m s " I h a v e i n m i n d . M y b a s i c p r o b l e m i s t h a t p r e c i s e l y t h i sd i f f e re n c e i n sc a l e i s o n e o f t h e m a jo r so u rc e s o f o u r d i f f i c u l t i e s i n p ro -g r a m m i n g t

    I t wo u ld b e v e ry n ic e i f I c o u ld i l l u s t r a t e t h e v a r io u s t e c h n iq u e s wi ths m a l l d e m o n s t r a t i o n p r o g r a m s a n d c o u l d c o n c lu d e w i t h " . . . a n d w h e n f a c e dw i t h a p r o g r a m a t h o u s a n d t i m e s a s l a r g e , y o u c o m p o s e i t i n t h e s a m e w a y . "T h i s c o m m o n e d u c a t i o n a l d e v i c e , h o w e v e r , w o u l d b e s e l f - de f e a t in g a s o n e o fm y c e n t r a l t h e m e s wi l l b e t h a t a n y two th in g s t h a t d i f f e r i n so m e re sp e c t b y af a c t o r o f a l r e a d y a h u n d r e d o r m o r e , a r e u t t e r ly i n c o m p a r a b l e .

    H i s t o r y h a s s h o w n t h a t t h i s t r u t h i s v e r y h a r d t o b e l i ev e . A p p a r e n t l y w e a r eto o m u c h t r a in e d to d i s r e g a rd d i f f e re n c e s i n sc a l e , t o t r e a t t h e m a s "g ra d u a ld i f f e re n c e s t h a t a r e n o t e s se n t i a l " . We t e l l o u r se lv e s t h a t wh a t we c a n d o o n c e ,we c a n a l so d o twic e a n d b y in d u c t io n we fo oI o u r se lv e s i n to b e l i e v in g th a t w ec a n d o i t a s m a n y t im e s a s n e e d e d , b u t t h i s i s j u s t n o t t ru e r A fa c to r o f at h o u s a n d i s a l r e a d y f a r b e y o n d o u r p o w e r s o f im a g i n a t i o n t

    Le t m e g iv e y o u two e x a m p le s t o ru b t h i s i n . A o n e -y e a r o ld c h i ld wi l lc ra w l o n a l l fo u r s w i th a sp e e d o f , s a y , o n e m i l e p e r h o u r . Bu t a sp e e d o f ath o u sa n d m i l e s p e r h o u r i s t h a t o f a su p e r so n ic j e t. Co n s id e re d a s o b j e c t sw i t h m o v i n g a b i l i t y th e c h i ld a n d t h e j e t a r e i n c o m p a r a b l e , f o r w h a t e v e r o n ec a n d o th e o th e r c a n n o t a n d v i c e v e r sa . Al so : o n e c a n c lo se o n e ' s e y e s a n dim a g in e h o w i t f e e l s t o b e s t a n d in g in a n o p e n p l a c e , a p ra i r i e o r a se a sh o re ,wh i l e f a r a wa y a b ig , r e in l e s s h o r se i s a p p ro a c h in g a t a g a l l o p , o n e c a n "se e "i t a p p r o a c h i n g a n d p a s s in g . T o d o t h e s a m e w i t h a p h a l a n x o f a t h o u s a n d o ft h e s e b ig b e a s t s is m e n t a l l y im p o s s i b le " y o u r h e a r t w o u l d m i s s a n u m b e r o fb e a t s b y p u re p a n i c , i f y o u c o u ld !

    To c o m p l i c a t e m a t t e r s s t i l l fu r th e r , p ro b l e m s o f s i z e d o n o t o n ly c a u se m ep r o b l e m s o f p r e s e n t a t i o n , b u t t h e y l i e a t t h e h e a r t o f t h e s u b j e c t : w i d e s p r e a du n d e re s t im a t io n o f t h e sp e c i f i c d i f f i c u l t i e s o f s i z e se e m s o n e o f t h e m a jo ru n d e r ly in g c a u se s o f t h e c u r re n t so f twa re f a i l u re . To a l l t h is I c a n se e o n lyo n e a n swe r , v i z . t o t r e a t p ro b l e m s o f s i z e a s e x p l i c i t l y a s p o ss ib l e . He n c e t h et i t le of th is sec t ion .

    T o s t a r t w i th , w e h a v e th e " s i z e " o f th e c o m p u t a t i o n , i .e . th e a m o u n t o fi n f o r m a t i o n a n d t h e n u m b e r o f o p e r a t i o n s i n v o l v e d i n i t . I t i s e s s e n t i a l t h a tt h i s s i ze i s la rg e , fo r i f i t we re r e a l l y sm a l l , i t wo u ld b e e a s i e r n o t t o u se t h ec o m p u t e r a t a l l a n d t o d o i t b y h a n d . T h e a u t o m a t i c c o m p u t e r o w e s it s ri g h tto e x i s t , i t s u se fu ln e ss , p re c i se ly t o i t s a b i l i t y t o p e r fo rm l a rg e c o m p u ta t i o n sw h e r e w e h u m a n s c a n n o t . W e w a n t t h e c o m p u t e r t o d o w h a t w e c o u l d n e v e rd o o u r s e l v e s a n d t h e p o w e r o f p r e s e n t - d a y m a c h i n e r y i s su c h t h a t e v e n s m a l lc o m p u t a t i o n s a r e b y t h e i r v e r y s i z e a l r e a d y f a r b e y o n d t h e p o w e r s o f o u ru n a i d e d i m a g i n a t i o n .

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    14/234

    NOTES ON STRUCTU RED PROGRAMMING 3Y e t w e m u s t o r g a n i s e t h e c o m p u t a t i o n s i n s u c h a w a y t h a t o u r l i m i t e d

    powers a re s u f f i c i en t to gua ran tee tha t the computa t ion wi l l e s t ab l i s h thedes i red e ffect . T h i s o rgan i s ing inc ludes the comp os i t ion o f the p ro gram andhe re we a re faced wi th the nex t p r ob lem of si ze , viz. the l eng th o f the p ro gramtex t , and we s hou ld g ive th i s p rob lem a l s o exp l i c i t r ecogn i t ion . We s hou ldrema in aw are o f the fac t tha t the ex ten t to which we can read o r wr i t e a t ex ti s ve ry much depen den t on i t s si ze . In my coun t ry the en t r i e s in the t e l ephoned i rec to ry a re g rouped by town or v i l l age and wi th in each s uch g roup thes ubs c r ibe rs a re l i s t ed by nam e in a lphabe t i ca l o rde r . I m ys e l f l ive in a s ma l lv i l l age and g iven a t e l ephone number I have on ly to s can a few co lumns tof ind ou t to whom the t e l ephone number be longs , bu t to do the s ame in a l a rgec i ty would be a ma jo r da ta p roces s ing t a s k !

    I t i s in the s ame mood tha t I s hou ld l ike to d raw the reade r ' s a t t en t ion tothe fac t tha t " c la r i ty" has p ronounced quan t i t a t ive a s pec t s , a fac t manymathema t ic ians , cu r ious ly enough , s eem to be unaware o f . A theorem s ta t ingthe va l id i ty o f a conc lus ion when t en pages fu l l o f cond i t ions a re s a t i sf i ed i sha rd ly a conven ien t too l , a s a l l cond i t ions have to be ve r i f i ed wheneve r thet h e o r e m i s a p p e a l e d t o . I n E u c l i d e a n g e o m e t r y , P y t h a g o r a s ' T h e o r e m h o l d sfo r any th ree po in t s A , B and C s uch tha t th rough A and C a s t ra igh t l inec a n b e d r a w n o r t h o g o n a l t o a s t r a i g h t l i n e t h r o u g h B a n d C . H o w m a n ym a t h e m a t i c i a n s a p p r e c i a te t h a t t h e t h e o r e m r e m a i n s a p p l i ca b l e w h e n s o m e o ra l l o f the po in t s A , B and C co inc ide? Ye t th i s s eems la rge ly re s pons ib le fo rt h e c o n v e n i e n c e w i t h w h i c h P y t h a g o r a s ' T h e o r e m c a n b e u s e d .

    Summar iz ing : a s a s low-wi t t ed human be ing I have a ve ry s ma l l head and Ihad be t t e r l ea rn to l ive wi th i t and to re s pec t m y l imi ta t ions an d g ive them fu l lc red i t , r a the r than to t ry to ignore them, fo r the l a t t e r va in e f fo r t wi l l bepun i s hed by fa i lu re .

    3. O N THE RELIABILITY OF M ECHANISMSB e i ng a p r o g r a m m e r b y t r a d e , p r o g r a m s a r e w h a t I a m t a l k i n g a b o u t a n d t h et rue s ub jec t o f th is s ec t ion rea l ly i s the re l i ab i li ty o f p rogram s . T h a t , neve r -the le s s , I have ment ioned "mechan i s ms " in i t s t i t l e i s becaus e I rega rdpro gram s a s s pec if i c ins tances o f mechan i s ms , and th a t I wan ted to expres s,a t l e a st once , my s t ron g feel ing tha t m any o f my cons ide ra t ion s conce rn ings of tware a re , muta t i s mutand i s , ju s t a s re levan t fo r ha rdware des ign .

    P r e s e n t - d a y c o m p u t e r s a r e a m a z i n g p i e ce s o f e q u i p m e n t , b u t m o s t a m a z i n gof a ll a re the unce r ta in g roun ds on acc oun t o f which we a t t ach any va l id i ty tothe i r ou tpu t . I t s t a r ts a l ready wi th our be l i e f tha t the ha rdw are func t ionsp r o p e r l y .

    L e t us re s t r i c t , fo r a moment , our a t t en t ion to the ha rdware and l e t uswo nde r to wha t ex ten t o ne can conv ince ones e l f o f i ts be ing p rope r ly con-

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    15/234

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    16/234

    NOTES ON STRUCTURED PROGRAM MING 5

    From now onwards the type of mechanisms we are going to deal with areprograms. (In many respects, programs are mechanisms much easier to dealwith than circuitry, which is really an analogue device and subject to we ar andtear.) And also with programs it is fairly hopeless to establish the correctnessbeyond even the mildest doubt by testing, without taking their structure intoaccount. In other words, we remark that the extent to which the programcorrectness can be established is not purely a function of the program'sexternal specifications and behaviour but depends critically upon its internalstructure.

    Recalling that our true concern is with really large programs, we observe asan aside that the size itself requires a high confidence level for the individualprogram components. If the chance of correctness of an individual compo nentequals p, the chance of correctness of a whole program, composed of N suchcomponents, is something like

    p=pN.As N will be very large, p should be very, very close to 1 if we desire P todiffer significantly from zero!

    When we now take the position that it is not only the programmer's task toproduce a correct program but also to demonstrate its correctness in a con-vincing manner, then the above remarks have a profound influence on theprogrammer's activity: the object he has to produce must be usefullystructured.

    The remaining part of this monograph will mainly be an exploration ofwhat program structure can be used to good advantage. In what follows itwill become apparent that program correctness is not my only concern,program adaptability or manageability will be another. This stress on programmanageability is my deliberate choice, a choice that, therefore, I should liketo justify.

    While in the past the growth in power of the generally available equipmenthas mit igat edth e urgency of the efficiency requirements, this very same growthhas created its new difficulties. Once one has a powerful machine at one'sdisposal one tries to use it and the size of the problems one tackles adjustsitself to the scope of the equipment: no one thinks about programming analgorithm that would take twenty years to execute. With processing powerincreased by a factor of a thousand over the last ten to fifteen years, Man hasbecome considerably more ambitious in selecting problems that now shouldbe "technically feasible". Size, complexity and sophistication of programsone should like to make have exploded and over the past years it has becomepatently clear that on the whole our programming ability has not kept pacewith these exploding demands made on it.

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    17/234

    6 E. W. DIJKSTRAT he po wer o f ava i l ab le equ ipm ent wi l l con t inue to g row" w e can expec t

    manufac tu re rs to deve lop s t i l l f a s t e r mach ines and even wi thou t tha t deve lop-men t we s ha l l w i tnes s tha t the type o f mach ine tha t i s p re s en t ly cons ide red a sexcep t iona l ly fa s t w i ll become m ore and m ore com mo n. T he th ings we s hou ldl ike to do wi th thes e mach ines wi l l g row in p ropor t ion and i t i s on th i se x t r a p o l a t i o n t h a t I h a v e f o r m e d m y p i c tu r e o f t h e p r o g r a m m e r ' s t a s k .

    My conc lus ion i s tha t i t i s becoming mos t u rgen t to s top to cons ide rp r o g r a m m i n g p r i m a r i l y as t he m i n i m i z a t i o n o f a c o s t /p e r f o r m a n c e r a t io . W es h o u l d r e c o g n is e t h a t a l r e a d y n o w p r o g r a m m i n g i s m u c h m o r e a n i n t e ll e c tu a lcha l l enge : the a r t o f p ro gram mi ng i s the a r t o f o rgan i s ing complex i ty , o fmas te r ing mul t i tude and avo id ing i t s ba s ta rd chaos a s e f fec t ive ly a s pos s ib le .

    My re fus a l to rega rd e f f i c i ency cons ide ra t ions a s the p rogrammer ' s p r imeconce rn i s no t mean t to imply tha t I d i s rega rd them. On the con t ra ry ,e f fi c iency cons ide ra t ions a re recogn i s ed a s on e o f the ma in incen t ives tomodi fy ing a log ica l ly co r rec t p rogram. My po in t , howeve r , i s tha t we canon ly a f fo rd to op t imis e (wha teve r tha t may be ) p rov ided tha t the p rogramrema ins s u f fi c ien t ly m anageab le .

    L e t me end th i s s ec t ion wi th a f inal a s ide on the s ign if i cance o f com pute rs .Compute rs a re ex t reme ly f l ex ib le and powerfu l too l s and many fee l tha t the i rapp l i ca t ion i s chang ing the face o f the ea r th . I wou ld v en tu re the op in ion th a ta s long a s we rega rd them pr imar i ly a s too l s , we migh t g ros s ly unde re s t ima tetheir s ignif icance. Their inf luence as tools might turn out to be but a r ippleon the s u r face o f ou r cu l tu re , whe reas I expec t them to have a mu ch m orepro f oun d in f luence in the i r capac i ty o f in tel l ec tua l cha l lenge !Corollary o f the firs t part of this section:

    Pro gra m te s t ing can be us ed to s how the p re s ence o f bugs, bu t neve r tos how the i r abs ence !

    4 . ON OU R ME NTAL AIDSIn the p rev ious s ec t ion we have s t a ted tha t the p rogrammer ' s du ty i s to makeh i s p r o d u c t " u s e fu l l y s t r u c tu r e d " a n d w e m e n t i o n e d t h e p r o g r a m s t r u c tu r e i nc o n n e c t i o n w i t h a c o n v i n c i n g d e m o n s t r a t i o n o f th e c o r r e c tn e s s o f t h ep r o g r a m .B u t h o w d o w e c o n v i n c e ? A n d h o w d o w e c o n v i n c e o u r s e l v e s ? W h a t a r ethe typ ica l pa t t e rns o f though t enab l ing ours e lves to unde rs ta nd? I t is to ab roa d s u rvey o f s uch ques t ions tha t the cu r re n t s ec t ion i s devo ted . I t is wr i t t enwi th my s ince re s t apo log ie s to the p ro fe s s iona l ps ycho log i s t , becaus e i t w i l lbe amateurishly superf ic ia l . Yet I hope (and t rus t) that i t wil l be suff ic ient tog ive us a ya rds t i ck by which to meas ure the us e fu lnes s o f a p ropos eds t ruc tu r ing .

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    18/234

    NOTES ON STRUCTURED PROGRAMMING 7

    A m o n g t h e m e n t a l a i d s a v ai l a b le t o u n d e r s t a n d a p r o g r a m ( o r a p r o o f o f i tsc o r r e c t n e s s ) t h e r e a r e t h r e e t h a t I s h o u l d l i k e t o m e n t i o n e x p l i c i t l y :

    ( 1 ) E n u m e r a t i o n( 2) M a t h e m a t i c a l i n d u c t io n( 3 ) A b s t r a c t i o n .

    4.1 . ON ENUMERATIONI r e g a r d a s a n a p p e a l t o e n u m e r a t i o n t h e e f f o r t to v e r i fy a p r o p e r t y o f t h ec o m p u t a t i o n s t h a t c a n b e e v o k e d b y a n e n u m e r a t e d s e t o f s t a t e m e n t s p e r-f o r m e d i n s e q u e n c e , i n c l u d i n g c o n d i t i o n a l c l a u s e s d i s t i n g u i s h i n g b e t w e e n t w oo r m o r e c a s e s . L e t m e g i ve a s im p l e e x a m p l e o f w h a t I c a l l " e n u m e r a t i v er e a s o n i n g " .

    I t is a s k e d t o e s t a b l i s h t h a t t h e s u c c e s s i v e e x e c u t i o n o f th e f o l l o w i n g t w os t a t e m e n t s

    " d d : = d d / 2 ;ifdd~< r d o r : = r - d d "

    o p e r a t i n g o n t h e v a r i a b l e s " r " a n d " d d " l e a v e s t h e r e l a t i o n s0 ~< r < d d (1 )

    i n v a r i a n t . O n e j u s t " f o l l o w s " t h e l i t tl e p i e c e o f p r o g r a m a s s u m i n g t h a t ( 1 ) i ss a t is f i ed t o s t a r t w i t h . A f t e r t h e e x e c u t i o n o f t h e f ir s t s t a t e m e n t , w h i c h h a l v e st h e v a l u e o f d d, b u t l e a v e s r u n c h a n g e d , t h e r e l a t i o n s

    0 ~< r < 2* dd (2 )w i l l h o l d . N o w w e d i s t i n g u i s h t w o m u t u a l l y e x c l u s i v e c a s e s .

    (1 ) dd ~< r. T o g e t h e r w i t h ( 2 ) t h i s l e a d s t o t h e r e l a t i o n sd d ~< r < 2* dd ; (3)

    I n t h i s c a s e t h e s t a t e m e n t f o l l o w i n g d o w i l t b e e x e c u t e d , o r d e r i n g a d e c r e a s eo f r by dd, s o t h a t f r o m ( 3 ) i t f o l l o w s t h a t e v e n t u a l l y

    O ~ < r < d d ,i . e . (1) wi l l be sa t i s f ied.

    (2) non dd ~< r (i .e. d d > r ). I n t h i s c a s e t h e s t a t e m e n t f o l l o w i n g d o w i l l b es k i p p e d a n d t h e r e f o r e a l s o r h a s i ts fi n a l v a l u e . I n t h i s c a se " d d > r " t o g e t h e rw i t h ( 2 ) , w h i c h i s v a l id a f t e r t h e e x e c u t i o n o f t h e f ir s t s t a t e m e n t l e a d si m m e d i a t e l y t o

    O ~< r < d dso t ha t a l so i n t he s econd case (1 ) w i l l be s a t i s f i ed .

    T h u s w e h a v e c o m p l e t e d o u r p r o o f o f t h e i n v a r i an c e o f r el a t io n s ( 1 ), w eh a v e a l s o c o m p l e t e d o u r e x a m p l e o f e n u m e r a t i v e r e a s o n i n g , c o n d i t i o n a lc l a u s e s i n c l u d e d .

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    19/234

    8 E. W. DIJKSTRA4.2 . ON MATHEMATICAL NDUCTIONI h a v e m e n t i o n e d m a t h e m a t i c a l i n d u c t i o n e x p l i c i t l y b e c a u s e i t i s t h e o n l yp a t t e r n o f r e a s o n i n g t h a t I a m a w a r e o f t h a t e v e n t u a l ly e n a b l e s u s t o c o p ew i t h l oops ( s uch a s c an be exp r e s s ed by r epe t i t i on c l aus e s ) and r ecu r s i vep r ocedur e s . I s hou l d l i ke t o g i ve an exam pl e .

    Le t u s cons i de r t he s equence o f va lue sdo, d l , d 2 , d 3 , . . . . . (1 )

    g i ven byf o r i = 0 d i = D (2a)fo r i > 0 d~ = f ( d ~ _ 1) (2b)w he r e D i s a g i ven va l ue and f a g i ven ( com pu t ab l e ) f unc t i on . I t is a s ked t om a k e t h e v a l u e o f t h e v a r i a b l e " d " e q u a l t o t h e f i rs t v a l u e dk i n t he s equencet ha t s a t i s f i e s a g i ven ( com pu t ab l e ) cond i t i on " p r op ' . I t i s g i ven t ha t s uch ava l ue ex i s t s f o r f i n it e k . A m or e f o r m a l de f i n i t i on o f t he r eq u i r em en t i s t oe s t ab l i s h t he r e l a t i on

    d = dk (3 )w he r e k i s g i ven by t he ( t r u t h o f t he ) exp r e s s i ons

    p r o p (dk ) (4 )a n d n o n pr op (d3 for a l l i sa t i s fy ing 0 ~< i < k (5) .

    W e n o w c o n s i d e r t h e f o l l o w i n g p r o g r a m p a r t :" d : = D ;

    w h i l e n o n p r o p ( d ) d o d : = f ( d ) " (6 )i n w h i ch t he f i r s t l i ne rep r e s en t s t he i n i t i a l i s a ti on and t he s econ d one t he l oo p ,c o n t r o l l e d b y t h e ( h o p e f u l l y s e l f - e x p l a n a t o r y ) r e p e t i t i o n c l a u s e w h i l e . . . d o .( I n t e r m s o f t h e c o n d i t i o n a l c l a u s e i f . . . d o , u se d i n o u r p r e v i o u s e x a m p l e , am o r e f o r m a l de f i n i t i on o f t he s em an t i c s o f t he r epe t i t i on c l aus e i s by s t a t i ngt h a t

    " w h i l e B d o S "i s s em an t i ca l l y equ i va l en t w i t h

    " i f B d ob e g i n S ; w h i l e B d o S e n d "

    exp r e s s i ng t ha t " non B " i s t he neces s a r y and s u f f i c i en t cond i t i on f o r t her e p e t i t io n t o t e r m i n a t e . )

    C a l l i ng i n t he cons t r uc t i on " w h i l e B d o S " t h e s t a t e m e n t S " t h e r e p e a t e ds t a t e m e n t " w e s h a l l p r o v e t h a t i n p r o g r a m ( 6 ) :

    a f t e r t he n t h execu t i on o f t he r epea t ed s t a t em en t w i l l ho l d ( f o r n > ~ 0 )d = d~ (7a)

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    20/234

    N O T ES O N S T R U C T U R E D P R O G R A M M I N G 9a n d n o n pr op (d i ) for a l l i sa t i s fy in g 0 ~< i < n . (7b)

    T h e a b o v e s t a t e m e n t h o l d s f o r n = 0 (b y e n u m e r a t i v e r e a s o n i n g ) ; w e h a v et o p r o v e ( b y e n u m e r a t i v e r e a s o n i n g ) t h a t w h e n i t h o l d s f o r n = N ( N ~ 0 ),i t w i l l a l s o h o l d f o r n = N + 1 .

    A f t e r t h e N t h e x e c u t i o n o f t h e r e p e a te d s t a t e m e n t r e l a t i o n s ( 7 a ) a n d ( 7 b)a r e s a t is f i e d f o r n = N . Fo r t h e N + 1s t e x e c u t i o n t o t a k e p l a c e , t h e n e c e s s a r ya n d s u f f i c i e n t c o n d i t i o n i s t h e t r u t h o f

    n o n p r o p ( d )wh i c h , t h a n k s t o ( 7 a ) f o r n = N ( i .e . d = d N) m e a n s

    n o n p r o p ( d N )l e a d i n g t o c o n d i t i o n ( 7 b ) b e i n g s a t is f i ed f o r n = N + 1 . F u r t h e r m o r e ,d = d N a n d ( 2 b ) l e a d s t o

    f ( d ) = d N + 1s o t h a t t h e n e t e f fe c t o f th e N + 1 s t e x e c u t i o n o f t h e r e p e a t e d s t a t e m e n t

    " d : = f ( d ) "e s t a b l i s h e d t h e r e l a t i o n

    d = d N + li .e . r e l a t i o n ( 7 a ) f o r N = N + 1 a n d t h u s t h e i n d u c t i o n s t e p ( 7) h a s b e e np r o v e d .N o w w e s h a l l s h o w t h a t t h e r e p e t i t i o n t e r m i n a t e s a f t e r t h e k t h e x e c u t i o no f t he r e p e a t e d s t a t e m e n t . T h e n t h e x e c u t io n c a n n o t t a k e p l a c e f o r n > kf o r ( o n a c c o u n t o f 7 b ) t h i s w o u l d i m p l y

    n o n p r o p ( d k )t h e r e b y v i o l a t i n g ( 4 ) . W h e n t h e r e p e t i t i o n t e r m i n a t e s a f t e r t h e n t h e x e c u t i o no f t h e r e p e a t e d s t a t e m e n t , t h e n e c e s s a r y a n d s u f fi c ie n t c o n d i t i o n f o r t e r m i n a -t i o n , v i z .

    n o n ( n o n p r o p ( d ) )b e c o m e s , t h a n k s t o ( 7 a )

    pr op (dn). (8)T h i s e x c l u d e s t e r m i n a t i o n f o r n < k , a s t h i s w o u l d v i o l a t e ( 5) . A s a r e s u l t th er e p e t i t i o n w i l l t e r m i n a t e w i t h n = k , s o t h a t ( 3 ) fo l l o w s f r o m ( 7 a ), ( 4) fo l l o w sf r o m ( 8 ) a n d ( 5 ) f o l l o w s f r o m ( T b ) . W h i c h t e r m i n a t e s o u r p r o o f .

    B e f o r e t u r n i n g o u r a t t e n t i o n a w a y f r o m t h is e x a m p l e i l l u s tr a t in g t h e u s e o fm a t h e m a t i c a l i n d u c t i o n a s a p a t t e r n o f r e a s o n in g , I s h o u l d l i k e t o a d d s o m er e m a r k s , b e c a u s e I h a v e t h e u n e a s y f e el in g t h a t b y n o w s o m e o f m y r e a d e r s( in p a r t i c u l a r e x p e r i e n c e d a n d c o m p e t e n t p r o g r a m m e r s ) w i ll b e t e r ri b l yi r r i t a t e d , v i z . t h o s e r e a d e r s f o r w h o m p r o g r a m ( 6 ) i s s o o b v i o u s l y c o r r e c tt h a t t h e y w o n d e r w h a t a l l t h e fu s s i s a b o u t : " W h y h i s p o m p o u s r e s t a t e m e n t

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    21/234

    10 E. W. DIJKSTRAo f t h e p r o b l e m , a s in ( 3 ), (4 ) a n d ( 5) , b e c a u s e a n y o n e k n o w s w h a t is m e a n tby t he f i r s t va l ue i n t he s equence , s a t i s f y i ng a cond i t i on? C e r t a i n l y he doesn o t e x p e c t u s , w h o h a v e w o r k t o d o , t o s u p p l y s u c h l e n g t h y p r o o f s , w i t h a l lt h e m a t h e m a t i c a l d r e s s i n g , w h e n e v e r w e u s e s u c h a s i m p l e l o o p a s t h a t ? "Et c .

    T o t e l l t h e h o n e s t t r u t h : t h e p o m p a n d l e n g t h o f t h e a b o v e p r o o f i n fu r i a tem e a s w e l l ! B u t a t p r e s e n t I c ann o t d o m uch be t t e r i f I r e a ll y t r y to p r ove t heco r r ec t ne s s o f t h i s p r o g r am . B u t it s om e t i m es f il ls m e w i t h t he s am e k i nd o fange r a s yea r s ago t he c r azy p r oo f s o f t he f i r s t s i m p l e t heo r em s i n p l aneg e o m e t r y d i d , p r o v i n g t h i n g s o f t h e s a m e d e g r e e o f " o b v i o u s n e s s " a s E u c l i d 'sa x i o m s t h e m s e lv e s .

    O f cou r s e I w ou l d no t da r e t o s ugges t ( a t l e a s t a t p r e s en t ! ) t ha t i t i s t hep r o g r a m m e r ' s d u t y t o s u p p l y s u c h a p r o o f w h e n e v e r h e w r i t e s a s i m p l e l o o pi n h i s p r o g r am . I f so , he cou l d nev e r w r i t e a p r o g r a m o f any s i ze a t a l l! I tw o u l d b e a s i m p r a c t i c a l a s r e d u c i n g e a c h p r o o f i n p l a n e g e o m e t r y e x p l i c i t l ya n d i n e x t e n s o t o E u c l i d ' s a x i o m s . ( C f . S e c t i o n " O n o u r i n a b i l i t y t o d om u c h . " )

    M y m o r a l i s t h r e e fo l d . F i r s t ly , w h e n a p r o g r a m m e r c o n s i d e r s a c o n s t r u c -t i on l i ke ( 6 ) a s obv i ous l y co r r ec t , he can do s o becaus e he i s f am i l i a r w i t h t hec o n s t r u c t i o n . I p r e f e r t o r e g a r d h i s b e h a v i o u r a s a n u n c o n s c i o u s a p p e a l t o at h e o r e m h e knows, a l t h o u g h p e r h a p s h e h a s n e v e r b o t h e r e d t o f o r m u l a t e i t ;and once i n h i s l if e he ha s conv i nced h i m s e l f o f it s t r u t h , a l t ho ugh he ha sp r o b a b l y f o r g o t t e n i n w h i c h w a y h e d i d i t a n d a l t h o u g h t h e w a y w a s( p r o b a b l y ) u n f i t f o r p r i n t . B u t w e c o u l d c a l l o u r a s s e r t i o n s a b o u t p r o g r a m( 6 ) , s a y , " T h e L i n e a r S e a r c h T h e o r e m " a n d k n o w i n g s u c h a n a m e i t i s m u c he a s ie r ( a n d m o r e n a t u r a l ) t o a p p e a l t o i t co n s c i o u s ly .

    S e c o n d l y , t o t h e b e s t o f m y k n o w l e d g e , t h e r e i s n o s e t o f t h e o r e m s o f th et ype i l l u s t r a t ed above , w hos e u s e f u l ne s s ha s been gene r a l l y accep t ed . B u t w es h o u l d n o t b e a m a z e d a b o u t t h a t , f o r t h e a b s e n c e o f s u c h a se t o f t h e o r e m s i s ad i re c t co n s e q u e n ce o f t h e f a c t t h a t t h e t y p e o f o b j e c t ~ i . e , p r o g r a m s ~ h a s n o ts e t tl e d d o w n . T h e k i n d o f o b j e c t th e p r o g r a m m e r i s d e a li n g w i t h , vi z.p r og r am s , i s m uch l e s s w e l l - e s t ab l i s hed t han t he k i nd o f ob j ec t t ha t i s dea l tw i t h i n p la n e g e o m e t r y . In t h e m e a n t i m e t h e in t u i ti v e ly c o m p e t e n t p r o g r a m m e ri s p r o b a b l y t h e o n e w h o c o n f i n e s h im s e l f, w h e n e v e r a c c e p t a b l e , t o p r o g r a ms t r u c t u r e s w i t h w h i c h h e i s v e r y f a m i l i a r , w h i l e b e c o m i n g v e r y a l e r t a n dc a r e f u l w h e n e v e r h e c o n s t r u c t s s o m e t h i n g u n u s u a l ( f o r h i m ) . F o r a n e s t a b -l is h e d s t y le o f p r o g r a m m i n g , h o w e v e r , i t m i g h t b e a u s e f u l a c t iv i t y t o l o o kf o r a b o d y o f th e o r e m s p e r t i n e n t t o s u c h p r o g r a m s .

    T h i r d l y , t h e l e n g t h o f t h e p r o o f w e n e e d e d i n o u r l a s t e x a m p l e i s a w a r n i n gt h a t s h o u l d n o t b e i g n o r e d . T h e r e i s o f c o u r s e t h e p o s s i b il i ty t h a t a b e t t e rm a t h e m a t i c i a n w i ll d o a m u c h s h o r t e r a n d m o r e e l e g a n t j o b t h a n I h a v e d o n e .P e r s o n a l l y I a m i n c l i n e d t o c o n c l u d e f r o m t h i s l e n g t h t h a t p r o g r a m m i n g i s

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    22/234

    NOTES ON STRUCTURED PROGRAMMING 11m o r e d i f f i c u l t t h a n i s c o m m o n l y a s s u m e d : l e t u s b e h o n e s t l y h u m b l e a n din t e rp re t t h e l e n g th o f t h e p ro o f a s a n u rg e n t a d v ic e t o r e s t r i c t o u r se lv e s t os im p le s t ru c tu re s wh e n e v e r p o ss ib l e a n d to a v o id i n a l l i n t e l l e c tu a l m o d e s ty" c l e v e r c o n s t r u c t i o n s " l i k e t h e p l a g u e .4.3. ON ABSTRACTIONAt th i s s t a g e I f i n d it h a rd t o b e v e ry e x p l i c it a b o u t t h e ro l e o f a b s t r a c t i o n ,p a r t l y b e c a u s e i t p e r m e a t e s t h e w h o l e s u b je c t . C o n s i d e r a n a l g o r i t h m a n d a l lp o s s ib l e c o m p u t a t i o n s i t c a n e v o k e : s t a r t i n g f r o m t h e c o m p u t a t i o n s t h ea lg o r i t h m i s wh a t r e m a in s wh e n o n e a b s t r a c t s f ro m th e sp e c i f i c v a lu e sm a n i p u l a t e d t h i s t im e . T h e c o n c e p t o f " a v a r i a b l e " r e p r e s e n ts a n a b s t r a c t i o nf ro m i t s c u r re n t v a lu e . I t h a s b e e n r e m a rk e d to m e ( t o m y g re a t r e g re t . Ic a n n o t r e m e m b e r b y w h o m a n d s o I a m u n a b l e t o g i v e c r e d i t w h e r e i t s e e m sd u e ) t h a t o n c e a p e r so n h a s u n d e r s to o d th e wa y in wh ic h v a r i a b l e s a re u se d inp r o g r a m m i n g , h e h a s u n d e r s t o o d t h e q u i n te s s e n c e o f p r o g r a m m i n g . W e c a nf in d a c o n f i r m a t i o n f o r t h is r e m a r k w h e n w e r e t u r n t o o u r u s e o f m a t h e m a t i c a li n d u c t io n wi th r e g a rd t o t h e r e p e t i t i o n : o n t h e o n e h a n d i t i s b y a b s t r a c t i o nt h a t t h e c o n c e p t s a r e i n t r o d u c e d i n t e r m s o f w h i c h th e i n d u c t i o n s t e p c a n b efo rm u la t e d ; o n t h e o th e r h a n d i t i s t h e r e p e t i t i o n t h a t r e a l l y c a l l s fo r t h ec o n c e p t o f " a v a r i a b l e " . ( W i t h o u t r e p e t i t i o n o n e c a n r e s t r i c t o n e s e lf to"q u a n t i t i e s " t h e v a lu e o f wh ic h h a s t o b e d e f in e d a s m o s t o n c e b u t n e v e r h a sto b e r e d e f in e d a s i n t h e c a se o f a v a r i a b l e . )

    Th e re i s a l so a n a b s t r a c t i o n i n v o lv e d in n a m in g a n o p e ra t i o n a n d u s in g i to n a c c o u n t o f " w h a t i t d o e s " w h i l e c o m p l e t e l y d i s r e g a r d i n g " h o w i t w o r k s " .( I n t h e s a m e w a y o n e s h o u l d s t a t e t h a t a p r o g r a m m i n g m a n u a l d e s c r i b e s a na b s t r a c t m a c h in e " t h e sp e c i f ic p i e c e o f h a rd wa re d e l i v e re d b y th e m a n u -f a c t u r e r i s n o t h i n g b u t a - - u s u a l l y i m p e r f e c t ! - - m e c h a n i c a l m o d e l o f th i sa b s t r a c t m a c h i n e . ) T h e r e i s a s t r o n g a n a l o g y b e t w e e n u s i n g a n a m e d o p e r a t i o ni n a p r o g r a m r e g a r d l e s s o f " h o w i t w o r k s " a n d u s i n g a t h e o r e m r e g a r d l e s so f h o w i t h a s b e e n p ro v e d . Ev e n i f it s p ro o f i s h ig h ly i n t r i c a t e , i t m a y b e av e r y c o n v e n i e n t t h e o r e m t o u s e !

    He re , a g a in , I r e f e r t o o u r i n a b i l i t y t o d o m u c h . En u m e ra t iv e r e a so n in g i sa l l r i g h t a s f a r a s i t g o e s , b u t a s we a re r a th e r s l o w-wi t t e d i t d o e s n o t g o v e ryf a r . E n u m e r a t i v e r e a s o n i n g i s o n l y a n a d e q u a t e m e n t a l t o o l u n d e r t h e s e v e r eb o u n d a r y c o n d i t i o n t h a t w e u s e i t o n l y v er y m o d e r a t e l y . W e s h o u l d a p p r e c i a t ea b s t r a c t i o n a s o u r m a i n m e n t a l t e c h n i q u e t o r e d u c e t h e d e m a n d s m a d e u p o ne n u m e r a t i v e r e a s o n i n g .

    ( H e r e M i k e W o o d g e r , N a t i o n a l P h y s i c a l L a b o r a t o r y , T e d d i n g t o n , E n g l a n d ,m a d e th e fo l l o win g r e m a rk , wh ic h I i n se r t i n g ra t i t u d e : "Th e re i s a p a ra l l e la n a l o g y b e t w e e n t h e u n a n a l y s e d t e r m s i n w h i c h a n a x i o m o r t h e o r e m i se x p r e s s e d a n d t h e u n a n a l y s e d o p e r a n d s u p o n w h i c h a n a m e d o p e r a t i o n i se x p e c t e d t o a c t . " )

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    23/234

    12 E. W. DIJKSTRA5 . A N EX A MPLE O F A C O R REC TNESS PR O O F

    L e t u s c o n s i d e r t h e f o l l o w i n g p r o g r a m s e c t i o n , w h e r e t h e i n t e g e r c o n s t a n t sa and d s a t i s f y t he r e l a t i ons

    a ~> 0 a n d d > 0 ."integer r , d d ;

    r ' = a ; d d : = d ;while dd ~< r do d d : = 2 * d d ;while d d - ~ d dobegin d d : = d d / 2 ;

    i f d d ~ < r d o r : = r - d dend".

    T o a p p l y t h e L i n e a r S e a r c h T h e o r e m ( s e e S e c t i o n " O n o u r m e n t a l a i d s " ,s u b s e c t i o n " O n m a t h e m a t i c a l i n d u c t i o n " ) w e c o n s id e r t h e s e q u en c e o f v a l u e sg i ven byf o r i = 0 d d ~ = dfor i > 0 d d i = 2 * d d i _ 1f r o m w h i c h dd n = d*2" (1)c a n b e d e r i v e d b y n o r m a l m a t h e m a t i c a l t e c h n i q u e s , w h i c h a l s o t e l l u s t h a t(beca use d > 0) for f in i t e r

    d d k > rw i l l ho l d f o r s om e f i n i t e k , t hus ens u r i ng t ha t t he f i r s t r epe t i t i on t e r m i na t e sw i t h

    d d = d * 2 kSo l v i ng t he r e l a t i on

    d~ = 2 * d i - 1for d~_ 1 g ives

    d i - 1 = d d 2a n d t h e L i n e a r S e a r c h T h e o r e m t h e n t e l l s u s , t h a t t h e s e c o n d r e p e t i t i o n w i l la l s o t e r m i n a t e . ( A s a m a t t e r o f fa c t t h e s e c o n d r e p e a t e d s t a t e m e n t w i ll b eexecu t ed exac t l y the s am e num ber o f ti m es a s t he fi r s t one . )

    A t t he t e r m i na t i o n o f t he f ir s t r epe t i t i on ,d d = d dk

    a n d t h e r e f o r e ,0 ~< r < d d (2 )

    h o l d s . A s s h o w n e a r l i e r ( S e c t i o n " O n o u r m e n t a l a i d s . " , s u b s e c t i o n " O ne n u m e r a t i o n " ) t h e r e p e a t e d s t a t e m e n t o f th e s e c o n d c l a u s e l e av e s t hi s r e l a ti o n

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    24/234

    NOTES ON STRUCTURED PROGRAMMING 13i n v a r i a n t . A f t e r t e r m i n a t i o n ( o n a c c o u n t o f "while d d - d d o " ) w e c a nc o n c l u d e

    d d = dwh ich t o g e th er wi th (2 ) g iv es

    0 ~< r < d (3)F u r t h e r m o r e w e p r o v e t h a t a f t e r t h e i n i t i a l i s a t i o n

    dd _= 0 m o d (d ) (4 )h o ld s ; t h i s fo l l o ws , fo r i n s t an ce , f ro m th e f ac t t h a t t h e p o ss ib l e v a lu es o f d dare (see (1 ))

    d * 2 i f o r O ~< i ~ . k .Ou r n ex t s t ep i s t o v e r i fy , t h a t a f t e r t h e i n i t i a l a s s i g n men t t o r t h e r e l a t i o n

    a ~ r m o d (d ) (5 )h o ld s .

    (1 ) I t h o ld s a f t e r t h e i n i t i a l a s s i g n men t s .(2) Th e r ep ea t ed s t a t em en t o f t h e f ir s t c l au se ( "d d : = 2 * r id " ) ma in t a in s

    th e i n v ar i an ce o f (5 ) an d t h e re fo re t h e wh o le f i rs t r ep e t i t i o n m ain t a in s t h eval id i ty o f (5 ) .

    (3 ) Th e sec o n d r ep ea t ed s t a t em en t co n s i s t s o f two s t a t em en t s . Th e f i r s t("r id : = d d / 2 " ) l eav es (5 ) in v ar i an t , t h e seco n d o n e a l so l eav es (5) i n v ar i an t fo re i t h e r i t l eav es r u n to u ch ed o r i t d ec reases r b y t h e cu r r en t v a lu e o f d d , a no p e ra t i o n wh ich o n a cco u n t o f (4) a lso ma in t a in s t h e v a l i d i t y o f (5 ) . Th ere fo reth e wh o le seco n d r ep ea t ed s t a t emen t l eav es (5 ) i n v ar i an t an d t h e re fo re t h ewh o le r ep e t i t i o n l eav es (5 ) i n v ar i an t . Co mb in in g (3 ) an d (5 ) , t h e f i n a l v a lu etherefore sat i sf ies

    0 ~< r < d a n d a = _ r m o d ( d )i .e . r is t h e smal l es t n o n -n eg a t i v e r em ain d e r o f th e d iv i s i o n o f a b y d .

    R e m a r k 1 . T h e p r o g r a m"integer r , d d , q ;

    r : = a ; d d : = d ; q : = 0 ;while dd

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    25/234

    14 E. W. DIJKSTRAR e m a r k 2 . I n t h e s u b s e c ti o n " O n m a t h e m a t i c a l i n d u c t i o n . " w e h av e p r o v e d

    t h e L i n e a r S e a r c h T h e o r e m . I n t h e p r e v i o u s p r o o f w e h a v e u se d a n o t h e rt h e o r e m a b o u t r e p e t i ti o n s ( a t h e o r e m t h a t , o b v i o u s l y , ca n o n l y b e p r o v e d b yma them a t ica l in duc t ion , bu t the p ro of i s s o simple tha t we l eave i t a s anexe rci s e to the reade r ) , v iz . tha t i f p r io r to en t ry o f a repe t i t ion a ce r t a inre la t ion P ho lds , whos e t r u th i s no t des t roy ed by a s ingle execu t ion o f therepea ted s t a temen t , th en re la t ion P wi ll st il l ho ld a f t e r t e rmina t ion o f therepe t i t ion . T h i s i s a ve ry us e fu l theorem, o f t en a l lowing us to bypas s anexp l ic i t appea l to ma thema t ica l induc t ion . (We can s t a te the theorem a l i t t l emore s ha rp ly ; in the repe t i t ion

    " w h i l e B d o S "one has to s how tha t S i s s uch tha t the t ru th o f

    P a n d Bpr io r to the execu t ion o f S impl ie s the t ru th o f

    Pafter i ts execut ion. )

    R e m a r k 3 . As an exe rc i s e fo r the reade r ( fo r which acknowledgement i sd u e t o J a m e s K i n g , C M U , P i t t s b u r g h , U S A ) , p r o v e t h a t w i t h i n t e g e r A , B ,x , y and z and

    A > 0 and B >t 0a f t e r the execu t ion o f the p rog ram s ec t ion" x : = A ; y : = B ; z : = l ;

    wh ile y - 0 dob e g i n i f o d d ( y ) d o b e g i n y: = y - 1; z: = z * x e n d ;

    y : = y /2 ; x : = x * xend"

    finally z = A s will hold .T h e p r o o f h a s t o s h o w t h a t ( in s p i te o f " y : = y / 2 " ) al l variables keep

    in tege r va lues ; the me thod s hows the inva r iance o fx > 0 a n d y t > 0 a n d A B = z * x y

    6 . ON T HE V AL IDIT Y OF PROOFS VE RSUS T HE V AL IDITY OFIMPLEMENTATIONS

    In the p rev ious s ec t ion I have a s s umed "pe r fec t a r i thme t ic " and in myexpe r ience the va l id i ty o f s uch p roofs o f t en ge t s ques t ioned by peop le whoa rgue th a t in p rac t i ce one neve r h~ i s pe r fec t a r i thme t ic a t ones d i s po s a l :admis s ib le in tege r va lues us ua l ly have an abs o lu te uppe r bound , rea l numbersa re on ly rep re s en ted to a f in i t e accuracy e tc . So wha t i s the va l id i ty o f s uchp r o o f s ?

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    26/234

    NOTES ON STRUCTURED PROGRAMMING 15T he ans wer to th i s ques t ion s eems to be the fo l lowing . I f one p roves the

    cor rec tnes s o f a p rogr am as s um ing an idea l i sed , pe r fec t wor ld , one s ho u ldno t be amaze d i f s om e th ing goes wrong w hen th i s idea l p rogra m ge t s execu tedb y a n " i m p e r f e c t " i m p l e m e n t a t i o n . O b v i o u s l y ! T h e r e f o r e , i f w e w is h to p r o v eprogram cor rec tnes s in a more rea l i s t i c wor ld , the th ing to do i s to acknow-ledge r igh t a t the s t a r t tha t a l l ope ra t ions appea led to in the p rogram ( inpa r t i cu la r a l l a r i thme t ic ope ra t ions ) need no t be pe r fec t , p rov ided we s t a te - -r a t h e r a x i o m a t i c a l l y - - t h e p r o p e r t i e s t h e y h a v e t o s a t i s f y f o r t h e p r o p e rexecu t ion o f the p rogr am , i . e . the p rope r t i e s o n which the co r rec tnes s p r oo fre li e s. ( In the exam ple o f the p rev iou s s ec t ion th i s requ i rem ent i s s implyexact integer ar i thmetic in the range [0 , 2a] . )

    W h e n w r i t i n g a p r o g r a m o p e r a t i n g o n r e a l n u m b e r s w i t h r o u n d e d o p e r a -t ions , one mus t be aware o f the a s s um pt ions one makes , s uch a s

    b > 0 i m p l i e s a + b f > aa * b = b * a- ( a ' b ) = ( - a ) * b0 * x = 00 + x = xl ' x = x e t c . e tc .

    Very often the val idi ty of such re la t ions is essent ia l to the logic of thep r o g r a m . F o r t h e s a k e o f c o m p a t i b il i t y , th e p r o g r a m m e r w o u l d b e w i se t o b ea s u n d e m a n d i n g a s p o s s i b l e , w h e r e a s a g o o d i m p l e m e n t a t i o n s h o u l d s a t i s f yas many rea s onab le requ i rements a s pos s ib le .

    T h i s is t he p l a ce t o c o n f e ss o n e o f m y b l u n d e r s . I n i m p l e m e n t i n g A L G O L 6 0we dec ided tha t " x = y" wou ld de l iver the va lue t rue no t on ly in the ca s e o fexact equal i ty , but a lso when the two values differed only in the leas t s ignif i -can t d ig i t r ep re s en ted , becaus e o the rw is e i t was s o ve ry imp robab le tha t theva lue t rue would eve r be com puted . W e were th ink ing o f conve rg ing i t e ra t ionstha t cou ld os c i l l a t e wi th in round ing accuracy . Whi le we had been gene rous(wi th the bes t o f in ten t ions ! ) in rega rd ing rea l numb ers a s equa l , i t qu ick lyt u r n e d o u t t h a t t h e c h o s e n o p e r a t i o n w a s s o w e a k a s t o b e h a r d l y o f a n y u s ea t a ll . Wha t i t bo il ed dow n to was tha t the e s tab l i shed t ru th o f a = b andb = c d id no t a l low the p ro gra mm er to conc lude the t ru th o f a = c . T hedec i s ion was qu ick ly changed . I t is becaus e o f tha t expe r ience tha t I kno wt h a t t h e p r o g r a m m e r c a n o n l y u s e h i s t o o l b y v i r t u e o f ( a n u m b e r o f ) i t sp rope r t i e s ; conve rs e ly , the p rogrammer mus t be ab le to s t a t e which p rope r t i e she requ ire s . (Us ua l ly p rogr am me rs do n ' t do s o becaus e , fo r l ack o f t rad i t ionas to wha t p rope r t i e s can be t aken fo r g ran ted , th i s wou ld requ i re moreexp l i c itnes s than i s o the rwis e des i rab le . T he p ro l i fe ra t ion o f mach ines w i th

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    27/234

    16 E . W . DIJKSTRAl o u s y f l o a t i ng - p o i n t h a r d w a r e ~ t o g e t h e r w i t h t h e m i s a p p r e h e n s i o n t h a t t h ea u t o m a t i c c o m p u t e r i s p r i m a r i l y th e t o o l o f t h e n u m e r i c a l a n a l y s t ~ h a s d o n em u c h h a r m t o t h e p r o f e s s i o n ! )

    7 . ON UNDERSTANDING PROGRAMSI n m y l i f e I h a v e s e e n m a n y p r o g r a m m i n g c o u r s e s t h a t w e r e e s s e n t i a l l y l i k eth e u su a l k in d o f d r iv in g l e sso n s , i n wh ic h o n e i s t a u g h t h o w to h a n d le a c a rin s t e a d o f h o w to u se a c a r to r e a c h o n e ' s d e s t i n a t i o n .My p o in t i s t h a t a p ro g ra m i s n e v e r a g o a l i n i t se l f ; t h e p u rp o se o f ap r o g r a m i s t o e v o k e c o m p u t a t i o n s a n d t h e p u r p o s e o f t h e c o m p u t a t i o n s i s toe s t a b l i sh a d e s i r e d e f f e c t . Al th o u g h th e p ro g ra m i s t h e f i n a l p ro d u c t m a d e b yt h e p r o g r a m m e r , t h e p o ss i bl e c o m p u t a t i o n s e v o ke d b y i t ~ t h e " m a k i n g " o fw h i c h i s l e ft to t h e m a c h i n e ! ~ a r e t h e t r u e s u b je c t m a t t e r o f h is t ra d e . F o ri n s t a nc e , w h e n e v e r a p r o g r a m m e r s t a te s t h a t h is p r o g r a m i s c o rr e c t , h e r e a l lym a k e s a n a s s e r t i o n a b o u t t h e c o m p u t a t i o n s i t m a y e v o k e .

    Th e f a c t t h a t t h e l a s t s t a g e o f t h e t o t a l a c t i v i t y , v i z. t h e t r a n s i t i o n f ro mth e ( s t a t i c ) p ro g ra m t e x t t o t h e (d y n a m ic ) c o m p u ta t i o n , i s e s se n t i a l l y l e f t t ot h e m a c h i n e i s a n a d d e d c o m p l i c a t i o n . I n a s e n se t h e m a k i n g o f a p r o g r a m i st h e r e f o r e m o r e d i f fi c ul t t h a n t h e m a k i n g o f a m a t h e m a t i c a l t h e o r y : b o t hp r o g r a m a n d t h e o r y a r e s t r u c t u r e d , t i m e l e s s o b j e c t s . B u t w h i l e t h e m a t h e -m a t i c a l t h e o r y m a k e s s e n se a s i t s t a n d s , t h e p r o g r a m o n l y m a k e s s e n s e v ia i tse x e c u t io n .

    In t h e r e m a in in g p a r t o f th i s se c t i o n I sh a ll r e s t r i c t m y s e l f t o p ro g r a m swr i t t e n fo r a se q u e n t i a l m a c h in e , a n d I sh a l l e x p lo re so m e o f t h e c o n se q u e n c e so f o u r d u t y to u s e o u r u n d e r s t a n d i n g o f a p r o g r a m t o m a k e a s s e r t i o n s a b o u tt h e e n s u i n g c o m p u t a t i o n s . I t i s m y ( u n p r o v e n ) c l a i m t h a t t h e e a s e a n dre l i a b i l i t y wi th wh ic h we c a n d o th i s d e p e n d s c r i t i c a l l y u p o n th e s im p l i c i t y o ft h e r e l a t i o n b e t w e e n t h e t w o , i n p a r t i c u l a r u p o n t h e n a t u r e o f s e qu e n c i n gc o n t ro l . In v a g u e t e rm s we m a y s t a t e t h e d e s i r a b i l i t y t h a t t h e s t ru c tu re o ft h e p r o g r a m t e x t re f le c ts t h e s t r u c t u re o f th e c o m p u t a t i o n . O r , i n o t h e r t e r m s ," W h a t c a n w e d o t o s h o r t e n th e c o n c e p t u a l g a p b e t w e e n t h e s ta t i c p r o g r a mt e x t ( s p r e a d o u t i n " t e x t s p a c e " ) a n d t h e c o r r e s p o n d i n g c o m p u t a t i o n s(e v o lv in g in t im e ) ? "

    I t is t h e p u rp o se o f t h e c o m p u ta t i o n t o e s t a b l i sh a c e r t a in d e s i r e d e f f e c t.Wh e n i t s t a r t s a t a d i sc re t e m o m e n t t o i t wi l l b e c o m p le t e d a t a l a t e r d i sc re t em o m e n t t a a n d w e a s s u m e t h a t i t s e f f e c t c a n b e d e s c r i b e d b y c o m p a r i n g " t h es t a t e a t t o " wi th " th e s t a t e a t t~" . I f n o i n t e rm e d ia t e s t a t e s a re t a k e n in toc o n s id e ra t i o n t h e e f f e c t i s r e g a rd e d a s b e in g e s t a b l i sh e d b y a p r im i t i v e a c t i o n .

    W h e n w e d o t a k e a n u m b e r o f i n t e r m e d i a t e s t a t e s i n t o c o n s i d e r a t i o n t h ism e a n s t h a t w e h a v e p a r s e d t h e h a p p e n i n g i n ti m e . W e r e g a r d i t as a s e q u e n t i a lc o m p u t a t i o n , i .e . th e t i m e - s u c c e s si o n o f a n u m b e r o f s u b a c t io n s a n d w e h a v e

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    28/234

    N O T E S O N S T R U C T U R E D P R O G R A M M I N G 17to conv ince ours e lves tha t the cumula t ive e f fec t o f th i s t ime-s ucces s iQn o fs ubac t ions indeed equa l s the des i red ne t ef fec t o f the to ta l com puta t ion .

    T he s imples t ca s e i s a pa rs ing , a decompos i t ion , in to a f ixed number o fs ubac t ions tha t can be enumera ted . In f lowcha r t fo rm th i s can be rep re s en tedas fol lows.

    [ . . . 2 ~ - - - - - - 1III 1 ,

    II i ' ' I!t t I

    !' [ 1 'S n II

    5 1 ; S 2 ; . . . . . ; 5 n

    T he va l id i ty o f th i s decomp os i t ion h as to be e s tab l is hed by enum era t ivereas on ing . In th i s ca s e, s hor ten in g o f the concep tua l gap b e tween p ro grama n d c o m p u t a t i o n c a n b e a c h i ev e d b y r e q u i r i n g t h a t a l i n e a r p ie c e o f p r o g r a mtex t con ta ins nam es o r des c r ip t ions o f the s ubac t ions in the o rde r in whichthey have to tak e place . In ou r earl ier examp le ( invarian ce o f 0 ~< r < d d )

    "dd : = d d / 2 ;i f d d ~ r d o r : = r - d d "

    th i s cond i t ion i s s a ti s fi ed . T he p r im ary deco mp os i t ion o f the comp uta t io n i sin to a t ime-s ucces s ion o f two ac t ions ; in the p rogram tex t we recogn i s e th i ss t ruc tu re

    " h a l v e d d ;r e d u c e r m o d u l o d d " .

    W e are con s ider ing a l l ini t ia l s ta tes sa t is fying 0 ~< r < d d and in a l lcomputa t ions then cons ide red , the g iven pa rs ing in to two s ubac t ions i sapp l i cab le . So fa r , s o good .

    T h e p r o g r a m , h o w e v e r , i s w r i t t e n u n d e r t h e a s s u m p t i o n t h a t " r e d u c e rm o d u l o d d " i s no t a p r imi t ive ac t ion , whi le "dec reas e r by d d " is . Viewing a l lp o s s i b l e h a p p e n i n g s d u r i n g " r e d u c e r m o d u l o d d " i t t hen becomes re levan tto d i s t ingu i s h tha t in s ome cas es "dec reas e r by d d " takes place , while in theo the r ca s es r rema ins unchanged . By wr i t ing

    " i f dd ~< r d o decrease r by d d "

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    29/234

    18 E.W. DIJKSTRAwe h a v e r e p re se n t e d t h a t a t t h e g iv e n l e v el o f d e t a i l t h e a c t i o n " re d u c e rm o d u l o rid" c a n t a k e o n e o f t w o m u t u a l l y e x c lu s iv e f o r m s a n d w e h a v e a l s og i v en t h e c r i te r i o n o n a c c o u n t o f w h i c h t he c h o i c e b e t w e e n t h e m i s m a d e . I fw e r e g a r d " i f dd ~ r d o " a s a c o n d i t i o n a l c l a u s e a t t a c h e d t o " d e c r e a s e r b yrid" i t i s n a tu ra l t h a t t h e c o n d i t i o n a l c l a u se i s p l a c e d in f ro n t o f t h e c o n d i t i o n e ds t a t e m e n t . ( In t h i s se n se t h e a l t e rn a t iv e c l a u se

    " i f c o n d i t i o n t h e n s t a t e m e n t 1 e ls e s t a t e m e n t 2 "i s " o v e r - o r d e r e d " w i t h r e s p e c t t o " s t a t e m e n t 1 " a n d " s t a t e m e n t 2 " : t h e y a r ej u s t t w o a l t e r n a t i v e s t h a t c a n n o t b e e x p r e s s e d s i m u l t a n e o u s l y o n a l i n e a rm e d i u m . )

    T h e a l t e r n a t i v e c l a u s e h a s b e e n g e n e r a l i s e d b y C . A . R . H o a r e w h o s e" c a s e - o f " c o n s t r u c t i o n p r o v i d e s a c h o i c e b e t w e e n m o r e t h a n t w o p o s s i bi l it i es .In f l o wc h a r t fo rm th e y c a n b e r e p re se n t e d a s fo l l o ws .

    r . . . . . . . . . . "I r TI I II

    ' 1 " 1 I

    ' L " L, " ' 2 ' ' ?I lL _ _ _ . a Li f ? d o 5 1 i f 7 t h e n S l e l s e 5 2

    ti I ~ II

    I ~I x \I x

    . . r " " " ' 7 ~ ' i[ ~ ' 7 L _ ~ - I ~,~ ~ ' , : / . c j - - f

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    30/234

    NOTES ON STRUCTURED PROGRAMMING 19T hes e f lowcha r t s s ha re the p rope r ty tha t they have a s ing le en t ry a t the top

    and a s ing le ex i t a t the bo t tom: a s ind ica ted by the do t t ed b lock they canaga in be in te rp re ted (by d i s rega rd ing wha t i s ins ide the do t t ed l ine s ) a s as ing le ac t ion in a s equen t i a l computa t ion . T o be a l i t t l e b i t more p rec i s e"w e a r e d e a l in g w i t h a g r e a t n u m b e r o f p o s si b l e c o m p u t a t i o n s , p r i m a r i l ydeco mp os ed in to the s ame t ime-s uccess ion o f s ubac t ions a nd i t is on ly onc los e r ins pec t ion- - i . e , by look ing ins ide the do t t ed b lock- - tha t i t i s revea ledtha t ove r the co l l ec tion o f pos s ib le com puta t ions s uch a s ubac t ion m ay t akeone o f an enume ra ted s e t o f d i s t ingu i s hed fo rms .

    T he ab ove i s su f f ic i en t to cons ide r a c la s s o f com puta t ions tha t a re p r im ar i lydecom pos ed in to the s ame s e t o f enum era ted s ubac t ions ; they a re ins uff i c ien tt o c o n s i d e r a c la s s o f c o m p u t a t i o n s t h a t a r e p r i m a r i l y d e c o m p o s e d i n t o ava ry ing num ber o f s ubac t ions ( i. e. va ry ing ove r the c las s o f com puta t ionscons ide red) . I t i s he re tha t the us e fu lnes s o f the repe t i t ion c laus es becomesa p p a r e n t . W e m e n t i o n " w h i l e c o n d i t i o n d o s ta t e m e n t " a n d " r e p e a t s t a t e m e n tun t i l cond i t ion" tha t may be repre s en ted in f lowcha r t fo rm as fo l lows .

    I l rI II I

    I _ ] ,I ' 'I

    I I5 I I

    I i] i i. . . . II i

    tt

    ?

    w h i l e ? d _~ S r e p e a t 5 u n t i l

    T hes e f lowcha r t s a l s o s ha re the p rop e r ty o f a s ing le en t ry a t the top an d as ingle ex i t a t the bo t to m. T hey enab le us to expres s tha t the ac t ion rep re s en tedby the do t t ed b lock i s on c los e r ins pec t ion a t ime-s ucces s ion o f " a s u f f i c i en tn u m b e r " o f s u b a c ti o n s o f a c e r ta i n t y p e.

    We have now s een th ree types o f decompos i t ion ; we cou ld ca l l them"co nca t ena t ion " , " s e le c t ion" an d " repe t i t i on" re s pec t ively . T he f ir s t two a reu n d e r s t o o d b y e n u m e r a t i v e r e a s o n i n g , t h e l a st o n e b y m a t h e m a t i c a l i n d u c t i o n .

    T he p rograms tha t can be wr i t t en us ing the s e lec t ion c laus es and therepe t i t ion c laus es a s on ly the means fo r s equenc ing con t ro l , pe rmi t s t ra igh t -f o r w a r d t r a n s l a t io n i n t o a p r o g r a m m i n g l a n g u a g e t h a t i s i d e n ti c a l b u t f o r t h e

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    31/234

    20 E. W. DIJKSTRAf a c t t h a t s e q u e n c i n g c o n t r o l h a s t o b e e x p r e s s e d b y j u m p s t o l a b e ll e d p o i n t s .T he conve r se i s no t t r ue . A l t e r na t i ve l y : r e s t r i c t i ng our se l ves t o t he t h r eem e n t i o n e d t y p e s o f d e c o m p o s i t i o n l e a d s to f l o w c h a r t s o f a re s t r ic t e d t o p o l o g yc o m p a r e d w i t h t h e f l o w c h a r t s o n e c a n m a k e w h e n a r r o w s c a n b e d r a w n f r o ma n y b l o c k l e a d i n g i n t o a n y o t h e r . C o m p a r e d w i t h t h a t g r e a t e r f r e e d o m , t or e s t r i c t onese l f t o t he c l auses p r e sen t s i t s e lf a s a s equen c i ng d i s c i p li ne .

    W hy d o I p r opos e t o ad he r e t o t h i s s equenc i ng d i s c i p li ne ? T he j us t i f i ca t ionf o r t h i s d e c i s i o n c a n b e p r e s e n t e d i n m a n y w a y s a n d l e t m e t r y a n u m b e r o ft h e m i n th e h o p e t h a t a t l e a s t o n e o f t h e m w i l l a p p e a l t o m y r e a d e r s .

    E v e n t u a l l y , o n e o f o u r a i m s i s t o m a k e s u c h w e l l - s t ru c t u r e d p r o g r a m s t h a tt he i n t e l l ec t ua l e f f o r t ( measur ed i n some l oose s ense ) needed t o unde r s t andt h e m i s p r o p o r t i o n a l t o p r o g r a m l e n g t h ( m e a s u r e d i n s o m e e q u a l l y l o o s es e ns e ). I n p a r t i c u l a r w e h a v e t o g u a r d a g a i n s t a n e x p l o d i n g a p p e a l t o e n u m e r a -t iv e r e a s o n in g , a t a s k t h a t f o r ce s u p o n u s s o m e a p p l i c a t i o n o f t h e o l d a d a g e" D i v i d e a n d R u l e " , a n d t h a t i s t h e r e a s o n w h y w e p r o p o s e t h e s t e p -w i s ed e c o m p o s i t i o n s o f t h e c o m p u t a t i o n s .

    W e c a n u n d e r s t a n d a d e c o m p o s i t i o n b y c o n c a t e n a t i o n v i a e n u m e r a t i v er e a s o n i n g . ( W e c a n d o s o , p r o v i d e d t h a t t h e n u m b e r o f s u b a c t i o n s i n t o w h i c ht he comput a t i on i s p r i mar i l y pa r sed , i s su f f i c i en t l y sma l l and t ha t t he spec i f i -ca t i on o f t he i r ne t e f f ec t i s su ff i ci en tl y conc i s e . I sha l l r e t u r n t o t hese r eq u i r e -men t s a t a l a t e r s t age , a t p r e sen t w e a s sume t he cond i t i ons me t . ) I t i s t henf e a si b le to m a k e a s s e r ti o n s a b o u t t h e c o m p u t a t i o n s o n a c c o u n t o f t h e p r o g r a mt e x t, t h a n k s t o t h e t r iv i a l it y o f th e r e l a t i o n b e t w e e n t h e p r o g r e s s t h r o u g h t h ec o m p u t a t i o n s a n d t h e p r o g r e s s t h r o u g h t h e p r o g r a m t e x t. I n p a r t i c u l a r : i f o nc l o s er i n s p e c ti o n o n e o f th e s u b a c t i o n s t r a n s p i r e s t o b e c o n t r o l l e d b y ase l ec t i ve c l ause o r a r epe t i t i on c l ause , t h i s f ac t does no t i mpose any bur deno n t h e u n d e r s t a n d a b i l i t y o f th e p r i m a r y d e c o m p o s i t i o n , b e c a u s e t h e r e o n l yt he subac t i on ' s ne t e f f ec t p l ays a r o l e .

    A s a co r o l l a r y" i f on c l ose r i n spec t i on a sub ac t i on i s con t r o l l ed by ase l ec t i ve c l ause t he spec i f i c pa t h t aken i s a l w ays i r r e l evan t a t t he p r i mar y l eve l( t h e o n l y th i n g t h a t m a t t e r s i s t h a t t h e c o r r e c t p a t h h a s b e e n t a k e n ) . A n d a l s o :i f on c l ose r i n spec t i on a suba c t i on i s con t r o l l ed by a r epe t i t i ve c l ause , t hen u m b e r o f t i m e s t h e r e p e a t e d s t a t e m e n t h a s b e e n e x e c u t e d i s , a s s u c h ,i r r e le v a n t ( t h e o n l y t h i n g t h a t m a t t e r s i s t h a t i t h a s b e e n r e p e a t e d t h e c o r r e c tn u m b e r o f t i m e s ) .

    W e can a l so unde r s t an d t he s e lec t ive c lauses a s such , v iz . by enu me r a t i ver e a s o n i n g ; w e c a n a ls o u n d e r s t a n d t h e r e p e t it i o n c l a u se , v iz . b y m a t h e m a t i c a li n d u c t i o n . F o r a l l t h r e e ty p e s o f d e c o m p o s i t i o n - - a n d t h is s e em s t o m e a g r e a th e l p - - w e k n o w t h e a p p r o p r i a te p a t t e r n o f r e a so n i n g.

    T h e r e i s a f u r t h e r b e n e f i t t o b e d e r i v e d f r o m t h e p r o p o s e d s e q u e n c i n gd i s c i p l i n e . I n u n d e r s t a n d i n g p r o g r a m s w e e s t a b l i s h r e l a t i o n s . I n o u r e x a m p l eo n e n u m e r a t i v e r e a s o n i n g w e e s t a b l i s h e d t h a t t h e p r o g r a m p a r t

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    32/234

    l e a v e s t h e r e l a t i o n

    NOTES ON STRUCTURED PROGRAMMING

    " d d : = dd / 2 ;i f dd

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    33/234

    22 E. W. DIJKSTRAt he t ex t ua l i ndex i den t i f i e s , s ay , s emi co l ons . ) T he t ex t ua l i ndex i s a k i nd o fgene r a l i s ed o r de r coun t e r , i t s va l ue po i n t s t o a p l ace i n t he t ex t .

    I f w e r e s tr i c t o u rs e l v es t o d e c o m p o s i t i o n b y c o n c a t e n a t i o n a n d s e le c ti o n , as i ng le t ex t ua l i ndex i s su f fi c ien t t o i den t i f y t he p r ogr es s o f t he com put a t i on .W i t h t he i nc l us i on o f r epe t i t i on c l auses t ex t ua l i nd i ces a r e no l ong e r su f f ic i en tt o d e s c r ib e t h e p r o g r e s s o f t h e c o m p u t a t i o n . W i t h e a c h e n t r y i n t o a r e p e t i t i o nc l a u s e , h o w e v e r , t h e s y s t e m c o u l d i n t r o d u c e a s o - c a l l e d " d y n a m i c i n d e x " ,i n e x o r a b l y c o u n t i n g t h e o r d i n a l n u m b e r o f th e c o r r e s p o n d i n g c u r r e n t r e p e t i-t i o n ; a t t e r m i n a t i o n o f th e r e p e t i t io n t h e s y s t e m s h o u l d a g a i n r e m o v e t h ec o r r e s p o n d i n g d y n a m i c i n d e x . A s r e p e t i t i o n c l a u s e s m a y o c c u r n e s t e d i n s i d eeach o t he r , t he a pp r o pr i a t e mec han i sm i s a s t ack ( i. e . a la s t - i n - f i rs t - ou t -me mo r y) . I n i t i a l l y the s t ack i s em pt y ; a t en t r y o f a r epe t i t i on c l ause a newd y n a m i c i n d e x ( se t t o z e r o o r o n e ) is a d d e d o n t h e t o p o f th e s t a c k ; w h e n e v e ri t i s dec i ded t h a t t he r epe t i t i on i s no t t e r m i na t e d t he t op e l emen t o f th i s s t acki s i nc r eased by 1 ; w heneve r i t i s dec i ded t ha t a r epe t i t i on i s t e r mi na t ed , t het op e l em en t o f t he s t ack is r emo ved . ( T h i s a r r an gem ent r e f lec t s ve r y c l ea r l yt h a t a f t e r t e r m i n a t i o n o f a r e p e ti t i o n t h e n u m b e r o f ti m e s , e v en t h e f a c t t h a ti t w as a r epe t i t i on , i s no l onge r r e l evan t . )

    A s s o o n a s t h e p r o g r a m m i n g l a n g u a g e a d m i t s p r o c e d u r e s , t h e n a s i n g l et ex t ua l i ndex i s no l onge r su f f i c i en t . I n t he ca se t ha t a t ex t ua l i ndex po i n t st o t h e i n te r i o r o f a p r o c e d u r e b o d y , t h e d y n a m i c p r o g r e s s o f th e c o m p u t a t i o ni s on l y cha r ac t e r i s ed w hen w e a l so desc r i be to w h i ch ca l l o f t he p r oce dur e w er e f e r , bu t t h i s can be done by g i v i ng t he t ex t ua l i ndex po i n t i ng t o t he p l aceo f t h e ca l l. W i t h t h e i n c l u si o n o f th e p r o c e d u r e t h e t e x t u a l i n d e x m u s t b egene r a l i s ed t o a s t ack o f t ex t ua l i nd i ces , i nc r eased b y one e l emen t a t p r oc edu r ec a l l a n d d e c r e a s e d b y o n e e l e m e n t a t p r o c e d u r e r e t u r n .

    T he m a i n po i n t i s t ha t t he va l ues o f t he se i nd i ces a r e ou t s i de t he p r o -g r a m m e r ' s c o n t r o l ; t h e y a r e d e fi n e d ( e it h e r b y t h e w r i t e -u p o f h i s p r o g r a m o rb y t h e d y n a m i c e v o l u t i o n o f th e c u r r e n t c o m p u t a t i o n ) w h e t h e r h e l i k es it o rn o t . T h e y p r o v i d e i n d e p e n d e n t c o - o r d i n a t e s i n w h i c h t o d e s c r ib e t h e p r o g r e s so f t h e c o m p u t a t i o n , a " v a r i a b l e - i n d e p e n d e n t " f r a m e o f re f e re n c e i n w h i c hm e a n i n g s t o v a r i a b l e s c a n b e a s s i g n e d .

    T h e r e i s , o f c o u r s e , e v e n w i t h t h e f re e u s e o f j u m p s , a p r o g r a m m e r i n d e -p e n d e n t c o - o r d i n a t e s y s t e m in t e r m s o f w h ic h t h e p r o g r e s s o f a se q u e n t i a lc o m p u t a t i o n c a n b e d e s c r ib e d u n i q u e l y , v i z. a k i n d o f n o r m a l i s e d c l o c k t h a tc o u n t s t h e n u m b e r o f " d i s c re t e p o i n t s o f c o m p u t a t i o n p r o g r e s s " p a s s e d s i n cepr o gr a m s t a r t . I t i s un i que , bu t u t t e r l y unhe l p f u l , because t he t ex t ua l i ndexi s n o l o n g e r a c o n s t i t u e n t c o m p o n e n t o f s u ch a c o - o r d i n a t e s y s te m .

    T h e m o r a l o f t h e s t o r y i s t h a t w h e n w e a c k n o w l e d g e o u r d u t y t o c o n t r o l t h ec o m p u t a t i o n s ( i n t e l l e c t u a l l y ! ) v i a t h e p r o g r a m t e x t e v o k i n g t h e m , t h a t t h e nw e shou l d r e s t r i c t ou r se l ves i n a l l humi l i t y t o t he mos t sys t ema t i c s equenc i ng

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    34/234

    N O T ES O N S T R U C T U R E D P R O G R A M M I N G 23m e c h a n i s m s , e n s u r i n g t h a t " p r o g r e s s t h r o u g h t h e c o m p u t a t i o n " i s m a p p e do n " p r o g r e s s t h r o u g h t h e t e x t " i n t h e m o s t s t r a i g h t f o r w a r d m a n n e r .

    8 . O N C O MPA R I N G PR OG R A MSI t i s a p r o g r a m m e r ' s e v e r y d a y e x p e r i e n c e t h a t f o r a g i v e n p r o b l e m t o b es o l v e d b y a g i v e n a l g o r i t h m , t h e p r o g r a m f o r a g i v e n m a c h i n e i s f a r f r o muni qu e l y de t e r m i ned . I n t he cour se o f t he des ign p r oces s he has t o s e l ec tbe t w een a l t e r na t i ve s ; once he has a co r r ec t p r ogr am, he w i l l o f t en be ca l l ed t omodi f y i t , f o r i n s t ance because i t i s f e l t t ha t an a l t e r na t i ve p r ogr am w oul d bem o r e a t t r a c t i v e a s f a r a s t h e d e m a n d s t h a t t h e c o m p u t a t i o n s m a k e u p o n t h ea v a i l a b l e e q u i p m e n t r e s o u r c e s a r e c o n c e r n e d .

    T h e s e c i r c u m s t a n c e s h a v e r a i s e d t h e q u e s t i o n o f t h e e q u i v a le n c e o fp r o g r a m s : g i v e n t w o p r o g r a m s , d o t h e y e v o k e c o m p u t a t i o n s e s t a b l i s h i n g t h esame ne t e f f ec t ? A f t e r su i t ab l e f o r m a l i s a t i on ( o f the w ay i n w h i ch t he p r o gr a msa r e g iv e n , o f t h e m a c h i n e t h a t p e r f o r m s t h e c o m p u t a t i o n s e v o k e d b y t h e ma n d o f t h e " n e t e f f ec t " o f t h e c o m p u t a t i o n s ) t h is c a n p r e s u m a b l y b e m a d ei n t o a w e l l - p o s e d p r o b l e m a p p e a l i n g t o c e r t a i n m a t h e m a t i c a l m i n d s . B u t Ido no t i n t end t o t ack l e i t i n t h i s gene r a l f o r m . O n t he con t r a r y"~ _ns t ead o fs t a r t i n g w i t h t w o a r b i t r a r i l y g i v e n p r o g r a m s ( s a y : i n d e p e n d e n t l y c o n c e i v e db y t w o d i f f e r e n t a u t h o r s ) I a m c o n c e r n e d w i t h a l t e r n a t i v e p r o g r a m s t h a t c a nb e c o n s i d e re d a s p r o d u c t s o f t h e s a m e m i n d a n d t h e n t h e q u e s t io n b e c o m e s :h o w c a n w e c o n c e i v e ( a n d s t r u c t u r e ) t h o s e t w o a l t e r n a t i v e p r o g r a m s s o a s t oe a s e th e j o b o f c o m p a r i n g t h e t w o ?

    I h a v e d o n e m a n y e x p e r i m e n t s a n d m y b a s i c e x p e r i e n c e g a i n e d b y t h e mc a n b e s u m m e d u p a s f o l l o w s . T w o p r o g r a m s e v o k i n g c o m p u t a t i o n s t h a te s t ab l i sh t he s ame ne t e f f ec t a r e equ i va l en t i n t h a t s e n s e a n d a p r i o r i n o t i na n y o t h e r . W h e n w e w i s h t o c o m p a r e p r o g r a m s i n o r d e r t o c o m p a r e t h e i rc o r r e s p o n d i n g c o m p u t a t i o n s , t h e b a s ic e x p e r i e n ce i s t h a t i t is im p o s s i b l e ( o rf r u i t l e s s , u n a t t r a c t i v e , o r t e r r i b l y h a r d o r w h a t y o u w i s h ) t o d o s o w h e n o nt h e l ev e l o f c o m p a r i s o n t h e s e q u e n c i n g t h r o u g h t h e t w o p r o g r a m s d i f fe r s.T o be a l i t t l e mor e exp l i c i t : i t i s on l y a t t r ac t i ve t o compar e t w o p r ogr amsa n d t h e c o m p u t a t i o n s t h e y m a y p o s s i b l y e v o k e , w h e n p a i r e d c o m p u t a t i o n sc a n b e p a r s e d i n t o a t i m e - s u c c e s s io n o f a c t i o n s t h a t c a n b e m a p p e d o n e a c ho t h e r a n d t h e c o r r e s p o n d i n g p r o g r a m t e x t s c a n b e e q u a l l y p a r s e d i n t oi n s t r u c t i o n s , e a c h c o r r e s p o n d i n g t o s u c h a n a c t i o n .

    T h i s i s a ve r y s t r ong cond i t i on . L e t me g i ve a f i r s t exampl e .E xc l ud i ng s i de -e f f ec t s o f t he boo l ea n i nspec t i on s and a s su mi ng t he va l ue

    " B 2 " c o n s t a n t ( i .e . u n a f f e c te d b y t h e e x e c u t io n o f e i t h e r " S I " o r " $ 2 " ) , w ec a n e s t a b l is h t h e e q u i v a l e n c e o f t h e f o ll o w i n g t w o p r o g r a m s :

  • 8/3/2019 Structured Programming, Dahl, Dijkstra, Hoare, Academic Press 1972

    35/234

    24

    a n d

    E . W . D I J K S T R A

    " i f B 2 t h e nb e g i n w h i l e B1 do S1 en d

    e l s eb e g i n w h i l e B 1 d o $ 2 e n d " (1 )

    "whi le B1 dob e g i n i f B 2 t h e n S 1 e l s e $ 2 e n d " ( 2 )Th e f i r s t c o n s t ru c t i o n i s p r im a r i l y o n e i n wh ic h se q u e n c in g i s c o n t ro l l e d

    b y a se l e c t i v e c l a u se , t h e se c o n d c o n s t ru c t io n i s p r im a r i l y o n e i n wh ic hse q u e n c in g i s c o n t ro l l e d b y a r e p e t i t i v e c l a u se . I c a n e s t a b l i sh t h e e q u iv a l e n c eo f th e o u t p u t o f t h e c o m p u t a t i o n s , b u t I c a n n o t r e g a r d t h e m a s e q u i v a l e n t ina n y o th e r u se fu l se n se. I h a d t o fo rc e m y se l f t o t h e c o n c lu s io n th a t (1) a n d( 2) a r e " h a r d t o c o m p a r e " . O r i g i n a ll y th i s c o n c lu s i o n a n n o y e d m e v e r y m u c h .I n t h e m e a n t i m e I h a v e g r o w n t o r e g a r d t h i s i n c o m p a r a b i l i t y a s o n e o f th efa c t s o f li fe a n d , t h e re fo re , a s o n e o f t h e m a jo r r e a so n s w h y I r e g a rd t h ec h o ic e b e twe e n (1 ) a n d (2 ) a s a r e l e v a n t d e s ig n d e c i s io n , t h a t sh o u ld n o t b et a k e n wi th o u t c a re fu l c o n s id e ra t i o n . I t i s p re c i se ly i t s a p p a re n t t r i v i a l i t yth a t h a s m a d e m e se n s i t i v e t o t h e c o n s id e ra t i o n s t h a t sh o u ld i n f lu e n c e su c h ac h o ic e . Th e y f a l l o u t s id e t h e sc o p e o f t h e p re se n t se c t i o n b u t I h o p e t o r e tu rnto t h e m l a t e r .

    Le t m e g iv e a se c o n d e x a m p le o f i n c o m p a ra b i l i t y t h a t i s s l i g h tly m o r esu b t l e .

    G i v e n tw o a r r a y s X [ I : N ] a n d Y [ I : N ] a n d a b o o l e a n v a ri a bl e " e q u a l " ,m a k e a p r o g r a m t h a t a s s i g n s t o t h e b o o l e a n v a r i a b l e " e q u a l " t h e v a l u e :" t h e t w o a r r a y s a r e e q u a l e l e m e n t - w i s e " . E m p t y a r r a y s ( i .e . N = 0) a r er e g a r d e d a s b e i n g e q ua l .

    I n t r o d u c i n g a v a r i a b l e j a n d g i vi n g t o " e q u a l " t h e m e a n i n g " a m o n g t h ef i r st j p a i r s n o d i f f e re n c e h a s b e e n d e t e c t e d " , we c a n wr i t e t h e fo l l o win gt w o p r o g r a m s .

    " j : = 0 ; e q u a l : = t ru e ;wh ile j ~= N dob e g i n j : = j + 1 ; e q u a l : = e q u a l a n d ( X [ j ] = Y D ' ] ) e n d " ( 3 )

    a n d" j : = 0 ; e q u a l : = true ;

    w h i l e j - N a n d e q u a l