Agenda. CS301 Session 11. Common type constructors. Things we could add to Impcore. Discussion: midterm exam - take-home or inclass?

Size: px
Start display at page:

Download "Agenda. CS301 Session 11. Common type constructors. Things we could add to Impcore. Discussion: midterm exam - take-home or inclass?"

Transcription

1 Agenda CS301 Session 11 Discussion: midterm exam - take-home or inclass? Interlude: common type constructors Type soundness 1 2 Things we could add to Impcore Common type constructors Array is a type constructor, not a single type We're familiar with other type constructors from the garden-variety programming languages we use all the time...but now is a good time to analyze them in a language-independent way Our typing rules will assume just one type environment Γ 3 4

2 Three common type constructors (First-class) functions Products Sums Type constructor First-class functions Infix, two arguments: Formation rule: τ 1 τ 2 τ 1 and τ 2 are types τ 1 τ 2 is a type 5 6 Typing rules for functions Introduction Γ{x τ} e : τ Γ lambda(x : τ, e) : τ τ Elimination Γ e 1 : τ τ Γ e 2 : τ Γ apply(e 1, e 2 ) : τ Products (pairs) Constituent types need not be the same Variously, "tuple", "struct", "record" Can be used to model objects (in the OO sense) Formation τ 1 and τ 2 are types τ 1 τ 2 is a type 7 8

3 Typing rules for products Introduction Γ e 1 : τ 1 Γ e 2 : τ 2 Γ pair(e 1, e 2 ) : τ 1 τ 2 An elegant elim rule Like a pattern match Γ e : τ 1 τ 2 Γ{x 1 τ 1, x 2 τ 2 } e : τ Elimination Γ e : τ 1 τ 2 Γ fst(e) : τ 1 Γ letpair(x 1, x 2, e, e ) : τ (and similarly for the second element) 9 10 Generalizing pairs In ML and related languages pairs are generalized to records with named fields Your homework contains a similar problem about sum types Formation: τ 1... τ n are types {name 1 : τ 1,..., name n : τ n } is a type Introduction Elimination Typing records {name 1 : τ 1,..., name n : τ n } is a type Γ e 1 : τ 1,..., Γ e n : τ n Γ record(name 1 = e 1,..., name n = e n ) : {name 1 : τ 1,..., name n : τ n } Γ e : {name 1 : τ 1,..., name n : τ n } name = name i, 1 i n Γ getfield(name, e) : τ i 11 12

