26.4 Safety

 < Free Open Study > 



26.4 Safety

The type preservation property can be established quite directly for both the kernel and full variants of F<:. We give proofs in detail here for kernel F<:; the argument for full F<: is very similar. When we consider subtyping and type-checking algorithms in Chapter 28, however, the two variants will turn out to be more different than the basic arguments in this chapter might suggest. We will find many points where the full system is much more complex to analyze than the kernel system, or indeed where the full system lacks useful properties (including decidable typechecking!) enjoyed by the kernel system.

We begin with some preliminary technical facts about the typing and sub-type relations. Their proofs proceed by routine induction on derivations.

26.4.1 Lemma [Permutation]

Suppose that Г is a well-formed context and that Δ is a permutation of Гthat is, Δ has the same bindings as Г, and their ordering in Δ preserves the scopes of type variables from Г, in the sense that, if one binding in Г introduces a type variable that is mentioned in another binding further to the right, then these bindings appear in the same order in Δ.

  1. If Г t : T, then Δ t : T.

  2. If Г S <: T, then Δ S <: T.

26.4.2 Lemma [Weakening]

  1. If Г t : T and Г, x:U is well formed, then Г x:U t : T.

  2. If Г t : T and Г, X<:U is well formed, then Г, X<:U t : T.

  3. If Г S <: T and Г, x:U is well formed, then Г, x:U S <: T.

  4. If Г S <: T and Г, X<:U is well formed, then Г, X<:U S <: T.

26.4.3 Exercise []

Where does the proof of weakening rely on permutation?

26.4.4 Lemma [Strengthening for Term Variables in Subtyping Derivations]

If Г, x:T, Δ S <: T, then Г, Δ S <: T.

Proof: Obvious: typing assumptions play no role in subtype derivations.

As usual, the proof of type preservation relies on several lemmas relating substitution with the typing and subtype relations.

26.4.5 Lemma [Narrowing]

  1. If Г, X<:Q, Δ S <: T and Г P <: Q, then Г, X<:P, Δ S <: T.

  2. If Г, X<:Q, Δ t : T and Г P <: Q, then Г, X<:P, Δ t : T.

These properties are often called narrowing because they involve restricting (narrowing) the range of the variable X.

Proof: Exercise [].

Next, we have the usual lemma relating substitution and the typing relation.

26.4.6 Lemma [Substitution Preserves Typing]

If Г, x:Q, Δ t : T and Г q : Q, then Г, Δ [x q]t : T.

Proof: Induction on a derivation of Г, x:Q, Δ t : T, using the properties above.

Since we may substitute types for type variables during reduction, we also need a lemma relating type substitution and typing. The proof of this lemma (specifically, the T-SUB case) depends on a new lemma relating substitution and subtyping.

26.4.7 Definition

We write [X S]Г for the context obtained by substituting S for X in the right-hand sides of all of the bindings in Г.

26.4.8 Lemma [Type Substitution Preserves Subtyping]

If Г, X<:Q, Δ S <: T and Г P <: Q, then Г, [X P] Δ [X P]S <: [X P]T.

Note that we need to substitute for X only in the part of the environment that follows the binding of X, since our conventions about scoping require that the types to the left of the binding of X do not contain X.

Proof: By induction on a derivation of Г, X<:Q, Δ S <: T. The only interesting cases are the last two:

Case S-TVAR:

S = Y

Y<:T Î (Г, X<:Q, Δ)

There are two subcases to consider. If Y X, then the result is immediate from S-TVAR. On the other hand, if Y = X, then we have T = Q and [X P]S = Q, so the result follows by S-REFL.

Case S-ALL:

S = "Z<:U1 .S2

T = "Z<:U1 .T2

 

Г, X<:Q, Δ, Z<:U1 S2 <: T2

