The Polymorphic Blame Calculus and Parametricity

Similar documents
Consistent Subtyping for All

Harvard School of Engineering and Applied Sciences Computer Science 152

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

Blame for All. Robert Bruce Findler. Amal Ahmed. Jeremy G. Siek. Philip Wadler. Abstract. 1. Introduction

A Blame for All (revised)

On Polymorphic Gradual Typing

Type Inference; Parametric Polymorphism; Records and Subtyping Section and Practice Problems Mar 20-23, 2018

CSE-321 Programming Languages 2011 Final

Gradual Typing with Inference

The gradual typing approach to mixing static and dynamic typing

Gradual Parametricity, Revisited

λ calculus is inconsistent

Gradual Typing with Union and Intersection Types

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

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

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

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

CSE-321 Programming Languages 2010 Final

Exercise 1 ( = 24 points)

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.

Programming Languages Lecture 15: Recursive Types & Subtyping

1 Introduction. 3 Syntax

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

Exploring the Design Space of Higher-Order Casts ; CU-CS

Functional Languages and Higher-Order Functions

Programming Languages

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

Concepts of programming languages

Polymorphism. Lecture 19 CS 565 4/17/08

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

Exercise 1 ( = 18 points)

Typed Lambda Calculus and Exception Handling

Mutable References. Chapter 1

Lambda Calculus and Type Inference

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

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

Lecture 13: Subtyping

Chapter 5: The Untyped Lambda Calculus

CSE-321 Programming Languages 2012 Midterm

The Typed λ Calculus and Type Inferencing in ML

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

Let Arguments Go First

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

Costly software bugs that could have been averted with type checking

Lecture 9: Typed Lambda Calculus

Exercise 1 ( = 22 points)

Second-Order Type Systems

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

CSE-321 Programming Languages 2014 Final

Lambda Calculi With Polymorphism

CIS 500 Software Foundations Fall October 2

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

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

One of a number of approaches to a mathematical challenge at the time (1930): Constructibility

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

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

CMSC 330: Organization of Programming Languages

Simply-Typed Lambda Calculus

Lambda Calculi With Polymorphism

Exercise 1 (2+2+2 points)

The Untyped Lambda Calculus

Let Arguments Go First

Programming Languages Lecture 14: Sum, Product, Recursive Types

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...

CSE-321 Programming Languages 2010 Midterm

Polymorphism and Type Inference

Overview. A normal-order language. Strictness. Recursion. Infinite data structures. Direct denotational semantics. Transition semantics

Programming Language Concepts: Lecture 19

Lambda Calculus. Concepts in Programming Languages Recitation 6:

Refined Criteria for Gradual Typing

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

Verifying Program Invariants with Refinement Types

Part III. Chapter 15: Subtyping

Calculus INT. Value Types A, B ::= types of the low-level language Interactive Types X, Y ::= [A] A X X Y A X

Part III Chapter 15: Subtyping

Space-Efficient Blame Tracking for Gradual Types

Well-typed programs can t be blamed

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

More Lambda Calculus and Intro to Type Systems

Type Checking and Type Inference

Typing & Static Analysis of Multi-Staged Programs

Programming Languages Fall 2014

CMSC 330: Organization of Programming Languages

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

Eta-equivalence in Core Dependent Haskell

Calculus of Inductive Constructions

The Untyped Lambda Calculus

Type checking in the presence of meta-variables

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

Introduction to Lambda Calculus. Lecture 7 CS /08/09

Lesson 4 Typed Arithmetic Typed Lambda Calculus

Types and Type Inference

Pure (Untyped) λ-calculus. Andrey Kruglyak, 2010

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

CITS3211 FUNCTIONAL PROGRAMMING

Types and Type Inference

Introductory Example

Type checking. Jianguo Lu. November 27, slides adapted from Sean Treichler and Alex Aiken s. Jianguo Lu November 27, / 39

Formal Systems and their Applications

Programming Languages. Programming with λ-calculus. Lecture 11: Type Systems. Special Hour to discuss HW? if-then-else int

Transcription:

