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