# SubSubtyping

Require Export MoreStlc.

# Concepts

*subtyping*, perhaps the most characteristic feature of the static type systems of recently designed programming languages and a key feature needed to support the object-oriented programming style.

## A Motivating Example

Person = {name:String, age:Nat} Student = {name:String, age:Nat, gpa:Nat}

(λr:Person. (r.age)+1) {name="Pat",age=21,gpa=1}is not typable: it involves an application of a function that wants a one-field record to an argument that actually provides two fields, while the T_App rule demands that the domain type of the function being applied must match the type of the argument precisely.

*better*argument than it needs! The only thing the body of the function can possibly do with its record argument r is project the field age from it: nothing else is allowed by the type, and the presence or absence of an extra gpa field makes no difference at all. So, intuitively, it seems that this function should be applicable to any record value that has at least an age field.

*safely*in any context expecting the shorter record type. If the context expects something with the shorter type but we actually give it something with the longer type, nothing bad will happen (formally, the program will not get stuck).

*subtyping*. We say that "S is a subtype of T", informally written S <: T, if a value of type S can safely be used in any context where a value of type T is expected. The idea of subtyping applies not only to records, but to all of the type constructors in the language — functions, pairs, etc.

## Subtyping and Object-Oriented Languages

*subclassing*in object-oriented languages.

*object*in Java, C#, etc. can be thought of as a record, some of whose fields are functions ("methods") and some of whose fields are data values ("fields" or "instance variables"). Invoking a method m of an object o on some arguments a1..an consists of projecting out the m field of o and applying it to a1..an.

*class*or an

*interface*. Both of these provide a description of which methods and which data fields the object offers.

*subclass*and

*subinterface*relations. An object belonging to a subclass (or subinterface) is required to provide all the methods and fields of one belonging to a superclass (or superinterface), plus possibly some more.

*code*that is used when constructing objects and implementing their methods, and the code in subclasses cooperate with code in superclasses via

*inheritance*. Classes can have static methods and fields, initializers, etc., etc.

*Types and Programming Languages*, if you are interested.) Instead, we'll study the core concepts behind the subclass / subinterface relation in the simplified setting of the STLC.

### Of course, real OO languages have lots of other features...

- mutable fields
- private
- method inheritance
- static fields
- etc., etc.

## The Subsumption Rule

- Defining a binary
*subtype relation*between types. - Enriching the typing relation to take subtyping into account.

*rule of subsumption*:

Γ ⊢ t : S S <: T | (T_Sub) |

Γ ⊢ t : T |

## The Subtype Relation

### Structural Rules

*transitivity*, which says intuitively that, if S is better than U and U is better than T, then S is better than T...

S <: U U <: T | (S_Trans) |

S <: T |

*reflexivity*, since any type T is always just as good as itself:

(S_Refl) | |

T <: T |

### Products

S_{1} <: T_{1} S_{2} <: T_{2} |
(S_Prod) |

S_{1}*S_{2} <: T_{1}*T_{2} |

### Arrows

f : C -> Student g : (C -> Person) -> DThat is, f is a function that yields a record of type Student, and g is a (higher-order) function that expects its (function) argument to yield a record of type Person. Also suppose, even though we haven't yet discussed subtyping for records, that Student is a subtype of Person. Then the application g f is safe even though their types do not match up precisely, because the only thing g can do with f is to apply it to some argument (of type C); the result will actually be a Student, while g will be expecting a Person, but this is safe because the only thing g can then do is to project out the two fields that it knows about (name and age), and these will certainly be among the fields that are present.

S_{2} <: T_{2} |
(S_Arrow_Co) |

S_{1}->S_{2} <: S_{1}->T_{2} |

T_{1} <: S_{1} S_{2} <: T_{2} |
(S_Arrow) |

S_{1}->S_{2} <: T_{1}->T_{2} |

_{1}→S

_{2}to be a subtype of T

_{1}→T

_{2}, it must be the case that T

_{1}is a subtype of S

_{1}. The arrow constructor is said to be

*contravariant*in its first argument and

*covariant*in its second.

f : Person -> C g : (Student -> C) -> DThe application g f is safe, because the only thing the body of g can do with f is to apply it to some argument of type Student. Since f requires records having (at least) the fields of a Person, this will always work. So Person → C is a subtype of Student → C since Student is a subtype of Person.

_{1}→S

_{2}, then we know that f accepts elements of type S

_{1}; clearly, f will also accept elements of any subtype T

_{1}of S

_{1}. The type of f also tells us that it returns elements of type S

_{2}; we can also view these results belonging to any supertype T

_{2}of S

_{2}. That is, any function f of type S

_{1}→S

_{2}can also be viewed as having type T

_{1}→T

_{2}.

#### Exercise: 2 stars (arrow_sub_wrong)

Suppose we had incorrectly defined subtyping as covariant on both the right and the left of arrow types:S_{1} <: T_{1} S_{2} <: T_{2} |
(S_Arrow_wrong) |

S_{1}->S_{2} <: T_{1}->T_{2} |

f : Student -> Nat g : (Person -> Nat) -> Nat... such that the application g f will get stuck during execution.

### Records

{name:String, age:Nat, gpa:Nat} <: {name:String, age:Nat} {name:String, age:Nat} <: {name:String} {name:String} <: {}This is known as "width subtyping" for records.

{x:Student} <: {x:Person}This is known as "depth subtyping".

{name:String,age:Nat} <: {age:Nat,name:String}This is known as "permutation subtyping".

for each jk in j1..jn, | |

∃ip in i1..im, such that | |

jk=ip and Sp <: Tk | (S_Rcd) |

{i1:S_{1}...im:Sm} <: {j1:T_{1}...jn:Tn} |

n > m | (S_RcdWidth) |

{i1:T_{1}...in:Tn} <: {i1:T_{1}...im:Tm} |

S_{1} <: T_{1} ... Sn <: Tn |
(S_RcdDepth) |

{i1:S_{1}...in:Sn} <: {i1:T_{1}...in:Tn} |

*end*of a record type. So we need:

{i1:S_{1}...in:Sn} is a permutation of {i1:T_{1}...in:Tn} |
(S_RcdPerm) |

{i1:S_{1}...in:Sn} <: {i1:T_{1}...in:Tn} |