4 More elegant elim rule Again like a pattern match Γ e : {name 1 : τ 1,..., name n : τ n } Γ{x 1 τ 1,..., x n τ n e : τ Γ letrecord(x 1,..., x n, e, e ) : τ A type that unions other types together Sum types Like C unions, but safer because you can always tell what's there Like simple ML datatypes (no recursion) Formation rule τ 1 and τ 2 are types τ 1 + τ 2 is a type Typing rules for unions Introduction Γ e : τ 1 τ 2 is a type Γ left τ2 (e) : τ 1 + τ 2 Typing rules for unions(2) Elimination: like case or switch Γ e : τ 1 + τ 2 Γ{x 1 τ 1 } e 1 : τ Γ{x 2 τ 2 } e 2 : τ Γ e : τ 2 τ 1 is a type Γ case e of left(x 1 ) e 1 right(x 2 ) e 2 : τ Γ right τ1 (e) : τ 1 + τ

5 About type soundness Why trust a type system? Given a complex enough type system, we might be unable to see whether it behaves reasonably Language designers prove type soundness both to increase trust and to be explicit about what guarantees the type system provides What is type soundness? A kind of claim we make about the relationship between the typing rules and the evaluation rules Loosely, "well-typed programs don't go wrong" Sample corollaries: Functions always receive the right number and kind of arguments No array access is out of bounds (a more advanced kind of type system) Machinery needed for soundness The meaning of a type τ is a set of values Examples int = {number(n) n is an integer} bool = {bool(#t), bool(#f)} This gives us a notation for the set of things a well typed expression is allowed to evaluate to 19 20

6 Proper environments If Γ and ρ are typing and value environments, respectively, we say ρ agrees with Γ whenever, for every x in dom (Γ), 1. x is also in dom (ρ), and 2. ρ(x) Γ(x) If A soundness claim 1. Γ and ρ are typing and value environments, and 2. ρ agrees with Γ, and 3. Γ e : τ and ρ, e v, then v τ 21 22

7 Agenda CS301 Session 12 Side trip: the semantics of defining and applying recursive functions Introduction to polymorphic type systems A polymorphic type system for uscheme 1 2 Recursive functions 4

8 How can recursion work? Rule: all names are evaluated by looking them up in an environment How do we arrange for the name f to be meaningful in: (define f (n e) (if (= e 0) 1 (* n (f n (- e 1))))) Simple case: Impcore Functions are not first-class; special function environment We just bind the function name to a piece of abstract syntax x 1,...,x n all distinct D DEFINE( f, x 1,...,x n,e),ξ,φ ξ,φ{ f USER( x 1,...,x n,e)} 5 6 Impcore function application By the time we use a recursive function, its definition is already in the function environment φ( f ) = USER( x 1,...,x n,e) x 1,...,x n all distinct e 1,ξ 0,φ,ρ 0 v 1,ξ 1,φ,ρ 1. e n,ξ n 1,φ,ρ n 1 v n,ξ n,φ,ρ n e,ξ n,φ,{x 1 v 1,...x n v n } v,ξ,φ,ρ n APPLY( f,e 1,e 2,...,e n ),ξ 0,φ,ρ 0 v,ξ,φ,ρ n First-class functions What about uscheme? How do we make sure the name of the recursive function is properly bound in the body? 7 8

9 Lambdas evaluate to closures Functions Function applications l 1,..., l n dom σ Functions x 1,..., x n all distinct lambda( x 1,..., x n, e), ρ, σ lambda( x 1,..., x n, e), ρ, σ e, ρ, σ lambda( x 1,..., x n, e c ), ρ c, σ 0 e 1, ρ, σ 0 v 1, σ 1. e n, ρ, σ n 1 v n, σ n e c, ρ c {x 1 l 1,..., x n l n }, σ n {l 1 v 1,..., l n v n } v, σ apply(e, e 1,..., e n ), ρ, σ v, σ 9 10 Local recursive definition When a recursive function is applied, how/where is its name bound? Top-level recursive definitions Left as an exercise...do it! l 1,..., l n dom σ ρ = ρ{x 1 l 1,..., x n l n } σ 0 = σ{l 1 unspecified,..., l n unspecified} e 1, ρ, σ 0 v 1, σ 1. e n, ρ, σ n 1 v n, σ n e, ρ, σ n {l 1 v 1,..., l n v n } v, σ letrec( x 1, e 1,..., x n, e n, e), ρ, σ v, σ 11 12

10 Perspective Polymorphic type systems Flexibility of dynamic typing (Scheme) both a blessing and a curse Great for small systems, prototypes, and god-like programmers Not so great for large systems, production code, trusted code, teams of ordinary mortals Limitations of monomorphic typing Example from typed Impcore: list processing functions Where we're going Introduce polymorphic type system with static type checking Now we can write one version of length with type α. α list int (forall ('a) (function ((list 'a)) int)) This will be flexible enough to type a lot of the programs we want - almost a "sweet spot"...but terribly verbose and impossible to use 15 16

11 20 Why torture ourselves with this type system? Why? To motivate type inference as in ML and related languages The real "sweet spot": polymorphic type system, plus type inference, yields a terse, flexible language with robust guarantees suitable for production programming Used in ML, OCaml, Haskell, etc. etc. Type variables A new kind of variable that stands for an unknown type Actual types are supplied by type instantiation, a.k.a. type application Type variables are bound in types by (abstractly), or forall (concretely) Bound in expressions by tylambda (abstractly), or type-lambda (concretely) Idea: lambda for types You've seen this before: Java/C++ generics Quantified types: α 1,..., α n. τ (forall ('a1... 'an) type) Type abstraction: tylambda(α 1,..., α n, e) (type-lambda ('a1... 'an) exp) Type application: tyapply(e, τ 1,..., τ n ) (@ exp type1... typen) Quantified types -> length <procedure> : (forall ('a) (function ((list 'a)) int)) -> cons <procedure> : (forall ('a) (function ('a (list 'a)) (list 'a))) -> car <procedure> : (forall ('a) (function ((list 'a)) 'a)) -> cdr <procedure> : (forall ('a) (function ((list 'a)) (list 'a))) -> '() () : (forall ('a) (list 'a)) 19

12 Type instantiation -> (val length-int length int)) length-int : (function ((list int)) int) -> (val length-bool (@ length bool)) length-bool : (function ((list bool)) int) -> (val nil-bool (@ '() bool)) () : (list bool) Type abstraction -> (val-rec (forall ('a) (function ((list 'a)) int)) len (type-lambda ('a) (lambda (((list 'a) l)) (if ((@ null? 'a) l) 0 (+ 1 ((@ len 'a) ((@ cdr 'a) l))))))) len : (forall ('a) (function ((list 'a)) int)) -> (@ len int) <procedure> : (function ((list int)) int) -> ((@ len int) '(1 2 3)) 3 : int Instantiation substitutes actual types for type variables 21 22

Agenda. CS301 Session 9. Abstract syntax: top level. Abstract syntax: expressions. The uscheme interpreter. Approaches to solving the homework problem

Agenda. CS301 Session 9. Abstract syntax: top level. Abstract syntax: expressions. The uscheme interpreter. Approaches to solving the homework problem CS301 Session 9 The uscheme interpreter Agenda Approaches to solving the homework problem 1 2 Abstract syntax: top level datatype toplevel = EXP of exp DEFINE of name * lambda VAL of name * exp USE of

More information

Programming Languages: the ultimate user interface. Welcome to CS301. Why programming languages? Some language features

Programming Languages: the ultimate user interface. Welcome to CS301. Why programming languages? Some language features Welcome to CS301 Programming Languages: the ultimate user interface 1 2 Why programming languages? Language influences thought P.L. features as tools for specifying computation Raise consciousness of language

More information

Meanings of syntax. Norman Ramsey Geoffrey Mainland. COMP 105 Programming Languages Tufts University. January 26, 2015

Meanings of syntax. Norman Ramsey Geoffrey Mainland. COMP 105 Programming Languages Tufts University. January 26, 2015 Meanings of syntax Norman Ramsey Geoffrey Mainland COMP 105 Programming Languages Tufts University January 26, 2015 (COMP 105) Meanings of syntax January 26, 2015 1 / 33 What is the meaning of a while

More information

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011

CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Dan Grossman Spring 2011 CS152: Programming Languages Lecture 11 STLC Extensions and Related Topics Dan Grossman Spring 2011 Review e ::= λx. e x e e c v ::= λx. e c τ ::= int τ τ Γ ::= Γ, x : τ (λx. e) v e[v/x] e 1 e 1 e 1 e

More information

Type Checking and Type Inference

Type Checking and Type Inference Type Checking and Type Inference Principles of Programming Languages CSE 307 1 Types in Programming Languages 2 Static Type Checking 3 Polymorphic Type Inference Version: 1.8 17:20:56 2014/08/25 Compiled

More information

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals

Review. CS152: Programming Languages. Lecture 11 STLC Extensions and Related Topics. Let bindings (CBV) Adding Stuff. Booleans and Conditionals Review CS152: Programming Languages Lecture 11 STLC Extensions and Related Topics e ::= λx. e x ee c v ::= λx. e c (λx. e) v e[v/x] e 1 e 2 e 1 e 2 τ ::= int τ τ Γ ::= Γ,x : τ e 2 e 2 ve 2 ve 2 e[e /x]:

More information

COMP 105 Homework: Type Systems

COMP 105 Homework: Type Systems Due Tuesday, March 29, at 11:59 PM (updated) The purpose of this assignment is to help you learn about type systems. Setup Make a clone of the book code: git clone linux.cs.tufts.edu:/comp/105/build-prove-compare

More information

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference Lecture #13: Type Inference and Unification Typing In the Language ML Examples from the language ML: fun map f [] = [] map f (a :: y) = (f a) :: (map f y) fun reduce f init [] = init reduce f init (a ::

More information

Programming Languages and Compilers (CS 421)

Programming Languages and Compilers (CS 421) Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC http://courses.engr.illinois.edu/cs421 Based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha 10/3/17

More information

Typed Scheme: Scheme with Static Types

Typed Scheme: Scheme with Static Types Typed Scheme: Scheme with Static Types Version 4.1.1 Sam Tobin-Hochstadt October 5, 2008 Typed Scheme is a Scheme-like language, with a type system that supports common Scheme programming idioms. Explicit

More information

CS153: Compilers Lecture 14: Type Checking

CS153: Compilers Lecture 14: Type Checking CS153: Compilers Lecture 14: Type Checking Stephen Chong https://www.seas.harvard.edu/courses/cs153 Announcements Project 4 out Due Thursday Oct 25 (7 days) Project 5 out Due Tuesday Nov 13 (26 days) Project

More information

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy 06-02552 Princ. of Progr. Languages (and Extended ) The University of Birmingham Spring Semester 2016-17 School of Computer Science c Uday Reddy2016-17 Handout 6: Polymorphic Type Systems 1. Polymorphic

More information

(Refer Slide Time: 4:00)

(Refer Slide Time: 4:00) Principles of Programming Languages Dr. S. Arun Kumar Department of Computer Science & Engineering Indian Institute of Technology, Delhi Lecture - 38 Meanings Let us look at abstracts namely functional

More information

Goal. CS152: Programming Languages. Lecture 15 Parametric Polymorphism. What the Library Likes. What The Client Likes. Start simpler.

Goal. CS152: Programming Languages. Lecture 15 Parametric Polymorphism. What the Library Likes. What The Client Likes. Start simpler. Goal Understand what this interface means and why it matters: CS152: Programming Languages Lecture 15 Parametric Polymorphism Dan Grossman Spring 2011 type a mylist; val mt_list : a mylist val cons : a

More information

Overview. CS301 Session 3. Problem set 1. Thinking recursively

Overview. CS301 Session 3. Problem set 1. Thinking recursively Overview CS301 Session 3 Review of problem set 1 S-expressions Recursive list processing Applicative programming A look forward: local variables 1 2 Problem set 1 Don't use begin unless you need a side

More information

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011

CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011 CS 6110 S11 Lecture 25 Typed λ-calculus 6 April 2011 1 Introduction Type checking is a lightweight technique for proving simple properties of programs. Unlike theorem-proving techniques based on axiomatic

More information

Harvard School of Engineering and Applied Sciences Computer Science 152

Harvard School of Engineering and Applied Sciences Computer Science 152 Harvard School of Engineering and Applied Sciences Computer Science 152 Lecture 17 Tuesday, March 30, 2010 1 Polymorph means many forms. Polymorphism is the ability of code to be used on values of different

More information

λ calculus is inconsistent

λ calculus is inconsistent Content Rough timeline COMP 4161 NICTA Advanced Course Advanced Topics in Software Verification Gerwin Klein, June Andronick, Toby Murray λ Intro & motivation, getting started [1] Foundations & Principles

More information

CS 4110 Programming Languages & Logics. Lecture 27 Recursive Types

CS 4110 Programming Languages & Logics. Lecture 27 Recursive Types CS 4110 Programming Languages & Logics Lecture 27 Recursive Types 4 November 2016 Announcements 2 My office hours are at the normal time today but canceled on Monday Guest lecture by Seung Hee Han on Monday

More information

1 Introduction. 3 Syntax

1 Introduction. 3 Syntax CS 6110 S18 Lecture 19 Typed λ-calculus 1 Introduction Type checking is a lightweight technique for proving simple properties of programs. Unlike theorem-proving techniques based on axiomatic semantics,

More information

Programming Languages Lecture 14: Sum, Product, Recursive Types

Programming Languages Lecture 14: Sum, Product, Recursive Types CSE 230: Winter 200 Principles of Programming Languages Lecture 4: Sum, Product, Recursive Types The end is nigh HW 3 No HW 4 (= Final) Project (Meeting + Talk) Ranjit Jhala UC San Diego Recap Goal: Relate

More information

Homework 3 COSE212, Fall 2018

Homework 3 COSE212, Fall 2018 Homework 3 COSE212, Fall 2018 Hakjoo Oh Due: 10/28, 24:00 Problem 1 (100pts) Let us design and implement a programming language called ML. ML is a small yet Turing-complete functional language that supports

More information

Lecture #23: Conversion and Type Inference

Lecture #23: Conversion and Type Inference Lecture #23: Conversion and Type Inference Administrivia. Due date for Project #2 moved to midnight tonight. Midterm mean 20, median 21 (my expectation: 17.5). Last modified: Fri Oct 20 10:46:40 2006 CS164:

More information

Typed Racket: Racket with Static Types

Typed Racket: Racket with Static Types Typed Racket: Racket with Static Types Version 5.0.2 Sam Tobin-Hochstadt November 6, 2010 Typed Racket is a family of languages, each of which enforce that programs written in the language obey a type

More information

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th Name: CAS ID (e.g., abc01234@pomona.edu): I encourage you to collaborate. collaborations below. Please record your Each question is worth

More information

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = Hello; Lecture #23: Conversion and Type Inference Administrivia. Due date for Project #2 moved to midnight tonight. Midterm mean 20, median 21 (my expectation: 17.5). In Java, this is legal: Object x = "Hello";

More information

Overloading, Type Classes, and Algebraic Datatypes

Overloading, Type Classes, and Algebraic Datatypes Overloading, Type Classes, and Algebraic Datatypes Delivered by Michael Pellauer Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. September 28, 2006 September 28, 2006 http://www.csg.csail.mit.edu/6.827

More information

Types-2. Polymorphism

Types-2. Polymorphism Types-2 Polymorphism Type reconstruction (type inference) for a simple PL Typing functions Coercion, conversion, reconstruction Rich area of programming language research as people try to provide safety

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Fall 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Type Inference Some statically typed languages, like ML (and to a lesser extent Scala), offer alternative

More information

Datatype declarations

Datatype declarations Datatype declarations datatype suit = HEARTS DIAMONDS CLUBS SPADES datatype a list = nil (* copy me NOT! *) op :: of a * a list datatype a heap = EHEAP HEAP of a * a heap * a heap type suit val HEARTS

More information

Comp 311: Sample Midterm Examination

Comp 311: Sample Midterm Examination Comp 311: Sample Midterm Examination October 29, 2007 Name: Id #: Instructions 1. The examination is closed book. If you forget the name for a Scheme operation, make up a name for it and write a brief

More information

TYPE INFERENCE. François Pottier. The Programming Languages Mentoring ICFP August 30, 2015

TYPE INFERENCE. François Pottier. The Programming Languages Mentoring ICFP August 30, 2015 TYPE INFERENCE François Pottier The Programming Languages Mentoring Workshop @ ICFP August 30, 2015 What is type inference? What is the type of this OCaml function? let f verbose msg = if verbose then

More information

Types, Expressions, and States

Types, Expressions, and States 8/27: solved Types, Expressions, and States CS 536: Science of Programming, Fall 2018 A. Why? Expressions represent values in programming languages, relative to a state. Types describe common properties

More information

CSE341 Spring 2017, Final Examination June 8, 2017

CSE341 Spring 2017, Final Examination June 8, 2017 CSE341 Spring 2017, Final Examination June 8, 2017 Please do not turn the page until 8:30. Rules: The exam is closed-book, closed-note, etc. except for both sides of one 8.5x11in piece of paper. Please

More information

Programming Languages Assignment #7

Programming Languages Assignment #7 Programming Languages Assignment #7 December 2, 2007 1 Introduction This assignment has 20 points total. In this assignment, you will write a type-checker for the PolyMinML language (a language that is

More information

CS421 Spring 2014 Midterm 1

CS421 Spring 2014 Midterm 1 CS421 Spring 2014 Midterm 1 Name: NetID: You have 75 minutes to complete this exam. This is a closed-book exam. All other materials (e.g., calculators, telephones, books, card sheets), except writing utensils

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2018 Lecture 7b Andrew Tolmach Portland State University 1994-2018 Dynamic Type Checking Static type checking offers the great advantage of catching errors early And

More information

Flang typechecker Due: February 27, 2015

Flang typechecker Due: February 27, 2015 CMSC 22610 Winter 2015 Implementation of Computer Languages I Flang typechecker Due: February 27, 2015 Project 3 February 9, 2015 1 Introduction The third project is to implement a type checker for Flang,

More information

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types CS 4110 Programming Languages & Logics Lecture 28 Recursive Types 7 November 2014 Announcements 2 Foster office hours 11-12pm Guest lecture by Fran on Monday Recursive Types 3 Many languages support recursive

More information

CSE341 Spring 2017, Final Examination June 8, 2017

CSE341 Spring 2017, Final Examination June 8, 2017 CSE341 Spring 2017, Final Examination June 8, 2017 Please do not turn the page until 8:30. Rules: The exam is closed-book, closed-note, etc. except for both sides of one 8.5x11in piece of paper. Please

More information

CSE341 Autumn 2017, Final Examination December 12, 2017

CSE341 Autumn 2017, Final Examination December 12, 2017 CSE341 Autumn 2017, Final Examination December 12, 2017 Please do not turn the page until 2:30. Rules: The exam is closed-book, closed-note, etc. except for both sides of one 8.5x11in piece of paper. Please

More information

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Lecture 14 Tuesday, March 24, 2015 1 Parametric polymorphism Polymorph means many forms. Polymorphism is the ability of

More information

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012

CS-XXX: Graduate Programming Languages. Lecture 9 Simply Typed Lambda Calculus. Dan Grossman 2012 CS-XXX: Graduate Programming Languages Lecture 9 Simply Typed Lambda Calculus Dan Grossman 2012 Types Major new topic worthy of several lectures: Type systems Continue to use (CBV) Lambda Caluclus as our

More information

Part VI. Imperative Functional Programming

Part VI. Imperative Functional Programming Part VI Imperative Functional Programming Chapter 14 Mutable Storage MinML is said to be a pure language because the execution model consists entirely of evaluating an expression for its value. ML is

More information

CSE505, Fall 2012, Midterm Examination October 30, 2012

CSE505, Fall 2012, Midterm Examination October 30, 2012 CSE505, Fall 2012, Midterm Examination October 30, 2012 Rules: The exam is closed-book, closed-notes, except for one side of one 8.5x11in piece of paper. Please stop promptly at Noon. You can rip apart

More information

6.821 Programming Languages Handout Fall MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Compvter Science

6.821 Programming Languages Handout Fall MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Compvter Science 6.821 Programming Languages Handout Fall 2002 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Compvter Science Problem Set 6 Problem 1: cellof Subtyping Do exercise 11.6

More information

Lists. Michael P. Fourman. February 2, 2010

Lists. Michael P. Fourman. February 2, 2010 Lists Michael P. Fourman February 2, 2010 1 Introduction The list is a fundamental datatype in most functional languages. ML is no exception; list is a built-in ML type constructor. However, to introduce

More information

CSCI-GA Scripting Languages

CSCI-GA Scripting Languages CSCI-GA.3033.003 Scripting Languages 12/02/2013 OCaml 1 Acknowledgement The material on these slides is based on notes provided by Dexter Kozen. 2 About OCaml A functional programming language All computation

More information

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106 CS 565: Programming Languages Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106 Administrivia Who am I? Course web page http://www.cs.purdue.edu/homes/peugster/cs565spring08/ Office hours By appointment Main

More information

Adding GADTs to OCaml the direct approach

Adding GADTs to OCaml the direct approach Adding GADTs to OCaml the direct approach Jacques Garrigue & Jacques Le Normand Nagoya University / LexiFi (Paris) https://sites.google.com/site/ocamlgadt/ Garrigue & Le Normand Adding GADTs to OCaml 1

More information

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes

Chapter 13: Reference. Why reference Typing Evaluation Store Typings Safety Notes Chapter 13: Reference Why reference Typing Evaluation Store Typings Safety Notes References Computational Effects Also known as side effects. A function or expression is said to have a side effect if,

More information

CSE-321: Assignment 8 (100 points)

CSE-321: Assignment 8 (100 points) CSE-321: Assignment 8 (100 points) gla@postech.ac.kr Welcome to the final assignment of CSE-321! In this assignment, you will implement a type reconstruction algorithm (the algorithm W discussed in class)

More information

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions

CVO103: Programming Languages. Lecture 5 Design and Implementation of PLs (1) Expressions CVO103: Programming Languages Lecture 5 Design and Implementation of PLs (1) Expressions Hakjoo Oh 2018 Spring Hakjoo Oh CVO103 2018 Spring, Lecture 5 April 3, 2018 1 / 23 Plan Part 1 (Preliminaries):

More information

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Harvard School of Engineering and Applied Sciences CS 152: Programming Languages Lecture 18 Thursday, March 29, 2018 In abstract algebra, algebraic structures are defined by a set of elements and operations

More information

Generic polymorphism on steroids

Generic polymorphism on steroids Generic polymorphism on steroids or How to Solve the Expression Problem with Polymorphic Variants Claudio Sacerdoti Coen Dipartimento di Informatica Scienza e Ingegneria

More information

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein COMP 4161 NICTA Advanced Course Advanced Topics in Software Verification Toby Murray, June Andronick, Gerwin Klein λ 1 Last time... λ calculus syntax free variables, substitution β reduction α and η conversion

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Values and Types We divide the universe of values according to types A type is a set of values and

More information

Introduction to OCaml

Introduction to OCaml Fall 2018 Introduction to OCaml Yu Zhang Course web site: http://staff.ustc.edu.cn/~yuzhang/tpl References Learn X in Y Minutes Ocaml Real World OCaml Cornell CS 3110 Spring 2018 Data Structures and Functional

More information

Programming Languages

Programming Languages Programming Languages Tevfik Koşar Lecture - XIII March 2 nd, 2006 1 Roadmap Functional Languages Lambda Calculus Intro to Scheme Basics Functions Bindings Equality Testing Searching 2 1 Functional Languages

More information

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking Agenda COMP 181 Type checking October 21, 2009 Next week OOPSLA: Object-oriented Programming Systems Languages and Applications One of the top PL conferences Monday (Oct 26 th ) In-class midterm Review

More information

The Substitution Model

The Substitution Model The Substitution Model Prof. Clarkson Fall 2017 Today s music: Substitute by The Who Review Previously in 3110: simple interpreter for expression language abstract syntax tree (AST) evaluation based on

More information

Types and Programming Languages. Lecture 5. Extensions of simple types

Types and Programming Languages. Lecture 5. Extensions of simple types Types and Programming Languages Lecture 5. Extensions of simple types Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao Tong University Fall, 2016 Coming soon Simply typed λ-calculus has enough structure

More information

The story so far. Elements of Programming Languages. Pairs in various languages. Pairs

The story so far. Elements of Programming Languages. Pairs in various languages. Pairs Elements of Programming Languages Lecture 6: Data structures James Cheney University of Edinburgh October 9, 2017 The story so far We ve now covered the main ingredients of any programming language: Abstract

More information

Tracing Ambiguity in GADT Type Inference

Tracing Ambiguity in GADT Type Inference Tracing Ambiguity in GADT Type Inference ML Workshop 2012, Copenhagen Jacques Garrigue & Didier Rémy Nagoya University / INRIA Garrigue & Rémy Tracing ambiguity 1 Generalized Algebraic Datatypes Algebraic

More information

CS-XXX: Graduate Programming Languages. Lecture 17 Recursive Types. Dan Grossman 2012

CS-XXX: Graduate Programming Languages. Lecture 17 Recursive Types. Dan Grossman 2012 CS-XXX: Graduate Programming Languages Lecture 17 Recursive Types Dan Grossman 2012 Where are we System F gave us type abstraction code reuse strong abstractions different from real languages (like ML),

More information

Lecture: Functional Programming

Lecture: Functional Programming Lecture: Functional Programming This course is an introduction to the mathematical foundations of programming languages and the implementation of programming languages and language-based tools. We use

More information

Integration of SMT Solvers with ITPs There and Back Again

Integration of SMT Solvers with ITPs There and Back Again Integration of SMT Solvers with ITPs There and Back Again Sascha Böhme and University of Sheffield 7 May 2010 1 2 Features: SMT-LIB vs. Yices Translation Techniques Caveats 3 4 Motivation Motivation System

More information

Simple Unification-based Type Inference for GADTs

Simple Unification-based Type Inference for GADTs Simple Unification-based Type Inference for GADTs Stephanie Weirich University of Pennsylvania joint work with Dimitrios Vytiniotis, Simon Peyton Jones and Geoffrey Washburn Overview Goal: Add GADTs to

More information

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1

A First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1 A First Look at ML Chapter Five Modern Programming Languages, 2nd ed. 1 ML Meta Language One of the more popular functional languages (which, admittedly, isn t saying much) Edinburgh, 1974, Robin Milner

More information

CS 11 Ocaml track: lecture 2

CS 11 Ocaml track: lecture 2 Today: CS 11 Ocaml track: lecture 2 comments algebraic data types more pattern matching records polymorphic types ocaml libraries exception handling Previously... ocaml interactive interpreter compiling

More information

Lecture Notes on Aggregate Data Structures

Lecture Notes on Aggregate Data Structures Lecture Notes on Aggregate Data Structures 15-312: Foundations of Programming Languages Frank Pfenning Lecture 8 September 23, 2004 In this lecture we discuss various language extensions which make MinML

More information

Implementing nml: Hindley-Milner Type Inference

Implementing nml: Hindley-Milner Type Inference Implementing nml: Hindley-Milner Type Inference Due Friday, April 10 at 5:59PM. In this assignment you will implement Hindley-Milner type inference, which represents the current ``best practice'' for flexible

More information

Type structure is a syntactic discipline for maintaining levels of abstraction John Reynolds, Types, Abstraction and Parametric Polymorphism

Type structure is a syntactic discipline for maintaining levels of abstraction John Reynolds, Types, Abstraction and Parametric Polymorphism Chapter 6 Abstraction Type structure is a syntactic discipline for maintaining levels of abstraction John Reynolds, Types, Abstraction and Parametric Polymorphism Abstraction, also known as information

More information

Static Checking and Type Systems

Static Checking and Type Systems 1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009 2 The Structure of our Compiler Revisited Character stream Lexical

More information

cs173: Programming Languages

cs173: Programming Languages The questions on this exam are worth 100 points. Exam rules: cs173: Programming Languages Midterm Exam Fall 2000 Your responses to this exam are due before the start of class on 2000-11-08. If you believe

More information

The Haskell HOP: Higher-order Programming

The Haskell HOP: Higher-order Programming The Haskell HOP: Higher-order Programming COS 441 Slides 6 Slide content credits: Ranjit Jhala, UCSD Agenda Haskell so far: First-order functions This time: Higher-order functions: Functions as data, arguments

More information

Type Systems, Type Inference, and Polymorphism

Type Systems, Type Inference, and Polymorphism 6 Type Systems, Type Inference, and Polymorphism Programming involves a wide range of computational constructs, such as data structures, functions, objects, communication channels, and threads of control.

More information

l e t print_name r = p r i n t _ e n d l i n e ( Name : ^ r. name)

l e t print_name r = p r i n t _ e n d l i n e ( Name : ^ r. name) Chapter 8 Row polymorphism Consider the following code: type name_home = {name : s t r i n g ; home : s t r i n g } type name_mobile = {name : s t r i n g ; mobile : s t r i n g } l e t jane = {name =

More information

The Substitution Model. Nate Foster Spring 2018

The Substitution Model. Nate Foster Spring 2018 The Substitution Model Nate Foster Spring 2018 Review Previously in 3110: simple interpreter for expression language abstract syntax tree (AST) evaluation based on single steps parser and lexer (in lab)

More information

The Design of Core C++ (Notes)

The Design of Core C++ (Notes) The Design of Core C++ (Notes) Uday Reddy May 13, 1994 This note is to define a small formal language called Core C++ which reflects the essential structure of C++. As the name implies, the design only

More information

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson 6.184 Lecture 4 Interpretation Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson 1 Interpretation Parts of an interpreter Arithmetic calculator

More information

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages CMSC 330: Organization of Programming Languages Operational Semantics CMSC 330 Summer 2018 1 Formal Semantics of a Prog. Lang. Mathematical description of the meaning of programs written in that language

More information

Exercises on ML. Programming Languages. Chanseok Oh

Exercises on ML. Programming Languages. Chanseok Oh Exercises on ML Programming Languages Chanseok Oh chanseok@cs.nyu.edu Dejected by an arcane type error? - foldr; val it = fn : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b - foldr (fn x=> fn y => fn z => (max

More information

LECTURE 16. Functional Programming

LECTURE 16. Functional Programming LECTURE 16 Functional Programming WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function of the inputs. Functional programming is a declarative

More information

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016 CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Spring 2016 Type-checking (Static) type-checking can reject a program before it runs to prevent the possibility of some errors A feature

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques () Lecture 13 February 12, 2018 Mutable State & Abstract Stack Machine Chapters 14 &15 Homework 4 Announcements due on February 20. Out this morning Midterm results

More information

Lambda Calculus and Type Inference

Lambda Calculus and Type Inference Lambda Calculus and Type Inference Björn Lisper Dept. of Computer Science and Engineering Mälardalen University bjorn.lisper@mdh.se http://www.idt.mdh.se/ blr/ August 17, 2007 Lambda Calculus and Type

More information

Polymorphism and Type Inference

Polymorphism and Type Inference Polymorphism and Type Inference Volker Stolz stolz@ifi.uio.no Department of Informatics University of Oslo Initially by Gerardo Schneider. Based on John C. Mitchell s slides (Stanford U.) Compile-time

More information

CSE 505, Fall 2008, Final Examination 11 December Please do not turn the page until everyone is ready.

CSE 505, Fall 2008, Final Examination 11 December Please do not turn the page until everyone is ready. CSE 505, Fall 2008, Final Examination 11 December 2008 Please do not turn the page until everyone is ready. Rules: The exam is closed-book, closed-note, except for one side of one 8.5x11in piece of paper.

More information

User-Defined Algebraic Data Types

User-Defined Algebraic Data Types 72 Static Semantics User-Defined Types User-Defined Algebraic Data Types An algebraic data type declaration has the general form: data cx T α 1... α k = K 1 τ 11... τ 1k1... K n τ n1... τ nkn introduces

More information

Metaprogramming assignment 3

Metaprogramming assignment 3 Metaprogramming assignment 3 Optimising embedded languages Due at noon on Thursday 29th November 2018 This exercise uses the BER MetaOCaml compiler, which you can install via opam. The end of this document

More information

Arbitrary-rank polymorphism in (GHC) Haskell

Arbitrary-rank polymorphism in (GHC) Haskell Arbitrary-rank polymorphism in (GHC) Haskell CAS 743 Stephen Forrest 20 March 2006 Damas-Milner Type System A Damas-Milner type system (also called Hindley-Milner) is a traditional type system for functional

More information

The Typed Racket Guide

The Typed Racket Guide The Typed Racket Guide Version 5.3.6 Sam Tobin-Hochstadt and Vincent St-Amour August 9, 2013 Typed Racket is a family of languages, each of which enforce

More information

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 10 February 5 th, 2016 Abstract types: sets Lecture notes: Chapter 10 What is the value of this expresssion? let f (x:bool) (y:int) : int = if x then

More information

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without previous written authorization. 1 2 The simplest way to create

More information

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming Introduction to ML Mooly Sagiv Cornell CS 3110 Data Structures and Functional Programming Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages Call-by-value Operational Semantics

More information

CS 314 Principles of Programming Languages. Lecture 16

CS 314 Principles of Programming Languages. Lecture 16 CS 314 Principles of Programming Languages Lecture 16 Zheng Zhang Department of Computer Science Rutgers University Friday 28 th October, 2016 Zheng Zhang 1 CS@Rutgers University Class Information Reminder:

More information

CSE341: Programming Languages Lecture 7 First-Class Functions. Dan Grossman Winter 2013

CSE341: Programming Languages Lecture 7 First-Class Functions. Dan Grossman Winter 2013 CSE341: Programming Languages Lecture 7 First-Class Functions Dan Grossman Winter 2013 What is functional programming? Functional programming can mean a few different things: 1. Avoiding mutation in most/all

More information

Ornaments in ML. Thomas Williams, Didier Rémy. April 18, Inria - Gallium

Ornaments in ML. Thomas Williams, Didier Rémy. April 18, Inria - Gallium Ornaments in ML Thomas Williams, Didier Rémy Inria - Gallium April 18, 2017 1 Motivation Two very similar functions let rec add m n = match m with Z n S m S (add m n) let rec append ml nl = match ml with

More information