1 / 31 The Polymorphic Blame Calculus and Parametricity Jeremy G. Siek Indiana University, Bloomington University of Strathclyde August 2015

2 / 31 Integrating static and dynamic typing Static Dynamic

3 / 31 Outline Quick review of gradual typing New: a polymorphic gradually typed lambda calculus Review: Poly. Blame Calculus and Parametricity

4 / 31 Gradual typing includes dynamic typing An untyped program: let f = λy. 1 + y h = λg. g 3 in h f 4

5 / 31 Gradual typing includes dynamic typing A buggy untyped program: 1 let 2 f = λy. 1 + y 3 h = λg. g true 4 in 5 h f blame l 2 Just like dynamic typing, the error is caught at run time.

6 / 31 Gradual typing includes static typing A typed program: let f = λy:int. 1 + y h = λg:int int. g 3 in h f 4

7 / 31 Gradual typing includes static typing An ill-typed program: 1 let 2 f = λy:int. 1 + y 3 h = λg:int int. g true 4 in 5 h f Just like static typing, the error is caught at compile time. Error on line 3, the argument true is a Boolean, but function g expects an int.

8 / 31 Gradual typing provides fine-grained mixing A partially typed program: let f = λy:int. 1 + y h = λg. g 3 in h f 4

9 / 31 Gradual typing protects type invariants A buggy, partially typed program: 1 let 2 f = λy:int. 1 + y 3 h = λg. g true 4 in 5 h f blame l 3

10 / 31 Gradually Typed Lambda Calculus Extends the STLC with a dynamic type, written. Types A, B, C ::= ι A B Terms L, M, N ::= c x λx:a. N L M Consistency A B A B int int A 1 B 1 A 2 B 2 A 1 A 2 B 1 B 2 Term Typing Γ M : A Γ L : A B C A Γ M : C Γ L M : B Γ L : Γ M : C Γ L M :

11 / 31 Outline Quick review of gradual typing New: a polymorphic gradually typed lambda calculus Review: Poly. Blame Calculus and Parametricity

12 / 31 Gradual typing and polymorphism Use polymorphic code in an untyped context: let pos = λx. x > 0 app = ΛX. ΛY. λf :X Y. λx:x. f x in app pos 1 Use untyped code in a polymorphic context: let pos : int bool = λx:int. x > 0 app = λf. λx. f x in app int bool pos 1

13 / 31 Gradually Typed Polymorphic Lambda Calculus Types A, B, C ::= ι A B X X. A Terms L, M, N ::= c x λx:a. N L M ΛX. N L A Consistency A B Term typing X Γ Γ X X Γ, X A B Γ A X. B Γ, X A B Γ X. A X. B Γ, X A B Γ X. A B Γ L : X. B Γ L A : B[X A] Γ L : Γ L A :

14 / 31 Consistency examples X. X X Y. Y Y X. X X X. X X X. X X int int X. X X int bool X. X X X. X X int int X. X X int bool X. X X

What about converting poly. to simple? One might also want implicit conversion from polymorphic types to simple types, such as X. X X int int That is a separate concern from gradual typing. We could handle it with a subtyping rule A[X C] <: B X. A <: B Then, for the type checking algorithm, combine subtyping and consistency as in Siek and Taha [2007]. 1 2 1 Polymorphic type inference and containment, John C. Mitchell, Information and Computation 1988. 2 Gradual Type for Objects, Siek and Taha, ECOOP 2007. 15 / 31

16 / 31 Translation semantics (cast insertion) The semantics is defined by translation to the Polymorphic Blame Calculus. Cast Insertion Γ L L : Γ L A (L : p X. ) A : Γ M M : A

17 / 31 Outline Quick review of gradual typing New: a polymorphic gradually typed lambda calculus Review: Poly. Blame Calculus and Parametricity

Semantics of casting from poly. to untyped Recall the example: So we have the cast: let pos = λx. x > 0 app = ΛX. ΛY. λf :X Y. λx:x. f x in app pos 1 app : X. Y. (X Y ) X Y p The Polymorphic Blame Calculus handles such casts by instantiating with. 3 V : ( X. A) p B (V ) : A[X ] 3 Blame for All. Ahmed et al. POPL 2011 p B 18 / 31

