RecordSub带记录体的子类型

在本章中,我们结合纯 STLC 的两个重要扩展: 来自 Records 一章的【记录体|Record】, 以及来自 Sub 一章的【子类型|Subtyping】, 并探讨它们之间的交互。 大多数概念已在那些章节中讨论过,因此这里的讲述较为简略。 我们只在非常规之处评注。
Set Warnings "-notation-overridden,-parsing,-deprecated-hint-without-locality".
From Stdlib Require Import Strings.String.
From PLF Require Import Maps.
From PLF Require Import Smallstep.

Module RecordSub.

核心定义

语法

Inductive ty : Type :=
  (* 普通类型 *)
  | Ty_Top : ty
  | Ty_Base : string → ty
  | Ty_Arrow : ty → ty → ty
  (* 记录类型 *)
  | Ty_RNil : ty
  | Ty_RCons : string → ty → ty → ty.

Inductive tm : Type :=
  (* 普通项 *)
  | tm_var : string → tm
  | tm_app : tm → tm → tm
  | tm_abs : string → ty → tm → tm
  | tm_rproj : tm → string → tm
  (* 记录项 *)
  | tm_rnil : tm
  | tm_rcons : string → tm → tm → tm.

Declare Custom Entry stlc.
Declare Custom Entry stlc_ty.

Notation "<{ e }>" := e (e custom stlc at level 99).
Notation "<{{ e }}>" := e (e custom stlc_ty at level 99).
Notation "( x )" := x (in custom stlc, x at level 99).
Notation "( x )" := x (in custom stlc_ty, x at level 99).
Notation "x" := x (in custom stlc at level 0, x constr at level 0).
Notation "x" := x (in custom stlc_ty at level 0, x constr at level 0).
Notation "S -> T" := (Ty_Arrow S T) (in custom stlc_ty at level 50, right associativity).
Notation "x y" := (tm_app x y) (in custom stlc at level 1, left associativity).
Notation "\ x : t , y" :=
  (tm_abs x t y) (in custom stlc at level 90, x at level 99,
                     t custom stlc_ty at level 99,
                     y custom stlc at level 99,
                     left associativity).
Coercion tm_var : string >-> tm.

Notation "{ x }" := x (in custom stlc at level 1, x constr).

Notation "'Base' x" := (Ty_Base x) (in custom stlc_ty at level 0).

Notation " l ':' t1 '::' t2" := (Ty_RCons l t1 t2) (in custom stlc_ty at level 3, right associativity).
Notation " l := e1 '::' e2" := (tm_rcons l e1 e2) (in custom stlc at level 3, right associativity).
Notation "'nil'" := (Ty_RNil) (in custom stlc_ty).
Notation "'nil'" := (tm_rnil) (in custom stlc).
Notation "o --> l" := (tm_rproj o l) (in custom stlc at level 0).

Notation "'Top'" := (Ty_Top) (in custom stlc_ty at level 0).

良构性

项和类型的语法有点过于宽松:它允许诸如最终「尾部」为 Top 或 某个箭头类型而非 Nil 的记录类型之类的东西。为避免此类情况, 假定我们见到的所有记录类型和项都满足某些简单的良构性条件是 有益的。
Inductive record_ty : ty → Prop :=
  | RTnil :
        record_ty <{{ nil }}>
  | RTcons : ∀ i T1 T2,
        record_ty <{{ i : T1 :: T2 }}>.

Inductive record_tm : tm → Prop :=
  | rtnil :
        record_tm <{ nil }>
  | rtcons : ∀ i t1 t2,
        record_tm <{ i := t1 :: t2 }>.

Inductive well_formed_ty : ty → Prop :=
  | wfTop :
        well_formed_ty <{{ Top }}>
  | wfBase : ∀ (i : string),
        well_formed_ty <{{ Base i }}>
  | wfArrow : ∀ T1 T2,
        well_formed_ty T1 →
        well_formed_ty T2 →
        well_formed_ty <{{ T1 → T2 }}>
  | wfRNil :
        well_formed_ty <{{ nil }}>
  | wfRCons : ∀ i T1 T2,
        well_formed_ty T1 →
        well_formed_ty T2 →
        record_ty T2 →
        well_formed_ty <{{ i : T1 :: T2 }}>.

