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

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

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

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

Programming Languages

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

Tradeoffs. CSE 505: Programming Languages. Lecture 15 Subtyping. Where shall we add useful completeness? Where shall we add completeness?

CSE 505: Concepts of Programming Languages

Harvard School of Engineering and Applied Sciences Computer Science 152

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

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

CSE-505: Programming Languages. Lecture 20.5 Recursive Types. Zach Tatlock 2016

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

CSE-505: Programming Languages. Lecture 18 Existential Types. Zach Tatlock 2016

1 Introduction. 3 Syntax

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

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

CS152: Programming Languages. Lecture 7 Lambda Calculus. Dan Grossman Spring 2011

CSE-321 Programming Languages 2010 Final

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

We defined congruence rules that determine the order of evaluation, using the following evaluation

Whereweare. CS-XXX: Graduate Programming Languages. Lecture 7 Lambda Calculus. Adding data structures. Data + Code. What about functions

Lecture 13: Subtyping

Programming Languages Lecture 14: Sum, Product, Recursive Types

CSCI-GA Scripting Languages

Simply-Typed Lambda Calculus

Programming Languages Lecture 15: Recursive Types & Subtyping

CSE505, Fall 2012, Midterm Examination October 30, 2012

CS152: Programming Languages. Lecture 24 Bounded Polymorphism; Classless OOP. Dan Grossman Spring 2011

Recitation 6: System F and Existential Types : Foundations of Programming Languages

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

Type Systems. Parametric Polymorphism. 1. Recall Let-Polymorphism. 1. Recall Let-Polymorphism. Lecture 9 Dec. 15th, 2004 Sebastian Maneth

Tracing Ambiguity in GADT Type Inference

λ calculus is inconsistent

Symmetry in Type Theory

Subsumption. Principle of safe substitution

Typed Lambda Calculus and Exception Handling

CIS 500 Software Foundations Fall December 6

Administrivia. Existential Types. CIS 500 Software Foundations Fall December 6. Administrivia. Motivation. Motivation

Featherweight Java (FJ)

Second-Order Type Systems

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

CS-XXX: Graduate Programming Languages. Lecture 22 Class-Based Object-Oriented Programming. Dan Grossman 2012

Part III. Chapter 15: Subtyping

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

Introduction to System F. Lecture 18 CS 565 4/20/09

Gradual Typing for Functional Languages. Jeremy Siek and Walid Taha (presented by Lindsey Kuper)

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

CMSC 336: Type Systems for Programming Languages Lecture 5: Simply Typed Lambda Calculus Acar & Ahmed January 24, 2008

CSE-321 Programming Languages 2011 Final

Lambda Calculus and Type Inference

Types for References, Exceptions and Continuations. Review of Subtyping. Γ e:τ τ <:σ Γ e:σ. Annoucements. How s the midterm going?

Dependent types and program equivalence. Stephanie Weirich, University of Pennsylvania with Limin Jia, Jianzhou Zhao, and Vilhelm Sjöberg

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

Lecture 9: Typed Lambda Calculus

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

Generic polymorphism on steroids

Lambda Calculus: Implementation Techniques and a Proof. COS 441 Slides 15

Lecture 5: The Untyped λ-calculus

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

Don t Believe the Hype. CS152: Programming Languages. Lecture 21 Object-Oriented Programming. Class-based OOP. So what is OOP?

Variables. Substitution

More Lambda Calculus and Intro to Type Systems

Higher-Order Intensional Type Analysis. Stephanie Weirich Cornell University

Part III Chapter 15: Subtyping

Adding GADTs to OCaml the direct approach

The design of a programming language for provably correct programs: success and failure

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

COS 320. Compiling Techniques

CS 4110 Programming Languages & Logics. Lecture 17 Programming in the λ-calculus

Lecture Notes on Data Representation

CS152: Programming Languages. Lecture 23 Advanced Concepts in Object-Oriented Programming. Dan Grossman Spring 2011

Dependent types and program equivalence. Stephanie Weirich, University of Pennsylvania with Limin Jia, Jianzhou Zhao, and Vilhelm Sjöberg

COMP 1130 Lambda Calculus. based on slides by Jeff Foster, U Maryland

Concepts of programming languages

Type Checking and Type Inference

Let Arguments Go First

Recursive Types and Subtyping

