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