- A subclass may 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).
- Each class has just one superclass ("single inheritance" of
classes).
- 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).

### Top

(S_Top) | |

S <: Top |

### Summary

- adding a base type Top,
- adding the rule of subsumption
to the typing relation, andΓ ⊢ t : S S <: T (T_Sub) Γ ⊢ t : T - defining a subtype relation as follows:
S <: U U <: T (S_Trans) S <: T (S_Refl) T <: T (S_Top) S <: Top S _{1}<: T_{1}S_{2}<: T_{2}(S_Prod) S _{1}*S_{2}<: T_{1}*T_{2}T _{1}<: S_{1}S_{2}<: T_{2}(S_Arrow) S _{1}->S_{2}<: T_{1}->T_{2}n > m (S_RcdWidth) {i1:T _{1}...in:Tn} <: {i1:T_{1}...im:Tm}S _{1}<: T_{1}... Sn <: Tn(S_RcdDepth) {i1:S _{1}...in:Sn} <: {i1:T_{1}...in:Tn}{i1:S _{1}...in:Sn} is a permutation of {i1:T_{1}...in:Tn}(S_RcdPerm) {i1:S _{1}...in:Sn} <: {i1:T_{1}...in:Tn}

## Exercises

#### Exercise: 1 star, optional (subtype_instances_tf_1)

Suppose we have types S, T, U, and V with S <: T and U <: V. Which of the following subtyping assertions are then true? Write*true*or

*false*after each one. (A, B, and C here are base types.)

- 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 (subtype_order)

The following types happen to form a linear order with respect to subtyping:- Top
- Top → Student
- Student → Person
- Student → Top
- Person → Student

#### Exercise: 1 star (subtype_instances_tf_2)

Which of the following statements are true? Write*true*or

*false*after each one.

∀S T,

S <: T →

S→S <: T→T

∀S,

S <: A→A →

∃T,

S = T→T ∧ T <: A

∀S T

(S <: T

∃S

S = S

∃S,

S <: S→S

∃S,

S→S <: S

∀S T

S <: T

∃S

S = S

☐
S <: T →

S→S <: T→T

∀S,

S <: A→A →

∃T,

S = T→T ∧ T <: A

∀S T

_{1}T_{1},(S <: T

_{1}→ T_{2}) →∃S

_{1}S_{2},S = S

_{1}→ S_{2}∧ T_{1}<: S_{1}∧ S_{2}<: T_{2}∃S,

S <: S→S

∃S,

S→S <: S

∀S T

_{2}T_{2},S <: T

_{1}×T_{2}→∃S

_{1}S_{2},S = S

_{1}×S_{2}∧ S_{1}<: T_{1}∧ S_{2}<: T_{2}#### Exercise: 1 star (subtype_concepts_tf)

Which of the following statements are true, and which are false?- 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, S
_{1}, 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, S_{1}, etc., such that all the Si's are different and each S(i+1) is a supertype of Si.

#### Exercise: 2 stars (proper_subtypes)

Is the following statement true or false? Briefly explain your answer.
∀T,

~(∃n, T = TBase n) →

∃S,

S <: T ∧ S ≠ T

☐
~(∃n, T = TBase n) →

∃S,

S <: T ∧ S ≠ T

#### Exercise: 2 stars (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) : A→A - What is the
*largest*type T that makes the same assertion true?

#### Exercise: 2 stars (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)) : T - What is the
*largest*type T that makes the same assertion true?

#### Exercise: 2 stars, 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) : A - What is the
*largest*type T that makes the same assertion true?

#### Exercise: 2 stars (small_large_4)

- What is the
*smallest*type T that makes the following assertion true?∃S,

empty ⊢ (λp:(A×T). (p.snd) (p.fst)) : S - What is the
*largest*type T that makes the same assertion true?

#### Exercise: 2 stars (smallest_1)

What is the*smallest*type T that makes the following assertion true?

∃S, ∃t,

empty ⊢ (λx:T. x x) t : S

☐
empty ⊢ (λx:T. x x) t : S

#### Exercise: 2 stars (smallest_2)

What is the*smallest*type T that makes the following assertion true?

empty ⊢ (λx:Top. x) ((λz:A.z) , (λz:B.z)) : T

☐
#### Exercise: 3 stars, optional (count_supertypes)

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.)#### Exercise: 2 stars (pair_permutation)

The subtyping rule for product typesS_{1} <: T_{1} S_{2} <: T_{2} |
(S_Prod) |

S_{1}*S_{2} <: T_{1}*T_{2} |

T_{1}*T_{2} <: T_{2}*T_{1} |

# Formal Definitions

### Syntax

Inductive ty : Type :=

| TTop : ty

| TBool : ty

| TBase : id → ty

| TArrow : ty → ty → ty

| TUnit : ty

.

Tactic Notation "T_cases" tactic(first) ident(c) :=

first;

[ Case_aux c "TTop" | Case_aux c "TBool"

| Case_aux c "TBase" | Case_aux c "TArrow"

| Case_aux c "TUnit" |

].

Inductive tm : Type :=

| tvar : id → tm

| tapp : tm → tm → tm

| tabs : id → ty → tm → tm

| ttrue : tm

| tfalse : tm

| tif : tm → tm → tm → tm

| tunit : tm

.

Tactic Notation "t_cases" tactic(first) ident(c) :=

first;

[ Case_aux c "tvar" | Case_aux c "tapp"

| Case_aux c "tabs" | Case_aux c "ttrue"

| Case_aux c "tfalse" | Case_aux c "tif"

| Case_aux c "tunit"

].

Fixpoint subst (x:id) (s:tm) (t:tm) : tm :=

match t with

| tvar y ⇒

if eq_id_dec x y then s else t

| tabs y T t

_{1}⇒

tabs y T (if eq_id_dec x y then t

_{1}else (subst x s t

_{1}))

| tapp t

_{1}t

_{2}⇒

tapp (subst x s t

_{1}) (subst x s t

_{2})

| ttrue ⇒

ttrue

| tfalse ⇒

tfalse

| tif t

_{1}t

_{2}t

_{3}⇒

tif (subst x s t

_{1}) (subst x s t

_{2}) (subst x s t

_{3})

| tunit ⇒

tunit

end.

