Case Study: ML John Mitchell Reading: Chapter 5.

Slides:



Advertisements
Similar presentations
Sml2java a source to source translator Justin Koser, Haakon Larsen, Jeffrey Vaughan PLI 2003 DP-COOL.
Advertisements

Kathleen Fisher cs242 Reading: “Concepts in Programming Languages”, Chapter 6.
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,
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Semantics Static semantics Dynamic semantics attribute grammars
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
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.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
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 =
Kathleen Fisher cs242 Reading: “Concepts in Programming Languages”, Chapter 6.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
5/11/2015IT 3271 Types in ML (Ch 11) datatype bool = true | false; datatype 'element list = nil | :: of 'element * 'element list n Predefined, but not.
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.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
1 Functional Programming and ML. 2 What’s wrong with Imperative Languages? State State Introduces context sensitivity Introduces context sensitivity Harder.
The Algol Family and ML John Mitchell CS 242 Reading: Chapter 5.
CS 2104 : Prog. Lang. Concepts. Functional Programming I Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
Types and Type Inference Mooly Sagiv Slides by Kathleen Fisher and John Mitchell Reading: “Concepts in Programming Languages”, Revised Chapter 6 - handout.
INF Polymorphism and Type Inference Fatemeh Kazemeyni Department of Informatics – University of Oslo Based on John C. Mitchell’s.
PrasadCS7761 Haskell Data Types/ADT/Modules Type/Class Hierarchy Lazy Functional Language.
Types John Mitchell CS 242. Type A type is a collection of computable values that share some structural property. uExamples Integers Strings int  bool.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
Chapter 9: Functional Programming in a Typed Language.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
Types John Mitchell CS 242 Reading: Chapter 6. Announcements uHomework 1 due today Turn in at the end of class, OR Turn in by 5PM to the homework drop.
CS 2104 – Prog. Lang. Concepts Functional Programming II Lecturer : Dr. Abhik Roychoudhury School of Computing From Dr. Khoo Siau Cheng’s lecture notes.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
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.
Types John Mitchell CS 242. Type A type is a collection of computable values that share some structural property. uExamples Integers Strings int  bool.
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
Artificial Intelligence and Lisp Lecture 6 LiU Course TDDC65 Autumn Semester,
CS314 – Section 5 Recitation 9
Functional Programming
Type Checking and Type Inference
Programming Languages and Compilers (CS 421)
Functional Programming
Functional Programming
Principles of programming languages 12: Functional programming
Types CSCE 314 Spring 2016.
ML: a quasi-functional language with strong typing
Introduction to Scheme
CS 242 The Algol Family and ML John Mitchell Reading: Chapter 5.
Programming Languages and Compilers (CS 421)
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
The Metacircular Evaluator
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.
CSE S. Tanimoto Introduction to ML
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
CSE S. Tanimoto Introduction to ML
CSE-321 Programming Languages Introduction to Functional Programming
CS 242 Types John Mitchell.
CS 242 Types John Mitchell Reading: Chapter 6.
PROGRAMMING IN HASKELL
CSE S. Tanimoto Introduction to ML
Assignments and Procs w/Params
CSE S. Tanimoto Introduction to ML
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists
Drew Wyborski Programming Languages
Presentation transcript:

Case Study: ML John Mitchell Reading: Chapter 5

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

ML Typed programming language Intended for interactive use 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 ? Learn an important language that’s different Discuss 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 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

Logic for Computable Functions Dana Scott, 1969 Formulate logic for proving properties of typed functional programs Milner 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

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.

Basic Overview of ML Interactive compiler: read-eval-print Examples Compiler infers type before compiling or executing Type system does not allow casts or other loopholes. 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

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

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

Patterns and Declarations Patterns can be used in place of variables <pat> ::= <var> | <tuple> | <cons> | <record> … Value declarations General form val <pat> = <exp> 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 Anonymous function fn x => x+1; like Lisp lambda Declaration form fun <name> <pat1> = <exp1> | <name> <pat2> = <exp2> … | <name> <patn> = <expn> … Examples 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 Apply 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 Reverse a list fun reverse nil = nil | reverse (x::xs) = append ((reverse xs), [x]); Append lists fun append(nil, ys) = ys | append(x::xs, ys) = x :: append(xs, ys); Questions 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; 1 2 3 1 2 3 1 2 3 1 2 3

Datatype Declarations General form datatype <name> = <clause> | … | <clause> <clause> ::= <constructor> |<contructor> of <type> Examples 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 Recursively 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)) ) Recursive function fun sum (leaf n) = n | sum (node(n,t1,t2)) = n + sum(t1) + sum(t2) 4 5 7 6 3 2 1

Example: Evaluating Expressions Define 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)) Evaluation 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 Datatype Case expression datatype exp = Var of int | Const of int | Plus of exp*exp; Case 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 Basic Types Patterns Declarations Functions Polymorphism Unit Booleans Integers Strings Reals Tuples Lists Records Patterns Declarations Functions Polymorphism Overloading Type declarations Exceptions Reference Cells

Variables and assignment General 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 Variables Most languages A variable names a storage location Contents of location can be read, can be changed ML reference cell A mutable cell is another type of value Explicit operations to read contents or change contents Separates naming (declaration of identifiers) from “variables”

ML imperative constructs ML 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 Create cell and change contents Create cell and increment val x = ref “Bob”; x := “Bill”; Create cell and increment val y = ref 0; y := !y + 1; While loop val i = ref 0; while !i < 10 do i := !i +1; !i; x Bill Bob y 1

Type Inference in ML John Mitchell Reading: Chapter 6

