+ All Categories
Home > Documents > Laboratorio di Programmazione 3

Laboratorio di Programmazione 3

Date post: 03-Feb-2022
Category:
Upload: others
View: 3 times
Download: 0 times
Share this document with a friend
23
Laboratorio di Programmazione III La famiglia dei linguaggi Algol e ML Massimo Tivoli
Transcript

Laboratorio di Programmazione III

La famiglia dei linguaggi Algol e ML

Massimo Tivoli

Language Sequence

Algol 60

Algol 68

Pascal

ML Modula

Lisp

Many other languages: Algol 58, Algol W, Euclid, EL1, Mesa (PARC), … Modula-2, Oberon, Modula-3 (DEC)

C

Algol 60

• Basic Language of 1960 – Proposed as one of the early general-purpose languages

• Simple imperative language + functions • colon-separated sequences of statements

– Successful syntax, BNF -- used by many successors • statement oriented • begin … end blocks (like { … } in C) • if … then … else

– Recursive functions and stack storage allocation – Fewer ad hoc restrictions than Fortran

• General array references: A[ x + B[3]*y]

– Primitive static type system • type discipline was improved by later languages

Algol 60 Sample

real procedure average(A,n);

real array A; integer n;

begin

real sum; sum := 0;

for i = 1 step 1 until n do

sum := sum + A[i];

average := sum/n

end; no ; here

no array bounds

set procedure return value by assignment

no parameter types

Algol oddity

• Question – Is x := x equivalent to doing nothing?

• Interesting answer in Algol integer procedure p;

begin

….

p := p

….

end;

– Assignment here is actually a recursive call

Some trouble spots in Algol 60

• Type discipline improved by later languages – parameter types can be array

• no array bounds

– parameter type can be procedure • no argument or return types for procedure parameter

• Parameter passing methods – Pass-by-name had various anomalies

• “Copy rule” based on substitution, interacts with side effects

– Pass-by-value expensive for arrays

Algol 60 Pass-by-name (1/2)

• Substitute text of actual parameter – Unpredictable with side effects!

• Example procedure inc2(i, j); integer i, j; begin i := i+1; j := j+1 end; inc2 (k, A[k]);

begin k := k+1; A[k] := A[k] +1 end;

Is this what you expected?

Algol 60 Pass-by-name (2/2)

begin integer i; integer procedure sum(i,j); integer i,j; begin integer sm; sm := 0; for i:=1 step 1 until 10 do sm := sm + j; sum := sm end; print(sum(i,i*10)) end

Does it calculate i+j, or not?

• Considered difficult to understand – Idiosyncratic terminology

• types were called “modes”

• arrays were called “multiple values”

– Elaborate type system

– Complicated type conversions

• Fixed some problems of Algol 60 – Eliminated pass-by-name

• Not widely adopted

Algol 68

Algol 68 Modes

• Primitive modes – int – real – char – bool – string – compl (complex) – bits – bytes – sema (semaphore) – format (I/O) – file

• Compound modes arrays structures procedures sets pointers

Rich and structured type system is a major contribution of Algol 68

Other features of Algol 68

• Storage management

– Local storage on stack

– Heap storage, explicit alloc and garbage collection

• similar to Pascal, different from C

• Parameter passing

– Pass-by-value

– Use pointer types to obtain Pass-by-reference

Parameter passing

begin integer i; integer array A[1:2]; procedure P(x); integer x; begin i := x; x := i end i:=1; A[1]:=2; A[2]:=1; P(A[i]); print(i, A[1], A[2]) end by-name -> before: i=1; A[1]=2; A[2]=1; | after: i=2; A[1]=2; A[2]=2; by-value -> before: i=1; A[1]=2; A[2]=1; | after: i=2; A[1]=2; A[2]=1; by-reference -> before: i=1; A[1]=2; A[2]=1; | after: i=2; A[1]=2; A[2]=1;

Pascal

• Revised type system of Algol

– Good data-structuring concepts

• records, variants, subranges

– More restrictive than Algol 60/68

• Procedure parameters cannot have procedure parameters