Hint Constructors record_ty record_tm well_formed_ty : core.

替换

替换和归约与之前相同。
Reserved Notation "'[' x ':=' s ']' t" (in custom stlc at level 20, x constr).

Fixpoint subst (x : string) (s : tm) (t : tm) : tm :=
  match t with
  | tm_var y ⇒
      if String.eqb x y then s else t
  | <{\y:T, t1}> ⇒
      if String.eqb x y then t else <{\y:T, [x:=s] t1}>
  | <{t1 t2}> ⇒
      <{([x:=s] t1) ([x:=s] t2)}>
  | <{ t1 --> i }> ⇒
      <{ ( [x := s] t1) --> i }>
  | <{ nil }> ⇒
      <{ nil }>
  | <{ i := t1 :: tr }> ⇒
     <{ i := [x := s] t1 :: ( [x := s] tr) }>
  end

where "'[' x ':=' s ']' t" := (subst x s t) (in custom stlc).

归约

Inductive value : tm → Prop :=
  | v_abs : ∀ x T2 t1,
      value <{ \ x : T2, t1 }>
  | v_rnil : value <{ nil }>
  | v_rcons : ∀ i v1 vr,
      value v1 →
      value vr →
      value <{ i := v1 :: vr }>.

Hint Constructors value : core.

Fixpoint Tlookup (i:string) (Tr:ty) : option ty :=
  match Tr with
  | <{{ i' : T :: Tr' }}> ⇒
      if String.eqb i i' then Some T else Tlookup i Tr'
  | _ ⇒ None
  end.

Fixpoint tlookup (i:string) (tr:tm) : option tm :=
  match tr with
  | <{ i' := t :: tr' }> ⇒
      if String.eqb i i' then Some t else tlookup i tr'
  | _ ⇒ None
  end.


