Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming...

63
Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai [email protected] BASICS Lab, Shanghai Jiao Tong University Fall, 2016

Transcript of Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming...

Page 1: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Types and Programming Languages

Lecture 7. Subtyping

Xiaojuan Cai

[email protected]

BASICS Lab, Shanghai Jiao Tong University

Fall, 2016

Page 2: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Motivation

T-AppΓ ` t1 : T1 → T2 Γ ` t2 : T1

Γ ` t1t2 : T2

Then(λr : {x : Nat}. r .x) {x = 0, y = 1} 6−→

How to make this well-behaved term reducible?

Page 3: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Motivation

T-AppΓ ` t1 : T1 → T2 Γ ` t2 : T1

Γ ` t1t2 : T2

Then(λr : {x : Nat}. r .x) {x = 0, y = 1} 6−→

How to make this well-behaved term reducible?

Page 4: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Outline

Subtype relation

Subtyping and other features

Metatheory of subtyping

Page 5: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Subtyping

I Subtyping is found in object-oriented languages and is anessential feature of the object-oriented style.

I Subtype S <: T : type S are more informative than T .

I Principle of safe substitution. If S <: T, then any term of typeS can safely be used in a context where a term of type T isexpected.

I We can simply consider <: as ⊆.

Page 6: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Subtyping

I Subtyping is found in object-oriented languages and is anessential feature of the object-oriented style.

I Subtype S <: T : type S are more informative than T .

I Principle of safe substitution. If S <: T, then any term of typeS can safely be used in a context where a term of type T isexpected.

I We can simply consider <: as ⊆.

Page 7: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Subtyping

I Subtyping is found in object-oriented languages and is anessential feature of the object-oriented style.

I Subtype S <: T : type S are more informative than T .

I Principle of safe substitution. If S <: T, then any term of typeS can safely be used in a context where a term of type T isexpected.

I We can simply consider <: as ⊆.

Page 8: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Subtyping

I Subtyping is found in object-oriented languages and is anessential feature of the object-oriented style.

I Subtype S <: T : type S are more informative than T .

I Principle of safe substitution. If S <: T, then any term of typeS can safely be used in a context where a term of type T isexpected.

I We can simply consider <: as ⊆.

Page 9: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Subtype rules

T-SubΓ ` t : S S <: T

Γ ` t : T

Quiz. Write rules to make <: reflexive and transitive.

S-ReflS <: S

S-TranS <: T T <: U

S <: U

Page 10: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Subtype rules

T-SubΓ ` t : S S <: T

Γ ` t : T

Quiz. Write rules to make <: reflexive and transitive.

S-ReflS <: S

S-TranS <: T T <: U

S <: U

Page 11: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Subtype rules

T-SubΓ ` t : S S <: T

Γ ` t : T

Quiz. Write rules to make <: reflexive and transitive.

S-ReflS <: S

S-TranS <: T T <: U

S <: U

Page 12: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Subtype rules for records

{li : Tii∈1..n}

Quiz. What rules should we add to generate the following subtyperelations?

I {x : Nat, y : nat} <: {x : Nat}I {x : {a : Nat, b : nat}, y : Nat} <: {x : {a : Nat}, y : Nat}I {x : Nat, y : nat} <: {y : Nat}

S-RcdWidth{li : Ti

i∈1..n+k} <: {li : Tii∈1..n}

S-RcdDepthforeach i ∈ 1..n Si <: Ti

{li : Sii∈1..n} <: {li : Ti

i∈1..n}

S-RcdPerm{kj : Sj

j∈1..n}is a permutation of {li : Tii∈1..n}

{kj : Sjj∈1..n} <: {li : Ti

i∈1..n}

Page 13: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Subtype rules for records

{li : Tii∈1..n}

Quiz. What rules should we add to generate the following subtyperelations?

I {x : Nat, y : nat} <: {x : Nat}I {x : {a : Nat, b : nat}, y : Nat} <: {x : {a : Nat}, y : Nat}I {x : Nat, y : nat} <: {y : Nat}

