Lesson 11 Universal Types. Universal Types and System F

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

Lesson 4 Typed Arithmetic Typed Lambda Calculus

CIS 500 Software Foundations Fall December 4

Fall 2013 Midterm Exam 10/22/13. This is a closed-book, closed-notes exam. Problem Points Score. Various definitions are provided in the exam.

Lambda Calculi With Polymorphism

Polymorphism. Lecture 19 CS 565 4/17/08

Part III. Chapter 15: Subtyping

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

Part III Chapter 15: Subtyping

Lambda Calculi With Polymorphism

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

Resources: The slides of this lecture were derived from [Järvi], with permission of the original author, by copy & x = 1 let x = 1 in...

Second-Order Type Systems

Recursive Types and Subtyping

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

Programming Language Concepts: Lecture 19

The Untyped Lambda Calculus

Polymorphism and System-F (OV)

Lecture slides & distribution files:

Type Systems. Pierce Ch. 3, 8, 11, 15 CSE

Subsumption. Principle of safe substitution

Graphical Untyped Lambda Calculus Interactive Interpreter

Formal Systems and their Applications

Subtyping. Lecture 13 CS 565 3/27/06

Dependent Polymorphism. Makoto Hamana

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

CSE-321 Programming Languages 2010 Final

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

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

Lecture 9: Typed Lambda Calculus

CIS 500 Software Foundations Midterm I Answer key October 8, 2003

上海交通大学试卷 ( A 卷 ) ( 2015 至 2016 学年第 2 学期 )

Programming Languages Lecture 15: Recursive Types & Subtyping

CS558 Programming Languages

CS558 Programming Languages

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

Type-indexed functions in Generic Haskell

More Untyped Lambda Calculus & Simply Typed Lambda Calculus

A Practical Optional Type System for Clojure. Ambrose Bonnaire-Sergeant

上海交通大学试卷 ( A 卷 ) ( 2015 至 2016 学年第 2 学期 )

Lambda Calculus and Type Inference

Homework 3 COSE212, Fall 2018

λ calculus is inconsistent

Costly software bugs that could have been averted with type checking

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

CS131 Typed Lambda Calculus Worksheet Due Thursday, April 19th

Relaxing a Linear Typing for In-Place Update

Type Checking and Type Inference

Chapter 5: The Untyped Lambda Calculus

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Quick announcement. Midterm date is Wednesday Oct 24, 11-12pm.

Functional Languages. Hwansoo Han

Type families and data kinds

Types, Type Inference and Unification

Typed Lambda Calculus and Exception Handling

Simply-Typed Lambda Calculus

Faith, Evolution, and Programming Languages. Philip Wadler University of Edinburgh

CSE 3302 Programming Languages Lecture 8: Functional Programming

Programming Languages Lecture 14: Sum, Product, Recursive Types

ADT. Typing. Recursive Types. Java. Preliminary: syntax, operational semantics. Untyped lambda calculus. Simply typed lambda calculus

Semantic Subtyping. Alain Frisch (ENS Paris) Giuseppe Castagna (ENS Paris) Véronique Benzaken (LRI U Paris Sud)

Polymorphism and Type Inference

COS 320. Compiling Techniques

Functional Languages and Higher-Order Functions

Types à la Milner. Benjamin C. Pierce University of Pennsylvania. April 2012

Introduction to Co-Induction in Coq

Overloading, Type Classes, and Algebraic Datatypes

Calculus of Inductive Constructions

Midterm 1. CMSC 430 Introduction to Compilers Spring Instructions Total 100. Name: March 14, 2012

CS 320: Concepts of Programming Languages

Type Systems, Type Inference, and Polymorphism

CIS 500 Software Foundations Midterm II. Review questions

Lambda Calculus. Concepts in Programming Languages Recitation 6:

Harvard School of Engineering and Applied Sciences Computer Science 152

CSE-321 Programming Languages 2011 Final

Types and Programming Languages. Lecture 8. Recursive type

The lambda calculus (An introduction)

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

Programming Languages Fall 2014

Combining Programming with Theorem Proving

CS-XXX: Graduate Programming Languages. Lecture 23 Types for OOP; Static Overloading and Multimethods. Dan Grossman 2012

Functional Programming

Programming Proofs and Proving Programs. Nick Benton Microsoft Research, Cambridge

Let Arguments Go First

Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

Logic - CM0845 Introduction to Haskell

The Calculator CS571. Abstract syntax of correct button push sequences. The Button Layout. Notes 16 Denotational Semantics of a Simple Calculator

Witnessing Purity, Constancy and Mutability APLAS Ben Lippmeier Australian National University 2009/12/14

Type Theory. Lecture 2: Dependent Types. Andreas Abel. Department of Computer Science and Engineering Chalmers and Gothenburg University

CMSC 330: Organization of Programming Languages

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

Type Soundness and Race Freedom for Mezzo

Less naive type theory

Meta-programming with Names and Necessity p.1

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Lecture 13: Subtyping

CIS 500 Software Foundations Midterm I

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

