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