S-RcdWidth{li : Ti

i∈1..n+k} <: {li : Tii∈1..n}

S-RcdDepthforeach i ∈ 1..n Si <: Ti

{li : Sii∈1..n} <: {li : Ti

i∈1..n}

S-RcdPerm{kj : Sj

j∈1..n}is a permutation of {li : Tii∈1..n}

{kj : Sjj∈1..n} <: {li : Ti

i∈1..n}

Page 14: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Back to the motivation example

Quiz. Come back to the motivation example:

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

Please draw its type derivation tree.

Page 15: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Arrow type

Give the subtyping rule for arrow type T1 → T2?

Examples.

Page 16: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Arrow type

Give the subtyping rule for arrow type T1 → T2?Examples.

Page 17: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Arrow type

Give the subtyping rule for arrow type T1 → T2.Examples.

Page 18: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Arrow type

Give the subtyping rule for arrow type T1 → T2.Examples.

Page 19: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Arrow type

Give the subtyping rule for arrow type T1 → T2.Examples.

S-ArrowT1 <: S1 S2 <: T2S1 → S2 <: T1 → T2

Page 20: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Arrow type

Give the subtyping rule for arrow type T1 → T2.Examples.

S-ArrowT1 <: S1 S2 <: T2S1 → S2 <: T1 → T2

Page 21: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Top and Bot

A final rule: Any type has a supertype top.

S <: top

The maximal type Top > is included in most presentations:

I It corresponds to the type Object.

I It is convenient technical device for combining subtypes andparametric polymorphism.

What about the minimal type Bot ⊥?

S-Bot ⊥ <: T

I Bot is empty.

I Bot is useful: We can give error : ⊥ and make this termwell-typed: λx:T. if x then t else error

Page 22: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Top and Bot

A final rule: Any type has a supertype top.

S <: top

The maximal type Top > is included in most presentations:

I It corresponds to the type Object.

I It is convenient technical device for combining subtypes andparametric polymorphism.

What about the minimal type Bot ⊥?

S-Bot ⊥ <: T

I Bot is empty.

I Bot is useful: We can give error : ⊥ and make this termwell-typed: λx:T. if x then t else error

Page 23: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Top and Bot

A final rule: Any type has a supertype top.

S <: top

The maximal type Top > is included in most presentations:

I It corresponds to the type Object.

I It is convenient technical device for combining subtypes andparametric polymorphism.

What about the minimal type Bot ⊥?

S-Bot ⊥ <: T

I Bot is empty.

I Bot is useful: We can give error : ⊥ and make this termwell-typed: λx:T. if x then t else error

Page 24: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Properties of subtyping

Theorem 15.3.5 [Preservation]: If Γ ` t : T and t −→ t ′, thenΓ ` t ′ : T.

Thoerem 15.3.7 [Progress]: If t is a closed, well-typed term,then either t is a value or else there is some t with t −→ t.

Quiz. Try to predict where difficulties might arise to prove typesafety of subtyping.

Page 25: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Properties of subtyping

Theorem 15.3.5 [Preservation]: If Γ ` t : T and t −→ t ′, thenΓ ` t ′ : T.

Thoerem 15.3.7 [Progress]: If t is a closed, well-typed term,then either t is a value or else there is some t with t −→ t.

Quiz. Try to predict where difficulties might arise to prove typesafety of subtyping.

Page 26: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Progress

Lemma 15.3.2 [Inversion of the subytpe relaiton]:

I If S <: T1 → T2, then S has the form S1 → S2 and T1 <: S1and S2 <: T2;

I If S <: {li : T i∈1..ni }, then S has the form {kj : S j∈1..m

j } and

{l i∈1..ni } ⊆ {k j∈1..mj } and for every common label li = kjSj <: Ti

Lemma 15.3.3

I if Γ ` λx : S1. s2 : T1 → T2, then T1 <: S1 andΓ, x : S1 ` s2 : T2.