Outline Type inference Polymorphism Good example of static analysis algorithm Will study algorithm and examples Polymorphism Polymorphism vs overloading Uniform vs non-uniform impl of polymorphism

Type checking and type inference Standard type checking int f(int x) { return x+1; }; int g(int y) { return f(y+1)*2;}; Look at body of each function and use declared types of identifies to check agreement. Type inference Look at code without type information and figure out what types could have been declared. ML is designed to make type inference tractable.

Motivation Types and type checking Type inference Type systems have improved steadily since Algol 60 Important for modularity, compilation, reliability Type inference A cool algorithm Widely regarded as important language innovation ML type inference gives you some idea of how many other static analysis algorithms work

ML Type Inference Example How does this work? - fun f(x) = 2+x; > val it = fn : int  int How does this work? + has two types: int*int  int, real*realreal 2 : int has only one type This implies + : int*int  int From context, need x: int Therefore f(x:int) = 2+x has type int  int Overloaded + is unusual. Most ML symbols have unique type. In many cases, unique type may be polymorphic.

Another presentation Example How does this work? x + 2 - fun f(x) = 2+x; > val it = fn : int  int How does this work? Graph for x. ((plus 2) x) Propagate to internal nodes and generate constraints int (t = int) intint tint Solve by substitution = intint x  @ + 2 Assign types to leaves : t int  int  int real  realreal : int

Application and Abstraction : r (s = t r) : s  t @ f x  : s : t x : s e : t Application f must have function type domain range domain of f must be type of argument x result type is range of f Function expression Type is function type domain range Domain is type of variable x Range is type of function body e

Types with type variables Example - fun f(g) = g(2); > val it = fn : (int  t)  t How does this work? Graph for g. (g 2) Propagate to internal nodes and generate constraints t (s = intt) st Solve by substitution = (intt)t 2  @ g Assign types to leaves : int : s

Use of Polymorphic Function - fun f(g) = g(2); > val it = fn : (int  t)  t Possible applications - fun add(x) = 2+x; > val it = fn : int  int - f(add); > val it = 4 : int - fun isEven(x) = ...; > val it = fn : int  bool - f(isEven); > val it = true : bool

Recognizing type errors Function - fun f(g) = g(2); > val it = fn : (int  t)  t Incorrect use - fun not(x) = if x then false else true; > val it = fn : bool  bool - f(not); Type error: cannot make bool  bool = int  t

Another Type Inference Example Function Definition - fun f(g,x) = g(g(x)); > val it = fn : (t  t)*t  t Type Inference Graph for g,x. g(g x) Solve by substitution = (vv)*vv Propagate to internal nodes and generate constraints v (s = uv) s*tv u (s = tu)  @ g x Assign types to leaves : t : s

Polymorphic Datatypes Datatype with type variable ’a is syntax for “type variable a” - datatype ‘a list = nil | cons of ‘a*(‘a list) > nil : ‘a list > cons : ‘a*(‘a list)  ‘a list Polymorphic function - fun length nil = 0 | length (cons(x,rest)) = 1 + length(rest) > length : ‘a list  int Type inference Infer separate type for each clause Combine by making two types equal (if necessary)

Type inference with recursion Second Clause length(cons(x,rest)) = 1 + length(rest) Type inference Assign types to leaves, including function name Proceed as usual Add constraint that type of function body = type of function name ‘a listint = t  @ @ @ @ cons : ‘a*‘a list ‘a list + 1 lenght rest : t x We do not expect you to master this.

Main Points about Type Inference Compute type of expression Does not require type declarations for variables Find most general type by solving constraints Leads to polymorphism Static type checking without type specifications May lead to better error detection than ordinary type checking Type may indicate a programming error even if there is no type error (example following slide).

Information from type inference An interesting function on lists fun reverse (nil) = nil | reverse (x::lst) = reverse(lst); Most general type reverse : ‘a list  ‘b list What does this mean? Since reversing a list does not change its type, there must be an error in the definition of “reverse” See Koenig paper on “Reading” page of CS242 site

Polymorphism vs Overloading Parametric polymorphism Single algorithm may be given many types Type variable may be replaced by any type f : tt => f : intint, f : boolbool, ... Overloading A single symbol may refer to more than one algorithm Each algorithm may have different type Choice of algorithm determined by type context Types of symbol may be arbitrarily different + has types int*intint, real*realreal, no others

Parametric Polymorphism: ML vs C++ ML polymorphic function Declaration has no type information Type inference: type expression with variables Type inference: substitute for variables as needed C++ function template Declaration gives type of function arg, result Place inside template to define type variables Function application: type checker does instantiation ML also has module system with explicit type parameters

Example: swap two values ML fun swap(x,y) = let val z = !x in x := !y; y := z end; val swap = fn : 'a ref * 'a ref -> unit C++ template <typename T> void swap(T& , T& y){ T tmp = x; x=y; y=tmp; } Declarations look similar, but compiled is very differently

ML Overloading Some predefined operators are overloaded User-defined functions must have unique type - fun plus(x,y) = x+y; This is compiled to int or real function, not both Why is a unique type needed? Need to compile code  need to know which + Efficiency of type inference Aside: General overloading is NP-complete Two types, true and false Overloaded functions and : {true*truetrue, false*truefalse, …}

Summary Types are important in modern languages Type inference Program organization and documentation Prevent program errors Provide important information to compiler Type inference Determine best type for an expression, based on known information about symbols in the expression Polymorphism Single algorithm (function) can have many types Overloading Symbol with multiple meanings, resolved at compile time