Part III. Chapter 15: Subtyping

Similar documents
Part III Chapter 15: Subtyping

Subsumption. Principle of safe substitution

Subtyping. Lecture 13 CS 565 3/27/06

Lecture 13: Subtyping

Subtyping (cont) Lecture 15 CS 565 4/3/08

Subtyping (cont) Formalization of Subtyping. Lecture 15 CS 565. Inversion of the subtype relation:

A Typed Lambda Calculus for Input Sanitation

Subtyping and Objects

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

Programming Languages Fall 2014

Lecture 9: Typed Lambda Calculus

Programming Languages Lecture 15: Recursive Types & Subtyping

Lambda Calculi With Polymorphism

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

Official Survey. Cunning Plan: Focus On Objects. The Need for a Calculus. Object Calculi Summary. Why Not Use λ-calculus for OO?

Recursive Types and Subtyping

Symmetry in Type Theory

COS 320. Compiling Techniques

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

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

Recursive Types and Subtyping

Lesson 4 Typed Arithmetic Typed Lambda Calculus

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

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

Harvard School of Engineering and Applied Sciences Computer Science 152

Lambda Calculi With Polymorphism

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

CIS 500 Software Foundations Fall December 6

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

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

Featherweight Java (FJ)

Second-Order Type Systems

CIS 500 Software Foundations Midterm I

Programming Languages Lecture 14: Sum, Product, Recursive Types

Formal Systems and their Applications

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

Type Checking and Type Inference

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

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

Assistant for Language Theory. SASyLF: An Educational Proof. Corporation. Microsoft. Key Shin. Workshop on Mechanizing Metatheory

Object Oriented Issues in VDM++

CSE 505 Graduate PL. Fall 2013

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

Extracting the Range of cps from Affine Typing

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

CIS 500 Software Foundations Fall October 2

Programming Languages

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

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

Typed Lambda Calculus and Exception Handling

Polymorphism. Lecture 19 CS 565 4/17/08

Lesson 11 Universal Types. Universal Types and System F

Type-indexed functions in Generic Haskell

CIS 500 Software Foundations Fall December 4

Type Systems Winter Semester 2006

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

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

CSE-321 Programming Languages 2014 Final

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

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.

Costly software bugs that could have been averted with type checking

INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

The Lambda Calculus. 27 September. Fall Software Foundations CIS 500. The lambda-calculus. Announcements

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

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

Dependent Object Types - A foundation for Scala s type system

λ calculus is inconsistent

Type Safety. Java and ML are type safe, or strongly typed, languages. C and C++ are often described as weakly typed languages.

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

Mid-Term 2 Grades

CSE-321 Programming Languages 2010 Final

CIS 500 Software Foundations Fall September 25

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

The Typed λ Calculus and Type Inferencing in ML

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

Generic polymorphism on steroids

Types and Type Inference

Verifying Program Invariants with Refinement Types

What s Conformance? Conformance. Conformance and Class Invariants Question: Conformance and Overriding

Simply-Typed Lambda Calculus

Concepts of programming languages

Rushikesh K Joshi. Department of Computer Science and Engineering Indian Institute of Technology Bombay

Lambda Calculus and Type Inference

Gradual Typing with Union and Intersection Types

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

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

Lambda Calculus and Type Inference

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

Part VI. Imperative Functional Programming

Object-Oriented Languages and Object-Oriented Design. Ghezzi&Jazayeri: OO Languages 1

CSE505, Fall 2012, Midterm Examination October 30, 2012

Lecture Notes on Program Equivalence

Type Systems Winter Semester 2006

Intersections and Unions of Session Types

Inheritance and object compatibility

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

At build time, not application time. Types serve as statically checkable invariants.

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

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

Transcription:

Part III Chapter 15: Subtyping Subsumption Subtype relation Properties of subtyping and typing Subtyping and other features Intersection and union types

Subtyping

Motivation With the usual typing rule for applications the term is not well typed.