I If Γ ` {ka = sa∈1..ma } : {li : T i∈1..ni }, then

{l i∈1..ni } ⊆ {ka∈1..ma } and Γ ` sa : Ti for each common labelka = li .

Lemma 15.3.4 [Substitution] If Γ, x : S ` t : T and Γ ` s : S ,then Γ ` [x 7→ s]t : T .

Page 27: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Progress

Lemma 15.3.2 [Inversion of the subytpe relaiton]:

I If S <: T1 → T2, then S has the form S1 → S2 and T1 <: S1and S2 <: T2;

I If S <: {li : T i∈1..ni }, then S has the form {kj : S j∈1..m

j } and

{l i∈1..ni } ⊆ {k j∈1..mj } and for every common label li = kjSj <: Ti

Lemma 15.3.3

I if Γ ` λx : S1. s2 : T1 → T2, then T1 <: S1 andΓ, x : S1 ` s2 : T2.

I If Γ ` {ka = sa∈1..ma } : {li : T i∈1..ni }, then

{l i∈1..ni } ⊆ {ka∈1..ma } and Γ ` sa : Ti for each common labelka = li .

Lemma 15.3.4 [Substitution] If Γ, x : S ` t : T and Γ ` s : S ,then Γ ` [x 7→ s]t : T .

Page 28: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Progress

Lemma 15.3.2 [Inversion of the subytpe relaiton]:

I If S <: T1 → T2, then S has the form S1 → S2 and T1 <: S1and S2 <: T2;

I If S <: {li : T i∈1..ni }, then S has the form {kj : S j∈1..m

j } and

{l i∈1..ni } ⊆ {k j∈1..mj } and for every common label li = kjSj <: Ti

Lemma 15.3.3

I if Γ ` λx : S1. s2 : T1 → T2, then T1 <: S1 andΓ, x : S1 ` s2 : T2.

I If Γ ` {ka = sa∈1..ma } : {li : T i∈1..ni }, then

{l i∈1..ni } ⊆ {ka∈1..ma } and Γ ` sa : Ti for each common labelka = li .

Lemma 15.3.4 [Substitution] If Γ, x : S ` t : T and Γ ` s : S ,then Γ ` [x 7→ s]t : T .

Page 29: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Outline

Subtype relation

Subtyping and other features

Metatheory of subtyping

Page 30: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Ascription and cast

Review

I The ascription operator t as T allows the programmer torecord some subterm of a complex expression has someparticular type.

T-AscribeΓ ` t : T

Γ ` t as T : T

I Up-cast: a term is ascribed a supertype of the type.

I Down-cast: a term is ascribed a subtype of the type.

A surprising rule for down-cast

T-DowncastΓ ` t : S

Γ ` t as T : T

Page 31: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Ascription and cast

Review

I The ascription operator t as T allows the programmer torecord some subterm of a complex expression has someparticular type.

T-AscribeΓ ` t : T

Γ ` t as T : T

I Up-cast: a term is ascribed a supertype of the type.

I Down-cast: a term is ascribed a subtype of the type.

A surprising rule for down-cast

T-DowncastΓ ` t : S

Γ ` t as T : T

Page 32: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Ascription and cast

Review

I The ascription operator t as T allows the programmer torecord some subterm of a complex expression has someparticular type.

T-AscribeΓ ` t : T

Γ ` t as T : T

I Up-cast: a term is ascribed a supertype of the type.

I Down-cast: a term is ascribed a subtype of the type.

A surprising rule for down-cast

T-DowncastΓ ` t : S

Γ ` t as T : T

Page 33: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Downcast

T-DowncastΓ ` t : S

Γ ` t as T : T

I f = λ(x : Top) .((x as {a : Nat}).a)

I ”I know that f will always be applied to record argumentswith numeric a fields; I want you to trust me on this one.”

I Our motto is ”trust, but verify”.

I We need dynamic type test:

E-Downcast` v1 : T

v1 as T −→ v1

