The Algol Family and ML John Mitchell CS 242. Language Sequence Algol 60 Algol 68 Pascal MLModula Lisp Many other languages: Algol 58, Algol W, Euclid,

Slides:



Advertisements
Similar presentations
Kathleen Fisher cs242 Reading: “Concepts in Programming Languages”, Chapter 6.
Advertisements

A Third Look At ML 1. Outline More pattern matching Function values and anonymous functions Higher-order functions and currying Predefined higher-order.
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Semantics Static semantics Dynamic semantics attribute grammars
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
ML Datatypes.1 Standard ML Data types. ML Datatypes.2 Concrete Datatypes  The datatype declaration creates new types  These are concrete data types,
Programming Languages and Paradigms
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Kathleen Fisher cs242 Reading: “Concepts in Programming Languages” Chapter 5 except “Real World Haskell”, Chapter 0 and Chapter 1 (
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Names and Bindings.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Slide 1 Vitaly Shmatikov CS 345 Functions. slide 2 Reading Assignment uMitchell, Chapter 7 uC Reference Manual, Chapters 4 and 9.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Chapter 9 Subprogram Control Consider program as a tree- –Each parent calls (transfers control to) child –Parent resumes when child completes –Copy rule.
Functional Design and Programming Lecture 1: Functional modeling, design and programming.
ALGOL 60 Design by committee of computer scientists: Naur, Backus, Bauer, McCarthy, van Wijngaarden, Landin, etc. Design by committee of computer scientists:
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Introduction to ML - Part 2 Kenny Zhu. What is next? ML has a rich set of structured values Tuples: (17, true, “stuff”) Records: {name = “george”, age.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
1 Functional Programming and ML. 2 What’s wrong with Imperative Languages? State State Introduces context sensitivity Introduces context sensitivity Harder.
CSCE 330 Project Algol 68 Joe PuzioWael AL-Fayez Gaurav ShahRonak Patel.
The Algol Family and ML John Mitchell CS 242 Reading: Chapter 5.
Kathleen Fisher cs242 Reading: “Concepts in Programming Languages” Chapter 5 except “Real World Haskell”, Chapter 0 and Chapter 1 (
Imperative Programming
Slide 1 Vitaly Shmatikov CS 345 Introduction to Scheme.
Dr. Muhammed Al-MulhemICS Chapter 5 The Algol Family and Haskell.
1 Programming Language History and Evolution In Text: Chapter 2.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Introduction A variable can be characterized by a collection of properties, or attributes, the most important of which is type, a fundamental concept in.
Programming Languages and Paradigms Imperative Programming.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
Ch. 5 Ch. 51 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (more notes) Dr. Carter Tiernan.
The Evolution of Programming Languages Day 2 Lecturer: Xiao Jia The Evolution of PLs1.
Chapter 3 Part II Describing Syntax and Semantics.
COMP313A Functional Programming (1)
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Slide 1 Dr. Mohammad El-Ramly Fall 2010 Set 7- II - Functions Slides by Vitaly Shmatikov Cairo University Faculty of Computers and Information CS317 Concepts.
Ch. 5 Ch. 51 jcmt Summer 2003Programming Languages CSE3302 Programming Languages (more notes) Summer 2003 Dr. Carter Tiernan.
1 Structure of Compilers Lexical Analyzer (scanner) Modified Source Program Parser Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Slide 1 Vitaly Shmatikov CS 345 Introduction to ML.
Names, Scope, and Bindings Programming Languages and Paradigms.
Types John Mitchell CS 242. Type A type is a collection of computable values that share some structural property. uExamples Integers Strings int  bool.
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
CS314 – Section 5 Recitation 9
Programming Language History and Evolution
Functional Programming
Data Types In Text: Chapter 6.
Programming Languages and Compilers (CS 421)
Zuse’s Plankalkül – 1945 Never implemented Problems Zuse Solved
Principles of programming languages 12: Functional programming
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
ML: a quasi-functional language with strong typing
CS 326 Programming Languages, Concepts and Implementation
CS 242 The Algol Family and ML John Mitchell Reading: Chapter 5.
Programming Language History and Evolution
Objective caml Daniel Jackson MIT Lab for Computer Science 6898: Advanced Topics in Software Design March 18, 2002.
FP Foundations, Scheme In Text: Chapter 14.
CSE 341 Section 5 Winter 2018.
Case Study: ML John Mitchell Reading: Chapter 5.
Course Overview PART I: overview material PART II: inside a compiler
CS 242 Types John Mitchell.
Presentation transcript:

The Algol Family and ML John Mitchell CS 242

Language Sequence Algol 60 Algol 68 Pascal MLModula Lisp Many other languages: Algol 58, Algol W, Euclid, EL1, Mesa (PARC), … Modula-2, Oberon, Modula-3 (DEC)

Algol 60 uBasic Language of 1960 Simple imperative language + functions Successful syntax, BNF -- used by many successors –statement oriented –Begin … End blocks (like C { … } ) –if … then … else Recursive functions and stack storage allocation Fewer ad hoc restrictions than Fortran –General array references: A[ x + B[3]*y] Type discipline was improved by later languages Very influential but not widely used in US

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

Algol Joke uQuestion Is x := x equivalent to doing nothing? uInteresting answer in Algol integer procedure p; begin …. p := p …. end; Assignment here is actually a recursive call

Some trouble spots in Algol 60 uType 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 uParameter passing methods Pass-by-name had various anomalies –“Copy rule” based on substitution, interacts with side effects Pass-by-value expensive for arrays uSome awkward control issues goto out of block requires memory management

Algol 60 Pass-by-name uSubstitute text of actual parameter Unpredictable with side effects! uExample 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 68 uConsidered difficult to understand Idiosyncratic terminology –types were called “modes” –arrays were called “multiple values” vW grammars instead of BNF –context-sensitive grammar invented by A. van Wijngaarden Elaborate type system Complicated type conversions uFixed some problems of Algol 60 Eliminated pass-by-name uNot widely adopted

Algol 68 Modes uPrimitive modes int real char bool string compl (complex) bits bytes sema (semaphore) format (I/O) file uCompound modes arrays structures procedures sets pointers Rich and structured type system is a major contribution of Algol 68

Other features of Algol 68 uStorage management Local storage on stack Heap storage, explicit alloc and garbage collection uParameter passing Pass-by-value Use pointer types to obtain Pass-by-reference uAssignable procedure variables Follow “orthogonality” principle rigorously Source: Tanenbaum, Computing Surveys

Pascal uRevised type system of Algol Good data-structuring concepts –records, variants, subranges More restrictive than Algol 60/68 –Procedure parameters cannot have procedure parameters uPopular teaching language uSimple one-pass compiler

Limitations of Pascal uArray 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 How could this have happened? Emphasis on teaching uNot successful for “industrial-strength” projects Kernighan -- Why Pascal is not my favorite language Left niche for C; niche has expanded!!

ML uTyped programming language uIntended for interactive use uCombination of Lisp and Algol-like features Expression-oriented Higher-order functions Garbage collection Abstract data types Module system Exceptions uGeneral purpose non-C-like, not OO language

Goals in study of ML uSurvey a modern procedural language uDiscuss general programming languages issues Types and type checking –General issues in static/dynamic typing –Type inference –Polymorphism and Generic Programming Memory management –Static scope and block structure –Function activation records, higher-order functions Control –Force and delay –Exceptions –Tail recursion and continuations

History of ML uRobin Milner uLogic for Computable Functions Stanford Edinburgh uMeta-Language of the LCF system Theorem proving Type system Higher-order functions

Logic for Computable Functions uDana Scott, 1969 Formulate logic for proving properties of typed functional programs uMilner Project to automate logic Notation for programs Notation for assertions and proofs Need to write programs that find proofs –Too much work to construct full formal proof by hand Make sure proofs are correct

LCF proof search uTactic: function that tries to find proof succeed and return proof tactic(formula) = search forever fail uExpress tactics in the Meta-Language (ML) uUse type system to facilitate correctness

Tactics in ML type system uTactic has a functional type tactic : formula  proof uType system must allow “failure” succeed and return proof tactic(formula) = search forever fail and raise exception

Function types in ML f : A  B means for every x  A, some element y=f(x)  B f(x) = run forever terminate by raising an 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 uTactic is a function uMethod for combining tactics is a function on functions uExample: f(tactic 1, tactic 2 ) = formula. try tactic 1 (formula) else tactic 2 (formula)

Basic Overview of ML uInteractive compiler: read-eval-print Compiler infers type before compiling or executing Type system does not allow casts or other loopholes. uExamples - (5+3)-2; > val it = 6 : int - if 5>3 then “Bob” else “Fido”; > val it = “Bob” : string - 5=4; > val it = false : bool

Overview by Type uBooleans true, false : bool if … then … else … (types must match) uIntegers 0, 1, 2, … : int +, *, … : int * int  int and so on … uStrings “Austin Powers” uReals 1.0, 2.2, , … decimal point used to disambiguate

Compound Types uTuples (4, 5, “noxious”) : int * int * string uLists nil 1 :: [2, 3, 4] infix cons notation uRecords {name = “Fido”, hungry=true} : {name : string, hungry : bool}

Patterns and Declarations uPatterns can be used in place of variables ::= | | | … uValue declarations General form val = Examples val myTuple = (“Conrad”, “Lorenz”); val (x,y) = myTuple; val myList = [1, 2, 3, 4]; val x::rest = myList; Local declarations let val x = 2+3 in x*4 end;

Functions and Pattern Matching uAnonymous function fn x => x+1; like Lisp lambda uDeclaration form fun = | = … uExamples fun f (x,y) = x+y; actual par must match pattern (x,y) fun length nil = 0 | length (x::s) = 1 + length(s);

Map function on lists uApply function to every element of list fun map (f, nil) = nil | map (f, x::xs) = f(x) :: map (f,xs); map (fn x => x+1, [1,2,3]); [2,3,4]  Compare to Lisp (define map (lambda (f xs) (if (eq? xs ()) () (cons (f (car xs)) (map f (cdr xs))) )))

More functions on lists uReverse a list fun reverse nil = nil | reverse (x::xs) = append ((reverse xs), [x]); uAppend lists fun append(nil, ys) = ys | append(x::xs, ys) = x :: append(xs, ys); uQuestions How efficient is reverse? Can you do this with only one pass through list?

More efficient reverse function fun reverse xs = let fun rev (nil, z) = (nil, z) | rev(y::ys, z) = rev(ys, y::z) val (u,v) = rev(xs,nil) in v end;

Datatype Declarations uGeneral form datatype = | … | ::= | of uExamples datatype color = red | yellow | blue –elements are red, yellow, blue datatype atom = atm of string | nmbr of int –elements are atm(“A”), atm(“B”), …, nmbr(0), nmbr(1),... datatype list = nil | cons of atom*list –elements are nil, cons(atm(“A”), nil), … cons(nmbr(2), cons(atm(“ugh”), nil)),...

Datatype and pattern matching uRecursively defined data structure datatype tree = leaf of int | node of int*tree*tree node(4, node(3,leaf(1), leaf(2)), node(5,leaf(6), leaf(7)) ) uRecursive function fun sum (leaf n) = n | sum (node(n,t1,t2)) = n + sum(t1) + sum(t2)

Example: Evaluating Expressions uDefine datatype of expressions datatype exp = Var of int | Const of int | Plus of exp*exp; Write (x+3)+y as Plus(Plus(Var(1),Const(3)), Var(2)) uEvaluation function fun ev(Var(n)) = Var(n) | ev(Const(n)) = Const(n) | ev(Plus(e1,e2)) = … Examples ev(Plus(Const(3),Const(2))) Const(5) ev(Plus(Var(1),Plus(Const(2),Const(3)))) ev(Plus(Var(1), Const(5))

Case expression uDatatype datatype exp = Var of int | Const of int | Plus of exp*exp; uCase expression case e of Var(n) => … | Const(n) => …. | Plus(e1,e2) => …

Evaluation by cases datatype exp = Var of int | Const of int | Plus of exp*exp; fun ev(Var(n)) = Var(n) | ev(Const(n)) = Const(n) | ev(Plus(e1,e2)) = ( case ev(e1) of Var(n) => Plus(Var(n),ev(e2)) | Const(n) => ( case ev(e2) of Var(m) => Plus(Const(n),Var(m)) | Const(m) => Const(n+m) | Plus(e3,e4) => Plus(Const(n),Plus(e3,e4)) ) | Plus(e3,e4) => Plus(Plus(e3,e4),ev(e2)) ) ;

Core ML uBasic Types Unit Booleans Integers Strings Reals Tuples Lists Records uPatterns uDeclarations uFunctions uPolymorphism uOverloading uType declarations uExceptions uReference Cells

Variables and assignment uGeneral terminology: L-values and R-values Assignment y := x+3 –Identifier on left refers to a memory location, called L-value –Identifier on right refers to contents, called R-value uVariables Basic properties –A variable names a storage location –Contents of location can be read, can be changed ML –A variable is another type of value –Explicit operations to read contents or change contents –Separates naming (declaration of identifiers) from “variables”

ML imperative constructs uML reference cells Different types for location and contents x : int non-assignable integer value y : int ref location whose contents must be integer !y the contents of location y ref x expression creating new cell initialized to x ML assignment operator := applied to memory cell and new contents Examples y := x+3 place value of x+3 in cell y; requires x:int y := !y + 3 add 3 to contents of y and store in location y

ML examples uCreate cell and change contents val x = ref “Bob”; x := “Bill”; uCreate cell and increment val y = ref 0; y := !y + 1; uWhile loop val i = ref 0; while !i < 10 do i := !i +1; !i;