Notation "'[' x ':=' s ']' t" := (subst x s t) (at level 20).

Inductive value : tm → Prop :=

| v_abs : ∀x T t,

value (tabs x T t)

| v_true :

value ttrue

| v_false :

value tfalse

| v_unit :

value tunit

.

Hint Constructors value.

Reserved Notation "t

_{1}'⇒' t

_{2}" (at level 40).

Inductive step : tm → tm → Prop :=

| ST_AppAbs : ∀x T t

_{12}v

_{2},

value v

_{2}→

(tapp (tabs x T t

_{12}) v

_{2}) ⇒ [x:=v

_{2}]t

_{12}

| ST_App1 : ∀t

_{1}t

_{1}' t

_{2},

t

_{1}⇒ t

_{1}' →

(tapp t

_{1}t

_{2}) ⇒ (tapp t

_{1}' t

_{2})

| ST_App2 : ∀v

_{1}t

_{2}t

_{2}',

value v

_{1}→

t

_{2}⇒ t

_{2}' →

(tapp v

_{1}t

_{2}) ⇒ (tapp v

_{1}t

_{2}')

| ST_IfTrue : ∀t

_{1}t

_{2},

(tif ttrue t

_{1}t

_{2}) ⇒ t

_{1}

| ST_IfFalse : ∀t

_{1}t

_{2},

(tif tfalse t

_{1}t

_{2}) ⇒ t

_{2}

| ST_If : ∀t

_{1}t

_{1}' t

_{2}t

_{3},

t

_{1}⇒ t

_{1}' →

(tif t

_{1}t

_{2}t

_{3}) ⇒ (tif t

_{1}' t

_{2}t

_{3})

where "t

_{1}'⇒' t

_{2}" := (step t

_{1}t

_{2}).

Tactic Notation "step_cases" tactic(first) ident(c) :=

first;

[ Case_aux c "ST_AppAbs" | Case_aux c "ST_App1"

| Case_aux c "ST_App2" | Case_aux c "ST_IfTrue"

| Case_aux c "ST_IfFalse" | Case_aux c "ST_If"

].

Hint Constructors step.

## 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 <: TTop

| S_Arrow : ∀S

_{1}S

_{2}T

_{1}T

_{2},

T

_{1}<: S

_{1}→

S

_{2}<: T

_{2}→

(TArrow S

_{1}S

_{2}) <: (TArrow T

_{1}T

_{2})

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

Note that we don't need any special rules for base types: they are
automatically subtypes of themselves (by S_Refl) and Top (by
S_Top), and that's all we want.

Hint Constructors subtype.

Tactic Notation "subtype_cases" tactic(first) ident(c) :=

first;

[ Case_aux c "S_Refl" | Case_aux c "S_Trans"

| Case_aux c "S_Top" | Case_aux c "S_Arrow"

].

Module Examples.

Notation x := (Id 0).

Notation y := (Id 1).

Notation z := (Id 2).

Notation A := (TBase (Id 6)).

Notation B := (TBase (Id 7)).

Notation C := (TBase (Id 8)).

Notation String := (TBase (Id 9)).

Notation Float := (TBase (Id 10)).

Notation Integer := (TBase (Id 11)).

#### Exercise: 2 stars, optional (subtyping_judgements)

Person := { name : String }

Student := { name : String ;

gpa : Float }

Employee := { name : String ;

ssn : Integer }

Student := { name : String ;

gpa : Float }

Employee := { name : String ;

ssn : Integer }

Definition Person : ty :=

(* FILL IN HERE *) admit.

Definition Student : ty :=

(* FILL IN HERE *) admit.

Definition Employee : ty :=

(* FILL IN HERE *) admit.

Example sub_student_person :

Student <: Person.

Proof.

(* FILL IN HERE *) Admitted.

Example sub_employee_person :

Employee <: Person.

Proof.

(* FILL IN HERE *) Admitted.

☐

Example subtyping_example_0 :

(TArrow C Person) <: (TArrow C TTop).

(* C->Person <: C->Top *)

Proof.

apply S_Arrow.

apply S_Refl. auto.

Qed.

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, optional (subtyping_example_1)

Example subtyping_example_1 :

(TArrow TTop Student) <: (TArrow (TArrow C C) Person).

(* Top->Student <: (C->C)->Person *)

Proof with eauto.

(* FILL IN HERE *) Admitted.

(TArrow TTop Student) <: (TArrow (TArrow C C) Person).

(* Top->Student <: (C->C)->Person *)

Proof with eauto.

(* FILL IN HERE *) Admitted.

Example subtyping_example_2 :

(TArrow TTop Person) <: (TArrow Person TTop).

(* Top->Person <: Person->Top *)

Proof with eauto.

(* FILL IN HERE *) Admitted.

(TArrow TTop Person) <: (TArrow Person TTop).

(* Top->Person <: Person->Top *)

Proof with eauto.

(* FILL IN HERE *) Admitted.

☐

End Examples.

Definition context := id → (option ty).

Definition empty : context := (fun _ ⇒ None).

Definition extend (Γ : context) (x:id) (T : ty) :=

fun x' ⇒ if eq_id_dec x x' then Some T else Γ x'.

Reserved Notation "Gamma '⊢' t '∈' T" (at level 40).

Inductive has_type : context → tm → ty → Prop :=

(* Same as before *)

| T_Var : ∀Γ x T,

Γ x = Some T →

Γ ⊢ (tvar x) ∈ T

| T_Abs : ∀Γ x T

_{11}T

_{12}t

_{12},

(extend Γ x T

_{11}) ⊢ t

_{12}∈ T

_{12}→

Γ ⊢ (tabs x T

_{11}t

_{12}) ∈ (TArrow T

_{11}T

_{12})

| T_App : ∀T

_{1}T

_{2}Γ t

_{1}t

_{2},

Γ ⊢ t

_{1}∈ (TArrow T

_{1}T

_{2}) →

Γ ⊢ t

_{2}∈ T

_{1}→

Γ ⊢ (tapp t

_{1}t

_{2}) ∈ T

_{2}

| T_True : ∀Γ,

Γ ⊢ ttrue ∈ TBool

| T_False : ∀Γ,

Γ ⊢ tfalse ∈ TBool

| T_If : ∀t

_{1}t

_{2}t

_{3}T Γ,

Γ ⊢ t

_{1}∈ TBool →

Γ ⊢ t

_{2}∈ T →

Γ ⊢ t

_{3}∈ T →

Γ ⊢ (tif t

_{1}t

_{2}t

_{3}) ∈ T

| T_Unit : ∀Γ,

Γ ⊢ tunit ∈ TUnit

(* New rule of subsumption *)

| T_Sub : ∀Γ t S T,

Γ ⊢ t ∈ S →

S <: T →

Γ ⊢ t ∈ T

where "Gamma '⊢' t '∈' T" := (has_type Γ t T).

Hint Constructors has_type.

Tactic Notation "has_type_cases" tactic(first) ident(c) :=

first;

[ Case_aux c "T_Var" | Case_aux c "T_Abs"

| Case_aux c "T_App" | Case_aux c "T_True"

| Case_aux c "T_False" | Case_aux c "T_If"

| Case_aux c "T_Unit"

| Case_aux c "T_Sub" ].

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, optional (typing_example_0)

(* empty |- ((λz:A.z), (λz:B.z))

: (A->A * B->B) *)

(* FILL IN HERE *)

: (A->A * B->B) *)

(* FILL IN HERE *)

(* empty |- (λx:(Top * B->B). x.snd) ((λz:A.z), (λz:B.z))

: B->B *)

(* FILL IN HERE *)

: B->B *)

(* FILL IN HERE *)

(* empty |- (λz:(C->C)->(Top * B->B). (z (λx:C.x)).snd)

(λz:C->C. ((λz:A.z), (λz:B.z)))

: B->B *)

(* FILL IN HERE *)

(λz:C->C. ((λz:A.z), (λz:B.z)))

: B->B *)

(* FILL IN HERE *)

☐

End Examples2.

# Properties

*statements*of these properties to take subtyping into account. However, their proofs do become a little bit more involved.

## Inversion Lemmas for Subtyping

- Bool is the only subtype of Bool
- every subtype of an arrow type
*is*an arrow type.

*inversion lemmas*because they play the same role in later proofs as the built-in inversion tactic: given a hypothesis that there exists a derivation of some subtyping statement S <: T and some constraints on the shape of S and/or T, each one reasons about what this derivation must look like to tell us something further about the shapes of S and T and the existence of subtype relations between their parts.

#### Exercise: 2 stars, optional (sub_inversion_Bool)

Lemma sub_inversion_Bool : ∀U,

U <: TBool →

U = TBool.

Proof with auto.

intros U Hs.

remember TBool as V.

(* FILL IN HERE *) Admitted.

U <: TBool →

U = TBool.

Proof with auto.

intros U Hs.

remember TBool as V.

(* FILL IN HERE *) Admitted.

Lemma sub_inversion_arrow : ∀U V1 V2,

U <: (TArrow V1 V2) →

∃U1, ∃U2,

U = (TArrow U1 U2) ∧ (V1 <: U1) ∧ (U2 <: V2).

Proof with eauto.

intros U V1 V2 Hs.

remember (TArrow V1 V2) as V.

generalize dependent V2. generalize dependent V1.

(* FILL IN HERE *) Admitted.

U <: (TArrow V1 V2) →

∃U1, ∃U2,

U = (TArrow U1 U2) ∧ (V1 <: U1) ∧ (U2 <: V2).

Proof with eauto.

intros U V1 V2 Hs.

remember (TArrow V1 V2) as V.

generalize dependent V2. generalize dependent V1.

(* FILL IN HERE *) Admitted.

☐

## Canonical Forms

_{1}t

_{2}where both t

_{1}and t

_{2}are values, we need to know that t

_{1}has the

*form*of a lambda-abstraction, so that we can apply the ST_AppAbs reduction rule. In the ordinary STLC, this is obvious: we know that t

_{1}has a function type T

_{11}→T

_{12}, and there is only one rule that can be used to give a function type to a value — rule T_Abs — and the form of the conclusion of this rule forces t

_{1}to be an abstraction.

_{1}: T

_{11}→T

_{12}is subsumption, then there is some

*sub*-derivation whose subject is also t

_{1}, and we can reason by induction until we finally bottom out at a use of T_Abs.

#### Exercise: 3 stars, optional (canonical_forms_of_arrow_types)

Lemma canonical_forms_of_arrow_types : ∀Γ s T

