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

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

Subtyping. Lecture 13 CS 565 3/27/06

Lecture 13: Subtyping

Recursive Types and Subtyping

Part III. Chapter 15: 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?

Recursive Types and Subtyping

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.

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

Subtyping: An introduction

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

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

Programming Languages Lecture 15: Recursive Types & Subtyping

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

Subtyping and Objects

CSE 431S Type Checking. Washington University Spring 2013

Object typing and subtypes

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";

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

Types and Type Inference

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

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

Subsumption. Principle of safe substitution

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

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

Types and Type Inference

Type Hierarchy. Lecture 6: OOP, autumn 2003

Programming Languages

8 Understanding Subtyping

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

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

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

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

A Type System for Checking Applet Isolation in Java Card

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

An Introduction to Subtyping

301AA - Advanced Programming [AP-2017]

Types, Type Inference and Unification

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

A Typed Lambda Calculus for Input Sanitation

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

Static Program Analysis Part 1 the TIP language

Functional programming Primer I

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

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

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

Lecture 9: Typed Lambda Calculus

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

Compilation 2012 Static Type Checking

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

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

CSolve: Verifying C With Liquid Types

Jam: A Smooth Extension With Java Mixins

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

CSE 505 Graduate PL. Fall 2013

COS 320. Compiling Techniques

CS4215 Programming Language Implementation

The Substitution Model

Functions. How is this definition written in symbolic logic notation?

Interfaces. Java interface. Notes. Example of code duplication. Why an interface construct? Interfaces & Types. good software engineering

Structure of Programming Languages Lecture 10

GADTs meet Subtyping

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

INSTRUCTIONS TO CANDIDATES

Concurrent Models of Computation for Embedded Software

An Annotated Language

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

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

Motivation for typed languages

The Java Programming Language

UNIVERSITY OF CALIFORNIA Department of Electrical Engineering and Computer Sciences Computer Science Division. P. N. Hilfinger

Lectures 20, 21: Axiomatic Semantics

A Short Summary of Javali

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

CSCE 314 Programming Languages. Type System

Operators and Expressions

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

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

Week 7. Statically-typed OO languages: C++ Closer look at subtyping

Cover Page. The handle holds various files of this Leiden University dissertation

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

MPRI course 2-4 Functional programming languages Exercises

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides.

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

Java Wildcards Meet Definition-Site Variance

Verifying Safety Property of Lustre Programs: Temporal Induction

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 6

CS558 Programming Languages

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

A Correctness Proof for a Practical Byzantine-Fault-Tolerant Replication Algorithm

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

Lecture Notes on Program Equivalence

G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

Principles of Computer Science

CSE 307: Principles of Programming Languages

9/7/17. Outline. Name, Scope and Binding. Names. Introduction. Names (continued) Names (continued) In Text: Chapter 5

Runtime Behavior of Conversion Interpretation of Subtyping

Transcription:

Subtyping (cont) Lecture 15 CS 565 Formalization of Subtyping Inversion of the subtype relation:! If "<:#1!#2 then " has the form "1! "2, and #1<:"1 and "2<:#1! If "<:{li:#i i!1 n}, then " has the form {kj:"j j!1 m} with at least the labels {li i!1 n}!{kj j!1 m} and with "j<:#i for each common label li = kj 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. Up 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.

Down Casts 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: " <: # Assume " <: # " ref <: # ref! 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):! Assume " <: # " <: # # ref <: " ref 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!, " " e 1 :source # 1!, " "!e 1 : # 1!, " " e 1 :sink #!, " " e 2 :#!, " " e 1 :=e 2 :Unit

Subtyping Rules " <: # covariant on reads source " <: source # # <: " contravariant on writes sink " <: sink # 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. 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 [[ # <: " ]] = [[ # <: " ]] o [[ # <: # ]] -- 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: [[ real!unit<:int!unit ]] printreal Which one is right?