src/ZF/Constructible/WF_absolute.thy
author paulson
Tue Jul 09 17:25:42 2002 +0200 (2002-07-09)
changeset 13324 39d1b3a4c6f4
parent 13323 2c287f50c9f3
child 13339 0f89104dd377
permissions -rw-r--r--
more and simpler separation proofs
paulson@13306
     1
header {*Absoluteness for Well-Founded Relations and Well-Founded Recursion*}
paulson@13306
     2
paulson@13242
     3
theory WF_absolute = WFrec:
paulson@13223
     4
paulson@13251
     5
subsection{*Every well-founded relation is a subset of some inverse image of
paulson@13247
     6
      an ordinal*}
paulson@13247
     7
paulson@13247
     8
lemma wf_rvimage_Ord: "Ord(i) \<Longrightarrow> wf(rvimage(A, f, Memrel(i)))"
paulson@13251
     9
by (blast intro: wf_rvimage wf_Memrel)
paulson@13247
    10
paulson@13247
    11
paulson@13247
    12
constdefs
paulson@13247
    13
  wfrank :: "[i,i]=>i"
paulson@13247
    14
    "wfrank(r,a) == wfrec(r, a, %x f. \<Union>y \<in> r-``{x}. succ(f`y))"
paulson@13247
    15
paulson@13247
    16
constdefs
paulson@13247
    17
  wftype :: "i=>i"
paulson@13247
    18
    "wftype(r) == \<Union>y \<in> range(r). succ(wfrank(r,y))"
paulson@13247
    19
paulson@13247
    20
lemma wfrank: "wf(r) ==> wfrank(r,a) = (\<Union>y \<in> r-``{a}. succ(wfrank(r,y)))"
paulson@13247
    21
by (subst wfrank_def [THEN def_wfrec], simp_all)
paulson@13247
    22
paulson@13247
    23
lemma Ord_wfrank: "wf(r) ==> Ord(wfrank(r,a))"
paulson@13247
    24
apply (rule_tac a="a" in wf_induct, assumption)
paulson@13247
    25
apply (subst wfrank, assumption)
paulson@13251
    26
apply (rule Ord_succ [THEN Ord_UN], blast)
paulson@13247
    27
done
paulson@13247
    28
paulson@13247
    29
lemma wfrank_lt: "[|wf(r); <a,b> \<in> r|] ==> wfrank(r,a) < wfrank(r,b)"
paulson@13247
    30
apply (rule_tac a1 = "b" in wfrank [THEN ssubst], assumption)
paulson@13247
    31
apply (rule UN_I [THEN ltI])
paulson@13247
    32
apply (simp add: Ord_wfrank vimage_iff)+
paulson@13247
    33
done
paulson@13247
    34
paulson@13247
    35
lemma Ord_wftype: "wf(r) ==> Ord(wftype(r))"
paulson@13247
    36
by (simp add: wftype_def Ord_wfrank)
paulson@13247
    37
paulson@13247
    38
lemma wftypeI: "\<lbrakk>wf(r);  x \<in> field(r)\<rbrakk> \<Longrightarrow> wfrank(r,x) \<in> wftype(r)"
paulson@13251
    39
apply (simp add: wftype_def)
paulson@13251
    40
apply (blast intro: wfrank_lt [THEN ltD])
paulson@13247
    41
done
paulson@13247
    42
paulson@13247
    43
paulson@13247
    44
lemma wf_imp_subset_rvimage:
paulson@13247
    45
     "[|wf(r); r \<subseteq> A*A|] ==> \<exists>i f. Ord(i) & r <= rvimage(A, f, Memrel(i))"
paulson@13251
    46
apply (rule_tac x="wftype(r)" in exI)
paulson@13251
    47
apply (rule_tac x="\<lambda>x\<in>A. wfrank(r,x)" in exI)
paulson@13251
    48
apply (simp add: Ord_wftype, clarify)
paulson@13251
    49
apply (frule subsetD, assumption, clarify)
paulson@13247
    50
apply (simp add: rvimage_iff wfrank_lt [THEN ltD])
paulson@13251
    51
apply (blast intro: wftypeI)
paulson@13247
    52
done
paulson@13247
    53
paulson@13247
    54
theorem wf_iff_subset_rvimage:
paulson@13247
    55
  "relation(r) ==> wf(r) <-> (\<exists>i f A. Ord(i) & r <= rvimage(A, f, Memrel(i)))"
paulson@13247
    56
by (blast dest!: relation_field_times_field wf_imp_subset_rvimage
paulson@13247
    57
          intro: wf_rvimage_Ord [THEN wf_subset])
paulson@13247
    58
paulson@13247
    59
paulson@13223
    60
subsection{*Transitive closure without fixedpoints*}
paulson@13223
    61
paulson@13223
    62
constdefs
paulson@13223
    63
  rtrancl_alt :: "[i,i]=>i"
paulson@13251
    64
    "rtrancl_alt(A,r) ==
paulson@13223
    65
       {p \<in> A*A. \<exists>n\<in>nat. \<exists>f \<in> succ(n) -> A.
paulson@13242
    66
                 (\<exists>x y. p = <x,y> &  f`0 = x & f`n = y) &
paulson@13223
    67
                       (\<forall>i\<in>n. <f`i, f`succ(i)> \<in> r)}"
paulson@13223
    68
paulson@13251
    69
lemma alt_rtrancl_lemma1 [rule_format]:
paulson@13223
    70
    "n \<in> nat
paulson@13251
    71
     ==> \<forall>f \<in> succ(n) -> field(r).
paulson@13223
    72
         (\<forall>i\<in>n. \<langle>f`i, f ` succ(i)\<rangle> \<in> r) --> \<langle>f`0, f`n\<rangle> \<in> r^*"
paulson@13251
    73
apply (induct_tac n)
paulson@13251
    74
