15.3 Properties of Subtyping and Typing

 < Free Open Study > 



15.3 Properties of Subtyping and Typing

Having decided on the definition of the lambda-calculus with subtyping, we now have some work to do to verify that it makes sensein particular, that the preservation and progress theorems of the simply typed lambda-calculus continue to hold in the presence of subtyping.

15.3.1 Exercise [Recommended, ⋆⋆]

Before reading on, try to predict where difficulties might arise. In particular, suppose we had made a mistake in defining the subtype relation and included a bogus subtyping rule in addition to those above. Which properties of the system can fail? On the other hand, suppose we omit one of the subtyping rulescan any properties then break?

We begin by recording one key property of the subtype relationan analog of the inversion lemma for the typing relation in the simply typed lambda-calculus (Lemma 9.3.1). If we know that some type S is a subtype of an arrow type, then the subtyping inversion lemma tells us that S itself must be an arrow type; moreover, it tells us that the left-hand sides of the arrows must be (contravariantly) related, and so (covariantly) must the right-hand sides. Similar considerations apply when S is known to be a subtype of a record type: we know that S has more fields (S-RCDWIDTH) in some order (S-RCDPERM), and that the types of common fields are in the subtype relation (S-RCDDEPTH).

15.3.2 Lemma [Inversion of the Subtype Relation]

  1. If S <: T1 T2, then S has the form S1 S2, with T1 <: S1 and S2 <: T2.

  2. If S <: {li:TiiÎ1..n}, then S has the form {kj :SjjÎ1..m}, with at least the labels {liiÎ1..n}i.e., {liiÎ1..n} {kjjÎ1..m}and with Sj <: Ti for each common label li = kj.

Proof: EXERCISE [RECOMMENDED, ⋆⋆].

To prove that types are preserved during evaluation, we begin with an inversion lemma for the typing relation (cf. Lemma 9.3.1 for the simply typed lambda-calculus). Rather than stating the lemma in its most general form, we give here just the cases that are actually needed in the proof of the preservation theorem below. (The general form can be read off from the algorithmic subtype relation in the next chapter, Definition 16.2.2.)

15.3.3 Lemma

  1. If Г λx:S1. s2 : T1 T2, then T1 <: S1 and Г, x:S1 S2 :T2.

  2. If Г {ka=saaÎ1..m} : {li:TiiÎ1..n}, then {liiÎ1..n} {kaaÎ1..m} and Г sa : Ti for each common label ka = li.

Proof: Straightforward induction on typing derivations, using Lemma 15.3.2 for the T-SUB case.

Next, we need a substitution lemma for the typing relation. The statement of this lemma is unchanged from the simply typed lambda-calculus (Lemma 9.3.8), and its proof is nearly identical.

15.3.4 Lemma [Substitution]

If Г, x:S t : T and Г s : S, then Г [x s]t : T.

Proof: By induction on typing derivations. We need new cases for T-SUB and for the record typing rules T-RCD and T-PROJ, making straightforward use of the induction hypothesis. The rest is just like the proof of 9.3.8.

Now, the preservation theorem has the same statement as before. Its proof, though, is somewhat complicated by subtyping at several points.

15.3.5 Theorem [Preservation]

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

Proof: Straightforward induction on typing derivations. Most of the cases are similar to the proof of preservation for the simply typed lambda-calculus (9.3.9). We need new cases for the record typing rules and for subsumption.

Case T-VAR:

 t = x 

Can't happen (there are no evaluation rules for variables).

Case T-ABS:

 t = λx:T1 .t2 

Can't happen (t is already a value).

Case T-APP:

 t = t1 t2    Г  t1 : T11T12    Г  t2 : T11     T = T12 

From the evaluation rules in Figures 15-1 and 15-2, we see that there are three rules by which t t can be derived: E-App1, E-App2, and E-AppAbs. Proceed by cases.

  •  

    Subcase E-APP1:

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

    Subcase E-App2:

    Similar.

    Subcase E-APPABS:

     t1 = λx:S11 . t12    t2 = v2    t = [x  v2]t12 

    By Lemma 15.3.3(1), T11 <: S11 and Г,x:S11 t12 : T12. By T-SUB, Г t2 : S11. From this and the substitution lemma (15.3.4), we obtain Г t : T12.

Case T-RCD:

 t = {li=ti iÎ l..n}     Г  ti : Ti   for each i T = {li : Ti iÎ l..n} 

The only evaluation rule whose left-hand side is a record is E-RCD. From the premise of this rule, we see that for some field tj. The result follows from the induction hypothesis (applied to the corresponding assumption Г tj : Tj) and T-RCD.

Case T-PROJ:

 t = t1 .lj     Г  t1 : {li : Ti iÎ l..n}    T = Tj 

From the evaluation rules in Figures 15-1 and 15-2, we see that there are two rules by which t t can be derived: E-PROJ, E-PROJRCD.

  •  

    Subcase E-PROJ:

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

    Subcase E-PROJRCD:

     t1 = {ka=va aÎ l..m}    lj = kb    t = vb 

    BY Lemma 15.3.3(2), we have {li iÎ l..n} {ka aÎ l..m} and Г va : Ti for each ka = li. In particular, Г vb : Tj, as desired.

Case T-SUB:

 t : S    S <: T 

By the induction hypothesis, Г t : S. By T-SUB, Г t : T.

To prove that well-typed terms cannot get stuck, we begin (as in Chapter 9) with a canonical forms lemma, which tells us the possible shapes of values belonging to arrow and record types.

15.3.6 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 {li :T i iÎ l..n}, then v has the form {kj =vj aÎ l..m}, with {li iÎ l..n} {ka aÎ l..m}.

Proof: EXERCISE [RECOMMENDED, ⋆⋆⋆].

The progress theorem and its proof are now quite close to what we saw in the simply typed lambda-calculus. Most of the burden of dealing with subtyping has been pushed into the canonical forms lemma, and only a few small changes are needed here.

15.3.7 Theorem [Progress]

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

Proof: By straightforward induction on typing derivations. The variable case cannot occur (because t is closed). The case for lambda-abstractions is immediate, since abstractions are values. The remaining cases are more interesting.

Case T-APP:

 t = t1 t2      t1 : T11  T12      t2 :T11    T = T12 

By the induction hypothesis, either t1 is a value or else it can make a step of evaluation; likewise t2. If t1 can take a step, then rule E-App1 applies to t. If t1 is a value and t2 can take a step, then rule E-App2 applies. Finally, if both t1 and t2 are values, then the canonical forms lemma (15.3.6) tells us that t1 has the form λx:S11 . t12, so rule E-AppAbs applies to t.

Case T-RCD:

 t = {li=ti iÎ l..n}    for each i Î l..n, ti : Ti T = {li : Ti iÎ l..n} 

By the induction hypothesis, each ti either is already a value or can make a step of evaluation. If all of them are values, then t is a value. On the other hand, if at least one can make a step, then rule E-RCD applies to t.

Case T-PROJ:

 t = t1 . lj     t1 : {li : Ti iÎ l..n}      T = Tj 

By the induction hypothesis, either t1 is a value or it can make an evaluation step. If t1 can make a step, then (by E-PROJ) so can t. If t1 is a value, then by the canonical forms lemma (15.3.6) t1 has the form {ka=vj aÎ l..m}, with {li iÎ l..n} {ka aÎ l..m} and with vj : Ti for each li = kj. In particular, lj is among the labels {ka aÎ l..m} of t1, from which rule E-PROJRCD tells us that t itself can take an evaluation step.

Case T-SUB:

 Г  t : S      S <: T 

The result follows directly from the induction hypothesis.



 < 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