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