apply (simp_all add: apply_funtype rtrancl_refl, clarify)
paulson@13251
    75
apply (rename_tac n f)
paulson@13251
    76
apply (rule rtrancl_into_rtrancl)
paulson@13223
    77
 prefer 2 apply assumption
paulson@13223
    78
apply (drule_tac x="restrict(f,succ(n))" in bspec)
paulson@13251
    79
 apply (blast intro: restrict_type2)
paulson@13251
    80
apply (simp add: Ord_succ_mem_iff nat_0_le [THEN ltD] leI [THEN ltD] ltI)
paulson@13223
    81
done
paulson@13223
    82
paulson@13223
    83
lemma rtrancl_alt_subset_rtrancl: "rtrancl_alt(field(r),r) <= r^*"
paulson@13223
    84
apply (simp add: rtrancl_alt_def)
paulson@13251
    85
apply (blast intro: alt_rtrancl_lemma1)
paulson@13223
    86
done
paulson@13223
    87
paulson@13223
    88
lemma rtrancl_subset_rtrancl_alt: "r^* <= rtrancl_alt(field(r),r)"
paulson@13251
    89
apply (simp add: rtrancl_alt_def, clarify)
paulson@13251
    90
apply (frule rtrancl_type [THEN subsetD], clarify, simp)
paulson@13251
    91
apply (erule rtrancl_induct)
paulson@13223
    92
 txt{*Base case, trivial*}
paulson@13251
    93
 apply (rule_tac x=0 in bexI)
paulson@13251
    94
  apply (rule_tac x="lam x:1. xa" in bexI)
paulson@13251
    95
   apply simp_all
paulson@13223
    96
txt{*Inductive step*}
paulson@13251
    97
apply clarify
paulson@13251
    98
apply (rename_tac n f)
paulson@13251
    99
apply (rule_tac x="succ(n)" in bexI)
paulson@13223
   100
 apply (rule_tac x="lam i:succ(succ(n)). if i=succ(n) then z else f`i" in bexI)
paulson@13251
   101
  apply (simp add: Ord_succ_mem_iff nat_0_le [THEN ltD] leI [THEN ltD] ltI)
paulson@13251
   102
  apply (blast intro: mem_asym)
paulson@13251
   103
 apply typecheck
paulson@13251
   104
 apply auto
paulson@13223
   105
done
paulson@13223
   106
paulson@13223
   107
lemma rtrancl_alt_eq_rtrancl: "rtrancl_alt(field(r),r) = r^*"
paulson@13223
   108
by (blast del: subsetI
paulson@13251
   109
	  intro: rtrancl_alt_subset_rtrancl rtrancl_subset_rtrancl_alt)
paulson@13223
   110
paulson@13223
   111
paulson@13242
   112
constdefs
paulson@13242
   113
paulson@13324
   114
  rtran_closure_mem :: "[i=>o,i,i,i] => o"
paulson@13324
   115
    --{*The property of belonging to @{text "rtran_closure(r)"}*}
paulson@13324
   116
    "rtran_closure_mem(M,A,r,p) ==
paulson@13324
   117
	      \<exists>nnat[M]. \<exists>n[M]. \<exists>n'[M]. 