• procedure P (j, k: integer); allowed

• procedure Q (procedure P(i: integer); j,k: integer); allowed

• procedure T(procedure S(procedure K(i:integer))); not allowed

• Popular teaching language

Limitations of Pascal

• Array bounds part of type procedure p(a : array [1..10] of integer)

procedure p(n: integer, a : array [1..n] of integer)

illegal

Attempt at orthogonal design backfires

– parameter must be given a type – type cannot contain variables

It is, in practice, an unnecessary feature… How could this have happened? Emphasis on teaching, ease the type checking, etc…

Not successful for “industrial-strength” projects

Index type Entrytype

C Programming Language

Designed by Dennis Ritchie for writing Unix

• Evolved from B (Bell Laboratories) – B was an untyped language; C adds some checking

• Relation between arrays and pointers – An array is treated as a pointer to first element

• int *p; • int p[5];

– A[10] is equivalent to ptr dereference *(A+10) – Pointer arithmetic is not common in other languages

• C had a tremendous success in industry

C pointers

• 2 operators:

– * : indirect reference operator;

– & : address operator.

• Example: int x = 3; int *y;

y = &x;

3 0x0012FF7C

. . . . . . .

. . . . . . . . . . .

0x00000000

0xFFFFFFFF

ML

• A mostly functional language with imperative features – function-oriented imperative language

• Typed programming language • Combination of Lisp and Algol-like features

– Expression-oriented – Higher-order functions – Garbage collection – Abstract data types – Module system – Exceptions

• General purpose non-C-like, not OO language – Related languages: Haskell, OCAML, …

Why study ML ?

– Types and type checking

• General issues in static/dynamic typing – e.g., “pointer-to-string” expression

• Type inference – many type declarations can be automatically deduced by the

compiler

• Polymorphism and Generic Programming

– Memory management • Static scope and block structure • Function activation records, higher-order functions

– Control • Exceptions • Tail recursion

History of ML

• Robin Milner • Logic for Computable

Functions – Stanford 1970-71 – Edinburgh 1972-1995

• Meta-Language of the LCF system – Theorem proving – Type system – Higher-order functions

Basic Overview of ML (SML97)

• Interactive compiler: read-eval-print – Compiler infers type before compiling or executing

• Examples - (5+3)-2; > val it = 6 : int - if 5>3 then “Bob” else “Fido”; > val it = “Bob” : string - 5=4; > val it = false : bool - if true then 3 else false; > stdIn:1.1-29.10 Error: types of rule don’t agree [literal]

Overview by Type • Unit

– () : unit (like void in C, as a result for functions defined only for side effects or for functions that do not take arguments as input)

• Booleans – true, false : bool – if … then … else … (types must match, then and else are mandatory) – not, andalso, orelse (the names emphasize the evaluation order)

• Integers – 0, 1, 2, … : int – +, * , … : int * int int and so on …

• Strings – “Austin Powers” : string (sting concatenation: “Austin” ^ “ “ ^ “Powers”)

• Reals – 1.0, 2.2, 3.14159, … : real decimal point used to disambiguate – 4.0+5.1 > val it = 9.1 : real – 4+5.1 > stdIn:1.1-1.6 Error: operator and operand don’t agree [literal] > operator domain: int * int > operand: int * real

Compound Types

• Tuples – (4, 5, “noxious”) : int * int * string – #2(4, 5, “noxious”) val it = 5 : int – #3(4, 5, “noxious”) val it = “noxious” : string

• Lists – nil – 1 :: [2, 3, 4] infix cons notation and all elements must have the same type

• Records – {name = “Fido”, hungry=true} > val it = {name = “Fido”, hungry=true} : {name : string, hungry : bool} – #hungry({name = “Fido”, hungry=true}); > val it = true : boolean

Lists

• [1,2,3,4]; val it = [1,2,3,4] : int list

• [true,false]; val it = [true,false] : bool list

• list of strings, list of functions, etc…

• 3 :: nil; val it = [3] : int list

• 4 :: 5 :: it; val it = [4,5,3] : int list


Recommended