Programming Languages Fall 2014

The Typed λ Calculus and Type Inferencing in ML

CIS 500 Software Foundations Fall October 2

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

Programming with Types

Subtyping. Lecture 13 CS 565 3/27/06

CS 4110 Programming Languages & Logics. Lecture 27 Recursive Types

CSE-321 Programming Languages 2014 Final

CS 6110 S14 Lecture 1 Introduction 24 January 2014

Type Systems Winter Semester 2006

Typing Control. Chapter Conditionals

Part VI. Imperative Functional Programming

CIS 500 Software Foundations Fall September 25

Advances in Programming Languages

CSE 505: Programming Languages. Lecture 10 Types. Zach Tatlock Winter 2015

Lambda Calculi With Polymorphism

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

Formal Systems and their Applications

Types and Programming Languages. Lecture 8. Recursive type

Polymorphism. Lecture 19 CS 565 4/17/08

Lecture #23: Conversion and Type Inference

Where is ML type inference headed?

Transcription:

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 -> a mylist -> a mylist val decons : a mylist -> (( a * a mylist) option) val length : a mylist -> int val map : ( a -> b) -> a mylist -> b mylist From two perspectives: 1. Library: Implement code to this partial specification 2. Client: Use code written to this partial specification Dan Grossman CS152 Spring 2011, Lecture 15 2 What The Client Likes 1. Library is reusable. Can make: Different lists with elements of different types New reusable functions outside of library, e.g.: val twocons : a -> a -> a mylist -> a mylist 2. Easier, faster, more reliable than subtyping No downcast to write, run, maybe-fail (cf. Java 1.4 Vector) 3. Library must behave the same for all type instantiations! a and b held abstract from library E.g., with built-in lists: If foo has type a list -> int, then foo [1;2;3] and foo [(5,4);(7,2);(9,2)] are totally equivalent! (Never true with downcasts) In theory, means less (re-)integration testing Proof is beyond this course, but not much What the Library Likes 1. Reusability For same reasons client likes it 2. Abstraction of mylist from clients Clients must behave the same for all equivalent implementations, even if hidden definition of a mylist changes Clients typechecked knowing only there exists a type constructor mylist Unlike Java, C++, R5RS Scheme, no way to downcast a t mylist to, e.g., a pair Dan Grossman CS152 Spring 2011, Lecture 15 3 Dan Grossman CS152 Spring 2011, Lecture 15 4 Start simpler The interface has a lot going on: 1. Element types held abstract from library 2. List type (constructor) held abstract from client 3. Reuse of type variables makes connections among expressions of abstract types 4. Lists need some form of recursive type This lecture considers just (1) and (3) First using a formal language with explicit type abstraction Then mention differences from ML Syntax e ::= c x λx:τ. e ee Λα. e e[τ ] τ ::= int τ τ α α.τ v ::= c λx:τ. e Λα. e Γ ::= Γ,x:τ Δ ::= Δ,α New things: Terms: Type abstraction and type application Types: Type variables and universal types Type contexts: what type variables are in scope Note: Much more interesting than not getting stuck Dan Grossman CS152 Spring 2011, Lecture 15 5 Dan Grossman CS152 Spring 2011, Lecture 15 6