Inductive step : tm → tm → Prop :=
  | ST_AppAbs : ∀ x T2 t1 v2,
         value v2 →
         <{(\x:T2, t1) v2}> --> <{ [x:=v2]t1 }>
  | ST_App1 : ∀ t1 t1' t2,
         t1 --> t1' →
         <{t1 t2}> --> <{t1' t2}>
  | ST_App2 : ∀ v1 t2 t2',
         value v1 →
         t2 --> t2' →
         <{v1 t2}> --> <{v1 t2'}>
  | ST_Proj1 : ∀ t1 t1' i,
        t1 --> t1' →
        <{ t1 --> i }> --> <{ t1' --> i }>
  | ST_ProjRcd : ∀ tr i vi,
        value tr →
        tlookup i tr = Some vi →
        <{ tr --> i }> --> vi
  | ST_Rcd_Head : ∀ i t1 t1' tr2,
        t1 --> t1' →
        <{ i := t1 :: tr2 }> --> <{ i := t1' :: tr2 }>
  | ST_Rcd_Tail : ∀ i v1 tr2 tr2',
        value v1 →
        tr2 --> tr2' →
        <{ i := v1 :: tr2 }> --> <{ i := v1 :: tr2' }>

where "t '-->' t'" := (step t t').

Hint Constructors step : core.

子类型

现在到了有趣的部分:我们添加的特性开始相互作用。我们先定义 子类型关系,并展开它的一些重要技术性质。

定义

子类型的定义本质上就是我们在 Sub 章讨论记录子类型时所 勾勒的那样,但我们需要为其中一些规则添加良构性附加条件。此外, 我们把「n 元」的宽度、深度和排列子类型规则,替换为只处理第一个 字段的二元规则。

Inductive subtype : ty → ty → Prop :=
  (* 普通类型之间的子类型 *)
  | S_Refl : ∀ T,
    well_formed_ty T →
    T <: T
  | S_Trans : ∀ S U T,
    S <: U →
    U <: T →
    S <: T
  | S_Top : ∀ S,
    well_formed_ty S →
    S <: <{{ Top }}>
  | S_Arrow : ∀ S1 S2 T1 T2,
    T1 <: S1 →
    S2 <: T2 →
    <{{ S1 → S2 }}> <: <{{ T1 → T2 }}>
  (* 记录类型之间的子类型 *)
  | S_RcdWidth : ∀ i T1 T2,
    well_formed_ty <{{ i : T1 :: T2 }}> →
    <{{ i : T1 :: T2 }}> <: <{{ nil }}>
  | S_RcdDepth : ∀ i S1 T1 Sr2 Tr2,
    S1 <: T1 →
    Sr2 <: Tr2 →
    record_ty Sr2 →
    record_ty Tr2 →
    <{{ i : S1 :: Sr2 }}> <: <{{ i : T1 :: Tr2 }}>
  | S_RcdPerm : ∀ i1 i2 T1 T2 Tr3,
    well_formed_ty <{{ i1 : T1 :: i2 : T2 :: Tr3 }}> →
    i1 ≠ i2 →
       <{{ i1 : T1 :: i2 : T2 :: Tr3 }}>
    <: <{{ i2 : T2 :: i1 : T1 :: Tr3 }}>

where "T '<:' U" := (subtype T U).

Hint Constructors subtype : core.

示例

Module Examples.
Open Scope string_scope.

Notation x := "x".
Notation y := "y".
Notation z := "z".
Notation j := "j".
Notation k := "k".
Notation i := "i".
Notation A := <{{ Base "A" }}>.
Notation B := <{{ Base "B" }}>.
Notation C := <{{ Base "C" }}>.

Definition TRcd_j :=
  <{{ j : (B → B) :: nil }}>. (* {j:B->B} *)
Definition TRcd_kj :=
  <{{ k : (A → A) :: TRcd_j }}>. (* {k:C->C,j:B->B} *)

Example subtyping_example_0 :
  <{{ C → TRcd_kj }}> <: <{{ C → nil }}>.
Proof.
  apply S_Arrow.
    apply S_Refl. auto.
    unfold TRcd_kj, TRcd_j. apply S_RcdWidth; auto.
Qed.
以下事实在 Rocq 中大多容易证明。要获得完整收益,请确保你也 理解如何在纸上证明它们!

练习: 2 stars,标准 (subtyping_example_1)

Example subtyping_example_1 :
  TRcd_kj <: TRcd_j.
(* {k:A->A,j:B->B} <: {j:B->B} *)
Proof with eauto.
  (* 在这里填写 *) Admitted.
☐

练习: 1 star,标准 (subtyping_example_2)

Example subtyping_example_2 :
  <{{ Top → TRcd_kj }}> <:
          <{{ (C → C) → TRcd_j }}>.
Proof with eauto.
  (* 在这里填写 *) Admitted.
☐

练习: 1 star,标准 (subtyping_example_3)

Example subtyping_example_3 :
  <{{ nil → (j : A :: nil) }}> <:
          <{{ (k : B :: nil) → nil }}>.
(* {}->{j:A} <: {k:B}->{} *)
Proof with eauto.
  (* 在这里填写 *) Admitted.
☐

练习: 2 stars,标准 (subtyping_example_4)

Example subtyping_example_4 :
  <{{ x : A :: y : B :: z : C :: nil }}> <:
  <{{ z : C :: y : B :: x : A :: nil }}>.
Proof with eauto.
  (* 在这里填写 *) Admitted.
☐
End Examples.

子类型的性质

良构性

要开始证明关于子类型的结论,我们需要几条技术引理。直观上, 它们有两方面作用:(1)允许我们提取嵌在子类型导出式中的 良构性假设;(2)记录良构记录类型的字段本身也是良构类型。
Lemma subtype__wf : ∀ S T,
  subtype S T →
  well_formed_ty T ∧ well_formed_ty S.
Proof with eauto.
  intros S T Hsub.
  induction Hsub;
    intros; try (destruct IHHsub1; destruct IHHsub2)...
  - (* S_RcdPerm *)
    split... inversion H. subst. inversion H5... Qed.

Lemma wf_rcd_lookup : ∀ i T Ti,
  well_formed_ty T →
  Tlookup i T = Some Ti →
  well_formed_ty Ti.
Proof with eauto.
  intros i T.
  induction T; intros; try solve_by_invert.
  - (* RCons *)
    inversion H. subst. unfold Tlookup in H0.
    destruct (String.eqb i s)... inversion H0; subst... Qed.

字段查找

在有子类型的情况下,记录匹配引理会变得稍微复杂一些,原因有二。 首先,记录类型不再必然描述对应项的确切结构。其次,对定型导出式 做归纳推理在一般情况下变得更困难,因为定型不再是语法导向的。
Lemma rcd_types_match : ∀ S T i Ti,
  subtype S T →
  Tlookup i T = Some Ti →
  ∃ Si, Tlookup i S = Some Si ∧ subtype Si Ti.
Proof with (eauto using wf_rcd_lookup).
  intros S T i Ti Hsub Hget. generalize dependent Ti.
  induction Hsub; intros Ti Hget;
    try solve_by_invert.
  - (* S_Refl *)
    ∃ Ti...
  - (* S_Trans *)
    destruct (IHHsub2 Ti) as [Ui Hui]... destruct Hui.
    destruct (IHHsub1 Ui) as [Si Hsi]... destruct Hsi.
    ∃ Si...
  - (* S_RcdDepth *)
    rename i0 into k.
    unfold Tlookup. unfold Tlookup in Hget.
    destruct (String.eqb i k)...
    + (* i = k:我们查找的是第一个字段 *)
      inversion Hget. subst. ∃ S1...
  - (* S_RcdPerm *)
    ∃ Ti. split.
    + (* 查找 *)
      unfold Tlookup. unfold Tlookup in Hget.
      destruct (eqb_spec i i1)...
      × (* i = i1:我们查找的是第一个字段 *)
        destruct (eqb_spec i i2)...
        (* i = i2:矛盾 *)
        destruct H0.
        subst...
    + (* 子类型 *)
      inversion H. subst. inversion H5. subst... Qed.

练习: 3 stars,标准 (rcd_types_match_informal)

写出 rcd_types_match 引理的一个仔细的非形式化证明。
(* 在这里填写 *)

(* 不要修改下面这行: *)
Definition manual_grade_for_rcd_types_match_informal : option (nat×string) := None.
☐

反演引理

练习: 3 stars,标准,可选 (sub_inversion_arrow)

Lemma sub_inversion_arrow : ∀ U V1 V2,
     U <: <{{ V1 → V2 }}> →
     ∃ U1 U2,
       (U= <{{ U1 → U2 }}> ) ∧ (V1 <: U1) ∧ (U2 <: V2).
Proof with eauto.
  intros U V1 V2 Hs.
  remember <{{ V1 → V2 }}> as V.
  generalize dependent V2. generalize dependent V1.
  (* 在这里填写 *) Admitted.
☐

定型

Definition context := partial_map ty.


Inductive has_type : context → tm → ty → Prop :=
  | T_Var : ∀ Gamma (x : string) T,
      Gamma x = Some T →
      well_formed_ty T →
      Gamma |-- x \in T
  | T_Abs : ∀ Gamma x T11 T12 t12,
      well_formed_ty T11 →
      (x ⊢> T11; Gamma) |-- t12 \in T12 →
      Gamma |-- (\ x : T11, t12) \in (T11 → T12)
  | T_App : ∀ T1 T2 Gamma t1 t2,
      Gamma |-- t1 \in (T1 → T2) →
      Gamma |-- t2 \in T1 →
      Gamma |-- t1 t2 \in T2
  | T_Proj : ∀ Gamma i t T Ti,
      Gamma |-- t \in T →
      Tlookup i T = Some Ti →
      Gamma |-- t --> i \in Ti
  (* 子类型化 *)
  | T_Sub : ∀ Gamma t S T,
      Gamma |-- t \in S →
      subtype S T →
      Gamma |-- t \in T
  (* 记录项的规则 *)
  | T_RNil : ∀ Gamma,
      Gamma |-- nil \in nil
  | T_RCons : ∀ Gamma i t T tr Tr,
      Gamma |-- t \in T →
      Gamma |-- tr \in Tr →
      record_ty Tr →
      record_tm tr →
      Gamma |-- i := t :: tr \in (i : T :: Tr)

where "Gamma '|--' t '∈' T" := (has_type Gamma t T).

Hint Constructors has_type : core.

定型示例

Module Examples2.
Import Examples.

练习: 1 star,标准 (typing_example_0)

Definition trcd_kj :=
  <{ k := (\z : A, z) :: j := (\z : B, z) :: nil }>.

Example typing_example_0 :
  empty |-- trcd_kj \in TRcd_kj.
(* empty |-- {k=(\z:A.z), j=(\z:B.z)} : {k:A->A,j:B->B} *)
Proof.
  (* 在这里填写 *) Admitted.
☐

练习: 2 stars,标准 (typing_example_1)

Example typing_example_1 :
  empty |-- (\x : TRcd_j, x --> j) trcd_kj \in (B → B).
(* empty |-- (\x:{k:A->A,j:B->B}, x.j)
              {k=(\z:A,z), j=(\z:B,z)}
         : B->B *)

Proof with eauto.
  (* 在这里填写 *) Admitted.
☐

练习: 2 stars,标准,可选 (typing_example_2)

Example typing_example_2 :
  empty |-- (\ z : (C → C) → TRcd_j, (z (\ x : C, x) ) --> j )
            ( \z : (C → C), trcd_kj ) \in (B → B).
(* empty |-- (\z:(C->C)->{j:B->B}, (z (\x:C,x)).j)
              (\z:C->C, {k=(\z:A,z), j=(\z:B,z)})
           : B->B *)

Proof with eauto.
  (* 在这里填写 *) Admitted.
☐

End Examples2.

定型的性质

良构性

Lemma has_type__wf : ∀ Gamma t T,
  has_type Gamma t T → well_formed_ty T.
Proof with eauto.
  intros Gamma t T Htyp.
  induction Htyp...
  - (* T_App *)
    inversion IHHtyp1...
  - (* T_Proj *)
    eapply wf_rcd_lookup...
  - (* T_Sub *)
    apply subtype__wf in H.
    destruct H...
Qed.

Lemma step_preserves_record_tm : ∀ tr tr',
  record_tm tr →
  tr --> tr' →
  record_tm tr'.
Proof.
  intros tr tr' Hrt Hstp.
  inversion Hrt; subst; inversion Hstp; subst; eauto.
Qed.

字段查找

Lemma lookup_field_in_value : ∀ v T i Ti,
  value v →
  empty |-- v \in T →
  Tlookup i T = Some Ti →
  ∃ vi, tlookup i v = Some vi ∧ empty |-- vi \in Ti.
Proof with eauto.
  remember empty as Gamma.
  intros t T i Ti Hval Htyp. generalize dependent Ti.
  induction Htyp; intros; subst; try solve_by_invert.
  - (* T_Sub *)
    apply (rcd_types_match S) in H0...
    destruct H0 as [Si [HgetSi Hsub]].
    eapply IHHtyp in HgetSi...
    destruct HgetSi as [vi [Hget Htyvi]]...
  - (* T_RCons *)
    simpl in H0. simpl. simpl in H1.
    destruct (String.eqb i i0).
    + (* i 为第一个字段 *)
      injection H1 as H1. subst. ∃ t...
    + (* i 在尾部 *)
      eapply IHHtyp2 in H1...
      inversion Hval... Qed.

可进性

练习: 3 stars,标准 (canonical_forms_of_arrow_types)

Lemma canonical_forms_of_arrow_types : ∀ Gamma s T1 T2,
     Gamma |-- s \in (T1 → T2) →
     value s →
     ∃ x S1 s2,
        s = <{ \ x : S1, s2 }>.
Proof with eauto.
  (* 在这里填写 *) Admitted.
☐

Theorem progress : ∀ t T,
     empty |-- t \in T →
     value t ∨ ∃ t', t --> t'.
Proof with eauto.
  intros t T Ht.
  remember empty as Gamma.
  revert HeqGamma.
  induction Ht;
    intros HeqGamma; subst...
  - (* T_Var *)
    inversion H.
  - (* T_App *)
    right.
    destruct IHHt1; subst...
    + (* t1 为值 *)
      destruct IHHt2; subst...
      × (* t2 为值 *)
        destruct (canonical_forms_of_arrow_types empty t1 T1 T2)
          as [x [S1 [t12 Heqt1]]]...
        subst. ∃ <{ [x:=t2] t12 }>...
      × (* t2 走一步 *)
        destruct H0 as [t2' Hstp]. ∃ <{ t1 t2' }> ...
    + (* t1 走一步 *)
      destruct H as [t1' Hstp]. ∃ <{ t1' t2 }>...
  - (* T_Proj *)
    right. destruct IHHt...
    + (* 记录体为值 *)
      destruct (lookup_field_in_value t T i Ti)
        as [t' [Hget Ht']]...
    + (* 记录体走一步 *)
      destruct H0 as [t' Hstp]. ∃ <{ t' --> i }>...
  - (* T_RCons *)
    destruct IHHt1...
    + (* 头部为值 *)
      destruct IHHt2...
      × (* 尾部走一步 *)
        right. destruct H2 as [tr' Hstp].
        ∃ <{ i := t :: tr' }>...
    + (* 头部走一步 *)
      right. destruct H1 as [t' Hstp].
      ∃ <{ i := t' :: tr}>... Qed.
【定理】 :对任意项 t 和类型 T,若 empty |-- t : T, 则 t 是值,或者对某个项 t' 有 t --> t'。
【证明】 :设 t 和 T 满足 empty |-- t : T。我们对给定的 定型导出式归纳。
  • 定型导出式的最后一步是 T_Abs 或 T_RNil 的情形是直接的, 因为抽象和 {} 总是值。T_Var 的情形是空虚的,因为变量 无法在空语境中定型。
  • 若定型导出式的最后一步由 T_App 得出,则存在项 t1 t2 和类型 T1 T2 使得 t = t1 t2、T = T2、 empty |-- t1 : T1 → T2 且 empty |-- t2 : T1。
    这些定型导出式的归纳假设给出:t1 是值或能走一步, 且 t2 是值或能走一步。
    • 假设对某个项 t1' 有 t1 --> t1'。则由 ST_App1 得 t1 t2 --> t1' t2。
    • 否则 t1 是值。
      • 假设对某个项 t2' 有 t2 --> t2'。则由规则 ST_App2 得 t1 t2 --> t1 t2',因为 t1 是值。
      • 否则 t2 是值。由引理 canonical_forms_for_arrow_types,对某个 x、S1 和 s2 有 t1 = \x:S1,s2。而由于 t2 是值,由 ST_AppAbs 得 (\x:S1,s2) t2 --> [x:=t2]s2。
  • 若导出式的最后一步由 T_Proj 得出,则存在项 tr、类型 Tr 和标签 i 使得 t = tr.i、empty |-- tr : Tr, 且 Tlookup i Tr = Some T。
    由归纳假设,tr 要么是值要么走一步。若对某个项 tr' 有 tr --> tr',则由规则 ST_Proj1 得 tr.i --> tr'.i。
    若 tr 是值,则引理 lookup_field_in_value 给出存在项 ti 使得 tlookup i tr = Some ti。于是由规则 ST_ProjRcd 得 tr.i --> ti。
  • 若导出式的最后一步由 T_Sub 得出,则存在类型 S 使得 S <: T 且 empty |-- t : S。所需结论正是定型子导出式 的归纳假设。
  • 若导出式的最后一步由 T_RCons 得出,则存在项 t1 tr、 类型 T1 Tr 和标签 t 使得 t = {i=t1, tr}、 T = {i:T1, Tr}、record_ty tr、record_tm Tr、 empty |-- t1 : T1 且 empty |-- tr : Tr。
    这些定型导出式的归纳假设给出:t1 是值或能走一步, 且 tr 是值或能走一步。我们考虑每种情形:
    • 假设对某个项 t1' 有 t1 --> t1'。则由规则 ST_Rcd_Head 得 {i=t1, tr} --> {i=t1', tr}。
    • 否则 t1 是值。
      • 假设对某个项 tr' 有 tr --> tr'。则由规则 ST_Rcd_Tail 得 {i=t1, tr} --> {i=t1, tr'}, 因为 t1 是值。
      • 否则 tr 也是值。于是由 v_rcons,{i=t1, tr} 是值。

反演引理

Lemma typing_inversion_abs : ∀ Gamma x S1 t2 T,
     Gamma |-- \ x : S1, t2 \in T →
     (∃ S2, <{{ S1 → S2 }}> <: T
              ∧ (x ⊢> S1; Gamma) |-- t2 \in S2).
Proof with eauto.
  intros Gamma x S1 t2 T H.
  remember <{ \ x : S1, t2 }> as t.
  induction H;
    inversion Heqt; subst; intros; try solve_by_invert.
  - (* T_Abs *)
    assert (Hwf := has_type__wf _ _ _ H0).
    ∃ T12...
  - (* T_Sub *)
    destruct IHhas_type as [S2 [Hsub Hty]]...
    Qed.

Lemma abs_arrow : ∀ x S1 s2 T1 T2,
  empty |-- \x : S1, s2 \in (T1 → T2) →
     T1 <: S1
  ∧ (x ⊢> S1) |-- s2 \in T2.
Proof with eauto.
  intros x S1 s2 T1 T2 Hty.
  apply typing_inversion_abs in Hty.
  destruct Hty as [S2 [Hsub Hty]].
  apply sub_inversion_arrow in Hsub.
  destruct Hsub as [U1 [U2 [Heq [Hsub1 Hsub2]]]].
  inversion Heq; subst... Qed.

弱化

弱化引理与纯 STLC 中的证明方式相同。
Lemma weakening : ∀ Gamma Gamma' t T,
     includedin Gamma Gamma' →
     Gamma |-- t \in T →
     Gamma' |-- t \in T.
Proof.
  intros Gamma Gamma' t T H Ht.
  generalize dependent Gamma'.
  induction Ht; eauto using includedin_update.
Qed.

Lemma weakening_empty : ∀ Gamma t T,
     empty |-- t \in T →
     Gamma |-- t \in T.
Proof.
  intros Gamma t T.
  eapply weakening.
  discriminate.
Qed.

保型性

Lemma substitution_preserves_typing : ∀ Gamma x U t v T,
   (x ⊢> U ; Gamma) |-- t \in T →
   empty |-- v \in U →
   Gamma |-- [x:=v]t \in T.
Proof.
  intros Gamma x U t v T Ht Hv.
  remember (x ⊢> U; Gamma) as Gamma'.
  generalize dependent Gamma.
  induction Ht; intros Gamma' G; simpl; eauto.
  - (* T_Var *)
    rename x0 into y.
    destruct (eqb_spec x y) as [Hxy|Hxy]; subst.
    + (* x = y *)
      rewrite update_eq in H.
      injection H as H. subst.
      apply weakening_empty. assumption.
    + (* x<>y *)
      apply T_Var; [|assumption].
      rewrite update_neq in H; assumption.
  - (* T_Abs *)
    rename x0 into y. subst.
    destruct (eqb_spec x y) as [Hxy|Hxy]; apply T_Abs; try assumption.
    + (* x=y *)
      subst. rewrite update_shadow in Ht. assumption.
    + (* x <> y *)
      subst. apply IHHt.
      rewrite update_permute; auto.
      - (* rcons *) (* <=== 与纯 STLC 相比唯一的新情形 *)
      apply T_RCons; eauto.
      inversion H0; subst; simpl; auto.
Qed.

Theorem preservation : ∀ t t' T,
     empty |-- t \in T →
     t --> t' →
     empty |-- t' \in T.
Proof with eauto.
  intros t t' T HT. generalize dependent t'.
  remember empty as Gamma.
  induction HT;
       intros t' HE; subst;
       try solve [inversion HE; subst; eauto].
  - (* T_App *)
    inversion HE; subst...
    + (* ST_AppAbs *)
      destruct (abs_arrow _ _ _ _ _ HT1) as [HA1 HA2].
      apply substitution_preserves_typing with T0...
  - (* T_Proj *)
    inversion HE; subst...
    destruct (lookup_field_in_value _ _ _ _ H2 HT H)
      as [vi [Hget Hty]].
    rewrite H4 in Hget. inversion Hget. subst...
  - (* T_RCons *)
    inversion HE; subst...
    eauto using step_preserves_record_tm. Qed.
【定理】 :若 t、t' 是项,T 是类型,满足 empty |-- t : T 且 t --> t',则 empty |-- t' : T。
【证明】 :设 t 和 T 满足 empty |-- t : T。我们对该定型 导出式的结构归纳,保持 t' 为一般情形。情形 T_Abs 和 T_RNil 是空虚的,因为抽象和 {} 不会步进。情形 T_Var 也是空虚的,因为语境为空。
  • 若导出式的最后一步由 T_App 得出,则存在项 t1 t2 和 类型 T1 T2 使得 t = t1 t2、T = T2、 empty |-- t1 : T1 → T2 且 empty |-- t2 : T1。
    检查步进关系的定义可知,t1 t2 有三种走法。情形 ST_App1 和 ST_App2 由定型子导出式的归纳假设并结合一次 T_App 的应用立即得出。
    另设 t1 t2 由 ST_AppAbs 步进。则对某个类型 S 和项 t12,有 t1 = \x:S,t12,且 t' = [x:=t2]t12。
    由引理 abs_arrow,我们有 T1 <: S 和 x:S1 |-- s2 : T2。于是由引理 substitution_preserves_typing 得 empty |-- [x:=t2] t12 : T2,即所欲。
  • 若导出式的最后一步由 T_Proj 得出,则存在项 tr、类型 Tr 和标签 i 使得 t = tr.i、empty |-- tr : Tr, 且 Tlookup i Tr = Some T。
    定型导出式的归纳假设告诉我们:对任意项 tr',若 tr --> tr' 则 empty |-- tr' Tr。检查步进关系的定义 可知投影有两种走法。情形 ST_Proj1 由归纳假设立即得出。
    另设 tr --> i 由 ST_ProjRcd 步进。则 tr 是值,且存在 某个项 vi 使得 tlookup i tr = Some vi 且 t' = vi。 但由引理 lookup_field_in_value,如所欲有 empty |-- vi : Ti。
  • 若导出式的最后一步由 T_Sub 得出,则存在类型 S 使得 S <: T 且 empty |-- t : S。结论由定型子导出式的归纳 假设并应用 T_Sub 直接得出。
  • 若导出式的最后一步由 T_RCons 得出,则存在项 t1 tr、 类型 T1 Tr 和标签 t 使得 t = i:=t1 :: tr}、 T = i:T1 :: Tr、record_ty tr、record_tm Tr、 empty |-- t1 : T1 且 empty |-- tr : Tr。
    由步进关系的定义,t 必由 ST_Rcd_Head 或 ST_Rcd_Tail 步进。第一种情形下,结论由 t1 定型导出式的归纳假设和 T_RCons 得出。第二种情形下,结论由 tr 定型导出式的 归纳假设、T_RCons,以及使用引理 step_preserves_record_tm 得出。
End RecordSub.

(* 2026-11-26 15:55 *)