<<

Programming With(out) the GOTO

B .M . Leavenworth, IB M

A brief history of the controversy (retentio n 1. Since transfer of control is subsumed b y or deletion of the goto statement) is presented . more powerful notions, to wit : After considering some of the theoretical an d sequential executio n practical aspects of the problem, a summary o f procedure call and retur n arguments both for and against the goto is given . conditional expression (statement ) repetition clause (for statemen t KEY WORDS AND PHRASES : goto statement, comput- in ALGOL, DO group in PL/I ) ability theory, goto-less programming, combinatory is not the led astray by givin g logic, , Post systems, Markov - him control over it ? rithms, Turing machines, , 2. The solution of the halting problem (th e . control structures determination of whether a given program ter- CR CATEGORIES : 1 .3, 4 .2, 5 . 2 minates) is made difficult by the unrestricte d use of the goto statement . After eliminatio n INTRODUCTION of the goto, there are only two ways in whic h a program may fail to stop : either by infinit e There are a number of issues connected wit h recursion, or by the repetition clause , the retention or deletion of the goto statemen t in programs or programming languages, and w e Val Schorre reported in 1966 (S1) on th e attempt to set the stage for a discussion of thes e development of two procedural languages, LISPX an d issues by giving a brief history of the goto con- MOL-32, without the goto . Further, that he ha d troversy . The possibility of eliminating the got o been writing programs since 1960 in outline for m has both theoretical and practical aspects . It i s using the principle of nested flow . These outlines , of interest to discover that the goto does no t which served the purpose of flow charts, showed th e appear in most formal systems of computabilit y flow of control graphically by indentation, an d theory, but does appear in programming languag e were used as original documentation of the program , extensions of these systems . Since programmin g which was coded in from the out - style is an important component of the controversy , line . This may be the first recorded instance o f we give one example of the influence of a hig h "goto-less " procedural programming (as distinc t level language on , and its re- from in LISP), albeit not i n lation to the goto statement . Finally, a summar y a high level language . of arguments both for and against the goto i s Professor Van Wijngaarden (Vl) showed that th e given . goto statement could, in principle, be eliminate d from ALGOL 60 programs by a preprocessing algorith m HISTORY which replaced the set of given programming con- structs by a smaller set of equivalent concepts . The proposition that there might be somethin g The purpose of this demonstration was the expli- wrong with the goto statement, one of the pillar s cation of syntax and semantics, rather than 'goto- of practical programming since the invention o f less " programming . , has slowly penetrated the consciousnes s Peter Landin also argued in 1966 (L3) for a of since Dijkstra ' s famous letter i n style of programming which eliminates not only th e the Communications of the ACM (D4) . Actually , goto, but the notion of explicit sequencing an d Professor Dijkstra considered s assignment as well . Landin introduced a languag e without benefit of either assignment or goto in a called ISWIM, and used a purely functional subse t paper presented at the 1965 IFIP Congress (D2) . H e of this language to program in this style . I t concluded that a language without the former wa s should be noted, however, that ISWI?I contains im- elegant but inadequate . As to the latter, he enun- perative features such as " program points " , roughl y ciated the criterion that the quality of a program- analogous to labels, and assignment so that th e mer was inversely proportional to the density o f programmer has an out . We shall see that thi s 2oto statements in his program . While admittin g theme constantly recurs in what follows . the possibility of a conflict between convenienc e and efficiency, he made the following points, whic h COMPUTABILITY THEORY WITH[OUT] THE GOT O are paraphrased below, Although the formal systems of computabilit y theory (see below) have for the most part theo -

54

