9/19/13

After • International language is needed – 1964: New language is proposed to break Algol away from platform dependence – Preliminary spec: NPL (new ), then PL/I (programming CS4100 language 1) From Principles of Programming Languages: Design, Evaluation, and Implementation (Third Edition), by – PL/I is too big Bruce J. MacLennan, Chapters 3and 4, and based on • Dijkstra: If Fortran is an infantile disorder, then slides by Istvan Jonyer. PL/I is a fatal disease • Trying to be everything to everyone backfires 1 2

Chapter 3: Generality and Implementations Hierarchy: ALGOL-60 • Because of the hype, many started • An international language is needed implementation quickly – This resulted in many dialects – A single, universal language would be – JOVIAL (Jules’ Own Version of the International valuable Algebraic Language) – International (American and European) • Committee meets again in 1960 to committee is set up to make incorporate suggestions recommendations – Algol-60 is born and is very different from the ’58 report. – Algol-58 is created in 8 days in Zurich, as a – Report is 17 pages long: remarkable achievement, preliminary report mainly due to BNF notation (reports used to – Algol: Algorithmic Language stretch to hundreds or thousands of pages) 3 4

Algol Report Algol’s Objectives • 1959 UNESCO Conference on Information Processing • The language should be very close to – Backus presents a description of Algol ‘58 • Uses formal syntax he developed mathematical notation – Naur is editor of Algol Bulletin • Should be useful in publications to • Disagrees with some of Backus’ interpretation • Need for more precise description describe • Develops a variant of Backus’ formal syntax • Mechanically translatable to machine Backus-Naur Form, aka BNF used for 1960 Algol code Report

5 6

1 9/19/13

Structural Organization Constructs • Declarative or Imperative • Hierarchically structured language – (like in FORTRAN) – Nesting is introduced (Fortran did not use nesting) • Variable declarations – Control structures can also be nested – Types: integer, real, Boolean integer i, j, k • One can be made the body of the other – Lower bounds of arrays need not be 1 if N > 0 then real array Data[-50:50] for i := 1 step 1 until N do – Switch, like FORTRAN’s computed GOTO sum := sum + Data[i] • Subprogram declarations • Advantage: decreases the number of GOTOs required – Keyword: procedure and – Procedures can be typed (functions) and untyped • Reserved words real procedure dist(x1, y1, x2, y2); real x1, y1, x2, y2; dist = sqrt((x1 – x2)^2 + (y1 – y2)^2)

7 8

Imperative Constructs Imperative Constructs

• Computational • Control-flow – Assignment: “variable := expression” – All imperative constructs alter flow of – Operators: control (except assignment) • Arithmetic: +, -, *, etc. – Has if-then-else • Relational: =, <, >, ≥, etc. – for-loop replaces do-loop • Logic: ∧, ∨, ¬, etc. – Why is assignment ‘:=’ and not ‘=’? • No input/output constructs • Assignment is different from definition and – I/O was left to be handled by platform- comparison dependent library calls • I = I + 1 ; I := I + 1 9 10

Name Structures Syntax - Program

• Algol-60 introduces the compound statement • ::= | – Where 1 statement is allowed, more can be used, using begin-end • ::= |

for i := 1 step 1 until N do • ::= begin begin sum := sum + Data[i]; • ::= Print Real (sum) ; end – Also, the body of a procedure is a single statement

11 12

2 9/19/13

Syntax - Statement Syntax - Block • ::= |

::= • ::= | | ; ::= begin | • ::= | | ; ::= | • ::= end |

13 14

Blocks Define Nested Scopes Name Binding • Fortran • Fortran binds a variable to a single memory – Had local and global declarations only location statically – Had to redeclare using COMMON to share • Algol-60 included the results of research done • Algol-60 on name structures, which were problematic – Introduces blocks in Fortran begin – Sharing of data between subprograms declarations; – Parameter passing modes statements end – Return values – Compound statements do not have ‘declarations’. – Dynamic arrays – All declarations are visible to all statements in the block • Result of research: block structure – Since statements can be blocks, scopes can be nested

15 16

Why do we need scopes? “COMMON” with Blocks

