ALGOL-60 GENERALITY AND HIERARCHY Programming Languages Course Computer Engineering Department Sharif University of Technology Outline 2 History and Motivation Structural Organization Name Structures Data Structures Control Structures Outline 3 History and Motivation Structural Organization Name Structures Data Structures Control Structures The International Algebraic Language 4 The International Algebraic Language 5 Designed by 8 representatives of ACM and GAMM Combined experiences of algebraic language design and implementing pseudo-codes. Essentially completed in 8 days Created a great stir Many dialects: NELIAC, JOVIAL Objectives of Algol 6 As close as possible to standard mathematical notation and readable with little further explanation. possible to be used for the description for computing processes in publications. Mechanically translatable into machine programs. Algol-60 7 Final report was published in May 1960, revised in 1962. The original algol-60 report is a paradigm of brevity and clarity, remains a standard. Syntax: BNF notation Semantics: clear, precise, unambiguous Englishlanguage description. Outline 8 History and Motivation Structural Organization Name Structures Data Structures Control Structures Hierarchical Structure 9 begin integer N; read int (N); begin real array Data[1:N]; integer i; sum := 0; for i := 1 step 1 until N do begin end … end end Category of Construct 10 Declaratives: bind name to the objects Variables Procedures Switches Imperative: do the work of computation Control-flow Computational (:=) The Familiar Control Structure 11 goto If-Then-Else For loop Procedure invocation Compile-time, Run-time Distinction 12 FORTRAN: static compilation process Algol: not completely static Dynamic arrays Recursive procedures The Stack, the Central Run-Time Data Structure 13 Dynamic allocation and deallocation are achieved by pushing and poping activation records on the stack. Outline 14 History and Motivation Structural Organization Name Structures Data Structures Control Structures Blocks and Compound Statements 15 A compound statement is a group of statements used where one statement is permitted. Blocks contain declarations, compound statements do not. An example of regularity in language design. Example 16 for i:=0 step 1 until N do sum:=sum+Data[i] for i:=0 step 1 until N do begin if Data[i]>10 then Data[i]:=10; sum:=sum+Data[i]; Print Real(sum); end Example 17 for i:=0 step 1 until N do sum:=sum+Data[i] for i:=0 step 1 until N do begin if Data[i]>10 then Data[i]:=10; The body of a for loop is one statement by default. sum:=sum+Data[i]; Print Real(sum); end Example 18 for i:=0 step 1 until N do sum:=sum+Data[i] The compound statement is used where one statement is permitted. for i:=0 step 1 until N do begin if Data[i]>10 then Data[i]:=10; sum:=sum+Data[i]; Print Real(sum); end Blocks Define Nested Scopes 19 In FORTRAN scopes are nested in two levels: global and subprogram-local. Algol-60 allows any number of scopes nested to any depth. Each block defines a scope that extends from the begin to the end. Name structures (such as blocks) restrict the visibility of names to particular parts of the program. Example 20 begin real x,y; procedure f(y,z); integer y,z; begin real array A[1:y]; end begin integer array Count [0:99] end end Sample Contour Diagram 21 x y f y z A count 22 Algol use of nested scopes prevents the programmer from committing errors that were possible in using FORTRAN COMMON blocks. Impossible Error Principle Making errors impossible to commit is preferable to detecting them after their commission. Static and Dynamic Scoping 23 Static and Dynamic Scoping 24 Static scoping: a procedure is called in the environment of its definition. Dynamic scoping: a procedure is called in the environment of its caller. Algol uses static scoping exclusively. Example 25 a: begin integer m; procedure P m:=1; b: begin integer m; P end; P end Example 26 a: begin integer m; procedure P m:=1; b: begin integer m; P end P end With dynamic scoping, the value of this m is 1 when the program finishes block b. Contour Diagram of Dynamic Scoping 27 (a) m P (b) m (P) DL Call P m:=1 Example 28 a: begin integer m; procedure P m:=1; b: begin integer m; P end; P end With static scoping, the value of this m is 1 when the program finishes block b. Contour Diagram of Static Scoping 29 (a) m P (b) m Call P (P) DL m:=1 Dynamic Scoping (Advantages) 30 begin real procedure sum; begin real S,x; S:=0; x:=0; for x:=x+0.01 while x<=1 do S:=S+f(x); sum:=S/100; end … end begin To real use the sum function procedure f(x); it is necessary only value x; real x; to namef:=x the function 2 + 1; to be summed f. sumf:=sum; end Dynamic Scoping (Advantages) 31 begin real procedure sum; begin real S,x; S:=0; x:=0; for x:=x+0.01 while x<=1 do S:=S+f(x); sum:=S/100; end … end begin real procedure f(x); value x; real x; f:=x↑2 + 1; sumf:=sum; end Dynamic Scoping (Advantages) 32 begin begin real procedure sum; real procedure f(x); begin real S,x; S:=0; x:=0; value x; real x; One advantage of dynamic scoping : for x:=x+0.01 while x<=1 f:=x 2 + 1; A general procedure that makes use of variables and do sumf:=sum; procedures environment. S:=S+f(x);supplied by the caller’s end sum:=S/100; end … end Dynamic Scoping (Problems) 33 begin real procedure discr(a,b,c) values a,b,c; real a,b,c; discr:=b↑2-4хaхc; procedure roots(a,b,c,r1,r2); value a,b,c; real a,b,c,r1,r2; begin …d:=discr(a,b,c); … end . . roots(c1,c2,c3,root1,root2); . . end Dynamic Scoping (Problems) 34 begin real procedure discr(a,b,c); values a,b,c; real a,b,c; discr:=b↑2-4хaхc; procedure roots(a,b,c,r1,r2); value a,b,c; real a,b,c,r1,r2; begin …d:=discr(a,b,c); … end . . roots(c1,c2,c3,root1,root2); . . end begin real procedure discr(x,y,z); value x,y,z; real x,y,z; discr:=sqrt(x ↑2); . . . roots(acoe,bcoe,ccoe,rt1,rt2); . . . end Dynamic Scoping (Problems) 35 begin begin real procedure discr(a,b,c); real procedure discr(x,y,z); values a,b,c; real a,b,c; value x,y,z; real x,y,z; discr:=b↑2-4хaхc; discr:=sqrt(x ↑2); procedure roots(a,b,c,r1,r2); . value a,b,c; real a,b,c,r1,r2; . begin . …d:=discr(a,b,c); … roots(acoe,bcoe,ccoe,rt1,rt2); end . . . . . roots(c1,c2,c3,root1,root2); endwrong discr function to .The roots procedure will use the . compute its result. end Dynamic Scoping (Problems) 36 begin begin real procedure discr(a,b,c); real procedure discr(x,y,z); values a,b,c; real a,b,c; value x,y,z; real x,y,z; discr:=b↑2-4хaхc; discr:=sqrt(x ↑2); This is called vulnerability. procedure roots(a,b,c,r1,r2); The root procedure is vulnerable .to being called from an value a,b,c; real a,b,c,r1,r2; . environment in which its auxiliary procedure is not begin . …d:=discr(a,b,c); … accessible. roots(acoe,bcoe,ccoe,rt1,rt2); end . . . . . roots(c1,c2,c3,root1,root2); end . . end Static and Dynamic Scoping 37 In dynamic scoping, the meanings of statements are determined at run-time. In static scoping, the meanings of statements are fixed. Static scoping aids reliable programming. Dynamic scoping is against the Structure Principle. Blocks and Efficient Storage Management 38 The value of a variable is retained In the scope of that variable. In a block or structure that returns to the scope of the variable. Variables of disjoint blocks can never coexist. Much more secure than FORTRAN EQUIVALENCE. Implemented by a stack. 39 Responsible Design Principle Do not ask users what they want, find out what they need. Outline 40 History and Motivation Structural Organization Name Structures Data Structures Control Structures Primitives 41 The primitive data types are mathematical scalars: integer, real, Boolean. Only one level of precision: real. No double-precision types, they violate Portability: Doubles need word size and floating point representation of the computer to be implemented. Primitives (cont.) 42 Complex numbers were omitted because: They are not primitive. The convenience and efficiency of adding them to the language is not worth the complexity of it. string data type: a second hand citizen of Algol-60. Can only be passed to procedures as arguments. Does not cause a loophole in the type system, as it does in FORTRAN. Algol was the last major language without strings. 43 Algol follows the Zero-One-Infinity principle. Zero-One-Infinity Principle The only reasonable numbers in a programming language design are zero, one, and infinity. Arrays are generalized in Algol: More than 3 dimensions. Lower bounds can be numbers other than 1. Dynamic Arrays 44 Arrays are dynamic in a limited fashion: The array’s dimension can be recomputed each time its block is entered. Once the array is allocated, its dimension remains fixed. Stack allocation permits dynamic arrays. The array is part of the activation record. The array’s size is known at block entry time. Strong Typing 45 Strong typing is where a language does not allow the programmer to treat blocks of memory defined as one type as another (casting). Example: Adding numbers to strings, doing a floating point multiply on Boolean values. This is different from legitimate conversions between types, i.e. converting reals to integers, which are machine independent operations. Outline 47 History and Motivation Structural Organization Name Structures Data Structures Control Structures Primitive Computational Statement The primitives from which control structures are build are those computational statements that do not affect the flow of control → ‘:=’ In Algol, function of control structures is to direct and manage the flow of control from one assignment statement to another Control Structures, Generalization of FORTRAN’s FORTRAN: IF (logical expression) simple statement Algol: if expression then statemtent1 else statement2 Control Structures, Generalization of FORTRAN’s Algol for-loop Includes the functions of a simple Do-loop for 1:=1 step 2 until M*N do inner[i] := outer [N*M-i]; Has a variant similar to while-loop for NewGuess for i:=i+1:= improve(oldGuess) while abs(while NewGuess - oldGuess)>0.1 i < 100 do A [i] := := NewGuess i; do oldGuess Control Structures, Generalization of FORTRAN’s Algol for-loop Includes the functions of a simple Do-loop for 1:=1 step 2 until M*N do inner[i] := outer [N*M-i]; Has a variant similar to while-loop for NewGuess := improve(oldGuess) while i < 100 do while abs(begin NewGuess - oldGuess)>0.1 A [i] := i; do oldGuess := NewGuess i=i+1; end Control Structures, Generalization of FORTRAN’s Algol-60 control structures are more: Regular Symmetric Powerful General FORTRAN has many restrictions, because of: Efficiency ( the array restriction) Compiler simplicity (the IF restriction) Control Structures, Generalization of FORTRAN’s Restrictions are inexplicable for programmer. Irregularity in a language, makes it hard to learn and remember. the Algol designers’ attitude: “Anything that you thing you ought to be able to do, you will be able to do.” Nested Statements FORTRAN IF: if the consequent is more than 1 statement there are some problems: Problem of maintainability Irregular syntax, source of error FORTRAN DO-loop: begin and end of the loop is marked. DO 20 I=1, N statement 1 statement 2 … statement m 20 CONTINUE Nested Statements the Algol designers’ realized: All control structures should be allowed to govern an arbitrary number of statements. Nested Statements Compound statement: begin statement 1 statement 2 ... statement n end Nested Statements Begin-end brackets do double duty: 1. 2. Group statements into compound statements. Delimit blocks, which define nested scopes. Nested Statements Begin-end brackets do double duty: 1. 2. Group statements into compound statements. Delimit blocks, which define nested scopes. Lack of orthogonality! Hierarchical Structure of Compound Statements Hierarchical structure is one of most important principles of language and program design. Complex structures are built up by hierarchically combining single statements into larger compound statements. Nested Led to Structured Programming Many fewer GOTO-statements were needed in Algol-60. Programs were much easier to read. Edsger Dijkstra: “Go To Statement Considered Harmful.” Nested Led to Structured Programming The Structure Principle the static structure of the program should correspond in a simply way to the dynamic structure of the corresponding computations. Recursive Procedures Example: integer procedure fac (n); value n; integer n; fac := if n =0 then 1 else n * fac(n-1); Recursive Procedures There must be a memory location to hold the formal parameter! Only one location for n? Each invocation of fac has its own instance of the formal parameter n. Creating new activation record for fac each time it is called, is instantiation. Parameter Passing by Value Example: procedure switch(n); value n, integer n; n:=3; Parameter Passing by Value Example: Procedure switch(n); Value n, integer n; n:=3; Useful only for input parameters! Parameter Passing by Value Pass by value is very inefficient for arrays: Real procedure avg (A , n); value A, n; real array A; integer n; begin . . end; Parameter Passing by Name Example: Procedure Inc (n); integer n; n := n+1; Pass by name is based on substitution! Parameter Passing by Name Copy Rule The procedure can be replaced by its body with the actuals substituted for the formals. Implementation is not based on substitution! Parameter Passing by Name Example: Procedure Inc (n); integer n; n := n+1; Satisfies Algol’s need for output parameters! Pass by name is based on substitution! Parameter Passing by Name Pass by Name Is Powerful! Example: real procedure Sum (k, l, u , ak); value l, u; integer k, l , u; real ak; begin real S; S := 0; for k:=1 step 1 until u do S := S + ak; Sum:=S; end; Parameter Passing by Name Pass by Name Is Powerful! Example: X := Sum (i, 1, n, v [i]) real procedure Sum (k, l, u , ak); value l, u; integer k, l , u; real ak; begin real S; S := 0; for I := 1 step 1 until n do S := S + v [i]; x := S; end; Parameter Passing by Name How is it implemented? Following the Copy Rule? Passing the address of the code sequence! A thunk is an invisible, parameterless, address-returning, function used by a compiler to implement name parameters and similar constructs. Parameter Passing by Name Pass by Name is Dangerous and Expensive! Example: Swap (A[i], i) and Swap (i, A[i]) procedure swap (x, y); integer x, y; begin integer t; t := x; x := y; y := t ; end Parameter Passing Summarized 1. Pass by value: At call time, the formal is bound to the value of the actual. 2. Pass by reference: At call time, the formal is bound to a reference to the actual. 3. Pass by name: At call time, the formal is bound to a thunk for the actual. Good Conceptual Models Donald Norman (psychologist): “A good conceptual model allows us to predict the effects of our actions” 3 kind of conceptual models: 1. 2. 3. Designer’s model System image User’s model; Expensive Out of Block GOTOs Algol Scope Rule An identifier declaration is visible if we are nested within the block in which it occurs . Expensive Out of Block GOTOs Example: a : begin array X [1:100]; … b : begin array Y [1:100]; … goto exit; … end; exit : … end Expensive Out of Block GOTOs Another example: begin procedure P (n); value n; integer n; if n=0 then goto out else P(n-1); P(25); out: end Expensive Out of Block GOTOs Another example: begin procedure P (n); value n; integer n; The goto-statement must be implemented as a call of if n=0 then goto out a run-time routine that find the appropriate activation record else P(n-1); and deletes all the ones above it! P(25); out: end Future Interaction, a Difficult Design Problem Algol visibility rules versus GOTO statement Problem of Interaction! Future Interaction, a Difficult Design Problem Designer must develop a “nose” for spotting possible problem areas! Generality of the for-Loop for var:=exp step exp' until exp" do stat for var:=exp while exp ' do stat for days:= 31, 28, 31, 30, do… for days:=31, if mod (year , 4) =0 then 29 else 28, 31, 30, do… The Baroque for-Loop Example: for i :=3, 7, 11 step 1 until 16, i/2 while i>=1, 2 step i until 32 Do print (i) 3 7 11 12 13 14 15 16 8 4 2 1 2 4 8 16 32 The Baroque for-Loop The Localized Cost Principle Users should pay only for what they use; avoid distributed costs. Handling Cases with the switch Example: begin switch marital status = single, married, divorced; … goto marital status [i]; single: … handle single case goto done: married: … handle married case goto done: divorced: … handled divorced case done: end; The Baroque switch Example: begin switch S=L, if i>0 then M else N, Q; goto S [j]; end The Baroque switch Example: begin switch S=L, if i>0 then M else N, Q; goto S [j]; end goto if i>0 then M else N; Handling Cases with the switch 88 Example: begin switch marital status = single, married, divorced; … goto marital status [i]; single: … handle single case goto done: married: … handle married case goto done: divorced: … handled divorced case done: end; The Baroque switch 89 Example: begin switch S=L, if i>0 then M else N, Q; goto S [j]; end The Baroque switch 90 Example: begin switch S=L, if i>0 then M else N, Q; goto S [j]; end goto if i>0 then M else N; 91 SYNTAX AND ELEGANCE: ALGOL-60 Programming Languages Course Computer Engineering Department Sharif University of Technology Outline 92 Syntactic Structures Descriptive tools: BNF Elegance Evaluation and Epilog Outline 93 Syntactic Structures Descriptive tools: BNF Elegance Evaluation and Epilog Software Portability 94 Portability Principle Avoid features or facilities that are dependent on a particular computer or small class of computers. Free Format 95 FORTRAN: fixed format Algol: free format Free Format 96 FORTRAN: fixed format Algol: free format Programmers had to think about: “How a program should be formatted?” Free Format 97 Example: FORTRAN style for i:=1 step 1 until N do begin real val; Read Real (val); Data [i] := val; end; for i:=1 step 1 until N do sum:= sum + Data [i]; avg := sum/N; Free Format 98 Example: English style for i:=1 step 1 until N do begin real val; Read Real (val); Data [i] := val; end; for i:=1 step 1 until N do sum:= sum + Data [i]; avg := sum/N; Free Format 99 Example: English style for i:=1 step 1 until N do begin real val; Read Real (val); Data [i] := val; end; for i:=1 step 1 until N do sum:= sum + Data [i]; avg := sum/N; Remember the Structure Principle! Free Format 100 Example: for i:=1 step 1 until N do begin real val; Read Real (val); Data [i] := val; end; for i:=1 step 1 until N do sum:= sum + Data [i]; avg := sum/N; Levels of Representation 101 No universal character set! How to design the lexical structure? Using those symbols available in all char sets. Design independent of particular char sets. Levels of Representation 102 Three levels of the language: 1. Reference Language a [i+1] := (a [i] + pi ˣ r↑2 ) / 6.021023 2. Publication language ai+1 ← { ai + π ˣ r2 } / 6.02 ˣ1023 3. Hardware representations a (/i+1/) := (a (/i/) + pi * r**2 ) / 6,02e23 Problems of FORTRAN Lexics 103 Example: IF IF THEN THEN = 0; ELSE; ELSE ELSE = 0; Problems of FORTRAN Lexics 104 Example: IF IF THEN THEN = 0; ELSE; ELSE ELSE = 0; Confusion! Problems of FORTRAN Lexics 105 How does Algol solve the problem? if procedure then until := until +1 else do := false; Lexical Conventions 106 Reserved words: reserved words cant be used by the programmer Keywords: used word by the language are marked in some unambiguous way Keywords in Context: used words by the language are keywords in those context they are expected. Arbitrary Restrictions, Eliminated! 107 Remember the Zero-One-Infinity Principle! Number of chars in an identifier Subscript expression Dangling else 108 if B then if C then S else T; What does the above code mean? if B then begin if C then S else T end; if B then begin if C then S end else T; Dangling else 109 if B then if C then S else T; What does the above code mean? Algol solved the problem: “Consequent of an if-statement must be an unconditional statement” 110 Algol’s lexical and syntactic structures became so popular that virtually all languages designed since have been “Algol-like”. Outline 111 Syntactic Structures Descriptive tools: BNF Elegance Evaluation and Epilog Various Descriptive tools 112 1. Example: numeric denotations Giving Examples: Integers such as: ‘-273’ Fractions such as: ‘3.76564’ Scientific notations: ‘6.021023’ Gives a clear idea, But not so precise. Various Descriptive tools 113 2. 1. 2. 3. 4. 5. 6. 7. 8. Example: numeric denotations English Description: A digit is either 0,1,2,…,9. An unsigned integer is a sequence of one or more digits. An integer is either a positive sign followed by an unsigned integer or … . A decimal fraction is a decimal point immediately followed by an unsigned integer. An exponent part is a subten symbol immediately followed by an integer A decimal number has one of three forms: … . A unsigned number has one of three forms: … . Finally, a number … . Precise, But not so clear! Various Descriptive tools 114 3. Backus-Naur Form (BNF) Backus Formal Syntactic Notation 115 How to combine perceptual clarity and precision? Example: FORTRAN subscript expressions c v v + c or v - c c*v c * v + c′ or c * v - c′ Backus Formal Syntactic Notation 116 How to combine perceptual clarity and precision? Example: FORTRAN subscript expressions c v v + c or v - c c*v c * v + c′ or c * v - c′ The formulas make use of syntactic categories! Naur’s Adaptation of the Backus Notation 117 BNF represents: Particular symbols by themselves, And classes of strings (syntactic categories) by phrases in angle brackets. <decimal fraction> ::== .<unsigned integer> Describing Alternates in BNF 118 Example: alternative forms <integer> ::== +<unsigned integer> | -<unsigned integer> | <unsigned integer> Describing Repetition in BNF 119 Example: recursive definition <unsigned integer> ::== <digit> | <unsigned integer><digit> Extended BNF 120 How to directly express the idea “sequence of …”? Kleene cross Kleene star <unsigned integer> ::== <digit>+ <identifier> ::== <letter>{<letter>|<digit>}* <integer> ::== [+/-]<unsigned integer> Extended BNF 121 How to directly express the idea “sequence of …”? Kleene cross Kleene star Why preferable? Remember the Structure principle! Mathematical Theory of PLs 122 Chomsky type 0, or recursively enumerable languages Chomsky type 1, or context-sensitive languages Chomsky type 2, or context-free languages Chomsky type 3, or regular languages Mathematical Theory of PLs 123 Languages described by BNF are context-free. →Mathematical analysis of the syntax and grammar of PLs. Outline 124 Syntactic Structures Descriptive tools: BNF Elegance Evaluation and Epilog Design Has Three Dimensions 125 Scientific Social Efficiency Symbolic Economy Elegance Efficiency 126 Efficiency seeks to minimize resources used. Resources : Memory usage Processing time Compile time Programmer typing time Economy 127 Economy seeks to maximize social benefit compared to its cost. The public: The programming community. Trade offs are hard to make since values change unpredictably. Social factors are often more important than scientific ones. (E.g. major manufacturers, prestigious universities, influential organizations) Elegance 128 The feature interaction problem. It is impossible to analyze all the possible feature interactions. All we need to do is to restrict our attention to designs in which feature interactions look good. Designs that look good will also be good. Good designers choose to work in a region of the design in which good designs look good. A sense of elegance is acquired through design experience. Design, criticize, revise, discard! Outline 129 Syntactic Structures Descriptive tools: BNF Elegance Evaluation and Epilog Evaluation 130 Algol-60 never achieved widespread use. Algol had no input-output Little uniformity among input-output conventions. It was decided that input-output would be accomplished by using library procedures. Eventually several sets of input-output procedures were designed for Algol: It was too late! Algol directly competed with FORTRAN 131 Same application area. FORTRAN had gained considerable ground. More focus on reductive aspects of Algol. IBM decided against supporting Algol. Algol became an almost exclusively academic language. A Major Milestone 132 Introduced programming language terminology. Influenced most succeeding languages. An over general language: Quest for simplicity that resulted in Pascal. The basis of several extension, like Simula. Computer architects began to support Algol implementation. Second Generation Programming Languages 133 Algol-60: The first second generation programming language. Data structures are close to first-generation structures. Hierarchically nested name structures. Structured control structures (e.g. recursive procedures, parameter passing modes). Shifted from free formats. 134 The End!