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