• Reduce the context programmers have to keep in • The error-prone COMMON in Fortran can be mind implemented in a much better way using • Make understanding and maintenance of program blocks easier begin • Scopes reduce visibility of names integer array Name, Loc, Type[1:100]; procedure Lookup (n); – Declare variable only where needed and used . . . Lookup procedure . . . • Nested blocks inherit names from outside procedure Var (n, l, t); – It would be very inconvenient if they did not . . . Var procedure . . . procedure Array1 (n, l, t, dim1); . . . Array1 procedure . . . end 17 18

3 9/19/13

Too Much Access Contour Diagrams

• Blocks provide “indiscriminate access” • Inner blocks implicitly inherit access to all variable in immediately surrounding block – Since functions must be accessible to • Names declared in a block are local to the block users, • Names declared in surrounding blocks are nonlocal – and data structures must be accessible to • Names declared in outermost block are global functions – à Data is also accessible to users • Violates information hiding principle

19 20

Contour Diagrams Dynamic vs Static Scoping

• See Figure 3.3, page 102 • Static scoping – Procedure is called in the context of its declaration • Do Exercise 3-1, page 104 • Environment of Definition – structure is determined at compile-time – Algol • Dynamic scoping – Procedure is called in the context of its caller • Environment of Caller – Scope structure is determined at run-time – LISP

21 22

Example Dynamic Scopes and Functions • Draw static contour diagram • Draw dynamic contour diagram for both calls to P • Dynamic scoping applies to all names (not just a:begin variables) integer m outer m procedure P • Advantage: m := 1; – We can write a general procedure that makes use of b:begin procedures in the caller’s environment integer m; inner m • No need to have all names defined in static context P inner call • Disadvantage: end – If caller’s environment provides a different function than P outer call what is intended to be used (see example page 109) end • Programmer has to think about envt when writing calls

23 24

4 9/19/13

Blocks Permit Which one is better? Efficient Storage Management • General rule: • Fortran used EQUIVALENCE – What is natural to humans will cause less problems in the – Not safe, since there is no guarantee of exclusive use of long run memory – If humans can understand static scoping better, than it will • Blocks permit reuse of memory result in higher quality programs in the long run a:begin integer m, n; • Dynamic scoping is confusing b:begin real array X[1:100], real y; – Generally rejected (not used in new languages) ... – Static scoping agrees more with the program’s dynamic end behavior ... c:begin integer k; real array M[0:50]; ... end 25 end 26

Run-Time Stacks Example

• Variables in blocks b and c are never used at the same time • From previous program

• When exiting b, its variables may be discarded y • Notice: Block entered last will be exited first M

– LIFO (last-in first-out) order X

– Can use a stack to organize activation records k – When block is entered, its AR is pushed onto stack n n n – When block is exited, its AR is popped off stack n n enter (a) enter (b) m exit (b) enter (c) m exit (c) m exit (a) – Assumption: No local variables are initialized m m … … … … …

27 28

Responsible Design Principles of Programming

• Algol designers did not include • The Responsible Design Principle EQUIVALENCE – Do not ask programmers what they want, – Programmers might have asked for it… find out what they need. – Instead, they looked at the root of the problem – “Don’t ask what they want, ask how the problem arises” – Language designers are responsible for the features in the language, not programmers 29 30

5 9/19/13

Data Structures Why no complex?

• Primitives • Not primitive – Mathematical scalars, like in Fortran – Can be constructed using other types easily (2 reals) – integer, real, Boolean • Is it easy to use reals for complex? – complex and double not included – Yes, but inconvenient • Double: platform dependent – Need supporting operations – Not portable • ComplexAdd(x, y, z), etc. – Why? Because we need to know the size of a • Designers’ choice: word to know how big double is. – Is it worthwhile to add the complexity/overhead of – Alternate approaches: another type? (conversions, coercion, operator • specify precision overload, etc.) • Let pick precision – Will they get enough use? 31 32

Strings Zero-One-Infinity

• Yet another data structure that needs full support • Programmers should not be required to (operation, etc.) remember arbitrary constants • Algol designers included strings as second-class • Fortran examples citizens – Identifiers have max. 6 characters – string type is only allowed for formal parameters – There are at most 19 continuation cards – String literals can only be actual parameters – No operations – Arrays can have at most 3 dimensions – Strings can only be passed around in procedures • Regularity in Algol requires number of – Cannot actually do anything with them exceptions • What’s the point??? – Gives rise to Zero-One-Infinity principle – String will end up getting passed to output procedure written in a lower (machine) language that can handle it – E.g.: Identifier names should be either 0, 1 or unlimited length. (0 & 1 don’t make much sense) 33 34