Quiz. With T-Downcast, E-Downcast, which property willlost, preservation or progress? Progress will lost.

Page 34: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Downcast

T-DowncastΓ ` t : S

Γ ` t as T : T

I f = λ(x : Top) .((x as {a : Nat}).a)

I ”I know that f will always be applied to record argumentswith numeric a fields; I want you to trust me on this one.”

I Our motto is ”trust, but verify”.

I We need dynamic type test:

E-Downcast` v1 : T

v1 as T −→ v1

Quiz. With T-Downcast, E-Downcast, which property willlost, preservation or progress? Progress will lost.

Page 35: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Downcast

T-DowncastΓ ` t : S

Γ ` t as T : T

I f = λ(x : Top) .((x as {a : Nat}).a)

I ”I know that f will always be applied to record argumentswith numeric a fields; I want you to trust me on this one.”

I Our motto is ”trust, but verify”.

I We need dynamic type test:

E-Downcast` v1 : T

v1 as T −→ v1

Quiz. With T-Downcast, E-Downcast, which property willlost, preservation or progress? Progress will lost.

Page 36: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Downcast

T-DowncastΓ ` t : S

Γ ` t as T : T

I f = λ(x : Top) .((x as {a : Nat}).a)

I ”I know that f will always be applied to record argumentswith numeric a fields; I want you to trust me on this one.”

I Our motto is ”trust, but verify”.

I We need dynamic type test:

E-Downcast` v1 : T

v1 as T −→ v1

Quiz. With T-Downcast, E-Downcast, which property willlost, preservation or progress?

Progress will lost.

Page 37: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Downcast

T-DowncastΓ ` t : S

Γ ` t as T : T

I f = λ(x : Top) .((x as {a : Nat}).a)

I ”I know that f will always be applied to record argumentswith numeric a fields; I want you to trust me on this one.”

I Our motto is ”trust, but verify”.

I We need dynamic type test:

E-Downcast` v1 : T

v1 as T −→ v1

Quiz. With T-Downcast, E-Downcast, which property willlost, preservation or progress? Progress will lost.

Page 38: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Subtyping for variants

Quiz. Try to give a subtyping rule like S-RcdWidth for varianttype.

S-VarWidth〈li : Ti

i∈1..n〉 <: 〈li : Tii∈1..n+k〉

S-VarDepthforeach i ∈ 1..n Si <: Ti

〈li : Sii∈1..n〉 <: 〈li : Ti

i∈1..n〉

S-VarPerm〈kj : Sj

j∈1..n〉 is a permutation of 〈li : Tii∈1..n〉

〈kj : Sjj∈1..n〉 <: 〈li : Ti

i∈1..n〉

ListsS-List

S1 <: S2list S1 <: list S2

Page 39: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Subtyping for variants

Quiz. Try to give a subtyping rule like S-RcdWidth for varianttype.

S-VarWidth〈li : Ti

i∈1..n〉 <: 〈li : Tii∈1..n+k〉

S-VarDepthforeach i ∈ 1..n Si <: Ti

〈li : Sii∈1..n〉 <: 〈li : Ti

i∈1..n〉

S-VarPerm〈kj : Sj

j∈1..n〉 is a permutation of 〈li : Tii∈1..n〉

〈kj : Sjj∈1..n〉 <: 〈li : Ti

i∈1..n〉

ListsS-List

S1 <: S2list S1 <: list S2

Page 40: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Subtyping for variants

Quiz. Try to give a subtyping rule like S-RcdWidth for varianttype.

S-VarWidth〈li : Ti

i∈1..n〉 <: 〈li : Tii∈1..n+k〉

S-VarDepthforeach i ∈ 1..n Si <: Ti

〈li : Sii∈1..n〉 <: 〈li : Ti

i∈1..n〉

S-VarPerm〈kj : Sj

j∈1..n〉 is a permutation of 〈li : Tii∈1..n〉

〈kj : Sjj∈1..n〉 <: 〈li : Ti

i∈1..n〉

