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
     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])
   145 apply (blast intro: elim:); 
   146 done
   147 
   148 locale M_trancl = M_axioms +
   149   assumes rtrancl_separation:
   150 	 "[| M(r); M(A) |] ==> separation (M, rtran_closure_mem(M,A,r))"
   151       and wellfounded_trancl_separation:
   152 	 "[| M(r); M(Z) |] ==> 
   153 	  separation (M, \<lambda>x. 
   154 	      \<exists>w[M]. \<exists>wx[M]. \<exists>rp[M]. 
   155 	       w \<in> Z & pair(M,w,x,wx) & tran_closure(M,r,rp) & wx \<in> rp)"
   156 
   157 
   158 lemma (in M_trancl) rtran_closure_rtrancl:
   159      "M(r) ==> rtran_closure(M,r,rtrancl(r))"
   160 apply (simp add: rtran_closure_def rtran_closure_mem_iff 
   161                  rtrancl_alt_eq_rtrancl [symmetric] rtrancl_alt_def)
   162 apply (auto simp add: nat_0_le [THEN ltD] apply_funtype); 
   163 done
   164 
   165 lemma (in M_trancl) rtrancl_closed [intro,simp]:
   166      "M(r) ==> M(rtrancl(r))"
   167 apply (insert rtrancl_separation [of r "field(r)"])
   168 apply (simp add: rtrancl_alt_eq_rtrancl [symmetric]
   169                  rtrancl_alt_def rtran_closure_mem_iff)
   170 done
   171 
   172 lemma (in M_trancl) rtrancl_abs [simp]:
   173      "[| M(r); M(z) |] ==> rtran_closure(M,r,z) <-> z = rtrancl(r)"
   174 apply (rule iffI)
   175  txt{*Proving the right-to-left implication*}
   176  prefer 2 apply (blast intro: rtran_closure_rtrancl)
   177 apply (rule M_equalityI)
   178 apply (simp add: rtran_closure_def rtrancl_alt_eq_rtrancl [symmetric]
   179                  rtrancl_alt_def rtran_closure_mem_iff)
   180 apply (auto simp add: nat_0_le [THEN ltD] apply_funtype); 
   181 done
   182 
   183 lemma (in M_trancl) trancl_closed [intro,simp]:
   184      "M(r) ==> M(trancl(r))"
   185 by (simp add: trancl_def comp_closed rtrancl_closed)
   186 
   187 lemma (in M_trancl) trancl_abs [simp]:
   188      "[| M(r); M(z) |] ==> tran_closure(M,r,z) <-> z = trancl(r)"
   189 by (simp add: tran_closure_def trancl_def)
   190 
   191 lemma (in M_trancl) wellfounded_trancl_separation':
   192      "[| M(r); M(Z) |] ==> separation (M, \<lambda>x. \<exists>w[M]. w \<in> Z & <w,x> \<in> r^+)"
   193 by (insert wellfounded_trancl_separation [of r Z], simp) 
   194 
   195 text{*Alternative proof of @{text wf_on_trancl}; inspiration for the
   196       relativized version.  Original version is on theory WF.*}
   197 lemma "[| wf[A](r);  r-``A <= A |] ==> wf[A](r^+)"
   198 apply (simp add: wf_on_def wf_def)
   199 apply (safe intro!: equalityI)
   200 apply (drule_tac x = "{x\<in>A. \<exists>w. \<langle>w,x\<rangle> \<in> r^+ & w \<in> Z}" in spec)
   201 apply (blast elim: tranclE)
   202 done
   203 
   204 lemma (in M_trancl) wellfounded_on_trancl:
   205      "[| wellfounded_on(M,A,r);  r-``A <= A; M(r); M(A) |]
   206       ==> wellfounded_on(M,A,r^+)"
   207 apply (simp add: wellfounded_on_def)
   208 apply (safe intro!: equalityI)
   209 apply (rename_tac Z x)
   210 apply (subgoal_tac "M({x\<in>A. \<exists>w[M]. w \<in> Z & \<langle>w,x\<rangle> \<in> r^+})")
   211  prefer 2
   212  apply (blast intro: wellfounded_trancl_separation') 
   213 apply (drule_tac x = "{x\<in>A. \<exists>w[M]. w \<in> Z & \<langle>w,x\<rangle> \<in> r^+}" in rspec, safe)
   214 apply (blast dest: transM, simp)
   215 apply (rename_tac y w)
   216 apply (drule_tac x=w in bspec, assumption, clarify)
   217 apply (erule tranclE)
   218   apply (blast dest: transM)   (*transM is needed to prove M(xa)*)
   219  apply blast
   220 done
   221 
   222 lemma (in M_trancl) wellfounded_trancl:
   223      "[|wellfounded(M,r); M(r)|] ==> wellfounded(M,r^+)"
   224 apply (rotate_tac -1)
   225 apply (simp add: wellfounded_iff_wellfounded_on_field)
   226 apply (rule wellfounded_on_subset_A, erule wellfounded_on_trancl)
   227    apply blast
   228   apply (simp_all add: trancl_type [THEN field_rel_subset])
   229 done
   230 
   231 text{*Relativized to M: Every well-founded relation is a subset of some
   232 inverse image of an ordinal.  Key step is the construction (in M) of a
   233 rank function.*}
   234 
   235 
   236 (*NEEDS RELATIVIZATION*)
   237 locale M_wfrank = M_trancl +
   238   assumes wfrank_separation':
   239      "M(r) ==>
   240 	separation
   241 	   (M, \<lambda>x. ~ (\<exists>f[M]. is_recfun(r^+, x, %x f. range(f), f)))"
   242  and wfrank_strong_replacement':
   243      "M(r) ==>
   244       strong_replacement(M, \<lambda>x z. \<exists>y[M]. \<exists>f[M]. 
   245 		  pair(M,x,y,z) & is_recfun(r^+, x, %x f. range(f), f) &
   246 		  y = range(f))"
   247  and Ord_wfrank_separation:
   248      "M(r) ==>
   249       separation (M, \<lambda>x. ~ (\<forall>f. M(f) \<longrightarrow>
   250                        is_recfun(r^+, x, \<lambda>x. range, f) \<longrightarrow> Ord(range(f))))"
   251 
   252 text{*This function, defined using replacement, is a rank function for
   253 well-founded relations within the class M.*}
   254 constdefs
   255  wellfoundedrank :: "[i=>o,i,i] => i"
   256     "wellfoundedrank(M,r,A) ==
   257         {p. x\<in>A, \<exists>y[M]. \<exists>f[M]. 
   258                        p = <x,y> & is_recfun(r^+, x, %x f. range(f), f) &
   259                        y = range(f)}"
   260 
   261 lemma (in M_wfrank) exists_wfrank:
   262     "[| wellfounded(M,r); M(a); M(r) |]
   263      ==> \<exists>f[M]. is_recfun(r^+, a, %x f. range(f), f)"
   264 apply (rule wellfounded_exists_is_recfun)
   265       apply (blast intro: wellfounded_trancl)
   266      apply (rule trans_trancl)
   267     apply (erule wfrank_separation')
   268    apply (erule wfrank_strong_replacement')
   269 apply (simp_all add: trancl_subset_times)
   270 done
   271 
   272 lemma (in M_wfrank) M_wellfoundedrank:
   273     "[| wellfounded(M,r); M(r); M(A) |] ==> M(wellfoundedrank(M,r,A))"
   274 apply (insert wfrank_strong_replacement' [of r])
   275 apply (simp add: wellfoundedrank_def)
   276 apply (rule strong_replacement_closed)
   277    apply assumption+
   278  apply (rule univalent_is_recfun)
   279    apply (blast intro: wellfounded_trancl)
   280   apply (rule trans_trancl)
   281  apply (simp add: trancl_subset_times, blast)
   282 done
   283 
   284 lemma (in M_wfrank) Ord_wfrank_range [rule_format]:
   285     "[| wellfounded(M,r); a\<in>A; M(r); M(A) |]
   286      ==> \<forall>f. M(f) --> is_recfun(r^+, a, %x f. range(f), f) --> Ord(range(f))"
   287 apply (drule wellfounded_trancl, assumption)
   288 apply (rule wellfounded_induct, assumption+)
   289   apply simp
   290  apply (blast intro: Ord_wfrank_separation, clarify)
   291 txt{*The reasoning in both cases is that we get @{term y} such that
   292    @{term "\<langle>y, x\<rangle> \<in> r^+"}.  We find that
   293    @{term "f`y = restrict(f, r^+ -`` {y})"}. *}
   294 apply (rule OrdI [OF _ Ord_is_Transset])
   295  txt{*An ordinal is a transitive set...*}
   296  apply (simp add: Transset_def)
   297  apply clarify
   298  apply (frule apply_recfun2, assumption)
   299  apply (force simp add: restrict_iff)
   300 txt{*...of ordinals.  This second case requires the induction hyp.*}
   301 apply clarify
   302 apply (rename_tac i y)
   303 apply (frule apply_recfun2, assumption)
   304 apply (frule is_recfun_imp_in_r, assumption)
   305 apply (frule is_recfun_restrict)
   306     (*simp_all won't work*)
   307     apply (simp add: trans_trancl trancl_subset_times)+
   308 apply (drule spec [THEN mp], assumption)
   309 apply (subgoal_tac "M(restrict(f, r^+ -`` {y}))")
   310  apply (drule_tac x="restrict(f, r^+ -`` {y})" in spec)
   311  apply (simp add: function_apply_equality [OF _ is_recfun_imp_function])
   312 apply (blast dest: pair_components_in_M)
   313 done
   314 
   315 lemma (in M_wfrank) Ord_range_wellfoundedrank:
   316     "[| wellfounded(M,r); r \<subseteq> A*A;  M(r); M(A) |]
   317      ==> Ord (range(wellfoundedrank(M,r,A)))"
   318 apply (frule wellfounded_trancl, assumption)
   319 apply (frule trancl_subset_times)
   320 apply (simp add: wellfoundedrank_def)
   321 apply (rule OrdI [OF _ Ord_is_Transset])
   322  prefer 2
   323  txt{*by our previous result the range consists of ordinals.*}
   324  apply (blast intro: Ord_wfrank_range)
   325 txt{*We still must show that the range is a transitive set.*}
   326 apply (simp add: Transset_def, clarify, simp)
   327 apply (rename_tac x i f u)
   328 apply (frule is_recfun_imp_in_r, assumption)
   329 apply (subgoal_tac "M(u) & M(i) & M(x)")
   330  prefer 2 apply (blast dest: transM, clarify)
   331 apply (rule_tac a=u in rangeI)
   332 apply (rule_tac x=u in ReplaceI)
   333   apply simp 
   334   apply (rule_tac x="restrict(f, r^+ -`` {u})" in rexI)
   335    apply (blast intro: is_recfun_restrict trans_trancl dest: apply_recfun2)
   336   apply simp 
   337 apply blast 
   338 txt{*Unicity requirement of Replacement*}
   339 apply clarify
   340 apply (frule apply_recfun2, assumption)
   341 apply (simp add: trans_trancl is_recfun_cut)
   342 done
   343 
   344 lemma (in M_wfrank) function_wellfoundedrank:
   345     "[| wellfounded(M,r); M(r); M(A)|]
   346      ==> function(wellfoundedrank(M,r,A))"
   347 apply (simp add: wellfoundedrank_def function_def, clarify)
   348 txt{*Uniqueness: repeated below!*}
   349 apply (drule is_recfun_functional, assumption)
   350      apply (blast intro: wellfounded_trancl)
   351     apply (simp_all add: trancl_subset_times trans_trancl)
   352 done
   353 
   354 lemma (in M_wfrank) domain_wellfoundedrank:
   355     "[| wellfounded(M,r); M(r); M(A)|]
   356      ==> domain(wellfoundedrank(M,r,A)) = A"
   357 apply (simp add: wellfoundedrank_def function_def)
   358 apply (rule equalityI, auto)
   359 apply (frule transM, assumption)
   360 apply (frule_tac a=x in exists_wfrank, assumption+, clarify)
   361 apply (rule_tac b="range(f)" in domainI)
   362 apply (rule_tac x=x in ReplaceI)
   363   apply simp 
   364   apply (rule_tac x=f in rexI, blast, simp_all)
   365 txt{*Uniqueness (for Replacement): repeated above!*}
   366 apply clarify
   367 apply (drule is_recfun_functional, assumption)
   368     apply (blast intro: wellfounded_trancl)
   369     apply (simp_all add: trancl_subset_times trans_trancl)
   370 done
   371 
   372 lemma (in M_wfrank) wellfoundedrank_type:
   373     "[| wellfounded(M,r);  M(r); M(A)|]
   374      ==> wellfoundedrank(M,r,A) \<in> A -> range(wellfoundedrank(M,r,A))"
   375 apply (frule function_wellfoundedrank [of r A], assumption+)
   376 apply (frule function_imp_Pi)
   377  apply (simp add: wellfoundedrank_def relation_def)
   378  apply blast
   379 apply (simp add: domain_wellfoundedrank)
   380 done
   381 
   382 lemma (in M_wfrank) Ord_wellfoundedrank:
   383     "[| wellfounded(M,r); a \<in> A; r \<subseteq> A*A;  M(r); M(A) |]
   384      ==> Ord(wellfoundedrank(M,r,A) ` a)"
   385 by (blast intro: apply_funtype [OF wellfoundedrank_type]
   386                  Ord_in_Ord [OF Ord_range_wellfoundedrank])
   387 
   388 lemma (in M_wfrank) wellfoundedrank_eq:
   389      "[| is_recfun(r^+, a, %x. range, f);
   390          wellfounded(M,r);  a \<in> A; M(f); M(r); M(A)|]
   391       ==> wellfoundedrank(M,r,A) ` a = range(f)"
   392 apply (rule apply_equality)
   393  prefer 2 apply (blast intro: wellfoundedrank_type)
   394 apply (simp add: wellfoundedrank_def)
   395 apply (rule ReplaceI)
   396   apply (rule_tac x="range(f)" in rexI) 
   397   apply blast
   398  apply simp_all
   399 txt{*Unicity requirement of Replacement*}
   400 apply clarify
   401 apply (drule is_recfun_functional, assumption)
   402     apply (blast intro: wellfounded_trancl)
   403     apply (simp_all add: trancl_subset_times trans_trancl)
   404 done
   405 
   406 
   407 lemma (in M_wfrank) wellfoundedrank_lt:
   408      "[| <a,b> \<in> r;
   409          wellfounded(M,r); r \<subseteq> A*A;  M(r); M(A)|]
   410       ==> wellfoundedrank(M,r,A) ` a < wellfoundedrank(M,r,A) ` b"
   411 apply (frule wellfounded_trancl, assumption)
   412 apply (subgoal_tac "a\<in>A & b\<in>A")
   413  prefer 2 apply blast
   414 apply (simp add: lt_def Ord_wellfoundedrank, clarify)
   415 apply (frule exists_wfrank [of concl: _ b], assumption+, clarify)
   416 apply (rename_tac fb)
   417 apply (frule is_recfun_restrict [of concl: "r^+" a])
   418     apply (rule trans_trancl, assumption)
   419    apply (simp_all add: r_into_trancl trancl_subset_times)
   420 txt{*Still the same goal, but with new @{text is_recfun} assumptions.*}
   421 apply (simp add: wellfoundedrank_eq)
   422 apply (frule_tac a=a in wellfoundedrank_eq, assumption+)
   423    apply (simp_all add: transM [of a])
   424 txt{*We have used equations for wellfoundedrank and now must use some
   425     for  @{text is_recfun}. *}
   426 apply (rule_tac a=a in rangeI)
   427 apply (simp add: is_recfun_type [THEN apply_iff] vimage_singleton_iff
   428                  r_into_trancl apply_recfun r_into_trancl)
   429 done
   430 
   431 
   432 lemma (in M_wfrank) wellfounded_imp_subset_rvimage:
   433      "[|wellfounded(M,r); r \<subseteq> A*A; M(r); M(A)|]
   434       ==> \<exists>i f. Ord(i) & r <= rvimage(A, f, Memrel(i))"
   435 apply (rule_tac x="range(wellfoundedrank(M,r,A))" in exI)
   436 apply (rule_tac x="wellfoundedrank(M,r,A)" in exI)
   437 apply (simp add: Ord_range_wellfoundedrank, clarify)
   438 apply (frule subsetD, assumption, clarify)
   439 apply (simp add: rvimage_iff wellfoundedrank_lt [THEN ltD])
   440 apply (blast intro: apply_rangeI wellfoundedrank_type)
   441 done
   442 
   443 lemma (in M_wfrank) wellfounded_imp_wf:
   444      "[|wellfounded(M,r); relation(r); M(r)|] ==> wf(r)"
   445 by (blast dest!: relation_field_times_field wellfounded_imp_subset_rvimage
   446           intro: wf_rvimage_Ord [THEN wf_subset])
   447 
   448 lemma (in M_wfrank) wellfounded_on_imp_wf_on:
   449      "[|wellfounded_on(M,A,r); relation(r); M(r); M(A)|] ==> wf[A](r)"
   450 apply (simp add: wellfounded_on_iff_wellfounded wf_on_def)
   451 apply (rule wellfounded_imp_wf)
   452 apply (simp_all add: relation_def)
   453 done
   454 
   455 
   456 theorem (in M_wfrank) wf_abs [simp]:
   457      "[|relation(r); M(r)|] ==> wellfounded(M,r) <-> wf(r)"
   458 by (blast intro: wellfounded_imp_wf wf_imp_relativized)
   459 
   460 theorem (in M_wfrank) wf_on_abs [simp]:
   461      "[|relation(r); M(r); M(A)|] ==> wellfounded_on(M,A,r) <-> wf[A](r)"
   462 by (blast intro: wellfounded_on_imp_wf_on wf_on_imp_relativized)
   463 
   464 
   465 text{*absoluteness for wfrec-defined functions.*}
   466 
   467 (*first use is_recfun, then M_is_recfun*)
   468 
   469 lemma (in M_trancl) wfrec_relativize:
   470   "[|wf(r); M(a); M(r);  
   471      strong_replacement(M, \<lambda>x z. \<exists>y[M]. \<exists>g[M].
   472           pair(M,x,y,z) & 
   473           is_recfun(r^+, x, \<lambda>x f. H(x, restrict(f, r -`` {x})), g) & 
   474           y = H(x, restrict(g, r -`` {x}))); 
   475      \<forall>x[M]. \<forall>g[M]. function(g) --> M(H(x,g))|] 
   476    ==> wfrec(r,a,H) = z <-> 
   477        (\<exists>f[M]. is_recfun(r^+, a, \<lambda>x f. H(x, restrict(f, r -`` {x})), f) & 
   478             z = H(a,restrict(f,r-``{a})))"
   479 apply (frule wf_trancl) 
   480 apply (simp add: wftrec_def wfrec_def, safe)
   481  apply (frule wf_exists_is_recfun 
   482               [of concl: "r^+" a "\<lambda>x f. H(x, restrict(f, r -`` {x}))"]) 
   483       apply (simp_all add: trans_trancl function_restrictI trancl_subset_times)
   484  apply (clarify, rule_tac x=x in rexI) 
   485  apply (simp_all add: the_recfun_eq trans_trancl trancl_subset_times)
   486 done
   487 
   488 
   489 text{*Assuming @{term r} is transitive simplifies the occurrences of @{text H}.
   490       The premise @{term "relation(r)"} is necessary 
   491       before we can replace @{term "r^+"} by @{term r}. *}
   492 theorem (in M_trancl) trans_wfrec_relativize:
   493   "[|wf(r);  trans(r);  relation(r);  M(r);  M(a);
   494      strong_replacement(M, \<lambda>x z. \<exists>y[M]. 
   495                 pair(M,x,y,z) & (\<exists>g[M]. is_recfun(r,x,H,g) & y = H(x,g))); 
   496      \<forall>x[M]. \<forall>g[M]. function(g) --> M(H(x,g))|] 
   497    ==> wfrec(r,a,H) = z <-> (\<exists>f[M]. is_recfun(r,a,H,f) & z = H(a,f))" 
   498 by (simp cong: is_recfun_cong
   499          add: wfrec_relativize trancl_eq_r
   500                is_recfun_restrict_idem domain_restrict_idem)
   501 
   502 
   503 lemma (in M_trancl) trans_eq_pair_wfrec_iff:
   504   "[|wf(r);  trans(r); relation(r); M(r);  M(y); 
   505      strong_replacement(M, \<lambda>x z. \<exists>y[M]. 
   506                 pair(M,x,y,z) & (\<exists>g[M]. is_recfun(r,x,H,g) & y = H(x,g))); 
   507      \<forall>x[M]. \<forall>g[M]. function(g) --> M(H(x,g))|] 
   508    ==> y = <x, wfrec(r, x, H)> <-> 
   509        (\<exists>f[M]. is_recfun(r,x,H,f) & y = <x, H(x,f)>)"
   510 apply safe 
   511  apply (simp add: trans_wfrec_relativize [THEN iff_sym, of concl: _ x]) 
   512 txt{*converse direction*}
   513 apply (rule sym)
   514 apply (simp add: trans_wfrec_relativize, blast) 
   515 done
   516 
   517 
   518 subsection{*M is closed under well-founded recursion*}
   519 
   520 text{*Lemma with the awkward premise mentioning @{text wfrec}.*}
   521 lemma (in M_wfrank) wfrec_closed_lemma [rule_format]:
   522      "[|wf(r); M(r); 
   523         strong_replacement(M, \<lambda>x y. y = \<langle>x, wfrec(r, x, H)\<rangle>);
   524         \<forall>x[M]. \<forall>g[M]. function(g) --> M(H(x,g)) |] 
   525       ==> M(a) --> M(wfrec(r,a,H))"
   526 apply (rule_tac a=a in wf_induct, assumption+)
   527 apply (subst wfrec, assumption, clarify)
   528 apply (drule_tac x1=x and x="\<lambda>x\<in>r -`` {x}. wfrec(r, x, H)" 
   529        in rspec [THEN rspec]) 
   530 apply (simp_all add: function_lam) 
   531 apply (blast intro: dest: pair_components_in_M ) 
   532 done
   533 
   534 text{*Eliminates one instance of replacement.*}
   535 lemma (in M_wfrank) wfrec_replacement_iff:
   536      "strong_replacement(M, \<lambda>x z. \<exists>y[M]. \<exists>g[M]. 
   537                 pair(M,x,y,z) & is_recfun(r,x,H,g) & y = H(x,g)) <->
   538       strong_replacement(M, 
   539            \<lambda>x y. \<exists>f[M]. is_recfun(r,x,H,f) & y = <x, H(x,f)>)"
   540 apply simp 
   541 apply (rule strong_replacement_cong, blast) 
   542 done
   543 
   544 text{*Useful version for transitive relations*}
   545 theorem (in M_wfrank) trans_wfrec_closed:
   546      "[|wf(r); trans(r); relation(r); M(r); M(a);
   547         strong_replacement(M, 
   548              \<lambda>x z. \<exists>y[M]. \<exists>g[M].
   549                     pair(M,x,y,z) & is_recfun(r,x,H,g) & y = H(x,g)); 
   550         \<forall>x[M]. \<forall>g[M]. function(g) --> M(H(x,g)) |] 
   551       ==> M(wfrec(r,a,H))"
   552 apply (frule wfrec_replacement_iff [THEN iffD1]) 
   553 apply (rule wfrec_closed_lemma, assumption+) 
   554 apply (simp_all add: wfrec_replacement_iff trans_eq_pair_wfrec_iff) 
   555 done
   556 
   557 section{*Absoluteness without assuming transitivity*}
   558 lemma (in M_trancl) eq_pair_wfrec_iff:
   559   "[|wf(r);  M(r);  M(y); 
   560      strong_replacement(M, \<lambda>x z. \<exists>y[M]. \<exists>g[M].
   561           pair(M,x,y,z) & 
   562           is_recfun(r^+, x, \<lambda>x f. H(x, restrict(f, r -`` {x})), g) & 
   563           y = H(x, restrict(g, r -`` {x}))); 
   564      \<forall>x[M]. \<forall>g[M]. function(g) --> M(H(x,g))|] 
   565    ==> y = <x, wfrec(r, x, H)> <-> 
   566        (\<exists>f[M]. is_recfun(r^+, x, \<lambda>x f. H(x, restrict(f, r -`` {x})), f) & 
   567             y = <x, H(x,restrict(f,r-``{x}))>)"
   568 apply safe  
   569  apply (simp add: wfrec_relativize [THEN iff_sym, of concl: _ x]) 
   570 txt{*converse direction*}
   571 apply (rule sym)
   572 apply (simp add: wfrec_relativize, blast) 
   573 done
   574 
   575 lemma (in M_wfrank) wfrec_closed_lemma [rule_format]:
   576      "[|wf(r); M(r); 
   577         strong_replacement(M, \<lambda>x y. y = \<langle>x, wfrec(r, x, H)\<rangle>);
   578         \<forall>x[M]. \<forall>g[M]. function(g) --> M(H(x,g)) |] 
   579       ==> M(a) --> M(wfrec(r,a,H))"
   580 apply (rule_tac a=a in wf_induct, assumption+)
   581 apply (subst wfrec, assumption, clarify)
   582 apply (drule_tac x1=x and x="\<lambda>x\<in>r -`` {x}. wfrec(r, x, H)" 
   583        in rspec [THEN rspec]) 
   584 apply (simp_all add: function_lam) 
   585 apply (blast intro: dest: pair_components_in_M ) 
   586 done
   587 
   588 text{*Full version not assuming transitivity, but maybe not very useful.*}
   589 theorem (in M_wfrank) wfrec_closed:
   590      "[|wf(r); M(r); M(a);
   591      strong_replacement(M, \<lambda>x z. \<exists>y[M]. \<exists>g[M].
   592           pair(M,x,y,z) & 
   593           is_recfun(r^+, x, \<lambda>x f. H(x, restrict(f, r -`` {x})), g) & 
   594           y = H(x, restrict(g, r -`` {x}))); 
   595         \<forall>x[M]. \<forall>g[M]. function(g) --> M(H(x,g)) |] 
   596       ==> M(wfrec(r,a,H))"
   597 apply (frule wfrec_replacement_iff [THEN iffD1]) 
   598 apply (rule wfrec_closed_lemma, assumption+) 
   599 apply (simp_all add: eq_pair_wfrec_iff) 
   600 done
   601 
   602 end