Library PLF.Sub
Set Warnings "-notation-overridden,-parsing,-deprecated-hint-without-locality".
From Stdlib Require Import Strings.String.
From PLF Require Import Maps.
From PLF Require Import Types.
From PLF Require Import Smallstep.
Set Default Goal Selector "!".
Concepts
A Motivating Example
- S is a subtype of T, written S <: T, if a value of type S can safely be used in any context where a value of type T is expected.
Subtyping and Object-Oriented Languages
The Subsumption Rule
- Defining a binary subtype relation between types.
- Enriching the typing relation to take subtyping into account.
(T_Sub) Gamma |-- t1 \in T2
The Subtype Relation
Structural Rules
(S_Trans) S <: T
(S_Refl) T <: T
Products
(S_Prod) S1 * S2 <: T1 * T2
Arrows
(S_Arrow_Co) S1 -> S2 <: S1 -> T2
(S_Arrow) S1 -> S2 <: T1 -> T2
Records
(S_Rcd) {i1:S1...im:Sm} <: {j1:T1...jn:Tn}
(S_RcdWidth) {i1:T1...in:Tn} <: {i1:T1...im:Tm}
(S_RcdDepth) {i1:S1...in:Sn} <: {i1:T1...in:Tn}
(S_RcdPerm) {i1:S1...in:Sn} <: {j1:T1...jn:Tn}
- Each class member (field or method) can be assigned a single
index, adding new indices "on the right" as more members are
added in subclasses (i.e., no permutation for classes).
- A class may implement multiple interfaces -- so-called "multiple
inheritance" of interfaces (i.e., permutation is allowed for
interfaces).
- In early versions of Java, a subclass could not change the argument or result types of a method of its superclass (i.e., no depth subtyping or no arrow subtyping, depending how you look at it).
Exercise: 2 stars, standard, especially useful (arrow_sub_wrong)
(S_Arrow_wrong) S1 -> S2 <: T1 -> T2
☐
Top
(S_Top) S <: Top
Summary
- adding a base type Top,
- adding the rule of subsumption
Gamma |-- t1 \in T1 T1 <: T2
- ------------------------------- (T_Sub)
Gamma |-- t1 \in T2
- ------------------------------- (T_Sub)
Gamma |-- t1 \in T2
- defining a subtype relation as follows:
S <: U U <: T
- --------------- (S_Trans)
S <: T
- ----- (S_Refl)
- ------- (S_Top)
- ------------------- (S_Prod)
S1 * S2 <: T1 * T2
- ------------------- (S_Arrow)
n > m- -------------------------------- (S_RcdWidth)
S1 <: T1 ... Sn <: Tn- --------------------------------- (S_RcdDepth)
{i1:S1...in:Sn} is a permutation of {j1:T1...jn:Tn}- -------------------------------------------------- (S_RcdPerm) {i1:S1...in:Sn} <: {j1:T1...jn:Tn}
- --------------- (S_Trans)
S <: T
Exercises
Exercise: 1 star, standard, optional (subtype_instances_tf_1)
- T→S <: T→S
- Top→U <: S→Top
- (C→C) → (A×B) <: (C→C) → (Top×B)
- T→T→U <: S→S→V
- (T→T)->U <: (S→S)->V
- ((T→S)->T)->U <: ((S→T)->S)->V
- S×V <: T×U
Exercise: 2 stars, standard (subtype_order)
- Top
- Top → Student
- Student → Person
- Student → Top
- Person → Student
☐
Which of the following statements are true? Write true or
false after each one.
forall S T,
S <: T ->
S->S <: T->T
forall S,
S <: A->A ->
exists T,
S = T->T /\ T <: A
forall S T1 T2,
(S <: T1 -> T2) ->
exists S1 S2,
S = S1 -> S2 /\ T1 <: S1 /\ S2 <: T2
exists S,
S <: S->S
exists S,
S->S <: S
forall S T1 T2,
S <: T1*T2 ->
exists S1 S2,
S = S1*S2 /\ S1 <: T1 /\ S2 <: T2
Exercise: 1 star, standard (subtype_instances_tf_2)
☐
Which of the following statements are true, and which are false?
Exercise: 1 star, standard (subtype_concepts_tf)
- There exists a type that is a supertype of every other type.
- There exists a type that is a subtype of every other type.
- There exists a pair type that is a supertype of every other
pair type.
- There exists a pair type that is a subtype of every other
pair type.
- There exists an arrow type that is a supertype of every other
arrow type.
- There exists an arrow type that is a subtype of every other
arrow type.
- There is an infinite descending chain of distinct types in the
subtype relation---that is, an infinite sequence of types
S0, S1, etc., such that all the Si's are different and
each S(i+1) is a subtype of Si.
- There is an infinite ascending chain of distinct types in the subtype relation---that is, an infinite sequence of types S0, S1, etc., such that all the Si's are different and each S(i+1) is a supertype of Si.
☐
Is the following statement true or false? Briefly explain your
answer. (Here Base n stands for a base type, where n is
a string standing for the name of the base type. See the
Syntax section below.)
forall T,
~(T = Bool \/ exists n, T = Base n) ->
exists S,
S <: T /\ S <> T
Exercise: 2 stars, standard (proper_subtypes)
☐
Exercise: 2 stars, standard (small_large_1)
- What is the smallest type T ("smallest" in the subtype
relation) that makes the following assertion true? (Assume we
have Unit among the base types and unit as a constant of this
type.)
empty |-- (\p:T*Top, p.fst) ((\z:A,z), unit) \in A->A
- What is the largest type T that makes the same assertion true?
☐
Exercise: 2 stars, standard (small_large_2)
- What is the smallest type T that makes the following
assertion true?
empty |-- (\p:(A->A * B->B), p) ((\z:A,z), (\z:B,z)) \in T
- What is the largest type T that makes the same assertion true?
☐
Exercise: 2 stars, standard, optional (small_large_3)
- What is the smallest type T that makes the following
assertion true?
a:A |-- (\p:(A*T), (p.snd) (p.fst)) (a, \z:A,z) \in A
- What is the largest type T that makes the same assertion true?
☐
Exercise: 2 stars, standard (small_large_4)
- What is the smallest type T (if one exists) that makes the
following assertion true?
exists S, empty |-- (\p:(A*T), (p.snd) (p.fst)) \in S
- What is the largest type T that makes the same assertion true?
☐
What is the smallest type T (if one exists) that makes
the following assertion true?
exists S t,
empty |-- (\x:T, x x) t \in S
Exercise: 2 stars, standard (smallest_1)
☐
What is the smallest type T that makes the following
assertion true?
empty |-- (\x:Top, x) ((\z:A,z) , (\z:B,z)) \in T
Exercise: 2 stars, standard (smallest_2)
☐
How many supertypes does the record type {x:A, y:C→C} have? That is,
how many different types T are there such that {x:A, y:C→C} <:
T? (We consider two types to be different if they are written
differently, even if each is a subtype of the other. For example,
{x:A,y:B} and {y:B,x:A} are different.)
☐
The subtyping rule for product types
S1 <: T1 S2 <: T2
(S_Prod) S1*S2 <: T1*T2
intuitively corresponds to the "depth" subtyping rule for records.
Extending the analogy, we might consider adding a "permutation" rule
T1*T2 <: T2*T1
for products. Is this a good idea? Briefly explain why or why not.
Exercise: 3 stars, standard, optional (count_supertypes)
Exercise: 2 stars, standard (pair_permutation)
(S_Prod) S1*S2 <: T1*T2
T1*T2 <: T2*T1
☐
Most of the definitions needed to formalize what we've discussed
above -- in particular, the syntax and operational semantics of
the language -- are identical to what we saw in the last chapter.
We just need to extend the typing relation with the subsumption
rule and add a new Inductive definition for the subtyping
relation. Let's first do the identical bits.
We include products in the syntax of types and terms, but not,
for the moment, anywhere else; the products exercise below will
ask you to extend the definitions of the value relation, operational
semantics, subtyping relation, and typing relation and to extend
the proofs of progress and preservation to fully support products.
Syntax
Inductive ty : Type :=
| Ty_Top : ty
| Ty_Bool : ty
| Ty_Base : string → ty
| Ty_Arrow : ty → ty → ty
| Ty_Unit : ty
| Ty_Prod : ty → ty → ty
.
Inductive tm : Type :=
| tm_var : string → tm
| tm_app : tm → tm → tm
| tm_abs : string → ty → tm → tm
| tm_true : tm
| tm_false : tm
| tm_if : tm → tm → tm → tm
| tm_unit : tm
| tm_pair : tm → tm → tm
| tm_fst : tm → tm
| tm_snd : tm → tm
.
Declare Custom Entry stlc_ty.
Declare Custom Entry stlc_tm.
Declare Scope stlc_scope.
Open Scope stlc_scope.
Notation "x" := x (in custom stlc_ty at level 0, x global) : stlc_scope.
Notation "<{{ x }}>" := x (x custom stlc_ty).
Notation "( t )" := t (in custom stlc_ty at level 0, t custom stlc_ty) : stlc_scope.
Notation "S -> T" := (Ty_Arrow S T) (in custom stlc_ty at level 99, right associativity) : stlc_scope.
Notation "$( t )" := t (in custom stlc_ty at level 0, t constr) : stlc_scope.
Notation "$( x )" := x (in custom stlc_tm at level 0, x constr, only parsing) : stlc_scope.
Notation "x" := x (in custom stlc_tm at level 0, x constr at level 0) : stlc_scope.
Notation "<{ e }>" := e (e custom stlc_tm at level 200) : stlc_scope.
Notation "( x )" := x (in custom stlc_tm at level 0, x custom stlc_tm) : stlc_scope.
Notation "x y" := (tm_app x y) (in custom stlc_tm at level 10, left associativity) : stlc_scope.
Notation "\ x : t , y" :=
(tm_abs x t y) (in custom stlc_tm at level 200, x global,
t custom stlc_ty,
y custom stlc_tm at level 200,
left associativity).
Coercion tm_var : string >-> tm.
Arguments tm_var _%_string.
Notation "'Bool'" := Ty_Bool (in custom stlc_ty at level 0) : stlc_scope.
Notation "'if' x 'then' y 'else' z" :=
(tm_if x y z) (in custom stlc_tm at level 200,
x custom stlc_tm,
y custom stlc_tm,
z custom stlc_tm at level 200,
left associativity).
Notation "'true'" := true (at level 1).
Notation "'true'" := tm_true (in custom stlc_tm at level 0).
Notation "'false'" := false (at level 1).
Notation "'false'" := tm_false (in custom stlc_tm at level 0).
Notation "'Base' x" := (Ty_Base x) (in custom stlc_ty at level 0, x constr at level 0) : stlc_scope.
Notation "'Unit'" :=
(Ty_Unit) (in custom stlc_ty at level 0) : stlc_scope.
Notation "'unit'" := tm_unit (in custom stlc_tm at level 0) : stlc_scope.
Notation "X * Y" :=
(Ty_Prod X Y) (in custom stlc_ty at level 2, X custom stlc_ty, Y custom stlc_ty at level 0).
Notation "( x ',' y )" := (tm_pair x y) (in custom stlc_tm at level 0,
x custom stlc_tm,
y custom stlc_tm) : stlc_scope.
Notation "t '.fst'" := (tm_fst t) (in custom stlc_tm at level 1) : stlc_scope.
Notation "t '.snd'" := (tm_snd t) (in custom stlc_tm at level 1) : stlc_scope.
Notation "'Top'" := (Ty_Top) (in custom stlc_ty at level 0) : stlc_scope.
Reserved Notation "'[' x ':=' s ']' t" (in custom stlc_tm at level 5, x global, s custom stlc_tm,
t custom stlc_tm at next level, right associativity).
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)}>
| <{true}> ⇒
<{true}>
| <{false}> ⇒
<{false}>
| <{if t1 then t2 else t3}> ⇒
<{if ([x:=s] t1) then ([x:=s] t2) else ([x:=s] t3)}>
| <{unit}> ⇒
<{unit}>
| <{ (t1, t2) }> ⇒
<{( [x:=s] t1, [x:=s] t2 )}>
| <{t0.fst}> ⇒
<{ ([x:=s] t0).fst}>
| <{t0.snd}> ⇒
<{ ([x:=s] t0).snd}>
end
where "'[' x ':=' s ']' t" := (subst x s t) (in custom stlc_tm) : stlc_scope.
Inductive value : tm → Prop :=
| v_abs : ∀ x T2 t1,
value <{\x:T2, t1}>
| v_true :
value <{true}>
| v_false :
value <{false}>
| v_unit :
value <{unit}>
.
Hint Constructors value : core.
Reserved Notation "t '-->' t'" (at level 40).
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_IfTrue : ∀ t1 t2,
<{if true then t1 else t2}> --> t1
| ST_IfFalse : ∀ t1 t2,
<{if false then t1 else t2}> --> t2
| ST_If : ∀ t1 t1' t2 t3,
t1 --> t1' →
<{if t1 then t2 else t3}> --> <{if t1' then t2 else t3}>
where "t '-->' t'" := (step t t').
Hint Constructors step : core.
Subtyping
Reserved Notation "T '<:' U" (at level 40).
Inductive subtype : ty → ty → Prop :=
| S_Refl : ∀ T,
T <: T
| S_Trans : ∀ S U T,
S <: U →
U <: T →
S <: T
| S_Top : ∀ S,
S <: <{{ Top }}>
| S_Arrow : ∀ S1 S2 T1 T2,
T1 <: S1 →
S2 <: T2 →
<{{ S1→S2 }}> <: <{{ T1→T2 }}>
where "T '<:' U" := (subtype T U).
Note that we don't need any special rules for base types (Bool
and Base): they are automatically subtypes of themselves (by
S_Refl) and Top (by S_Top), and that's all we want.
Hint Constructors subtype : core.
Module Examples.
Open Scope string_scope.
Notation x := "x".
Notation y := "y".
Notation z := "z".
Notation A := <{{ Base "A" }}>.
Notation B := <{{ Base "B" }}>.
Notation C := <{{ Base "C" }}>.
Notation String := <{{ Base "String" }}>.
Notation Float := <{{ Base "Float" }}>.
Notation Integer := <{{ Base "Integer" }}>.
Example subtyping_example_0 :
<{{ C→Bool }}> <: <{{ C→Top }}>.
Proof. auto. Qed.
Exercise: 2 stars, standard, optional (subtyping_judgements)
Definition Person : ty
. Admitted.
Definition Student : ty
. Admitted.
Definition Employee : ty
. Admitted.
. Admitted.
Definition Student : ty
. Admitted.
Definition Employee : ty
. Admitted.
Now use the definition of the subtype relation to prove the following:
Example sub_student_person :
Student <: Person.
Proof.
Admitted.
Example sub_employee_person :
Employee <: Person.
Proof.
Admitted.
☐
The following facts are mostly easy to prove in Coq. To get
full benefit from the exercises, make sure you also
understand how to prove them on paper!
Exercise: 1 star, standard, optional (subtyping_example_1)
Example subtyping_example_1 :
<{{ Top→Student }}> <: <{{ (C→C)→Person }}>.
Proof with eauto.
Admitted.
<{{ Top→Student }}> <: <{{ (C→C)→Person }}>.
Proof with eauto.
Admitted.
☐
Definition context := partial_map ty.
Notation "x '|->' v ';' m " := (update m x v)
(in custom stlc_tm at level 0, x constr at level 0, v custom stlc_ty, right associativity) : stlc_scope.
Notation "x '|->' v " := (update empty x v)
(in custom stlc_tm at level 0, x constr at level 0, v custom stlc_ty) : stlc_scope.
Notation "'empty'" := empty (in custom stlc_tm) : stlc_scope.
Reserved Notation "<{ Gamma '|--' t '\in' T }>"
(at level 0, Gamma custom stlc_tm at level 200, t custom stlc_tm, T custom stlc_ty).
Inductive has_type : context → tm → ty → Prop :=
| T_Var : ∀ Gamma x T1,
Gamma x = Some T1 →
<{ Gamma |-- x \in T1 }>
| T_Abs : ∀ Gamma x T1 T2 t1,
<{ x |-> T2 ; Gamma |-- t1 \in T1 }> →
<{ Gamma |-- \x:T2, t1 \in T2 → T1 }>
| T_App : ∀ T1 T2 Gamma t1 t2,
<{ Gamma |-- t1 \in T2 → T1 }> →
<{ Gamma |-- t2 \in T2 }> →
<{ Gamma |-- t1 t2 \in T1 }>
| T_True : ∀ Gamma,
<{ Gamma |-- true \in Bool }>
| T_False : ∀ Gamma,
<{ Gamma |-- false \in Bool }>
| T_If : ∀ t1 t2 t3 T1 Gamma,
<{ Gamma |-- t1 \in Bool }> →
<{ Gamma |-- t2 \in T1 }> →
<{ Gamma |-- t3 \in T1 }> →
<{ Gamma |-- if t1 then t2 else t3 \in T1 }>
| T_Unit : ∀ Gamma,
<{ Gamma |-- unit \in Unit }>
| T_Sub : ∀ Gamma t1 T1 T2,
<{ Gamma |-- t1 \in T1 }> →
T1 <: T2 →
<{ Gamma |-- t1 \in T2 }>
where "<{ Gamma '|--' t '\in' T }>" := (has_type Gamma t T) : stlc_scope.
Hint Constructors has_type : core.
Module Examples2.
Import Examples.
Do the following exercises after you have added product types to
the language. For each informal typing judgement, write it as a
formal statement in Coq and prove it.
Exercise: 1 star, standard, optional (typing_example_0)
Properties
Inversion Lemmas for Subtyping
- Bool is the only subtype of Bool, and
- every subtype of an arrow type is itself an arrow type.
Exercise: 2 stars, standard, optional (sub_inversion_Bool)
Lemma sub_inversion_Bool : ∀ U,
U <: <{{ Bool }}> →
U = <{{ Bool }}>.
Proof with auto.
intros U Hs.
remember <{{ Bool }}> as V.
Admitted.
U <: <{{ Bool }}> →
U = <{{ Bool }}>.
Proof with auto.
intros U Hs.
remember <{{ Bool }}> as V.
Admitted.
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.
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.
☐
There are additional inversion lemmas for the other types:
- Unit is the only subtype of Unit, and
- Base n is the only subtype of Base n, and
- Top is the only supertype of Top.
Exercise: 2 stars, standard, optional (sub_inversion_Unit)
Lemma sub_inversion_Unit : ∀ U,
U <: <{{ Unit }}> →
U = <{{ Unit }}>.
Proof with auto.
intros U Hs.
remember <{{ Unit }}> as V.
Admitted.
U <: <{{ Unit }}> →
U = <{{ Unit }}>.
Proof with auto.
intros U Hs.
remember <{{ Unit }}> as V.
Admitted.
Lemma sub_inversion_Base : ∀ U s,
U <: <{{ Base s }}> →
U = <{{ Base s }}>.
Proof with auto.
intros U s Hs.
remember <{{ Base s }}> as V.
Admitted.
U <: <{{ Base s }}> →
U = <{{ Base s }}>.
Proof with auto.
intros U s Hs.
remember <{{ Base s }}> as V.
Admitted.
Lemma sub_inversion_Top : ∀ U,
<{{ Top }}> <: U →
U = <{{ Top }}>.
Proof with auto.
intros U Hs.
remember <{{ Top }}> as V.
Admitted.
<{{ Top }}> <: U →
U = <{{ Top }}>.
Proof with auto.
intros U Hs.
remember <{{ Top }}> as V.
Admitted.
☐
Canonical Forms
Exercise: 3 stars, standard, optional (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.
<{ Gamma |-- s \in T1→T2 }> →
value s →
∃ x S1 s2,
s = <{\x:S1,s2}>.
Proof with eauto.
Admitted.
☐
Similarly, the canonical forms of type Bool are the constants
tm_true and tm_false.
Lemma canonical_forms_of_Bool : ∀ Gamma s,
<{ Gamma |-- s \in Bool }> →
value s →
s = tm_true ∨ s = tm_false.
Proof with eauto.
intros Gamma s Hty Hv.
remember <{{ Bool }}> as T.
induction Hty; try solve_by_invert...
-
subst. apply sub_inversion_Bool in H. subst...
Qed.
Progress
- If the last step in the typing derivation uses rule T_App,
then there are terms t1 t2 and types T1 and T2 such that
t = t1 t2, T = T2, empty |-- t1 \in T1 → T2, and empty
|-- t2 \in T1. Moreover, by the induction hypothesis, either
t1 is a value or it steps, and either t2 is a value or it
steps. There are three possibilities to consider:
- First, suppose t1 --> t1' for some term t1'. Then t1
t2 --> t1' t2 by ST_App1.
- Second, suppose t1 is a value and t2 --> t2' for some term
t2'. Then t1 t2 --> t1 t2' by rule ST_App2 because t1
is a value.
- Third, suppose t1 and t2 are both values. By the
canonical forms lemma for arrow types, we know that t1 has
the form \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.
- First, suppose t1 --> t1' for some term t1'. Then t1
t2 --> t1' t2 by ST_App1.
- If the final step of the derivation uses rule T_If, then
there are terms t1, t2, and t3 such that t = if t1
then t2 else t3, with empty |-- t1 \in Bool and with empty
|-- t2 \in T and empty |-- t3 \in T. Moreover, by the
induction hypothesis, either t1 is a value or it steps.
- If t1 is a value, then by the canonical forms lemma for
booleans, either t1 = true or t1 = false. In
either case, t can step, using rule ST_IfTrue or
ST_IfFalse.
- If t1 can step, then so can t, by rule ST_If.
- If t1 is a value, then by the canonical forms lemma for
booleans, either t1 = true or t1 = false. In
either case, t can step, using rule ST_IfTrue or
ST_IfFalse.
- If the final step of the derivation is by T_Sub, then there is a type T2 such that T1 <: T2 and empty |-- t1 \in T1. The desired result is exactly the induction hypothesis for the typing subderivation.
Theorem progress : ∀ t T,
<{ empty |-- t \in T }> →
value t ∨ ∃ t', t --> t'.
Proof with eauto.
intros t T Ht.
remember empty as Gamma.
induction Ht; subst Gamma; auto.
-
discriminate.
-
right.
destruct IHHt1; subst...
+
destruct IHHt2; subst...
×
eapply canonical_forms_of_arrow_types in Ht1; [|assumption].
destruct Ht1 as [x [S1 [s2 H1]]]. subst.
∃ (<{ [x:=t2]s2 }>)...
×
destruct H0 as [t2' Hstp]. ∃ <{ t1 t2' }>...
+
destruct H as [t1' Hstp]. ∃ <{ t1' t2 }>...
-
right.
destruct IHHt1.
+ eauto.
+ apply canonical_forms_of_Bool in Ht1; [|assumption].
destruct Ht1; subst...
+ destruct H. rename x into t1'. eauto.
Qed.
Inversion Lemmas for Typing
- If the last step of the derivation is a use of T_Abs then
there is a type T12 such that T = S1 → T12 and x:S1;
Gamma |-- t2 \in T12. Picking T12 for S2 gives us what we
need, since S1 → T12 <: S1 → T12 follows from S_Refl.
- If the last step of the derivation is a use of T_Sub then there is a type S such that S <: T and Gamma |-- \x:S1,t2 \in S. The IH for the typing subderivation tells us that there is some type S2 with S1 → S2 <: S and x:S1; Gamma |-- t2 \in S2. Picking type S2 gives us what we need, since S1 → S2 <: T then follows by S_Trans.
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.
-
∃ T1...
-
destruct IHhas_type as [S2 [Hsub Hty]]...
Qed.
Lemma typing_inversion_var : ∀ Gamma (x:string) T,
<{ Gamma |-- x \in T }> →
∃ S,
Gamma x = Some S ∧ S <: T.
Proof with eauto.
Admitted.
<{ Gamma |-- x \in T }> →
∃ S,
Gamma x = Some S ∧ S <: T.
Proof with eauto.
Admitted.
Lemma typing_inversion_app : ∀ Gamma t1 t2 T2,
<{ Gamma |-- t1 t2 \in T2 }> →
∃ T1,
<{ Gamma |-- t1 \in T1→T2 }> ∧
<{ Gamma |-- t2 \in T1 }>.
Proof with eauto.
Admitted.
<{ Gamma |-- t1 t2 \in T2 }> →
∃ T1,
<{ Gamma |-- t1 \in T1→T2 }> ∧
<{ Gamma |-- t2 \in T1 }>.
Proof with eauto.
Admitted.
☐
Lemma typing_inversion_unit : ∀ Gamma T,
<{ Gamma |-- unit \in T }> →
<{{ Unit }}> <: T.
Proof with eauto.
intros Gamma T Htyp. remember <{ unit }> as tu.
induction Htyp;
inversion Heqtu; subst; intros...
Qed.
The inversion lemmas for typing and for subtyping between arrow
types can be packaged up as a useful "combination lemma" telling
us exactly what we'll actually require below.
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 Hty1]].
apply sub_inversion_arrow in Hsub.
destruct Hsub as [U1 [U2 [Heq [Hsub1 Hsub2]]]].
injection Heq as Heq; subst... Qed.
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.
Corollary weakening_empty : ∀ Gamma t T,
<{ empty |-- t \in T }> →
<{ Gamma |-- t \in T }>.
Proof.
intros Gamma t T.
eapply weakening.
discriminate.
Qed.
Substitution
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.
Admitted.
Preservation
- If the final step of the derivation is by T_App, then there
are terms t1 and t2 and types T1 and T2 such that t =
t1 t2, T = T2, empty |-- t1 \in T1 → T2, and empty |--
t2 \in T1.
By 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 \in T2. It then follows by the substitution lemma (substitution_preserves_typing) that empty |-- [x:=t2] t12 \in T2 as desired.
- If the final step of the derivation uses rule T_If, then
there are terms t1, t2, and t3 such that t = if t1 then
t2 else t3, with empty |-- t1 \in Bool and with empty |--
t2 \in T and empty |-- t3 \in T. Moreover, by the induction
hypothesis, if t1 steps to t1' then empty |-- t1' : Bool.
There are three cases to consider, depending on which rule was
used to show t --> t'.
- If t --> t' by rule ST_If, then t' = if t1' then t2
else t3 with t1 --> t1'. By the induction hypothesis,
empty |-- t1' \in Bool, and so empty |-- t' \in T by
T_If.
- If t --> t' by rule ST_IfTrue or ST_IfFalse, then
either t' = t2 or t' = t3, and empty |-- t' \in T
follows by assumption.
- If t --> t' by rule ST_If, then t' = if t1' then t2
else t3 with t1 --> t1'. By the induction hypothesis,
empty |-- t1' \in Bool, and so empty |-- t' \in T by
T_If.
- If the final step of the derivation is by T_Sub, then there is a type S such that S <: T and empty |-- t \in S. The result is immediate by the induction hypothesis for the typing subderivation and an application of T_Sub. ☐
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].
-
inversion HE; subst...
+
destruct (abs_arrow _ _ _ _ _ HT1) as [HA1 HA2].
apply substitution_preserves_typing with T0...
Qed.
Records, via Products and Top
Exercises
Exercise: 2 stars, standard (variations)
- Suppose we add the following typing rule:
<{ Gamma |-- t \in S1->S2 S1 <: T1 T1 <: S1 S2 <: T2
- ---------------------------------- (T_Funny1)
<{ Gamma |-- t \in T1->T2
- ---------------------------------- (T_Funny1)
<{ Gamma |-- t \in T1->T2
- Suppose we add the following reduction rule:
- ------------------- (ST_Funny21)
- Suppose we add the following subtyping rule:
- --------------- (S_Funny3)
- Suppose we add the following subtyping rule:
- --------------- (S_Funny4)
- Suppose we add the following reduction rule:
- -------------------- (ST_Funny5)
- Suppose we add the same reduction rule and a new typing rule:
- -------------------- (ST_Funny5)
- -------------------------- (T_Funny6)
- Suppose we change the arrow subtyping rule to:
S1 <: T1 S2 <: T2
- ---------------- (S_Arrow')
☐
Exercise: Adding Products
Exercise: 5 stars, standard (products)
- Constructors for pairs, first and second projections, and
product types have already been added to the definitions of
ty and tm. Also, the definition of substitution has been
extended.
- Extend the surrounding definitions accordingly (refer to chapter
MoreSTLC):
- value relation
- operational semantics
- typing relation
- Extend the subtyping relation with this rule:
S1 <: T1 S2 <: T2
- ------------------- (S_Prod)
S1 * S2 <: T1 * T2
- ------------------- (S_Prod)
S1 * S2 <: T1 * T2
- Extend the proofs of progress, preservation, and all their supporting lemmas to deal with the new constructs. (You'll also need to add a couple of completely new lemmas.)
Definition manual_grade_for_products_value_step : option (nat×string) := None.
Definition manual_grade_for_products_subtype_has_type : option (nat×string) := None.
Definition manual_grade_for_products_progress : option (nat×string) := None.
Definition manual_grade_for_products_preservation : option (nat×string) := None.
☐
Formalized "Thought Exercises"
Module FormalThoughtExercises.
Import Examples.
Notation p := "p".
Notation a := "a".
Definition TF P := P ∨ ¬P.
Theorem formal_subtype_instances_tf_1a:
TF (∀ S T U V, S <: T → U <: V →
<{{ T→S }}> <: <{{ T→S }}>).
Proof.
Admitted.
TF (∀ S T U V, S <: T → U <: V →
<{{ T→S }}> <: <{{ T→S }}>).
Proof.
Admitted.
Theorem formal_subtype_instances_tf_1b:
TF (∀ S T U V, S <: T → U <: V →
<{{ Top→U }}> <: <{{ S→Top }}>).
Proof.
Admitted.
TF (∀ S T U V, S <: T → U <: V →
<{{ Top→U }}> <: <{{ S→Top }}>).
Proof.
Admitted.
Theorem formal_subtype_instances_tf_1c:
TF (∀ S T U V, S <: T → U <: V →
<{{ (C→C)→(A×B) }}> <: <{{ (C→C)→(Top×B) }}>).
Proof.
Admitted.
TF (∀ S T U V, S <: T → U <: V →
<{{ (C→C)→(A×B) }}> <: <{{ (C→C)→(Top×B) }}>).
Proof.
Admitted.
Theorem formal_subtype_instances_tf_1d:
TF (∀ S T U V, S <: T → U <: V →
<{{ T→(T→U) }}> <: <{{ S→(S→V) }}>).
Proof.
Admitted.
TF (∀ S T U V, S <: T → U <: V →
<{{ T→(T→U) }}> <: <{{ S→(S→V) }}>).
Proof.
Admitted.
Theorem formal_subtype_instances_tf_1e:
TF (∀ S T U V, S <: T → U <: V →
<{{ (T→T)→U }}> <: <{{ (S→S)→V }}>).
Proof.
Admitted.
TF (∀ S T U V, S <: T → U <: V →
<{{ (T→T)→U }}> <: <{{ (S→S)→V }}>).
Proof.
Admitted.
Theorem formal_subtype_instances_tf_1f:
TF (∀ S T U V, S <: T → U <: V →
<{{ ((T→S)→T)→U }}> <: <{{ ((S→T)→S)→V }}>).
Proof.
Admitted.
TF (∀ S T U V, S <: T → U <: V →
<{{ ((T→S)→T)→U }}> <: <{{ ((S→T)→S)→V }}>).
Proof.
Admitted.
Theorem formal_subtype_instances_tf_1g:
TF (∀ S T U V, S <: T → U <: V →
<{{ S×V }}> <: <{{ T×U }}>).
Proof.
Admitted.
TF (∀ S T U V, S <: T → U <: V →
<{{ S×V }}> <: <{{ T×U }}>).
Proof.
Admitted.
Theorem formal_subtype_instances_tf_2a:
TF (∀ S T,
S <: T →
<{{ S→S }}> <: <{{ T→T }}>).
Proof.
Admitted.
TF (∀ S T,
S <: T →
<{{ S→S }}> <: <{{ T→T }}>).
Proof.
Admitted.
Theorem formal_subtype_instances_tf_2b:
TF (∀ S,
S <: <{{ A→A }}> →
∃ T,
S = <{{ T→T }}> ∧ T <: A).
Proof.
Admitted.
TF (∀ S,
S <: <{{ A→A }}> →
∃ T,
S = <{{ T→T }}> ∧ T <: A).
Proof.
Admitted.
☐
Hint: Assert a generalization of the statement to be proved and
use induction on a type (rather than on a subtyping
derviation).
Exercise: 2 stars, standard, optional (formal_subtype_instances_tf_2d)
Theorem formal_subtype_concepts_tfc:
TF (∃ T1 T2, ∀ S1 S2, <{{ S1×S2 }}> <: <{{ T1×T2 }}>).
Proof.
Admitted.
TF (∃ T1 T2, ∀ S1 S2, <{{ S1×S2 }}> <: <{{ T1×T2 }}>).
Proof.
Admitted.
Theorem formal_subtype_concepts_tfd:
TF (∃ T1 T2, ∀ S1 S2, <{{ T1×T2 }}> <: <{{ S1×S2 }}>).
Proof.
Admitted.
TF (∃ T1 T2, ∀ S1 S2, <{{ T1×T2 }}> <: <{{ S1×S2 }}>).
Proof.
Admitted.
Theorem formal_subtype_concepts_tfe:
TF (∃ T1 T2, ∀ S1 S2, <{{ S1→S2 }}> <: <{{ T1→T2 }}>).
Proof.
Admitted.
TF (∃ T1 T2, ∀ S1 S2, <{{ S1→S2 }}> <: <{{ T1→T2 }}>).
Proof.
Admitted.
Theorem formal_subtype_concepts_tff:
TF (∃ T1 T2, ∀ S1 S2, <{{ T1→T2 }}> <: <{{ S1→S2 }}>).
Proof.
Admitted.
TF (∃ T1 T2, ∀ S1 S2, <{{ T1→T2 }}> <: <{{ S1→S2 }}>).
Proof.
Admitted.
Theorem formal_subtype_concepts_tfg:
TF (∃ f : nat → ty,
(∀ i j, i ≠ j → f i ≠ f j) ∧
(∀ i, f (S i) <: f i)).
Proof.
Admitted.
Theorem formal_subtype_concepts_tfh:
TF (∃ f : nat → ty,
(∀ i j, i ≠ j → f i ≠ f j) ∧
(∀ i, f i <: f (S i))).
Proof.
Admitted.
TF (∃ f : nat → ty,
(∀ i j, i ≠ j → f i ≠ f j) ∧
(∀ i, f i <: f (S i))).
Proof.
Admitted.
Theorem formal_proper_subtypes:
TF (∀ T,
~(T = <{{ Bool }}> ∨ (∃ n, T = <{{ Base n }}>) ∨ T = <{{ Unit }}>) →
∃ S,
S <: T ∧ S ≠ T).
Proof.
Admitted.
TF (∀ T,
~(T = <{{ Bool }}> ∨ (∃ n, T = <{{ Base n }}>) ∨ T = <{{ Unit }}>) →
∃ S,
S <: T ∧ S ≠ T).
Proof.
Admitted.
☐