Transitivity of Algorithmic Subtyping

This example follows the proof in the proof pearl description which appeared at TPHOLs2007 [Pientka 2007]. It shows a solution of the POPLMARK Challenge, part 1A : Transitivity of Subtyping.

This implementation not only uses higher-order abstract syntax in specifying the types, but we will exploit extensively the power of parametric and hypothetical judgment. As a benefit we do not need to implement a narrowing lemma separately, but get it "for free".

First, we define our type tp with higher-order abstract syntax in LF as follows:

LF tp : type =
| top : tp
| arr : tptptp
| forall : tp → (tptp) → tp;

--name tp T.

Algorithmic subtyping

We do not give a general reflexivity and transitivity rule for type variables, but instead we provide for each type variable its own reflexivity and transitivity version. In other words, our rule for polymorphic types implements the following: $$\frac{\Gamma \vdash T_1 <: S_1 \qquad \Gamma, tr:a <: U \rightarrow U <: T \rightarrow a <: T, w:a <: T, ref : a <: a \vdash S_2 <: T_2 }{\Gamma \vdash \forall a:S_1.S_2(a) <: \forall a:T_1.T_2(a)}$$

We use (\(<:\)) or sub to denote algorithmic subtyping.

LF sub : tptptype =
| sa_top : sub S top
| sa_arr : sub T1 S1sub S2 T2sub (arr S1 S2) (arr T1 T2)
| sa_all :
  sub T1 S1 →
    ({a : tp}
       ({U : tp} {T : tp} sub a Usub U Tsub a T) →
         sub a T1sub a asub (S2 a) (T2 a)) →
      sub (forall S1 S2) (forall T1 T2);

We specify the structure of the context using context schema s_ctx as follows:

schema s_ctx =
  some [u : tp]
  block (a : tp,
         tr : {U : tp} {T : tp} sub a Usub U Tsub a T,
         w : sub a u,
         ref : sub a a);

Reflexivity

Theorem : For every \(\Gamma\) and \(T\), \(\Gamma\vdash T <: T\).

The reflexivity Theorem is simply described as {g:s_ctx} {T: [g |- tp]} [g |- sub T T] in computational-level in Beluga. Context variable g and term T are written explicitly using curly brackets {g:s_ctx} and {T: [g |- tp]}. We do case analysis on the term T of type [g |- tp], which is written as [g |- T].

