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

type systems
SMART_READER_LITE
LIVE PREVIEW

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) (


slide-1
SLIDE 1

Type Systems

Lecture 5 Nov. 17th, 2004 Sebastian Maneth

http://lampwww.epfl.ch/teaching/typeSystems/2004

slide-2
SLIDE 2

Today:

  • 1. Subtyping (functions and records)
  • 2. Algorithmic Subtyping
  • 3. Joins and Meets
slide-3
SLIDE 3
  • 1. Subtyping (functions and records)

( λr : { x: Nat } . r . x) { x=0, y=1}

slide-4
SLIDE 4
  • 1. Subtyping (functions and records)

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

slide-5
SLIDE 5
  • 1. Subtyping (functions and records)

( λr : { x: Nat } . r . x) { x=0, y=1} ill-typed! this function can ONLY be applied to records of the type {x:Nat} Actually, the function can be applied to ANY record that has at least the field x:Nat! the type system is WAY too strict! too much slack! 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

slide-6
SLIDE 6
  • 1. Subtyping (functions and records)

{x:Nat,y:Nat} <: {x:Nat} (“is subtype of”)

slide-7
SLIDE 7
  • 1. Subtyping (functions and records)

{x:Nat,y:Nat} <: {x:Nat} (“is subytpe of”) If t satisfies S in some context, then it also satisfies T!! S T

slide-8
SLIDE 8
  • 1. Subtyping (functions and records)

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

slide-9
SLIDE 9
  • 1. Subtyping (functions and records)

Rules for the Subtype Relation <: reflexive, transitive: S <: S S <: U U <: T S <: T “top type”: S <: Top function f : S1

  • S2

f

slide-10
SLIDE 10
  • 1. Subtyping (functions and records)

Rules for the Subtype Relation <: reflexive, transitive: S <: S S <: U U <: T S <: T “top type”: S <: Top function f : S1

  • S2

f T1 T2

slide-11
SLIDE 11
  • 1. Subtyping (functions and records)

Rules for the Subtype Relation <: reflexive, transitive: S <: S S <: U U <: T S <: T “top type”: S <: Top function f : S1

  • S2

f S1 S2 <: T1 T2 T1 <: S1 S2 <: T2 T1 T2

slide-12
SLIDE 12
  • 1. Subtyping (functions and records)

Rules for the Subtype Relation <: reflexive, transitive: S <: S S <: U U <: T S <: T “top type”: S <: Top function f : S1

  • S2

f contravariant in argument covariant in result S1 S2 <: T1 T2 T1 <: S1 S2 <: T2 T1 T2

slide-13
SLIDE 13
  • 1. Subtyping (functions and records)

Rules for the Subtype Relation <: reflexive, transitive: S <: S S <: U U <: T S <: T S1 S2 <: T1 T2 T1 <: S1 S2 <: T2 records: { l 1: T1, … , l n+k: Tn+k} <: { l 1: T1, … , l n: Tn} (forget fields) for each i Si <: Ti { l 1: S1, … , l n: Sn} <: { l 1: T1, … , l n: Tn} S <: Top (subtype inside of record) { k 1: S1, … , k n: Sn} is permutation of { l 1: T1, … , l n: Tn} { k 1: S1, … , k n: Sn} <: { l 1: T1, … , l n: Tn} (don’t care about order)

slide-14
SLIDE 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}

slide-15
SLIDE 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=0, y=1} ` ` ( λr : { x: Nat } . r . x) : { x: Nat } Nat ` ` { x=0, y=1} : { x: Nat }

slide-16
SLIDE 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? ` ` ( λr : { x: Nat } . r . x) { x=0, y=1} ` ` ( λr : { x: Nat } . r . x) : { x: Nat } Nat ` ` { x=0, y=1} : { x: Nat } not derivable before NOW: use subsumption ! ` ` { x=0, y=1} : { x: Nat , y: Nat } { x: Nat , y: Nat } <: { x: Nat }

