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

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

Subtyping. Lecture 13 CS 565 3/27/06

Lecture 13: Subtyping

Part III. Chapter 15: Subtyping

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

Part III Chapter 15: Subtyping

Recursive Types and Subtyping

Recursive Types and Subtyping

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

Programming Languages Lecture 15: Recursive Types & Subtyping

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

Subtyping: An introduction

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.

Subtyping and Objects

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

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

Featherweight Java (FJ)

Lecture #23: Conversion and Type Inference

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

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

Programming Languages

Harvard School of Engineering and Applied Sciences Computer Science 152

Subsumption. Principle of safe substitution

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

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

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

A Typed Lambda Calculus for Input Sanitation

Program Syntax; Operational Semantics

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

Second-Order Type Systems

Runtime Behavior of Conversion Interpretation of Subtyping

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

Programming Languages Lecture 14: Sum, Product, Recursive Types

GADTs meet Subtyping

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

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

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

Object typing and subtypes

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

Lecture 9: Typed Lambda Calculus

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

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

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

Compilation 2012 Static Type Checking

CSE 431S Type Checking. Washington University Spring 2013

Part VI. Imperative Functional Programming

Polymorphism. Lecture 19 CS 565 4/17/08

Lecture Notes on Aggregate Data Structures

CSE-321 Programming Languages 2011 Final

Assignment 4: Semantics

Hierarchical Pointer Analysis for Distributed Programs

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

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

8 Understanding Subtyping

Hiding local state in direct style: a higher-order anti-frame rule

JAVASCRIPT BASICS. Type-Conversion in JavaScript. Type conversion or typecasting is one of the very important concept in

Lambda Calculi With Polymorphism

CS6202: Advanced Topics in Programming Languages and Systems Lecture 0 : Overview

MPRI course 2-4 Functional programming languages Exercises

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

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

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

A Type System for Checking Applet Isolation in Java Card

Static Type Checking. Static Type Checking. The Type Checker. Type Annotations. Types Describe Possible Values

1 Introduction. 3 Syntax

Types and Type Inference

Java Inheritance. Written by John Bell for CS 342, Spring Based on chapter 6 of Learning Java by Niemeyer & Leuck, and other sources.

CSolve: Verifying C With Liquid Types

COS 320. Compiling Techniques

An Introduction to Subtyping

Concurrent Models of Computation for Embedded Software

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

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

Programming with Types

CS152: Programming Languages. Lecture 24 Bounded Polymorphism; Classless OOP. Dan Grossman Spring 2011

Types, Expressions, and States

Lecture Notes on Program Equivalence

Semantics of programming languages

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

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

Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

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

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

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

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

Refinement Types as Proof Irrelevance. William Lovas with Frank Pfenning

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

Ornaments in ML. Thomas Williams, Didier Rémy. April 18, Inria - Gallium

Intersections and Unions of Session Types

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

CSE-321 Programming Languages 2012 Midterm

Types and Static Type Checking (Introducing Micro-Haskell)

Jam: A Smooth Extension With Java Mixins

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Lambda Calculi With Polymorphism

CSE 505 Graduate PL. Fall 2013

Type Hierarchy. Lecture 6: OOP, autumn 2003

Compiler Construction I

Refinement Types for TypeScript

Structure of Programming Languages Lecture 10

Transcription:

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

Formalization of Subtyping Inversion of the subtype relation: If σ <: τ 1 τ 2 then σ has the form σ 1 σ 2, and τ 1 <: σ 1 and σ 2 <: τ 1 If σ <: {l i :τ i i 1...n }, then σ has the form {kj : σ j j 1...m } with at least the labels {l i i 1...n } {kj j 1...m } and with σj <: τ i for each common label l i = k j This lemma says that a subtype of an arrow type must be an arrow type that is contravariant in the argument and covariant in the result, and a subtype of a record type must be a record type that either has more fields (width) in some order (permute) and that the types of common fields obey the subtype relation (depth) Proof of preservation and progress follow as before using induction on typing derivations.

Casts and Ascription Ascription (t as τ) is a form of checked documentation: it allows the programmer to record an assertion that the subterm of a complex expression has some specific type. Ascription in the presence of subtyping raises interesting issues: Casting

Casts Upcasts: a term is ascribed a supertype of the type that would normally be assigned. Benign since it effectively hides some parts of a value so they cannot be used in some surrounding context. Downcasts: assign a type to a term that would not be assigned by the typechecker. No specific relation between the term expected and the term desired. List objects in Java expect elements belonging to Object. When an element of type τ is stored, it is promoted to Object. When an element is extracted, it must be downcast to a type suitable for the context in which it is to be used. Requires runtime checks Complicates implementations since type information must be presented at runtime to validate the cast.