rec refl : {g : s_ctx} {T : (gtp)} (gsub T T) =
mlam gmlam Tcase [gT] of
  | [g#p.1] ⇒ [g#p.4]
  | [gtop] ⇒ [gsa_top]
  | [garr T1 T2] ⇒ let [gS1] = refl [g] [gT1] in
    let [gS2] = refl [g] [gT2] in [gsa_arr S1 S2]
  | [gforall T1 (\a. T2)] ⇒ let [gS1] = refl [g] [gT1] in
    let
      [g, b :
        block (a : tp,
               tr : {u' : tp} {t' : tp} sub a u'sub u' t'sub a t',
               w : sub a T1[…],
               ref : sub a a) ⊢ S2[…, b.1, b.2, b.3, b.4]] =
      refl
        [g, b :
          block (a : tp,
                 tr : {u' : tp} {t' : tp} sub a u'sub u' t'sub a t',
                 w : sub a T1[…],
                 ref : sub a a)] [g, bT2[…, b.1]] in
    [gsa_all S1 (\a. \tr. \w. \ref. S2)];

Transitivity

Theorem : For every \(\Gamma\), \(S\), \(Q\) and \(T\), if \(\Gamma\vdash S <: Q\) and \(\Gamma\vdash Q <: T\), then \(\Gamma\vdash S <: T\).

The transitivity Theorem is described as (g:s_ctx){Q:[g |- tp]}[g |- sub S Q] -> [g |- sub Q T] -> [g |- sub S T]. The context variable is written implicitly in round bracket as (g:s_ctx). We do case analysis on the derivation of type [g |- sub S Q]. In some cases we need to provide extral type annotation for the branch. For example, in the case [g |- sa_arr D1 D2], which uses sa_arr rule for the last step of derivation d1, we add type annotation [g |- sub (arr S1 S2) (arr Q1 Q2)] to obtain terms Q1 and Q2, which are later used as arguments. We use the same technique in the branch of sa_all. We also need to specify the type of some variables when we don't have enough information on them. For example, in the case [g |- #p.2 U' T' D1 D2], we need to specify the type of parameter variable #p and the type of D1.

rec trans :
  (g : s_ctx) {Q : (gtp)} (gsub S Q) → (gsub Q T) → (gsub S T) =
mlam Qfn d1fn d2case d1 of
  | [g#p.3] ⇒ let [gD2] = d2 in [g#p.2 Q _ #p.3 D2]
  | [g#p.4] ⇒ d2
  | {#p :
      #(gblock (a : tp,
               tr : {u' : tp} {t' : tp} sub a u'sub u' t'sub a t',
               w : sub a U'[…],
               ref : sub a a))} {D1 : (gsub #p.1 U')}
    [g#p.2 U' T' D1 D2] ⇒ let [gF1] = trans [gT'] [gD2] d2 in
    trans [gU'] [gD1] [gF1]
  | [gsa_top] ⇒ let [gsa_top] = d2 in [gsa_top]
  | [gsa_arr D1 D2] : (gsub (arr S1 S2) (arr Q1 Q2)) ⇒
    (case d2 of
     | [gsa_arr E1 E2] ⇒ let [gF1] = trans [gQ1] [gE1] [gD1]
       in let [gF2] = trans [gQ2] [gD2] [gE2] in
       [gsa_arr F1 F2]
     | [gsa_top] ⇒ [gsa_top])
  | [gsa_all D1 (\a. \tr. \w. \ref. D2)] :
      (gsub (forall S1 (\a. S2)) (forall Q1 (\a. Q2))) ⇒
    (case d2 of
     | [gsa_top] ⇒ [gsa_top]
     | [gsa_all E1 (\a. \tr. \w. \ref. E2)] :
         (gsub (forall Q1 (\a. Q2)) (forall T1 (\a. T2))) ⇒
       let [gF1] = trans [gQ1] [gE1] [gD1] in
       let
         [g, b :
           block (a : tp,
                  tr : {u' : tp} {t' : tp} sub a u'sub u' t'sub a t',
                  w : sub a T1[…],
                  ref : sub a a) ⊢ F2[…, b.1, b.2, b.3, b.4]] =
         trans
           [g, b :
             block (a : tp,
                    tr : {u' : tp} {t' : tp} sub a u'sub u' t'sub a t',
                    w : sub a T1[…],
                    ref : sub a a) ⊢ Q2[…, b.1]]
           [g, bD2[…, b.1, b.2, b.2 T1[…] Q1[…] b.3 E1[…], b.4]]
           [g, bE2[…, b.1, b.2, b.3, b.4]] in
       [gsa_all F1 (\a. \tr. \w. \ref. F2)]);

We only mark out this special case:

If d1 : [g |- sub (forall S1 (\a.S2)) (forall Q1 (\a.Q2))]

and d2 : [g |- sub (forall Q1 (\a.Q2)) (forall T1 (\a.T2))]

then f : [g |- sub (forall S1 (\a.S2) (forall T1 (\a.T2))].

By inversion on d1, we have:

D1 : [g |- sub Q1 S1]

D2 : [g, b : block (a:tp, tr:{u':tp}{t':tp} sub a u' -> sub u' t' -> sub a t', w: sub a Q1[..], ref: sub a a ) |- sub S2[.. b.1] Q2[.., b.1]]

By inversion on d2:

E1 : [g |- sub T1 Q1]

E2 : [g, b : block (a:tp, tr:{u':tp}{t':tp} sub a u' -> sub u' t' -> sub a t', w: sub a T1, ref: sub a a ) |- sub (Q2 b.1) (T2 b.1)]

By the i.h. on E1 and D1 we can obtain a derivation F1 : [g |- sub T1 S1].

We would like to obtain another derivation F2 : [g, b : block (a:tp, tr:{u':tp}{t':tp} sub a u' -> sub u' t' -> sub a t', w: sub a T1[…], ref: sub a a ) |- sub S2[… , b.1] T2[ … , b.1]]. However, applying the induction hypothesis on D2 and E2 will not work because D2 depends on the assumption w: sub a Q1 while E2 depends on the assumption w: sub a T1. We need to first create a derivation D2' which depends on w: sub a T1[..]. Recall D2 is a parametric and hypothetical derivation which can be viewed as a function which expects as inputs tr: sub a u' -> sub u' t' -> sub a t', ref: sub a a, and an object of type sub a Q1[..]. In other words, we need to turn a function which expects sub a Q1 into a function which expects an object of type sub a T1. We use b.2 T1[..] Q1[..] b.3 E1[..] to achieve this transformation.

Declarative subtyping

We can also define declarative subtyping in Beluga and prove the equivalence between algoritmic and declarative subtyping through soundness and completeness proofs. We use (\(<\)) or subtype to denote declarative subtyping.

LF subtype : tptptype =
| subtype_top : subtype T top
| subtype_refl : subtype T T
| subtype_trans : subtype T1 T2subtype T2 T3subtype T1 T3
| subtype_arrow :
  subtype T1 S1subtype S2 T2subtype (arr S1 S2) (arr T1 T2)
| subtype_forall :
  subtype T1 S1 →
    ({x : tp} subtype x T1subtype (S2 x) (T2 x)) →
      subtype (forall S1 S2) (forall T1 T2);

In order to prove soundness and completeness, we define a new context which includes both algorithmic subtyping and declarative subtyping.

schema ss_ctx =
  some [u : tp]
  block (a : tp,
         tr : {U : tp} {T : tp} sub a Usub U Tsub a T,
         w : sub a u,
         ref : sub a a,
         w' : subtype a u);

Soundness

Theorem : For every \(\Gamma\), \(T\) and \(S\), if \(\Gamma\vdash T<:S\), then \(\Gamma\vdash T < S\) .

rec sound : (g : ss_ctx) (gsub T S) → (gsubtype T S) =
fn dcase d of
  | [gsa_top] ⇒ [gsubtype_top]
  | [gsa_arr D1 D2] ⇒ let [gE1] = sound [gD1] in
    let [gE2] = sound [gD2] in [gsubtype_arrow E1 E2]
  | [gsa_all D1 (\a. \tr. \w. \ref. D2)] :
      (gsub (forall S1 (\a. S2)) (forall T1 (\a. T2))) ⇒
    let [gE1] = sound [gD1] in
    let
      [g, b :
        block (a : tp,
               tr : {u' : tp} {t' : tp} sub a u'sub u' t'sub a t',
               w : sub a T1[…],
               ref : sub a a,
               w' : subtype a T1[…]) ⊢ E2[…, b.1, b.5]] =
      sound
        [g, b :
          block (a : tp,
                 tr : {u' : tp} {t' : tp} sub a u'sub u' t'sub a t',
                 w : sub a T1[…],
                 ref : sub a a,
                 w' : subtype a T1[…]) ⊢ D2[…, b.1, b.2, b.3, b.4]] in
    [gsubtype_forall E1 (\a. \w. E2)]
  | [g#p.3] ⇒ [g#p.5]
  | [g#p.4] ⇒ [gsubtype_refl]
  | {#p :
      #(gblock (a : tp,
               tr : {u' : tp} {t' : tp} sub a u'sub u' t'sub a t',
               w : sub a U'[…],
               ref : sub a a,
               w' : subtype a U'[…]))} {D1 : (gsub #p.1 U')}
    [g#p.2 U' T' D1 D2] ⇒ let [gE1] = sound [gD1] in
    let [gE2] = sound [gD2] in [gsubtype_trans E1 E2];

Completeness

Theorem: For every \(\Gamma\), \(T\) and \(S\), if \(\Gamma\vdash T < S\), then \(\Gamma\vdash T <: S\).

rec complete : (g : ss_ctx) (gsubtype T S) → (gsub T S) =
fn dcase d of
  | [g#p.5] ⇒ [g#p.3]
  | [gsubtype_top] ⇒ [gsa_top]
  | [gsubtype_refl] ⇒ refl [g] [g ⊢ _]
  | [gsubtype_trans E1 E2] ⇒ let [gD1] = complete [gE1] in
    let [gD2] = complete [gE2] in trans [g ⊢ _] [gD1] [gD2]
  | [gsubtype_arrow E1 E2] ⇒ let [gD1] = complete [gE1] in
    let [gD2] = complete [gE2] in [gsa_arr D1 D2]
  | [gsubtype_forall E1 (\x. \y. E2)] :
      (gsubtype (forall S1 (\a. S2)) (forall T1 (\a. T2))) ⇒
    let [gD1] = complete [gE1] in
    let
      [g, b :
        block (a : tp,
               tr : {u' : tp} {t' : tp} sub a u'sub u' t'sub a t',
               w : sub a T1[…],
               ref : sub a a,
               w' : subtype a T1[…]) ⊢ D2[…, b.1, b.2, b.3, b.4]] =
      complete
        [g, b :
          block (a : tp,
                 tr : {u' : tp} {t' : tp} sub a u'sub u' t'sub a t',
                 w : sub a T1[…],
                 ref : sub a a,
                 w' : subtype a T1[…]) ⊢ E2[…, b.1, b.5]] in
    [gsa_all D1 (\a. \tr. \w. \ref. D2)];
Download the code

© Computation and Logic Group McGill University Logo
Department of Computer Science, McGill University
3480 University Street, Montreal, Quebec, Canada, H3A 0E9