Principles of Programming Arrays are Generalized

• Arrays can have any number of dimensions • The Zero-One-Infinity Principle • Lower bound can be number other than 1 – The only reasonable numbers in – More intuitive, and less error prone than fixed lower bound programming language design are zero, • Arrays are dynamic one and infinity. – Array bounds can be given as expressions, which allows recomputation every time the block is entered – Array size is set until block is exited • (Fortran had fixed array sizes.)

35 36

6 9/19/13

Strong Typing Control Structures • Strong typed language – Prevents programmer to perform meaningless operations on data • Primitive statements are similar to – Not to be confused with legitimate type conversions (integer + real (coercion)) Fortran’s • Fortran – Assignment – Weakly typed – Permits adding to a Hollerith constant, etc. – – Equivalence allows setting up the same memory for different types – No input/output • Security and maintenance problem • Intentional type violation is not portable • Exception: System programming (C) – Have to treat memory cells as raw storage without regard to type

37 38

Controls are Generalized: if Controls are Generalized: for • Algol’s for is more general than Fortran’s do for i := 1 step 1 until N do • Fortran had many restrictions sum := sum + Data[i] – if (exp) simple statement – Leading-decision loop: • Statement restricted to GOTO, CALL, or for NewGuess := Improve(OldGuess) while abs(NewGuess – OldGuess) > 0.01 assignment do OldGuess := NewGuess

• Algol removes restrictions – Same as while loop in newer languages: – All statements are allowed (even ‘if’ in NewGuess := Improve(OldGuess); body of if ) while abs(NewGuess – OldGuess) > 0.01 do ‘ ’ begin – ‘else’ added to address false condition OldGuess := NewGuess; NewGuess := Improve(OldGuess); end 39 40

Another Goal: Regularity

for i := 3, 7, 11 step 1 until 16, • Algol was designed around regularity i ÷ 2 while i >= 1, – “Anything that you think you ought to be 2 step i until 32 do able to do, you will be able to do.” print( i ); – Elaboration on zero-one-infinity principle • Remove inexplicable exceptions from the language

3 7 11 12 13 14 15 16 8 4 2 1 2 4 8 16 32

41 42

7 9/19/13

begin … end Example • Algol-58: – All control structures should be allowed to have for i := 1 step 1 until N do any number of statements sum := sum + Data[i] – All control statements were considered an opening bracket, with corresponding closing bracket • if … endif for i := 1 step 1 until N do • Algol-60 begin – Largely due to the BNF notation, they realized that sum := sum + Data[i]; one bracketing mechanism is enough for all – Defined begin-end bracketing Print Real (sum) • Define compound statements end • Makes one statement out of a group of statements • Allowed anywhere a single statement is expected

43 44

begin-end Issues begin-end Has Double Duty • begin-end are used for • Easy to omit begin-end – Compound statements – Especially when single statement is used • Collection of statements is handled as one statement first, then another is added – Blocks • Define nested scopes – Especially the case with well-indented code • Include definitions, in addition to statements for i := 1 step 1 until N do • Any difference? sum := sum + Data[i]; – Compound statements do not need an activation record Print Real (sum) – Compiler must determine whether begin-end has – This is a maintenance problem declarations, and generate block-entry code if so – Good convention: always use bracketing

45 46

Structured Programming Principles of Programming • Compound statements drastically reduce the number of GOTOs required • The Structure Principle – In Fortran, GOTO was the workhorse for control – Example: if-then-else – The static structure of the program should • GOTO-less programs were easier to read correspond in a simple way to the dynamic – This led people to experiment with abolishing GOTO structure of corresponding computations. – Dijkstra: “Go To Statement Considered Harmful” • Difficulty in reading programs came from conceptual gap between static and dynamic structure of program • i.e.: static layout on paper, versus runtime operation • Result: languages still have GOTOs, but we don’t use them

47 48

8 9/19/13

Procedures are Recursive Implementing Recursion

