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