paulson@13324
   118
               omega(M,nnat) & n\<in>nnat & successor(M,n,n') &
paulson@13324
   119
	       (\<exists>f[M]. typed_function(M,n',A,f) &
paulson@13324
   120
		(\<exists>x[M]. \<exists>y[M]. \<exists>zero[M]. pair(M,x,y,p) & empty(M,zero) &
paulson@13324
   121
		  fun_apply(M,f,zero,x) & fun_apply(M,f,n,y)) &
paulson@13324
   122
		  (\<forall>j[M]. j\<in>n --> 
paulson@13324
   123
		    (\<exists>fj[M]. \<exists>sj[M]. \<exists>fsj[M]. \<exists>ffp[M]. 
paulson@13324
   124
		      fun_apply(M,f,j,fj) & successor(M,j,sj) &
paulson@13324
   125
		      fun_apply(M,f,sj,fsj) & pair(M,fj,fsj,ffp) & ffp \<in> r)))"
paulson@13324
   126
paulson@13242
   127
  rtran_closure :: "[i=>o,i,i] => o"
paulson@13324
   128
    "rtran_closure(M,r,s) == 
paulson@13324
   129
        \<forall>A[M]. is_field(M,r,A) -->
paulson@13324
   130
 	 (\<forall>p[M]. p \<in> s <-> rtran_closure_mem(M,A,r,p))"
paulson@13242
   131
paulson@13242
   132
  tran_closure :: "[i=>o,i,i] => o"
paulson@13251
   133
    "tran_closure(M,r,t) ==
paulson@13268
   134
         \<exists>s[M]. rtran_closure(M,r,s) & composition(M,r,s,t)"
paulson@13242
   135
paulson@13324
   136
lemma (in M_axioms) rtran_closure_mem_iff:
paulson@13324
   137
     "[|M(A); M(r); M(p)|]
paulson@13324
   138
      ==> rtran_closure_mem(M,A,r,p) <->
paulson@13324
   139
          (\<exists>n[M]. n\<in>nat & 
paulson@13324
   140
           (\<exists>f[M]. f \<in> succ(n) -> A &
paulson@13324
   141
            (\<exists>x[M]. \<exists>y[M]. p = <x,y> & f`0 = x & f`n = y) &
paulson@13324
   142
                           (\<forall>i\<in>n. <f`i, f`succ(i)> \<in> r)))"
paulson@13324
   143
apply (simp add: rtran_closure_mem_def typed_apply_abs
paulson@13324
   144
                 Ord_succ_mem_iff nat_0_le [THEN ltD])
paulson@13324
   145
apply (blast intro: elim:); 
paulson@13324
   146
done
paulson@13242
   147
paulson@13242
   148
locale M_trancl = M_axioms +
paulson@13242
   149
  assumes rtrancl_separation:
paulson@13324
   150
	 "[| M(r); M(A) |] ==> separation (M, rtran_closure_mem(M,A,r))"
paulson@13242
   151
      and wellfounded_trancl_separation:
paulson@13323
   152
	 "[| M(r); M(Z) |] ==> 
paulson@13323
   153
	  separation (M, \<lambda>x. 
paulson@13323
   154
	      \<exists>w[M]. \<exists>wx[M]. \<exists>rp[M]. 
paulson@13323
   155
	       w \<in> Z & pair(M,w,x,wx) & tran_closure(M,r,rp) & wx \<in> rp)"
paulson@13242
   156
paulson@13242
   157
paulson@13251
   158
lemma (in M_trancl) rtran_closure_rtrancl:
paulson@13242
   159
     "M(r) ==> rtran_closure(M,r,rtrancl(r))"
paulson@13324
   160
apply (simp add: rtran_closure_def rtran_closure_mem_iff 
paulson@13324
   161
                 rtrancl_alt_eq_rtrancl [symmetric] rtrancl_alt_def)
paulson@13324
   162
apply (auto simp add: nat_0_le [THEN ltD] apply_funtype); 
paulson@13242
   163
done
paulson@13242
   164
paulson@13251
   165
lemma (in M_trancl) rtrancl_closed [intro,simp]:
paulson@13242
   166
     "M(r) ==> M(rtrancl(r))"
paulson@13251
   167
apply (insert rtrancl_separation [of r "field(r)"])
paulson@13251
   168
apply (simp add: rtrancl_alt_eq_rtrancl [symmetric]
paulson@13324
   169
                 rtrancl_alt_def rtran_closure_mem_iff)
paulson@13242
   170
done
paulson@13242
   171
paulson@13251
   172
lemma (in M_trancl) rtrancl_abs [simp]:
paulson@13242
   173
     "[| M(r); M(z) |] ==> rtran_closure(M,r,z) <-> z = rtrancl(r)"
paulson@13242
   174
apply (rule iffI)
paulson@13242
   175
 txt{*Proving the right-to-left implication*}
paulson@13251
   176
 prefer 2 apply (blast intro: rtran_closure_rtrancl)
paulson@13242
   177
apply (rule M_equalityI)
paulson@13251
   178
apply (simp add: rtran_closure_def rtrancl_alt_eq_rtrancl [symmetric]
paulson@13324
   179
                 rtrancl_alt_def rtran_closure_mem_iff)
paulson@13324
   180
apply (auto simp add: nat_0_le [THEN ltD] apply_funtype); 
paulson@13242
   181
done
paulson@13242
   182
paulson@13251
   183
lemma (in M_trancl) trancl_closed [intro,simp]:
paulson@13242
   184
     "M(r) ==> M(trancl(r))"
paulson@13251
   185
by (simp add: trancl_def comp_closed rtrancl_closed)
paulson@13242
   186
paulson@13251
   187
lemma (in M_trancl) trancl_abs [simp]:
paulson@13242
   188
     "[| M(r); M(z) |] ==> tran_closure(M,r,z) <-> z = trancl(r)"
paulson@13251
   189
by (simp add: tran_closure_def trancl_def)
paulson@13242
   190
paulson@13323
   191
lemma (in M_trancl) wellfounded_trancl_separation':
paulson@13323
   192
     "[| M(r); M(Z) |] ==> separation (M, \<lambda>x. \<exists>w[M]. w \<in> Z & <w,x> \<in> r^+)"
paulson@13323
   193
by (insert wellfounded_trancl_separation [of r Z], simp) 
paulson@13242
   194
paulson@13251
   195
text{*Alternative proof of @{text wf_on_trancl}; inspiration for the
paulson@13242
   196
      relativized version.  Original version is on theory WF.*}
paulson@13242
   197
lemma "[| wf[A](r);  r-``A <= A |] ==> wf[A](r^+)"
paulson@13251
   198
apply (simp add: wf_on_def wf_def)
paulson@13242
   199
apply (safe intro!: equalityI)
paulson@13251
   200
apply (drule_tac x = "{x\<in>A. \<exists>w. \<langle>w,x\<rangle> \<in> r^+ & w \<in> Z}" in spec)
paulson@13251
   201
apply (blast elim: tranclE)
paulson@13242
   202
done
paulson@13242
   203
paulson@13242
   204
lemma (in M_trancl) wellfounded_on_trancl:
paulson@13242
   205
     "[| wellfounded_on(M,A,r);  r-``A <= A; M(r); M(A) |]
paulson@13251
   206
      ==> wellfounded_on(M,A,r^+)"
paulson@13251
   207
apply (simp add: wellfounded_on_def)
paulson@13242
   208
apply (safe intro!: equalityI)
paulson@13242
   209
apply (rename_tac Z x)
paulson@13268
   210
apply (subgoal_tac "M({x\<in>A. \<exists>w[M]. w \<in> Z & \<langle>w,x\<rangle> \<in> r^+})")
paulson@13251
   211
 prefer 2
paulson@13323
   212
 apply (blast intro: wellfounded_trancl_separation') 
paulson@13299
   213
apply (drule_tac x = "{x\<in>A. \<exists>w[M]. w \<in> Z & \<langle>w,x\<rangle> \<in> r^+}" in rspec, safe)
paulson@13251
   214
apply (blast dest: transM, simp)
paulson@13251
   215
apply (rename_tac y w)
paulson@13242
   216
apply (drule_tac x=w in bspec, assumption, clarify)
paulson@13242
   217
apply (erule tranclE)
paulson@13242
   218
  apply (blast dest: transM)   (*transM is needed to prove M(xa)*)
paulson@13251
   219
 apply blast
paulson@13242
   220
done
paulson@13242
   221
paulson@13251
   222
lemma (in M_trancl) wellfounded_trancl:
paulson@13251
   223
     "[|wellfounded(M,r); M(r)|] ==> wellfounded(M,r^+)"
paulson@13251
   224
apply (rotate_tac -1)
paulson@13251
   225
apply (simp add: wellfounded_iff_wellfounded_on_field)
paulson@13251
   226
apply (rule wellfounded_on_subset_A, erule wellfounded_on_trancl)
paulson@13251
   227
   apply blast
paulson@13251
   228
  apply (simp_all add: trancl_type [THEN field_rel_subset])
paulson@13251
   229
done
paulson@13242
   230
paulson@13223
   231
text{*Relativized to M: Every well-founded relation is a subset of some
paulson@13251
   232
inverse image of an ordinal.  Key step is the construction (in M) of a
paulson@13223
   233
rank function.*}
paulson@13223
   234
paulson@13223
   235
paulson@13223
   236
(*NEEDS RELATIVIZATION*)
paulson@13268
   237
locale M_wfrank = M_trancl +
paulson@13223
   238
  assumes wfrank_separation':
paulson@13251
   239
     "M(r) ==>
paulson@13223
   240
	separation
paulson@13268
   241
	   (M, \<lambda>x. ~ (\<exists>f[M]. is_recfun(r^+, x, %x f. range(f), f)))"
paulson@13223
   242
 and wfrank_strong_replacement':
paulson@13242
   243
     "M(r) ==>
paulson@13268
   244
      strong_replacement(M, \<lambda>x z. \<exists>y[M]. \<exists>f[M]. 
paulson@13251
   245
		  pair(M,x,y,z) & is_recfun(r^+, x, %x f. range(f), f) &
paulson@13242
   246
		  y = range(f))"
paulson@13242
   247
 and Ord_wfrank_separation:
paulson@13251
   248
     "M(r) ==>
paulson@13251
   249
      separation (M, \<lambda>x. ~ (\<forall>f. M(f) \<longrightarrow>
paulson@13242
   250
                       is_recfun(r^+, x, \<lambda>x. range, f) \<longrightarrow> Ord(range(f))))"
paulson@13223
   251
paulson@13251
   252
text{*This function, defined using replacement, is a rank function for
paulson@13251
   253
well-founded relations within the class M.*}
paulson@13251
   254
constdefs
paulson@13242
   255
 wellfoundedrank :: "[i=>o,i,i] => i"
paulson@13251
   256
    "wellfoundedrank(M,r,A) ==
paulson@13268
   257
        {p. x\<in>A, \<exists>y[M]. \<exists>f[M]. 
paulson@13251
   258
                       p = <x,y> & is_recfun(r^+, x, %x f. range(f), f) &
paulson@13242
   259
                       y = range(f)}"
paulson@13223
   260
paulson@13268
   261
lemma (in M_wfrank) exists_wfrank:
paulson@13251
   262
    "[| wellfounded(M,r); M(a); M(r) |]
paulson@13268
   263
     ==> \<exists>f[M]. is_recfun(r^+, a, %x f. range(f), f)"
paulson@13251
   264
apply (rule wellfounded_exists_is_recfun)
paulson@13251
   265
      apply (blast intro: wellfounded_trancl)
paulson@13251
   266
     apply (rule trans_trancl)
paulson@13251
   267
    apply (erule wfrank_separation')
paulson@13251
   268
   apply (erule wfrank_strong_replacement')
paulson@13251
   269
apply (simp_all add: trancl_subset_times)
paulson@13223
   270
done
paulson@13223
   271
paulson@13268
   272
lemma (in M_wfrank) M_wellfoundedrank:
paulson@13251
   273
    "[| wellfounded(M,r); M(r); M(A) |] ==> M(wellfoundedrank(M,r,A))"
paulson@13251
   274
apply (insert wfrank_strong_replacement' [of r])
paulson@13251
   275
apply (simp add: wellfoundedrank_def)
paulson@13251
   276
apply (rule strong_replacement_closed)
paulson@13242
   277
   apply assumption+
paulson@13251
   278
 apply (rule univalent_is_recfun)
paulson@13251
   279
   apply (blast intro: wellfounded_trancl)
paulson@13251
   280
  apply (rule trans_trancl)
paulson@13254
   281
 apply (simp add: trancl_subset_times, blast)
paulson@13223
   282
done
paulson@13223
   283
paulson@13268
   284
lemma (in M_wfrank) Ord_wfrank_range [rule_format]:
paulson@13251
   285
    "[| wellfounded(M,r); a\<in>A; M(r); M(A) |]
paulson@13242
   286
     ==> \<forall>f. M(f) --> is_recfun(r^+, a, %x f. range(f), f) --> Ord(range(f))"
paulson@13251
   287
apply (drule wellfounded_trancl, assumption)
paulson@13251
   288
apply (rule wellfounded_induct, assumption+)
paulson@13254
   289
  apply simp
paulson@13254
   290
 apply (blast intro: Ord_wfrank_separation, clarify)
paulson@13242
   291
txt{*The reasoning in both cases is that we get @{term y} such that
paulson@13251
   292
   @{term "\<langle>y, x\<rangle> \<in> r^+"}.  We find that
paulson@13242
   293
   @{term "f`y = restrict(f, r^+ -`` {y})"}. *}
paulson@13242
   294
apply (rule OrdI [OF _ Ord_is_Transset])
paulson@13242
   295
 txt{*An ordinal is a transitive set...*}
paulson@13251
   296
 apply (simp add: Transset_def)
paulson@13242
   297
 apply clarify
paulson@13251
   298
 apply (frule apply_recfun2, assumption)
paulson@13242
   299
 apply (force simp add: restrict_iff)
paulson@13251
   300
txt{*...of ordinals.  This second case requires the induction hyp.*}
paulson@13251
   301
apply clarify
paulson@13242
   302
apply (rename_tac i y)
paulson@13251
   303
apply (frule apply_recfun2, assumption)
paulson@13251
   304
apply (frule is_recfun_imp_in_r, assumption)
paulson@13251
   305
apply (frule is_recfun_restrict)
paulson@13242
   306
    (*simp_all won't work*)
paulson@13251
   307
    apply (simp add: trans_trancl trancl_subset_times)+
paulson@13242
   308
apply (drule spec [THEN mp], assumption)
paulson@13242
   309
apply (subgoal_tac "M(restrict(f, r^+ -`` {y}))")
paulson@13251
   310
 apply (drule_tac x="restrict(f, r^+ -`` {y})" in spec)
paulson@13242
   311
 apply (simp add: function_apply_equality [OF _ is_recfun_imp_function])
paulson@13242
   312
apply (blast dest: pair_components_in_M)
paulson@13223
   313
done
paulson@13223
   314
paulson@13268
   315
lemma (in M_wfrank) Ord_range_wellfoundedrank:
paulson@13251
   316
    "[| wellfounded(M,r); r \<subseteq> A*A;  M(r); M(A) |]
paulson@13242
   317
     ==> Ord (range(wellfoundedrank(M,r,A)))"
paulson@13251
   318
apply (frule wellfounded_trancl, assumption)
paulson@13251
   319
apply (frule trancl_subset_times)
paulson@13242
   320
apply (simp add: wellfoundedrank_def)
paulson@13242
   321
apply (rule OrdI [OF _ Ord_is_Transset])
paulson@13242
   322
 prefer 2
paulson@13251
   323
 txt{*by our previous result the range consists of ordinals.*}
paulson@13251
   324
 apply (blast intro: Ord_wfrank_range)
paulson@13242
   325
txt{*We still must show that the range is a transitive set.*}
paulson@13247
   326
apply (simp add: Transset_def, clarify, simp)
paulson@13293
   327
apply (rename_tac x i f u)
paulson@13251
   328
apply (frule is_recfun_imp_in_r, assumption)
paulson@13251
   329
apply (subgoal_tac "M(u) & M(i) & M(x)")
paulson@13251
   330
 prefer 2 apply (blast dest: transM, clarify)
paulson@13251
   331
apply (rule_tac a=u in rangeI)
paulson@13293
   332
apply (rule_tac x=u in ReplaceI)
paulson@13293
   333
  apply simp 
paulson@13293
   334
  apply (rule_tac x="restrict(f, r^+ -`` {u})" in rexI)
paulson@13293
   335
   apply (blast intro: is_recfun_restrict trans_trancl dest: apply_recfun2)
paulson@13293
   336
  apply simp 
paulson@13293
   337
apply blast 
paulson@13251
   338
txt{*Unicity requirement of Replacement*}
paulson@13242
   339
apply clarify
paulson@13251
   340
apply (frule apply_recfun2, assumption)
paulson@13293
   341
apply (simp add: trans_trancl is_recfun_cut)
paulson@13223
   342
done
paulson@13223
   343
paulson@13268
   344
lemma (in M_wfrank) function_wellfoundedrank:
paulson@13251
   345
    "[| wellfounded(M,r); M(r); M(A)|]
paulson@13242
   346
     ==> function(wellfoundedrank(M,r,A))"
paulson@13251
   347
apply (simp add: wellfoundedrank_def function_def, clarify)
paulson@13242
   348
txt{*Uniqueness: repeated below!*}
paulson@13242
   349
apply (drule is_recfun_functional, assumption)
paulson@13251
   350
     apply (blast intro: wellfounded_trancl)
paulson@13251
   351
    apply (simp_all add: trancl_subset_times trans_trancl)
paulson@13223
   352
done
paulson@13223
   353
paulson@13268
   354
lemma (in M_wfrank) domain_wellfoundedrank:
paulson@13251
   355
    "[| wellfounded(M,r); M(r); M(A)|]
paulson@13242
   356
     ==> domain(wellfoundedrank(M,r,A)) = A"
paulson@13251
   357
apply (simp add: wellfoundedrank_def function_def)
paulson@13242
   358
apply (rule equalityI, auto)
paulson@13251
   359
apply (frule transM, assumption)
paulson@13251
   360
apply (frule_tac a=x in exists_wfrank, assumption+, clarify)
paulson@13293
   361
apply (rule_tac b="range(f)" in domainI)
paulson@13293
   362
apply (rule_tac x=x in ReplaceI)
paulson@13293
   363
  apply simp 
paulson@13268
   364
  apply (rule_tac x=f in rexI, blast, simp_all)
paulson@13242
   365
txt{*Uniqueness (for Replacement): repeated above!*}
paulson@13242
   366
apply clarify
paulson@13242
   367
apply (drule is_recfun_functional, assumption)
paulson@13251
   368
    apply (blast intro: wellfounded_trancl)
paulson@13251
   369
    apply (simp_all add: trancl_subset_times trans_trancl)
paulson@13223
   370
done
paulson@13223
   371
paulson@13268
   372
lemma (in M_wfrank) wellfoundedrank_type:
paulson@13251
   373
    "[| wellfounded(M,r);  M(r); M(A)|]
paulson@13242
   374
     ==> wellfoundedrank(M,r,A) \<in> A -> range(wellfoundedrank(M,r,A))"
paulson@13251
   375
apply (frule function_wellfoundedrank [of r A], assumption+)
paulson@13251
   376
apply (frule function_imp_Pi)
paulson@13251
   377
 apply (simp add: wellfoundedrank_def relation_def)
paulson@13251
   378
 apply blast
paulson@13242
   379
apply (simp add: domain_wellfoundedrank)
paulson@13223
   380
done
paulson@13223
   381
paulson@13268
   382
lemma (in M_wfrank) Ord_wellfoundedrank:
paulson@13251
   383
    "[| wellfounded(M,r); a \<in> A; r \<subseteq> A*A;  M(r); M(A) |]
paulson@13242
   384
     ==> Ord(wellfoundedrank(M,r,A) ` a)"
paulson@13242
   385
by (blast intro: apply_funtype [OF wellfoundedrank_type]
paulson@13242
   386
                 Ord_in_Ord [OF Ord_range_wellfoundedrank])
paulson@13223
   387
paulson@13268
   388
lemma (in M_wfrank) wellfoundedrank_eq:
paulson@13242
   389
     "[| is_recfun(r^+, a, %x. range, f);
paulson@13251
   390
         wellfounded(M,r);  a \<in> A; M(f); M(r); M(A)|]
paulson@13242
   391
      ==> wellfoundedrank(M,r,A) ` a = range(f)"
paulson@13251
   392
apply (rule apply_equality)
paulson@13251
   393
 prefer 2 apply (blast intro: wellfoundedrank_type)
paulson@13242
   394
apply (simp add: wellfoundedrank_def)
paulson@13242
   395
apply (rule ReplaceI)
paulson@13268
   396
  apply (rule_tac x="range(f)" in rexI) 
paulson@13251
   397
  apply blast
paulson@13268
   398
 apply simp_all
paulson@13251
   399
txt{*Unicity requirement of Replacement*}
paulson@13242
   400
apply clarify
paulson@13242
   401
apply (drule is_recfun_functional, assumption)
paulson@13251
   402
    apply (blast intro: wellfounded_trancl)
paulson@13251
   403
    apply (simp_all add: trancl_subset_times trans_trancl)
paulson@13223
   404
done
paulson@13223
   405
paulson@13247
   406
paulson@13268
   407
lemma (in M_wfrank) wellfoundedrank_lt:
paulson@13247
   408
     "[| <a,b> \<in> r;
paulson@13251
   409
         wellfounded(M,r); r \<subseteq> A*A;  M(r); M(A)|]
paulson@13247
   410
      ==> wellfoundedrank(M,r,A) ` a < wellfoundedrank(M,r,A) ` b"
paulson@13251
   411
apply (frule wellfounded_trancl, assumption)
paulson@13247
   412
apply (subgoal_tac "a\<in>A & b\<in>A")
paulson@13247
   413
 prefer 2 apply blast
paulson@13251
   414
apply (simp add: lt_def Ord_wellfoundedrank, clarify)
paulson@13251
   415
apply (frule exists_wfrank [of concl: _ b], assumption+, clarify)
paulson@13247
   416
apply (rename_tac fb)
paulson@13251
   417
apply (frule is_recfun_restrict [of concl: "r^+" a])
paulson@13251
   418
    apply (rule trans_trancl, assumption)
paulson@13251
   419
   apply (simp_all add: r_into_trancl trancl_subset_times)
paulson@13247
   420
txt{*Still the same goal, but with new @{text is_recfun} assumptions.*}
paulson@13251
   421
apply (simp add: wellfoundedrank_eq)
paulson@13247
   422
apply (frule_tac a=a in wellfoundedrank_eq, assumption+)
paulson@13247
   423
   apply (simp_all add: transM [of a])
paulson@13247
   424
txt{*We have used equations for wellfoundedrank and now must use some
paulson@13247
   425
    for  @{text is_recfun}. *}
paulson@13251
   426
apply (rule_tac a=a in rangeI)
paulson@13251
   427
apply (simp add: is_recfun_type [THEN apply_iff] vimage_singleton_iff
paulson@13251
   428
                 r_into_trancl apply_recfun r_into_trancl)
paulson@13247
   429
done
paulson@13247
   430
paulson@13247
   431
paulson@13268
   432
lemma (in M_wfrank) wellfounded_imp_subset_rvimage:
paulson@13251
   433
     "[|wellfounded(M,r); r \<subseteq> A*A; M(r); M(A)|]
paulson@13247
   434
      ==> \<exists>i f. Ord(i) & r <= rvimage(A, f, Memrel(i))"
paulson@13247
   435
apply (rule_tac x="range(wellfoundedrank(M,r,A))" in exI)
paulson@13247
   436
apply (rule_tac x="wellfoundedrank(M,r,A)" in exI)
paulson@13251
   437
apply (simp add: Ord_range_wellfoundedrank, clarify)
paulson@13251
   438
apply (frule subsetD, assumption, clarify)
paulson@13247
   439
apply (simp add: rvimage_iff wellfoundedrank_lt [THEN ltD])
paulson@13251
   440
apply (blast intro: apply_rangeI wellfoundedrank_type)
paulson@13247
   441
done
paulson@13247
   442
paulson@13268
   443
lemma (in M_wfrank) wellfounded_imp_wf:
paulson@13251
   444
     "[|wellfounded(M,r); relation(r); M(r)|] ==> wf(r)"
paulson@13247
   445
by (blast dest!: relation_field_times_field wellfounded_imp_subset_rvimage
paulson@13247
   446
          intro: wf_rvimage_Ord [THEN wf_subset])
paulson@13247
   447
paulson@13268
   448
lemma (in M_wfrank) wellfounded_on_imp_wf_on:
paulson@13251
   449
     "[|wellfounded_on(M,A,r); relation(r); M(r); M(A)|] ==> wf[A](r)"
paulson@13251
   450
apply (simp add: wellfounded_on_iff_wellfounded wf_on_def)
paulson@13247
   451
apply (rule wellfounded_imp_wf)
paulson@13251
   452
apply (simp_all add: relation_def)
paulson@13247
   453
done
paulson@13247
   454
paulson@13247
   455
paulson@13268
   456
theorem (in M_wfrank) wf_abs [simp]:
paulson@13247
   457
     "[|relation(r); M(r)|] ==> wellfounded(M,r) <-> wf(r)"
paulson@13251
   458
by (blast intro: wellfounded_imp_wf wf_imp_relativized)
paulson@13247
   459
paulson@13268
   460
theorem (in M_wfrank) wf_on_abs [simp]:
paulson@13247
   461
     "[|relation(r); M(r); M(A)|] ==> wellfounded_on(M,A,r) <-> wf[A](r)"
paulson@13251
   462
by (blast intro: wellfounded_on_imp_wf_on wf_on_imp_relativized)
paulson@13247
   463
paulson@13254
   464
paulson@13254
   465
text{*absoluteness for wfrec-defined functions.*}
paulson@13254
   466
paulson@13254
   467
(*first use is_recfun, then M_is_recfun*)
paulson@13254
   468
paulson@13254
   469
lemma (in M_trancl) wfrec_relativize:
paulson@13254
   470
  "[|wf(r); M(a); M(r);  
paulson@13268
   471
     strong_replacement(M, \<lambda>x z. \<exists>y[M]. \<exists>g[M].
paulson@13254
   472
          pair(M,x,y,z) & 
paulson@13254
   473
          is_recfun(r^+, x, \<lambda>x f. H(x, restrict(f, r -`` {x})), g) & 
paulson@13254
   474
          y = H(x, restrict(g, r -`` {x}))); 
paulson@13254
   475
     \<forall>x[M]. \<forall>g[M]. function(g) --> M(H(x,g))|] 
paulson@13254
   476
   ==> wfrec(r,a,H) = z <-> 
paulson@13268
   477
       (\<exists>f[M]. is_recfun(r^+, a, \<lambda>x f. H(x, restrict(f, r -`` {x})), f) & 
paulson@13254
   478
            z = H(a,restrict(f,r-``{a})))"
paulson@13254
   479
apply (frule wf_trancl) 
paulson@13254
   480
apply (simp add: wftrec_def wfrec_def, safe)
paulson@13254
   481
 apply (frule wf_exists_is_recfun 
paulson@13254
   482
              [of concl: "r^+" a "\<lambda>x f. H(x, restrict(f, r -`` {x}))"]) 
paulson@13254
   483
      apply (simp_all add: trans_trancl function_restrictI trancl_subset_times)
paulson@13268
   484
 apply (clarify, rule_tac x=x in rexI) 
paulson@13254
   485
 apply (simp_all add: the_recfun_eq trans_trancl trancl_subset_times)
paulson@13254
   486
done
paulson@13254
   487
paulson@13254
   488
paulson@13254
   489
text{*Assuming @{term r} is transitive simplifies the occurrences of @{text H}.
paulson@13254
   490
      The premise @{term "relation(r)"} is necessary 
paulson@13254
   491
      before we can replace @{term "r^+"} by @{term r}. *}
paulson@13254
   492
theorem (in M_trancl) trans_wfrec_relativize:
paulson@13254
   493
  "[|wf(r);  trans(r);  relation(r);  M(r);  M(a);
paulson@13293
   494
     strong_replacement(M, \<lambda>x z. \<exists>y[M]. 
paulson@13293
   495
                pair(M,x,y,z) & (\<exists>g[M]. is_recfun(r,x,H,g) & y = H(x,g))); 
paulson@13254
   496
     \<forall>x[M]. \<forall>g[M]. function(g) --> M(H(x,g))|] 
paulson@13268
   497
   ==> wfrec(r,a,H) = z <-> (\<exists>f[M]. is_recfun(r,a,H,f) & z = H(a,f))" 
paulson@13254
   498
by (simp cong: is_recfun_cong
paulson@13254
   499
         add: wfrec_relativize trancl_eq_r
paulson@13254
   500
               is_recfun_restrict_idem domain_restrict_idem)
paulson@13254
   501
paulson@13254
   502
paulson@13254
   503
lemma (in M_trancl) trans_eq_pair_wfrec_iff:
paulson@13254
   504
  "[|wf(r);  trans(r); relation(r); M(r);  M(y); 
paulson@13293
   505
     strong_replacement(M, \<lambda>x z. \<exists>y[M]. 
paulson@13293
   506
                pair(M,x,y,z) & (\<exists>g[M]. is_recfun(r,x,H,g) & y = H(x,g))); 
paulson@13254
   507
     \<forall>x[M]. \<forall>g[M]. function(g) --> M(H(x,g))|] 
paulson@13254
   508
   ==> y = <x, wfrec(r, x, H)> <-> 
paulson@13268
   509
       (\<exists>f[M]. is_recfun(r,x,H,f) & y = <x, H(x,f)>)"
paulson@13293
   510
apply safe 
paulson@13293
   511
 apply (simp add: trans_wfrec_relativize [THEN iff_sym, of concl: _ x]) 
paulson@13254
   512
txt{*converse direction*}
paulson@13254
   513
apply (rule sym)
paulson@13254
   514
apply (simp add: trans_wfrec_relativize, blast) 
paulson@13254
   515
done
paulson@13254
   516
paulson@13254
   517
paulson@13254
   518
subsection{*M is closed under well-founded recursion*}
paulson@13254
   519
paulson@13254
   520
text{*Lemma with the awkward premise mentioning @{text wfrec}.*}
paulson@13268
   521
lemma (in M_wfrank) wfrec_closed_lemma [rule_format]:
paulson@13254
   522
     "[|wf(r); M(r); 
paulson@13254
   523
        strong_replacement(M, \<lambda>x y. y = \<langle>x, wfrec(r, x, H)\<rangle>);
paulson@13254
   524
        \<forall>x[M]. \<forall>g[M]. function(g) --> M(H(x,g)) |] 
paulson@13254
   525
      ==> M(a) --> M(wfrec(r,a,H))"
paulson@13254
   526
apply (rule_tac a=a in wf_induct, assumption+)
paulson@13254
   527
apply (subst wfrec, assumption, clarify)
paulson@13254
   528
apply (drule_tac x1=x and x="\<lambda>x\<in>r -`` {x}. wfrec(r, x, H)" 
paulson@13254
   529
       in rspec [THEN rspec]) 
paulson@13254
   530
apply (simp_all add: function_lam) 
paulson@13254
   531
apply (blast intro: dest: pair_components_in_M ) 
paulson@13254
   532
done
paulson@13254
   533
paulson@13254
   534
text{*Eliminates one instance of replacement.*}
paulson@13268
   535
lemma (in M_wfrank) wfrec_replacement_iff:
paulson@13268
   536
     "strong_replacement(M, \<lambda>x z. \<exists>y[M]. \<exists>g[M]. 
paulson@13254
   537
                pair(M,x,y,z) & is_recfun(r,x,H,g) & y = H(x,g)) <->
paulson@13254
   538
      strong_replacement(M, 
paulson@13268
   539
           \<lambda>x y. \<exists>f[M]. is_recfun(r,x,H,f) & y = <x, H(x,f)>)"
paulson@13254
   540
apply simp 
paulson@13254
   541
apply (rule strong_replacement_cong, blast) 
paulson@13254
   542
done
paulson@13254
   543
paulson@13254
   544
text{*Useful version for transitive relations*}
paulson@13268
   545
theorem (in M_wfrank) trans_wfrec_closed:
paulson@13254
   546
     "[|wf(r); trans(r); relation(r); M(r); M(a);
paulson@13254
   547
        strong_replacement(M, 
paulson@13268
   548
             \<lambda>x z. \<exists>y[M]. \<exists>g[M].
paulson@13254
   549
                    pair(M,x,y,z) & is_recfun(r,x,H,g) & y = H(x,g)); 
paulson@13254
   550
        \<forall>x[M]. \<forall>g[M]. function(g) --> M(H(x,g)) |] 
paulson@13254
   551
      ==> M(wfrec(r,a,H))"
paulson@13254
   552
apply (frule wfrec_replacement_iff [THEN iffD1]) 
paulson@13254
   553
apply (rule wfrec_closed_lemma, assumption+) 
paulson@13254
   554
apply (simp_all add: wfrec_replacement_iff trans_eq_pair_wfrec_iff) 
paulson@13254
   555
done
paulson@13254
   556
paulson@13254
   557
section{*Absoluteness without assuming transitivity*}
paulson@13254
   558
lemma (in M_trancl) eq_pair_wfrec_iff:
paulson@13254
   559
  "[|wf(r);  M(r);  M(y); 
paulson@13268
   560
     strong_replacement(M, \<lambda>x z. \<exists>y[M]. \<exists>g[M].
paulson@13254
   561
          pair(M,x,y,z) & 
paulson@13254
   562
          is_recfun(r^+, x, \<lambda>x f. H(x, restrict(f, r -`` {x})), g) & 
paulson@13254
   563
          y = H(x, restrict(g, r -`` {x}))); 
paulson@13254
   564
     \<forall>x[M]. \<forall>g[M]. function(g) --> M(H(x,g))|] 