Motivation With the usual typing rule for applications the term is not well typed. This is silly: all we re doing is passing the function a better argument than it needs.

Subsumption More generally: some types are better than others, in the sense that a value of one can always safely be used where a value of the other is expected. We can formalize this intuition by introducing:

Subsumption More generally: some types are better than others, in the sense that a value of one can always safely be used where a value of the other is expected. We can formalize this intuition by introducing: 1. a subtyping relation between types, written S <: T

Subsumption More generally: some types are better than others, in the sense that a value of one can always safely be used where a value of the other is expected. We can formalize this intuition by introducing: 1. a subtyping relation between types, written S <: T 2. a rule of subsumption stating that, if S <: T, then any value of type S can also be regarded as having type T

Subsumption More generally: some types are better than others, in the sense that a value of one can always safely be used where a value of the other is expected. We can formalize this intuition by introducing: 1. a subtyping relation between types, written S <: T 2. a rule of subsumption stating that, if S <: T, then any value of type S can also be regarded as having type T Principle of safe substitution

Subtyping Intuitions: S <: T means... An element of S may safely be used wherever an element of T is expected. (Official.)

Subtyping Intuitions: S <: T means... An element of S may safely be used wherever an element of T is expected. (Official.) S is better than T S is a subset of T S is more informative / richer than T.

Example We will define subtyping between record types so that, for example {x: Nat, y: Nat} <: {x: Nat} So, by subsumption, {x = 0, y = 1} {x: Nat}

Example We will define subtyping between record types so that, for example {x: Nat, y: Nat} <: {x: Nat} So, by subsumption, {x = 0, y = 1} {x: Nat} and hence (λr: {x: Nat}. r. x) {x = 0, y = 1} is well typed.

The Subtype Relation: Records Width subtyping (forgetting fields on the right): {l i : T i i 1..n+k } <: l i : T i i 1..n (S-RcdWidth) Intuition: {x: Nat} is the type of all records with at least a numeric x field.

The Subtype Relation: Records Width subtyping (forgetting fields on the right): {l i : T i i 1..n+k } <: l i : T i i 1..n (S-RcdWidth) Intuition: {x: Nat} is the type of all records with at least a numeric x field. Note that the record type with more fields is a subtype of the record type with fewer fields. Reason: the type with more fields places a stronger constraint on values, so it describes fewer values.

The Subtype Relation: Records Permutation of fields:

Order of fields in Records The order of fields in a record does not make any difference to how we can safely use it, since the only thing that we can do with records (projecting their fields) is insensitive to the order of fields.

Order of fields in Records The order of fields in a record does not make any difference to how we can safely use it, since the only thing that we can do with records (projecting their fields) is insensitive to the order of fields. S-RcdPerm tells us that {c:top, b: Bool, a: Nat} <: {a: Nat, b: Bool, c:top} and {a: Nat, b: Bool, c:top} <: {c:top, b: Bool, a: Nat}

The Subtype Relation: Records Permutation of fields: By using S-RcdPerm together with S-RcdWidth and S-Trans allows us to drop arbitrary fields within records.

The Subtype Relation: Records Depth subtyping within fields: The types of individual fields may change, as long as the type of each corresponding field in the two records are in the subtype relation.

Example

Example We can also use S-RcdDepth to refine the type of just a single record field (instead of refining every field), by using S-RefL to obtain trivial subtyping derivations for other fields. S RRRRRRRR S RRRR a: NNN, b: NNN <: a: NNN m: NNN <: m: NNN x: a: NNN, b: NNN, y: m: NNN <: {x: a: NNN, y: m: NNN } S RcdDepth

Variations Real languages often choose not to adopt all of these record subtyping rules. For example, in Java, A subclass may not change the argument or result types of a method of its superclass (i.e., no depth subtyping) Each class has just one superclass ( single inheritance of classes) each class member (field or method) can be assigned a single index, adding new indices on the right as more members are added in subclasses (i.e., no permutation for classes) A class may implement multiple interfaces ( multiple inheritance of interfaces) I.e., permutation is allowed for interfaces.

