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