• Recursive definitions are frequent in math and science – Define thing in terms of itself • What happens to local variable n on – Example: recursive call? • Factorial: n! = n * (n – 1)! if n > 0 – fac(3) is called, then fac(2), then fac(1), 1 if n = 0 then fac(0) • Algol permits recursive procedures – Would location holding 3 be overwritten? integer procedure fac(n); value n; integer n; • Yes, if same activation record was used fac := if n = 0 then 1 else n*fac(n-1); – Solution: – ‘n = 0’ is called the stopping condition • Create new activation record for each invocation of fac()

49 50

Parameter Passing Pass by Value

integer procedure fac(n); • Modes in Algol value n; integer n; – Pass by value • First part of pass by value-result (in Fortran) – Actual copied into variable corresponding to formal – Pass by name – Secure; local variable will not overwrite actual parameter • Two modes attempt to distinguish – Does not allow output parameters (input only) – Inefficient for arrays (or other non-primitive data structures, between input only and input/output in general) parameters • Copy must be made of entire array in activation record • Copying takes time

51 52

Pass by Name Copy Rule

• Based on substitution • Procedure can be replaced by its body – Consider integer procedure Inc(n); with actuals substituted for formals integer n; n := n + 1; • Revised Report 4.7.3 – And the call Inc(i) • Body of Inc(n) • We need output parameter that will effect i, not just local n – i := i + 1 – Acts like i is substituted for n – A[k] := A[k] + 1 i := i + 1 • Not how it is implemented

53 54

9 9/19/13

Pass by Name is Powerful “Thunks” • Implementing pass by name • Evaluate the following using pass by value, reference, and name – Passing the text? procedure S(el,k); • Would need to compile at runtime integer el, k; – not possible begin k := 2; – Copying compiled code? el := 0; • Would increase size of code… end A[1] := A[2] := 1; – Solution: “Thunks” i := 1; • Pass address to compiled code S(A[i], i) • Value A[1] = 1, A[2] = 1, i = 1 • Address of memory location is returned to • Reference A[1] = 0, A[2] = 1, i = 2 callee to use as variable • Name A[1] = 1, A[2] = 0, i = 2 55 56

Pass by Name is Dangerous!

procedure Swap(x, y); integer x, y; • Swap(r,s), where r=1,s=2! begin integer t; procedure Swap(x, y); t := x; integer x, y; x := y; begin integer t; y := t; t := r; t=1 end r := s; r=2 • What is the effect of s := t; s=1 – Swap(A[i], i)? – Swap(i, A[i])? end

57 58

• Swap(A[i], i) where A[i]=27, i=1 • Swap(i, A[i]), where i=1, A[i]=27 procedure Swap(x, y); procedure Swap(x, y); integer x, y; integer x, y; begin integer t; begin integer t; t := A[i]; t=27 t := i; t=1 A[i] := i; A[1]=1 i := A[i]; i=27 i := t; i=27 A[i] := t; A[27]=1

end

end

59 60

10 9/19/13

Pass-by-name Parameter Passing Modes • Pass by value • It can be shown that there is no way to – Bind to value at time of call define swap in Algol-60 that works for – Preserves actual (no output parameters) all parameters – Inefficient for arrays • Pass by reference • Design mistake when a simple – Bind to address at time of call (common) procedure has such – Changes actual (can be used for output) – Efficient for all data types surprising properties • Pass by name – Bind to address of thunk at time of call – Changes actual (can be used for output) – Efficient, but expensive 61 62

Out-of-Block GOTOs Even worse… A: begin array x[1:100]; ... begin B: begin array y[1:100]; procedure P(n); ... value n; integer n; goto exit; if n = 0 then goto out ... else P(n-1); end; P(25); exit: end out: • What happens to activation records? end – Program continues in different block • Recursive call 25 times then jump to out! – Pop activation record – Pop 25 activation records – Makes goto complicated • Data dependent, can’t know at compile time 63 – Implement goto as call of run-time routine 64

Feature Interaction The for-loop is Very General • Example: for var := exp step exp2 until exp3 do stat – GOTOs are simple for var := exp while exp2 do stat – Recursion is simple • Expressions can be any arithmetic expression, including – Combination is very messy • for i := i/2 while i>1 do stat – Lists • In theory, each feature must be tested with every • for days := 31, 28, 31,30, 31, 30 do stat other one to avoid unintended consequences – Conditional expressions (vs. conditional statements!) • 100 features: • for days := 31, if mod(year, 4) = 0 then 29 else 28, – Every pair: 100x100 = 10,000 combinations 28, 31, 30, 31, 30 do stat – Every three: 1003 = 1,000,000 – Combinations of above – … • for i := 3, 7, i/2 while i>1, 11 step 1 until 16 do stat