The Subtype Relation: Arrow types

The Subtype Relation: Arrow types Note the order of T 1 and S 1 in the first premise. The subtype relation is contravariant in the left-hand sides of arrows and covariant in the right-hand sides.

The Subtype Relation: Arrow types Note the order of T 1 and S 1 in the first premise. The subtype relation is contravariant in the left-hand sides of arrows and covariant in the right-hand sides. Intuition: if we have a function f of type S 1 S 2, then we know that f accepts elements of type S 1 ; clearly, f will also accept elements of any subtype T 1 of S 1. The type of f also tells us that it returns elements of type S 2 ; we can also view these results belonging to any supertype T 2 of S 2. That is, any function f of type S 1 S 2 can also be viewed as having type T 1 T 2.

The Subtype Relation: Top It is convenient to have a type that is a supertype of every type. We introduce a new type constant Top, plus a rule that makes Top a maximum element of the subtype relation.

The Subtype Relation: Top It is convenient to have a type that is a supertype of every type. We introduce a new type constant Top, plus a rule that makes Top a maximum element of the subtype relation. Cf. Object in Java.

Subtype Relation: General rules

Subtype Relation

Properties of Subtyping

Safety Statements of progress and preservation theorems are unchanged from λ.

Safety Statements of progress and preservation theorems are unchanged from λ. Proofs become a bit more involved, because the typing relation is no longer syntax directed. Given a derivation, we don t always know what rule was used in the last step. The rule T-SUB could appear anywhere.

Syntax-directed rules When we say a set of rules is syntax-directed we mean two things: 1. There is exactly one rule in the set that applies to each syntactic form. (We can tell by the syntax of a term which rule to use.) In order to derive a type for t 1 t 2, we must use T-App. 2. We don't have to guess" an input (or output) for any rule. To derive a type for t 1 t 2, we need to derive a type for t 1 and a type for t 2.

Preservation Theorem: If Γ t: T aaa t t, tttt Γ t T. Proof: By induction on typing derivations. Which cases are likely to be hard?

Subsumption case Case T-Sub: t S S <: T By the induction hypothesis, Γ t S. By T-Sub, Γ t. Not hard!

Application case Case T-APP : t = t 1 t 2 Γ t 1 : T 11 T 12 Γ t 2 : T 11 T = T 12 By the inversion lemma for evaluation, there are three rules by which t t can be derived: E-APP1, E-APP2, and E-APPABS. Proceed by cases.

Application case Case T-APP : t = t 1 t 2 Γ t 1 : T 11 T 12 Γ t 2 : T 11 T = T 12 By the inversion lemma for evaluation, there are three rules by which t t can be derived: E-APP1, E-APP2, and E-APPABS. Proceed by cases. Subcase E-APP1: t 1 t 1 t = t 1 t 2 The result follows from the induction hypothesis and T- APP.

Application case Case T-APP : t = t 1 t 2 Γ t 1 : T 11 T 12 Γ t 2 : T 11 T = T 12 Subcase E-APP2 : t 1 = v 1 t 2 t 2 t = v 1 t 2 Similar.

Application case Case T-APP: t = t 1 t 2 Γ t 1 : T 11 T 12 Γ t 2 : T 11 T = T 12 Subcase E-APPABS : t 1 = λx: S 11. t 12 t 2 = v 2 t = [x v 2 ] t 12 By the inversion lemma for the typing relation... T 11 <: S 11 and Γ, x: S 11 t 12 : T 12. By T-Sub, Γ t 2 : S 11. By the substitution lemma, Γ t : T 12.

Inversion Lemma for Typing Lemma: If Γ λx: S 1. s 2 : T 1 T 2, then T 1 <: S 1 and Γ, x: S 1 s 2 : T 2.

