2. 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.
3. 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
6. Subtyping on functions
Before 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)
8. TOP and BOT
TOP
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
9. Upcasting and downcasting
Upcasting
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?
10. Conclusion
A 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?
11. 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.“
15. 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 “()” ??
16. Bot and its twins - Void type
Bot 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
17. Higher Order Subtyping in Dependent types
Lets 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)