paulson@13254
   565
   ==> y = <x, wfrec(r, x, H)> <-> 
paulson@13268
   566
       (\<exists>f[M]. is_recfun(r^+, x, \<lambda>x f. H(x, restrict(f, r -`` {x})), f) & 
paulson@13254
   567
            y = <x, H(x,restrict(f,r-``{x}))>)"
paulson@13254
   568
apply safe  
paulson@13293
   569
 apply (simp add: wfrec_relativize [THEN iff_sym, of concl: _ x]) 
paulson@13254
   570
txt{*converse direction*}
paulson@13254
   571
apply (rule sym)
paulson@13254
   572
apply (simp add: wfrec_relativize, blast) 
paulson@13254
   573
done
paulson@13254
   574
paulson@13268
   575
lemma (in M_wfrank) wfrec_closed_lemma [rule_format]:
paulson@13254
   576
     "[|wf(r); M(r); 
paulson@13254
   577
        strong_replacement(M, \<lambda>x y. y = \<langle>x, wfrec(r, x, H)\<rangle>);
paulson@13254
   578
        \<forall>x[M]. \<forall>g[M]. function(g) --> M(H(x,g)) |] 
paulson@13254
   579
      ==> M(a) --> M(wfrec(r,a,H))"
paulson@13254
   580