Inversion Lemma for Typing Lemma: If Γ λx: S 1. s 2 : T 1 T 2, then T 1 <: S 1 and Γ, x: S 1 s 2 : T 2. Proof: Induction on typing derivations. Case T Sub: λx: S 1. s 2 : U U: T 1 T 2 We want to say By the induction hypothesis..., but the IH does not apply (we do not know that U is an arrow type). Need another lemma... Lemma: If U <: T 1 T 2, then U has the form: U 1 U 2, with T 1 <: U 1 and U 2 <: T 2. (Proof: by induction on subtyping derivations.)

Inversion Lemma for Typing By this lemma, we know U = U 1 U 2, with T 1 <: U 1 and U 2 <: T 2. The IH now applies, yielding U 1 <: S 1 and Γ, x: S 1 s 2 : U 2. From U 1 <: S 1 and T 1 <: U 1, rule S-Trans gives T 1 <: S 1. From Γ, x: S 1 s 2 : U 2 and U 2 <: T 2, rule T-Sub gives Γ, x: S 1 s 2 : T 2, and we are done

Subtyping with Other Features

Ascription and Casting Ordinary ascription:

Ascription and Casting Ordinary ascription: Casting (cf. Java):

Subtyping and Variants

Subtyping and Lists i.e., List is a covariant type constructor.

Subtyping and References i.e., Ref is not a covariant (nor a contravariant) type constructor.

Subtyping and References i.e., Ref is not a covariant (nor a contravariant) type constructor. Why? When a reference is read, the context expects a T 1, so if S 1 <: T 1 then an S 1 is ok.

Subtyping and References i.e., Ref is not a covariant (nor a contravariant) type constructor. Why? When a reference is read, the context expects a T 1, so if S 1 <: T 1 then an S 1 is ok. When a reference is written, the context provides a T 1 and if the actual type of the reference is Ref S 1, someone else may use the T 1 as an S 1. So we need T 1 <: S 1.

Subtyping and Arrays Similarly... This is regarded (even by the Java designers) as a mistake in the design.

References again Observation: a value of type Ref T can be used in two different ways: as a source for values of type T and as a sink for values of type T.

References again Observation: a value of type Ref T can be used in two different ways: as a source for values of type T and as a sink for values of type T. Idea:Split Ref T into three parts: Source T: reference cell with read capability Sink T: reference cell with write capability Ref T: cell with both capabilities

Modified Typing Rules

Subtyping rules

Capabilities Other kinds of capabilities (e.g., send and receive capabilities on communication channels, encrypt/decrypt capabilities of cryptographic keys,...) can be treated similarly.

Intersection and Union Types

Intersection Types The inhabitants of T 1 T 2 are terms belonging to both S and T i.e., T 1 T 2 is an order-theoretic meet (greatest lower bound) of T 1 and T 2.

Intersection Types Intersection types permit a very flexible form of finitary overloading. This form of overloading is extremely powerful. Every strongly normalizing untyped lambda-term can be typed in the simply typed lambda-calculus with intersection types. type reconstruction problem is undecidable Intersection types have not been used much in language designs (too powerful!), but are being intensively investigated as type systems for intermediate languages in highly optimizing compilers (cf. Church project).

Union types Union types are also useful. T 1 T 2 is an untagged (non-disjoint) union of T 1 and T 2. No tags : no case construct. The only operations we can safely perform on elements of T 1 T 2 are ones that make sense for both T 1 and T 2. N.b: untagged union types in C are a source of type safety violations precisely because they ignores this restriction, allowing any operation on an element of T 1 T 2 that makes sense for either T 1 or T 2. Union types are being used recently in type systems for XML processing languages (cf. Xduce, Xtatic).

Varieties of Polymorphism Parametric polymorphism (ML-style) Subtype polymorphism (OO-style) Ad-hoc polymorphism (overloading)

Homework Read chapter 14 & 15 Read and chew over the codes of chap 17. HW: 14.3.1, 15.5.2