ListsS-List

S1 <: S2list S1 <: list S2

Page 41: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

References

Quiz.

S-Ref?

Ref S1 <: Ref T1

Not all type constructors are covariant or contravariant.

S-RefS1 <: T1 T1 <: S1Ref S1 <: Ref T1

Array is similar to Ref type. So we have

S-ArrayS1 <: T1 T1 <: S1

Array S1 <: Array T1

Java actually permits covariant subtyping of arrays

S-ArrayS1 <: T1

Array S1 <: Array T1

Page 42: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

References

Quiz.

S-Ref?

Ref S1 <: Ref T1

Not all type constructors are covariant or contravariant.

S-RefS1 <: T1 T1 <: S1Ref S1 <: Ref T1

Array is similar to Ref type. So we have

S-ArrayS1 <: T1 T1 <: S1

Array S1 <: Array T1

Java actually permits covariant subtyping of arrays

S-ArrayS1 <: T1

Array S1 <: Array T1

Page 43: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

References

Quiz.

S-Ref?

Ref S1 <: Ref T1

Not all type constructors are covariant or contravariant.

S-RefS1 <: T1 T1 <: S1Ref S1 <: Ref T1

Array is similar to Ref type. So we have

S-ArrayS1 <: T1 T1 <: S1

Array S1 <: Array T1

Java actually permits covariant subtyping of arrays

S-ArrayS1 <: T1

Array S1 <: Array T1

Page 44: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Refined references

More refined reference type [by Reynolds] is by introducing twonew type constructors, Source and Sink.

I Source T is for reading values of type T.

I Sink T is for writing values of type T.

Γ,Σ ` t1 : Source T1

Γ,Σ ` !t1 : T1

Γ,Σ ` t1 : Sink T1, Γ, Σ ` t2 : T1Γ,Σ ` t1:=t2 : unit

S1 <: T1Souce S1 <: Souce T1

T1 <: S1Sink S1 <: Sink T1

Ref T1 <: Souce T1 Ref T1 <: Sink T1

In Pict, the input channel is Source type and the output channelis Sink type.

Page 45: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Outline

Subtype relation

Subtyping and other features

Metatheory of subtyping

Page 46: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Towards implementatoin

I Subtype relation is not immediately suitable forimplementation. It is not syntax directed.

I Subtype cannot just be ”read from bottom to top” to yield atypechecker:

I T-Abs applies only to abstractions, T-Var applies only tovaraibles, etc. However, T-Sub can be applied to any term t.

T-SubΓ ` t : S S <: T

Γ ` t : T

I S-Trans requires to guess a T.

S-TranS <: T T <: U

S <: U

I Coming soon: Replacing the subtyping and typing relation bytwo new relations: algorithmic subtyping and algorithmictyping, whose inference rules are syntax directed.

Page 47: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Towards implementatoin

I Subtype relation is not immediately suitable forimplementation. It is not syntax directed.

I Subtype cannot just be ”read from bottom to top” to yield atypechecker:

I T-Abs applies only to abstractions, T-Var applies only tovaraibles, etc.

However, T-Sub can be applied to any term t.

T-SubΓ ` t : S S <: T

Γ ` t : T

I S-Trans requires to guess a T.

S-TranS <: T T <: U

S <: U

I Coming soon: Replacing the subtyping and typing relation bytwo new relations: algorithmic subtyping and algorithmictyping, whose inference rules are syntax directed.

Page 48: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Towards implementatoin

I Subtype relation is not immediately suitable forimplementation. It is not syntax directed.

I Subtype cannot just be ”read from bottom to top” to yield atypechecker:

I T-Abs applies only to abstractions, T-Var applies only tovaraibles, etc. However, T-Sub can be applied to any term t.

T-SubΓ ` t : S S <: T

