src/HOL/Nominal/Examples/SN.thy
author urbanc
Sun Dec 04 12:14:27 2005 +0100 (2005-12-04)
changeset 18345 d37fb71754fe
parent 18313 e61d2424863d
child 18348 b5d7649f8aca
permissions -rw-r--r--
added an Isar-proof for the abs_ALL lemma
urbanc@18263
     1
(* $Id$ *)
urbanc@18106
     2
urbanc@18106
     3
theory sn
urbanc@18106
     4
imports lam_substs  Accessible_Part
urbanc@18106
     5
begin
urbanc@18106
     6
urbanc@18269
     7
text {* Strong Normalisation proof from the Proofs and Types book *}
urbanc@18106
     8
urbanc@18106
     9
section {* Beta Reduction *}
urbanc@18106
    10
urbanc@18313
    11
urbanc@18106
    12
lemma subst_rename[rule_format]: 
urbanc@18106
    13
  shows "c\<sharp>t1 \<longrightarrow> (t1[a::=t2] = ([(c,a)]\<bullet>t1)[c::=t2])"
urbanc@18313
    14
apply(nominal_induct t1 avoiding: a c t2 rule: lam_induct)
urbanc@18313
    15
apply(auto simp add: calc_atm fresh_atm abs_fresh)
urbanc@18106
    16
done
urbanc@18106
    17
urbanc@18313
    18
lemma forget: 
urbanc@18313
    19
  assumes a: "a\<sharp>t1"
urbanc@18313
    20
  shows "t1[a::=t2] = t1"
urbanc@18313
    21
  using a
urbanc@18313
    22
apply (nominal_induct t1 avoiding: a t2 rule: lam_induct)
urbanc@18313
    23
apply(auto simp add: abs_fresh fresh_atm)
urbanc@18106
    24
done
urbanc@18106
    25
urbanc@18313
    26
lemma fresh_fact: 
urbanc@18313
    27
  fixes a::"name"
urbanc@18313
    28
  assumes a: "a\<sharp>t1"
urbanc@18313
    29
  and     b: "a\<sharp>t2"
urbanc@18313
    30
  shows "a\<sharp>(t1[b::=t2])"
urbanc@18313
    31
using a b
urbanc@18313
    32
apply(nominal_induct t1 avoiding: a b t2 rule: lam_induct)
urbanc@18313
    33
apply(auto simp add: abs_fresh fresh_atm)
urbanc@18106
    34
done
urbanc@18106
    35
urbanc@18106
    36
lemma subs_lemma:  
urbanc@18313
    37
  assumes a: "x\<noteq>y"
urbanc@18313
    38
  and     b: "x\<sharp>L"
urbanc@18313
    39
  shows "M[x::=N][y::=L] = M[y::=L][x::=N[y::=L]]"
urbanc@18313
    40
using a b
urbanc@18313
    41
by (nominal_induct M avoiding: x y N L rule: lam_induct)
urbanc@18313
    42
   (auto simp add: fresh_fact forget)
urbanc@18106
    43
urbanc@18106
    44
lemma id_subs: "t[x::=Var x] = t"
urbanc@18313
    45
apply(nominal_induct t avoiding: x rule: lam_induct)
urbanc@18106
    46
apply(simp_all add: fresh_atm)
urbanc@18106
    47
done
urbanc@18106
    48
urbanc@18106
    49
consts
urbanc@18106
    50
  Beta :: "(lam\<times>lam) set"
urbanc@18106
    51
syntax 
urbanc@18106
    52
  "_Beta"       :: "lam\<Rightarrow>lam\<Rightarrow>bool" (" _ \<longrightarrow>\<^isub>\<beta> _" [80,80] 80)
urbanc@18106
    53
  "_Beta_star"  :: "lam\<Rightarrow>lam\<Rightarrow>bool" (" _ \<longrightarrow>\<^isub>\<beta>\<^sup>* _" [80,80] 80)
urbanc@18106
    54
translations 
urbanc@18106
    55
  "t1 \<longrightarrow>\<^isub>\<beta> t2" \<rightleftharpoons> "(t1,t2) \<in> Beta"
urbanc@18106
    56
  "t1 \<longrightarrow>\<^isub>\<beta>\<^sup>* t2" \<rightleftharpoons> "(t1,t2) \<in> Beta\<^sup>*"
urbanc@18106
    57
inductive Beta
urbanc@18106
    58
  intros
urbanc@18106
    59
  b1[intro!]: "s1\<longrightarrow>\<^isub>\<beta>s2 \<Longrightarrow> (App s1 t)\<longrightarrow>\<^isub>\<beta>(App s2 t)"
urbanc@18106
    60
  b2[intro!]: "s1\<longrightarrow>\<^isub>\<beta>s2 \<Longrightarrow> (App t s1)\<longrightarrow>\<^isub>\<beta>(App t s2)"
urbanc@18106
    61
  b3[intro!]: "s1\<longrightarrow>\<^isub>\<beta>s2 \<Longrightarrow> (Lam [a].s1)\<longrightarrow>\<^isub>\<beta> (Lam [(a::name)].s2)"
urbanc@18106
    62
  b4[intro!]: "(App (Lam [(a::name)].s1) s2)\<longrightarrow>\<^isub>\<beta>(s1[a::=s2])"
urbanc@18106
    63
urbanc@18106
    64
lemma eqvt_beta: 
urbanc@18106
    65
  fixes pi :: "name prm"
urbanc@18106
    66
  and   t  :: "lam"
urbanc@18106
    67
  and   s  :: "lam"
urbanc@18313
    68
  assumes a: "t\<longrightarrow>\<^isub>\<beta>s"
urbanc@18313
    69
  shows "(pi\<bullet>t)\<longrightarrow>\<^isub>\<beta>(pi\<bullet>s)"
urbanc@18313
    70
  using a by (induct, auto)
urbanc@18106
    71
urbanc@18313
    72
lemma beta_induct[consumes 1, case_names b1 b2 b3 b4]:
urbanc@18313
    73
  fixes  P :: "'a::fs_name\<Rightarrow>lam \<Rightarrow> lam \<Rightarrow>bool"
urbanc@18106
    74
  and    t :: "lam"
urbanc@18106
    75
  and    s :: "lam"
urbanc@18106
    76
  and    x :: "'a::fs_name"
urbanc@18106
    77
  assumes a: "t\<longrightarrow>\<^isub>\<beta>s"
urbanc@18313
    78
  and a1:    "\<And>t s1 s2 x. s1\<longrightarrow>\<^isub>\<beta>s2 \<Longrightarrow> (\<And>z. P z s1 s2) \<Longrightarrow> P x (App s1 t) (App s2 t)"
urbanc@18313
    79
  and a2:    "\<And>t s1 s2 x. s1\<longrightarrow>\<^isub>\<beta>s2 \<Longrightarrow> (\<And>z. P z s1 s2) \<Longrightarrow> P x (App t s1) (App t s2)"
urbanc@18313
    80
  and a3:    "\<And>a s1 s2 x. a\<sharp>x \<Longrightarrow> s1\<longrightarrow>\<^isub>\<beta>s2 \<Longrightarrow> (\<And>z. P z s1 s2) \<Longrightarrow> P x (Lam [a].s1) (Lam [a].s2)"
urbanc@18313
    81
  and a4:    "\<And>a t1 s1 x. a\<sharp>(s1,x) \<Longrightarrow> P x (App (Lam [a].t1) s1) (t1[a::=s1])"
urbanc@18313
    82
  shows "P x t s"
urbanc@18313
    83
proof -
urbanc@18313
    84
  from a have "\<And>(pi::name prm) x. P x (pi\<bullet>t) (pi\<bullet>s)"
urbanc@18313
    85
  proof (induct)
urbanc@18313
    86
    case b1 thus ?case using a1 by (simp, blast intro: eqvt_beta)
urbanc@18313
    87
  next
urbanc@18313
    88
    case b2 thus ?case using a2 by (simp, blast intro: eqvt_beta)
urbanc@18313
    89
  next
urbanc@18313
    90
    case (b3 a s1 s2)
urbanc@18313
    91
    have j1: "s1 \<longrightarrow>\<^isub>\<beta> s2" by fact
urbanc@18313
    92
    have j2: "\<And>x (pi::name prm). P x (pi\<bullet>s1) (pi\<bullet>s2)" by fact
urbanc@18313
    93
    show ?case 
urbanc@18313
    94
    proof (simp)
urbanc@18313
    95
      have f: "\<exists>c::name. c\<sharp>(pi\<bullet>a,pi\<bullet>s1,pi\<bullet>s2,x)"
urbanc@18313
    96
	by (rule at_exists_fresh[OF at_name_inst], simp add: fs_name1)
urbanc@18313
    97
      then obtain c::"name" 
urbanc@18313
    98
	where f1: "c\<noteq>(pi\<bullet>a)" and f2: "c\<sharp>x" and f3: "c\<sharp>(pi\<bullet>s1)" and f4: "c\<sharp>(pi\<bullet>s2)"
urbanc@18313
    99
	by (force simp add: fresh_prod fresh_atm)
urbanc@18313
   100
      have x: "P x (Lam [c].(([(c,pi\<bullet>a)]@pi)\<bullet>s1)) (Lam [c].(([(c,pi\<bullet>a)]@pi)\<bullet>s2))"
urbanc@18313
   101
	using a3 f2 j1 j2 by (simp, blast intro: eqvt_beta)
urbanc@18313
   102
      have alpha1: "(Lam [c].([(c,pi\<bullet>a)]\<bullet>(pi\<bullet>s1))) = (Lam [(pi\<bullet>a)].(pi\<bullet>s1))" using f1 f3
urbanc@18313
   103
	by (simp add: lam.inject alpha)
urbanc@18313
   104
      have alpha2: "(Lam [c].([(c,pi\<bullet>a)]\<bullet>(pi\<bullet>s2))) = (Lam [(pi\<bullet>a)].(pi\<bullet>s2))" using f1 f3
urbanc@18313
   105
	by (simp add: lam.inject alpha)