int f(char a, char b) {

CIS 500 Software Foundations Fall September 25

Transcription:

Lesson 11 Universal Types 2/28 Chapter 23 Universal Types and System F Varieties of polymorphism System F Examples Basic properties Erasure Evaluation issues Parametricity Impredicativity Lesson 11: Universal Types 2 1

Varieties of polymorphism parametric polymorphism ad hoc polymorphism (overloading) conventional multimethods Haskell type classes intensional polymorphism analyzing and dispatching off of type structure subtype polymorphism (subsumption) OO "polymorphism" ("dynamic binding") row polymorphism (open, extensible record types) Lesson 11: Universal Types 3 System F History: Girard 1972; Reynolds 1974 Idea: lambda abstraction over type variables, defining functions over types. id = LX. lx: X. x id : "X. X -> X id [Nat] Æ [X => Nat] lx: X. x = lx: Nat. x id [Nat] : [X => Nat](X -> X) = Nat -> Nat Lesson 11: Universal Types 4 2

System F: abstract syntax Terms, values, types, contexts: t ::= x lx: T. t t t LX. t t[t] v ::= lx: T. t LX. t T ::= X T -> T "X. T <base types> G ::= G, x : T G, X Lesson 11: Universal Types 5 System F: evaluation Type-passing semantics: evaluation involves types t 1 Æ t 1 ' t 1 [T 2 ] Æ t 1 '[T 2 ] (E-TApp) (LX. t 1 )[T 2 ] Æ [X => T 2 ] t 1 (E-TAppTabs) Lesson 11: Universal Types 6 3

System F: typing Type-level abstraction and application rules: G, X - t : T G - LX. t : "X. T (E-TAbs) G - t : "X. T 1 (E-TApp) G - t[t 2 ] : [X => T 2 ] T 1 Lesson 11: Universal Types 7 System F: examples double = LX. lf: X -> X. la: X. f(f a) double : "X. (X -> X) -> X -> X doublenat = double[nat] doublenat : (Nat -> Nat) -> Nat -> Nat selfapp = lx: "X. (X -> X). x["x. (X -> X)] x selfapp : ("X. (X -> X)) -> ("X. (X -> X)) guadruple = LX. double[x -> X] (double[x]) quadruple : "X. (X -> X) -> X -> X Lesson 11: Universal Types 8 4

System F: lists nil cons isnil head tail : "X. List X : "X. X -> List X -> List X : "X. List X -> Bool : "X. List X -> X : "X. List X -> List X map = LX. LX. lf: X -> Y. fix(lm: List X -> List Y). ll: List X. if isnil [X] l then nil [Y] else cons [Y] (f (head[x] l)) (m (tail [X] l)))) map : "X. "X. (X -> Y) -> List X -> List Y Lesson 11: Universal Types 9 System F: Church encodings CBool = "X. X -> X -> X tru = LX. lx: X. ly: X. x : CBool fls = LX. lx: X. ly: X. y : CBool Any other terms of type CBool? CNat = CBool = "X. (X -> X) -> X -> X c 0 = LX. ls: X -> X. lz: X. z c 1 = LX. ls: X -> X. lz: X. s z c 2 = LX. ls: X -> X. lz: X. s (s z)... Any other terms of type CNat? Lesson 11: Universal Types 10 5

System F: Encoding Lists List X = "R. (X -> R -> R) -> R -> R nil = LX. (LR. lc: X -> R -> R. ln: R. n) as List X nil : "X. List X cons = LX. lhd:x. ltl: List X. (LR. lc: X -> R -> R. ln: R. c hd (tl[r] c n)) as List X cons : "X. X -> List X -> List X Lesson 11: Universal Types 11 Theoretical properties Thm [Preservation]: If G - t : T and t Æ t' then G - t' : T. Thm [Progress]: If t is a closed, well-typed term ( - t : T) then either t is a value or t Æ t' for some t'. Proofs are similar to those for simply typed lambda calculus with added cases for type abstraction and application. Theorem [Normalization]: Well-typed terms of System F are normalizing. Proof: very delicate! Lesson 11: Universal Types 12 6

Erasure and type reconstruction Easy to map System F to untyped lambda calculus: erase (x) = x erase (lx: T. t) = lx. erase(t) erase (t 1 t 2 ) = (erase(t 1 )) (erase(t 2 )) erase (LX. t) = erase(t) erase (t[t]) = erase(t) Thm [Wells, 94]: It is undecidable whether, given a closed term m of the untyped lambda calculus, there is a well-typed term t in System F such that m = erase(t). However, there is lots of work on partial solutions to the type reconstruction problem for System F. Lesson 11: Universal Types 13 Erasure and evaluation Erasure operational semantics throws away types before evaluation. But have to preserve value nature of LX. t: let f = LX. error in 0 produces no error because LX is suspending. So define erasure for evaluation as follows: erase (x) = x erase (lx: T. t) = lx. erase(t) erase (t 1 t 2 ) = (erase(t 1 )) (erase(t 2 )) erase (LX. t) = l_.erase(t) erase (t[t]) = erase(t)() Lesson 11: Universal Types 14 7

Impredicativity System F is impredicative, meaning that polymorphic types are defined by (universal) quantification over the universe of all types, including the polymorphic types themselves. Another way of puting it is that polymorphic types are first-class in the world of types. Polymorphism in ML is predicative, and polymorphic types are therefore second-class (e.g terms do not have polymorphic types). Lesson 11: Universal Types 15 8