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:

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

A Typed Lambda Calculus for Input Sanitation

Subtyping and Objects

Programming Languages Lecture 15: Recursive Types & Subtyping

Programming Languages Fall 2014

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?

Lecture 9: Typed Lambda Calculus

Lambda Calculi With Polymorphism

Recursive Types and Subtyping

Symmetry in Type Theory

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

COS 320. Compiling Techniques

Recursive Types and Subtyping

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

Lesson 4 Typed Arithmetic Typed Lambda Calculus

Harvard School of Engineering and Applied Sciences Computer Science 152

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

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

CIS 500 Software Foundations Fall December 6

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

Programming Languages Lecture 14: Sum, Product, Recursive Types

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

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

Featherweight Java (FJ)

Lambda Calculi With Polymorphism

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

Second-Order Type Systems

CIS 500 Software Foundations Midterm I

Formal Systems and their Applications

Type Checking and Type Inference

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

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

Object Oriented Issues in VDM++

CSE 505 Graduate PL. Fall 2013

Extracting the Range of cps from Affine Typing

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

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

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

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

Programming Languages

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

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

CIS 500 Software Foundations Fall October 2

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

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

CSE-321 Programming Languages 2014 Final

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.

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

λ calculus is inconsistent

The Typed λ Calculus and Type Inferencing in ML

Costly software bugs that could have been averted with type checking

Type Systems Winter Semester 2006

Dependent Object Types - A foundation for Scala s type system

Verifying Program Invariants with Refinement Types

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

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

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

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

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

CIS 500 Software Foundations Fall December 4

CSE-321 Programming Languages 2010 Final

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

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

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

Generic polymorphism on steroids

Types and Type Inference

Concepts of programming languages

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

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

Lambda Calculus and Type Inference

Part VI. Imperative Functional Programming

CSE505, Fall 2012, Midterm Examination October 30, 2012

Lecture Notes on Program Equivalence

CIS 500 Software Foundations Fall September 25

Type Systems Winter Semester 2006

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

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

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

4.5 Pure Linear Functional Programming

Inheritance and object compatibility

CSE-321 Programming Languages 2011 Final

Let Arguments Go First

Less naive type theory

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

Mid-Term 2 Grades

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

CMSC 330: Organization of Programming Languages

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 Is the term right? It is not well typed.

Motivation With the usual typing rule for applications the term is not well typed. This is silly: what 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, i.e.,

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, i.e., 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 Back to the example, we will define subtyping between record types so that, for example {x: Nat, y: Nat} <: {x: Nat} 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} by subsumption, and hence {x = 0, y = 1} {x: Nat} is well typed.

The Subtype Relation: Records Width subtyping (forgetting fields on the right): l < : T <?..ABC <?..A < <: l < : T < (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 < : T < <?..ABC Intuition: <: l < : T < <?..A (S-RcdWidth) {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 stronger constraints on values, so it describes fewer values.

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.

Examples

Examples 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 RCDWIDTH S REFL a: Nat, b: Nat <: a: Nat m: Nat <: m: Nat x: a: Nat, b:nat, y: m: Nat <: {x: a: Nat, y: m: Nat } S RcdDepth

Order of fields in Records The order of fields in a record does not make any difference to how wecan safely use it, sincethe only thing that we can do with records (projecting their fields) is insensitive to the orderof 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.

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 insubclasses (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 A high-order language, functions can be passed as arguments to other functions

The Subtype Relation: Arrow types Note the order of T? and S? 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? and S? 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? S V, then we know 1. f accepts elements of type S? ; clearly, f will also accept elements of any subtype T? of S?. 2. the type of f also tells us that it returns elements of type S V ; we can also view these results belonging to any supertype T V of S V. i.e., any function f of type S? S V can also be viewed as having type T? T V.

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 IIt 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 subtyperelation. Cf. Object in Java.

Subtype Relation: General rules

Subtype Relation: General rules A subtyping is a binary relation between types that is closed under the 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 λ. However, Proofs become a bit more involved, because the typingrelation is nolongersyntax directed. Given a derivation, we don t always know what rule was used in the last step. e.g., therule T-SUB couldappear 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 and t t, then Γ 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 : T. Not hard!

Application case Case T-App : t = t? t V Γ t? : T?? T?V Γ t V : T?? T = T?V 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 bycases.

Application case Case T-App : t = t? t V Γ t? : T?? T?V Γ t V : T?? T = T?V 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? t h? t = t? t V The result follows from the induction hypothesisand T-App.

Application case Case T-App : t = t? t V Γ t? : T?? T?V Γ t V : T?? T = T?V Subcase E-App2: t? = v? t V t V Similar. t = v? t V

Application case Case T-App: t = t? t V Γ t? : T?? T?V Γ t V : T?? T = T?V Subcase E-AppAbs : t? = λx: S??. t?v t V = v V t = [x v V ] t?v by the inversion lemma for the typing relation... T?? <: S?? and Γ, x: S?? t?v : T?V. By using T-Sub, Γ t V : S??. by the substitution lemma, Γ t h : T?V.

Inversion Lemma for Typing Lemma: if Γ λx: S?. s V : T? T V, then T? <: S? and Γ, x: S? s V : T V.

Inversion Lemma for Typing Lemma: if Γ λx: S?. s V : T? T V, then T? <: S? and Γ, x: S? s V : T V. 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 ( since we do notknow that U is an arrow type). Need another lemma... Lemma: If U <: T? T V, then U has the form of U? U V, with T? <: U? and U V <: T V. (Proof: by induction on subtyping derivations.)

Inversion Lemma for Typing By this lemma, we know U = U? U V, with T? <: U? and U V <: T V. TheIH nowapplies, yielding U? <: S? and Γ, x: S? s V : U V. From U? <: S? and T? <: U?, rule S-Trans gives T? <: S?. From Γ, x: S? s V : U V and U V <: T V, rule T-Sub gives Γ, x: S? s V : T V, 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, but an invariant.

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?, so if S? <: T? then an S? 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?, so if S? <: T? then an S? is ok. When a reference is written, the context provides a T? and if the actual type of the reference is Ref S?, someone else may use the T? as an S?. So we need T? <: S?.

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 can be treated similarly, e.g., send and receive capabilities on communication channels, encrypt/decrypt capabilities of cryptographic keys,...

Intersection and Union Types

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

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 intersectiontypes. 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? T V is an untagged (non-disjoint) union of T? and T V. No tags : no case construct. The only operations we can safely perform on elements of T? T V are ones that make sense for both T? and T V. 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? T V that makes sense for either T? or T V. 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)

HW for Chap15 & 16 15.2.5 15.3.2