19 / 31 Semantics of casting from untyped to poly. Recall the example: So we have the cast: let pos : int bool = λx:int. x > 0 app = λf. λx. f x in app int bool pos 1 app : p X. Y. (X Y ) X Y The Polymorphic Blame Calculus handles such casts by generalizing. V : A p ( X. B) ΛX. (V : A p B) if X / ftv(a)

20 / 31 Semantics of casts and parametricity Consider casting the constant function K = λx:. λy:. x to the following polymorphic types K 1 K : p X. Y. X Y X K 2 K : q X. Y. X Y Y and the following scenarios: (K 1 int bool) 1 false 1 (K 1 int int) 1 2 1 (K 2 int bool) 1 false (K 2 int int) 1 2

Instantiation as type substition Recall the traditional reduction rule: (ΛX. N) A N[X A] K 2 K : q X. Y. X Y Y (K 2 int bool) 1 false (K : p int bool bool) 1 false 1 : int p bool blame p so far so good... 21 / 31

The problem with type substitution K 2 K : q X. Y. X Y Y The second scenario for K 2 : (K 2 int int) 1 2 (K : p int int int) 1 2 1 : int p int 1 but a polymorphic function of type X. Y. X Y Y must return its second argument, not first! 22 / 31

Solution: don t substitute, seal The example revisited: (ΛX. V ) A νx A. V K 2 K : q X. Y. X Y Y (K 2 int int) 1 2 (νx int. νy int. K : p X Y Y ) 1 2 νx int. νy int. 1 : X p Y blame p 4 4 Types are not sets, James H. Morris, Jr., POPL 1973. 23 / 31

What to do with escaping seals? (ΛX. λx:x. x : X p ) int 2 νx int. 2 : X p blame p ν Contrast with (ΛX. λx:x. inl x as (X + bool)) int 2 inl 2 as (int + bool) Why not? νx A. (V : X p ) (νx A. V ) : A p 24 / 31

25 / 31 Properties of the Polymorphic Blame Calculus Type Safety Blame Theorem Subtyping Theorem (weak version) Subtyping Theorem (strong version) Parametricity

Blame Theorem Theorem (Blame Theorem) Let M be a program with a subterm N : A p B where the cast is labelled by the only occurrence of p in M, and p does not appear in M. If A <: + B, then M blame p. If A <: B, then M blame p. If A <: n B, then M blame p. If B <: n A, then M blame p. 26 / 31

Subtyping Theorem Theorem (Subtyping Theorem) Let M be a program with a subterm N : A p B where the cast is labelled by the only occurrence of p in M, and p does not appear in M. If A <: B, then M blame p and M blame p. Weak version: (Proved in STOP 2009.) Strong version: A[X ] <: B ( X. A) <: B A[X T ] <: B ( X. A) <: B (Incorrect proof in POPL 2011.) 27 / 31

Jack of all trades Conjecture (Jack-of-All-Trades) If V : X. A and A[X C] B (and hence A[X ] B) then (V C : A[X C] p p B) (V : A[X ] B). 28 / 31

Speculating about parametricity Logical Relation Terms E[A]δk = {(M, N) VW. M j V, N j W, (V, W ) V [A]δ(k j)} Values V [int]δk = {(n, n) n Z} V [A 1 + A 2 ]δk = {(inj i V, inj i W ) i 1..2, (V, W ) V [A i ]δk} V [ X. A]δk = {(V 1, V 2 ) R. (V 1 [ ], V 2 [ ]) E[A]δ(X R)k} V [X]δk = δ(x) k V [ ]δ(1 + k) = {(V : G, W : G ) (V, W ) V [G]δk} 29 / 31

Parametricity Conjecture (Soundness of the Logical Relation) If ; Γ M N : A, then ; Γ M = ctx N : A. Conjecture (Fund. Theorem of Logical Relations) If ; Γ M : A, then ; Γ M M : A. 30 / 31

31 / 31