section 4: algol evolution of software languagessoft.vub.ac.be › ~tjdhondt › esl › algol_files...
TRANSCRIPT
Evolution of Software Languages 1
Evolution of
Software Languages
Theo D'Hondt
Bachelor of Computer Science Faculty of Sciences and Bio-Engineering Sciences
Vrije Universiteit Brussel Academic Year 2015-2016
Section 4: ALGOL
4: ALGOL
Evolution of Software Languages 2 4: ALGOL
Peter Naur - Edsger Dijkstra
https://en.wikipedia.org/wiki/Peter_Naur
https://en.wikipedia.org/wiki/Edsger_W._Dijkstra
https://en.wikipedia.org/wiki/John_Backus
Evolution of Software Languages 3 4: ALGOL
Overview
The first block-structured language: ALGOL 60
• developed as of 1955 by ACM - GAMM
• universal language for scientific computer processing
• not commercially inspired
• systematically conceived
• systematically documented using a formal grammar (BNF), report < 20p.
Evolution of Software Languages 4 4: ALGOL
ALGOL report
http://web.eecs.umich.edu/~bchandra/courses/papers/Naure_Algol60.pdf
Evolution of Software Languages 5 4: ALGOL
BNF
syntax ::= { rule } rule ::= identifier "::=" expression expression ::= term { "|" term } term ::= factor { factor } factor ::= identifier | quoted_symbol | "(" expression ")" | "[" expression "]" | "{" expression "}" identifier ::= letter { letter | digit } quoted_symbol ::= """ { any_character } """
http://cui.unige.ch/db-research/Enseignement/analyseinfo/AboutBNF.html
http://ebooks.sohuvv.com/search.php?req=PASCAL%20User%20Manual%20and%20Report&nametype=orig
s t a t e m e n t
~~-~~
lock
. . . . . . ] =
rog tawl
i 0
Evolution of Software Languages 6 4: ALGOL
Example1COMPLEX: ≠BEGIN≠ ≠COMMENT≠ THIS PROGRAM COMPUTES THE SQUARE ROOT OF A COMPLEX NUMBER, REPRESENTED BY (X,Y);
≠REAL≠ ≠PROCEDURE≠ CABS(X,Y); ≠COMMENT≠ ABSOLUTE VALUE OF COMPLEX NUMBER; ≠VALUE≠ X,Y; ≠REAL≠ X,Y; ≠BEGIN≠ X := ABS(X); Y := ABS(Y); CABS := ≠IF≠ X = 0 ≠THEN≠ Y ≠ELSE≠ ≠IF≠ Y = 0 ≠THEN≠ X ≠ELSE≠ ≠IF≠ X > Y ≠THEN≠ X * SQRT(1 + (Y/X)**2) ≠ELSE≠ Y * SQRT(1 + (X/Y)**2) ≠END≠ CABS;
≠BEGIN≠ ≠COMMENT≠ MAIN PROGRAM; ≠REAL≠ A,B,X,Y; ≠INTEGER≠ OUTP; OUTP := 61; X := 3.0; Y := 4.0; CSQRT(X,Y,A,B); OUTPUT(OUTP, ≠(≠2(2B,+7D.DD)≠)≠,A,B) ≠END≠ MAIN ≠END≠
≠PROCEDURE≠ CSQRT(X,Y,A,B); ≠COMMENT≠ SQUARE ROOT OF A COMPLEX NUMBER; ≠VALUE≠ X,Y; REAL X,Y,A,B; ≠BEGIN≠ ≠IF≠ X = 0 ≠AND≠ Y = 0 ≠THEN≠ A := B := 0 ≠ELSE≠ ≠BEGIN≠ A := SQRT((ABS(X)+CABS(X,Y))*0.5); ≠IF≠ X >= 0 ≠THEN≠ B := Y/(A+A) ≠ELSE≠ ≠BEGIN≠ B := ≠IF≠ Y < 0 ≠THEN≠ -A ≠ELSE≠ A; A := Y/(B+B) ≠END≠ ≠END≠ ≠END≠ CSQRT;
↪︎
↪︎
↪︎
↪︎
CDC version (1980)
Evolution of Software Languages 7 4: ALGOL
Example2
begin comment MAIN PROGRAM; real A,B,X,Y; X := 3.0; Y := 4.0; CSQRT(X,Y,A,B); printnln(A); printnln(B) end end
begin comment THIS PROGRAM COMPUTES THE SQUARE ROOT OF A COMPLEX NUMBER, REPRESENTED BY (X,Y);
real procedure CABS(X,Y); comment ABSOLUTE VALUE OF COMPLEX NUMBER; value X,Y; real X,Y; begin X := abs(X); Y := abs(Y); CABS := if X = 0 then Y else if Y = 0 then X else if X > Y then X * sqrt(1 + (Y/X)^2) else Y * sqrt(1 + (X/Y)^2) end;
procedure CSQRT(X,Y,A,B); comment SQUARE ROOT OF A COMPLEX NUMBER; value X,Y; real X,Y,A,B; begin if X = 0 & Y = 0 then A := B := 0 else begin A := sqrt((abs(X)+CABS(X,Y))*0.5); if X >= 0 then B := Y/(A+A) else begin B := if Y < 0 then -A else A; A := Y/(B+B) end end end;
↪︎
↪︎
↪︎
↪︎
Racket version (2016)
Evolution of Software Languages 8 4: ALGOL
Block structureALGOL 60 is the first ‘block structured’ language
• control structures can be nested • commands are still declarative and imperative • there are three kinds of declarations:
• variables • procedures • ‘switches’
• imperative commands comprehend: • computational commands • control structures
Evolution of Software Languages 9 4: ALGOL
Primitive types
• the only types are integer, real and boolean
• variables and functions get a type prefix when they are declared:
REAL a; BOOLEAN b;REAL PROCEDURE x
Evolution of Software Languages 10 4: ALGOL
Blocks• the fundamental constructor is the block • blocks contain declarations and commands:
"begin" { declaration ";"} command {";" command} "end"
• blocks define nested ‘scopes’ • blocks prefixed by a heading and declarations
define a procedure • blocks are used in loops and conditionals in
order to group commands • ALGOL60 opted for static scoping instead of
dynamic scoping
Evolution of Software Languages 11 4: ALGOL
Static scope
begin boolean ok; boolean procedure x; begin x := ok end; ok := true; begin boolean ok; ok := false; output(ch, x) --> false with dynamic scope end --> true with static scope end
Evolution of Software Languages 12 4: ALGOL
Data structures• Specific representation (e.g. precision) must be
annotated outside ALGOL: e.g. options in comments, compiler directives, ...
• Complex numbers are considered non primitive and need to be implemented using procedures
COMPLEX x,y,z x = x*y+z <=> REAL ARRAY x,y,z,t [1:2] ComplexMultiply(x,y,t); ComplexAdd(t,z,x)
• Strings exist in ALGOL only as actual parameters and are only used to communicate with non-ALGOL routines (e.g. I/O)
Evolution of Software Languages 13 4: ALGOL
Primitives
The problem of a new primitive data structure:
• requires a new type name XYZ
• requires a mechanism for declaring variables, arrays and procedures of type XYZ
• requires ‘coercion’ rules with other primitive types
• requires extraction rules between XYZ and other primitive types (e.g. real part of COMPLEX)
Evolution of Software Languages 14 4: ALGOL
Restrictions
• no restrictions on the length of names as e.g. in FORTRAN (max 6 characters)
• no lexical restrictions (e.g. max 19 continuation lines in FORTRAN)
• no syntactical restrictions (e.g. in FORTRAN max. 3 indexes in an array)
• no restrictions on the representation of primitive values (e.g. # significant digits in a REAL)
Evolution of Software Languages 15 4: ALGOL
Restrictions
• no restrictions on the length of names as e.g. in FORTRAN (max 6 characters)
• no lexical restrictions (e.g. max 19 continuation lines in FORTRAN)
• no syntactical restrictions (e.g. in FORTRAN max. 3 indexes in an array)
• no restrictions on the representation of primitive values (e.g. # significant digits in a REAL)
The 0,1, infinity principle
When designing a programming language, the only numbers under consideration are 0, 1
and infinity
Evolution of Software Languages 16 4: ALGOL
Arrays1
The only compound data structures are arrays
• arrays are generalisations of DIMENSION’ed variables in FORTRAN
• arrays are explicitly typed
• arrays are explicitly indexed
boolean
integer
real
array identifier
expression
,
[ : expression ]
,
Evolution of Software Languages 17 4: ALGOL
Arrays2Using (semi-)dynamical arrays
integer size; input(...,size); begin real array x [1:size]; integer count; for count:=1 step 1 until size do input(...,x[size]); for count:=size step -1 until 1 do output(...,x[size]) end
• dimension is limited dynamically => within the scope the dimension is fixed
• flexible arrays as e.g. in ALGOL68 can grow and shrink within their scope
• implemented within the run-time stack
Evolution of Software Languages 18 4: ALGOL
Expressions
• subdivision in Boolean and arithmetic expressions
• rich set of Boolean operations:
¬ ∧ ∨ → ≡ ≥ ≤ > < ¬=
• standard set of arithmetic operations:
+ - * / // ** ≠
• conditional expressions
if boolean expression then
expression
else expression
Evolution of Software Languages 19 4: ALGOL
Control structures1
• generalisation of FORTRAN-structures • there remains a goto-command, requiring explicit
declaration of labels • the if-command has 2 branches • the for-loop is the generalised iteration • in order to have the if- and for-command operate
on more than one statement, the ‘compound statement’ was introduced
• the ‘compound statement ’ and the ‘block’ both use BEGIN and END statement brackets
• declarations are allowed in a ‘block’ but not within a ‘compound statement’
Evolution of Software Languages 20 4: ALGOL
for-loop• very sophisticated:
for i := 3, 7, 11 step 1 until 16, i/2 while i ≥ 1, 2 step i until 32 do output(..., i)
• not free from side-effects:
for i := j step k until l do begin ... if i > (j + l)/2 then i :=l-3*k; ... end
identifier := iteratorfor statementdo
until expression
step expression
while expressionexpression
,
Evolution of Software Languages 21 4: ALGOL
The for command• very sophisticated:
for i := 3, 7, 11 step 1 until 16, i/2 while i ≥ 1, 2 step i until 32 do output(..., i)
• not free from side-effects:
for i := j step k until l do begin ... if i > (j + l)/2 then i :=l-3*k; ... end
Principle of local cost:
One should only pay for what is really used
Evolution of Software Languages 22 4: ALGOL
The switch command1• is a generalisation of the FORTRAN ‘computed goto’ • labels are explicitly enumerated:
switch direction = north, east, south, west
• this defines an array of labels, indexed by ordinal numbers
• the ‘goto’ may then refer to an indexed label:
goto direction[3]
• a kind of ‘case-statement’ is possible: begin switch direction = north, east, south, west; goto direction [x]; north: ... ;goto exit; east : ... ;goto exit; south: ... ;goto exit; west : ... ; exit: end
Evolution of Software Languages 23 4: ALGOL
The switch command2
The goto and the switch are ‘dangerous’ commands:
• they open up possibilities of unclear ‘control-flow’
• labels may have conditional values
switch S = L1, if a>b then L2 else L3, L4;
goto if x>0 then L
• labels are explicitly declared => it is impossible to jump into a block; it is however possible to jump out of a block requiring an update of the ‘run-time stack’
Evolution of Software Languages 24 4: ALGOL
Procedures1
• procedures may be typed and are then considered to be functions:
real procedure Discr(a,b,c)
• procedures can have parameters, necessarily typed:
real procedure Discr(a,b,c); real a,b,c;
Evolution of Software Languages 25 4: ALGOL
Procedures1• parameters are passed ‘call by value’ or
‘call by name’
procedure Discr(a,b,c,D); value a,b,c; real a,b,c,D; begin D := sqrt(b**2-4.0*a*c) end
• function results are transmitted by assignment to the function name:
real procedure Discr(a,b,c); value a,b,c; real a,b,c; begin Discr := sqrt(b**2-4.0*a*c) end
Evolution of Software Languages
procedure S(EL, K); real EL; integer K; begin K := 2; EL := 0.0 end; ... real array A[1:100];integer I; A[1] := A[2] := 1.0; I := 1; S(A,I)
26 4: ALGOL
Call by name1
• formal parameters that are passed ‘call by name’ are re-evaluated (= re-addressed) at each reference in the body of the procedure
in FORTRAN:
in ALGOL60:SUBROUTINE S(EL, K) K = 2 EL = 0.0 RETURN END ... DIMENSION A(2) A(1) = A(2) = 1.0 I = 1 S(A(I),I)
Evolution of Software Languages 27 4: ALGOL
Call by name2How is ‘call by name’ implemented?
• each reference within the body of the procedure to a formal parameter that is passed ‘call by name’ requires a re-evaluation of the corresponding actual parameter expression
• the compiler encapsulates each actual ‘call by name’ parameter expression in a parameterless function object called a ‘thunk’
• when the procedure is called each formal ‘call by name’ parameter is assigned the address of its corresponding thunk
• at each reference to a formal ‘call by name’ parameter the thunk is called and its returned value is used
Evolution of Software Languages 28 4: ALGOL
Call by name3‘call by name’ is dangerous and expensive:
procedure swap(x, y); integer x, y; begin integer z; z := x; x := y; y := z end
begin integer array a[1:10]; a[1] := 10; a[10] := 5; Swap(a[1], 10); Swap(10, a[1]) end
Evolution of Software Languages 29 4: ALGOL
Jensen's devicereal procedure Sum(k, l, u, ak); value l, u; integer k, l, u; real ak; begin real S; S := 0.0; for k := 1 step 1 until u do S := S + ak; Sum := S end;
begin real array x(1:100); integer i,j; real z; z := Sum(i,1,100,Sum(j,1,i,x[i,j]) end
Evolution of Software Languages 30 4: ALGOL
Recursion
• allowed by the ‘nested block’ structure with nested visibility of local variables
• implemented by means of a ‘run-time stack’ holding activations frames of called procedures
• an activation frame holds copies of local variables and value-parameters of the procedures
• the size of an activation frame can not always be determined ‘at compile-time’ due to the possible presence of dynamical arrays
• blocks and procedures can have ‘own’ variables; these are local variables that survive a ‘return’ and are still available in the next call; they are not stored on the run-time stack
Evolution of Software Languages 31 4: ALGOL
Grammar1
• machine-independent format (=> portable)
• free format (<> FORTRAN)
• separation between grammatical elements ("terminal symbols") and lexical elements (names, numbers,...)
• new convention for "layout" (=> indentation)
Evolution of Software Languages 32 4: ALGOL
Grammar2Three levels of specification:
• reference language: language used for the specification of examples in the ALGOL 60 report
a[i+1]:=(a[i]+pi×r ↑ 2)/6.021023
• publication language: language used for the publication of algorithms; a rich typography is allowed:
ai+1 ← { ai + π × r2 } / 6.02 x 102
• machine representation language: mapping of the reference language on the particular alphabet of a given machine
A(.I+1.):=(A(.I.)+PI*R**2)/6.02E23
Evolution of Software Languages 33 4: ALGOL
Grammar3
Many FORTRAN ambiguities have disappeared
• no reserved words
• typical ‘keywords’ : in the machine representation this requires a specific annotation (e.g. escape character)
• no ‘keywords in context’ IF IF THEN
THEN:=ELSE;
ELSE
ELSE:=THEN;
Evolution of Software Languages 34 4: ALGOL
Grammar4
Many FORTRAN ambiguities have disappeared
• no arbitrary limitations such as: • 6 characters / name • 3 indexes/array • <const>*<var>±<const> as index-expression
• still a ‘dangling else’ problem:
if something then if somethingelse then yetsomethingelse else againsomething
Evolution of Software Languages 35 4: ALGOL
Grammar5ALGOL60 is considered to be:
• a general, regular, orthogonal language
• an elegant language
• ‘everything one should be able to do can be done’
• basis for the lexical en grammatical structure of most modern languages
• basis just about everything we now today about compiler construction
Evolution of Software Languages 36 4: ALGOL
Grammar6
ALGOL60 is the beginning of formal grammars:
• The very first introduction of a formalism: BNF
• Is later extended (e.g. EBNF)
• Gives rise to graphical versions (e.g. railroad diagrams)
• Very closely linked to Chomsky’s formalisms for natural languages
• Result: quasi-monopoly of context free languages etc.
Evolution of Software Languages 37 4: ALGOL
Conclusion• ALGOL 60 was never widely used • not a US development • complex terminology • block structure viewed as difficult & inefficient • stack mechanism viewed as inefficient • call-by-name was a mistake • ALGOL 60 did not have any input-output
primitives • ALGOL 60 was a direct competitor for FORTRAN • ALGOL 60 was the most important step in the
evolution of (imperative) programming languages • ALGOL 60 evolved into ALGOL 68
Evolution of Software Languages 38 4: ALGOL
Assignment
1. Simulate call-by-name in Scheme
2. Simulate own variables in C