slide-17
SLIDE 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? ` ` ( λr : { x: Nat } . r . x) { x=0, y=1} ` ` ( λr : { x: Nat } . r . x) : { x: Nat } Nat ` ` { x=0, y=1} : { x: Nat } not derivable before NOW: use subsumption ! ` ` { x=0, y=1} : { x: Nat , y: Nat } { x: Nat , y: Nat } <: { x: Nat } OK! (forget fields rule)

slide-18
SLIDE 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? ` ` ( λr : { x: Nat } . r . x) { x=0, y=1} ` ` ( λr : { x: Nat } . r . x) : { x: Nat } Nat ` ` { x=0, y=1} : { x: Nat } not derivable before NOW: use subsumption ! ` ` { x=0, y=1} : { x: Nat , y: Nat } { x: Nat , y: Nat } <: { x: Nat } OK! (forget fields rule) ` ` 0: Nat ` ` 1: Nat

slide-19
SLIDE 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? ` ` ( λr : { x: Nat } . r . x) { x=0, y=1} ` ` ( λr : { x: Nat } . r . x) : { x: Nat } Nat ` ` { x=0, y=1} : { x: Nat } OK!

slide-20
SLIDE 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) { x=0, y=1} ` ` ( λr : { x: Nat } . r . x) : { x: Nat } Nat ` ` { x=0, y=1} : { x: Nat } OK! r : { x: Nat } ` ` r . x: Nat

slide-21
SLIDE 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) { x=0, y=1} ` ` ( λr : { x: Nat } . r . x) : { x: Nat } Nat ` ` { x=0, y=1} : { x: Nat } OK! r : { x: Nat } ` ` r . x: Nat r : { x: Nat } ` ` r : { x: Nat }

slide-22
SLIDE 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? ` ` ( λr : { x: Nat } . r . x) { x=0, y=1} ` ` ( λr : { x: Nat } . r . x) : { x: Nat } Nat ` ` { x=0, y=1} : { x: Nat } OK! r : { x: Nat } ` ` r . x: Nat r : { x: Nat } ` ` r : { x: Nat } r : { x: Nat } ∈ r : { x: Nat } OK!

slide-23
SLIDE 23
  • 2. Algorithmic Subtyping

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

slide-24
SLIDE 24
  • 2. Algorithmic Subtyping

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

slide-25
SLIDE 25
  • 2. Algorithmic Subtyping

Problematic rules for implementing subtyping: Γ ` t : S S <: T Γ ` t : T S <: U U <: T S <: T S <: S needed to merge subtyping derivation of records { k 1: S1, … , k n: Sn} ⊆ { l 1: T1, … , l m : Tm } k j =l i i m pl i es Sj <: Ti { k 1: S1, … , k n: Sn} <: { l 1: T1, … , l m : Tm } new:

slide-26
SLIDE 26
  • 2. Algorithmic Subtyping

Problematic rules for implementing subtyping: Γ ` t : S S <: T Γ ` t : T S <: U U <: T S <: T S <: S needed to merge subtyping derivation of records { k 1: S1, … , k n: Sn} ⊆ { l 1: T1, … , l m : Tm } k j =l i i m pl i es Sj <: Ti { k 1: S1, … , k n: Sn} <: { l 1: T1, … , l m : Tm } new: ONLY needed for fu. application!

slide-27
SLIDE 27
  • 2. Algorithmic Subtyping

Problematic rules for implementing subtyping: Γ ` t : S S <: T Γ ` t : T S <: U U <: T S <: T S <: S needed to merge subtyping derivation of records { k 1: S1, … , k n: Sn} ⊆ { l 1: T1, … , l m : Tm } k j =l i i m pl i es Sj <: Ti { k 1: S1, … , k n: Sn} <: { l 1: T1, … , l m : Tm } new: ONLY needed for fu. application!

  • new:

Γ ` t1:T1 T2 Γ ` t2 : R R <: T1 Γ ` t1 t2 : T2

slide-28
SLIDE 28
  • 2. Algorithmic Subtyping

Implementing subtyping: Γ ` t : S S <: T Γ ` t : T S <: U U <: T S <: T S <: S { k 1: S1, … , k n: Sn} ⊆ { l 1: T1, … , l m : Tm } k j =l i i m pl i es Sj <: Ti { k 1: S1, … , k n: Sn} <: { l 1: T1, … , l m : Tm } Γ ` t1:T1 T2 Γ ` t2 : R R <: T1 Γ ` t1 t2 : T2 S1 S2 <: T1 T2 T1 <: S1 S2 <: T2

slide-29
SLIDE 29
  • 2. Algorithmic Subtyping

Implementing subtyping: Γ ` t : S S <: T Γ ` t : T S <: U U <: T S <: T S <: S { k 1: S1, … , k n: Sn} ⊆ { l 1: T1, … , l m : Tm } k j =l i i m pl i es Sj <: Ti { k 1: S1, … , k n: Sn} <: { l 1: T1, … , l m : Tm } Γ ` t1:T1 T2 Γ ` t2 : R R <: T1 Γ ` t1 t2 : T2 S1 S2 <: T1 T2 T1 <: S1 S2 <: T2 AND: S <: S for every base type S.

slide-30
SLIDE 30
  • 2. Algorithmic Subtyping

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

slide-31
SLIDE 31
  • 2. Algorithmic Subtyping

Call the rules used for implementation “algorithmic typing” ( ° ) are these rules sound and complete w.r.t. the previous rules ( ` )?? Soundness: If Γ ° t : T, then Γ ` t : T YES, prove this by straightforward induction!

slide-32
SLIDE 32
  • 2. Algorithmic Subtyping

Call the rules used for implementation “algorithmic typing” ( ° ) are these rules sound and complete w.r.t. the previous rules ( ` )?? YES, prove this by straightforward induction! Completeness: If Γ ` t : T, then Γ ° t : T Soundness: If Γ ° t : T, then Γ ` t : T

slide-33
SLIDE 33
  • 2. Algorithmic Subtyping

