Session POPLmark-DeBruijn

Theory Basis

(*  Author:     Stefan Berghofer, TU Muenchen, 2005
*)

theory Basis
imports Main
begin

section ‹General Utilities›

text ‹
This section introduces some general utilities that will be useful later on in
the formalization of System \fsub{}.

The following rewrite rules are useful for simplifying mutual induction rules.
›

lemma True_simps:
  "(True  PROP P)  PROP P"
  "(PROP P  True)  PROP Trueprop True"
  "(x. True)  PROP Trueprop True"
  apply -
  apply rule
  apply (erule meta_mp)
  apply (rule TrueI)
  apply assumption
  apply rule
  apply (rule TrueI)
  apply assumption
  apply rule
  apply (rule TrueI)+
  done

text ‹
Unfortunately, the standard introduction and elimination rules for bounded
universal and existential quantifier do not work properly for sets of pairs.
›

lemma ballpI: "(x y. (x, y)  A  P x y)  (x, y)  A. P x y"
  by blast

lemma bpspec: "(x, y)  A. P x y  (x, y)  A  P x y"
  by blast

lemma ballpE: "(x, y)  A. P x y  (P x y  Q) 
  ((x, y)  A  Q)  Q"
  by blast

lemma bexpI: "P x y  (x, y)  A  (x, y)  A. P x y"
  by blast

lemma bexpE: "(x, y)  A. P x y 
  (x y. (x, y)  A  P x y  Q)  Q"
  by blast

lemma ball_eq_sym: "(x, y)  S. f x y = g x y  (x, y)  S. g x y = f x y"
  by auto

lemma wf_measure_size: "wf (measure size)" by simp

notation
  Some ("_")

notation
  None ("")

notation
  length ("_")

notation
  Cons ("_ / _" [66, 65] 65)

text ‹
The following variant of the standard nth› function returns
⊥› if the index is out of range.
›

primrec
  nth_el :: "'a list  nat  'a option" ("__" [90, 0] 91)
where
  "[]i = "
| "(x # xs)i = (case i of 0  x | Suc j  xs j)"

lemma [simp]: "i < xs  (xs @ ys)i = xsi"
  apply (induct xs arbitrary: i)
  apply simp
  apply (case_tac i)
  apply simp_all
  done

lemma [simp]: "xs  i  (xs @ ys)i = ysi - xs"
  apply (induct xs arbitrary: i)
  apply simp
  apply (case_tac i)
  apply simp_all
  done

text ‹Association lists›

primrec assoc :: "('a × 'b) list  'a  'b option" ("__?" [90, 0] 91)
where
  "[]a? = "
| "(x # xs)a? = (if fst x = a then snd x else xsa?)"

primrec unique :: "('a × 'b) list  bool"
where
  "unique [] = True"
| "unique (x # xs) = (xsfst x? =   unique xs)"

lemma assoc_set: "psx? = y  (x, y)  set ps"
  by (induct ps) (auto split: if_split_asm)

lemma map_assoc_None [simp]:
  "psx? =   map (λ(x, y). (x, f x y)) psx? = "
  by (induct ps) auto

no_syntax
  "_Map" :: "maplets => 'a  'b"  ("(1[_])")


end

Theory POPLmark

(*  Title:      POPLmark/POPLmark.thy
    Author:     Stefan Berghofer, TU Muenchen, 2005
*)

theory POPLmark
imports Basis
begin


section ‹Formalization of the basic calculus›

text ‹
\label{sec:basic-calculus}
In this section, we describe the formalization of the basic calculus
without records. As a main result, we prove {\it type safety}, presented
as two separate theorems, namely {\it preservation} and {\it progress}.
›


subsection ‹Types and Terms›

text ‹
The types of System \fsub{} are represented by the following datatype:
›

datatype type =
    TVar nat
  | Top
  | Fun type type    (infixr "" 200)
  | TyAll type type  ("(3∀<:_./ _)" [0, 10] 10)

text ‹
The subtyping and typing judgements depend on a {\it context} (or environment) @{term Γ}
containing bindings for term and type variables. A context is a list of bindings,
where the @{term i}th element @{term "Γi"} corresponds to the variable
with index @{term i}.
›

datatype binding = VarB type | TVarB type
type_synonym env = "binding list"

text ‹
In contrast to the usual presentation of type systems often found in textbooks, new
elements are added to the left of a context using the Cons› operator ∷› for lists.
We write @{term is_TVarB} for the predicate that returns @{term True} when applied to
a type variable binding, function @{term type_ofB} extracts the type contained in a binding,
and @{term "mapB f"} applies @{term f} to the type contained in a binding.
›

primrec is_TVarB :: "binding  bool"
where
  "is_TVarB (VarB T) = False"
| "is_TVarB (TVarB T) = True"

primrec type_ofB :: "binding  type"
where
  "type_ofB (VarB T) = T"
| "type_ofB (TVarB T) = T"

primrec mapB :: "(type  type)  binding  binding"
where
  "mapB f (VarB T) = VarB (f T)"
| "mapB f (TVarB T) = TVarB (f T)"

text ‹
The following datatype represents the terms of System \fsub{}:
›

datatype trm =
    Var nat
  | Abs type trm   ("(3λ:_./ _)" [0, 10] 10)
  | TAbs type trm  ("(3λ<:_./ _)" [0, 10] 10)
  | App trm trm    (infixl "" 200)
  | TApp trm type  (infixl "τ" 200)


subsection ‹Lifting and Substitution›

text ‹
One of the central operations of $\lambda$-calculus is {\it substitution}.
In order to avoid that free variables in a term or type get ``captured''
when substituting it for a variable occurring in the scope of a binder,
we have to increment the indices of its free variables during substitution.
This is done by the lifting functions τ n k› and ↑ n k›
for types and terms, respectively, which increment the indices of all free
variables with indices ≥ k› by @{term n}. The lifting functions on
types and terms are defined by
›

primrec liftT :: "nat  nat  type  type" ("τ")
where
  "τ n k (TVar i) = (if i < k then TVar i else TVar (i + n))"
| "τ n k Top = Top"
| "τ n k (T  U) = τ n k T  τ n k U"
| "τ n k (∀<:T. U) = (∀<:τ n k T. τ n (k + 1) U)"

primrec lift :: "nat  nat  trm  trm" ("")
where
  " n k (Var i) = (if i < k then Var i else Var (i + n))"
| " n k (λ:T. t) = (λ:τ n k T.  n (k + 1) t)"
| " n k (λ<:T. t) = (λ<:τ n k T.  n (k + 1) t)"
| " n k (s  t) =  n k s   n k t"
| " n k (t τ T) =  n k t τ τ n k T"

text ‹
It is useful to also define an ``unlifting'' function τ n k› for
decrementing all free variables with indices ≥ k› by @{term n}.
Moreover, we need several substitution functions, denoted by
\mbox{T[k ↦τ S]τ}, \mbox{t[k ↦τ S]›}, and \mbox{t[k ↦ s]›},
which substitute type variables in types, type variables in terms,
and term variables in terms, respectively. They are defined as follows:
›

primrec substTT :: "type  nat  type  type"  ("_[_ τ _]τ" [300, 0, 0] 300)
where
  "(TVar i)[k τ S]τ =
     (if k < i then TVar (i - 1) else if i = k then τ k 0 S else TVar i)"
| "Top[k τ S]τ = Top"
| "(T  U)[k τ S]τ = T[k τ S]τ  U[k τ S]τ"
| "(∀<:T. U)[k τ S]τ = (∀<:T[k τ S]τ. U[k+1 τ S]τ)"

primrec decT :: "nat  nat  type  type"  ("τ")
where
  "τ 0 k T = T"
| "τ (Suc n) k T = τ n k (T[k τ Top]τ)"

primrec subst :: "trm  nat  trm  trm"  ("_[_  _]" [300, 0, 0] 300)
where
  "(Var i)[k  s] = (if k < i then Var (i - 1) else if i = k then  k 0 s else Var i)"
| "(t  u)[k  s] = t[k  s]  u[k  s]"
| "(t τ T)[k  s] = t[k  s] τ τ 1 k T"
| "(λ:T. t)[k  s] = (λ:τ 1 k T. t[k+1  s])"
| "(λ<:T. t)[k  s] = (λ<:τ 1 k T. t[k+1  s])"

primrec substT :: "trm  nat  type  trm"    ("_[_ τ _]" [300, 0, 0] 300)
where
  "(Var i)[k τ S] = (if k < i then Var (i - 1) else Var i)"
| "(t  u)[k τ S] = t[k τ S]  u[k τ S]"
| "(t τ T)[k τ S] = t[k τ S] τ T[k τ S]τ"
| "(λ:T. t)[k τ S] = (λ:T[k τ S]τ. t[k+1 τ S])"
| "(λ<:T. t)[k τ S] = (λ<:T[k τ S]τ. t[k+1 τ S])"

text ‹
Lifting and substitution extends to typing contexts as follows:
›

primrec liftE :: "nat  nat  env  env" ("e")
where
  "e n k [] = []"
| "e n k (B  Γ) = mapB (τ n (k + Γ)) B  e n k Γ"

primrec substE :: "env  nat  type  env"  ("_[_ τ _]e" [300, 0, 0] 300)
where
  "[][k τ T]e = []"
| "(B  Γ)[k τ T]e = mapB (λU. U[k + Γ τ T]τ) B  Γ[k τ T]e"

primrec decE :: "nat  nat  env  env"  ("e")
where
  "e 0 k Γ = Γ"
| "e (Suc n) k Γ = e n k (Γ[k τ Top]e)"

text ‹
Note that in a context of the form @{term "B  Γ"}, all variables in @{term B} with
indices smaller than the length of @{term Γ} refer to entries in @{term Γ} and therefore
must not be affected by substitution and lifting. This is the reason why an
additional offset @{term "Γ"} needs to be added to the index @{term k}
in the second clauses of the above functions. Some standard properties of lifting
and substitution, which can be proved by structural induction on terms and types,
are proved below. Properties of this kind are
quite standard for encodings using de Bruijn indices and can also be found in
papers by Barras and Werner \cite{Barras-Werner-JAR} and Nipkow \cite{Nipkow-JAR01}.
›

lemma liftE_length [simp]: "e n k Γ = Γ"
  by (induct Γ) simp_all

lemma substE_length [simp]: "Γ[k τ U]e = Γ"
  by (induct Γ) simp_all

lemma liftE_nth [simp]:
  "(e n k Γ)i = map_option (mapB (τ n (k + Γ - i - 1))) (Γi)"
  apply (induct Γ arbitrary: i)
  apply simp
  apply simp
  apply (case_tac i)
  apply simp
  apply simp
  done

lemma substE_nth [simp]:
  "(Γ[0 τ T]e)i = map_option (mapB (λU. U[Γ - i - 1 τ T]τ)) (Γi)"
  apply (induct Γ arbitrary: i)
  apply simp
  apply simp
  apply (case_tac i)
  apply simp
  apply simp
  done

lemma liftT_liftT [simp]:
  "i  j  j  i + m  τ n j (τ m i T) = τ (m + n) i T"
  by (induct T arbitrary: i j m n) simp_all

lemma liftT_liftT' [simp]:
  "i + m  j  τ n j (τ m i T) = τ m i (τ n (j - m) T)"
  apply (induct T arbitrary: i j m n)
  apply simp_all
  apply arith
  apply (subgoal_tac "Suc j - m = Suc (j - m)")
  apply simp
  apply arith
  done

lemma lift_size [simp]: "size (τ n k T) = size T"
  by (induct T arbitrary: k) simp_all

lemma liftT0 [simp]: "τ 0 i T = T"
  by (induct T arbitrary: i) simp_all

lemma lift0 [simp]: " 0 i t = t"
  by (induct t arbitrary: i) simp_all

theorem substT_liftT [simp]:
  "k  k'  k' < k + n  (τ n k T)[k' τ U]τ = τ (n - 1) k T"
  by (induct T arbitrary: k k') simp_all

theorem liftT_substT [simp]:
  "k  k'  τ n k (T[k' τ U]τ) = τ n k T[k' + n τ U]τ"
  apply (induct T arbitrary: k k')
  apply simp_all
  done

theorem liftT_substT' [simp]: "k' < k 
  τ n k (T[k' τ U]τ) = τ n (k + 1) T[k' τ τ n (k - k') U]τ"
  apply (induct T arbitrary: k k')
  apply simp_all
  apply arith
  done

lemma liftT_substT_Top [simp]:
  "k  k'  τ n k' (T[k τ Top]τ) = τ n (Suc k') T[k τ Top]τ"
  apply (induct T arbitrary: k k')
  apply simp_all
  apply arith
  done

lemma liftT_substT_strange:
  "τ n k T[n + k τ U]τ = τ n (Suc k) T[k τ τ n 0 U]τ"
  apply (induct T arbitrary: n k)
  apply simp_all
  apply (thin_tac "x. PROP P x" for P :: "_  prop")
  apply (drule_tac x=n in meta_spec)
  apply (drule_tac x="Suc k" in meta_spec)
  apply simp
  done

lemma lift_lift [simp]:
  "k  k'  k'  k + n   n' k' ( n k t) =  (n + n') k t"
  by (induct t arbitrary: k k') simp_all

lemma substT_substT:
  "i  j  T[Suc j τ V]τ[i τ U[j - i τ V]τ]τ = T[i τ U]τ[j τ V]τ"
  apply (induct T arbitrary: i j U V)
  apply (simp_all add: diff_Suc split: nat.split)
  apply (thin_tac "x. PROP P x" for P :: "_  prop")
  apply (drule_tac x="Suc i" in meta_spec)
  apply (drule_tac x="Suc j" in meta_spec)
  apply simp
  done


subsection ‹Well-formedness›

text ‹
\label{sec:wf}
The subtyping and typing judgements to be defined in \secref{sec:subtyping}
and \secref{sec:typing} may only operate on types and contexts that
are well-formed. Intuitively, a type @{term T} is well-formed with respect to a
context @{term Γ}, if all variables occurring in it are defined in @{term Γ}.
More precisely, if @{term T} contains a type variable @{term "TVar i"}, then
the @{term i}th element of @{term Γ} must exist and have the form @{term "TVarB U"}.
›

inductive
  well_formed :: "env  type  bool"  ("_ wf _" [50, 50] 50)
where
  wf_TVar: "Γi = TVarB T  Γ wf TVar i"
| wf_Top: "Γ wf Top"
| wf_arrow: "Γ wf T  Γ wf U  Γ wf T  U"
| wf_all: "Γ wf T  TVarB T  Γ wf U  Γ wf (∀<:T. U)"

text ‹
A context @{term "Γ"} is well-formed, if all types occurring in it only refer to type variables
declared ``further to the right'':
›

inductive
  well_formedE :: "env  bool"  ("_ wf" [50] 50)
  and well_formedB :: "env  binding  bool"  ("_ wfB _" [50, 50] 50)
where
  "Γ wfB B  Γ wf type_ofB B"
| wf_Nil: "[] wf"
| wf_Cons: "Γ wfB B  Γ wf  B  Γ wf"

text ‹
The judgement Γ ⊢wfB B›, which denotes well-formedness of the binding @{term B}
with respect to context @{term Γ}, is just an abbreviation for Γ ⊢wf type_ofB B›.
We now present a number of properties of the well-formedness judgements that will be used
in the proofs in the following sections.
›

inductive_cases well_formed_cases:
  "Γ wf TVar i"
  "Γ wf Top"
  "Γ wf T  U"
  "Γ wf (∀<:T. U)"

inductive_cases well_formedE_cases:
  "B  Γ wf"

lemma wf_TVarB: "Γ wf T  Γ wf  TVarB T  Γ wf"
  by (rule wf_Cons) simp_all

lemma wf_VarB: "Γ wf T  Γ wf  VarB T  Γ wf"
  by (rule wf_Cons) simp_all

lemma map_is_TVarb:
  "map is_TVarB Γ' = map is_TVarB Γ 
    Γi = TVarB T  T. Γ'i = TVarB T"
  apply (induct Γ arbitrary: Γ' T i)
  apply simp
  apply (auto split: nat.split_asm)
  apply (case_tac z)
  apply simp_all
  done

text ‹
A type that is well-formed in a context @{term Γ} is also well-formed in another context
@{term Γ'} that contains type variable bindings at the same positions as @{term Γ}:
›

lemma wf_equallength:
  assumes H: "Γ wf T"
  shows "map is_TVarB Γ' = map is_TVarB Γ  Γ' wf T" using H
  by (induct arbitrary: Γ') (auto intro: well_formed.intros dest: map_is_TVarb)

text ‹
A well-formed context of the form @{term "Δ @ B  Γ"} remains well-formed if we replace
the binding @{term B} by another well-formed binding @{term B'}:
›

lemma wfE_replace:
  "Δ @ B  Γ wf  Γ wfB B'  is_TVarB B' = is_TVarB B 
    Δ @ B'  Γ wf"
  apply (induct Δ)
  apply simp
  apply (erule wf_Cons)
  apply (erule well_formedE_cases)
  apply assumption
  apply simp
  apply (erule well_formedE_cases)
  apply (rule wf_Cons)
  apply (case_tac a)
  apply simp
  apply (rule wf_equallength)
  apply assumption
  apply simp
  apply simp
  apply (rule wf_equallength)
  apply assumption
  apply simp
  apply simp
  done

text ‹
The following weakening lemmas can easily be proved by structural induction on
types and contexts:
›

lemma wf_weaken:
  assumes H: "Δ @ Γ wf T"
  shows "e (Suc 0) 0 Δ @ B  Γ wf τ (Suc 0) Δ T"
  using H
  apply (induct "Δ @ Γ" T arbitrary: Δ)
  apply simp_all
  apply (rule conjI)
  apply (rule impI)
  apply (rule wf_TVar)
  apply simp
  apply (rule impI)
  apply (rule wf_TVar)
  apply (subgoal_tac "Suc i - Δ = Suc (i - Δ)")
  apply simp
  apply arith
  apply (rule wf_Top)
  apply (rule wf_arrow)
  apply simp
  apply simp
  apply (rule wf_all)
  apply simp
  apply simp
  done

lemma wf_weaken': "Γ wf T  Δ @ Γ wf τ Δ 0 T"
  apply (induct Δ)
  apply simp_all
  apply (drule_tac B=a in wf_weaken [of "[]", simplified])
  apply simp
  done

lemma wfE_weaken: "Δ @ Γ wf  Γ wfB B  e (Suc 0) 0 Δ @ B  Γ wf"
  apply (induct Δ)
  apply simp
  apply (rule wf_Cons)
  apply assumption+
  apply simp
  apply (rule wf_Cons)
  apply (erule well_formedE_cases)
  apply (case_tac a)
  apply simp
  apply (rule wf_weaken)
  apply assumption
  apply simp
  apply (rule wf_weaken)
  apply assumption
  apply (erule well_formedE_cases)
  apply simp
  done

text ‹
Intuitively, lemma wf_weaken› states that a type @{term T} which is well-formed
in a context is still well-formed in a larger context, whereas lemma wfE_weaken›
states that a well-formed context remains well-formed when extended with a
well-formed binding. Owing to the encoding of variables using de Bruijn
indices, the statements of the above lemmas involve additional lifting functions.
The typing judgement, which will be described in \secref{sec:typing}, involves
the lookup of variables in a context. It has already been pointed out earlier that each
entry in a context may only depend on types declared ``further to the right''. To ensure that
a type @{term T} stored at position @{term i} in an environment @{term Γ} is valid in the full
environment, as opposed to the smaller environment consisting only of the entries in
@{term Γ} at positions greater than @{term i}, we need to increment the indices of all
free type variables in @{term T} by @{term "Suc i"}:
›

lemma wf_liftB:
  assumes H: "Γ wf"
  shows "Γi = VarB T  Γ wf τ (Suc i) 0 T"
  using H
  apply (induct arbitrary: i)
  apply simp
  apply (simp split: nat.split_asm)
  apply (frule_tac B="VarB T" in wf_weaken [of "[]", simplified])
  apply simp+
  apply (rename_tac nat)
  apply (drule_tac x=nat in meta_spec)
  apply simp
  apply (frule_tac T="τ (Suc nat) 0 T" in wf_weaken [of "[]", simplified])
  apply simp
  done

text ‹
We also need lemmas stating that substitution of well-formed types preserves the well-formedness
of types and contexts:
›

theorem wf_subst:
  "Δ @ B  Γ wf T  Γ wf U  Δ[0 τ U]e @ Γ wf T[Δ τ U]τ"
  apply (induct T arbitrary: Δ)
  apply simp_all
  apply (rule conjI)
  apply (rule impI)
  apply (drule_tac Γ=Γ and Δ="Δ[0 τ U]e" in wf_weaken')
  apply simp
  apply (rule impI conjI)+
  apply (erule well_formed_cases)
  apply (rule wf_TVar)
  apply (simp split: nat.split_asm)
  apply (rename_tac nat Δ T nata)
  apply (subgoal_tac "Δ  nat - Suc 0")
  apply (subgoal_tac "nat - Suc Δ = nata")
  apply (simp (no_asm_simp))
  apply arith
  apply arith
  apply (rule impI)
  apply (erule well_formed_cases)
  apply (rule wf_TVar)
  apply simp
  apply (rule wf_Top)
  apply (erule well_formed_cases)
  apply (rule wf_arrow)
  apply simp+
  apply (erule well_formed_cases)
  apply (rule wf_all)
  apply simp
  apply (thin_tac "x. PROP P x" for P :: "_  prop")
  apply (drule_tac x="TVarB T1  Δ" in meta_spec)
  apply simp
  done

theorem wfE_subst: "Δ @ B  Γ wf  Γ wf U  Δ[0 τ U]e @ Γ wf"
  apply (induct Δ)
  apply simp
  apply (erule well_formedE_cases)
  apply assumption
  apply simp
  apply (case_tac a)
  apply (erule well_formedE_cases)
  apply (rule wf_Cons)
  apply simp
  apply (rule wf_subst)
  apply assumption+
  apply simp
  apply (erule well_formedE_cases)
  apply (rule wf_Cons)
  apply simp
  apply (rule wf_subst)
  apply assumption+
  done


subsection ‹Subtyping›

text ‹
\label{sec:subtyping}
We now come to the definition of the subtyping judgement Γ ⊢ T <: U›.
›

inductive
  subtyping :: "env  type  type  bool"  ("_  _ <: _" [50, 50, 50] 50)
where
  SA_Top: "Γ wf  Γ wf S  Γ  S <: Top"
| SA_refl_TVar: "Γ wf  Γ wf TVar i  Γ  TVar i <: TVar i"
| SA_trans_TVar: "Γi = TVarB U 
    Γ  τ (Suc i) 0 U <: T  Γ  TVar i <: T"
| SA_arrow: "Γ  T1 <: S1  Γ  S2 <: T2  Γ  S1  S2 <: T1  T2"
| SA_all: "Γ  T1 <: S1  TVarB T1  Γ  S2 <: T2 
    Γ  (∀<:S1. S2) <: (∀<:T1. T2)"

text ‹
The rules SA_Top› and SA_refl_TVar›, which appear at the leaves of
the derivation tree for a judgement @{term "Γ  T <: U"}, contain additional
side conditions ensuring the well-formedness of the contexts and types involved.
In order for the rule SA_trans_TVar› to be applicable, the context @{term Γ}
must be of the form \mbox{@{term "Γ1 @ B  Γ2"}}, where @{term "Γ1"} has the length @{term i}.
Since the indices of variables in @{term B} can only refer to variables defined in
@{term "Γ2"}, they have to be incremented by @{term "Suc i"} to ensure that they point
to the right variables in the larger context Γ›.
›

lemma wf_subtype_env:
  assumes PQ: "Γ  P <: Q"
  shows "Γ wf" using PQ
  by induct assumption+

lemma wf_subtype:
  assumes PQ: "Γ  P <: Q"
  shows "Γ wf P  Γ wf Q" using PQ
  by induct (auto intro: well_formed.intros elim!: wf_equallength)

lemma wf_subtypeE:
  assumes H: "Γ  T <: U"
  and H': "Γ wf  Γ wf T  Γ wf U  P"
  shows "P"
  apply (rule H')
  apply (rule wf_subtype_env)
  apply (rule H)
  apply (rule wf_subtype [OF H, THEN conjunct1])
  apply (rule wf_subtype [OF H, THEN conjunct2])
  done

text ‹
By induction on the derivation of @{term "Γ  T <: U"}, it can easily be shown
that all types and contexts occurring in a subtyping judgement must be well-formed:
›

lemma wf_subtype_conj:
  "Γ  T <: U  Γ wf  Γ wf T  Γ wf U"
  by (erule wf_subtypeE) iprover

text ‹
By induction on types, we can prove that the subtyping relation is reflexive:
›

lemma subtype_refl: ― ‹A.1›
  "Γ wf  Γ wf T  Γ  T <: T"
  by (induct T arbitrary: Γ) (blast intro:
    subtyping.intros wf_Nil wf_TVarB elim: well_formed_cases)+

text ‹
The weakening lemma for the subtyping relation is proved in two steps:
by induction on the derivation of the subtyping relation, we first prove
that inserting a single type into the context preserves subtyping:
›

lemma subtype_weaken:
  assumes H: "Δ @ Γ  P <: Q"
  and wf: "Γ wfB B"
  shows "e 1 0 Δ @ B  Γ  τ 1 Δ P <: τ 1 Δ Q" using H
proof (induct "Δ @ Γ" P Q arbitrary: Δ)
  case SA_Top
  with wf show ?case
    by (auto intro: subtyping.SA_Top wfE_weaken wf_weaken)
next
  case SA_refl_TVar
  with wf show ?case
    by (auto intro!: subtyping.SA_refl_TVar wfE_weaken dest: wf_weaken)
next
  case (SA_trans_TVar i U T)
  thus ?case
  proof (cases "i < Δ")
    case True
    with SA_trans_TVar
    have "(e 1 0 Δ @ B  Γ)i = TVarB (τ 1 (Δ - Suc i) U)"
      by simp
    moreover from True SA_trans_TVar
    have "e 1 0 Δ @ B  Γ 
      τ (Suc i) 0 (τ 1 (Δ - Suc i) U) <: τ 1 Δ T"
      by simp
    ultimately have "e 1 0 Δ @ B  Γ  TVar i <: τ 1 Δ T"
      by (rule subtyping.SA_trans_TVar)
    with True show ?thesis by simp
  next
    case False
    then have "Suc i - Δ = Suc (i - Δ)" by arith
    with False SA_trans_TVar have "(e 1 0 Δ @ B  Γ)Suc i = TVarB U"
      by simp
    moreover from False SA_trans_TVar
    have "e 1 0 Δ @ B  Γ  τ (Suc (Suc i)) 0 U <: τ 1 Δ T"
      by simp
    ultimately have "e 1 0 Δ @ B  Γ  TVar (Suc i) <: τ 1 Δ T"
      by (rule subtyping.SA_trans_TVar)
    with False show ?thesis by simp
  qed
next
  case SA_arrow
  thus ?case by simp (iprover intro: subtyping.SA_arrow)
next
  case (SA_all T1 S1 S2 T2 Δ)
  with SA_all(4) [of "TVarB T1  Δ"]
  show ?case by simp (iprover intro: subtyping.SA_all)
qed

text ‹
All cases are trivial, except for the SA_trans_TVar› case, which
requires a case distinction on whether the index of the variable is smaller
than @{term "Δ"}.
The stronger result that appending a new context @{term Δ} to a context
@{term Γ} preserves subtyping can be proved by induction on @{term Δ},
using the previous result in the induction step:
›

lemma subtype_weaken': ― ‹A.2›
  "Γ  P <: Q  Δ @ Γ wf  Δ @ Γ  τ Δ 0 P <: τ Δ 0 Q"
  apply (induct Δ)
  apply simp_all
  apply (erule well_formedE_cases)
  apply simp
  apply (drule_tac B="a" and Γ="Δ @ Γ" in subtype_weaken [of "[]", simplified])
  apply simp_all
  done

text ‹
An unrestricted transitivity rule has the disadvantage that it can
be applied in any situation. In order to make the above definition of the
subtyping relation {\it syntax-directed}, the transitivity rule SA_trans_TVar›
is restricted to the case where the type on the left-hand side of the <:›
operator is a variable. However, the unrestricted transitivity rule
can be derived from this definition.
In order for the proof to go through, we have to simultaneously prove
another property called {\it narrowing}.
The two properties are proved by nested induction. The outer induction
is on the size of the type @{term Q}, whereas the two inner inductions for
proving transitivity and narrowing are on the derivation of the
subtyping judgements. The transitivity property is needed in the proof of
narrowing, which is by induction on the derivation of \mbox{@{term "Δ @ TVarB Q  Γ  M <: N"}}.
In the case corresponding to the rule SA_trans_TVar›, we must prove
\mbox{@{term "Δ @ TVarB P  Γ  TVar i <: T"}}. The only interesting case
is the one where @{term "i = Δ"}. By induction hypothesis, we know that
@{term "Δ @ TVarB P  Γ  τ (i+1) 0 Q <: T"} and
@{term "(Δ @ TVarB Q  Γ)i = TVarB Q"}.
By assumption, we have @{term "Γ  P <: Q"} and hence 
\mbox{@{term "Δ @ TVarB P  Γ  τ (i+1) 0 P <: τ (i+1) 0 Q"}} by weakening.
Since @{term "τ (i+1) 0 Q"} has the same size as @{term Q}, we can use
the transitivity property, which yields
@{term "Δ @ TVarB P  Γ  τ (i+1) 0 P <: T"}. The claim then follows
easily by an application of SA_trans_TVar›.
›

lemma subtype_trans: ― ‹A.3›
  "Γ  S <: Q  Γ  Q <: T  Γ  S <: T"
  "Δ @ TVarB Q  Γ  M <: N  Γ  P <: Q 
     Δ @ TVarB P  Γ  M <: N"
  using wf_measure_size
proof (induct Q arbitrary: Γ S T Δ P M N rule: wf_induct_rule)
  case (less Q)
  {
    fix Γ S T Q'
    assume "Γ  S <: Q'"
    then have "Γ  Q' <: T  size Q = size Q'  Γ  S <: T"
    proof (induct arbitrary: T)
      case SA_Top
      from SA_Top(3) show ?case
        by cases (auto intro: subtyping.SA_Top SA_Top)
    next
      case SA_refl_TVar show ?case by fact
    next
      case SA_trans_TVar
      thus ?case by (auto intro: subtyping.SA_trans_TVar)
    next
      case (SA_arrow Γ T1 S1 S2 T2)
      note SA_arrow' = SA_arrow
      from SA_arrow(5) show ?case
      proof cases
        case SA_Top
        with SA_arrow show ?thesis
          by (auto intro: subtyping.SA_Top wf_arrow elim: wf_subtypeE)
      next
        case (SA_arrow T1' T2')
        from SA_arrow SA_arrow' have "Γ  S1  S2 <: T1'  T2'"
          by (auto intro!: subtyping.SA_arrow intro: less(1) [of "T1"] less(1) [of "T2"])
        with SA_arrow show ?thesis by simp
      qed
    next
      case (SA_all Γ T1 S1 S2 T2)
      note SA_all' = SA_all
      from SA_all(5) show ?case
      proof cases
        case SA_Top
        with SA_all show ?thesis by (auto intro!:
          subtyping.SA_Top wf_all intro: wf_equallength elim: wf_subtypeE)
      next
        case (SA_all T1' T2')
        from SA_all SA_all' have "Γ  T1' <: S1"
          by - (rule less(1), simp_all)
        moreover from SA_all SA_all' have "TVarB T1'  Γ  S2 <: T2"
          by - (rule less(2) [of _ "[]", simplified], simp_all)
        with SA_all SA_all' have "TVarB T1'  Γ  S2 <: T2'"
          by - (rule less(1), simp_all)
        ultimately have "Γ  (∀<:S1. S2) <: (∀<:T1'. T2')"
          by (rule subtyping.SA_all)
        with SA_all show ?thesis by simp
      qed
    qed
  }
  note tr = this
  {
    case 1
    thus ?case using refl by (rule tr)
  next
    case 2
    from 2(1) show "Δ @ TVarB P  Γ  M <: N"
    proof (induct "Δ @ TVarB Q  Γ" M N arbitrary: Δ)
      case SA_Top
      with 2 show ?case by (auto intro!: subtyping.SA_Top
        intro: wf_equallength wfE_replace elim!: wf_subtypeE)
    next
      case SA_refl_TVar
      with 2 show ?case by (auto intro!: subtyping.SA_refl_TVar
        intro: wf_equallength wfE_replace elim!: wf_subtypeE)
    next
      case (SA_trans_TVar i U T)
      show ?case
      proof (cases "i < Δ")
        case True
        with SA_trans_TVar show ?thesis
          by (auto intro!: subtyping.SA_trans_TVar)
      next
        case False
        note False' = False
        show ?thesis
        proof (cases "i = Δ")
          case True
          from SA_trans_TVar have "(Δ @ [TVarB P]) @ Γ wf"
            by (auto elim!: wf_subtypeE)
          with Γ  P <: Q
          have "(Δ @ [TVarB P]) @ Γ  τ Δ @ [TVarB P] 0 P <: τ Δ @ [TVarB P] 0 Q"
            by (rule subtype_weaken')
          with SA_trans_TVar True False have "Δ @ TVarB P  Γ  τ (Suc Δ) 0 P <: T"
            by - (rule tr, simp+)
          with True and False and SA_trans_TVar show ?thesis
            by (auto intro!: subtyping.SA_trans_TVar)
        next
          case False
          with False' have "i - Δ = Suc (i - Δ - 1)" by arith
          with False False' SA_trans_TVar show ?thesis
            by - (rule subtyping.SA_trans_TVar, simp+)
        qed
      qed
    next
      case SA_arrow
      thus ?case by (auto intro!: subtyping.SA_arrow)
    next
      case (SA_all T1 S1 S2 T2)
      thus ?case by (auto intro: subtyping.SA_all
        SA_all(4) [of "TVarB T1  Δ", simplified])
    qed
  }
qed

text ‹
In the proof of the preservation theorem presented in \secref{sec:evaluation},
we will also need a substitution theorem, which is proved by
induction on the subtyping derivation:
›

lemma substT_subtype: ― ‹A.10›
  assumes H: "Δ @ TVarB Q  Γ  S <: T"
  shows "Γ  P <: Q  Δ[0 τ P]e @ Γ  S[Δ τ P]τ <: T[Δ τ P]τ"
  using H
  apply (induct "Δ @ TVarB Q  Γ" S T arbitrary: Δ)
  apply simp_all
  apply (rule SA_Top)
  apply (rule wfE_subst)
  apply assumption
  apply (erule wf_subtypeE)
  apply assumption
  apply (rule wf_subst)
  apply assumption
  apply (erule wf_subtypeE)
  apply assumption
  apply (rule impI conjI)+
  apply (rule subtype_refl)
  apply (rule wfE_subst)
  apply assumption
  apply (erule wf_subtypeE)
  apply assumption
  apply (erule wf_subtypeE)
  apply (drule_tac T=P and Δ="Δ[0 τ P]e" in wf_weaken')
  apply simp
  apply (rule conjI impI)+
  apply (rule SA_refl_TVar)
  apply (rule wfE_subst)
  apply assumption
  apply (erule wf_subtypeE)
  apply assumption
  apply (erule wf_subtypeE)
  apply (drule wf_subst)
  apply assumption
  apply simp
  apply (rule impI)
  apply (rule SA_refl_TVar)
  apply (rule wfE_subst)
  apply assumption
  apply (erule wf_subtypeE)
  apply assumption
  apply (erule wf_subtypeE)
  apply (drule wf_subst)
  apply assumption
  apply simp
  apply (rule conjI impI)+
  apply simp
  apply (drule_tac Γ=Γ and Δ="Δ[0 τ P]e" in subtype_weaken')
  apply (erule wf_subtypeE)+
  apply assumption
  apply simp
  apply (rule subtype_trans(1))
  apply assumption+
  apply (rule conjI impI)+
  apply (rule SA_trans_TVar)
  apply (simp split: nat.split_asm)
  apply (subgoal_tac "Δ  i - Suc 0")
  apply (rename_tac nat)
  apply (subgoal_tac "i - Suc Δ = nat")
  apply (simp (no_asm_simp))
  apply arith
  apply arith
  apply simp
  apply (rule impI)
  apply (rule SA_trans_TVar)
  apply (simp split: nat.split_asm)
  apply (subgoal_tac "Suc (Δ - Suc 0) = Δ")
  apply (simp (no_asm_simp))
  apply arith
  apply (rule SA_arrow)
  apply simp+
  apply (rule SA_all)
  apply simp
  apply simp
  done

lemma subst_subtype:
  assumes H: "Δ @ VarB V  Γ  T <: U"
  shows "e 1 0 Δ @ Γ  τ 1 Δ T <: τ 1 Δ U"
  using H
  apply (induct "Δ @ VarB V  Γ" T U arbitrary: Δ)
  apply simp_all
  apply (rule SA_Top)
  apply (rule wfE_subst)
  apply assumption
  apply (rule wf_Top)
  apply (rule wf_subst)
  apply assumption
  apply (rule wf_Top)
  apply (rule impI conjI)+
  apply (rule SA_Top)
  apply (rule wfE_subst)
  apply assumption
  apply (rule wf_Top)+
  apply (rule conjI impI)+
  apply (rule SA_refl_TVar)
  apply (rule wfE_subst)
  apply assumption
  apply (rule wf_Top)
  apply (drule wf_subst)
  apply (rule wf_Top)
  apply simp
  apply (rule impI)
  apply (rule SA_refl_TVar)
  apply (rule wfE_subst)
  apply assumption
  apply (rule wf_Top)
  apply (drule wf_subst)
  apply (rule wf_Top)
  apply simp
  apply (rule conjI impI)+
  apply simp
  apply (rule conjI impI)+
  apply (simp split: nat.split_asm)
  apply (rule SA_trans_TVar)
  apply (subgoal_tac "Δ  i - Suc 0")
  apply (rename_tac nat)
  apply (subgoal_tac "i - Suc Δ = nat")
  apply (simp (no_asm_simp))
  apply arith
  apply arith
  apply simp
  apply (rule impI)
  apply (rule SA_trans_TVar)
  apply simp
  apply (subgoal_tac "0 < Δ")
  apply simp
  apply arith
  apply (rule SA_arrow)
  apply simp+
  apply (rule SA_all)
  apply simp
  apply simp
  done


subsection ‹Typing›

text ‹
\label{sec:typing}
We are now ready to give a definition of the typing judgement Γ ⊢ t : T›.
›

inductive
  typing :: "env  trm  type  bool"    ("_  _ : _" [50, 50, 50] 50)
where
  T_Var: "Γ wf  Γi = VarB U  T = τ (Suc i) 0 U  Γ  Var i : T"
| T_Abs: "VarB T1  Γ  t2 : T2  Γ  (λ:T1. t2) : T1  τ 1 0 T2"
| T_App: "Γ  t1 : T11  T12  Γ  t2 : T11  Γ  t1  t2 : T12"
| T_TAbs: "TVarB T1  Γ  t2 : T2  Γ  (λ<:T1. t2) : (∀<:T1. T2)"
| T_TApp: "Γ  t1 : (∀<:T11. T12)  Γ  T2 <: T11 
    Γ  t1 τ T2 : T12[0 τ T2]τ"
| T_Sub: "Γ  t : S  Γ  S <: T  Γ  t : T"

text ‹
Note that in the rule T_Var›, the indices of the type @{term U} looked up in
the context @{term Γ} need to be incremented in order for the type to be well-formed
with respect to @{term Γ}. In the rule T_Abs›, the type @{term "T2"} of the
abstraction body @{term "t2"} may not contain the variable with index 0›,
since it is a term variable. To compensate for the disappearance of the context
element @{term "VarB T1"} in the conclusion of thy typing rule, the indices of all
free type variables in @{term "T2"} have to be decremented by 1›.
›

theorem wf_typeE1:
  assumes H: "Γ  t : T"
  shows "Γ wf" using H
  by induct (blast elim: well_formedE_cases)+

theorem wf_typeE2:
  assumes H: "Γ  t : T"
  shows "Γ wf T" using H
  apply induct
  apply simp
  apply (rule wf_liftB)
  apply assumption+
  apply (drule wf_typeE1)+
  apply (erule well_formedE_cases)+
  apply (rule wf_arrow)
  apply simp
  apply simp
  apply (rule wf_subst [of "[]", simplified])
  apply assumption
  apply (rule wf_Top)
  apply (erule well_formed_cases)
  apply assumption
  apply (rule wf_all)
  apply (drule wf_typeE1)
  apply (erule well_formedE_cases)
  apply simp  
  apply assumption
  apply (erule well_formed_cases)
  apply (rule wf_subst [of "[]", simplified])
  apply assumption
  apply (erule wf_subtypeE)
  apply assumption
  apply (erule wf_subtypeE)
  apply assumption
  done

text ‹
Like for the subtyping judgement, we can again prove that all types and contexts
involved in a typing judgement are well-formed:
›
lemma wf_type_conj: "Γ  t : T  Γ wf  Γ wf T"
  by (frule wf_typeE1, drule wf_typeE2) iprover

text ‹
The narrowing theorem for the typing judgement states that replacing the type
of a variable in the context by a subtype preserves typability:
›

lemma narrow_type: ― ‹A.7›
  assumes H: "Δ @ TVarB Q  Γ  t : T"
  shows "Γ  P <: Q  Δ @ TVarB P  Γ  t : T"
  using H
  apply (induct "Δ @ TVarB Q  Γ" t T arbitrary: Δ)
  apply simp_all
  apply (rule T_Var)
  apply (erule wfE_replace)
  apply (erule wf_subtypeE)
  apply simp+
  apply (case_tac "i < Δ")
  apply simp
  apply (case_tac "i = Δ")
  apply simp
  apply (simp split: nat.split nat.split_asm)+
  apply (rule T_Abs [simplified])
  apply simp
  apply (rule_tac T11=T11 in T_App)
  apply simp+
  apply (rule T_TAbs)
  apply simp
  apply (rule_tac T11=T11 in T_TApp)
  apply simp
  apply (rule subtype_trans(2))
  apply assumption+
  apply (rule_tac S=S in T_Sub)
  apply simp
  apply (rule subtype_trans(2))
  apply assumption+
  done

lemma subtype_refl':
  assumes t: "Γ  t : T"
  shows "Γ  T <: T"
proof (rule subtype_refl)
  from t show "Γ wf" by (rule wf_typeE1)
  from t show "Γ wf T" by (rule wf_typeE2)
qed

lemma Abs_type: ― ‹A.13(1)›
  assumes H: "Γ  (λ:S. s) : T"
  shows "Γ  T <: U  U' 
    (S'. Γ  U <: S  VarB S  Γ  s : S' 
      Γ  τ 1 0 S' <: U'  P)  P"
  using H
proof (induct Γ "λ:S. s" T arbitrary: U U' S s P)
  case (T_Abs T1 Γ t2 T2)
  from Γ  T1  τ 1 0 T2 <: U  U'
  obtain ty1: "Γ  U <: T1" and ty2: "Γ  τ 1 0 T2 <: U'"
    by cases simp_all
  from ty1 ‹VarB T1  Γ  t2 : T2 ty2
  show ?case by (rule T_Abs)
next
  case (T_Sub Γ S' T)
  from Γ  S' <: T and Γ  T <: U  U'
  have "Γ  S' <: U  U'" by (rule subtype_trans(1))
  then show ?case
    by (rule T_Sub) (rule T_Sub(5))
qed

lemma Abs_type':
  assumes H: "Γ  (λ:S. s) : U  U'"
  and R: "S'. Γ  U <: S  VarB S  Γ  s : S' 
    Γ  τ 1 0 S' <: U'  P"
  shows "P" using H subtype_refl' [OF H]
  by (rule Abs_type) (rule R)

lemma TAbs_type: ― ‹A.13(2)›
  assumes H: "Γ  (λ<:S. s) : T"
  shows "Γ  T <: (∀<:U. U') 
    (S'. Γ  U <: S  TVarB U  Γ  s : S' 
      TVarB U  Γ  S' <: U'  P)  P"
  using H
proof (induct Γ "λ<:S. s" T arbitrary: U U' S s P)
  case (T_TAbs T1 Γ t2 T2)
  from Γ  (∀<:T1. T2) <: (∀<:U. U')
  obtain ty1: "Γ  U <: T1" and ty2: "TVarB U  Γ  T2 <: U'"
    by cases simp_all
  from ‹TVarB T1  Γ  t2 : T2
  have "TVarB U  Γ  t2 : T2" using ty1
    by (rule narrow_type [of "[]", simplified])
  with ty1 show ?case using ty2 by (rule T_TAbs)
next
  case (T_Sub Γ S' T)
  from Γ  S' <: T and Γ  T <: (∀<:U. U')
  have "Γ  S' <: (∀<:U. U')" by (rule subtype_trans(1))
  then show ?case
    by (rule T_Sub) (rule T_Sub(5))
qed

lemma TAbs_type':
  assumes H: "Γ  (λ<:S. s) : (∀<:U. U')"
  and R: "S'. Γ  U <: S  TVarB U  Γ  s : S' 
    TVarB U  Γ  S' <: U'  P"
  shows "P" using H subtype_refl' [OF H]
  by (rule TAbs_type) (rule R)

lemma T_eq: "Γ  t : T  T = T'  Γ  t : T'" by simp

text ‹
The weakening theorem states that inserting a binding @{term B}
does not affect typing:
›

lemma type_weaken:
  assumes H: "Δ @ Γ  t : T"
  shows "Γ wfB B 
    e 1 0 Δ @ B  Γ   1 Δ t : τ 1 Δ T" using H
  apply (induct "Δ @ Γ" t T arbitrary: Δ)
  apply simp_all
  apply (rule conjI)
  apply (rule impI)
  apply (rule T_Var)
  apply (erule wfE_weaken)
  apply simp+
  apply (rule impI)
  apply (rule T_Var)
  apply (erule wfE_weaken)
  apply assumption
  apply (subgoal_tac "Suc i - Δ = Suc (i - Δ)")
  apply simp
  apply arith
  apply (rule refl)
  apply (rule T_Abs [THEN T_eq])
  apply simp
  apply simp
  apply (rule_tac T11="τ (Suc 0) Δ T11" in T_App)
  apply simp
  apply simp
  apply (rule T_TAbs)
  apply simp
  apply (erule_tac T_TApp [THEN T_eq])
  apply (drule subtype_weaken)
  apply simp+
  apply (case_tac Δ)
  apply (simp add: liftT_substT_strange [of _ 0, simplified])+
  apply (rule_tac S="τ (Suc 0) Δ S" in T_Sub)
  apply simp
  apply (drule subtype_weaken)
  apply simp+
  done

text ‹
We can strengthen this result, so as to mean that concatenating a new context
@{term Δ} to the context @{term Γ} preserves typing:
›

lemma type_weaken': ― ‹A.5(6)›
  "Γ  t : T  Δ @ Γ wf  Δ @ Γ   Δ 0 t : τ Δ 0 T"
  apply (induct Δ)
  apply simp
  apply simp
  apply (erule well_formedE_cases)
  apply simp
  apply (drule_tac B=a in type_weaken [of "[]", simplified])
  apply simp+
  done

text ‹
This property is proved by structural induction on the context @{term Δ},
using the previous result in the induction step. In the proof of the preservation
theorem, we will need two substitution theorems for term and type variables,
both of which are proved by induction on the typing derivation.
Since term and type variables are stored in the same context, we again have to
decrement the free type variables in @{term Δ} and @{term T} by 1›
in the substitution rule for term variables in order to compensate for the
disappearance of the variable.
›

theorem subst_type: ― ‹A.8›
  assumes H: "Δ @ VarB U  Γ  t : T"
  shows "Γ  u : U 
    e 1 0 Δ @ Γ  t[Δ  u] : τ 1 Δ T" using H
  apply (induct "Δ @ VarB U  Γ" t T arbitrary: Δ)
  apply simp
  apply (rule conjI)
  apply (rule impI)
  apply simp
  apply (drule_tac Δ="Δ[0 τ Top]e" in type_weaken')
  apply (rule wfE_subst)
  apply assumption
  apply (rule wf_Top)
  apply simp
  apply (rule impI conjI)+
  apply (simp split: nat.split_asm)
  apply (rule T_Var)
  apply (erule wfE_subst)
  apply (rule wf_Top)
  apply (subgoal_tac "Δ  i - Suc 0")
  apply (rename_tac nat)
  apply (subgoal_tac "i - Suc Δ = nat")
  apply (simp (no_asm_simp))
  apply arith
  apply arith
  apply simp
  apply (rule impI)
  apply (rule T_Var)
  apply (erule wfE_subst)
  apply (rule wf_Top)
  apply simp
  apply (subgoal_tac "Suc (Δ - Suc 0) = Δ")
  apply (simp (no_asm_simp))
  apply arith
  apply simp
  apply (rule T_Abs [THEN T_eq])
  apply simp
  apply (simp add: substT_substT [symmetric])
  apply simp
  apply (rule_tac T11="T11[Δ τ Top]τ" in T_App)
  apply simp+
  apply (rule T_TAbs)
  apply simp
  apply simp
  apply (rule T_TApp [THEN T_eq])
  apply simp
  apply (rule subst_subtype [simplified])
  apply assumption
  apply (simp add: substT_substT [symmetric])
  apply (rule_tac S="S[Δ τ Top]τ" in T_Sub)
  apply simp
  apply simp
  apply (rule subst_subtype [simplified])
  apply assumption
  done

theorem substT_type: ― ‹A.11›
  assumes H: "Δ @ TVarB Q  Γ  t : T"
  shows "Γ  P <: Q 
    Δ[0 τ P]e @ Γ  t[Δ τ P] : T[Δ τ P]τ" using H
  apply (induct "Δ @ TVarB Q  Γ" t T arbitrary: Δ)
  apply simp_all
  apply (rule impI conjI)+
  apply simp
  apply (rule T_Var)
  apply (erule wfE_subst)
  apply (erule wf_subtypeE)
  apply assumption
  apply (simp split: nat.split_asm)
  apply (subgoal_tac "Δ  i - Suc 0")
  apply (rename_tac nat)
  apply (subgoal_tac "i - Suc Δ = nat")
  apply (simp (no_asm_simp))
  apply arith
  apply arith
  apply simp
  apply (rule impI)
  apply (case_tac "i = Δ")
  apply simp
  apply (rule T_Var)
  apply (erule wfE_subst)
  apply (erule wf_subtypeE)
  apply assumption
  apply simp
  apply (subgoal_tac "i < Δ")
  apply (subgoal_tac "Suc (Δ - Suc 0) = Δ")
  apply (simp (no_asm_simp))
  apply arith
  apply arith
  apply (rule T_Abs [THEN T_eq])
  apply simp
  apply (simp add: substT_substT [symmetric])
  apply (rule_tac T11="T11[Δ τ P]τ" in T_App)
  apply simp+
  apply (rule T_TAbs)
  apply simp
  apply (rule T_TApp [THEN T_eq])
  apply simp
  apply (rule substT_subtype)
  apply assumption
  apply assumption
  apply (simp add: substT_substT [symmetric])
  apply (rule_tac S="S[Δ τ P]τ" in T_Sub)
  apply simp
  apply (rule substT_subtype)
  apply assumption
  apply assumption
  done


subsection ‹Evaluation›

text ‹
\label{sec:evaluation}
For the formalization of the evaluation strategy, it is useful to first define
a set of {\it canonical values} that are not evaluated any further. The canonical
values of call-by-value \fsub{} are exactly the abstractions over term and type variables:
›

inductive_set
  "value" :: "trm set"
where
  Abs: "(λ:T. t)  value"
| TAbs: "(λ<:T. t)  value"

text ‹
The notion of a @{term value} is now used in the defintion of the evaluation
relation \mbox{t ⟼ t'›}. There are several ways for defining this evaluation
relation: Aydemir et al.\ \cite{PoplMark} advocate the use of {\it evaluation
contexts} that allow to separate the description of the ``immediate'' reduction rules,
i.e.\ $\beta$-reduction, from the description of the context in which these reductions
may occur in. The rationale behind this approach is to keep the formalization more modular.
We will take a closer look at this style of presentation in section
\secref{sec:evaluation-ctxt}. For the rest of this section, we will use a different
approach: both the ``immediate'' reductions and the reduction context are described
within the same inductive definition, where the context is described by additional
congruence rules.
›

inductive
  eval :: "trm  trm  bool"  (infixl "" 50)
where
  E_Abs: "v2  value  (λ:T11. t12)  v2  t12[0  v2]"
| E_TAbs: "(λ<:T11. t12) τ T2  t12[0 τ T2]"
| E_App1: "t  t'  t  u  t'  u"
| E_App2: "v  value  t  t'  v  t  v  t'"
| E_TApp: "t  t'  t τ T  t' τ T"

text ‹
Here, the rules E_Abs› and E_TAbs› describe the ``immediate'' reductions,
whereas E_App1›, E_App2›, and E_TApp› are additional congruence
rules describing reductions in a context. The most important theorems of this section
are the {\it preservation} theorem, stating that the reduction of a well-typed term
does not change its type, and the {\it progress} theorem, stating that reduction of
a well-typed term does not ``get stuck'' -- in other words, every well-typed, closed
term @{term t} is either a value, or there is a term @{term t'} to which @{term t}
can be reduced. The preservation theorem
is proved by induction on the derivation of @{term "Γ  t : T"}, followed by a case
distinction on the last rule used in the derivation of @{term "t  t'"}.
›

theorem preservation: ― ‹A.20›
  assumes H: "Γ  t : T"
  shows "t  t'  Γ  t' : T" using H
proof (induct arbitrary: t')
  case (T_Var Γ i U T t')
  from ‹Var i  t'
  show ?case by cases
next
  case (T_Abs T1 Γ t2 T2 t')
  from (λ:T1. t2)  t'
  show ?case by cases
next
  case (T_App Γ t1 T11 T12 t2 t')
  from t1  t2  t'
  show ?case
  proof cases
    case (E_Abs T11' t12)
    with T_App have "Γ  (λ:T11'. t12) : T11  T12" by simp
    then obtain S'
      where T11: "Γ  T11 <: T11'"
      and t12: "VarB T11'  Γ  t12 : S'"
      and S': "Γ  S'[0 τ Top]τ <: T12" by (rule Abs_type' [simplified]) blast
    from Γ  t2 : T11
    have "Γ  t2 : T11'" using T11 by (rule T_Sub)
    with t12 have "Γ  t12[0  t2] : S'[0 τ Top]τ"
      by (rule subst_type [where Δ="[]", simplified])
    hence "Γ  t12[0  t2] : T12" using S' by (rule T_Sub)
    with E_Abs show ?thesis by simp
  next
    case (E_App1 t'')
    from t1  t''
    have "Γ  t'' : T11  T12" by (rule T_App)
    hence "Γ  t''  t2 : T12" using Γ  t2 : T11
      by (rule typing.T_App)
    with E_App1 show ?thesis by simp
  next
    case (E_App2 t'')
    from t2  t''
    have "Γ  t'' : T11" by (rule T_App)
    with T_App(1) have "Γ  t1  t'' : T12"
      by (rule typing.T_App)
    with E_App2 show ?thesis by simp
  qed
next
  case (T_TAbs T1 Γ t2 T2 t')
  from (λ<:T1. t2)  t'
  show ?case by cases
next
  case (T_TApp Γ t1 T11 T12 T2 t')
  from t1 τ T2  t'
  show ?case
  proof cases
    case (E_TAbs T11' t12)
    with T_TApp have "Γ  (λ<:T11'. t12) : (∀<:T11. T12)" by simp
    then obtain S'
      where "TVarB T11  Γ  t12 : S'"
      and "TVarB T11  Γ  S' <: T12" by (rule TAbs_type') blast
    hence "TVarB T11  Γ  t12 : T12" by (rule T_Sub)
    hence "Γ  t12[0 τ T2] : T12[0 τ T2]τ" using T_TApp(3)
      by (rule substT_type [where Δ="[]", simplified])
    with E_TAbs show ?thesis by simp
  next
    case (E_TApp t'')
    from t1  t''
    have "Γ  t'' : (∀<:T11. T12)" by (rule T_TApp)
    hence "Γ  t'' τ T2 : T12[0 τ T2]τ" using Γ  T2 <: T11
      by (rule typing.T_TApp)
    with E_TApp show ?thesis by simp
  qed
next
  case (T_Sub Γ t S T t')
  from t  t'
  have "Γ  t' : S" by (rule T_Sub)
  then show ?case using Γ  S <: T
    by (rule typing.T_Sub)
qed

text ‹
The progress theorem is also proved by induction on the derivation of
@{term "[]  t : T"}. In the induction steps, we need the following two lemmas
about {\it canonical forms}
stating that closed values of types @{term "T1  T2"} and @{term "∀<:T1. T2"}
must be abstractions over term and type variables, respectively.
›

lemma Fun_canonical: ― ‹A.14(1)›
  assumes ty: "[]  v : T1  T2"
  shows "v  value  t S. v = (λ:S. t)" using ty
proof (induct "[]::env" v "T1  T2" arbitrary: T1 T2)
  case T_Abs
  show ?case by iprover
next
  case (T_App t1 T11 t2 T1 T2)
  from t1  t2  value›
  show ?case by cases
next
  case (T_TApp t1 T11 T12 T2 T1 T2')
  from t1 τ T2  value›
  show ?case by cases
next
  case (T_Sub t S T1 T2)
  from []  S <: T1  T2
  obtain S1 S2 where S: "S = S1  S2"
    by cases (auto simp add: T_Sub)
  show ?case by (rule T_Sub S)+
qed simp

lemma TyAll_canonical: ― ‹A.14(3)›
  assumes ty: "[]  v : (∀<:T1. T2)"
  shows "v  value  t S. v = (λ<:S. t)" using ty
proof (induct "[]::env" v "∀<:T1. T2" arbitrary: T1 T2)
  case (T_App t1 T11 t2 T1 T2)
  from t1  t2  value›
  show ?case by cases
next
  case T_TAbs
  show ?case by iprover
next
  case (T_TApp t1 T11 T12 T2 T1 T2')
  from t1 τ T2  value›
  show ?case by cases
next
  case (T_Sub t S T1 T2)
  from []  S <: (∀<:T1. T2)
  obtain S1 S2 where S: "S = (∀<:S1. S2)"
    by cases (auto simp add: T_Sub)
  show ?case by (rule T_Sub S)+
qed simp

theorem progress:
  assumes ty: "[]  t : T"
  shows "t  value  (t'. t  t')" using ty
proof (induct "[]::env" t T)
  case T_Var
  thus ?case by simp
next
  case T_Abs
  from value.Abs show ?case ..
next
  case (T_App t1 T11 T12 t2)
  hence "t1  value  (t'. t1  t')" by simp
  thus ?case
  proof
    assume t1_val: "t1  value"
    with T_App obtain t S where t1: "t1 = (λ:S. t)"
      by (auto dest!: Fun_canonical)
    from T_App have "t2  value  (t'. t2  t')" by simp
    thus ?thesis
    proof
      assume "t2  value"
      with t1 have "t1  t2  t[0  t2]"
        by simp (rule eval.intros)
      thus ?thesis by iprover
    next
      assume "t'. t2  t'"
      then obtain t' where "t2  t'" by iprover
      with t1_val have "t1  t2  t1  t'" by (rule eval.intros)
      thus ?thesis by iprover
    qed
  next
    assume "t'. t1  t'"
    then obtain t' where "t1  t'" ..
    hence "t1  t2  t'  t2" by (rule eval.intros)
    thus ?thesis by iprover
  qed
next
  case T_TAbs
  from value.TAbs show ?case ..
next
  case (T_TApp t1 T11 T12 T2)
  hence "t1  value  (t'. t1  t')" by simp
  thus ?case
  proof
    assume "t1  value"
    with T_TApp obtain t S where "t1 = (λ<:S. t)"
      by (auto dest!: TyAll_canonical)
    hence "t1 τ T2  t[0 τ T2]" by simp (rule eval.intros)
    thus ?thesis by iprover
  next
    assume "t'. t1  t'"
    then obtain t' where "t1  t'" ..
    hence "t1 τ T2  t' τ T2" by (rule eval.intros)
    thus ?thesis by iprover
  qed
next
  case (T_Sub t S T)
  show ?case by (rule T_Sub)
qed

end

Theory POPLmarkRecord

(*  Title:      POPLmark/POPLmarkRecord.thy
    Author:     Stefan Berghofer, TU Muenchen, 2005
*)

theory POPLmarkRecord
imports Basis
begin

section ‹Extending the calculus with records›

text ‹
\label{sec:record-calculus}
We now describe how the calculus introduced in the previous section can
be extended with records. An important point to note is that many of the
definitions and proofs developed for the simple calculus can be reused.
›


subsection ‹Types and Terms›

text ‹
In order to represent records, we also need a type of {\it field names}.
For this purpose, we simply use the type of {\it strings}. We extend the
datatype of types of System \fsub{} by a new constructor RcdT›
representing record types.
›

type_synonym name = string

datatype type =
    TVar nat
  | Top
  | Fun type type    (infixr "" 200)
  | TyAll type type  ("(3∀<:_./ _)" [0, 10] 10)
  | RcdT "(name × type) list"

type_synonym fldT = "name × type"
type_synonym rcdT = "(name × type) list"

datatype binding = VarB type | TVarB type

type_synonym env = "binding list"

primrec is_TVarB :: "binding  bool"
where
  "is_TVarB (VarB T) = False"
| "is_TVarB (TVarB T) = True"

primrec type_ofB :: "binding  type"
where
  "type_ofB (VarB T) = T"
| "type_ofB (TVarB T) = T"

primrec mapB :: "(type  type)  binding  binding"
where
  "mapB f (VarB T) = VarB (f T)"
| "mapB f (TVarB T) = TVarB (f T)"

text ‹
A record type is essentially an association list, mapping names of record fields
to their types.
The types of bindings and environments remain unchanged. The datatype trm›
of terms is extended with three new constructors Rcd›, Proj›,
and LET›, denoting construction of a new record, selection of
a specific field of a record (projection), and matching of a record against
a pattern, respectively. A pattern, represented by datatype pat›,
can be either a variable matching any value of a given type, or a nested
record pattern. Due to the encoding of variables using de Bruijn indices,
a variable pattern only consists of a type.
›

datatype pat = PVar type | PRcd "(name × pat) list"

datatype trm =
    Var nat
  | Abs type trm   ("(3λ:_./ _)" [0, 10] 10)
  | TAbs type trm  ("(3λ<:_./ _)" [0, 10] 10)
  | App trm trm    (infixl "" 200)
  | TApp trm type  (infixl "τ" 200)
  | Rcd "(name × trm) list"
  | Proj trm name  ("(_.._)" [90, 91] 90)
  | LET pat trm trm ("(LET (_ =/ _)/ IN (_))" 10)

type_synonym fld = "name × trm"
type_synonym rcd = "(name × trm) list"
type_synonym fpat = "name × pat"
type_synonym rpat = "(name × pat) list"

text ‹
In order to motivate the typing and evaluation rules for the LET›, it is
important to note that an expression of the form
@{text [display] "LET PRcd [(l1, PVar T1), …, (ln, PVar Tn)] = Rcd [(l1, v1), …, (ln, vn)] IN t"}
can be treated like a nested abstraction (λ:T1. … λ:Tn. t) ∙ v1 ∙ … ∙ vn


subsection ‹Lifting and Substitution›

primrec psize :: "pat  nat" ("_p")
  and rsize :: "rpat  nat" ("_r")
  and fsize :: "fpat  nat" ("_f")
where
  "PVar Tp = 1"
| "PRcd fsp = fsr"
| "[]r = 0"
| "f  fsr = ff + fsr"
| "(l, p)f = pp"

primrec liftT :: "nat  nat  type  type" ("τ")
  and liftrT :: "nat  nat  rcdT  rcdT" ("rτ")
  and liftfT :: "nat  nat  fldT  fldT" ("fτ")
where
  "τ n k (TVar i) = (if i < k then TVar i else TVar (i + n))"
| "τ n k Top = Top"
| "τ n k (T  U) = τ n k T  τ n k U"
| "τ n k (∀<:T. U) = (∀<:τ n k T. τ n (k + 1) U)"
| "τ n k (RcdT fs) = RcdT (rτ n k fs)"
| "rτ n k [] = []"
| "rτ n k (f  fs) = fτ n k f  rτ n k fs"
| "fτ n k (l, T) = (l, τ n k T)"

primrec liftp :: "nat  nat  pat  pat" ("p")
  and liftrp :: "nat  nat  rpat  rpat" ("rp")
  and liftfp :: "nat  nat  fpat  fpat" ("fp")
where
  "p n k (PVar T) = PVar (τ n k T)"
| "p n k (PRcd fs) = PRcd (rp n k fs)"
| "rp n k [] = []"
| "rp n k (f  fs) = fp n k f  rp n k fs"
| "fp n k (l, p) = (l, p n k p)"

primrec lift :: "nat  nat  trm  trm" ("")
  and liftr :: "nat  nat  rcd  rcd" ("r")
  and liftf :: "nat  nat  fld  fld" ("f")
where
  " n k (Var i) = (if i < k then Var i else Var (i + n))"
| " n k (λ:T. t) = (λ:τ n k T.  n (k + 1) t)"
| " n k (λ<:T. t) = (λ<:τ n k T.  n (k + 1) t)"
| " n k (s  t) =  n k s   n k t"
| " n k (t τ T) =  n k t τ τ n k T"
| " n k (Rcd fs) = Rcd (r n k fs)"
| " n k (t..a) = ( n k t)..a"
| " n k (LET p = t IN u) = (LET p n k p =  n k t IN  n (k + pp) u)"
| "r n k [] = []"
| "r n k (f  fs) = f n k f  r n k fs"
| "f n k (l, t) = (l,  n k t)"

primrec substTT :: "type  nat  type  type"  ("_[_ τ _]τ" [300, 0, 0] 300)
  and substrTT :: "rcdT  nat  type  rcdT"  ("_[_ τ _]rτ" [300, 0, 0] 300)
  and substfTT :: "fldT  nat  type  fldT"  ("_[_ τ _]fτ" [300, 0, 0] 300)
where
  "(TVar i)[k τ S]τ =
     (if k < i then TVar (i - 1) else if i = k then τ k 0 S else TVar i)"
| "Top[k τ S]τ = Top"
| "(T  U)[k τ S]τ = T[k τ S]τ  U[k τ S]τ"
| "(∀<:T. U)[k τ S]τ = (∀<:T[k τ S]τ. U[k+1 τ S]τ)"
| "(RcdT fs)[k τ S]τ = RcdT (fs[k τ S]rτ)"
| "[][k τ S]rτ = []"
| "(f  fs)[k τ S]rτ = f[k τ S]fτ  fs[k τ S]rτ"
| "(l, T)[k τ S]fτ = (l, T[k τ S]τ)"

primrec substpT :: "pat  nat  type  pat"  ("_[_ τ _]p" [300, 0, 0] 300)
  and substrpT :: "rpat  nat  type  rpat"  ("_[_ τ _]rp" [300, 0, 0] 300)
  and substfpT :: "fpat  nat  type  fpat"  ("_[_ τ _]fp" [300, 0, 0] 300)
where
  "(PVar T)[k τ S]p = PVar (T[k τ S]τ)"
| "(PRcd fs)[k τ S]p = PRcd (fs[k τ S]rp)"
| "[][k τ S]rp = []"
| "(f  fs)[k τ S]rp = f[k τ S]fp  fs[k τ S]rp"
| "(l, p)[k τ S]fp = (l, p[k τ S]p)"

primrec decp :: "nat  nat  pat  pat"  ("p")
where
  "p 0 k p = p"
| "p (Suc n) k p = p n k (p[k τ Top]p)"

text ‹
In addition to the lifting and substitution functions already needed for the
basic calculus, we also have to define lifting and substitution functions
for patterns, which we denote by @{term "p n k p"} and @{term "T[k τ S]p"},
respectively. The extension of the existing lifting and substitution
functions to records is fairly standard.
›

primrec subst :: "trm  nat  trm  trm"  ("_[_  _]" [300, 0, 0] 300)
  and substr :: "rcd  nat  trm  rcd"  ("_[_  _]r" [300, 0, 0] 300)
  and substf :: "fld  nat  trm  fld"  ("_[_  _]f" [300, 0, 0] 300)
where
  "(Var i)[k  s] =
     (if k < i then Var (i - 1) else if i = k then  k 0 s else Var i)"
| "(t  u)[k  s] = t[k  s]  u[k  s]"
| "(t τ T)[k  s] = t[k  s] τ T[k τ Top]τ"
| "(λ:T. t)[k  s] = (λ:T[k τ Top]τ. t[k+1  s])"
| "(λ<:T. t)[k  s] = (λ<:T[k τ Top]τ. t[k+1  s])"
| "(Rcd fs)[k  s] = Rcd (fs[k  s]r)"
| "(t..a)[k  s] = (t[k  s])..a"
| "(LET p = t IN u)[k  s] = (LET p 1 k p = t[k  s] IN u[k + pp  s])"
| "[][k  s]r = []"
| "(f  fs)[k  s]r = f[k  s]f  fs[k  s]r"
| "(l, t)[k  s]f = (l, t[k  s])"

text ‹
Note that the substitution function on terms is defined simultaneously
with a substitution function @{term "fs[k  s]r"} on records (i.e.\ lists
of fields), and a substitution function @{term "f[k  s]f"} on fields.
To avoid conflicts with locally bound variables, we have to add an offset
@{term "pp"} to @{term k} when performing substitution in the body of
the LET› binder, where @{term "pp"} is the number of variables
in the pattern @{term p}.
›

primrec substT :: "trm  nat  type  trm"  ("_[_ τ _]" [300, 0, 0] 300)
  and substrT :: "rcd  nat  type  rcd"  ("_[_ τ _]r" [300, 0, 0] 300)
  and substfT :: "fld  nat  type  fld"  ("_[_ τ _]f" [300, 0, 0] 300)
where
  "(Var i)[k τ S] = (if k < i then Var (i - 1) else Var i)"
| "(t  u)[k τ S] = t[k τ S]  u[k τ S]"
| "(t τ T)[k τ S] = t[k τ S] τ T[k τ S]τ"
| "(λ:T. t)[k τ S] = (λ:T[k τ S]τ. t[k+1 τ S])"
| "(λ<:T. t)[k τ S] = (λ<:T[k τ S]τ. t[k+1 τ S])"
| "(Rcd fs)[k τ S] = Rcd (fs[k τ S]r)"
| "(t..a)[k τ S] = (t[k τ S])..a"
| "(LET p = t IN u)[k τ S] =
     (LET p[k τ S]p = t[k τ S] IN u[k + pp τ S])"
| "[][k τ S]r = []"
| "(f  fs)[k τ S]r = f[k τ S]f  fs[k τ S]r"
| "(l, t)[k τ S]f = (l, t[k τ S])"

primrec liftE :: "nat  nat  env  env" ("e")
where
  "e n k [] = []"
| "e n k (B  Γ) = mapB (τ n (k + Γ)) B  e n k Γ"

primrec substE :: "env  nat  type  env"  ("_[_ τ _]e" [300, 0, 0] 300)
where
  "[][k τ T]e = []"
| "(B  Γ)[k τ T]e = mapB (λU. U[k + Γ τ T]τ) B  Γ[k τ T]e"

text ‹
For the formalization of the reduction
rules for LET›, we need a function \mbox{t[k ↦s us]›}
for simultaneously substituting terms @{term us} for variables with
consecutive indices:
›

primrec substs :: "trm  nat  trm list  trm"  ("_[_ s _]" [300, 0, 0] 300)
where
  "t[k s []] = t"
| "t[k s u  us] = t[k + us  u][k s us]"

primrec decT :: "nat  nat  type  type"  ("τ")
where
  "τ 0 k T = T"
| "τ (Suc n) k T = τ n k (T[k τ Top]τ)"

primrec decE :: "nat  nat  env  env"  ("e")
where
  "e 0 k Γ = Γ"
| "e (Suc n) k Γ = e n k (Γ[k τ Top]e)"

primrec decrT :: "nat  nat  rcdT  rcdT"  ("rτ")
where
  "rτ 0 k fTs = fTs"
| "rτ (Suc n) k fTs = rτ n k (fTs[k τ Top]rτ)"

text ‹
The lemmas about substitution and lifting are very similar to those needed
for the simple calculus without records, with the difference that most
of them have to be proved simultaneously with a suitable property for
records.
›

lemma liftE_length [simp]: "e n k Γ = Γ"
  by (induct Γ) simp_all

lemma substE_length [simp]: "Γ[k τ U]e = Γ"
  by (induct Γ) simp_all

lemma liftE_nth [simp]:
  "(e n k Γ)i = map_option (mapB (τ n (k + Γ - i - 1))) (Γi)"
  apply (induct Γ arbitrary: i)
  apply simp
  apply simp
  apply (case_tac i)
  apply simp
  apply simp
  done

lemma substE_nth [simp]:
  "(Γ[0 τ T]e)i = map_option (mapB (λU. U[Γ - i - 1 τ T]τ)) (Γi)"
  apply (induct Γ arbitrary: i)
  apply simp
  apply simp
  apply (case_tac i)
  apply simp
  apply simp
  done

lemma liftT_liftT [simp]:
  "i  j  j  i + m  τ n j (τ m i T) = τ (m + n) i T"
  "i  j  j  i + m  rτ n j (rτ m i rT) = rτ (m + n) i rT"
  "i  j  j  i + m  fτ n j (fτ m i fT) = fτ (m + n) i fT"
  by (induct T and rT and fT arbitrary: i j m n and i j m n and i j m n
    rule: liftT.induct liftrT.induct liftfT.induct) simp_all

lemma liftT_liftT' [simp]:
  "i + m  j  τ n j (τ m i T) = τ m i (τ n (j - m) T)"
  "i + m  j  rτ n j (rτ m i rT) = rτ m i (rτ n (j - m) rT)"
  "i + m  j  fτ n j (fτ m i fT) = fτ m i (fτ n (j - m) fT)"
  apply (induct T and rT and fT arbitrary: i j m n and i j m n and i j m n
    rule: liftT.induct liftrT.induct liftfT.induct)
  apply simp_all
  apply arith
  apply (subgoal_tac "Suc j - m = Suc (j - m)")
  apply simp
  apply arith
  done

lemma lift_size [simp]:
  "size (τ n k T) = size T"
  "size_list (size_prod (λx. 0) size) (rτ n k rT) = size_list (size_prod (λx. 0) size) rT"
  "size_prod (λx. 0) size (fτ n k fT) = size_prod (λx. 0) size fT"
  by (induct T and rT and fT arbitrary: k and k and k
    rule: liftT.induct liftrT.induct liftfT.induct) simp_all

lemma liftT0 [simp]:
  "τ 0 i T = T"
  "rτ 0 i rT = rT"
  "fτ 0 i fT = fT"
  by (induct T and rT and fT arbitrary: i and i and i
    rule: liftT.induct liftrT.induct liftfT.induct) simp_all

lemma liftp0 [simp]:
  "p 0 i p = p"
  "rp 0 i fs = fs"
  "fp 0 i f = f"
  by (induct p and fs and f arbitrary: i and i and i
    rule: liftp.induct liftrp.induct liftfp.induct) simp_all

lemma lift0 [simp]:
  " 0 i t = t"
  "r 0 i fs = fs"
  "f 0 i f = f"
  by (induct t and fs and f arbitrary: i and i and i
    rule: lift.induct liftr.induct liftf.induct) simp_all

theorem substT_liftT [simp]:
  "k  k'  k' < k + n  (τ n k T)[k' τ U]τ = τ (n - 1) k T"
  "k  k'  k' < k + n  (rτ n k rT)[k' τ U]rτ = rτ (n - 1) k rT"
  "k  k'  k' < k + n  (fτ n k fT)[k' τ U]fτ = fτ (n - 1) k fT"
  by (induct T and rT and fT arbitrary: k k' and k k' and k k'
    rule: liftT.induct liftrT.induct liftfT.induct) simp_all

theorem liftT_substT [simp]:
  "k  k'  τ n k (T[k' τ U]τ) = τ n k T[k' + n τ U]τ"
  "k  k'  rτ n k (rT[k' τ U]rτ) = rτ n k rT[k' + n τ U]rτ"
  "k  k'  fτ n k (fT[k' τ U]fτ) = fτ n k fT[k' + n τ U]fτ"
  apply (induct T and rT and fT arbitrary: k k' and k k' and k k'
    rule: liftT.induct liftrT.induct liftfT.induct)
  apply simp_all
  done

theorem liftT_substT' [simp]:
  "k' < k 
     τ n k (T[k' τ U]τ) = τ n (k + 1) T[k' τ τ n (k - k') U]τ"
  "k' < k 
     rτ n k (rT[k' τ U]rτ) = rτ n (k + 1) rT[k' τ τ n (k - k') U]rτ"
  "k' < k 
     fτ n k (fT[k' τ U]fτ) = fτ n (k + 1) fT[k' τ τ n (k - k') U]fτ"
  apply (induct T and rT and fT arbitrary: k k' and k k' and k k'
    rule: liftT.induct liftrT.induct liftfT.induct)
  apply simp_all
  apply arith
  done

lemma liftT_substT_Top [simp]:
  "k  k'  τ n k' (T[k τ Top]τ) = τ n (Suc k') T[k τ Top]τ"
  "k  k'  rτ n k' (rT[k τ Top]rτ) = rτ n (Suc k') rT[k τ Top]rτ"
  "k  k'  fτ n k' (fT[k τ Top]fτ) = fτ n (Suc k') fT[k τ Top]fτ"
  apply (induct T and rT and fT arbitrary: k k' and k k' and k k'
    rule: liftT.induct liftrT.induct liftfT.induct)
  apply simp_all
  apply arith
  done

theorem liftE_substE [simp]:
  "k  k'  e n k (Γ[k' τ U]e) = e n k Γ[k' + n τ U]e"
  apply (induct Γ arbitrary: k k' and k k' and k k')
  apply simp_all
  apply (case_tac a)
  apply (simp_all add: ac_simps)
  done

lemma liftT_decT [simp]:
  "k  k'  τ n k' (τ m k T) = τ m k (τ n (m + k') T)"
  by (induct m arbitrary: T) simp_all

lemma liftT_substT_strange:
  "τ n k T[n + k τ U]τ = τ n (Suc k) T[k τ τ n 0 U]τ"
  "rτ n k rT[n + k τ U]rτ = rτ n (Suc k) rT[k τ τ n 0 U]rτ"
  "fτ n k fT[n + k τ U]fτ = fτ n (Suc k) fT[k τ τ n 0 U]fτ"
  apply (induct T and rT and fT arbitrary: n k and n k and n k
    rule: liftT.induct liftrT.induct liftfT.induct)
  apply simp_all
  apply (thin_tac "x. PROP P x" for P :: "_  prop")
  apply (drule_tac x=n in meta_spec)
  apply (drule_tac x="Suc k" in meta_spec)
  apply simp
  done

lemma liftp_liftp [simp]:
  "k  k'  k'  k + n  p n' k' (p n k p) = p (n + n') k p"
  "k  k'  k'  k + n  rp n' k' (rp n k rp) = rp (n + n') k rp"
  "k  k'  k'  k + n  fp n' k' (fp n k fp) = fp (n + n') k fp"
  by (induct p and rp and fp arbitrary: k k' and k k' and k k'
    rule: liftp.induct liftrp.induct liftfp.induct) simp_all

lemma liftp_psize[simp]:
  "p n k pp = pp"
  "rp n k fsr = fsr"
  "fp n k ff = ff"
  by (induct p and fs and f rule: liftp.induct liftrp.induct liftfp.induct) simp_all

lemma lift_lift [simp]:
  "k  k'  k'  k + n   n' k' ( n k t) =  (n + n') k t"
  "k  k'  k'  k + n  r n' k' (r n k fs) = r (n + n') k fs"
  "k  k'  k'  k + n  f n' k' (f n k f) = f (n + n') k f"
 by (induct t and fs and f arbitrary: k k' and k k' and k k'
   rule: lift.induct liftr.induct liftf.induct) simp_all

lemma liftE_liftE [simp]:
  "k  k'  k'  k + n  e n' k' (e n k Γ) = e (n + n') k Γ"
  apply (induct Γ arbitrary: k k')
  apply simp_all
  apply (case_tac a)
  apply simp_all
  done

lemma liftE_liftE' [simp]:
  "i + m  j  e n j (e m i Γ) = e m i (e n (j - m) Γ)"
  apply (induct Γ arbitrary: i j m n)
  apply simp_all
  apply (case_tac a)
  apply simp_all
  done

lemma substT_substT:
  "i  j 
     T[Suc j τ V]τ[i τ U[j - i τ V]τ]τ = T[i τ U]τ[j τ V]τ"
  "i  j 
     rT[Suc j τ V]rτ[i τ U[j - i τ V]τ]rτ = rT[i τ U]rτ[j τ V]rτ"
  "i  j 
     fT[Suc j τ V]fτ[i τ U[j - i τ V]τ]fτ = fT[i τ U]fτ[j τ V]fτ"
  apply (induct T and rT and fT arbitrary: i j U V and i j U V and i j U V
    rule: liftT.induct liftrT.induct liftfT.induct)
  apply (simp_all add: diff_Suc split: nat.split)
  apply (thin_tac "x. PROP P x" for P :: "_  prop")
  apply (drule_tac x="Suc i" in meta_spec)
  apply (drule_tac x="Suc j" in meta_spec)
  apply simp
  done

lemma substT_decT [simp]:
  "k  j  (τ i k T)[j τ U]τ = τ i k (T[i + j τ U]τ)"
  by (induct i arbitrary: T j) (simp_all add: substT_substT [symmetric])

lemma substT_decT' [simp]:
  "i  j  τ k (Suc j) T[i τ Top]τ = τ k j (T[i τ Top]τ)"
  by (induct k arbitrary: i j T) (simp_all add: substT_substT [of _ _ _ _ Top, simplified])

lemma substE_substE:
  "i  j  Γ[Suc j τ V]e[i τ U[j - i τ V]τ]e = Γ[i τ U]e[j τ V]e"
  apply (induct Γ)
  apply (case_tac [2] a)
  apply (simp_all add: substT_substT [symmetric])
  done

lemma substT_decE [simp]:
  "i  j  e k (Suc j) Γ[i τ Top]e = e k j (Γ[i τ Top]e)"
  by (induct k arbitrary: i j Γ) (simp_all add: substE_substE [of _ _ _ _ Top, simplified])

lemma liftE_app [simp]: "e n k (Γ @ Δ) = e n (k + Δ) Γ @ e n k Δ"
  by (induct Γ arbitrary: k) (simp_all add: ac_simps)

lemma substE_app [simp]:
  "(Γ @ Δ)[k τ T]e = Γ[k + Δ τ T]e @ Δ[k τ T]e"
  by (induct Γ) (simp_all add: ac_simps)

lemma substs_app [simp]: "t[k s ts @ us] = t[k + us s ts][k s us]"
  by (induct ts arbitrary: t k) (simp_all add: ac_simps)

theorem decE_Nil [simp]: "e n k [] = []"
  by (induct n) simp_all

theorem decE_Cons [simp]:
  "e n k (B  Γ) = mapB (τ n (k + Γ)) B  e n k Γ"
  apply (induct n arbitrary: B Γ)
  apply (case_tac B)
  apply (case_tac [3] B)
  apply simp_all
  done

theorem decE_app [simp]:
  "e n k (Γ @ Δ) = e n (k + Δ) Γ @ e n k Δ"
  by (induct n arbitrary: Γ Δ) simp_all

theorem decT_liftT [simp]:
  "k  k'  k' + m  k + n  τ m k' (τ n k Γ) = τ (n - m) k Γ"
  apply (induct m arbitrary: n)
  apply (subgoal_tac [2] "k' + m  k + (n - Suc 0)")
  apply simp_all
  done

theorem decE_liftE [simp]:
  "k  k'  k' + m  k + n  e m k' (e n k Γ) = e (n - m) k Γ"
  apply (induct Γ arbitrary: k k')
  apply (case_tac [2] a)
  apply simp_all
  done

theorem liftE0 [simp]: "e 0 k Γ = Γ"
  apply (induct Γ)
  apply (case_tac [2] a)
  apply simp_all
  done

lemma decT_decT [simp]: "τ n k (τ n' (k + n) T) = τ (n + n') k T"
  by (induct n arbitrary: k T) simp_all

lemma decE_decE [simp]: "e n k (e n' (k + n) Γ) = e (n + n') k Γ"
  by (induct n arbitrary: k Γ) simp_all

lemma decE_length [simp]: "e n k Γ = Γ"
  by (induct Γ) simp_all

lemma liftrT_assoc_None [simp]: "(rτ n k fsl? = ) = (fsl? = )"
  by (induct fs rule: list.induct) auto

lemma liftrT_assoc_Some: "fsl? = T  rτ n k fsl? = τ n k T"
  by (induct fs rule: list.induct) auto

lemma liftrp_assoc_None [simp]: "(rp n k fpsl? = ) = (fpsl? = )"
  by (induct fps rule: list.induct) auto

lemma liftr_assoc_None [simp]: "(r n k fsl? = ) = (fsl? = )"
  by (induct fs rule: list.induct) auto

lemma unique_liftrT [simp]: "unique (rτ n k fs) = unique fs"
  by (induct fs rule: list.induct) auto

lemma substrTT_assoc_None [simp]: "(fs[k τ U]rτa? = ) = (fsa? = )"
  by (induct fs rule: list.induct) auto

lemma substrTT_assoc_Some [simp]:
  "fsa? = T  fs[k τ U]rτa? = T[k τ U]τ"
  by (induct fs rule: list.induct) auto

lemma substrT_assoc_None [simp]: "(fs[k τ P]rl? = ) = (fsl? = )"
  by (induct fs rule: list.induct) auto

lemma substrp_assoc_None [simp]: "(fps[k τ U]rpl? = ) = (fpsl? = )"
  by (induct fps rule: list.induct) auto

lemma substr_assoc_None [simp]: "(fs[k  u]rl? = ) = (fsl? = )"
  by (induct fs rule: list.induct) auto

lemma unique_substrT [simp]: "unique (fs[k τ U]rτ) = unique fs"
  by (induct fs rule: list.induct) auto

lemma liftrT_set: "(a, T)  set fs  (a, τ n k T)  set (rτ n k fs)"
  by (induct fs rule: list.induct) auto

lemma liftrT_setD:
  "(a, T)  set (rτ n k fs)  T'. (a, T')  set fs  T = τ n k T'"
  by (induct fs rule: list.induct) auto

lemma substrT_set: "(a, T)  set fs  (a, T[k τ U]τ)  set (fs[k τ U]rτ)"
  by (induct fs rule: list.induct) auto

lemma substrT_setD:
  "(a, T)  set (fs[k τ U]rτ)  T'. (a, T')  set fs  T = T'[k τ U]τ"
  by (induct fs rule: list.induct) auto


subsection ‹Well-formedness›

text ‹
The definition of well-formedness is extended with a rule stating that a
record type @{term "RcdT fs"} is well-formed, if for all fields @{term "(l, T)"}
contained in the list @{term fs}, the type @{term T} is well-formed, and
all labels @{term l} in @{term fs} are {\it unique}.
›

inductive
  well_formed :: "env  type  bool"  ("_ wf _" [50, 50] 50)
where
  wf_TVar: "Γi = TVarB T  Γ wf TVar i"
| wf_Top: "Γ wf Top"
| wf_arrow: "Γ wf T  Γ wf U  Γ wf T  U"
| wf_all: "Γ wf T  TVarB T  Γ wf U  Γ wf (∀<:T. U)"
| wf_RcdT: "unique fs  (l, T)set fs. Γ wf T  Γ wf RcdT fs"

inductive
  well_formedE :: "env  bool"  ("_ wf" [50] 50)
  and well_formedB :: "env  binding  bool"  ("_ wfB _" [50, 50] 50)
where
  "Γ wfB B  Γ wf type_ofB B"
| wf_Nil: "[] wf"
| wf_Cons: "Γ wfB B  Γ wf  B  Γ wf"

inductive_cases well_formed_cases:
  "Γ wf TVar i"
  "Γ wf Top"
  "Γ wf T  U"
  "Γ wf (∀<:T. U)"
  "Γ wf (RcdT fTs)"

inductive_cases well_formedE_cases:
  "B  Γ wf"

lemma wf_TVarB: "Γ wf T  Γ wf  TVarB T  Γ wf"
  by (rule wf_Cons) simp_all

lemma wf_VarB: "Γ wf T  Γ wf  VarB T  Γ wf"
  by (rule wf_Cons) simp_all

lemma map_is_TVarb:
  "map is_TVarB Γ' = map is_TVarB Γ 
    Γi = TVarB T  T. Γ'i = TVarB T"
  apply (induct Γ arbitrary: Γ' T i)
  apply simp
  apply (auto split: nat.split_asm)
  apply (case_tac z)
  apply simp_all
  done

lemma wf_equallength:
  assumes H: "Γ wf T"
  shows "map is_TVarB Γ' = map is_TVarB Γ  Γ' wf T" using H
  apply (induct arbitrary: Γ')
  apply (auto intro: well_formed.intros dest: map_is_TVarb)+
  apply (fastforce intro: well_formed.intros)
  done

lemma wfE_replace:
  "Δ @ B  Γ wf  Γ wfB B'  is_TVarB B' = is_TVarB B 
     Δ @ B'  Γ wf"
  apply (induct Δ)
  apply simp
  apply (erule wf_Cons)
  apply (erule well_formedE_cases)
  apply assumption
  apply simp
  apply (erule well_formedE_cases)
  apply (rule wf_Cons)
  apply (case_tac a)
  apply simp
  apply (rule wf_equallength)
  apply assumption
  apply simp
  apply simp
  apply (rule wf_equallength)
  apply assumption
  apply simp
  apply simp
  done

lemma wf_weaken:
  assumes H: "Δ @ Γ wf T"
  shows "e (Suc 0) 0 Δ @ B  Γ wf τ (Suc 0) Δ T"
  using H
  apply (induct "Δ @ Γ" T arbitrary: Δ)
  apply simp_all
  apply (rule conjI)
  apply (rule impI)
  apply (rule wf_TVar)
  apply simp
  apply (rule impI)
  apply (rule wf_TVar)
  apply (subgoal_tac "Suc i - Δ = Suc (i - Δ)")
  apply simp
  apply arith
  apply (rule wf_Top)
  apply (rule wf_arrow)
  apply simp
  apply simp
  apply (rule wf_all)
  apply simp
  apply simp
  ― ‹records›
  apply (rule wf_RcdT)
  apply simp
  apply (rule ballpI)
  apply (drule liftrT_setD)
  apply (erule exE conjE)+
  apply (drule_tac x=l and y="T[Δ τ Top]τ" in bpspec)
  apply simp+
  done

lemma wf_weaken': "Γ wf T  Δ @ Γ wf τ Δ 0 T"
  apply (induct Δ)
  apply simp_all
  apply (drule_tac B=a in wf_weaken [of "[]", simplified])
  apply simp
  done

lemma wfE_weaken: "Δ @ Γ wf  Γ wfB B  e (Suc 0) 0 Δ @ B  Γ wf"
  apply (induct Δ)
  apply simp
  apply (rule wf_Cons)
  apply assumption+
  apply simp
  apply (rule wf_Cons)
  apply (erule well_formedE_cases)
  apply (case_tac a)
  apply simp
  apply (rule wf_weaken)
  apply assumption
  apply simp
  apply (rule wf_weaken)
  apply assumption
  apply (erule well_formedE_cases)
  apply simp
  done

lemma wf_liftB:
  assumes H: "Γ wf"
  shows "Γi = VarB T  Γ wf τ (Suc i) 0 T"
  using H
  apply (induct arbitrary: i)
  apply simp
  apply (simp split: nat.split_asm)
  apply (frule_tac B="VarB T" in wf_weaken [of "[]", simplified])
  apply simp+
  apply (rename_tac nat)
  apply (drule_tac x=nat in meta_spec)
  apply simp
  apply (frule_tac T="τ (Suc nat) 0 T" in wf_weaken [of "[]", simplified])
  apply simp
  done

theorem wf_subst:
  "Δ @ B  Γ wf T  Γ wf U  Δ[0 τ U]e @ Γ wf T[Δ τ U]τ"
  "(l, T)  set (rT::rcdT). Δ @ B  Γ wf T  Γ wf U 
     (l, T)  set rT. Δ[0 τ U]e @ Γ wf T[Δ τ U]τ"
  "Δ @ B  Γ wf snd (fT::fldT)  Γ wf U 
     Δ[0 τ U]e @ Γ wf snd fT[Δ τ U]τ"
  apply (induct T and rT and fT arbitrary: Δ and Δ and Δ
    rule: liftT.induct liftrT.induct liftfT.induct)
  apply (rename_tac nat Δ)
  apply simp_all
  apply (rule conjI)
  apply (rule impI)
  apply (drule_tac Γ=Γ and Δ="Δ[0 τ U]e" in wf_weaken')
  apply simp
  apply (rule impI conjI)+
  apply (erule well_formed_cases)
  apply (rule wf_TVar)
  apply (simp split: nat.split_asm)
  apply (subgoal_tac "Δ  nat - Suc 0")
  apply (rename_tac nata)
  apply (subgoal_tac "nat - Suc Δ = nata")
  apply (simp (no_asm_simp))
  apply arith
  apply arith
  apply (rule impI)
  apply (erule well_formed_cases)
  apply (rule wf_TVar)
  apply simp
  apply (rule wf_Top)
  apply (erule well_formed_cases)
  apply (rule wf_arrow)
  apply simp+
  apply (rename_tac type1 type2 Δ)
  apply (erule well_formed_cases)
  apply (rule wf_all)
  apply simp
  apply (thin_tac "x. PROP P x" for P :: "_  prop")
  apply (drule_tac x="TVarB type1  Δ" in meta_spec)
  apply simp
  apply (erule well_formed_cases)
  apply (rule wf_RcdT)
  apply simp
  apply (rule ballpI)
  apply (drule substrT_setD)
  apply (erule exE conjE)+
  apply (drule meta_spec)
  apply (drule meta_mp)
  apply assumption
  apply (thin_tac "x  S. P x" for S P)
  apply (drule bpspec)
  apply assumption
  apply simp
  apply (simp add: split_paired_all)
  done

theorem wf_dec: "Δ @ Γ wf T  Γ wf τ Δ 0 T"
  apply (induct Δ arbitrary: T)
  apply simp
  apply simp
  apply (drule wf_subst(1) [of "[]", simplified])
  apply (rule wf_Top)
  apply simp
  done

theorem wfE_subst: "Δ @ B  Γ wf  Γ wf U  Δ[0 τ U]e @ Γ wf"
  apply (induct Δ)
  apply simp
  apply (erule well_formedE_cases)
  apply assumption
  apply simp
  apply (case_tac a)
  apply (erule well_formedE_cases)
  apply (rule wf_Cons)
  apply simp
  apply (rule wf_subst)
  apply assumption+
  apply simp
  apply (erule well_formedE_cases)
  apply (rule wf_Cons)
  apply simp
  apply (rule wf_subst)
  apply assumption+
  done

subsection ‹Subtyping›

text ‹
The definition of the subtyping judgement is extended with a rule SA_Rcd› stating
that a record type @{term "RcdT fs"} is a subtype of @{term "RcdT fs'"}, if
for all fields \mbox{@{term "(l, T)"}} contained in @{term fs'}, there exists a
corresponding field @{term "(l, S)"} such that @{term S} is a subtype of @{term T}.
If the list @{term fs'} is empty, SA_Rcd› can appear as a leaf in
the derivation tree of the subtyping judgement. Therefore, the introduction
rule needs an additional premise @{term "Γ wf"} to make sure that only
subtyping judgements with well-formed contexts are derivable. Moreover,
since @{term fs} can contain additional fields not present in @{term fs'},
we also have to require that the type @{term "RcdT fs"} is well-formed.
In order to ensure that the type @{term "RcdT fs'"} is well-formed, too,
we only have to require that labels in @{term fs'} are unique, since,
by induction on the subtyping derivation, all types contained in @{term fs'}
are already well-formed.
›

inductive
  subtyping :: "env  type  type  bool"  ("_  _ <: _" [50, 50, 50] 50)
where
  SA_Top: "Γ wf  Γ wf S  Γ  S <: Top"
| SA_refl_TVar: "Γ wf  Γ wf TVar i  Γ  TVar i <: TVar i"
| SA_trans_TVar: "Γi = TVarB U 
    Γ  τ (Suc i) 0 U <: T  Γ  TVar i <: T"
| SA_arrow: "Γ  T1 <: S1  Γ  S2 <: T2  Γ  S1  S2 <: T1  T2"
| SA_all: "Γ  T1 <: S1  TVarB T1  Γ  S2 <: T2 
    Γ  (∀<:S1. S2) <: (∀<:T1. T2)"
| SA_Rcd: "Γ wf  Γ wf RcdT fs  unique fs' 
    (l, T)set fs'. S. (l, S)set fs  Γ  S <: T  Γ  RcdT fs <: RcdT fs'"

lemma wf_subtype_env:
  assumes PQ: "Γ  P <: Q"
  shows "Γ wf" using PQ
  by induct assumption+

lemma wf_subtype:
  assumes PQ: "Γ  P <: Q"
  shows "Γ wf P  Γ wf Q" using PQ
  by induct (auto intro: well_formed.intros elim!: wf_equallength)

lemma wf_subtypeE:
  assumes H: "Γ  T <: U"
  and H': "Γ wf  Γ wf T  Γ wf U  P"
  shows "P"
  apply (rule H')
  apply (rule wf_subtype_env)
  apply (rule H)
  apply (rule wf_subtype [OF H, THEN conjunct1])
  apply (rule wf_subtype [OF H, THEN conjunct2])
  done

lemma subtype_refl: ― ‹A.1›
  "Γ wf  Γ wf T  Γ  T <: T"
  "Γ wf  (l::name, T)set fTs. Γ wf T  Γ  T <: T"
  "Γ wf  Γ wf snd (fT::fldT)  Γ  snd fT <: snd fT"
  by (induct T and fTs and fT arbitrary: Γ and Γ and Γ
    rule: liftT.induct liftrT.induct liftfT.induct,
    simp_all add: split_paired_all, simp_all)
    (blast intro: subtyping.intros wf_Nil wf_TVarB bexpI intro!: ballpI
       elim: well_formed_cases ballpE elim!: bexpE)+

lemma subtype_weaken:
  assumes H: "Δ @ Γ  P <: Q"
  and wf: "Γ wfB B"
  shows "e 1 0 Δ @ B  Γ  τ 1 Δ P <: τ 1 Δ Q" using H
proof (induct "Δ @ Γ" P Q arbitrary: Δ)
  case SA_Top
  with wf show ?case
    by (auto intro: subtyping.SA_Top wfE_weaken wf_weaken)
next
  case SA_refl_TVar
  with wf show ?case
    by (auto intro!: subtyping.SA_refl_TVar wfE_weaken dest: wf_weaken)
next
  case (SA_trans_TVar i U T)
  thus ?case
  proof (cases "i < Δ")
    case True
    with SA_trans_TVar
    have "(e 1 0 Δ @ B  Γ)i = TVarB (τ 1 (Δ - Suc i) U)"
      by simp
    moreover from True SA_trans_TVar
    have "e 1 0 Δ @ B  Γ 
      τ (Suc i) 0 (τ 1 (Δ - Suc i) U) <: τ 1 Δ T"
      by simp
    ultimately have "e 1 0 Δ @ B  Γ  TVar i <: τ 1 Δ T"
      by (rule subtyping.SA_trans_TVar)
    with True show ?thesis by simp
  next
    case False
    then have "Suc i - Δ = Suc (i - Δ)" by arith
    with False SA_trans_TVar have "(e 1 0 Δ @ B  Γ)Suc i = TVarB U"
      by simp
    moreover from False SA_trans_TVar
    have "e 1 0 Δ @ B  Γ  τ (Suc (Suc i)) 0 U <: τ 1 Δ T"
      by simp
    ultimately have "e 1 0 Δ @ B  Γ  TVar (Suc i) <: τ 1 Δ T"
      by (rule subtyping.SA_trans_TVar)
    with False show ?thesis by simp
  qed
next
  case SA_arrow
  thus ?case by simp (iprover intro: subtyping.SA_arrow)
next
  case (SA_all T1 S1 S2 T2)
  with SA_all(4) [of "TVarB T1  Δ"]
  show ?case by simp (iprover intro: subtyping.SA_all)
next
  case (SA_Rcd fs fs')
  with wf have "e (Suc 0) 0 Δ @ B  Γ wf" by simp (rule wfE_weaken)
  moreover from Δ @ Γ wf RcdT fs
  have "e (Suc 0) 0 Δ @ B  Γ wf τ (Suc 0) Δ (RcdT fs)"
    by (rule wf_weaken)
  hence "e (Suc 0) 0 Δ @ B  Γ wf RcdT (rτ (Suc 0) Δ fs)" by simp
  moreover from SA_Rcd have "unique (rτ (Suc 0) Δ fs')" by simp
  moreover have "(l, T)set (rτ (Suc 0) Δ fs').
    S. (l, S)set (rτ (Suc 0) Δ fs)  e (Suc 0) 0 Δ @ B  Γ  S <: T"
  proof (rule ballpI)
    fix l T
    assume "(l, T)  set (rτ (Suc 0) Δ fs')"
    then obtain T' where "(l, T')  set fs'" and T: "T = τ (Suc 0) Δ T'"
      by (blast dest: liftrT_setD)
    with SA_Rcd obtain S where
      lS: "(l, S)  set fs"
      and ST: "e (Suc 0) 0 Δ @ B  Γ  τ (Suc 0) Δ S <: τ (Suc 0) Δ (T[Δ τ Top]τ)"
      by fastforce
    with T have "e (Suc 0) 0 Δ @ B  Γ  τ (Suc 0) Δ S <: τ (Suc 0) Δ T'"
      by simp
    moreover from lS have "(l, τ (Suc 0) Δ S)  set (rτ (Suc 0) Δ fs)"
      by (rule liftrT_set)
    moreover note T
    ultimately show "S. (l, S)set (rτ (Suc 0) Δ fs)  e (Suc 0) 0 Δ @ B  Γ  S <: T"
      by auto
  qed
  ultimately have "e (Suc 0) 0 Δ @ B  Γ  RcdT (rτ (Suc 0) Δ fs) <: RcdT (rτ (Suc 0) Δ fs')"
    by (rule subtyping.SA_Rcd)
  thus ?case by simp
qed

lemma subtype_weaken': ― ‹A.2›
  "Γ  P <: Q  Δ @ Γ wf  Δ @ Γ  τ Δ 0 P <: τ Δ 0 Q"
  apply (induct Δ)
  apply simp_all
  apply (erule well_formedE_cases)
  apply simp
  apply (drule_tac B="a" and Γ="Δ @ Γ" in subtype_weaken [of "[]", simplified])
  apply simp_all
  done

lemma fieldT_size [simp]:
  "(a, T)  set fs  size T < Suc (size_list (size_prod (λx. 0) size) fs)"
  by (induct fs arbitrary: a T rule: list.induct) fastforce+

lemma subtype_trans: ― ‹A.3›
  "Γ  S <: Q  Γ  Q <: T  Γ  S <: T"
  "Δ @ TVarB Q  Γ  M <: N  Γ  P <: Q 
     Δ @ TVarB P  Γ  M <: N"
  using wf_measure_size
proof (induct Q arbitrary: Γ S T Δ P M N rule: wf_induct_rule)
  case (less Q)
  {
    fix Γ S T Q'
    assume "Γ  S <: Q'"
    then have "Γ  Q' <: T  size Q = size Q'  Γ  S <: T"
    proof (induct arbitrary: T)
      case SA_Top
      from SA_Top(3) show ?case
        by cases (auto intro: subtyping.SA_Top SA_Top)
    next
      case SA_refl_TVar show ?case by fact
    next
      case SA_trans_TVar
      thus ?case by (auto intro: subtyping.SA_trans_TVar)
    next
      case (SA_arrow Γ T1 S1 S2 T2)
      note SA_arrow' = SA_arrow
      from SA_arrow(5) show ?case
      proof cases
        case SA_Top
        with SA_arrow show ?thesis
          by (auto intro: subtyping.SA_Top wf_arrow elim: wf_subtypeE)
      next
        case (SA_arrow T1' T2')
        from SA_arrow SA_arrow' have "Γ  S1  S2 <: T1'  T2'"
          by (auto intro!: subtyping.SA_arrow intro: less(1) [of "T1"] less(1) [of "T2"])
        with SA_arrow show ?thesis by simp
      qed
    next
      case (SA_all Γ T1 S1 S2 T2)
      note SA_all' = SA_all
      from SA_all(5) show ?case
      proof cases
        case SA_Top
        with SA_all show ?thesis by (auto intro!:
          subtyping.SA_Top wf_all intro: wf_equallength elim: wf_subtypeE)
      next
        case (SA_all T1' T2')
        from SA_all SA_all' have "Γ  T1' <: S1"
          by - (rule less(1), simp_all)
        moreover from SA_all SA_all' have "TVarB T1'  Γ  S2 <: T2"
          by - (rule less(2) [of _ "[]", simplified], simp_all)
        with SA_all SA_all' have "TVarB T1'  Γ  S2 <: T2'"
          by - (rule less(1), simp_all)
        ultimately have "Γ  (∀<:S1. S2) <: (∀<:T1'. T2')"
          by (rule subtyping.SA_all)
        with SA_all show ?thesis by simp
      qed
    next
      case (SA_Rcd Γ fs1 fs2)
      note SA_Rcd' = SA_Rcd
      from SA_Rcd(5) show ?case
      proof cases
        case SA_Top
        with SA_Rcd show ?thesis by (auto intro!: subtyping.SA_Top)
      next
        case (SA_Rcd fs2')
        note Γ wf
        moreover note Γ wf RcdT fs1
        moreover note ‹unique fs2'
        moreover have "(l, T)set fs2'. S. (l, S)set fs1  Γ  S <: T"
        proof (rule ballpI)
          fix l T
          assume lT: "(l, T)  set fs2'"
          with SA_Rcd obtain U where
            fs2: "(l, U)  set fs2" and UT: "Γ  U <: T" by blast
          with SA_Rcd SA_Rcd' obtain S where
            fs1: "(l, S)  set fs1" and SU: "Γ  S <: U" by blast
          from SA_Rcd SA_Rcd' fs2 have "(U, Q)  measure size" by simp
          hence "Γ  S <: T" using SU UT by (rule less(1))
          with fs1 show "S. (l, S)set fs1  Γ  S <: T" by blast
        qed
        ultimately have "Γ  RcdT fs1 <: RcdT fs2'" by (rule subtyping.SA_Rcd)
        with SA_Rcd show ?thesis by simp
      qed
    qed
  }
  note tr = this
  {
    case 1
    thus ?case using refl by (rule tr)
  next
    case 2
    from 2(1) show "Δ @ TVarB P  Γ  M <: N"
    proof (induct "Δ @ TVarB Q  Γ" M N arbitrary: Δ)
      case SA_Top
      with 2 show ?case by (auto intro!: subtyping.SA_Top
        intro: wf_equallength wfE_replace elim!: wf_subtypeE)
    next
      case SA_refl_TVar
      with 2 show ?case by (auto intro!: subtyping.SA_refl_TVar
        intro: wf_equallength wfE_replace elim!: wf_subtypeE)
    next
      case (SA_trans_TVar i U T)
      show ?case
      proof (cases "i < Δ")
        case True
        with SA_trans_TVar show ?thesis
          by (auto intro!: subtyping.SA_trans_TVar)
      next
        case False
        note False' = False
        show ?thesis
        proof (cases "i = Δ")
          case True
          from SA_trans_TVar have "(Δ @ [TVarB P]) @ Γ wf"
            by (auto intro: wfE_replace elim!: wf_subtypeE)
          with Γ  P <: Q
          have "(Δ @ [TVarB P]) @ Γ  τ Δ @ [TVarB P] 0 P <: τ Δ @ [TVarB P] 0 Q"
            by (rule subtype_weaken')
          with SA_trans_TVar True False have "Δ @ TVarB P  Γ  τ (Suc Δ) 0 P <: T"
            by - (rule tr, simp+)
          with True and False and SA_trans_TVar show ?thesis
            by (auto intro!: subtyping.SA_trans_TVar)
        next
          case False
          with False' have "i - Δ = Suc (i - Δ - 1)" by arith
          with False False' SA_trans_TVar show ?thesis
            by - (rule subtyping.SA_trans_TVar, simp+)
        qed
      qed
    next
      case SA_arrow
      thus ?case by (auto intro!: subtyping.SA_arrow)
    next
      case (SA_all T1 S1 S2 T2)
      thus ?case by (auto intro: subtyping.SA_all
        SA_all(4) [of "TVarB T1  Δ", simplified])
    next
      case (SA_Rcd fs fs')
      from Γ  P <: Q have "Γ wf P" by (rule wf_subtypeE)
      with SA_Rcd have "Δ @ TVarB P  Γ wf"
        by - (rule wfE_replace, simp+)
      moreover from SA_Rcd have "Δ @ TVarB Q  Γ wf RcdT fs" by simp
      hence "Δ @ TVarB P  Γ wf RcdT fs" by (rule wf_equallength) simp_all
      moreover note ‹unique fs'
      moreover from SA_Rcd
      have "(l, T)set fs'. S. (l, S)set fs  Δ @ TVarB P  Γ  S <: T"
        by blast
      ultimately show ?case by (rule subtyping.SA_Rcd)
    qed
  }
qed

lemma substT_subtype: ― ‹A.10›
  assumes H: "Δ @ TVarB Q  Γ  S <: T"
  shows "Γ  P <: Q 
    Δ[0 τ P]e @ Γ  S[Δ τ P]τ <: T[Δ τ P]τ"
  using H
  apply (induct "Δ @ TVarB Q  Γ" S T arbitrary: Δ)
  apply simp_all
  apply (rule SA_Top)
  apply (rule wfE_subst)
  apply assumption
  apply (erule wf_subtypeE)
  apply assumption
  apply (rule wf_subst)
  apply assumption
  apply (erule wf_subtypeE)
  apply assumption
  apply (rule impI conjI)+
  apply (rule subtype_refl)
  apply (rule wfE_subst)
  apply assumption
  apply (erule wf_subtypeE)
  apply assumption
  apply (erule wf_subtypeE)
  apply (drule_tac T=P and Δ="Δ[0 τ P]e" in wf_weaken')
  apply simp
  apply (rule conjI impI)+
  apply (rule SA_refl_TVar)
  apply (rule wfE_subst)
  apply assumption
  apply (erule wf_subtypeE)
  apply assumption
  apply (erule wf_subtypeE)
  apply (drule wf_subst)
  apply assumption
  apply simp
  apply (rule impI)
  apply (rule SA_refl_TVar)
  apply (rule wfE_subst)
  apply assumption
  apply (erule wf_subtypeE)
  apply assumption
  apply (erule wf_subtypeE)
  apply (drule wf_subst)
  apply assumption
  apply simp
  apply (rule conjI impI)+
  apply simp
  apply (drule_tac Γ=Γ and Δ="Δ[0 τ P]e" in subtype_weaken')
  apply (erule wf_subtypeE)+
  apply assumption
  apply simp
  apply (rule subtype_trans(1))
  apply assumption+
  apply (rule conjI impI)+
  apply (rule SA_trans_TVar)
  apply (simp split: nat.split_asm)
  apply (subgoal_tac "Δ  i - Suc 0")
  apply (rename_tac nat)
  apply (subgoal_tac "i - Suc Δ = nat")
  apply (simp (no_asm_simp))
  apply arith
  apply arith
  apply simp
  apply (rule impI)
  apply (rule SA_trans_TVar)
  apply (simp split: nat.split_asm)
  apply (subgoal_tac "Suc (Δ - Suc 0) = Δ")
  apply (simp (no_asm_simp))
  apply arith
  apply (rule SA_arrow)
  apply simp+
  apply (rule SA_all)
  apply simp
  apply simp
  apply (erule wf_subtypeE)
  apply (rule SA_Rcd)
  apply (erule wfE_subst)
  apply assumption
  apply (drule wf_subst)
  apply assumption
  apply simp
  apply simp
  apply (rule ballpI)
  apply (drule substrT_setD)
  apply (erule exE conjE)+
  apply (drule bpspec)
  apply assumption
  apply simp
  apply (erule exE)
  apply (erule conjE)+
  apply (rule exI)
  apply (rule conjI)
  apply (erule substrT_set)
  apply assumption
  done

lemma subst_subtype:
  assumes H: "Δ @ VarB V  Γ  T <: U"
  shows "e 1 0 Δ @ Γ  τ 1 Δ T <: τ 1 Δ U"
  using H
  apply (induct "Δ @ VarB V  Γ" T U arbitrary: Δ)
  apply simp_all
  apply (rule SA_Top)
  apply (rule wfE_subst)
  apply assumption
  apply (rule wf_Top)
  apply (rule wf_subst)
  apply assumption
  apply (rule wf_Top)
  apply (rule impI conjI)+
  apply (rule SA_Top)
  apply (rule wfE_subst)
  apply assumption
  apply (rule wf_Top)+
  apply (rule conjI impI)+
  apply (rule SA_refl_TVar)
  apply (rule wfE_subst)
  apply assumption
  apply (rule wf_Top)
  apply (drule wf_subst)
  apply (rule wf_Top)
  apply simp
  apply (rule impI)
  apply (rule SA_refl_TVar)
  apply (rule wfE_subst)
  apply assumption
  apply (rule wf_Top)
  apply (drule wf_subst)
  apply (rule wf_Top)
  apply simp
  apply (rule conjI impI)+
  apply simp
  apply (rule conjI impI)+
  apply (simp split: nat.split_asm)
  apply (rule SA_trans_TVar)
  apply (subgoal_tac "Δ  i - Suc 0")
  apply (rename_tac nat)
  apply (subgoal_tac "i - Suc Δ = nat")
  apply (simp (no_asm_simp))
  apply arith
  apply arith
  apply simp
  apply (rule impI)
  apply (rule SA_trans_TVar)
  apply simp
  apply (subgoal_tac "0 < Δ")
  apply simp
  apply arith
  apply (rule SA_arrow)
  apply simp+
  apply (rule SA_all)
  apply simp
  apply simp
  apply (rule SA_Rcd)
  apply (erule wfE_subst)
  apply (rule wf_Top)
  apply (drule wf_subst)
  apply (rule wf_Top)
  apply simp
  apply simp
  apply (rule ballpI)
  apply (drule substrT_setD)
  apply (erule exE conjE)+
  apply (drule bpspec)
  apply assumption
  apply simp
  apply (erule exE)
  apply (erule conjE)+
  apply (rule exI)
  apply (rule conjI)
  apply (erule substrT_set)
  apply assumption
  done


subsection ‹Typing›

text ‹
In the formalization of the type checking rule for the LET› binder,
we use an additional judgement ⊢ p : T ⇒ Δ› for checking whether a
given pattern @{term p} is compatible with the type @{term T} of an object that
is to be matched against this pattern. The judgement will be defined simultaneously
with a judgement \mbox{⊢ ps [:] Ts ⇒ Δ›} for type checking field patterns.
Apart from checking the type, the judgement also returns a list of bindings @{term Δ},
which can be thought of as a ``flattened'' list of types of the variables occurring
in the pattern. Since typing environments are extended ``to the left'', the bindings
in @{term Δ} appear in reverse order.
›

inductive
  ptyping :: "pat  type  env  bool"  (" _ : _  _" [50, 50, 50] 50)
  and ptypings :: "rpat  rcdT  env  bool"  (" _ [:] _  _" [50, 50, 50] 50)
where
  P_Var: " PVar T : T  [VarB T]"
| P_Rcd: " fps [:] fTs  Δ   PRcd fps : RcdT fTs  Δ"
| P_Nil: " [] [:] []  []"
| P_Cons: " p : T  Δ1   fps [:] fTs  Δ2  fpsl? =  
     ((l, p)  fps) [:] ((l, T)  fTs)  e Δ1 0 Δ2 @ Δ1"

text ‹
The definition of the typing judgement for terms is extended with the rules T_Let›,
@{term "T_Rcd"}, and @{term "T_Proj"} for pattern matching, record construction and
field selection, respectively. The above typing judgement for patterns is used in
the rule T_Let›. The typing judgement for terms is defined simultaneously
with a typing judgement Γ ⊢ fs [:] fTs› for record fields.
›

inductive
  typing :: "env  trm  type  bool"  ("_  _ : _" [50, 50, 50] 50)
  and typings :: "env  rcd  rcdT  bool"  ("_  _ [:] _" [50, 50, 50] 50)
where
  T_Var: "Γ wf  Γi = VarB U  T = τ (Suc i) 0 U  Γ  Var i : T"
| T_Abs: "VarB T1  Γ  t2 : T2  Γ  (λ:T1. t2) : T1  τ 1 0 T2"
| T_App: "Γ  t1 : T11  T12  Γ  t2 : T11  Γ  t1  t2 : T12"
| T_TAbs: "TVarB T1  Γ  t2 : T2  Γ  (λ<:T1. t2) : (∀<:T1. T2)"
| T_TApp: "Γ  t1 : (∀<:T11. T12)  Γ  T2 <: T11 
    Γ  t1 τ T2 : T12[0 τ T2]τ"
| T_Sub: "Γ  t : S  Γ  S <: T  Γ  t : T"
| T_Let: "Γ  t1 : T1   p : T1  Δ  Δ @ Γ  t2 : T2 
    Γ  (LET p = t1 IN t2) : τ Δ 0 T2"
| T_Rcd: "Γ  fs [:] fTs  Γ  Rcd fs : RcdT fTs"
| T_Proj: "Γ  t : RcdT fTs  fTsl? = T  Γ  t..l : T"
| T_Nil: "Γ wf  Γ  [] [:] []"
| T_Cons: "Γ  t : T  Γ  fs [:] fTs  fsl? =  
    Γ  (l, t)  fs [:] (l, T)  fTs"

theorem wf_typeE1:
  "Γ  t : T  Γ wf"
  "Γ  fs [:] fTs  Γ wf"
  by (induct set: typing typings) (blast elim: well_formedE_cases)+

theorem wf_typeE2:
  "Γ  t : T  Γ wf T"
  "Γ'  fs [:] fTs  ((l, T)  set fTs. Γ' wf T) 
     unique fTs  (l. (fsl? = ) = (fTsl? = ))"
  apply (induct set: typing typings)
  apply simp
  apply (rule wf_liftB)
  apply assumption+
  apply (drule wf_typeE1)+
  apply (erule well_formedE_cases)+
  apply (rule wf_arrow)
  apply simp
  apply simp
  apply (rule wf_subst [of "[]", simplified])
  apply assumption
  apply (rule wf_Top)
  apply (erule well_formed_cases)
  apply assumption
  apply (rule wf_all)
  apply (drule wf_typeE1)
  apply (erule well_formedE_cases)
  apply simp  
  apply assumption
  apply (erule well_formed_cases)
  apply (rule wf_subst [of "[]", simplified])
  apply assumption
  apply (erule wf_subtypeE)
  apply assumption
  apply (erule wf_subtypeE)
  apply assumption
  ― ‹records›
  apply (erule wf_dec)
  apply (erule conjE)+
  apply (rule wf_RcdT)
  apply assumption+
  apply (erule well_formed_cases)
  apply (blast dest: assoc_set)
  apply simp
  apply simp
  done

lemmas ptyping_induct = ptyping_ptypings.inducts(1)
  [of _ _ _ _ "λx y z. True", simplified True_simps, consumes 1,
   case_names P_Var P_Rcd]

lemmas ptypings_induct = ptyping_ptypings.inducts(2)
  [of _ _ _ "λx y z. True", simplified True_simps, consumes 1,
   case_names P_Nil P_Cons]

lemmas typing_induct = typing_typings.inducts(1)
  [of _ _ _ _ "λx y z. True", simplified True_simps, consumes 1,
   case_names T_Var T_Abs T_App T_TAbs T_TApp T_Sub T_Let T_Rcd T_Proj]

lemmas typings_induct = typing_typings.inducts(2)
  [of _ _ _ "λx y z. True", simplified True_simps, consumes 1,
   case_names T_Nil T_Cons]

lemma narrow_type: ― ‹A.7›
  "Δ @ TVarB Q  Γ  t : T 
     Γ  P <: Q  Δ @ TVarB P  Γ  t : T"
  "Δ @ TVarB Q  Γ  ts [:] Ts 
     Γ  P <: Q  Δ @ TVarB P  Γ  ts [:] Ts"
  apply (induct "Δ @ TVarB Q  Γ" t T and "Δ @ TVarB Q  Γ" ts Ts
    arbitrary: Δ and Δ set: typing typings)
  apply simp_all
  apply (rule T_Var)
  apply (erule wfE_replace)
  apply (erule wf_subtypeE)
  apply simp+
  apply (case_tac "i < Δ")
  apply simp
  apply (case_tac "i = Δ")
  apply simp
  apply (simp split: nat.split nat.split_asm)+
  apply (rule T_Abs [simplified])
  apply simp
  apply (rule_tac T11=T11 in T_App)
  apply simp+
  apply (rule T_TAbs)
  apply simp
  apply (rule_tac T11=T11 in T_TApp)
  apply simp
  apply (rule subtype_trans(2))
  apply assumption+
  apply (rule_tac S=S in T_Sub)
  apply simp
  apply (rule subtype_trans(2))
  apply assumption+
  ― ‹records›
  apply (rule T_Let)
  apply blast
  apply assumption
  apply simp
  apply (rule T_Rcd)
  apply simp
  apply (rule T_Proj)
  apply blast
  apply assumption
  apply (rule T_Nil)
  apply (erule wfE_replace)
  apply (erule wf_subtypeE)
  apply simp+
  apply (rule T_Cons)
  apply simp+
  done

lemma typings_setD:
  assumes H: "Γ  fs [:] fTs"
  shows "(l, T)  set fTs  t. fsl? = t  Γ  t : T"
  using H
  by (induct arbitrary: l T rule: typings_induct) fastforce+

lemma subtype_refl':
  assumes t: "Γ  t : T"
  shows "Γ  T <: T"
proof (rule subtype_refl)
  from t show "Γ wf" by (rule wf_typeE1)
  from t show "Γ