Subtyping COS 441 Princeton University Fall 2004.

Slides:



Advertisements
Similar presentations
Overriding CMPS Overriding Recall, a method in a child class overrides a method in the parent class, if it has the same name and type signature.
Advertisements

Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
ISBN Chapter 7 Expressions and Assignment Statements.
Chapter 7:: Data Types Programming Language Pragmatics
Nir Piterman Department of Computer Science TexPoint fonts used in EMF. Read the TexPoint manual before you delete this box.: AAAAA Bypassing Complexity.
Lightweight Abstraction for Mathematical Computation in Java 1 Pavel Bourdykine and Stephen M. Watt Department of Computer Science Western University London.
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
CSE-321 Programming Languages Subtyping POSTECH May 14, 2007 박성우.
Chapter 5 Types. 5-2 Topics in this Chapter Values vs. Variables Types vs. Representations Type Definition Operators Type Generators SQL Facilities.
Type Checking.
Compiler Construction
Catriel Beeri Pls/Winter 2004/5 last 55 Two comments on let polymorphism I. What is the (time, space) complexity of type reconstruction? In practice –
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
CSE341: Programming Languages Lecture 26 Subtyping for OOP Dan Grossman Fall 2011.
Data Abstraction COS 441 Princeton University Fall 2004.
Parametric Polymorphism COS 441 Princeton University Fall 2004.
Encapsulation by Subprograms and Type Definitions
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra Chapter 4, Part A.
Aggregate Data Structures COS 441 Princeton University Fall 2004.
Feather-Weight Java COS 441 Princeton University Fall 2004.
Programming in Scala Chapter 1. Scala: both object-oriented and functional Scala blends –object-oriented and –functional programming in a –statically.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Semantics for MinML COS 441 Princeton University Fall 2004.
1/25 Pointer Logic Changki PSWLAB Pointer Logic Daniel Kroening and Ofer Strichman Decision Procedure.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
Abstract Interpretation (Cousot, Cousot 1977) also known as Data-Flow Analysis.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
Procedure specifications CSE 331. Outline Satisfying a specification; substitutability Stronger and weaker specifications - Comparing by hand - Comparing.
CSE 331 Software Design & Implementation Hal Perkins Autumn 2012 Java Classes, Interfaces, and Types 1.
C H A P T E R S E V E N Expressions and Assignment Statements.
Type Equivalence Rules Ada –Strict name equivalence except for almost everything Unique array constructors give rise to unique types Subtypes can create.
IT253: Computer Organization Lecture 4: Instruction Set Architecture Tonga Institute of Higher Education.
1 Abstraction  Identify important aspects and ignore the details  Permeates software development programming languages are abstractions built on hardware.
1 Chapter 10: Data Abstraction and Object Orientation Aaron Bloomfield CS 415 Fall 2005.
1 COSC3557: Object-Oriented Programming Haibin Zhu, Ph. D. Associate Professor of CS, Nipissing University.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 Relational Algebra.
1 Relational Algebra and Calculas Chapter 4, Part A.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 Database Management Systems Chapter 4 Relational Algebra.
Homogeneous tuples What are they? –S 2 = S x S –S n = S x S x … x S Cardinalities –#(S 2 )= (#S) 2 –#(S n )= (#S) n –#(S 0 )= (#S) 0 =1 What is S 0 ? –It.
3C-1 Purity Typing Language semantics Inheritance model  Single vs. Multiple inheritance  Common root Modular mechanisms Generics Object Oriented Languages.
Polymorphism Liskov 8. Outline equals() Revisiting Liskov’s mutable vs. not rule Polymorphism Uniform methods for different types “easy” polymorphism.
Ch. 5 Ch. 51 jcmt Summer 2003Programming Languages CSE3302 Programming Languages (more notes) Summer 2003 Dr. Carter Tiernan.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Chapter Seven: Expressions and Assignment Statements Lesson 07.
Polymorphism SWE 619. Outline equals() Revisiting Liskov’s mutable vs. not rule Polymorphism Uniform methods for different types “easy” polymorphism Element.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Cs2220: Engineering Software Class 12: Substitution Principle Fall 2010 University of Virginia David Evans.
Zach Tatlock / Winter 2016 CSE 331 Software Design and Implementation Lecture 13 Generics 1.
Types and Programming Languages Lecture 10 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CSE 341 Section 10 Subtyping, Review, and The Future.
Arrays Using array as an expression, on the right-hand side Assigning to an array.
Subtying Kangwon National University 임현승 Programming Languages These slides were originally created by Prof. Sungwoo Park at POSTECH.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
Expressions and Assignment Statements
Expressions and Assignment Statements
Type Checking and Type Inference
Expressions and Assignment Statements
CS 326 Programming Languages, Concepts and Implementation
Expressions and Assignment Statements
Expressions and Assignment Statements
Arithmetic Expressions
User-Oriented Language Design
Conversions of the type of the value of an expression
Expressions and Assignment Statements
Compiler Construction
Java Programming Language
Compiler Construction
Presentation transcript:

Subtyping COS 441 Princeton University Fall 2004

Inclusive vs. Coercive Relationships Inclusive –Every cat is a feline –Every dog is a canine –Every feline is a mammal Coercive/isomorphism –Integers can be converted into floating point numbers –Booleans can be converted into interegers –Mammals with a tail can be converted to a mammals without a tail (ouch!)

Subtype Relation Read  1 <:  2 as  1 is a subtype of  2 or  2 is a supertype of  1 Subtype relation is reflexive and transitive We say (  1 =  2 ) iff (  1 <:  2 ) and (  2 <:  1 )  1 <:  2 refl  1 <:  3  1 <:  2  2 <:  3 trans

