the evolution of programming languages

66
The Evolution of Programming Languages Day 1 Lecturer: Xiao Jia [email protected] The Evolution of PLs 1

Upload: levana

Post on 14-Feb-2016

55 views

Category:

Documents


1 download

DESCRIPTION

The Evolution of Programming Languages. Day 1 Lecturer : Xiao Jia [email protected]. In order to …. Understand why PLs are as they are today Predict how they might develop in the future. If this course is successful …. What is a good PL? - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: The Evolution of Programming Languages

The Evolution of PLs 1

The Evolution of Programming Languages

Day 1Lecturer: Xiao [email protected]

Page 2: The Evolution of Programming Languages

The Evolution of PLs 2

In order to …

• Understand why PLs are as they are today• Predict how they might develop in the

future

Page 3: The Evolution of Programming Languages

The Evolution of PLs 3

If this course is successful …

• What is a good PL?• How should we choose an appropriate PL

for a particular task?• How should we approach the problem of

designing a PL?

Page 4: The Evolution of Programming Languages

The Evolution of PLs 4

Anomalies

Page 5: The Evolution of Programming Languages

The Evolution of PLs 5

Anomalies

• Programs that look as if they ought to work but don’t

• Programs that look as if they shouldn’t work but do

Page 6: The Evolution of Programming Languages

The Evolution of PLs 6

Example: Assignments

• v := E– v is a variable– E is an expression

• a[i] := E• r.f := E• v := top(stack) // get the top• CANNOT:• top(stack) := 6 // replace the top

Page 7: The Evolution of Programming Languages

The Evolution of PLs 7

Exercise

• top(stack) := 6;• Explain why the asymmetry exists.• Can you see any problems that might be

encountered in implementing a function like top?

Page 8: The Evolution of Programming Languages

The Evolution of PLs 8

Example: Parameters & Results

• PLs are fussy about the kinds of objects that can be

• (i) passed as parameters to a function, or• (ii) returned as results by a function

Page 9: The Evolution of Programming Languages

The Evolution of PLs 9

Example: Parameters & Results

• C:• an array can be passed to a function

(only by reference)• a function cannot return an array

Page 10: The Evolution of Programming Languages

The Evolution of PLs 10

Example: Parameters & Results

• Pascal:• Arrays can be passed to a function• (i) either by value• (ii) or by reference• A function cannot return an array

Page 11: The Evolution of Programming Languages

The Evolution of PLs 11

Example: Parameters & Results

• Most PLs do NOT allow types to be passed as parameters

• void sort (type t, t a[]) { … }

• sort(float, scores)

Page 12: The Evolution of Programming Languages

The Evolution of PLs 12

Exercise

• Suppose you added types as parameters to a language

• What other changes would you have to make to the language?

Page 13: The Evolution of Programming Languages

The Evolution of PLs 13

Exercise

• C++ provides templates• Is it equivalent to having types as

parameters?

Page 14: The Evolution of Programming Languages

The Evolution of PLs 14

Example: Data Structures and Files

• (Conceptually) There is NOT a great deal of difference between a DS and a file

• (Accidentally) DS’s live in memory and files live in a disk

• PLs treat DS’s and files in completely different ways

Page 15: The Evolution of Programming Languages

The Evolution of PLs 15

Exercise

• Explain why most PLs treat DS’s and files differently

Page 16: The Evolution of Programming Languages

The Evolution of PLs 16

Example: Arrays

• C/Pascal: size is fixed at compile-time• Algol 60: size is not fixed until run-time• (Algol is even older than C and Pascal)

• What difference does this make to• (i) the implementer of the PL• (ii) the programmers who use the PL

Page 17: The Evolution of Programming Languages

The Evolution of PLs 17

Example: Arrays

• 2D array: a• Access a component: a[i,j] (or a[i][j] in C)• Access a row: a[i]

• Access a column: a[,j]• Why can’t we do this?

Page 18: The Evolution of Programming Languages

The Evolution of PLs 18

Example: Arrays

• Easy to declare rectangular arrays in most PLs

• Why no provision for … arrays?• (i) triangular• (ii) symmetric• (iii) banded• (iv) sparse

Page 19: The Evolution of Programming Languages

The Evolution of PLs 19

Exercise

• Suggest ways of declaring … arrays• (i) triangular• (ii) symmetric• (iii) banded• (iv) sparse

Page 20: The Evolution of Programming Languages

The Evolution of PLs 20

Example: DS’s and Functions

• Pascal/C provide data aggregation• (i) record in Pascal• (ii) struct in C

• A record looks like a small program• (i) it contains data declarations• (ii) cannot contain function declarations

Page 21: The Evolution of Programming Languages

The Evolution of PLs 21

Exercise

• Suggest some applications for records with both data and function components

Page 22: The Evolution of Programming Languages

The Evolution of PLs 22

Example: Memory Management