retical rather than practical significance, they statements in ALGOL and PL/I so that they coul d demonstrate that the goto is not needed as a prim- write itive in order to compute all computable functions . IF expr THEN DO It is interesting to discover, however, that th e ; . . ' END ; goto has been included in pragmatic extensions t o continued to writ e these systems . The goto does not appear in the following for - IF expr THEN GO TO LAB ; mal systems : from force of habit . Thus we see the influenc e that machine primitives have exerted through th e Formal System programming Extensio n present evolution of high level programming lan- combinatory logic of a jumping operator ' J ' guages ! Curry & Feys (Cll), and was defined by Landi n the lambda calculus of (L3) in ISWIN, which i s SUMMARY OF ISSUE S Church (C4) an extension of th e lambda calculu s Since the theoretical possibility of elimi - nating the goto has been demonstrated, it wil l Post systems (P5) and labelled Markov algor - not be discussed further . We will therefor e Narkov algorithms (Ml) ithms with branchin g attempt to summarize the practical arguments bot h were defined by Cara- for and against the goto . The arguments for elimi - cciolo et al (Cl), an d nating the goto (at the same time, replacing it b y the language CO>IIT (Yl ) other control structures) are essentially the fol- and SNOBOL (Fl) can b e lowing : considered to be exten- 1 . Goto-less sions of Markov algor - programs are easier to understand , ithms with goto command s debug and modify . This is the structured o r top-down programming argument (D2) (D4) (D6 ) Kleene general recur- LISP (212), which was als o (ES) (W7) (W8) (W9) . sive functions (Kl) strongly influenced b y the lambda calculus, ha s 2, If the goto statement is not replaced b y more sophisticated control structures, th e the PROG feature which allows assignment an d programmer is likely to misuse it (the goto. ) goto (see below ) in order to synthesize those structures (D4 ) (W9) . We see in each case that the goto is missing i n 3 . It is easier to prove assertions about " goto- the pristine form of the system, but has been adde d less " programs (L3) (P3) (S3) . in programming extensions (whether for the sake o f tradition or " convenience" is a matter for debate) . The technical means of replacing b y The FROG feature (B5) was added to LISP in order t o other control structures are as follows : incorporate the goto, among other things, althoug h 1. by recursive procedures . This is a theo- a wide range of applications have been written i n retical, rather than a practical, device (V1 ) pure (no assignment or goto) LISP . (K2) . The goto appears in Turing machines (Ti) (sinc e instructions or states have explicit successors) , 2. by the while construction . This can al - and related automata such as Minsky's program ma - ways be done without changing the progra m chines (M6) . It also appears in program schemat a topology, by the introduction of auxiliary (L7), which can be characterized as flow charts wit h variables (Al) . assignment statements in the boxes . And finally , 3. by node splitting . This requires redundan t it appears in the order codes of the general pur- code or procedure calls (K2) (W7) . pose computer . The arguments against eliminating the goto ca n THE INFLUENCE OF NOTATIO N be surmarized as follows : 1. the goto is needed for abnormal exits fro m It may be truly said that the goto statement i n a block or procedure . The ' repeat-exit' mech- its form as a machine primitive has profoundly in- anism of Knuth and Floyd (K2) only allows a one - fluenced the long line of procedural high level lan- level exit, whereas Wulf ' s leave constructio n guage descendants . We wish to explore this poin t 0?7) requires the reintroduction of labels fo r and its relation to what shall be called the FORTRA N multi-level exits . As Landin (L3) has admitted , II IF Syndrome . " the most recalcitrant uses of explicit sequenc- The FORTRAN II IF statement --- IF (expr ) ing appear to be associated with success/failur e nl, n2, n3 ---- is a prime example of the power o f situations and the action needed on failure , language to influence program organization, an d probably corrupted a generation of programmers . 2. the goto is often more efficient . For, con- sider the overhead introduced by node splittin g This statement effectively generates multiple goto s (which reflect the unconditional transfers in ma - and the while construction (setting of flags) . Also, Knuth and Floyd (K2) have pointed out tha t chine code), as can be seen by the equivalent PL R procedure calls can sometimes be replaced b y statements : goto statements . IF expr < 0 THEN GO TO nl ; IF expr = 0 THEN GO TO n2 ; 3. the goto is useful for synthesis purposes (W2 ) (H2) . Two examples : the can b e IF expr > 0 THEN GO TO n3 ; synthesized by goto, and the case statement o f The sad fact is that many programmers, eve n Wirth and Hoare (W3) can be synthesized in a after being liberated by compound and conditional language, say PL/I, which lacks it .

55