Γ ` t : T

I S-Trans requires to guess a T.

S-TranS <: T T <: U

S <: U

I Coming soon: Replacing the subtyping and typing relation bytwo new relations: algorithmic subtyping and algorithmictyping, whose inference rules are syntax directed.

Page 49: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Towards implementatoin

I Subtype relation is not immediately suitable forimplementation. It is not syntax directed.

I Subtype cannot just be ”read from bottom to top” to yield atypechecker:

I T-Abs applies only to abstractions, T-Var applies only tovaraibles, etc. However, T-Sub can be applied to any term t.

T-SubΓ ` t : S S <: T

Γ ` t : T

I S-Trans requires to guess a T.

S-TranS <: T T <: U

S <: U

I Coming soon: Replacing the subtyping and typing relation bytwo new relations: algorithmic subtyping and algorithmictyping, whose inference rules are syntax directed.

Page 50: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Towards implementatoin

I Subtype relation is not immediately suitable forimplementation. It is not syntax directed.

I Subtype cannot just be ”read from bottom to top” to yield atypechecker:

I T-Abs applies only to abstractions, T-Var applies only tovaraibles, etc. However, T-Sub can be applied to any term t.

T-SubΓ ` t : S S <: T

Γ ` t : T

I S-Trans requires to guess a T.

S-TranS <: T T <: U

S <: U

I Coming soon: Replacing the subtyping and typing relation bytwo new relations: algorithmic subtyping and algorithmictyping, whose inference rules are syntax directed.

Page 51: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Subtype relation

The first calculus we study is simply typed λ-calculus withsubtyping and records. (No booleans, nats).

S-ReflS <: S

S-TranS <: T T <: U

S <: US-Top

S <: Top

S-ArrowT1 <: S1 S2 <: T2S1 → S2 <: T1 → T2

S-Rcd{li∈1..ni } ⊆ {kj∈1..mj } and kj = li implies Sj <: Ti

{kj : Sj∈1..mj } <: {li : Ti∈1..ni }

We compact S-RcdWidth, S-RcdDepth, S-RcdPerm intoone rule S-Rcd.

Page 52: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Algorithmic subtyping

Lemma 16.1.2.

I S<:S can be derived for every type S without using S-Refl.

I If S<:T is derivable, then it can be derived without usingS-Trans.

Algorithmic subtyping

SA-Top|= S <: Top

SA-Arrow|= T1 <: S1 |= S2 <: T2

|= S1 → S2 <: T1 → T2

SA-Rcd{li∈1..ni } ⊆ {kj∈1..mj } and kj = li implies |= Sj <: Ti

|= {kj : Sj∈1..mj } <: {li : Ti∈1..ni }

Page 53: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Algorithmic subtyping

Lemma 16.1.2.

I S<:S can be derived for every type S without using S-Refl.

I If S<:T is derivable, then it can be derived without usingS-Trans.

Algorithmic subtyping

SA-Top|= S <: Top

SA-Arrow|= T1 <: S1 |= S2 <: T2

|= S1 → S2 <: T1 → T2

SA-Rcd{li∈1..ni } ⊆ {kj∈1..mj } and kj = li implies |= Sj <: Ti

|= {kj : Sj∈1..mj } <: {li : Ti∈1..ni }

Page 54: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Correctness of SA-rules

Proposition 16.1.5 [Soundness and Completeness].S<:T iff |= S<:T

An algorithm for subtyping:

subtype(S, T) = if T = top then true

else if S = S1 → S2 and T = T1 → T2

then subtype(T1, S1) ∧ subtype(S2, T2)

else if S = {kj : Sj∈1..mj } and T = {li : Ti∈1..ni }

then {li∈1..ni } ⊆ {kj∈1..mj }∧ for all i there is some j ∈ 1..m with kj = li

and subtype(Sj, Ti)

Proposition 16.1.16 [Termination]. If |= S <: T is derivable,then subtype(S, T) will return true. If not, then it will return false.

Page 55: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Correctness of SA-rules

Proposition 16.1.5 [Soundness and Completeness].S<:T iff |= S<:T

An algorithm for subtyping:

subtype(S, T) = if T = top then true

else if S = S1 → S2 and T = T1 → T2

then subtype(T1, S1) ∧ subtype(S2, T2)

else if S = {kj : Sj∈1..mj } and T = {li : Ti∈1..ni }

then {li∈1..ni } ⊆ {kj∈1..mj }∧ for all i there is some j ∈ 1..m with kj = li

and subtype(Sj, Ti)

Proposition 16.1.16 [Termination]. If |= S <: T is derivable,then subtype(S, T) will return true. If not, then it will return false.

Page 56: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Correctness of SA-rules

Proposition 16.1.5 [Soundness and Completeness].S<:T iff |= S<:T

An algorithm for subtyping:

subtype(S, T) = if T = top then true

else if S = S1 → S2 and T = T1 → T2

then subtype(T1, S1) ∧ subtype(S2, T2)

else if S = {kj : Sj∈1..mj } and T = {li : Ti∈1..ni }

then {li∈1..ni } ⊆ {kj∈1..mj }∧ for all i there is some j ∈ 1..m with kj = li

and subtype(Sj, Ti)

Proposition 16.1.16 [Termination]. If |= S <: T is derivable,then subtype(S, T) will return true. If not, then it will return false.

Page 57: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Algorithmic typing

I The only rule we need to consider is T-Sub:

T-SubΓ ` t : S S <: T