65 66

11 9/19/13

::= |

67 68

Baroque Handling Cases: switch • 1 : of, relating to, or having the characteristics of a style of artistic expression prevalent especially in the 17th century that is begin switch wageStatus = fulltime, parttime, hourly; marked generally by use of complex forms, bold ornamentation, ... and the juxtaposition of contrasting elements often conveying a goto wageStatus[i]; sense of drama, movement, and tension fulltime: ...handle fulltime case... • 2 : characterized by grotesqueness, extravagance, complexity, goto done; parttime: ...handle parttime case... or flamboyance goto done; • 3 : irregularly shaped —used of gems hourly: ...handle hourly case... • baroque. (2009). In Merriam-Webster Online Dictionary. Retrieved April goto done; 28, 2009, from http://www.merriam-webster.com/dictionary/baroque done: ... end; • Elaboration on computed GOTO of Fortran (and IBM 650) • Confusing, since switch, goto, and labels can be anywhere in the program • Label list can contain conditionals (if i>0 then M else N)

69 70

Machine Independence Machine Independence

• Get away from formats tied to particular computers, • Representation issues punch cards -> free format – Different hardware • How should a program be formatted? – Left justify, one statement per line • Input devices – Like English sentence • Character sets – Structured (hierarchical) – Different conventions • Obeys structure principle • Math vs cs • Most languages followed Algol in free format • American vs European • Comma (European) vs point (American) almost defeats Algol

71 72

12 9/19/13

Machine Independence Compromise

• Theorem: The more trivial the point the • Three representations – Reference language used in language specifications more vehemently people will fight over it • E.g. “up arrow” • Which symbols – Publication language used in publications • E.g. sub- and super-scripts – Only those available in all sets – Hardware language to be used by implementers • Too limiting • Use appropriate character set • I/O for the computer system – Independent of particular sets • chosen

73 74

Lexical Conventions Keywords in context

• Reserved words • FORTRAN – Cannot be used as identifiers • Words used by language are only keywords in – Most languages context where expected • Key words – Hard to catch errors – Words used by language are marked • Legal in PL/I • E.g. Different font or bold IF IF THEN • Hard to type THEN = 0; – Algol ELSE • Keywords in context ELSE = 0;

75 76

Some Design Considerations Efficiency

• From David Billington, The Tower and • Materials used the Bridge 1993 • Scientific Issue • Techological Activities • Memory – Values • Dimensions • Time • Efficiency • Scientific – Programmer • Economy • Social • Elegance • Symbolic – Compiler

– Run

77 78

13 9/19/13

Economy Economy

• Cost-benefit • Social Influences • Social Issue – Manufacturer support – Prestigious universities teach • Benefit to programming community – Approved by influential organizations • Cost: trade-offs – Standardized – Computer vs programmer time – Used by “real” programmers – Increasing cost of residual bugs • Monetary values are unstable as is – Program maintenance vs development social climate

79 80

Elegance Elegance

• Under-engineered • General Principle: Designs that look – Risk of unanticipated interactions good are good • Over-engineered – Inefficient or uneconomical • Function follows form • Can’t always rely solely on mathematical analysis – But needs to be deep (not superficial) – Always incomplete • Simplifications • Should be a joy to use • assumptions – Comfortable and safe

81 82

Elegance In Summary, Algol

• Aesthetics comes from experience • Never had widespread use – No I/O • Design obsessively – Competing directly with FORTRAN – Criticize • Major milestones – Block-structured – Revise – Nested – Discard – Recursive – Free-form – BNF - mathematical theory of formal languages

83 84

14 9/19/13

Algol by reputation Second Generation

• General • Elaborations and generalizations of first generation • Regular – Strong typing of built-in types • Elegant – Name structures hierarchically nested – Structured control structures • Orthogonal • Recursion • Parameter passing – Syntactic structures • Machine independent • Moving away from fixed formats

85 86

15