12 KiB
This lemma wasn't part of the exercises assigned but it's needed for the exercises and not proven in the book so I thought I'd sketch it out quickly.
Lemma 15.3.2 (Inversion of the subtype relation):
- If
S <: T₁ → T₂
, thenS = S₁ → S₂
such thatT₁ <: S₁
andS₂ <: T₂
. - If
S <: {lᵢ : Tᵢ}
, thenS = {kⱼ : Sⱼ}
such that{lᵢ} ⊆ {kⱼ}
andSⱼ <: Tᵢ
whenlᵢ = kⱼ
. Proof: - By induction on the subtyping derivation. The applicable cases are
S-Refl
(holds directly),S-Trans
(holds by induction hypothesis twice), andS-Arrow
(holds directly). - By induction on the subtyping derivation. The applicable cases are
S-Refl
(holds directly),S-Trans
(holds by induction hypothesis twice, usingS-Trans
and transitivity of subsets of labels),S-RcdWidth
(holds directly usingS-Refl
and{l₁..lₙ} ⊆ {l₁..lₙ₊ₖ})
,S-RcdDepth
(holds directly), andS-RcdPerm
(holds by induction hypothesis).
Here are the assigned exercises.
Lemma 15.3.6 (Canonical forms):
- If
v
is a closed value of typeT₁ → T₂
, thenv
has the formλx: S₁. t₂
. - If
v
is a closed value of type{lᵢ : Tᵢ}
, thenv
has the form{kⱼ = vⱼ}
, where{lᵢ} ⊆ {kⱼ}
. Proof: - By induction on the typing derivation of
Γ ⊢ v : T₁ → T₂
. The only applicable cases areT-Abs
andT-Sub
; the first case holds directly. In the second case, we haveΓ ⊢ v : S
andS <: T₁ → T₂
. By inversion on subtyping (Lemma 15.3.2),S = S₁ → S₂
. Thenv
has the form of a function by the induction hypothesis. - By induction on the typing derivation of
Γ ⊢ v : {lᵢ : Tᵢ}
. We proceed similarly to the above, where the applicable cases areT-Rcd
andT-Sub
, the first case holds directly, and the second case holds using inversion (Lemma 15.3.2) and the induction hypothesis. The top-level lemmas hold by instantiatingΓ = ∅
.
Exercise 16.1.3: If we add Bool
, we also need to add the subtyping rule Bool <: Bool
so that the judgement remains reflexive.
Theorem 16.2.5: If Γ ⊢ t : T
, then Γ ⊢> t : S
and ⊢> S <: T
.
Proof: By induction on the typing derivation.
- Case
T-Var
: Holds byTA-Var
. - Case
T-Abs
: The premise isΓ, x: T₁ ⊢ t₂ : T₂
, concluding withΓ ⊢ λx: T₁. t₂ : T₁ → T₂
. By the induction hypothesis, we haveΓ, x: T₁ ⊢> t₂ : S₂
and⊢> S₂ <: T₂
. ByTA-Abs
, we haveΓ ⊢> λx: T₁. t₂ : T₁ → S₂
, and by reflexivity andSA-Arrow
we have⊢> T₁ → S₂ <: T₁ → T₂
. - Case
T-App
: The premises areΓ ⊢ t₁ : T₁₁ → T₁₂
andΓ ⊢ t₂ : T₁₁
, concluding withΓ ⊢ t₁ t₂ : T₁₂
. By the induction hypothesis, we haveΓ ⊢> t₁ : S₁
,⊢> S₁ <: T₁₁ → T₁₂
for the first premise andΓ ⊢> t₂ : S₂
,⊢> S₂ <: T₁₁
for the second. By inversion on subtyping,S₁ = S₁₁ → S₁₂
,⊢> T₁₁ <: S₁₁
, and⊢> S₁₂ <: T₁₂
. By transitivity,⊢> S₂ <: T₁₁ <: S₁₁
. Then we can useTA-App
to buildΓ ⊢> t₁ t₂ : S₁₂
, with⊢> S₁₂ <: T₁₂
as needed. - Case
T-Rcd
: The premises areΓ ⊢ tᵢ : Tᵢ
, concluding withΓ ⊢ {lᵢ = tᵢ} : {lᵢ : Tᵢ}
. By the induction hypothesis, we haveΓ ⊢> tᵢ : Sᵢ
and⊢> Sᵢ <: Tᵢ
. ByTA-Rcd
, we haveΓ ⊢> {lᵢ = tᵢ} : {lᵢ : Sᵢ}
, and bySA-Rcd
, we have⊢> {lᵢ : Sᵢ} <: {lᵢ : Tᵢ}
. - Case
T-Proj
: The premise isΓ ⊢ t : {lᵢ : Tᵢ}
, concluding withΓ ⊢ t.lⱼ : Tⱼ
. By the induction hypothesis, we haveΓ ⊢> t : S
and⊢> S <: {lᵢ : Tᵢ}
. By inversion on subtyping,S = {kᵢ : Sᵢ}
, where{lᵢ} ⊆ {kᵢ}
and⊢> Sᵢ <: Tⱼ
whenlⱼ = kᵢ
. Lettinglⱼ = kᵢ
, we can useTA-Proj
to buildΓ ⊢> t.lⱼ : Sᵢ
, with⊢> Sᵢ <: Tⱼ
. - Case
T-Sub
: The premises areΓ ⊢ t : S
andS <: T
, concluding withΓ ⊢ t : T
. By the induction hypothesis, we haveΓ ⊢> t : U
and⊢> U <: S
. By completeness (Proposition 16.1.5), we get⊢> S <: T
. Then by transitivity, we get⊢> U <: S <: T
as needed.
Exercise 16.2.6: I think the system without S-Arrow
would still have the minimal typing property. The proof would proceed exactly as in Theorem 16.2.5. Completeness (S <: T
implies ⊢> S <: T
) still holds, since the induction would just be missing one case.
Lemma (existence of a subtype): For every S
, T
, either there exists some N
such that N <: S
or N <: T
, or no such N
is possible.
Proof: By induction on the structure of S
and T
. If one of the types is Top
, then the other type is a subtype of both by reflexivity and S-Top
. If the types have different shapes (i.e. one is a function type, the other a record type), supposing we have a subtype N
, by inversion on the subtyping derivations, N
would have to be both a function type and a record type, which is a contradiction, so no such N
exists. That leaves the cases where both types are function types or record types.
- Case
S₁ → S₂
,T₁ → T₂
. By the induction hypothesis, eitherS₂
andT₂
have a subtypeN₂
, or no such subtype exists. If it exists, thenTop → N₂
is a subtype, sinceS₁ <: Top
,T₁ <: Top
,N₂ <: S₂
, andN₂ <: T₂
, and we constructTop → N₂ <: S₁ → S₂
andTop → N₂ <: T₁ → T₂
byS-Arrow
. If it does not exist, then supposing we had a subtypeN₁ → N₂
of bothS₁ → S₂
andT₁ → T₂
, by inversion, we haveN₂ <: S₂
andN₂ <: T₂
, which is a contradiction, so there must be no such subtype. - Case
{kᵢ : Sᵢ}
,{lⱼ : Tⱼ}
. By the induction hypothesis, each pairSᵢ
,Tᵢ
either have a subtypeNᵢⱼ
, or no such subtype exists. Let{klᵢⱼ} = {kᵢ} ∪ {lⱼ}
.- If all
Sᵢ
andTⱼ
for whichkᵢ = lⱼ
have a subtype, then we claim the subtype is{klᵢⱼ : Uᵢⱼ}
such thatUᵢⱼ = Nᵢⱼ
ifklᵢⱼ = kᵢ = lⱼ
,Uᵢⱼ = Sᵢ
ifklᵢⱼ = kᵢ
only, orUᵢⱼ = Tⱼ
ifklᵢⱼ = lⱼ
only. We can construct{klᵢⱼ : Uᵢⱼ} <: {kᵢ : Sᵢ}
byS-Rcd
since{kᵢ} ⊆ {klᵢⱼ}
, andkᵢ = kₗᵢⱼ
impliesSᵢ <: Uᵢⱼ
, whereUᵢⱼ = Sᵢ
orUᵢⱼ = Nᵢⱼ
. Similarly, we can construct{klᵢⱼ : Uᵢⱼ} <: {lⱼ : Tⱼ}
byS-Rcd
. - If there exists some
klᵢⱼ
such thatklᵢⱼ = kᵢ = lⱼ
butSᵢ
andTⱼ
have no subtype, then we claim there is no subtype. Supposing there were a subtypeN
, by inversion we must have thatN = {nₘ : Nₘ}
wherekᵢ = nₘ
impliesNₘ <: Sᵢ
andlⱼ = nₘ
impliesNₘ <: Tⱼ
. In the case wherenₘ = klᵢⱼ
, this gives us a subtype ofSᵢ
andTⱼ
, which is a contradiction.
- If all
Proposition 16.3.2:
- For every
S
,T
, there exists someJ = S ∨ T
. - For every
S
,T
,N
such thatN <: S
andN <: T
, there exists someM = S ∧ T
. Proof: By induction on the structure ofS
andT
, proving both simultaneously, although I will write them separately, even though I use the induction hypotheses jointly. - Goal
S ∨ T
. If one of the types isTop
, then the join isTop
. If the types have different shapes, then the join must beTop
, since a function type cannot be the supertype of a record type and vice versa by inversion. That leaves the cases where both types are function types or record types.- Case
S₁ → S₂
,T₁ → T₂
. By the induction hypothesis, we know thatJ₂ = S₂ ∨ T₂
exists. There are two possible subcases to consider, using the above lemma (existence of a subtype).- If
S₁
andT₁
have a subtype, then by the induction hypothesis, we know thatM₁ = S₁ ∧ T₁
exists. We claim thatM₁ → J₂
is the join; we know it is a supertype byS-Arrow
. It remains to show that it is minimal, i.e. for everyU
such thatS₁ → S₂ <: U
andT₁ → T₂ <: U
, we haveM₁ → J₂ <: U
. IfU
isTop
, then we are done. Otherwise, it must be thatU = U₁ → U₂
, and by inversion,U₁ <: S₁
,U₁ <: T₁
,S₂ <: U₂
, andT₂ <: U₂
. It suffices to show thatU₁ <: M₁
andJ₂ <: U₂
. The former holds by maximality of the meetM₁ = S₁ ∨ T₁
, while the latter holds by minimality of the joinJ₂ = S₂ ∨ T₂
. - If
S₁
andT₁
do not have a subtype, then we claim thatTop
is the join. We must show it to be minimal: for everyU
such thatS₁ → S₂ <: U
andT₁ → T₂ <: U
, we haveTop <: U
. IfU
isTop
, then we are done. Otherwise, it must be thatU = U₁ → U₂
, and by inversion,U₁ <: S₁
,U₁ <: T₁
,S₂ <: U₂
, andT₂ <: U₂
. However, this makesU₁
a subtype ofS₁
andT₁
, which is a contradiction.
- If
- Case
{kᵢ : Sᵢ}
,{lⱼ : Tⱼ}
. By the induction hypothesis, we know that everySᵢ
,Tⱼ
have a join. Let{klᵢⱼ | klᵢⱼ = kᵢ = lⱼ}
be the common labels, i.e.{kᵢ} ∩ {lⱼ}
, and letJᵢⱼ = Sᵢ ∨ Tⱼ
. We claim that{klᵢⱼ : Jᵢⱼ}
is the join; we know it is a supertype byS-Rcd
. It remains to show that it is minimal, i.e. for everyU
such that{kᵢ : Sᵢ} <: U
and{lⱼ : Tⱼ} <: U
, we have{klᵢⱼ : Jᵢⱼ} <: U
. IfU
isTop
, then we are done. Otherwise, it must be thatU = {uₙ : Uₙ}
, and by inversion,{uₙ} ⊆ {kᵢ}
,{uₙ} ⊆ {lⱼ}
,kᵢ = uₙ
impliesSᵢ <: Uₙ
, andlⱼ = uₙ
impliesTⱼ <: Uₙ
. Then we know{uₙ} ⊆ {kᵢ} ∩ {lⱼ} = {klᵢⱼ}
, and ifkᵢⱼ = uₙ
, thenSᵢ <: Uₙ
andTᵢ <: Uₙ
. By minimality of the joinJᵢⱼ
, we haveJᵢⱼ <: Uₙ
. Then we can useS-Rcd
to build{klᵢⱼ : Jᵢⱼ} <: U
.
- Case
- Goal
S ∧ T
. If one of the types isTop
, then the meet is the other type. If the types have different shapes, thenN
being a subtype of both is a contradiction by inversion. That leaves the cases where both types are function types or record types.- Case
S₁ → S₂
,T₁ → T₂
. By inversion,N = N₁ → N₂
whereS₁ <: N₁
,T₁ <: N₁
,N₂ <: S₂
,N₂ <: T₂
. By the induction hypothesis, we know thatM₂ = S₂ ∨ T₂
andJ₁ = S₁ ∧ T₁
exist. We claim thatJ₁ → M₂
is the meet; we know it is a subtype byS-Arrow
. It remains to show that it is maximal, i.e. for everyU
such thatU <: S₁ → S₂
andU <: T₁ → T₂
, we haveU <: J₁ → M₂
. By inversion,U = U₁ → U₂
such thatS₁ <: U₁
,T₁ <: U₁
,U₂ <: S₂
, andU₂ <: T₂
. It suffices to show thatJ₁ <: U₁
andU₂ <: M₂
. The former holds by minimality of the joinJ₁ = S₁ ∧ T₁
, while the latter holds by maximality of the meetM₂ = S₂ ∨ T₂
. - Case
{kᵢ : Sᵢ}
,{lⱼ : Tⱼ}
. By inversion,N = {nₘ : Nₘ}
where{nₘ} ⊆ {kᵢ}
,{nₘ} ⊆ {lⱼ}
,kᵢ = nₘ
impliesNₘ <: Sᵢ
, andlⱼ = nₘ
impliesNₘ <: Tⱼ
. By the induction hypothesis, we know that everySᵢ
,Tⱼ
have a meet. Let{klᵢⱼ} = {kᵢ} ∪ {lⱼ}
be the union of all labels, and for everyklᵢⱼ
, letMᵢⱼ = Sᵢ ∧ Tᵢ
ifklᵢⱼ ∈ {kᵢ} ∩ {lⱼ}
,Mᵢⱼ = Sᵢ
ifklᵢⱼ ∈ {kᵢ}
only, andMᵢⱼ = Tᵢ
ifklᵢⱼ ∈ {lⱼ}
only. We claim that{klᵢⱼ : Mᵢⱼ}
is the meet; we know it is a subtype byS-Rcd
. It remains to show that it is maximal, ie. for everyU
such thatU <: {kᵢ : Sᵢ}
andU <: {lⱼ : Tⱼ}
, we haveU <: {Mᵢⱼ : Jᵢⱼ}
. By inversion,U = {uₙ : Uₙ}
where{kᵢ} ⊆ {uₙ}
,{lⱼ} ⊆ {uₙ}
,kᵢ = uₙ
impliesUₙ <: Sᵢ
, andlⱼ = uₙ
impliesUₙ <: Tⱼ
. Then we know{kᵢⱼ} = {kᵢ} ∪ {lⱼ} ⊆ {uₙ}
. Supposingkᵢⱼ = uₙ
, eitheruₙ = kᵢ = lⱼ
, in which caseUₙ <: Sᵢ ∧ Tⱼ
by maximality of the meet, oruₙ = kᵢ
only, in which caseUₙ <: Sᵢ
, oruₙ = lⱼ
only, in which caseUₙ <: Tⱼ
, so together we haveUₙ <: Mᵢⱼ
. Then we can useS-Rcd
to buildU <: {klᵢⱼ : Mᵢⱼ}
.
- Case