CS-XXX: Graduate Programming Languages. Lecture 27 Higher-Order Polymorphism. Matthew Fluet 2012

Σχετικά έγγραφα
The Simply Typed Lambda Calculus

About these lecture notes. Simply Typed λ-calculus. Types

C.S. 430 Assignment 6, Sample Solutions

derivation of the Laplacian from rectangular to spherical coordinates

2 Composition. Invertible Mappings

Dynamic types, Lambda calculus machines Section and Practice Problems Apr 21 22, 2016

Lecture 2. Soundness and completeness of propositional logic

Ordinal Arithmetic: Addition, Multiplication, Exponentiation and Limit

Overview. Transition Semantics. Configurations and the transition relation. Executions and computation

Chapter 3: Ordinal Numbers

4.6 Autoregressive Moving Average Model ARMA(1,1)

The λ-calculus. Lecturer: John Wickerson. Phil Wadler

Nowhere-zero flows Let be a digraph, Abelian group. A Γ-circulation in is a mapping : such that, where, and : tail in X, head in

HOMEWORK 4 = G. In order to plot the stress versus the stretch we define a normalized stretch:

6.1. Dirac Equation. Hamiltonian. Dirac Eq.

EE512: Error Control Coding

Econ 2110: Fall 2008 Suggested Solutions to Problem Set 8 questions or comments to Dan Fetter 1

3.4 SUM AND DIFFERENCE FORMULAS. NOTE: cos(α+β) cos α + cos β cos(α-β) cos α -cos β

Lecture 2: Dirac notation and a review of linear algebra Read Sakurai chapter 1, Baym chatper 3

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 19/5/2007

Section 8.3 Trigonometric Equations

ΓΡΑΜΜΙΚΟΣ & ΔΙΚΤΥΑΚΟΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ

Statistical Inference I Locally most powerful tests

Section 9.2 Polar Equations and Graphs

Second Order Partial Differential Equations

CHAPTER 25 SOLVING EQUATIONS BY ITERATIVE METHODS

Matrices and Determinants

A Lambda Model Characterizing Computational Behaviours of Terms

Partial Trace and Partial Transpose

Phys460.nb Solution for the t-dependent Schrodinger s equation How did we find the solution? (not required)

Concrete Mathematics Exercises from 30 September 2016

Finite Field Problems: Solutions

Homework 3 Solutions

Problem Set 3: Solutions

Abstract Storage Devices

From the finite to the transfinite: Λµ-terms and streams

k A = [k, k]( )[a 1, a 2 ] = [ka 1,ka 2 ] 4For the division of two intervals of confidence in R +

Math 6 SL Probability Distributions Practice Test Mark Scheme

Every set of first-order formulas is equivalent to an independent set

ΕΛΛΗΝΙΚΗ ΔΗΜΟΚΡΑΤΙΑ ΠΑΝΕΠΙΣΤΗΜΙΟ ΚΡΗΤΗΣ. Ψηφιακή Οικονομία. Διάλεξη 7η: Consumer Behavior Mαρίνα Μπιτσάκη Τμήμα Επιστήμης Υπολογιστών

TMA4115 Matematikk 3

Math221: HW# 1 solutions

CRASH COURSE IN PRECALCULUS

Example Sheet 3 Solutions

Section 7.6 Double and Half Angle Formulas

