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

Post on 04-Jul-2020

10 views 0 download

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

Types and Programming Languages

Lecture 7. Subtyping

Xiaojuan Cai

cxj@sjtu.edu.cn

BASICS Lab, Shanghai Jiao Tong University

Fall, 2016

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?

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?

Outline

Subtype relation

Subtyping and other features

Metatheory of subtyping

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 ⊆.

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 ⊆.

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 ⊆.

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 ⊆.

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

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

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

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}

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}

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.

Arrow type

Give the subtyping rule for arrow type T1 → T2?

Examples.

Arrow type

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

Arrow type

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

Arrow type

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

Arrow type

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

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

Arrow type

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

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

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

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

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

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.

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.

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 .

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 .

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 .

Outline

Subtype relation

Subtyping and other features

Metatheory of subtyping

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

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

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

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.

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.

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.

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.

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.

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

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

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

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

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

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

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.

Outline

Subtype relation

Subtyping and other features

Metatheory of subtyping

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.

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.

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.

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.

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.

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.

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 }

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 }

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.

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.

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.

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.

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.

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.

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 .

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 .

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.

Homework

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