Informal semantics 1. Λα. e: A value that when used runs e (with some τ for α) To type-check e, knowα is one type, but not which type 2. e[τ ]: Evaluatee to some Λα. e and then run e With τ for α, but the choice of τ is irrelevant at run-time τ used for type-checking and proof of Preservation 3. Types can use type variables α, β, etc., but only if they re in scope (just like term variables) Type-checking will be Δ; Γ e : τ using Δ to know what type variables are in scope in e In universal type α.τ, can also use α in τ Dan Grossman CS152 Spring 2011, Lecture 15 7 Operational semantics Small-step, CBV, left-to-right operational semantics: Note: Λα. e is a value Old: e 1 e 1 e 1 e 2 e 1 e 2 New: e[τ ] e [τ ] e 2 e 2 ve 2 ve 2 (λx:τ. e) v e[v/x] (Λα. e)[τ ] e[τ/α] Plus now have 3 different kinds of substitution, all defined in straightforward capture-avoiding way: e 1 [e 2 /x] (old) e[τ /α] (new) τ [τ /α] (new) Dan Grossman CS152 Spring 2011, Lecture 15 8 Example Example (using addition): (Λα. Λβ. λx : α. λf:α β. f x)[int][int]3(λy : int.y+ y) (Λβ. λx : int. λf:int β. f x) [int] 3(λy : int. y+ y) (λx : int. λf:int int. fx)3(λy : int. y+ y) (λf:int int. f3) (λy : int. y+ y) (λy : int. y+ y) 3 3+3 Type System, part 1 Mostly just need to be picky about no free type variables Typing judgment has the form Δ; Γ e : τ (whole program ; e : τ ) Next slide Uses helper judgment Δ τ all free type variables in τ are in Δ Δ τ α Δ Δ α Δ int Δ τ 1 Δ τ 2 Δ τ 1 τ 2 Δ,α τ Δ α.τ Rules are boring, but trust me, allowing free type variables is a pernicious source of language/compiler bugs 6 Dan Grossman CS152 Spring 2011, Lecture 15 9 Dan Grossman CS152 Spring 2011, Lecture 15 10 Type System, part 2 Old (with one technical change to prevent free type variables): Δ; Γ x :Γ(x) Δ; Γ c : int Example Example (using addition): (Λα. Λβ. λx : α. λf:α β. f x)[int][int]3(λy : int.y+ y) Δ; Γ,x:τ 1 e : τ 2 Δ τ 1 Δ; Γ λx:τ 1.e: τ 1 τ 2 (The typing derivation is rather tall and painful, but just a syntax-directed derivation by instantiating the typing rules) Δ; Γ e 1 : τ 2 τ 1 Δ; Γ e 2 : τ 2 Δ; Γ e 1 e 2 : τ 1 New: Δ,α;Γ e : τ 1 Δ; Γ e : α.τ 1 Δ τ 2 Δ; Γ Λα. e : α.τ 1 Δ; Γ e[τ 2 ]:τ 1 [τ 2 /α] Dan Grossman CS152 Spring 2011, Lecture 15 11 Dan Grossman CS152 Spring 2011, Lecture 15 12

The Whole Language, Called System F e ::= c x λx:τ. e ee Λα. e e[τ ] τ ::= int τ τ α α.τ v ::= c λx:τ. e Λα. e Γ ::= Γ,x:τ Δ ::= Δ,α ee 2 e e 2 ve ve e[τ ] e [τ ] (λx:τ. e) v e[v/x] (Λα. e)[τ ] e[τ/α] Δ; Γ x :Γ(x) Δ; Γ c : int Examples An overly simple polymorphic function... Let id = Λα. λx : α. x id has type α.α α id [int] has type int int id [int int] has type (int int) (int int) (id [ α.α α]) id has type α.α α In ML you can t do the last one; in System F you can Δ; Γ,x:τ 1 e : τ 2 Δ τ 1 Δ; Γ λx:τ 1.e: τ 1 τ 2 Δ,α;Γ e : τ 1 Δ; Γ Λα. e : α.τ 1 Δ; Γ e 1 : τ 2 τ 1 Δ; Γ e 2 : τ 2 Δ; Γ e : α.τ 1 Δ τ 2 Δ; Γ e 1 e 2 : τ 1 Δ; Γ e[τ 2 ]:τ 1 [τ 2 /α] Dan Grossman CS152 Spring 2011, Lecture 15 13 Dan Grossman CS152 Spring 2011, Lecture 15 14 More Examples Let apply1 = Λα. Λβ. λx : α. λf : α β. f x apply1 has type α. β.α (α β) β ; g:int int (apply1 [int][int] 3g) :int Let apply2 = Λα. λx : α. Λβ. λf : α β. f x apply2 has type α.α ( β.(α β) β) (impossible in ML) ; g:int string,h:int int (let z = apply2 [int] in z (z 3[int] h) [string] g) : string What next? Having defined System F... Metatheory (what properties does it have) What (else) is it good for How/why ML is more restrictive and implicit Let twice = Λα. λx : α. λf : α α. f (f x). twice has type α.α (α α) α Cannot be made more polymorphic Dan Grossman CS152 Spring 2011, Lecture 15 15 Dan Grossman CS152 Spring 2011, Lecture 15 16 Metatheory Safety: Language is type-safe Need a Type Substitution Lemma Termination: All programs terminate Surprising we saw id [τ ]id Parametricity, a.k.a. theorems for free Example: If ; e: α. β.(α β) (β α), thene is equivalent to Λα. Λβ. λx:α β. (x.2,x.1). Every term with this type is the swap function!! Intuition: e has no way to make an α or a β anditcannottell what α or β are or raise an exception or diverge... Erasure: Types do not affect run-time behavior Security from safety? Example: A process e should not access files it did not open (fopen can check permissions) Require an untrusted process e to type-check as follows: ; e : α.{fopen : string α, fread : α int} unit. This type ensures that a process won t forge a file handle and pass it to fread So fread doesn t need to check (faster), file handles don t need to be encrypted (safer), etc. Note: Mutation breaks everything depth subtyping: hw4, termination: hw3, parametricity: hw5 Dan Grossman CS152 Spring 2011, Lecture 15 17 Dan Grossman CS152 Spring 2011, Lecture 15 18

