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), … Reading: Chapter 5 Modula-2, Oberon, Modula-3 (DEC) Algol 60 Algol 60 Sample � Basic Language of 1960 real procedure average(A,n); • Simple imperative language + functions real array A; integer n; no array bounds • 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 average := sum/n no ; here – 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 – no array bounds � Interesting answer in Algol • 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); • vW 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 j := j+1 A[k] := A[k] +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
C Programming Language ML � Typed programming language Designed for writing Unix by Dennis Ritchie � Intended for interactive use � Evolved from B, which was based on BCPL � Combination of Lisp and Algol-like features • B was an untyped language; C adds some checking • Expression-oriented � Relation between arrays and pointers • Higher-order functions • An array is treated as a pointer to first element • Garbage collection • E1[E2] is equivalent to ptr dereference *((E1)+(E2)) • Abstract data types • Pointer arithmetic is not common in other languages • Module system � Ritchie quote • Exceptions • “C is quirky, flawed, and a tremendous success.” � General purpose non-C-like, not OO language • Related languages: Haskell, OCAML, … Why study ML ? History of ML � Learn an important language that’s different � Robin Milner � Discuss general programming languages issues � Logic for Computable • Types and type checking Functions – General issues in static/dynamic typing • Stanford 1970-71 – Type inference • Edinburgh 1972-1995 – Polymorphism and Generic Programming • Memory management � Meta-Language of the – Static scope and block structure LCF system – Function activation records, higher-order functions • Theorem proving • Control • Type system – Force and delay – Exceptions • Higher-order functions – Tail recursion and continuations Logic for Computable Functions LCF proof search � Dana Scott, 1969 � Tactic: function that tries to find proof • Formulate logic for proving properties of typed functional programs succeed and return proof � Milner tactic(formula) = search forever • Project to automate logic fail • Notation for programs • Notation for assertions and proofs • Need to write programs that find proofs � Express tactics in the Meta-Language (ML) – Too much work to construct full formal proof by hand � Use type system to facilitate correctness • Make sure proofs are correct 3
Tactics in ML type system Function types in ML f : A → B means � Tactic has a functional type for every x ∈ A, tactic : formula → proof � Type system must allow “failure” some element y=f(x) ∈ B f(x) = run forever terminate by raising an exception succeed and return proof tactic(formula) = search forever fail and raise exception In words, “if f(x) terminates normally, then f(x) ∈ B.” 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. Higher-Order Functions Basic Overview of ML � Tactic is a function � Interactive compiler: read-eval-print � Method for combining tactics is a function on • Compiler infers type before compiling or executing functions Type system does not allow casts or other loopholes. � Examples � Example: - (5+3)-2; f(tactic 1 , tactic 2 ) = > val it = 6 : int λ formula. try tactic 1 (formula) - if 5>3 then “Bob” else “Fido”; else tactic 2 (formula) > val it = “Bob” : string - 5=4; > val it = false : bool Overview by Type Compound Types � Booleans � Tuples • true, false : bool • (4, 5, “noxious”) : int * int * string • if … then … else … (types must match) � Lists � Integers • nil • 0, 1, 2, … : int • 1 :: [2, 3, 4] infix cons notation • +, * , … : int * int → int � Records and so on … � Strings • {name = “Fido”, hungry=true} • “Austin Powers” : {name : string, hungry : bool} � Reals • 1.0, 2.2, 3.14159, … decimal point used to disambiguate 4
Patterns and Declarations Functions and Pattern Matching � Patterns can be used in place of variables � Anonymous function <pat> ::= <var> | <tuple> | <cons> | <record> … • fn x => x+1; like Lisp lambda � Value declarations � Declaration form • General form • fun <name> <pat 1 > = <exp 1 > val <pat> = <exp> | <name> <pat 2 > = <exp 2 > … • Examples | <name> <pat n > = <exp n > … val myTuple = (“Conrad”, “Lorenz”); val (x,y) = myTuple; � Examples val myList = [1, 2, 3, 4]; • fun f (x,y) = x+y; actual par must match pattern (x,y) val x::rest = myList; • fun length nil = 0 • Local declarations | length (x::s) = 1 + length(s); let val x = 2+3 in x*4 end; Map function on lists More functions on lists � Apply function to every element of list � Reverse a list fun map (f, nil) = nil fun reverse nil = nil | map (f, x::xs) = f(x) :: map (f,xs); | reverse (x::xs) = append ((reverse xs), [x]); � Append lists map (fn x => x+1, [1,2,3]); [2,3,4] fun append(nil, ys) = ys � Compare to Lisp | append(x::xs, ys) = x :: append(xs, ys); � Questions (define map • How efficient is reverse? (lambda (f xs) • Can you do this with only one pass through list? (if (eq? xs ()) () (cons (f (car xs)) (map f (cdr xs))) ))) More efficient reverse function Datatype Declarations fun reverse xs = � General form let fun rev (nil, z) = (nil, z) datatype <name> = <clause> | … | <clause> | rev(y::ys, z) = rev(ys, y::z) <clause> ::= <constructor> |<contructor> of <type> val (u,v) = rev(xs,nil) � Examples in v • datatype color = red | yellow | blue end; – elements are red, yellow, blue • datatype atom = atm of string | nmbr of int – elements are atm(“A”), atm(“B”), …, nmbr(0), nmbr(1), ... 1 3 • datatype list = nil | cons of atom*list 2 2 2 2 – elements are nil, cons(atm(“A”), nil), … 3 3 1 3 1 1 cons(nmbr(2), cons(atm(“ugh”), nil)), ... 5
Recommend
More recommend