Γ ` t : T

I Can we remove it as we remove S-Trans in subtypingrelation?

I No. The term (λr : {x : Nat}. r .x) {x = 0, y = 1} is nottypable without T-Sub.

I However, we can alway transform the derivation to the onewhere T-Sub only appears before T-App or at the very end.

Page 58: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Algorithmic typing

I The only rule we need to consider is T-Sub:

T-SubΓ ` t : S S <: T

Γ ` t : T

I Can we remove it as we remove S-Trans in subtypingrelation?

I No. The term (λr : {x : Nat}. r .x) {x = 0, y = 1} is nottypable without T-Sub.

I However, we can alway transform the derivation to the onewhere T-Sub only appears before T-App or at the very end.

Page 59: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Algorithmic typing

I The only rule we need to consider is T-Sub:

T-SubΓ ` t : S S <: T

Γ ` t : T

I Can we remove it as we remove S-Trans in subtypingrelation?

I No. The term (λr : {x : Nat}. r .x) {x = 0, y = 1} is nottypable without T-Sub.

I However, we can alway transform the derivation to the onewhere T-Sub only appears before T-App or at the very end.

Page 60: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Algorithmic typing

Instead of T-Sub, we add

TA-AppΓ |= t1 : T1 → T2 Γ |= t2 : S1 |= S1 <: T1

Γ |= t1 t2 : T2

Theorem 16.2.4 [Soundness]. If Γ |= t : T , then Γ ` t : T .

Theorem 16.2.5 [Completeness]. If Γ ` t : T , then Γ |= t : S forsome S <: T .

Page 61: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Algorithmic typing

Instead of T-Sub, we add

TA-AppΓ |= t1 : T1 → T2 Γ |= t2 : S1 |= S1 <: T1

Γ |= t1 t2 : T2

Theorem 16.2.4 [Soundness]. If Γ |= t : T , then Γ ` t : T .

Theorem 16.2.5 [Completeness]. If Γ ` t : T , then Γ |= t : S forsome S <: T .

Page 62: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Conclusion

I Subtype relation can be founded in many OO languages.

I We do not have any evaluation rules for subtyping, instead,we use subsumption rule:

T-SubΓ ` t : S S <: T

Γ ` t : T

I Some features, such as downcasts, may break type safety.

Page 63: Types and Programming Languages - SJTUxiaojuan/tapl2016/files/lec7.pdf · Types and Programming Languages Lecture 7. Subtyping Xiaojuan Cai cxj@sjtu.edu.cn BASICS Lab, Shanghai Jiao

Homework

I 15.2.1, 15.2.2, 15.2.3, 15.2.4, 15.2.5, 16.2.3