src/ZF/Constructible/Datatype_absolute.thy
author paulson
Wed Oct 09 11:07:13 2002 +0200 (2002-10-09)
changeset 13634 99a593b49b04
parent 13615 449a70d88b38
child 13647 7f6f0ffc45c3
permissions -rw-r--r--
Re-organization of Constructible theories
     1 (*  Title:      ZF/Constructible/Datatype_absolute.thy
     2     ID: $Id$
     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
     4 *)
     5 
     6 header {*Absoluteness Properties for Recursive Datatypes*}
     7 
     8 theory Datatype_absolute = Formula + WF_absolute:
     9 
    10 
    11 subsection{*The lfp of a continuous function can be expressed as a union*}
    12 
    13 constdefs
    14   directed :: "i=>o"
    15    "directed(A) == A\<noteq>0 & (\<forall>x\<in>A. \<forall>y\<in>A. x \<union> y \<in> A)"
    16 
    17   contin :: "(i=>i) => o"
    18    "contin(h) == (\<forall>A. directed(A) --> h(\<Union>A) = (\<Union>X\<in>A. h(X)))"
    19 
    20 lemma bnd_mono_iterates_subset: "[|bnd_mono(D, h); n \<in> nat|] ==> h^n (0) <= D"
    21 apply (induct_tac n) 
    22  apply (simp_all add: bnd_mono_def, blast) 
    23 done
    24 
    25 lemma bnd_mono_increasing [rule_format]:
    26      "[|i \<in> nat; j \<in> nat; bnd_mono(D,h)|] ==> i \<le> j --> h^i(0) \<subseteq> h^j(0)"
    27 apply (rule_tac m=i and n=j in diff_induct, simp_all)
    28 apply (blast del: subsetI
    29 	     intro: bnd_mono_iterates_subset bnd_monoD2 [of concl: h]) 
    30 done
    31 
    32 lemma directed_iterates: "bnd_mono(D,h) ==> directed({h^n (0). n\<in>nat})"
    33 apply (simp add: directed_def, clarify) 
    34 apply (rename_tac i j)
    35 apply (rule_tac x="i \<union> j" in bexI) 
    36 apply (rule_tac i = i and j = j in Ord_linear_le)
    37 apply (simp_all add: subset_Un_iff [THEN iffD1] le_imp_subset
    38                      subset_Un_iff2 [THEN iffD1])
    39 apply (simp_all add: subset_Un_iff [THEN iff_sym] bnd_mono_increasing
    40                      subset_Un_iff2 [THEN iff_sym])
    41 done
    42 
    43 
    44 lemma contin_iterates_eq: 
    45     "[|bnd_mono(D, h); contin(h)|] 
    46      ==> h(\<Union>n\<in>nat. h^n (0)) = (\<Union>n\<in>nat. h^n (0))"
    47 apply (simp add: contin_def directed_iterates) 
    48 apply (rule trans) 
    49 apply (rule equalityI) 
    50  apply (simp_all add: UN_subset_iff)
    51  apply safe
    52  apply (erule_tac [2] natE) 
    53   apply (rule_tac a="succ(x)" in UN_I) 
    54    apply simp_all 
    55 apply blast 
    56 done
    57 
    58 lemma lfp_subset_Union:
    59      "[|bnd_mono(D, h); contin(h)|] ==> lfp(D,h) <= (\<Union>n\<in>nat. h^n(0))"
    60 apply (rule lfp_lowerbound) 
    61  apply (simp add: contin_iterates_eq) 
    62 apply (simp add: contin_def bnd_mono_iterates_subset UN_subset_iff) 
    63 done
    64 
    65 lemma Union_subset_lfp:
    66      "bnd_mono(D,h) ==> (\<Union>n\<in>nat. h^n(0)) <= lfp(D,h)"
    67 apply (simp add: UN_subset_iff)
    68 apply (rule ballI)  
    69 apply (induct_tac n, simp_all) 
    70 apply (rule subset_trans [of _ "h(lfp(D,h))"])
    71  apply (blast dest: bnd_monoD2 [OF _ _ lfp_subset])  
    72 apply (erule lfp_lemma2) 
    73 done
    74 
    75 lemma lfp_eq_Union:
    76      "[|bnd_mono(D, h); contin(h)|] ==> lfp(D,h) = (\<Union>n\<in>nat. h^n(0))"
    77 by (blast del: subsetI 
    78           intro: lfp_subset_Union Union_subset_lfp)
    79 
    80 
    81 subsubsection{*Some Standard Datatype Constructions Preserve Continuity*}
    82 
    83 lemma contin_imp_mono: "[|X\<subseteq>Y; contin(F)|] ==> F(X) \<subseteq> F(Y)"
    84 apply (simp add: contin_def) 
    85 apply (drule_tac x="{X,Y}" in spec) 
    86 apply (simp add: directed_def subset_Un_iff2 Un_commute) 
    87 done
    88 
    89 lemma sum_contin: "[|contin(F); contin(G)|] ==> contin(\<lambda>X. F(X) + G(X))"
    90 by (simp add: contin_def, blast)
    91 
    92 lemma prod_contin: "[|contin(F); contin(G)|] ==> contin(\<lambda>X. F(X) * G(X))" 
    93 apply (subgoal_tac "\<forall>B C. F(B) \<subseteq> F(B \<union> C)")
    94  prefer 2 apply (simp add: Un_upper1 contin_imp_mono) 
    95 apply (subgoal_tac "\<forall>B C. G(C) \<subseteq> G(B \<union> C)")
    96  prefer 2 apply (simp add: Un_upper2 contin_imp_mono) 
    97 apply (simp add: contin_def, clarify) 
    98 apply (rule equalityI) 
    99  prefer 2 apply blast 
   100 apply clarify 
   101 apply (rename_tac B C) 
   102 apply (rule_tac a="B \<union> C" in UN_I) 
   103  apply (simp add: directed_def, blast)  
   104 done
   105 
   106 lemma const_contin: "contin(\<lambda>X. A)"
   107 by (simp add: contin_def directed_def)
   108 
   109 lemma id_contin: "contin(\<lambda>X. X)"
   110 by (simp add: contin_def)
   111 
   112 
   113 
   114 subsection {*Absoluteness for "Iterates"*}
   115 
   116 constdefs
   117 
   118   iterates_MH :: "[i=>o, [i,i]=>o, i, i, i, i] => o"
   119    "iterates_MH(M,isF,v,n,g,z) ==
   120         is_nat_case(M, v, \<lambda>m u. \<exists>gm[M]. fun_apply(M,g,m,gm) & isF(gm,u),
   121                     n, z)"
   122 
   123   iterates_replacement :: "[i=>o, [i,i]=>o, i] => o"
   124    "iterates_replacement(M,isF,v) ==
   125       \<forall>n[M]. n\<in>nat --> 
   126          wfrec_replacement(M, iterates_MH(M,isF,v), Memrel(succ(n)))"
   127 
   128 lemma (in M_basic) iterates_MH_abs:
   129   "[| relation1(M,isF,F); M(n); M(g); M(z) |] 
   130    ==> iterates_MH(M,isF,v,n,g,z) <-> z = nat_case(v, \<lambda>m. F(g`m), n)"
   131 by (simp add: nat_case_abs [of _ "\<lambda>m. F(g ` m)"]
   132               relation1_def iterates_MH_def)  
   133 
   134 lemma (in M_basic) iterates_imp_wfrec_replacement:
   135   "[|relation1(M,isF,F); n \<in> nat; iterates_replacement(M,isF,v)|] 
   136    ==> wfrec_replacement(M, \<lambda>n f z. z = nat_case(v, \<lambda>m. F(f`m), n), 
   137                        Memrel(succ(n)))" 
   138 by (simp add: iterates_replacement_def iterates_MH_abs)
   139 
   140 theorem (in M_trancl) iterates_abs:
   141   "[| iterates_replacement(M,isF,v); relation1(M,isF,F);
   142       n \<in> nat; M(v); M(z); \<forall>x[M]. M(F(x)) |] 
   143    ==> is_wfrec(M, iterates_MH(M,isF,v), Memrel(succ(n)), n, z) <->
   144        z = iterates(F,n,v)" 
   145 apply (frule iterates_imp_wfrec_replacement, assumption+)
   146 apply (simp add: wf_Memrel trans_Memrel relation_Memrel nat_into_M
   147                  relation2_def iterates_MH_abs 
   148                  iterates_nat_def recursor_def transrec_def 
   149                  eclose_sing_Ord_eq nat_into_M
   150          trans_wfrec_abs [of _ _ _ _ "\<lambda>n g. nat_case(v, \<lambda>m. F(g`m), n)"])
   151 done
   152 
   153 
   154 lemma (in M_trancl) iterates_closed [intro,simp]:
   155   "[| iterates_replacement(M,isF,v); relation1(M,isF,F);
   156       n \<in> nat; M(v); \<forall>x[M]. M(F(x)) |] 
   157    ==> M(iterates(F,n,v))"
   158 apply (frule iterates_imp_wfrec_replacement, assumption+)
   159 apply (simp add: wf_Memrel trans_Memrel relation_Memrel nat_into_M
   160                  relation2_def iterates_MH_abs 
   161                  iterates_nat_def recursor_def transrec_def 
   162                  eclose_sing_Ord_eq nat_into_M
   163          trans_wfrec_closed [of _ _ _ "\<lambda>n g. nat_case(v, \<lambda>m. F(g`m), n)"])
   164 done
   165 
   166 
   167 subsection {*lists without univ*}
   168 
   169 lemmas datatype_univs = Inl_in_univ Inr_in_univ 
   170                         Pair_in_univ nat_into_univ A_into_univ 
   171 
   172 lemma list_fun_bnd_mono: "bnd_mono(univ(A), \<lambda>X. {0} + A*X)"
   173 apply (rule bnd_monoI)
   174  apply (intro subset_refl zero_subset_univ A_subset_univ 
   175 	      sum_subset_univ Sigma_subset_univ) 
   176 apply (rule subset_refl sum_mono Sigma_mono | assumption)+
   177 done
   178 
   179 lemma list_fun_contin: "contin(\<lambda>X. {0} + A*X)"
   180 by (intro sum_contin prod_contin id_contin const_contin) 
   181 
   182 text{*Re-expresses lists using sum and product*}
   183 lemma list_eq_lfp2: "list(A) = lfp(univ(A), \<lambda>X. {0} + A*X)"
   184 apply (simp add: list_def) 
   185 apply (rule equalityI) 
   186  apply (rule lfp_lowerbound) 
   187   prefer 2 apply (rule lfp_subset)
   188  apply (clarify, subst lfp_unfold [OF list_fun_bnd_mono])
   189  apply (simp add: Nil_def Cons_def)
   190  apply blast 
   191 txt{*Opposite inclusion*}
   192 apply (rule lfp_lowerbound) 
   193  prefer 2 apply (rule lfp_subset) 
   194 apply (clarify, subst lfp_unfold [OF list.bnd_mono]) 
   195 apply (simp add: Nil_def Cons_def)
   196 apply (blast intro: datatype_univs
   197              dest: lfp_subset [THEN subsetD])
   198 done
   199 
   200 text{*Re-expresses lists using "iterates", no univ.*}
   201 lemma list_eq_Union:
   202      "list(A) = (\<Union>n\<in>nat. (\<lambda>X. {0} + A*X) ^ n (0))"
   203 by (simp add: list_eq_lfp2 lfp_eq_Union list_fun_bnd_mono list_fun_contin)
   204 
   205 
   206 constdefs
   207   is_list_functor :: "[i=>o,i,i,i] => o"
   208     "is_list_functor(M,A,X,Z) == 
   209         \<exists>n1[M]. \<exists>AX[M]. 
   210          number1(M,n1) & cartprod(M,A,X,AX) & is_sum(M,n1,AX,Z)"
   211 
   212 lemma (in M_basic) list_functor_abs [simp]: 
   213      "[| M(A); M(X); M(Z) |] ==> is_list_functor(M,A,X,Z) <-> (Z = {0} + A*X)"
   214 by (simp add: is_list_functor_def singleton_0 nat_into_M)
   215 
   216 
   217 subsection {*formulas without univ*}
   218 
   219 lemma formula_fun_bnd_mono:
   220      "bnd_mono(univ(0), \<lambda>X. ((nat*nat) + (nat*nat)) + (X*X + X))"
   221 apply (rule bnd_monoI)
   222  apply (intro subset_refl zero_subset_univ A_subset_univ 
   223 	      sum_subset_univ Sigma_subset_univ nat_subset_univ) 
   224 apply (rule subset_refl sum_mono Sigma_mono | assumption)+
   225 done
   226 
   227 lemma formula_fun_contin:
   228      "contin(\<lambda>X. ((nat*nat) + (nat*nat)) + (X*X + X))"
   229 by (intro sum_contin prod_contin id_contin const_contin) 
   230 
   231 
   232 text{*Re-expresses formulas using sum and product*}
   233 lemma formula_eq_lfp2:
   234     "formula = lfp(univ(0), \<lambda>X. ((nat*nat) + (nat*nat)) + (X*X + X))"
   235 apply (simp add: formula_def) 
   236 apply (rule equalityI) 
   237  apply (rule lfp_lowerbound) 
   238   prefer 2 apply (rule lfp_subset)
   239  apply (clarify, subst lfp_unfold [OF formula_fun_bnd_mono])
   240  apply (simp add: Member_def Equal_def Nand_def Forall_def)
   241  apply blast 
   242 txt{*Opposite inclusion*}
   243 apply (rule lfp_lowerbound) 
   244  prefer 2 apply (rule lfp_subset, clarify) 
   245 apply (subst lfp_unfold [OF formula.bnd_mono, simplified]) 
   246 apply (simp add: Member_def Equal_def Nand_def Forall_def)  
   247 apply (elim sumE SigmaE, simp_all) 
   248 apply (blast intro: datatype_univs dest: lfp_subset [THEN subsetD])+  
   249 done
   250 
   251 text{*Re-expresses formulas using "iterates", no univ.*}
   252 lemma formula_eq_Union:
   253      "formula = 
   254       (\<Union>n\<in>nat. (\<lambda>X. ((nat*nat) + (nat*nat)) + (X*X + X)) ^ n (0))"
   255 by (simp add: formula_eq_lfp2 lfp_eq_Union formula_fun_bnd_mono 
   256               formula_fun_contin)
   257 
   258 
   259 constdefs
   260   is_formula_functor :: "[i=>o,i,i] => o"
   261     "is_formula_functor(M,X,Z) == 
   262         \<exists>nat'[M]. \<exists>natnat[M]. \<exists>natnatsum[M]. \<exists>XX[M]. \<exists>X3[M]. 
   263           omega(M,nat') & cartprod(M,nat',nat',natnat) & 
   264           is_sum(M,natnat,natnat,natnatsum) &
   265           cartprod(M,X,X,XX) & is_sum(M,XX,X,X3) & 
   266           is_sum(M,natnatsum,X3,Z)"
   267 
   268 lemma (in M_basic) formula_functor_abs [simp]: 
   269      "[| M(X); M(Z) |] 
   270       ==> is_formula_functor(M,X,Z) <-> 
   271           Z = ((nat*nat) + (nat*nat)) + (X*X + X)"
   272 by (simp add: is_formula_functor_def) 
   273 
   274 
   275 subsection{*@{term M} Contains the List and Formula Datatypes*}
   276 
   277 constdefs
   278   list_N :: "[i,i] => i"
   279     "list_N(A,n) == (\<lambda>X. {0} + A * X)^n (0)"
   280 
   281 lemma Nil_in_list_N [simp]: "[] \<in> list_N(A,succ(n))"
   282 by (simp add: list_N_def Nil_def)
   283 
   284 lemma Cons_in_list_N [simp]:
   285      "Cons(a,l) \<in> list_N(A,succ(n)) <-> a\<in>A & l \<in> list_N(A,n)"
   286 by (simp add: list_N_def Cons_def) 
   287 
   288 text{*These two aren't simprules because they reveal the underlying
   289 list representation.*}
   290 lemma list_N_0: "list_N(A,0) = 0"
   291 by (simp add: list_N_def)
   292 
   293 lemma list_N_succ: "list_N(A,succ(n)) = {0} + A * (list_N(A,n))"
   294 by (simp add: list_N_def)
   295 
   296 lemma list_N_imp_list:
   297   "[| l \<in> list_N(A,n); n \<in> nat |] ==> l \<in> list(A)"
   298 by (force simp add: list_eq_Union list_N_def)
   299 
   300 lemma list_N_imp_length_lt [rule_format]:
   301      "n \<in> nat ==> \<forall>l \<in> list_N(A,n). length(l) < n"
   302 apply (induct_tac n)  
   303 apply (auto simp add: list_N_0 list_N_succ 
   304                       Nil_def [symmetric] Cons_def [symmetric]) 
   305 done
   306 
   307 lemma list_imp_list_N [rule_format]:
   308      "l \<in> list(A) ==> \<forall>n\<in>nat. length(l) < n --> l \<in> list_N(A, n)"
   309 apply (induct_tac l)
   310 apply (force elim: natE)+
   311 done
   312 
   313 lemma list_N_imp_eq_length:
   314       "[|n \<in> nat; l \<notin> list_N(A, n); l \<in> list_N(A, succ(n))|] 
   315        ==> n = length(l)"
   316 apply (rule le_anti_sym) 
   317  prefer 2 apply (simp add: list_N_imp_length_lt) 
   318 apply (frule list_N_imp_list, simp)
   319 apply (simp add: not_lt_iff_le [symmetric]) 
   320 apply (blast intro: list_imp_list_N) 
   321 done
   322   
   323 text{*Express @{term list_rec} without using @{term rank} or @{term Vset},
   324 neither of which is absolute.*}
   325 lemma (in M_trivial) list_rec_eq:
   326   "l \<in> list(A) ==>
   327    list_rec(a,g,l) = 
   328    transrec (succ(length(l)),
   329       \<lambda>x h. Lambda (list(A),
   330                     list_case' (a, 
   331                            \<lambda>a l. g(a, l, h ` succ(length(l)) ` l)))) ` l"
   332 apply (induct_tac l) 
   333 apply (subst transrec, simp) 
   334 apply (subst transrec) 
   335 apply (simp add: list_imp_list_N) 
   336 done
   337 
   338 constdefs
   339   is_list_N :: "[i=>o,i,i,i] => o"
   340     "is_list_N(M,A,n,Z) == 
   341       \<exists>zero[M]. \<exists>sn[M]. \<exists>msn[M]. 
   342        empty(M,zero) & 
   343        successor(M,n,sn) & membership(M,sn,msn) &
   344        is_wfrec(M, iterates_MH(M, is_list_functor(M,A),zero), msn, n, Z)"
   345   
   346   mem_list :: "[i=>o,i,i] => o"
   347     "mem_list(M,A,l) == 
   348       \<exists>n[M]. \<exists>listn[M]. 
   349        finite_ordinal(M,n) & is_list_N(M,A,n,listn) & l \<in> listn"
   350 
   351   is_list :: "[i=>o,i,i] => o"
   352     "is_list(M,A,Z) == \<forall>l[M]. l \<in> Z <-> mem_list(M,A,l)"
   353 
   354 subsubsection{*Towards Absoluteness of @{term formula_rec}*}
   355 
   356 consts   depth :: "i=>i"
   357 primrec
   358   "depth(Member(x,y)) = 0"
   359   "depth(Equal(x,y))  = 0"
   360   "depth(Nand(p,q)) = succ(depth(p) \<union> depth(q))"
   361   "depth(Forall(p)) = succ(depth(p))"
   362 
   363 lemma depth_type [TC]: "p \<in> formula ==> depth(p) \<in> nat"
   364 by (induct_tac p, simp_all) 
   365 
   366 
   367 constdefs
   368   formula_N :: "i => i"
   369     "formula_N(n) == (\<lambda>X. ((nat*nat) + (nat*nat)) + (X*X + X)) ^ n (0)"
   370 
   371 lemma Member_in_formula_N [simp]:
   372      "Member(x,y) \<in> formula_N(succ(n)) <-> x \<in> nat & y \<in> nat"
   373 by (simp add: formula_N_def Member_def) 
   374 
   375 lemma Equal_in_formula_N [simp]:
   376      "Equal(x,y) \<in> formula_N(succ(n)) <-> x \<in> nat & y \<in> nat"
   377 by (simp add: formula_N_def Equal_def) 
   378 
   379 lemma Nand_in_formula_N [simp]:
   380      "Nand(x,y) \<in> formula_N(succ(n)) <-> x \<in> formula_N(n) & y \<in> formula_N(n)"
   381 by (simp add: formula_N_def Nand_def) 
   382 
   383 lemma Forall_in_formula_N [simp]:
   384      "Forall(x) \<in> formula_N(succ(n)) <-> x \<in> formula_N(n)"
   385 by (simp add: formula_N_def Forall_def) 
   386 
   387 text{*These two aren't simprules because they reveal the underlying
   388 formula representation.*}
   389 lemma formula_N_0: "formula_N(0) = 0"
   390 by (simp add: formula_N_def)
   391 
   392 lemma formula_N_succ:
   393      "formula_N(succ(n)) = 
   394       ((nat*nat) + (nat*nat)) + (formula_N(n) * formula_N(n) + formula_N(n))"
   395 by (simp add: formula_N_def)
   396 
   397 lemma formula_N_imp_formula:
   398   "[| p \<in> formula_N(n); n \<in> nat |] ==> p \<in> formula"
   399 by (force simp add: formula_eq_Union formula_N_def)
   400 
   401 lemma formula_N_imp_depth_lt [rule_format]:
   402      "n \<in> nat ==> \<forall>p \<in> formula_N(n). depth(p) < n"
   403 apply (induct_tac n)  
   404 apply (auto simp add: formula_N_0 formula_N_succ 
   405                       depth_type formula_N_imp_formula Un_least_lt_iff
   406                       Member_def [symmetric] Equal_def [symmetric]
   407                       Nand_def [symmetric] Forall_def [symmetric]) 
   408 done
   409 
   410 lemma formula_imp_formula_N [rule_format]:
   411      "p \<in> formula ==> \<forall>n\<in>nat. depth(p) < n --> p \<in> formula_N(n)"
   412 apply (induct_tac p)
   413 apply (simp_all add: succ_Un_distrib Un_least_lt_iff) 
   414 apply (force elim: natE)+
   415 done
   416 
   417 lemma formula_N_imp_eq_depth:
   418       "[|n \<in> nat; p \<notin> formula_N(n); p \<in> formula_N(succ(n))|] 
   419        ==> n = depth(p)"
   420 apply (rule le_anti_sym) 
   421  prefer 2 apply (simp add: formula_N_imp_depth_lt) 
   422 apply (frule formula_N_imp_formula, simp)
   423 apply (simp add: not_lt_iff_le [symmetric]) 
   424 apply (blast intro: formula_imp_formula_N) 
   425 done
   426 
   427 
   428 
   429 lemma formula_N_mono [rule_format]:
   430   "[| m \<in> nat; n \<in> nat |] ==> m\<le>n --> formula_N(m) \<subseteq> formula_N(n)"
   431 apply (rule_tac m = m and n = n in diff_induct)
   432 apply (simp_all add: formula_N_0 formula_N_succ, blast) 
   433 done
   434 
   435 lemma formula_N_distrib:
   436   "[| m \<in> nat; n \<in> nat |] ==> formula_N(m \<union> n) = formula_N(m) \<union> formula_N(n)"
   437 apply (rule_tac i = m and j = n in Ord_linear_le, auto) 
   438 apply (simp_all add: subset_Un_iff [THEN iffD1] subset_Un_iff2 [THEN iffD1] 
   439                      le_imp_subset formula_N_mono)
   440 done
   441 
   442 constdefs
   443   is_formula_N :: "[i=>o,i,i] => o"
   444     "is_formula_N(M,n,Z) == 
   445       \<exists>zero[M]. \<exists>sn[M]. \<exists>msn[M]. 
   446        empty(M,zero) & 
   447        successor(M,n,sn) & membership(M,sn,msn) &
   448        is_wfrec(M, iterates_MH(M, is_formula_functor(M),zero), msn, n, Z)"
   449   
   450 
   451 constdefs
   452   
   453   mem_formula :: "[i=>o,i] => o"
   454     "mem_formula(M,p) == 
   455       \<exists>n[M]. \<exists>formn[M]. 
   456        finite_ordinal(M,n) & is_formula_N(M,n,formn) & p \<in> formn"
   457 
   458   is_formula :: "[i=>o,i] => o"
   459     "is_formula(M,Z) == \<forall>p[M]. p \<in> Z <-> mem_formula(M,p)"
   460 
   461 locale M_datatypes = M_trancl +
   462  assumes list_replacement1: 
   463    "M(A) ==> iterates_replacement(M, is_list_functor(M,A), 0)"
   464   and list_replacement2: 
   465    "M(A) ==> strong_replacement(M, 
   466          \<lambda>n y. n\<in>nat & 
   467                (\<exists>sn[M]. \<exists>msn[M]. successor(M,n,sn) & membership(M,sn,msn) &
   468                is_wfrec(M, iterates_MH(M,is_list_functor(M,A), 0), 
   469                         msn, n, y)))"
   470   and formula_replacement1: 
   471    "iterates_replacement(M, is_formula_functor(M), 0)"
   472   and formula_replacement2: 
   473    "strong_replacement(M, 
   474          \<lambda>n y. n\<in>nat & 
   475                (\<exists>sn[M]. \<exists>msn[M]. successor(M,n,sn) & membership(M,sn,msn) &
   476                is_wfrec(M, iterates_MH(M,is_formula_functor(M), 0), 
   477                         msn, n, y)))"
   478   and nth_replacement:
   479    "M(l) ==> iterates_replacement(M, %l t. is_tl(M,l,t), l)"
   480         
   481 
   482 subsubsection{*Absoluteness of the List Construction*}
   483 
   484 lemma (in M_datatypes) list_replacement2': 
   485   "M(A) ==> strong_replacement(M, \<lambda>n y. n\<in>nat & y = (\<lambda>X. {0} + A * X)^n (0))"
   486 apply (insert list_replacement2 [of A]) 
   487 apply (rule strong_replacement_cong [THEN iffD1])  
   488 apply (rule conj_cong [OF iff_refl iterates_abs [of "is_list_functor(M,A)"]]) 
   489 apply (simp_all add: list_replacement1 relation1_def) 
   490 done
   491 
   492 lemma (in M_datatypes) list_closed [intro,simp]:
   493      "M(A) ==> M(list(A))"
   494 apply (insert list_replacement1)
   495 by  (simp add: RepFun_closed2 list_eq_Union 
   496                list_replacement2' relation1_def
   497                iterates_closed [of "is_list_functor(M,A)"])
   498 
   499 text{*WARNING: use only with @{text "dest:"} or with variables fixed!*}
   500 lemmas (in M_datatypes) list_into_M = transM [OF _ list_closed]
   501 
   502 lemma (in M_datatypes) list_N_abs [simp]:
   503      "[|M(A); n\<in>nat; M(Z)|] 
   504       ==> is_list_N(M,A,n,Z) <-> Z = list_N(A,n)"
   505 apply (insert list_replacement1)
   506 apply (simp add: is_list_N_def list_N_def relation1_def nat_into_M
   507                  iterates_abs [of "is_list_functor(M,A)" _ "\<lambda>X. {0} + A*X"])
   508 done
   509 
   510 lemma (in M_datatypes) list_N_closed [intro,simp]:
   511      "[|M(A); n\<in>nat|] ==> M(list_N(A,n))"
   512 apply (insert list_replacement1)
   513 apply (simp add: is_list_N_def list_N_def relation1_def nat_into_M
   514                  iterates_closed [of "is_list_functor(M,A)"])
   515 done
   516 
   517 lemma (in M_datatypes) mem_list_abs [simp]:
   518      "M(A) ==> mem_list(M,A,l) <-> l \<in> list(A)"
   519 apply (insert list_replacement1)
   520 apply (simp add: mem_list_def list_N_def relation1_def list_eq_Union
   521                  iterates_closed [of "is_list_functor(M,A)"]) 
   522 done
   523 
   524 lemma (in M_datatypes) list_abs [simp]:
   525      "[|M(A); M(Z)|] ==> is_list(M,A,Z) <-> Z = list(A)"
   526 apply (simp add: is_list_def, safe)
   527 apply (rule M_equalityI, simp_all)
   528 done
   529 
   530 subsubsection{*Absoluteness of Formulas*}
   531 
   532 lemma (in M_datatypes) formula_replacement2': 
   533   "strong_replacement(M, \<lambda>n y. n\<in>nat & y = (\<lambda>X. ((nat*nat) + (nat*nat)) + (X*X + X))^n (0))"
   534 apply (insert formula_replacement2) 
   535 apply (rule strong_replacement_cong [THEN iffD1])  
   536 apply (rule conj_cong [OF iff_refl iterates_abs [of "is_formula_functor(M)"]]) 
   537 apply (simp_all add: formula_replacement1 relation1_def) 
   538 done
   539 
   540 lemma (in M_datatypes) formula_closed [intro,simp]:
   541      "M(formula)"
   542 apply (insert formula_replacement1)
   543 apply  (simp add: RepFun_closed2 formula_eq_Union 
   544                   formula_replacement2' relation1_def
   545                   iterates_closed [of "is_formula_functor(M)"])
   546 done
   547 
   548 lemmas (in M_datatypes) formula_into_M = transM [OF _ formula_closed]
   549 
   550 lemma (in M_datatypes) formula_N_abs [simp]:
   551      "[|n\<in>nat; M(Z)|] 
   552       ==> is_formula_N(M,n,Z) <-> Z = formula_N(n)"
   553 apply (insert formula_replacement1)
   554 apply (simp add: is_formula_N_def formula_N_def relation1_def nat_into_M
   555                  iterates_abs [of "is_formula_functor(M)" _ 
   556                                   "\<lambda>X. ((nat*nat) + (nat*nat)) + (X*X + X)"])
   557 done
   558 
   559 lemma (in M_datatypes) formula_N_closed [intro,simp]:
   560      "n\<in>nat ==> M(formula_N(n))"
   561 apply (insert formula_replacement1)
   562 apply (simp add: is_formula_N_def formula_N_def relation1_def nat_into_M
   563                  iterates_closed [of "is_formula_functor(M)"])
   564 done
   565 
   566 lemma (in M_datatypes) mem_formula_abs [simp]:
   567      "mem_formula(M,l) <-> l \<in> formula"
   568 apply (insert formula_replacement1)
   569 apply (simp add: mem_formula_def relation1_def formula_eq_Union formula_N_def
   570                  iterates_closed [of "is_formula_functor(M)"]) 
   571 done
   572 
   573 lemma (in M_datatypes) formula_abs [simp]:
   574      "[|M(Z)|] ==> is_formula(M,Z) <-> Z = formula"
   575 apply (simp add: is_formula_def, safe)
   576 apply (rule M_equalityI, simp_all)
   577 done
   578 
   579 
   580 subsection{*Absoluteness for Some List Operators*}
   581 
   582 subsection{*Absoluteness for @{text \<epsilon>}-Closure: the @{term eclose} Operator*}
   583 
   584 text{*Re-expresses eclose using "iterates"*}
   585 lemma eclose_eq_Union:
   586      "eclose(A) = (\<Union>n\<in>nat. Union^n (A))"
   587 apply (simp add: eclose_def) 
   588 apply (rule UN_cong) 
   589 apply (rule refl)
   590 apply (induct_tac n)
   591 apply (simp add: nat_rec_0)  
   592 apply (simp add: nat_rec_succ) 
   593 done
   594 
   595 constdefs
   596   is_eclose_n :: "[i=>o,i,i,i] => o"
   597     "is_eclose_n(M,A,n,Z) == 
   598       \<exists>sn[M]. \<exists>msn[M]. 
   599        successor(M,n,sn) & membership(M,sn,msn) &
   600        is_wfrec(M, iterates_MH(M, big_union(M), A), msn, n, Z)"
   601   
   602   mem_eclose :: "[i=>o,i,i] => o"
   603     "mem_eclose(M,A,l) == 
   604       \<exists>n[M]. \<exists>eclosen[M]. 
   605        finite_ordinal(M,n) & is_eclose_n(M,A,n,eclosen) & l \<in> eclosen"
   606 
   607   is_eclose :: "[i=>o,i,i] => o"
   608     "is_eclose(M,A,Z) == \<forall>u[M]. u \<in> Z <-> mem_eclose(M,A,u)"
   609 
   610 
   611 locale M_eclose = M_datatypes +
   612  assumes eclose_replacement1: 
   613    "M(A) ==> iterates_replacement(M, big_union(M), A)"
   614   and eclose_replacement2: 
   615    "M(A) ==> strong_replacement(M, 
   616          \<lambda>n y. n\<in>nat & 
   617                (\<exists>sn[M]. \<exists>msn[M]. successor(M,n,sn) & membership(M,sn,msn) &
   618                is_wfrec(M, iterates_MH(M,big_union(M), A), 
   619                         msn, n, y)))"
   620 
   621 lemma (in M_eclose) eclose_replacement2': 
   622   "M(A) ==> strong_replacement(M, \<lambda>n y. n\<in>nat & y = Union^n (A))"
   623 apply (insert eclose_replacement2 [of A]) 
   624 apply (rule strong_replacement_cong [THEN iffD1])  
   625 apply (rule conj_cong [OF iff_refl iterates_abs [of "big_union(M)"]]) 
   626 apply (simp_all add: eclose_replacement1 relation1_def) 
   627 done
   628 
   629 lemma (in M_eclose) eclose_closed [intro,simp]:
   630      "M(A) ==> M(eclose(A))"
   631 apply (insert eclose_replacement1)
   632 by  (simp add: RepFun_closed2 eclose_eq_Union 
   633                eclose_replacement2' relation1_def
   634                iterates_closed [of "big_union(M)"])
   635 
   636 lemma (in M_eclose) is_eclose_n_abs [simp]:
   637      "[|M(A); n\<in>nat; M(Z)|] ==> is_eclose_n(M,A,n,Z) <-> Z = Union^n (A)"
   638 apply (insert eclose_replacement1)
   639 apply (simp add: is_eclose_n_def relation1_def nat_into_M
   640                  iterates_abs [of "big_union(M)" _ "Union"])
   641 done
   642 
   643 lemma (in M_eclose) mem_eclose_abs [simp]:
   644      "M(A) ==> mem_eclose(M,A,l) <-> l \<in> eclose(A)"
   645 apply (insert eclose_replacement1)
   646 apply (simp add: mem_eclose_def relation1_def eclose_eq_Union
   647                  iterates_closed [of "big_union(M)"]) 
   648 done
   649 
   650 lemma (in M_eclose) eclose_abs [simp]:
   651      "[|M(A); M(Z)|] ==> is_eclose(M,A,Z) <-> Z = eclose(A)"
   652 apply (simp add: is_eclose_def, safe)
   653 apply (rule M_equalityI, simp_all)
   654 done
   655 
   656 
   657 
   658 
   659 subsection {*Absoluteness for @{term transrec}*}
   660 
   661 
   662 text{* @{term "transrec(a,H) \<equiv> wfrec(Memrel(eclose({a})), a, H)"} *}
   663 constdefs
   664 
   665   is_transrec :: "[i=>o, [i,i,i]=>o, i, i] => o"
   666    "is_transrec(M,MH,a,z) == 
   667       \<exists>sa[M]. \<exists>esa[M]. \<exists>mesa[M]. 
   668        upair(M,a,a,sa) & is_eclose(M,sa,esa) & membership(M,esa,mesa) &
   669        is_wfrec(M,MH,mesa,a,z)"
   670 
   671   transrec_replacement :: "[i=>o, [i,i,i]=>o, i] => o"
   672    "transrec_replacement(M,MH,a) ==
   673       \<exists>sa[M]. \<exists>esa[M]. \<exists>mesa[M]. 
   674        upair(M,a,a,sa) & is_eclose(M,sa,esa) & membership(M,esa,mesa) &
   675        wfrec_replacement(M,MH,mesa)"
   676 
   677 text{*The condition @{term "Ord(i)"} lets us use the simpler 
   678   @{text "trans_wfrec_abs"} rather than @{text "trans_wfrec_abs"},
   679   which I haven't even proved yet. *}
   680 theorem (in M_eclose) transrec_abs:
   681   "[|transrec_replacement(M,MH,i);  relation2(M,MH,H);
   682      Ord(i);  M(i);  M(z);
   683      \<forall>x[M]. \<forall>g[M]. function(g) --> M(H(x,g))|] 
   684    ==> is_transrec(M,MH,i,z) <-> z = transrec(i,H)" 
   685 by (simp add: trans_wfrec_abs transrec_replacement_def is_transrec_def
   686        transrec_def eclose_sing_Ord_eq wf_Memrel trans_Memrel relation_Memrel)
   687 
   688 
   689 theorem (in M_eclose) transrec_closed:
   690      "[|transrec_replacement(M,MH,i);  relation2(M,MH,H);
   691 	Ord(i);  M(i);  
   692 	\<forall>x[M]. \<forall>g[M]. function(g) --> M(H(x,g))|] 
   693       ==> M(transrec(i,H))"
   694 by (simp add: trans_wfrec_closed transrec_replacement_def is_transrec_def
   695         transrec_def eclose_sing_Ord_eq wf_Memrel trans_Memrel relation_Memrel)
   696 
   697 
   698 text{*Helps to prove instances of @{term transrec_replacement}*}
   699 lemma (in M_eclose) transrec_replacementI: 
   700    "[|M(a);
   701     strong_replacement (M, 
   702                   \<lambda>x z. \<exists>y[M]. pair(M, x, y, z) \<and> is_wfrec(M,MH,Memrel(eclose({a})),x,y))|]
   703     ==> transrec_replacement(M,MH,a)"
   704 by (simp add: transrec_replacement_def wfrec_replacement_def) 
   705 
   706 
   707 subsection{*Absoluteness for the List Operator @{term length}*}
   708 constdefs
   709 
   710   is_length :: "[i=>o,i,i,i] => o"
   711     "is_length(M,A,l,n) == 
   712        \<exists>sn[M]. \<exists>list_n[M]. \<exists>list_sn[M]. 
   713         is_list_N(M,A,n,list_n) & l \<notin> list_n &
   714         successor(M,n,sn) & is_list_N(M,A,sn,list_sn) & l \<in> list_sn"
   715 
   716 
   717 lemma (in M_datatypes) length_abs [simp]:
   718      "[|M(A); l \<in> list(A); n \<in> nat|] ==> is_length(M,A,l,n) <-> n = length(l)"
   719 apply (subgoal_tac "M(l) & M(n)")
   720  prefer 2 apply (blast dest: transM)  
   721 apply (simp add: is_length_def)
   722 apply (blast intro: list_imp_list_N nat_into_Ord list_N_imp_eq_length
   723              dest: list_N_imp_length_lt)
   724 done
   725 
   726 text{*Proof is trivial since @{term length} returns natural numbers.*}
   727 lemma (in M_trivial) length_closed [intro,simp]:
   728      "l \<in> list(A) ==> M(length(l))"
   729 by (simp add: nat_into_M) 
   730 
   731 
   732 subsection {*Absoluteness for @{term nth}*}
   733 
   734 lemma nth_eq_hd_iterates_tl [rule_format]:
   735      "xs \<in> list(A) ==> \<forall>n \<in> nat. nth(n,xs) = hd' (tl'^n (xs))"
   736 apply (induct_tac xs) 
   737 apply (simp add: iterates_tl_Nil hd'_Nil, clarify) 
   738 apply (erule natE)
   739 apply (simp add: hd'_Cons) 
   740 apply (simp add: tl'_Cons iterates_commute) 
   741 done
   742 
   743 lemma (in M_basic) iterates_tl'_closed:
   744      "[|n \<in> nat; M(x)|] ==> M(tl'^n (x))"
   745 apply (induct_tac n, simp) 
   746 apply (simp add: tl'_Cons tl'_closed) 
   747 done
   748 
   749 text{*Immediate by type-checking*}
   750 lemma (in M_datatypes) nth_closed [intro,simp]:
   751      "[|xs \<in> list(A); n \<in> nat; M(A)|] ==> M(nth(n,xs))" 
   752 apply (case_tac "n < length(xs)")
   753  apply (blast intro: nth_type transM)
   754 apply (simp add: not_lt_iff_le nth_eq_0)
   755 done
   756 
   757 constdefs
   758   is_nth :: "[i=>o,i,i,i] => o"
   759     "is_nth(M,n,l,Z) == 
   760       \<exists>X[M]. \<exists>sn[M]. \<exists>msn[M]. 
   761        successor(M,n,sn) & membership(M,sn,msn) &
   762        is_wfrec(M, iterates_MH(M, is_tl(M), l), msn, n, X) &
   763        is_hd(M,X,Z)"
   764  
   765 lemma (in M_datatypes) nth_abs [simp]:
   766      "[|M(A); n \<in> nat; l \<in> list(A); M(Z)|] 
   767       ==> is_nth(M,n,l,Z) <-> Z = nth(n,l)"
   768 apply (subgoal_tac "M(l)") 
   769  prefer 2 apply (blast intro: transM)
   770 apply (simp add: is_nth_def nth_eq_hd_iterates_tl nat_into_M
   771                  tl'_closed iterates_tl'_closed 
   772                  iterates_abs [OF _ relation1_tl] nth_replacement)
   773 done
   774 
   775 
   776 subsection{*Relativization and Absoluteness for the @{term formula} Constructors*}
   777 
   778 constdefs
   779   is_Member :: "[i=>o,i,i,i] => o"
   780      --{* because @{term "Member(x,y) \<equiv> Inl(Inl(\<langle>x,y\<rangle>))"}*}
   781     "is_Member(M,x,y,Z) ==
   782 	\<exists>p[M]. \<exists>u[M]. pair(M,x,y,p) & is_Inl(M,p,u) & is_Inl(M,u,Z)"
   783 
   784 lemma (in M_trivial) Member_abs [simp]:
   785      "[|M(x); M(y); M(Z)|] ==> is_Member(M,x,y,Z) <-> (Z = Member(x,y))"
   786 by (simp add: is_Member_def Member_def)
   787 
   788 lemma (in M_trivial) Member_in_M_iff [iff]:
   789      "M(Member(x,y)) <-> M(x) & M(y)"
   790 by (simp add: Member_def) 
   791 
   792 constdefs
   793   is_Equal :: "[i=>o,i,i,i] => o"
   794      --{* because @{term "Equal(x,y) \<equiv> Inl(Inr(\<langle>x,y\<rangle>))"}*}
   795     "is_Equal(M,x,y,Z) ==
   796 	\<exists>p[M]. \<exists>u[M]. pair(M,x,y,p) & is_Inr(M,p,u) & is_Inl(M,u,Z)"
   797 
   798 lemma (in M_trivial) Equal_abs [simp]:
   799      "[|M(x); M(y); M(Z)|] ==> is_Equal(M,x,y,Z) <-> (Z = Equal(x,y))"
   800 by (simp add: is_Equal_def Equal_def)
   801 
   802 lemma (in M_trivial) Equal_in_M_iff [iff]: "M(Equal(x,y)) <-> M(x) & M(y)"
   803 by (simp add: Equal_def) 
   804 
   805 constdefs
   806   is_Nand :: "[i=>o,i,i,i] => o"
   807      --{* because @{term "Nand(x,y) \<equiv> Inr(Inl(\<langle>x,y\<rangle>))"}*}
   808     "is_Nand(M,x,y,Z) ==
   809 	\<exists>p[M]. \<exists>u[M]. pair(M,x,y,p) & is_Inl(M,p,u) & is_Inr(M,u,Z)"
   810 
   811 lemma (in M_trivial) Nand_abs [simp]:
   812      "[|M(x); M(y); M(Z)|] ==> is_Nand(M,x,y,Z) <-> (Z = Nand(x,y))"
   813 by (simp add: is_Nand_def Nand_def)
   814 
   815 lemma (in M_trivial) Nand_in_M_iff [iff]: "M(Nand(x,y)) <-> M(x) & M(y)"
   816 by (simp add: Nand_def) 
   817 
   818 constdefs
   819   is_Forall :: "[i=>o,i,i] => o"
   820      --{* because @{term "Forall(x) \<equiv> Inr(Inr(p))"}*}
   821     "is_Forall(M,p,Z) == \<exists>u[M]. is_Inr(M,p,u) & is_Inr(M,u,Z)"
   822 
   823 lemma (in M_trivial) Forall_abs [simp]:
   824      "[|M(x); M(Z)|] ==> is_Forall(M,x,Z) <-> (Z = Forall(x))"
   825 by (simp add: is_Forall_def Forall_def)
   826 
   827 lemma (in M_trivial) Forall_in_M_iff [iff]: "M(Forall(x)) <-> M(x)"
   828 by (simp add: Forall_def)
   829 
   830 
   831 subsection {*Absoluteness for @{term formula_rec}*}
   832 
   833 subsubsection{*@{term is_formula_case}: relativization of @{term formula_case}*}
   834 
   835 constdefs
   836 
   837  is_formula_case :: 
   838     "[i=>o, [i,i,i]=>o, [i,i,i]=>o, [i,i,i]=>o, [i,i]=>o, i, i] => o"
   839   --{*no constraint on non-formulas*}
   840   "is_formula_case(M, is_a, is_b, is_c, is_d, p, z) == 
   841       (\<forall>x[M]. \<forall>y[M]. finite_ordinal(M,x) --> finite_ordinal(M,y) --> 
   842                       is_Member(M,x,y,p) --> is_a(x,y,z)) &
   843       (\<forall>x[M]. \<forall>y[M]. finite_ordinal(M,x) --> finite_ordinal(M,y) --> 
   844                       is_Equal(M,x,y,p) --> is_b(x,y,z)) &
   845       (\<forall>x[M]. \<forall>y[M]. mem_formula(M,x) --> mem_formula(M,y) --> 
   846                      is_Nand(M,x,y,p) --> is_c(x,y,z)) &
   847       (\<forall>x[M]. mem_formula(M,x) --> is_Forall(M,x,p) --> is_d(x,z))"
   848 
   849 lemma (in M_datatypes) formula_case_abs [simp]: 
   850      "[| Relation2(M,nat,nat,is_a,a); Relation2(M,nat,nat,is_b,b); 
   851          Relation2(M,formula,formula,is_c,c); Relation1(M,formula,is_d,d); 
   852          p \<in> formula; M(z) |] 
   853       ==> is_formula_case(M,is_a,is_b,is_c,is_d,p,z) <-> 
   854           z = formula_case(a,b,c,d,p)"
   855 apply (simp add: formula_into_M is_formula_case_def)
   856 apply (erule formula.cases) 
   857    apply (simp_all add: Relation1_def Relation2_def) 
   858 done
   859 
   860 lemma (in M_datatypes) formula_case_closed [intro,simp]:
   861   "[|p \<in> formula; 
   862      \<forall>x[M]. \<forall>y[M]. x\<in>nat --> y\<in>nat --> M(a(x,y)); 
   863      \<forall>x[M]. \<forall>y[M]. x\<in>nat --> y\<in>nat --> M(b(x,y)); 
   864      \<forall>x[M]. \<forall>y[M]. x\<in>formula --> y\<in>formula --> M(c(x,y)); 
   865      \<forall>x[M]. x\<in>formula --> M(d(x))|] ==> M(formula_case(a,b,c,d,p))"
   866 by (erule formula.cases, simp_all) 
   867 
   868 
   869 subsection{*Absoluteness for the Formula Operator @{term depth}*}
   870 constdefs
   871 
   872   is_depth :: "[i=>o,i,i] => o"
   873     "is_depth(M,p,n) == 
   874        \<exists>sn[M]. \<exists>formula_n[M]. \<exists>formula_sn[M]. 
   875         is_formula_N(M,n,formula_n) & p \<notin> formula_n &
   876         successor(M,n,sn) & is_formula_N(M,sn,formula_sn) & p \<in> formula_sn"
   877 
   878 
   879 lemma (in M_datatypes) depth_abs [simp]:
   880      "[|p \<in> formula; n \<in> nat|] ==> is_depth(M,p,n) <-> n = depth(p)"
   881 apply (subgoal_tac "M(p) & M(n)")
   882  prefer 2 apply (blast dest: transM)  
   883 apply (simp add: is_depth_def)
   884 apply (blast intro: formula_imp_formula_N nat_into_Ord formula_N_imp_eq_depth
   885              dest: formula_N_imp_depth_lt)
   886 done
   887 
   888 text{*Proof is trivial since @{term depth} returns natural numbers.*}
   889 lemma (in M_trivial) depth_closed [intro,simp]:
   890      "p \<in> formula ==> M(depth(p))"
   891 by (simp add: nat_into_M) 
   892 
   893 
   894 subsection {*Absoluteness for @{term formula_rec}*}
   895 
   896 constdefs
   897 
   898   formula_rec_case :: "[[i,i]=>i, [i,i]=>i, [i,i,i,i]=>i, [i,i]=>i, i, i] => i"
   899     --{* the instance of @{term formula_case} in @{term formula_rec}*}
   900    "formula_rec_case(a,b,c,d,h) ==
   901         formula_case (a, b,
   902                 \<lambda>u v. c(u, v, h ` succ(depth(u)) ` u, 
   903                               h ` succ(depth(v)) ` v),
   904                 \<lambda>u. d(u, h ` succ(depth(u)) ` u))"
   905 
   906   is_formula_rec :: "[i=>o, [i,i,i]=>o, i, i] => o"
   907     --{* predicate to relativize the functional @{term formula_rec}*}
   908    "is_formula_rec(M,MH,p,z)  ==
   909       \<exists>dp[M]. \<exists>i[M]. \<exists>f[M]. finite_ordinal(M,dp) & is_depth(M,p,dp) & 
   910              successor(M,dp,i) & fun_apply(M,f,p,z) & is_transrec(M,MH,i,f)"
   911 
   912 text{*Unfold @{term formula_rec} to @{term formula_rec_case}.
   913      Express @{term formula_rec} without using @{term rank} or @{term Vset},
   914 neither of which is absolute.*}
   915 lemma (in M_trivial) formula_rec_eq:
   916   "p \<in> formula ==>
   917    formula_rec(a,b,c,d,p) = 
   918    transrec (succ(depth(p)),
   919              \<lambda>x h. Lambda (formula, formula_rec_case(a,b,c,d,h))) ` p"
   920 apply (simp add: formula_rec_case_def)
   921 apply (induct_tac p)
   922    txt{*Base case for @{term Member}*}
   923    apply (subst transrec, simp add: formula.intros) 
   924   txt{*Base case for @{term Equal}*}
   925   apply (subst transrec, simp add: formula.intros)
   926  txt{*Inductive step for @{term Nand}*}
   927  apply (subst transrec) 
   928  apply (simp add: succ_Un_distrib formula.intros)
   929 txt{*Inductive step for @{term Forall}*}
   930 apply (subst transrec) 
   931 apply (simp add: formula_imp_formula_N formula.intros) 
   932 done
   933 
   934 
   935 text{*Sufficient conditions to relative the instance of @{term formula_case}
   936       in @{term formula_rec}*}
   937 lemma (in M_datatypes) Relation1_formula_rec_case:
   938      "[|Relation2(M, nat, nat, is_a, a);
   939         Relation2(M, nat, nat, is_b, b);
   940         Relation2 (M, formula, formula, 
   941            is_c, \<lambda>u v. c(u, v, h`succ(depth(u))`u, h`succ(depth(v))`v));
   942         Relation1(M, formula, 
   943            is_d, \<lambda>u. d(u, h ` succ(depth(u)) ` u));
   944  	M(h) |] 
   945       ==> Relation1(M, formula,
   946                          is_formula_case (M, is_a, is_b, is_c, is_d),
   947                          formula_rec_case(a, b, c, d, h))"
   948 apply (simp (no_asm) add: formula_rec_case_def Relation1_def) 
   949 apply (simp add: formula_case_abs) 
   950 done
   951 
   952 
   953 text{*This locale packages the premises of the following theorems,
   954       which is the normal purpose of locales.  It doesn't accumulate
   955       constraints on the class @{term M}, as in most of this deveopment.*}
   956 locale Formula_Rec = M_eclose +
   957   fixes a and is_a and b and is_b and c and is_c and d and is_d and MH
   958   defines
   959       "MH(u::i,f,z) ==
   960 	\<forall>fml[M]. is_formula(M,fml) -->
   961              is_lambda
   962 	 (M, fml, is_formula_case (M, is_a, is_b, is_c(f), is_d(f)), z)"
   963 
   964   assumes a_closed: "[|x\<in>nat; y\<in>nat|] ==> M(a(x,y))"
   965       and a_rel:    "Relation2(M, nat, nat, is_a, a)"
   966       and b_closed: "[|x\<in>nat; y\<in>nat|] ==> M(b(x,y))"
   967       and b_rel:    "Relation2(M, nat, nat, is_b, b)"
   968       and c_closed: "[|x \<in> formula; y \<in> formula; M(gx); M(gy)|]
   969                      ==> M(c(x, y, gx, gy))"
   970       and c_rel:
   971          "M(f) ==> 
   972           Relation2 (M, formula, formula, is_c(f),
   973              \<lambda>u v. c(u, v, f ` succ(depth(u)) ` u, f ` succ(depth(v)) ` v))"
   974       and d_closed: "[|x \<in> formula; M(gx)|] ==> M(d(x, gx))"
   975       and d_rel:
   976          "M(f) ==> 
   977           Relation1(M, formula, is_d(f), \<lambda>u. d(u, f ` succ(depth(u)) ` u))"
   978       and fr_replace: "n \<in> nat ==> transrec_replacement(M,MH,n)"
   979       and fr_lam_replace:
   980            "M(g) ==>
   981             strong_replacement
   982 	      (M, \<lambda>x y. x \<in> formula &
   983 		  y = \<langle>x, formula_rec_case(a,b,c,d,g,x)\<rangle>)";
   984 
   985 lemma (in Formula_Rec) formula_rec_case_closed:
   986     "[|M(g); p \<in> formula|] ==> M(formula_rec_case(a, b, c, d, g, p))"
   987 by (simp add: formula_rec_case_def a_closed b_closed c_closed d_closed) 
   988 
   989 lemma (in Formula_Rec) formula_rec_lam_closed:
   990     "M(g) ==> M(Lambda (formula, formula_rec_case(a,b,c,d,g)))"
   991 by (simp add: lam_closed2 fr_lam_replace formula_rec_case_closed)
   992 
   993 lemma (in Formula_Rec) MH_rel2:
   994      "relation2 (M, MH,
   995              \<lambda>x h. Lambda (formula, formula_rec_case(a,b,c,d,h)))"
   996 apply (simp add: relation2_def MH_def, clarify) 
   997 apply (rule lambda_abs2) 
   998 apply (rule fr_lam_replace, assumption)
   999 apply (rule Relation1_formula_rec_case)  
  1000 apply (simp_all add: a_rel b_rel c_rel d_rel formula_rec_case_closed) 
  1001 done
  1002 
  1003 lemma (in Formula_Rec) fr_transrec_closed:
  1004     "n \<in> nat
  1005      ==> M(transrec
  1006           (n, \<lambda>x h. Lambda(formula, formula_rec_case(a, b, c, d, h))))"
  1007 by (simp add: transrec_closed [OF fr_replace MH_rel2]  
  1008               nat_into_M formula_rec_lam_closed) 
  1009 
  1010 text{*The main two results: @{term formula_rec} is absolute for @{term M}.*}
  1011 theorem (in Formula_Rec) formula_rec_closed:
  1012     "p \<in> formula ==> M(formula_rec(a,b,c,d,p))"
  1013 by (simp add: formula_rec_eq fr_transrec_closed 
  1014               transM [OF _ formula_closed])
  1015 
  1016 theorem (in Formula_Rec) formula_rec_abs:
  1017   "[| p \<in> formula; M(z)|] 
  1018    ==> is_formula_rec(M,MH,p,z) <-> z = formula_rec(a,b,c,d,p)" 
  1019 by (simp add: is_formula_rec_def formula_rec_eq transM [OF _ formula_closed]
  1020               transrec_abs [OF fr_replace MH_rel2] depth_type
  1021               fr_transrec_closed formula_rec_lam_closed eq_commute)
  1022 
  1023 
  1024 end