Moral of Example In simply-typed lambda-calculus, type safety just means not getting stuck With type abstraction, it enables secure interfaces! Suppose we (the system library) implement file-handles as ints. Then we instantiate α with int, but untrusted code cannot tell Memory safety is a necessary but insufficient condition for language-based enforcement of strong abstractions Are types used at run-time? We said polymorphism was about many types for same term, but for clarity and easy checking, we changed: The syntax via Λα. e and e [τ ] The operational semantics via type substitution The type system via Δ Claim: The operational semantics did not really change; types need not exist at run-time More formally: Erasing all types from System F produces an equivalent program in the untyped lambda calculus Strengthened induction hypothesis: If e e 1 in System F and erase(e) e 2 in untyped lambda-calculus, then e 2 = erase(e 1 ) Dan Grossman CS152 Spring 2011, Lecture 15 19 Erasure and evaluation commute Dan Grossman CS152 Spring 2011, Lecture 15 20 Erasure Connection to reality Erasure is easy to define: erase(c) = c erase(x) = x erase(e 1 e 2 ) = erase(e 1 ) erase(e 2 ) erase(λx:τ. e) = λx. erase(e) erase(λα. e) = λ. erase(e) erase(e [τ ]) = erase(e) 0 System F has been one of the most important theoretical PL models since the 1970s and inspires languages like ML. But you have seen ML polymorphism and it looks different. In fact, it is an implicitly typed restriction of System F. These two qualifications ((1) implicit, (2) restriction) are deeply related. In pure System F, preserving evaluation order isn t crucial, but it is with fix, exceptions, mutation, etc. Dan Grossman CS152 Spring 2011, Lecture 15 21 Dan Grossman CS152 Spring 2011, Lecture 15 22 Restrictions All types have the form α 1,...,α n.τ where n 0 and τ has no. (Prenex-quantification; no first-class polymorphism.) Only let (rec) variables (e.g., x in let x = e1 in e2)can have polymorphic types. So n =0for function arguments, pattern variables, etc. (Let-bound polymorphism) So cannot (always) desugar let to λ in ML In let rec f x = e1 in e2, the variable f can have type α 1,...,α n.τ 1 τ 2 only if every use of f in e1 instantiates each α i with α i. (No polymorphic recursion) Let variables can be polymorphic only if e1 is a syntactic value a variable, constant, function definition,... Called the value restriction (relaxed partially in OCaml) Dan Grossman CS152 Spring 2011, Lecture 15 23 Why? ML-style polymorphism can seem weird after you have seen System F. And the restrictions do come up in practice, though tolerable. Type inference for System F (given untyped e, is there a System F term e such that erase(e )=e) is undecidable (1995) Type inference for ML with polymorphic recursion is undecidable (1992) Type inference for ML is decidable and efficient in practice, though pathological programs of size O(n) and run-time O(n) can have types of size O(2 2n ) The type inference algorithm is unsound in the presence of ML-style mutation, but value-restriction restores soundness Based on unification guest lecture coming soon Dan Grossman CS152 Spring 2011, Lecture 15 24

Recovering lost ground? Extensions to the ML type system to be closer to System F: Usually require some type annotations Are judged by: Soundness: Do programs still not get stuck? Conservatism: Do all (or most) old ML programs still type-check? Power: Does it accept many more useful programs? Convenience: Are many new types still inferred? Dan Grossman CS152 Spring 2011, Lecture 15 25