Assalamu `alaikum wr. wb.

ST5224: Advanced Statistical Theory II

5. Choice under Uncertainty

SCHOOL OF MATHEMATICAL SCIENCES G11LMA Linear Mathematics Examination Solutions

Notes on the Open Economy

Συστήματα Διαχείρισης Βάσεων Δεδομένων

department listing department name αχχουντσ ϕανε βαλικτ δδσϕηασδδη σδηφγ ασκϕηλκ τεχηνιχαλ αλαν ϕουν διξ τεχηνιχαλ ϕοην µαριανι

Sequent Calculi for the Modal µ-calculus over S5. Luca Alberucci, University of Berne. Logic Colloquium Berne, July 4th 2008

Partial Differential Equations in Biology The boundary element method. March 26, 2013

Other Test Constructions: Likelihood Ratio & Bayes Tests

Practice Exam 2. Conceptual Questions. 1. State a Basic identity and then verify it. (a) Identity: Solution: One identity is csc(θ) = 1

Instruction Execution Times

Inverse trigonometric functions & General Solution of Trigonometric Equations

Problem Set 9 Solutions. θ + 1. θ 2 + cotθ ( ) sinθ e iφ is an eigenfunction of the ˆ L 2 operator. / θ 2. φ 2. sin 2 θ φ 2. ( ) = e iφ. = e iφ cosθ.

Approximation of distance between locations on earth given by latitude and longitude

forms This gives Remark 1. How to remember the above formulas: Substituting these into the equation we obtain with

PARTIAL NOTES for 6.1 Trigonometric Identities

Srednicki Chapter 55

Chapter 6: Systems of Linear Differential. be continuous functions on the interval

Εργαστήριο Ανάπτυξης Εφαρμογών Βάσεων Δεδομένων. Εξάμηνο 7 ο

Congruence Classes of Invertible Matrices of Order 3 over F 2

ANSWERSHEET (TOPIC = DIFFERENTIAL CALCULUS) COLLECTION #2. h 0 h h 0 h h 0 ( ) g k = g 0 + g 1 + g g 2009 =?

A Note on Intuitionistic Fuzzy. Equivalence Relation

Fourier Series. MATH 211, Calculus II. J. Robert Buchanan. Spring Department of Mathematics

Solutions to Exercise Sheet 5

Jesse Maassen and Mark Lundstrom Purdue University November 25, 2013

Commutative Monoids in Intuitionistic Fuzzy Sets

Solutions to the Schrodinger equation atomic orbitals. Ψ 1 s Ψ 2 s Ψ 2 px Ψ 2 py Ψ 2 pz

( ) 2 and compare to M.

Foundations of Computer Science ENGR 3520 Fall 2013 Thursday, Nov 21, 2013

Απόκριση σε Μοναδιαία Ωστική Δύναμη (Unit Impulse) Απόκριση σε Δυνάμεις Αυθαίρετα Μεταβαλλόμενες με το Χρόνο. Απόστολος Σ.

Bounding Nonsplitting Enumeration Degrees

MATH423 String Theory Solutions 4. = 0 τ = f(s). (1) dτ ds = dxµ dτ f (s) (2) dτ 2 [f (s)] 2 + dxµ. dτ f (s) (3)

Space-Time Symmetries

Second Order RLC Filters

The challenges of non-stable predicates

ω ω ω ω ω ω+2 ω ω+2 + ω ω ω ω+2 + ω ω+1 ω ω+2 2 ω ω ω ω ω ω ω ω+1 ω ω2 ω ω2 + ω ω ω2 + ω ω ω ω2 + ω ω+1 ω ω2 + ω ω+1 + ω ω ω ω2 + ω

Χρειάζεται να φέρω μαζί μου τα πρωτότυπα έγγραφα ή τα αντίγραφα; Asking if you need to provide the original documents or copies Ποια είναι τα κριτήρια

Homework 8 Model Solution Section

SUPERPOSITION, MEASUREMENT, NORMALIZATION, EXPECTATION VALUES. Reading: QM course packet Ch 5 up to 5.6

DESIGN OF MACHINERY SOLUTION MANUAL h in h 4 0.

Chap. 6 Pushdown Automata

Exercises 10. Find a fundamental matrix of the given system of equations. Also find the fundamental matrix Φ(t) satisfying Φ(0) = I. 1.

[1] P Q. Fig. 3.1

Finitary proof systems for Kozen s µ

Fractional Colorings and Zykov Products of graphs

9.09. # 1. Area inside the oval limaçon r = cos θ. To graph, start with θ = 0 so r = 6. Compute dr

λρ-calculus 1. each λ-variable is a λρ-term, called an atom or atomic term; 2. if M and N are λρ-term then (MN) is a λρ-term called an application;

Συντακτικές λειτουργίες

Proving with Computer Assistance Lecture 2. Herman Geuvers

Rewrite semantics for guarded recursion with universal quantification over clocks

Numerical Analysis FMN011

Εγχειρίδια Μαθηµατικών και Χταποδάκι στα Κάρβουνα

Tridiagonal matrices. Gérard MEURANT. October, 2008

Transcript:

CS-XXX: Graduate Programming Languages Lecture 27 Higher-Order Polymorphism Matthew Fluet 2012

Looking back, looking forward Have defined System F. Metatheory (what properties does it have) What (else) is it good for How/why ML is more restrictive and implicit Recursive types (also use type variables, but differently) Existential types (dual to universal types) Next: Type operators and type-level computations Matthew Fluet CS-XXX 2012, Lecture 27 2

System F with Recursive and Existential Types e cbv e e ::= c x λx:τ. e e e Λα. e e [τ ] pack α. τ (τ, e) unpack e as (α, x) in e roll µα. τ (e) unroll(e) v ::= c λx:τ. e Λα. e pack α. τ (τ, v) roll µα. τ (v) (λx: τ. e b ) v a cbv e b [v a/x] e f cbv e f e f e a cbv e f ea e a cbv ea v f e a cbv v f e a (Λα. e b ) [τ a] cbv e b [τ a/α] e f cbv e f e f [τ a] cbv e f [τa] e a cbv e a pack α. τ (τ w, e a) cbv pack α. τ (τ w, e a ) e a cbv e a unpack e a as (α, x) in e b cbv unpack e a as (α, x) in e b unpack pack α. τ (τ w, v a) as (α, x) in e b cbv e b [τ w/α][v a/x] e a cbv e a unroll(e a) cbv unroll(e a ) unroll(rollµα. τ (va)) cbv va Matthew Fluet CS-XXX 2012, Lecture 27 3

System F with Recursive and Existential Types τ ::= int τ τ α α. τ α. τ µα. τ ::=, α Γ ::= Γ, x:τ ; Γ e : τ ; Γ c : int Γ(x) = τ ; Γ x : τ τ a ; Γ, x : τ a e b : τ r ; Γ e f : τ a τ r ; Γ e a : τ a ; Γ λx:τ a. e b : τ a τ r ; Γ e f e a : τ r, α; Γ e b : τ r ; Γ e f : α. τ r τ a ; Γ Λα. e b : α. τ r ; Γ e f [τ a] : τ r[τ a/α] ; Γ e a : τ [τ w/α] ; Γ pack α. τ (τ w, e a) : α. τ ; Γ e a : τ [(µα. τ )/α] ; Γ roll µα. τ (e a) : µα. τ ; Γ e a : α. τ, α; Γ, x:τ e b : τ r τ r ; Γ unpack e a as (α, x) in e b : τ r ; Γ e a : µα. τ ; Γ unroll(e a) : τ [(µα. τ )/α] Matthew Fluet CS-XXX 2012, Lecture 27 4

Goal Understand what this interface means and why it matters: type 'a list val empty : 'a list val cons : 'a -> 'a list -> 'a list val unlist : 'a list -> (' a * 'a list ) option val size : 'a list -> int val map : (' a -> 'b) -> 'a list -> 'b list Story so far: Recursive types to define list data structure Universal types to keep element type abstract in library Existential types to keep list type abstract in client But, cheated when abstracting the list type in client: considered just intlist. Matthew Fluet CS-XXX 2012, Lecture 27 5

(Integer) List Library with List library is an existential package: pack(µξ. unit + (int ξ), list library) as L. {empty : L; cons : int L L; unlist : L unit + (int L); map : (int int) L L;...} The witness type is integer lists: µξ. unit + (int ξ). The existential type variable L represents integer lists. List operations are monomorphic in element type (int). The map function only allows mapping integer lists to integer lists. Matthew Fluet CS-XXX 2012, Lecture 27 6

(Polymorphic?) List Library with / List library is a type abstraction that yields an existential package: Λα. pack(µξ. unit + (α ξ), list library) as L. {empty : L; cons : α L L; unlist : L unit + (α L); map : (α α) L L;...} The witness type is α lists: µξ. unit + (α ξ). The existential type variable L represents α lists. List operations are monomorphic in element type (α). The map function only allows mapping α lists to α lists. Matthew Fluet CS-XXX 2012, Lecture 27 7

Type Abbreviations and Type Operators Reasonable enough to provide list type as a (parametric) type abbreviation: L α = µξ. unit + (α ξ) replace occurrences of L τ in programs with (µξ. unit + (α ξ))[τ /α] Gives an informal notion of functions at the type-level. But, doesn t help with with list library, because this exposes the definition of list type. How modular and safe are libraries built from cpp macros? Matthew Fluet CS-XXX 2012, Lecture 27 8

Type Abbreviations and Type Operators Instead, provide list type as a type operator: a function from types to types L = λα. µξ. unit + (α ξ) Gives a formal notion of functions at the type-level. abstraction and application at the type-level equivalence of type-level expressions well-formedness of type-level expressions List library will be an existential package that hides a type operator, (rather than a type). Matthew Fluet CS-XXX 2012, Lecture 27 9

Type-level Expressions Abstraction and application at the type level makes it possible to write the same type with different syntax. Id = λα. α int bool int Id bool Id int bool Id int Id bool Id (int bool) Id (Id (int bool))... Matthew Fluet CS-XXX 2012, Lecture 27 10

Type-level Expressions Abstraction and application at the type level makes it possible to write the same type with different syntax. Id = λα. α int bool int Id bool Id int bool Id int Id bool Id (int bool) Id (Id (int bool))... Require a precise definition of when two types are the same: τ τ... (λα. τ b ) τ a τ b [α/τ a ]... Matthew Fluet CS-XXX 2012, Lecture 27 10

Type-level Expressions Abstraction and application at the type level makes it possible to write the same type with different syntax. Id = λα. α int bool int Id bool Id int bool Id int Id bool Id (int bool) Id (Id (int bool))... Require a typing rule to exploit types that are the same: ; Γ e : τ... ; Γ e : τ τ τ ; Γ e : τ... Matthew Fluet CS-XXX 2012, Lecture 27 10

Type-level Expressions Abstraction and application at the type level makes it possible to write the same type with different syntax. Id = λα. α int bool int Id bool Id int bool Id int Id bool Id (int bool) Id (Id (int bool))... Admits wrong/bad/meaningless types:... bool int (Id bool) int bool (Id int)... Matthew Fluet CS-XXX 2012, Lecture 27 10

Type-level Expressions Abstraction and application at the type level makes it possible to write the same type with different syntax. Id = λα. α int bool int Id bool Id int bool Id int Id bool Id (int bool) Id (Id (int bool))... Require a type system for types: τ :: κ... τ f :: κ a κ r τ a :: κ a... τ f τ a :: κ r Matthew Fluet CS-XXX 2012, Lecture 27 10

Terms, Types, and Kinds, Oh My Matthew Fluet CS-XXX 2012, Lecture 27 11

Terms, Types, and Kinds, Oh My Terms: e ::= c x λx:τ. e e e Λα::κ. e e [τ ] v ::= c λx:τ. e Λα::κ. e atomic values (e.g., c) and operations (e.g., e + e) compound values (e.g., (v,v)) and operations (e.g., e.1) value abstraction and application type abstraction and application classified by types (but not all terms have a type) Matthew Fluet CS-XXX 2012, Lecture 27 11

Terms, Types, and Kinds, Oh My Terms: e ::= c x λx:τ. e e e Λα::κ. e e [τ ] v ::= c λx:τ. e Λα::κ. e atomic values (e.g., c) and operations (e.g., e + e) compound values (e.g., (v,v)) and operations (e.g., e.1) value abstraction and application type abstraction and application classified by types (but not all terms have a type) Types: τ ::= int τ τ α α::κ. τ λα::κ. τ τ τ atomic types (e.g., int) classify the terms that evaluate to atomic values compound types (e.g., τ τ ) classify the terms that evaluate to compound values function types τ τ classify the terms that evaluate to value abstractions universal types α. τ classify the terms that evaluate to type abstractions type abstraction and application type abstractions do not classify terms, but can be applied to type arguments to form types that do classify terms classified by kinds (but not all types have a kind) Matthew Fluet CS-XXX 2012, Lecture 27 11

Terms, Types, and Kinds, Oh My Types: τ ::= int τ τ α α::κ. τ λα::κ. τ τ τ atomic types (e.g., int) classify the terms that evaluate to atomic values compound types (e.g., τ τ ) classify the terms that evaluate to compound values function types τ τ classify the terms that evaluate to value abstractions universal types α. τ classify the terms that evaluate to type abstractions type abstraction and application type abstractions do not classify terms, but can be applied to type arguments to form types that do classify terms classified by kinds (but not all types have a kind) Matthew Fluet CS-XXX 2012, Lecture 27 11

Terms, Types, and Kinds, Oh My Types: τ ::= int τ τ α α::κ. τ λα::κ. τ τ τ atomic types (e.g., int) classify the terms that evaluate to atomic values compound types (e.g., τ τ ) classify the terms that evaluate to compound values function types τ τ classify the terms that evaluate to value abstractions universal types α. τ classify the terms that evaluate to type abstractions type abstraction and application type abstractions do not classify terms, but can be applied to type arguments to form types that do classify terms classified by kinds (but not all types have a kind) Kinds κ ::= κ κ kind of proper types classify the types (that are the same as the types) that classify terms arrow kinds κ κ classify the types (that are the same as the types) that are type abstractions Matthew Fluet CS-XXX 2012, Lecture 27 11

Kind Examples Matthew Fluet CS-XXX 2012, Lecture 27 12

Kind Examples the kind of proper types Bool, Bool Bool,... Matthew Fluet CS-XXX 2012, Lecture 27 12

Kind Examples the kind of proper types Bool, Bool Bool,... the kind of (unary) type operators List, Maybe,... Matthew Fluet CS-XXX 2012, Lecture 27 12

Kind Examples the kind of proper types Bool, Bool Bool, Maybe Bool, Maybe Bool Maybe Bool,... the kind of (unary) type operators List, Maybe,... Matthew Fluet CS-XXX 2012, Lecture 27 12

Kind Examples the kind of proper types Bool, Bool Bool, Maybe Bool, Maybe Bool Maybe Bool,... the kind of (unary) type operators List, Maybe,... the kind of (binary) type operators Either, Map,... Matthew Fluet CS-XXX 2012, Lecture 27 12

Kind Examples the kind of proper types Bool, Bool Bool, Maybe Bool, Maybe Bool Maybe Bool,... the kind of (unary) type operators List, Maybe, Map Int, Either (List Bool),... the kind of (binary) type operators Either, Map,... Matthew Fluet CS-XXX 2012, Lecture 27 12

Kind Examples the kind of proper types Bool, Bool Bool, Maybe Bool, Maybe Bool Maybe Bool,... the kind of (unary) type operators List, Maybe, Map Int, Either (List Bool),... the kind of (binary) type operators Either, Map,... ( ) the kind of higher-order type operators taking unary type operators to proper types???,... Matthew Fluet CS-XXX 2012, Lecture 27 12

Kind Examples the kind of proper types Bool, Bool Bool, Maybe Bool, Maybe Bool Maybe Bool,... the kind of (unary) type operators List, Maybe, Map Int, Either (List Bool),... the kind of (binary) type operators Either, Map,... ( ) the kind of higher-order type operators taking unary type operators to proper types???,... ( ) the kind of higher-order type operators taking unary type operators to unary type operators MaybeT, ListT,... Matthew Fluet CS-XXX 2012, Lecture 27 12

Kind Examples the kind of proper types Bool, Bool Bool, Maybe Bool, Maybe Bool Maybe Bool,... the kind of (unary) type operators List, Maybe, Map Int, Either (List Bool), ListT Maybe,... the kind of (binary) type operators Either, Map,... ( ) the kind of higher-order type operators taking unary type operators to proper types???,... ( ) the kind of higher-order type operators taking unary type operators to unary type operators MaybeT, ListT,... Matthew Fluet CS-XXX 2012, Lecture 27 12

System F ω : Syntax e ::= c x λx:τ. e e e Λα::κ. e e [τ ] v ::= c λx:τ. e Λα::κ. e Γ ::= Γ, x:τ τ ::= int τ τ α α::κ. τ λα::κ. τ τ τ ::=, α::κ κ ::= κ κ New things: Types: type abstraction and type application Kinds: the types of types : kind of proper types κa κ r : kind of type operators Matthew Fluet CS-XXX 2012, Lecture 27 13

System F ω : Operational Semantics Small-step, call-by-value (CBV), left-to-right operational semantics: e cbv e (λx: τ. e b ) v a cbv e b [v a /x] e f cbv e f e f e a cbv e f e a e a cbv e a v f e a cbv v f e a (Λα::κ a. e b ) [τ a ] cbv e b [τ a /α] e f cbv e f e f [τ a ] cbv e f [τ a] Unchanged! All of the new action is at the type-level. Matthew Fluet CS-XXX 2012, Lecture 27 14

System F ω : Type System, part 1 In the context the type τ has kind κ: τ :: κ int :: (α) = κ α :: κ τ a :: τ r :: τ a τ r ::, α :: κ a τ r :: α::κ a. τ r ::, α :: κ a τ b :: κ r λα::κ a. τ b :: κ a κ r τ f :: κ a κ r τ a :: κ a τ f τ a :: κ r Should look familiar: Matthew Fluet CS-XXX 2012, Lecture 27 15

System F ω : Type System, part 1 In the context the type τ has kind κ: τ :: κ int :: (α) = κ α :: κ τ a :: τ r :: τ a τ r ::, α :: κ a τ r :: α::κ a. τ r ::, α :: κ a τ b :: κ r λα::κ a. τ b :: κ a κ r τ f :: κ a κ r τ a :: κ a τ f τ a :: κ r Should look familiar: the typing rules of the Simply-Typed Lambda Calculus one level up Matthew Fluet CS-XXX 2012, Lecture 27 15

System F ω : Type System, part 2 Definitional Equivalence of τ and τ : τ τ τ τ τ 2 τ 1 τ 1 τ 2 τ 1 τ 2 τ 2 τ 3 τ 1 τ 3 τ a1 τ a2 τ r1 τ r2 τ r1 τ r2 τ a1 τ r1 τ a2 τ r2 α::κ a. τ r1 α::κ a. τ r2 τ b1 τ b2 λα::κ a. τ b1 λα::κ a. τ b2 τ f1 τ f2 τ a1 τ a2 τ f1 τ a1 τ f2 τ a2 (λα::κ a. τ b ) τ a τ b [α/τ a ] Should look familiar: Matthew Fluet CS-XXX 2012, Lecture 27 16

System F ω : Type System, part 2 Definitional Equivalence of τ and τ : τ τ τ τ τ 2 τ 1 τ 1 τ 2 τ 1 τ 2 τ 2 τ 3 τ 1 τ 3 τ a1 τ a2 τ r1 τ r2 τ r1 τ r2 τ a1 τ r1 τ a2 τ r2 α::κ a. τ r1 α::κ a. τ r2 τ b1 τ b2 λα::κ a. τ b1 λα::κ a. τ b2 τ f1 τ f2 τ a1 τ a2 τ f1 τ a1 τ f2 τ a2 (λα::κ a. τ b ) τ a τ b [α/τ a ] Should look familiar: the full reduction rules of the Lambda Calculus one level up Matthew Fluet CS-XXX 2012, Lecture 27 16

System F ω : Type System, part 3 In the contexts and Γ the expression e has type τ : ; Γ e : τ ; Γ c : int Γ(x) = τ ; Γ x : τ τ a :: ; Γ, x : τ a e b : τ r ; Γ e f : τ a τ r ; Γ e a : τ a ; Γ λx:τ a. e b : τ a τ r ; Γ e f e a : τ r, α :: κ a; Γ e b : τ r ; Γ e f : α::κ a. τ r τ a :: κ a ; Γ Λα. e b : α::κ a. τ r ; Γ e f [τ a] : τ r[τ a/α] ; Γ e : τ τ τ τ :: ; Γ e : τ Matthew Fluet CS-XXX 2012, Lecture 27 17

System F ω : Type System, part 3 In the contexts and Γ the expression e has type τ : ; Γ e : τ ; Γ c : int Γ(x) = τ ; Γ x : τ τ a :: ; Γ, x : τ a e b : τ r ; Γ e f : τ a τ r ; Γ e a : τ a ; Γ λx:τ a. e b : τ a τ r ; Γ e f e a : τ r, α :: κ a; Γ e b : τ r ; Γ e f : α::κ a. τ r τ a :: κ a ; Γ Λα. e b : α::κ a. τ r ; Γ e f [τ a] : τ r[τ a/α] ; Γ e : τ τ τ τ :: ; Γ e : τ Syntax and type system easily extended with recursive and existential types. Matthew Fluet CS-XXX 2012, Lecture 27 17

Polymorphic List Library with higher-order List library is an existential package: pack(λα::. µξ::. unit + (α ξ), list library) as L::. {empty : α::. L α; cons : α::. α L α L α; unlist : α::. L α unit + (α L α); map : α::. β::. (α β) L α L β;...} The witness type operator is poly.lists: λα::. µξ::. unit + (α ξ). The existential type operator variable L represents poly. lists. List operations are polymorphic in element type. The map function only allows mapping α lists to β lists. Matthew Fluet CS-XXX 2012, Lecture 27 18

Other Kinds of Kinds Kinding systems for checking and tracking properties of type expressions: Record kinds records at the type-level; define systems of mutually recursive types Polymorphic kinds kind abstraction and application in types; System F one level up Dependent kinds dependent types one level up Row kinds describe pieces of record types for record polymorphism Power kinds alternative presentation of subtyping Singleton kinds formalize module systems with type sharing Matthew Fluet CS-XXX 2012, Lecture 27 19

Metatheory System F ω is type safe. Matthew Fluet CS-XXX 2012, Lecture 27 20

Metatheory System F ω is type safe. Preservation: Induction on typing derivation, using substitution lemmas: Term Substitution: if 1, 2 ; Γ 1, x : τ x, Γ 2 e 1 : τ and 1 ; Γ 1 e 2 : τ x, then 1, 2 ; Γ 1, Γ 2 e 1 [e 2 /x] : τ. Type Substitution: if 1, α::κ α, 2 τ 1 :: κ and 1 τ 2 :: κ α, then 1, 2 τ 1 [τ 2 /α] :: κ. Type Substitution: if τ 1 τ 2, then τ 1 [τ /α] τ 2 [τ /α]. Type Substitution: if 1, α::κ α, 2 ; Γ 1, Γ 2 e 1 : τ and 1 τ 2 :: κ α, then 1, 2 ; Γ 1, Γ 2 [τ 2 /α] e 1 [τ 2 /α] : τ. All straightforward inductions, using various weakening and exchange lemmas. Matthew Fluet CS-XXX 2012, Lecture 27 20

Metatheory System F ω is type safe. Progress: Induction on typing derivation, using canonical form lemmas: If ; v : int, then v = c. If ; v : τa τ r, then v = λx:τ a. e b. If ; v : α::κa. τ r, then v = Λα::κ a. e b. Complicated by typing derivations that end with: ; Γ e : τ τ τ τ :: ; Γ e : τ (just like with subtyping and subsumption). Matthew Fluet CS-XXX 2012, Lecture 27 20

Definitional Equivalence and Parallel Reduction Parallel Reduction of τ to τ : τ τ τ τ τ a1 τ a2 τ r1 τ r2 τ r1 τ r2 τ a1 τ r1 τ a2 τ r2 α::κ a. τ r1 α::κ a. τ r2 τ b1 τ b2 λα::κ a. τ b1 λα::κ a. τ b2 τ f1 τ f2 τ a1 τ a2 τ f1 τ a1 τ f2 τ a2 τ b τ b τ a τ a (λα::κ a. τ b) τ a τ b[α/τ a] A more computational relation. Matthew Fluet CS-XXX 2012, Lecture 27 21

Definitional Equivalence and Parallel Reduction Key properties: Matthew Fluet CS-XXX 2012, Lecture 27 22

Definitional Equivalence and Parallel Reduction Key properties: Transitive and symmetric closure of parallel reduction and type equivalence coincide: τ τ iff τ τ Matthew Fluet CS-XXX 2012, Lecture 27 22

Definitional Equivalence and Parallel Reduction Key properties: Transitive and symmetric closure of parallel reduction and type equivalence coincide: τ τ iff τ τ Parallel reduction has the Church-Rosser property: If τ τ 1 and τ τ 2, then there exists τ such that τ 1 τ and τ 2 τ Matthew Fluet CS-XXX 2012, Lecture 27 22

Definitional Equivalence and Parallel Reduction Key properties: Transitive and symmetric closure of parallel reduction and type equivalence coincide: τ τ iff τ τ Parallel reduction has the Church-Rosser property: If τ τ 1 and τ τ 2, then there exists τ such that τ 1 τ and τ 2 τ Equivalent types share a common reduct: If τ 1 τ 2, then there exists τ such that τ 1 τ and τ 2 τ Matthew Fluet CS-XXX 2012, Lecture 27 22

Definitional Equivalence and Parallel Reduction Key properties: Transitive and symmetric closure of parallel reduction and type equivalence coincide: τ τ iff τ τ Parallel reduction has the Church-Rosser property: If τ τ 1 and τ τ 2, then there exists τ such that τ 1 τ and τ 2 τ Equivalent types share a common reduct: If τ 1 τ 2, then there exists τ such that τ 1 τ and τ 2 τ Reduction preserves shapes: If int τ, then τ = int If τa τ r τ, then τ = τ a τ r and τ a τ a and τ r τ r If α::κ a. τ r τ, then τ = α::κ a. τ r and τ r τ r Matthew Fluet CS-XXX 2012, Lecture 27 22

Canonical Forms If ; v : τ a τ r, then v = λx:τ a. e b. Proof: By cases on the form of v: Matthew Fluet CS-XXX 2012, Lecture 27 23

Canonical Forms If ; v : τ a τ r, then v = λx:τ a. e b. Proof: By cases on the form of v: v = λx:τ a. e b. We have that v = λx:τ a. e b. Matthew Fluet CS-XXX 2012, Lecture 27 23

Canonical Forms If ; v : τ a τ r, then v = λx:τ a. e b. Proof: By cases on the form of v: v = c. Derivation of ; v : τ a τ r must be of the form:. ; c : int int τ 1 ; c : τ 1. ; c : τ n 1 τ n 1 τ n ; c : τ n τ n τ a τ r ; c : τ a τ r Therefore, we can construct the derivation int τ a τ r. We can find a common reduct: int τ and τ a τ r τ. Reduction preserves shape: int τ implies τ = int. Reduction preserves shape: τ a τ r τ implies τ = τ a τ r. But, τ = int and τ = τ a τ r is a contradiction. Matthew Fluet CS-XXX 2012, Lecture 27 23

Canonical Forms If ; v : τ a τ r, then v = λx:τ a. e b. Proof: By cases on the form of v: v = Λα::κ a. e b. Derivation of ; v : τ a τ r must be of the form:. ; Λα::κ a. e b : α::κ a. τ z α::κ a. τ z τ 1 ; Λα::κ a. e b : τ 1. ; Λα::κ a. e b : τ n 1 τ n 1 τ n ; Λα::κ a. e b : τ n τ n τ a τ r ; Λα::κ a. e b : τ a τ r Therefore, we can construct the derivation α::κ a. τ z τ a τ r. We can find a common reduct: α::κ a. τ z τ and τ a τ r τ. Reduction preserves shape: α::κ a. τ z τ implies τ = α::κ a. τ z. Reduction preserves shape: τ a τ r τ implies τ = τ a τ r. But, τ = α::κ a. τ z and τ = τ a τ r is a contradiction. Matthew Fluet CS-XXX 2012, Lecture 27 23

Metatheory System F ω is type safe. Where was the τ :: κ judgement used in the proof? Matthew Fluet CS-XXX 2012, Lecture 27 24

Metatheory System F ω is type safe. Where was the τ :: κ judgement used in the proof? In Type Substitution lemmas, but only in an inessential way. Matthew Fluet CS-XXX 2012, Lecture 27 24

Metatheory System F ω is type safe. Where was the τ :: κ judgement used in the proof? In Type Substitution lemmas, but only in an inessential way. After weeks of thinking about type systems, kinding seems natural; but kinding is not required for type safety! Matthew Fluet CS-XXX 2012, Lecture 27 24

System F ω without Kinds / System F with Type-Level Abstraction and Application e ::= c x λx:τ. e e e Λα. e e [τ ] v ::= c λx:τ. e Λα. e τ ::= int τ τ α α. τ λα. τ τ τ Γ ::= Γ, x:τ ::=, α Matthew Fluet CS-XXX 2012, Lecture 27 25

System F ω without Kinds / System F with Type-Level Abstraction and Application e ::= c x λx:τ. e e e Λα. e e [τ ] v ::= c λx:τ. e Λα. e τ ::= int τ τ α α. τ λα. τ τ τ Γ ::= Γ, x:τ ::=, α e cbv e (λx: τ. e b) v a cbv e b[v a/x] e f cbv e f e a cbv e a e f e a cbv e f e a v f e a cbv v f e a (Λα. e b) [τ a] cbv e b[τ a/α] e f cbv e f e f [τ a] cbv e f [τ a] Matthew Fluet CS-XXX 2012, Lecture 27 25

System F ω without Kinds / System F with Type-Level Abstraction and Application e ::= c x λx:τ. e e e Λα. e e [τ ] v ::= c λx:τ. e Λα. e τ ::= int τ τ α α. τ λα. τ τ τ Γ ::= Γ, x:τ ::=, α τ :: int :: α α ::, α τ b :: λα. τ b :: τ a :: τ r :: τ a τ r ::, α τ r :: α. τ r :: τ f :: τ a :: τ f τ a :: Check that free type variables of τ are in, but nothing else. Matthew Fluet CS-XXX 2012, Lecture 27 25

System F ω without Kinds / System F with Type-Level Abstraction and Application e ::= c x λx:τ. e e e Λα. e e [τ ] v ::= c λx:τ. e Λα. e τ ::= int τ τ α α. τ λα. τ τ τ Γ ::= Γ, x:τ ::=, α τ τ τ τ τ 2 τ 1 τ 1 τ 2 τ 1 τ 2 τ 2 τ 3 τ 1 τ 3 τ a1 τ a2 τ r1 τ r2 τ r1 τ r2 τ a1 τ r1 τ a2 τ r2 α. τ r1 α. τ r2 τ b1 τ b2 λα. τ b1 λα. τ b2 τ f1 τ f2 τ a1 τ a2 τ f1 τ a1 τ f2 τ a2 (λα. τ b) τ a τ b[α/τ a] Matthew Fluet CS-XXX 2012, Lecture 27 25

System F ω without Kinds / System F with Type-Level Abstraction and Application e ::= c x λx:τ. e e e Λα. e e [τ ] v ::= c λx:τ. e Λα. e τ ::= int τ τ α α. τ λα. τ τ τ Γ ::= Γ, x:τ ::=, α ; Γ e : τ ; Γ c : int Γ(x) = τ ; Γ x : τ τ a :: ; Γ, x : τ a e b : τ r ; Γ e f : τ a τ r ; Γ e a : τ a ; Γ λx:τ a. e b : τ a τ r ; Γ e f e a : τ r, α; Γ e b : τ r ; Γ e f : α. τ r τ a :: ; Γ Λα. e b : α. τ r ; Γ e f [τ a] : τ r[τ a/α] ; Γ e : τ τ τ ; Γ e : τ Matthew Fluet CS-XXX 2012, Lecture 27 25

System F ω without Kinds / System F with Type-Level Abstraction and Application This language is type safe. Matthew Fluet CS-XXX 2012, Lecture 27 26

System F ω without Kinds / System F with Type-Level Abstraction and Application This language is type safe. Preservation: Induction on typing derivation, using substitution lemmas: Term Substitution: if 1, 2 ; Γ 1, x : τ x, Γ 2 e 1 : τ and 1 ; Γ 1 e 2 : τ x, then 1, 2 ; Γ 1, Γ 2 e 1 [e 2 /x] : τ. Type Substitution: if 1, α, 2 τ 1 :: and 1 τ 2 ::, then 1, 2 τ 1 [τ 2 /α] ::. Type Substitution: if τ 1 τ 2, then τ 1 [τ /α] τ 2 [τ /α]. Type Substitution: if 1, α, 2 ; Γ 1, Γ 2 e 1 : τ and 1 τ 2 ::, then 1, 2 ; Γ 1, Γ 2 [τ 2 /α] e 1 [τ 2 /α] : τ. All straightforward inductions, using various weakening and exchange lemmas. Matthew Fluet CS-XXX 2012, Lecture 27 26

System F ω without Kinds / System F with Type-Level Abstraction and Application This language is type safe. Progress: Induction on typing derivation, using canonical form lemmas: If ; v : int, then v = c. If ; v : τa τ r, then v = λx:τ a. e b. If ; v : α. τr, then v = Λα. e b. Using parallel reduction relation. Matthew Fluet CS-XXX 2012, Lecture 27 26

Why Kinds? Why aren t kinds required for type safety? Matthew Fluet CS-XXX 2012, Lecture 27 27

Why Kinds? Why aren t kinds required for type safety? Recall statement of type safety: If ; e : τ, then e does not get stuck. Matthew Fluet CS-XXX 2012, Lecture 27 27

Why Kinds? Why aren t kinds required for type safety? Recall statement of type safety: If ; e : τ, then e does not get stuck. The typing derivation ; e : τ includes definitional-equivalence sub-derivations τ τ, which are explicit evidence that τ and τ are the same. E.g., to show that the natural type of the function expression in an application is equivalent to an arrow type:. ; Γ e f : τ f. τ f τ a τ r ; Γ e f : τ a τ r ; Γ e f e a : τ r. ; Γ e a : τ a Matthew Fluet CS-XXX 2012, Lecture 27 27

Why Kinds? Why aren t kinds required for type safety? Recall statement of type safety: If ; e : τ, then e does not get stuck. The typing derivation ; e : τ includes definitional-equivalence sub-derivations τ τ, which are explicit evidence that τ and τ are the same. Definitional equivalence (τ τ ) and parallel reduction (τ τ ) do not require well-kinded types (although they preserve the kinds of well-kinded types). E.g., (λα. α α) (int int) (int int) (int int) Matthew Fluet CS-XXX 2012, Lecture 27 27

Why Kinds? Why aren t kinds required for type safety? Recall statement of type safety: If ; e : τ, then e does not get stuck. The typing derivation ; e : τ includes definitional-equivalence sub-derivations τ τ, which are explicit evidence that τ and τ are the same. Definitional equivalence (τ τ ) and parallel reduction (τ τ ) do not require well-kinded types (although they preserve the kinds of well-kinded types). Type (and kind) erasure means that wrong/bad/meaningless types do not affect run-time behavior. Ill-kinded types can t make well-typed terms get stuck. Matthew Fluet CS-XXX 2012, Lecture 27 27

Why Kinds? Kinds aren t for type safety: Because a typing derivation (even with ill-kinded types), carries enough evidence to guarantee that expressions don t get stuck. Matthew Fluet CS-XXX 2012, Lecture 27 28

Why Kinds? Kinds aren t for type safety: Because a typing derivation (even with ill-kinded types), carries enough evidence to guarantee that expressions don t get stuck. Kinds are for type checking: Because programmers write programs, not typing derivations. Because type checkers are algorithms. Matthew Fluet CS-XXX 2012, Lecture 27 28

Why Kinds? Kinds are for type checking: Because programmers write programs, not typing derivations. Because type checkers are algorithms. Matthew Fluet CS-XXX 2012, Lecture 27 28

Why Kinds? Kinds are for type checking: Because programmers write programs, not typing derivations. Because type checkers are algorithms. Recall the statement of type checking: Given, Γ, and e, does there exist τ such that ; Γ e : τ. Matthew Fluet CS-XXX 2012, Lecture 27 28

Why Kinds? Kinds are for type checking: Because programmers write programs, not typing derivations. Because type checkers are algorithms. Recall the statement of type checking: Given, Γ, and e, does there exist τ such that ; Γ e : τ. Two issues: ; Γ e : τ τ τ τ :: ; Γ e : τ is a non-syntax-directed rule τ τ is a non-syntax-directed relation One non-issue: τ :: κ is a syntax-directed relation (STLC one level up ) Matthew Fluet CS-XXX 2012, Lecture 27 28

Type Checking for System F ω Remove non-syntax-directed rules and relations: ; Γ e : τ ; Γ c : int Γ(x) = τ ; Γ x : τ τ a :: ; Γ, x : τ a e b : τ r, α::κ a; Γ e b : τ r ; Γ λx:τ a. e b : τ a τ r ; Γ Λα. e b : α::κ a. τ r ; Γ e f : τ f τ f τ f τ f = τ fa τ fr ; Γ e a : τ a τ a τ a τ fa = τ a ; Γ e f e a : τ fr ; Γ e f : τ f τ f τ f τ f = α::κ fa. τ fr τ a :: κ a κ fa = κ a ; Γ e f [τ a] : τ fr[τ a/α] Matthew Fluet CS-XXX 2012, Lecture 27 29

Type Checking for System F ω Kinds are for type checking. Given, Γ, and e, does there exist τ such that ; Γ e : τ. Metatheory for kind system: Matthew Fluet CS-XXX 2012, Lecture 27 30

Type Checking for System F ω Kinds are for type checking. Given, Γ, and e, does there exist τ such that ; Γ e : τ. Metatheory for kind system: Well-kinded types don t get stuck. If τ :: κ and τ τ, then either τ is in (weak-head) normal form (i.e., a type-level value ) or τ τ. Proofs by Progress and Preservation on kinding and parallel reduction derivations. Matthew Fluet CS-XXX 2012, Lecture 27 30

Type Checking for System F ω Kinds are for type checking. Given, Γ, and e, does there exist τ such that ; Γ e : τ. Metatheory for kind system: Well-kinded types don t get stuck. If τ :: κ and τ τ, then either τ is in (weak-head) normal form (i.e., a type-level value ) or τ τ. Proofs by Progress and Preservation on kinding and parallel reduction derivations. But, irrelevant for type checking of expressions. If τ f τ f gets stuck at a type τ f that is not an arrow type, then the application typing rule does not apply and a typing derivation does not exist. Matthew Fluet CS-XXX 2012, Lecture 27 30

Type Checking for System F ω Kinds are for type checking. Given, Γ, and e, does there exist τ such that ; Γ e : τ. Metatheory for kind system: Well-kinded types don t get stuck. If τ :: κ and τ τ, then either τ is in (weak-head) normal form (i.e., a type-level value ) or τ τ. But, irrelevant for type checking of expressions. Matthew Fluet CS-XXX 2012, Lecture 27 30

Type Checking for System F ω Kinds are for type checking. Given, Γ, and e, does there exist τ such that ; Γ e : τ. Metatheory for kind system: Well-kinded types don t get stuck. If τ :: κ and τ τ, then either τ is in (weak-head) normal form (i.e., a type-level value ) or τ τ. But, irrelevant for type checking of expressions. Well-kinded types terminate. If τ :: κ, then there exists τ such that τ τ. Proof is similar to that of termination of STLC. Matthew Fluet CS-XXX 2012, Lecture 27 30

Type Checking for System F ω Kinds are for type checking. Given, Γ, and e, does there exist τ such that ; Γ e : τ. Metatheory for kind system: Well-kinded types don t get stuck. If τ :: κ and τ τ, then either τ is in (weak-head) normal form (i.e., a type-level value ) or τ τ. But, irrelevant for type checking of expressions. Well-kinded types terminate. If τ :: κ, then there exists τ such that τ τ. Proof is similar to that of termination of STLC. Type checking for System F ω is decidable. Matthew Fluet CS-XXX 2012, Lecture 27 30

Going Further This is just the tip of an iceberg. Pure type systems Why stop at three levels of expressions (terms, types, and kinds)? Allow abstraction and application at the level of kinds, and introduce sorts to classify kinds. Why stop at four levels of expressions?... For programming languages, however, three levels have proved sufficient. Matthew Fluet CS-XXX 2012, Lecture 27 31