src/ZF/Constructible/Internalize.thy
 author wenzelm Thu Dec 14 11:24:26 2017 +0100 (21 months ago) changeset 67198 694f29a5433b parent 61798 27f3c10b0b50 child 69593 3dda49e08b9d permissions -rw-r--r--
merged
```     1 (*  Title:      ZF/Constructible/Internalize.thy
```
```     2     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
```
```     3 *)
```
```     4
```
```     5 theory Internalize imports L_axioms Datatype_absolute begin
```
```     6
```
```     7 subsection\<open>Internalized Forms of Data Structuring Operators\<close>
```
```     8
```
```     9 subsubsection\<open>The Formula @{term is_Inl}, Internalized\<close>
```
```    10
```
```    11 (*  is_Inl(M,a,z) == \<exists>zero[M]. empty(M,zero) & pair(M,zero,a,z) *)
```
```    12 definition
```
```    13   Inl_fm :: "[i,i]=>i" where
```
```    14     "Inl_fm(a,z) == Exists(And(empty_fm(0), pair_fm(0,succ(a),succ(z))))"
```
```    15
```
```    16 lemma Inl_type [TC]:
```
```    17      "[| x \<in> nat; z \<in> nat |] ==> Inl_fm(x,z) \<in> formula"
```
```    18 by (simp add: Inl_fm_def)
```
```    19
```
```    20 lemma sats_Inl_fm [simp]:
```
```    21    "[| x \<in> nat; z \<in> nat; env \<in> list(A)|]
```
```    22     ==> sats(A, Inl_fm(x,z), env) \<longleftrightarrow> is_Inl(##A, nth(x,env), nth(z,env))"
```
```    23 by (simp add: Inl_fm_def is_Inl_def)
```
```    24
```
```    25 lemma Inl_iff_sats:
```
```    26       "[| nth(i,env) = x; nth(k,env) = z;
```
```    27           i \<in> nat; k \<in> nat; env \<in> list(A)|]
```
```    28        ==> is_Inl(##A, x, z) \<longleftrightarrow> sats(A, Inl_fm(i,k), env)"
```
```    29 by simp
```
```    30
```
```    31 theorem Inl_reflection:
```
```    32      "REFLECTS[\<lambda>x. is_Inl(L,f(x),h(x)),
```
```    33                \<lambda>i x. is_Inl(##Lset(i),f(x),h(x))]"
```
```    34 apply (simp only: is_Inl_def)
```
```    35 apply (intro FOL_reflections function_reflections)
```
```    36 done
```
```    37
```
```    38
```
```    39 subsubsection\<open>The Formula @{term is_Inr}, Internalized\<close>
```
```    40
```
```    41 (*  is_Inr(M,a,z) == \<exists>n1[M]. number1(M,n1) & pair(M,n1,a,z) *)
```
```    42 definition
```
```    43   Inr_fm :: "[i,i]=>i" where
```
```    44     "Inr_fm(a,z) == Exists(And(number1_fm(0), pair_fm(0,succ(a),succ(z))))"
```
```    45
```
```    46 lemma Inr_type [TC]:
```
```    47      "[| x \<in> nat; z \<in> nat |] ==> Inr_fm(x,z) \<in> formula"
```
```    48 by (simp add: Inr_fm_def)
```
```    49
```
```    50 lemma sats_Inr_fm [simp]:
```
```    51    "[| x \<in> nat; z \<in> nat; env \<in> list(A)|]
```
```    52     ==> sats(A, Inr_fm(x,z), env) \<longleftrightarrow> is_Inr(##A, nth(x,env), nth(z,env))"
```
```    53 by (simp add: Inr_fm_def is_Inr_def)
```
```    54
```
```    55 lemma Inr_iff_sats:
```
```    56       "[| nth(i,env) = x; nth(k,env) = z;
```
```    57           i \<in> nat; k \<in> nat; env \<in> list(A)|]
```
```    58        ==> is_Inr(##A, x, z) \<longleftrightarrow> sats(A, Inr_fm(i,k), env)"
```
```    59 by simp
```
```    60
```
```    61 theorem Inr_reflection:
```
```    62      "REFLECTS[\<lambda>x. is_Inr(L,f(x),h(x)),
```
```    63                \<lambda>i x. is_Inr(##Lset(i),f(x),h(x))]"
```
```    64 apply (simp only: is_Inr_def)
```
```    65 apply (intro FOL_reflections function_reflections)
```
```    66 done
```
```    67
```
```    68
```
```    69 subsubsection\<open>The Formula @{term is_Nil}, Internalized\<close>
```
```    70
```
```    71 (* is_Nil(M,xs) == \<exists>zero[M]. empty(M,zero) & is_Inl(M,zero,xs) *)
```
```    72
```
```    73 definition
```
```    74   Nil_fm :: "i=>i" where
```
```    75     "Nil_fm(x) == Exists(And(empty_fm(0), Inl_fm(0,succ(x))))"
```
```    76
```
```    77 lemma Nil_type [TC]: "x \<in> nat ==> Nil_fm(x) \<in> formula"
```
```    78 by (simp add: Nil_fm_def)
```
```    79
```
```    80 lemma sats_Nil_fm [simp]:
```
```    81    "[| x \<in> nat; env \<in> list(A)|]
```
```    82     ==> sats(A, Nil_fm(x), env) \<longleftrightarrow> is_Nil(##A, nth(x,env))"
```
```    83 by (simp add: Nil_fm_def is_Nil_def)
```
```    84
```
```    85 lemma Nil_iff_sats:
```
```    86       "[| nth(i,env) = x; i \<in> nat; env \<in> list(A)|]
```
```    87        ==> is_Nil(##A, x) \<longleftrightarrow> sats(A, Nil_fm(i), env)"
```
```    88 by simp
```
```    89
```
```    90 theorem Nil_reflection:
```
```    91      "REFLECTS[\<lambda>x. is_Nil(L,f(x)),
```
```    92                \<lambda>i x. is_Nil(##Lset(i),f(x))]"
```
```    93 apply (simp only: is_Nil_def)
```
```    94 apply (intro FOL_reflections function_reflections Inl_reflection)
```
```    95 done
```
```    96
```
```    97
```
```    98 subsubsection\<open>The Formula @{term is_Cons}, Internalized\<close>
```
```    99
```
```   100
```
```   101 (*  "is_Cons(M,a,l,Z) == \<exists>p[M]. pair(M,a,l,p) & is_Inr(M,p,Z)" *)
```
```   102 definition
```
```   103   Cons_fm :: "[i,i,i]=>i" where
```
```   104     "Cons_fm(a,l,Z) ==
```
```   105        Exists(And(pair_fm(succ(a),succ(l),0), Inr_fm(0,succ(Z))))"
```
```   106
```
```   107 lemma Cons_type [TC]:
```
```   108      "[| x \<in> nat; y \<in> nat; z \<in> nat |] ==> Cons_fm(x,y,z) \<in> formula"
```
```   109 by (simp add: Cons_fm_def)
```
```   110
```
```   111 lemma sats_Cons_fm [simp]:
```
```   112    "[| x \<in> nat; y \<in> nat; z \<in> nat; env \<in> list(A)|]
```
```   113     ==> sats(A, Cons_fm(x,y,z), env) \<longleftrightarrow>
```
```   114        is_Cons(##A, nth(x,env), nth(y,env), nth(z,env))"
```
```   115 by (simp add: Cons_fm_def is_Cons_def)
```
```   116
```
```   117 lemma Cons_iff_sats:
```
```   118       "[| nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
```
```   119           i \<in> nat; j \<in> nat; k \<in> nat; env \<in> list(A)|]
```
```   120        ==>is_Cons(##A, x, y, z) \<longleftrightarrow> sats(A, Cons_fm(i,j,k), env)"
```
```   121 by simp
```
```   122
```
```   123 theorem Cons_reflection:
```
```   124      "REFLECTS[\<lambda>x. is_Cons(L,f(x),g(x),h(x)),
```
```   125                \<lambda>i x. is_Cons(##Lset(i),f(x),g(x),h(x))]"
```
```   126 apply (simp only: is_Cons_def)
```
```   127 apply (intro FOL_reflections pair_reflection Inr_reflection)
```
```   128 done
```
```   129
```
```   130 subsubsection\<open>The Formula @{term is_quasilist}, Internalized\<close>
```
```   131
```
```   132 (* is_quasilist(M,xs) == is_Nil(M,z) | (\<exists>x[M]. \<exists>l[M]. is_Cons(M,x,l,z))" *)
```
```   133
```
```   134 definition
```
```   135   quasilist_fm :: "i=>i" where
```
```   136     "quasilist_fm(x) ==
```
```   137        Or(Nil_fm(x), Exists(Exists(Cons_fm(1,0,succ(succ(x))))))"
```
```   138
```
```   139 lemma quasilist_type [TC]: "x \<in> nat ==> quasilist_fm(x) \<in> formula"
```
```   140 by (simp add: quasilist_fm_def)
```
```   141
```
```   142 lemma sats_quasilist_fm [simp]:
```
```   143    "[| x \<in> nat; env \<in> list(A)|]
```
```   144     ==> sats(A, quasilist_fm(x), env) \<longleftrightarrow> is_quasilist(##A, nth(x,env))"
```
```   145 by (simp add: quasilist_fm_def is_quasilist_def)
```
```   146
```
```   147 lemma quasilist_iff_sats:
```
```   148       "[| nth(i,env) = x; i \<in> nat; env \<in> list(A)|]
```
```   149        ==> is_quasilist(##A, x) \<longleftrightarrow> sats(A, quasilist_fm(i), env)"
```
```   150 by simp
```
```   151
```
```   152 theorem quasilist_reflection:
```
```   153      "REFLECTS[\<lambda>x. is_quasilist(L,f(x)),
```
```   154                \<lambda>i x. is_quasilist(##Lset(i),f(x))]"
```
```   155 apply (simp only: is_quasilist_def)
```
```   156 apply (intro FOL_reflections Nil_reflection Cons_reflection)
```
```   157 done
```
```   158
```
```   159
```
```   160 subsection\<open>Absoluteness for the Function @{term nth}\<close>
```
```   161
```
```   162
```
```   163 subsubsection\<open>The Formula @{term is_hd}, Internalized\<close>
```
```   164
```
```   165 (*   "is_hd(M,xs,H) ==
```
```   166        (is_Nil(M,xs) \<longrightarrow> empty(M,H)) &
```
```   167        (\<forall>x[M]. \<forall>l[M]. ~ is_Cons(M,x,l,xs) | H=x) &
```
```   168        (is_quasilist(M,xs) | empty(M,H))" *)
```
```   169 definition
```
```   170   hd_fm :: "[i,i]=>i" where
```
```   171     "hd_fm(xs,H) ==
```
```   172        And(Implies(Nil_fm(xs), empty_fm(H)),
```
```   173            And(Forall(Forall(Or(Neg(Cons_fm(1,0,xs#+2)), Equal(H#+2,1)))),
```
```   174                Or(quasilist_fm(xs), empty_fm(H))))"
```
```   175
```
```   176 lemma hd_type [TC]:
```
```   177      "[| x \<in> nat; y \<in> nat |] ==> hd_fm(x,y) \<in> formula"
```
```   178 by (simp add: hd_fm_def)
```
```   179
```
```   180 lemma sats_hd_fm [simp]:
```
```   181    "[| x \<in> nat; y \<in> nat; env \<in> list(A)|]
```
```   182     ==> sats(A, hd_fm(x,y), env) \<longleftrightarrow> is_hd(##A, nth(x,env), nth(y,env))"
```
```   183 by (simp add: hd_fm_def is_hd_def)
```
```   184
```
```   185 lemma hd_iff_sats:
```
```   186       "[| nth(i,env) = x; nth(j,env) = y;
```
```   187           i \<in> nat; j \<in> nat; env \<in> list(A)|]
```
```   188        ==> is_hd(##A, x, y) \<longleftrightarrow> sats(A, hd_fm(i,j), env)"
```
```   189 by simp
```
```   190
```
```   191 theorem hd_reflection:
```
```   192      "REFLECTS[\<lambda>x. is_hd(L,f(x),g(x)),
```
```   193                \<lambda>i x. is_hd(##Lset(i),f(x),g(x))]"
```
```   194 apply (simp only: is_hd_def)
```
```   195 apply (intro FOL_reflections Nil_reflection Cons_reflection
```
```   196              quasilist_reflection empty_reflection)
```
```   197 done
```
```   198
```
```   199
```
```   200 subsubsection\<open>The Formula @{term is_tl}, Internalized\<close>
```
```   201
```
```   202 (*     "is_tl(M,xs,T) ==
```
```   203        (is_Nil(M,xs) \<longrightarrow> T=xs) &
```
```   204        (\<forall>x[M]. \<forall>l[M]. ~ is_Cons(M,x,l,xs) | T=l) &
```
```   205        (is_quasilist(M,xs) | empty(M,T))" *)
```
```   206 definition
```
```   207   tl_fm :: "[i,i]=>i" where
```
```   208     "tl_fm(xs,T) ==
```
```   209        And(Implies(Nil_fm(xs), Equal(T,xs)),
```
```   210            And(Forall(Forall(Or(Neg(Cons_fm(1,0,xs#+2)), Equal(T#+2,0)))),
```
```   211                Or(quasilist_fm(xs), empty_fm(T))))"
```
```   212
```
```   213 lemma tl_type [TC]:
```
```   214      "[| x \<in> nat; y \<in> nat |] ==> tl_fm(x,y) \<in> formula"
```
```   215 by (simp add: tl_fm_def)
```
```   216
```
```   217 lemma sats_tl_fm [simp]:
```
```   218    "[| x \<in> nat; y \<in> nat; env \<in> list(A)|]
```
```   219     ==> sats(A, tl_fm(x,y), env) \<longleftrightarrow> is_tl(##A, nth(x,env), nth(y,env))"
```
```   220 by (simp add: tl_fm_def is_tl_def)
```
```   221
```
```   222 lemma tl_iff_sats:
```
```   223       "[| nth(i,env) = x; nth(j,env) = y;
```
```   224           i \<in> nat; j \<in> nat; env \<in> list(A)|]
```
```   225        ==> is_tl(##A, x, y) \<longleftrightarrow> sats(A, tl_fm(i,j), env)"
```
```   226 by simp
```
```   227
```
```   228 theorem tl_reflection:
```
```   229      "REFLECTS[\<lambda>x. is_tl(L,f(x),g(x)),
```
```   230                \<lambda>i x. is_tl(##Lset(i),f(x),g(x))]"
```
```   231 apply (simp only: is_tl_def)
```
```   232 apply (intro FOL_reflections Nil_reflection Cons_reflection
```
```   233              quasilist_reflection empty_reflection)
```
```   234 done
```
```   235
```
```   236
```
```   237 subsubsection\<open>The Operator @{term is_bool_of_o}\<close>
```
```   238
```
```   239 (*   is_bool_of_o :: "[i=>o, o, i] => o"
```
```   240    "is_bool_of_o(M,P,z) == (P & number1(M,z)) | (~P & empty(M,z))" *)
```
```   241
```
```   242 text\<open>The formula @{term p} has no free variables.\<close>
```
```   243 definition
```
```   244   bool_of_o_fm :: "[i, i]=>i" where
```
```   245   "bool_of_o_fm(p,z) ==
```
```   246     Or(And(p,number1_fm(z)),
```
```   247        And(Neg(p),empty_fm(z)))"
```
```   248
```
```   249 lemma is_bool_of_o_type [TC]:
```
```   250      "[| p \<in> formula; z \<in> nat |] ==> bool_of_o_fm(p,z) \<in> formula"
```
```   251 by (simp add: bool_of_o_fm_def)
```
```   252
```
```   253 lemma sats_bool_of_o_fm:
```
```   254   assumes p_iff_sats: "P \<longleftrightarrow> sats(A, p, env)"
```
```   255   shows
```
```   256       "[|z \<in> nat; env \<in> list(A)|]
```
```   257        ==> sats(A, bool_of_o_fm(p,z), env) \<longleftrightarrow>
```
```   258            is_bool_of_o(##A, P, nth(z,env))"
```
```   259 by (simp add: bool_of_o_fm_def is_bool_of_o_def p_iff_sats [THEN iff_sym])
```
```   260
```
```   261 lemma is_bool_of_o_iff_sats:
```
```   262   "[| P \<longleftrightarrow> sats(A, p, env); nth(k,env) = z; k \<in> nat; env \<in> list(A)|]
```
```   263    ==> is_bool_of_o(##A, P, z) \<longleftrightarrow> sats(A, bool_of_o_fm(p,k), env)"
```
```   264 by (simp add: sats_bool_of_o_fm)
```
```   265
```
```   266 theorem bool_of_o_reflection:
```
```   267      "REFLECTS [P(L), \<lambda>i. P(##Lset(i))] ==>
```
```   268       REFLECTS[\<lambda>x. is_bool_of_o(L, P(L,x), f(x)),
```
```   269                \<lambda>i x. is_bool_of_o(##Lset(i), P(##Lset(i),x), f(x))]"
```
```   270 apply (simp (no_asm) only: is_bool_of_o_def)
```
```   271 apply (intro FOL_reflections function_reflections, assumption+)
```
```   272 done
```
```   273
```
```   274
```
```   275 subsection\<open>More Internalizations\<close>
```
```   276
```
```   277 subsubsection\<open>The Operator @{term is_lambda}\<close>
```
```   278
```
```   279 text\<open>The two arguments of @{term p} are always 1, 0. Remember that
```
```   280  @{term p} will be enclosed by three quantifiers.\<close>
```
```   281
```
```   282 (* is_lambda :: "[i=>o, i, [i,i]=>o, i] => o"
```
```   283     "is_lambda(M, A, is_b, z) ==
```
```   284        \<forall>p[M]. p \<in> z \<longleftrightarrow>
```
```   285         (\<exists>u[M]. \<exists>v[M]. u\<in>A & pair(M,u,v,p) & is_b(u,v))" *)
```
```   286 definition
```
```   287   lambda_fm :: "[i, i, i]=>i" where
```
```   288   "lambda_fm(p,A,z) ==
```
```   289     Forall(Iff(Member(0,succ(z)),
```
```   290             Exists(Exists(And(Member(1,A#+3),
```
```   291                            And(pair_fm(1,0,2), p))))))"
```
```   292
```
```   293 text\<open>We call @{term p} with arguments x, y by equating them with
```
```   294   the corresponding quantified variables with de Bruijn indices 1, 0.\<close>
```
```   295
```
```   296 lemma is_lambda_type [TC]:
```
```   297      "[| p \<in> formula; x \<in> nat; y \<in> nat |]
```
```   298       ==> lambda_fm(p,x,y) \<in> formula"
```
```   299 by (simp add: lambda_fm_def)
```
```   300
```
```   301 lemma sats_lambda_fm:
```
```   302   assumes is_b_iff_sats:
```
```   303       "!!a0 a1 a2.
```
```   304         [|a0\<in>A; a1\<in>A; a2\<in>A|]
```
```   305         ==> is_b(a1, a0) \<longleftrightarrow> sats(A, p, Cons(a0,Cons(a1,Cons(a2,env))))"
```
```   306   shows
```
```   307       "[|x \<in> nat; y \<in> nat; env \<in> list(A)|]
```
```   308        ==> sats(A, lambda_fm(p,x,y), env) \<longleftrightarrow>
```
```   309            is_lambda(##A, nth(x,env), is_b, nth(y,env))"
```
```   310 by (simp add: lambda_fm_def is_lambda_def is_b_iff_sats [THEN iff_sym])
```
```   311
```
```   312 theorem is_lambda_reflection:
```
```   313   assumes is_b_reflection:
```
```   314     "!!f g h. REFLECTS[\<lambda>x. is_b(L, f(x), g(x), h(x)),
```
```   315                      \<lambda>i x. is_b(##Lset(i), f(x), g(x), h(x))]"
```
```   316   shows "REFLECTS[\<lambda>x. is_lambda(L, A(x), is_b(L,x), f(x)),
```
```   317                \<lambda>i x. is_lambda(##Lset(i), A(x), is_b(##Lset(i),x), f(x))]"
```
```   318 apply (simp (no_asm_use) only: is_lambda_def)
```
```   319 apply (intro FOL_reflections is_b_reflection pair_reflection)
```
```   320 done
```
```   321
```
```   322 subsubsection\<open>The Operator @{term is_Member}, Internalized\<close>
```
```   323
```
```   324 (*    "is_Member(M,x,y,Z) ==
```
```   325         \<exists>p[M]. \<exists>u[M]. pair(M,x,y,p) & is_Inl(M,p,u) & is_Inl(M,u,Z)" *)
```
```   326 definition
```
```   327   Member_fm :: "[i,i,i]=>i" where
```
```   328     "Member_fm(x,y,Z) ==
```
```   329        Exists(Exists(And(pair_fm(x#+2,y#+2,1),
```
```   330                       And(Inl_fm(1,0), Inl_fm(0,Z#+2)))))"
```
```   331
```
```   332 lemma is_Member_type [TC]:
```
```   333      "[| x \<in> nat; y \<in> nat; z \<in> nat |] ==> Member_fm(x,y,z) \<in> formula"
```
```   334 by (simp add: Member_fm_def)
```
```   335
```
```   336 lemma sats_Member_fm [simp]:
```
```   337    "[| x \<in> nat; y \<in> nat; z \<in> nat; env \<in> list(A)|]
```
```   338     ==> sats(A, Member_fm(x,y,z), env) \<longleftrightarrow>
```
```   339         is_Member(##A, nth(x,env), nth(y,env), nth(z,env))"
```
```   340 by (simp add: Member_fm_def is_Member_def)
```
```   341
```
```   342 lemma Member_iff_sats:
```
```   343       "[| nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
```
```   344           i \<in> nat; j \<in> nat; k \<in> nat; env \<in> list(A)|]
```
```   345        ==> is_Member(##A, x, y, z) \<longleftrightarrow> sats(A, Member_fm(i,j,k), env)"
```
```   346 by (simp add: sats_Member_fm)
```
```   347
```
```   348 theorem Member_reflection:
```
```   349      "REFLECTS[\<lambda>x. is_Member(L,f(x),g(x),h(x)),
```
```   350                \<lambda>i x. is_Member(##Lset(i),f(x),g(x),h(x))]"
```
```   351 apply (simp only: is_Member_def)
```
```   352 apply (intro FOL_reflections pair_reflection Inl_reflection)
```
```   353 done
```
```   354
```
```   355 subsubsection\<open>The Operator @{term is_Equal}, Internalized\<close>
```
```   356
```
```   357 (*    "is_Equal(M,x,y,Z) ==
```
```   358         \<exists>p[M]. \<exists>u[M]. pair(M,x,y,p) & is_Inr(M,p,u) & is_Inl(M,u,Z)" *)
```
```   359 definition
```
```   360   Equal_fm :: "[i,i,i]=>i" where
```
```   361     "Equal_fm(x,y,Z) ==
```
```   362        Exists(Exists(And(pair_fm(x#+2,y#+2,1),
```
```   363                       And(Inr_fm(1,0), Inl_fm(0,Z#+2)))))"
```
```   364
```
```   365 lemma is_Equal_type [TC]:
```
```   366      "[| x \<in> nat; y \<in> nat; z \<in> nat |] ==> Equal_fm(x,y,z) \<in> formula"
```
```   367 by (simp add: Equal_fm_def)
```
```   368
```
```   369 lemma sats_Equal_fm [simp]:
```
```   370    "[| x \<in> nat; y \<in> nat; z \<in> nat; env \<in> list(A)|]
```
```   371     ==> sats(A, Equal_fm(x,y,z), env) \<longleftrightarrow>
```
```   372         is_Equal(##A, nth(x,env), nth(y,env), nth(z,env))"
```
```   373 by (simp add: Equal_fm_def is_Equal_def)
```
```   374
```
```   375 lemma Equal_iff_sats:
```
```   376       "[| nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
```
```   377           i \<in> nat; j \<in> nat; k \<in> nat; env \<in> list(A)|]
```
```   378        ==> is_Equal(##A, x, y, z) \<longleftrightarrow> sats(A, Equal_fm(i,j,k), env)"
```
```   379 by (simp add: sats_Equal_fm)
```
```   380
```
```   381 theorem Equal_reflection:
```
```   382      "REFLECTS[\<lambda>x. is_Equal(L,f(x),g(x),h(x)),
```
```   383                \<lambda>i x. is_Equal(##Lset(i),f(x),g(x),h(x))]"
```
```   384 apply (simp only: is_Equal_def)
```
```   385 apply (intro FOL_reflections pair_reflection Inl_reflection Inr_reflection)
```
```   386 done
```
```   387
```
```   388 subsubsection\<open>The Operator @{term is_Nand}, Internalized\<close>
```
```   389
```
```   390 (*    "is_Nand(M,x,y,Z) ==
```
```   391         \<exists>p[M]. \<exists>u[M]. pair(M,x,y,p) & is_Inl(M,p,u) & is_Inr(M,u,Z)" *)
```
```   392 definition
```
```   393   Nand_fm :: "[i,i,i]=>i" where
```
```   394     "Nand_fm(x,y,Z) ==
```
```   395        Exists(Exists(And(pair_fm(x#+2,y#+2,1),
```
```   396                       And(Inl_fm(1,0), Inr_fm(0,Z#+2)))))"
```
```   397
```
```   398 lemma is_Nand_type [TC]:
```
```   399      "[| x \<in> nat; y \<in> nat; z \<in> nat |] ==> Nand_fm(x,y,z) \<in> formula"
```
```   400 by (simp add: Nand_fm_def)
```
```   401
```
```   402 lemma sats_Nand_fm [simp]:
```
```   403    "[| x \<in> nat; y \<in> nat; z \<in> nat; env \<in> list(A)|]
```
```   404     ==> sats(A, Nand_fm(x,y,z), env) \<longleftrightarrow>
```
```   405         is_Nand(##A, nth(x,env), nth(y,env), nth(z,env))"
```
```   406 by (simp add: Nand_fm_def is_Nand_def)
```
```   407
```
```   408 lemma Nand_iff_sats:
```
```   409       "[| nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
```
```   410           i \<in> nat; j \<in> nat; k \<in> nat; env \<in> list(A)|]
```
```   411        ==> is_Nand(##A, x, y, z) \<longleftrightarrow> sats(A, Nand_fm(i,j,k), env)"
```
```   412 by (simp add: sats_Nand_fm)
```
```   413
```
```   414 theorem Nand_reflection:
```
```   415      "REFLECTS[\<lambda>x. is_Nand(L,f(x),g(x),h(x)),
```
```   416                \<lambda>i x. is_Nand(##Lset(i),f(x),g(x),h(x))]"
```
```   417 apply (simp only: is_Nand_def)
```
```   418 apply (intro FOL_reflections pair_reflection Inl_reflection Inr_reflection)
```
```   419 done
```
```   420
```
```   421 subsubsection\<open>The Operator @{term is_Forall}, Internalized\<close>
```
```   422
```
```   423 (* "is_Forall(M,p,Z) == \<exists>u[M]. is_Inr(M,p,u) & is_Inr(M,u,Z)" *)
```
```   424 definition
```
```   425   Forall_fm :: "[i,i]=>i" where
```
```   426     "Forall_fm(x,Z) ==
```
```   427        Exists(And(Inr_fm(succ(x),0), Inr_fm(0,succ(Z))))"
```
```   428
```
```   429 lemma is_Forall_type [TC]:
```
```   430      "[| x \<in> nat; y \<in> nat |] ==> Forall_fm(x,y) \<in> formula"
```
```   431 by (simp add: Forall_fm_def)
```
```   432
```
```   433 lemma sats_Forall_fm [simp]:
```
```   434    "[| x \<in> nat; y \<in> nat; env \<in> list(A)|]
```
```   435     ==> sats(A, Forall_fm(x,y), env) \<longleftrightarrow>
```
```   436         is_Forall(##A, nth(x,env), nth(y,env))"
```
```   437 by (simp add: Forall_fm_def is_Forall_def)
```
```   438
```
```   439 lemma Forall_iff_sats:
```
```   440       "[| nth(i,env) = x; nth(j,env) = y;
```
```   441           i \<in> nat; j \<in> nat; env \<in> list(A)|]
```
```   442        ==> is_Forall(##A, x, y) \<longleftrightarrow> sats(A, Forall_fm(i,j), env)"
```
```   443 by (simp add: sats_Forall_fm)
```
```   444
```
```   445 theorem Forall_reflection:
```
```   446      "REFLECTS[\<lambda>x. is_Forall(L,f(x),g(x)),
```
```   447                \<lambda>i x. is_Forall(##Lset(i),f(x),g(x))]"
```
```   448 apply (simp only: is_Forall_def)
```
```   449 apply (intro FOL_reflections pair_reflection Inr_reflection)
```
```   450 done
```
```   451
```
```   452
```
```   453 subsubsection\<open>The Operator @{term is_and}, Internalized\<close>
```
```   454
```
```   455 (* is_and(M,a,b,z) == (number1(M,a)  & z=b) |
```
```   456                        (~number1(M,a) & empty(M,z)) *)
```
```   457 definition
```
```   458   and_fm :: "[i,i,i]=>i" where
```
```   459     "and_fm(a,b,z) ==
```
```   460        Or(And(number1_fm(a), Equal(z,b)),
```
```   461           And(Neg(number1_fm(a)),empty_fm(z)))"
```
```   462
```
```   463 lemma is_and_type [TC]:
```
```   464      "[| x \<in> nat; y \<in> nat; z \<in> nat |] ==> and_fm(x,y,z) \<in> formula"
```
```   465 by (simp add: and_fm_def)
```
```   466
```
```   467 lemma sats_and_fm [simp]:
```
```   468    "[| x \<in> nat; y \<in> nat; z \<in> nat; env \<in> list(A)|]
```
```   469     ==> sats(A, and_fm(x,y,z), env) \<longleftrightarrow>
```
```   470         is_and(##A, nth(x,env), nth(y,env), nth(z,env))"
```
```   471 by (simp add: and_fm_def is_and_def)
```
```   472
```
```   473 lemma is_and_iff_sats:
```
```   474       "[| nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
```
```   475           i \<in> nat; j \<in> nat; k \<in> nat; env \<in> list(A)|]
```
```   476        ==> is_and(##A, x, y, z) \<longleftrightarrow> sats(A, and_fm(i,j,k), env)"
```
```   477 by simp
```
```   478
```
```   479 theorem is_and_reflection:
```
```   480      "REFLECTS[\<lambda>x. is_and(L,f(x),g(x),h(x)),
```
```   481                \<lambda>i x. is_and(##Lset(i),f(x),g(x),h(x))]"
```
```   482 apply (simp only: is_and_def)
```
```   483 apply (intro FOL_reflections function_reflections)
```
```   484 done
```
```   485
```
```   486
```
```   487 subsubsection\<open>The Operator @{term is_or}, Internalized\<close>
```
```   488
```
```   489 (* is_or(M,a,b,z) == (number1(M,a)  & number1(M,z)) |
```
```   490                      (~number1(M,a) & z=b) *)
```
```   491
```
```   492 definition
```
```   493   or_fm :: "[i,i,i]=>i" where
```
```   494     "or_fm(a,b,z) ==
```
```   495        Or(And(number1_fm(a), number1_fm(z)),
```
```   496           And(Neg(number1_fm(a)), Equal(z,b)))"
```
```   497
```
```   498 lemma is_or_type [TC]:
```
```   499      "[| x \<in> nat; y \<in> nat; z \<in> nat |] ==> or_fm(x,y,z) \<in> formula"
```
```   500 by (simp add: or_fm_def)
```
```   501
```
```   502 lemma sats_or_fm [simp]:
```
```   503    "[| x \<in> nat; y \<in> nat; z \<in> nat; env \<in> list(A)|]
```
```   504     ==> sats(A, or_fm(x,y,z), env) \<longleftrightarrow>
```
```   505         is_or(##A, nth(x,env), nth(y,env), nth(z,env))"
```
```   506 by (simp add: or_fm_def is_or_def)
```
```   507
```
```   508 lemma is_or_iff_sats:
```
```   509       "[| nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
```
```   510           i \<in> nat; j \<in> nat; k \<in> nat; env \<in> list(A)|]
```
```   511        ==> is_or(##A, x, y, z) \<longleftrightarrow> sats(A, or_fm(i,j,k), env)"
```
```   512 by simp
```
```   513
```
```   514 theorem is_or_reflection:
```
```   515      "REFLECTS[\<lambda>x. is_or(L,f(x),g(x),h(x)),
```
```   516                \<lambda>i x. is_or(##Lset(i),f(x),g(x),h(x))]"
```
```   517 apply (simp only: is_or_def)
```
```   518 apply (intro FOL_reflections function_reflections)
```
```   519 done
```
```   520
```
```   521
```
```   522
```
```   523 subsubsection\<open>The Operator @{term is_not}, Internalized\<close>
```
```   524
```
```   525 (* is_not(M,a,z) == (number1(M,a)  & empty(M,z)) |
```
```   526                      (~number1(M,a) & number1(M,z)) *)
```
```   527 definition
```
```   528   not_fm :: "[i,i]=>i" where
```
```   529     "not_fm(a,z) ==
```
```   530        Or(And(number1_fm(a), empty_fm(z)),
```
```   531           And(Neg(number1_fm(a)), number1_fm(z)))"
```
```   532
```
```   533 lemma is_not_type [TC]:
```
```   534      "[| x \<in> nat; z \<in> nat |] ==> not_fm(x,z) \<in> formula"
```
```   535 by (simp add: not_fm_def)
```
```   536
```
```   537 lemma sats_is_not_fm [simp]:
```
```   538    "[| x \<in> nat; z \<in> nat; env \<in> list(A)|]
```
```   539     ==> sats(A, not_fm(x,z), env) \<longleftrightarrow> is_not(##A, nth(x,env), nth(z,env))"
```
```   540 by (simp add: not_fm_def is_not_def)
```
```   541
```
```   542 lemma is_not_iff_sats:
```
```   543       "[| nth(i,env) = x; nth(k,env) = z;
```
```   544           i \<in> nat; k \<in> nat; env \<in> list(A)|]
```
```   545        ==> is_not(##A, x, z) \<longleftrightarrow> sats(A, not_fm(i,k), env)"
```
```   546 by simp
```
```   547
```
```   548 theorem is_not_reflection:
```
```   549      "REFLECTS[\<lambda>x. is_not(L,f(x),g(x)),
```
```   550                \<lambda>i x. is_not(##Lset(i),f(x),g(x))]"
```
```   551 apply (simp only: is_not_def)
```
```   552 apply (intro FOL_reflections function_reflections)
```
```   553 done
```
```   554
```
```   555
```
```   556 lemmas extra_reflections =
```
```   557     Inl_reflection Inr_reflection Nil_reflection Cons_reflection
```
```   558     quasilist_reflection hd_reflection tl_reflection bool_of_o_reflection
```
```   559     is_lambda_reflection Member_reflection Equal_reflection Nand_reflection
```
```   560     Forall_reflection is_and_reflection is_or_reflection is_not_reflection
```
```   561
```
```   562 subsection\<open>Well-Founded Recursion!\<close>
```
```   563
```
```   564 subsubsection\<open>The Operator @{term M_is_recfun}\<close>
```
```   565
```
```   566 text\<open>Alternative definition, minimizing nesting of quantifiers around MH\<close>
```
```   567 lemma M_is_recfun_iff:
```
```   568    "M_is_recfun(M,MH,r,a,f) \<longleftrightarrow>
```
```   569     (\<forall>z[M]. z \<in> f \<longleftrightarrow>
```
```   570      (\<exists>x[M]. \<exists>f_r_sx[M]. \<exists>y[M].
```
```   571              MH(x, f_r_sx, y) & pair(M,x,y,z) &
```
```   572              (\<exists>xa[M]. \<exists>sx[M]. \<exists>r_sx[M].
```
```   573                 pair(M,x,a,xa) & upair(M,x,x,sx) &
```
```   574                pre_image(M,r,sx,r_sx) & restriction(M,f,r_sx,f_r_sx) &
```
```   575                xa \<in> r)))"
```
```   576 apply (simp add: M_is_recfun_def)
```
```   577 apply (rule rall_cong, blast)
```
```   578 done
```
```   579
```
```   580
```
```   581 (* M_is_recfun :: "[i=>o, [i,i,i]=>o, i, i, i] => o"
```
```   582    "M_is_recfun(M,MH,r,a,f) ==
```
```   583      \<forall>z[M]. z \<in> f \<longleftrightarrow>
```
```   584                2      1           0
```
```   585 new def     (\<exists>x[M]. \<exists>f_r_sx[M]. \<exists>y[M].
```
```   586              MH(x, f_r_sx, y) & pair(M,x,y,z) &
```
```   587              (\<exists>xa[M]. \<exists>sx[M]. \<exists>r_sx[M].
```
```   588                 pair(M,x,a,xa) & upair(M,x,x,sx) &
```
```   589                pre_image(M,r,sx,r_sx) & restriction(M,f,r_sx,f_r_sx) &
```
```   590                xa \<in> r)"
```
```   591 *)
```
```   592
```
```   593 text\<open>The three arguments of @{term p} are always 2, 1, 0 and z\<close>
```
```   594 definition
```
```   595   is_recfun_fm :: "[i, i, i, i]=>i" where
```
```   596   "is_recfun_fm(p,r,a,f) ==
```
```   597    Forall(Iff(Member(0,succ(f)),
```
```   598     Exists(Exists(Exists(
```
```   599      And(p,
```
```   600       And(pair_fm(2,0,3),
```
```   601        Exists(Exists(Exists(
```
```   602         And(pair_fm(5,a#+7,2),
```
```   603          And(upair_fm(5,5,1),
```
```   604           And(pre_image_fm(r#+7,1,0),
```
```   605            And(restriction_fm(f#+7,0,4), Member(2,r#+7)))))))))))))))"
```
```   606
```
```   607 lemma is_recfun_type [TC]:
```
```   608      "[| p \<in> formula; x \<in> nat; y \<in> nat; z \<in> nat |]
```
```   609       ==> is_recfun_fm(p,x,y,z) \<in> formula"
```
```   610 by (simp add: is_recfun_fm_def)
```
```   611
```
```   612
```
```   613 lemma sats_is_recfun_fm:
```
```   614   assumes MH_iff_sats:
```
```   615       "!!a0 a1 a2 a3.
```
```   616         [|a0\<in>A; a1\<in>A; a2\<in>A; a3\<in>A|]
```
```   617         ==> MH(a2, a1, a0) \<longleftrightarrow> sats(A, p, Cons(a0,Cons(a1,Cons(a2,Cons(a3,env)))))"
```
```   618   shows
```
```   619       "[|x \<in> nat; y \<in> nat; z \<in> nat; env \<in> list(A)|]
```
```   620        ==> sats(A, is_recfun_fm(p,x,y,z), env) \<longleftrightarrow>
```
```   621            M_is_recfun(##A, MH, nth(x,env), nth(y,env), nth(z,env))"
```
```   622 by (simp add: is_recfun_fm_def M_is_recfun_iff MH_iff_sats [THEN iff_sym])
```
```   623
```
```   624 lemma is_recfun_iff_sats:
```
```   625   assumes MH_iff_sats:
```
```   626       "!!a0 a1 a2 a3.
```
```   627         [|a0\<in>A; a1\<in>A; a2\<in>A; a3\<in>A|]
```
```   628         ==> MH(a2, a1, a0) \<longleftrightarrow> sats(A, p, Cons(a0,Cons(a1,Cons(a2,Cons(a3,env)))))"
```
```   629   shows
```
```   630   "[| nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
```
```   631       i \<in> nat; j \<in> nat; k \<in> nat; env \<in> list(A)|]
```
```   632    ==> M_is_recfun(##A, MH, x, y, z) \<longleftrightarrow> sats(A, is_recfun_fm(p,i,j,k), env)"
```
```   633 by (simp add: sats_is_recfun_fm [OF MH_iff_sats])
```
```   634
```
```   635 text\<open>The additional variable in the premise, namely @{term f'}, is essential.
```
```   636 It lets @{term MH} depend upon @{term x}, which seems often necessary.
```
```   637 The same thing occurs in \<open>is_wfrec_reflection\<close>.\<close>
```
```   638 theorem is_recfun_reflection:
```
```   639   assumes MH_reflection:
```
```   640     "!!f' f g h. REFLECTS[\<lambda>x. MH(L, f'(x), f(x), g(x), h(x)),
```
```   641                      \<lambda>i x. MH(##Lset(i), f'(x), f(x), g(x), h(x))]"
```
```   642   shows "REFLECTS[\<lambda>x. M_is_recfun(L, MH(L,x), f(x), g(x), h(x)),
```
```   643              \<lambda>i x. M_is_recfun(##Lset(i), MH(##Lset(i),x), f(x), g(x), h(x))]"
```
```   644 apply (simp (no_asm_use) only: M_is_recfun_def)
```
```   645 apply (intro FOL_reflections function_reflections
```
```   646              restriction_reflection MH_reflection)
```
```   647 done
```
```   648
```
```   649 subsubsection\<open>The Operator @{term is_wfrec}\<close>
```
```   650
```
```   651 text\<open>The three arguments of @{term p} are always 2, 1, 0;
```
```   652       @{term p} is enclosed by 5 quantifiers.\<close>
```
```   653
```
```   654 (* is_wfrec :: "[i=>o, i, [i,i,i]=>o, i, i] => o"
```
```   655     "is_wfrec(M,MH,r,a,z) ==
```
```   656       \<exists>f[M]. M_is_recfun(M,MH,r,a,f) & MH(a,f,z)" *)
```
```   657 definition
```
```   658   is_wfrec_fm :: "[i, i, i, i]=>i" where
```
```   659   "is_wfrec_fm(p,r,a,z) ==
```
```   660     Exists(And(is_recfun_fm(p, succ(r), succ(a), 0),
```
```   661            Exists(Exists(Exists(Exists(
```
```   662              And(Equal(2,a#+5), And(Equal(1,4), And(Equal(0,z#+5), p)))))))))"
```
```   663
```
```   664 text\<open>We call @{term p} with arguments a, f, z by equating them with
```
```   665   the corresponding quantified variables with de Bruijn indices 2, 1, 0.\<close>
```
```   666
```
```   667 text\<open>There's an additional existential quantifier to ensure that the
```
```   668       environments in both calls to MH have the same length.\<close>
```
```   669
```
```   670 lemma is_wfrec_type [TC]:
```
```   671      "[| p \<in> formula; x \<in> nat; y \<in> nat; z \<in> nat |]
```
```   672       ==> is_wfrec_fm(p,x,y,z) \<in> formula"
```
```   673 by (simp add: is_wfrec_fm_def)
```
```   674
```
```   675 lemma sats_is_wfrec_fm:
```
```   676   assumes MH_iff_sats:
```
```   677       "!!a0 a1 a2 a3 a4.
```
```   678         [|a0\<in>A; a1\<in>A; a2\<in>A; a3\<in>A; a4\<in>A|]
```
```   679         ==> MH(a2, a1, a0) \<longleftrightarrow> sats(A, p, Cons(a0,Cons(a1,Cons(a2,Cons(a3,Cons(a4,env))))))"
```
```   680   shows
```
```   681       "[|x \<in> nat; y < length(env); z < length(env); env \<in> list(A)|]
```
```   682        ==> sats(A, is_wfrec_fm(p,x,y,z), env) \<longleftrightarrow>
```
```   683            is_wfrec(##A, MH, nth(x,env), nth(y,env), nth(z,env))"
```
```   684 apply (frule_tac x=z in lt_length_in_nat, assumption)
```
```   685 apply (frule lt_length_in_nat, assumption)
```
```   686 apply (simp add: is_wfrec_fm_def sats_is_recfun_fm is_wfrec_def MH_iff_sats [THEN iff_sym], blast)
```
```   687 done
```
```   688
```
```   689
```
```   690 lemma is_wfrec_iff_sats:
```
```   691   assumes MH_iff_sats:
```
```   692       "!!a0 a1 a2 a3 a4.
```
```   693         [|a0\<in>A; a1\<in>A; a2\<in>A; a3\<in>A; a4\<in>A|]
```
```   694         ==> MH(a2, a1, a0) \<longleftrightarrow> sats(A, p, Cons(a0,Cons(a1,Cons(a2,Cons(a3,Cons(a4,env))))))"
```
```   695   shows
```
```   696   "[|nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
```
```   697       i \<in> nat; j < length(env); k < length(env); env \<in> list(A)|]
```
```   698    ==> is_wfrec(##A, MH, x, y, z) \<longleftrightarrow> sats(A, is_wfrec_fm(p,i,j,k), env)"
```
```   699 by (simp add: sats_is_wfrec_fm [OF MH_iff_sats])
```
```   700
```
```   701 theorem is_wfrec_reflection:
```
```   702   assumes MH_reflection:
```
```   703     "!!f' f g h. REFLECTS[\<lambda>x. MH(L, f'(x), f(x), g(x), h(x)),
```
```   704                      \<lambda>i x. MH(##Lset(i), f'(x), f(x), g(x), h(x))]"
```
```   705   shows "REFLECTS[\<lambda>x. is_wfrec(L, MH(L,x), f(x), g(x), h(x)),
```
```   706                \<lambda>i x. is_wfrec(##Lset(i), MH(##Lset(i),x), f(x), g(x), h(x))]"
```
```   707 apply (simp (no_asm_use) only: is_wfrec_def)
```
```   708 apply (intro FOL_reflections MH_reflection is_recfun_reflection)
```
```   709 done
```
```   710
```
```   711
```
```   712 subsection\<open>For Datatypes\<close>
```
```   713
```
```   714 subsubsection\<open>Binary Products, Internalized\<close>
```
```   715
```
```   716 definition
```
```   717   cartprod_fm :: "[i,i,i]=>i" where
```
```   718 (* "cartprod(M,A,B,z) ==
```
```   719         \<forall>u[M]. u \<in> z \<longleftrightarrow> (\<exists>x[M]. x\<in>A & (\<exists>y[M]. y\<in>B & pair(M,x,y,u)))" *)
```
```   720     "cartprod_fm(A,B,z) ==
```
```   721        Forall(Iff(Member(0,succ(z)),
```
```   722                   Exists(And(Member(0,succ(succ(A))),
```
```   723                          Exists(And(Member(0,succ(succ(succ(B)))),
```
```   724                                     pair_fm(1,0,2)))))))"
```
```   725
```
```   726 lemma cartprod_type [TC]:
```
```   727      "[| x \<in> nat; y \<in> nat; z \<in> nat |] ==> cartprod_fm(x,y,z) \<in> formula"
```
```   728 by (simp add: cartprod_fm_def)
```
```   729
```
```   730 lemma sats_cartprod_fm [simp]:
```
```   731    "[| x \<in> nat; y \<in> nat; z \<in> nat; env \<in> list(A)|]
```
```   732     ==> sats(A, cartprod_fm(x,y,z), env) \<longleftrightarrow>
```
```   733         cartprod(##A, nth(x,env), nth(y,env), nth(z,env))"
```
```   734 by (simp add: cartprod_fm_def cartprod_def)
```
```   735
```
```   736 lemma cartprod_iff_sats:
```
```   737       "[| nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
```
```   738           i \<in> nat; j \<in> nat; k \<in> nat; env \<in> list(A)|]
```
```   739        ==> cartprod(##A, x, y, z) \<longleftrightarrow> sats(A, cartprod_fm(i,j,k), env)"
```
```   740 by (simp add: sats_cartprod_fm)
```
```   741
```
```   742 theorem cartprod_reflection:
```
```   743      "REFLECTS[\<lambda>x. cartprod(L,f(x),g(x),h(x)),
```
```   744                \<lambda>i x. cartprod(##Lset(i),f(x),g(x),h(x))]"
```
```   745 apply (simp only: cartprod_def)
```
```   746 apply (intro FOL_reflections pair_reflection)
```
```   747 done
```
```   748
```
```   749
```
```   750 subsubsection\<open>Binary Sums, Internalized\<close>
```
```   751
```
```   752 (* "is_sum(M,A,B,Z) ==
```
```   753        \<exists>A0[M]. \<exists>n1[M]. \<exists>s1[M]. \<exists>B1[M].
```
```   754          3      2       1        0
```
```   755        number1(M,n1) & cartprod(M,n1,A,A0) & upair(M,n1,n1,s1) &
```
```   756        cartprod(M,s1,B,B1) & union(M,A0,B1,Z)"  *)
```
```   757 definition
```
```   758   sum_fm :: "[i,i,i]=>i" where
```
```   759     "sum_fm(A,B,Z) ==
```
```   760        Exists(Exists(Exists(Exists(
```
```   761         And(number1_fm(2),
```
```   762             And(cartprod_fm(2,A#+4,3),
```
```   763                 And(upair_fm(2,2,1),
```
```   764                     And(cartprod_fm(1,B#+4,0), union_fm(3,0,Z#+4)))))))))"
```
```   765
```
```   766 lemma sum_type [TC]:
```
```   767      "[| x \<in> nat; y \<in> nat; z \<in> nat |] ==> sum_fm(x,y,z) \<in> formula"
```
```   768 by (simp add: sum_fm_def)
```
```   769
```
```   770 lemma sats_sum_fm [simp]:
```
```   771    "[| x \<in> nat; y \<in> nat; z \<in> nat; env \<in> list(A)|]
```
```   772     ==> sats(A, sum_fm(x,y,z), env) \<longleftrightarrow>
```
```   773         is_sum(##A, nth(x,env), nth(y,env), nth(z,env))"
```
```   774 by (simp add: sum_fm_def is_sum_def)
```
```   775
```
```   776 lemma sum_iff_sats:
```
```   777       "[| nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
```
```   778           i \<in> nat; j \<in> nat; k \<in> nat; env \<in> list(A)|]
```
```   779        ==> is_sum(##A, x, y, z) \<longleftrightarrow> sats(A, sum_fm(i,j,k), env)"
```
```   780 by simp
```
```   781
```
```   782 theorem sum_reflection:
```
```   783      "REFLECTS[\<lambda>x. is_sum(L,f(x),g(x),h(x)),
```
```   784                \<lambda>i x. is_sum(##Lset(i),f(x),g(x),h(x))]"
```
```   785 apply (simp only: is_sum_def)
```
```   786 apply (intro FOL_reflections function_reflections cartprod_reflection)
```
```   787 done
```
```   788
```
```   789
```
```   790 subsubsection\<open>The Operator @{term quasinat}\<close>
```
```   791
```
```   792 (* "is_quasinat(M,z) == empty(M,z) | (\<exists>m[M]. successor(M,m,z))" *)
```
```   793 definition
```
```   794   quasinat_fm :: "i=>i" where
```
```   795     "quasinat_fm(z) == Or(empty_fm(z), Exists(succ_fm(0,succ(z))))"
```
```   796
```
```   797 lemma quasinat_type [TC]:
```
```   798      "x \<in> nat ==> quasinat_fm(x) \<in> formula"
```
```   799 by (simp add: quasinat_fm_def)
```
```   800
```
```   801 lemma sats_quasinat_fm [simp]:
```
```   802    "[| x \<in> nat; env \<in> list(A)|]
```
```   803     ==> sats(A, quasinat_fm(x), env) \<longleftrightarrow> is_quasinat(##A, nth(x,env))"
```
```   804 by (simp add: quasinat_fm_def is_quasinat_def)
```
```   805
```
```   806 lemma quasinat_iff_sats:
```
```   807       "[| nth(i,env) = x; nth(j,env) = y;
```
```   808           i \<in> nat; env \<in> list(A)|]
```
```   809        ==> is_quasinat(##A, x) \<longleftrightarrow> sats(A, quasinat_fm(i), env)"
```
```   810 by simp
```
```   811
```
```   812 theorem quasinat_reflection:
```
```   813      "REFLECTS[\<lambda>x. is_quasinat(L,f(x)),
```
```   814                \<lambda>i x. is_quasinat(##Lset(i),f(x))]"
```
```   815 apply (simp only: is_quasinat_def)
```
```   816 apply (intro FOL_reflections function_reflections)
```
```   817 done
```
```   818
```
```   819
```
```   820 subsubsection\<open>The Operator @{term is_nat_case}\<close>
```
```   821 text\<open>I could not get it to work with the more natural assumption that
```
```   822  @{term is_b} takes two arguments.  Instead it must be a formula where 1 and 0
```
```   823  stand for @{term m} and @{term b}, respectively.\<close>
```
```   824
```
```   825 (* is_nat_case :: "[i=>o, i, [i,i]=>o, i, i] => o"
```
```   826     "is_nat_case(M, a, is_b, k, z) ==
```
```   827        (empty(M,k) \<longrightarrow> z=a) &
```
```   828        (\<forall>m[M]. successor(M,m,k) \<longrightarrow> is_b(m,z)) &
```
```   829        (is_quasinat(M,k) | empty(M,z))" *)
```
```   830 text\<open>The formula @{term is_b} has free variables 1 and 0.\<close>
```
```   831 definition
```
```   832   is_nat_case_fm :: "[i, i, i, i]=>i" where
```
```   833  "is_nat_case_fm(a,is_b,k,z) ==
```
```   834     And(Implies(empty_fm(k), Equal(z,a)),
```
```   835         And(Forall(Implies(succ_fm(0,succ(k)),
```
```   836                    Forall(Implies(Equal(0,succ(succ(z))), is_b)))),
```
```   837             Or(quasinat_fm(k), empty_fm(z))))"
```
```   838
```
```   839 lemma is_nat_case_type [TC]:
```
```   840      "[| is_b \<in> formula;
```
```   841          x \<in> nat; y \<in> nat; z \<in> nat |]
```
```   842       ==> is_nat_case_fm(x,is_b,y,z) \<in> formula"
```
```   843 by (simp add: is_nat_case_fm_def)
```
```   844
```
```   845 lemma sats_is_nat_case_fm:
```
```   846   assumes is_b_iff_sats:
```
```   847       "!!a. a \<in> A ==> is_b(a,nth(z, env)) \<longleftrightarrow>
```
```   848                       sats(A, p, Cons(nth(z,env), Cons(a, env)))"
```
```   849   shows
```
```   850       "[|x \<in> nat; y \<in> nat; z < length(env); env \<in> list(A)|]
```
```   851        ==> sats(A, is_nat_case_fm(x,p,y,z), env) \<longleftrightarrow>
```
```   852            is_nat_case(##A, nth(x,env), is_b, nth(y,env), nth(z,env))"
```
```   853 apply (frule lt_length_in_nat, assumption)
```
```   854 apply (simp add: is_nat_case_fm_def is_nat_case_def is_b_iff_sats [THEN iff_sym])
```
```   855 done
```
```   856
```
```   857 lemma is_nat_case_iff_sats:
```
```   858   "[| (!!a. a \<in> A ==> is_b(a,z) \<longleftrightarrow>
```
```   859                       sats(A, p, Cons(z, Cons(a,env))));
```
```   860       nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
```
```   861       i \<in> nat; j \<in> nat; k < length(env); env \<in> list(A)|]
```
```   862    ==> is_nat_case(##A, x, is_b, y, z) \<longleftrightarrow> sats(A, is_nat_case_fm(i,p,j,k), env)"
```
```   863 by (simp add: sats_is_nat_case_fm [of A is_b])
```
```   864
```
```   865
```
```   866 text\<open>The second argument of @{term is_b} gives it direct access to @{term x},
```
```   867   which is essential for handling free variable references.  Without this
```
```   868   argument, we cannot prove reflection for @{term iterates_MH}.\<close>
```
```   869 theorem is_nat_case_reflection:
```
```   870   assumes is_b_reflection:
```
```   871     "!!h f g. REFLECTS[\<lambda>x. is_b(L, h(x), f(x), g(x)),
```
```   872                      \<lambda>i x. is_b(##Lset(i), h(x), f(x), g(x))]"
```
```   873   shows "REFLECTS[\<lambda>x. is_nat_case(L, f(x), is_b(L,x), g(x), h(x)),
```
```   874                \<lambda>i x. is_nat_case(##Lset(i), f(x), is_b(##Lset(i), x), g(x), h(x))]"
```
```   875 apply (simp (no_asm_use) only: is_nat_case_def)
```
```   876 apply (intro FOL_reflections function_reflections
```
```   877              restriction_reflection is_b_reflection quasinat_reflection)
```
```   878 done
```
```   879
```
```   880
```
```   881 subsection\<open>The Operator @{term iterates_MH}, Needed for Iteration\<close>
```
```   882
```
```   883 (*  iterates_MH :: "[i=>o, [i,i]=>o, i, i, i, i] => o"
```
```   884    "iterates_MH(M,isF,v,n,g,z) ==
```
```   885         is_nat_case(M, v, \<lambda>m u. \<exists>gm[M]. fun_apply(M,g,m,gm) & isF(gm,u),
```
```   886                     n, z)" *)
```
```   887 definition
```
```   888   iterates_MH_fm :: "[i, i, i, i, i]=>i" where
```
```   889  "iterates_MH_fm(isF,v,n,g,z) ==
```
```   890     is_nat_case_fm(v,
```
```   891       Exists(And(fun_apply_fm(succ(succ(succ(g))),2,0),
```
```   892                      Forall(Implies(Equal(0,2), isF)))),
```
```   893       n, z)"
```
```   894
```
```   895 lemma iterates_MH_type [TC]:
```
```   896      "[| p \<in> formula;
```
```   897          v \<in> nat; x \<in> nat; y \<in> nat; z \<in> nat |]
```
```   898       ==> iterates_MH_fm(p,v,x,y,z) \<in> formula"
```
```   899 by (simp add: iterates_MH_fm_def)
```
```   900
```
```   901 lemma sats_iterates_MH_fm:
```
```   902   assumes is_F_iff_sats:
```
```   903       "!!a b c d. [| a \<in> A; b \<in> A; c \<in> A; d \<in> A|]
```
```   904               ==> is_F(a,b) \<longleftrightarrow>
```
```   905                   sats(A, p, Cons(b, Cons(a, Cons(c, Cons(d,env)))))"
```
```   906   shows
```
```   907       "[|v \<in> nat; x \<in> nat; y \<in> nat; z < length(env); env \<in> list(A)|]
```
```   908        ==> sats(A, iterates_MH_fm(p,v,x,y,z), env) \<longleftrightarrow>
```
```   909            iterates_MH(##A, is_F, nth(v,env), nth(x,env), nth(y,env), nth(z,env))"
```
```   910 apply (frule lt_length_in_nat, assumption)
```
```   911 apply (simp add: iterates_MH_fm_def iterates_MH_def sats_is_nat_case_fm
```
```   912               is_F_iff_sats [symmetric])
```
```   913 apply (rule is_nat_case_cong)
```
```   914 apply (simp_all add: setclass_def)
```
```   915 done
```
```   916
```
```   917 lemma iterates_MH_iff_sats:
```
```   918   assumes is_F_iff_sats:
```
```   919       "!!a b c d. [| a \<in> A; b \<in> A; c \<in> A; d \<in> A|]
```
```   920               ==> is_F(a,b) \<longleftrightarrow>
```
```   921                   sats(A, p, Cons(b, Cons(a, Cons(c, Cons(d,env)))))"
```
```   922   shows
```
```   923   "[| nth(i',env) = v; nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
```
```   924       i' \<in> nat; i \<in> nat; j \<in> nat; k < length(env); env \<in> list(A)|]
```
```   925    ==> iterates_MH(##A, is_F, v, x, y, z) \<longleftrightarrow>
```
```   926        sats(A, iterates_MH_fm(p,i',i,j,k), env)"
```
```   927 by (simp add: sats_iterates_MH_fm [OF is_F_iff_sats])
```
```   928
```
```   929 text\<open>The second argument of @{term p} gives it direct access to @{term x},
```
```   930   which is essential for handling free variable references.  Without this
```
```   931   argument, we cannot prove reflection for @{term list_N}.\<close>
```
```   932 theorem iterates_MH_reflection:
```
```   933   assumes p_reflection:
```
```   934     "!!f g h. REFLECTS[\<lambda>x. p(L, h(x), f(x), g(x)),
```
```   935                      \<lambda>i x. p(##Lset(i), h(x), f(x), g(x))]"
```
```   936  shows "REFLECTS[\<lambda>x. iterates_MH(L, p(L,x), e(x), f(x), g(x), h(x)),
```
```   937                \<lambda>i x. iterates_MH(##Lset(i), p(##Lset(i),x), e(x), f(x), g(x), h(x))]"
```
```   938 apply (simp (no_asm_use) only: iterates_MH_def)
```
```   939 apply (intro FOL_reflections function_reflections is_nat_case_reflection
```
```   940              restriction_reflection p_reflection)
```
```   941 done
```
```   942
```
```   943
```
```   944 subsubsection\<open>The Operator @{term is_iterates}\<close>
```
```   945
```
```   946 text\<open>The three arguments of @{term p} are always 2, 1, 0;
```
```   947       @{term p} is enclosed by 9 (??) quantifiers.\<close>
```
```   948
```
```   949 (*    "is_iterates(M,isF,v,n,Z) ==
```
```   950       \<exists>sn[M]. \<exists>msn[M]. successor(M,n,sn) & membership(M,sn,msn) &
```
```   951        1       0       is_wfrec(M, iterates_MH(M,isF,v), msn, n, Z)"*)
```
```   952
```
```   953 definition
```
```   954   is_iterates_fm :: "[i, i, i, i]=>i" where
```
```   955   "is_iterates_fm(p,v,n,Z) ==
```
```   956      Exists(Exists(
```
```   957       And(succ_fm(n#+2,1),
```
```   958        And(Memrel_fm(1,0),
```
```   959               is_wfrec_fm(iterates_MH_fm(p, v#+7, 2, 1, 0),
```
```   960                           0, n#+2, Z#+2)))))"
```
```   961
```
```   962 text\<open>We call @{term p} with arguments a, f, z by equating them with
```
```   963   the corresponding quantified variables with de Bruijn indices 2, 1, 0.\<close>
```
```   964
```
```   965
```
```   966 lemma is_iterates_type [TC]:
```
```   967      "[| p \<in> formula; x \<in> nat; y \<in> nat; z \<in> nat |]
```
```   968       ==> is_iterates_fm(p,x,y,z) \<in> formula"
```
```   969 by (simp add: is_iterates_fm_def)
```
```   970
```
```   971 lemma sats_is_iterates_fm:
```
```   972   assumes is_F_iff_sats:
```
```   973       "!!a b c d e f g h i j k.
```
```   974               [| a \<in> A; b \<in> A; c \<in> A; d \<in> A; e \<in> A; f \<in> A;
```
```   975                  g \<in> A; h \<in> A; i \<in> A; j \<in> A; k \<in> A|]
```
```   976               ==> is_F(a,b) \<longleftrightarrow>
```
```   977                   sats(A, p, Cons(b, Cons(a, Cons(c, Cons(d, Cons(e, Cons(f,
```
```   978                       Cons(g, Cons(h, Cons(i, Cons(j, Cons(k, env))))))))))))"
```
```   979   shows
```
```   980       "[|x \<in> nat; y < length(env); z < length(env); env \<in> list(A)|]
```
```   981        ==> sats(A, is_iterates_fm(p,x,y,z), env) \<longleftrightarrow>
```
```   982            is_iterates(##A, is_F, nth(x,env), nth(y,env), nth(z,env))"
```
```   983 apply (frule_tac x=z in lt_length_in_nat, assumption)
```
```   984 apply (frule lt_length_in_nat, assumption)
```
```   985 apply (simp add: is_iterates_fm_def is_iterates_def sats_is_nat_case_fm
```
```   986               is_F_iff_sats [symmetric] sats_is_wfrec_fm sats_iterates_MH_fm)
```
```   987 done
```
```   988
```
```   989
```
```   990 lemma is_iterates_iff_sats:
```
```   991   assumes is_F_iff_sats:
```
```   992       "!!a b c d e f g h i j k.
```
```   993               [| a \<in> A; b \<in> A; c \<in> A; d \<in> A; e \<in> A; f \<in> A;
```
```   994                  g \<in> A; h \<in> A; i \<in> A; j \<in> A; k \<in> A|]
```
```   995               ==> is_F(a,b) \<longleftrightarrow>
```
```   996                   sats(A, p, Cons(b, Cons(a, Cons(c, Cons(d, Cons(e, Cons(f,
```
```   997                       Cons(g, Cons(h, Cons(i, Cons(j, Cons(k, env))))))))))))"
```
```   998   shows
```
```   999   "[| nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
```
```  1000       i \<in> nat; j < length(env); k < length(env); env \<in> list(A)|]
```
```  1001    ==> is_iterates(##A, is_F, x, y, z) \<longleftrightarrow>
```
```  1002        sats(A, is_iterates_fm(p,i,j,k), env)"
```
```  1003 by (simp add: sats_is_iterates_fm [OF is_F_iff_sats])
```
```  1004
```
```  1005 text\<open>The second argument of @{term p} gives it direct access to @{term x},
```
```  1006   which is essential for handling free variable references.  Without this
```
```  1007   argument, we cannot prove reflection for @{term list_N}.\<close>
```
```  1008 theorem is_iterates_reflection:
```
```  1009   assumes p_reflection:
```
```  1010     "!!f g h. REFLECTS[\<lambda>x. p(L, h(x), f(x), g(x)),
```
```  1011                      \<lambda>i x. p(##Lset(i), h(x), f(x), g(x))]"
```
```  1012  shows "REFLECTS[\<lambda>x. is_iterates(L, p(L,x), f(x), g(x), h(x)),
```
```  1013                \<lambda>i x. is_iterates(##Lset(i), p(##Lset(i),x), f(x), g(x), h(x))]"
```
```  1014 apply (simp (no_asm_use) only: is_iterates_def)
```
```  1015 apply (intro FOL_reflections function_reflections p_reflection
```
```  1016              is_wfrec_reflection iterates_MH_reflection)
```
```  1017 done
```
```  1018
```
```  1019
```
```  1020 subsubsection\<open>The Formula @{term is_eclose_n}, Internalized\<close>
```
```  1021
```
```  1022 (* is_eclose_n(M,A,n,Z) == is_iterates(M, big_union(M), A, n, Z) *)
```
```  1023
```
```  1024 definition
```
```  1025   eclose_n_fm :: "[i,i,i]=>i" where
```
```  1026   "eclose_n_fm(A,n,Z) == is_iterates_fm(big_union_fm(1,0), A, n, Z)"
```
```  1027
```
```  1028 lemma eclose_n_fm_type [TC]:
```
```  1029  "[| x \<in> nat; y \<in> nat; z \<in> nat |] ==> eclose_n_fm(x,y,z) \<in> formula"
```
```  1030 by (simp add: eclose_n_fm_def)
```
```  1031
```
```  1032 lemma sats_eclose_n_fm [simp]:
```
```  1033    "[| x \<in> nat; y < length(env); z < length(env); env \<in> list(A)|]
```
```  1034     ==> sats(A, eclose_n_fm(x,y,z), env) \<longleftrightarrow>
```
```  1035         is_eclose_n(##A, nth(x,env), nth(y,env), nth(z,env))"
```
```  1036 apply (frule_tac x=z in lt_length_in_nat, assumption)
```
```  1037 apply (frule_tac x=y in lt_length_in_nat, assumption)
```
```  1038 apply (simp add: eclose_n_fm_def is_eclose_n_def
```
```  1039                  sats_is_iterates_fm)
```
```  1040 done
```
```  1041
```
```  1042 lemma eclose_n_iff_sats:
```
```  1043       "[| nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
```
```  1044           i \<in> nat; j < length(env); k < length(env); env \<in> list(A)|]
```
```  1045        ==> is_eclose_n(##A, x, y, z) \<longleftrightarrow> sats(A, eclose_n_fm(i,j,k), env)"
```
```  1046 by (simp add: sats_eclose_n_fm)
```
```  1047
```
```  1048 theorem eclose_n_reflection:
```
```  1049      "REFLECTS[\<lambda>x. is_eclose_n(L, f(x), g(x), h(x)),
```
```  1050                \<lambda>i x. is_eclose_n(##Lset(i), f(x), g(x), h(x))]"
```
```  1051 apply (simp only: is_eclose_n_def)
```
```  1052 apply (intro FOL_reflections function_reflections is_iterates_reflection)
```
```  1053 done
```
```  1054
```
```  1055
```
```  1056 subsubsection\<open>Membership in @{term "eclose(A)"}\<close>
```
```  1057
```
```  1058 (* mem_eclose(M,A,l) ==
```
```  1059       \<exists>n[M]. \<exists>eclosen[M].
```
```  1060        finite_ordinal(M,n) & is_eclose_n(M,A,n,eclosen) & l \<in> eclosen *)
```
```  1061 definition
```
```  1062   mem_eclose_fm :: "[i,i]=>i" where
```
```  1063     "mem_eclose_fm(x,y) ==
```
```  1064        Exists(Exists(
```
```  1065          And(finite_ordinal_fm(1),
```
```  1066            And(eclose_n_fm(x#+2,1,0), Member(y#+2,0)))))"
```
```  1067
```
```  1068 lemma mem_eclose_type [TC]:
```
```  1069      "[| x \<in> nat; y \<in> nat |] ==> mem_eclose_fm(x,y) \<in> formula"
```
```  1070 by (simp add: mem_eclose_fm_def)
```
```  1071
```
```  1072 lemma sats_mem_eclose_fm [simp]:
```
```  1073    "[| x \<in> nat; y \<in> nat; env \<in> list(A)|]
```
```  1074     ==> sats(A, mem_eclose_fm(x,y), env) \<longleftrightarrow> mem_eclose(##A, nth(x,env), nth(y,env))"
```
```  1075 by (simp add: mem_eclose_fm_def mem_eclose_def)
```
```  1076
```
```  1077 lemma mem_eclose_iff_sats:
```
```  1078       "[| nth(i,env) = x; nth(j,env) = y;
```
```  1079           i \<in> nat; j \<in> nat; env \<in> list(A)|]
```
```  1080        ==> mem_eclose(##A, x, y) \<longleftrightarrow> sats(A, mem_eclose_fm(i,j), env)"
```
```  1081 by simp
```
```  1082
```
```  1083 theorem mem_eclose_reflection:
```
```  1084      "REFLECTS[\<lambda>x. mem_eclose(L,f(x),g(x)),
```
```  1085                \<lambda>i x. mem_eclose(##Lset(i),f(x),g(x))]"
```
```  1086 apply (simp only: mem_eclose_def)
```
```  1087 apply (intro FOL_reflections finite_ordinal_reflection eclose_n_reflection)
```
```  1088 done
```
```  1089
```
```  1090
```
```  1091 subsubsection\<open>The Predicate ``Is @{term "eclose(A)"}''\<close>
```
```  1092
```
```  1093 (* is_eclose(M,A,Z) == \<forall>l[M]. l \<in> Z \<longleftrightarrow> mem_eclose(M,A,l) *)
```
```  1094 definition
```
```  1095   is_eclose_fm :: "[i,i]=>i" where
```
```  1096     "is_eclose_fm(A,Z) ==
```
```  1097        Forall(Iff(Member(0,succ(Z)), mem_eclose_fm(succ(A),0)))"
```
```  1098
```
```  1099 lemma is_eclose_type [TC]:
```
```  1100      "[| x \<in> nat; y \<in> nat |] ==> is_eclose_fm(x,y) \<in> formula"
```
```  1101 by (simp add: is_eclose_fm_def)
```
```  1102
```
```  1103 lemma sats_is_eclose_fm [simp]:
```
```  1104    "[| x \<in> nat; y \<in> nat; env \<in> list(A)|]
```
```  1105     ==> sats(A, is_eclose_fm(x,y), env) \<longleftrightarrow> is_eclose(##A, nth(x,env), nth(y,env))"
```
```  1106 by (simp add: is_eclose_fm_def is_eclose_def)
```
```  1107
```
```  1108 lemma is_eclose_iff_sats:
```
```  1109       "[| nth(i,env) = x; nth(j,env) = y;
```
```  1110           i \<in> nat; j \<in> nat; env \<in> list(A)|]
```
```  1111        ==> is_eclose(##A, x, y) \<longleftrightarrow> sats(A, is_eclose_fm(i,j), env)"
```
```  1112 by simp
```
```  1113
```
```  1114 theorem is_eclose_reflection:
```
```  1115      "REFLECTS[\<lambda>x. is_eclose(L,f(x),g(x)),
```
```  1116                \<lambda>i x. is_eclose(##Lset(i),f(x),g(x))]"
```
```  1117 apply (simp only: is_eclose_def)
```
```  1118 apply (intro FOL_reflections mem_eclose_reflection)
```
```  1119 done
```
```  1120
```
```  1121
```
```  1122 subsubsection\<open>The List Functor, Internalized\<close>
```
```  1123
```
```  1124 definition
```
```  1125   list_functor_fm :: "[i,i,i]=>i" where
```
```  1126 (* "is_list_functor(M,A,X,Z) ==
```
```  1127         \<exists>n1[M]. \<exists>AX[M].
```
```  1128          number1(M,n1) & cartprod(M,A,X,AX) & is_sum(M,n1,AX,Z)" *)
```
```  1129     "list_functor_fm(A,X,Z) ==
```
```  1130        Exists(Exists(
```
```  1131         And(number1_fm(1),
```
```  1132             And(cartprod_fm(A#+2,X#+2,0), sum_fm(1,0,Z#+2)))))"
```
```  1133
```
```  1134 lemma list_functor_type [TC]:
```
```  1135      "[| x \<in> nat; y \<in> nat; z \<in> nat |] ==> list_functor_fm(x,y,z) \<in> formula"
```
```  1136 by (simp add: list_functor_fm_def)
```
```  1137
```
```  1138 lemma sats_list_functor_fm [simp]:
```
```  1139    "[| x \<in> nat; y \<in> nat; z \<in> nat; env \<in> list(A)|]
```
```  1140     ==> sats(A, list_functor_fm(x,y,z), env) \<longleftrightarrow>
```
```  1141         is_list_functor(##A, nth(x,env), nth(y,env), nth(z,env))"
```
```  1142 by (simp add: list_functor_fm_def is_list_functor_def)
```
```  1143
```
```  1144 lemma list_functor_iff_sats:
```
```  1145   "[| nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
```
```  1146       i \<in> nat; j \<in> nat; k \<in> nat; env \<in> list(A)|]
```
```  1147    ==> is_list_functor(##A, x, y, z) \<longleftrightarrow> sats(A, list_functor_fm(i,j,k), env)"
```
```  1148 by simp
```
```  1149
```
```  1150 theorem list_functor_reflection:
```
```  1151      "REFLECTS[\<lambda>x. is_list_functor(L,f(x),g(x),h(x)),
```
```  1152                \<lambda>i x. is_list_functor(##Lset(i),f(x),g(x),h(x))]"
```
```  1153 apply (simp only: is_list_functor_def)
```
```  1154 apply (intro FOL_reflections number1_reflection
```
```  1155              cartprod_reflection sum_reflection)
```
```  1156 done
```
```  1157
```
```  1158
```
```  1159 subsubsection\<open>The Formula @{term is_list_N}, Internalized\<close>
```
```  1160
```
```  1161 (* "is_list_N(M,A,n,Z) ==
```
```  1162       \<exists>zero[M]. empty(M,zero) &
```
```  1163                 is_iterates(M, is_list_functor(M,A), zero, n, Z)" *)
```
```  1164
```
```  1165 definition
```
```  1166   list_N_fm :: "[i,i,i]=>i" where
```
```  1167   "list_N_fm(A,n,Z) ==
```
```  1168      Exists(
```
```  1169        And(empty_fm(0),
```
```  1170            is_iterates_fm(list_functor_fm(A#+9#+3,1,0), 0, n#+1, Z#+1)))"
```
```  1171
```
```  1172 lemma list_N_fm_type [TC]:
```
```  1173  "[| x \<in> nat; y \<in> nat; z \<in> nat |] ==> list_N_fm(x,y,z) \<in> formula"
```
```  1174 by (simp add: list_N_fm_def)
```
```  1175
```
```  1176 lemma sats_list_N_fm [simp]:
```
```  1177    "[| x \<in> nat; y < length(env); z < length(env); env \<in> list(A)|]
```
```  1178     ==> sats(A, list_N_fm(x,y,z), env) \<longleftrightarrow>
```
```  1179         is_list_N(##A, nth(x,env), nth(y,env), nth(z,env))"
```
```  1180 apply (frule_tac x=z in lt_length_in_nat, assumption)
```
```  1181 apply (frule_tac x=y in lt_length_in_nat, assumption)
```
```  1182 apply (simp add: list_N_fm_def is_list_N_def sats_is_iterates_fm)
```
```  1183 done
```
```  1184
```
```  1185 lemma list_N_iff_sats:
```
```  1186       "[| nth(i,env) = x; nth(j,env) = y; nth(k,env) = z;
```
```  1187           i \<in> nat; j < length(env); k < length(env); env \<in> list(A)|]
```
```  1188        ==> is_list_N(##A, x, y, z) \<longleftrightarrow> sats(A, list_N_fm(i,j,k), env)"
```
```  1189 by (simp add: sats_list_N_fm)
```
```  1190
```
```  1191 theorem list_N_reflection:
```
```  1192      "REFLECTS[\<lambda>x. is_list_N(L, f(x), g(x), h(x)),
```
```  1193                \<lambda>i x. is_list_N(##Lset(i), f(x), g(x), h(x))]"
```
```  1194 apply (simp only: is_list_N_def)
```
```  1195 apply (intro FOL_reflections function_reflections
```
```  1196              is_iterates_reflection list_functor_reflection)
```
```  1197 done
```
```  1198
```
```  1199
```
```  1200
```
```  1201 subsubsection\<open>The Predicate ``Is A List''\<close>
```
```  1202
```
```  1203 (* mem_list(M,A,l) ==
```
```  1204       \<exists>n[M]. \<exists>listn[M].
```
```  1205        finite_ordinal(M,n) & is_list_N(M,A,n,listn) & l \<in> listn *)
```
```  1206 definition
```
```  1207   mem_list_fm :: "[i,i]=>i" where
```
```  1208     "mem_list_fm(x,y) ==
```
```  1209        Exists(Exists(
```
```  1210          And(finite_ordinal_fm(1),
```
```  1211            And(list_N_fm(x#+2,1,0), Member(y#+2,0)))))"
```
```  1212
```
```  1213 lemma mem_list_type [TC]:
```
```  1214      "[| x \<in> nat; y \<in> nat |] ==> mem_list_fm(x,y) \<in> formula"
```
```  1215 by (simp add: mem_list_fm_def)
```
```  1216
```
```  1217 lemma sats_mem_list_fm [simp]:
```
```  1218    "[| x \<in> nat; y \<in> nat; env \<in> list(A)|]
```
```  1219     ==> sats(A, mem_list_fm(x,y), env) \<longleftrightarrow> mem_list(##A, nth(x,env), nth(y,env))"
```
```  1220 by (simp add: mem_list_fm_def mem_list_def)
```
```  1221
```
```  1222 lemma mem_list_iff_sats:
```
```  1223       "[| nth(i,env) = x; nth(j,env) = y;
```
```  1224           i \<in> nat; j \<in> nat; env \<in> list(A)|]
```
```  1225        ==> mem_list(##A, x, y) \<longleftrightarrow> sats(A, mem_list_fm(i,j), env)"
```
```  1226 by simp
```
```  1227
```
```  1228 theorem mem_list_reflection:
```
```  1229      "REFLECTS[\<lambda>x. mem_list(L,f(x),g(x)),
```
```  1230                \<lambda>i x. mem_list(##Lset(i),f(x),g(x))]"
```
```  1231 apply (simp only: mem_list_def)
```
```  1232 apply (intro FOL_reflections finite_ordinal_reflection list_N_reflection)
```
```  1233 done
```
```  1234
```
```  1235
```
```  1236 subsubsection\<open>The Predicate ``Is @{term "list(A)"}''\<close>
```
```  1237
```
```  1238 (* is_list(M,A,Z) == \<forall>l[M]. l \<in> Z \<longleftrightarrow> mem_list(M,A,l) *)
```
```  1239 definition
```
```  1240   is_list_fm :: "[i,i]=>i" where
```
```  1241     "is_list_fm(A,Z) ==
```
```  1242        Forall(Iff(Member(0,succ(Z)), mem_list_fm(succ(A),0)))"
```
```  1243
```
```  1244 lemma is_list_type [TC]:
```
```  1245      "[| x \<in> nat; y \<in> nat |] ==> is_list_fm(x,y) \<in> formula"
```
```  1246 by (simp add: is_list_fm_def)
```
```  1247
```
```  1248 lemma sats_is_list_fm [simp]:
```
```  1249    "[| x \<in> nat; y \<in> nat; env \<in> list(A)|]
```
```  1250     ==> sats(A, is_list_fm(x,y), env) \<longleftrightarrow> is_list(##A, nth(x,env), nth(y,env))"
```
```  1251 by (simp add: is_list_fm_def is_list_def)
```
```  1252
```
```  1253 lemma is_list_iff_sats:
```
```  1254       "[| nth(i,env) = x; nth(j,env) = y;
```
```  1255           i \<in> nat; j \<in> nat; env \<in> list(A)|]
```
```  1256        ==> is_list(##A, x, y) \<longleftrightarrow> sats(A, is_list_fm(i,j), env)"
```
```  1257 by simp
```
```  1258
```
```  1259 theorem is_list_reflection:
```
```  1260      "REFLECTS[\<lambda>x. is_list(L,f(x),g(x)),
```
```  1261                \<lambda>i x. is_list(##Lset(i),f(x),g(x))]"
```
```  1262 apply (simp only: is_list_def)
```
```  1263 apply (intro FOL_reflections mem_list_reflection)
```
```  1264 done
```
```  1265
```
```  1266
```
```  1267 subsubsection\<open>The Formula Functor, Internalized\<close>
```
```  1268
```
```  1269 definition formula_functor_fm :: "[i,i]=>i" where
```
```  1270 (*     "is_formula_functor(M,X,Z) ==
```
```  1271         \<exists>nat'[M]. \<exists>natnat[M]. \<exists>natnatsum[M]. \<exists>XX[M]. \<exists>X3[M].
```
```  1272            4           3               2       1       0
```
```  1273           omega(M,nat') & cartprod(M,nat',nat',natnat) &
```
```  1274           is_sum(M,natnat,natnat,natnatsum) &
```
```  1275           cartprod(M,X,X,XX) & is_sum(M,XX,X,X3) &
```
```  1276           is_sum(M,natnatsum,X3,Z)" *)
```
```  1277     "formula_functor_fm(X,Z) ==
```
```  1278        Exists(Exists(Exists(Exists(Exists(
```
```  1279         And(omega_fm(4),
```
```  1280          And(cartprod_fm(4,4,3),
```
```  1281           And(sum_fm(3,3,2),
```
```  1282            And(cartprod_fm(X#+5,X#+5,1),
```
```  1283             And(sum_fm(1,X#+5,0), sum_fm(2,0,Z#+5)))))))))))"
```
```  1284
```
```  1285 lemma formula_functor_type [TC]:
```
```  1286      "[| x \<in> nat; y \<in> nat |] ==> formula_functor_fm(x,y) \<in> formula"
```
```  1287 by (simp add: formula_functor_fm_def)
```
```  1288
```
```  1289 lemma sats_formula_functor_fm [simp]:
```
```  1290    "[| x \<in> nat; y \<in> nat; env \<in> list(A)|]
```
```  1291     ==> sats(A, formula_functor_fm(x,y), env) \<longleftrightarrow>
```
```  1292         is_formula_functor(##A, nth(x,env), nth(y,env))"
```
```  1293 by (simp add: formula_functor_fm_def is_formula_functor_def)
```
```  1294
```
```  1295 lemma formula_functor_iff_sats:
```
```  1296   "[| nth(i,env) = x; nth(j,env) = y;
```
```  1297       i \<in> nat; j \<in> nat; env \<in> list(A)|]
```
```  1298    ==> is_formula_functor(##A, x, y) \<longleftrightarrow> sats(A, formula_functor_fm(i,j), env)"
```
```  1299 by simp
```
```  1300
```
```  1301 theorem formula_functor_reflection:
```
```  1302      "REFLECTS[\<lambda>x. is_formula_functor(L,f(x),g(x)),
```
```  1303                \<lambda>i x. is_formula_functor(##Lset(i),f(x),g(x))]"
```
```  1304 apply (simp only: is_formula_functor_def)
```
```  1305 apply (intro FOL_reflections omega_reflection
```
```  1306              cartprod_reflection sum_reflection)
```
```  1307 done
```
```  1308
```
```  1309
```
```  1310 subsubsection\<open>The Formula @{term is_formula_N}, Internalized\<close>
```
```  1311
```
```  1312 (*  "is_formula_N(M,n,Z) ==
```
```  1313       \<exists>zero[M]. empty(M,zero) &
```
```  1314                 is_iterates(M, is_formula_functor(M), zero, n, Z)" *)
```
```  1315 definition
```
```  1316   formula_N_fm :: "[i,i]=>i" where
```
```  1317   "formula_N_fm(n,Z) ==
```
```  1318      Exists(
```
```  1319        And(empty_fm(0),
```
```  1320            is_iterates_fm(formula_functor_fm(1,0), 0, n#+1, Z#+1)))"
```
```  1321
```
```  1322 lemma formula_N_fm_type [TC]:
```
```  1323  "[| x \<in> nat; y \<in> nat |] ==> formula_N_fm(x,y) \<in> formula"
```
```  1324 by (simp add: formula_N_fm_def)
```
```  1325
```
```  1326 lemma sats_formula_N_fm [simp]:
```
```  1327    "[| x < length(env); y < length(env); env \<in> list(A)|]
```
```  1328     ==> sats(A, formula_N_fm(x,y), env) \<longleftrightarrow>
```
```  1329         is_formula_N(##A, nth(x,env), nth(y,env))"
```
```  1330 apply (frule_tac x=y in lt_length_in_nat, assumption)
```
```  1331 apply (frule lt_length_in_nat, assumption)
```
```  1332 apply (simp add: formula_N_fm_def is_formula_N_def sats_is_iterates_fm)
```
```  1333 done
```
```  1334
```
```  1335 lemma formula_N_iff_sats:
```
```  1336       "[| nth(i,env) = x; nth(j,env) = y;
```
```  1337           i < length(env); j < length(env); env \<in> list(A)|]
```
```  1338        ==> is_formula_N(##A, x, y) \<longleftrightarrow> sats(A, formula_N_fm(i,j), env)"
```
```  1339 by (simp add: sats_formula_N_fm)
```
```  1340
```
```  1341 theorem formula_N_reflection:
```
```  1342      "REFLECTS[\<lambda>x. is_formula_N(L, f(x), g(x)),
```
```  1343                \<lambda>i x. is_formula_N(##Lset(i), f(x), g(x))]"
```
```  1344 apply (simp only: is_formula_N_def)
```
```  1345 apply (intro FOL_reflections function_reflections
```
```  1346              is_iterates_reflection formula_functor_reflection)
```
```  1347 done
```
```  1348
```
```  1349
```
```  1350
```
```  1351 subsubsection\<open>The Predicate ``Is A Formula''\<close>
```
```  1352
```
```  1353 (*  mem_formula(M,p) ==
```
```  1354       \<exists>n[M]. \<exists>formn[M].
```
```  1355        finite_ordinal(M,n) & is_formula_N(M,n,formn) & p \<in> formn *)
```
```  1356 definition
```
```  1357   mem_formula_fm :: "i=>i" where
```
```  1358     "mem_formula_fm(x) ==
```
```  1359        Exists(Exists(
```
```  1360          And(finite_ordinal_fm(1),
```
```  1361            And(formula_N_fm(1,0), Member(x#+2,0)))))"
```
```  1362
```
```  1363 lemma mem_formula_type [TC]:
```
```  1364      "x \<in> nat ==> mem_formula_fm(x) \<in> formula"
```
```  1365 by (simp add: mem_formula_fm_def)
```
```  1366
```
```  1367 lemma sats_mem_formula_fm [simp]:
```
```  1368    "[| x \<in> nat; env \<in> list(A)|]
```
```  1369     ==> sats(A, mem_formula_fm(x), env) \<longleftrightarrow> mem_formula(##A, nth(x,env))"
```
```  1370 by (simp add: mem_formula_fm_def mem_formula_def)
```
```  1371
```
```  1372 lemma mem_formula_iff_sats:
```
```  1373       "[| nth(i,env) = x; i \<in> nat; env \<in> list(A)|]
```
```  1374        ==> mem_formula(##A, x) \<longleftrightarrow> sats(A, mem_formula_fm(i), env)"
```
```  1375 by simp
```
```  1376
```
```  1377 theorem mem_formula_reflection:
```
```  1378      "REFLECTS[\<lambda>x. mem_formula(L,f(x)),
```
```  1379                \<lambda>i x. mem_formula(##Lset(i),f(x))]"
```
```  1380 apply (simp only: mem_formula_def)
```
```  1381 apply (intro FOL_reflections finite_ordinal_reflection formula_N_reflection)
```
```  1382 done
```
```  1383
```
```  1384
```
```  1385
```
```  1386 subsubsection\<open>The Predicate ``Is @{term "formula"}''\<close>
```
```  1387
```
```  1388 (* is_formula(M,Z) == \<forall>p[M]. p \<in> Z \<longleftrightarrow> mem_formula(M,p) *)
```
```  1389 definition
```
```  1390   is_formula_fm :: "i=>i" where
```
```  1391     "is_formula_fm(Z) == Forall(Iff(Member(0,succ(Z)), mem_formula_fm(0)))"
```
```  1392
```
```  1393 lemma is_formula_type [TC]:
```
```  1394      "x \<in> nat ==> is_formula_fm(x) \<in> formula"
```
```  1395 by (simp add: is_formula_fm_def)
```
```  1396
```
```  1397 lemma sats_is_formula_fm [simp]:
```
```  1398    "[| x \<in> nat; env \<in> list(A)|]
```
```  1399     ==> sats(A, is_formula_fm(x), env) \<longleftrightarrow> is_formula(##A, nth(x,env))"
```
```  1400 by (simp add: is_formula_fm_def is_formula_def)
```
```  1401
```
```  1402 lemma is_formula_iff_sats:
```
```  1403       "[| nth(i,env) = x; i \<in> nat; env \<in> list(A)|]
```
```  1404        ==> is_formula(##A, x) \<longleftrightarrow> sats(A, is_formula_fm(i), env)"
```
```  1405 by simp
```
```  1406
```
```  1407 theorem is_formula_reflection:
```
```  1408      "REFLECTS[\<lambda>x. is_formula(L,f(x)),
```
```  1409                \<lambda>i x. is_formula(##Lset(i),f(x))]"
```
```  1410 apply (simp only: is_formula_def)
```
```  1411 apply (intro FOL_reflections mem_formula_reflection)
```
```  1412 done
```
```  1413
```
```  1414
```
```  1415 subsubsection\<open>The Operator @{term is_transrec}\<close>
```
```  1416
```
```  1417 text\<open>The three arguments of @{term p} are always 2, 1, 0.  It is buried
```
```  1418    within eight quantifiers!
```
```  1419    We call @{term p} with arguments a, f, z by equating them with
```
```  1420   the corresponding quantified variables with de Bruijn indices 2, 1, 0.\<close>
```
```  1421
```
```  1422 (* is_transrec :: "[i=>o, [i,i,i]=>o, i, i] => o"
```
```  1423    "is_transrec(M,MH,a,z) ==
```
```  1424       \<exists>sa[M]. \<exists>esa[M]. \<exists>mesa[M].
```
```  1425        2       1         0
```
```  1426        upair(M,a,a,sa) & is_eclose(M,sa,esa) & membership(M,esa,mesa) &
```
```  1427        is_wfrec(M,MH,mesa,a,z)" *)
```
```  1428 definition
```
```  1429   is_transrec_fm :: "[i, i, i]=>i" where
```
```  1430  "is_transrec_fm(p,a,z) ==
```
```  1431     Exists(Exists(Exists(
```
```  1432       And(upair_fm(a#+3,a#+3,2),
```
```  1433        And(is_eclose_fm(2,1),
```
```  1434         And(Memrel_fm(1,0), is_wfrec_fm(p,0,a#+3,z#+3)))))))"
```
```  1435
```
```  1436
```
```  1437 lemma is_transrec_type [TC]:
```
```  1438      "[| p \<in> formula; x \<in> nat; z \<in> nat |]
```
```  1439       ==> is_transrec_fm(p,x,z) \<in> formula"
```
```  1440 by (simp add: is_transrec_fm_def)
```
```  1441
```
```  1442 lemma sats_is_transrec_fm:
```
```  1443   assumes MH_iff_sats:
```
```  1444       "!!a0 a1 a2 a3 a4 a5 a6 a7.
```
```  1445         [|a0\<in>A; a1\<in>A; a2\<in>A; a3\<in>A; a4\<in>A; a5\<in>A; a6\<in>A; a7\<in>A|]
```
```  1446         ==> MH(a2, a1, a0) \<longleftrightarrow>
```
```  1447             sats(A, p, Cons(a0,Cons(a1,Cons(a2,Cons(a3,
```
```  1448                           Cons(a4,Cons(a5,Cons(a6,Cons(a7,env)))))))))"
```
```  1449   shows
```
```  1450       "[|x < length(env); z < length(env); env \<in> list(A)|]
```
```  1451        ==> sats(A, is_transrec_fm(p,x,z), env) \<longleftrightarrow>
```
```  1452            is_transrec(##A, MH, nth(x,env), nth(z,env))"
```
```  1453 apply (frule_tac x=z in lt_length_in_nat, assumption)
```
```  1454 apply (frule_tac x=x in lt_length_in_nat, assumption)
```
```  1455 apply (simp add: is_transrec_fm_def sats_is_wfrec_fm is_transrec_def MH_iff_sats [THEN iff_sym])
```
```  1456 done
```
```  1457
```
```  1458
```
```  1459 lemma is_transrec_iff_sats:
```
```  1460   assumes MH_iff_sats:
```
```  1461       "!!a0 a1 a2 a3 a4 a5 a6 a7.
```
```  1462         [|a0\<in>A; a1\<in>A; a2\<in>A; a3\<in>A; a4\<in>A; a5\<in>A; a6\<in>A; a7\<in>A|]
```
```  1463         ==> MH(a2, a1, a0) \<longleftrightarrow>
```
```  1464             sats(A, p, Cons(a0,Cons(a1,Cons(a2,Cons(a3,
```
```  1465                           Cons(a4,Cons(a5,Cons(a6,Cons(a7,env)))))))))"
```
```  1466   shows
```
```  1467   "[|nth(i,env) = x; nth(k,env) = z;
```
```  1468       i < length(env); k < length(env); env \<in> list(A)|]
```
```  1469    ==> is_transrec(##A, MH, x, z) \<longleftrightarrow> sats(A, is_transrec_fm(p,i,k), env)"
```
```  1470 by (simp add: sats_is_transrec_fm [OF MH_iff_sats])
```
```  1471
```
```  1472 theorem is_transrec_reflection:
```
```  1473   assumes MH_reflection:
```
```  1474     "!!f' f g h. REFLECTS[\<lambda>x. MH(L, f'(x), f(x), g(x), h(x)),
```
```  1475                      \<lambda>i x. MH(##Lset(i), f'(x), f(x), g(x), h(x))]"
```
```  1476   shows "REFLECTS[\<lambda>x. is_transrec(L, MH(L,x), f(x), h(x)),
```
```  1477                \<lambda>i x. is_transrec(##Lset(i), MH(##Lset(i),x), f(x), h(x))]"
```
```  1478 apply (simp (no_asm_use) only: is_transrec_def)
```
```  1479 apply (intro FOL_reflections function_reflections MH_reflection
```
```  1480              is_wfrec_reflection is_eclose_reflection)
```
```  1481 done
```
```  1482
```
```  1483 end
```