urbanc@18313
   106
      show " P x (Lam [(pi\<bullet>a)].(pi\<bullet>s1)) (Lam [(pi\<bullet>a)].(pi\<bullet>s2))"
urbanc@18313
   107
	using x alpha1 alpha2 by (simp only: pt_name2)
urbanc@18313
   108
    qed
urbanc@18313
   109
  next
urbanc@18313
   110
    case (b4 a s1 s2)
urbanc@18313
   111
    show ?case
urbanc@18313
   112
    proof (simp add: subst_eqvt)
urbanc@18313
   113
      have f: "\<exists>c::name. c\<sharp>(pi\<bullet>a,pi\<bullet>s1,pi\<bullet>s2,x)"
urbanc@18313
   114
	by (rule at_exists_fresh[OF at_name_inst], simp add: fs_name1)
urbanc@18313
   115
      then obtain c::"name" 
urbanc@18313
   116
	where f1: "c\<noteq>(pi\<bullet>a)" and f2: "c\<sharp>(pi\<bullet>s2,x)" and f3: "c\<sharp>(pi\<bullet>s1)" and f4: "c\<sharp>(pi\<bullet>s2)"
urbanc@18313
   117
	by (force simp add: fresh_prod fresh_atm)
urbanc@18313
   118
      have x: "P x (App (Lam [c].(([(c,pi\<bullet>a)]@pi)\<bullet>s1)) (pi\<bullet>s2)) ((([(c,pi\<bullet>a)]@pi)\<bullet>s1)[c::=(pi\<bullet>s2)])"
urbanc@18313
   119
	using a4 f2 by (blast intro!: eqvt_beta)
urbanc@18313
   120
      have alpha1: "(Lam [c].([(c,pi\<bullet>a)]\<bullet>(pi\<bullet>s1))) = (Lam [(pi\<bullet>a)].(pi\<bullet>s1))" using f1 f3
urbanc@18313
   121
	by (simp add: lam.inject alpha)
urbanc@18313
   122
      have alpha2: "(([(c,pi\<bullet>a)]@pi)\<bullet>s1)[c::=(pi\<bullet>s2)] = (pi\<bullet>s1)[(pi\<bullet>a)::=(pi\<bullet>s2)]"
urbanc@18313
   123
	using f3 by (simp only: subst_rename[symmetric] pt_name2)
urbanc@18313
   124
      show "P x (App (Lam [(pi\<bullet>a)].(pi\<bullet>s1)) (pi\<bullet>s2)) ((pi\<bullet>s1)[(pi\<bullet>a)::=(pi\<bullet>s2)])"
urbanc@18313
   125
	using x alpha1 alpha2 by (simp only: pt_name2)
urbanc@18313
   126
    qed
urbanc@18313
   127
  qed
urbanc@18313
   128
  hence "P x (([]::name prm)\<bullet>t) (([]::name prm)\<bullet>s)" by blast 
urbanc@18313
   129
  thus ?thesis by simp
urbanc@18313
   130
qed
urbanc@18106
   131
urbanc@18106
   132
lemma supp_beta: "t\<longrightarrow>\<^isub>\<beta> s\<Longrightarrow>(supp s)\<subseteq>((supp t)::name set)"
urbanc@18106
   133
apply(erule Beta.induct)
urbanc@18106
   134
apply(auto intro!: simp add: abs_supp lam.supp subst_supp)
urbanc@18106
   135
done
urbanc@18313
   136
urbanc@18106
   137
lemma beta_abs: "Lam [a].t\<longrightarrow>\<^isub>\<beta> t'\<Longrightarrow>\<exists>t''. t'=Lam [a].t'' \<and> t\<longrightarrow>\<^isub>\<beta> t''"
urbanc@18106
   138
apply(ind_cases "Lam [a].t  \<longrightarrow>\<^isub>\<beta> t'")
urbanc@18106
   139
apply(auto simp add: lam.distinct lam.inject)
urbanc@18106
   140
apply(auto simp add: alpha)
urbanc@18106
   141
apply(rule_tac x="[(a,aa)]\<bullet>s2" in exI)
urbanc@18106
   142
apply(rule conjI)
urbanc@18106
   143
apply(rule sym)
urbanc@18106
   144
apply(rule pt_bij2[OF pt_name_inst, OF at_name_inst])
urbanc@18106
   145
apply(simp)
urbanc@18106
   146
apply(rule pt_name3)
urbanc@18106
   147
apply(simp add: at_ds5[OF at_name_inst])
urbanc@18106
   148
apply(rule conjI)
urbanc@18106
   149
apply(simp add: pt_fresh_left[OF pt_name_inst, OF at_name_inst] calc_atm)
urbanc@18106
   150
apply(force dest!: supp_beta simp add: fresh_def)
urbanc@18106
   151
apply(force intro!: eqvt_beta)
urbanc@18106
   152
done
urbanc@18106
   153
urbanc@18313
   154
lemma beta_subst: 
urbanc@18106
   155
  assumes a: "M \<longrightarrow>\<^isub>\<beta> M'"
urbanc@18106
   156
  shows "M[x::=N]\<longrightarrow>\<^isub>\<beta> M'[x::=N]" 
urbanc@18106
   157
using a
urbanc@18313
   158