Subtyping References Try covariance: σ <: τ σ ref <: τ ref Assume σ <: τ The following holds: x : τ, y : σ ref, f : σ int y := x ; f (! y) Unsound: f is called on a τ but is defined only on σ If we want covariance of references we can recover type safety with a runtime check for each y:=x assignment The actual type of x matches the actual type of y This is not a particularly good design. Note: Java has covariant arrays

Subtyping References Another approach (contravariance): σ <: τ τ ref <: σ ref Assume σ <: τ The following holds: x : τ, y : τ ref, f : σ int y := x; f (!y) Unsound: f is called on a τ but is defined only on σ references can be used in two ways: for reading, context expects a value of type σ but the reference yields a value of type τ then we need τ <: σ. for writing, the new value provided by the context will have type σ. If the actual type of the reference is ref τ, then this value may be read and used as a τ. This will only be safe if σ <: τ.

Solution References should be typed invariantly: No subtyping for references (unless there are runtime checks) Arrays (which are implemented using updates) should be typed invariantly. Similarly, mutable records should also be invariant.

Refinements A value of type ref τ can be used in two different ways: as a source for reading and a sink for writing: Split ref τ into three parts: source τ: reference cell with read capability sink τ: reference cell with write capability ref τ: cell with both capabilities

Modified Typing Rules Γ, Σ e1 : source τ1 Γ, Σ!e1 : τ1 Γ, Σ e1 : sink τ Γ, Σ e2 : τ Γ, Σ e1 := e2 : Unit

Subtyping Rules σ <: τ source σ <: source τ covariant on reads τ <: σ sink σ <: sink τ contravariant on writes ref τ <: source τ ref τ <: sink τ refs can be downgraded to source or sink

Coercion Semantics We have used a subset interpretation to reason about subtyping about base types. Thus, int <: real, but this is not particularly practical since integers and reals typically have different internal representations. 4.5 + 6 is well-typed under this interpretation Performance penalities Tagging datatypes permutation rule on records

Coercions Adopt a different semantics: compile-away subtyping by replacing it with runtime coercions: If an Int is promoted to Real by the typechecker, at runtime we convert its representation from a machine integer to a machine float. Express coercions using a conversion rule on types.

Type Coercions «Top Unit «τ1 τ 2 «τ1 «τ 2 «{li : τi} {li : «τi }

Subsumption Coercion «τ <: τ λ x: «τ.x «τ <: Top λ x: «τ.unit «C1:: τ 1 <: τ 2 C2::τ 2 <: τ 3 τ 1 <: τ 3 λ x:«τ1.«c2 («C1 x) «C1:: τ 3 <: τ 1 C2::τ 2 <: τ 4 τ 1 τ 2 <: τ 3 τ 4 λ f:«τ1 τ2. λ x:«τ3.«c2 (f(«c1 x))

Typing Statement Coercion «x: τ Γ Γ x : τ x «D:: Γ, x: τ 1 t : τ 2 Γ λ x : τ 1. t : τ 1 τ 2 λ x:«τ1. «D «D1:: Γ t1 : τ 1 τ 2 D2:: Γ t2 : τ 1 Γ t1 t2 : τ 2 «D1 «D2 «D:: Γ t : τ 1 C:: τ 1 <: τ 2 Γ t : τ 2 «C «D

Coherence The place where coercions are injected into type derivations is relevant: There may be several possible derivations that inject coercions at different places, leading to different behaviors. Other issues: Can we build arbitrary subtype relations just because we can write conversion functions? Is real <: int simply because the floor function is a conversion? What is the conversion from real int to int int? A system of conversion functions is coherent if whenever we have τ <: τ <: σ: «τ <: τ = λ x.x «τ 1 <: τ 2 = «τ 3 <: τ 2 o«τ 1 <: τ 3 -- coercions compositional

Example Say we want the following subtyping relations: int <: real λ x:int. toieee x real <: int λ x:real. floor x For this system to be coherent we need: «int <: real o «real <: int = λ x.x «real <: int o «int <: real = λ x.x This means that x: real, toieee (floor x) = x This is not true

Example Consider the type derivation of printreal 1 : 1 : int int <: real printreal: real unit 1 : real printreal 1 : unit we convert 1 to real: «int <: real 1 This is also a valid derivation: printreal: real unit real unit <: int unit printreal: int unit 1: int printreal 1: unit we convert printreal: «real unit <: int unit printreal Which one is right?