SlideShare a Scribd company logo
1 of 17
Download to read offline
Subtyping
Yuting Chen
Alexander Lissenko
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.
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
Rules / Properties
Reflexivity
S <: S
Subsumption
Transitivity
Width and Depth
Record Permutation
since record fields are projected out
thus should be insensitive to order
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)
Subtyping on functions
“Don’t surprise the functions”
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
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?
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?
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.“
Messy
With polymorphism and reference, things run
out of control really really fast !
Java Example
java.lang.Object is not really Top
(Slides from prof. Wolfgang)
(from SEFM course)
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 “()” ??
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
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)

More Related Content

What's hot (8)

MSTC'14 (windows 8 + windows phone) Workshops_Day 2
MSTC'14 (windows 8 + windows phone) Workshops_Day 2MSTC'14 (windows 8 + windows phone) Workshops_Day 2
MSTC'14 (windows 8 + windows phone) Workshops_Day 2
 
The Awesome Python Class Part-5
The Awesome Python Class Part-5The Awesome Python Class Part-5
The Awesome Python Class Part-5
 
Data types in python
Data types in pythonData types in python
Data types in python
 
New c sharp4_features_part_ii
New c sharp4_features_part_iiNew c sharp4_features_part_ii
New c sharp4_features_part_ii
 
Type Theory and Practical Application
Type Theory and Practical ApplicationType Theory and Practical Application
Type Theory and Practical Application
 
Intoduction to Homotopy Type Therory
Intoduction to Homotopy Type TheroryIntoduction to Homotopy Type Therory
Intoduction to Homotopy Type Therory
 
Data types in Java
Data types in JavaData types in Java
Data types in Java
 
The Awesome Python Class Part-3
The Awesome Python Class Part-3The Awesome Python Class Part-3
The Awesome Python Class Part-3
 

Similar to Subtyping

Real World Haskell: Lecture 3
Real World Haskell: Lecture 3Real World Haskell: Lecture 3
Real World Haskell: Lecture 3
Bryan O'Sullivan
 
Mdst3703 2013-09-10-textual-signals
Mdst3703 2013-09-10-textual-signalsMdst3703 2013-09-10-textual-signals
Mdst3703 2013-09-10-textual-signals
Rafael Alvarado
 

Similar to Subtyping (20)

From DOT to Dotty
From DOT to DottyFrom DOT to Dotty
From DOT to Dotty
 
Real World Haskell: Lecture 3
Real World Haskell: Lecture 3Real World Haskell: Lecture 3
Real World Haskell: Lecture 3
 
A Featherweight Approach to FOOL
A Featherweight Approach to FOOLA Featherweight Approach to FOOL
A Featherweight Approach to FOOL
 
Xtext: type checking and scoping
Xtext: type checking and scopingXtext: type checking and scoping
Xtext: type checking and scoping
 
Objects of Value
Objects of ValueObjects of Value
Objects of Value
 
Substitutability
SubstitutabilitySubstitutability
Substitutability
 
SOLID Deconstruction
SOLID DeconstructionSOLID Deconstruction
SOLID Deconstruction
 
Deep Learning for Search
Deep Learning for SearchDeep Learning for Search
Deep Learning for Search
 
Type Systems
Type SystemsType Systems
Type Systems
 
SOLID Deconstruction
SOLID DeconstructionSOLID Deconstruction
SOLID Deconstruction
 
01. design pattern
01. design pattern01. design pattern
01. design pattern
 
DTS s03e04 Typing
DTS s03e04 TypingDTS s03e04 Typing
DTS s03e04 Typing
 
Entity Linking
Entity LinkingEntity Linking
Entity Linking
 
Systemic Functional Grammar
Systemic Functional Grammar Systemic Functional Grammar
Systemic Functional Grammar
 
20170113 julia’s type system and multiple dispatch
20170113 julia’s type system and multiple dispatch20170113 julia’s type system and multiple dispatch
20170113 julia’s type system and multiple dispatch
 
Mdst3703 2013-09-10-textual-signals
Mdst3703 2013-09-10-textual-signalsMdst3703 2013-09-10-textual-signals
Mdst3703 2013-09-10-textual-signals
 
Kotlin generics
Kotlin genericsKotlin generics
Kotlin generics
 
Object? You Keep Using that Word
Object? You Keep Using that WordObject? You Keep Using that Word
Object? You Keep Using that Word
 
Object-Oriented Programming in Java (Module 1)
Object-Oriented Programming in Java (Module 1)Object-Oriented Programming in Java (Module 1)
Object-Oriented Programming in Java (Module 1)
 
Framework Design Guidelines
Framework Design GuidelinesFramework Design Guidelines
Framework Design Guidelines
 

Recently uploaded

Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 bAsymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Sérgio Sacani
 
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune WaterworldsBiogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Sérgio Sacani
 
development of diagnostic enzyme assay to detect leuser virus
development of diagnostic enzyme assay to detect leuser virusdevelopment of diagnostic enzyme assay to detect leuser virus
development of diagnostic enzyme assay to detect leuser virus
NazaninKarimi6
 
Module for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learningModule for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learning
levieagacer
 
Porella : features, morphology, anatomy, reproduction etc.
Porella : features, morphology, anatomy, reproduction etc.Porella : features, morphology, anatomy, reproduction etc.
Porella : features, morphology, anatomy, reproduction etc.
Cherry
 
