src/ZF/Constructible/Rec_Separation.thy
author paulson
Thu Jul 18 15:21:42 2002 +0200 (2002-07-18)
changeset 13395 4eb948d1eb4e
parent 13387 b7464ca2ebbb
child 13398 1cadd412da48
permissions -rw-r--r--
absoluteness for "formula" and "eclose"
     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]. \<exists>X4[M]. 
   878           5          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) & is_sum(M,X,X3,X4) &
   882           is_sum(M,natnatsum,X4,Z)" *) 
   883     "formula_functor_fm(X,Z) == 
   884        Exists(Exists(Exists(Exists(Exists(Exists(
   885 	And(omega_fm(5),
   886          And(cartprod_fm(5,5,4),
   887           And(sum_fm(4,4,3),
   888            And(cartprod_fm(X#+6,X#+6,2),
   889             And(sum_fm(2,X#+6,1),
   890              And(sum_fm(X#+6,1,0), sum_fm(3,0,Z#+6)))))))))))))"
   891 
   892 lemma formula_functor_type [TC]:
   893      "[| x \<in> nat; y \<in> nat |] ==> formula_functor_fm(x,y) \<in> formula"
   894 by (simp add: formula_functor_fm_def) 
   895 
   896 lemma sats_formula_functor_fm [simp]:
   897    "[| x \<in> nat; y \<in> nat; env \<in> list(A)|]
   898     ==> sats(A, formula_functor_fm(x,y), env) <-> 
   899         is_formula_functor(**A, nth(x,env), nth(y,env))"
   900 by (simp add: formula_functor_fm_def is_formula_functor_def)
   901 
   902 lemma formula_functor_iff_sats:
   903   "[| nth(i,env) = x; nth(j,env) = y; 
   904       i \<in> nat; j \<in> nat; env \<in> list(A)|]
   905    ==> is_formula_functor(**A, x, y) <-> sats(A, formula_functor_fm(i,j), env)"
   906 by simp
   907 
   908 theorem formula_functor_reflection:
   909      "REFLECTS[\<lambda>x. is_formula_functor(L,f(x),g(x)), 
   910                \<lambda>i x. is_formula_functor(**Lset(i),f(x),g(x))]"
   911 apply (simp only: is_formula_functor_def setclass_simps)
   912 apply (intro FOL_reflections omega_reflection
   913              cartprod_reflection sum_reflection)  
   914 done
   915 
   916 subsubsection{*Instances of Replacement for Formulas*}
   917 
   918 lemma formula_replacement1_Reflects:
   919  "REFLECTS
   920    [\<lambda>x. \<exists>u[L]. u \<in> B \<and> (\<exists>y[L]. pair(L,u,y,x) \<and>
   921          is_wfrec(L, iterates_MH(L, is_formula_functor(L), 0), memsn, u, y)),
   922     \<lambda>i x. \<exists>u \<in> Lset(i). u \<in> B \<and> (\<exists>y \<in> Lset(i). pair(**Lset(i), u, y, x) \<and>
   923          is_wfrec(**Lset(i), 
   924                   iterates_MH(**Lset(i), 
   925                           is_formula_functor(**Lset(i)), 0), memsn, u, y))]"
   926 by (intro FOL_reflections function_reflections is_wfrec_reflection 
   927           iterates_MH_reflection formula_functor_reflection) 
   928 
   929 lemma formula_replacement1: 
   930    "iterates_replacement(L, is_formula_functor(L), 0)"
   931 apply (unfold iterates_replacement_def wfrec_replacement_def, clarify)
   932 apply (rule strong_replacementI) 
   933 apply (rule rallI)
   934 apply (rename_tac B)   
   935 apply (rule separation_CollectI) 
   936 apply (insert nonempty) 
   937 apply (subgoal_tac "L(Memrel(succ(n)))") 
   938 apply (rule_tac A="{B,n,z,0,Memrel(succ(n))}" in subset_LsetE, blast ) 
   939 apply (rule ReflectsE [OF formula_replacement1_Reflects], assumption)
   940 apply (drule subset_Lset_ltD, assumption) 
   941 apply (erule reflection_imp_L_separation)
   942   apply (simp_all add: lt_Ord2 Memrel_closed)
   943 apply (rule DPow_LsetI)
   944 apply (rename_tac v) 
   945 apply (rule bex_iff_sats conj_iff_sats)+
   946 apply (rule_tac env = "[u,v,n,B,0,Memrel(succ(n))]" in mem_iff_sats)
   947 apply (rule sep_rules | simp)+
   948 txt{*Can't get sat rules to work for higher-order operators, so just expand them!*}
   949 apply (simp add: is_wfrec_def M_is_recfun_def iterates_MH_def is_nat_case_def)
   950 apply (rule sep_rules formula_functor_iff_sats quasinat_iff_sats | simp)+
   951 txt{*SLOW: like 40 seconds!*}
   952 done
   953 
   954 lemma formula_replacement2_Reflects:
   955  "REFLECTS
   956    [\<lambda>x. \<exists>u[L]. u \<in> B \<and> u \<in> nat \<and>
   957          (\<exists>sn[L]. \<exists>msn[L]. successor(L, u, sn) \<and> membership(L, sn, msn) \<and>
   958            is_wfrec (L, iterates_MH (L, is_formula_functor(L), 0),
   959                               msn, u, x)),
   960     \<lambda>i x. \<exists>u \<in> Lset(i). u \<in> B \<and> u \<in> nat \<and>
   961          (\<exists>sn \<in> Lset(i). \<exists>msn \<in> Lset(i). 
   962           successor(**Lset(i), u, sn) \<and> membership(**Lset(i), sn, msn) \<and>
   963            is_wfrec (**Lset(i), 
   964                  iterates_MH (**Lset(i), is_formula_functor(**Lset(i)), 0),
   965                      msn, u, x))]"
   966 by (intro FOL_reflections function_reflections is_wfrec_reflection 
   967           iterates_MH_reflection formula_functor_reflection) 
   968 
   969 
   970 lemma formula_replacement2: 
   971    "strong_replacement(L, 
   972          \<lambda>n y. n\<in>nat & 
   973                (\<exists>sn[L]. \<exists>msn[L]. successor(L,n,sn) & membership(L,sn,msn) &
   974                is_wfrec(L, iterates_MH(L,is_formula_functor(L), 0), 
   975                         msn, n, y)))"
   976 apply (rule strong_replacementI) 
   977 apply (rule rallI)
   978 apply (rename_tac B)   
   979 apply (rule separation_CollectI) 
   980 apply (insert nonempty) 
   981 apply (rule_tac A="{B,z,0,nat}" in subset_LsetE) 
   982 apply (blast intro: L_nat) 
   983 apply (rule ReflectsE [OF formula_replacement2_Reflects], assumption)
   984 apply (drule subset_Lset_ltD, assumption) 
   985 apply (erule reflection_imp_L_separation)
   986   apply (simp_all add: lt_Ord2)
   987 apply (rule DPow_LsetI)
   988 apply (rename_tac v) 
   989 apply (rule bex_iff_sats conj_iff_sats)+
   990 apply (rule_tac env = "[u,v,B,0,nat]" in mem_iff_sats)
   991 apply (rule sep_rules | simp)+
   992 apply (simp add: is_wfrec_def M_is_recfun_def iterates_MH_def is_nat_case_def)
   993 apply (rule sep_rules formula_functor_iff_sats quasinat_iff_sats | simp)+
   994 done
   995 
   996 text{*NB The proofs for type @{term formula} are virtually identical to those
   997 for @{term "list(A)"}.  It was a cut-and-paste job! *}
   998 
   999 
  1000 subsubsection{*Instantiating the locale @{text M_datatypes}*}
  1001 ML
  1002 {*
  1003 val list_replacement1 = thm "list_replacement1"; 
  1004 val list_replacement2 = thm "list_replacement2";
  1005 val formula_replacement1 = thm "formula_replacement1";
  1006 val formula_replacement2 = thm "formula_replacement2";
  1007 
  1008 val m_datatypes = [list_replacement1, list_replacement2, 
  1009                    formula_replacement1, formula_replacement2];
  1010 
  1011 fun datatypes_L th =
  1012     kill_flex_triv_prems (m_datatypes MRS (wfrank_L th));
  1013 
  1014 bind_thm ("list_closed", datatypes_L (thm "M_datatypes.list_closed"));
  1015 bind_thm ("formula_closed", datatypes_L (thm "M_datatypes.formula_closed"));
  1016 bind_thm ("list_abs", datatypes_L (thm "M_datatypes.list_abs"));
  1017 bind_thm ("formula_abs", datatypes_L (thm "M_datatypes.formula_abs"));
  1018 *}
  1019 
  1020 declare list_closed [intro,simp]
  1021 declare formula_closed [intro,simp]
  1022 declare list_abs [intro,simp]
  1023 declare formula_abs [intro,simp]
  1024 
  1025 
  1026 
  1027 subsection{*@{term L} is Closed Under the Operator @{term eclose}*} 
  1028 
  1029 subsubsection{*Instances of Replacement for @{term eclose}*}
  1030 
  1031 lemma eclose_replacement1_Reflects:
  1032  "REFLECTS
  1033    [\<lambda>x. \<exists>u[L]. u \<in> B \<and> (\<exists>y[L]. pair(L,u,y,x) \<and>
  1034          is_wfrec(L, iterates_MH(L, big_union(L), A), memsn, u, y)),
  1035     \<lambda>i x. \<exists>u \<in> Lset(i). u \<in> B \<and> (\<exists>y \<in> Lset(i). pair(**Lset(i), u, y, x) \<and>
  1036          is_wfrec(**Lset(i), 
  1037                   iterates_MH(**Lset(i), big_union(**Lset(i)), A), 
  1038                   memsn, u, y))]"
  1039 by (intro FOL_reflections function_reflections is_wfrec_reflection 
  1040           iterates_MH_reflection) 
  1041 
  1042 lemma eclose_replacement1: 
  1043    "L(A) ==> iterates_replacement(L, big_union(L), A)"
  1044 apply (unfold iterates_replacement_def wfrec_replacement_def, clarify)
  1045 apply (rule strong_replacementI) 
  1046 apply (rule rallI)
  1047 apply (rename_tac B)   
  1048 apply (rule separation_CollectI) 
  1049 apply (subgoal_tac "L(Memrel(succ(n)))") 
  1050 apply (rule_tac A="{B,A,n,z,Memrel(succ(n))}" in subset_LsetE, blast ) 
  1051 apply (rule ReflectsE [OF eclose_replacement1_Reflects], assumption)
  1052 apply (drule subset_Lset_ltD, assumption) 
  1053 apply (erule reflection_imp_L_separation)
  1054   apply (simp_all add: lt_Ord2 Memrel_closed)
  1055 apply (elim conjE) 
  1056 apply (rule DPow_LsetI)
  1057 apply (rename_tac v) 
  1058 apply (rule bex_iff_sats conj_iff_sats)+
  1059 apply (rule_tac env = "[u,v,A,n,B,Memrel(succ(n))]" in mem_iff_sats)
  1060 apply (rule sep_rules | simp)+
  1061 txt{*Can't get sat rules to work for higher-order operators, so just expand them!*}
  1062 apply (simp add: is_wfrec_def M_is_recfun_def iterates_MH_def is_nat_case_def)
  1063 apply (rule sep_rules big_union_iff_sats quasinat_iff_sats | simp)+
  1064 done
  1065 
  1066 
  1067 lemma eclose_replacement2_Reflects:
  1068  "REFLECTS
  1069    [\<lambda>x. \<exists>u[L]. u \<in> B \<and> u \<in> nat \<and>
  1070          (\<exists>sn[L]. \<exists>msn[L]. successor(L, u, sn) \<and> membership(L, sn, msn) \<and>
  1071            is_wfrec (L, iterates_MH (L, big_union(L), A),
  1072                               msn, u, x)),
  1073     \<lambda>i x. \<exists>u \<in> Lset(i). u \<in> B \<and> u \<in> nat \<and>
  1074          (\<exists>sn \<in> Lset(i). \<exists>msn \<in> Lset(i). 
  1075           successor(**Lset(i), u, sn) \<and> membership(**Lset(i), sn, msn) \<and>
  1076            is_wfrec (**Lset(i), 
  1077                  iterates_MH (**Lset(i), big_union(**Lset(i)), A),
  1078                      msn, u, x))]"
  1079 by (intro FOL_reflections function_reflections is_wfrec_reflection 
  1080           iterates_MH_reflection) 
  1081 
  1082 
  1083 lemma eclose_replacement2: 
  1084    "L(A) ==> strong_replacement(L, 
  1085          \<lambda>n y. n\<in>nat & 
  1086                (\<exists>sn[L]. \<exists>msn[L]. successor(L,n,sn) & membership(L,sn,msn) &
  1087                is_wfrec(L, iterates_MH(L,big_union(L), A), 
  1088                         msn, n, y)))"
  1089 apply (rule strong_replacementI) 
  1090 apply (rule rallI)
  1091 apply (rename_tac B)   
  1092 apply (rule separation_CollectI) 
  1093 apply (rule_tac A="{A,B,z,nat}" in subset_LsetE) 
  1094 apply (blast intro: L_nat) 
  1095 apply (rule ReflectsE [OF eclose_replacement2_Reflects], assumption)
  1096 apply (drule subset_Lset_ltD, assumption) 
  1097 apply (erule reflection_imp_L_separation)
  1098   apply (simp_all add: lt_Ord2)
  1099 apply (rule DPow_LsetI)
  1100 apply (rename_tac v) 
  1101 apply (rule bex_iff_sats conj_iff_sats)+
  1102 apply (rule_tac env = "[u,v,A,B,nat]" in mem_iff_sats)
  1103 apply (rule sep_rules | simp)+
  1104 apply (simp add: is_wfrec_def M_is_recfun_def iterates_MH_def is_nat_case_def)
  1105 apply (rule sep_rules big_union_iff_sats quasinat_iff_sats | simp)+
  1106 done
  1107 
  1108 
  1109 subsubsection{*Instantiating the locale @{text M_eclose}*}
  1110 ML
  1111 {*
  1112 val eclose_replacement1 = thm "eclose_replacement1"; 
  1113 val eclose_replacement2 = thm "eclose_replacement2";
  1114 
  1115 val m_eclose = [eclose_replacement1, eclose_replacement2];
  1116 
  1117 fun eclose_L th =
  1118     kill_flex_triv_prems (m_eclose MRS (wfrank_L th));
  1119 
  1120 bind_thm ("eclose_closed", eclose_L (thm "M_eclose.eclose_closed"));
  1121 bind_thm ("eclose_abs", eclose_L (thm "M_eclose.eclose_abs"));
  1122 *}
  1123 
  1124 declare eclose_closed [intro,simp]
  1125 declare eclose_abs [intro,simp]
  1126 
  1127 
  1128 
  1129 
  1130 end