RecordSub: Subtyping with Records

In this chapter, we combine two significant extensions of the pure STLC -- records (from chapter Records) and subtyping (from chapter Sub) -- and explore their interactions. Most of the concepts have already been discussed in those chapters, so the presentation here is somewhat terse. We just comment where things are nonstandard.

Core Definitions

Syntax

Well-Formedness

The syntax of terms and types is a bit too loose, in the sense that it admits things like a record type whose final tail is Top or some arrow type rather than Nil. To avoid such cases, it is useful to assume that all the record types and terms that we see will obey some simple well-formedness conditions.

An interesting technical question is whether the basic properties of the system -- progress and preservation -- remain true if we drop these conditions. I believe they do, and I would encourage motivated readers to try to check this by dropping the conditions from the definitions of typing and subtyping and adjusting the proofs in the rest of the chapter accordingly. This is not a trivial exercise (or I'd have done it!), but it should not involve changing the basic structure of the proofs. If someone does do it, please let me know. --BCP 5/16.

Substitution

Substitution and reduction are as before.

Reduction

Subtyping

Now we come to the interesting part, where the features we've added start to interact. We begin by defining the subtyping relation and developing some of its important technical properties.

Definition

The definition of subtyping is essentially just what we sketched in the discussion of record subtyping in chapter Sub, but we need to add well-formedness side conditions to some of the rules. Also, we replace the n-ary width, depth, and permutation subtyping rules by binary rules that deal with just the first field.

Examples

The following facts are mostly easy to prove in Coq. To get full benefit, make sure you also understand how to prove them on paper!

Exercise: 2 stars, standard (subtyping_example_1)

Exercise: 1 star, standard (subtyping_example_2)

Exercise: 1 star, standard (subtyping_example_3)

Exercise: 2 stars, standard (subtyping_example_4)

Properties of Subtyping

Well-Formedness

To get started proving things about subtyping, we need a couple of technical lemmas that intuitively (1) allow us to extract the well-formedness assumptions embedded in subtyping derivations and (2) record the fact that fields of well-formed record types are themselves well-formed types.

Field Lookup

The record matching lemmas get a little more complicated in the presence of subtyping, for two reasons. First, record types no longer necessarily describe the exact structure of the corresponding terms. And second, reasoning by induction on typing derivations becomes harder in general, because typing is no longer syntax directed.

Exercise: 3 stars, standard (rcd_types_match_informal)

Write a careful informal proof of the rcd_types_match lemma.

Inversion Lemmas

Exercise: 3 stars, standard, optional (sub_inversion_arrow)

Typing

Typing Examples

Exercise: 1 star, standard (typing_example_0)

Exercise: 2 stars, standard (typing_example_1)

Exercise: 2 stars, standard, optional (typing_example_2)

Properties of Typing

Well-Formedness

Field Lookup

Progress

Exercise: 3 stars, standard (canonical_forms_of_arrow_types)

Theorem : For any term t and type T, if empty |- t : T then t is a value or t --> t' for some term t'.

Proof: Let t and T be given such that empty |- t : T. We proceed by induction on the given typing derivation.

  • The cases where the last step in the typing derivation is T_Abs or T_RNil are immediate because abstractions and {} are always values. The case for T_Var is vacuous because variables cannot be typed in the empty context.

  • If the last step in the typing derivation is by T_App, then there are terms t1 t2 and types T1 T2 such that t = t1 t2, T = T2, empty |- t1 : T1 -> T2 and empty |- t2 : T1.

    The induction hypotheses for these typing derivations yield that t1 is a value or steps, and that t2 is a value or steps.

    • Suppose t1 --> t1' for some term t1'. Then t1 t2 --> t1' t2 by ST_App1.

    • Otherwise t1 is a value.

      • Suppose t2 --> t2' for some term t2'. Then t1 t2 --> t1 t2' by rule ST_App2 because t1 is a value.

      • Otherwise, t2 is a value. By Lemma canonical_forms_for_arrow_types, t1 = \x:S1.s2 for some x, S1, and s2. But then (\x:S1.s2) t2 --> [x:=t2]s2 by ST_AppAbs, since t2 is a value.

  • If the last step of the derivation is by T_Proj, then there are a term tr, a type Tr, and a label i such that t = tr.i, empty |- tr : Tr, and Tlookup i Tr = Some T.

    By the IH, either tr is a value or it steps. If tr --> tr' for some term tr', then tr.i --> tr'.i by rule ST_Proj1.

    If tr is a value, then Lemma lookup_field_in_value yields that there is a term ti such that tlookup i tr = Some ti. It follows that tr.i --> ti by rule ST_ProjRcd.

  • If the final step of the derivation is by T_Sub, then there is a type S such that S <: T and empty |- t : S. The desired result is exactly the induction hypothesis for the typing subderivation.

  • If the final step of the derivation is by T_RCons, then there exist some terms t1 tr, types T1 Tr and a label t such that t = {i=t1, tr}, T = {i:T1, Tr}, record_ty tr, record_tm Tr, empty |- t1 : T1 and empty |- tr : Tr.

    The induction hypotheses for these typing derivations yield that t1 is a value or steps, and that tr is a value or steps. We consider each case:

    • Suppose t1 --> t1' for some term t1'. Then {i=t1, tr} --> {i=t1', tr} by rule ST_Rcd_Head.

    • Otherwise t1 is a value.

      • Suppose tr --> tr' for some term tr'. Then {i=t1, tr} --> {i=t1, tr'} by rule ST_Rcd_Tail, since t1 is a value.

      • Otherwise, tr is also a value. So, {i=t1, tr} is a value by v_rcons.

Inversion Lemmas

Context Invariance

Preservation

Theorem: If t, t' are terms and T is a type such that empty |- t : T and t --> t', then empty |- t' : T.

Proof: Let t and T be given such that empty |- t : T. We go by induction on the structure of this typing derivation, leaving t' general. Cases T_Abs and T_RNil are vacuous because abstractions and {} don't step. Case T_Var is vacuous as well, since the context is empty.

  • If the final step of the derivation is by T_App, then there are terms t1 t2 and types T1 T2 such that t = t1 t2, T = T2, empty |- t1 : T1 -> T2 and empty |- t2 : T1.

    By inspection of the definition of the step relation, there are three ways t1 t2 can step. Cases ST_App1 and ST_App2 follow immediately by the induction hypotheses for the typing subderivations and a use of T_App.

    Suppose instead t1 t2 steps by ST_AppAbs. Then t1 = \x:S.t12 for some type S and term t12, and t' = [x:=t2]t12.

    By Lemma abs_arrow, we have T1 <: S and x:S1 |- s2 : T2. It then follows by lemma substitution_preserves_typing that empty |- [x:=t2] t12 : T2 as desired.

  • If the final step of the derivation is by T_Proj, then there is a term tr, type Tr and label i such that t = tr.i, empty |- tr : Tr, and Tlookup i Tr = Some T.

    The IH for the typing derivation gives us that, for any term tr', if tr --> tr' then empty |- tr' Tr. Inspection of the definition of the step relation reveals that there are two ways a projection can step. Case ST_Proj1 follows immediately by the IH.

    Instead suppose tr.i steps by ST_ProjRcd. Then tr is a value and there is some term vi such that tlookup i tr = Some vi and t' = vi. But by lemma lookup_field_in_value, empty |- vi : Ti as desired.

  • If the final step of the derivation is by T_Sub, then there is a type S such that S <: T and empty |- t : S. The result is immediate by the induction hypothesis for the typing subderivation and an application of T_Sub.

  • If the final step of the derivation is by T_RCons, then there exist some terms t1 tr, types T1 Tr and a label t such that t = {i=t1, tr}, T = {i:T1, Tr}, record_ty tr, record_tm Tr, empty |- t1 : T1 and empty |- tr : Tr.

    By the definition of the step relation, t must have stepped by ST_Rcd_Head or ST_Rcd_Tail. In the first case, the result follows by the IH for t1's typing derivation and T_RCons. In the second case, the result follows by the IH for tr's typing derivation, T_RCons, and a use of the step_preserves_record_tm lemma.