By the induction hypothesis, Г, [X P]Δ, Z<:[X P]U1 [X P]S2 <: [X P]T2. By S-ALL, Г, [X P]Δ "Z<:[X P]U1 .[X P]S2 <: "Z<:[X P]U1 .[X P]T2, that is, Г, [X P]Δ [X P]("Z<:U1 .S2) <: [X P]("Z<:U1 .T2), as required.

A similar lemma relates type substitution and typing.

26.4.9 Lemma [Type Substitution Preserves Typing]

If Г, X<:Q, Δ t : T and Г P <: Q, then Г, [X P]Δ [X P]t : [X P]T.

Proof: By induction on a derivation of Г, X<:Q, Δ t : T. We give just the interesting cases.

Case T-TAPP:

t = t1 [T2]

Г, X<:Q, Δ t1 : "Z<:T11 . T12

 

T = [Z T2]T12

 

By the induction hypothesis, Г, [X P]Δ [X P]t1 : [X P]("Z<:T11 .T12), i.e, Г, [X P]Δ [X P]t1 : "Z<:T11 .[X P]T12. By T-TAPP, Г, [X P]Δ [X P]t1 [[X P]T2] : [Z [X P]T2]([X P]T12), i.e., Г, [X P]Δ [X P](t1 [T2]) : [X P]([Z T2]T12).

Case T-SUB:

Г, X<:Q, Δ t : SГ, X<:Q, Δ S <:T

By the induction hypothesis, Г, [X P] Δ [X P]t : [X P]T. By the preservation of subtyping under substitution (Lemma 26.4.8), we have Г, [X P]Δ [X P]S <: [X P]T, and the result follows by T-SUB.

Next, we establish some simple structural properties of subtyping.

26.4.10 Lemma [Inversion of the Subtype Relation, From Right to Left]

  1. If Г S <: X, then S is a type variable.

  2. If Г S <: T1 T2, then either S is a type variable or else S has the form S1 S2, with Г T1 <: S1 and Г S2 <: T2.

  3. If Г S <: "X<:U1 . T2, then either S is a type variable or else S has the form "X<:U1 .S2 with Г, X<:U1 S2 <: T2.

Proof: Part (1) is an easy induction on subtyping derivations. The only interesting case is the rule S-TRANS, which proceeds by two uses of the induction hypothesis, first on the right premise and then on the left premise. The arguments for the other parts are similar, using part (1) in the transitivity cases.

26.4.11 Exercise [Recommended, ⋆⋆]

Show the following "left to right inversion" properties:

  1. If Г S1 S2 <: T, then either T = Top or else T = T1 T2 with Г T1 <: S1 and Г S2 <: T2.

  2. If Г "X<:U.S2 <: T, then either T = Top or else T = "X<:U.T2 with Г, X<:U ↦; S2 <: T2.

  3. If Г X <: T, then either T = Top or T = X or Г S <: T with X<:S Î Г.

  4. If Г Top <: T, then T = Top.

Lemma 26.4.10 is used, in turn, to establish one straightforward structural property of the typing relation that is needed in the critical cases of the type preservation proof.

26.4.12 Lemma

  1. If Г λx:S1 .s2 : T and Г T <: U1 U2, then Г U1 <: S1 and there is some S2 such that Г, x:S1 s2 : S2 and Г S2 <: U2.

  2. If Г λX<:S1 .s2 : T and Г T <: "X<:U1 .U2, then U1 = S1 and there is some S2 such that Г, X<:S1 s2 : S2 and Г, X<:S1 S2 <: U2.

Proof: Straightforward induction on typing derivations, using Lemma 26.4.10 for the induction case (rule T-SUB).

With these facts in hand, the proof of type preservation is straightforward.

26.4.13 Theorem [Preservation]

If Г t : T and t t, then Г t : T.

Proof: By induction on a derivation of Г t : T. All of the cases are straightforward, using the facts established above.

Case T-VAR, T-ABS, T-TABS:

t = x,

t = λx:T1 .t2,

or

t = λX<:U.t

These case cannot actually arise, since we assumed t t and there are no evaluation rules for variables, abstractions, or type abstractions.

Case T-APP:

t = t1 t2

Г t1 : T11T12

T = T12

Г t2 : T11

By the definition of the evaluation relation, there are three subcases:

  •  

    Subcase:

    Then the result follows from the induction hypothesis and T-APP.

    Subcase:

    t1 is a value

    Ditto.

    Subcase:

    t1 = λx:U11 .u12

    t = [x t2]u12

    By Lemma 26.4.12, Г, x:U11 u12 : U12 for some U12 with Г T11 <: U11 and Г U12 <: T12. By the preservation of typing under substitution (Lemma 26.4.6), Г [x t2]u12 : U12, from which we obtain Г [x t2]u12 : T12 by T-SUB.

Case T-TAPP:

t = t1 [T2]

Г t : "X<:T11 .T12

 

T = [X T2]T12

Г T2 <: T11

By the definition of the evaluation relation, there are two subcases:

  •  

    Subcase:

    The result follows from the induction hypothesis and T-TAPP.

    Subcase:

    t1 = λX<:U11 .u12

    t = [X T2]u12

    By Lemma 26.4.12, U11 = T11 and Г, X<:U11 u12 : U12 with Г, X<:U11 U12 <: T12. By the preservation of typing under substitution (Lemma 26.4.6), Г [X T2]u12 : [X T2]U12, from which Г [X T2]u12 : [X T2]T12 follows by Lemma 26.4.8 and T-SUB.

Case T-SUB:

Г t : S

Г S <:T

By the induction hypothesis, Г t : S, and the result follows by T-SUB.

The progress theorem for F<: is a straightforward extension of the one for the simply typed lambda-calculus with subtyping. As always, we begin by recording a canonical forms property telling us the possible shapes of closed values of arrow and quantifier types.

26.4.14 Lemma [Canonical Forms]

  1. If v is a closed value of type T1 T2, then v has the form λx:S1 .t2.

  2. If v is a closed value of type "X<:T1 .T2, then v has the form λX<:T1 .t2.

Proof: Both parts proceed by induction on typing derivations; we give the argument just for the second part. By inspection of the typing rules, it is clear that the final rule in a derivation of v : "X<:T1 .T2 must be either T-TABS or T-SUB. If it is T-TABS, then the desired result is immediate from the premise of the rule. So suppose the last rule is T-SUB. From the premises of this rule, we have v : S and S <: "X<:T1 .T2. From the inversion lemma (26.4.10), we know that S has the form "X<:T1 S2. The result now follows from the induction hypothesis.

With this in hand, the proof of progress is straightforward.

26.4.15 Theorem [Progress]

If t is a closed, well-typed F<: term, then either t is a value or else there is some t with t t.

Proof: By induction on typing derivations. The variable case cannot occur because t is closed. The two cases for lambda-abstractions are immediate, since both term and type abstractions are values. The cases for application, type application, and subsumption are more interesting; we show just the latter two (term application is similar to type application).

Case T-TAPP:

t = t1 [T2]

t1 : "X<:T11 .T12

 

Г T2 <: T11

T = [X T2]T2

By the induction hypothesis, either t1 is a value or else it can make a step of evaluation. If t1 can take a step, then rule E-TAPP1 applies to t. Otherwise, if t1 is a value, then part (2) of the canonical forms lemma (26.4.14) tells us that t1 has the form λX<:T11 .t12, so rule E-TAPPTABS applies to t.

Case T-SUB:

Г t : S

Г S <: T

The result follows directly from the induction hypothesis.

26.4.16 Exercise [⋆⋆⋆ ]

Extend the argument in this section to full F<:.



 < Free Open Study > 



Types and Programming Languages
Types and Programming Languages
ISBN: 0262162091
EAN: 2147483647
Year: 2002
Pages: 262

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net