Lecture 13: Subtyping

Similar documents
Subtyping. Lecture 13 CS 565 3/27/06

Part III. Chapter 15: Subtyping

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

Part III Chapter 15: Subtyping

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

Subsumption. Principle of safe substitution

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

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

Subtyping and Objects

Featherweight Java (FJ)

Lambda Calculi With Polymorphism

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

CSE 505 Graduate PL. Fall 2013

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

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

Lecture 14: Recursive Types

Lambda Calculi With Polymorphism

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

Typed Lambda Calculus and Exception Handling

Programming Languages Lecture 15: Recursive Types & Subtyping

Symmetry in Type Theory

Harvard School of Engineering and Applied Sciences Computer Science 152

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

Programming Languages

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

CIS 500 Software Foundations Fall December 6

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

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

Lecture 7: The Untyped Lambda Calculus

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

CS-XXX: Graduate Programming Languages. Lecture 17 Recursive Types. Dan Grossman 2012

Lecture 5: The Untyped λ-calculus

Programming Languages Fall 2014

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

Recursive Types and Subtyping

CSE-505: Programming Languages. Lecture 20.5 Recursive Types. Zach Tatlock 2016

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

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

CIS 500 Software Foundations Fall October 2

Lecture 9: Typed Lambda Calculus

Subtyping: An introduction

Recitation 8: Dynamic and Unityped Languages : Foundations of Programming Languages

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

From Math to Machine A formal derivation of an executable Krivine Machine Wouter Swierstra Brouwer Seminar

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

A Typed Lambda Calculus for Input Sanitation

CSE505, Fall 2012, Midterm Examination October 30, 2012

CS558 Programming Languages

Let Arguments Go First

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

From IMP to Java. Andreas Lochbihler. parts based on work by Gerwin Klein and Tobias Nipkow ETH Zurich

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

Lesson 4 Typed Arithmetic Typed Lambda Calculus

8 Understanding Subtyping

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

Recursive Types and Subtyping

CSE-321 Programming Languages 2014 Final

The Polymorphic Blame Calculus and Parametricity

Simply-Typed Lambda Calculus

Dependent Object Types - A foundation for Scala s type system

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

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

Inductive Definitions, continued

COS 320. Compiling Techniques

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

CSE-321 Programming Languages 2012 Midterm

Static and User-Extensible Proof Checking. Antonis Stampoulis Zhong Shao Yale University POPL 2012

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

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

Collage of Static Analysis

CSE-321 Programming Languages 2010 Final

CS4215 Programming Language Implementation. Martin Henz

Costly software bugs that could have been averted with type checking

Lectures 20, 21: Axiomatic Semantics

Consistent Subtyping for All

Rules and syntax for inheritance. The boring stuff

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

Data Types (cont.) Administrative Issues. Academic Dishonesty. How do we detect plagiarism? Strongly Typed Languages. Type System

CMSC 330: Organization of Programming Languages

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

More Lambda Calculus and Intro to Type Systems

Dynamic Types, Concurrency, Type and effect system Section and Practice Problems Apr 24 27, 2018

A formal derivation of an executable Krivine machine

Programming Languages Lecture 14: Sum, Product, Recursive Types

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

Type Systems. Today. 1. Organizational Matters. 1. Organizational Matters. Lecture 1 Oct. 20th, 2004 Sebastian Maneth. 1. Organizational Matters

CSE Lecture 7: Polymorphism and generics 16 September Nate Nystrom UTA

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

Higher-Order Intensional Type Analysis. Stephanie Weirich Cornell University

A Type is a Set of Values. Here we declare n to be a variable of type int; what we mean, n can take on any value from the set of all integer values.

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

Implementing objects as in Javascript, Lua, 164 is expensive because object attributes are implemented as hashtable accesses:

CS 565: Programming Languages. Spring 2008 Tu, Th: 16:30-17:45 Room LWSN 1106

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

CSE 341: Programming Languages

References and Exceptions. CS 565 Lecture 14 4/1/08

Type Hierarchy. Lecture 6: OOP, autumn 2003

1 Introduction. 3 Syntax

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

CS153: Compilers Lecture 15: Local Optimization

Transcription:

Lecture 13: Subtyping Polyvios Pratikakis Computer Science Department, University of Crete Type Systems and Programming Languages Pratikakis (CSD) Subtyping CS546, 2018-2019 1 / 15

Subtyping Usually found in Object Oriented languages One form of polymorphism: a program can have more than one types So far, each language feature we saw is compositional: can be added without affecting the rest of the language Subtyping is not: we might need to change the type rules for other features Roughly: if all expressions of type T also have type T, then T is a subtype of T Alternatively: if we can always substitute an expression of type T with an expression of type T in any context and still have a valid program, T is a subtype of T Pratikakis (CSD) Subtyping CS546, 2018-2019 2 / 15

Background Simply typed lambda calculus with numbers and records: e ::= x λx : Te e e n {l 1 = e 1,, l n = e n } case e of {l 1 (x) => e 1 l n (x) e n } v ::= n λx : Te {l 1 = v 1,, l n = v n } T ::= T T Nat {l 1 : T 1,, l n : T n } Type rule for function application: [T-App] Γ e 1 : T T Γ e 2 : T Γ (e 1 e 2 ) : T Not allowed: (λx : {foo : Nat}(xfoo)) {foo = 5, bar = 42} Even though it is always safe! Pratikakis (CSD) Subtyping CS546, 2018-2019 3 / 15

