type systems
play

Type Systems Lecture 5 Nov. 17th, 2004 Sebastian Maneth - PowerPoint PPT Presentation

Type Systems Lecture 5 Nov. 17th, 2004 Sebastian Maneth http://lampwww.epfl.ch/teaching/typeSystems/2004 Today: 1. Subtyping (functions and records) 2. Algorithmic Subtyping 3. Joins and Meets 1. Subtyping (functions and records) (


  1. Type Systems Lecture 5 Nov. 17th, 2004 Sebastian Maneth http://lampwww.epfl.ch/teaching/typeSystems/2004

  2. Today: 1. Subtyping (functions and records) 2. Algorithmic Subtyping 3. Joins and Meets

  3. 1. Subtyping (functions and records) ( λ r : { x: Nat } . r . x) { x=0, y=1}

  4. 1. Subtyping (functions and records) ( λ r : { x: Nat } . r . x) { x=0, y=1} this function can ONLY be applied to ill-typed! records of the type {x:Nat} � the type system is WAY too strict! too much slack! Actually, the function can be applied to ANY record that has at least the field x:Nat!

  5. 1. Subtyping (functions and records) ( λ r : { x: Nat } . r . x) { x=0, y=1} this function can ONLY be applied to ill-typed! records of the type {x:Nat} � the type system is WAY too strict! too much slack! Actually, the function can be applied to ANY record that has at least the field x:Nat! a subtype of {x:Nat} Why “ sub ”? {x:Nat,y:Nat} is a subtype, written <: of {x:Nat} because # of records having this is LESS than # of records having this

  6. 1. Subtyping (functions and records) {x:Nat,y:Nat} <: {x:Nat} (“is subtype of”)

  7. 1. Subtyping (functions and records) {x:Nat,y:Nat} <: {x:Nat} (“is subytpe of”) S T � If t satisfies S in some context, then it also satisfies T!!

  8. 1. Subtyping (functions and records) {x:Nat,y:Nat} <: {x:Nat} (“is subytpe of”) S T � If t satisfies S in some context, then it also satisfies T!! is of type is of type Γ ` t : S S <: T Rule of subsumtion: Γ ` t : T … = “move to a more general type (supertype)”

  9. 1. Subtyping (functions and records) Rules for the Subtype Relation <: S <: U U <: T reflexive, transitive: S <: S S <: T “top type”: S <: Top f � function f : S 1 S 2

  10. 1. Subtyping (functions and records) Rules for the Subtype Relation <: S <: U U <: T reflexive, transitive: S <: S S <: T “top type”: S <: Top f T 1 T 2 � function f : S 1 S 2

  11. 1. Subtyping (functions and records) Rules for the Subtype Relation <: S <: U U <: T reflexive, transitive: S <: S S <: T “top type”: S <: Top f T 1 T 2 � function f : S 1 S 2 T 1 <: S 1 S 2 <: T 2 S 1 � S 2 <: T 1 � T 2

  12. 1. Subtyping (functions and records) Rules for the Subtype Relation <: S <: U U <: T reflexive, transitive: S <: S S <: T “top type”: S <: Top f T 1 T 2 � function f : S 1 S 2 contravariant T 1 <: S 1 S 2 <: T 2 covariant in argument in result S 1 � S 2 <: T 1 � T 2

  13. 1. Subtyping (functions and records) Rules for the Subtype Relation <: S <: U U <: T reflexive, transitive: S <: S S <: Top S <: T records: T 1 <: S 1 S 2 <: T 2 S 1 � S 2 <: T 1 � T 2 { l 1 : T 1 , … , l n+k : T n+k } <: { l 1 : T 1 , … , l n : T n } (forget fields) for each i S i <: T i (subtype inside of record) { l 1 : S 1 , … , l n : S n } <: { l 1 : T 1 , … , l n : T n } { k 1 : S 1 , … , k n : S n } is permutation of { l 1 : T 1 , … , l n : T n } (don’t care { k 1 : S 1 , … , k n : S n } <: { l 1 : T 1 , … , l n : T n } about order)

  14. 1. Subtyping (functions and records) ( λ r : { x: Nat } . r . x) { x=0, y=1} ill-typed! (in simply lambda) Can we type this now, using subtyping? ` ( λ r : { x: Nat } . r . x) { x=0, y=1} `

  15. 1. Subtyping (functions and records) ( λ r : { x: Nat } . r . x) { x=0, y=1} ill-typed! (in simply lambda) Can we type this now, using subtyping? ` ( λ r : { x: Nat } . r . x) : { x: Nat } � Nat ` ` { x=0, y=1} : { x: Nat } ` ` ( λ r : { x: Nat } . r . x) { x=0, y=1} `

  16. 1. Subtyping (functions and records) ( λ r : { x: Nat } . r . x) { x=0, y=1} ill-typed! (in simply lambda) Can we type this now, using subtyping? ` ` { x=0, y=1} : { x: Nat , y: Nat } { x: Nat , y: Nat } <: { x: Nat } not derivable before NOW: use subsumption ! ` ( λ r : { x: Nat } . r . x) : { x: Nat } � Nat ` ` { x=0, y=1} : { x: Nat } ` ` ( λ r : { x: Nat } . r . x) { x=0, y=1} `

  17. 1. Subtyping (functions and records) ( λ r : { x: Nat } . r . x) { x=0, y=1} ill-typed! (in simply lambda) Can we type this now, using subtyping? OK! (forget fields rule) ` ` { x=0, y=1} : { x: Nat , y: Nat } { x: Nat , y: Nat } <: { x: Nat } not derivable before NOW: use subsumption ! ` ( λ r : { x: Nat } . r . x) : { x: Nat } � Nat ` ` { x=0, y=1} : { x: Nat } ` ` ( λ r : { x: Nat } . r . x) { x=0, y=1} `

  18. 1. Subtyping (functions and records) ( λ r : { x: Nat } . r . x) { x=0, y=1} ill-typed! (in simply lambda) Can we type this now, using subtyping? ` 0: Nat ` ` ` 1: Nat OK! (forget fields rule) ` ` { x=0, y=1} : { x: Nat , y: Nat } { x: Nat , y: Nat } <: { x: Nat } not derivable before NOW: use subsumption ! ` ( λ r : { x: Nat } . r . x) : { x: Nat } � Nat ` ` { x=0, y=1} : { x: Nat } ` ` ( λ r : { x: Nat } . r . x) { x=0, y=1} `

  19. 1. Subtyping (functions and records) ( λ r : { x: Nat } . r . x) { x=0, y=1} ill-typed! (in simply lambda) Can we type this now, using subtyping? OK! ` ( λ r : { x: Nat } . r . x) : { x: Nat } � Nat ` ` { x=0, y=1} : { x: Nat } ` ` ( λ r : { x: Nat } . r . x) { x=0, y=1} `

  20. 1. Subtyping (functions and records) ( λ r : { x: Nat } . r . x) { x=0, y=1} ill-typed! (in simply lambda) Can we type this now, using subtyping? ` ` r . x: Nat r : { x: Nat } OK! ` ( λ r : { x: Nat } . r . x) : { x: Nat } � Nat ` ` { x=0, y=1} : { x: Nat } ` ` ( λ r : { x: Nat } . r . x) { x=0, y=1} `

  21. 1. Subtyping (functions and records) ( λ r : { x: Nat } . r . x) { x=0, y=1} ill-typed! (in simply lambda) Can we type this now, using subtyping? ` ` r : { x: Nat } r : { x: Nat } ` ` r . x: Nat r : { x: Nat } OK! ` ( λ r : { x: Nat } . r . x) : { x: Nat } � Nat ` { x=0, y=1} : { x: Nat } ` ` ` ( λ r : { x: Nat } . r . x) { x=0, y=1} `

  22. 1. Subtyping (functions and records) ( λ r : { x: Nat } . r . x) { x=0, y=1} ill-typed! (in simply lambda) Can we type this now, using subtyping? OK! ∈ r : { x: Nat } r : { x: Nat } ` ` r : { x: Nat } r : { x: Nat } ` r . x: Nat ` r : { x: Nat } OK! ` ( λ r : { x: Nat } . r . x) : { x: Nat } � Nat ` ` { x=0, y=1} : { x: Nat } ` ` ( λ r : { x: Nat } . r . x) { x=0, y=1} `

  23. 2. Algorithmic Subtyping Problematic rules for implementing subtyping: S <: U U <: T Are NOT syntax-directed!! S <: T � When to apply them?? S <: S Γ ` t : S S <: T Γ ` t : T

  24. 2. Algorithmic Subtyping Problematic rules for implementing subtyping: S <: U U <: T � needed to merge subtyping derivation of records S <: T to see this, find a derivation for S <: S { x: { a: Nat , b: Nat } , y: { m : Nat } } <: { x: { a: Nat } } Γ ` t : S S <: T Γ ` t : T

  25. 2. Algorithmic Subtyping Problematic rules for implementing subtyping: � needed to merge subtyping derivation of S <: U U <: T records S <: T new: , k n : S n } ⊆ { l 1 : T 1 , … { k 1 : S 1 , … , l m : T m } S <: S k j =l i i m pl i es S j <: T i Γ ` t : S S <: T { k 1 : S 1 , … , k n : S n } <: { l 1 : T 1 , … , l m : T m } Γ ` t : T

  26. 2. Algorithmic Subtyping Problematic rules for implementing subtyping: � needed to merge subtyping derivation of S <: U U <: T records S <: T new: , k n : S n } ⊆ { l 1 : T 1 , … { k 1 : S 1 , … , l m : T m } S <: S k j =l i i m pl i es S j <: T i Γ ` t : S S <: T { k 1 : S 1 , … , k n : S n } <: { l 1 : T 1 , … , l m : T m } Γ ` t : T � ONLY needed for fu. application!

  27. 2. Algorithmic Subtyping Problematic rules for implementing subtyping: � needed to merge subtyping derivation of S <: U U <: T records S <: T new: , k n : S n } ⊆ { l 1 : T 1 , … { k 1 : S 1 , … , l m : T m } S <: S k j =l i i m pl i es S j <: T i Γ ` t : S S <: T { k 1 : S 1 , … , k n : S n } <: { l 1 : T 1 , … , l m : T m } Γ ` t : T new: � Γ ` t 1 :T 1 � T 2 Γ ` t 2 : R R <: T 1 ONLY needed for fu. Γ ` application! t 1 t 2 : T 2

  28. 2. Algorithmic Subtyping Implementing subtyping: T 1 <: S 1 S 2 <: T 2 S <: U U <: T S 1 � S 2 <: T 1 � T 2 S <: T , k n : S n } ⊆ { l 1 : T 1 , … { k 1 : S 1 , … , l m : T m } S <: S k j =l i i m pl i es S j <: T i Γ ` t : S S <: T { k 1 : S 1 , … , k n : S n } <: { l 1 : T 1 , … , l m : T m } Γ ` t : T Γ ` t 1 :T 1 � T 2 Γ ` t 2 : R R <: T 1 Γ ` t 1 t 2 : T 2

  29. 2. Algorithmic Subtyping Implementing subtyping: T 1 <: S 1 S 2 <: T 2 S <: U U <: T S 1 � S 2 <: T 1 � T 2 S <: T , k n : S n } ⊆ { l 1 : T 1 , … { k 1 : S 1 , … , l m : T m } S <: S k j =l i i m pl i es S j <: T i Γ ` t : S S <: T { k 1 : S 1 , … , k n : S n } <: { l 1 : T 1 , … , l m : T m } Γ ` t : T Γ ` t 1 :T 1 � T 2 Γ ` t 2 : R R <: T 1 Γ ` t 1 t 2 : T 2 AND: S <: S for every base type S.

  30. 2. Algorithmic Subtyping ( ° ) Call the rules used for implementation “algorithmic typing” � are these rules sound and complete w.r.t. the previous rules ( ` )??

Recommend


More recommend