Call the rules used for implementation “algorithmic typing” ( ° ) are these rules sound and complete w.r.t. the previous rules ( ` )?? YES, prove this by straightforward induction! NO, ` { a=0} : { a: Nat , b: Nat } , but not true for ° Soundness: If Γ ° t : T, then Γ ` t : T Completeness: If Γ ` t : T, then Γ ° t : T

slide-34
SLIDE 34
  • 2. Algorithmic Subtyping

Call the rules used for implementation “algorithmic typing” ( ° ) are these rules sound and complete w.r.t. the previous rules ( ` )?? YES, prove this by straightforward induction! Soundness: If Γ ° t : T, then Γ ` t : T Completeness: If Γ ` t : T, then Γ ° t : T

slide-35
SLIDE 35
  • 2. Algorithmic Subtyping

Call the rules used for implementation “algorithmic typing” ( ° ) are these rules sound and complete w.r.t. the previous rules ( ` )?? YES, prove this by straightforward induction! Completeness: If Γ ` t : T, then Γ ° t : S for some S <: T = “Minimal Typing Theorem” Can you prove that S is actually minimal?? Soundness: If Γ ° t : T, then Γ ` t : T

slide-36
SLIDE 36
  • 3. Joins and Meets

How to type if-then-else, in the presence of subsumtion? i f t r ue t hen { x=t r ue, y=f al se} el se { x=f al se, z=t r ue} What is the type of this term?

slide-37
SLIDE 37
  • 3. Joins and Meets

How to type if-then-else, in the presence of subsumtion? i f t r ue t hen { x=t r ue, y=f al se} el se { x=f al se, z=t r ue} What is the type of this term? maybe { x: Bool } ?

slide-38
SLIDE 38
  • 3. Joins and Meets

How to type if-then-else, in the presence of subsumtion? i f t r ue t hen { x=t r ue, y=f al se} el se { x=f al se, z=t r ue} What is the type of this term? maybe { x: Bool } ?

  • r { x: Top} ?
slide-39
SLIDE 39
  • 3. Joins and Meets

How to type if-then-else, in the presence of subsumtion? i f t r ue t hen { x=t r ue, y=f al se} el se { x=f al se, z=t r ue} What is the type of this term? maybe { x: Bool } ?

  • r { x: Top} ?
  • r { } ?
  • r Top?

<: <: <:

slide-40
SLIDE 40
  • 3. Joins and Meets

How to type if-then-else, in the presence of subsumtion? i f t r ue t hen { x=t r ue, y=f al se} el se { x=f al se, z=t r ue} What is the type of this term?

  • { x: Bool }
  • r { x: Top} ?
  • r { } ?
  • r Top?

<: <: <: take the least (most precise) common supertype of S and T

slide-41
SLIDE 41
  • 3. Joins and Meets

How to type if-then-else, in the presence of subsumtion? i f t r ue t hen { x=t r ue, y=f al se} el se { x=f al se, z=t r ue} What is the type of this term?

  • { x: Bool }
  • r { x: Top} ?
  • r { } ?
  • r Top?

<: <: <: take the least (most precise) common supertype of S and T = “the join of S and T” =: S ∨ T

slide-42
SLIDE 42
  • 3. Joins and Meets

How to type if-then-else, in the presence of subsumtion? t1 : Bool t2 : T2 t3: T3 T=T2 ∨ T3 i f t1 t hen t2 el se t3 : T

slide-43
SLIDE 43
  • 3. Joins and Meets

How to type if-then-else, in the presence of subsumtion? t1 : Bool t2 : T2 t3: T3 T=T2 ∨ T3 i f t1 t hen t2 el se t3 : T S ∨ T := Bool , if S = T = Bool { j 1: J 1, … , j q: J q} if S = {k1:S1,…,km:Sm}, T = {l1:T1,…,ln:Tn}, {j1:J1,…,jq:Jq} = S ∩ T, and Ju = Sv ∨ Tw for ju = kv = lw

slide-44
SLIDE 44
  • 3. Joins and Meets

How to type if-then-else, in the presence of subsumtion? t1 : Bool t2 : T2 t3: T3 T=T2 ∨ T3 i f t1 t hen t2 el se t3 : T S ∨ T := Bool , if S = T = Bool { j 1: J 1, … , j q: J q} if S = {k1:S1,…,km:Sm}, T = {l1:T1,…,ln:Tn}, {j1:J1,…,jq:Jq} = S ∩ T, and Ju = Sv ∨ Tw for ju = kv = lw E F, if S = A B and T = C D and E = A ∧ C and F = B ∨ D A ∧ C := meet of A and C = greatest common subtype

slide-45
SLIDE 45
  • 3. Joins and Meets

How to type if-then-else, in the presence of subsumtion? t1 : Bool t2 : T2 t3: T3 T=T2 ∨ T3 i f t1 t hen t2 el se t3 : T S ∨ T := Bool , if S = T = Bool { j 1: J 1, … , j q: J q} if S = {k1:S1,…,km:Sm}, T = {l1:T1,…,ln:Tn}, {j1:J1,…,jq:Jq} = S ∩ T, and Ju = Sv ∨ Tw for ju = kv = lw E F, if S = A B and T = C D and E = A ∧ C and F = B ∨ D Top, otherwise. A ∧ C := meet of A and C = greatest common subtype

slide-46
SLIDE 46
  • 3. Joins and Meets

How to type if-then-else, in the presence of subsumtion? t1 : Bool t2 : T2 t3: T3 T=T2 ∨ T3 i f t1 t hen t2 el se t3 : T S ∧ T := A ∧ C := meet of A and C = greatest common subtype define this in a similar way as the join! Implement subtyping for our language. First, leave if-then-else untouched. Then, add joins and meets. IN LAB TODAY: