src/HOL/Nominal/Examples/Fsub.thy
changeset 32011 01da62fb4a20
parent 30986 047fa04a9fe8
child 32960 69916a850301
equal deleted inserted replaced
32007:a2a3685f61c3 32011:01da62fb4a20
    89 text {* Typing contexts are represented as lists that ``grow" on the left; we
    89 text {* Typing contexts are represented as lists that ``grow" on the left; we
    90   thereby deviating from the convention in the POPLmark-paper. The lists contain
    90   thereby deviating from the convention in the POPLmark-paper. The lists contain
    91   pairs of type-variables and types (this is sufficient for Part 1A). *}
    91   pairs of type-variables and types (this is sufficient for Part 1A). *}
    92 
    92 
    93 text {* In order to state validity-conditions for typing-contexts, the notion of
    93 text {* In order to state validity-conditions for typing-contexts, the notion of
    94   a @{text "domain"} of a typing-context is handy. *}
    94   a @{text "dom"} of a typing-context is handy. *}
    95 
    95 
    96 nominal_primrec
    96 nominal_primrec
    97   "tyvrs_of" :: "binding \<Rightarrow> tyvrs set"
    97   "tyvrs_of" :: "binding \<Rightarrow> tyvrs set"
    98 where
    98 where
    99   "tyvrs_of (VarB  x y) = {}"
    99   "tyvrs_of (VarB  x y) = {}"
   106   "vrs_of (VarB  x y) = {x}"
   106   "vrs_of (VarB  x y) = {x}"
   107 | "vrs_of (TVarB x y) = {}"
   107 | "vrs_of (TVarB x y) = {}"
   108 by auto
   108 by auto
   109 
   109 
   110 consts
   110 consts
   111   "ty_domain" :: "env \<Rightarrow> tyvrs set"
   111   "ty_dom" :: "env \<Rightarrow> tyvrs set"
   112 primrec
   112 primrec
   113   "ty_domain [] = {}"
   113   "ty_dom [] = {}"
   114   "ty_domain (X#\<Gamma>) = (tyvrs_of X)\<union>(ty_domain \<Gamma>)" 
   114   "ty_dom (X#\<Gamma>) = (tyvrs_of X)\<union>(ty_dom \<Gamma>)" 
   115 
   115 
   116 consts
   116 consts
   117   "trm_domain" :: "env \<Rightarrow> vrs set"
   117   "trm_dom" :: "env \<Rightarrow> vrs set"
   118 primrec
   118 primrec
   119   "trm_domain [] = {}"
   119   "trm_dom [] = {}"
   120   "trm_domain (X#\<Gamma>) = (vrs_of X)\<union>(trm_domain \<Gamma>)" 
   120   "trm_dom (X#\<Gamma>) = (vrs_of X)\<union>(trm_dom \<Gamma>)" 
   121 
   121 
   122 lemma vrs_of_eqvt[eqvt]:
   122 lemma vrs_of_eqvt[eqvt]:
   123   fixes pi ::"tyvrs prm"
   123   fixes pi ::"tyvrs prm"
   124   and   pi'::"vrs   prm"
   124   and   pi'::"vrs   prm"
   125   shows "pi \<bullet>(tyvrs_of x) = tyvrs_of (pi\<bullet>x)"
   125   shows "pi \<bullet>(tyvrs_of x) = tyvrs_of (pi\<bullet>x)"
   126   and   "pi'\<bullet>(tyvrs_of x) = tyvrs_of (pi'\<bullet>x)"
   126   and   "pi'\<bullet>(tyvrs_of x) = tyvrs_of (pi'\<bullet>x)"
   127   and   "pi \<bullet>(vrs_of x)   = vrs_of   (pi\<bullet>x)"
   127   and   "pi \<bullet>(vrs_of x)   = vrs_of   (pi\<bullet>x)"
   128   and   "pi'\<bullet>(vrs_of x)   = vrs_of   (pi'\<bullet>x)"
   128   and   "pi'\<bullet>(vrs_of x)   = vrs_of   (pi'\<bullet>x)"
   129 by (nominal_induct x rule: binding.strong_induct) (simp_all add: tyvrs_of.simps eqvts)
   129 by (nominal_induct x rule: binding.strong_induct) (simp_all add: tyvrs_of.simps eqvts)
   130 
   130 
   131 lemma domains_eqvt[eqvt]:
   131 lemma doms_eqvt[eqvt]:
   132   fixes pi::"tyvrs prm"
   132   fixes pi::"tyvrs prm"
   133   and   pi'::"vrs prm"
   133   and   pi'::"vrs prm"
   134   shows "pi \<bullet>(ty_domain \<Gamma>)  = ty_domain  (pi\<bullet>\<Gamma>)"
   134   shows "pi \<bullet>(ty_dom \<Gamma>)  = ty_dom  (pi\<bullet>\<Gamma>)"
   135   and   "pi'\<bullet>(ty_domain \<Gamma>)  = ty_domain  (pi'\<bullet>\<Gamma>)"
   135   and   "pi'\<bullet>(ty_dom \<Gamma>)  = ty_dom  (pi'\<bullet>\<Gamma>)"
   136   and   "pi \<bullet>(trm_domain \<Gamma>) = trm_domain (pi\<bullet>\<Gamma>)"
   136   and   "pi \<bullet>(trm_dom \<Gamma>) = trm_dom (pi\<bullet>\<Gamma>)"
   137   and   "pi'\<bullet>(trm_domain \<Gamma>) = trm_domain (pi'\<bullet>\<Gamma>)"
   137   and   "pi'\<bullet>(trm_dom \<Gamma>) = trm_dom (pi'\<bullet>\<Gamma>)"
   138 by (induct \<Gamma>) (simp_all add: eqvts)
   138 by (induct \<Gamma>) (simp_all add: eqvts)
   139 
   139 
   140 lemma finite_vrs:
   140 lemma finite_vrs:
   141   shows "finite (tyvrs_of x)"
   141   shows "finite (tyvrs_of x)"
   142   and   "finite (vrs_of x)"
   142   and   "finite (vrs_of x)"
   143 by (nominal_induct rule:binding.strong_induct, auto)
   143 by (nominal_induct rule:binding.strong_induct, auto)
   144  
   144  
   145 lemma finite_domains:
   145 lemma finite_doms:
   146   shows "finite (ty_domain \<Gamma>)"
   146   shows "finite (ty_dom \<Gamma>)"
   147   and   "finite (trm_domain \<Gamma>)"
   147   and   "finite (trm_dom \<Gamma>)"
   148 by (induct \<Gamma>, auto simp add: finite_vrs)
   148 by (induct \<Gamma>, auto simp add: finite_vrs)
   149 
   149 
   150 lemma ty_domain_supp:
   150 lemma ty_dom_supp:
   151   shows "(supp (ty_domain  \<Gamma>)) = (ty_domain  \<Gamma>)"
   151   shows "(supp (ty_dom  \<Gamma>)) = (ty_dom  \<Gamma>)"
   152   and   "(supp (trm_domain \<Gamma>)) = (trm_domain \<Gamma>)"
   152   and   "(supp (trm_dom \<Gamma>)) = (trm_dom \<Gamma>)"
   153 by (simp only: at_fin_set_supp at_tyvrs_inst at_vrs_inst finite_domains)+
   153 by (simp only: at_fin_set_supp at_tyvrs_inst at_vrs_inst finite_doms)+
   154 
   154 
   155 lemma ty_domain_inclusion:
   155 lemma ty_dom_inclusion:
   156   assumes a: "(TVarB X T)\<in>set \<Gamma>" 
   156   assumes a: "(TVarB X T)\<in>set \<Gamma>" 
   157   shows "X\<in>(ty_domain \<Gamma>)"
   157   shows "X\<in>(ty_dom \<Gamma>)"
   158 using a by (induct \<Gamma>, auto)
   158 using a by (induct \<Gamma>, auto)
   159 
   159 
   160 lemma ty_binding_existence:
   160 lemma ty_binding_existence:
   161   assumes "X \<in> (tyvrs_of a)"
   161   assumes "X \<in> (tyvrs_of a)"
   162   shows "\<exists>T.(TVarB X T=a)"
   162   shows "\<exists>T.(TVarB X T=a)"
   163   using assms
   163   using assms
   164 by (nominal_induct a rule: binding.strong_induct, auto)
   164 by (nominal_induct a rule: binding.strong_induct, auto)
   165 
   165 
   166 lemma ty_domain_existence:
   166 lemma ty_dom_existence:
   167   assumes a: "X\<in>(ty_domain \<Gamma>)" 
   167   assumes a: "X\<in>(ty_dom \<Gamma>)" 
   168   shows "\<exists>T.(TVarB X T)\<in>set \<Gamma>"
   168   shows "\<exists>T.(TVarB X T)\<in>set \<Gamma>"
   169   using a 
   169   using a 
   170   apply (induct \<Gamma>, auto) 
   170   apply (induct \<Gamma>, auto) 
   171   apply (subgoal_tac "\<exists>T.(TVarB X T=a)")
   171   apply (subgoal_tac "\<exists>T.(TVarB X T=a)")
   172   apply (auto)
   172   apply (auto)
   173   apply (auto simp add: ty_binding_existence)
   173   apply (auto simp add: ty_binding_existence)
   174 done
   174 done
   175 
   175 
   176 lemma domains_append:
   176 lemma doms_append:
   177   shows "ty_domain (\<Gamma>@\<Delta>) = ((ty_domain \<Gamma>) \<union> (ty_domain \<Delta>))"
   177   shows "ty_dom (\<Gamma>@\<Delta>) = ((ty_dom \<Gamma>) \<union> (ty_dom \<Delta>))"
   178   and   "trm_domain (\<Gamma>@\<Delta>) = ((trm_domain \<Gamma>) \<union> (trm_domain \<Delta>))"
   178   and   "trm_dom (\<Gamma>@\<Delta>) = ((trm_dom \<Gamma>) \<union> (trm_dom \<Delta>))"
   179   by (induct \<Gamma>, auto)
   179   by (induct \<Gamma>, auto)
   180 
   180 
   181 lemma ty_vrs_prm_simp:
   181 lemma ty_vrs_prm_simp:
   182   fixes pi::"vrs prm"
   182   fixes pi::"vrs prm"
   183   and   S::"ty"
   183   and   S::"ty"
   184   shows "pi\<bullet>S = S"
   184   shows "pi\<bullet>S = S"
   185 by (induct S rule: ty.induct) (auto simp add: calc_atm)
   185 by (induct S rule: ty.induct) (auto simp add: calc_atm)
   186 
   186 
   187 lemma fresh_ty_domain_cons:
   187 lemma fresh_ty_dom_cons:
   188   fixes X::"tyvrs"
   188   fixes X::"tyvrs"
   189   shows "X\<sharp>(ty_domain (Y#\<Gamma>)) = (X\<sharp>(tyvrs_of Y) \<and> X\<sharp>(ty_domain \<Gamma>))"
   189   shows "X\<sharp>(ty_dom (Y#\<Gamma>)) = (X\<sharp>(tyvrs_of Y) \<and> X\<sharp>(ty_dom \<Gamma>))"
   190   apply (nominal_induct rule:binding.strong_induct)
   190   apply (nominal_induct rule:binding.strong_induct)
   191   apply (auto)
   191   apply (auto)
   192   apply (simp add: fresh_def supp_def eqvts)
   192   apply (simp add: fresh_def supp_def eqvts)
   193   apply (simp add: fresh_fin_insert [OF pt_tyvrs_inst at_tyvrs_inst fs_tyvrs_inst] finite_domains)
   193   apply (simp add: fresh_fin_insert [OF pt_tyvrs_inst at_tyvrs_inst fs_tyvrs_inst] finite_doms)
   194   apply (simp add: fresh_def supp_def eqvts)
   194   apply (simp add: fresh_def supp_def eqvts)
   195   apply (simp add: fresh_fin_insert [OF pt_tyvrs_inst at_tyvrs_inst fs_tyvrs_inst] finite_domains)+
   195   apply (simp add: fresh_fin_insert [OF pt_tyvrs_inst at_tyvrs_inst fs_tyvrs_inst] finite_doms)+
   196   done
   196   done
   197 
   197 
   198 lemma tyvrs_fresh:
   198 lemma tyvrs_fresh:
   199   fixes   X::"tyvrs"
   199   fixes   X::"tyvrs"
   200   assumes "X \<sharp> a" 
   200   assumes "X \<sharp> a" 
   204   apply (nominal_induct a rule:binding.strong_induct)
   204   apply (nominal_induct a rule:binding.strong_induct)
   205   apply (auto)
   205   apply (auto)
   206   apply (fresh_guess)+
   206   apply (fresh_guess)+
   207 done
   207 done
   208 
   208 
   209 lemma fresh_domain:
   209 lemma fresh_dom:
   210   fixes X::"tyvrs"
   210   fixes X::"tyvrs"
   211   assumes a: "X\<sharp>\<Gamma>" 
   211   assumes a: "X\<sharp>\<Gamma>" 
   212   shows "X\<sharp>(ty_domain \<Gamma>)"
   212   shows "X\<sharp>(ty_dom \<Gamma>)"
   213 using a
   213 using a
   214 apply(induct \<Gamma>)
   214 apply(induct \<Gamma>)
   215 apply(simp add: fresh_set_empty) 
   215 apply(simp add: fresh_set_empty) 
   216 apply(simp only: fresh_ty_domain_cons)
   216 apply(simp only: fresh_ty_dom_cons)
   217 apply(auto simp add: fresh_prod fresh_list_cons tyvrs_fresh) 
   217 apply(auto simp add: fresh_prod fresh_list_cons tyvrs_fresh) 
   218 done
   218 done
   219 
   219 
   220 text {* Not all lists of type @{typ "env"} are well-formed. One condition
   220 text {* Not all lists of type @{typ "env"} are well-formed. One condition
   221   requires that in @{term "TVarB X S#\<Gamma>"} all free variables of @{term "S"} must be 
   221   requires that in @{term "TVarB X S#\<Gamma>"} all free variables of @{term "S"} must be 
   222   in the @{term "ty_domain"} of @{term "\<Gamma>"}, that is @{term "S"} must be @{text "closed"} 
   222   in the @{term "ty_dom"} of @{term "\<Gamma>"}, that is @{term "S"} must be @{text "closed"} 
   223   in @{term "\<Gamma>"}. The set of free variables of @{term "S"} is the 
   223   in @{term "\<Gamma>"}. The set of free variables of @{term "S"} is the 
   224   @{text "support"} of @{term "S"}. *}
   224   @{text "support"} of @{term "S"}. *}
   225 
   225 
   226 constdefs
   226 constdefs
   227   "closed_in" :: "ty \<Rightarrow> env \<Rightarrow> bool" ("_ closed'_in _" [100,100] 100)
   227   "closed_in" :: "ty \<Rightarrow> env \<Rightarrow> bool" ("_ closed'_in _" [100,100] 100)
   228   "S closed_in \<Gamma> \<equiv> (supp S)\<subseteq>(ty_domain \<Gamma>)"
   228   "S closed_in \<Gamma> \<equiv> (supp S)\<subseteq>(ty_dom \<Gamma>)"
   229 
   229 
   230 lemma closed_in_eqvt[eqvt]:
   230 lemma closed_in_eqvt[eqvt]:
   231   fixes pi::"tyvrs prm"
   231   fixes pi::"tyvrs prm"
   232   assumes a: "S closed_in \<Gamma>" 
   232   assumes a: "S closed_in \<Gamma>" 
   233   shows "(pi\<bullet>S) closed_in (pi\<bullet>\<Gamma>)"
   233   shows "(pi\<bullet>S) closed_in (pi\<bullet>\<Gamma>)"
   249   fixes x::"vrs"
   249   fixes x::"vrs"
   250   and   T::"ty"
   250   and   T::"ty"
   251   shows "x \<sharp> T"
   251   shows "x \<sharp> T"
   252 by (simp add: fresh_def supp_def ty_vrs_prm_simp)
   252 by (simp add: fresh_def supp_def ty_vrs_prm_simp)
   253 
   253 
   254 lemma ty_domain_vrs_prm_simp:
   254 lemma ty_dom_vrs_prm_simp:
   255   fixes pi::"vrs prm"
   255   fixes pi::"vrs prm"
   256   and   \<Gamma>::"env"
   256   and   \<Gamma>::"env"
   257   shows "(ty_domain (pi\<bullet>\<Gamma>)) = (ty_domain \<Gamma>)"
   257   shows "(ty_dom (pi\<bullet>\<Gamma>)) = (ty_dom \<Gamma>)"
   258   apply(induct \<Gamma>) 
   258   apply(induct \<Gamma>) 
   259   apply (simp add: eqvts)
   259   apply (simp add: eqvts)
   260   apply(simp add:  tyvrs_vrs_prm_simp)
   260   apply(simp add:  tyvrs_vrs_prm_simp)
   261 done
   261 done
   262 
   262 
   263 lemma closed_in_eqvt'[eqvt]:
   263 lemma closed_in_eqvt'[eqvt]:
   264   fixes pi::"vrs prm"
   264   fixes pi::"vrs prm"
   265   assumes a: "S closed_in \<Gamma>" 
   265   assumes a: "S closed_in \<Gamma>" 
   266   shows "(pi\<bullet>S) closed_in (pi\<bullet>\<Gamma>)"
   266   shows "(pi\<bullet>S) closed_in (pi\<bullet>\<Gamma>)"
   267 using a
   267 using a
   268 by (simp add: closed_in_def ty_domain_vrs_prm_simp  ty_vrs_prm_simp)
   268 by (simp add: closed_in_def ty_dom_vrs_prm_simp  ty_vrs_prm_simp)
   269 
   269 
   270 lemma fresh_vrs_of: 
   270 lemma fresh_vrs_of: 
   271   fixes x::"vrs"
   271   fixes x::"vrs"
   272   shows "x\<sharp>vrs_of b = x\<sharp>b"
   272   shows "x\<sharp>vrs_of b = x\<sharp>b"
   273   by (nominal_induct b rule: binding.strong_induct)
   273   by (nominal_induct b rule: binding.strong_induct)
   274     (simp_all add: fresh_singleton [OF pt_vrs_inst at_vrs_inst] fresh_set_empty ty_vrs_fresh fresh_atm)
   274     (simp_all add: fresh_singleton [OF pt_vrs_inst at_vrs_inst] fresh_set_empty ty_vrs_fresh fresh_atm)
   275 
   275 
   276 lemma fresh_trm_domain: 
   276 lemma fresh_trm_dom: 
   277   fixes x::"vrs"
   277   fixes x::"vrs"
   278   shows "x\<sharp> trm_domain \<Gamma> = x\<sharp>\<Gamma>"
   278   shows "x\<sharp> trm_dom \<Gamma> = x\<sharp>\<Gamma>"
   279   by (induct \<Gamma>)
   279   by (induct \<Gamma>)
   280     (simp_all add: fresh_set_empty fresh_list_cons
   280     (simp_all add: fresh_set_empty fresh_list_cons
   281      fresh_fin_union [OF pt_vrs_inst at_vrs_inst fs_vrs_inst]
   281      fresh_fin_union [OF pt_vrs_inst at_vrs_inst fs_vrs_inst]
   282      finite_domains finite_vrs fresh_vrs_of fresh_list_nil)
   282      finite_doms finite_vrs fresh_vrs_of fresh_list_nil)
   283 
   283 
   284 lemma closed_in_fresh: "(X::tyvrs) \<sharp> ty_domain \<Gamma> \<Longrightarrow> T closed_in \<Gamma> \<Longrightarrow> X \<sharp> T"
   284 lemma closed_in_fresh: "(X::tyvrs) \<sharp> ty_dom \<Gamma> \<Longrightarrow> T closed_in \<Gamma> \<Longrightarrow> X \<sharp> T"
   285   by (auto simp add: closed_in_def fresh_def ty_domain_supp)
   285   by (auto simp add: closed_in_def fresh_def ty_dom_supp)
   286 
   286 
   287 text {* Now validity of a context is a straightforward inductive definition. *}
   287 text {* Now validity of a context is a straightforward inductive definition. *}
   288   
   288   
   289 inductive
   289 inductive
   290   valid_rel :: "env \<Rightarrow> bool" ("\<turnstile> _ ok" [100] 100)
   290   valid_rel :: "env \<Rightarrow> bool" ("\<turnstile> _ ok" [100] 100)
   291 where
   291 where
   292   valid_nil[simp]:   "\<turnstile> [] ok"
   292   valid_nil[simp]:   "\<turnstile> [] ok"
   293 | valid_consT[simp]: "\<lbrakk>\<turnstile> \<Gamma> ok; X\<sharp>(ty_domain  \<Gamma>); T closed_in \<Gamma>\<rbrakk>  \<Longrightarrow>  \<turnstile> (TVarB X T#\<Gamma>) ok"
   293 | valid_consT[simp]: "\<lbrakk>\<turnstile> \<Gamma> ok; X\<sharp>(ty_dom  \<Gamma>); T closed_in \<Gamma>\<rbrakk>  \<Longrightarrow>  \<turnstile> (TVarB X T#\<Gamma>) ok"
   294 | valid_cons [simp]: "\<lbrakk>\<turnstile> \<Gamma> ok; x\<sharp>(trm_domain \<Gamma>); T closed_in \<Gamma>\<rbrakk>  \<Longrightarrow>  \<turnstile> (VarB  x T#\<Gamma>) ok"
   294 | valid_cons [simp]: "\<lbrakk>\<turnstile> \<Gamma> ok; x\<sharp>(trm_dom \<Gamma>); T closed_in \<Gamma>\<rbrakk>  \<Longrightarrow>  \<turnstile> (VarB  x T#\<Gamma>) ok"
   295 
   295 
   296 equivariance valid_rel
   296 equivariance valid_rel
   297 
   297 
   298 declare binding.inject [simp add]
   298 declare binding.inject [simp add]
   299 declare trm.inject     [simp add]
   299 declare trm.inject     [simp add]
   300 
   300 
   301 inductive_cases validE[elim]: "\<turnstile> (TVarB X T#\<Gamma>) ok" "\<turnstile> (VarB  x T#\<Gamma>) ok" "\<turnstile> (b#\<Gamma>) ok" 
   301 inductive_cases validE[elim]: 
       
   302   "\<turnstile> (TVarB X T#\<Gamma>) ok" 
       
   303   "\<turnstile> (VarB  x T#\<Gamma>) ok" 
       
   304   "\<turnstile> (b#\<Gamma>) ok" 
   302 
   305 
   303 declare binding.inject [simp del]
   306 declare binding.inject [simp del]
   304 declare trm.inject     [simp del]
   307 declare trm.inject     [simp del]
   305 
   308 
   306 lemma validE_append:
   309 lemma validE_append:
   319   and     b: "S closed_in \<Gamma>"
   322   and     b: "S closed_in \<Gamma>"
   320   shows "\<turnstile> (\<Delta>@(TVarB X S)#\<Gamma>) ok"
   323   shows "\<turnstile> (\<Delta>@(TVarB X S)#\<Gamma>) ok"
   321 using a b
   324 using a b
   322 proof(induct \<Delta>)
   325 proof(induct \<Delta>)
   323   case Nil
   326   case Nil
   324   then show ?case by (auto elim: validE intro: valid_cons simp add: domains_append closed_in_def)
   327   then show ?case by (auto elim: validE intro: valid_cons simp add: doms_append closed_in_def)
   325 next
   328 next
   326   case (Cons a \<Gamma>')
   329   case (Cons a \<Gamma>')
   327   then show ?case 
   330   then show ?case 
   328     by (nominal_induct a rule:binding.strong_induct)
   331     by (nominal_induct a rule:binding.strong_induct)
   329        (auto elim: validE intro!: valid_cons simp add: domains_append closed_in_def)
   332        (auto elim: validE intro!: valid_cons simp add: doms_append closed_in_def)
   330 qed
   333 qed
   331 
   334 
   332 text {* Well-formed contexts have a unique type-binding for a type-variable. *} 
   335 text {* Well-formed contexts have a unique type-binding for a type-variable. *} 
   333 
   336 
   334 lemma uniqueness_of_ctxt:
   337 lemma uniqueness_of_ctxt:
   340 using a b c
   343 using a b c
   341 proof (induct)
   344 proof (induct)
   342   case (valid_consT \<Gamma> X' T')
   345   case (valid_consT \<Gamma> X' T')
   343   moreover
   346   moreover
   344   { fix \<Gamma>'::"env"
   347   { fix \<Gamma>'::"env"
   345     assume a: "X'\<sharp>(ty_domain \<Gamma>')" 
   348     assume a: "X'\<sharp>(ty_dom \<Gamma>')" 
   346     have "\<not>(\<exists>T.(TVarB X' T)\<in>(set \<Gamma>'))" using a 
   349     have "\<not>(\<exists>T.(TVarB X' T)\<in>(set \<Gamma>'))" using a 
   347     proof (induct \<Gamma>')
   350     proof (induct \<Gamma>')
   348       case (Cons Y \<Gamma>')
   351       case (Cons Y \<Gamma>')
   349       thus "\<not> (\<exists>T.(TVarB X' T)\<in>set(Y#\<Gamma>'))"
   352       thus "\<not> (\<exists>T.(TVarB X' T)\<in>set(Y#\<Gamma>'))"
   350 	by (simp add:  fresh_ty_domain_cons 
   353 	by (simp add:  fresh_ty_dom_cons 
   351                        fresh_fin_union[OF pt_tyvrs_inst  at_tyvrs_inst fs_tyvrs_inst]  
   354                        fresh_fin_union[OF pt_tyvrs_inst  at_tyvrs_inst fs_tyvrs_inst]  
   352                        finite_vrs finite_domains, 
   355                        finite_vrs finite_doms, 
   353             auto simp add: fresh_atm fresh_singleton [OF pt_tyvrs_inst at_tyvrs_inst])
   356             auto simp add: fresh_atm fresh_singleton [OF pt_tyvrs_inst at_tyvrs_inst])
   354     qed (simp)
   357     qed (simp)
   355   }
   358   }
   356   ultimately show "T=S" by (auto simp add: binding.inject)
   359   ultimately show "T=S" by (auto simp add: binding.inject)
   357 qed (auto)
   360 qed (auto)
   365 using a b c
   368 using a b c
   366 proof (induct)
   369 proof (induct)
   367   case (valid_cons \<Gamma> x' T')
   370   case (valid_cons \<Gamma> x' T')
   368   moreover
   371   moreover
   369   { fix \<Gamma>'::"env"
   372   { fix \<Gamma>'::"env"
   370     assume a: "x'\<sharp>(trm_domain \<Gamma>')" 
   373     assume a: "x'\<sharp>(trm_dom \<Gamma>')" 
   371     have "\<not>(\<exists>T.(VarB x' T)\<in>(set \<Gamma>'))" using a 
   374     have "\<not>(\<exists>T.(VarB x' T)\<in>(set \<Gamma>'))" using a 
   372     proof (induct \<Gamma>')
   375     proof (induct \<Gamma>')
   373       case (Cons y \<Gamma>')
   376       case (Cons y \<Gamma>')
   374       thus "\<not> (\<exists>T.(VarB x' T)\<in>set(y#\<Gamma>'))" 
   377       thus "\<not> (\<exists>T.(VarB x' T)\<in>set(y#\<Gamma>'))" 
   375 	by (simp add:  fresh_fin_union[OF pt_vrs_inst  at_vrs_inst fs_vrs_inst]  
   378 	by (simp add:  fresh_fin_union[OF pt_vrs_inst  at_vrs_inst fs_vrs_inst]  
   376                        finite_vrs finite_domains, 
   379                        finite_vrs finite_doms, 
   377             auto simp add: fresh_atm fresh_singleton [OF pt_vrs_inst at_vrs_inst])
   380             auto simp add: fresh_atm fresh_singleton [OF pt_vrs_inst at_vrs_inst])
   378     qed (simp)
   381     qed (simp)
   379   }
   382   }
   380   ultimately show "T=S" by (auto simp add: binding.inject)
   383   ultimately show "T=S" by (auto simp add: binding.inject)
   381 qed (auto)
   384 qed (auto)
   399   subst_ty :: "ty \<Rightarrow> tyvrs \<Rightarrow> ty \<Rightarrow> ty" ("_[_ \<mapsto> _]\<^sub>\<tau>" [300, 0, 0] 300)
   402   subst_ty :: "ty \<Rightarrow> tyvrs \<Rightarrow> ty \<Rightarrow> ty" ("_[_ \<mapsto> _]\<^sub>\<tau>" [300, 0, 0] 300)
   400 where
   403 where
   401   "(Tvar X)[Y \<mapsto> T]\<^sub>\<tau> = (if X=Y then T else Tvar X)"
   404   "(Tvar X)[Y \<mapsto> T]\<^sub>\<tau> = (if X=Y then T else Tvar X)"
   402 | "(Top)[Y \<mapsto> T]\<^sub>\<tau> = Top"
   405 | "(Top)[Y \<mapsto> T]\<^sub>\<tau> = Top"
   403 | "(T\<^isub>1 \<rightarrow> T\<^isub>2)[Y \<mapsto> T]\<^sub>\<tau> = T\<^isub>1[Y \<mapsto> T]\<^sub>\<tau> \<rightarrow> T\<^isub>2[Y \<mapsto> T]\<^sub>\<tau>"
   406 | "(T\<^isub>1 \<rightarrow> T\<^isub>2)[Y \<mapsto> T]\<^sub>\<tau> = T\<^isub>1[Y \<mapsto> T]\<^sub>\<tau> \<rightarrow> T\<^isub>2[Y \<mapsto> T]\<^sub>\<tau>"
   404 | "\<lbrakk>X\<sharp>(Y,T); X\<sharp>T\<^isub>1\<rbrakk> \<Longrightarrow> (\<forall>X<:T\<^isub>1. T\<^isub>2)[Y \<mapsto> T]\<^sub>\<tau> = (\<forall>X<:T\<^isub>1[Y \<mapsto> T]\<^sub>\<tau>. T\<^isub>2[Y \<mapsto> T]\<^sub>\<tau>)"
   407 | "X\<sharp>(Y,T,T\<^isub>1) \<Longrightarrow> (\<forall>X<:T\<^isub>1. T\<^isub>2)[Y \<mapsto> T]\<^sub>\<tau> = (\<forall>X<:T\<^isub>1[Y \<mapsto> T]\<^sub>\<tau>. T\<^isub>2[Y \<mapsto> T]\<^sub>\<tau>)"
   405   apply (finite_guess)+
   408   apply (finite_guess)+
   406   apply (rule TrueI)+
   409   apply (rule TrueI)+
   407   apply (simp add: abs_fresh)
   410   apply (simp add: abs_fresh)
   408   apply (fresh_guess)+
   411   apply (fresh_guess)+
   409   done
   412   done
   422   by (nominal_induct T avoiding: X T' rule: ty.strong_induct)
   425   by (nominal_induct T avoiding: X T' rule: ty.strong_induct)
   423      (perm_simp add: fresh_left)+
   426      (perm_simp add: fresh_left)+
   424 
   427 
   425 lemma type_subst_fresh:
   428 lemma type_subst_fresh:
   426   fixes X::"tyvrs"
   429   fixes X::"tyvrs"
   427   assumes "X \<sharp> T" and "X \<sharp> P"
   430   assumes "X\<sharp>T" and "X\<sharp>P"
   428   shows   "X \<sharp> T[Y \<mapsto> P]\<^sub>\<tau>"
   431   shows   "X\<sharp>T[Y \<mapsto> P]\<^sub>\<tau>"
   429 using assms
   432 using assms
   430 by (nominal_induct T avoiding: X Y P rule:ty.strong_induct)
   433 by (nominal_induct T avoiding: X Y P rule:ty.strong_induct)
   431    (auto simp add: abs_fresh)
   434    (auto simp add: abs_fresh)
   432 
   435 
   433 lemma fresh_type_subst_fresh:
   436 lemma fresh_type_subst_fresh:
   435     shows "X\<sharp>T[X \<mapsto> T']\<^sub>\<tau>"
   438     shows "X\<sharp>T[X \<mapsto> T']\<^sub>\<tau>"
   436 using assms 
   439 using assms 
   437 by (nominal_induct T avoiding: X T' rule: ty.strong_induct)
   440 by (nominal_induct T avoiding: X T' rule: ty.strong_induct)
   438    (auto simp add: fresh_atm abs_fresh fresh_nat) 
   441    (auto simp add: fresh_atm abs_fresh fresh_nat) 
   439 
   442 
   440 lemma type_subst_identity: "X \<sharp> T \<Longrightarrow> T[X \<mapsto> U]\<^sub>\<tau> = T"
   443 lemma type_subst_identity: 
       
   444   "X\<sharp>T \<Longrightarrow> T[X \<mapsto> U]\<^sub>\<tau> = T"
   441   by (nominal_induct T avoiding: X U rule: ty.strong_induct)
   445   by (nominal_induct T avoiding: X U rule: ty.strong_induct)
   442     (simp_all add: fresh_atm abs_fresh)
   446     (simp_all add: fresh_atm abs_fresh)
   443 
   447 
   444 lemma type_substitution_lemma:  
   448 lemma type_substitution_lemma:  
   445   "X \<noteq> Y \<Longrightarrow> X \<sharp> L \<Longrightarrow> M[X \<mapsto> N]\<^sub>\<tau>[Y \<mapsto> L]\<^sub>\<tau> = M[Y \<mapsto> L]\<^sub>\<tau>[X \<mapsto> N[Y \<mapsto> L]\<^sub>\<tau>]\<^sub>\<tau>"
   449   "X \<noteq> Y \<Longrightarrow> X\<sharp>L \<Longrightarrow> M[X \<mapsto> N]\<^sub>\<tau>[Y \<mapsto> L]\<^sub>\<tau> = M[Y \<mapsto> L]\<^sub>\<tau>[X \<mapsto> N[Y \<mapsto> L]\<^sub>\<tau>]\<^sub>\<tau>"
   446   by (nominal_induct M avoiding: X Y N L rule: ty.strong_induct)
   450   by (nominal_induct M avoiding: X Y N L rule: ty.strong_induct)
   447     (auto simp add: type_subst_fresh type_subst_identity)
   451     (auto simp add: type_subst_fresh type_subst_identity)
   448 
   452 
   449 lemma type_subst_rename:
   453 lemma type_subst_rename:
   450   "Y \<sharp> T \<Longrightarrow> ([(Y, X)] \<bullet> T)[Y \<mapsto> U]\<^sub>\<tau> = T[X \<mapsto> U]\<^sub>\<tau>"
   454   "Y\<sharp>T \<Longrightarrow> ([(Y,X)]\<bullet>T)[Y \<mapsto> U]\<^sub>\<tau> = T[X \<mapsto> U]\<^sub>\<tau>"
   451   by (nominal_induct T avoiding: X Y U rule: ty.strong_induct)
   455   by (nominal_induct T avoiding: X Y U rule: ty.strong_induct)
   452     (simp_all add: fresh_atm calc_atm abs_fresh fresh_aux)
   456     (simp_all add: fresh_atm calc_atm abs_fresh fresh_aux)
   453 
   457 
   454 nominal_primrec
   458 nominal_primrec
   455   subst_tyb :: "binding \<Rightarrow> tyvrs \<Rightarrow> ty \<Rightarrow> binding" ("_[_ \<mapsto> _]\<^sub>b" [100,100,100] 100)
   459   subst_tyb :: "binding \<Rightarrow> tyvrs \<Rightarrow> ty \<Rightarrow> binding" ("_[_ \<mapsto> _]\<^sub>b" [100,100,100] 100)
   458 | "(VarB  X U)[Y \<mapsto> T]\<^sub>b =  VarB X (U[Y \<mapsto> T]\<^sub>\<tau>)"
   462 | "(VarB  X U)[Y \<mapsto> T]\<^sub>b =  VarB X (U[Y \<mapsto> T]\<^sub>\<tau>)"
   459 by auto
   463 by auto
   460 
   464 
   461 lemma binding_subst_fresh:
   465 lemma binding_subst_fresh:
   462   fixes X::"tyvrs"
   466   fixes X::"tyvrs"
   463   assumes "X \<sharp> a"
   467   assumes "X\<sharp>a"
   464   and     "X \<sharp> P"
   468   and     "X\<sharp>P"
   465   shows "X \<sharp> a[Y \<mapsto> P]\<^sub>b"
   469   shows "X\<sharp>a[Y \<mapsto> P]\<^sub>b"
   466 using assms
   470 using assms
   467 by (nominal_induct a rule: binding.strong_induct)
   471 by (nominal_induct a rule: binding.strong_induct)
   468    (auto simp add: type_subst_fresh)
   472    (auto simp add: type_subst_fresh)
   469 
   473 
   470 lemma binding_subst_identity: 
   474 lemma binding_subst_identity: 
   471   shows "X \<sharp> B \<Longrightarrow> B[X \<mapsto> U]\<^sub>b = B"
   475   shows "X\<sharp>B \<Longrightarrow> B[X \<mapsto> U]\<^sub>b = B"
   472 by (induct B rule: binding.induct)
   476 by (induct B rule: binding.induct)
   473    (simp_all add: fresh_atm type_subst_identity)
   477    (simp_all add: fresh_atm type_subst_identity)
   474 
   478 
   475 consts 
   479 consts 
   476   subst_tyc :: "env \<Rightarrow> tyvrs \<Rightarrow> ty \<Rightarrow> env" ("_[_ \<mapsto> _]\<^sub>e" [100,100,100] 100)
   480   subst_tyc :: "env \<Rightarrow> tyvrs \<Rightarrow> ty \<Rightarrow> env" ("_[_ \<mapsto> _]\<^sub>e" [100,100,100] 100)
   479 "([])[Y \<mapsto> T]\<^sub>e= []"
   483 "([])[Y \<mapsto> T]\<^sub>e= []"
   480 "(B#\<Gamma>)[Y \<mapsto> T]\<^sub>e = (B[Y \<mapsto> T]\<^sub>b)#(\<Gamma>[Y \<mapsto> T]\<^sub>e)"
   484 "(B#\<Gamma>)[Y \<mapsto> T]\<^sub>e = (B[Y \<mapsto> T]\<^sub>b)#(\<Gamma>[Y \<mapsto> T]\<^sub>e)"
   481 
   485 
   482 lemma ctxt_subst_fresh':
   486 lemma ctxt_subst_fresh':
   483   fixes X::"tyvrs"
   487   fixes X::"tyvrs"
   484   assumes "X \<sharp> \<Gamma>"
   488   assumes "X\<sharp>\<Gamma>"
   485   and     "X \<sharp> P"
   489   and     "X\<sharp>P"
   486   shows   "X \<sharp> \<Gamma>[Y \<mapsto> P]\<^sub>e"
   490   shows   "X\<sharp>\<Gamma>[Y \<mapsto> P]\<^sub>e"
   487 using assms
   491 using assms
   488 by (induct \<Gamma>)
   492 by (induct \<Gamma>)
   489    (auto simp add: fresh_list_cons binding_subst_fresh)
   493    (auto simp add: fresh_list_cons binding_subst_fresh)
   490 
   494 
   491 lemma ctxt_subst_mem_TVarB: "TVarB X T \<in> set \<Gamma> \<Longrightarrow> TVarB X (T[Y \<mapsto> U]\<^sub>\<tau>) \<in> set (\<Gamma>[Y \<mapsto> U]\<^sub>e)"
   495 lemma ctxt_subst_mem_TVarB: "TVarB X T \<in> set \<Gamma> \<Longrightarrow> TVarB X (T[Y \<mapsto> U]\<^sub>\<tau>) \<in> set (\<Gamma>[Y \<mapsto> U]\<^sub>e)"
   492   by (induct \<Gamma>) auto
   496   by (induct \<Gamma>) auto
   493 
   497 
   494 lemma ctxt_subst_mem_VarB: "VarB x T \<in> set \<Gamma> \<Longrightarrow> VarB x (T[Y \<mapsto> U]\<^sub>\<tau>) \<in> set (\<Gamma>[Y \<mapsto> U]\<^sub>e)"
   498 lemma ctxt_subst_mem_VarB: "VarB x T \<in> set \<Gamma> \<Longrightarrow> VarB x (T[Y \<mapsto> U]\<^sub>\<tau>) \<in> set (\<Gamma>[Y \<mapsto> U]\<^sub>e)"
   495   by (induct \<Gamma>) auto
   499   by (induct \<Gamma>) auto
   496 
   500 
   497 lemma ctxt_subst_identity: "X \<sharp> \<Gamma> \<Longrightarrow> \<Gamma>[X \<mapsto> U]\<^sub>e = \<Gamma>"
   501 lemma ctxt_subst_identity: "X\<sharp>\<Gamma> \<Longrightarrow> \<Gamma>[X \<mapsto> U]\<^sub>e = \<Gamma>"
   498   by (induct \<Gamma>) (simp_all add: fresh_list_cons binding_subst_identity)
   502   by (induct \<Gamma>) (simp_all add: fresh_list_cons binding_subst_identity)
   499 
   503 
   500 lemma ctxt_subst_append: "(\<Delta> @ \<Gamma>)[X \<mapsto> T]\<^sub>e = \<Delta>[X \<mapsto> T]\<^sub>e @ \<Gamma>[X \<mapsto> T]\<^sub>e"
   504 lemma ctxt_subst_append: "(\<Delta> @ \<Gamma>)[X \<mapsto> T]\<^sub>e = \<Delta>[X \<mapsto> T]\<^sub>e @ \<Gamma>[X \<mapsto> T]\<^sub>e"
   501   by (induct \<Delta>) simp_all
   505   by (induct \<Delta>) simp_all
   502 
   506 
   513 apply(simp add: abs_fresh)+
   517 apply(simp add: abs_fresh)+
   514 apply(fresh_guess add: ty_vrs_fresh abs_fresh)+
   518 apply(fresh_guess add: ty_vrs_fresh abs_fresh)+
   515 done
   519 done
   516 
   520 
   517 lemma subst_trm_fresh_tyvar:
   521 lemma subst_trm_fresh_tyvar:
   518   "(X::tyvrs) \<sharp> t \<Longrightarrow> X \<sharp> u \<Longrightarrow> X \<sharp> t[x \<mapsto> u]"
   522   fixes X::"tyvrs"
       
   523   shows "X\<sharp>t \<Longrightarrow> X\<sharp>u \<Longrightarrow> X\<sharp>t[x \<mapsto> u]"
   519   by (nominal_induct t avoiding: x u rule: trm.strong_induct)
   524   by (nominal_induct t avoiding: x u rule: trm.strong_induct)
   520     (auto simp add: trm.fresh abs_fresh)
   525     (auto simp add: trm.fresh abs_fresh)
   521 
   526 
   522 lemma subst_trm_fresh_var: "x \<sharp> u \<Longrightarrow> x \<sharp> t[x \<mapsto> u]"
   527 lemma subst_trm_fresh_var: 
       
   528   "x\<sharp>u \<Longrightarrow> x\<sharp>t[x \<mapsto> u]"
   523   by (nominal_induct t avoiding: x u rule: trm.strong_induct)
   529   by (nominal_induct t avoiding: x u rule: trm.strong_induct)
   524     (simp_all add: abs_fresh fresh_atm ty_vrs_fresh)
   530     (simp_all add: abs_fresh fresh_atm ty_vrs_fresh)
   525 
   531 
   526 lemma subst_trm_eqvt[eqvt]:
   532 lemma subst_trm_eqvt[eqvt]:
   527   fixes pi::"tyvrs prm" 
   533   fixes pi::"tyvrs prm" 
   536   shows "pi\<bullet>(t[x \<mapsto> u]) = (pi\<bullet>t)[(pi\<bullet>x) \<mapsto> (pi\<bullet>u)]"
   542   shows "pi\<bullet>(t[x \<mapsto> u]) = (pi\<bullet>t)[(pi\<bullet>x) \<mapsto> (pi\<bullet>u)]"
   537   by (nominal_induct t avoiding: x u rule: trm.strong_induct)
   543   by (nominal_induct t avoiding: x u rule: trm.strong_induct)
   538      (perm_simp add: fresh_left)+
   544      (perm_simp add: fresh_left)+
   539 
   545 
   540 lemma subst_trm_rename:
   546 lemma subst_trm_rename:
   541   "y \<sharp> t \<Longrightarrow> ([(y, x)] \<bullet> t)[y \<mapsto> u] = t[x \<mapsto> u]"
   547   "y\<sharp>t \<Longrightarrow> ([(y, x)] \<bullet> t)[y \<mapsto> u] = t[x \<mapsto> u]"
   542   by (nominal_induct t avoiding: x y u rule: trm.strong_induct)
   548   by (nominal_induct t avoiding: x y u rule: trm.strong_induct)
   543     (simp_all add: fresh_atm calc_atm abs_fresh fresh_aux ty_vrs_fresh perm_fresh_fresh)
   549     (simp_all add: fresh_atm calc_atm abs_fresh fresh_aux ty_vrs_fresh perm_fresh_fresh)
   544 
   550 
   545 nominal_primrec (freshness_context: "T2::ty")
   551 nominal_primrec (freshness_context: "T2::ty")
   546   subst_trm_ty :: "trm \<Rightarrow> tyvrs \<Rightarrow> ty \<Rightarrow> trm"  ("_[_ \<mapsto>\<^sub>\<tau> _]" [300, 0, 0] 300)
   552   subst_trm_ty :: "trm \<Rightarrow> tyvrs \<Rightarrow> ty \<Rightarrow> trm"  ("_[_ \<mapsto>\<^sub>\<tau> _]" [300, 0, 0] 300)
   556 apply(simp add: type_subst_fresh)
   562 apply(simp add: type_subst_fresh)
   557 apply(fresh_guess add: ty_vrs_fresh abs_fresh)+
   563 apply(fresh_guess add: ty_vrs_fresh abs_fresh)+
   558 done
   564 done
   559 
   565 
   560 lemma subst_trm_ty_fresh:
   566 lemma subst_trm_ty_fresh:
   561   "(X::tyvrs) \<sharp> t \<Longrightarrow> X \<sharp> T \<Longrightarrow> X \<sharp> t[Y \<mapsto>\<^sub>\<tau> T]"
   567   fixes X::"tyvrs"
       
   568   shows "X\<sharp>t \<Longrightarrow> X\<sharp>T \<Longrightarrow> X\<sharp>t[Y \<mapsto>\<^sub>\<tau> T]"
   562   by (nominal_induct t avoiding: Y T rule: trm.strong_induct)
   569   by (nominal_induct t avoiding: Y T rule: trm.strong_induct)
   563     (auto simp add: abs_fresh type_subst_fresh)
   570     (auto simp add: abs_fresh type_subst_fresh)
   564 
   571 
   565 lemma subst_trm_ty_fresh':
   572 lemma subst_trm_ty_fresh':
   566   "X \<sharp> T \<Longrightarrow> X \<sharp> t[X \<mapsto>\<^sub>\<tau> T]"
   573   "X\<sharp>T \<Longrightarrow> X\<sharp>t[X \<mapsto>\<^sub>\<tau> T]"
   567   by (nominal_induct t avoiding: X T rule: trm.strong_induct)
   574   by (nominal_induct t avoiding: X T rule: trm.strong_induct)
   568     (simp_all add: abs_fresh fresh_type_subst_fresh fresh_atm)
   575     (simp_all add: abs_fresh fresh_type_subst_fresh fresh_atm)
   569 
   576 
   570 lemma subst_trm_ty_eqvt[eqvt]:
   577 lemma subst_trm_ty_eqvt[eqvt]:
   571   fixes pi::"tyvrs prm" 
   578   fixes pi::"tyvrs prm" 
   580   shows "pi\<bullet>(t[X \<mapsto>\<^sub>\<tau> T]) = (pi\<bullet>t)[(pi\<bullet>X) \<mapsto>\<^sub>\<tau> (pi\<bullet>T)]"
   587   shows "pi\<bullet>(t[X \<mapsto>\<^sub>\<tau> T]) = (pi\<bullet>t)[(pi\<bullet>X) \<mapsto>\<^sub>\<tau> (pi\<bullet>T)]"
   581   by (nominal_induct t avoiding: X T rule: trm.strong_induct)
   588   by (nominal_induct t avoiding: X T rule: trm.strong_induct)
   582      (perm_simp add: fresh_left subst_eqvt')+
   589      (perm_simp add: fresh_left subst_eqvt')+
   583 
   590 
   584 lemma subst_trm_ty_rename:
   591 lemma subst_trm_ty_rename:
   585   "Y \<sharp> t \<Longrightarrow> ([(Y, X)] \<bullet> t)[Y \<mapsto>\<^sub>\<tau> U] = t[X \<mapsto>\<^sub>\<tau> U]"
   592   "Y\<sharp>t \<Longrightarrow> ([(Y, X)] \<bullet> t)[Y \<mapsto>\<^sub>\<tau> U] = t[X \<mapsto>\<^sub>\<tau> U]"
   586   by (nominal_induct t avoiding: X Y U rule: trm.strong_induct)
   593   by (nominal_induct t avoiding: X Y U rule: trm.strong_induct)
   587     (simp_all add: fresh_atm calc_atm abs_fresh fresh_aux type_subst_rename)
   594     (simp_all add: fresh_atm calc_atm abs_fresh fresh_aux type_subst_rename)
   588 
   595 
   589 section {* Subtyping-Relation *}
   596 section {* Subtyping-Relation *}
   590 
   597 
   597 
   604 
   598 inductive 
   605 inductive 
   599   subtype_of :: "env \<Rightarrow> ty \<Rightarrow> ty \<Rightarrow> bool"   ("_\<turnstile>_<:_" [100,100,100] 100)
   606   subtype_of :: "env \<Rightarrow> ty \<Rightarrow> ty \<Rightarrow> bool"   ("_\<turnstile>_<:_" [100,100,100] 100)
   600 where
   607 where
   601   SA_Top[intro]:    "\<lbrakk>\<turnstile> \<Gamma> ok; S closed_in \<Gamma>\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> S <: Top"
   608   SA_Top[intro]:    "\<lbrakk>\<turnstile> \<Gamma> ok; S closed_in \<Gamma>\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> S <: Top"
   602 | SA_refl_TVar[intro]:   "\<lbrakk>\<turnstile> \<Gamma> ok; X \<in> ty_domain \<Gamma>\<rbrakk>\<Longrightarrow> \<Gamma> \<turnstile> Tvar X <: Tvar X"
   609 | SA_refl_TVar[intro]:   "\<lbrakk>\<turnstile> \<Gamma> ok; X \<in> ty_dom \<Gamma>\<rbrakk>\<Longrightarrow> \<Gamma> \<turnstile> Tvar X <: Tvar X"
   603 | SA_trans_TVar[intro]:    "\<lbrakk>(TVarB X S) \<in> set \<Gamma>; \<Gamma> \<turnstile> S <: T\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> (Tvar X) <: T"
   610 | SA_trans_TVar[intro]:    "\<lbrakk>(TVarB X S) \<in> set \<Gamma>; \<Gamma> \<turnstile> S <: T\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> (Tvar X) <: T"
   604 | SA_arrow[intro]:  "\<lbrakk>\<Gamma> \<turnstile> T\<^isub>1 <: S\<^isub>1; \<Gamma> \<turnstile> S\<^isub>2 <: T\<^isub>2\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> (S\<^isub>1 \<rightarrow> S\<^isub>2) <: (T\<^isub>1 \<rightarrow> T\<^isub>2)" 
   611 | SA_arrow[intro]:  "\<lbrakk>\<Gamma> \<turnstile> T\<^isub>1 <: S\<^isub>1; \<Gamma> \<turnstile> S\<^isub>2 <: T\<^isub>2\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> (S\<^isub>1 \<rightarrow> S\<^isub>2) <: (T\<^isub>1 \<rightarrow> T\<^isub>2)" 
   605 | SA_all[intro]: "\<lbrakk>\<Gamma> \<turnstile> T\<^isub>1 <: S\<^isub>1; ((TVarB X T\<^isub>1)#\<Gamma>) \<turnstile> S\<^isub>2 <: T\<^isub>2\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> (\<forall>X<:S\<^isub>1. S\<^isub>2) <: (\<forall>X<:T\<^isub>1. T\<^isub>2)"
   612 | SA_all[intro]: "\<lbrakk>\<Gamma> \<turnstile> T\<^isub>1 <: S\<^isub>1; ((TVarB X T\<^isub>1)#\<Gamma>) \<turnstile> S\<^isub>2 <: T\<^isub>2\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> (\<forall>X<:S\<^isub>1. S\<^isub>2) <: (\<forall>X<:T\<^isub>1. T\<^isub>2)"
   606 
   613 
   607 lemma subtype_implies_ok:
   614 lemma subtype_implies_ok:
   621   have "S closed_in \<Gamma>" by fact
   628   have "S closed_in \<Gamma>" by fact
   622   ultimately show "S closed_in \<Gamma> \<and> Top closed_in \<Gamma>" by simp
   629   ultimately show "S closed_in \<Gamma> \<and> Top closed_in \<Gamma>" by simp
   623 next
   630 next
   624   case (SA_trans_TVar X S \<Gamma> T)
   631   case (SA_trans_TVar X S \<Gamma> T)
   625   have "(TVarB X S)\<in>set \<Gamma>" by fact
   632   have "(TVarB X S)\<in>set \<Gamma>" by fact
   626   hence "X \<in> ty_domain \<Gamma>" by (rule ty_domain_inclusion)
   633   hence "X \<in> ty_dom \<Gamma>" by (rule ty_dom_inclusion)
   627   hence "(Tvar X) closed_in \<Gamma>" by (simp add: closed_in_def ty.supp supp_atm)
   634   hence "(Tvar X) closed_in \<Gamma>" by (simp add: closed_in_def ty.supp supp_atm)
   628   moreover
   635   moreover
   629   have "S closed_in \<Gamma> \<and> T closed_in \<Gamma>" by fact
   636   have "S closed_in \<Gamma> \<and> T closed_in \<Gamma>" by fact
   630   hence "T closed_in \<Gamma>" by force
   637   hence "T closed_in \<Gamma>" by force
   631   ultimately show "(Tvar X) closed_in \<Gamma> \<and> T closed_in \<Gamma>" by simp
   638   ultimately show "(Tvar X) closed_in \<Gamma> \<and> T closed_in \<Gamma>" by simp
   636   assumes a1: "\<Gamma> \<turnstile> S <: T"
   643   assumes a1: "\<Gamma> \<turnstile> S <: T"
   637   and     a2: "X\<sharp>\<Gamma>"
   644   and     a2: "X\<sharp>\<Gamma>"
   638   shows "X\<sharp>S \<and> X\<sharp>T"  
   645   shows "X\<sharp>S \<and> X\<sharp>T"  
   639 proof -
   646 proof -
   640   from a1 have "\<turnstile> \<Gamma> ok" by (rule subtype_implies_ok)
   647   from a1 have "\<turnstile> \<Gamma> ok" by (rule subtype_implies_ok)
   641   with a2 have "X\<sharp>ty_domain(\<Gamma>)" by (simp add: fresh_domain)
   648   with a2 have "X\<sharp>ty_dom(\<Gamma>)" by (simp add: fresh_dom)
   642   moreover
   649   moreover
   643   from a1 have "S closed_in \<Gamma> \<and> T closed_in \<Gamma>" by (rule subtype_implies_closed)
   650   from a1 have "S closed_in \<Gamma> \<and> T closed_in \<Gamma>" by (rule subtype_implies_closed)
   644   hence "supp S \<subseteq> ((supp (ty_domain \<Gamma>))::tyvrs set)" 
   651   hence "supp S \<subseteq> ((supp (ty_dom \<Gamma>))::tyvrs set)" 
   645     and "supp T \<subseteq> ((supp (ty_domain \<Gamma>))::tyvrs set)" by (simp_all add: ty_domain_supp closed_in_def)
   652     and "supp T \<subseteq> ((supp (ty_dom \<Gamma>))::tyvrs set)" by (simp_all add: ty_dom_supp closed_in_def)
   646   ultimately show "X\<sharp>S \<and> X\<sharp>T" by (force simp add: supp_prod fresh_def)
   653   ultimately show "X\<sharp>S \<and> X\<sharp>T" by (force simp add: supp_prod fresh_def)
   647 qed
   654 qed
   648 
   655 
   649 lemma valid_ty_domain_fresh:
   656 lemma valid_ty_dom_fresh:
   650   fixes X::"tyvrs"
   657   fixes X::"tyvrs"
   651   assumes valid: "\<turnstile> \<Gamma> ok"
   658   assumes valid: "\<turnstile> \<Gamma> ok"
   652   shows "X\<sharp>(ty_domain \<Gamma>) = X\<sharp>\<Gamma>" 
   659   shows "X\<sharp>(ty_dom \<Gamma>) = X\<sharp>\<Gamma>" 
   653   using valid
   660   using valid
   654   apply induct
   661   apply induct
   655   apply (simp add: fresh_list_nil fresh_set_empty)
   662   apply (simp add: fresh_list_nil fresh_set_empty)
   656   apply (simp_all add: binding.fresh fresh_list_cons
   663   apply (simp_all add: binding.fresh fresh_list_cons
   657      fresh_fin_insert [OF pt_tyvrs_inst at_tyvrs_inst fs_tyvrs_inst] finite_domains fresh_atm)
   664      fresh_fin_insert [OF pt_tyvrs_inst at_tyvrs_inst fs_tyvrs_inst] finite_doms fresh_atm)
   658   apply (auto simp add: closed_in_fresh)
   665   apply (auto simp add: closed_in_fresh)
   659   done
   666   done
   660 
   667 
   661 equivariance subtype_of
   668 equivariance subtype_of
   662 
   669 
   663 nominal_inductive subtype_of
   670 nominal_inductive subtype_of
   664   apply (simp_all add: abs_fresh)
   671   apply (simp_all add: abs_fresh)
   665   apply (fastsimp simp add: valid_ty_domain_fresh dest: subtype_implies_ok)
   672   apply (fastsimp simp add: valid_ty_dom_fresh dest: subtype_implies_ok)
   666   apply (force simp add: closed_in_fresh dest: subtype_implies_closed subtype_implies_ok)+
   673   apply (force simp add: closed_in_fresh dest: subtype_implies_closed subtype_implies_ok)+
   667   done
   674   done
   668 
   675 
   669 section {* Reflexivity of Subtyping *}
   676 section {* Reflexivity of Subtyping *}
   670 
   677 
   676 proof(nominal_induct T avoiding: \<Gamma> rule: ty.strong_induct)
   683 proof(nominal_induct T avoiding: \<Gamma> rule: ty.strong_induct)
   677   case (Forall X T\<^isub>1 T\<^isub>2)
   684   case (Forall X T\<^isub>1 T\<^isub>2)
   678   have ih_T\<^isub>1: "\<And>\<Gamma>. \<lbrakk>\<turnstile> \<Gamma> ok; T\<^isub>1 closed_in \<Gamma>\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> T\<^isub>1 <: T\<^isub>1" by fact 
   685   have ih_T\<^isub>1: "\<And>\<Gamma>. \<lbrakk>\<turnstile> \<Gamma> ok; T\<^isub>1 closed_in \<Gamma>\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> T\<^isub>1 <: T\<^isub>1" by fact 
   679   have ih_T\<^isub>2: "\<And>\<Gamma>. \<lbrakk>\<turnstile> \<Gamma> ok; T\<^isub>2 closed_in \<Gamma>\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> T\<^isub>2 <: T\<^isub>2" by fact
   686   have ih_T\<^isub>2: "\<And>\<Gamma>. \<lbrakk>\<turnstile> \<Gamma> ok; T\<^isub>2 closed_in \<Gamma>\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> T\<^isub>2 <: T\<^isub>2" by fact
   680   have fresh_cond: "X\<sharp>\<Gamma>" by fact
   687   have fresh_cond: "X\<sharp>\<Gamma>" by fact
   681   hence fresh_ty_domain: "X\<sharp>(ty_domain \<Gamma>)" by (simp add: fresh_domain)
   688   hence fresh_ty_dom: "X\<sharp>(ty_dom \<Gamma>)" by (simp add: fresh_dom)
   682   have "(\<forall>X<:T\<^isub>2. T\<^isub>1) closed_in \<Gamma>" by fact
   689   have "(\<forall>X<:T\<^isub>2. T\<^isub>1) closed_in \<Gamma>" by fact
   683   hence closed\<^isub>T\<^isub>2: "T\<^isub>2 closed_in \<Gamma>" and closed\<^isub>T\<^isub>1: "T\<^isub>1 closed_in ((TVarB  X T\<^isub>2)#\<Gamma>)" 
   690   hence closed\<^isub>T\<^isub>2: "T\<^isub>2 closed_in \<Gamma>" and closed\<^isub>T\<^isub>1: "T\<^isub>1 closed_in ((TVarB  X T\<^isub>2)#\<Gamma>)" 
   684     by (auto simp add: closed_in_def ty.supp abs_supp)
   691     by (auto simp add: closed_in_def ty.supp abs_supp)
   685   have ok: "\<turnstile> \<Gamma> ok" by fact  
   692   have ok: "\<turnstile> \<Gamma> ok" by fact  
   686   hence ok': "\<turnstile> ((TVarB X T\<^isub>2)#\<Gamma>) ok" using closed\<^isub>T\<^isub>2 fresh_ty_domain by simp
   693   hence ok': "\<turnstile> ((TVarB X T\<^isub>2)#\<Gamma>) ok" using closed\<^isub>T\<^isub>2 fresh_ty_dom by simp
   687   have "\<Gamma> \<turnstile> T\<^isub>2 <: T\<^isub>2" using ih_T\<^isub>2 closed\<^isub>T\<^isub>2 ok by simp
   694   have "\<Gamma> \<turnstile> T\<^isub>2 <: T\<^isub>2" using ih_T\<^isub>2 closed\<^isub>T\<^isub>2 ok by simp
   688   moreover
   695   moreover
   689   have "((TVarB X T\<^isub>2)#\<Gamma>) \<turnstile> T\<^isub>1 <: T\<^isub>1" using ih_T\<^isub>1 closed\<^isub>T\<^isub>1 ok' by simp
   696   have "((TVarB X T\<^isub>2)#\<Gamma>) \<turnstile> T\<^isub>1 <: T\<^isub>1" using ih_T\<^isub>1 closed\<^isub>T\<^isub>1 ok' by simp
   690   ultimately show "\<Gamma> \<turnstile> (\<forall>X<:T\<^isub>2. T\<^isub>1) <: (\<forall>X<:T\<^isub>2. T\<^isub>1)" using fresh_cond 
   697   ultimately show "\<Gamma> \<turnstile> (\<forall>X<:T\<^isub>2. T\<^isub>1) <: (\<forall>X<:T\<^isub>2. T\<^isub>1)" using fresh_cond 
   691     by (simp add: subtype_of.SA_all)
   698     by (simp add: subtype_of.SA_all)
   700 apply(auto simp add: ty.supp abs_supp supp_atm closed_in_def)
   707 apply(auto simp add: ty.supp abs_supp supp_atm closed_in_def)
   701   --{* Too bad that this instantiation cannot be found automatically by
   708   --{* Too bad that this instantiation cannot be found automatically by
   702   \isakeyword{auto}; \isakeyword{blast} would find it if we had not used 
   709   \isakeyword{auto}; \isakeyword{blast} would find it if we had not used 
   703   an explicit definition for @{text "closed_in_def"}. *}
   710   an explicit definition for @{text "closed_in_def"}. *}
   704 apply(drule_tac x="(TVarB tyvrs ty2)#\<Gamma>" in meta_spec)
   711 apply(drule_tac x="(TVarB tyvrs ty2)#\<Gamma>" in meta_spec)
   705 apply(force dest: fresh_domain simp add: closed_in_def)
   712 apply(force dest: fresh_dom simp add: closed_in_def)
   706 done
   713 done
   707 
   714 
   708 section {* Weakening *}
   715 section {* Weakening *}
   709 
   716 
   710 text {* In order to prove weakening we introduce the notion of a type-context extending 
   717 text {* In order to prove weakening we introduce the notion of a type-context extending 
   713 
   720 
   714 constdefs 
   721 constdefs 
   715   extends :: "env \<Rightarrow> env \<Rightarrow> bool" ("_ extends _" [100,100] 100)
   722   extends :: "env \<Rightarrow> env \<Rightarrow> bool" ("_ extends _" [100,100] 100)
   716   "\<Delta> extends \<Gamma> \<equiv> \<forall>X Q. (TVarB X Q)\<in>set \<Gamma> \<longrightarrow> (TVarB X Q)\<in>set \<Delta>"
   723   "\<Delta> extends \<Gamma> \<equiv> \<forall>X Q. (TVarB X Q)\<in>set \<Gamma> \<longrightarrow> (TVarB X Q)\<in>set \<Delta>"
   717 
   724 
   718 lemma extends_ty_domain:
   725 lemma extends_ty_dom:
   719   assumes a: "\<Delta> extends \<Gamma>"
   726   assumes a: "\<Delta> extends \<Gamma>"
   720   shows "ty_domain \<Gamma> \<subseteq> ty_domain \<Delta>"
   727   shows "ty_dom \<Gamma> \<subseteq> ty_dom \<Delta>"
   721   using a 
   728   using a 
   722   apply (auto simp add: extends_def)
   729   apply (auto simp add: extends_def)
   723   apply (drule ty_domain_existence)
   730   apply (drule ty_dom_existence)
   724   apply (force simp add: ty_domain_inclusion)
   731   apply (force simp add: ty_dom_inclusion)
   725   done
   732   done
   726 
   733 
   727 lemma extends_closed:
   734 lemma extends_closed:
   728   assumes a1: "T closed_in \<Gamma>"
   735   assumes a1: "T closed_in \<Gamma>"
   729   and     a2: "\<Delta> extends \<Gamma>"
   736   and     a2: "\<Delta> extends \<Gamma>"
   730   shows "T closed_in \<Delta>"
   737   shows "T closed_in \<Delta>"
   731   using a1 a2
   738   using a1 a2
   732   by (auto dest: extends_ty_domain simp add: closed_in_def)
   739   by (auto dest: extends_ty_dom simp add: closed_in_def)
   733 
   740 
   734 lemma extends_memb:
   741 lemma extends_memb:
   735   assumes a: "\<Delta> extends \<Gamma>"
   742   assumes a: "\<Delta> extends \<Gamma>"
   736   and b: "(TVarB X T) \<in> set \<Gamma>"
   743   and b: "(TVarB X T) \<in> set \<Gamma>"
   737   shows "(TVarB X T) \<in> set \<Delta>"
   744   shows "(TVarB X T) \<in> set \<Delta>"
   761   moreover
   768   moreover
   762   have "\<Delta> \<turnstile> S <: T" using ok extends ih by simp
   769   have "\<Delta> \<turnstile> S <: T" using ok extends ih by simp
   763   ultimately show "\<Delta> \<turnstile> Tvar X <: T" using ok by force
   770   ultimately show "\<Delta> \<turnstile> Tvar X <: T" using ok by force
   764 next
   771 next
   765   case (SA_refl_TVar \<Gamma> X)
   772   case (SA_refl_TVar \<Gamma> X)
   766   have lh_drv_prem: "X \<in> ty_domain \<Gamma>" by fact
   773   have lh_drv_prem: "X \<in> ty_dom \<Gamma>" by fact
   767   have "\<turnstile> \<Delta> ok" by fact
   774   have "\<turnstile> \<Delta> ok" by fact
   768   moreover
   775   moreover
   769   have "\<Delta> extends \<Gamma>" by fact
   776   have "\<Delta> extends \<Gamma>" by fact
   770   hence "X \<in> ty_domain \<Delta>" using lh_drv_prem by (force dest: extends_ty_domain)
   777   hence "X \<in> ty_dom \<Delta>" using lh_drv_prem by (force dest: extends_ty_dom)
   771   ultimately show "\<Delta> \<turnstile> Tvar X <: Tvar X" by force
   778   ultimately show "\<Delta> \<turnstile> Tvar X <: Tvar X" by force
   772 next 
   779 next 
   773   case (SA_arrow \<Gamma> T\<^isub>1 S\<^isub>1 S\<^isub>2 T\<^isub>2) thus "\<Delta> \<turnstile> S\<^isub>1 \<rightarrow> S\<^isub>2 <: T\<^isub>1 \<rightarrow> T\<^isub>2" by blast
   780   case (SA_arrow \<Gamma> T\<^isub>1 S\<^isub>1 S\<^isub>2 T\<^isub>2) thus "\<Delta> \<turnstile> S\<^isub>1 \<rightarrow> S\<^isub>2 <: T\<^isub>1 \<rightarrow> T\<^isub>2" by blast
   774 next
   781 next
   775   case (SA_all \<Gamma> T\<^isub>1 S\<^isub>1 X S\<^isub>2 T\<^isub>2)
   782   case (SA_all \<Gamma> T\<^isub>1 S\<^isub>1 X S\<^isub>2 T\<^isub>2)
   776   have fresh_cond: "X\<sharp>\<Delta>" by fact
   783   have fresh_cond: "X\<sharp>\<Delta>" by fact
   777   hence fresh_domain: "X\<sharp>(ty_domain \<Delta>)" by (simp add: fresh_domain)
   784   hence fresh_dom: "X\<sharp>(ty_dom \<Delta>)" by (simp add: fresh_dom)
   778   have ih\<^isub>1: "\<And>\<Delta>. \<turnstile> \<Delta> ok \<Longrightarrow> \<Delta> extends \<Gamma> \<Longrightarrow> \<Delta> \<turnstile> T\<^isub>1 <: S\<^isub>1" by fact
   785   have ih\<^isub>1: "\<And>\<Delta>. \<turnstile> \<Delta> ok \<Longrightarrow> \<Delta> extends \<Gamma> \<Longrightarrow> \<Delta> \<turnstile> T\<^isub>1 <: S\<^isub>1" by fact
   779   have ih\<^isub>2: "\<And>\<Delta>. \<turnstile> \<Delta> ok \<Longrightarrow> \<Delta> extends ((TVarB X T\<^isub>1)#\<Gamma>) \<Longrightarrow> \<Delta> \<turnstile> S\<^isub>2 <: T\<^isub>2" by fact
   786   have ih\<^isub>2: "\<And>\<Delta>. \<turnstile> \<Delta> ok \<Longrightarrow> \<Delta> extends ((TVarB X T\<^isub>1)#\<Gamma>) \<Longrightarrow> \<Delta> \<turnstile> S\<^isub>2 <: T\<^isub>2" by fact
   780   have lh_drv_prem: "\<Gamma> \<turnstile> T\<^isub>1 <: S\<^isub>1" by fact
   787   have lh_drv_prem: "\<Gamma> \<turnstile> T\<^isub>1 <: S\<^isub>1" by fact
   781   hence closed\<^isub>T\<^isub>1: "T\<^isub>1 closed_in \<Gamma>" by (simp add: subtype_implies_closed) 
   788   hence closed\<^isub>T\<^isub>1: "T\<^isub>1 closed_in \<Gamma>" by (simp add: subtype_implies_closed) 
   782   have ok: "\<turnstile> \<Delta> ok" by fact
   789   have ok: "\<turnstile> \<Delta> ok" by fact
   783   have ext: "\<Delta> extends \<Gamma>" by fact
   790   have ext: "\<Delta> extends \<Gamma>" by fact
   784   have "T\<^isub>1 closed_in \<Delta>" using ext closed\<^isub>T\<^isub>1 by (simp only: extends_closed)
   791   have "T\<^isub>1 closed_in \<Delta>" using ext closed\<^isub>T\<^isub>1 by (simp only: extends_closed)
   785   hence "\<turnstile> ((TVarB X T\<^isub>1)#\<Delta>) ok" using fresh_domain ok by force   
   792   hence "\<turnstile> ((TVarB X T\<^isub>1)#\<Delta>) ok" using fresh_dom ok by force   
   786   moreover 
   793   moreover 
   787   have "((TVarB X T\<^isub>1)#\<Delta>) extends ((TVarB X T\<^isub>1)#\<Gamma>)" using ext by (force simp add: extends_def)
   794   have "((TVarB X T\<^isub>1)#\<Delta>) extends ((TVarB X T\<^isub>1)#\<Gamma>)" using ext by (force simp add: extends_def)
   788   ultimately have "((TVarB X T\<^isub>1)#\<Delta>) \<turnstile> S\<^isub>2 <: T\<^isub>2" using ih\<^isub>2 by simp
   795   ultimately have "((TVarB X T\<^isub>1)#\<Delta>) \<turnstile> S\<^isub>2 <: T\<^isub>2" using ih\<^isub>2 by simp
   789   moreover
   796   moreover
   790   have "\<Delta> \<turnstile> T\<^isub>1 <: S\<^isub>1" using ok ext ih\<^isub>1 by simp 
   797   have "\<Delta> \<turnstile> T\<^isub>1 <: S\<^isub>1" using ok ext ih\<^isub>1 by simp 
   800   shows "\<Delta> \<turnstile> S <: T"
   807   shows "\<Delta> \<turnstile> S <: T"
   801   using a b c 
   808   using a b c 
   802 proof (nominal_induct \<Gamma> S T avoiding: \<Delta> rule: subtype_of.strong_induct)
   809 proof (nominal_induct \<Gamma> S T avoiding: \<Delta> rule: subtype_of.strong_induct)
   803   case (SA_all \<Gamma> T\<^isub>1 S\<^isub>1 X S\<^isub>2 T\<^isub>2)
   810   case (SA_all \<Gamma> T\<^isub>1 S\<^isub>1 X S\<^isub>2 T\<^isub>2)
   804   have fresh_cond: "X\<sharp>\<Delta>" by fact
   811   have fresh_cond: "X\<sharp>\<Delta>" by fact
   805   hence fresh_domain: "X\<sharp>(ty_domain \<Delta>)" by (simp add: fresh_domain)
   812   hence fresh_dom: "X\<sharp>(ty_dom \<Delta>)" by (simp add: fresh_dom)
   806   have ih\<^isub>1: "\<And>\<Delta>. \<turnstile> \<Delta> ok \<Longrightarrow> \<Delta> extends \<Gamma> \<Longrightarrow> \<Delta> \<turnstile> T\<^isub>1 <: S\<^isub>1" by fact
   813   have ih\<^isub>1: "\<And>\<Delta>. \<turnstile> \<Delta> ok \<Longrightarrow> \<Delta> extends \<Gamma> \<Longrightarrow> \<Delta> \<turnstile> T\<^isub>1 <: S\<^isub>1" by fact
   807   have ih\<^isub>2: "\<And>\<Delta>. \<turnstile> \<Delta> ok \<Longrightarrow> \<Delta> extends ((TVarB X T\<^isub>1)#\<Gamma>) \<Longrightarrow> \<Delta> \<turnstile> S\<^isub>2 <: T\<^isub>2" by fact
   814   have ih\<^isub>2: "\<And>\<Delta>. \<turnstile> \<Delta> ok \<Longrightarrow> \<Delta> extends ((TVarB X T\<^isub>1)#\<Gamma>) \<Longrightarrow> \<Delta> \<turnstile> S\<^isub>2 <: T\<^isub>2" by fact
   808   have lh_drv_prem: "\<Gamma> \<turnstile> T\<^isub>1 <: S\<^isub>1" by fact
   815   have lh_drv_prem: "\<Gamma> \<turnstile> T\<^isub>1 <: S\<^isub>1" by fact
   809   hence closed\<^isub>T\<^isub>1: "T\<^isub>1 closed_in \<Gamma>" by (simp add: subtype_implies_closed) 
   816   hence closed\<^isub>T\<^isub>1: "T\<^isub>1 closed_in \<Gamma>" by (simp add: subtype_implies_closed) 
   810   have ok: "\<turnstile> \<Delta> ok" by fact
   817   have ok: "\<turnstile> \<Delta> ok" by fact
   811   have ext: "\<Delta> extends \<Gamma>" by fact
   818   have ext: "\<Delta> extends \<Gamma>" by fact
   812   have "T\<^isub>1 closed_in \<Delta>" using ext closed\<^isub>T\<^isub>1 by (simp only: extends_closed)
   819   have "T\<^isub>1 closed_in \<Delta>" using ext closed\<^isub>T\<^isub>1 by (simp only: extends_closed)
   813   hence "\<turnstile> ((TVarB X T\<^isub>1)#\<Delta>) ok" using fresh_domain ok by force   
   820   hence "\<turnstile> ((TVarB X T\<^isub>1)#\<Delta>) ok" using fresh_dom ok by force   
   814   moreover
   821   moreover
   815   have "((TVarB X T\<^isub>1)#\<Delta>) extends ((TVarB X T\<^isub>1)#\<Gamma>)" using ext by (force simp add: extends_def)
   822   have "((TVarB X T\<^isub>1)#\<Delta>) extends ((TVarB X T\<^isub>1)#\<Gamma>)" using ext by (force simp add: extends_def)
   816   ultimately have "((TVarB X T\<^isub>1)#\<Delta>) \<turnstile> S\<^isub>2 <: T\<^isub>2" using ih\<^isub>2 by simp
   823   ultimately have "((TVarB X T\<^isub>1)#\<Delta>) \<turnstile> S\<^isub>2 <: T\<^isub>2" using ih\<^isub>2 by simp
   817   moreover
   824   moreover
   818   have "\<Delta> \<turnstile> T\<^isub>1 <: S\<^isub>1" using ok ext ih\<^isub>1 by simp 
   825   have "\<Delta> \<turnstile> T\<^isub>1 <: S\<^isub>1" using ok ext ih\<^isub>1 by simp 
   819   ultimately show "\<Delta> \<turnstile> (\<forall>X<:S\<^isub>1. S\<^isub>2) <: (\<forall>X<:T\<^isub>1. T\<^isub>2)" using ok by (force intro: SA_all)
   826   ultimately show "\<Delta> \<turnstile> (\<forall>X<:S\<^isub>1. S\<^isub>2) <: (\<forall>X<:T\<^isub>1. T\<^isub>2)" using ok by (force intro: SA_all)
   820 qed (blast intro: extends_closed extends_memb dest: extends_ty_domain)+
   827 qed (blast intro: extends_closed extends_memb dest: extends_ty_dom)+
   821 
   828 
   822 section {* Transitivity and Narrowing *}
   829 section {* Transitivity and Narrowing *}
   823 
   830 
   824 text {* Some inversion lemmas that are needed in the transitivity and narrowing proof.*}
   831 text {* Some inversion lemmas that are needed in the transitivity and narrowing proof.*}
   825 
   832 
   829 inductive_cases S_ArrowE_left: "\<Gamma> \<turnstile> S\<^isub>1 \<rightarrow> S\<^isub>2 <: T" 
   836 inductive_cases S_ArrowE_left: "\<Gamma> \<turnstile> S\<^isub>1 \<rightarrow> S\<^isub>2 <: T" 
   830 
   837 
   831 declare ty.inject [simp del]
   838 declare ty.inject [simp del]
   832 
   839 
   833 lemma S_ForallE_left:
   840 lemma S_ForallE_left:
   834   shows "\<lbrakk>\<Gamma> \<turnstile> (\<forall>X<:S\<^isub>1. S\<^isub>2) <: T; X\<sharp>\<Gamma>; X\<sharp>S\<^isub>1\<rbrakk>
   841   shows "\<lbrakk>\<Gamma> \<turnstile> (\<forall>X<:S\<^isub>1. S\<^isub>2) <: T; X\<sharp>\<Gamma>; X\<sharp>S\<^isub>1; X\<sharp>T\<rbrakk>
   835          \<Longrightarrow> T = Top \<or> (\<exists>T\<^isub>1 T\<^isub>2. T = (\<forall>X<:T\<^isub>1. T\<^isub>2) \<and> \<Gamma> \<turnstile> T\<^isub>1 <: S\<^isub>1 \<and> ((TVarB X T\<^isub>1)#\<Gamma>) \<turnstile> S\<^isub>2 <: T\<^isub>2)"
   842          \<Longrightarrow> T = Top \<or> (\<exists>T\<^isub>1 T\<^isub>2. T = (\<forall>X<:T\<^isub>1. T\<^isub>2) \<and> \<Gamma> \<turnstile> T\<^isub>1 <: S\<^isub>1 \<and> ((TVarB X T\<^isub>1)#\<Gamma>) \<turnstile> S\<^isub>2 <: T\<^isub>2)"
   836   apply(frule subtype_implies_ok)
   843 apply(erule subtype_of.strong_cases[where X="X"])
   837   apply(ind_cases "\<Gamma> \<turnstile> (\<forall>X<:S\<^isub>1. S\<^isub>2) <: T")
   844 apply(auto simp add: abs_fresh ty.inject alpha)
   838   apply(auto simp add: ty.inject alpha)
   845 done
   839   apply(rule_tac x="[(X,Xa)]\<bullet>T\<^isub>2" in exI)
       
   840   apply(rule conjI)
       
   841   apply(rule sym)
       
   842   apply(rule pt_bij2[OF pt_tyvrs_inst, OF at_tyvrs_inst])
       
   843   apply(rule pt_tyvrs3)
       
   844   apply(simp)
       
   845   apply(rule at_ds5[OF at_tyvrs_inst])
       
   846   apply(rule conjI)
       
   847   apply(simp add: pt_fresh_left[OF pt_tyvrs_inst, OF at_tyvrs_inst] calc_atm)
       
   848   apply(drule_tac \<Gamma>="((TVarB Xa T\<^isub>1)#\<Gamma>)" in  subtype_implies_closed)+
       
   849   apply(simp add: closed_in_def)
       
   850   apply(drule fresh_domain)+
       
   851   apply(simp add: fresh_def)
       
   852   apply(subgoal_tac "X \<notin> (insert Xa (ty_domain \<Gamma>))")(*A*)
       
   853   apply(force)
       
   854   (*A*)apply(simp add: at_fin_set_supp[OF at_tyvrs_inst, OF finite_domains(1)])
       
   855   (* 2nd conjunct *)apply(frule_tac X="X" in subtype_implies_fresh)
       
   856   apply(assumption)
       
   857   apply (frule_tac \<Gamma>="TVarB Xa T\<^isub>1 # \<Gamma>" in subtype_implies_ok)
       
   858   apply (erule validE)
       
   859   apply (simp add: valid_ty_domain_fresh)
       
   860   apply(drule_tac X="Xa" in subtype_implies_fresh)
       
   861   apply(assumption)
       
   862   apply(drule_tac pi="[(X,Xa)]" in subtype_of.eqvt(2))
       
   863   apply(simp add: calc_atm)
       
   864   apply(simp add: pt_fresh_fresh[OF pt_tyvrs_inst, OF at_tyvrs_inst])
       
   865   done
       
   866 
   846 
   867 text {* Next we prove the transitivity and narrowing for the subtyping-relation. 
   847 text {* Next we prove the transitivity and narrowing for the subtyping-relation. 
   868 The POPLmark-paper says the following:
   848 The POPLmark-paper says the following:
   869 
   849 
   870 \begin{quote}
   850 \begin{quote}
   896 lemma 
   876 lemma 
   897   shows subtype_transitivity: "\<Gamma>\<turnstile>S<:Q \<Longrightarrow> \<Gamma>\<turnstile>Q<:T \<Longrightarrow> \<Gamma>\<turnstile>S<:T" 
   877   shows subtype_transitivity: "\<Gamma>\<turnstile>S<:Q \<Longrightarrow> \<Gamma>\<turnstile>Q<:T \<Longrightarrow> \<Gamma>\<turnstile>S<:T" 
   898   and subtype_narrow: "(\<Delta>@[(TVarB X Q)]@\<Gamma>)\<turnstile>M<:N \<Longrightarrow> \<Gamma>\<turnstile>P<:Q \<Longrightarrow> (\<Delta>@[(TVarB X P)]@\<Gamma>)\<turnstile>M<:N"
   878   and subtype_narrow: "(\<Delta>@[(TVarB X Q)]@\<Gamma>)\<turnstile>M<:N \<Longrightarrow> \<Gamma>\<turnstile>P<:Q \<Longrightarrow> (\<Delta>@[(TVarB X P)]@\<Gamma>)\<turnstile>M<:N"
   899 proof (induct Q arbitrary: \<Gamma> S T \<Delta> X P M N taking: "size_ty" rule: measure_induct_rule)
   879 proof (induct Q arbitrary: \<Gamma> S T \<Delta> X P M N taking: "size_ty" rule: measure_induct_rule)
   900   case (less Q)
   880   case (less Q)
   901     --{* \begin{minipage}[t]{0.9\textwidth}
       
   902     First we mention the induction hypotheses of the outer induction for later
       
   903     reference:\end{minipage}*}
       
   904   have IH_trans:  
   881   have IH_trans:  
   905     "\<And>Q' \<Gamma> S T. \<lbrakk>size_ty Q' < size_ty Q; \<Gamma>\<turnstile>S<:Q'; \<Gamma>\<turnstile>Q'<:T\<rbrakk> \<Longrightarrow> \<Gamma>\<turnstile>S<:T" by fact
   882     "\<And>Q' \<Gamma> S T. \<lbrakk>size_ty Q' < size_ty Q; \<Gamma>\<turnstile>S<:Q'; \<Gamma>\<turnstile>Q'<:T\<rbrakk> \<Longrightarrow> \<Gamma>\<turnstile>S<:T" by fact
   906   have IH_narrow:
   883   have IH_narrow:
   907     "\<And>Q' \<Delta> \<Gamma> X M N P. \<lbrakk>size_ty Q' < size_ty Q; (\<Delta>@[(TVarB X Q')]@\<Gamma>)\<turnstile>M<:N; \<Gamma>\<turnstile>P<:Q'\<rbrakk> 
   884     "\<And>Q' \<Delta> \<Gamma> X M N P. \<lbrakk>size_ty Q' < size_ty Q; (\<Delta>@[(TVarB X Q')]@\<Gamma>)\<turnstile>M<:N; \<Gamma>\<turnstile>P<:Q'\<rbrakk> 
   908     \<Longrightarrow> (\<Delta>@[(TVarB X P)]@\<Gamma>)\<turnstile>M<:N" by fact
   885     \<Longrightarrow> (\<Delta>@[(TVarB X P)]@\<Gamma>)\<turnstile>M<:N" by fact
   909     --{* \begin{minipage}[t]{0.9\textwidth}
   886 
   910     We proceed with the transitivity proof as an auxiliary lemma, because it needs 
   887   { fix \<Gamma> S T
   911     to be referenced in the narrowing proof.\end{minipage}*}
   888     have "\<lbrakk>\<Gamma> \<turnstile> S <: Q; \<Gamma> \<turnstile> Q <: T\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> S <: T" 
   912   have transitivity_aux:
   889     proof (induct \<Gamma> S Q\<equiv>Q rule: subtype_of.induct) 
   913     "\<And>\<Gamma> S T. \<lbrakk>\<Gamma> \<turnstile> S <: Q; \<Gamma> \<turnstile> Q <: T\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> S <: T"
       
   914   proof - 
       
   915     fix \<Gamma>' S' T
       
   916     assume "\<Gamma>' \<turnstile> S' <: Q" --{* left-hand derivation *}
       
   917       and  "\<Gamma>' \<turnstile> Q <: T"  --{* right-hand derivation *}
       
   918     thus "\<Gamma>' \<turnstile> S' <: T"
       
   919     proof (nominal_induct \<Gamma>' S' Q\<equiv>Q rule: subtype_of.strong_induct) 
       
   920       case (SA_Top \<Gamma> S) 
   890       case (SA_Top \<Gamma> S) 
   921 	--{* \begin{minipage}[t]{0.9\textwidth}
   891       then have rh_drv: "\<Gamma> \<turnstile> Top <: T" by simp
   922 	In this case the left-hand derivation is @{term "\<Gamma> \<turnstile> S <: Top"}, giving
   892       then have T_inst: "T = Top" by (auto elim: S_TopE)
   923 	us @{term "\<turnstile> \<Gamma> ok"} and @{term "S closed_in \<Gamma>"}. This case is straightforward, 
       
   924 	because the right-hand derivation must be of the form @{term "\<Gamma> \<turnstile> Top <: Top"} 
       
   925 	giving us the equation @{term "T = Top"}.\end{minipage}*}
       
   926       hence rh_drv: "\<Gamma> \<turnstile> Top <: T" by simp
       
   927       hence T_inst: "T = Top" by (auto elim: S_TopE)
       
   928       from `\<turnstile> \<Gamma> ok` and `S closed_in \<Gamma>`
   893       from `\<turnstile> \<Gamma> ok` and `S closed_in \<Gamma>`
   929       have "\<Gamma> \<turnstile> S <: Top" by (simp add: subtype_of.SA_Top)
   894       have "\<Gamma> \<turnstile> S <: Top" by auto
   930       thus "\<Gamma> \<turnstile> S <: T" using T_inst by simp
   895       then show "\<Gamma> \<turnstile> S <: T" using T_inst by simp 
   931     next
   896     next
   932       case (SA_trans_TVar Y U \<Gamma>) 
   897       case (SA_trans_TVar Y U \<Gamma>) 
   933 	-- {* \begin{minipage}[t]{0.9\textwidth}
   898       then have IH_inner: "\<Gamma> \<turnstile> U <: T" by simp
   934 	In this case the left-hand derivation is @{term "\<Gamma> \<turnstile> Tvar Y <: Q"} 
       
   935 	with @{term "S = Tvar Y"}. We have therefore @{term "(Y,U)"} 
       
   936 	is in @{term "\<Gamma>"} and by inner induction hypothesis that @{term "\<Gamma> \<turnstile> U <: T"}. 
       
   937 	By @{text "S_Var"} follows @{term "\<Gamma> \<turnstile> Tvar Y <: T"}.\end{minipage}*}
       
   938       hence IH_inner: "\<Gamma> \<turnstile> U <: T" by simp
       
   939       have "(TVarB Y U) \<in> set \<Gamma>" by fact
   899       have "(TVarB Y U) \<in> set \<Gamma>" by fact
   940       with IH_inner show "\<Gamma> \<turnstile> Tvar Y <: T" by (simp add: subtype_of.SA_trans_TVar)
   900       with IH_inner show "\<Gamma> \<turnstile> Tvar Y <: T" by auto
   941     next
   901     next
   942       case (SA_refl_TVar \<Gamma> X) 
   902       case (SA_refl_TVar \<Gamma> X) 
   943 	--{* \begin{minipage}[t]{0.9\textwidth}
   903       then show "\<Gamma> \<turnstile> Tvar X <: T" by simp
   944         In this case the left-hand derivation is @{term "\<Gamma>\<turnstile>(Tvar X) <: (Tvar X)"} with
       
   945         @{term "Q=Tvar X"}. The goal then follows immediately from the right-hand 
       
   946 	derivation.\end{minipage}*}
       
   947       thus "\<Gamma> \<turnstile> Tvar X <: T" by simp
       
   948     next
   904     next
   949       case (SA_arrow \<Gamma> Q\<^isub>1 S\<^isub>1 S\<^isub>2 Q\<^isub>2) 
   905       case (SA_arrow \<Gamma> Q\<^isub>1 S\<^isub>1 S\<^isub>2 Q\<^isub>2) 
   950 	--{* \begin{minipage}[t]{0.9\textwidth}
   906       then have rh_drv: "\<Gamma> \<turnstile> Q\<^isub>1 \<rightarrow> Q\<^isub>2 <: T" by simp
   951 	In this case the left-hand derivation is @{term "\<Gamma> \<turnstile> S\<^isub>1 \<rightarrow> S\<^isub>2 <: Q\<^isub>1 \<rightarrow> Q\<^isub>2"} with
       
   952         @{term "S\<^isub>1\<rightarrow>S\<^isub>2=S"} and @{term "Q\<^isub>1\<rightarrow>Q\<^isub>2=Q"}. We know that the @{text "size_ty"} of 
       
   953 	@{term Q\<^isub>1} and @{term Q\<^isub>2} is smaller than that of @{term Q};
       
   954 	so we can apply the outer induction hypotheses for @{term Q\<^isub>1} and @{term Q\<^isub>2}. 
       
   955 	We also have the sub-derivations  @{term "\<Gamma>\<turnstile>Q\<^isub>1<:S\<^isub>1"} and @{term "\<Gamma>\<turnstile>S\<^isub>2<:Q\<^isub>2"}.
       
   956 	The right-hand derivation is @{term "\<Gamma> \<turnstile> Q\<^isub>1 \<rightarrow> Q\<^isub>2 <: T"}. There exist types 
       
   957 	@{text "T\<^isub>1,T\<^isub>2"} such that @{term "T=Top \<or> T=T\<^isub>1\<rightarrow>T\<^isub>2"}. The @{term "Top"}-case is 
       
   958 	straightforward once we know @{term "(S\<^isub>1 \<rightarrow> S\<^isub>2) closed_in \<Gamma>"} and @{term "\<turnstile> \<Gamma> ok"}. 
       
   959 	In the other case we have the sub-derivations @{term "\<Gamma>\<turnstile>T\<^isub>1<:Q\<^isub>1"} and @{term "\<Gamma>\<turnstile>Q\<^isub>2<:T\<^isub>2"}. 
       
   960 	Using the outer induction hypothesis for transitivity we can derive @{term "\<Gamma>\<turnstile>T\<^isub>1<:S\<^isub>1"} 
       
   961 	and @{term "\<Gamma>\<turnstile>S\<^isub>2<:T\<^isub>2"}. By rule @{text "S_Arrow"} follows @{term "\<Gamma> \<turnstile> S\<^isub>1 \<rightarrow> S\<^isub>2 <: T\<^isub>1 \<rightarrow> T\<^isub>2"},
       
   962 	which is @{term "\<Gamma> \<turnstile> S\<^isub>1 \<rightarrow> S\<^isub>2 <: T\<^isub>"}.\end{minipage}*}
       
   963       hence rh_drv: "\<Gamma> \<turnstile> Q\<^isub>1 \<rightarrow> Q\<^isub>2 <: T" by simp
       
   964       from `Q\<^isub>1 \<rightarrow> Q\<^isub>2 = Q` 
   907       from `Q\<^isub>1 \<rightarrow> Q\<^isub>2 = Q` 
   965       have Q\<^isub>1\<^isub>2_less: "size_ty Q\<^isub>1 < size_ty Q" "size_ty Q\<^isub>2 < size_ty Q" by auto
   908       have Q\<^isub>1\<^isub>2_less: "size_ty Q\<^isub>1 < size_ty Q" "size_ty Q\<^isub>2 < size_ty Q" by auto
   966       have lh_drv_prm\<^isub>1: "\<Gamma> \<turnstile> Q\<^isub>1 <: S\<^isub>1" by fact
   909       have lh_drv_prm\<^isub>1: "\<Gamma> \<turnstile> Q\<^isub>1 <: S\<^isub>1" by fact
   967       have lh_drv_prm\<^isub>2: "\<Gamma> \<turnstile> S\<^isub>2 <: Q\<^isub>2" by fact      
   910       have lh_drv_prm\<^isub>2: "\<Gamma> \<turnstile> S\<^isub>2 <: Q\<^isub>2" by fact      
   968       from rh_drv have "T=Top \<or> (\<exists>T\<^isub>1 T\<^isub>2. T=T\<^isub>1\<rightarrow>T\<^isub>2 \<and> \<Gamma>\<turnstile>T\<^isub>1<:Q\<^isub>1 \<and> \<Gamma>\<turnstile>Q\<^isub>2<:T\<^isub>2)" 
   911       from rh_drv have "T=Top \<or> (\<exists>T\<^isub>1 T\<^isub>2. T=T\<^isub>1\<rightarrow>T\<^isub>2 \<and> \<Gamma>\<turnstile>T\<^isub>1<:Q\<^isub>1 \<and> \<Gamma>\<turnstile>Q\<^isub>2<:T\<^isub>2)" 
   969 	by (auto elim: S_ArrowE_left)  
   912 	by (auto elim: S_ArrowE_left)
   970       moreover
   913       moreover
   971       have "S\<^isub>1 closed_in \<Gamma>" and "S\<^isub>2 closed_in \<Gamma>" 
   914       have "S\<^isub>1 closed_in \<Gamma>" and "S\<^isub>2 closed_in \<Gamma>" 
   972 	using lh_drv_prm\<^isub>1 lh_drv_prm\<^isub>2 by (simp_all add: subtype_implies_closed)
   915 	using lh_drv_prm\<^isub>1 lh_drv_prm\<^isub>2 by (simp_all add: subtype_implies_closed)
   973       hence "(S\<^isub>1 \<rightarrow> S\<^isub>2) closed_in \<Gamma>" by (simp add: closed_in_def ty.supp)
   916       hence "(S\<^isub>1 \<rightarrow> S\<^isub>2) closed_in \<Gamma>" by (simp add: closed_in_def ty.supp)
   974       moreover
   917       moreover
   975       have "\<turnstile> \<Gamma> ok" using rh_drv by (rule subtype_implies_ok)
   918       have "\<turnstile> \<Gamma> ok" using rh_drv by (rule subtype_implies_ok)
   976       moreover
   919       moreover
   977       { assume "\<exists>T\<^isub>1 T\<^isub>2. T=T\<^isub>1\<rightarrow>T\<^isub>2 \<and> \<Gamma>\<turnstile>T\<^isub>1<:Q\<^isub>1 \<and> \<Gamma>\<turnstile>Q\<^isub>2<:T\<^isub>2"
   920       { assume "\<exists>T\<^isub>1 T\<^isub>2. T = T\<^isub>1\<rightarrow>T\<^isub>2 \<and> \<Gamma> \<turnstile> T\<^isub>1 <: Q\<^isub>1 \<and> \<Gamma> \<turnstile> Q\<^isub>2 <: T\<^isub>2"
   978 	then obtain T\<^isub>1 T\<^isub>2 
   921 	then obtain T\<^isub>1 T\<^isub>2 
   979 	  where T_inst: "T = T\<^isub>1 \<rightarrow> T\<^isub>2" 
   922 	  where T_inst: "T = T\<^isub>1 \<rightarrow> T\<^isub>2" 
   980 	  and   rh_drv_prm\<^isub>1: "\<Gamma> \<turnstile> T\<^isub>1 <: Q\<^isub>1"
   923 	  and   rh_drv_prm\<^isub>1: "\<Gamma> \<turnstile> T\<^isub>1 <: Q\<^isub>1"
   981 	  and   rh_drv_prm\<^isub>2: "\<Gamma> \<turnstile> Q\<^isub>2 <: T\<^isub>2" by force
   924 	  and   rh_drv_prm\<^isub>2: "\<Gamma> \<turnstile> Q\<^isub>2 <: T\<^isub>2" by force
   982 	from IH_trans[of "Q\<^isub>1"] 
   925 	from IH_trans[of "Q\<^isub>1"] 
   983 	have "\<Gamma> \<turnstile> T\<^isub>1 <: S\<^isub>1" using Q\<^isub>1\<^isub>2_less rh_drv_prm\<^isub>1 lh_drv_prm\<^isub>1 by simp 
   926 	have "\<Gamma> \<turnstile> T\<^isub>1 <: S\<^isub>1" using Q\<^isub>1\<^isub>2_less rh_drv_prm\<^isub>1 lh_drv_prm\<^isub>1 by simp 
   984 	moreover
   927 	moreover
   985 	from IH_trans[of "Q\<^isub>2"] 
   928 	from IH_trans[of "Q\<^isub>2"] 
   986 	have "\<Gamma> \<turnstile> S\<^isub>2 <: T\<^isub>2" using Q\<^isub>1\<^isub>2_less rh_drv_prm\<^isub>2 lh_drv_prm\<^isub>2 by simp
   929 	have "\<Gamma> \<turnstile> S\<^isub>2 <: T\<^isub>2" using Q\<^isub>1\<^isub>2_less rh_drv_prm\<^isub>2 lh_drv_prm\<^isub>2 by simp
   987 	ultimately have "\<Gamma> \<turnstile> S\<^isub>1 \<rightarrow> S\<^isub>2 <: T\<^isub>1 \<rightarrow> T\<^isub>2" by (simp add: subtype_of.SA_arrow)
   930 	ultimately have "\<Gamma> \<turnstile> S\<^isub>1 \<rightarrow> S\<^isub>2 <: T\<^isub>1 \<rightarrow> T\<^isub>2" by auto
   988 	hence "\<Gamma> \<turnstile> S\<^isub>1 \<rightarrow> S\<^isub>2 <: T" using T_inst by simp
   931 	then have "\<Gamma> \<turnstile> S\<^isub>1 \<rightarrow> S\<^isub>2 <: T" using T_inst by simp
   989       }
   932       }
   990       ultimately show "\<Gamma> \<turnstile> S\<^isub>1 \<rightarrow> S\<^isub>2 <: T" by blast
   933       ultimately show "\<Gamma> \<turnstile> S\<^isub>1 \<rightarrow> S\<^isub>2 <: T" by blast
   991     next
   934     next
   992       case (SA_all \<Gamma> Q\<^isub>1 S\<^isub>1 X S\<^isub>2 Q\<^isub>2) 
   935       case (SA_all \<Gamma> Q\<^isub>1 S\<^isub>1 X S\<^isub>2 Q\<^isub>2) 
   993 	--{* \begin{minipage}[t]{0.9\textwidth}
   936       then have rh_drv: "\<Gamma> \<turnstile> (\<forall>X<:Q\<^isub>1. Q\<^isub>2) <: T" by simp
   994 	In this case the left-hand derivation is @{term "\<Gamma>\<turnstile>(\<forall>X<:S\<^isub>1. S\<^isub>2) <: (\<forall>X<:Q\<^isub>1. Q\<^isub>2)"} with 
       
   995 	@{term "(\<forall>X<:S\<^isub>1. S\<^isub>2)=S"} and @{term "(\<forall>X<:Q\<^isub>1. Q\<^isub>2)=Q"}. We therefore have the sub-derivations  
       
   996 	@{term "\<Gamma>\<turnstile>Q\<^isub>1<:S\<^isub>1"} and @{term "((TVarB X Q\<^isub>1)#\<Gamma>)\<turnstile>S\<^isub>2<:Q\<^isub>2"}. Since @{term "X"} is a binder, we
       
   997 	assume that it is sufficiently fresh; in particular we have the freshness conditions
       
   998 	@{term "X\<sharp>\<Gamma>"} and @{term "X\<sharp>Q\<^isub>1"} (these assumptions are provided by the strong 
       
   999 	induction-rule @{text "subtype_of_induct"}). We know that the @{text "size_ty"} of 
       
  1000 	@{term Q\<^isub>1} and @{term Q\<^isub>2} is smaller than that of @{term Q};
       
  1001 	so we can apply the outer induction hypotheses for @{term Q\<^isub>1} and @{term Q\<^isub>2}. 
       
  1002 	The right-hand derivation is @{term "\<Gamma> \<turnstile> (\<forall>X<:Q\<^isub>1. Q\<^isub>2) <: T"}. Since @{term "X\<sharp>\<Gamma>"} 
       
  1003 	and @{term "X\<sharp>Q\<^isub>1"} there exists types @{text "T\<^isub>1,T\<^isub>2"} such that 
       
  1004 	@{term "T=Top \<or> T=(\<forall>X<:T\<^isub>1. T\<^isub>2)"}. The @{term "Top"}-case is straightforward once we know 
       
  1005 	@{term "(\<forall>X<:S\<^isub>1. S\<^isub>2) closed_in \<Gamma>"} and @{term "\<turnstile> \<Gamma> ok"}. In the other case we have 
       
  1006 	the sub-derivations @{term "\<Gamma>\<turnstile>T\<^isub>1<:Q\<^isub>1"} and @{term "((TVarB X T\<^isub>1)#\<Gamma>)\<turnstile>Q\<^isub>2<:T\<^isub>2"}. Using the outer 
       
  1007 	induction hypothesis for transitivity we can derive @{term "\<Gamma>\<turnstile>T\<^isub>1<:S\<^isub>1"}. From the outer 
       
  1008 	induction for narrowing we get @{term "((TVarB X T\<^isub>1)#\<Gamma>) \<turnstile> S\<^isub>2 <: Q\<^isub>2"} and then using again 
       
  1009 	induction for transitivity we obtain @{term "((TVarB X T\<^isub>1)#\<Gamma>) \<turnstile> S\<^isub>2 <: T\<^isub>2"}. By rule 
       
  1010 	@{text "S_Forall"} and the freshness condition @{term "X\<sharp>\<Gamma>"} follows 
       
  1011 	@{term "\<Gamma> \<turnstile> (\<forall>X<:S\<^isub>1. S\<^isub>2) <: (\<forall>X<:T\<^isub>1. T\<^isub>2)"}, which is @{term "\<Gamma> \<turnstile> (\<forall>X<:S\<^isub>1. S\<^isub>2) <: T\<^isub>"}.
       
  1012 	\end{minipage}*}
       
  1013       hence rh_drv: "\<Gamma> \<turnstile> (\<forall>X<:Q\<^isub>1. Q\<^isub>2) <: T" by simp
       
  1014       have lh_drv_prm\<^isub>1: "\<Gamma> \<turnstile> Q\<^isub>1 <: S\<^isub>1" by fact
   937       have lh_drv_prm\<^isub>1: "\<Gamma> \<turnstile> Q\<^isub>1 <: S\<^isub>1" by fact
  1015       have lh_drv_prm\<^isub>2: "((TVarB X Q\<^isub>1)#\<Gamma>) \<turnstile> S\<^isub>2 <: Q\<^isub>2" by fact
   938       have lh_drv_prm\<^isub>2: "((TVarB X Q\<^isub>1)#\<Gamma>) \<turnstile> S\<^isub>2 <: Q\<^isub>2" by fact
  1016       then have "X\<sharp>\<Gamma>" by (force dest: subtype_implies_ok simp add: valid_ty_domain_fresh)
   939       then have "X\<sharp>\<Gamma>" by (force dest: subtype_implies_ok simp add: valid_ty_dom_fresh)
  1017       then have fresh_cond: "X\<sharp>\<Gamma>" "X\<sharp>Q\<^isub>1" using lh_drv_prm\<^isub>1 by (simp_all add: subtype_implies_fresh)
   940       then have fresh_cond: "X\<sharp>\<Gamma>" "X\<sharp>Q\<^isub>1" "X\<sharp>T" using rh_drv lh_drv_prm\<^isub>1 
  1018       from `(\<forall>X<:Q\<^isub>1. Q\<^isub>2) = Q` 
   941 	by (simp_all add: subtype_implies_fresh)
  1019       have Q\<^isub>1\<^isub>2_less: "size_ty Q\<^isub>1 < size_ty Q" "size_ty Q\<^isub>2 < size_ty Q " using fresh_cond by auto
       
  1020       from rh_drv 
   942       from rh_drv 
  1021       have "T=Top \<or> (\<exists>T\<^isub>1 T\<^isub>2. T=(\<forall>X<:T\<^isub>1. T\<^isub>2) \<and> \<Gamma>\<turnstile>T\<^isub>1<:Q\<^isub>1 \<and> ((TVarB X T\<^isub>1)#\<Gamma>)\<turnstile>Q\<^isub>2<:T\<^isub>2)" 
   943       have "T = Top \<or> 
       
   944           (\<exists>T\<^isub>1 T\<^isub>2. T = (\<forall>X<:T\<^isub>1. T\<^isub>2) \<and> \<Gamma> \<turnstile> T\<^isub>1 <: Q\<^isub>1 \<and> ((TVarB X T\<^isub>1)#\<Gamma>) \<turnstile> Q\<^isub>2 <: T\<^isub>2)" 
  1022 	using fresh_cond by (simp add: S_ForallE_left)
   945 	using fresh_cond by (simp add: S_ForallE_left)
  1023       moreover
   946       moreover
  1024       have "S\<^isub>1 closed_in \<Gamma>" and "S\<^isub>2 closed_in ((TVarB X Q\<^isub>1)#\<Gamma>)" 
   947       have "S\<^isub>1 closed_in \<Gamma>" and "S\<^isub>2 closed_in ((TVarB X Q\<^isub>1)#\<Gamma>)" 
  1025 	using lh_drv_prm\<^isub>1 lh_drv_prm\<^isub>2 by (simp_all add: subtype_implies_closed)
   948 	using lh_drv_prm\<^isub>1 lh_drv_prm\<^isub>2 by (simp_all add: subtype_implies_closed)
  1026       hence "(\<forall>X<:S\<^isub>1. S\<^isub>2) closed_in \<Gamma>" by (force simp add: closed_in_def ty.supp abs_supp)
   949       then have "(\<forall>X<:S\<^isub>1. S\<^isub>2) closed_in \<Gamma>" by (force simp add: closed_in_def ty.supp abs_supp)
  1027       moreover
   950       moreover
  1028       have "\<turnstile> \<Gamma> ok" using rh_drv by (rule subtype_implies_ok)
   951       have "\<turnstile> \<Gamma> ok" using rh_drv by (rule subtype_implies_ok)
  1029       moreover
   952       moreover
  1030       { assume "\<exists>T\<^isub>1 T\<^isub>2. T=(\<forall>X<:T\<^isub>1. T\<^isub>2) \<and> \<Gamma>\<turnstile>T\<^isub>1<:Q\<^isub>1 \<and> ((TVarB X T\<^isub>1)#\<Gamma>)\<turnstile>Q\<^isub>2<:T\<^isub>2"
   953       { assume "\<exists>T\<^isub>1 T\<^isub>2. T=(\<forall>X<:T\<^isub>1. T\<^isub>2) \<and> \<Gamma>\<turnstile>T\<^isub>1<:Q\<^isub>1 \<and> ((TVarB X T\<^isub>1)#\<Gamma>)\<turnstile>Q\<^isub>2<:T\<^isub>2"
  1031 	then obtain T\<^isub>1 T\<^isub>2 
   954 	then obtain T\<^isub>1 T\<^isub>2 
  1032 	  where T_inst: "T = (\<forall>X<:T\<^isub>1. T\<^isub>2)" 
   955 	  where T_inst: "T = (\<forall>X<:T\<^isub>1. T\<^isub>2)" 
  1033 	  and   rh_drv_prm\<^isub>1: "\<Gamma> \<turnstile> T\<^isub>1 <: Q\<^isub>1" 
   956 	  and   rh_drv_prm\<^isub>1: "\<Gamma> \<turnstile> T\<^isub>1 <: Q\<^isub>1" 
  1034 	  and   rh_drv_prm\<^isub>2:"((TVarB X T\<^isub>1)#\<Gamma>) \<turnstile> Q\<^isub>2 <: T\<^isub>2" by force
   957 	  and   rh_drv_prm\<^isub>2:"((TVarB X T\<^isub>1)#\<Gamma>) \<turnstile> Q\<^isub>2 <: T\<^isub>2" by force
       
   958 	have "(\<forall>X<:Q\<^isub>1. Q\<^isub>2) = Q" by fact 
       
   959 	then have Q\<^isub>1\<^isub>2_less: "size_ty Q\<^isub>1 < size_ty Q" "size_ty Q\<^isub>2 < size_ty Q" 
       
   960 	  using fresh_cond by auto
  1035 	from IH_trans[of "Q\<^isub>1"] 
   961 	from IH_trans[of "Q\<^isub>1"] 
  1036 	have "\<Gamma> \<turnstile> T\<^isub>1 <: S\<^isub>1" using lh_drv_prm\<^isub>1 rh_drv_prm\<^isub>1 Q\<^isub>1\<^isub>2_less by blast
   962 	have "\<Gamma> \<turnstile> T\<^isub>1 <: S\<^isub>1" using lh_drv_prm\<^isub>1 rh_drv_prm\<^isub>1 Q\<^isub>1\<^isub>2_less by blast
  1037 	moreover
   963 	moreover
  1038 	from IH_narrow[of "Q\<^isub>1" "[]"] 
   964 	from IH_narrow[of "Q\<^isub>1" "[]"] 
  1039 	have "((TVarB X T\<^isub>1)#\<Gamma>) \<turnstile> S\<^isub>2 <: Q\<^isub>2" using Q\<^isub>1\<^isub>2_less lh_drv_prm\<^isub>2 rh_drv_prm\<^isub>1 by simp
   965 	have "((TVarB X T\<^isub>1)#\<Gamma>) \<turnstile> S\<^isub>2 <: Q\<^isub>2" using Q\<^isub>1\<^isub>2_less lh_drv_prm\<^isub>2 rh_drv_prm\<^isub>1 by simp
  1043 	  using fresh_cond by (simp add: subtype_of.SA_all)
   969 	  using fresh_cond by (simp add: subtype_of.SA_all)
  1044 	hence "\<Gamma> \<turnstile> (\<forall>X<:S\<^isub>1. S\<^isub>2) <: T" using T_inst by simp
   970 	hence "\<Gamma> \<turnstile> (\<forall>X<:S\<^isub>1. S\<^isub>2) <: T" using T_inst by simp
  1045       }
   971       }
  1046       ultimately show "\<Gamma> \<turnstile> (\<forall>X<:S\<^isub>1. S\<^isub>2) <: T" by blast
   972       ultimately show "\<Gamma> \<turnstile> (\<forall>X<:S\<^isub>1. S\<^isub>2) <: T" by blast
  1047     qed
   973     qed
  1048   qed
   974   } note transitivity_lemma = this  
  1049 
   975 
  1050   { --{* The transitivity proof is now by the auxiliary lemma. *}
   976   { --{* The transitivity proof is now by the auxiliary lemma. *}
  1051     case 1 
   977     case 1 
  1052     from `\<Gamma> \<turnstile> S <: Q` and `\<Gamma> \<turnstile> Q <: T`
   978     from `\<Gamma> \<turnstile> S <: Q` and `\<Gamma> \<turnstile> Q <: T`
  1053     show "\<Gamma> \<turnstile> S <: T" by (rule transitivity_aux) 
   979     show "\<Gamma> \<turnstile> S <: T" by (rule transitivity_lemma) 
  1054   next 
   980   next 
  1055     --{* The narrowing proof proceeds by an induction over @{term "(\<Delta>@[(TVarB X Q)]@\<Gamma>) \<turnstile> M <: N"}. *}
       
  1056     case 2
   981     case 2
  1057     from `(\<Delta>@[(TVarB X Q)]@\<Gamma>) \<turnstile> M <: N` --{* left-hand derivation *}
   982     from `(\<Delta>@[(TVarB X Q)]@\<Gamma>) \<turnstile> M <: N` 
  1058       and `\<Gamma> \<turnstile> P<:Q` --{* right-hand derivation *}
   983       and `\<Gamma> \<turnstile> P<:Q` 
  1059     show "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> M <: N" 
   984     show "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> M <: N" 
  1060     proof (nominal_induct \<Gamma>\<equiv>"\<Delta>@[(TVarB X Q)]@\<Gamma>" M N avoiding: \<Delta> \<Gamma> X rule: subtype_of.strong_induct) 
   985     proof (induct \<Gamma>\<equiv>"\<Delta>@[(TVarB X Q)]@\<Gamma>" M N arbitrary: \<Gamma> X \<Delta> rule: subtype_of.induct) 
  1061       case (SA_Top _ S \<Delta> \<Gamma> X)
   986       case (SA_Top _ S \<Gamma> X \<Delta>)
  1062 	--{* \begin{minipage}[t]{0.9\textwidth}
   987       then have lh_drv_prm\<^isub>1: "\<turnstile> (\<Delta>@[(TVarB X Q)]@\<Gamma>) ok" 
  1063 	In this case the left-hand derivation is @{term "(\<Delta>@[(TVarB X Q)]@\<Gamma>) \<turnstile> S <: Top"}. We show
       
  1064 	that the context @{term "\<Delta>@[(TVarB X P)]@\<Gamma>"} is ok and that @{term S} is closed in 
       
  1065 	@{term "\<Delta>@[(TVarB X P)]@\<Gamma>"}. Then we can apply the @{text "S_Top"}-rule.\end{minipage}*}
       
  1066       hence lh_drv_prm\<^isub>1: "\<turnstile> (\<Delta>@[(TVarB X Q)]@\<Gamma>) ok" 
       
  1067 	and lh_drv_prm\<^isub>2: "S closed_in (\<Delta>@[(TVarB X Q)]@\<Gamma>)" by simp_all
   988 	and lh_drv_prm\<^isub>2: "S closed_in (\<Delta>@[(TVarB X Q)]@\<Gamma>)" by simp_all
  1068       have rh_drv: "\<Gamma> \<turnstile> P <: Q" by fact
   989       have rh_drv: "\<Gamma> \<turnstile> P <: Q" by fact
  1069       hence "P closed_in \<Gamma>" by (simp add: subtype_implies_closed)
   990       hence "P closed_in \<Gamma>" by (simp add: subtype_implies_closed)
  1070       with lh_drv_prm\<^isub>1 have "\<turnstile> (\<Delta>@[(TVarB X P)]@\<Gamma>) ok" by (simp add: replace_type)
   991       with lh_drv_prm\<^isub>1 have "\<turnstile> (\<Delta>@[(TVarB X P)]@\<Gamma>) ok" by (simp add: replace_type)
  1071       moreover
   992       moreover
  1072       from lh_drv_prm\<^isub>2 have "S closed_in (\<Delta>@[(TVarB X P)]@\<Gamma>)" 
   993       from lh_drv_prm\<^isub>2 have "S closed_in (\<Delta>@[(TVarB X P)]@\<Gamma>)" 
  1073 	by (simp add: closed_in_def domains_append)
   994 	by (simp add: closed_in_def doms_append)
  1074       ultimately show "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> S <: Top" by (simp add: subtype_of.SA_Top)
   995       ultimately show "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> S <: Top" by (simp add: subtype_of.SA_Top)
  1075     next
   996     next
  1076       case (SA_trans_TVar Y S _ N \<Delta> \<Gamma> X) 
   997       case (SA_trans_TVar Y S _ N \<Gamma> X \<Delta>) 
  1077 	--{* \begin{minipage}[t]{0.9\textwidth}
   998       then have IH_inner: "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> S <: N"
  1078 	In this case the left-hand derivation is @{term "(\<Delta>@[(TVarB X Q)]@\<Gamma>) \<turnstile> Tvar Y <: N"} and
       
  1079 	by inner induction hypothesis we have @{term "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> S <: N"}. We therefore 
       
  1080 	know that the contexts @{term "\<Delta>@[(TVarB X Q)]@\<Gamma>"} and @{term "\<Delta>@[(TVarB X P)]@\<Gamma>"} are ok, and that 
       
  1081 	@{term "(Y,S)"} is in @{term "\<Delta>@[(TVarB X Q)]@\<Gamma>"}. We need to show that 
       
  1082 	@{term "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> Tvar Y <: N"}  holds. In case @{term "X\<noteq>Y"} we know that 
       
  1083 	@{term "(Y,S)"} is in @{term "\<Delta>@[(TVarB X P)]@\<Gamma>"} and can use the inner induction hypothesis 
       
  1084 	and rule @{text "S_Var"} to conclude. In case @{term "X=Y"} we can infer that 
       
  1085 	@{term "S=Q"}; moreover we have that  @{term "(\<Delta>@[(TVarB X P)]@\<Gamma>) extends \<Gamma>"} and therefore 
       
  1086 	by @{text "weakening"} that @{term "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> P <: Q"} holds. By transitivity we 
       
  1087 	obtain then @{term "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> P <: N"} and can conclude by applying rule 
       
  1088 	@{text "S_Var"}.\end{minipage}*}
       
  1089       hence IH_inner: "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> S <: N"
       
  1090 	and lh_drv_prm: "(TVarB Y S) \<in> set (\<Delta>@[(TVarB X Q)]@\<Gamma>)"
   999 	and lh_drv_prm: "(TVarB Y S) \<in> set (\<Delta>@[(TVarB X Q)]@\<Gamma>)"
  1091 	and rh_drv: "\<Gamma> \<turnstile> P<:Q"
  1000 	and rh_drv: "\<Gamma> \<turnstile> P<:Q"
  1092 	and ok\<^isub>Q: "\<turnstile> (\<Delta>@[(TVarB X Q)]@\<Gamma>) ok" by (simp_all add: subtype_implies_ok)
  1001 	and ok\<^isub>Q: "\<turnstile> (\<Delta>@[(TVarB X Q)]@\<Gamma>) ok" by (simp_all add: subtype_implies_ok)
  1093       hence ok\<^isub>P: "\<turnstile> (\<Delta>@[(TVarB X P)]@\<Gamma>) ok" by (simp add: subtype_implies_ok) 
  1002       then have ok\<^isub>P: "\<turnstile> (\<Delta>@[(TVarB X P)]@\<Gamma>) ok" by (simp add: subtype_implies_ok) 
  1094       show "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> Tvar Y <: N"
  1003       show "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> Tvar Y <: N"
  1095       proof (cases "X=Y")
  1004       proof (cases "X=Y")
  1096 	case False
  1005 	case False
  1097 	have "X\<noteq>Y" by fact
  1006 	have "X\<noteq>Y" by fact
  1098 	hence "(TVarB Y S)\<in>set (\<Delta>@[(TVarB X P)]@\<Gamma>)" using lh_drv_prm by (simp add:binding.inject)
  1007 	hence "(TVarB Y S)\<in>set (\<Delta>@[(TVarB X P)]@\<Gamma>)" using lh_drv_prm by (simp add:binding.inject)
  1105 	hence "S=Q" using ok\<^isub>Q lh_drv_prm memb\<^isub>X\<^isub>Q by (simp only: uniqueness_of_ctxt)
  1014 	hence "S=Q" using ok\<^isub>Q lh_drv_prm memb\<^isub>X\<^isub>Q by (simp only: uniqueness_of_ctxt)
  1106 	hence "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> Q <: N" using IH_inner by simp
  1015 	hence "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> Q <: N" using IH_inner by simp
  1107 	moreover
  1016 	moreover
  1108 	have "(\<Delta>@[(TVarB X P)]@\<Gamma>) extends \<Gamma>" by (simp add: extends_def)
  1017 	have "(\<Delta>@[(TVarB X P)]@\<Gamma>) extends \<Gamma>" by (simp add: extends_def)
  1109 	hence "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> P <: Q" using rh_drv ok\<^isub>P by (simp only: weakening)
  1018 	hence "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> P <: Q" using rh_drv ok\<^isub>P by (simp only: weakening)
  1110 	ultimately have "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> P <: N" by (simp add: transitivity_aux) 
  1019 	ultimately have "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> P <: N" by (simp add: transitivity_lemma) 
  1111 	thus "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> Tvar Y <: N" using memb\<^isub>X\<^isub>P eq by (simp only: subtype_of.SA_trans_TVar)
  1020 	then show "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> Tvar Y <: N" using memb\<^isub>X\<^isub>P eq by auto
  1112       qed
  1021       qed
  1113     next
  1022     next
  1114       case (SA_refl_TVar _ Y \<Delta> \<Gamma> X)
  1023       case (SA_refl_TVar _ Y \<Gamma> X \<Delta>)
  1115 	--{* \begin{minipage}[t]{0.9\textwidth}
  1024       then have lh_drv_prm\<^isub>1: "\<turnstile> (\<Delta>@[(TVarB X Q)]@\<Gamma>) ok" 
  1116 	In this case the left-hand derivation is @{term "(\<Delta>@[(TVarB X Q)]@\<Gamma>) \<turnstile> Tvar Y <: Tvar Y"} and we
  1025 	and lh_drv_prm\<^isub>2: "Y \<in> ty_dom (\<Delta>@[(TVarB X Q)]@\<Gamma>)" by simp_all
  1117 	therefore know that @{term "\<Delta>@[(TVarB X Q)]@\<Gamma>"} is ok and that @{term "Y"} is in 
       
  1118 	the domain of @{term "\<Delta>@[(TVarB X Q)]@\<Gamma>"}. We therefore know that @{term "\<Delta>@[(TVarB X P)]@\<Gamma>"} is ok
       
  1119 	and that @{term Y} is in the domain of @{term "\<Delta>@[(TVarB X P)]@\<Gamma>"}. We can conclude by applying 
       
  1120 	rule @{text "S_Refl"}.\end{minipage}*}
       
  1121       hence lh_drv_prm\<^isub>1: "\<turnstile> (\<Delta>@[(TVarB X Q)]@\<Gamma>) ok" 
       
  1122 	and lh_drv_prm\<^isub>2: "Y \<in> ty_domain (\<Delta>@[(TVarB X Q)]@\<Gamma>)" by simp_all
       
  1123       have "\<Gamma> \<turnstile> P <: Q" by fact
  1026       have "\<Gamma> \<turnstile> P <: Q" by fact
  1124       hence "P closed_in \<Gamma>" by (simp add: subtype_implies_closed)
  1027       hence "P closed_in \<Gamma>" by (simp add: subtype_implies_closed)
  1125       with lh_drv_prm\<^isub>1 have "\<turnstile> (\<Delta>@[(TVarB X P)]@\<Gamma>) ok" by (simp add: replace_type)
  1028       with lh_drv_prm\<^isub>1 have "\<turnstile> (\<Delta>@[(TVarB X P)]@\<Gamma>) ok" by (simp add: replace_type)
  1126       moreover
  1029       moreover
  1127       from lh_drv_prm\<^isub>2 have "Y \<in> ty_domain (\<Delta>@[(TVarB X P)]@\<Gamma>)" by (simp add: domains_append)
  1030       from lh_drv_prm\<^isub>2 have "Y \<in> ty_dom (\<Delta>@[(TVarB X P)]@\<Gamma>)" by (simp add: doms_append)
  1128       ultimately show "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> Tvar Y <: Tvar Y" by (simp add: subtype_of.SA_refl_TVar)
  1031       ultimately show "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> Tvar Y <: Tvar Y" by (simp add: subtype_of.SA_refl_TVar)
  1129     next
  1032     next
  1130       case (SA_arrow _ S\<^isub>1 Q\<^isub>1 Q\<^isub>2 S\<^isub>2 \<Delta> \<Gamma> X) 
  1033       case (SA_arrow _ S\<^isub>1 Q\<^isub>1 Q\<^isub>2 S\<^isub>2 \<Gamma> X \<Delta>) 
  1131 	--{* \begin{minipage}[t]{0.9\textwidth}
  1034       then show "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> Q\<^isub>1 \<rightarrow> Q\<^isub>2 <: S\<^isub>1 \<rightarrow> S\<^isub>2" by blast 
  1132 	In this case the left-hand derivation is @{term "(\<Delta>@[(TVarB X Q)]@\<Gamma>) \<turnstile> Q\<^isub>1 \<rightarrow> Q\<^isub>2 <: S\<^isub>1 \<rightarrow> S\<^isub>2"} 
       
  1133 	and the proof is trivial.\end{minipage}*}
       
  1134       thus "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> Q\<^isub>1 \<rightarrow> Q\<^isub>2 <: S\<^isub>1 \<rightarrow> S\<^isub>2" by blast 
       
  1135     next
  1035     next
  1136       case (SA_all \<Gamma>' T\<^isub>1 S\<^isub>1 Y S\<^isub>2 T\<^isub>2 \<Delta> \<Gamma> X)
  1036       case (SA_all _ T\<^isub>1 S\<^isub>1 Y S\<^isub>2 T\<^isub>2 \<Gamma> X \<Delta>)
  1137 	--{* \begin{minipage}[t]{0.9\textwidth}
  1037       from SA_all(2,4,5,6)
  1138 	In this case the left-hand derivation is @{term "(\<Delta>@[(TVarB X Q)]@\<Gamma>) \<turnstile> (\<forall>Y<:S\<^isub>1. S\<^isub>2) <: (\<forall>Y<:T\<^isub>1. T\<^isub>2)"}
  1038       have IH_inner\<^isub>1: "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> T\<^isub>1 <: S\<^isub>1" 
  1139 	and therfore we know that the binder @{term Y} is fresh for @{term "\<Delta>@[(TVarB X Q)]@\<Gamma>"}. By
  1039 	and IH_inner\<^isub>2: "(((TVarB Y T\<^isub>1)#\<Delta>)@[(TVarB X P)]@\<Gamma>) \<turnstile> S\<^isub>2 <: T\<^isub>2" by force+
  1140 	the inner induction hypothesis we have that @{term "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> T\<^isub>1 <: S\<^isub>1"} and 
  1040       then show "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> (\<forall>Y<:S\<^isub>1. S\<^isub>2) <: (\<forall>Y<:T\<^isub>1. T\<^isub>2)" by auto
  1141 	@{term "((TVarB Y T\<^isub>1)#\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> S\<^isub>2 <: T\<^isub>2"}. Since @{term P} is a subtype of @{term Q}
       
  1142 	we can infer that @{term Y} is fresh for @{term P} and thus also fresh for 
       
  1143 	@{term "\<Delta>@[(TVarB X P)]@\<Gamma>"}. We can then conclude by applying rule @{text "S_Forall"}.
       
  1144 	\end{minipage}*}
       
  1145       hence rh_drv: "\<Gamma> \<turnstile> P <: Q"
       
  1146 	and IH_inner\<^isub>1: "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> T\<^isub>1 <: S\<^isub>1" 
       
  1147 	and "TVarB Y T\<^isub>1 # \<Gamma>' = ((TVarB Y T\<^isub>1)#\<Delta>) @ [TVarB X Q] @ \<Gamma>" by auto
       
  1148       moreover have " \<lbrakk>\<Gamma>\<turnstile>P<:Q; TVarB Y T\<^isub>1 # \<Gamma>' = ((TVarB Y T\<^isub>1)#\<Delta>) @ [TVarB X Q] @ \<Gamma>\<rbrakk> \<Longrightarrow> (((TVarB Y T\<^isub>1)#\<Delta>) @ [TVarB X P] @ \<Gamma>)\<turnstile>S\<^isub>2<:T\<^isub>2" by fact
       
  1149       ultimately have IH_inner\<^isub>2: "(((TVarB Y T\<^isub>1)#\<Delta>) @ [TVarB X P] @ \<Gamma>)\<turnstile>S\<^isub>2<:T\<^isub>2" by auto
       
  1150       with IH_inner\<^isub>1 IH_inner\<^isub>2 
       
  1151       show "(\<Delta>@[(TVarB X P)]@\<Gamma>) \<turnstile> (\<forall>Y<:S\<^isub>1. S\<^isub>2) <: (\<forall>Y<:T\<^isub>1. T\<^isub>2)" by (simp add: subtype_of.SA_all)
       
  1152     qed
  1041     qed
  1153   } 
  1042   } 
  1154 qed
  1043 qed
  1155 
  1044 
  1156 section {* Typing *}
  1045 section {* Typing *}
  1161   T_Var[intro]: "\<lbrakk> VarB x T \<in> set \<Gamma>; \<turnstile> \<Gamma> ok \<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> Var x : T"
  1050   T_Var[intro]: "\<lbrakk> VarB x T \<in> set \<Gamma>; \<turnstile> \<Gamma> ok \<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> Var x : T"
  1162 | T_App[intro]: "\<lbrakk> \<Gamma> \<turnstile> t\<^isub>1 : T\<^isub>1 \<rightarrow> T\<^isub>2; \<Gamma> \<turnstile> t\<^isub>2 : T\<^isub>1 \<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> t\<^isub>1 \<cdot> t\<^isub>2 : T\<^isub>2"
  1051 | T_App[intro]: "\<lbrakk> \<Gamma> \<turnstile> t\<^isub>1 : T\<^isub>1 \<rightarrow> T\<^isub>2; \<Gamma> \<turnstile> t\<^isub>2 : T\<^isub>1 \<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> t\<^isub>1 \<cdot> t\<^isub>2 : T\<^isub>2"
  1163 | T_Abs[intro]: "\<lbrakk> VarB x T\<^isub>1 # \<Gamma> \<turnstile> t\<^isub>2 : T\<^isub>2 \<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> (\<lambda>x:T\<^isub>1. t\<^isub>2) : T\<^isub>1 \<rightarrow> T\<^isub>2"
  1052 | T_Abs[intro]: "\<lbrakk> VarB x T\<^isub>1 # \<Gamma> \<turnstile> t\<^isub>2 : T\<^isub>2 \<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> (\<lambda>x:T\<^isub>1. t\<^isub>2) : T\<^isub>1 \<rightarrow> T\<^isub>2"
  1164 | T_Sub[intro]: "\<lbrakk> \<Gamma> \<turnstile> t : S; \<Gamma> \<turnstile> S <: T \<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> t : T"
  1053 | T_Sub[intro]: "\<lbrakk> \<Gamma> \<turnstile> t : S; \<Gamma> \<turnstile> S <: T \<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> t : T"
  1165 | T_TAbs[intro]:"\<lbrakk> TVarB X T\<^isub>1 # \<Gamma> \<turnstile> t\<^isub>2 : T\<^isub>2 \<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> (\<lambda>X<:T\<^isub>1. t\<^isub>2) : (\<forall>X<:T\<^isub>1. T\<^isub>2)"
  1054 | T_TAbs[intro]:"\<lbrakk> TVarB X T\<^isub>1 # \<Gamma> \<turnstile> t\<^isub>2 : T\<^isub>2 \<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> (\<lambda>X<:T\<^isub>1. t\<^isub>2) : (\<forall>X<:T\<^isub>1. T\<^isub>2)"
  1166 | T_TApp[intro]:"\<lbrakk> X \<sharp> (\<Gamma>, t\<^isub>1, T\<^isub>2); \<Gamma> \<turnstile> t\<^isub>1 : (\<forall>X<:T\<^isub>1\<^isub>1. T\<^isub>1\<^isub>2); \<Gamma> \<turnstile> T\<^isub>2 <: T\<^isub>1\<^isub>1 \<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> t\<^isub>1 \<cdot>\<^sub>\<tau> T\<^isub>2 : (T\<^isub>1\<^isub>2[X \<mapsto> T\<^isub>2]\<^sub>\<tau>)" 
  1055 | T_TApp[intro]:"\<lbrakk>X\<sharp>(\<Gamma>,t\<^isub>1,T\<^isub>2); \<Gamma> \<turnstile> t\<^isub>1 : (\<forall>X<:T\<^isub>1\<^isub>1. T\<^isub>1\<^isub>2); \<Gamma> \<turnstile> T\<^isub>2 <: T\<^isub>1\<^isub>1\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> t\<^isub>1 \<cdot>\<^sub>\<tau> T\<^isub>2 : (T\<^isub>1\<^isub>2[X \<mapsto> T\<^isub>2]\<^sub>\<tau>)" 
  1167 
  1056 
  1168 equivariance typing
  1057 equivariance typing
  1169 
  1058 
  1170 lemma better_T_TApp:
  1059 lemma better_T_TApp:
  1171   assumes H1: "\<Gamma> \<turnstile> t\<^isub>1 : (\<forall>X<:T11. T12)"
  1060   assumes H1: "\<Gamma> \<turnstile> t\<^isub>1 : (\<forall>X<:T11. T12)"
  1187   assumes "\<Gamma> \<turnstile> t : T"
  1076   assumes "\<Gamma> \<turnstile> t : T"
  1188   shows   "\<turnstile> \<Gamma> ok"
  1077   shows   "\<turnstile> \<Gamma> ok"
  1189 using assms by (induct, auto)
  1078 using assms by (induct, auto)
  1190 
  1079 
  1191 nominal_inductive typing
  1080 nominal_inductive typing
  1192 by (auto dest!: typing_ok intro: closed_in_fresh fresh_domain type_subst_fresh
  1081 by (auto dest!: typing_ok intro: closed_in_fresh fresh_dom type_subst_fresh
  1193     simp: abs_fresh fresh_type_subst_fresh ty_vrs_fresh valid_ty_domain_fresh fresh_trm_domain)
  1082     simp: abs_fresh fresh_type_subst_fresh ty_vrs_fresh valid_ty_dom_fresh fresh_trm_dom)
  1194 
  1083 
  1195 lemma ok_imp_VarB_closed_in:
  1084 lemma ok_imp_VarB_closed_in:
  1196   assumes ok: "\<turnstile> \<Gamma> ok"
  1085   assumes ok: "\<turnstile> \<Gamma> ok"
  1197   shows "VarB x T \<in> set \<Gamma> \<Longrightarrow> T closed_in \<Gamma>" using ok
  1086   shows "VarB x T \<in> set \<Gamma> \<Longrightarrow> T closed_in \<Gamma>" using ok
  1198   by induct (auto simp add: binding.inject closed_in_def)
  1087   by induct (auto simp add: binding.inject closed_in_def)
  1199 
  1088 
  1200 lemma tyvrs_of_subst: "tyvrs_of (B[X \<mapsto> T]\<^sub>b) = tyvrs_of B"
  1089 lemma tyvrs_of_subst: "tyvrs_of (B[X \<mapsto> T]\<^sub>b) = tyvrs_of B"
  1201   by (nominal_induct B rule: binding.strong_induct) simp_all
  1090   by (nominal_induct B rule: binding.strong_induct) simp_all
  1202 
  1091 
  1203 lemma ty_domain_subst: "ty_domain (\<Gamma>[X \<mapsto> T]\<^sub>e) = ty_domain \<Gamma>"
  1092 lemma ty_dom_subst: "ty_dom (\<Gamma>[X \<mapsto> T]\<^sub>e) = ty_dom \<Gamma>"
  1204   by (induct \<Gamma>) (simp_all add: tyvrs_of_subst)
  1093   by (induct \<Gamma>) (simp_all add: tyvrs_of_subst)
  1205 
  1094 
  1206 lemma vrs_of_subst: "vrs_of (B[X \<mapsto> T]\<^sub>b) = vrs_of B"
  1095 lemma vrs_of_subst: "vrs_of (B[X \<mapsto> T]\<^sub>b) = vrs_of B"
  1207   by (nominal_induct B rule: binding.strong_induct) simp_all
  1096   by (nominal_induct B rule: binding.strong_induct) simp_all
  1208 
  1097 
  1209 lemma trm_domain_subst: "trm_domain (\<Gamma>[X \<mapsto> T]\<^sub>e) = trm_domain \<Gamma>"
  1098 lemma trm_dom_subst: "trm_dom (\<Gamma>[X \<mapsto> T]\<^sub>e) = trm_dom \<Gamma>"
  1210   by (induct \<Gamma>) (simp_all add: vrs_of_subst)
  1099   by (induct \<Gamma>) (simp_all add: vrs_of_subst)
  1211 
  1100 
  1212 lemma subst_closed_in:
  1101 lemma subst_closed_in:
  1213   "T closed_in (\<Delta> @ TVarB X S # \<Gamma>) \<Longrightarrow> U closed_in \<Gamma> \<Longrightarrow> T[X \<mapsto> U]\<^sub>\<tau> closed_in (\<Delta>[X \<mapsto> U]\<^sub>e @ \<Gamma>)"
  1102   "T closed_in (\<Delta> @ TVarB X S # \<Gamma>) \<Longrightarrow> U closed_in \<Gamma> \<Longrightarrow> T[X \<mapsto> U]\<^sub>\<tau> closed_in (\<Delta>[X \<mapsto> U]\<^sub>e @ \<Gamma>)"
  1214   apply (nominal_induct T avoiding: X U \<Gamma> rule: ty.strong_induct)
  1103   apply (nominal_induct T avoiding: X U \<Gamma> rule: ty.strong_induct)
  1215   apply (simp add: closed_in_def ty.supp supp_atm domains_append ty_domain_subst)
  1104   apply (simp add: closed_in_def ty.supp supp_atm doms_append ty_dom_subst)
  1216   apply blast
  1105   apply blast
  1217   apply (simp add: closed_in_def ty.supp)
  1106   apply (simp add: closed_in_def ty.supp)
  1218   apply (simp add: closed_in_def ty.supp)
  1107   apply (simp add: closed_in_def ty.supp)
  1219   apply (simp add: closed_in_def ty.supp abs_supp)
  1108   apply (simp add: closed_in_def ty.supp abs_supp)
  1220   apply (drule_tac x = X in meta_spec)
  1109   apply (drule_tac x = X in meta_spec)
  1221   apply (drule_tac x = U in meta_spec)
  1110   apply (drule_tac x = U in meta_spec)
  1222   apply (drule_tac x = "(TVarB tyvrs ty2) # \<Gamma>" in meta_spec)
  1111   apply (drule_tac x = "(TVarB tyvrs ty2) # \<Gamma>" in meta_spec)
  1223   apply (simp add: domains_append ty_domain_subst)
  1112   apply (simp add: doms_append ty_dom_subst)
  1224   apply blast
  1113   apply blast
  1225   done
  1114   done
  1226 
  1115 
  1227 lemmas subst_closed_in' = subst_closed_in [where \<Delta>="[]", simplified]
  1116 lemmas subst_closed_in' = subst_closed_in [where \<Delta>="[]", simplified]
  1228 
  1117 
  1321 inductive_cases eval_inv_auto[elim]: 
  1210 inductive_cases eval_inv_auto[elim]: 
  1322   "Var x \<longmapsto> t'" 
  1211   "Var x \<longmapsto> t'" 
  1323   "(\<lambda>x:T. t) \<longmapsto> t'" 
  1212   "(\<lambda>x:T. t) \<longmapsto> t'" 
  1324   "(\<lambda>X<:T. t) \<longmapsto> t'" 
  1213   "(\<lambda>X<:T. t) \<longmapsto> t'" 
  1325 
  1214 
  1326 lemma ty_domain_cons:
  1215 lemma ty_dom_cons:
  1327   shows "ty_domain (\<Gamma>@[VarB X Q]@\<Delta>) = ty_domain (\<Gamma>@\<Delta>)"
  1216   shows "ty_dom (\<Gamma>@[VarB X Q]@\<Delta>) = ty_dom (\<Gamma>@\<Delta>)"
  1328 by (induct \<Gamma>, auto)
  1217 by (induct \<Gamma>, auto)
  1329 
  1218 
  1330 lemma closed_in_cons: 
  1219 lemma closed_in_cons: 
  1331   assumes "S closed_in (\<Gamma> @ VarB X Q # \<Delta>)"
  1220   assumes "S closed_in (\<Gamma> @ VarB X Q # \<Delta>)"
  1332   shows "S closed_in (\<Gamma>@\<Delta>)"
  1221   shows "S closed_in (\<Gamma>@\<Delta>)"
  1333 using assms ty_domain_cons closed_in_def by auto
  1222 using assms ty_dom_cons closed_in_def by auto
  1334 
  1223 
  1335 lemma closed_in_weaken: "T closed_in (\<Delta> @ \<Gamma>) \<Longrightarrow> T closed_in (\<Delta> @ B # \<Gamma>)"
  1224 lemma closed_in_weaken: "T closed_in (\<Delta> @ \<Gamma>) \<Longrightarrow> T closed_in (\<Delta> @ B # \<Gamma>)"
  1336   by (auto simp add: closed_in_def domains_append)
  1225   by (auto simp add: closed_in_def doms_append)
  1337 
  1226 
  1338 lemma closed_in_weaken': "T closed_in \<Gamma> \<Longrightarrow> T closed_in (\<Delta> @ \<Gamma>)"
  1227 lemma closed_in_weaken': "T closed_in \<Gamma> \<Longrightarrow> T closed_in (\<Delta> @ \<Gamma>)"
  1339   by (auto simp add: closed_in_def domains_append)
  1228   by (auto simp add: closed_in_def doms_append)
  1340 
  1229 
  1341 lemma valid_subst:
  1230 lemma valid_subst:
  1342   assumes ok: "\<turnstile> (\<Delta> @ TVarB X Q # \<Gamma>) ok"
  1231   assumes ok: "\<turnstile> (\<Delta> @ TVarB X Q # \<Gamma>) ok"
  1343   and closed: "P closed_in \<Gamma>"
  1232   and closed: "P closed_in \<Gamma>"
  1344   shows "\<turnstile> (\<Delta>[X \<mapsto> P]\<^sub>e @ \<Gamma>) ok" using ok closed
  1233   shows "\<turnstile> (\<Delta>[X \<mapsto> P]\<^sub>e @ \<Gamma>) ok" using ok closed
  1348   apply assumption
  1237   apply assumption
  1349   apply (erule validE)
  1238   apply (erule validE)
  1350   apply simp
  1239   apply simp
  1351   apply (rule valid_consT)
  1240   apply (rule valid_consT)
  1352   apply assumption
  1241   apply assumption
  1353   apply (simp add: domains_append ty_domain_subst)
  1242   apply (simp add: doms_append ty_dom_subst)
  1354   apply (simp add: fresh_fin_insert [OF pt_tyvrs_inst at_tyvrs_inst fs_tyvrs_inst] finite_domains)
  1243   apply (simp add: fresh_fin_insert [OF pt_tyvrs_inst at_tyvrs_inst fs_tyvrs_inst] finite_doms)
  1355   apply (rule_tac S=Q in subst_closed_in')
  1244   apply (rule_tac S=Q in subst_closed_in')
  1356   apply (simp add: closed_in_def domains_append ty_domain_subst)
  1245   apply (simp add: closed_in_def doms_append ty_dom_subst)
  1357   apply (simp add: closed_in_def domains_append)
  1246   apply (simp add: closed_in_def doms_append)
  1358   apply blast
  1247   apply blast
  1359   apply simp
  1248   apply simp
  1360   apply (rule valid_cons)
  1249   apply (rule valid_cons)
  1361   apply assumption
  1250   apply assumption
  1362   apply (simp add: domains_append trm_domain_subst)
  1251   apply (simp add: doms_append trm_dom_subst)
  1363   apply (rule_tac S=Q in subst_closed_in')
  1252   apply (rule_tac S=Q in subst_closed_in')
  1364   apply (simp add: closed_in_def domains_append ty_domain_subst)
  1253   apply (simp add: closed_in_def doms_append ty_dom_subst)
  1365   apply (simp add: closed_in_def domains_append)
  1254   apply (simp add: closed_in_def doms_append)
  1366   apply blast
  1255   apply blast
  1367   done
  1256   done
  1368 
  1257 
  1369 lemma ty_domain_vrs:
  1258 lemma ty_dom_vrs:
  1370   shows "ty_domain (G @ [VarB x Q] @ D) = ty_domain (G @ D)"
  1259   shows "ty_dom (G @ [VarB x Q] @ D) = ty_dom (G @ D)"
  1371 by (induct G, auto)
  1260 by (induct G, auto)
  1372 
  1261 
  1373 lemma valid_cons':
  1262 lemma valid_cons':
  1374   assumes "\<turnstile> (\<Gamma> @ VarB x Q # \<Delta>) ok"
  1263   assumes "\<turnstile> (\<Gamma> @ VarB x Q # \<Delta>) ok"
  1375   shows "\<turnstile> (\<Gamma> @ \<Delta>) ok"
  1264   shows "\<turnstile> (\<Gamma> @ \<Delta>) ok"
  1387     with valid_consT show ?thesis by simp
  1276     with valid_consT show ?thesis by simp
  1388   next
  1277   next
  1389     case (Cons b bs)
  1278     case (Cons b bs)
  1390     with valid_consT
  1279     with valid_consT
  1391     have "\<turnstile> (bs @ \<Delta>) ok" by simp
  1280     have "\<turnstile> (bs @ \<Delta>) ok" by simp
  1392     moreover from Cons and valid_consT have "X \<sharp> ty_domain (bs @ \<Delta>)"
  1281     moreover from Cons and valid_consT have "X \<sharp> ty_dom (bs @ \<Delta>)"
  1393       by (simp add: domains_append)
  1282       by (simp add: doms_append)
  1394     moreover from Cons and valid_consT have "T closed_in (bs @ \<Delta>)"
  1283     moreover from Cons and valid_consT have "T closed_in (bs @ \<Delta>)"
  1395       by (simp add: closed_in_def domains_append)
  1284       by (simp add: closed_in_def doms_append)
  1396     ultimately have "\<turnstile> (TVarB X T # bs @ \<Delta>) ok"
  1285     ultimately have "\<turnstile> (TVarB X T # bs @ \<Delta>) ok"
  1397       by (rule valid_rel.valid_consT)
  1286       by (rule valid_rel.valid_consT)
  1398     with Cons and valid_consT show ?thesis by simp
  1287     with Cons and valid_consT show ?thesis by simp
  1399   qed
  1288   qed
  1400 next
  1289 next
  1405     with valid_cons show ?thesis by simp
  1294     with valid_cons show ?thesis by simp
  1406   next
  1295   next
  1407     case (Cons b bs)
  1296     case (Cons b bs)
  1408     with valid_cons
  1297     with valid_cons
  1409     have "\<turnstile> (bs @ \<Delta>) ok" by simp
  1298     have "\<turnstile> (bs @ \<Delta>) ok" by simp
  1410     moreover from Cons and valid_cons have "x \<sharp> trm_domain (bs @ \<Delta>)"
  1299     moreover from Cons and valid_cons have "x \<sharp> trm_dom (bs @ \<Delta>)"
  1411       by (simp add: domains_append finite_domains
  1300       by (simp add: doms_append finite_doms
  1412 	fresh_fin_insert [OF pt_vrs_inst at_vrs_inst fs_vrs_inst])
  1301 	fresh_fin_insert [OF pt_vrs_inst at_vrs_inst fs_vrs_inst])
  1413     moreover from Cons and valid_cons have "T closed_in (bs @ \<Delta>)"
  1302     moreover from Cons and valid_cons have "T closed_in (bs @ \<Delta>)"
  1414       by (simp add: closed_in_def domains_append)
  1303       by (simp add: closed_in_def doms_append)
  1415     ultimately have "\<turnstile> (VarB x T # bs @ \<Delta>) ok"
  1304     ultimately have "\<turnstile> (VarB x T # bs @ \<Delta>) ok"
  1416       by (rule valid_rel.valid_cons)
  1305       by (rule valid_rel.valid_cons)
  1417     with Cons and valid_cons show ?thesis by simp
  1306     with Cons and valid_cons show ?thesis by simp
  1418   qed
  1307   qed
  1419 qed
  1308 qed
  1437   then have closed: "T\<^isub>1 closed_in (\<Delta> @ \<Gamma>)"
  1326   then have closed: "T\<^isub>1 closed_in (\<Delta> @ \<Gamma>)"
  1438     by (auto dest: typing_ok)
  1327     by (auto dest: typing_ok)
  1439   have "\<turnstile> (VarB y T\<^isub>1 # \<Delta> @ B # \<Gamma>) ok"
  1328   have "\<turnstile> (VarB y T\<^isub>1 # \<Delta> @ B # \<Gamma>) ok"
  1440     apply (rule valid_cons)
  1329     apply (rule valid_cons)
  1441     apply (rule T_Abs)
  1330     apply (rule T_Abs)
  1442     apply (simp add: domains_append
  1331     apply (simp add: doms_append
  1443       fresh_fin_insert [OF pt_vrs_inst at_vrs_inst fs_vrs_inst]
  1332       fresh_fin_insert [OF pt_vrs_inst at_vrs_inst fs_vrs_inst]
  1444       fresh_fin_union [OF pt_vrs_inst at_vrs_inst fs_vrs_inst]
  1333       fresh_fin_union [OF pt_vrs_inst at_vrs_inst fs_vrs_inst]
  1445       finite_domains finite_vrs fresh_vrs_of T_Abs fresh_trm_domain)
  1334       finite_doms finite_vrs fresh_vrs_of T_Abs fresh_trm_dom)
  1446     apply (rule closed_in_weaken)
  1335     apply (rule closed_in_weaken)
  1447     apply (rule closed)
  1336     apply (rule closed)
  1448     done
  1337     done
  1449   then have "\<turnstile> ((VarB y T\<^isub>1 # \<Delta>) @ B # \<Gamma>) ok" by simp
  1338   then have "\<turnstile> ((VarB y T\<^isub>1 # \<Delta>) @ B # \<Gamma>) ok" by simp
  1450   then have "(VarB y T\<^isub>1 # \<Delta>) @ B # \<Gamma> \<turnstile> t\<^isub>2 : T\<^isub>2"
  1339   then have "(VarB y T\<^isub>1 # \<Delta>) @ B # \<Gamma> \<turnstile> t\<^isub>2 : T\<^isub>2"
  1465   then have closed: "T\<^isub>1 closed_in (\<Delta> @ \<Gamma>)"
  1354   then have closed: "T\<^isub>1 closed_in (\<Delta> @ \<Gamma>)"
  1466     by (auto dest: typing_ok)
  1355     by (auto dest: typing_ok)
  1467   have "\<turnstile> (TVarB X T\<^isub>1 # \<Delta> @ B # \<Gamma>) ok"
  1356   have "\<turnstile> (TVarB X T\<^isub>1 # \<Delta> @ B # \<Gamma>) ok"
  1468     apply (rule valid_consT)
  1357     apply (rule valid_consT)
  1469     apply (rule T_TAbs)
  1358     apply (rule T_TAbs)
  1470     apply (simp add: domains_append
  1359     apply (simp add: doms_append
  1471       fresh_fin_insert [OF pt_tyvrs_inst at_tyvrs_inst fs_tyvrs_inst]
  1360       fresh_fin_insert [OF pt_tyvrs_inst at_tyvrs_inst fs_tyvrs_inst]
  1472       fresh_fin_union [OF pt_tyvrs_inst at_tyvrs_inst fs_tyvrs_inst]
  1361       fresh_fin_union [OF pt_tyvrs_inst at_tyvrs_inst fs_tyvrs_inst]
  1473       finite_domains finite_vrs tyvrs_fresh T_TAbs fresh_domain)
  1362       finite_doms finite_vrs tyvrs_fresh T_TAbs fresh_dom)
  1474     apply (rule closed_in_weaken)
  1363     apply (rule closed_in_weaken)
  1475     apply (rule closed)
  1364     apply (rule closed)
  1476     done
  1365     done
  1477   then have "\<turnstile> ((TVarB X T\<^isub>1 # \<Delta>) @ B # \<Gamma>) ok" by simp
  1366   then have "\<turnstile> ((TVarB X T\<^isub>1 # \<Delta>) @ B # \<Gamma>) ok" by simp
  1478   then have "(TVarB X T\<^isub>1 # \<Delta>) @ B # \<Gamma> \<turnstile> t\<^isub>2 : T\<^isub>2"
  1367   then have "(TVarB X T\<^isub>1 # \<Delta>) @ B # \<Gamma> \<turnstile> t\<^isub>2 : T\<^isub>2"
  1511   ultimately show ?case using subtype_of.SA_Top by auto
  1400   ultimately show ?case using subtype_of.SA_Top by auto
  1512 next
  1401 next
  1513   case (SA_refl_TVar G X' G')
  1402   case (SA_refl_TVar G X' G')
  1514   then have "\<turnstile> (G' @ VarB x Q # \<Delta>) ok" by simp
  1403   then have "\<turnstile> (G' @ VarB x Q # \<Delta>) ok" by simp
  1515   then have h1:"\<turnstile> (G' @ \<Delta>) ok" by (auto dest: valid_cons')
  1404   then have h1:"\<turnstile> (G' @ \<Delta>) ok" by (auto dest: valid_cons')
  1516   have "X' \<in> ty_domain (G' @ VarB x Q # \<Delta>)" using SA_refl_TVar by auto
  1405   have "X' \<in> ty_dom (G' @ VarB x Q # \<Delta>)" using SA_refl_TVar by auto
  1517   then have h2:"X' \<in> ty_domain (G' @ \<Delta>)" using ty_domain_vrs by auto
  1406   then have h2:"X' \<in> ty_dom (G' @ \<Delta>)" using ty_dom_vrs by auto
  1518   show ?case using h1 h2 by auto
  1407   show ?case using h1 h2 by auto
  1519 next
  1408 next
  1520   case (SA_all G T1 S1 X S2 T2 G')
  1409   case (SA_all G T1 S1 X S2 T2 G')
  1521   have ih1:"TVarB X T1 # G = (TVarB X T1 # G') @ VarB x Q # \<Delta> \<Longrightarrow> ((TVarB X T1 # G') @ \<Delta>)\<turnstile>S2<:T2" by fact
  1410   have ih1:"TVarB X T1 # G = (TVarB X T1 # G') @ VarB x Q # \<Delta> \<Longrightarrow> ((TVarB X T1 # G') @ \<Delta>)\<turnstile>S2<:T2" by fact
  1522   then have h1:"(TVarB X T1 # (G' @ \<Delta>))\<turnstile>S2<:T2" using SA_all by auto
  1411   then have h1:"(TVarB X T1 # (G' @ \<Delta>))\<turnstile>S2<:T2" using SA_all by auto
  1590    moreover have "(D @ \<Gamma>) \<turnstile> S<:T" using T_Sub by (auto dest: strengthening)
  1479    moreover have "(D @ \<Gamma>) \<turnstile> S<:T" using T_Sub by (auto dest: strengthening)
  1591    ultimately show ?case by auto 
  1480    ultimately show ?case by auto 
  1592  next
  1481  next
  1593    case (T_TAbs X T1 G t2 T2 x u D)
  1482    case (T_TAbs X T1 G t2 T2 x u D)
  1594    from `TVarB X T1 # G \<turnstile> t2 : T2` have "X \<sharp> T1"
  1483    from `TVarB X T1 # G \<turnstile> t2 : T2` have "X \<sharp> T1"
  1595      by (auto simp add: valid_ty_domain_fresh dest: typing_ok intro!: closed_in_fresh)
  1484      by (auto simp add: valid_ty_dom_fresh dest: typing_ok intro!: closed_in_fresh)
  1596    with `X \<sharp> u` and T_TAbs show ?case by fastsimp
  1485    with `X \<sharp> u` and T_TAbs show ?case by fastsimp
  1597  next
  1486  next
  1598    case (T_TApp X G t1 T2 T11 T12 x u D)
  1487    case (T_TApp X G t1 T2 T11 T12 x u D)
  1599    then have "(D@\<Gamma>) \<turnstile>T2<:T11" using T_TApp by (auto dest: strengthening)
  1488    then have "(D@\<Gamma>) \<turnstile>T2<:T11" using T_TApp by (auto dest: strengthening)
  1600    then show "((D @ \<Gamma>) \<turnstile> ((t1 \<cdot>\<^sub>\<tau> T2)[x \<mapsto> u]) : (T12[X \<mapsto> T2]\<^sub>\<tau>))" using T_TApp
  1489    then show "((D @ \<Gamma>) \<turnstile> ((t1 \<cdot>\<^sub>\<tau> T2)[x \<mapsto> u]) : (T12[X \<mapsto> T2]\<^sub>\<tau>))" using T_TApp
  1625   show "(D[X \<mapsto> P]\<^sub>e @ \<Gamma>) \<turnstile> Tvar Y[X \<mapsto> P]\<^sub>\<tau><:T[X \<mapsto> P]\<^sub>\<tau>"
  1514   show "(D[X \<mapsto> P]\<^sub>e @ \<Gamma>) \<turnstile> Tvar Y[X \<mapsto> P]\<^sub>\<tau><:T[X \<mapsto> P]\<^sub>\<tau>"
  1626   proof (cases "X = Y")
  1515   proof (cases "X = Y")
  1627     assume eq: "X = Y"
  1516     assume eq: "X = Y"
  1628     from eq and SA_trans_TVar have "TVarB Y Q \<in> set G" by simp
  1517     from eq and SA_trans_TVar have "TVarB Y Q \<in> set G" by simp
  1629     with G_ok have QS: "Q = S" using `TVarB Y S \<in> set G` by (rule uniqueness_of_ctxt)
  1518     with G_ok have QS: "Q = S" using `TVarB Y S \<in> set G` by (rule uniqueness_of_ctxt)
  1630     from X\<Gamma>_ok have "X \<sharp> ty_domain \<Gamma>" and "Q closed_in \<Gamma>" by auto
  1519     from X\<Gamma>_ok have "X \<sharp> ty_dom \<Gamma>" and "Q closed_in \<Gamma>" by auto
  1631     then have XQ: "X \<sharp> Q" by (rule closed_in_fresh)
  1520     then have XQ: "X \<sharp> Q" by (rule closed_in_fresh)
  1632     note `\<Gamma>\<turnstile>P<:Q`
  1521     note `\<Gamma>\<turnstile>P<:Q`
  1633     moreover from ST have "\<turnstile> (D[X \<mapsto> P]\<^sub>e @ \<Gamma>) ok" by (rule subtype_implies_ok)
  1522     moreover from ST have "\<turnstile> (D[X \<mapsto> P]\<^sub>e @ \<Gamma>) ok" by (rule subtype_implies_ok)
  1634     moreover have "(D[X \<mapsto> P]\<^sub>e @ \<Gamma>) extends \<Gamma>" by (simp add: extends_def)
  1523     moreover have "(D[X \<mapsto> P]\<^sub>e @ \<Gamma>) extends \<Gamma>" by (simp add: extends_def)
  1635     ultimately have "(D[X \<mapsto> P]\<^sub>e @ \<Gamma>) \<turnstile> P<:Q" by (rule weakening)
  1524     ultimately have "(D[X \<mapsto> P]\<^sub>e @ \<Gamma>) \<turnstile> P<:Q" by (rule weakening)
  1650 	by (rule ctxt_subst_mem_TVarB)
  1539 	by (rule ctxt_subst_mem_TVarB)
  1651       then have "TVarB Y (S[X \<mapsto> P]\<^sub>\<tau>) \<in> set (D[X \<mapsto> P]\<^sub>e @ \<Gamma>)" by simp
  1540       then have "TVarB Y (S[X \<mapsto> P]\<^sub>\<tau>) \<in> set (D[X \<mapsto> P]\<^sub>e @ \<Gamma>)" by simp
  1652       with neq and ST show ?thesis by auto
  1541       with neq and ST show ?thesis by auto
  1653     next
  1542     next
  1654       assume Y: "TVarB Y S \<in> set \<Gamma>"
  1543       assume Y: "TVarB Y S \<in> set \<Gamma>"
  1655       from X\<Gamma>_ok have "X \<sharp> ty_domain \<Gamma>" and "\<turnstile> \<Gamma> ok" by auto
  1544       from X\<Gamma>_ok have "X \<sharp> ty_dom \<Gamma>" and "\<turnstile> \<Gamma> ok" by auto
  1656       then have "X \<sharp> \<Gamma>" by (simp add: valid_ty_domain_fresh)
  1545       then have "X \<sharp> \<Gamma>" by (simp add: valid_ty_dom_fresh)
  1657       with Y have "X \<sharp> S"
  1546       with Y have "X \<sharp> S"
  1658 	by (induct \<Gamma>) (auto simp add: fresh_list_nil fresh_list_cons)
  1547 	by (induct \<Gamma>) (auto simp add: fresh_list_nil fresh_list_cons)
  1659       with ST have "(D[X \<mapsto> P]\<^sub>e @ \<Gamma>)\<turnstile>S<:T[X \<mapsto> P]\<^sub>\<tau>"
  1548       with ST have "(D[X \<mapsto> P]\<^sub>e @ \<Gamma>)\<turnstile>S<:T[X \<mapsto> P]\<^sub>\<tau>"
  1660 	by (simp add: type_subst_identity)
  1549 	by (simp add: type_subst_identity)
  1661       moreover from Y have "TVarB Y S \<in> set (D[X \<mapsto> P]\<^sub>e @ \<Gamma>)" by simp
  1550       moreover from Y have "TVarB Y S \<in> set (D[X \<mapsto> P]\<^sub>e @ \<Gamma>)" by simp
  1675     assume "X = Y"
  1564     assume "X = Y"
  1676     with closed' and ok show ?thesis
  1565     with closed' and ok show ?thesis
  1677       by (auto intro: subtype_reflexivity)
  1566       by (auto intro: subtype_reflexivity)
  1678   next
  1567   next
  1679     assume neq: "X \<noteq> Y"
  1568     assume neq: "X \<noteq> Y"
  1680     with SA_refl_TVar have "Y \<in> ty_domain (D[X \<mapsto> P]\<^sub>e @ \<Gamma>)"
  1569     with SA_refl_TVar have "Y \<in> ty_dom (D[X \<mapsto> P]\<^sub>e @ \<Gamma>)"
  1681       by (simp add: ty_domain_subst domains_append)
  1570       by (simp add: ty_dom_subst doms_append)
  1682     with neq and ok show ?thesis by auto
  1571     with neq and ok show ?thesis by auto
  1683   qed
  1572   qed
  1684 next
  1573 next
  1685   case (SA_arrow G T1 S1 S2 T2 X P D)
  1574   case (SA_arrow G T1 S1 S2 T2 X P D)
  1686   then have h1:"(D[X \<mapsto> P]\<^sub>e @ \<Gamma>)\<turnstile>T1[X \<mapsto> P]\<^sub>\<tau><:S1[X \<mapsto> P]\<^sub>\<tau>" using SA_arrow by auto
  1575   then have h1:"(D[X \<mapsto> P]\<^sub>e @ \<Gamma>)\<turnstile>T1[X \<mapsto> P]\<^sub>\<tau><:S1[X \<mapsto> P]\<^sub>\<tau>" using SA_arrow by auto
  1687   from SA_arrow have h2:"(D[X \<mapsto> P]\<^sub>e @ \<Gamma>)\<turnstile>S2[X \<mapsto> P]\<^sub>\<tau><:T2[X \<mapsto> P]\<^sub>\<tau>" using SA_arrow by auto
  1576   from SA_arrow have h2:"(D[X \<mapsto> P]\<^sub>e @ \<Gamma>)\<turnstile>S2[X \<mapsto> P]\<^sub>\<tau><:T2[X \<mapsto> P]\<^sub>\<tau>" using SA_arrow by auto
  1688   show ?case using subtype_of.SA_arrow h1 h2 by auto
  1577   show ?case using subtype_of.SA_arrow h1 h2 by auto
  1689 next
  1578 next
  1690   case (SA_all G T1 S1 Y S2 T2 X P D)
  1579   case (SA_all G T1 S1 Y S2 T2 X P D)
  1691   then have Y: "Y \<sharp> ty_domain (D @ TVarB X Q # \<Gamma>)"
  1580   then have Y: "Y \<sharp> ty_dom (D @ TVarB X Q # \<Gamma>)"
  1692     by (auto dest: subtype_implies_ok intro: fresh_domain)
  1581     by (auto dest: subtype_implies_ok intro: fresh_dom)
  1693   moreover from SA_all have "S1 closed_in (D @ TVarB X Q # \<Gamma>)"
  1582   moreover from SA_all have "S1 closed_in (D @ TVarB X Q # \<Gamma>)"
  1694     by (auto dest: subtype_implies_closed)
  1583     by (auto dest: subtype_implies_closed)
  1695   ultimately have S1: "Y \<sharp> S1" by (rule closed_in_fresh)
  1584   ultimately have S1: "Y \<sharp> S1" by (rule closed_in_fresh)
  1696   from SA_all have "T1 closed_in (D @ TVarB X Q # \<Gamma>)"
  1585   from SA_all have "T1 closed_in (D @ TVarB X Q # \<Gamma>)"
  1697     by (auto dest: subtype_implies_closed)
  1586     by (auto dest: subtype_implies_closed)
  1720       by (rule ctxt_subst_mem_VarB)
  1609       by (rule ctxt_subst_mem_VarB)
  1721     then show ?thesis by simp
  1610     then show ?thesis by simp
  1722   next
  1611   next
  1723     assume x: "VarB x T \<in> set G"
  1612     assume x: "VarB x T \<in> set G"
  1724     from T_Var have ok: "\<turnstile> G ok" by (auto dest: subtype_implies_ok)
  1613     from T_Var have ok: "\<turnstile> G ok" by (auto dest: subtype_implies_ok)
  1725     then have "X \<sharp> ty_domain G" using T_Var by (auto dest: validE_append)
  1614     then have "X \<sharp> ty_dom G" using T_Var by (auto dest: validE_append)
  1726     with ok have "X \<sharp> G" by (simp add: valid_ty_domain_fresh)
  1615     with ok have "X \<sharp> G" by (simp add: valid_ty_dom_fresh)
  1727     moreover from x have "VarB x T \<in> set (D' @ G)" by simp
  1616     moreover from x have "VarB x T \<in> set (D' @ G)" by simp
  1728     then have "VarB x (T[X \<mapsto> P]\<^sub>\<tau>) \<in> set ((D' @ G)[X \<mapsto> P]\<^sub>e)"
  1617     then have "VarB x (T[X \<mapsto> P]\<^sub>\<tau>) \<in> set ((D' @ G)[X \<mapsto> P]\<^sub>e)"
  1729       by (rule ctxt_subst_mem_VarB)
  1618       by (rule ctxt_subst_mem_VarB)
  1730     ultimately show ?thesis
  1619     ultimately show ?thesis
  1731       by (simp add: ctxt_subst_append ctxt_subst_identity)
  1620       by (simp add: ctxt_subst_append ctxt_subst_identity)
  1742 next
  1631 next
  1743   case (T_Sub G' t S T X P D')
  1632   case (T_Sub G' t S T X P D')
  1744   then show ?case using substT_subtype by force
  1633   then show ?case using substT_subtype by force
  1745 next
  1634 next
  1746   case (T_TAbs X' G' T1 t2 T2 X P D')
  1635   case (T_TAbs X' G' T1 t2 T2 X P D')
  1747   then have "X' \<sharp> ty_domain (D' @ TVarB X Q # G)"
  1636   then have "X' \<sharp> ty_dom (D' @ TVarB X Q # G)"
  1748   and "G' closed_in (D' @ TVarB X Q # G)"
  1637   and "G' closed_in (D' @ TVarB X Q # G)"
  1749     by (auto dest: typing_ok)
  1638     by (auto dest: typing_ok)
  1750   then have "X' \<sharp> G'" by (rule closed_in_fresh)
  1639   then have "X' \<sharp> G'" by (rule closed_in_fresh)
  1751   with T_TAbs show ?case by force
  1640   with T_TAbs show ?case by force
  1752 next
  1641 next
  1753   case (T_TApp X' G' t1 T2 T11 T12 X P D')
  1642   case (T_TApp X' G' t1 T2 T11 T12 X P D')
  1754   then have "X' \<sharp> ty_domain (D' @ TVarB X Q # G)"
  1643   then have "X' \<sharp> ty_dom (D' @ TVarB X Q # G)"
  1755     by (simp add: fresh_domain)
  1644     by (simp add: fresh_dom)
  1756   moreover from T_TApp have "T11 closed_in (D' @ TVarB X Q # G)"
  1645   moreover from T_TApp have "T11 closed_in (D' @ TVarB X Q # G)"
  1757     by (auto dest: subtype_implies_closed)
  1646     by (auto dest: subtype_implies_closed)
  1758   ultimately have X': "X' \<sharp> T11" by (rule closed_in_fresh)
  1647   ultimately have X': "X' \<sharp> T11" by (rule closed_in_fresh)
  1759   from T_TApp have "D'[X \<mapsto> P]\<^sub>e @ G \<turnstile> t1[X \<mapsto>\<^sub>\<tau> P] : (\<forall>X'<:T11. T12)[X \<mapsto> P]\<^sub>\<tau>"
  1648   from T_TApp have "D'[X \<mapsto> P]\<^sub>e @ G \<turnstile> t1[X \<mapsto>\<^sub>\<tau> P] : (\<forall>X'<:T11. T12)[X \<mapsto> P]\<^sub>\<tau>"
  1760     by simp
  1649     by simp
  1781   from T_Abs have "VarB y S # \<Gamma> \<turnstile> [(y, x)] \<bullet> s : T\<^isub>2"
  1670   from T_Abs have "VarB y S # \<Gamma> \<turnstile> [(y, x)] \<bullet> s : T\<^isub>2"
  1782     by (simp add: trm.inject alpha fresh_atm)
  1671     by (simp add: trm.inject alpha fresh_atm)
  1783   then have "[(y, x)] \<bullet> (VarB y S # \<Gamma>) \<turnstile> [(y, x)] \<bullet> [(y, x)] \<bullet> s : [(y, x)] \<bullet> T\<^isub>2"
  1672   then have "[(y, x)] \<bullet> (VarB y S # \<Gamma>) \<turnstile> [(y, x)] \<bullet> [(y, x)] \<bullet> s : [(y, x)] \<bullet> T\<^isub>2"
  1784     by (rule typing.eqvt)
  1673     by (rule typing.eqvt)
  1785   moreover from T_Abs have "y \<sharp> \<Gamma>"
  1674   moreover from T_Abs have "y \<sharp> \<Gamma>"
  1786     by (auto dest!: typing_ok simp add: fresh_trm_domain)
  1675     by (auto dest!: typing_ok simp add: fresh_trm_dom)
  1787   ultimately have "VarB x S # \<Gamma> \<turnstile> s : T\<^isub>2" using T_Abs
  1676   ultimately have "VarB x S # \<Gamma> \<turnstile> s : T\<^isub>2" using T_Abs
  1788     by (perm_simp add: ty_vrs_prm_simp)
  1677     by (perm_simp add: ty_vrs_prm_simp)
  1789   with ty1 show ?case using ty2 by (rule T_Abs)
  1678   with ty1 show ?case using ty2 by (rule T_Abs)
  1790 next
  1679 next
  1791   case (T_Sub \<Gamma> t S T)
  1680   case (T_Sub \<Gamma> t S T)
  1817   and   "(TVarB X U # \<Gamma>) \<turnstile> S' <: U'"
  1706   and   "(TVarB X U # \<Gamma>) \<turnstile> S' <: U'"
  1818   using H H' fresh
  1707   using H H' fresh
  1819 proof (nominal_induct \<Gamma> t \<equiv> "\<lambda>X<:S. s" T avoiding: X U U' S arbitrary: s rule: typing.strong_induct)
  1708 proof (nominal_induct \<Gamma> t \<equiv> "\<lambda>X<:S. s" T avoiding: X U U' S arbitrary: s rule: typing.strong_induct)
  1820   case (T_TAbs Y T\<^isub>1 \<Gamma> t\<^isub>2 T\<^isub>2)
  1709   case (T_TAbs Y T\<^isub>1 \<Gamma> t\<^isub>2 T\<^isub>2)
  1821   from `TVarB Y T\<^isub>1 # \<Gamma> \<turnstile> t\<^isub>2 : T\<^isub>2` have Y: "Y \<sharp> \<Gamma>"
  1710   from `TVarB Y T\<^isub>1 # \<Gamma> \<turnstile> t\<^isub>2 : T\<^isub>2` have Y: "Y \<sharp> \<Gamma>"
  1822     by (auto dest!: typing_ok simp add: valid_ty_domain_fresh)
  1711     by (auto dest!: typing_ok simp add: valid_ty_dom_fresh)
  1823   from `Y \<sharp> U'` and `Y \<sharp> X`
  1712   from `Y \<sharp> U'` and `Y \<sharp> X`
  1824   have "(\<forall>X<:U. U') = (\<forall>Y<:U. [(Y, X)] \<bullet> U')"
  1713   have "(\<forall>X<:U. U') = (\<forall>Y<:U. [(Y, X)] \<bullet> U')"
  1825     by (simp add: ty.inject alpha' fresh_atm)
  1714     by (simp add: ty.inject alpha' fresh_atm)
  1826   with T_TAbs have "\<Gamma> \<turnstile> (\<forall>Y<:S. T\<^isub>2) <: (\<forall>Y<:U. [(Y, X)] \<bullet> U')" by (simp add: trm.inject)
  1715   with T_TAbs have "\<Gamma> \<turnstile> (\<forall>Y<:S. T\<^isub>2) <: (\<forall>Y<:U. [(Y, X)] \<bullet> U')" by (simp add: trm.inject)
  1827   then obtain ty1: "\<Gamma> \<turnstile> U <: S" and ty2: "(TVarB Y U # \<Gamma>) \<turnstile> T\<^isub>2 <: ([(Y, X)] \<bullet> U')" using T_TAbs Y
  1716   then obtain ty1: "\<Gamma> \<turnstile> U <: S" and ty2: "(TVarB Y U # \<Gamma>) \<turnstile> T\<^isub>2 <: ([(Y, X)] \<bullet> U')" using T_TAbs Y
  1905   proof (cases rule: eval.strong_cases [where X=X and x=x])
  1794   proof (cases rule: eval.strong_cases [where X=X and x=x])
  1906     case (E_TAbs T\<^isub>1\<^isub>1' T\<^isub>2' t\<^isub>1\<^isub>2)
  1795     case (E_TAbs T\<^isub>1\<^isub>1' T\<^isub>2' t\<^isub>1\<^isub>2)
  1907     with T_TApp have "\<Gamma> \<turnstile> (\<lambda>X<:T\<^isub>1\<^isub>1'. t\<^isub>1\<^isub>2) : (\<forall>X<:T\<^isub>1\<^isub>1. T\<^isub>1\<^isub>2)" and "X \<sharp> \<Gamma>" and "X \<sharp> T\<^isub>1\<^isub>1'"
  1796     with T_TApp have "\<Gamma> \<turnstile> (\<lambda>X<:T\<^isub>1\<^isub>1'. t\<^isub>1\<^isub>2) : (\<forall>X<:T\<^isub>1\<^isub>1. T\<^isub>1\<^isub>2)" and "X \<sharp> \<Gamma>" and "X \<sharp> T\<^isub>1\<^isub>1'"
  1908       by (simp_all add: trm.inject)
  1797       by (simp_all add: trm.inject)
  1909     moreover from `\<Gamma>\<turnstile>T\<^isub>2<:T\<^isub>1\<^isub>1` and `X \<sharp> \<Gamma>` have "X \<sharp> T\<^isub>1\<^isub>1"
  1798     moreover from `\<Gamma>\<turnstile>T\<^isub>2<:T\<^isub>1\<^isub>1` and `X \<sharp> \<Gamma>` have "X \<sharp> T\<^isub>1\<^isub>1"
  1910       by (blast intro: closed_in_fresh fresh_domain dest: subtype_implies_closed)
  1799       by (blast intro: closed_in_fresh fresh_dom dest: subtype_implies_closed)
  1911     ultimately obtain S'
  1800     ultimately obtain S'
  1912       where "TVarB X T\<^isub>1\<^isub>1 # \<Gamma> \<turnstile> t\<^isub>1\<^isub>2 : S'"
  1801       where "TVarB X T\<^isub>1\<^isub>1 # \<Gamma> \<turnstile> t\<^isub>1\<^isub>2 : S'"
  1913       and "(TVarB X T\<^isub>1\<^isub>1 # \<Gamma>) \<turnstile> S' <: T\<^isub>1\<^isub>2"
  1802       and "(TVarB X T\<^isub>1\<^isub>1 # \<Gamma>) \<turnstile> S' <: T\<^isub>1\<^isub>2"
  1914       by (rule TAbs_type') blast
  1803       by (rule TAbs_type') blast
  1915     hence "TVarB X T\<^isub>1\<^isub>1 # \<Gamma> \<turnstile> t\<^isub>1\<^isub>2 : T\<^isub>1\<^isub>2" by (rule T_Sub)
  1804     hence "TVarB X T\<^isub>1\<^isub>1 # \<Gamma> \<turnstile> t\<^isub>1\<^isub>2 : T\<^isub>1\<^isub>2" by (rule T_Sub)