Subsumption It is always safe to pass a struct with more fields If the function can be typed assuming its argument x has type {foo : Nat}, then it only accesses the foo field of record x It won t hurt if x has additional fields We say {foo : Nat, bar : Nat} is a subtype of {foo : Nat} Also written as {foo : Nat, bar : Nat} <: {foo : Nat} To use the subtype relation <: during type-checking, we add one more type rule: Γ e : T 1 T 1 <: T 2 [T-Sub] Γ e : T 2 It says we can use a subtype instead of a supertype Pratikakis (CSD) Subtyping CS546, 2018-2019 4 / 15

Defining subtype We define a relation <: between types as usual Inductively, using inference rules Each rule produces a judgement T <: T The relation is the smallest set of subtyping judgements produced by the inference rules The same as all definitions so far Pratikakis (CSD) Subtyping CS546, 2018-2019 5 / 15

Subtyping relation The subtyping relation is reflexive: [S-Refl] T <: T The subtyping relation is transitive: [S-Trans] T 1 <: T 2 T 2 <: T 3 T 1 <: T 3 Both, from the intuition of safely substituting a subtype for a supertype Pratikakis (CSD) Subtyping CS546, 2018-2019 6 / 15

Subtyping relation (cont d) A record type is a subtype of another if it has more fields: [S-Wide] {l 1 : T 1,, l n+k : T n+k } <: {l 1 : T 1,, l n : T n } or if all its fields are subtypes: [S-Deep] T i <: T i, for each 0 i n {l 1 : T 1,, l n : T n } <: {l 1 : T 1,, l n : T n} or if the fields are reordered: [S-Perm] {l 1 : T 1,, l i : T i,, l j : T j,, l n : T n } <: {l 1 : T 1,, l j : T j,, l i : T i,, l n : T n } Pratikakis (CSD) Subtyping CS546, 2018-2019 7 / 15

Subtyping relation (cont d) A function type is a subtype of another if it can be used instead The subtype should accept all arguments the supertype accepts (contravariant) The subtype shouldn t return anything not returned by the supertype (covariant) T 2 <: T 1 T 1 <: [S-Fun] T 2 T 1 T 1 <: T 2 T 2 One supertype to rule them all (like javalangobject): [T-Top] T <: Pratikakis (CSD) Subtyping CS546, 2018-2019 8 / 15

Metatheory Inversion lemma of the subtyping relation If T <: T1 T 2 then T has the form T 1 T 2 with T 1 <: T 1 and T 2 <: T 2 If T <: {l1 : T 1,, l n : T n } then T has the form {k 1 : T 1,, k n : T n} with at least the labels l 1,, l n and for all 0 i n, if k j = l i then T j <: T i Inversion lemma of the typing relation If Γ (λx : Te) : T 1 T 2, then T 1 <: T and Γ, x : T e : T 2 If Γ {k1 = e 1,, k n = e n } : {l 1 : T 1,, l m : T m } then for each i 0m there is a j 0n such that l i = k j and Γ e j : T i Pratikakis (CSD) Subtyping CS546, 2018-2019 9 / 15

Metatheory (cont d) Substitution and preservation remain the same (their proof changes) Substitution lemma If Γ, x : T1 e : T 2 and Γ e : T 1 then Γ e [e/x] : T 2 Preservation theorem If Γ e : T and e e then Γ e : T Pratikakis (CSD) Subtyping CS546, 2018-2019 10 / 15

Metatheory (cont d) Canonical forms lemma If v is a value and v : T 1 T 2 then v has the form λx : Te If v is a value and v : {l1 : T 1,, l n : T n } then v has the form {k 1 = v 1,, k m = v m } where for all l i there is a k j = l i Progress theorem If e : T then either e is a value or there is some e with e e Pratikakis (CSD) Subtyping CS546, 2018-2019 11 / 15

Subtyping and casts Ascription: explicitly stating the type of an expression in ML, written (e : T) Also called casting in languages like C/C++, Java, C#, etc written (T)e Two very different forms of casting Up-cast: T is a supertype of the typechecker s type for e Down-cast: T is a subtype of the typechecker s type for e Down-cast is unsafe What happens if at runtime e does not have type T? Down-casts usually compiled into run-time checks that raise a dynamic exception Alternatively, down-casts only allowed as a test (like instanceof), providing an else case Pratikakis (CSD) Subtyping CS546, 2018-2019 12 / 15

Subtyping and references References are like implicit function arguments and also like implicit function results They have to be both covariant and contravariant! References are invariant under subtyping to preserve type safety: T 1 <: T 2 T 2 <: T 1 Ref T 1 <: Ref T 2 This restriction is caused by the two operations supported Read causes a covariant constraint Write causes a contravariant constraint Pratikakis (CSD) Subtyping CS546, 2018-2019 13 / 15

Subtyping and arrays Arrays are like references: can read and write the contents Like references, we need invariant subtyping for type-safety T 1 <: T 2 T 2 <: T 1 T 1 [ ] <: T 2 [ ] Pratikakis (CSD) Subtyping CS546, 2018-2019 14 / 15

Arrays in Java Interestingly, Java permits covariant subtyping for arrays T 1 <: T 2 T 1 [ ] <: T 2 [ ] But, consider: Integer[] x = new Integer[10]; Object[] y = (Object[]) x; y[3] = new Object(); x[3]intvalue(); // OOPS, no such method! Bad design, big performance hit to keep safe: Every array assignment is equivalent to a downcast Must check every assignment to every array at runtime! Pratikakis (CSD) Subtyping CS546, 2018-2019 15 / 15