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