Phenolics: types, biosynthesis and functions.
Phenolics: types, biosynthesis and functions.Phenolics: types, biosynthesis and functions.
Phenolics: types, biosynthesis and functions.
Cherry
 
Reboulia: features, anatomy, morphology etc.
Reboulia: features, anatomy, morphology etc.Reboulia: features, anatomy, morphology etc.
Reboulia: features, anatomy, morphology etc.
Cherry
 

Recently uploaded (20)

module for grade 9 for distance learning
module for grade 9 for distance learningmodule for grade 9 for distance learning
module for grade 9 for distance learning
 
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 bAsymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
Asymmetry in the atmosphere of the ultra-hot Jupiter WASP-76 b
 
Gwalior ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Gwalior ESCORT SERVICE❤CALL GIRL
Gwalior ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Gwalior ESCORT SERVICE❤CALL GIRLGwalior ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Gwalior ESCORT SERVICE❤CALL GIRL
Gwalior ❤CALL GIRL 84099*07087 ❤CALL GIRLS IN Gwalior ESCORT SERVICE❤CALL GIRL
 
Human & Veterinary Respiratory Physilogy_DR.E.Muralinath_Associate Professor....
Human & Veterinary Respiratory Physilogy_DR.E.Muralinath_Associate Professor....Human & Veterinary Respiratory Physilogy_DR.E.Muralinath_Associate Professor....
Human & Veterinary Respiratory Physilogy_DR.E.Muralinath_Associate Professor....
 
Genetics and epigenetics of ADHD and comorbid conditions
Genetics and epigenetics of ADHD and comorbid conditionsGenetics and epigenetics of ADHD and comorbid conditions
Genetics and epigenetics of ADHD and comorbid conditions
 
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune WaterworldsBiogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
Biogenic Sulfur Gases as Biosignatures on Temperate Sub-Neptune Waterworlds
 
development of diagnostic enzyme assay to detect leuser virus
development of diagnostic enzyme assay to detect leuser virusdevelopment of diagnostic enzyme assay to detect leuser virus
development of diagnostic enzyme assay to detect leuser virus
 
Factory Acceptance Test( FAT).pptx .
Factory Acceptance Test( FAT).pptx       .Factory Acceptance Test( FAT).pptx       .
Factory Acceptance Test( FAT).pptx .
 
Site Acceptance Test .
Site Acceptance Test                    .Site Acceptance Test                    .
Site Acceptance Test .
 
Use of mutants in understanding seedling development.pptx
Use of mutants in understanding seedling development.pptxUse of mutants in understanding seedling development.pptx
Use of mutants in understanding seedling development.pptx
 
Role of AI in seed science Predictive modelling and Beyond.pptx
Role of AI in seed science  Predictive modelling and  Beyond.pptxRole of AI in seed science  Predictive modelling and  Beyond.pptx
Role of AI in seed science Predictive modelling and Beyond.pptx
 
Cyanide resistant respiration pathway.pptx
Cyanide resistant respiration pathway.pptxCyanide resistant respiration pathway.pptx
Cyanide resistant respiration pathway.pptx
 
Module for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learningModule for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learning
 
Porella : features, morphology, anatomy, reproduction etc.
Porella : features, morphology, anatomy, reproduction etc.Porella : features, morphology, anatomy, reproduction etc.
Porella : features, morphology, anatomy, reproduction etc.
 
FAIRSpectra - Enabling the FAIRification of Analytical Science
FAIRSpectra - Enabling the FAIRification of Analytical ScienceFAIRSpectra - Enabling the FAIRification of Analytical Science
FAIRSpectra - Enabling the FAIRification of Analytical Science
 
Phenolics: types, biosynthesis and functions.
Phenolics: types, biosynthesis and functions.Phenolics: types, biosynthesis and functions.
Phenolics: types, biosynthesis and functions.
 
Reboulia: features, anatomy, morphology etc.
Reboulia: features, anatomy, morphology etc.Reboulia: features, anatomy, morphology etc.
Reboulia: features, anatomy, morphology etc.
 
Early Development of Mammals (Mouse and Human).pdf
Early Development of Mammals (Mouse and Human).pdfEarly Development of Mammals (Mouse and Human).pdf
Early Development of Mammals (Mouse and Human).pdf
 
Thyroid Physiology_Dr.E. Muralinath_ Associate Professor
Thyroid Physiology_Dr.E. Muralinath_ Associate ProfessorThyroid Physiology_Dr.E. Muralinath_ Associate Professor
Thyroid Physiology_Dr.E. Muralinath_ Associate Professor
 
Terpineol and it's characterization pptx
Terpineol and it's characterization pptxTerpineol and it's characterization pptx
Terpineol and it's characterization pptx
 

Subtyping

  • 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
  • 4. Rules / Properties Reflexivity S <: S Subsumption Transitivity Width and Depth
  • 5. Record Permutation since record fields are projected out thus should be insensitive to order
  • 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)
  • 7. Subtyping on functions “Don’t surprise the functions”
  • 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.“
  • 12.
  • 13. Messy With polymorphism and reference, things run out of control really really fast ! Java Example
  • 14. java.lang.Object is not really Top (Slides from prof. Wolfgang) (from SEFM course)
  • 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)