char *money (int amt){ char buf[16]; sprintf(buf, “%d.%d”, amt/100, amt%100); return buf;}

Page 23: The Evolution of Programming Languages

The Evolution of PLs 23

Exercise

• What’s wrong with the function money ?

• Why is it difficult for a C function to return a string?

Page 24: The Evolution of Programming Languages

The Evolution of PLs 24

Example: Factoring

• In algebra:A x + B x (A + B) x

• In PLs:z = A * x + B * x; z = (A + B) * x;

Page 25: The Evolution of Programming Languages

The Evolution of PLs 25

Example: Factoring

• Most PLs:if (x > PI) y = sin(x) else y = cos(x)

• A few PLs:y = if (x > PI) then sin(x) else cos(x);

• Very few PLs:y = (if (x > PI) then sin else cos)(x);

Page 26: The Evolution of Programming Languages

The Evolution of PLs 26

Example: Returning Functions

• Suppose we allow this:typedef void intint(int);intint addk (int k){ int f (int n) { return n + k; } return f;} int add6 (int) = addk(6);

printf(“%d”, add6(4));

Page 27: The Evolution of Programming Languages

The Evolution of PLs 27

Exercise

• It would be fairly easy to change C so that a function could return a function.

Page 28: The Evolution of Programming Languages

The Evolution of PLs 28

Exercise

• It would be fairly easy to change C so that a function could return a function.

• TRUE or FALSE ?

Page 29: The Evolution of Programming Languages

The Evolution of PLs 29

Exercise

• It would be fairly easy to change C so that a function could return a function.

• TRUE or FALSE ?

• You can represent the function itself by a pointer to its first instruction.

Page 30: The Evolution of Programming Languages

The Evolution of PLs 30

Example: Functions as Values

if (x > PI) f = sin else f = cos;f(x);

Page 31: The Evolution of Programming Languages

The Evolution of PLs 31

Exercise

if (x > PI) f = sin else f = cos;f(x);

• Why are statements like this permitted in C (with appropriate syntax) but not in Pascal?

Page 32: The Evolution of Programming Languages

The Evolution of PLs 32

Example: Constraint Functions

• The function Add(x,y,z) attempts to satisfy the constraint x+y=z

• Add(2,3,n) assigns 5 to n

• Add(m,3,5) assigns 2 to m

Page 33: The Evolution of Programming Languages

The Evolution of PLs 33

Example: Logic Computation

• It would be convenient if we could encode logical assertions

forall (int i = 1; i < N; i++)a[i-1] < a[i]

exists (int i = 0; i < N; i++)a[i] == k

Is it convenient? Find an application?

Page 34: The Evolution of Programming Languages

The Evolution of PLs 34

Example: Implicit Looping

• It is well known that …• we can rewrite programs with loops as

programs using recursive functions

• (?) eliminate the recursion as well

Page 35: The Evolution of Programming Languages

The Evolution of PLs 35

Example: Implicit Looping

• Eliminate the recursion:typedef TYPE …TYPE fac (int n, TYPE f){ if (n <= 1) return 1; else return n * f(n-1, f);}printf(“%d”, fac(3, fac));

Page 36: The Evolution of Programming Languages

The Evolution of PLs 36

Example: Implicit Looping

• Eliminate the recursion

fac(3,fac) = 3 * fac(2,fac) = 3 * 2 * fac(1,fac) = 3 * 2 * 1 = 6

Page 37: The Evolution of Programming Languages

The Evolution of PLs 37

Exercise

typedef TYPE …TYPE fac (int n, TYPE f){ if (n <= 1) return 1; else return n * f(n-1, f);}printf(“%d”, fac(3, fac));• Complete the definition of TYPE

Page 38: The Evolution of Programming Languages

The Evolution of PLs 38

PLs affect the way we think

• It would not occur to most programmers to write programs in the style of the preceding sectionsbecausemost PLs do NOT permit these constructions

Page 39: The Evolution of Programming Languages

The Evolution of PLs 39

Theoretical Issues

Page 40: The Evolution of Programming Languages

The Evolution of PLs 40

Syntactic & Lexical Issues

• Fact: C++ is not context-free

• Exercise:• Give examples to demonstrate the

difficulties that the C++ preprocessor causes in a program development environment

Page 41: The Evolution of Programming Languages

The Evolution of PLs 41

Semantics

• Axiomatic• Denotational• Operational

Page 42: The Evolution of Programming Languages

The Evolution of PLs 42

Type Theory

• A function declaration:• T f(S x) { B; return y; }

• If there’s a type theory associated with the PL, we should be able to prove a theorem:

• If x has type S then the evaluation of f(x) yields a value of type T.

Page 43: The Evolution of Programming Languages

The Evolution of PLs 43

Type Theory

• Theorem: If x has type S then the evaluation of f(x) yields a value of type T.

• If we can do this for all legal programs in language L, then L is statically typed

Page 44: The Evolution of Programming Languages

The Evolution of PLs 44

