src/ZF/Constructible/Rec_Separation.thy
author wenzelm
Mon Jul 29 18:07:53 2002 +0200 (2002-07-29)
changeset 13429 2232810416fc
parent 13428 99e52e78eb65
child 13434 78b93a667c01
permissions -rw-r--r--
tuned;
     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 constdefs is_recfun_fm :: "[[i,i,i]=>i, i, i, i]=>i"
   243  "is_recfun_fm(p,r,a,f) ==
   244    Forall(Iff(Member(0,succ(f)),
   245     Exists(Exists(Exists(Exists(Exists(Exists(
   246      And(pair_fm(5,4,6),
   247       And(pair_fm(5,a#+7,3),
   248        And(upair_fm(5,5,2),
   249         And(pre_image_fm(r#+7,2,1),
   250          And(restriction_fm(f#+7,1,0),
   251           And(Member(3,r#+7), p(5,0,4)))))))))))))))"
   252 
   253 
   254 lemma is_recfun_type_0:
   255      "[| !!x y z. [| x \<in> nat; y \<in> nat; z \<in> nat |] ==> p(x,y,z) \<in> formula;
   256          x \<in> nat; y \<in> nat; z \<in> nat |]
   257       ==> is_recfun_fm(p,x,y,z) \<in> formula"
   258 apply (unfold is_recfun_fm_def)
   259 (*FIXME: FIND OUT why simp loops!*)
   260 apply typecheck
   261 by simp
   262 
   263 lemma is_recfun_type [TC]:
   264      "[| p(5,0,4) \<in> formula;
   265          x \<in> nat; y \<in> nat; z \<in> nat |]
   266       ==> is_recfun_fm(p,x,y,z) \<in> formula"
   267 by (simp add: is_recfun_fm_def)
   268 
   269 lemma arity_is_recfun_fm [simp]:
   270      "[| arity(p(5,0,4)) le 8; x \<in> nat; y \<in> nat; z \<in> nat |]
   271       ==> arity(is_recfun_fm(p,x,y,z)) = succ(x) \<union> succ(y) \<union> succ(z)"
   272 apply (frule lt_nat_in_nat, simp)
   273 apply (simp add: is_recfun_fm_def succ_Un_distrib [symmetric] )
   274 apply (subst subset_Un_iff2 [of "arity(p(5,0,4))", THEN iffD1])
   275 apply (rule le_imp_subset)
   276 apply (erule le_trans, simp)
   277 apply (simp add: succ_Un_distrib [symmetric] Un_ac)
   278 done
   279 
   280 lemma sats_is_recfun_fm:
   281   assumes MH_iff_sats:
   282       "!!x y z env.
   283          [| x \<in> nat; y \<in> nat; z \<in> nat; env \<in> list(A)|]
   284          ==> MH(nth(x,env), nth(y,env), nth(z,env)) <-> sats(A, p(x,y,z), env)"
   285   shows
   286       "[|x \<in> nat; y \<in> nat; z \<in> nat; env \<in> list(A)|]
   287        ==> sats(A, is_recfun_fm(p,x,y,z), env) <->
   288            M_is_recfun(**A, MH, nth(x,env), nth(y,env), nth(z,env))"
   289 by (simp add: is_recfun_fm_def M_is_recfun_def MH_iff_sats [THEN iff_sym])
   290 
   291 lemma is_recfun_iff_sats:
   292   "[| (!!x y z env. [| x \<in> nat; y \<in> nat; z \<in> nat; env \<in> list(A)|]
   293                     ==> MH(nth(x,env), nth(y,env), nth(z,env)) <->
   294                         sats(A, p(x,y,z), env));
   295       nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
   296       i \<in> nat; j \<in> nat; k \<in> nat; env \<in> list(A)|]
   297    ==> M_is_recfun(**A, MH, x, y, z) <-> sats(A, is_recfun_fm(p,i,j,k), env)"
   298 by (simp add: sats_is_recfun_fm [of A MH])
   299 
   300 theorem is_recfun_reflection:
   301   assumes MH_reflection:
   302     "!!f g h. REFLECTS[\<lambda>x. MH(L, f(x), g(x), h(x)),
   303                      \<lambda>i x. MH(**Lset(i), f(x), g(x), h(x))]"
   304   shows "REFLECTS[\<lambda>x. M_is_recfun(L, MH(L), f(x), g(x), h(x)),
   305                \<lambda>i x. M_is_recfun(**Lset(i), MH(**Lset(i)), f(x), g(x), h(x))]"
   306 apply (simp (no_asm_use) only: M_is_recfun_def setclass_simps)
   307 apply (intro FOL_reflections function_reflections
   308              restriction_reflection MH_reflection)
   309 done
   310 
   311 text{*Currently, @{text sats}-theorems for higher-order operators don't seem
   312 useful.  Reflection theorems do work, though.  This one avoids the repetition
   313 of the @{text MH}-term.*}
   314 theorem is_wfrec_reflection:
   315   assumes MH_reflection:
   316     "!!f g h. REFLECTS[\<lambda>x. MH(L, f(x), g(x), h(x)),
   317                      \<lambda>i x. MH(**Lset(i), f(x), g(x), h(x))]"
   318   shows "REFLECTS[\<lambda>x. is_wfrec(L, MH(L), f(x), g(x), h(x)),
   319                \<lambda>i x. is_wfrec(**Lset(i), MH(**Lset(i)), f(x), g(x), h(x))]"
   320 apply (simp (no_asm_use) only: is_wfrec_def setclass_simps)
   321 apply (intro FOL_reflections MH_reflection is_recfun_reflection)
   322 done
   323 
   324 subsection{*The Locale @{text "M_wfrank"}*}
   325 
   326 subsubsection{*Separation for @{term "wfrank"}*}
   327 
   328 lemma wfrank_Reflects:
   329  "REFLECTS[\<lambda>x. \<forall>rplus[L]. tran_closure(L,r,rplus) -->
   330               ~ (\<exists>f[L]. M_is_recfun(L, %x f y. is_range(L,f,y), rplus, x, f)),
   331       \<lambda>i x. \<forall>rplus \<in> Lset(i). tran_closure(**Lset(i),r,rplus) -->
   332          ~ (\<exists>f \<in> Lset(i).
   333             M_is_recfun(**Lset(i), %x f y. is_range(**Lset(i),f,y),
   334                         rplus, x, f))]"
   335 by (intro FOL_reflections function_reflections is_recfun_reflection tran_closure_reflection)
   336 
   337 lemma wfrank_separation:
   338      "L(r) ==>
   339       separation (L, \<lambda>x. \<forall>rplus[L]. tran_closure(L,r,rplus) -->
   340          ~ (\<exists>f[L]. M_is_recfun(L, %x f y. is_range(L,f,y), rplus, x, f)))"
   341 apply (rule separation_CollectI)
   342 apply (rule_tac A="{r,z}" in subset_LsetE, blast )
   343 apply (rule ReflectsE [OF wfrank_Reflects], assumption)
   344 apply (drule subset_Lset_ltD, assumption)
   345 apply (erule reflection_imp_L_separation)
   346   apply (simp_all add: lt_Ord2, clarify)
   347 apply (rule DPow_LsetI)
   348 apply (rename_tac u)
   349 apply (rule ball_iff_sats imp_iff_sats)+
   350 apply (rule_tac env="[rplus,u,r]" in tran_closure_iff_sats)
   351 apply (rule sep_rules is_recfun_iff_sats | simp)+
   352 done
   353 
   354 
   355 subsubsection{*Replacement for @{term "wfrank"}*}
   356 
   357 lemma wfrank_replacement_Reflects:
   358  "REFLECTS[\<lambda>z. \<exists>x[L]. x \<in> A &
   359         (\<forall>rplus[L]. tran_closure(L,r,rplus) -->
   360          (\<exists>y[L]. \<exists>f[L]. pair(L,x,y,z)  &
   361                         M_is_recfun(L, %x f y. is_range(L,f,y), rplus, x, f) &
   362                         is_range(L,f,y))),
   363  \<lambda>i z. \<exists>x \<in> Lset(i). x \<in> A &
   364       (\<forall>rplus \<in> Lset(i). tran_closure(**Lset(i),r,rplus) -->
   365        (\<exists>y \<in> Lset(i). \<exists>f \<in> Lset(i). pair(**Lset(i),x,y,z)  &
   366          M_is_recfun(**Lset(i), %x f y. is_range(**Lset(i),f,y), rplus, x, f) &
   367          is_range(**Lset(i),f,y)))]"
   368 by (intro FOL_reflections function_reflections fun_plus_reflections
   369              is_recfun_reflection tran_closure_reflection)
   370 
   371 
   372 lemma wfrank_strong_replacement:
   373      "L(r) ==>
   374       strong_replacement(L, \<lambda>x z.
   375          \<forall>rplus[L]. tran_closure(L,r,rplus) -->
   376          (\<exists>y[L]. \<exists>f[L]. pair(L,x,y,z)  &
   377                         M_is_recfun(L, %x f y. is_range(L,f,y), rplus, x, f) &
   378                         is_range(L,f,y)))"
   379 apply (rule strong_replacementI)
   380 apply (rule rallI)
   381 apply (rename_tac B)
   382 apply (rule separation_CollectI)
   383 apply (rule_tac A="{B,r,z}" in subset_LsetE, blast )
   384 apply (rule ReflectsE [OF wfrank_replacement_Reflects], assumption)
   385 apply (drule subset_Lset_ltD, assumption)
   386 apply (erule reflection_imp_L_separation)
   387   apply (simp_all add: lt_Ord2)
   388 apply (rule DPow_LsetI)
   389 apply (rename_tac u)
   390 apply (rule bex_iff_sats ball_iff_sats conj_iff_sats)+
   391 apply (rule_tac env = "[x,u,B,r]" in mem_iff_sats)
   392 apply (rule sep_rules tran_closure_iff_sats is_recfun_iff_sats | simp)+
   393 done
   394 
   395 
   396 subsubsection{*Separation for Proving @{text Ord_wfrank_range}*}
   397 
   398 lemma Ord_wfrank_Reflects:
   399  "REFLECTS[\<lambda>x. \<forall>rplus[L]. tran_closure(L,r,rplus) -->
   400           ~ (\<forall>f[L]. \<forall>rangef[L].
   401              is_range(L,f,rangef) -->
   402              M_is_recfun(L, \<lambda>x f y. is_range(L,f,y), rplus, x, f) -->
   403              ordinal(L,rangef)),
   404       \<lambda>i x. \<forall>rplus \<in> Lset(i). tran_closure(**Lset(i),r,rplus) -->
   405           ~ (\<forall>f \<in> Lset(i). \<forall>rangef \<in> Lset(i).
   406              is_range(**Lset(i),f,rangef) -->
   407              M_is_recfun(**Lset(i), \<lambda>x f y. is_range(**Lset(i),f,y),
   408                          rplus, x, f) -->
   409              ordinal(**Lset(i),rangef))]"
   410 by (intro FOL_reflections function_reflections is_recfun_reflection
   411           tran_closure_reflection ordinal_reflection)
   412 
   413 lemma  Ord_wfrank_separation:
   414      "L(r) ==>
   415       separation (L, \<lambda>x.
   416          \<forall>rplus[L]. tran_closure(L,r,rplus) -->
   417           ~ (\<forall>f[L]. \<forall>rangef[L].
   418              is_range(L,f,rangef) -->
   419              M_is_recfun(L, \<lambda>x f y. is_range(L,f,y), rplus, x, f) -->
   420              ordinal(L,rangef)))"
   421 apply (rule separation_CollectI)
   422 apply (rule_tac A="{r,z}" in subset_LsetE, blast )
   423 apply (rule ReflectsE [OF Ord_wfrank_Reflects], assumption)
   424 apply (drule subset_Lset_ltD, assumption)
   425 apply (erule reflection_imp_L_separation)
   426   apply (simp_all add: lt_Ord2, clarify)
   427 apply (rule DPow_LsetI)
   428 apply (rename_tac u)
   429 apply (rule ball_iff_sats imp_iff_sats)+
   430 apply (rule_tac env="[rplus,u,r]" in tran_closure_iff_sats)
   431 apply (rule sep_rules is_recfun_iff_sats | simp)+
   432 done
   433 
   434 
   435 subsubsection{*Instantiating the locale @{text M_wfrank}*}
   436 
   437 theorem M_wfrank_L: "PROP M_wfrank(L)"
   438   apply (rule M_wfrank.intro)
   439      apply (rule M_trancl.axioms [OF M_trancl_L])+
   440   apply (rule M_wfrank_axioms.intro)
   441    apply (assumption | rule
   442      wfrank_separation wfrank_strong_replacement Ord_wfrank_separation)+
   443   done
   444 
   445 lemmas iterates_closed = M_wfrank.iterates_closed [OF M_wfrank_L]
   446   and exists_wfrank = M_wfrank.exists_wfrank [OF M_wfrank_L]
   447   and M_wellfoundedrank = M_wfrank.M_wellfoundedrank [OF M_wfrank_L]
   448   and Ord_wfrank_range = M_wfrank.Ord_wfrank_range [OF M_wfrank_L]
   449   and Ord_range_wellfoundedrank = M_wfrank.Ord_range_wellfoundedrank [OF M_wfrank_L]
   450   and function_wellfoundedrank = M_wfrank.function_wellfoundedrank [OF M_wfrank_L]
   451   and domain_wellfoundedrank = M_wfrank.domain_wellfoundedrank [OF M_wfrank_L]
   452   and wellfoundedrank_type = M_wfrank.wellfoundedrank_type [OF M_wfrank_L]
   453   and Ord_wellfoundedrank = M_wfrank.Ord_wellfoundedrank [OF M_wfrank_L]
   454   and wellfoundedrank_eq = M_wfrank.wellfoundedrank_eq [OF M_wfrank_L]
   455   and wellfoundedrank_lt = M_wfrank.wellfoundedrank_lt [OF M_wfrank_L]
   456   and wellfounded_imp_subset_rvimage = M_wfrank.wellfounded_imp_subset_rvimage [OF M_wfrank_L]
   457   and wellfounded_imp_wf = M_wfrank.wellfounded_imp_wf [OF M_wfrank_L]
   458   and wellfounded_on_imp_wf_on = M_wfrank.wellfounded_on_imp_wf_on [OF M_wfrank_L]
   459   and wf_abs = M_wfrank.wf_abs [OF M_wfrank_L]
   460   and wf_on_abs = M_wfrank.wf_on_abs [OF M_wfrank_L]
   461   and wfrec_replacement_iff = M_wfrank.wfrec_replacement_iff [OF M_wfrank_L]
   462   and trans_wfrec_closed = M_wfrank.trans_wfrec_closed [OF M_wfrank_L]
   463   and wfrec_closed = M_wfrank.wfrec_closed [OF M_wfrank_L]
   464 
   465 declare iterates_closed [intro,simp]
   466 declare Ord_wfrank_range [rule_format]
   467 declare wf_abs [simp]
   468 declare wf_on_abs [simp]
   469 
   470 
   471 subsection{*For Datatypes*}
   472 
   473 subsubsection{*Binary Products, Internalized*}
   474 
   475 constdefs cartprod_fm :: "[i,i,i]=>i"
   476 (* "cartprod(M,A,B,z) ==
   477         \<forall>u[M]. u \<in> z <-> (\<exists>x[M]. x\<in>A & (\<exists>y[M]. y\<in>B & pair(M,x,y,u)))" *)
   478     "cartprod_fm(A,B,z) ==
   479        Forall(Iff(Member(0,succ(z)),
   480                   Exists(And(Member(0,succ(succ(A))),
   481                          Exists(And(Member(0,succ(succ(succ(B)))),
   482                                     pair_fm(1,0,2)))))))"
   483 
   484 lemma cartprod_type [TC]:
   485      "[| x \<in> nat; y \<in> nat; z \<in> nat |] ==> cartprod_fm(x,y,z) \<in> formula"
   486 by (simp add: cartprod_fm_def)
   487 
   488 lemma arity_cartprod_fm [simp]:
   489      "[| x \<in> nat; y \<in> nat; z \<in> nat |]
   490       ==> arity(cartprod_fm(x,y,z)) = succ(x) \<union> succ(y) \<union> succ(z)"
   491 by (simp add: cartprod_fm_def succ_Un_distrib [symmetric] Un_ac)
   492 
   493 lemma sats_cartprod_fm [simp]:
   494    "[| x \<in> nat; y \<in> nat; z \<in> nat; env \<in> list(A)|]
   495     ==> sats(A, cartprod_fm(x,y,z), env) <->
   496         cartprod(**A, nth(x,env), nth(y,env), nth(z,env))"
   497 by (simp add: cartprod_fm_def cartprod_def)
   498 
   499 lemma cartprod_iff_sats:
   500       "[| nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
   501           i \<in> nat; j \<in> nat; k \<in> nat; env \<in> list(A)|]
   502        ==> cartprod(**A, x, y, z) <-> sats(A, cartprod_fm(i,j,k), env)"
   503 by (simp add: sats_cartprod_fm)
   504 
   505 theorem cartprod_reflection:
   506      "REFLECTS[\<lambda>x. cartprod(L,f(x),g(x),h(x)),
   507                \<lambda>i x. cartprod(**Lset(i),f(x),g(x),h(x))]"
   508 apply (simp only: cartprod_def setclass_simps)
   509 apply (intro FOL_reflections pair_reflection)
   510 done
   511 
   512 
   513 subsubsection{*Binary Sums, Internalized*}
   514 
   515 (* "is_sum(M,A,B,Z) ==
   516        \<exists>A0[M]. \<exists>n1[M]. \<exists>s1[M]. \<exists>B1[M].
   517          3      2       1        0
   518        number1(M,n1) & cartprod(M,n1,A,A0) & upair(M,n1,n1,s1) &
   519        cartprod(M,s1,B,B1) & union(M,A0,B1,Z)"  *)
   520 constdefs sum_fm :: "[i,i,i]=>i"
   521     "sum_fm(A,B,Z) ==
   522        Exists(Exists(Exists(Exists(
   523         And(number1_fm(2),
   524             And(cartprod_fm(2,A#+4,3),
   525                 And(upair_fm(2,2,1),
   526                     And(cartprod_fm(1,B#+4,0), union_fm(3,0,Z#+4)))))))))"
   527 
   528 lemma sum_type [TC]:
   529      "[| x \<in> nat; y \<in> nat; z \<in> nat |] ==> sum_fm(x,y,z) \<in> formula"
   530 by (simp add: sum_fm_def)
   531 
   532 lemma arity_sum_fm [simp]:
   533      "[| x \<in> nat; y \<in> nat; z \<in> nat |]
   534       ==> arity(sum_fm(x,y,z)) = succ(x) \<union> succ(y) \<union> succ(z)"
   535 by (simp add: sum_fm_def succ_Un_distrib [symmetric] Un_ac)
   536 
   537 lemma sats_sum_fm [simp]:
   538    "[| x \<in> nat; y \<in> nat; z \<in> nat; env \<in> list(A)|]
   539     ==> sats(A, sum_fm(x,y,z), env) <->
   540         is_sum(**A, nth(x,env), nth(y,env), nth(z,env))"
   541 by (simp add: sum_fm_def is_sum_def)
   542 
   543 lemma sum_iff_sats:
   544       "[| nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
   545           i \<in> nat; j \<in> nat; k \<in> nat; env \<in> list(A)|]
   546        ==> is_sum(**A, x, y, z) <-> sats(A, sum_fm(i,j,k), env)"
   547 by simp
   548 
   549 theorem sum_reflection:
   550      "REFLECTS[\<lambda>x. is_sum(L,f(x),g(x),h(x)),
   551                \<lambda>i x. is_sum(**Lset(i),f(x),g(x),h(x))]"
   552 apply (simp only: is_sum_def setclass_simps)
   553 apply (intro FOL_reflections function_reflections cartprod_reflection)
   554 done
   555 
   556 
   557 subsubsection{*The Operator @{term quasinat}*}
   558 
   559 (* "is_quasinat(M,z) == empty(M,z) | (\<exists>m[M]. successor(M,m,z))" *)
   560 constdefs quasinat_fm :: "i=>i"
   561     "quasinat_fm(z) == Or(empty_fm(z), Exists(succ_fm(0,succ(z))))"
   562 
   563 lemma quasinat_type [TC]:
   564      "x \<in> nat ==> quasinat_fm(x) \<in> formula"
   565 by (simp add: quasinat_fm_def)
   566 
   567 lemma arity_quasinat_fm [simp]:
   568      "x \<in> nat ==> arity(quasinat_fm(x)) = succ(x)"
   569 by (simp add: quasinat_fm_def succ_Un_distrib [symmetric] Un_ac)
   570 
   571 lemma sats_quasinat_fm [simp]:
   572    "[| x \<in> nat; env \<in> list(A)|]
   573     ==> sats(A, quasinat_fm(x), env) <-> is_quasinat(**A, nth(x,env))"
   574 by (simp add: quasinat_fm_def is_quasinat_def)
   575 
   576 lemma quasinat_iff_sats:
   577       "[| nth(i,env) = x; nth(j,env) = y;
   578           i \<in> nat; env \<in> list(A)|]
   579        ==> is_quasinat(**A, x) <-> sats(A, quasinat_fm(i), env)"
   580 by simp
   581 
   582 theorem quasinat_reflection:
   583      "REFLECTS[\<lambda>x. is_quasinat(L,f(x)),
   584                \<lambda>i x. is_quasinat(**Lset(i),f(x))]"
   585 apply (simp only: is_quasinat_def setclass_simps)
   586 apply (intro FOL_reflections function_reflections)
   587 done
   588 
   589 
   590 subsubsection{*The Operator @{term is_nat_case}*}
   591 
   592 (* is_nat_case :: "[i=>o, i, [i,i]=>o, i, i] => o"
   593     "is_nat_case(M, a, is_b, k, z) ==
   594        (empty(M,k) --> z=a) &
   595        (\<forall>m[M]. successor(M,m,k) --> is_b(m,z)) &
   596        (is_quasinat(M,k) | empty(M,z))" *)
   597 text{*The formula @{term is_b} has free variables 1 and 0.*}
   598 constdefs is_nat_case_fm :: "[i, [i,i]=>i, i, i]=>i"
   599  "is_nat_case_fm(a,is_b,k,z) ==
   600     And(Implies(empty_fm(k), Equal(z,a)),
   601         And(Forall(Implies(succ_fm(0,succ(k)),
   602                    Forall(Implies(Equal(0,succ(succ(z))), is_b(1,0))))),
   603             Or(quasinat_fm(k), empty_fm(z))))"
   604 
   605 lemma is_nat_case_type [TC]:
   606      "[| is_b(1,0) \<in> formula;
   607          x \<in> nat; y \<in> nat; z \<in> nat |]
   608       ==> is_nat_case_fm(x,is_b,y,z) \<in> formula"
   609 by (simp add: is_nat_case_fm_def)
   610 
   611 lemma arity_is_nat_case_fm [simp]:
   612      "[| is_b(1,0) \<in> formula; x \<in> nat; y \<in> nat; z \<in> nat |]
   613       ==> arity(is_nat_case_fm(x,is_b,y,z)) =
   614           succ(x) \<union> succ(y) \<union> succ(z) \<union> (arity(is_b(1,0)) #- 2)"
   615 apply (subgoal_tac "arity(is_b(1,0)) \<in> nat")
   616 apply typecheck
   617 (*FIXME: could nat_diff_split work?*)
   618 apply (auto simp add: diff_def raw_diff_succ is_nat_case_fm_def nat_imp_quasinat
   619                  succ_Un_distrib [symmetric] Un_ac
   620                  split: split_nat_case)
   621 done
   622 
   623 lemma sats_is_nat_case_fm:
   624   assumes is_b_iff_sats:
   625       "!!a b. [| a \<in> A; b \<in> A|]
   626               ==> is_b(a,b) <-> sats(A, p(1,0), Cons(b, Cons(a,env)))"
   627   shows
   628       "[|x \<in> nat; y \<in> nat; z < length(env); env \<in> list(A)|]
   629        ==> sats(A, is_nat_case_fm(x,p,y,z), env) <->
   630            is_nat_case(**A, nth(x,env), is_b, nth(y,env), nth(z,env))"
   631 apply (frule lt_length_in_nat, assumption)
   632 apply (simp add: is_nat_case_fm_def is_nat_case_def is_b_iff_sats [THEN iff_sym])
   633 done
   634 
   635 lemma is_nat_case_iff_sats:
   636   "[| (!!a b. [| a \<in> A; b \<in> A|]
   637               ==> is_b(a,b) <-> sats(A, p(1,0), Cons(b, Cons(a,env))));
   638       nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
   639       i \<in> nat; j \<in> nat; k < length(env); env \<in> list(A)|]
   640    ==> is_nat_case(**A, x, is_b, y, z) <-> sats(A, is_nat_case_fm(i,p,j,k), env)"
   641 by (simp add: sats_is_nat_case_fm [of A is_b])
   642 
   643 
   644 text{*The second argument of @{term is_b} gives it direct access to @{term x},
   645   which is essential for handling free variable references.  Without this
   646   argument, we cannot prove reflection for @{term iterates_MH}.*}
   647 theorem is_nat_case_reflection:
   648   assumes is_b_reflection:
   649     "!!h f g. REFLECTS[\<lambda>x. is_b(L, h(x), f(x), g(x)),
   650                      \<lambda>i x. is_b(**Lset(i), h(x), f(x), g(x))]"
   651   shows "REFLECTS[\<lambda>x. is_nat_case(L, f(x), is_b(L,x), g(x), h(x)),
   652                \<lambda>i x. is_nat_case(**Lset(i), f(x), is_b(**Lset(i), x), g(x), h(x))]"
   653 apply (simp (no_asm_use) only: is_nat_case_def setclass_simps)
   654 apply (intro FOL_reflections function_reflections
   655              restriction_reflection is_b_reflection quasinat_reflection)
   656 done
   657 
   658 
   659 
   660 subsection{*The Operator @{term iterates_MH}, Needed for Iteration*}
   661 
   662 (*  iterates_MH :: "[i=>o, [i,i]=>o, i, i, i, i] => o"
   663    "iterates_MH(M,isF,v,n,g,z) ==
   664         is_nat_case(M, v, \<lambda>m u. \<exists>gm[M]. fun_apply(M,g,m,gm) & isF(gm,u),
   665                     n, z)" *)
   666 constdefs iterates_MH_fm :: "[[i,i]=>i, i, i, i, i]=>i"
   667  "iterates_MH_fm(isF,v,n,g,z) ==
   668     is_nat_case_fm(v,
   669       \<lambda>m u. Exists(And(fun_apply_fm(succ(succ(succ(g))),succ(m),0),
   670                      Forall(Implies(Equal(0,succ(succ(u))), isF(1,0))))),
   671       n, z)"
   672 
   673 lemma iterates_MH_type [TC]:
   674      "[| p(1,0) \<in> formula;
   675          v \<in> nat; x \<in> nat; y \<in> nat; z \<in> nat |]
   676       ==> iterates_MH_fm(p,v,x,y,z) \<in> formula"
   677 by (simp add: iterates_MH_fm_def)
   678 
   679 
   680 lemma arity_iterates_MH_fm [simp]:
   681      "[| p(1,0) \<in> formula;
   682          v \<in> nat; x \<in> nat; y \<in> nat; z \<in> nat |]
   683       ==> arity(iterates_MH_fm(p,v,x,y,z)) =
   684           succ(v) \<union> succ(x) \<union> succ(y) \<union> succ(z) \<union> (arity(p(1,0)) #- 4)"
   685 apply (subgoal_tac "arity(p(1,0)) \<in> nat")
   686 apply typecheck
   687 apply (simp add: nat_imp_quasinat iterates_MH_fm_def Un_ac
   688             split: split_nat_case, clarify)
   689 apply (rename_tac i j)
   690 apply (drule eq_succ_imp_eq_m1, simp)
   691 apply (drule eq_succ_imp_eq_m1, simp)
   692 apply (simp add: diff_Un_distrib succ_Un_distrib Un_ac diff_diff_left)
   693 done
   694 
   695 lemma sats_iterates_MH_fm:
   696   assumes is_F_iff_sats:
   697       "!!a b c d. [| a \<in> A; b \<in> A; c \<in> A; d \<in> A|]
   698               ==> is_F(a,b) <->
   699                   sats(A, p(1,0), Cons(b, Cons(a, Cons(c, Cons(d,env)))))"
   700   shows
   701       "[|v \<in> nat; x \<in> nat; y \<in> nat; z < length(env); env \<in> list(A)|]
   702        ==> sats(A, iterates_MH_fm(p,v,x,y,z), env) <->
   703            iterates_MH(**A, is_F, nth(v,env), nth(x,env), nth(y,env), nth(z,env))"
   704 by (simp add: iterates_MH_fm_def iterates_MH_def sats_is_nat_case_fm
   705               is_F_iff_sats [symmetric])
   706 
   707 lemma iterates_MH_iff_sats:
   708   "[| (!!a b c d. [| a \<in> A; b \<in> A; c \<in> A; d \<in> A|]
   709               ==> is_F(a,b) <->
   710                   sats(A, p(1,0), Cons(b, Cons(a, Cons(c, Cons(d,env))))));
   711       nth(i',env) = v; nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
   712       i' \<in> nat; i \<in> nat; j \<in> nat; k < length(env); env \<in> list(A)|]
   713    ==> iterates_MH(**A, is_F, v, x, y, z) <->
   714        sats(A, iterates_MH_fm(p,i',i,j,k), env)"
   715 apply (rule iff_sym)
   716 apply (rule iff_trans)
   717 apply (rule sats_iterates_MH_fm [of A is_F], blast, simp_all)
   718 done
   719 
   720 theorem iterates_MH_reflection:
   721   assumes p_reflection:
   722     "!!f g h. REFLECTS[\<lambda>x. p(L, f(x), g(x)),
   723                      \<lambda>i x. p(**Lset(i), f(x), g(x))]"
   724  shows "REFLECTS[\<lambda>x. iterates_MH(L, p(L), e(x), f(x), g(x), h(x)),
   725                \<lambda>i x. iterates_MH(**Lset(i), p(**Lset(i)), e(x), f(x), g(x), h(x))]"
   726 apply (simp (no_asm_use) only: iterates_MH_def)
   727 txt{*Must be careful: simplifying with @{text setclass_simps} above would
   728      change @{text "\<exists>gm[**Lset(i)]"} into @{text "\<exists>gm \<in> Lset(i)"}, when
   729      it would no longer match rule @{text is_nat_case_reflection}. *}
   730 apply (rule is_nat_case_reflection)
   731 apply (simp (no_asm_use) only: setclass_simps)
   732 apply (intro FOL_reflections function_reflections is_nat_case_reflection
   733              restriction_reflection p_reflection)
   734 done
   735 
   736 
   737 
   738 subsection{*@{term L} is Closed Under the Operator @{term list}*}
   739 
   740 subsubsection{*The List Functor, Internalized*}
   741 
   742 constdefs list_functor_fm :: "[i,i,i]=>i"
   743 (* "is_list_functor(M,A,X,Z) ==
   744         \<exists>n1[M]. \<exists>AX[M].
   745          number1(M,n1) & cartprod(M,A,X,AX) & is_sum(M,n1,AX,Z)" *)
   746     "list_functor_fm(A,X,Z) ==
   747        Exists(Exists(
   748         And(number1_fm(1),
   749             And(cartprod_fm(A#+2,X#+2,0), sum_fm(1,0,Z#+2)))))"
   750 
   751 lemma list_functor_type [TC]:
   752      "[| x \<in> nat; y \<in> nat; z \<in> nat |] ==> list_functor_fm(x,y,z) \<in> formula"
   753 by (simp add: list_functor_fm_def)
   754 
   755 lemma arity_list_functor_fm [simp]:
   756      "[| x \<in> nat; y \<in> nat; z \<in> nat |]
   757       ==> arity(list_functor_fm(x,y,z)) = succ(x) \<union> succ(y) \<union> succ(z)"
   758 by (simp add: list_functor_fm_def succ_Un_distrib [symmetric] Un_ac)
   759 
   760 lemma sats_list_functor_fm [simp]:
   761    "[| x \<in> nat; y \<in> nat; z \<in> nat; env \<in> list(A)|]
   762     ==> sats(A, list_functor_fm(x,y,z), env) <->
   763         is_list_functor(**A, nth(x,env), nth(y,env), nth(z,env))"
   764 by (simp add: list_functor_fm_def is_list_functor_def)
   765 
   766 lemma list_functor_iff_sats:
   767   "[| nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
   768       i \<in> nat; j \<in> nat; k \<in> nat; env \<in> list(A)|]
   769    ==> is_list_functor(**A, x, y, z) <-> sats(A, list_functor_fm(i,j,k), env)"
   770 by simp
   771 
   772 theorem list_functor_reflection:
   773      "REFLECTS[\<lambda>x. is_list_functor(L,f(x),g(x),h(x)),
   774                \<lambda>i x. is_list_functor(**Lset(i),f(x),g(x),h(x))]"
   775 apply (simp only: is_list_functor_def setclass_simps)
   776 apply (intro FOL_reflections number1_reflection
   777              cartprod_reflection sum_reflection)
   778 done
   779 
   780 
   781 subsubsection{*Instances of Replacement for Lists*}
   782 
   783 lemma list_replacement1_Reflects:
   784  "REFLECTS
   785    [\<lambda>x. \<exists>u[L]. u \<in> B \<and> (\<exists>y[L]. pair(L,u,y,x) \<and>
   786          is_wfrec(L, iterates_MH(L, is_list_functor(L,A), 0), memsn, u, y)),
   787     \<lambda>i x. \<exists>u \<in> Lset(i). u \<in> B \<and> (\<exists>y \<in> Lset(i). pair(**Lset(i), u, y, x) \<and>
   788          is_wfrec(**Lset(i),
   789                   iterates_MH(**Lset(i),
   790                           is_list_functor(**Lset(i), A), 0), memsn, u, y))]"
   791 by (intro FOL_reflections function_reflections is_wfrec_reflection
   792           iterates_MH_reflection list_functor_reflection)
   793 
   794 lemma list_replacement1:
   795    "L(A) ==> iterates_replacement(L, is_list_functor(L,A), 0)"
   796 apply (unfold iterates_replacement_def wfrec_replacement_def, clarify)
   797 apply (rule strong_replacementI)
   798 apply (rule rallI)
   799 apply (rename_tac B)
   800 apply (rule separation_CollectI)
   801 apply (insert nonempty)
   802 apply (subgoal_tac "L(Memrel(succ(n)))")
   803 apply (rule_tac A="{B,A,n,z,0,Memrel(succ(n))}" in subset_LsetE, blast )
   804 apply (rule ReflectsE [OF list_replacement1_Reflects], assumption)
   805 apply (drule subset_Lset_ltD, assumption)
   806 apply (erule reflection_imp_L_separation)
   807   apply (simp_all add: lt_Ord2 Memrel_closed)
   808 apply (elim conjE)
   809 apply (rule DPow_LsetI)
   810 apply (rename_tac v)
   811 apply (rule bex_iff_sats conj_iff_sats)+
   812 apply (rule_tac env = "[u,v,A,n,B,0,Memrel(succ(n))]" in mem_iff_sats)
   813 apply (rule sep_rules | simp)+
   814 txt{*Can't get sat rules to work for higher-order operators, so just expand them!*}
   815 apply (simp add: is_wfrec_def M_is_recfun_def iterates_MH_def is_nat_case_def)
   816 apply (rule sep_rules list_functor_iff_sats quasinat_iff_sats | simp)+
   817 done
   818 
   819 
   820 lemma list_replacement2_Reflects:
   821  "REFLECTS
   822    [\<lambda>x. \<exists>u[L]. u \<in> B \<and> u \<in> nat \<and>
   823          (\<exists>sn[L]. \<exists>msn[L]. successor(L, u, sn) \<and> membership(L, sn, msn) \<and>
   824            is_wfrec (L, iterates_MH (L, is_list_functor(L, A), 0),
   825                               msn, u, x)),
   826     \<lambda>i x. \<exists>u \<in> Lset(i). u \<in> B \<and> u \<in> nat \<and>
   827          (\<exists>sn \<in> Lset(i). \<exists>msn \<in> Lset(i).
   828           successor(**Lset(i), u, sn) \<and> membership(**Lset(i), sn, msn) \<and>
   829            is_wfrec (**Lset(i),
   830                  iterates_MH (**Lset(i), is_list_functor(**Lset(i), A), 0),
   831                      msn, u, x))]"
   832 by (intro FOL_reflections function_reflections is_wfrec_reflection
   833           iterates_MH_reflection list_functor_reflection)
   834 
   835 
   836 lemma list_replacement2:
   837    "L(A) ==> strong_replacement(L,
   838          \<lambda>n y. n\<in>nat &
   839                (\<exists>sn[L]. \<exists>msn[L]. successor(L,n,sn) & membership(L,sn,msn) &
   840                is_wfrec(L, iterates_MH(L,is_list_functor(L,A), 0),
   841                         msn, n, y)))"
   842 apply (rule strong_replacementI)
   843 apply (rule rallI)
   844 apply (rename_tac B)
   845 apply (rule separation_CollectI)
   846 apply (insert nonempty)
   847 apply (rule_tac A="{A,B,z,0,nat}" in subset_LsetE)
   848 apply (blast intro: L_nat)
   849 apply (rule ReflectsE [OF list_replacement2_Reflects], assumption)
   850 apply (drule subset_Lset_ltD, assumption)
   851 apply (erule reflection_imp_L_separation)
   852   apply (simp_all add: lt_Ord2)
   853 apply (rule DPow_LsetI)
   854 apply (rename_tac v)
   855 apply (rule bex_iff_sats conj_iff_sats)+
   856 apply (rule_tac env = "[u,v,A,B,0,nat]" in mem_iff_sats)
   857 apply (rule sep_rules | simp)+
   858 apply (simp add: is_wfrec_def M_is_recfun_def iterates_MH_def is_nat_case_def)
   859 apply (rule sep_rules list_functor_iff_sats quasinat_iff_sats | simp)+
   860 done
   861 
   862 
   863 subsection{*@{term L} is Closed Under the Operator @{term formula}*}
   864 
   865 subsubsection{*The Formula Functor, Internalized*}
   866 
   867 constdefs formula_functor_fm :: "[i,i]=>i"
   868 (*     "is_formula_functor(M,X,Z) ==
   869         \<exists>nat'[M]. \<exists>natnat[M]. \<exists>natnatsum[M]. \<exists>XX[M]. \<exists>X3[M].
   870            4           3               2       1       0
   871           omega(M,nat') & cartprod(M,nat',nat',natnat) &
   872           is_sum(M,natnat,natnat,natnatsum) &
   873           cartprod(M,X,X,XX) & is_sum(M,XX,X,X3) &
   874           is_sum(M,natnatsum,X3,Z)" *)
   875     "formula_functor_fm(X,Z) ==
   876        Exists(Exists(Exists(Exists(Exists(
   877         And(omega_fm(4),
   878          And(cartprod_fm(4,4,3),
   879           And(sum_fm(3,3,2),
   880            And(cartprod_fm(X#+5,X#+5,1),
   881             And(sum_fm(1,X#+5,0), sum_fm(2,0,Z#+5)))))))))))"
   882 
   883 lemma formula_functor_type [TC]:
   884      "[| x \<in> nat; y \<in> nat |] ==> formula_functor_fm(x,y) \<in> formula"
   885 by (simp add: formula_functor_fm_def)
   886 
   887 lemma sats_formula_functor_fm [simp]:
   888    "[| x \<in> nat; y \<in> nat; env \<in> list(A)|]
   889     ==> sats(A, formula_functor_fm(x,y), env) <->
   890         is_formula_functor(**A, nth(x,env), nth(y,env))"
   891 by (simp add: formula_functor_fm_def is_formula_functor_def)
   892 
   893 lemma formula_functor_iff_sats:
   894   "[| nth(i,env) = x; nth(j,env) = y;
   895       i \<in> nat; j \<in> nat; env \<in> list(A)|]
   896    ==> is_formula_functor(**A, x, y) <-> sats(A, formula_functor_fm(i,j), env)"
   897 by simp
   898 
   899 theorem formula_functor_reflection:
   900      "REFLECTS[\<lambda>x. is_formula_functor(L,f(x),g(x)),
   901                \<lambda>i x. is_formula_functor(**Lset(i),f(x),g(x))]"
   902 apply (simp only: is_formula_functor_def setclass_simps)
   903 apply (intro FOL_reflections omega_reflection
   904              cartprod_reflection sum_reflection)
   905 done
   906 
   907 subsubsection{*Instances of Replacement for Formulas*}
   908 
   909 lemma formula_replacement1_Reflects:
   910  "REFLECTS
   911    [\<lambda>x. \<exists>u[L]. u \<in> B \<and> (\<exists>y[L]. pair(L,u,y,x) \<and>
   912          is_wfrec(L, iterates_MH(L, is_formula_functor(L), 0), memsn, u, y)),
   913     \<lambda>i x. \<exists>u \<in> Lset(i). u \<in> B \<and> (\<exists>y \<in> Lset(i). pair(**Lset(i), u, y, x) \<and>
   914          is_wfrec(**Lset(i),
   915                   iterates_MH(**Lset(i),
   916                           is_formula_functor(**Lset(i)), 0), memsn, u, y))]"
   917 by (intro FOL_reflections function_reflections is_wfrec_reflection
   918           iterates_MH_reflection formula_functor_reflection)
   919 
   920 lemma formula_replacement1:
   921    "iterates_replacement(L, is_formula_functor(L), 0)"
   922 apply (unfold iterates_replacement_def wfrec_replacement_def, clarify)
   923 apply (rule strong_replacementI)
   924 apply (rule rallI)
   925 apply (rename_tac B)
   926 apply (rule separation_CollectI)
   927 apply (insert nonempty)
   928 apply (subgoal_tac "L(Memrel(succ(n)))")
   929 apply (rule_tac A="{B,n,z,0,Memrel(succ(n))}" in subset_LsetE, blast )
   930 apply (rule ReflectsE [OF formula_replacement1_Reflects], assumption)
   931 apply (drule subset_Lset_ltD, assumption)
   932 apply (erule reflection_imp_L_separation)
   933   apply (simp_all add: lt_Ord2 Memrel_closed)
   934 apply (rule DPow_LsetI)
   935 apply (rename_tac v)
   936 apply (rule bex_iff_sats conj_iff_sats)+
   937 apply (rule_tac env = "[u,v,n,B,0,Memrel(succ(n))]" in mem_iff_sats)
   938 apply (rule sep_rules | simp)+
   939 txt{*Can't get sat rules to work for higher-order operators, so just expand them!*}
   940 apply (simp add: is_wfrec_def M_is_recfun_def iterates_MH_def is_nat_case_def)
   941 apply (rule sep_rules formula_functor_iff_sats quasinat_iff_sats | simp)+
   942 txt{*SLOW: like 40 seconds!*}
   943 done
   944 
   945 lemma formula_replacement2_Reflects:
   946  "REFLECTS
   947    [\<lambda>x. \<exists>u[L]. u \<in> B \<and> u \<in> nat \<and>
   948          (\<exists>sn[L]. \<exists>msn[L]. successor(L, u, sn) \<and> membership(L, sn, msn) \<and>
   949            is_wfrec (L, iterates_MH (L, is_formula_functor(L), 0),
   950                               msn, u, x)),
   951     \<lambda>i x. \<exists>u \<in> Lset(i). u \<in> B \<and> u \<in> nat \<and>
   952          (\<exists>sn \<in> Lset(i). \<exists>msn \<in> Lset(i).
   953           successor(**Lset(i), u, sn) \<and> membership(**Lset(i), sn, msn) \<and>
   954            is_wfrec (**Lset(i),
   955                  iterates_MH (**Lset(i), is_formula_functor(**Lset(i)), 0),
   956                      msn, u, x))]"
   957 by (intro FOL_reflections function_reflections is_wfrec_reflection
   958           iterates_MH_reflection formula_functor_reflection)
   959 
   960 
   961 lemma formula_replacement2:
   962    "strong_replacement(L,
   963          \<lambda>n y. n\<in>nat &
   964                (\<exists>sn[L]. \<exists>msn[L]. successor(L,n,sn) & membership(L,sn,msn) &
   965                is_wfrec(L, iterates_MH(L,is_formula_functor(L), 0),
   966                         msn, n, y)))"
   967 apply (rule strong_replacementI)
   968 apply (rule rallI)
   969 apply (rename_tac B)
   970 apply (rule separation_CollectI)
   971 apply (insert nonempty)
   972 apply (rule_tac A="{B,z,0,nat}" in subset_LsetE)
   973 apply (blast intro: L_nat)
   974 apply (rule ReflectsE [OF formula_replacement2_Reflects], assumption)
   975 apply (drule subset_Lset_ltD, assumption)
   976 apply (erule reflection_imp_L_separation)
   977   apply (simp_all add: lt_Ord2)
   978 apply (rule DPow_LsetI)
   979 apply (rename_tac v)
   980 apply (rule bex_iff_sats conj_iff_sats)+
   981 apply (rule_tac env = "[u,v,B,0,nat]" in mem_iff_sats)
   982 apply (rule sep_rules | simp)+
   983 apply (simp add: is_wfrec_def M_is_recfun_def iterates_MH_def is_nat_case_def)
   984 apply (rule sep_rules formula_functor_iff_sats quasinat_iff_sats | simp)+
   985 done
   986 
   987 text{*NB The proofs for type @{term formula} are virtually identical to those
   988 for @{term "list(A)"}.  It was a cut-and-paste job! *}
   989 
   990 
   991 subsection{*Internalized Forms of Data Structuring Operators*}
   992 
   993 subsubsection{*The Formula @{term is_Inl}, Internalized*}
   994 
   995 (*  is_Inl(M,a,z) == \<exists>zero[M]. empty(M,zero) & pair(M,zero,a,z) *)
   996 constdefs Inl_fm :: "[i,i]=>i"
   997     "Inl_fm(a,z) == Exists(And(empty_fm(0), pair_fm(0,succ(a),succ(z))))"
   998 
   999 lemma Inl_type [TC]:
  1000      "[| x \<in> nat; z \<in> nat |] ==> Inl_fm(x,z) \<in> formula"
  1001 by (simp add: Inl_fm_def)
  1002 
  1003 lemma sats_Inl_fm [simp]:
  1004    "[| x \<in> nat; z \<in> nat; env \<in> list(A)|]
  1005     ==> sats(A, Inl_fm(x,z), env) <-> is_Inl(**A, nth(x,env), nth(z,env))"
  1006 by (simp add: Inl_fm_def is_Inl_def)
  1007 
  1008 lemma Inl_iff_sats:
  1009       "[| nth(i,env) = x; nth(k,env) = z;
  1010           i \<in> nat; k \<in> nat; env \<in> list(A)|]
  1011        ==> is_Inl(**A, x, z) <-> sats(A, Inl_fm(i,k), env)"
  1012 by simp
  1013 
  1014 theorem Inl_reflection:
  1015      "REFLECTS[\<lambda>x. is_Inl(L,f(x),h(x)),
  1016                \<lambda>i x. is_Inl(**Lset(i),f(x),h(x))]"
  1017 apply (simp only: is_Inl_def setclass_simps)
  1018 apply (intro FOL_reflections function_reflections)
  1019 done
  1020 
  1021 
  1022 subsubsection{*The Formula @{term is_Inr}, Internalized*}
  1023 
  1024 (*  is_Inr(M,a,z) == \<exists>n1[M]. number1(M,n1) & pair(M,n1,a,z) *)
  1025 constdefs Inr_fm :: "[i,i]=>i"
  1026     "Inr_fm(a,z) == Exists(And(number1_fm(0), pair_fm(0,succ(a),succ(z))))"
  1027 
  1028 lemma Inr_type [TC]:
  1029      "[| x \<in> nat; z \<in> nat |] ==> Inr_fm(x,z) \<in> formula"
  1030 by (simp add: Inr_fm_def)
  1031 
  1032 lemma sats_Inr_fm [simp]:
  1033    "[| x \<in> nat; z \<in> nat; env \<in> list(A)|]
  1034     ==> sats(A, Inr_fm(x,z), env) <-> is_Inr(**A, nth(x,env), nth(z,env))"
  1035 by (simp add: Inr_fm_def is_Inr_def)
  1036 
  1037 lemma Inr_iff_sats:
  1038       "[| nth(i,env) = x; nth(k,env) = z;
  1039           i \<in> nat; k \<in> nat; env \<in> list(A)|]
  1040        ==> is_Inr(**A, x, z) <-> sats(A, Inr_fm(i,k), env)"
  1041 by simp
  1042 
  1043 theorem Inr_reflection:
  1044      "REFLECTS[\<lambda>x. is_Inr(L,f(x),h(x)),
  1045                \<lambda>i x. is_Inr(**Lset(i),f(x),h(x))]"
  1046 apply (simp only: is_Inr_def setclass_simps)
  1047 apply (intro FOL_reflections function_reflections)
  1048 done
  1049 
  1050 
  1051 subsubsection{*The Formula @{term is_Nil}, Internalized*}
  1052 
  1053 (* is_Nil(M,xs) == \<exists>zero[M]. empty(M,zero) & is_Inl(M,zero,xs) *)
  1054 
  1055 constdefs Nil_fm :: "i=>i"
  1056     "Nil_fm(x) == Exists(And(empty_fm(0), Inl_fm(0,succ(x))))"
  1057 
  1058 lemma Nil_type [TC]: "x \<in> nat ==> Nil_fm(x) \<in> formula"
  1059 by (simp add: Nil_fm_def)
  1060 
  1061 lemma sats_Nil_fm [simp]:
  1062    "[| x \<in> nat; env \<in> list(A)|]
  1063     ==> sats(A, Nil_fm(x), env) <-> is_Nil(**A, nth(x,env))"
  1064 by (simp add: Nil_fm_def is_Nil_def)
  1065 
  1066 lemma Nil_iff_sats:
  1067       "[| nth(i,env) = x; i \<in> nat; env \<in> list(A)|]
  1068        ==> is_Nil(**A, x) <-> sats(A, Nil_fm(i), env)"
  1069 by simp
  1070 
  1071 theorem Nil_reflection:
  1072      "REFLECTS[\<lambda>x. is_Nil(L,f(x)),
  1073                \<lambda>i x. is_Nil(**Lset(i),f(x))]"
  1074 apply (simp only: is_Nil_def setclass_simps)
  1075 apply (intro FOL_reflections function_reflections Inl_reflection)
  1076 done
  1077 
  1078 
  1079 subsubsection{*The Formula @{term is_Cons}, Internalized*}
  1080 
  1081 
  1082 (*  "is_Cons(M,a,l,Z) == \<exists>p[M]. pair(M,a,l,p) & is_Inr(M,p,Z)" *)
  1083 constdefs Cons_fm :: "[i,i,i]=>i"
  1084     "Cons_fm(a,l,Z) ==
  1085        Exists(And(pair_fm(succ(a),succ(l),0), Inr_fm(0,succ(Z))))"
  1086 
  1087 lemma Cons_type [TC]:
  1088      "[| x \<in> nat; y \<in> nat; z \<in> nat |] ==> Cons_fm(x,y,z) \<in> formula"
  1089 by (simp add: Cons_fm_def)
  1090 
  1091 lemma sats_Cons_fm [simp]:
  1092    "[| x \<in> nat; y \<in> nat; z \<in> nat; env \<in> list(A)|]
  1093     ==> sats(A, Cons_fm(x,y,z), env) <->
  1094        is_Cons(**A, nth(x,env), nth(y,env), nth(z,env))"
  1095 by (simp add: Cons_fm_def is_Cons_def)
  1096 
  1097 lemma Cons_iff_sats:
  1098       "[| nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
  1099           i \<in> nat; j \<in> nat; k \<in> nat; env \<in> list(A)|]
  1100        ==>is_Cons(**A, x, y, z) <-> sats(A, Cons_fm(i,j,k), env)"
  1101 by simp
  1102 
  1103 theorem Cons_reflection:
  1104      "REFLECTS[\<lambda>x. is_Cons(L,f(x),g(x),h(x)),
  1105                \<lambda>i x. is_Cons(**Lset(i),f(x),g(x),h(x))]"
  1106 apply (simp only: is_Cons_def setclass_simps)
  1107 apply (intro FOL_reflections pair_reflection Inr_reflection)
  1108 done
  1109 
  1110 subsubsection{*The Formula @{term is_quasilist}, Internalized*}
  1111 
  1112 (* is_quasilist(M,xs) == is_Nil(M,z) | (\<exists>x[M]. \<exists>l[M]. is_Cons(M,x,l,z))" *)
  1113 
  1114 constdefs quasilist_fm :: "i=>i"
  1115     "quasilist_fm(x) ==
  1116        Or(Nil_fm(x), Exists(Exists(Cons_fm(1,0,succ(succ(x))))))"
  1117 
  1118 lemma quasilist_type [TC]: "x \<in> nat ==> quasilist_fm(x) \<in> formula"
  1119 by (simp add: quasilist_fm_def)
  1120 
  1121 lemma sats_quasilist_fm [simp]:
  1122    "[| x \<in> nat; env \<in> list(A)|]
  1123     ==> sats(A, quasilist_fm(x), env) <-> is_quasilist(**A, nth(x,env))"
  1124 by (simp add: quasilist_fm_def is_quasilist_def)
  1125 
  1126 lemma quasilist_iff_sats:
  1127       "[| nth(i,env) = x; i \<in> nat; env \<in> list(A)|]
  1128        ==> is_quasilist(**A, x) <-> sats(A, quasilist_fm(i), env)"
  1129 by simp
  1130 
  1131 theorem quasilist_reflection:
  1132      "REFLECTS[\<lambda>x. is_quasilist(L,f(x)),
  1133                \<lambda>i x. is_quasilist(**Lset(i),f(x))]"
  1134 apply (simp only: is_quasilist_def setclass_simps)
  1135 apply (intro FOL_reflections Nil_reflection Cons_reflection)
  1136 done
  1137 
  1138 
  1139 subsection{*Absoluteness for the Function @{term nth}*}
  1140 
  1141 
  1142 subsubsection{*The Formula @{term is_tl}, Internalized*}
  1143 
  1144 (*     "is_tl(M,xs,T) ==
  1145        (is_Nil(M,xs) --> T=xs) &
  1146        (\<forall>x[M]. \<forall>l[M]. ~ is_Cons(M,x,l,xs) | T=l) &
  1147        (is_quasilist(M,xs) | empty(M,T))" *)
  1148 constdefs tl_fm :: "[i,i]=>i"
  1149     "tl_fm(xs,T) ==
  1150        And(Implies(Nil_fm(xs), Equal(T,xs)),
  1151            And(Forall(Forall(Or(Neg(Cons_fm(1,0,xs#+2)), Equal(T#+2,0)))),
  1152                Or(quasilist_fm(xs), empty_fm(T))))"
  1153 
  1154 lemma tl_type [TC]:
  1155      "[| x \<in> nat; y \<in> nat |] ==> tl_fm(x,y) \<in> formula"
  1156 by (simp add: tl_fm_def)
  1157 
  1158 lemma sats_tl_fm [simp]:
  1159    "[| x \<in> nat; y \<in> nat; env \<in> list(A)|]
  1160     ==> sats(A, tl_fm(x,y), env) <-> is_tl(**A, nth(x,env), nth(y,env))"
  1161 by (simp add: tl_fm_def is_tl_def)
  1162 
  1163 lemma tl_iff_sats:
  1164       "[| nth(i,env) = x; nth(j,env) = y;
  1165           i \<in> nat; j \<in> nat; env \<in> list(A)|]
  1166        ==> is_tl(**A, x, y) <-> sats(A, tl_fm(i,j), env)"
  1167 by simp
  1168 
  1169 theorem tl_reflection:
  1170      "REFLECTS[\<lambda>x. is_tl(L,f(x),g(x)),
  1171                \<lambda>i x. is_tl(**Lset(i),f(x),g(x))]"
  1172 apply (simp only: is_tl_def setclass_simps)
  1173 apply (intro FOL_reflections Nil_reflection Cons_reflection
  1174              quasilist_reflection empty_reflection)
  1175 done
  1176 
  1177 
  1178 subsubsection{*An Instance of Replacement for @{term nth}*}
  1179 
  1180 lemma nth_replacement_Reflects:
  1181  "REFLECTS
  1182    [\<lambda>x. \<exists>u[L]. u \<in> B \<and> (\<exists>y[L]. pair(L,u,y,x) \<and>
  1183          is_wfrec(L, iterates_MH(L, is_tl(L), z), memsn, u, y)),
  1184     \<lambda>i x. \<exists>u \<in> Lset(i). u \<in> B \<and> (\<exists>y \<in> Lset(i). pair(**Lset(i), u, y, x) \<and>
  1185          is_wfrec(**Lset(i),
  1186                   iterates_MH(**Lset(i),
  1187                           is_tl(**Lset(i)), z), memsn, u, y))]"
  1188 by (intro FOL_reflections function_reflections is_wfrec_reflection
  1189           iterates_MH_reflection list_functor_reflection tl_reflection)
  1190 
  1191 lemma nth_replacement:
  1192    "L(w) ==> iterates_replacement(L, %l t. is_tl(L,l,t), w)"
  1193 apply (unfold iterates_replacement_def wfrec_replacement_def, clarify)
  1194 apply (rule strong_replacementI)
  1195 apply (rule rallI)
  1196 apply (rule separation_CollectI)
  1197 apply (subgoal_tac "L(Memrel(succ(n)))")
  1198 apply (rule_tac A="{A,n,w,z,Memrel(succ(n))}" in subset_LsetE, blast )
  1199 apply (rule ReflectsE [OF nth_replacement_Reflects], assumption)
  1200 apply (drule subset_Lset_ltD, assumption)
  1201 apply (erule reflection_imp_L_separation)
  1202   apply (simp_all add: lt_Ord2 Memrel_closed)
  1203 apply (elim conjE)
  1204 apply (rule DPow_LsetI)
  1205 apply (rename_tac v)
  1206 apply (rule bex_iff_sats conj_iff_sats)+
  1207 apply (rule_tac env = "[u,v,A,z,w,Memrel(succ(n))]" in mem_iff_sats)
  1208 apply (rule sep_rules | simp)+
  1209 apply (simp add: is_wfrec_def M_is_recfun_def iterates_MH_def is_nat_case_def)
  1210 apply (rule sep_rules quasinat_iff_sats tl_iff_sats | simp)+
  1211 done
  1212 
  1213 
  1214 
  1215 subsubsection{*Instantiating the locale @{text M_datatypes}*}
  1216 
  1217 theorem M_datatypes_L: "PROP M_datatypes(L)"
  1218   apply (rule M_datatypes.intro)
  1219       apply (rule M_wfrank.axioms [OF M_wfrank_L])+
  1220   apply (rule M_datatypes_axioms.intro)
  1221       apply (assumption | rule
  1222         list_replacement1 list_replacement2
  1223         formula_replacement1 formula_replacement2
  1224         nth_replacement)+
  1225   done
  1226 
  1227 lemmas list_closed = M_datatypes.list_closed [OF M_datatypes_L]
  1228   and formula_closed = M_datatypes.formula_closed [OF M_datatypes_L]
  1229   and list_abs = M_datatypes.list_abs [OF M_datatypes_L]
  1230   and formula_abs = M_datatypes.formula_abs [OF M_datatypes_L]
  1231   and nth_abs = M_datatypes.nth_abs [OF M_datatypes_L]
  1232 
  1233 declare list_closed [intro,simp]
  1234 declare formula_closed [intro,simp]
  1235 declare list_abs [simp]
  1236 declare formula_abs [simp]
  1237 declare nth_abs [simp]
  1238 
  1239 
  1240 subsection{*@{term L} is Closed Under the Operator @{term eclose}*}
  1241 
  1242 subsubsection{*Instances of Replacement for @{term eclose}*}
  1243 
  1244 lemma eclose_replacement1_Reflects:
  1245  "REFLECTS
  1246    [\<lambda>x. \<exists>u[L]. u \<in> B \<and> (\<exists>y[L]. pair(L,u,y,x) \<and>
  1247          is_wfrec(L, iterates_MH(L, big_union(L), A), memsn, u, y)),
  1248     \<lambda>i x. \<exists>u \<in> Lset(i). u \<in> B \<and> (\<exists>y \<in> Lset(i). pair(**Lset(i), u, y, x) \<and>
  1249          is_wfrec(**Lset(i),
  1250                   iterates_MH(**Lset(i), big_union(**Lset(i)), A),
  1251                   memsn, u, y))]"
  1252 by (intro FOL_reflections function_reflections is_wfrec_reflection
  1253           iterates_MH_reflection)
  1254 
  1255 lemma eclose_replacement1:
  1256    "L(A) ==> iterates_replacement(L, big_union(L), A)"
  1257 apply (unfold iterates_replacement_def wfrec_replacement_def, clarify)
  1258 apply (rule strong_replacementI)
  1259 apply (rule rallI)
  1260 apply (rename_tac B)
  1261 apply (rule separation_CollectI)
  1262 apply (subgoal_tac "L(Memrel(succ(n)))")
  1263 apply (rule_tac A="{B,A,n,z,Memrel(succ(n))}" in subset_LsetE, blast )
  1264 apply (rule ReflectsE [OF eclose_replacement1_Reflects], assumption)
  1265 apply (drule subset_Lset_ltD, assumption)
  1266 apply (erule reflection_imp_L_separation)
  1267   apply (simp_all add: lt_Ord2 Memrel_closed)
  1268 apply (elim conjE)
  1269 apply (rule DPow_LsetI)
  1270 apply (rename_tac v)
  1271 apply (rule bex_iff_sats conj_iff_sats)+
  1272 apply (rule_tac env = "[u,v,A,n,B,Memrel(succ(n))]" in mem_iff_sats)
  1273 apply (rule sep_rules | simp)+
  1274 txt{*Can't get sat rules to work for higher-order operators, so just expand them!*}
  1275 apply (simp add: is_wfrec_def M_is_recfun_def iterates_MH_def is_nat_case_def)
  1276 apply (rule sep_rules big_union_iff_sats quasinat_iff_sats | simp)+
  1277 done
  1278 
  1279 
  1280 lemma eclose_replacement2_Reflects:
  1281  "REFLECTS
  1282    [\<lambda>x. \<exists>u[L]. u \<in> B \<and> u \<in> nat \<and>
  1283          (\<exists>sn[L]. \<exists>msn[L]. successor(L, u, sn) \<and> membership(L, sn, msn) \<and>
  1284            is_wfrec (L, iterates_MH (L, big_union(L), A),
  1285                               msn, u, x)),
  1286     \<lambda>i x. \<exists>u \<in> Lset(i). u \<in> B \<and> u \<in> nat \<and>
  1287          (\<exists>sn \<in> Lset(i). \<exists>msn \<in> Lset(i).
  1288           successor(**Lset(i), u, sn) \<and> membership(**Lset(i), sn, msn) \<and>
  1289            is_wfrec (**Lset(i),
  1290                  iterates_MH (**Lset(i), big_union(**Lset(i)), A),
  1291                      msn, u, x))]"
  1292 by (intro FOL_reflections function_reflections is_wfrec_reflection
  1293           iterates_MH_reflection)
  1294 
  1295 
  1296 lemma eclose_replacement2:
  1297    "L(A) ==> strong_replacement(L,
  1298          \<lambda>n y. n\<in>nat &
  1299                (\<exists>sn[L]. \<exists>msn[L]. successor(L,n,sn) & membership(L,sn,msn) &
  1300                is_wfrec(L, iterates_MH(L,big_union(L), A),
  1301                         msn, n, y)))"
  1302 apply (rule strong_replacementI)
  1303 apply (rule rallI)
  1304 apply (rename_tac B)
  1305 apply (rule separation_CollectI)
  1306 apply (rule_tac A="{A,B,z,nat}" in subset_LsetE)
  1307 apply (blast intro: L_nat)
  1308 apply (rule ReflectsE [OF eclose_replacement2_Reflects], assumption)
  1309 apply (drule subset_Lset_ltD, assumption)
  1310 apply (erule reflection_imp_L_separation)
  1311   apply (simp_all add: lt_Ord2)
  1312 apply (rule DPow_LsetI)
  1313 apply (rename_tac v)
  1314 apply (rule bex_iff_sats conj_iff_sats)+
  1315 apply (rule_tac env = "[u,v,A,B,nat]" in mem_iff_sats)
  1316 apply (rule sep_rules | simp)+
  1317 apply (simp add: is_wfrec_def M_is_recfun_def iterates_MH_def is_nat_case_def)
  1318 apply (rule sep_rules big_union_iff_sats quasinat_iff_sats | simp)+
  1319 done
  1320 
  1321 
  1322 subsubsection{*Instantiating the locale @{text M_eclose}*}
  1323 
  1324 theorem M_eclose_L: "PROP M_eclose(L)"
  1325   apply (rule M_eclose.intro)
  1326        apply (rule M_datatypes.axioms [OF M_datatypes_L])+
  1327   apply (rule M_eclose_axioms.intro)
  1328    apply (assumption | rule eclose_replacement1 eclose_replacement2)+
  1329   done
  1330 
  1331 lemmas eclose_closed [intro, simp] = M_eclose.eclose_closed [OF M_eclose_L]
  1332   and eclose_abs [intro, simp] = M_eclose.eclose_abs [OF M_eclose_L]
  1333 
  1334 end