apply (rule_tac a=a in wf_induct, assumption+)
paulson@13254
   581
apply (subst wfrec, assumption, clarify)
paulson@13254
   582
apply (drule_tac x1=x and x="\<lambda>x\<in>r -`` {x}. wfrec(r, x, H)" 
paulson@13254
   583
       in rspec [THEN rspec]) 
paulson@13254
   584
apply (simp_all add: function_lam) 
paulson@13254
   585
apply (blast intro: dest: pair_components_in_M ) 
paulson@13254
   586
done
paulson@13254
   587
paulson@13254
   588
text{*Full version not assuming transitivity, but maybe not very useful.*}
paulson@13268
   589
theorem (in M_wfrank) wfrec_closed:
paulson@13254
   590
     "[|wf(r); M(r); M(a);
paulson@13268
   591
     strong_replacement(M, \<lambda>x z. \<exists>y[M]. \<exists>g[M].
paulson@13254
   592
          pair(M,x,y,z) & 
paulson@13254
   593
          is_recfun(r^+, x, \<lambda>x f. H(x, restrict(f, r -`` {x})), g) & 
paulson@13254
   594
          y = H(x, restrict(g, r -`` {x}))); 
paulson@13254
   595
        \<forall>x[M]. \<forall>g[M]. function(g) --> M(H(x,g)) |] 
paulson@13254
   596
      ==> M(wfrec(r,a,H))"
paulson@13254
   597
apply (frule wfrec_replacement_iff [THEN iffD1]) 
paulson@13254
   598
apply (rule wfrec_closed_lemma, assumption+) 
paulson@13254
   599
apply (simp_all add: eq_pair_wfrec_iff) 
paulson@13254
   600
done
paulson@13254
   601
paulson@13223
   602
end