src/ZF/Constructible/Rec_Separation.thy
author paulson
Tue Jul 30 11:39:57 2002 +0200 (2002-07-30)
changeset 13434 78b93a667c01
parent 13429 2232810416fc
child 13437 01b3fc0cc1b8
permissions -rw-r--r--
better sats rules for higher-order operators
     1 
     2 header {*Separation for Facts About Recursion*}
     3 
     4 theory Rec_Separation = Separation + Datatype_absolute:
     5 
     6 text{*This theory proves all instances needed for locales @{text
     7 "M_trancl"}, @{text "M_wfrank"} and @{text "M_datatypes"}*}
     8 
     9 lemma eq_succ_imp_lt: "[|i = succ(j); Ord(i)|] ==> j<i"
    10 by simp
    11 
    12 subsection{*The Locale @{text "M_trancl"}*}
    13 
    14 subsubsection{*Separation for Reflexive/Transitive Closure*}
    15 
    16 text{*First, The Defining Formula*}
    17 
    18 (* "rtran_closure_mem(M,A,r,p) ==
    19       \<exists>nnat[M]. \<exists>n[M]. \<exists>n'[M].
    20        omega(M,nnat) & n\<in>nnat & successor(M,n,n') &
    21        (\<exists>f[M]. typed_function(M,n',A,f) &
    22         (\<exists>x[M]. \<exists>y[M]. \<exists>zero[M]. pair(M,x,y,p) & empty(M,zero) &
    23           fun_apply(M,f,zero,x) & fun_apply(M,f,n,y)) &
    24         (\<forall>j[M]. j\<in>n -->
    25           (\<exists>fj[M]. \<exists>sj[M]. \<exists>fsj[M]. \<exists>ffp[M].
    26             fun_apply(M,f,j,fj) & successor(M,j,sj) &
    27             fun_apply(M,f,sj,fsj) & pair(M,fj,fsj,ffp) & ffp \<in> r)))"*)
    28 constdefs rtran_closure_mem_fm :: "[i,i,i]=>i"
    29  "rtran_closure_mem_fm(A,r,p) ==
    30    Exists(Exists(Exists(
    31     And(omega_fm(2),
    32      And(Member(1,2),
    33       And(succ_fm(1,0),
    34        Exists(And(typed_function_fm(1, A#+4, 0),
    35         And(Exists(Exists(Exists(
    36               And(pair_fm(2,1,p#+7),
    37                And(empty_fm(0),
    38                 And(fun_apply_fm(3,0,2), fun_apply_fm(3,5,1))))))),
    39             Forall(Implies(Member(0,3),
    40              Exists(Exists(Exists(Exists(
    41               And(fun_apply_fm(5,4,3),
    42                And(succ_fm(4,2),
    43                 And(fun_apply_fm(5,2,1),
    44                  And(pair_fm(3,1,0), Member(0,r#+9))))))))))))))))))))"
    45 
    46 
    47 lemma rtran_closure_mem_type [TC]:
    48  "[| x \<in> nat; y \<in> nat; z \<in> nat |] ==> rtran_closure_mem_fm(x,y,z) \<in> formula"
    49 by (simp add: rtran_closure_mem_fm_def)
    50 
    51 lemma arity_rtran_closure_mem_fm [simp]:
    52      "[| x \<in> nat; y \<in> nat; z \<in> nat |]
    53       ==> arity(rtran_closure_mem_fm(x,y,z)) = succ(x) \<union> succ(y) \<union> succ(z)"
    54 by (simp add: rtran_closure_mem_fm_def succ_Un_distrib [symmetric] Un_ac)
    55 
    56 lemma sats_rtran_closure_mem_fm [simp]:
    57    "[| x \<in> nat; y \<in> nat; z \<in> nat; env \<in> list(A)|]
    58     ==> sats(A, rtran_closure_mem_fm(x,y,z), env) <->
    59         rtran_closure_mem(**A, nth(x,env), nth(y,env), nth(z,env))"
    60 by (simp add: rtran_closure_mem_fm_def rtran_closure_mem_def)
    61 
    62 lemma rtran_closure_mem_iff_sats:
    63       "[| nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
    64           i \<in> nat; j \<in> nat; k \<in> nat; env \<in> list(A)|]
    65        ==> rtran_closure_mem(**A, x, y, z) <-> sats(A, rtran_closure_mem_fm(i,j,k), env)"
    66 by (simp add: sats_rtran_closure_mem_fm)
    67 
    68 theorem rtran_closure_mem_reflection:
    69      "REFLECTS[\<lambda>x. rtran_closure_mem(L,f(x),g(x),h(x)),
    70                \<lambda>i x. rtran_closure_mem(**Lset(i),f(x),g(x),h(x))]"
    71 apply (simp only: rtran_closure_mem_def setclass_simps)
    72 apply (intro FOL_reflections function_reflections fun_plus_reflections)
    73 done
    74 
    75 text{*Separation for @{term "rtrancl(r)"}.*}
    76 lemma rtrancl_separation:
    77      "[| L(r); L(A) |] ==> separation (L, rtran_closure_mem(L,A,r))"
    78 apply (rule separation_CollectI)
    79 apply (rule_tac A="{r,A,z}" in subset_LsetE, blast )
    80 apply (rule ReflectsE [OF rtran_closure_mem_reflection], assumption)
    81 apply (drule subset_Lset_ltD, assumption)
    82 apply (erule reflection_imp_L_separation)
    83   apply (simp_all add: lt_Ord2)
    84 apply (rule DPow_LsetI)
    85 apply (rename_tac u)
    86 apply (rule_tac env = "[u,r,A]" in rtran_closure_mem_iff_sats)
    87 apply (rule sep_rules | simp)+
    88 done
    89 
    90 
    91 subsubsection{*Reflexive/Transitive Closure, Internalized*}
    92 
    93 (*  "rtran_closure(M,r,s) ==
    94         \<forall>A[M]. is_field(M,r,A) -->
    95          (\<forall>p[M]. p \<in> s <-> rtran_closure_mem(M,A,r,p))" *)
    96 constdefs rtran_closure_fm :: "[i,i]=>i"
    97  "rtran_closure_fm(r,s) ==
    98    Forall(Implies(field_fm(succ(r),0),
    99                   Forall(Iff(Member(0,succ(succ(s))),
   100                              rtran_closure_mem_fm(1,succ(succ(r)),0)))))"
   101 
   102 lemma rtran_closure_type [TC]:
   103      "[| x \<in> nat; y \<in> nat |] ==> rtran_closure_fm(x,y) \<in> formula"
   104 by (simp add: rtran_closure_fm_def)
   105 
   106 lemma arity_rtran_closure_fm [simp]:
   107      "[| x \<in> nat; y \<in> nat |]
   108       ==> arity(rtran_closure_fm(x,y)) = succ(x) \<union> succ(y)"
   109 by (simp add: rtran_closure_fm_def succ_Un_distrib [symmetric] Un_ac)
   110 
   111 lemma sats_rtran_closure_fm [simp]:
   112    "[| x \<in> nat; y \<in> nat; env \<in> list(A)|]
   113     ==> sats(A, rtran_closure_fm(x,y), env) <->
   114         rtran_closure(**A, nth(x,env), nth(y,env))"
   115 by (simp add: rtran_closure_fm_def rtran_closure_def)
   116 
   117 lemma rtran_closure_iff_sats:
   118       "[| nth(i,env) = x; nth(j,env) = y;
   119           i \<in> nat; j \<in> nat; env \<in> list(A)|]
   120        ==> rtran_closure(**A, x, y) <-> sats(A, rtran_closure_fm(i,j), env)"
   121 by simp
   122 
   123 theorem rtran_closure_reflection:
   124      "REFLECTS[\<lambda>x. rtran_closure(L,f(x),g(x)),
   125                \<lambda>i x. rtran_closure(**Lset(i),f(x),g(x))]"
   126 apply (simp only: rtran_closure_def setclass_simps)
   127 apply (intro FOL_reflections function_reflections rtran_closure_mem_reflection)
   128 done
   129 
   130 
   131 subsubsection{*Transitive Closure of a Relation, Internalized*}
   132 
   133 (*  "tran_closure(M,r,t) ==
   134          \<exists>s[M]. rtran_closure(M,r,s) & composition(M,r,s,t)" *)
   135 constdefs tran_closure_fm :: "[i,i]=>i"
   136  "tran_closure_fm(r,s) ==
   137    Exists(And(rtran_closure_fm(succ(r),0), composition_fm(succ(r),0,succ(s))))"
   138 
   139 lemma tran_closure_type [TC]:
   140      "[| x \<in> nat; y \<in> nat |] ==> tran_closure_fm(x,y) \<in> formula"
   141 by (simp add: tran_closure_fm_def)
   142 
   143 lemma arity_tran_closure_fm [simp]:
   144      "[| x \<in> nat; y \<in> nat |]
   145       ==> arity(tran_closure_fm(x,y)) = succ(x) \<union> succ(y)"
   146 by (simp add: tran_closure_fm_def succ_Un_distrib [symmetric] Un_ac)
   147 
   148 lemma sats_tran_closure_fm [simp]:
   149    "[| x \<in> nat; y \<in> nat; env \<in> list(A)|]
   150     ==> sats(A, tran_closure_fm(x,y), env) <->
   151         tran_closure(**A, nth(x,env), nth(y,env))"
   152 by (simp add: tran_closure_fm_def tran_closure_def)
   153 
   154 lemma tran_closure_iff_sats:
   155       "[| nth(i,env) = x; nth(j,env) = y;
   156           i \<in> nat; j \<in> nat; env \<in> list(A)|]
   157        ==> tran_closure(**A, x, y) <-> sats(A, tran_closure_fm(i,j), env)"
   158 by simp
   159 
   160 theorem tran_closure_reflection:
   161      "REFLECTS[\<lambda>x. tran_closure(L,f(x),g(x)),
   162                \<lambda>i x. tran_closure(**Lset(i),f(x),g(x))]"
   163 apply (simp only: tran_closure_def setclass_simps)
   164 apply (intro FOL_reflections function_reflections
   165              rtran_closure_reflection composition_reflection)
   166 done
   167 
   168 
   169 subsection{*Separation for the Proof of @{text "wellfounded_on_trancl"}*}
   170 
   171 lemma wellfounded_trancl_reflects:
   172   "REFLECTS[\<lambda>x. \<exists>w[L]. \<exists>wx[L]. \<exists>rp[L].
   173                  w \<in> Z & pair(L,w,x,wx) & tran_closure(L,r,rp) & wx \<in> rp,
   174    \<lambda>i x. \<exists>w \<in> Lset(i). \<exists>wx \<in> Lset(i). \<exists>rp \<in> Lset(i).
   175        w \<in> Z & pair(**Lset(i),w,x,wx) & tran_closure(**Lset(i),r,rp) &
   176        wx \<in> rp]"
   177 by (intro FOL_reflections function_reflections fun_plus_reflections
   178           tran_closure_reflection)
   179 
   180 
   181 lemma wellfounded_trancl_separation:
   182          "[| L(r); L(Z) |] ==>
   183           separation (L, \<lambda>x.
   184               \<exists>w[L]. \<exists>wx[L]. \<exists>rp[L].
   185                w \<in> Z & pair(L,w,x,wx) & tran_closure(L,r,rp) & wx \<in> rp)"
   186 apply (rule separation_CollectI)
   187 apply (rule_tac A="{r,Z,z}" in subset_LsetE, blast )
   188 apply (rule ReflectsE [OF wellfounded_trancl_reflects], assumption)
   189 apply (drule subset_Lset_ltD, assumption)
   190 apply (erule reflection_imp_L_separation)
   191   apply (simp_all add: lt_Ord2)
   192 apply (rule DPow_LsetI)
   193 apply (rename_tac u)
   194 apply (rule bex_iff_sats conj_iff_sats)+
   195 apply (rule_tac env = "[w,u,r,Z]" in mem_iff_sats)
   196 apply (rule sep_rules tran_closure_iff_sats | simp)+
   197 done
   198 
   199 
   200 subsubsection{*Instantiating the locale @{text M_trancl}*}
   201 
   202 theorem M_trancl_L: "PROP M_trancl(L)"
   203   apply (rule M_trancl.intro)
   204     apply (rule M_axioms.axioms [OF M_axioms_L])+
   205   apply (rule M_trancl_axioms.intro)
   206    apply (assumption | rule
   207      rtrancl_separation wellfounded_trancl_separation)+
   208   done
   209 
   210 lemmas iterates_abs = M_trancl.iterates_abs [OF M_trancl_L]
   211   and rtran_closure_rtrancl = M_trancl.rtran_closure_rtrancl [OF M_trancl_L]
   212   and rtrancl_closed = M_trancl.rtrancl_closed [OF M_trancl_L]
   213   and rtrancl_abs = M_trancl.rtrancl_abs [OF M_trancl_L]
   214   and trancl_closed = M_trancl.trancl_closed [OF M_trancl_L]
   215   and trancl_abs = M_trancl.trancl_abs [OF M_trancl_L]
   216   and wellfounded_on_trancl = M_trancl.wellfounded_on_trancl [OF M_trancl_L]
   217   and wellfounded_trancl = M_trancl.wellfounded_trancl [OF M_trancl_L]
   218   and wfrec_relativize = M_trancl.wfrec_relativize [OF M_trancl_L]
   219   and trans_wfrec_relativize = M_trancl.trans_wfrec_relativize [OF M_trancl_L]
   220   and trans_wfrec_abs = M_trancl.trans_wfrec_abs [OF M_trancl_L]
   221   and trans_eq_pair_wfrec_iff = M_trancl.trans_eq_pair_wfrec_iff [OF M_trancl_L]
   222   and eq_pair_wfrec_iff = M_trancl.eq_pair_wfrec_iff [OF M_trancl_L]
   223 
   224 declare rtrancl_closed [intro,simp]
   225 declare rtrancl_abs [simp]
   226 declare trancl_closed [intro,simp]
   227 declare trancl_abs [simp]
   228 
   229 
   230 subsection{*Well-Founded Recursion!*}
   231 
   232 (* M_is_recfun :: "[i=>o, [i,i,i]=>o, i, i, i] => o"
   233    "M_is_recfun(M,MH,r,a,f) ==
   234      \<forall>z[M]. z \<in> f <->
   235             5      4       3       2       1           0
   236             (\<exists>x[M]. \<exists>y[M]. \<exists>xa[M]. \<exists>sx[M]. \<exists>r_sx[M]. \<exists>f_r_sx[M].
   237                pair(M,x,y,z) & pair(M,x,a,xa) & upair(M,x,x,sx) &
   238                pre_image(M,r,sx,r_sx) & restriction(M,f,r_sx,f_r_sx) &
   239                xa \<in> r & MH(x, f_r_sx, y))"
   240 *)
   241 
   242 text{*The three arguments of @{term p} are always 5, 0, 4.*}
   243 constdefs is_recfun_fm :: "[i, i, i, i]=>i"
   244  "is_recfun_fm(p,r,a,f) == 
   245    Forall(Iff(Member(0,succ(f)),
   246     Exists(Exists(Exists(Exists(Exists(Exists(
   247      And(pair_fm(5,4,6),
   248       And(pair_fm(5,a#+7,3),
   249        And(upair_fm(5,5,2),
   250         And(pre_image_fm(r#+7,2,1),
   251          And(restriction_fm(f#+7,1,0),
   252           And(Member(3,r#+7), p))))))))))))))"
   253 
   254 
   255 lemma is_recfun_type [TC]:
   256      "[| p \<in> formula; x \<in> nat; y \<in> nat; z \<in> nat |] 
   257       ==> is_recfun_fm(p,x,y,z) \<in> formula"
   258 by (simp add: is_recfun_fm_def)
   259 
   260 lemma sats_is_recfun_fm:
   261   assumes MH_iff_sats: 
   262       "!!a0 a1 a2 a3 a4 a5 a6. 
   263         [|a0\<in>A; a1\<in>A; a2\<in>A; a3\<in>A; a4\<in>A; a5\<in>A; a6\<in>A|] ==> 
   264         MH(a5, a0, a4) <-> 
   265        sats(A, p, Cons(a0,Cons(a1,Cons(a2,Cons(a3,Cons(a4,Cons(a5,Cons(a6,env))))))))"
   266   shows 
   267       "[|x \<in> nat; y \<in> nat; z \<in> nat; env \<in> list(A)|]
   268        ==> sats(A, is_recfun_fm(p,x,y,z), env) <->
   269            M_is_recfun(**A, MH, nth(x,env), nth(y,env), nth(z,env))"
   270 by (simp add: is_recfun_fm_def M_is_recfun_def MH_iff_sats [THEN iff_sym])
   271 (*
   272 apply (rule ball_cong bex_cong iff_cong conj_cong refl iff_refl) +
   273  sats(A, p,
   274    Cons(xf, Cons(xe, Cons(xd, Cons(xc, Cons(xb, Cons(xaa, Cons(xa, env)))))))) 
   275 \<longleftrightarrow> MH(xaa, xf, xb)
   276 
   277 MH(nth(5,env), nth(0,env), nth(4,env)) <-> sats(A, p, env);
   278 *)
   279 
   280 (*      "!!x y z. [|x\<in>A; y\<in>A; z\<in>A|] ==> MH(x,y,z) <-> sats(A, p, env)"
   281 *)
   282 
   283 lemma is_recfun_iff_sats:
   284   assumes MH_iff_sats: 
   285       "!!a0 a1 a2 a3 a4 a5 a6. 
   286         [|a0\<in>A; a1\<in>A; a2\<in>A; a3\<in>A; a4\<in>A; a5\<in>A; a6\<in>A|] ==> 
   287         MH(a5, a0, a4) <-> 
   288        sats(A, p, Cons(a0,Cons(a1,Cons(a2,Cons(a3,Cons(a4,Cons(a5,Cons(a6,env))))))))"
   289   shows
   290   "[| nth(i,env) = x; nth(j,env) = y; nth(k,env) = z; 
   291       i \<in> nat; j \<in> nat; k \<in> nat; env \<in> list(A)|]
   292    ==> M_is_recfun(**A, MH, x, y, z) <-> sats(A, is_recfun_fm(p,i,j,k), env)"
   293 apply (rule iff_sym) 
   294 apply (rule iff_trans)
   295 apply (rule sats_is_recfun_fm [of A MH]) 
   296 apply (rule MH_iff_sats, simp_all) 
   297 done
   298 (*FIXME: surely proof can be improved?*)
   299 
   300 
   301 theorem is_recfun_reflection:
   302   assumes MH_reflection:
   303     "!!f g h. REFLECTS[\<lambda>x. MH(L, f(x), g(x), h(x)),
   304                      \<lambda>i x. MH(**Lset(i), f(x), g(x), h(x))]"
   305   shows "REFLECTS[\<lambda>x. M_is_recfun(L, MH(L), f(x), g(x), h(x)),
   306                \<lambda>i x. M_is_recfun(**Lset(i), MH(**Lset(i)), f(x), g(x), h(x))]"
   307 apply (simp (no_asm_use) only: M_is_recfun_def setclass_simps)
   308 apply (intro FOL_reflections function_reflections
   309              restriction_reflection MH_reflection)
   310 done
   311 
   312 text{*Currently, @{text sats}-theorems for higher-order operators don't seem
   313 useful.  Reflection theorems do work, though.  This one avoids the repetition
   314 of the @{text MH}-term.*}
   315 theorem is_wfrec_reflection:
   316   assumes MH_reflection:
   317     "!!f g h. REFLECTS[\<lambda>x. MH(L, f(x), g(x), h(x)),
   318                      \<lambda>i x. MH(**Lset(i), f(x), g(x), h(x))]"
   319   shows "REFLECTS[\<lambda>x. is_wfrec(L, MH(L), f(x), g(x), h(x)),
   320                \<lambda>i x. is_wfrec(**Lset(i), MH(**Lset(i)), f(x), g(x), h(x))]"
   321 apply (simp (no_asm_use) only: is_wfrec_def setclass_simps)
   322 apply (intro FOL_reflections MH_reflection is_recfun_reflection)
   323 done
   324 
   325 subsection{*The Locale @{text "M_wfrank"}*}
   326 
   327 subsubsection{*Separation for @{term "wfrank"}*}
   328 
   329 lemma wfrank_Reflects:
   330  "REFLECTS[\<lambda>x. \<forall>rplus[L]. tran_closure(L,r,rplus) -->
   331               ~ (\<exists>f[L]. M_is_recfun(L, %x f y. is_range(L,f,y), rplus, x, f)),
   332       \<lambda>i x. \<forall>rplus \<in> Lset(i). tran_closure(**Lset(i),r,rplus) -->
   333          ~ (\<exists>f \<in> Lset(i).
   334             M_is_recfun(**Lset(i), %x f y. is_range(**Lset(i),f,y),
   335                         rplus, x, f))]"
   336 by (intro FOL_reflections function_reflections is_recfun_reflection tran_closure_reflection)
   337 
   338 lemma wfrank_separation:
   339      "L(r) ==>
   340       separation (L, \<lambda>x. \<forall>rplus[L]. tran_closure(L,r,rplus) -->
   341          ~ (\<exists>f[L]. M_is_recfun(L, %x f y. is_range(L,f,y), rplus, x, f)))"
   342 apply (rule separation_CollectI)
   343 apply (rule_tac A="{r,z}" in subset_LsetE, blast )
   344 apply (rule ReflectsE [OF wfrank_Reflects], assumption)
   345 apply (drule subset_Lset_ltD, assumption)
   346 apply (erule reflection_imp_L_separation)
   347   apply (simp_all add: lt_Ord2, clarify)
   348 apply (rule DPow_LsetI)
   349 apply (rename_tac u)
   350 apply (rule ball_iff_sats imp_iff_sats)+
   351 apply (rule_tac env="[rplus,u,r]" in tran_closure_iff_sats)
   352 apply (rule sep_rules is_recfun_iff_sats | simp)+
   353 done
   354 
   355 
   356 subsubsection{*Replacement for @{term "wfrank"}*}
   357 
   358 lemma wfrank_replacement_Reflects:
   359  "REFLECTS[\<lambda>z. \<exists>x[L]. x \<in> A &
   360         (\<forall>rplus[L]. tran_closure(L,r,rplus) -->
   361          (\<exists>y[L]. \<exists>f[L]. pair(L,x,y,z)  &
   362                         M_is_recfun(L, %x f y. is_range(L,f,y), rplus, x, f) &
   363                         is_range(L,f,y))),
   364  \<lambda>i z. \<exists>x \<in> Lset(i). x \<in> A &
   365       (\<forall>rplus \<in> Lset(i). tran_closure(**Lset(i),r,rplus) -->
   366        (\<exists>y \<in> Lset(i). \<exists>f \<in> Lset(i). pair(**Lset(i),x,y,z)  &
   367          M_is_recfun(**Lset(i), %x f y. is_range(**Lset(i),f,y), rplus, x, f) &
   368          is_range(**Lset(i),f,y)))]"
   369 by (intro FOL_reflections function_reflections fun_plus_reflections
   370              is_recfun_reflection tran_closure_reflection)
   371 
   372 
   373 lemma wfrank_strong_replacement:
   374      "L(r) ==>
   375       strong_replacement(L, \<lambda>x z.
   376          \<forall>rplus[L]. tran_closure(L,r,rplus) -->
   377          (\<exists>y[L]. \<exists>f[L]. pair(L,x,y,z)  &
   378                         M_is_recfun(L, %x f y. is_range(L,f,y), rplus, x, f) &
   379                         is_range(L,f,y)))"
   380 apply (rule strong_replacementI)
   381 apply (rule rallI)
   382 apply (rename_tac B)
   383 apply (rule separation_CollectI)
   384 apply (rule_tac A="{B,r,z}" in subset_LsetE, blast )
   385 apply (rule ReflectsE [OF wfrank_replacement_Reflects], assumption)
   386 apply (drule subset_Lset_ltD, assumption)
   387 apply (erule reflection_imp_L_separation)
   388   apply (simp_all add: lt_Ord2)
   389 apply (rule DPow_LsetI)
   390 apply (rename_tac u)
   391 apply (rule bex_iff_sats ball_iff_sats conj_iff_sats)+
   392 apply (rule_tac env = "[x,u,B,r]" in mem_iff_sats)
   393 apply (rule sep_rules tran_closure_iff_sats is_recfun_iff_sats | simp)+
   394 done
   395 
   396 
   397 subsubsection{*Separation for Proving @{text Ord_wfrank_range}*}
   398 
   399 lemma Ord_wfrank_Reflects:
   400  "REFLECTS[\<lambda>x. \<forall>rplus[L]. tran_closure(L,r,rplus) -->
   401           ~ (\<forall>f[L]. \<forall>rangef[L].
   402              is_range(L,f,rangef) -->
   403              M_is_recfun(L, \<lambda>x f y. is_range(L,f,y), rplus, x, f) -->
   404              ordinal(L,rangef)),
   405       \<lambda>i x. \<forall>rplus \<in> Lset(i). tran_closure(**Lset(i),r,rplus) -->
   406           ~ (\<forall>f \<in> Lset(i). \<forall>rangef \<in> Lset(i).
   407              is_range(**Lset(i),f,rangef) -->
   408              M_is_recfun(**Lset(i), \<lambda>x f y. is_range(**Lset(i),f,y),
   409                          rplus, x, f) -->
   410              ordinal(**Lset(i),rangef))]"
   411 by (intro FOL_reflections function_reflections is_recfun_reflection
   412           tran_closure_reflection ordinal_reflection)
   413 
   414 lemma  Ord_wfrank_separation:
   415      "L(r) ==>
   416       separation (L, \<lambda>x.
   417          \<forall>rplus[L]. tran_closure(L,r,rplus) -->
   418           ~ (\<forall>f[L]. \<forall>rangef[L].
   419              is_range(L,f,rangef) -->
   420              M_is_recfun(L, \<lambda>x f y. is_range(L,f,y), rplus, x, f) -->
   421              ordinal(L,rangef)))"
   422 apply (rule separation_CollectI)
   423 apply (rule_tac A="{r,z}" in subset_LsetE, blast )
   424 apply (rule ReflectsE [OF Ord_wfrank_Reflects], assumption)
   425 apply (drule subset_Lset_ltD, assumption)
   426 apply (erule reflection_imp_L_separation)
   427   apply (simp_all add: lt_Ord2, clarify)
   428 apply (rule DPow_LsetI)
   429 apply (rename_tac u)
   430 apply (rule ball_iff_sats imp_iff_sats)+
   431 apply (rule_tac env="[rplus,u,r]" in tran_closure_iff_sats)
   432 apply (rule sep_rules is_recfun_iff_sats | simp)+
   433 done
   434 
   435 
   436 subsubsection{*Instantiating the locale @{text M_wfrank}*}
   437 
   438 theorem M_wfrank_L: "PROP M_wfrank(L)"
   439   apply (rule M_wfrank.intro)
   440      apply (rule M_trancl.axioms [OF M_trancl_L])+
   441   apply (rule M_wfrank_axioms.intro)
   442    apply (assumption | rule
   443      wfrank_separation wfrank_strong_replacement Ord_wfrank_separation)+
   444   done
   445 
   446 lemmas iterates_closed = M_wfrank.iterates_closed [OF M_wfrank_L]
   447   and exists_wfrank = M_wfrank.exists_wfrank [OF M_wfrank_L]
   448   and M_wellfoundedrank = M_wfrank.M_wellfoundedrank [OF M_wfrank_L]
   449   and Ord_wfrank_range = M_wfrank.Ord_wfrank_range [OF M_wfrank_L]
   450   and Ord_range_wellfoundedrank = M_wfrank.Ord_range_wellfoundedrank [OF M_wfrank_L]
   451   and function_wellfoundedrank = M_wfrank.function_wellfoundedrank [OF M_wfrank_L]
   452   and domain_wellfoundedrank = M_wfrank.domain_wellfoundedrank [OF M_wfrank_L]
   453   and wellfoundedrank_type = M_wfrank.wellfoundedrank_type [OF M_wfrank_L]
   454   and Ord_wellfoundedrank = M_wfrank.Ord_wellfoundedrank [OF M_wfrank_L]
   455   and wellfoundedrank_eq = M_wfrank.wellfoundedrank_eq [OF M_wfrank_L]
   456   and wellfoundedrank_lt = M_wfrank.wellfoundedrank_lt [OF M_wfrank_L]
   457   and wellfounded_imp_subset_rvimage = M_wfrank.wellfounded_imp_subset_rvimage [OF M_wfrank_L]
   458   and wellfounded_imp_wf = M_wfrank.wellfounded_imp_wf [OF M_wfrank_L]
   459   and wellfounded_on_imp_wf_on = M_wfrank.wellfounded_on_imp_wf_on [OF M_wfrank_L]
   460   and wf_abs = M_wfrank.wf_abs [OF M_wfrank_L]
   461   and wf_on_abs = M_wfrank.wf_on_abs [OF M_wfrank_L]
   462   and wfrec_replacement_iff = M_wfrank.wfrec_replacement_iff [OF M_wfrank_L]
   463   and trans_wfrec_closed = M_wfrank.trans_wfrec_closed [OF M_wfrank_L]
   464   and wfrec_closed = M_wfrank.wfrec_closed [OF M_wfrank_L]
   465 
   466 declare iterates_closed [intro,simp]
   467 declare Ord_wfrank_range [rule_format]
   468 declare wf_abs [simp]
   469 declare wf_on_abs [simp]
   470 
   471 
   472 subsection{*For Datatypes*}
   473 
   474 subsubsection{*Binary Products, Internalized*}
   475 
   476 constdefs cartprod_fm :: "[i,i,i]=>i"
   477 (* "cartprod(M,A,B,z) ==
   478         \<forall>u[M]. u \<in> z <-> (\<exists>x[M]. x\<in>A & (\<exists>y[M]. y\<in>B & pair(M,x,y,u)))" *)
   479     "cartprod_fm(A,B,z) ==
   480        Forall(Iff(Member(0,succ(z)),
   481                   Exists(And(Member(0,succ(succ(A))),
   482                          Exists(And(Member(0,succ(succ(succ(B)))),
   483                                     pair_fm(1,0,2)))))))"
   484 
   485 lemma cartprod_type [TC]:
   486      "[| x \<in> nat; y \<in> nat; z \<in> nat |] ==> cartprod_fm(x,y,z) \<in> formula"
   487 by (simp add: cartprod_fm_def)
   488 
   489 lemma arity_cartprod_fm [simp]:
   490      "[| x \<in> nat; y \<in> nat; z \<in> nat |]
   491       ==> arity(cartprod_fm(x,y,z)) = succ(x) \<union> succ(y) \<union> succ(z)"
   492 by (simp add: cartprod_fm_def succ_Un_distrib [symmetric] Un_ac)
   493 
   494 lemma sats_cartprod_fm [simp]:
   495    "[| x \<in> nat; y \<in> nat; z \<in> nat; env \<in> list(A)|]
   496     ==> sats(A, cartprod_fm(x,y,z), env) <->
   497         cartprod(**A, nth(x,env), nth(y,env), nth(z,env))"
   498 by (simp add: cartprod_fm_def cartprod_def)
   499 
   500 lemma cartprod_iff_sats:
   501       "[| nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
   502           i \<in> nat; j \<in> nat; k \<in> nat; env \<in> list(A)|]
   503        ==> cartprod(**A, x, y, z) <-> sats(A, cartprod_fm(i,j,k), env)"
   504 by (simp add: sats_cartprod_fm)
   505 
   506 theorem cartprod_reflection:
   507      "REFLECTS[\<lambda>x. cartprod(L,f(x),g(x),h(x)),
   508                \<lambda>i x. cartprod(**Lset(i),f(x),g(x),h(x))]"
   509 apply (simp only: cartprod_def setclass_simps)
   510 apply (intro FOL_reflections pair_reflection)
   511 done
   512 
   513 
   514 subsubsection{*Binary Sums, Internalized*}
   515 
   516 (* "is_sum(M,A,B,Z) ==
   517        \<exists>A0[M]. \<exists>n1[M]. \<exists>s1[M]. \<exists>B1[M].
   518          3      2       1        0
   519        number1(M,n1) & cartprod(M,n1,A,A0) & upair(M,n1,n1,s1) &
   520        cartprod(M,s1,B,B1) & union(M,A0,B1,Z)"  *)
   521 constdefs sum_fm :: "[i,i,i]=>i"
   522     "sum_fm(A,B,Z) ==
   523        Exists(Exists(Exists(Exists(
   524         And(number1_fm(2),
   525             And(cartprod_fm(2,A#+4,3),
   526                 And(upair_fm(2,2,1),
   527                     And(cartprod_fm(1,B#+4,0), union_fm(3,0,Z#+4)))))))))"
   528 
   529 lemma sum_type [TC]:
   530      "[| x \<in> nat; y \<in> nat; z \<in> nat |] ==> sum_fm(x,y,z) \<in> formula"
   531 by (simp add: sum_fm_def)
   532 
   533 lemma arity_sum_fm [simp]:
   534      "[| x \<in> nat; y \<in> nat; z \<in> nat |]
   535       ==> arity(sum_fm(x,y,z)) = succ(x) \<union> succ(y) \<union> succ(z)"
   536 by (simp add: sum_fm_def succ_Un_distrib [symmetric] Un_ac)
   537 
   538 lemma sats_sum_fm [simp]:
   539    "[| x \<in> nat; y \<in> nat; z \<in> nat; env \<in> list(A)|]
   540     ==> sats(A, sum_fm(x,y,z), env) <->
   541         is_sum(**A, nth(x,env), nth(y,env), nth(z,env))"
   542 by (simp add: sum_fm_def is_sum_def)
   543 
   544 lemma sum_iff_sats:
   545       "[| nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
   546           i \<in> nat; j \<in> nat; k \<in> nat; env \<in> list(A)|]
   547        ==> is_sum(**A, x, y, z) <-> sats(A, sum_fm(i,j,k), env)"
   548 by simp
   549 
   550 theorem sum_reflection:
   551      "REFLECTS[\<lambda>x. is_sum(L,f(x),g(x),h(x)),
   552                \<lambda>i x. is_sum(**Lset(i),f(x),g(x),h(x))]"
   553 apply (simp only: is_sum_def setclass_simps)
   554 apply (intro FOL_reflections function_reflections cartprod_reflection)
   555 done
   556 
   557 
   558 subsubsection{*The Operator @{term quasinat}*}
   559 
   560 (* "is_quasinat(M,z) == empty(M,z) | (\<exists>m[M]. successor(M,m,z))" *)
   561 constdefs quasinat_fm :: "i=>i"
   562     "quasinat_fm(z) == Or(empty_fm(z), Exists(succ_fm(0,succ(z))))"
   563 
   564 lemma quasinat_type [TC]:
   565      "x \<in> nat ==> quasinat_fm(x) \<in> formula"
   566 by (simp add: quasinat_fm_def)
   567 
   568 lemma arity_quasinat_fm [simp]:
   569      "x \<in> nat ==> arity(quasinat_fm(x)) = succ(x)"
   570 by (simp add: quasinat_fm_def succ_Un_distrib [symmetric] Un_ac)
   571 
   572 lemma sats_quasinat_fm [simp]:
   573    "[| x \<in> nat; env \<in> list(A)|]
   574     ==> sats(A, quasinat_fm(x), env) <-> is_quasinat(**A, nth(x,env))"
   575 by (simp add: quasinat_fm_def is_quasinat_def)
   576 
   577 lemma quasinat_iff_sats:
   578       "[| nth(i,env) = x; nth(j,env) = y;
   579           i \<in> nat; env \<in> list(A)|]
   580        ==> is_quasinat(**A, x) <-> sats(A, quasinat_fm(i), env)"
   581 by simp
   582 
   583 theorem quasinat_reflection:
   584      "REFLECTS[\<lambda>x. is_quasinat(L,f(x)),
   585                \<lambda>i x. is_quasinat(**Lset(i),f(x))]"
   586 apply (simp only: is_quasinat_def setclass_simps)
   587 apply (intro FOL_reflections function_reflections)
   588 done
   589 
   590 
   591 subsubsection{*The Operator @{term is_nat_case}*}
   592 text{*I could not get it to work with the more natural assumption that 
   593  @{term is_b} takes two arguments.  Instead it must be a formula where 1 and 0
   594  stand for @{term m} and @{term b}, respectively.*}
   595 
   596 (* is_nat_case :: "[i=>o, i, [i,i]=>o, i, i] => o"
   597     "is_nat_case(M, a, is_b, k, z) ==
   598        (empty(M,k) --> z=a) &
   599        (\<forall>m[M]. successor(M,m,k) --> is_b(m,z)) &
   600        (is_quasinat(M,k) | empty(M,z))" *)
   601 text{*The formula @{term is_b} has free variables 1 and 0.*}
   602 constdefs is_nat_case_fm :: "[i, i, i, i]=>i"
   603  "is_nat_case_fm(a,is_b,k,z) == 
   604     And(Implies(empty_fm(k), Equal(z,a)),
   605         And(Forall(Implies(succ_fm(0,succ(k)), 
   606                    Forall(Implies(Equal(0,succ(succ(z))), is_b)))),
   607             Or(quasinat_fm(k), empty_fm(z))))"
   608 
   609 lemma is_nat_case_type [TC]:
   610      "[| is_b \<in> formula;  
   611          x \<in> nat; y \<in> nat; z \<in> nat |] 
   612       ==> is_nat_case_fm(x,is_b,y,z) \<in> formula"
   613 by (simp add: is_nat_case_fm_def)
   614 
   615 lemma sats_is_nat_case_fm:
   616   assumes is_b_iff_sats: 
   617       "!!a. a \<in> A ==> is_b(a,nth(z, env)) <-> 
   618                       sats(A, p, Cons(nth(z,env), Cons(a, env)))"
   619   shows 
   620       "[|x \<in> nat; y \<in> nat; z < length(env); env \<in> list(A)|]
   621        ==> sats(A, is_nat_case_fm(x,p,y,z), env) <->
   622            is_nat_case(**A, nth(x,env), is_b, nth(y,env), nth(z,env))"
   623 apply (frule lt_length_in_nat, assumption)
   624 apply (simp add: is_nat_case_fm_def is_nat_case_def is_b_iff_sats [THEN iff_sym])
   625 done
   626 
   627 lemma is_nat_case_iff_sats:
   628   "[| (!!a. a \<in> A ==> is_b(a,z) <->
   629                       sats(A, p, Cons(z, Cons(a,env))));
   630       nth(i,env) = x; nth(j,env) = y; nth(k,env) = z; 
   631       i \<in> nat; j \<in> nat; k < length(env); env \<in> list(A)|]
   632    ==> is_nat_case(**A, x, is_b, y, z) <-> sats(A, is_nat_case_fm(i,p,j,k), env)"
   633 by (simp add: sats_is_nat_case_fm [of A is_b])
   634 
   635 
   636 text{*The second argument of @{term is_b} gives it direct access to @{term x},
   637   which is essential for handling free variable references.  Without this
   638   argument, we cannot prove reflection for @{term iterates_MH}.*}
   639 theorem is_nat_case_reflection:
   640   assumes is_b_reflection:
   641     "!!h f g. REFLECTS[\<lambda>x. is_b(L, h(x), f(x), g(x)),
   642                      \<lambda>i x. is_b(**Lset(i), h(x), f(x), g(x))]"
   643   shows "REFLECTS[\<lambda>x. is_nat_case(L, f(x), is_b(L,x), g(x), h(x)),
   644                \<lambda>i x. is_nat_case(**Lset(i), f(x), is_b(**Lset(i), x), g(x), h(x))]"
   645 apply (simp (no_asm_use) only: is_nat_case_def setclass_simps)
   646 apply (intro FOL_reflections function_reflections
   647              restriction_reflection is_b_reflection quasinat_reflection)
   648 done
   649 
   650 
   651 
   652 subsection{*The Operator @{term iterates_MH}, Needed for Iteration*}
   653 
   654 (*  iterates_MH :: "[i=>o, [i,i]=>o, i, i, i, i] => o"
   655    "iterates_MH(M,isF,v,n,g,z) ==
   656         is_nat_case(M, v, \<lambda>m u. \<exists>gm[M]. fun_apply(M,g,m,gm) & isF(gm,u),
   657                     n, z)" *)
   658 constdefs iterates_MH_fm :: "[i, i, i, i, i]=>i"
   659  "iterates_MH_fm(isF,v,n,g,z) == 
   660     is_nat_case_fm(v, 
   661       Exists(And(fun_apply_fm(succ(succ(succ(g))),2,0), 
   662                      Forall(Implies(Equal(0,2), isF)))), 
   663       n, z)"
   664 
   665 lemma iterates_MH_type [TC]:
   666      "[| p \<in> formula;  
   667          v \<in> nat; x \<in> nat; y \<in> nat; z \<in> nat |] 
   668       ==> iterates_MH_fm(p,v,x,y,z) \<in> formula"
   669 by (simp add: iterates_MH_fm_def)
   670 
   671 lemma sats_iterates_MH_fm:
   672   assumes is_F_iff_sats:
   673       "!!a b c d. [| a \<in> A; b \<in> A; c \<in> A; d \<in> A|]
   674               ==> is_F(a,b) <->
   675                   sats(A, p, Cons(b, Cons(a, Cons(c, Cons(d,env)))))"
   676   shows 
   677       "[|v \<in> nat; x \<in> nat; y \<in> nat; z < length(env); env \<in> list(A)|]
   678        ==> sats(A, iterates_MH_fm(p,v,x,y,z), env) <->
   679            iterates_MH(**A, is_F, nth(v,env), nth(x,env), nth(y,env), nth(z,env))"
   680 apply (frule lt_length_in_nat, assumption)  
   681 apply (simp add: iterates_MH_fm_def iterates_MH_def sats_is_nat_case_fm 
   682               is_F_iff_sats [symmetric])
   683 apply (rule is_nat_case_cong) 
   684 apply (simp_all add: setclass_def)
   685 done
   686 
   687 
   688 lemma iterates_MH_iff_sats:
   689   "[| (!!a b c d. [| a \<in> A; b \<in> A; c \<in> A; d \<in> A|]
   690               ==> is_F(a,b) <->
   691                   sats(A, p, Cons(b, Cons(a, Cons(c, Cons(d,env))))));
   692       nth(i',env) = v; nth(i,env) = x; nth(j,env) = y; nth(k,env) = z; 
   693       i' \<in> nat; i \<in> nat; j \<in> nat; k < length(env); env \<in> list(A)|]
   694    ==> iterates_MH(**A, is_F, v, x, y, z) <->
   695        sats(A, iterates_MH_fm(p,i',i,j,k), env)"
   696 apply (rule iff_sym) 
   697 apply (rule iff_trans)
   698 apply (rule sats_iterates_MH_fm [of A is_F], blast)  
   699 apply simp_all 
   700 done
   701 (*FIXME: surely proof can be improved?*)
   702 
   703 
   704 theorem iterates_MH_reflection:
   705   assumes p_reflection:
   706     "!!f g h. REFLECTS[\<lambda>x. p(L, f(x), g(x)),
   707                      \<lambda>i x. p(**Lset(i), f(x), g(x))]"
   708  shows "REFLECTS[\<lambda>x. iterates_MH(L, p(L), e(x), f(x), g(x), h(x)),
   709                \<lambda>i x. iterates_MH(**Lset(i), p(**Lset(i)), e(x), f(x), g(x), h(x))]"
   710 apply (simp (no_asm_use) only: iterates_MH_def)
   711 txt{*Must be careful: simplifying with @{text setclass_simps} above would
   712      change @{text "\<exists>gm[**Lset(i)]"} into @{text "\<exists>gm \<in> Lset(i)"}, when
   713      it would no longer match rule @{text is_nat_case_reflection}. *}
   714 apply (rule is_nat_case_reflection)
   715 apply (simp (no_asm_use) only: setclass_simps)
   716 apply (intro FOL_reflections function_reflections is_nat_case_reflection
   717              restriction_reflection p_reflection)
   718 done
   719 
   720 
   721 
   722 subsection{*@{term L} is Closed Under the Operator @{term list}*}
   723 
   724 subsubsection{*The List Functor, Internalized*}
   725 
   726 constdefs list_functor_fm :: "[i,i,i]=>i"
   727 (* "is_list_functor(M,A,X,Z) ==
   728         \<exists>n1[M]. \<exists>AX[M].
   729          number1(M,n1) & cartprod(M,A,X,AX) & is_sum(M,n1,AX,Z)" *)
   730     "list_functor_fm(A,X,Z) ==
   731        Exists(Exists(
   732         And(number1_fm(1),
   733             And(cartprod_fm(A#+2,X#+2,0), sum_fm(1,0,Z#+2)))))"
   734 
   735 lemma list_functor_type [TC]:
   736      "[| x \<in> nat; y \<in> nat; z \<in> nat |] ==> list_functor_fm(x,y,z) \<in> formula"
   737 by (simp add: list_functor_fm_def)
   738 
   739 lemma arity_list_functor_fm [simp]:
   740      "[| x \<in> nat; y \<in> nat; z \<in> nat |]
   741       ==> arity(list_functor_fm(x,y,z)) = succ(x) \<union> succ(y) \<union> succ(z)"
   742 by (simp add: list_functor_fm_def succ_Un_distrib [symmetric] Un_ac)
   743 
   744 lemma sats_list_functor_fm [simp]:
   745    "[| x \<in> nat; y \<in> nat; z \<in> nat; env \<in> list(A)|]
   746     ==> sats(A, list_functor_fm(x,y,z), env) <->
   747         is_list_functor(**A, nth(x,env), nth(y,env), nth(z,env))"
   748 by (simp add: list_functor_fm_def is_list_functor_def)
   749 
   750 lemma list_functor_iff_sats:
   751   "[| nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
   752       i \<in> nat; j \<in> nat; k \<in> nat; env \<in> list(A)|]
   753    ==> is_list_functor(**A, x, y, z) <-> sats(A, list_functor_fm(i,j,k), env)"
   754 by simp
   755 
   756 theorem list_functor_reflection:
   757      "REFLECTS[\<lambda>x. is_list_functor(L,f(x),g(x),h(x)),
   758                \<lambda>i x. is_list_functor(**Lset(i),f(x),g(x),h(x))]"
   759 apply (simp only: is_list_functor_def setclass_simps)
   760 apply (intro FOL_reflections number1_reflection
   761              cartprod_reflection sum_reflection)
   762 done
   763 
   764 
   765 subsubsection{*Instances of Replacement for Lists*}
   766 
   767 lemma list_replacement1_Reflects:
   768  "REFLECTS
   769    [\<lambda>x. \<exists>u[L]. u \<in> B \<and> (\<exists>y[L]. pair(L,u,y,x) \<and>
   770          is_wfrec(L, iterates_MH(L, is_list_functor(L,A), 0), memsn, u, y)),
   771     \<lambda>i x. \<exists>u \<in> Lset(i). u \<in> B \<and> (\<exists>y \<in> Lset(i). pair(**Lset(i), u, y, x) \<and>
   772          is_wfrec(**Lset(i),
   773                   iterates_MH(**Lset(i),
   774                           is_list_functor(**Lset(i), A), 0), memsn, u, y))]"
   775 by (intro FOL_reflections function_reflections is_wfrec_reflection
   776           iterates_MH_reflection list_functor_reflection)
   777 
   778 lemma list_replacement1:
   779    "L(A) ==> iterates_replacement(L, is_list_functor(L,A), 0)"
   780 apply (unfold iterates_replacement_def wfrec_replacement_def, clarify)
   781 apply (rule strong_replacementI)
   782 apply (rule rallI)
   783 apply (rename_tac B)
   784 apply (rule separation_CollectI)
   785 apply (insert nonempty)
   786 apply (subgoal_tac "L(Memrel(succ(n)))")
   787 apply (rule_tac A="{B,A,n,z,0,Memrel(succ(n))}" in subset_LsetE, blast )
   788 apply (rule ReflectsE [OF list_replacement1_Reflects], assumption)
   789 apply (drule subset_Lset_ltD, assumption)
   790 apply (erule reflection_imp_L_separation)
   791   apply (simp_all add: lt_Ord2 Memrel_closed)
   792 apply (elim conjE)
   793 apply (rule DPow_LsetI)
   794 apply (rename_tac v)
   795 apply (rule bex_iff_sats conj_iff_sats)+
   796 apply (rule_tac env = "[u,v,A,n,B,0,Memrel(succ(n))]" in mem_iff_sats)
   797 apply (rule sep_rules | simp)+
   798 apply (simp add: is_wfrec_def)
   799 apply (rule sep_rules is_nat_case_iff_sats list_functor_iff_sats
   800             is_recfun_iff_sats iterates_MH_iff_sats quasinat_iff_sats | simp)+
   801 done
   802 
   803 lemma list_replacement2_Reflects:
   804  "REFLECTS
   805    [\<lambda>x. \<exists>u[L]. u \<in> B \<and> u \<in> nat \<and>
   806          (\<exists>sn[L]. \<exists>msn[L]. successor(L, u, sn) \<and> membership(L, sn, msn) \<and>
   807            is_wfrec (L, iterates_MH (L, is_list_functor(L, A), 0),
   808                               msn, u, x)),
   809     \<lambda>i x. \<exists>u \<in> Lset(i). u \<in> B \<and> u \<in> nat \<and>
   810          (\<exists>sn \<in> Lset(i). \<exists>msn \<in> Lset(i).
   811           successor(**Lset(i), u, sn) \<and> membership(**Lset(i), sn, msn) \<and>
   812            is_wfrec (**Lset(i),
   813                  iterates_MH (**Lset(i), is_list_functor(**Lset(i), A), 0),
   814                      msn, u, x))]"
   815 by (intro FOL_reflections function_reflections is_wfrec_reflection
   816           iterates_MH_reflection list_functor_reflection)
   817 
   818 
   819 lemma list_replacement2:
   820    "L(A) ==> strong_replacement(L,
   821          \<lambda>n y. n\<in>nat &
   822                (\<exists>sn[L]. \<exists>msn[L]. successor(L,n,sn) & membership(L,sn,msn) &
   823                is_wfrec(L, iterates_MH(L,is_list_functor(L,A), 0),
   824                         msn, n, y)))"
   825 apply (rule strong_replacementI)
   826 apply (rule rallI)
   827 apply (rename_tac B)
   828 apply (rule separation_CollectI)
   829 apply (insert nonempty)
   830 apply (rule_tac A="{A,B,z,0,nat}" in subset_LsetE)
   831 apply (blast intro: L_nat)
   832 apply (rule ReflectsE [OF list_replacement2_Reflects], assumption)
   833 apply (drule subset_Lset_ltD, assumption)
   834 apply (erule reflection_imp_L_separation)
   835   apply (simp_all add: lt_Ord2)
   836 apply (rule DPow_LsetI)
   837 apply (rename_tac v)
   838 apply (rule bex_iff_sats conj_iff_sats)+
   839 apply (rule_tac env = "[u,v,A,B,0,nat]" in mem_iff_sats)
   840 apply (rule sep_rules | simp)+
   841 apply (simp add: is_wfrec_def)
   842 apply (rule sep_rules is_nat_case_iff_sats list_functor_iff_sats
   843             is_recfun_iff_sats iterates_MH_iff_sats quasinat_iff_sats | simp)+
   844 done
   845 
   846 
   847 subsection{*@{term L} is Closed Under the Operator @{term formula}*}
   848 
   849 subsubsection{*The Formula Functor, Internalized*}
   850 
   851 constdefs formula_functor_fm :: "[i,i]=>i"
   852 (*     "is_formula_functor(M,X,Z) ==
   853         \<exists>nat'[M]. \<exists>natnat[M]. \<exists>natnatsum[M]. \<exists>XX[M]. \<exists>X3[M].
   854            4           3               2       1       0
   855           omega(M,nat') & cartprod(M,nat',nat',natnat) &
   856           is_sum(M,natnat,natnat,natnatsum) &
   857           cartprod(M,X,X,XX) & is_sum(M,XX,X,X3) &
   858           is_sum(M,natnatsum,X3,Z)" *)
   859     "formula_functor_fm(X,Z) ==
   860        Exists(Exists(Exists(Exists(Exists(
   861         And(omega_fm(4),
   862          And(cartprod_fm(4,4,3),
   863           And(sum_fm(3,3,2),
   864            And(cartprod_fm(X#+5,X#+5,1),
   865             And(sum_fm(1,X#+5,0), sum_fm(2,0,Z#+5)))))))))))"
   866 
   867 lemma formula_functor_type [TC]:
   868      "[| x \<in> nat; y \<in> nat |] ==> formula_functor_fm(x,y) \<in> formula"
   869 by (simp add: formula_functor_fm_def)
   870 
   871 lemma sats_formula_functor_fm [simp]:
   872    "[| x \<in> nat; y \<in> nat; env \<in> list(A)|]
   873     ==> sats(A, formula_functor_fm(x,y), env) <->
   874         is_formula_functor(**A, nth(x,env), nth(y,env))"
   875 by (simp add: formula_functor_fm_def is_formula_functor_def)
   876 
   877 lemma formula_functor_iff_sats:
   878   "[| nth(i,env) = x; nth(j,env) = y;
   879       i \<in> nat; j \<in> nat; env \<in> list(A)|]
   880    ==> is_formula_functor(**A, x, y) <-> sats(A, formula_functor_fm(i,j), env)"
   881 by simp
   882 
   883 theorem formula_functor_reflection:
   884      "REFLECTS[\<lambda>x. is_formula_functor(L,f(x),g(x)),
   885                \<lambda>i x. is_formula_functor(**Lset(i),f(x),g(x))]"
   886 apply (simp only: is_formula_functor_def setclass_simps)
   887 apply (intro FOL_reflections omega_reflection
   888              cartprod_reflection sum_reflection)
   889 done
   890 
   891 subsubsection{*Instances of Replacement for Formulas*}
   892 
   893 lemma formula_replacement1_Reflects:
   894  "REFLECTS
   895    [\<lambda>x. \<exists>u[L]. u \<in> B \<and> (\<exists>y[L]. pair(L,u,y,x) \<and>
   896          is_wfrec(L, iterates_MH(L, is_formula_functor(L), 0), memsn, u, y)),
   897     \<lambda>i x. \<exists>u \<in> Lset(i). u \<in> B \<and> (\<exists>y \<in> Lset(i). pair(**Lset(i), u, y, x) \<and>
   898          is_wfrec(**Lset(i),
   899                   iterates_MH(**Lset(i),
   900                           is_formula_functor(**Lset(i)), 0), memsn, u, y))]"
   901 by (intro FOL_reflections function_reflections is_wfrec_reflection
   902           iterates_MH_reflection formula_functor_reflection)
   903 
   904 lemma formula_replacement1:
   905    "iterates_replacement(L, is_formula_functor(L), 0)"
   906 apply (unfold iterates_replacement_def wfrec_replacement_def, clarify)
   907 apply (rule strong_replacementI)
   908 apply (rule rallI)
   909 apply (rename_tac B)
   910 apply (rule separation_CollectI)
   911 apply (insert nonempty)
   912 apply (subgoal_tac "L(Memrel(succ(n)))")
   913 apply (rule_tac A="{B,n,z,0,Memrel(succ(n))}" in subset_LsetE, blast )
   914 apply (rule ReflectsE [OF formula_replacement1_Reflects], assumption)
   915 apply (drule subset_Lset_ltD, assumption)
   916 apply (erule reflection_imp_L_separation)
   917   apply (simp_all add: lt_Ord2 Memrel_closed)
   918 apply (rule DPow_LsetI)
   919 apply (rename_tac v)
   920 apply (rule bex_iff_sats conj_iff_sats)+
   921 apply (rule_tac env = "[u,v,n,B,0,Memrel(succ(n))]" in mem_iff_sats)
   922 apply (rule sep_rules | simp)+
   923 apply (simp add: is_wfrec_def)
   924 apply (rule sep_rules is_nat_case_iff_sats formula_functor_iff_sats
   925             is_recfun_iff_sats iterates_MH_iff_sats quasinat_iff_sats | simp)+
   926 done
   927 
   928 lemma formula_replacement2_Reflects:
   929  "REFLECTS
   930    [\<lambda>x. \<exists>u[L]. u \<in> B \<and> u \<in> nat \<and>
   931          (\<exists>sn[L]. \<exists>msn[L]. successor(L, u, sn) \<and> membership(L, sn, msn) \<and>
   932            is_wfrec (L, iterates_MH (L, is_formula_functor(L), 0),
   933                               msn, u, x)),
   934     \<lambda>i x. \<exists>u \<in> Lset(i). u \<in> B \<and> u \<in> nat \<and>
   935          (\<exists>sn \<in> Lset(i). \<exists>msn \<in> Lset(i).
   936           successor(**Lset(i), u, sn) \<and> membership(**Lset(i), sn, msn) \<and>
   937            is_wfrec (**Lset(i),
   938                  iterates_MH (**Lset(i), is_formula_functor(**Lset(i)), 0),
   939                      msn, u, x))]"
   940 by (intro FOL_reflections function_reflections is_wfrec_reflection
   941           iterates_MH_reflection formula_functor_reflection)
   942 
   943 
   944 lemma formula_replacement2:
   945    "strong_replacement(L,
   946          \<lambda>n y. n\<in>nat &
   947                (\<exists>sn[L]. \<exists>msn[L]. successor(L,n,sn) & membership(L,sn,msn) &
   948                is_wfrec(L, iterates_MH(L,is_formula_functor(L), 0),
   949                         msn, n, y)))"
   950 apply (rule strong_replacementI)
   951 apply (rule rallI)
   952 apply (rename_tac B)
   953 apply (rule separation_CollectI)
   954 apply (insert nonempty)
   955 apply (rule_tac A="{B,z,0,nat}" in subset_LsetE)
   956 apply (blast intro: L_nat)
   957 apply (rule ReflectsE [OF formula_replacement2_Reflects], assumption)
   958 apply (drule subset_Lset_ltD, assumption)
   959 apply (erule reflection_imp_L_separation)
   960   apply (simp_all add: lt_Ord2)
   961 apply (rule DPow_LsetI)
   962 apply (rename_tac v)
   963 apply (rule bex_iff_sats conj_iff_sats)+
   964 apply (rule_tac env = "[u,v,B,0,nat]" in mem_iff_sats)
   965 apply (rule sep_rules | simp)+
   966 apply (simp add: is_wfrec_def)
   967 apply (rule sep_rules is_nat_case_iff_sats formula_functor_iff_sats
   968             is_recfun_iff_sats iterates_MH_iff_sats quasinat_iff_sats | simp)+
   969 done
   970 
   971 text{*NB The proofs for type @{term formula} are virtually identical to those
   972 for @{term "list(A)"}.  It was a cut-and-paste job! *}
   973 
   974 
   975 subsection{*Internalized Forms of Data Structuring Operators*}
   976 
   977 subsubsection{*The Formula @{term is_Inl}, Internalized*}
   978 
   979 (*  is_Inl(M,a,z) == \<exists>zero[M]. empty(M,zero) & pair(M,zero,a,z) *)
   980 constdefs Inl_fm :: "[i,i]=>i"
   981     "Inl_fm(a,z) == Exists(And(empty_fm(0), pair_fm(0,succ(a),succ(z))))"
   982 
   983 lemma Inl_type [TC]:
   984      "[| x \<in> nat; z \<in> nat |] ==> Inl_fm(x,z) \<in> formula"
   985 by (simp add: Inl_fm_def)
   986 
   987 lemma sats_Inl_fm [simp]:
   988    "[| x \<in> nat; z \<in> nat; env \<in> list(A)|]
   989     ==> sats(A, Inl_fm(x,z), env) <-> is_Inl(**A, nth(x,env), nth(z,env))"
   990 by (simp add: Inl_fm_def is_Inl_def)
   991 
   992 lemma Inl_iff_sats:
   993       "[| nth(i,env) = x; nth(k,env) = z;
   994           i \<in> nat; k \<in> nat; env \<in> list(A)|]
   995        ==> is_Inl(**A, x, z) <-> sats(A, Inl_fm(i,k), env)"
   996 by simp
   997 
   998 theorem Inl_reflection:
   999      "REFLECTS[\<lambda>x. is_Inl(L,f(x),h(x)),
  1000                \<lambda>i x. is_Inl(**Lset(i),f(x),h(x))]"
  1001 apply (simp only: is_Inl_def setclass_simps)
  1002 apply (intro FOL_reflections function_reflections)
  1003 done
  1004 
  1005 
  1006 subsubsection{*The Formula @{term is_Inr}, Internalized*}
  1007 
  1008 (*  is_Inr(M,a,z) == \<exists>n1[M]. number1(M,n1) & pair(M,n1,a,z) *)
  1009 constdefs Inr_fm :: "[i,i]=>i"
  1010     "Inr_fm(a,z) == Exists(And(number1_fm(0), pair_fm(0,succ(a),succ(z))))"
  1011 
  1012 lemma Inr_type [TC]:
  1013      "[| x \<in> nat; z \<in> nat |] ==> Inr_fm(x,z) \<in> formula"
  1014 by (simp add: Inr_fm_def)
  1015 
  1016 lemma sats_Inr_fm [simp]:
  1017    "[| x \<in> nat; z \<in> nat; env \<in> list(A)|]
  1018     ==> sats(A, Inr_fm(x,z), env) <-> is_Inr(**A, nth(x,env), nth(z,env))"
  1019 by (simp add: Inr_fm_def is_Inr_def)
  1020 
  1021 lemma Inr_iff_sats:
  1022       "[| nth(i,env) = x; nth(k,env) = z;
  1023           i \<in> nat; k \<in> nat; env \<in> list(A)|]
  1024        ==> is_Inr(**A, x, z) <-> sats(A, Inr_fm(i,k), env)"
  1025 by simp
  1026 
  1027 theorem Inr_reflection:
  1028      "REFLECTS[\<lambda>x. is_Inr(L,f(x),h(x)),
  1029                \<lambda>i x. is_Inr(**Lset(i),f(x),h(x))]"
  1030 apply (simp only: is_Inr_def setclass_simps)
  1031 apply (intro FOL_reflections function_reflections)
  1032 done
  1033 
  1034 
  1035 subsubsection{*The Formula @{term is_Nil}, Internalized*}
  1036 
  1037 (* is_Nil(M,xs) == \<exists>zero[M]. empty(M,zero) & is_Inl(M,zero,xs) *)
  1038 
  1039 constdefs Nil_fm :: "i=>i"
  1040     "Nil_fm(x) == Exists(And(empty_fm(0), Inl_fm(0,succ(x))))"
  1041 
  1042 lemma Nil_type [TC]: "x \<in> nat ==> Nil_fm(x) \<in> formula"
  1043 by (simp add: Nil_fm_def)
  1044 
  1045 lemma sats_Nil_fm [simp]:
  1046    "[| x \<in> nat; env \<in> list(A)|]
  1047     ==> sats(A, Nil_fm(x), env) <-> is_Nil(**A, nth(x,env))"
  1048 by (simp add: Nil_fm_def is_Nil_def)
  1049 
  1050 lemma Nil_iff_sats:
  1051       "[| nth(i,env) = x; i \<in> nat; env \<in> list(A)|]
  1052        ==> is_Nil(**A, x) <-> sats(A, Nil_fm(i), env)"
  1053 by simp
  1054 
  1055 theorem Nil_reflection:
  1056      "REFLECTS[\<lambda>x. is_Nil(L,f(x)),
  1057                \<lambda>i x. is_Nil(**Lset(i),f(x))]"
  1058 apply (simp only: is_Nil_def setclass_simps)
  1059 apply (intro FOL_reflections function_reflections Inl_reflection)
  1060 done
  1061 
  1062 
  1063 subsubsection{*The Formula @{term is_Cons}, Internalized*}
  1064 
  1065 
  1066 (*  "is_Cons(M,a,l,Z) == \<exists>p[M]. pair(M,a,l,p) & is_Inr(M,p,Z)" *)
  1067 constdefs Cons_fm :: "[i,i,i]=>i"
  1068     "Cons_fm(a,l,Z) ==
  1069        Exists(And(pair_fm(succ(a),succ(l),0), Inr_fm(0,succ(Z))))"
  1070 
  1071 lemma Cons_type [TC]:
  1072      "[| x \<in> nat; y \<in> nat; z \<in> nat |] ==> Cons_fm(x,y,z) \<in> formula"
  1073 by (simp add: Cons_fm_def)
  1074 
  1075 lemma sats_Cons_fm [simp]:
  1076    "[| x \<in> nat; y \<in> nat; z \<in> nat; env \<in> list(A)|]
  1077     ==> sats(A, Cons_fm(x,y,z), env) <->
  1078        is_Cons(**A, nth(x,env), nth(y,env), nth(z,env))"
  1079 by (simp add: Cons_fm_def is_Cons_def)
  1080 
  1081 lemma Cons_iff_sats:
  1082       "[| nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
  1083           i \<in> nat; j \<in> nat; k \<in> nat; env \<in> list(A)|]
  1084        ==>is_Cons(**A, x, y, z) <-> sats(A, Cons_fm(i,j,k), env)"
  1085 by simp
  1086 
  1087 theorem Cons_reflection:
  1088      "REFLECTS[\<lambda>x. is_Cons(L,f(x),g(x),h(x)),
  1089                \<lambda>i x. is_Cons(**Lset(i),f(x),g(x),h(x))]"
  1090 apply (simp only: is_Cons_def setclass_simps)
  1091 apply (intro FOL_reflections pair_reflection Inr_reflection)
  1092 done
  1093 
  1094 subsubsection{*The Formula @{term is_quasilist}, Internalized*}
  1095 
  1096 (* is_quasilist(M,xs) == is_Nil(M,z) | (\<exists>x[M]. \<exists>l[M]. is_Cons(M,x,l,z))" *)
  1097 
  1098 constdefs quasilist_fm :: "i=>i"
  1099     "quasilist_fm(x) ==
  1100        Or(Nil_fm(x), Exists(Exists(Cons_fm(1,0,succ(succ(x))))))"
  1101 
  1102 lemma quasilist_type [TC]: "x \<in> nat ==> quasilist_fm(x) \<in> formula"
  1103 by (simp add: quasilist_fm_def)
  1104 
  1105 lemma sats_quasilist_fm [simp]:
  1106    "[| x \<in> nat; env \<in> list(A)|]
  1107     ==> sats(A, quasilist_fm(x), env) <-> is_quasilist(**A, nth(x,env))"
  1108 by (simp add: quasilist_fm_def is_quasilist_def)
  1109 
  1110 lemma quasilist_iff_sats:
  1111       "[| nth(i,env) = x; i \<in> nat; env \<in> list(A)|]
  1112        ==> is_quasilist(**A, x) <-> sats(A, quasilist_fm(i), env)"
  1113 by simp
  1114 
  1115 theorem quasilist_reflection:
  1116      "REFLECTS[\<lambda>x. is_quasilist(L,f(x)),
  1117                \<lambda>i x. is_quasilist(**Lset(i),f(x))]"
  1118 apply (simp only: is_quasilist_def setclass_simps)
  1119 apply (intro FOL_reflections Nil_reflection Cons_reflection)
  1120 done
  1121 
  1122 
  1123 subsection{*Absoluteness for the Function @{term nth}*}
  1124 
  1125 
  1126 subsubsection{*The Formula @{term is_tl}, Internalized*}
  1127 
  1128 (*     "is_tl(M,xs,T) ==
  1129        (is_Nil(M,xs) --> T=xs) &
  1130        (\<forall>x[M]. \<forall>l[M]. ~ is_Cons(M,x,l,xs) | T=l) &
  1131        (is_quasilist(M,xs) | empty(M,T))" *)
  1132 constdefs tl_fm :: "[i,i]=>i"
  1133     "tl_fm(xs,T) ==
  1134        And(Implies(Nil_fm(xs), Equal(T,xs)),
  1135            And(Forall(Forall(Or(Neg(Cons_fm(1,0,xs#+2)), Equal(T#+2,0)))),
  1136                Or(quasilist_fm(xs), empty_fm(T))))"
  1137 
  1138 lemma tl_type [TC]:
  1139      "[| x \<in> nat; y \<in> nat |] ==> tl_fm(x,y) \<in> formula"
  1140 by (simp add: tl_fm_def)
  1141 
  1142 lemma sats_tl_fm [simp]:
  1143    "[| x \<in> nat; y \<in> nat; env \<in> list(A)|]
  1144     ==> sats(A, tl_fm(x,y), env) <-> is_tl(**A, nth(x,env), nth(y,env))"
  1145 by (simp add: tl_fm_def is_tl_def)
  1146 
  1147 lemma tl_iff_sats:
  1148       "[| nth(i,env) = x; nth(j,env) = y;
  1149           i \<in> nat; j \<in> nat; env \<in> list(A)|]
  1150        ==> is_tl(**A, x, y) <-> sats(A, tl_fm(i,j), env)"
  1151 by simp
  1152 
  1153 theorem tl_reflection:
  1154      "REFLECTS[\<lambda>x. is_tl(L,f(x),g(x)),
  1155                \<lambda>i x. is_tl(**Lset(i),f(x),g(x))]"
  1156 apply (simp only: is_tl_def setclass_simps)
  1157 apply (intro FOL_reflections Nil_reflection Cons_reflection
  1158              quasilist_reflection empty_reflection)
  1159 done
  1160 
  1161 
  1162 subsubsection{*An Instance of Replacement for @{term nth}*}
  1163 
  1164 lemma nth_replacement_Reflects:
  1165  "REFLECTS
  1166    [\<lambda>x. \<exists>u[L]. u \<in> B \<and> (\<exists>y[L]. pair(L,u,y,x) \<and>
  1167          is_wfrec(L, iterates_MH(L, is_tl(L), z), memsn, u, y)),
  1168     \<lambda>i x. \<exists>u \<in> Lset(i). u \<in> B \<and> (\<exists>y \<in> Lset(i). pair(**Lset(i), u, y, x) \<and>
  1169          is_wfrec(**Lset(i),
  1170                   iterates_MH(**Lset(i),
  1171                           is_tl(**Lset(i)), z), memsn, u, y))]"
  1172 by (intro FOL_reflections function_reflections is_wfrec_reflection
  1173           iterates_MH_reflection list_functor_reflection tl_reflection)
  1174 
  1175 lemma nth_replacement:
  1176    "L(w) ==> iterates_replacement(L, %l t. is_tl(L,l,t), w)"
  1177 apply (unfold iterates_replacement_def wfrec_replacement_def, clarify)
  1178 apply (rule strong_replacementI)
  1179 apply (rule rallI)
  1180 apply (rule separation_CollectI)
  1181 apply (subgoal_tac "L(Memrel(succ(n)))")
  1182 apply (rule_tac A="{A,n,w,z,Memrel(succ(n))}" in subset_LsetE, blast )
  1183 apply (rule ReflectsE [OF nth_replacement_Reflects], assumption)
  1184 apply (drule subset_Lset_ltD, assumption)
  1185 apply (erule reflection_imp_L_separation)
  1186   apply (simp_all add: lt_Ord2 Memrel_closed)
  1187 apply (elim conjE)
  1188 apply (rule DPow_LsetI)
  1189 apply (rename_tac v)
  1190 apply (rule bex_iff_sats conj_iff_sats)+
  1191 apply (rule_tac env = "[u,v,A,z,w,Memrel(succ(n))]" in mem_iff_sats)
  1192 apply (rule sep_rules | simp)+
  1193 apply (simp add: is_wfrec_def)
  1194 apply (rule sep_rules is_nat_case_iff_sats tl_iff_sats
  1195             is_recfun_iff_sats iterates_MH_iff_sats quasinat_iff_sats | simp)+
  1196 done
  1197 
  1198 
  1199 
  1200 subsubsection{*Instantiating the locale @{text M_datatypes}*}
  1201 
  1202 theorem M_datatypes_L: "PROP M_datatypes(L)"
  1203   apply (rule M_datatypes.intro)
  1204       apply (rule M_wfrank.axioms [OF M_wfrank_L])+
  1205   apply (rule M_datatypes_axioms.intro)
  1206       apply (assumption | rule
  1207         list_replacement1 list_replacement2
  1208         formula_replacement1 formula_replacement2
  1209         nth_replacement)+
  1210   done
  1211 
  1212 lemmas list_closed = M_datatypes.list_closed [OF M_datatypes_L]
  1213   and formula_closed = M_datatypes.formula_closed [OF M_datatypes_L]
  1214   and list_abs = M_datatypes.list_abs [OF M_datatypes_L]
  1215   and formula_abs = M_datatypes.formula_abs [OF M_datatypes_L]
  1216   and nth_abs = M_datatypes.nth_abs [OF M_datatypes_L]
  1217 
  1218 declare list_closed [intro,simp]
  1219 declare formula_closed [intro,simp]
  1220 declare list_abs [simp]
  1221 declare formula_abs [simp]
  1222 declare nth_abs [simp]
  1223 
  1224 
  1225 subsection{*@{term L} is Closed Under the Operator @{term eclose}*}
  1226 
  1227 subsubsection{*Instances of Replacement for @{term eclose}*}
  1228 
  1229 lemma eclose_replacement1_Reflects:
  1230  "REFLECTS
  1231    [\<lambda>x. \<exists>u[L]. u \<in> B \<and> (\<exists>y[L]. pair(L,u,y,x) \<and>
  1232          is_wfrec(L, iterates_MH(L, big_union(L), A), memsn, u, y)),
  1233     \<lambda>i x. \<exists>u \<in> Lset(i). u \<in> B \<and> (\<exists>y \<in> Lset(i). pair(**Lset(i), u, y, x) \<and>
  1234          is_wfrec(**Lset(i),
  1235                   iterates_MH(**Lset(i), big_union(**Lset(i)), A),
  1236                   memsn, u, y))]"
  1237 by (intro FOL_reflections function_reflections is_wfrec_reflection
  1238           iterates_MH_reflection)
  1239 
  1240 lemma eclose_replacement1:
  1241    "L(A) ==> iterates_replacement(L, big_union(L), A)"
  1242 apply (unfold iterates_replacement_def wfrec_replacement_def, clarify)
  1243 apply (rule strong_replacementI)
  1244 apply (rule rallI)
  1245 apply (rename_tac B)
  1246 apply (rule separation_CollectI)
  1247 apply (subgoal_tac "L(Memrel(succ(n)))")
  1248 apply (rule_tac A="{B,A,n,z,Memrel(succ(n))}" in subset_LsetE, blast )
  1249 apply (rule ReflectsE [OF eclose_replacement1_Reflects], assumption)
  1250 apply (drule subset_Lset_ltD, assumption)
  1251 apply (erule reflection_imp_L_separation)
  1252   apply (simp_all add: lt_Ord2 Memrel_closed)
  1253 apply (elim conjE)
  1254 apply (rule DPow_LsetI)
  1255 apply (rename_tac v)
  1256 apply (rule bex_iff_sats conj_iff_sats)+
  1257 apply (rule_tac env = "[u,v,A,n,B,Memrel(succ(n))]" in mem_iff_sats)
  1258 apply (rule sep_rules | simp)+
  1259 apply (simp add: is_wfrec_def)
  1260 apply (rule sep_rules iterates_MH_iff_sats is_nat_case_iff_sats
  1261              is_recfun_iff_sats big_union_iff_sats quasinat_iff_sats | simp)+
  1262 done
  1263 
  1264 
  1265 lemma eclose_replacement2_Reflects:
  1266  "REFLECTS
  1267    [\<lambda>x. \<exists>u[L]. u \<in> B \<and> u \<in> nat \<and>
  1268          (\<exists>sn[L]. \<exists>msn[L]. successor(L, u, sn) \<and> membership(L, sn, msn) \<and>
  1269            is_wfrec (L, iterates_MH (L, big_union(L), A),
  1270                               msn, u, x)),
  1271     \<lambda>i x. \<exists>u \<in> Lset(i). u \<in> B \<and> u \<in> nat \<and>
  1272          (\<exists>sn \<in> Lset(i). \<exists>msn \<in> Lset(i).
  1273           successor(**Lset(i), u, sn) \<and> membership(**Lset(i), sn, msn) \<and>
  1274            is_wfrec (**Lset(i),
  1275                  iterates_MH (**Lset(i), big_union(**Lset(i)), A),
  1276                      msn, u, x))]"
  1277 by (intro FOL_reflections function_reflections is_wfrec_reflection
  1278           iterates_MH_reflection)
  1279 
  1280 
  1281 lemma eclose_replacement2:
  1282    "L(A) ==> strong_replacement(L,
  1283          \<lambda>n y. n\<in>nat &
  1284                (\<exists>sn[L]. \<exists>msn[L]. successor(L,n,sn) & membership(L,sn,msn) &
  1285                is_wfrec(L, iterates_MH(L,big_union(L), A),
  1286                         msn, n, y)))"
  1287 apply (rule strong_replacementI)
  1288 apply (rule rallI)
  1289 apply (rename_tac B)
  1290 apply (rule separation_CollectI)
  1291 apply (rule_tac A="{A,B,z,nat}" in subset_LsetE)
  1292 apply (blast intro: L_nat)
  1293 apply (rule ReflectsE [OF eclose_replacement2_Reflects], assumption)
  1294 apply (drule subset_Lset_ltD, assumption)
  1295 apply (erule reflection_imp_L_separation)
  1296   apply (simp_all add: lt_Ord2)
  1297 apply (rule DPow_LsetI)
  1298 apply (rename_tac v)
  1299 apply (rule bex_iff_sats conj_iff_sats)+
  1300 apply (rule_tac env = "[u,v,A,B,nat]" in mem_iff_sats)
  1301 apply (rule sep_rules | simp)+
  1302 apply (simp add: is_wfrec_def)
  1303 apply (rule sep_rules is_nat_case_iff_sats iterates_MH_iff_sats
  1304               is_recfun_iff_sats big_union_iff_sats quasinat_iff_sats | simp)+
  1305 done
  1306 
  1307 
  1308 subsubsection{*Instantiating the locale @{text M_eclose}*}
  1309 
  1310 theorem M_eclose_L: "PROP M_eclose(L)"
  1311   apply (rule M_eclose.intro)
  1312        apply (rule M_datatypes.axioms [OF M_datatypes_L])+
  1313   apply (rule M_eclose_axioms.intro)
  1314    apply (assumption | rule eclose_replacement1 eclose_replacement2)+
  1315   done
  1316 
  1317 lemmas eclose_closed [intro, simp] = M_eclose.eclose_closed [OF M_eclose_L]
  1318   and eclose_abs [intro, simp] = M_eclose.eclose_abs [OF M_eclose_L]
  1319 
  1320 end