Implicit vs Explicit Typing rules for subtyping can be rendered in either implicit or explicit form cast

Simplification for Type-Safety Inclusive/Coercive distinction independent of Implicit/Explicit distinction Harper associates inclusive with implicit typing and coercive with explicit typing because it simplifies the type safety proof –You can have a inclusive semantics with explicit type casts –You can have a coercive semantics with implicit typing

Dynamic Semantics For inclusive system primitives must operate equally well for all subtypes of a give type for which the primitive is defined For coercive systems dynamic semantics simply must cast/convert the value appropriately

Varieties of Systems Implicit, Inclusive – Described by Harper Explicit, Coercive – Described by Harper Implicit, Coercive – Non-deterministic insertion of coercions Explicit, Inclusive – Type casts are no-ops in the operational semantics

Subtype Relation (cont.) Given via transitivity we can conclude ( bool <: float ) bool <: int b2i int <: float i2f

Subtyping of Functions weight: mammal ! float numberOfTeeth: mammal ! int numberOfWiskers: feline ! int pitchOfMeow: feline ! float printInfo: (mammal * (mammal ! float)) ! unit printFelineInfo: (feline * (feline ! float)) ! unit

Subtyping Quiz mammal ! int <: mammal ! float feline ! int <: feline ! float mammal ! float <: feline ! float mammal ! int <: feline ! int mammal ! int <: feline ! float

Co/Contra Variance  1 !  2 <:  1 ’ !  2 ’  1 ’ <:  1  2 <:  2 ’ (  1 *  2 ) <: (  1 ’ *  2 ’)  1 <:  1 ’  2 <:  2 ’ argument is contravariant return type is covariant both are covariant

Width vs Depth Subtyping Consider the n-tuple (  1 * … *  n ) Width Subtyping ( int * int * float ) <: ( int * int ) Depth Subtyping ( int * int ) <: ( float * float ) (  1 * … *  n ) <: (  ’ 1 * … *  ’ n )  1 <:  ’ 1 …  n <:  ’ n depth (  1 * … *  m ) <: (  1 * … *  n ) m > n width

Width and Depth for Records Similar rule for records {l 1 :  1,…,l n :  n } Width considers any subset of labels since order of labels doesn’t matter. Implementing this efficiently can be tricky but doable

Subtyping and Mutability Mutability destroys the ability to subtype  ref = {get:unit ! , set:  ! unit}  ’ ref = {get:unit !  ’, set:  ’ ! unit} Assume  <:  ’ from that we conclude unit !  <: unit !  ’ and  ’ ! unit <:  ! unit

Subtyping Defines Preorder/DAG Subtyping relation can form any DAG mammal feline canine dogcattigerwolf domesticatedwild

Typechecking With Subtyping With explicit typing every expression has a unique type so we can use type synthesis to compute the type of an expression Under implicit typing an expression may have many different types, which one should we choose? e.g. CalicoCat : mammal, CalicoCat : feline, and CalicoCat : cat

Which Type to Use? Consider weight: mammal ! float countWiskers: feline ! int let val c = CalicoCat in (weight c,countWiskers c) end What type should we use for c?

Which Type to Use? Consider weight: mammal ! float countWiskers: feline ! int let val c: mammal = CalicoCat in (weight c,countWiskers c) end What type should we use for c?

Which Type to Use? Consider weight: mammal ! float countWiskers: feline ! int let val c: feline = CalicoCat in (weight c,countWiskers c) end How do we know this is the “best” solution?

Which Type to Use? Consider weight: mammal ! float countWiskers: feline ! int let val c: cat = CalicoCat in (weight c,countWiskers c) end Choose the most specific type.

Principal Types Principal type is the “most specific” type. It is the least type in a given pre-order defined by the subtype relation Lack of principal types makes type synthesis impossible with implicit subtyping unless programmer annotates code Not at as big a problem for explicit subtyping rules

Subtyping Defines Preorder/DAG Q: What is the least element “principal type” for “mammal”? mammal feline canine dogcattigerwolf domesticatedwild

Subtyping Defines Preorder/DAG A: “mammal” has no principal type in the subtyping relation defined below mammal feline canine dogcattigerwolf domesticatedwild

Implementing Subtyping For inclusive based semantics maybe hard to implement or impose restrictions on representations of values Coercive based semantics give more freedom on choosing appropriate representation of values Can use “type-directed translation” to convert inclusive system to coercive system

Subtyping with Coercions Define a new relation  1 <:  2 Ã v Where v is a function of type (  1 !  2 )

Subtyping with Coercions (cont)

Implementing Record Subtyping Implementing subtyping on tuples is easy since address index “does the right thing” ((1, 2, 3) : (int * int * int)).2 ((1, 2, 3) : (int * int)).2 Selecting the field label with records is more challenging ({a=1,b=2,c=3} : {a:int,b:int,c:int}).c ({a=1,b=2,c=3} : {a:int,c:int}).c

Approaches to Record Subtyping Represent record as a “hash-table” keyed by label name Convert record to tuple when coercing create new tuple that represents different record with appropriate fields Two level approach represent record as “view” and value. Dynamically coerce views. (Java interfaces are implemented this way, but you can statically compute all the views in Java)

By Name vs Structural Subtyping Harper adopts a structural view of subtyping. Things are subtypes if they are some how isomorphic. Java adopts a “by name” view. Things are subtypes if they are structurally compatible and the user declared them as subtypes. Java approach leads to simpler type-checking and implementation but is arguably less modular than a pure structural approach

Summary Coercive vs Inclusive Operational view of what subtyping means Implicit vs Explicit How type system represents subtyping Systems can support all possible combinations Need to think things through to avoid bugs Tuples/records have both width and depth subtyping Functions are contravariant in argument type References are invariant