CS 242 Language Sequence The Algol Family and ML Lisp Algol 60 Algol 68 John Mitchell Pascal ML Modula Many other languages: Algol 58, Algol W, Euclid, EL1, Mesa (PARC), … Modula-2, Oberon, Modula-3 (DEC) Algol 60 Algol 60 Sample � Basic Language of 1960 real procedure average(A,n); no array bounds • Simple imperative language + functions real array A; integer n; • Successful syntax, BNF -- used by many successors begin – statement oriented real sum; sum := 0; – Begin … End blocks (like C { … } ) for i = 1 step 1 until n do – if … then … else • Recursive functions and stack storage allocation sum := sum + A[i]; • Fewer ad hoc restrictions than Fortran no ; here average := sum/n – General array references: A[ x + B[3]* y] end; • Type discipline was improved by later languages • Very influential but not widely used in US set procedure return value by assignment Algol Joke Some trouble spots in Algol 60 � Question � Type discipline improved by later languages • Is x := x equivalent to doing nothing? • parameter types can be array � Interesting answer in Algol – no array bounds • parameter type can be procedure integer procedure p; – no argument or return types for procedure parameter begin � Parameter passing methods … . • Pass-by -name had various anomalies p := p – “Copy rule” based on substitution, interacts with side effects … . • Pass-by -value expensive for arrays end; � Some awkward control issues • Assignment here is actually a recursive call • goto out of block requires memory management 1
Algol 60 Pass-by-name Algol 68 � Substitute text of actual parameter � Considered difficult to understand • Unpredictable with side effects! • Idiosyncratic terminology � Example – types were called “modes” – arrays were called “multiple values” procedure inc2(i, j); • v W grammars instead of BNF integer i, j; – context-sensitive grammar invented by A. van Wijngaarden begin begin • Elaborate type system i := i+ 1; k := k+ 1; • Complicated type conversions A[ k] := A[ k] + 1 j := j+ 1 � Fixed some problems of Algol 60 end; end; • Eliminated pass-by -name inc2 (k, A[k]); � Not widely adopted Is this what you expected? Algol 68 Modes Other features of Algol 68 � Primitive modes � Compound modes � Storage management • int • arrays • Local storage on stack • real • structures • Heap storage, explicit alloc and garbage collection • char • procedures � Parameter passing • bool • sets • Pass-by -value • string • pointers • Use pointer types to obtain Pass-by -reference • compl (complex) � Assignable procedure variables • bits Rich and structured • Follow “orthogonality ” principle rigorously • bytes type system is a • sema (semaphore) major contribution of • format (I / O) Algol 68 Source: Tanenbaum , Computing Surveys • file Pascal Limitations of Pascal � Revised type system of Algol � Array bounds part of type illegal • Good data-structuring concepts procedure p(a : array [1..10] of integer) – records, variants, subranges procedure p(n: integer, a : array [1..n] of integer) • More restrictive than Algol 60/68 • Attempt at orthogonal design backfires – Procedure parameters cannot have procedure parameters – parameter must be given a type � Popular teaching language – type cannot contain variables � Simple one-pass compiler How could this have happened? Emphasis on teaching � Not successful for “industrial-strength” projects • Kernighan -- Why Pascal is not my favorite language • Left niche for C; niche has expanded!! 2
ML Goals in study of ML � Survey a modern procedural language � Typed programming language � Discuss general programming languages issues � Intended for interactive use • Types and type checking � Combination of Lisp and Algol- like features – General issues in static/dynamic typing • Expression-oriented – Type inference • Higher-order functions – Polymorphism and Generic Programming • Memory management • Garbage collection – Static scope and block structure • Abstract data types – Function activation records, higher-order functions • Module system • Control • Exceptions – Force and delay – Exceptions � General purpose non-C-like, not OO language – Tail recursion and continuations History of ML Logic for Computable Functions � Robin Milner � Dana Scott, 1969 � Logic for Computable • Formulate logic for proving properties of typed functional programs Functions � Milner • Stanford 1970-71 • Project to automate logic • Edinburgh 1972 -1995 • Notation for programs � Meta-Language of the • Notation for assertions and proofs LCF system • Need to write programs that find proofs • Theorem proving – Too much work to construct full formal proof by hand • Type system • Make sure proofs are correct • Higher-order functions LCF proof search Tactics in ML type system � Tactic: function that tries to find proof � Tactic has a functional type tactic : formula → proof � Type system must allow “failure” succeed and return proof tactic(formula) = search forever fail succeed and return proof tactic(formula) = search forever fail and raise exception � Express tactics in the Meta-Language (ML) � Use type system to facilitate correctness 3
Function types in ML Higher-Order Functions f : A → B means � Tactic is a function for every x ∈ A, � Method for combining tactics is a function on some element y= f(x) ∈ B functions � Example: f(x) = run forever terminate by raising an exception f(tactic 1 , tactic 2 ) = λ formula. try tactic 1 (formula) In words, “if f(x) terminates normally, then f(x) ∈ B.” else tactic 2 (formula) Addition never occurs in f(x)+ 3 if f(x) raises exception. This form of function type arises directly from motivating application for ML. Integration of type system and exception mechanism mentioned in Milner’s 1991 Turing Award. Basic Overview of ML Overview by Type � Interactive compiler: read-eval-print � Booleans • Compiler infers type before compiling or executing • true, false : bool Type system does not allow casts or other loopholes. • if … then … else … (types must match) � Examples � Integers - (5+ 3)-2; • 0, 1, 2, … : int • + , * , … : int * int → int > val it = 6 : int and so on … � Strings - if 5> 3 then “Bob” else “Fido” ; > val it = “Bob” : string • “Austin Powers” - 5= 4; � Reals > val it = false : bool • 1.0, 2.2, 3.14159, … decimal point used to disambiguate Compound Types Patterns and Declarations � Patterns can be used in place of variables � Tuples < pat> ::= < var> | <tuple> | < cons> | < record> … • (4, 5, “noxious”) : int * int * string � Value declarations � Lists • General form • nil val < pat> = < exp> • 1 :: [2, 3, 4] infix cons notation • Examples � Records val myTuple = (“Conrad”, “Lorenz”); • { name = “Fido”, hungry= true} val (x,y) = myTuple; : { name : string, hungry : bool} val myList = [1, 2, 3, 4]; val x::rest = myList; • Local declarations let val x = 2+3 in x*4 end; 4
Functions and Pattern Matching Map function on lists � Anonymous function � Apply function to every element of list • fn x = > x+ 1; like Lisp lambda fun map (f, nil) = nil � Declaration form | map (f, x::xs) = f(x) :: map (f,xs) ; • fun < name> < pat 1 > = < exp 1 > map (fn x = > x+ 1, [1,2,3]); [2,3,4] | < name> < pat 2 > = < exp 2 > … � Compare to Lisp | < name> < pat n > = < exp n > … (define map � Examples (lambda (f xs) • fun f (x,y) = x+ y; actual par must match pattern (x,y) (if (eq? xs ()) () • fun length nil = 0 (cons (f (car xs)) (map f (cdr xs))) | length (x::s) = 1 + length(s); ))) More functions on lists More efficient reverse function � Reverse a list fun reverse xs = let fun rev (nil, z) = (nil, z) fun reverse nil = nil | rev(y::ys , z) = rev(ys , y::z) | reverse (x::xs) = append ((reverse xs), [x]); val (u,v) = rev(xs,nil) � Append lists in v fun append(nil, y s) = y s end; | append(x::xs, y s) = x :: append(xs, y s); � Questions • How efficient is reverse? 1 3 • Can you do this with only one pass through list? 2 2 2 2 3 3 1 3 1 1 Datatype Declarations Datatype and pattern matching � General form � Recursively defined data structure datatype < name> = < clause> | … | < clause> datatype tree = leaf of int | node of int* tree* tree < clause> ::= < constructor> | < contructor> of < type> 4 node(4, node(3,leaf(1), leaf(2)), � Examples node(5,leaf(6), leaf(7)) • datatype color = red | yellow | blue 3 5 ) – elements are red, yellow, blue • datatype atom = atm of string | nmbr of int 1 2 6 7 � Recursive function – elements are atm(“A”), atm(“B”), … , nmbr(0), nmbr(1), ... • datatype list = nil | cons of atom* list fun sum (leaf n) = n – elements are nil, cons(atm(“A”), nil), … | sum (node(n,t1,t2)) = n + sum(t1) + sum(t2) cons(nmbr(2), cons(atm(“ugh”), nil)), ... 5
Recommend
More recommend