apply(nominal_induct M M' avoiding: x N rule: beta_induct)
urbanc@18313
   159
apply(auto simp add: fresh_atm subs_lemma)
urbanc@18106
   160
done
urbanc@18106
   161
urbanc@18106
   162
datatype ty =
urbanc@18106
   163
    TVar "string"
urbanc@18106
   164
  | TArr "ty" "ty" (infix "\<rightarrow>" 200)
urbanc@18106
   165
urbanc@18106
   166
primrec
urbanc@18106
   167
 "pi\<bullet>(TVar s) = TVar s"
urbanc@18106
   168
 "pi\<bullet>(\<tau> \<rightarrow> \<sigma>) = (\<tau> \<rightarrow> \<sigma>)"
urbanc@18106
   169
urbanc@18106
   170
lemma perm_ty[simp]:
urbanc@18106
   171
  fixes pi ::"name prm"
urbanc@18106
   172
  and   \<tau>  ::"ty"
urbanc@18106
   173
  shows "pi\<bullet>\<tau> = \<tau>"
urbanc@18106
   174
  by (cases \<tau>, simp_all)
urbanc@18106
   175
urbanc@18106
   176
lemma fresh_ty:
urbanc@18106
   177
  fixes a ::"name"
urbanc@18106
   178
  and   \<tau>  ::"ty"
urbanc@18106
   179
  shows "a\<sharp>\<tau>"
urbanc@18106
   180
  by (simp add: fresh_def supp_def)
urbanc@18106
   181
urbanc@18106
   182
instance ty :: pt_name
urbanc@18106
   183
apply(intro_classes)   
urbanc@18106
   184
apply(simp_all)
urbanc@18106
   185
done
urbanc@18106
   186
urbanc@18106
   187
instance ty :: fs_name
urbanc@18106
   188
apply(intro_classes)
urbanc@18106
   189
apply(simp add: supp_def)
urbanc@18106
   190
done
urbanc@18106
   191
urbanc@18106
   192
(* valid contexts *)
urbanc@18106
   193
urbanc@18106
   194
consts
urbanc@18106
   195
  "dom_ty" :: "(name\<times>ty) list \<Rightarrow> (name list)"
urbanc@18106
   196
primrec
urbanc@18106
   197
  "dom_ty []    = []"
urbanc@18106
   198
  "dom_ty (x#\<Gamma>) = (fst x)#(dom_ty \<Gamma>)" 
urbanc@18106
   199
urbanc@18106
   200
consts
urbanc@18106
   201
  ctxts :: "((name\<times>ty) list) set" 
urbanc@18106
   202
  valid :: "(name\<times>ty) list \<Rightarrow> bool"
urbanc@18106
   203
translations
urbanc@18106
   204
  "valid \<Gamma>" \<rightleftharpoons> "\<Gamma> \<in> ctxts"  
urbanc@18106
   205
inductive ctxts
urbanc@18106
   206
intros
urbanc@18106
   207
v1[intro]: "valid []"
urbanc@18106
   208
v2[intro]: "\<lbrakk>valid \<Gamma>;a\<sharp>\<Gamma>\<rbrakk>\<Longrightarrow> valid ((a,\<sigma>)#\<Gamma>)"
urbanc@18106
   209
urbanc@18106
   210
lemma valid_eqvt:
urbanc@18106
   211
  fixes   pi:: "name prm"
urbanc@18106
   212
  assumes a: "valid \<Gamma>"
urbanc@18106
   213
  shows   "valid (pi\<bullet>\<Gamma>)"
urbanc@18106
   214
using a
urbanc@18106
   215
apply(induct)
urbanc@18106
   216
apply(auto simp add: pt_fresh_bij[OF pt_name_inst, OF at_name_inst])
urbanc@18106
   217
done
urbanc@18106
   218
urbanc@18106
   219
(* typing judgements *)
urbanc@18106
   220
urbanc@18106
   221
lemma fresh_context[rule_format]: 
urbanc@18106
   222
  fixes  \<Gamma> :: "(name\<times>ty)list"
urbanc@18106
   223
  and    a :: "name"
urbanc@18106
   224
  shows "a\<sharp>\<Gamma>\<longrightarrow>\<not>(\<exists>\<tau>::ty. (a,\<tau>)\<in>set \<Gamma>)"
urbanc@18106
   225
apply(induct_tac \<Gamma>)
urbanc@18106
   226
apply(auto simp add: fresh_prod fresh_list_cons fresh_atm)
urbanc@18106
   227
done
urbanc@18106
   228
urbanc@18106
   229
lemma valid_elim: 
urbanc@18106
   230
  fixes  \<Gamma> :: "(name\<times>ty)list"
urbanc@18106
   231
  and    pi:: "name prm"
urbanc@18106
   232
  and    a :: "name"
urbanc@18106
   233
  and    \<tau> :: "ty"
urbanc@18106
   234
  shows "valid ((a,\<tau>)#\<Gamma>) \<Longrightarrow> valid \<Gamma> \<and> a\<sharp>\<Gamma>"
urbanc@18106
   235
apply(ind_cases "valid ((a,\<tau>)#\<Gamma>)", simp)
urbanc@18106
   236
done
urbanc@18106
   237
urbanc@18106
   238
lemma valid_unicity[rule_format]: 
urbanc@18106
   239
  shows "valid \<Gamma>\<longrightarrow>(c,\<sigma>)\<in>set \<Gamma>\<longrightarrow>(c,\<tau>)\<in>set \<Gamma>\<longrightarrow>\<sigma>=\<tau>" 
urbanc@18106
   240
apply(induct_tac \<Gamma>)
urbanc@18106
   241
apply(auto dest!: valid_elim fresh_context)
urbanc@18106
   242
done
urbanc@18106
   243
urbanc@18106
   244
consts
urbanc@18106
   245
  typing :: "(((name\<times>ty) list)\<times>lam\<times>ty) set" 
urbanc@18106
   246
syntax
urbanc@18106
   247
  "_typing_judge" :: "(name\<times>ty) list\<Rightarrow>lam\<Rightarrow>ty\<Rightarrow>bool" (" _ \<turnstile> _ : _ " [80,80,80] 80) 
urbanc@18106
   248
translations
urbanc@18106
   249
  "\<Gamma> \<turnstile> t : \<tau>" \<rightleftharpoons> "(\<Gamma>,t,\<tau>) \<in> typing"  
urbanc@18106
   250
urbanc@18106
   251
inductive typing
urbanc@18106
   252
intros
urbanc@18106
   253
t1[intro]: "\<lbrakk>valid \<Gamma>; (a,\<tau>)\<in>set \<Gamma>\<rbrakk>\<Longrightarrow> \<Gamma> \<turnstile> Var a : \<tau>"
urbanc@18106
   254
t2[intro]: "\<lbrakk>\<Gamma> \<turnstile> t1 : \<tau>\<rightarrow>\<sigma>; \<Gamma> \<turnstile> t2 : \<tau>\<rbrakk>\<Longrightarrow> \<Gamma> \<turnstile> App t1 t2 : \<sigma>"
urbanc@18106
   255
t3[intro]: "\<lbrakk>a\<sharp>\<Gamma>;((a,\<tau>)#\<Gamma>) \<turnstile> t : \<sigma>\<rbrakk> \<Longrightarrow> \<Gamma> \<turnstile> Lam [a].t : \<tau>\<rightarrow>\<sigma>"
urbanc@18106
   256
urbanc@18313
   257
lemma eqvt_typing: 
urbanc@18106
   258
  fixes  \<Gamma> :: "(name\<times>ty) list"
urbanc@18106
   259
  and    t :: "lam"
urbanc@18106
   260
  and    \<tau> :: "ty"
urbanc@18106
   261
  and    pi:: "name prm"
urbanc@18106
   262
  assumes a: "\<Gamma> \<turnstile> t : \<tau>"
urbanc@18106
   263
  shows "(pi\<bullet>\<Gamma>) \<turnstile> (pi\<bullet>t) : \<tau>"
urbanc@18106
   264
using a
urbanc@18106
   265
proof (induct)
urbanc@18106
   266
  case (t1 \<Gamma> \<tau> a)
urbanc@18106
   267
  have "valid (pi\<bullet>\<Gamma>)" by (rule valid_eqvt)
urbanc@18106
   268
  moreover
urbanc@18106
   269
  have "(pi\<bullet>(a,\<tau>))\<in>((pi::name prm)\<bullet>set \<Gamma>)" by (rule pt_set_bij2[OF pt_name_inst, OF at_name_inst])
urbanc@18313
   270
  ultimately show "(pi\<bullet>\<Gamma>) \<turnstile> ((pi::name prm)\<bullet>Var a) : \<tau>"
urbanc@18313
   271
    using typing.intros by (force simp add: pt_list_set_pi[OF pt_name_inst, symmetric])
urbanc@18106
   272
next 
urbanc@18106
   273
  case (t3 \<Gamma> \<sigma> \<tau> a t)
urbanc@18106
   274
  moreover have "(pi\<bullet>a)\<sharp>(pi\<bullet>\<Gamma>)" by (rule pt_fresh_bij1[OF pt_name_inst, OF at_name_inst])
urbanc@18313
   275
  ultimately show "(pi\<bullet>\<Gamma>) \<turnstile> (pi\<bullet>Lam [a].t) :\<tau>\<rightarrow>\<sigma>" by force 
urbanc@18106
   276
qed (auto)
urbanc@18106
   277
urbanc@18313
   278
lemma typing_induct[consumes 1, case_names t1 t2 t3]:
urbanc@18313
   279
  fixes  P :: "'a::fs_name\<Rightarrow>(name\<times>ty) list \<Rightarrow> lam \<Rightarrow> ty \<Rightarrow>bool"
urbanc@18106
   280
  and    \<Gamma> :: "(name\<times>ty) list"
urbanc@18106
   281
  and    t :: "lam"
urbanc@18106
   282
  and    \<tau> :: "ty"
urbanc@18106
   283
  and    x :: "'a::fs_name"
urbanc@18106
   284
  assumes a: "\<Gamma> \<turnstile> t : \<tau>"
urbanc@18313
   285
  and a1:    "\<And>\<Gamma> (a::name) \<tau> x. valid \<Gamma> \<Longrightarrow> (a,\<tau>) \<in> set \<Gamma> \<Longrightarrow> P x \<Gamma> (Var a) \<tau>"
urbanc@18313
   286
  and a2:    "\<And>\<Gamma> \<tau> \<sigma> t1 t2 x. 
urbanc@18313
   287
              \<Gamma> \<turnstile> t1 : \<tau>\<rightarrow>\<sigma> \<Longrightarrow> (\<And>z. P z \<Gamma> t1 (\<tau>\<rightarrow>\<sigma>)) \<Longrightarrow> \<Gamma> \<turnstile> t2 : \<tau> \<Longrightarrow> (\<And>z. P z \<Gamma> t2 \<tau>)
urbanc@18313
   288
              \<Longrightarrow> P x \<Gamma> (App t1 t2) \<sigma>"
urbanc@18313
   289
  and a3:    "\<And>a \<Gamma> \<tau> \<sigma> t x. a\<sharp>x \<Longrightarrow> a\<sharp>\<Gamma> \<Longrightarrow> ((a,\<tau>) # \<Gamma>) \<turnstile> t : \<sigma> \<Longrightarrow> (\<And>z. P z ((a,\<tau>)#\<Gamma>) t \<sigma>)
urbanc@18313
   290
              \<Longrightarrow> P x \<Gamma> (Lam [a].t) (\<tau>\<rightarrow>\<sigma>)"
urbanc@18313
   291
  shows "P x \<Gamma> t \<tau>"
urbanc@18313
   292
proof -
urbanc@18313
   293
  from a have "\<And>(pi::name prm) x. P x (pi\<bullet>\<Gamma>) (pi\<bullet>t) \<tau>"
urbanc@18313
   294
  proof (induct)
urbanc@18313
   295
    case (t1 \<Gamma> \<tau> a)
urbanc@18313
   296
    have j1: "valid \<Gamma>" by fact
urbanc@18313
   297
    have j2: "(a,\<tau>)\<in>set \<Gamma>" by fact
urbanc@18313
   298
    from j1 have j3: "valid (pi\<bullet>\<Gamma>)" by (rule valid_eqvt)
urbanc@18313
   299
    from j2 have "pi\<bullet>(a,\<tau>)\<in>pi\<bullet>(set \<Gamma>)" by (simp only: pt_set_bij[OF pt_name_inst, OF at_name_inst])  
urbanc@18313
   300
    hence j4: "(pi\<bullet>a,\<tau>)\<in>set (pi\<bullet>\<Gamma>)" by (simp add: pt_list_set_pi[OF pt_name_inst])
urbanc@18313
   301
    show "P x (pi\<bullet>\<Gamma>) (pi\<bullet>(Var a)) \<tau>" using a1 j3 j4 by simp
urbanc@18313
   302
  next
urbanc@18313
   303
    case (t2 \<Gamma> \<sigma> \<tau> t1 t2)
urbanc@18313
   304
    thus ?case using a2 by (simp, blast intro: eqvt_typing)
urbanc@18313
   305
  next
urbanc@18313
   306
    case (t3 \<Gamma> \<sigma> \<tau> a t)
urbanc@18313
   307
    have k1: "a\<sharp>\<Gamma>" by fact
urbanc@18313
   308
    have k2: "((a,\<tau>)#\<Gamma>)\<turnstile>t:\<sigma>" by fact
urbanc@18313
   309
    have k3: "\<And>(pi::name prm) (x::'a::fs_name). P x (pi \<bullet>((a,\<tau>)#\<Gamma>)) (pi\<bullet>t) \<sigma>" by fact
urbanc@18313
   310
    have f: "\<exists>c::name. c\<sharp>(pi\<bullet>a,pi\<bullet>t,pi\<bullet>\<Gamma>,x)"
urbanc@18313
   311
      by (rule at_exists_fresh[OF at_name_inst], simp add: fs_name1)
urbanc@18313
   312
    then obtain c::"name" 
urbanc@18313
   313
      where f1: "c\<noteq>(pi\<bullet>a)" and f2: "c\<sharp>x" and f3: "c\<sharp>(pi\<bullet>t)" and f4: "c\<sharp>(pi\<bullet>\<Gamma>)"
urbanc@18313
   314
      by (force simp add: fresh_prod at_fresh[OF at_name_inst])
urbanc@18313
   315
    from k1 have k1a: "(pi\<bullet>a)\<sharp>(pi\<bullet>\<Gamma>)" 
urbanc@18313
   316
      by (simp add: pt_fresh_left[OF pt_name_inst, OF at_name_inst] 
urbanc@18313
   317
                    pt_rev_pi[OF pt_name_inst, OF at_name_inst])
urbanc@18313
   318
    have l1: "(([(c,pi\<bullet>a)]@pi)\<bullet>\<Gamma>) = (pi\<bullet>\<Gamma>)" using f4 k1a 
urbanc@18313
   319
      by (simp only: pt2[OF pt_name_inst], rule pt_fresh_fresh[OF pt_name_inst, OF at_name_inst])
urbanc@18313
   320
    have "\<And>x. P x (([(c,pi\<bullet>a)]@pi)\<bullet>((a,\<tau>)#\<Gamma>)) (([(c,pi\<bullet>a)]@pi)\<bullet>t) \<sigma>" using k3 by force
urbanc@18313
   321
    hence l2: "\<And>x. P x ((c, \<tau>)#(pi\<bullet>\<Gamma>)) (([(c,pi\<bullet>a)]@pi)\<bullet>t) \<sigma>" using f1 l1
urbanc@18313
   322
      by (force simp add: pt2[OF pt_name_inst]  at_calc[OF at_name_inst])
urbanc@18313
   323
    have "(([(c,pi\<bullet>a)]@pi)\<bullet>((a,\<tau>)#\<Gamma>)) \<turnstile> (([(c,pi\<bullet>a)]@pi)\<bullet>t) : \<sigma>" using k2 by (rule eqvt_typing)
urbanc@18313
   324
    hence l3: "((c, \<tau>)#(pi\<bullet>\<Gamma>)) \<turnstile> (([(c,pi\<bullet>a)]@pi)\<bullet>t) : \<sigma>" using l1 f1 
urbanc@18313
   325
      by (force simp add: pt2[OF pt_name_inst]  at_calc[OF at_name_inst])
urbanc@18313
   326
    have l4: "P x (pi\<bullet>\<Gamma>) (Lam [c].(([(c,pi\<bullet>a)]@pi)\<bullet>t)) (\<tau> \<rightarrow> \<sigma>)" using f2 f4 l2 l3 a3 by auto
urbanc@18313
   327
    have alpha: "(Lam [c].([(c,pi\<bullet>a)]\<bullet>(pi\<bullet>t))) = (Lam [(pi\<bullet>a)].(pi\<bullet>t))" using f1 f3
urbanc@18313
   328
      by (simp add: lam.inject alpha)
urbanc@18313
   329
    show "P x (pi\<bullet>\<Gamma>) (pi\<bullet>(Lam [a].t)) (\<tau> \<rightarrow> \<sigma>)" using l4 alpha 
urbanc@18313
   330
      by (simp only: pt2[OF pt_name_inst], simp)
urbanc@18313
   331
  qed
urbanc@18313
   332
  hence "P x (([]::name prm)\<bullet>\<Gamma>) (([]::name prm)\<bullet>t) \<tau>" by blast
urbanc@18313
   333
  thus "P x \<Gamma> t \<tau>" by simp
urbanc@18313
   334
qed
urbanc@18106
   335
urbanc@18106
   336
constdefs
urbanc@18106
   337
  "sub" :: "(name\<times>ty) list \<Rightarrow> (name\<times>ty) list \<Rightarrow> bool" (" _ \<lless> _ " [80,80] 80)
urbanc@18106
   338
  "\<Gamma>1 \<lless> \<Gamma>2 \<equiv> \<forall>a \<sigma>. (a,\<sigma>)\<in>set \<Gamma>1 \<longrightarrow>  (a,\<sigma>)\<in>set \<Gamma>2"
urbanc@18106
   339
urbanc@18313
   340
lemma weakening: 
urbanc@18313
   341
  assumes a: "\<Gamma>1 \<turnstile> t : \<sigma>" 
urbanc@18313
   342
  and     b: "valid \<Gamma>2" 
urbanc@18313
   343
  and     c: "\<Gamma>1 \<lless> \<Gamma>2"
urbanc@18313
   344
  shows "\<Gamma>2 \<turnstile> t:\<sigma>"
urbanc@18313
   345
using a b c
urbanc@18313
   346
apply(nominal_induct \<Gamma>1 t \<sigma> avoiding: \<Gamma>2 rule: typing_induct)
urbanc@18106
   347
apply(auto simp add: sub_def)
urbanc@18313
   348
(* FIXME: before using meta-connectives and the new induction *)
urbanc@18313
   349
(* method, this was completely automatic *)
urbanc@18313
   350
apply(atomize)
urbanc@18313
   351
apply(auto)
urbanc@18106
   352
done
urbanc@18106
   353
urbanc@18106
   354
lemma in_ctxt[rule_format]: "(a,\<tau>)\<in>set \<Gamma> \<longrightarrow> (a\<in>set(dom_ty \<Gamma>))"
urbanc@18106
   355
apply(induct_tac \<Gamma>)
urbanc@18106
   356
apply(auto)
urbanc@18106
   357
done
urbanc@18106
   358
urbanc@18106
   359
lemma free_vars: 
urbanc@18106
   360
  assumes a: "\<Gamma> \<turnstile> t : \<tau>"
urbanc@18106
   361
  shows " (supp t)\<subseteq>set(dom_ty \<Gamma>)"
urbanc@18106
   362
using a
urbanc@18106
   363
apply(nominal_induct \<Gamma> t \<tau> rule: typing_induct)
urbanc@18106
   364
apply(auto simp add: lam.supp abs_supp supp_atm in_ctxt)
urbanc@18106
   365
done
urbanc@18106
   366
urbanc@18106
   367
lemma t1_elim: "\<Gamma> \<turnstile> Var a : \<tau> \<Longrightarrow> valid \<Gamma> \<and> (a,\<tau>) \<in> set \<Gamma>"
urbanc@18106
   368
apply(ind_cases "\<Gamma> \<turnstile> Var a : \<tau>")
urbanc@18106
   369
apply(auto simp add: lam.inject lam.distinct)
urbanc@18106
   370
done
urbanc@18106
   371
urbanc@18106
   372
lemma t2_elim: "\<Gamma> \<turnstile> App t1 t2 : \<sigma> \<Longrightarrow> \<exists>\<tau>. (\<Gamma> \<turnstile> t1 : \<tau>\<rightarrow>\<sigma> \<and> \<Gamma> \<turnstile> t2 : \<tau>)"
urbanc@18106
   373
apply(ind_cases "\<Gamma> \<turnstile> App t1 t2 : \<sigma>")
urbanc@18106
   374
apply(auto simp add: lam.inject lam.distinct)
urbanc@18106
   375
done
urbanc@18106
   376
urbanc@18106
   377
lemma t3_elim: "\<lbrakk>\<Gamma> \<turnstile> Lam [a].t : \<sigma>;a\<sharp>\<Gamma>\<rbrakk>\<Longrightarrow> \<exists>\<tau> \<tau>'. \<sigma>=\<tau>\<rightarrow>\<tau>' \<and> ((a,\<tau>)#\<Gamma>) \<turnstile> t : \<tau>'"
urbanc@18106
   378
apply(ind_cases "\<Gamma> \<turnstile> Lam [a].t : \<sigma>")
urbanc@18106
   379
apply(auto simp add: lam.distinct lam.inject alpha) 
urbanc@18313
   380
apply(drule_tac pi="[(a,aa)]::name prm" in eqvt_typing)
urbanc@18106
   381
apply(simp)
urbanc@18106
   382
apply(subgoal_tac "([(a,aa)]::name prm)\<bullet>\<Gamma> = \<Gamma>")(*A*)
urbanc@18106
   383
apply(force simp add: calc_atm)
urbanc@18106
   384
(*A*)
urbanc@18106
   385
apply(force intro!: pt_fresh_fresh[OF pt_name_inst, OF at_name_inst])
urbanc@18106
   386
done
urbanc@18106
   387
urbanc@18106
   388
lemma typing_valid: 
urbanc@18106
   389
  assumes a: "\<Gamma> \<turnstile> t : \<tau>" 
urbanc@18106
   390
  shows "valid \<Gamma>"
urbanc@18106
   391
using a by (induct, auto dest!: valid_elim)
urbanc@18106
   392
urbanc@18106
   393
lemma ty_subs[rule_format]:
urbanc@18106
   394
  fixes \<Gamma> ::"(name\<times>ty) list"
urbanc@18106
   395
  and   t1 ::"lam"
urbanc@18106
   396
  and   t2 ::"lam"
urbanc@18106
   397
  and   \<tau>  ::"ty"
urbanc@18106
   398
  and   \<sigma>  ::"ty" 
urbanc@18106
   399
  and   c  ::"name"
urbanc@18106
   400
  shows  "((c,\<sigma>)#\<Gamma>) \<turnstile> t1:\<tau>\<longrightarrow> \<Gamma>\<turnstile> t2:\<sigma>\<longrightarrow> \<Gamma> \<turnstile> t1[c::=t2]:\<tau>"
urbanc@18313
   401
proof(nominal_induct t1 avoiding: \<Gamma> \<sigma> \<tau> c t2 rule: lam_induct)
urbanc@18313
   402
  case (Var a) 
urbanc@18106
   403
  show ?case
urbanc@18106
   404
  proof(intro strip)
urbanc@18106
   405
    assume a1: "\<Gamma> \<turnstile>t2:\<sigma>"
urbanc@18106
   406
    assume a2: "((c,\<sigma>)#\<Gamma>) \<turnstile> Var a:\<tau>"
urbanc@18106
   407
    hence a21: "(a,\<tau>)\<in>set((c,\<sigma>)#\<Gamma>)" and a22: "valid((c,\<sigma>)#\<Gamma>)" by (auto dest: t1_elim)
urbanc@18106
   408
    from a22 have a23: "valid \<Gamma>" and a24: "c\<sharp>\<Gamma>" by (auto dest: valid_elim) 
urbanc@18106
   409
    from a24 have a25: "\<not>(\<exists>\<tau>. (c,\<tau>)\<in>set \<Gamma>)" by (rule fresh_context)
urbanc@18106
   410
    show "\<Gamma>\<turnstile>(Var a)[c::=t2] : \<tau>"
urbanc@18106
   411
    proof (cases "a=c", simp_all)
urbanc@18106
   412
      assume case1: "a=c"
urbanc@18106
   413
      show "\<Gamma> \<turnstile> t2:\<tau>" using a1
urbanc@18106
   414
      proof (cases "\<sigma>=\<tau>")
urbanc@18106
   415
	assume "\<sigma>=\<tau>" thus ?thesis using a1 by simp 
urbanc@18106
   416
      next
urbanc@18106
   417
	assume a3: "\<sigma>\<noteq>\<tau>"
urbanc@18106
   418
	show ?thesis
urbanc@18106
   419
	proof (rule ccontr)
urbanc@18106
   420
	  from a3 a21 have "(a,\<tau>)\<in>set \<Gamma>" by force
urbanc@18106
   421
	  with case1 a25 show False by force 
urbanc@18106
   422
	qed
urbanc@18106
   423
      qed
urbanc@18106
   424
    next
urbanc@18106
   425
      assume case2: "a\<noteq>c"
urbanc@18106
   426
      with a21 have a26: "(a,\<tau>)\<in>set \<Gamma>" by force 
urbanc@18106
   427
      from a23 a26 show "\<Gamma> \<turnstile> Var a:\<tau>" by force
urbanc@18106
   428
    qed
urbanc@18106
   429
  qed
urbanc@18106
   430
next
urbanc@18313
   431
  case (App s1 s2)
urbanc@18106
   432
  show ?case
urbanc@18106
   433
  proof (intro strip, simp)
urbanc@18106
   434
    assume b1: "\<Gamma> \<turnstile>t2:\<sigma>" 
urbanc@18106
   435
    assume b2: " ((c,\<sigma>)#\<Gamma>)\<turnstile>App s1 s2 : \<tau>"
urbanc@18106
   436
    hence "\<exists>\<tau>'. (((c,\<sigma>)#\<Gamma>)\<turnstile>s1:\<tau>'\<rightarrow>\<tau> \<and> ((c,\<sigma>)#\<Gamma>)\<turnstile>s2:\<tau>')" by (rule t2_elim) 
urbanc@18106
   437
    then obtain \<tau>' where b3a: "((c,\<sigma>)#\<Gamma>)\<turnstile>s1:\<tau>'\<rightarrow>\<tau>" and b3b: "((c,\<sigma>)#\<Gamma>)\<turnstile>s2:\<tau>'" by force
urbanc@18106
   438
    show "\<Gamma> \<turnstile>  App (s1[c::=t2]) (s2[c::=t2]) : \<tau>" 
urbanc@18106
   439
      using b1 b3a b3b App by (rule_tac \<tau>="\<tau>'" in t2, auto)
urbanc@18106
   440
  qed
urbanc@18106
   441
next
urbanc@18313
   442
  case (Lam a s)
urbanc@18313
   443
  have "a\<sharp>\<Gamma>" "a\<sharp>\<sigma>" "a\<sharp>\<tau>" "a\<sharp>c" "a\<sharp>t2" by fact 
urbanc@18106
   444
  hence f1: "a\<sharp>\<Gamma>" and f2: "a\<noteq>c" and f2': "c\<sharp>a" and f3: "a\<sharp>t2" and f4: "a\<sharp>((c,\<sigma>)#\<Gamma>)"
urbanc@18106
   445
    by (auto simp add: fresh_atm fresh_prod fresh_list_cons)
urbanc@18106
   446
  show ?case using f2 f3
urbanc@18106
   447
  proof(intro strip, simp)
urbanc@18106
   448
    assume c1: "((c,\<sigma>)#\<Gamma>)\<turnstile>Lam [a].s : \<tau>"
urbanc@18106
   449
    hence "\<exists>\<tau>1 \<tau>2. \<tau>=\<tau>1\<rightarrow>\<tau>2 \<and> ((a,\<tau>1)#(c,\<sigma>)#\<Gamma>) \<turnstile> s : \<tau>2" using f4 by (auto dest: t3_elim) 
urbanc@18106
   450
    then obtain \<tau>1 \<tau>2 where c11: "\<tau>=\<tau>1\<rightarrow>\<tau>2" and c12: "((a,\<tau>1)#(c,\<sigma>)#\<Gamma>) \<turnstile> s : \<tau>2" by force
urbanc@18106
   451
    from c12 have "valid ((a,\<tau>1)#(c,\<sigma>)#\<Gamma>)" by (rule typing_valid)
urbanc@18106
   452
    hence ca: "valid \<Gamma>" and cb: "a\<sharp>\<Gamma>" and cc: "c\<sharp>\<Gamma>" 
urbanc@18106
   453
      by (auto dest: valid_elim simp add: fresh_list_cons) 
urbanc@18106
   454
    from c12 have c14: "((c,\<sigma>)#(a,\<tau>1)#\<Gamma>) \<turnstile> s : \<tau>2"
urbanc@18106
   455
    proof -
urbanc@18106
   456
      have c2: "((a,\<tau>1)#(c,\<sigma>)#\<Gamma>) \<lless> ((c,\<sigma>)#(a,\<tau>1)#\<Gamma>)" by (force simp add: sub_def)
urbanc@18106
   457
      have c3: "valid ((c,\<sigma>)#(a,\<tau>1)#\<Gamma>)"
urbanc@18106
   458
	by (rule v2, rule v2, auto simp add: fresh_list_cons fresh_prod ca cb cc f2' fresh_ty)
urbanc@18106
   459
      from c12 c2 c3 show ?thesis by (force intro: weakening)
urbanc@18106
   460
    qed
urbanc@18106
   461
    assume c8: "\<Gamma> \<turnstile> t2 : \<sigma>"
urbanc@18106
   462
    have c81: "((a,\<tau>1)#\<Gamma>)\<turnstile>t2 :\<sigma>"
urbanc@18106
   463
    proof -
urbanc@18106
   464
      have c82: "\<Gamma> \<lless> ((a,\<tau>1)#\<Gamma>)" by (force simp add: sub_def)
urbanc@18106
   465
      have c83: "valid ((a,\<tau>1)#\<Gamma>)" using f1 ca by force
urbanc@18106
   466
      with c8 c82 c83 show ?thesis by (force intro: weakening)
urbanc@18106
   467
    qed
urbanc@18106
   468
    show "\<Gamma> \<turnstile> Lam [a].(s[c::=t2]) : \<tau>"
urbanc@18106
   469
      using c11 Lam c14 c81 f1 by force
urbanc@18106
   470
  qed
urbanc@18106
   471
qed
urbanc@18106
   472
urbanc@18106
   473
lemma subject[rule_format]: 
urbanc@18106
   474
  fixes \<Gamma>  ::"(name\<times>ty) list"
urbanc@18106
   475
  and   t1 ::"lam"
urbanc@18106
   476
  and   t2 ::"lam"
urbanc@18106
   477
  and   \<tau>  ::"ty"
urbanc@18106
   478
  assumes a: "t1\<longrightarrow>\<^isub>\<beta>t2"
urbanc@18106
   479
  shows "(\<Gamma> \<turnstile> t1:\<tau>) \<longrightarrow> (\<Gamma> \<turnstile> t2:\<tau>)"
urbanc@18106
   480
using a
urbanc@18313
   481
proof (nominal_induct t1 t2 avoiding: \<Gamma> \<tau> rule: beta_induct, auto)
urbanc@18313
   482
  case (b1 t s1 s2)
urbanc@18313
   483
  have i: "\<And>\<Gamma> \<tau>. \<Gamma> \<turnstile> s1:\<tau> \<longrightarrow> \<Gamma> \<turnstile> s2 : \<tau>" by fact
urbanc@18313
   484
  assume "\<Gamma> \<turnstile> App s1 t : \<tau>" 
urbanc@18106
   485
  hence "\<exists>\<sigma>. (\<Gamma> \<turnstile> s1 : \<sigma>\<rightarrow>\<tau> \<and> \<Gamma> \<turnstile> t : \<sigma>)" by (rule t2_elim)
urbanc@18106
   486
  then obtain \<sigma> where a1: "\<Gamma> \<turnstile> s1 : \<sigma>\<rightarrow>\<tau>" and a2: "\<Gamma> \<turnstile> t : \<sigma>" by force
urbanc@18106
   487
  thus "\<Gamma> \<turnstile> App s2 t : \<tau>" using i by force
urbanc@18106
   488
next
urbanc@18313
   489
  case (b2 t s1 s2)
urbanc@18313
   490
  have i: "\<And>\<Gamma> \<tau>. \<Gamma> \<turnstile> s1 : \<tau> \<longrightarrow> \<Gamma> \<turnstile> s2 : \<tau>" by fact 
urbanc@18313
   491
  assume "\<Gamma> \<turnstile> App t s1 : \<tau>" 
urbanc@18106
   492
  hence "\<exists>\<sigma>. (\<Gamma> \<turnstile> t : \<sigma>\<rightarrow>\<tau> \<and> \<Gamma> \<turnstile> s1 : \<sigma>)" by (rule t2_elim)
urbanc@18106
   493
  then obtain \<sigma> where a1: "\<Gamma> \<turnstile> t : \<sigma>\<rightarrow>\<tau>" and a2: "\<Gamma> \<turnstile> s1 : \<sigma>" by force
urbanc@18106
   494
  thus "\<Gamma> \<turnstile> App t s2 : \<tau>" using i by force
urbanc@18106
   495
next
urbanc@18313
   496
  case (b3 a s1 s2)
urbanc@18313
   497
  have f: "a\<sharp>\<Gamma>" and "a\<sharp>\<tau>" by fact+
urbanc@18313
   498
  have i: "\<And>\<Gamma> \<tau>. \<Gamma> \<turnstile> s1 : \<tau> \<longrightarrow> \<Gamma> \<turnstile> s2 : \<tau>" by fact
urbanc@18106
   499
  assume "\<Gamma> \<turnstile> Lam [a].s1 : \<tau>"
urbanc@18106
   500
  with f have "\<exists>\<tau>1 \<tau>2. \<tau>=\<tau>1\<rightarrow>\<tau>2 \<and> ((a,\<tau>1)#\<Gamma>) \<turnstile> s1 : \<tau>2" by (force dest: t3_elim)
urbanc@18106
   501
  then obtain \<tau>1 \<tau>2 where a1: "\<tau>=\<tau>1\<rightarrow>\<tau>2" and a2: "((a,\<tau>1)#\<Gamma>) \<turnstile> s1 : \<tau>2" by force
urbanc@18106
   502
  thus "\<Gamma> \<turnstile> Lam [a].s2 : \<tau>" using f i by force 
urbanc@18106
   503
next
urbanc@18313
   504
  case (b4 a s1 s2)
urbanc@18313
   505
  have f: "a\<sharp>\<Gamma>" by fact
urbanc@18106
   506
  assume "\<Gamma> \<turnstile> App (Lam [a].s1) s2 : \<tau>"
urbanc@18106
   507
  hence "\<exists>\<sigma>. (\<Gamma> \<turnstile> (Lam [a].s1) : \<sigma>\<rightarrow>\<tau> \<and> \<Gamma> \<turnstile> s2 : \<sigma>)" by (rule t2_elim)
urbanc@18106
   508
  then obtain \<sigma> where a1: "\<Gamma> \<turnstile> (Lam [(a::name)].s1) : \<sigma>\<rightarrow>\<tau>" and a2: "\<Gamma> \<turnstile> s2 : \<sigma>" by force
urbanc@18106
   509
  have  "((a,\<sigma>)#\<Gamma>) \<turnstile> s1 : \<tau>" using a1 f by (auto dest!: t3_elim)
urbanc@18106
   510
  with a2 show "\<Gamma> \<turnstile>  s1[a::=s2] : \<tau>" by (force intro: ty_subs)
urbanc@18106
   511
qed
urbanc@18106
   512
urbanc@18106
   513
lemma subject[rule_format]: 
urbanc@18106
   514
  fixes \<Gamma>  ::"(name\<times>ty) list"
urbanc@18106
   515
  and   t1 ::"lam"
urbanc@18106
   516
  and   t2 ::"lam"
urbanc@18106
   517
  and   \<tau>  ::"ty"
urbanc@18106
   518
  assumes a: "t1\<longrightarrow>\<^isub>\<beta>t2"
urbanc@18106
   519
  shows "\<Gamma> \<turnstile> t1:\<tau> \<longrightarrow> \<Gamma> \<turnstile> t2:\<tau>"
urbanc@18106
   520
using a
urbanc@18313
   521
apply(nominal_induct t1 t2 avoiding: \<Gamma> \<tau> rule: beta_induct)
urbanc@18313
   522
apply(auto dest!: t2_elim t3_elim intro: ty_subs)
urbanc@18106
   523
done
urbanc@18106
   524
urbanc@18106
   525
subsection {* some facts about beta *}
urbanc@18106
   526
urbanc@18106
   527
constdefs
urbanc@18106
   528
  "NORMAL" :: "lam \<Rightarrow> bool"
urbanc@18106
   529
  "NORMAL t \<equiv> \<not>(\<exists>t'. t\<longrightarrow>\<^isub>\<beta> t')"
urbanc@18106
   530
urbanc@18106
   531
constdefs
urbanc@18106
   532
  "SN" :: "lam \<Rightarrow> bool"
urbanc@18106
   533
  "SN t \<equiv> t\<in>termi Beta"
urbanc@18106
   534
urbanc@18106
   535
lemma qq1: "\<lbrakk>SN(t1);t1\<longrightarrow>\<^isub>\<beta> t2\<rbrakk>\<Longrightarrow>SN(t2)"
urbanc@18106
   536
apply(simp add: SN_def)
urbanc@18106
   537
apply(drule_tac a="t2" in acc_downward)
urbanc@18106
   538
apply(auto)
urbanc@18106
   539
done
urbanc@18106
   540
urbanc@18106
   541
lemma qq2: "(\<forall>t2. t1\<longrightarrow>\<^isub>\<beta>t2 \<longrightarrow> SN(t2))\<Longrightarrow>SN(t1)"
urbanc@18106
   542
apply(simp add: SN_def)
urbanc@18106
   543
apply(rule accI)
urbanc@18106
   544
apply(auto)
urbanc@18106
   545
done
urbanc@18106
   546
urbanc@18106
   547
urbanc@18106
   548
section {* Candidates *}
urbanc@18106
   549
urbanc@18106
   550
consts
urbanc@18106
   551
  RED :: "ty \<Rightarrow> lam set"
urbanc@18106
   552
primrec
urbanc@18106
   553
 "RED (TVar X) = {t. SN(t)}"
urbanc@18106
   554
 "RED (\<tau>\<rightarrow>\<sigma>) =   {t. \<forall>u. (u\<in>RED \<tau> \<longrightarrow> (App t u)\<in>RED \<sigma>)}"
urbanc@18106
   555
urbanc@18106
   556
constdefs
urbanc@18106
   557
  NEUT :: "lam \<Rightarrow> bool"
urbanc@18106
   558
  "NEUT t \<equiv> (\<exists>a. t=Var a)\<or>(\<exists>t1 t2. t=App t1 t2)" 
urbanc@18106
   559
urbanc@18106
   560
(* a slight hack to get the first element of applications *)
urbanc@18106
   561
consts
urbanc@18106
   562
  FST :: "(lam\<times>lam) set"
urbanc@18106
   563
syntax 
urbanc@18106
   564
  "FST_judge"   :: "lam\<Rightarrow>lam\<Rightarrow>bool" (" _ \<guillemotright> _" [80,80] 80)
urbanc@18106
   565
translations 
urbanc@18106
   566
  "t1 \<guillemotright> t2" \<rightleftharpoons> "(t1,t2) \<in> FST"
urbanc@18106
   567
inductive FST
urbanc@18106
   568
intros
urbanc@18106
   569
fst[intro!]:  "(App t s) \<guillemotright> t"
urbanc@18106
   570
urbanc@18106
   571
lemma fst_elim[elim!]: "(App t s) \<guillemotright> t' \<Longrightarrow> t=t'"
urbanc@18106
   572
apply(ind_cases "App t s \<guillemotright> t'")
urbanc@18106
   573
apply(simp add: lam.inject)
urbanc@18106
   574
done
urbanc@18106
   575
urbanc@18106
   576
lemma qq3: "SN(App t s)\<Longrightarrow>SN(t)"
urbanc@18106
   577
apply(simp add: SN_def)
urbanc@18106
   578
apply(subgoal_tac "\<forall>z. (App t s \<guillemotright> z) \<longrightarrow> z\<in>termi Beta")(*A*)
urbanc@18106
   579
apply(force)
urbanc@18106
   580
(*A*)
urbanc@18106
   581
apply(erule acc_induct)
urbanc@18106
   582
apply(clarify)
urbanc@18106
   583
apply(ind_cases "x \<guillemotright> z")
urbanc@18106
   584
apply(clarify)
urbanc@18106
   585
apply(rule accI)
urbanc@18106
   586
apply(auto intro: b1)
urbanc@18106
   587
done
urbanc@18106
   588
urbanc@18106
   589
constdefs
urbanc@18106
   590
   "CR1" :: "ty \<Rightarrow> bool"
urbanc@18106
   591
   "CR1 \<tau> \<equiv> \<forall> t. (t\<in>RED \<tau> \<longrightarrow> SN(t))"
urbanc@18106
   592
urbanc@18106
   593
   "CR2" :: "ty \<Rightarrow> bool"
urbanc@18106
   594
   "CR2 \<tau> \<equiv> \<forall>t t'. ((t\<in>RED \<tau> \<and> t \<longrightarrow>\<^isub>\<beta> t') \<longrightarrow> t'\<in>RED \<tau>)"
urbanc@18106
   595
urbanc@18106
   596
   "CR3_RED" :: "lam \<Rightarrow> ty \<Rightarrow> bool"
urbanc@18106
   597
   "CR3_RED t \<tau> \<equiv> \<forall>t'. (t\<longrightarrow>\<^isub>\<beta> t' \<longrightarrow>  t'\<in>RED \<tau>)" 
urbanc@18106
   598
urbanc@18106
   599
   "CR3" :: "ty \<Rightarrow> bool"
urbanc@18106
   600
   "CR3 \<tau> \<equiv> \<forall>t. (NEUT t \<and> CR3_RED t \<tau>) \<longrightarrow> t\<in>RED \<tau>"
urbanc@18106
   601
   
urbanc@18106
   602
   "CR4" :: "ty \<Rightarrow> bool"
urbanc@18106
   603
   "CR4 \<tau> \<equiv> \<forall>t. (NEUT t \<and> NORMAL t) \<longrightarrow>t\<in>RED \<tau>"
urbanc@18106
   604
urbanc@18106
   605
lemma CR3_CR4: "CR3 \<tau> \<Longrightarrow> CR4 \<tau>"
urbanc@18106
   606
apply(simp (no_asm_use) add: CR3_def CR3_RED_def CR4_def NORMAL_def)
urbanc@18106
   607
apply(blast)
urbanc@18106
   608
done
urbanc@18106
   609
urbanc@18106
   610
lemma sub_ind: 
urbanc@18106
   611
  "SN(u)\<Longrightarrow>(u\<in>RED \<tau>\<longrightarrow>(\<forall>t. (NEUT t\<and>CR2 \<tau>\<and>CR3 \<sigma>\<and>CR3_RED t (\<tau>\<rightarrow>\<sigma>))\<longrightarrow>(App t u)\<in>RED \<sigma>))"
urbanc@18106
   612
apply(simp add: SN_def)
urbanc@18106
   613
apply(erule acc_induct)
urbanc@18106
   614
apply(auto)
urbanc@18106
   615
apply(simp add: CR3_def)
urbanc@18106
   616
apply(rotate_tac 5)
urbanc@18106
   617
apply(drule_tac x="App t x" in spec)
urbanc@18106
   618
apply(drule mp)
urbanc@18106
   619
apply(rule conjI)
urbanc@18106
   620
apply(force simp only: NEUT_def)
urbanc@18106
   621
apply(simp (no_asm) add: CR3_RED_def)
urbanc@18106
   622
apply(clarify)
urbanc@18106
   623
apply(ind_cases "App t x \<longrightarrow>\<^isub>\<beta> t'")
urbanc@18106
   624
apply(simp_all add: lam.inject)
urbanc@18106
   625
apply(simp only:  CR3_RED_def)
urbanc@18106
   626
apply(drule_tac x="s2" in spec)
urbanc@18106
   627
apply(simp)
urbanc@18106
   628
apply(drule_tac x="s2" in spec)
urbanc@18106
   629
apply(simp)
urbanc@18106
   630
apply(drule mp)
urbanc@18106
   631
apply(simp (no_asm_use) add: CR2_def)
urbanc@18106
   632
apply(blast)
urbanc@18106
   633
apply(drule_tac x="ta" in spec)
urbanc@18106
   634
apply(force)
urbanc@18106
   635
apply(auto simp only: NEUT_def lam.inject lam.distinct)
urbanc@18106
   636
done
urbanc@18106
   637
urbanc@18106
   638
lemma RED_props: "CR1 \<tau> \<and> CR2 \<tau> \<and> CR3 \<tau>"
urbanc@18106
   639
apply(induct_tac \<tau>)
urbanc@18106
   640
apply(auto)
urbanc@18106
   641
(* atom types *)
urbanc@18106
   642
(* C1 *)
urbanc@18106
   643
apply(simp add: CR1_def)
urbanc@18106
   644
(* C2 *)
urbanc@18106
   645
apply(simp add: CR2_def)
urbanc@18106
   646
apply(clarify)
urbanc@18106
   647
apply(drule_tac ?t2.0="t'" in  qq1)
urbanc@18106
   648
apply(assumption)+
urbanc@18106
   649
(* C3 *)
urbanc@18106
   650
apply(simp add: CR3_def CR3_RED_def)
urbanc@18106
   651
apply(clarify)
urbanc@18106
   652
apply(rule qq2)
urbanc@18106
   653
apply(assumption)
urbanc@18106
   654
(* arrow types *)
urbanc@18106
   655
(* C1 *)
urbanc@18106
   656
apply(simp (no_asm) add: CR1_def)
urbanc@18106
   657
apply(clarify)
urbanc@18106
   658
apply(subgoal_tac "NEUT (Var a)")(*A*)
urbanc@18106
   659
apply(subgoal_tac "(Var a)\<in>RED ty1")(*C*)
urbanc@18106
   660
apply(drule_tac x="Var a" in spec)
urbanc@18106
   661
apply(simp)
urbanc@18106
   662
apply(simp add: CR1_def)
urbanc@18106
   663
apply(rotate_tac 1)
urbanc@18106
   664
apply(drule_tac x="App t (Var a)" in spec)
urbanc@18106
   665
apply(simp)
urbanc@18106
   666
apply(drule qq3) 
urbanc@18106
   667
apply(assumption)
urbanc@18106
   668
(*C*)
urbanc@18106
   669
apply(simp (no_asm_use) add: CR3_def CR3_RED_def)
urbanc@18106
   670
apply(drule_tac x="Var a" in spec)
urbanc@18106
   671
apply(drule mp)
urbanc@18106
   672
apply(clarify)
urbanc@18106
   673
apply(ind_cases " Var a \<longrightarrow>\<^isub>\<beta> t'")
urbanc@18106
   674
apply(simp (no_asm_use) add: lam.distinct)+ 
urbanc@18106
   675
(*A*)
urbanc@18106
   676
apply(simp (no_asm) only: NEUT_def)
urbanc@18106
   677
apply(rule disjCI)
urbanc@18106
   678
apply(rule_tac x="a" in exI)
urbanc@18106
   679
apply(simp (no_asm))
urbanc@18106
   680
(* C2 *)
urbanc@18106
   681
apply(simp (no_asm) add: CR2_def)
urbanc@18106
   682
apply(clarify)
urbanc@18106
   683
apply(drule_tac x="u" in spec)
urbanc@18106
   684
apply(simp)
urbanc@18106
   685
apply(subgoal_tac "App t u \<longrightarrow>\<^isub>\<beta> App t' u")(*X*)
urbanc@18106
   686
apply(simp (no_asm_use) only: CR2_def)
urbanc@18106
   687
apply(blast)
urbanc@18106
   688
(*X*)
urbanc@18106
   689
apply(force intro!: b1)
urbanc@18106
   690
(* C3 *)
urbanc@18106
   691
apply(unfold CR3_def)
urbanc@18106
   692
apply(rule allI)
urbanc@18106
   693
apply(rule impI)
urbanc@18106
   694
apply(erule conjE)
urbanc@18106
   695
apply(simp (no_asm))
urbanc@18106
   696
apply(rule allI)
urbanc@18106
   697
apply(rule impI)
urbanc@18106
   698
apply(subgoal_tac "SN(u)")(*Z*)
urbanc@18106
   699
apply(fold CR3_def)
urbanc@18106
   700
apply(drule_tac \<tau>="ty1" and \<sigma>="ty2" in sub_ind)
urbanc@18106
   701
apply(simp)
urbanc@18106
   702
(*Z*)
urbanc@18106
   703
apply(simp add: CR1_def)
urbanc@18106
   704
done
urbanc@18106
   705
urbanc@18106
   706
lemma double_acc_aux:
urbanc@18106
   707
  assumes a_acc: "a \<in> acc r"
urbanc@18106
   708
  and b_acc: "b \<in> acc r"
urbanc@18106
   709
  and hyp: "\<And>x z.
urbanc@18106
   710
    (\<And>y. (y, x) \<in> r \<Longrightarrow> y \<in> acc r) \<Longrightarrow>
urbanc@18106
   711
    (\<And>y. (y, x) \<in> r \<Longrightarrow> P y z) \<Longrightarrow>
urbanc@18106
   712
    (\<And>u. (u, z) \<in> r \<Longrightarrow> u \<in> acc r) \<Longrightarrow>
urbanc@18106
   713
    (\<And>u. (u, z) \<in> r \<Longrightarrow> P x u) \<Longrightarrow> P x z"
urbanc@18106
   714
  shows "P a b"
urbanc@18106
   715
proof -
urbanc@18106
   716
  from a_acc
urbanc@18106
   717
  have r: "\<And>b. b \<in> acc r \<Longrightarrow> P a b"
urbanc@18106
   718
  proof (induct a rule: acc.induct)
urbanc@18106
   719
    case (accI x)
urbanc@18106
   720
    note accI' = accI
urbanc@18106
   721
    have "b \<in> acc r" .
urbanc@18106
   722
    thus ?case
urbanc@18106
   723
    proof (induct b rule: acc.induct)
urbanc@18106
   724
      case (accI y)
urbanc@18106
   725
      show ?case
urbanc@18106
   726
	apply (rule hyp)
urbanc@18106
   727
	apply (erule accI')
urbanc@18106
   728
	apply (erule accI')
urbanc@18106
   729
	apply (rule acc.accI)
urbanc@18106
   730
	apply (erule accI)
urbanc@18106
   731
	apply (erule accI)
urbanc@18106
   732
	apply (erule accI)
urbanc@18106
   733
	done
urbanc@18106
   734
    qed
urbanc@18106
   735
  qed
urbanc@18106
   736
  from b_acc show ?thesis by (rule r)
urbanc@18106
   737
qed
urbanc@18106
   738
urbanc@18106
   739
lemma double_acc:
urbanc@18106
   740
  "\<lbrakk>a \<in> acc r; b \<in> acc r; \<forall>x z. ((\<forall>y. (y, x)\<in>r\<longrightarrow>P y z)\<and>(\<forall>u. (u, z)\<in>r\<longrightarrow>P x u))\<longrightarrow>P x z\<rbrakk>\<Longrightarrow>P a b"
urbanc@18106
   741
apply(rule_tac r="r" in double_acc_aux)
urbanc@18106
   742
apply(assumption)+
urbanc@18106
   743
apply(blast)
urbanc@18106
   744
done
urbanc@18106
   745
urbanc@18263
   746
lemma abs_RED: "(\<forall>s\<in>RED \<tau>. t[x::=s]\<in>RED \<sigma>)\<longrightarrow>Lam [x].t\<in>RED (\<tau>\<rightarrow>\<sigma>)"
urbanc@18106
   747
apply(simp)
urbanc@18106
   748
apply(clarify)
urbanc@18106
   749
apply(subgoal_tac "t\<in>termi Beta")(*1*)
urbanc@18106
   750
apply(erule rev_mp)
urbanc@18106
   751
apply(subgoal_tac "u \<in> RED \<tau>")(*A*)
urbanc@18106
   752
apply(erule rev_mp)
urbanc@18106
   753
apply(rule_tac a="t" and b="u" in double_acc)
urbanc@18106
   754
apply(assumption)
urbanc@18106
   755
apply(subgoal_tac "CR1 \<tau>")(*A*)
urbanc@18106
   756
apply(simp add: CR1_def SN_def)
urbanc@18106
   757
(*A*)
urbanc@18106
   758
apply(force simp add: RED_props)
urbanc@18106
   759
apply(simp)
urbanc@18106
   760
apply(clarify)
urbanc@18106
   761
apply(subgoal_tac "CR3 \<sigma>")(*B*)
urbanc@18106
   762
apply(simp add: CR3_def)
urbanc@18106
   763
apply(rotate_tac 6)
urbanc@18106
   764
apply(drule_tac x="App(Lam[x].xa ) z" in spec)
urbanc@18106
   765
apply(drule mp)
urbanc@18106
   766
apply(rule conjI)
urbanc@18106
   767
apply(force simp add: NEUT_def)
urbanc@18106
   768
apply(simp add: CR3_RED_def)
urbanc@18106
   769
apply(clarify)
urbanc@18106
   770
apply(ind_cases "App(Lam[x].xa) z \<longrightarrow>\<^isub>\<beta> t'")
urbanc@18106
   771
apply(auto simp add: lam.inject lam.distinct)
urbanc@18106
   772
apply(drule beta_abs)
urbanc@18106
   773
apply(auto)
urbanc@18106
   774
apply(drule_tac x="t''" in spec)
urbanc@18106
   775
apply(simp)
urbanc@18106
   776
apply(drule mp)
urbanc@18106
   777
apply(clarify)
urbanc@18106
   778
apply(drule_tac x="s" in bspec)
urbanc@18106
   779
apply(assumption)
urbanc@18106
   780
apply(subgoal_tac "xa [ x ::= s ] \<longrightarrow>\<^isub>\<beta>  t'' [ x ::= s ]")(*B*)
urbanc@18106
   781
apply(subgoal_tac "CR2 \<sigma>")(*C*)
urbanc@18106
   782
apply(simp (no_asm_use) add: CR2_def)
urbanc@18106
   783
apply(blast)
urbanc@18106
   784
(*C*)
urbanc@18106
   785
apply(force simp add: RED_props)
urbanc@18106
   786
(*B*)
urbanc@18106
   787
apply(force intro!: beta_subst)
urbanc@18106
   788
apply(assumption)
urbanc@18106
   789
apply(rotate_tac 3)
urbanc@18106
   790
apply(drule_tac x="s2" in spec)
urbanc@18106
   791
apply(subgoal_tac "s2\<in>RED \<tau>")(*D*)
urbanc@18106
   792
apply(simp)
urbanc@18106
   793
(*D*)
urbanc@18106
   794
apply(subgoal_tac "CR2 \<tau>")(*E*)
urbanc@18106
   795
apply(simp (no_asm_use) add: CR2_def)
urbanc@18106
   796
apply(blast)
urbanc@18106
   797
(*E*)
urbanc@18106
   798
apply(force simp add: RED_props)
urbanc@18106
   799
apply(simp add: alpha)
urbanc@18106
   800
apply(erule disjE)
urbanc@18106
   801
apply(force)
urbanc@18106
   802
apply(auto)
urbanc@18106
   803
apply(simp add: subst_rename)
urbanc@18106
   804
apply(drule_tac x="z" in bspec)
urbanc@18106
   805
apply(assumption)
urbanc@18106
   806
(*B*)
urbanc@18106
   807
apply(force simp add: RED_props)
urbanc@18106
   808
(*1*)
urbanc@18106
   809
apply(drule_tac x="Var x" in bspec)
urbanc@18106
   810
apply(subgoal_tac "CR3 \<tau>")(*2*) 
urbanc@18106
   811
apply(drule CR3_CR4)
urbanc@18106
   812
apply(simp add: CR4_def)
urbanc@18106
   813
apply(drule_tac x="Var x" in spec)
urbanc@18106
   814
apply(drule mp)
urbanc@18106
   815
apply(rule conjI)
urbanc@18106
   816
apply(force simp add: NEUT_def)
urbanc@18106
   817
apply(simp add: NORMAL_def)
urbanc@18106
   818
apply(clarify)
urbanc@18106
   819
apply(ind_cases "Var x \<longrightarrow>\<^isub>\<beta> t'")
urbanc@18106
   820
apply(auto simp add: lam.inject lam.distinct)
urbanc@18106
   821
apply(force simp add: RED_props)
urbanc@18106
   822
apply(simp add: id_subs)
urbanc@18106
   823
apply(subgoal_tac "CR1 \<sigma>")(*3*)
urbanc@18106
   824
apply(simp add: CR1_def SN_def)
urbanc@18106
   825
(*3*)
urbanc@18106
   826
apply(force simp add: RED_props)
urbanc@18106
   827
done
urbanc@18106
   828
urbanc@18263
   829
lemma fresh_domain[rule_format]: "a\<sharp>\<theta>\<longrightarrow>a\<notin>set(domain \<theta>)"
urbanc@18263
   830
apply(induct_tac \<theta>)
urbanc@18263
   831
apply(auto simp add: fresh_prod fresh_list_cons fresh_atm)
urbanc@18263
   832
done
urbanc@18263
   833
urbanc@18263
   834
lemma fresh_at[rule_format]: "a\<in>set(domain \<theta>) \<longrightarrow> c\<sharp>\<theta>\<longrightarrow>c\<sharp>(\<theta><a>)"
urbanc@18263
   835
apply(induct_tac \<theta>)   
urbanc@18263
   836
apply(auto simp add: fresh_prod fresh_list_cons)
urbanc@18263
   837
done
urbanc@18263
   838
urbanc@18345
   839
lemma psubst_subst[rule_format]: "c\<sharp>\<theta>\<longrightarrow> (t[<\<theta>>])[c::=s] = t[<((c,s)#\<theta>)>]"
urbanc@18313
   840
apply(nominal_induct t avoiding: \<theta> c s rule: lam_induct)
urbanc@18263
   841
apply(auto dest: fresh_domain)
urbanc@18263
   842
apply(drule fresh_at)
urbanc@18263
   843
apply(assumption)
urbanc@18263
   844
apply(rule forget)
urbanc@18263
   845
apply(assumption)
urbanc@18313
   846
apply(subgoal_tac "a\<sharp>((c,s)#\<theta>)")(*A*)
urbanc@18313
   847
apply(simp)
urbanc@18263
   848
(*A*)
urbanc@18263
   849
apply(simp add: fresh_list_cons fresh_prod)
urbanc@18263
   850
done
urbanc@18263
   851
urbanc@18345
   852
thm fresh_context
urbanc@18345
   853
urbanc@18106
   854
lemma all_RED: 
urbanc@18345
   855
  assumes a: "\<Gamma>\<turnstile>t:\<tau>"
urbanc@18345
   856
  and     b: "\<forall>a \<sigma>. (a,\<sigma>)\<in>set(\<Gamma>) \<longrightarrow> (a\<in>set(domain \<theta>)\<and>\<theta><a>\<in>RED \<sigma>)" 
urbanc@18345
   857
  shows "t[<\<theta>>]\<in>RED \<tau>"
urbanc@18345
   858
using a b
urbanc@18345
   859
proof(nominal_induct t avoiding: \<Gamma> \<tau> \<theta> rule: lam_induct)
urbanc@18345
   860
  case (Lam a t) --"lambda case"
urbanc@18345
   861
  have ih: "\<And>\<Gamma> \<tau> \<theta>. \<Gamma> \<turnstile> t:\<tau> \<Longrightarrow> 
urbanc@18345
   862
                    (\<forall>c \<sigma>. (c,\<sigma>)\<in>set \<Gamma> \<longrightarrow> c\<in>set (domain \<theta>) \<and>  \<theta><c>\<in>RED \<sigma>) 
urbanc@18345
   863
                    \<Longrightarrow> t[<\<theta>>]\<in>RED \<tau>" 
urbanc@18345
   864
  and  \<theta>_cond: "\<forall>c \<sigma>. (c,\<sigma>)\<in>set \<Gamma> \<longrightarrow> c\<in>set (domain \<theta>) \<and>  \<theta><c>\<in>RED \<sigma>" 
urbanc@18345
   865
  and fresh: "a\<sharp>\<Gamma>" "a\<sharp>\<theta>" 
urbanc@18345
   866
  and "\<Gamma> \<turnstile> Lam [a].t:\<tau>" by fact
urbanc@18345
   867
  hence "\<exists>\<tau>1 \<tau>2. \<tau>=\<tau>1\<rightarrow>\<tau>2 \<and> ((a,\<tau>1)#\<Gamma>)\<turnstile>t:\<tau>2" using t3_elim fresh by simp
urbanc@18345
   868
  then obtain \<tau>1 \<tau>2 where \<tau>_inst: "\<tau>=\<tau>1\<rightarrow>\<tau>2" and typing: "((a,\<tau>1)#\<Gamma>)\<turnstile>t:\<tau>2" by blast
urbanc@18345
   869
  from ih have "\<forall>s\<in>RED \<tau>1. t[<\<theta>>][a::=s] \<in> RED \<tau>2" using fresh typing \<theta>_cond
urbanc@18345
   870
    by (force dest: fresh_context simp add: psubst_subst)
urbanc@18345
   871
  hence "(Lam [a].(t[<\<theta>>])) \<in> RED (\<tau>1 \<rightarrow> \<tau>2)" by (simp only: abs_RED)
urbanc@18345
   872
  thus "(Lam [a].t)[<\<theta>>] \<in> RED \<tau>" using fresh \<tau>_inst by simp
urbanc@18345
   873
qed (force dest!: t1_elim t2_elim)+
urbanc@18345
   874
urbanc@18345
   875
lemma all_RED:
urbanc@18345
   876
  assumes a: "\<Gamma>\<turnstile>t:\<tau>"
urbanc@18345
   877
  and     b: "\<And>a \<sigma>. (a,\<sigma>)\<in>set(\<Gamma>) \<Longrightarrow> (a\<in>set(domain \<theta>)\<and>\<theta><a>\<in>RED \<sigma>)" 
urbanc@18345
   878
  shows "t[<\<theta>>]\<in>RED \<tau>"
urbanc@18345
   879
using a b
urbanc@18313
   880
apply(nominal_induct t avoiding: \<Gamma> \<tau> \<theta> rule: lam_induct)
urbanc@18106
   881
(* Variables *)
urbanc@18106
   882
apply(force dest: t1_elim)
urbanc@18106
   883
(* Applications *)
urbanc@18313
   884
apply(atomize)
urbanc@18313
   885
apply(force dest!: t2_elim)
urbanc@18345
   886
(* Abstractions  *)
urbanc@18345
   887
apply(auto dest!: t3_elim simp only: psubst_Lam)
urbanc@18313
   888
apply(rule abs_RED[THEN mp])
urbanc@18313
   889
apply(force dest: fresh_context simp add: psubs_subs)
urbanc@18345
   890
done