ACKNOWLEDGEMEN T ton Univ . Press, Princeton, New Jersey (1951) , C5. Cohen, K . and Wegstein, J . H ., " AXLE, a n Thanks are due to Jean Sammet who suggested th e axiomatic language for string transformation s " , writing of this paper, and who provided valuabl e CACM 8, (1965), 657-661 . comments for improving several early versions . C6. Cooper, D . . " On the equivalence of cer- REFERENCES & BIBLIOGRAPHY tain computations" . Computer Journal 9 (1966) , 45-52 . Al . Ashcroft, Edward and Manna, Zohar . " Th e C7. Cooper, D . C . " Reduction of programs to a translation of ' goto ' programs to 'while' pro - standard form by graph transformation " , Theor y grams " . Proc . IFIP Congress 71, Ljubljana, Aug . of Graphs, International Symposium, Rome 196 6 1971 . (Ed . Rosenstiehl, P .), Gordon and Breach, New York, 1967 . B]. . de Bakker, J . W . " Semantics of programmin g languages " . Advances in Information System s C8. Cooper, D . C . " Bohm and Jacopini ' s reduc- Science 2 (Ed . Tou, J .T .) Plenum Press, New York , tion of flow charts " . Letter to the Editor , 1969 . CACM 10 (Aug . 1967) . B2, Barron, D .W . Recursive Techniques in Pro- C9, Cooper, D . C . " Some transformations an d gramming . American Elsevier, New York, 1968 . standard forms of graphs, with applications t o s " , Machine Intelligence 2 (Ed . B3. Barron, D .W . and Strachey, C . " Programming " . Dale & Michie), American Elsevier, New York , Advances in Programming and Non-Numerical Com- 1968 . putation . (Ed . Fox, L .), Pergamon Press, Ne w York, 1966 . C10 . Coulouris, G . F . "Principles for imple- menting useful subsets of advanced programmin g B4. Berry, D . M . " Introduction to Oregano " . language s " , Machine Intelligence 1 (Ed . Collin s Proc . Symposium on Data Structures in Program- & Michie), Oliver & Boyd, Edinburgh, 1967 . ming Languages, SIGPLAN Notices 6,2 (Feb . 1971) . Cll . Curry, H . and Feys, R . Combinatory Logic , B5. Black, Fischer . " Styles of programming i n Vol . 1, North-Holland, Amsterdam, 1958 . LISP " The Programming Language LISP : Its Oper- ation and Applications (Ed . Berkeley and Bobrow) , D1. Dijkstra, E . W ., ' An attempt to modify th e Information International, Cambridge, Mass . 1964 . constituent concepts of serial program executio n ' , B6. Bohm, Corrado and Jacopini, Giuseppe . " Flow Proc . ICC Symposium on ymbolic Languages in Dat a diagrams, Turing machines and languages with onl y Processing, Gordon & Breach, New York, 1962 . two formation rules " . CACM 9 (May 1966) . D2. Dijkstra, E . W . " Programming considered as a B7. Burge, W .H . "The evaluation, classification human activit y " , Proceedings IFIP Congress 65,65 , and interpretation of expressions " . Proc . ACM edited by W . A . Kalenich, Spartan Books, Washing - 19th National Conf . 1964 , ton, D . C ., 1965 . B8. Burge, W .H . " Notes on a model for programmin g D3. Dijkstra, E . W . " Recursive programming " , systems : Part I" . Report RC 2188 (Aug . 1968) . IB M Programming Systems and Languages (Ed . Rosen, S .) , Research Division, Yorktown Heights, N .Y . McGraw-Hill, New York 1967 . B9. Burstall, R .M . " Writing search algorithms i n D4. Dijkstra, E . W . " Go to statement considere d functional form " Machine Intelligence_ 3 (Ed . harmful " , Letter to the Editor, CACM 11 (Marc h Michie, D .) Edinburgh Univ . Press, Edinburgh, 1968 . 1968) . B10. Burstall, R .M . " Proving properties of program s D5. Dijkstra, E . W . 'A constructive approach t o by structural induction " , Computer Journal 12, 1 the problem of program correctnes s " , BIT 8 (1968) . (Feb . 1969) . D6. Dijkstra, E . W . " Notes on structured pro- B11, Burstall, R .M . and Popplestone, R .J . "POP- 2 gramming", EWD 249, Technical University, Eind- reference manua l " Machine Intelligence 2 (Ed . Dal e hoven, Netherlands, 1969 . & Michie), American Elsevier, New York 1968 . El . Ershov, A . P . " Theory of program schemat a B12, Burstall, R .M . and Landin, P . J . " Program s Proc . IFIP Congress71, Ljubljana, Aug . 1971 . and their proofs : an algebraic approac h " , Machin e Intelligence 4 (Eds . Meltzer & Michie) Edinburg h Fl . Farber, D . J ., Griswold, R . E . and Polonsky , Univ . Press, Edinburgh, 1969 . " SNOBOL, a string manipulation language " , JACM 11 (January 1964) . Cl . Caracciolo di Forino, A ., Spanedda, L . an d Wolkenstein, N . "PANON-1B : A programming languag e F2. Fisher, David A . " Control structures for pro- for symbol manipulation " , Calcolo, Vol . 3, 1966 . gramming language s " , PhD . Thesis, Carnegie-Mello n Univ ., Pittsburgh, Pa ., May 1970 . C2. Caracciolo di Forino, A . " Generalized Marko v algorithms and automata", Automata Theory (Ed . F3. Floyd, R . W . "A descriptive language fo r Caianiello, E . R .), Academic Press, New York, 1966 , symbol manipulation '" , JACM 8,4 (1961) . C3. Christenson, Carlos, " Examples of symbo l F4. Floyd, R . W . " Nondeterministic algorithms " , manipulation in the AMBIT programming languag e " . JACM 14 (Oct . 1967) . Proc . ACM 20th National Conf ., Cleveland, Ohio , F5. Floyd, R . W . " Assigning meanings to programs " Aug . 1965 . Proc . Symp . Applied Math ., AMS Vol . 19, 1967 . C4. Church, A ., " The calculi of lambda-con- version " , Annals of Math . Studies No . 6, Prince - G1 . Caller, B . A . and Fischer, M .J, "The iteration

56

element " , CACM 8 (June 1965) . Office of Technical Services No . OTS 60-51085 .

G2. Galler, B . A . and Perlis, A, J . A View o f M2. McCarthy, J . at al, LISP 1 .5 Programmer s Programming Languages, Addison-Wesley, Reading , Manual, The M .I .T . Press, Cambridge, Mass . 1962 . Mass ., 1970 . M3. McCarthy, J . " Towards a mathematical scienc e G3. Gilmore, P .C . " An abstract computer wit h of computation " , Proc . IFIP Congress, Munich 1962 , LISP-like machine language without a labe l North--Holland, Amsterdam , operator " , and Formal Sys- 114 . McCarthy, J . " Basis for a mathematical theor y tems (Eds . Braffort & Hirschberg), North-Holland , of computation " , Computer Programming and Forma l Amsterdam, 1963 . Systems (Eds . Braffort & Hirschberg), North-Holland , G4. Goodstein, R . L . Recursive Analysis, North - Amsterdam, 1963 . Holland, Amsterdam, 1961 . . M5. Mills H . " Top down programming in large sys- G5. Griswold, R . E . Poage, J . F . and Polonsky , tems " , Debugging Techni q ues in Large Systems (Ed . I . P . The SNOBOL4 Programming Language, Prentice - Rustin, Randall), Prentice-Hall, Englewood Cliffs , Hall, Englewood Cliffs, N .J . 1968 . N .J . 1971 .

G6. Guzman, Adolfo and McIntosh, H . " CONVERT " , M6. Minsky, M . L . Computation : Finite and Infinit e CACM 9 (Aug . 1966) . Machines, Prentice-Hall, Englewood Cliffs, N .J . 1967 . Hl . Hopkins, Martin, " A case for the goto " M7. Mooers, C . N . and Deutsch, L .P . " TRAC : A tex t Proceedings ACM '72, Boston, August 1972 . handling language " , Proc . ACM 20th National Conf . Cleveland, Ohio (Aug . 1965) . Il . Ianov, Y . I . " On the equivalence and transformation of program schemes " , CACM 1 N1. Naur, P . " Proof of algorithms general snap - (1958), 8-12 . shots", BIT 6, 1966 . I2 . Ianov, I . The logical schemes of algor- N2. Naur, P . " Programming by action cluste r ithms " , Problems of Cybernetics I (English trans - BIT 9, 1969 . lation) Pergamon Press, Oxford 1960, 82-140 .

P1. Paterson, M . S . " Program schemata " , Machin e J1. Johansen, Peter, "Non-deterministic prog- Intelligence 3 (Ed . Michie . D .), Edinburgh Unty , ramming " , BIT 7 (1967), 289-304 . Press, Edinburgh, 1968 . J2. Johnston, John B . ' The contour model o f P2. Paterson, M .S . and Hewitt, C . E . " Comparative block structured processes " , Proc . Symposiumo n schematology " , Prol . MAC Conference on Concurren t Data Structures in Programming Languages, SIGPLAN Systems and ParallelComnutation (June 1970), ACM , Notices 6,2 (Feb . 1971) . New York, 1970 .

K1. Kleene, S . C . Introduction to Metamathe- P3. Perlis, A,J ., Lecture Notes on Seminar on Ex - matics, Van Nostrand, New York, 1952 . tensible Languages . Carnegie-Mellon University , Fall, 1968 . K2. Knuth, D . E . and Floyd, R . W . " Notes o n avoiding ' goto ' statements " , Information Pro- P4. Peter, Rozsa . Recursive Functions, Academi c cessing Letters 1, North-Holland, Amsterda m Press, New York, 1967 . (1971), 23-31 . P5. Post, E . I . " Finite combinatory processes - formulation I", Journal of Symbolic Logic, Vol_ . 1 , Ll . Landin, P . J, "The mechanical evaluatio n (1936) . of expressions", Computer Journal 6,4 (1964) . L2. Landin, P . J . "A correspondence betwee n Rl . Reynolds, J .C . "GEDANKEN : A simple typeles s ALGOL 60 and Churc h ' s lambda--notation", CACM language based on the principle of completeness an d 8,2 and 3 (1965) , the reference concept " , CACM 13 (May 1970) .

L3. Landin, P . J, " The next 700 programmin g R2. Rice, H . G . " Recursion and iteration ' , CAC M languages", CACM 9 (March 1966) . 8 (Feb . 1965) , L4. Leavenworth, B . M . " The definition of con- R3. Rice, J .R . " The goto statement reconsidered " , trol structures in MCG360 " . Report RC237 6 Letter to the Editor, CACM 11 (1968) 538 . (Feb . 1969) . IBM Research Division, Yorktown R4. Rutledge, J .D . "On Ianov ' s program schemata ' ' , Heights, N .Y . JACM 11 (1964), 1-9 . L5. Ledgard, H . F . "Ten mini-languages : A stud y of topical issues in programming languages " , ACM S1. Schorre, D .V . " Improved organization for pro- Computing Surveys, 3,3 (Sept . 1971) . cedural languages " , Technical Memo, August 1966 , System Development Corp ., Santa Monica, Calif . L6. Lucas, P . et al "Method and notation for th e formal definition of programming languages " , Tech . S2. Shepherdson, J .C . and Sturgis, H .E . " Comput- Report TR 25 .087, IBM Laboratory, Vienna, 1968 . ability of recursive functions " , JACM 10,2 (1963) ,

L7. Luckham, D . C ., Park, D .M .R . and Paterson , S3. Stark, R . "A language for algorithms " , Com- M .S ., "On formalized computer programs " , Journa l puter Journal, Vol . 14, No . 1 (Feb . 1971) . of Computer and System Sciences, June 1970 . S4. Strache y_, C, "A general purpose macrogener.ator ' , Computer Journal Vol . 8, (Oct, 1965) . Ml . Markov, A .A . " The theory of algorithms " (Russian Translation), U .S . Dept . of Commerce, S5. Strachey, C . ' Fundamental concepts in program-

57 ming languages " , NATO Conf ., Copenhagen 1967 . S6 . Strong, H . R ., Jr . " Translating recursio n equations into flow chart s " , journal of Computer an d System Sciences, 5,3 (June 1971) .

T1 . Turing, A .M . "On computable numbers with a n application to the Entscheidungsproble m" , Proc . Math . Soc ., ser . 2, Vol . 42 (1936-1937) .

V1 . Van Wijngaarden, A . " Recursive definition o f syntax and semantics " , Formal Language Descript- ion Languages for Computer Programming, edited b y T•B . Steel, Jr ., North-Holland, Amsterdam, 1966 .

W1. Wang, H . " A variant to Turing ' s theory o f computing machines, JACM 4,1 (1957) . W2. Wegbreit, B . " Studies in extensible programmin g languages", ESD-TR-70-297, Directorate of System s Design & Development, L . G . Hanscom Field, Bedford , Mass ., May 1970 . W3. Wirth, Niklaus and Hoare, C .A .R . "A contributio n to the development of ALGOL " , CACM 9 (June 1966) , W4. Wirth, N . " On certain concepts of pro- gramming languages " , Technica l Report No . CS65, Stanford University, 1967 . W5. Wirth, N . " Program development by stepwis e refinement " , CACM 14 (April 1971) . W6. Wozencraft, J . M . and Evans, A . Jr ., 'Note s on programming linguistic s " , Dept . of Electrica l Engineering, MIT, Cambridge, Mass ., Feb . 1971 . W7. Wulf, W . A . " Programming without the goto " , Proc, IFIP Congress 71, Ljubljana, Aug . 1971 . W8. Wulf, W . A . Russell, D .B . and Habermann, A .N . " BLISS : a language for systems programming, CAC M 14 (Dec . 1971) . W9. Wulf, W . A . "A case against the goto " . Pro- ceedings ACM '72, Boston, August 1972 .

Y1 . Yngve, V . H . Computer Programming with COMI T II, The M .I .T . Press, Cambridge, Mass . 1972 .

58