Type Theory

• If L is indeed statically typed:• (i) A compiler for L can check the type

correctness of all programs.• (ii) A program that is type-correct will not

fail because of a type error when it is executed

Page 45: The Evolution of Programming Languages

The Evolution of PLs 45

Exercise

• Give an example of an expression or statement in Pascal or C that contains a type error that the compiler cannot detect.

Page 46: The Evolution of Programming Languages

The Evolution of PLs 46

Regular Languages

• sequence• choice• repetition

Page 47: The Evolution of Programming Languages

The Evolution of PLs 47

REs and Control Structures

RE Control Structurex statementrs statement ; statementr* while expression do statementr+s if condition then statement

else statement

Page 48: The Evolution of Programming Languages

The Evolution of PLs 48

REs and Data Structures

RE Data Structurex int n;rs struct { int n; float y; }rn int a[n];r* int a[]r+s union { int n; float y; }

Page 49: The Evolution of Programming Languages

The Evolution of PLs 49

The Procedural Paradigm

Page 50: The Evolution of Programming Languages

The Evolution of PLs 50

Early DaysLocation Order100 A 104101 A 2102 T 104103 H 24104 C 50105 T 104

(address 104)

Page 51: The Evolution of Programming Languages

The Evolution of PLs 51

Early Days

A a3A 2T a3H 24

a3) C 50T a3

(symbol a3)

Page 52: The Evolution of Programming Languages

The Evolution of PLs 52

FORTRAN

• 1957• IBM• John Backus

Page 53: The Evolution of Programming Languages

The Evolution of PLs 53

• “The IBM Mathematical Formula Translation System or briefly, FORTRAN, will comprise a large set of programs to enable the IBM 704 to accept a concise formulation of a problem in terms of a mathematical notation and to produce automatically a high-speed 704 program for the solution of the problem.”

Page 54: The Evolution of Programming Languages

The Evolution of PLs 54

Major Achievements

• efficient compilation• separate compilation

(programs as separate subroutines, but the compiler doesn’t check for consistency between components)

• demonstration that high-level programming, with automatic translation to machine code, is feasible

Page 55: The Evolution of Programming Languages

The Evolution of PLs 55

Principal Limitations

• Flat, uniform structure– no concept of nesting

• Limited control structures– no compound statements

• Unsafe memory allocation– do NOT check consistent usage of memory

• No recursion– allocate data statically

Page 56: The Evolution of Programming Languages

The Evolution of PLs 56

Exercise

• The FORTRAN 1966 Standard stated that a FORTRAN implementation may allow recursion but is not required to do so.

• How would you interpret this statement if you were:

• (i) writing a FORTRAN program?• (ii) writing a FORTRAN compiler?

Page 57: The Evolution of Programming Languages

The Evolution of PLs 57

Algol 60

• Goal: universal PL• Algol was a failure

– few compilers, not widely used• Algol was a success

– standard language for describing algorithms

Page 58: The Evolution of Programming Languages

The Evolution of PLs 58

Major Innovations

• Block Structure– block: introduce nested scopes– runtime entity: activation record (AR) on stack

• Dynamic Arrays (discussed later)– dope vector: a pointer and an integer (size)

• Call By Name (discussed later)

• Own Variables– keyword: own– analogy: static in C++ (within a function)

Page 59: The Evolution of Programming Languages

The Evolution of PLs 59

Exercise

• Own Variables:• local scope• global extent

• Discuss the initialization of own variables.

Page 60: The Evolution of Programming Languages

The Evolution of PLs 60

Dynamic Arrays

procedure average (n); integer n; begin real array a[1:n]; … end;

Page 61: The Evolution of Programming Languages

The Evolution of PLs 61

Call By Name

procedure count (n); integer n; begin n := n + 1 end

count(widgets) begin widgets := widgets + 1 end

Page 62: The Evolution of Programming Languages

The Evolution of PLs 62

Call By Name

integer procedure sum (max, i, val); integer max, i, val; begin integer s; s := 0; for i := 1 until n do s := s + val; sum := s end

sum(3, i, a[i])computes

a[1]+a[2]+a[3]

Page 63: The Evolution of Programming Languages

The Evolution of PLs 63

Call By Name

try(x > 0, 1.0 / x)

Page 64: The Evolution of Programming Languages

The Evolution of PLs 64

Call By Name

try(x > 0, 1.0 / x)

real procedure try(b, x); boolean b; real x; begin try := if b then x else 0.0 end

Page 65: The Evolution of Programming Languages

The Evolution of PLs 65

Exercise

integer procedure sum (max, i, val); integer max, i, val; begin integer s; s := 0; for i := 1 until n do s := s + val; sum := s end

sum(3, i, a[i])computes

a[1]+a[2]+a[3]

Why does i appear in the parameter list of sum?

Page 66: The Evolution of Programming Languages

The Evolution of PLs 66

still more …

• come back tomorrow