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

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 2Amira Gamal
 
The Awesome Python Class Part-5
The Awesome Python Class Part-5The Awesome Python Class Part-5
The Awesome Python Class Part-5Binay Kumar Ray
 
New c sharp4_features_part_ii
New c sharp4_features_part_iiNew c sharp4_features_part_ii
New c sharp4_features_part_iiNico Ludwig
 
Type Theory and Practical Application
Type Theory and Practical ApplicationType Theory and Practical Application
Type Theory and Practical ApplicationJack Fox
 
Intoduction to Homotopy Type Therory
Intoduction to Homotopy Type TheroryIntoduction to Homotopy Type Therory
Intoduction to Homotopy Type TheroryJack Fox
 
Data types in Java
Data types in JavaData types in Java
Data types in JavaPlay Store
 
The Awesome Python Class Part-3
The Awesome Python Class Part-3The Awesome Python Class Part-3
The Awesome Python Class Part-3Binay Kumar Ray
 

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 3Bryan O'Sullivan
 
A Featherweight Approach to FOOL
A Featherweight Approach to FOOLA Featherweight Approach to FOOL
A Featherweight Approach to FOOLgreenwop
 
Xtext: type checking and scoping
Xtext: type checking and scopingXtext: type checking and scoping
Xtext: type checking and scopingMikhail Barash
 
SOLID Deconstruction
SOLID DeconstructionSOLID Deconstruction
SOLID DeconstructionKevlin Henney
 
Deep Learning for Search
Deep Learning for SearchDeep Learning for Search
Deep Learning for SearchBhaskar Mitra
 
SOLID Deconstruction
SOLID DeconstructionSOLID Deconstruction
SOLID DeconstructionKevlin Henney
 
DTS s03e04 Typing
DTS s03e04 TypingDTS s03e04 Typing
DTS s03e04 TypingTuenti
 
Systemic Functional Grammar
Systemic Functional Grammar Systemic Functional Grammar
Systemic Functional Grammar Sugeng Hariyanto
 
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-signalsRafael Alvarado
 
Kotlin generics
Kotlin genericsKotlin generics
Kotlin genericsLinusMuema
 
Object? You Keep Using that Word
Object? You Keep Using that WordObject? You Keep Using that Word
Object? You Keep Using that WordKevlin Henney
 
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)muhammadmubinmacadad2
 
Framework Design Guidelines
Framework Design GuidelinesFramework Design Guidelines
Framework Design GuidelinesMohamed Meligy
 

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

NAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdf
NAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdfNAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdf
NAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdfWadeK3
 
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.aasikanpl
 
Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)PraveenaKalaiselvan1
 
Isotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoIsotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoSérgio Sacani
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Sérgio Sacani
 
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfBehavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfSELF-EXPLANATORY
 
Spermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatidSpermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatidSarthak Sekhar Mondal
 
Work, Energy and Power for class 10 ICSE Physics
Work, Energy and Power for class 10 ICSE PhysicsWork, Energy and Power for class 10 ICSE Physics
Work, Energy and Power for class 10 ICSE Physicsvishikhakeshava1
 
Disentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTDisentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTSérgio Sacani
 
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...anilsa9823
 
Natural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsNatural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsAArockiyaNisha
 
Luciferase in rDNA technology (biotechnology).pptx
Luciferase in rDNA technology (biotechnology).pptxLuciferase in rDNA technology (biotechnology).pptx
Luciferase in rDNA technology (biotechnology).pptxAleenaTreesaSaji
 
Scheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxScheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxyaramohamed343013
 
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.aasikanpl
 
VIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C PVIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C PPRINCE C P
 
G9 Science Q4- Week 1-2 Projectile Motion.ppt
G9 Science Q4- Week 1-2 Projectile Motion.pptG9 Science Q4- Week 1-2 Projectile Motion.ppt
G9 Science Q4- Week 1-2 Projectile Motion.pptMAESTRELLAMesa2
 
Boyles law module in the grade 10 science
Boyles law module in the grade 10 scienceBoyles law module in the grade 10 science
Boyles law module in the grade 10 sciencefloriejanemacaya1
 
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCRStunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCRDelhi Call girls
 

Recently uploaded (20)

NAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdf
NAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdfNAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdf
NAVSEA PEO USC - Unmanned & Small Combatants 26Oct23.pdf
 
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Munirka Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
 
Engler and Prantl system of classification in plant taxonomy
Engler and Prantl system of classification in plant taxonomyEngler and Prantl system of classification in plant taxonomy
Engler and Prantl system of classification in plant taxonomy
 
Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)Recombinant DNA technology (Immunological screening)
Recombinant DNA technology (Immunological screening)
 
Isotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoIsotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on Io
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
 
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdfBehavioral Disorder: Schizophrenia & it's Case Study.pdf
Behavioral Disorder: Schizophrenia & it's Case Study.pdf
 
Spermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatidSpermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatid
 
Work, Energy and Power for class 10 ICSE Physics
Work, Energy and Power for class 10 ICSE PhysicsWork, Energy and Power for class 10 ICSE Physics
Work, Energy and Power for class 10 ICSE Physics
 
Disentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTDisentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOST
 
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow 💋 Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
 
Natural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsNatural Polymer Based Nanomaterials
Natural Polymer Based Nanomaterials
 
Luciferase in rDNA technology (biotechnology).pptx
Luciferase in rDNA technology (biotechnology).pptxLuciferase in rDNA technology (biotechnology).pptx
Luciferase in rDNA technology (biotechnology).pptx
 
Scheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docxScheme-of-Work-Science-Stage-4 cambridge science.docx
Scheme-of-Work-Science-Stage-4 cambridge science.docx
 
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
Call Girls in Mayapuri Delhi 💯Call Us 🔝9953322196🔝 💯Escort.
 
VIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C PVIRUSES structure and classification ppt by Dr.Prince C P
VIRUSES structure and classification ppt by Dr.Prince C P
 
The Philosophy of Science
The Philosophy of ScienceThe Philosophy of Science
The Philosophy of Science
 
G9 Science Q4- Week 1-2 Projectile Motion.ppt
G9 Science Q4- Week 1-2 Projectile Motion.pptG9 Science Q4- Week 1-2 Projectile Motion.ppt
G9 Science Q4- Week 1-2 Projectile Motion.ppt
 
Boyles law module in the grade 10 science
Boyles law module in the grade 10 scienceBoyles law module in the grade 10 science
Boyles law module in the grade 10 science
 
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCRStunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
Stunning ➥8448380779▻ Call Girls In Panchshil Enclave Delhi NCR
 

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)