subtyping

17
Subtyping Yuting Chen Alexander Lissenko

Upload: jeff-chen

Post on 14-Apr-2017

40 views

Category:

Science


0 download

TRANSCRIPT

Page 1: Subtyping

SubtypingYuting Chen

Alexander Lissenko

Page 2: Subtyping

Why Subtyping?

to express the relations between types :

“Cat is one instance of Mammals;

where Mammals are one kind of Animals”

“ Taxonomy “

systems for naming

and organizing things

into groups which

share similar qualities.

Page 3: Subtyping

Formal Subtypes The need of subtyping:

is this typable ?

lookup : Person -> PersonalNumber

can we pass “A computer science majoring swedish-native student” into lookup ??

Notations:

S <: T

(S is the subtype of T)

Rules: (substitutability)

“it is safe to substitute T with S”

every value in T is also described in S

S has more detail than T

Page 4: Subtyping

Rules / PropertiesReflexivity

S <: S

Subsumption

Transitivity

Width and Depth

Page 5: Subtyping

Record Permutation

since record fields are projected out

thus should be insensitive to order

Page 6: Subtyping

Subtyping on functionsBefore we look into subtypes of functions

there’re some terminologies we wish to settle

( to learn more see “functors”

in Category Theory )

Nice blog post by Bartosz Milewski

Given f : A -> B

covariant

“Maintain the same direction”

F(f) : F(A) -> F(B)

contravariant

“Reverse the direction”

G(f) : G(B) -> G(A)

Page 7: Subtyping

Subtyping on functions“Don’t surprise the functions”

Page 8: Subtyping

TOP and BOTTOP

Maximal type

All types are subtypes of TOP

In OO, TOP corresponds to Object

BOT

Minimal type, subtype of all types

In OO, BOT corresponds to the divergence

Type checking - not so straightforward

Page 9: Subtyping

Upcasting and downcastingUpcasting

From subtype to supertype

Abstraction, “hiding” some fields

Straightforward for typechecker

Downcasting

From supertype to subtype

Typechecker - “Trust, but verify”

Type checking in run time - potential risk?

Page 10: Subtyping

ConclusionA powerful extension to simply typed lambda calculus

Essential feature of object-oriented languages

Code can be written in a more abstract manner

Type checking becomes more complicated

Hurts decidability?

Subtyping applied in other studies?

Page 11: Subtyping

Nominal & Structural subtyping From a paper from Donna M. and Jonathan A. of CMU

“Integrating Nominal and Structural Subtyping”

“a language with structural subtyping, a type U is a subtype of T if its methods and fields are a superset of T’s methods and fields. “

“language with nominal subtyping, on the other hand, U is a subtype of T if and only if it is declared to be.“

Page 12: Subtyping
Page 13: Subtyping

Messy With polymorphism and reference, things run out of control really really fast !

Java Example

Page 14: Subtyping

java.lang.Object is not really Top

(Slides from prof. Wolfgang)

(from SEFM course)

Page 15: Subtyping

Billion-dollar mistake ? I call it my billion-dollar mistake . It was the invention of the null reference in 1965. At that time, I was designing the first comprehensive type system for references in an object-oriented language. My goal was to ensure that all use of references should be absolutely safe, with checking performed automatically by the compiler. But I couldn’t resist the temptation to put in a null reference, simply because it was so easy to implement. This has led to innumerable errors, vulnerabilities, and system crashes, which have probably caused a billion dollars of pain and damage in the last forty years.

– Sir Tony Hoare

A ) int x = null; // compile error

B ) Integer i = null;

int x = i; // source of pain

What is Null ?

It’s not Bottom … not yet !

It’s an unit type.

It’s like the “Nothing” in Maybe type from Haskell

it subverts types (used with reference)

Wait, isn’t Unit type in Haskell called “()” ??

Page 16: Subtyping

Bot and its twins - Void typeBot can be used for expressing divergence on functions

and to the duality of this, we have void type (also an unit type like null)

to indicate normal returns of functions (but there exists no meaningful value to return)

putStr :: String -> IO ()

These exists an isomorphism between any two such sets

Page 17: Subtyping

Higher Order Subtyping in Dependent typesLets do some subtyping with higher order subjects :

List / Reference / Array

How would subtyping looks like in Agda ?

See more in prof. Andreas Abel’s lecture note (IOC 2011)