Γ ⊢ s ∈ (TArrow T

value s →

∃x, ∃S

s = tabs x S

Proof with eauto.

(* FILL IN HERE *) Admitted.

_{1}T_{2},Γ ⊢ s ∈ (TArrow T

_{1}T_{2}) →value s →

∃x, ∃S

_{1}, ∃s_{2},s = tabs x S

_{1}s_{2}.Proof with eauto.

(* FILL IN HERE *) Admitted.

☐
Similarly, the canonical forms of type Bool are the constants
true and false.

Lemma canonical_forms_of_Bool : ∀Γ s,

Γ ⊢ s ∈ TBool →

value s →

(s = ttrue ∨ s = tfalse).

Proof with eauto.

intros Γ s Hty Hv.

remember TBool as T.

has_type_cases (induction Hty) Case; try solve by inversion...

Case "T_Sub".

subst. apply sub_inversion_Bool in H. subst...

Qed.

intros Γ s Hty Hv.

remember TBool as T.

has_type_cases (induction Hty) Case; try solve by inversion...

Case "T_Sub".

subst. apply sub_inversion_Bool in H. subst...

Qed.

## Progress

*Theorem*(Progress): 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, with empty ⊢ t : T. Proceed by induction on the typing derivation.

- If the last step in the typing derivation uses rule T_App,
then there are terms t
_{1}t_{2}and types T_{1}and T_{2}such that t = t_{1}t_{2}, T = T_{2}, empty ⊢ t_{1}: T_{1}→ T_{2}, and empty ⊢ t_{2}: T_{1}. Moreover, by the induction hypothesis, either t_{1}is a value or it steps, and either t_{2}is a value or it steps. There are three possibilities to consider:- Suppose t
_{1}⇒ t_{1}' for some term t_{1}'. Then t_{1}t_{2}⇒ t_{1}' t_{2}by ST_App1. - Suppose t
_{1}is a value and t_{2}⇒ t_{2}' for some term t_{2}'. Then t_{1}t_{2}⇒ t_{1}t_{2}' by rule ST_App2 because t_{1}is a value. - Finally, suppose t
_{1}and t_{2}are both values. By Lemma canonical_forms_for_arrow_types, we know that t_{1}has the form λx:S_{1}.s_{2}for some x, S_{1}, and s_{2}. But then (λx:S_{1}.s_{2}) t_{2}⇒ [x:=t_{2}]s_{2}by ST_AppAbs, since t_{2}is a value.

- Suppose t
- If the final step of the derivation uses rule T_If, then there
are terms t
_{1}, t_{2}, and t_{3}such that t = if t_{1}then t_{2}else t_{3}, with empty ⊢ t_{1}: Bool and with empty ⊢ t_{2}: T and empty ⊢ t_{3}: T. Moreover, by the induction hypothesis, either t_{1}is a value or it steps.- If t
_{1}is a value, then by the canonical forms lemma for booleans, either t_{1}= true or t_{1}= false. In either case, t can step, using rule ST_IfTrue or ST_IfFalse. - If t
_{1}can step, then so can t, by rule ST_If.

- If t
- 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.

Theorem progress : ∀t T,

empty ⊢ t ∈ T →

value t ∨ ∃t', t ⇒ t'.

Proof with eauto.

intros t T Ht.

remember empty as Γ.

revert HeqGamma.

has_type_cases (induction Ht) Case;

intros HeqGamma; subst...

Case "T_Var".

inversion H.

Case "T_App".

right.

destruct IHHt1; subst...

SCase "t

destruct IHHt2; subst...

SSCase "t

destruct (canonical_forms_of_arrow_types empty t

as [x [S

subst. ∃([x:=t

SSCase "t

inversion H0 as [t

SCase "t

inversion H as [t

Case "T_If".

right.

destruct IHHt1.

SCase "t

assert (t

by (eapply canonical_forms_of_Bool; eauto).

inversion H0; subst...

inversion H. rename x into t

Qed.

intros t T Ht.

remember empty as Γ.

revert HeqGamma.

has_type_cases (induction Ht) Case;

intros HeqGamma; subst...

Case "T_Var".

inversion H.

Case "T_App".

right.

destruct IHHt1; subst...

SCase "t

_{1}is a value".destruct IHHt2; subst...

SSCase "t

_{2}is a value".destruct (canonical_forms_of_arrow_types empty t

_{1}T_{1}T_{2})as [x [S

_{1}[t_{12}Heqt1]]]...subst. ∃([x:=t

_{2}]t_{12})...SSCase "t

_{2}steps".inversion H0 as [t

_{2}' Hstp]. ∃(tapp t_{1}t_{2}')...SCase "t

_{1}steps".inversion H as [t

_{1}' Hstp]. ∃(tapp t_{1}' t_{2})...Case "T_If".

right.

destruct IHHt1.

SCase "t

_{1}is a value"...assert (t

_{1}= ttrue ∨ t_{1}= tfalse)by (eapply canonical_forms_of_Bool; eauto).

inversion H0; subst...

inversion H. rename x into t

_{1}'. eauto.Qed.

## Inversion Lemmas for Typing

_{1}.t

_{2}: T whose subject is an abstraction, then there must be some subderivation giving a type to the body t

_{2}.

*Lemma*: If Γ ⊢ λx:S

_{1}.t

_{2}: T, then there is a type S

_{2}such that Γ, x:S

_{1}⊢ t

_{2}: S

_{2}and S

_{1}→ S

_{2}<: T.

*not*say, "then T itself is an arrow type" — this is tempting, but false!)

*Proof*: Let Γ, x, S

_{1}, t

_{2}and T be given as described. Proceed by induction on the derivation of Γ ⊢ λx:S

_{1}.t

_{2}: T. Cases T_Var, T_App, are vacuous as those rules cannot be used to give a type to a syntactic abstraction.

- If the last step of the derivation is a use of T_Abs then
there is a type T
_{12}such that T = S_{1}→ T_{12}and Γ, x:S_{1}⊢ t_{2}: T_{12}. Picking T_{12}for S_{2}gives us what we need: S_{1}→ T_{12}<: S_{1}→ T_{12}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 Γ ⊢ λx:S
_{1}.t_{2}: S. The IH for the typing subderivation tell us that there is some type S_{2}with S_{1}→ S_{2}<: S and Γ, x:S_{1}⊢ t_{2}: S_{2}. Picking type S_{2}gives us what we need, since S_{1}→ S_{2}<: T then follows by S_Trans.

Lemma typing_inversion_abs : ∀Γ x S

_{1}t

_{2}T,

Γ ⊢ (tabs x S

_{1}t

_{2}) ∈ T →

(∃S

_{2}, (TArrow S

_{1}S

_{2}) <: T

∧ (extend Γ x S

_{1}) ⊢ t

_{2}∈ S

_{2}).

Proof with eauto.

intros Γ x S

remember (tabs x S

has_type_cases (induction H) Case;

inversion Heqt; subst; intros; try solve by inversion.

Case "T_Abs".

∃T

Case "T_Sub".

destruct IHhas_type as [S

Qed.

intros Γ x S

_{1}t_{2}T H.remember (tabs x S

_{1}t_{2}) as t.has_type_cases (induction H) Case;

inversion Heqt; subst; intros; try solve by inversion.

Case "T_Abs".

∃T

_{12}...Case "T_Sub".

destruct IHhas_type as [S

_{2}[Hsub Hty]]...Qed.

Similarly...

Lemma typing_inversion_var : ∀Γ x T,

Γ ⊢ (tvar x) ∈ T →

∃S,

Γ x = Some S ∧ S <: T.

Proof with eauto.

intros Γ x T Hty.

remember (tvar x) as t.

has_type_cases (induction Hty) Case; intros;

inversion Heqt; subst; try solve by inversion.

Case "T_Var".

∃T...

Case "T_Sub".

destruct IHHty as [U [Hctx HsubU]]... Qed.

intros Γ x T Hty.

remember (tvar x) as t.

has_type_cases (induction Hty) Case; intros;

inversion Heqt; subst; try solve by inversion.

Case "T_Var".

∃T...

Case "T_Sub".

destruct IHHty as [U [Hctx HsubU]]... Qed.

Lemma typing_inversion_app : ∀Γ t

_{1}t

_{2}T

_{2},

Γ ⊢ (tapp t

_{1}t

_{2}) ∈ T

_{2}→

∃T

_{1},

Γ ⊢ t

_{1}∈ (TArrow T

_{1}T

_{2}) ∧

Γ ⊢ t

_{2}∈ T

_{1}.

Proof with eauto.

intros Γ t

remember (tapp t

has_type_cases (induction Hty) Case; intros;

inversion Heqt; subst; try solve by inversion.

Case "T_App".

∃T

Case "T_Sub".

destruct IHHty as [U1 [Hty1 Hty2]]...

Qed.

intros Γ t

_{1}t_{2}T_{2}Hty.remember (tapp t

_{1}t_{2}) as t.has_type_cases (induction Hty) Case; intros;

inversion Heqt; subst; try solve by inversion.

Case "T_App".

∃T

_{1}...Case "T_Sub".

destruct IHHty as [U1 [Hty1 Hty2]]...

Qed.

Lemma typing_inversion_true : ∀Γ T,

Γ ⊢ ttrue ∈ T →

TBool <: T.

Proof with eauto.

intros Γ T Htyp. remember ttrue as tu.

has_type_cases (induction Htyp) Case;

inversion Heqtu; subst; intros...

Qed.

intros Γ T Htyp. remember ttrue as tu.

has_type_cases (induction Htyp) Case;

inversion Heqtu; subst; intros...

Qed.

Lemma typing_inversion_false : ∀Γ T,

Γ ⊢ tfalse ∈ T →

TBool <: T.

Proof with eauto.

intros Γ T Htyp. remember tfalse as tu.

has_type_cases (induction Htyp) Case;

inversion Heqtu; subst; intros...

Qed.

intros Γ T Htyp. remember tfalse as tu.

has_type_cases (induction Htyp) Case;

inversion Heqtu; subst; intros...

Qed.

Lemma typing_inversion_if : ∀Γ t

_{1}t

_{2}t

_{3}T,

Γ ⊢ (tif t

_{1}t

_{2}t

_{3}) ∈ T →

Γ ⊢ t

_{1}∈ TBool

∧ Γ ⊢ t

_{2}∈ T

∧ Γ ⊢ t

_{3}∈ T.

Proof with eauto.

intros Γ t

remember (tif t

has_type_cases (induction Hty) Case; intros;

inversion Heqt; subst; try solve by inversion.

Case "T_If".

auto.

Case "T_Sub".

destruct (IHHty H0) as [H1 [H2 H3]]...

Qed.

intros Γ t

_{1}t_{2}t_{3}T Hty.remember (tif t

_{1}t_{2}t_{3}) as t.has_type_cases (induction Hty) Case; intros;

inversion Heqt; subst; try solve by inversion.

Case "T_If".

auto.

Case "T_Sub".

destruct (IHHty H0) as [H1 [H2 H3]]...

Qed.

Lemma typing_inversion_unit : ∀Γ T,

Γ ⊢ tunit ∈ T →

TUnit <: T.

Proof with eauto.

intros Γ T Htyp. remember tunit as tu.

has_type_cases (induction Htyp) Case;

inversion Heqtu; subst; intros...

Qed.

intros Γ T Htyp. remember tunit as tu.

has_type_cases (induction Htyp) Case;

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 S

_{1}s

_{2}T

_{1}T

_{2},

empty ⊢ (tabs x S

_{1}s

_{2}) ∈ (TArrow T

_{1}T

_{2}) →

T

_{1}<: S

_{1}

∧ (extend empty x S

_{1}) ⊢ s

_{2}∈ T

_{2}.

Proof with eauto.

intros x S

apply typing_inversion_abs in Hty.

inversion Hty as [S

apply sub_inversion_arrow in Hsub.

inversion Hsub as [U1 [U2 [Heq [Hsub1 Hsub2]]]].

inversion Heq; subst... Qed.

intros x S

_{1}s_{2}T_{1}T_{2}Hty.apply typing_inversion_abs in Hty.

inversion Hty as [S

_{2}[Hsub Hty1]].apply sub_inversion_arrow in Hsub.

inversion Hsub as [U1 [U2 [Heq [Hsub1 Hsub2]]]].

inversion Heq; subst... Qed.

Inductive appears_free_in : id → tm → Prop :=

| afi_var : ∀x,

appears_free_in x (tvar x)

| afi_app1 : ∀x t

_{1}t

_{2},

appears_free_in x t

_{1}→ appears_free_in x (tapp t

_{1}t

_{2})

| afi_app2 : ∀x t

_{1}t

_{2},

appears_free_in x t

_{2}→ appears_free_in x (tapp t

_{1}t

_{2})

| afi_abs : ∀x y T

_{11}t

_{12},

y ≠ x →

appears_free_in x t

_{12}→

appears_free_in x (tabs y T

_{11}t

_{12})

| afi_if1 : ∀x t

_{1}t

_{2}t

_{3},

appears_free_in x t

_{1}→

appears_free_in x (tif t

_{1}t

_{2}t

_{3})

| afi_if2 : ∀x t

_{1}t

_{2}t

_{3},

appears_free_in x t

_{2}→

appears_free_in x (tif t

_{1}t

_{2}t

_{3})

| afi_if3 : ∀x t

_{1}t

_{2}t

_{3},

appears_free_in x t

_{3}→

appears_free_in x (tif t

_{1}t

_{2}t

_{3})

.

Hint Constructors appears_free_in.

Lemma context_invariance : ∀Γ Γ' t S,

Γ ⊢ t ∈ S →

(∀x, appears_free_in x t → Γ x = Γ' x) →

Γ' ⊢ t ∈ S.

Proof with eauto.

intros. generalize dependent Γ'.

has_type_cases (induction H) Case;

intros Γ' Heqv...

Case "T_Var".

apply T_Var... rewrite ← Heqv...

Case "T_Abs".

apply T_Abs... apply IHhas_type. intros x0 Hafi.

unfold extend. destruct (eq_id_dec x x0)...

Case "T_App".

apply T_App with T

Case "T_If".

apply T_If...

Qed.

intros. generalize dependent Γ'.

has_type_cases (induction H) Case;

intros Γ' Heqv...

Case "T_Var".

apply T_Var... rewrite ← Heqv...

Case "T_Abs".

apply T_Abs... apply IHhas_type. intros x0 Hafi.

unfold extend. destruct (eq_id_dec x x0)...

Case "T_App".

apply T_App with T

_{1}...Case "T_If".

apply T_If...

Qed.

Lemma free_in_context : ∀x t T Γ,

appears_free_in x t →

Γ ⊢ t ∈ T →

∃T', Γ x = Some T'.

Proof with eauto.

intros x t T Γ Hafi Htyp.

has_type_cases (induction Htyp) Case;

subst; inversion Hafi; subst...

Case "T_Abs".

destruct (IHHtyp H4) as [T Hctx]. ∃T.

unfold extend in Hctx. rewrite neq_id in Hctx... Qed.

intros x t T Γ Hafi Htyp.

has_type_cases (induction Htyp) Case;

subst; inversion Hafi; subst...

Case "T_Abs".

destruct (IHHtyp H4) as [T Hctx]. ∃T.

unfold extend in Hctx. rewrite neq_id in Hctx... Qed.

## Substitution

*substitution lemma*is proved along the same lines as for the pure STLC. The only significant change is that there are several places where, instead of the built-in inversion tactic, we need to use the inversion lemmas that we proved above to extract structural information from assumptions about the well-typedness of subterms.

Lemma substitution_preserves_typing : ∀Γ x U v t S,

(extend Γ x U) ⊢ t ∈ S →

empty ⊢ v ∈ U →

Γ ⊢ ([x:=v]t) ∈ S.

Proof with eauto.

intros Γ x U v t S Htypt Htypv.

generalize dependent S. generalize dependent Γ.

t_cases (induction t) Case; intros; simpl.

Case "tvar".

rename i into y.

destruct (typing_inversion_var _ _ _ Htypt)

as [T [Hctx Hsub]].

unfold extend in Hctx.

destruct (eq_id_dec x y)...

SCase "x=y".

subst.

inversion Hctx; subst. clear Hctx.

apply context_invariance with empty...

intros x Hcontra.

destruct (free_in_context _ _ S empty Hcontra)

as [T' HT']...

inversion HT'.

Case "tapp".

destruct (typing_inversion_app _ _ _ _ Htypt)

as [T

eapply T_App...

Case "tabs".

rename i into y. rename t into T

destruct (typing_inversion_abs _ _ _ _ _ Htypt)

as [T

apply T_Sub with (TArrow T

destruct (eq_id_dec x y).

SCase "x=y".

eapply context_invariance...

subst.

intros x Hafi. unfold extend.

destruct (eq_id_dec y x)...

SCase "x≠y".

apply IHt. eapply context_invariance...

intros z Hafi. unfold extend.

destruct (eq_id_dec y z)...

subst. rewrite neq_id...

Case "ttrue".

assert (TBool <: S)

by apply (typing_inversion_true _ _ Htypt)...

Case "tfalse".

assert (TBool <: S)

by apply (typing_inversion_false _ _ Htypt)...

Case "tif".

assert ((extend Γ x U) ⊢ t

∧ (extend Γ x U) ⊢ t

∧ (extend Γ x U) ⊢ t

by apply (typing_inversion_if _ _ _ _ _ Htypt).

inversion H as [H1 [H2 H3]].

apply IHt1 in H1. apply IHt2 in H2. apply IHt3 in H3.

auto.

Case "tunit".

assert (TUnit <: S)

by apply (typing_inversion_unit _ _ Htypt)...

Qed.

The proof of preservation now proceeds pretty much as in earlier
chapters, using the substitution lemma at the appropriate point
and again using inversion lemmas from above to extract structural
information from typing assumptions.

Theorem preservation : ∀t t' T,

empty ⊢ t ∈ T →

t ⇒ t' →

empty ⊢ t' ∈ T.

This formalization of the STLC with subtyping has omitted record
types, for brevity. If we want to deal with them more seriously,
we have two choices.
First, we can treat them as part of the core language, writing
down proper syntax, typing, and subtyping rules for them. Chapter
RecordSub shows how this extension works.
On the other hand, if we are treating them as a derived form that
is desugared in the parser, then we shouldn't need any new rules:
we should just check that the existing rules for subtyping product
and Unit types give rise to reasonable rules for record
subtyping via this encoding. To do this, we just need to make one
small change to the encoding described earlier: instead of using
Unit as the base case in the encoding of tuples and the "don't
care" placeholder in the encoding of records, we use Top. So:

☐

(* $Date: 2013-07-18 09:59:22 -0400 (Thu, 18 Jul 2013) $ *)

intros Γ x U v t S Htypt Htypv.

generalize dependent S. generalize dependent Γ.

t_cases (induction t) Case; intros; simpl.

Case "tvar".

rename i into y.

destruct (typing_inversion_var _ _ _ Htypt)

as [T [Hctx Hsub]].

unfold extend in Hctx.

destruct (eq_id_dec x y)...

SCase "x=y".

subst.

inversion Hctx; subst. clear Hctx.

apply context_invariance with empty...

intros x Hcontra.

destruct (free_in_context _ _ S empty Hcontra)

as [T' HT']...

inversion HT'.

Case "tapp".

destruct (typing_inversion_app _ _ _ _ Htypt)

as [T

_{1}[Htypt1 Htypt2]].eapply T_App...

Case "tabs".

rename i into y. rename t into T

_{1}.destruct (typing_inversion_abs _ _ _ _ _ Htypt)

as [T

_{2}[Hsub Htypt2]].apply T_Sub with (TArrow T

_{1}T_{2})... apply T_Abs...destruct (eq_id_dec x y).

SCase "x=y".

eapply context_invariance...

subst.

intros x Hafi. unfold extend.

destruct (eq_id_dec y x)...

SCase "x≠y".

apply IHt. eapply context_invariance...

intros z Hafi. unfold extend.

destruct (eq_id_dec y z)...

subst. rewrite neq_id...

Case "ttrue".

assert (TBool <: S)

by apply (typing_inversion_true _ _ Htypt)...

Case "tfalse".

assert (TBool <: S)

by apply (typing_inversion_false _ _ Htypt)...

Case "tif".

assert ((extend Γ x U) ⊢ t

_{1}∈ TBool∧ (extend Γ x U) ⊢ t

_{2}∈ S∧ (extend Γ x U) ⊢ t

_{3}∈ S)by apply (typing_inversion_if _ _ _ _ _ Htypt).

inversion H as [H1 [H2 H3]].

apply IHt1 in H1. apply IHt2 in H2. apply IHt3 in H3.

auto.

Case "tunit".

assert (TUnit <: S)

by apply (typing_inversion_unit _ _ Htypt)...

Qed.

## Preservation

*Theorem*(Preservation): 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. The cases T_Abs, T_Unit, T_True, and T_False cases are vacuous because abstractions and constants 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 t
_{1}t_{2}and types T_{1}T_{2}such that t = t_{1}t_{2}, T = T_{2}, empty ⊢ t_{1}: T_{1}→ T_{2}and empty ⊢ t_{2}: T_{1}._{1}t_{2}can step. Cases ST_App1 and ST_App2 follow immediately by the induction hypotheses for the typing subderivations and a use of T_App._{1}t_{2}steps by ST_AppAbs. Then t_{1}= λx:S.t_{12}for some type S and term t_{12}, and t' = [x:=t_{2}]t_{12}._{1}<: S and x:S_{1}⊢ s_{2}: T_{2}. It then follows by the substitution lemma (substitution_preserves_typing) that empty ⊢ [x:=t_{2}] t_{12}: T_{2}as desired.- If the final step of the derivation uses rule T_If, then
there are terms t
_{1}, t_{2}, and t_{3}such that t = if t_{1}then t_{2}else t_{3}, with empty ⊢ t_{1}: Bool and with empty ⊢ t_{2}: T and empty ⊢ t_{3}: T. Moreover, by the induction hypothesis, if t_{1}steps to t_{1}' then empty ⊢ t_{1}' : 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 t
_{1}' then t_{2}else t_{3}with t_{1}⇒ t_{1}'. By the induction hypothesis, empty ⊢ t_{1}' : Bool, and so empty ⊢ t' : T by T_If. - If t ⇒ t' by rule ST_IfTrue or ST_IfFalse, then
either t' = t
_{2}or t' = t_{3}, and empty ⊢ t' : T follows by assumption.

- If t ⇒ t' by rule ST_If, then t' = if t

- If the final step of the derivation uses rule T_If, then
there are terms t
- 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. ☐

Theorem preservation : ∀t t' T,

empty ⊢ t ∈ T →

t ⇒ t' →

empty ⊢ t' ∈ T.

Proof with eauto.

intros t t' T HT.

remember empty as Γ. generalize dependent HeqGamma.

generalize dependent t'.

has_type_cases (induction HT) Case;

intros t' HeqGamma HE; subst; inversion HE; subst...

Case "T_App".

inversion HE; subst...

SCase "ST_AppAbs".

destruct (abs_arrow _ _ _ _ _ HT1) as [HA1 HA2].

apply substitution_preserves_typing with T...

Qed.

intros t t' T HT.

remember empty as Γ. generalize dependent HeqGamma.

generalize dependent t'.

has_type_cases (induction HT) Case;

intros t' HeqGamma HE; subst; inversion HE; subst...

Case "T_App".

inversion HE; subst...

SCase "ST_AppAbs".

destruct (abs_arrow _ _ _ _ _ HT1) as [HA1 HA2].

apply substitution_preserves_typing with T...

Qed.

## Records, via Products and Top

{a:Nat, b:Nat} ----> {Nat,Nat} i.e. (Nat,(Nat,Top)) {c:Nat, a:Nat} ----> {Nat,Top,Nat} i.e. (Nat,(Top,(Nat,Top)))The encoding of record values doesn't change at all. It is easy (and instructive) to check that the subtyping rules above are validated by the encoding. For the rest of this chapter, we'll follow this encoding-based approach.

## Exercises

#### Exercise: 2 stars (variations)

Each part of this problem suggests a different way of changing the definition of the STLC with Unit and subtyping. (These changes are not cumulative: each part starts from the original language.) In each part, list which properties (Progress, Preservation, both, or neither) become false. If a property becomes false, give a counterexample.- Suppose we add the following typing rule:
Γ ⊢ t : S _{1}->S_{2}S _{1}<: T_{1}T_{1}<: S_{1}S_{2}<: T_{2}(T_Funny1) Γ ⊢ t : T _{1}->T_{2} - Suppose we add the following reduction rule:
(ST_Funny21) unit ⇒ (λx:Top. x) - Suppose we add the following subtyping rule:
(S_Funny3) Unit <: Top->Top - Suppose we add the following subtyping rule:
(S_Funny4) Top->Top <: Unit - Suppose we add the following evaluation rule:
(ST_Funny5) (unit t) ⇒ (t unit) - Suppose we add the same evaluation rule
*and*a new typing rule:(ST_Funny5) (unit t) ⇒ (t unit) (T_Funny6) empty ⊢ Unit : Top->Top - Suppose we
*change*the arrow subtyping rule to:S _{1}<: T_{1}S_{2}<: T_{2}(S_Arrow') S _{1}->S_{2}<: T_{1}->T_{2}

# Exercise: Adding Products

#### Exercise: 4 stars, optional (products)

Adding pairs, projections, and product types to the system we have defined is a relatively straightforward matter. Carry out this extension:- Add constructors for pairs, first and second projections, and
product types to the definitions of ty and tm. (Don't
forget to add corresponding cases to T_cases and t_cases.)
- Extend the well-formedness relation in the obvious way.
- Extend the operational semantics with the same reduction rules
as in the last chapter.
- Extend the subtyping relation with this rule:
S _{1}<: T_{1}S_{2}<: T_{2}(Sub_Prod) S _{1}× S_{2}<: T_{1}× T_{2} - Extend the typing relation with the same rules for pairs and
projections as in the last chapter.
- Extend the proofs of progress, preservation, and all their supporting lemmas to deal with the new constructs. (You'll also need to add some completely new lemmas.) ☐

(* $Date: 2013-07-18 09:59:22 -0400 (Thu, 18 Jul 2013) $ *)