thys/UF.thy
changeset 248 aea02b5a58d2
parent 240 696081f445c2
child 250 745547bdc1c7
equal deleted inserted replaced
247:89ed51d72e4a 248:aea02b5a58d2
     3 *)
     3 *)
     4 
     4 
     5 header {* Construction of a Universal Function *}
     5 header {* Construction of a Universal Function *}
     6 
     6 
     7 theory UF
     7 theory UF
     8 imports Rec_Def GCD Abacus "~~/src/HOL/Number_Theory/Primes"
     8 imports Rec_Def GCD Abacus
     9 begin
     9 begin
    10 
    10 
    11 text {*
    11 text {*
    12   This theory file constructs the Universal Function @{text "rec_F"}, which is the UTM defined
    12   This theory file constructs the Universal Function @{text "rec_F"}, which is the UTM defined
    13   in terms of recursive functions. This @{text "rec_F"} is essentially an 
    13   in terms of recursive functions. This @{text "rec_F"} is essentially an 
   150                     [Cn (vl - 1) (constn 0) [id (vl - 1) 0]])) 
   150                     [Cn (vl - 1) (constn 0) [id (vl - 1) 0]])) 
   151              (Cn (Suc vl) rec_add [id (Suc vl) vl, 
   151              (Cn (Suc vl) rec_add [id (Suc vl) vl, 
   152                     Cn (Suc vl) rf (get_fstn_args (Suc vl) (vl - 1) 
   152                     Cn (Suc vl) rf (get_fstn_args (Suc vl) (vl - 1) 
   153                         @ [Cn (Suc vl) s [id (Suc vl) (vl - 1)]])]))"
   153                         @ [Cn (Suc vl) s [id (Suc vl) (vl - 1)]])]))"
   154 
   154 
   155 
   155 text {*
   156 declare rec_eval.simps[simp del] constn.simps[simp del]
   156   @{text "rec_exec"} is the interpreter function for
   157 
   157   reursive functions. The function is defined such that 
   158 
   158   it always returns meaningful results for primitive recursive 
   159 section {* Correctness of various recursive functions *}
   159   functions.
   160 
   160   *}
   161 
   161 
   162 lemma add_lemma: "rec_eval rec_add [x, y] =  x + y"
   162 declare rec_exec.simps[simp del] constn.simps[simp del]
   163 by(induct_tac y, auto simp: rec_add_def rec_eval.simps)
   163 
   164 
   164 text {*
   165 lemma mult_lemma: "rec_eval rec_mult [x, y] = x * y"
   165   Correctness of @{text "rec_add"}.
   166 by(induct_tac y, auto simp: rec_mult_def rec_eval.simps add_lemma)
   166   *}
   167 
   167 lemma add_lemma: "\<And> x y. rec_exec rec_add [x, y] =  x + y"
   168 lemma pred_lemma: "rec_eval rec_pred [x] =  x - 1"
   168 by(induct_tac y, auto simp: rec_add_def rec_exec.simps)
   169 by(induct_tac x, auto simp: rec_pred_def rec_eval.simps)
   169 
   170 
   170 text {*
   171 lemma minus_lemma: "rec_eval rec_minus [x, y] = x - y"
   171   Correctness of @{text "rec_mult"}.
   172 by(induct_tac y, auto simp: rec_eval.simps rec_minus_def pred_lemma)
   172   *}
   173 
   173 lemma mult_lemma: "\<And> x y. rec_exec rec_mult [x, y] = x * y"
   174 lemma sg_lemma: "rec_eval rec_sg [x] = (if x = 0 then 0 else 1)"
   174 by(induct_tac y, auto simp: rec_mult_def rec_exec.simps add_lemma)
   175 by(auto simp: rec_sg_def minus_lemma rec_eval.simps constn.simps)
   175 
   176 
   176 text {*
   177 lemma constn_lemma: "rec_eval (constn n) [x] = n"
   177   Correctness of @{text "rec_pred"}.
   178 by(induct n, auto simp: rec_eval.simps constn.simps)
   178   *}
   179 
   179 lemma pred_lemma: "\<And> x. rec_exec rec_pred [x] =  x - 1"
   180 lemma less_lemma: "rec_eval rec_less [x, y] = (if x < y then 1 else 0)"
   180 by(induct_tac x, auto simp: rec_pred_def rec_exec.simps)
   181 by(induct_tac y, auto simp: rec_eval.simps 
   181 
       
   182 text {*
       
   183   Correctness of @{text "rec_minus"}.
       
   184   *}
       
   185 lemma minus_lemma: "\<And> x y. rec_exec rec_minus [x, y] = x - y"
       
   186 by(induct_tac y, auto simp: rec_exec.simps rec_minus_def pred_lemma)
       
   187 
       
   188 text {*
       
   189   Correctness of @{text "rec_sg"}.
       
   190   *}
       
   191 lemma sg_lemma: "\<And> x. rec_exec rec_sg [x] = (if x = 0 then 0 else 1)"
       
   192 by(auto simp: rec_sg_def minus_lemma rec_exec.simps constn.simps)
       
   193 
       
   194 text {*
       
   195   Correctness of @{text "constn"}.
       
   196   *}
       
   197 lemma constn_lemma: "rec_exec (constn n) [x] = n"
       
   198 by(induct n, auto simp: rec_exec.simps constn.simps)
       
   199 
       
   200 text {*
       
   201   Correctness of @{text "rec_less"}.
       
   202   *}
       
   203 lemma less_lemma: "\<And> x y. rec_exec rec_less [x, y] = 
       
   204   (if x < y then 1 else 0)"
       
   205 by(induct_tac y, auto simp: rec_exec.simps 
   182   rec_less_def minus_lemma sg_lemma)
   206   rec_less_def minus_lemma sg_lemma)
   183 
   207 
   184 lemma not_lemma: "rec_eval rec_not [x] = (if x = 0 then 1 else 0)"
   208 text {*
   185 by(induct_tac x, auto simp: rec_eval.simps rec_not_def
   209   Correctness of @{text "rec_not"}.
       
   210   *}
       
   211 lemma not_lemma: 
       
   212   "\<And> x. rec_exec rec_not [x] = (if x = 0 then 1 else 0)"
       
   213 by(induct_tac x, auto simp: rec_exec.simps rec_not_def
   186   constn_lemma minus_lemma)
   214   constn_lemma minus_lemma)
   187 
   215 
   188 lemma eq_lemma: "rec_eval rec_eq [x, y] = (if x = y then 1 else 0)"
   216 text {*
   189 by(induct_tac y, auto simp: rec_eval.simps rec_eq_def constn_lemma add_lemma minus_lemma)
   217   Correctness of @{text "rec_eq"}.
   190 
   218   *}
   191 lemma conj_lemma: "rec_eval rec_conj [x, y] = (if x = 0 \<or> y = 0 then 0 else 1)"
   219 lemma eq_lemma: "\<And> x y. rec_exec rec_eq [x, y] = (if x = y then 1 else 0)"
   192 by(induct_tac y, auto simp: rec_eval.simps sg_lemma rec_conj_def mult_lemma)
   220 by(induct_tac y, auto simp: rec_exec.simps rec_eq_def constn_lemma add_lemma minus_lemma)
   193 
   221 
   194 lemma disj_lemma: "rec_eval rec_disj [x, y] = (if x = 0 \<and> y = 0 then 0 else 1)"
   222 text {*
   195 by(induct_tac y, auto simp: rec_disj_def sg_lemma add_lemma rec_eval.simps)
   223   Correctness of @{text "rec_conj"}.
   196 
   224   *}
   197 declare mult_lemma[simp] add_lemma[simp] pred_lemma[simp] 
   225 lemma conj_lemma: "\<And> x y. rec_exec rec_conj [x, y] = (if x = 0 \<or> y = 0 then 0 
   198         minus_lemma[simp] sg_lemma[simp] constn_lemma[simp] 
   226                                                        else 1)"
   199         less_lemma[simp] not_lemma[simp] eq_lemma[simp]
   227 by(induct_tac y, auto simp: rec_exec.simps sg_lemma rec_conj_def mult_lemma)
   200         conj_lemma[simp] disj_lemma[simp]
   228 
   201 
   229 text {*
   202 text {*
   230   Correctness of @{text "rec_disj"}.
   203   @{text "primrec recf n"} is true iff @{text "recf"} is a primitive
   231   *}
   204   recursive function with arity @{text "n"}.
   232 lemma disj_lemma: "\<And> x y. rec_exec rec_disj [x, y] = (if x = 0 \<and> y = 0 then 0
   205   *}
   233                                                      else 1)"
   206 
   234 by(induct_tac y, auto simp: rec_disj_def sg_lemma add_lemma rec_exec.simps)
       
   235 
       
   236 
       
   237 text {*
       
   238   @{text "primrec recf n"} is true iff 
       
   239   @{text "recf"} is a primitive recursive function 
       
   240   with arity @{text "n"}.
       
   241   *}
   207 inductive primerec :: "recf \<Rightarrow> nat \<Rightarrow> bool"
   242 inductive primerec :: "recf \<Rightarrow> nat \<Rightarrow> bool"
   208   where
   243   where
   209 prime_z[intro]:  "primerec z (Suc 0)" |
   244 prime_z[intro]:  "primerec z (Suc 0)" |
   210 prime_s[intro]:  "primerec s (Suc 0)" |
   245 prime_s[intro]:  "primerec s (Suc 0)" |
   211 prime_id[intro!]: "\<lbrakk>n < m\<rbrakk> \<Longrightarrow> primerec (id m n) m" |
   246 prime_id[intro!]: "\<lbrakk>n < m\<rbrakk> \<Longrightarrow> primerec (id m n) m" |
   222 inductive_cases prime_s_reverse[elim]: "primerec s n"
   257 inductive_cases prime_s_reverse[elim]: "primerec s n"
   223 inductive_cases prime_id_reverse[elim]: "primerec (id m n) k"
   258 inductive_cases prime_id_reverse[elim]: "primerec (id m n) k"
   224 inductive_cases prime_cn_reverse[elim]: "primerec (Cn n f gs) m"
   259 inductive_cases prime_cn_reverse[elim]: "primerec (Cn n f gs) m"
   225 inductive_cases prime_pr_reverse[elim]: "primerec (Pr n f g) m"
   260 inductive_cases prime_pr_reverse[elim]: "primerec (Pr n f g) m"
   226 
   261 
   227 
   262 declare mult_lemma[simp] add_lemma[simp] pred_lemma[simp] 
       
   263         minus_lemma[simp] sg_lemma[simp] constn_lemma[simp] 
       
   264         less_lemma[simp] not_lemma[simp] eq_lemma[simp]
       
   265         conj_lemma[simp] disj_lemma[simp]
   228 
   266 
   229 text {*
   267 text {*
   230   @{text "Sigma"} is the logical specification of 
   268   @{text "Sigma"} is the logical specification of 
   231   the recursive function @{text "rec_sigma"}.
   269   the recursive function @{text "rec_sigma"}.
   232   *}
   270   *}
   233 function Sigma :: "(nat list \<Rightarrow> nat) \<Rightarrow> nat list \<Rightarrow> nat"
   271 function Sigma :: "(nat list \<Rightarrow> nat) \<Rightarrow> nat list \<Rightarrow> nat"
   234   where
   272   where
   235   "Sigma g xs = (if last xs = 0 then g xs
   273   "Sigma g xs = (if last xs = 0 then g xs
   236                  else (Sigma g (butlast xs @ [last xs - 1]) + g xs)) "
   274                  else (Sigma g (butlast xs @ [last xs - 1]) +
       
   275                        g xs)) "
   237 by pat_completeness auto
   276 by pat_completeness auto
   238 
       
   239 termination
   277 termination
   240 proof
   278 proof
   241   show "wf (measure (\<lambda> (f, xs). last xs))" by auto
   279   show "wf (measure (\<lambda> (f, xs). last xs))" by auto
   242 next
   280 next
   243   fix g xs
   281   fix g xs
   244   assume "last (xs::nat list) \<noteq> 0"
   282   assume "last (xs::nat list) \<noteq> 0"
   245   thus "((g, butlast xs @ [last xs - 1]), g, xs) \<in> measure (\<lambda>(f, xs). last xs)"
   283   thus "((g, butlast xs @ [last xs - 1]), g, xs)  
       
   284                    \<in> measure (\<lambda>(f, xs). last xs)"
   246     by auto
   285     by auto
   247 qed
   286 qed
   248 
   287 
   249 declare rec_eval.simps[simp del] get_fstn_args.simps[simp del]
   288 declare rec_exec.simps[simp del] get_fstn_args.simps[simp del]
   250         arity.simps[simp del] Sigma.simps[simp del]
   289         arity.simps[simp del] Sigma.simps[simp del]
   251         rec_sigma.simps[simp del]
   290         rec_sigma.simps[simp del]
   252 
   291 
   253 lemma [simp]: "arity z = 1"
   292 lemma [simp]: "arity z = 1"
   254   by(simp add: arity.simps)
   293  by(simp add: arity.simps)
   255 
   294 
   256 lemma rec_pr_0_simp_rewrite: "
   295 lemma rec_pr_0_simp_rewrite: "
   257   rec_eval (Pr n f g) (xs @ [0]) = rec_eval f xs"
   296   rec_exec (Pr n f g) (xs @ [0]) = rec_exec f xs"
   258   by(simp add: rec_eval.simps)
   297 by(simp add: rec_exec.simps)
   259 
   298 
   260 lemma rec_pr_0_simp_rewrite_single_param: "
   299 lemma rec_pr_0_simp_rewrite_single_param: "
   261   rec_eval (Pr n f g) [0] = rec_eval f []"
   300   rec_exec (Pr n f g) [0] = rec_exec f []"
   262   by(simp add: rec_eval.simps)
   301 by(simp add: rec_exec.simps)
   263 
   302 
   264 lemma rec_pr_Suc_simp_rewrite: 
   303 lemma rec_pr_Suc_simp_rewrite: 
   265   "rec_eval (Pr n f g) (xs @ [Suc x]) = rec_eval g (xs @ [x] @ [rec_eval (Pr n f g) (xs @ [x])])"
   304   "rec_exec (Pr n f g) (xs @ [Suc x]) =
   266 by(simp add: rec_eval.simps)
   305                        rec_exec g (xs @ [x] @ 
       
   306                         [rec_exec (Pr n f g) (xs @ [x])])"
       
   307 by(simp add: rec_exec.simps)
   267 
   308 
   268 lemma rec_pr_Suc_simp_rewrite_single_param: 
   309 lemma rec_pr_Suc_simp_rewrite_single_param: 
   269   "rec_eval (Pr n f g) ([Suc x]) = rec_eval g ([x] @ [rec_eval (Pr n f g) ([x])])"
   310   "rec_exec (Pr n f g) ([Suc x]) =
   270 by(simp add: rec_eval.simps)
   311            rec_exec g ([x] @ [rec_exec (Pr n f g) ([x])])"
       
   312 by(simp add: rec_exec.simps)
   271 
   313 
   272 lemma Sigma_0_simp_rewrite_single_param:
   314 lemma Sigma_0_simp_rewrite_single_param:
   273   "Sigma f [0] = f [0]"
   315   "Sigma f [0] = f [0]"
   274 by(simp add: Sigma.simps)
   316 by(simp add: Sigma.simps)
   275 
   317 
   287 
   329 
   288 lemma  [simp]: "(xs @ ys) ! (Suc (length xs)) = ys ! 1"
   330 lemma  [simp]: "(xs @ ys) ! (Suc (length xs)) = ys ! 1"
   289 by(simp add: nth_append)
   331 by(simp add: nth_append)
   290   
   332   
   291 lemma get_fstn_args_take: "\<lbrakk>length xs = m; n \<le> m\<rbrakk> \<Longrightarrow> 
   333 lemma get_fstn_args_take: "\<lbrakk>length xs = m; n \<le> m\<rbrakk> \<Longrightarrow> 
   292   map (\<lambda> f. rec_eval f xs) (get_fstn_args m n)= take n xs"
   334   map (\<lambda> f. rec_exec f xs) (get_fstn_args m n)= take n xs"
   293 proof(induct n)
   335 proof(induct n)
   294   case 0 thus "?case"
   336   case 0 thus "?case"
   295     by(simp add: get_fstn_args.simps)
   337     by(simp add: get_fstn_args.simps)
   296 next
   338 next
   297   case (Suc n) thus "?case"
   339   case (Suc n) thus "?case"
   298     by(simp add: get_fstn_args.simps rec_eval.simps 
   340     by(simp add: get_fstn_args.simps rec_exec.simps 
   299              take_Suc_conv_app_nth)
   341              take_Suc_conv_app_nth)
   300 qed
   342 qed
   301     
   343     
   302 lemma [simp]: "primerec f n \<Longrightarrow> arity f = n"
   344 lemma [simp]: "primerec f n \<Longrightarrow> arity f = n"
   303   apply(case_tac f)
   345   apply(case_tac f)
   305   apply(erule_tac prime_mn_reverse)
   347   apply(erule_tac prime_mn_reverse)
   306   done
   348   done
   307 
   349 
   308 lemma rec_sigma_Suc_simp_rewrite: 
   350 lemma rec_sigma_Suc_simp_rewrite: 
   309   "primerec f (Suc (length xs))
   351   "primerec f (Suc (length xs))
   310     \<Longrightarrow> rec_eval (rec_sigma f) (xs @ [Suc x]) = 
   352     \<Longrightarrow> rec_exec (rec_sigma f) (xs @ [Suc x]) = 
   311     rec_eval (rec_sigma f) (xs @ [x]) + rec_eval f (xs @ [Suc x])"
   353     rec_exec (rec_sigma f) (xs @ [x]) + rec_exec f (xs @ [Suc x])"
   312   apply(induct x)
   354   apply(induct x)
   313   apply(auto simp: rec_sigma.simps Let_def rec_pr_Suc_simp_rewrite
   355   apply(auto simp: rec_sigma.simps Let_def rec_pr_Suc_simp_rewrite
   314                    rec_eval.simps get_fstn_args_take)
   356                    rec_exec.simps get_fstn_args_take)
   315   done      
   357   done      
   316 
   358 
   317 text {*
   359 text {*
   318   The correctness of @{text "rec_sigma"} with respect to its specification.
   360   The correctness of @{text "rec_sigma"} with respect to its specification.
   319   *}
   361   *}
   320 lemma sigma_lemma: 
   362 lemma sigma_lemma: 
   321   "primerec rg (Suc (length xs))
   363   "primerec rg (Suc (length xs))
   322      \<Longrightarrow> rec_eval (rec_sigma rg) (xs @ [x]) = Sigma (rec_eval rg) (xs @ [x])"
   364      \<Longrightarrow> rec_exec (rec_sigma rg) (xs @ [x]) = Sigma (rec_exec rg) (xs @ [x])"
   323 apply(induct x)
   365 apply(induct x)
   324 apply(auto simp: rec_eval.simps rec_sigma.simps Let_def 
   366 apply(auto simp: rec_exec.simps rec_sigma.simps Let_def 
   325          get_fstn_args_take Sigma_0_simp_rewrite
   367          get_fstn_args_take Sigma_0_simp_rewrite
   326          Sigma_Suc_simp_rewrite) 
   368          Sigma_Suc_simp_rewrite) 
   327 done
   369 done
   328 
   370 
   329 text {*
   371 text {*
   364     by auto
   406     by auto
   365 qed
   407 qed
   366 
   408 
   367 lemma rec_accum_Suc_simp_rewrite: 
   409 lemma rec_accum_Suc_simp_rewrite: 
   368   "primerec f (Suc (length xs))
   410   "primerec f (Suc (length xs))
   369     \<Longrightarrow> rec_eval (rec_accum f) (xs @ [Suc x]) = 
   411     \<Longrightarrow> rec_exec (rec_accum f) (xs @ [Suc x]) = 
   370     rec_eval (rec_accum f) (xs @ [x]) * rec_eval f (xs @ [Suc x])"
   412     rec_exec (rec_accum f) (xs @ [x]) * rec_exec f (xs @ [Suc x])"
   371   apply(induct x)
   413   apply(induct x)
   372   apply(auto simp: rec_sigma.simps Let_def rec_pr_Suc_simp_rewrite
   414   apply(auto simp: rec_sigma.simps Let_def rec_pr_Suc_simp_rewrite
   373                    rec_eval.simps get_fstn_args_take)
   415                    rec_exec.simps get_fstn_args_take)
   374   done  
   416   done  
   375 
   417 
   376 text {*
   418 text {*
   377   The correctness of @{text "rec_accum"} with respect to its specification.
   419   The correctness of @{text "rec_accum"} with respect to its specification.
   378 *}
   420 *}
   379 lemma accum_lemma :
   421 lemma accum_lemma :
   380   "primerec rg (Suc (length xs))
   422   "primerec rg (Suc (length xs))
   381      \<Longrightarrow> rec_eval (rec_accum rg) (xs @ [x]) = Accum (rec_eval rg) (xs @ [x])"
   423      \<Longrightarrow> rec_exec (rec_accum rg) (xs @ [x]) = Accum (rec_exec rg) (xs @ [x])"
   382 apply(induct x)
   424 apply(induct x)
   383 apply(auto simp: rec_eval.simps rec_sigma.simps Let_def 
   425 apply(auto simp: rec_exec.simps rec_sigma.simps Let_def 
   384                      get_fstn_args_take)
   426                      get_fstn_args_take)
   385 done
   427 done
   386 
   428 
   387 declare rec_accum.simps [simp del]
   429 declare rec_accum.simps [simp del]
   388 
   430 
   397     (let vl = arity rf in
   439     (let vl = arity rf in
   398        Cn (vl - 1) rec_sg [Cn (vl - 1) (rec_accum rf) 
   440        Cn (vl - 1) rec_sg [Cn (vl - 1) (rec_accum rf) 
   399                  (get_fstn_args (vl - 1) (vl - 1) @ [rt])])"
   441                  (get_fstn_args (vl - 1) (vl - 1) @ [rt])])"
   400 
   442 
   401 lemma rec_accum_ex: "primerec rf (Suc (length xs)) \<Longrightarrow>
   443 lemma rec_accum_ex: "primerec rf (Suc (length xs)) \<Longrightarrow>
   402      (rec_eval (rec_accum rf) (xs @ [x]) = 0) = 
   444      (rec_exec (rec_accum rf) (xs @ [x]) = 0) = 
   403       (\<exists> t \<le> x. rec_eval rf (xs @ [t]) = 0)"
   445       (\<exists> t \<le> x. rec_exec rf (xs @ [t]) = 0)"
   404 apply(induct x, simp_all add: rec_accum_Suc_simp_rewrite)
   446 apply(induct x, simp_all add: rec_accum_Suc_simp_rewrite)
   405 apply(simp add: rec_eval.simps rec_accum.simps get_fstn_args_take, 
   447 apply(simp add: rec_exec.simps rec_accum.simps get_fstn_args_take, 
   406       auto)
   448       auto)
   407 apply(rule_tac x = ta in exI, simp)
   449 apply(rule_tac x = ta in exI, simp)
   408 apply(case_tac "t = Suc x", simp_all)
   450 apply(case_tac "t = Suc x", simp_all)
   409 apply(rule_tac x = t in exI, simp)
   451 apply(rule_tac x = t in exI, simp)
   410 done
   452 done
   413   The correctness of @{text "rec_all"}.
   455   The correctness of @{text "rec_all"}.
   414   *}
   456   *}
   415 lemma all_lemma: 
   457 lemma all_lemma: 
   416   "\<lbrakk>primerec rf (Suc (length xs));
   458   "\<lbrakk>primerec rf (Suc (length xs));
   417     primerec rt (length xs)\<rbrakk>
   459     primerec rt (length xs)\<rbrakk>
   418   \<Longrightarrow> rec_eval (rec_all rt rf) xs = (if (\<forall> x \<le> (rec_eval rt xs). 0 < rec_eval rf (xs @ [x])) then 1
   460   \<Longrightarrow> rec_exec (rec_all rt rf) xs = (if (\<forall> x \<le> (rec_exec rt xs). 0 < rec_exec rf (xs @ [x])) then 1
   419                                                                                               else 0)"
   461                                                                                               else 0)"
   420 apply(auto simp: rec_all.simps)
   462 apply(auto simp: rec_all.simps)
   421 apply(simp add: rec_eval.simps map_append get_fstn_args_take split: if_splits)
   463 apply(simp add: rec_exec.simps map_append get_fstn_args_take split: if_splits)
   422 apply(drule_tac x = "rec_eval rt xs" in rec_accum_ex)
   464 apply(drule_tac x = "rec_exec rt xs" in rec_accum_ex)
   423 apply(case_tac "rec_eval (rec_accum rf) (xs @ [rec_eval rt xs]) = 0", simp_all)
   465 apply(case_tac "rec_exec (rec_accum rf) (xs @ [rec_exec rt xs]) = 0", simp_all)
   424 apply(erule_tac exE, erule_tac x = t in allE, simp)
   466 apply(erule_tac exE, erule_tac x = t in allE, simp)
   425 apply(simp add: rec_eval.simps map_append get_fstn_args_take)
   467 apply(simp add: rec_exec.simps map_append get_fstn_args_take)
   426 apply(drule_tac x = "rec_eval rt xs" in rec_accum_ex)
   468 apply(drule_tac x = "rec_exec rt xs" in rec_accum_ex)
   427 apply(case_tac "rec_eval (rec_accum rf) (xs @ [rec_eval rt xs]) = 0", simp, simp)
   469 apply(case_tac "rec_exec (rec_accum rf) (xs @ [rec_exec rt xs]) = 0", simp, simp)
   428 apply(erule_tac x = x in allE, simp)
   470 apply(erule_tac x = x in allE, simp)
   429 done
   471 done
   430 
   472 
   431 text {*
   473 text {*
   432   @{text "rec_ex t f (x1, x2, \<dots>, xn)"} 
   474   @{text "rec_ex t f (x1, x2, \<dots>, xn)"} 
   439        (let vl = arity rf in 
   481        (let vl = arity rf in 
   440          Cn (vl - 1) rec_sg [Cn (vl - 1) (rec_sigma rf) 
   482          Cn (vl - 1) rec_sg [Cn (vl - 1) (rec_sigma rf) 
   441                   (get_fstn_args (vl - 1) (vl - 1) @ [rt])])"
   483                   (get_fstn_args (vl - 1) (vl - 1) @ [rt])])"
   442 
   484 
   443 lemma rec_sigma_ex: "primerec rf (Suc (length xs))
   485 lemma rec_sigma_ex: "primerec rf (Suc (length xs))
   444           \<Longrightarrow> (rec_eval (rec_sigma rf) (xs @ [x]) = 0) = 
   486           \<Longrightarrow> (rec_exec (rec_sigma rf) (xs @ [x]) = 0) = 
   445                           (\<forall> t \<le> x. rec_eval rf (xs @ [t]) = 0)"
   487                           (\<forall> t \<le> x. rec_exec rf (xs @ [t]) = 0)"
   446 apply(induct x, simp_all add: rec_sigma_Suc_simp_rewrite)
   488 apply(induct x, simp_all add: rec_sigma_Suc_simp_rewrite)
   447 apply(simp add: rec_eval.simps rec_sigma.simps 
   489 apply(simp add: rec_exec.simps rec_sigma.simps 
   448                 get_fstn_args_take, auto)
   490                 get_fstn_args_take, auto)
   449 apply(case_tac "t = Suc x", simp_all)
   491 apply(case_tac "t = Suc x", simp_all)
   450 done
   492 done
   451 
   493 
   452 text {*
   494 text {*
   453   The correctness of @{text "ex_lemma"}.
   495   The correctness of @{text "ex_lemma"}.
   454   *}
   496   *}
   455 lemma ex_lemma:"
   497 lemma ex_lemma:"
   456   \<lbrakk>primerec rf (Suc (length xs));
   498   \<lbrakk>primerec rf (Suc (length xs));
   457    primerec rt (length xs)\<rbrakk>
   499    primerec rt (length xs)\<rbrakk>
   458 \<Longrightarrow> (rec_eval (rec_ex rt rf) xs =
   500 \<Longrightarrow> (rec_exec (rec_ex rt rf) xs =
   459     (if (\<exists> x \<le> (rec_eval rt xs). 0 <rec_eval rf (xs @ [x])) then 1
   501     (if (\<exists> x \<le> (rec_exec rt xs). 0 <rec_exec rf (xs @ [x])) then 1
   460      else 0))"
   502      else 0))"
   461 apply(auto simp: rec_ex.simps rec_eval.simps map_append get_fstn_args_take 
   503 apply(auto simp: rec_ex.simps rec_exec.simps map_append get_fstn_args_take 
   462             split: if_splits)
   504             split: if_splits)
   463 apply(drule_tac x = "rec_eval rt xs" in rec_sigma_ex, simp)
   505 apply(drule_tac x = "rec_exec rt xs" in rec_sigma_ex, simp)
   464 apply(drule_tac x = "rec_eval rt xs" in rec_sigma_ex, simp)
   506 apply(drule_tac x = "rec_exec rt xs" in rec_sigma_ex, simp)
   465 done
   507 done
   466 
   508 
   467 text {*
   509 text {*
   468   Definition of @{text "Min[R]"} on page 77 of Boolos's book.
   510   Definition of @{text "Min[R]"} on page 77 of Boolos's book.
   469 *}
   511 *}
   521        else if (Rr (xs @ [Suc w])) then (Suc w)
   563        else if (Rr (xs @ [Suc w])) then (Suc w)
   522        else Suc (Suc w))"
   564        else Suc (Suc w))"
   523 by(insert Minr_range[of Rr xs w], auto)
   565 by(insert Minr_range[of Rr xs w], auto)
   524 
   566 
   525 text {* 
   567 text {* 
   526   @{text "rec_Minmr"} is the recursive function 
   568   @{text "rec_Minr"} is the recursive function 
   527   used to implement @{text "Minr"}:
   569   used to implement @{text "Minr"}:
   528   if @{text "Rr"} is implemented by a recursive function @{text "recf"},
   570   if @{text "Rr"} is implemented by a recursive function @{text "recf"},
   529   then @{text "rec_Minr recf"} is the recursive function used to 
   571   then @{text "rec_Minr recf"} is the recursive function used to 
   530   implement @{text "Minr Rr"}
   572   implement @{text "Minr Rr"}
   531  *}
   573  *}
   532 fun rec_Minr :: "recf \<Rightarrow> recf"
   574 fun rec_Minr :: "recf \<Rightarrow> recf"
   533   where
   575   where
   534   "rec_Minr rf = 
   576   "rec_Minr rf = 
   535      (let vl = arity rf
   577      (let vl = arity rf
   536       in let rq = rec_all (id vl (vl - 1)) 
   578       in let rq = rec_all (id vl (vl - 1)) (Cn (Suc vl) 
   537                   (Cn (Suc vl) rec_not [Cn (Suc vl) rf 
   579               rec_not [Cn (Suc vl) rf 
   538                     (get_fstn_args (Suc vl) (vl - 1) @ [id (Suc vl) vl])]) 
   580                     (get_fstn_args (Suc vl) (vl - 1) @
       
   581                                         [id (Suc vl) (vl)])]) 
   539       in  rec_sigma rq)"
   582       in  rec_sigma rq)"
   540 
   583 
   541 lemma length_getpren_params[simp]: "length (get_fstn_args m n) = n"
   584 lemma length_getpren_params[simp]: "length (get_fstn_args m n) = n"
   542 by(induct n, auto simp: get_fstn_args.simps)
   585 by(induct n, auto simp: get_fstn_args.simps)
   543 
   586 
   635 apply(simp add: rec_not_def)
   678 apply(simp add: rec_not_def)
   636 apply(rule prime_cn, auto)
   679 apply(rule prime_cn, auto)
   637 apply(case_tac i, auto intro: prime_id)
   680 apply(case_tac i, auto intro: prime_id)
   638 done
   681 done
   639 
   682 
   640 lemma Min_false1[simp]: "\<lbrakk>\<not> Min {uu. uu \<le> w \<and> 0 < rec_eval rf (xs @ [uu])} \<le> w;
   683 lemma Min_false1[simp]: "\<lbrakk>\<not> Min {uu. uu \<le> w \<and> 0 < rec_exec rf (xs @ [uu])} \<le> w;
   641        x \<le> w; 0 < rec_eval rf (xs @ [x])\<rbrakk>
   684        x \<le> w; 0 < rec_exec rf (xs @ [x])\<rbrakk>
   642       \<Longrightarrow>  False"
   685       \<Longrightarrow>  False"
   643 apply(subgoal_tac "finite {uu. uu \<le> w \<and> 0 < rec_eval rf (xs @ [uu])}")
   686 apply(subgoal_tac "finite {uu. uu \<le> w \<and> 0 < rec_exec rf (xs @ [uu])}")
   644 apply(subgoal_tac "{uu. uu \<le> w \<and> 0 < rec_eval rf (xs @ [uu])} \<noteq> {}")
   687 apply(subgoal_tac "{uu. uu \<le> w \<and> 0 < rec_exec rf (xs @ [uu])} \<noteq> {}")
   645 apply(simp add: Min_le_iff, simp)
   688 apply(simp add: Min_le_iff, simp)
   646 apply(rule_tac x = x in exI, simp)
   689 apply(rule_tac x = x in exI, simp)
   647 apply(simp)
   690 apply(simp)
   648 done
   691 done
   649 
   692 
   650 lemma sigma_minr_lemma: 
   693 lemma sigma_minr_lemma: 
   651   assumes prrf:  "primerec rf (Suc (length xs))"
   694   assumes prrf:  "primerec rf (Suc (length xs))"
   652   shows "UF.Sigma (rec_eval (rec_all (recf.id (Suc (length xs)) (length xs))
   695   shows "UF.Sigma (rec_exec (rec_all (recf.id (Suc (length xs)) (length xs))
   653      (Cn (Suc (Suc (length xs))) rec_not
   696      (Cn (Suc (Suc (length xs))) rec_not
   654       [Cn (Suc (Suc (length xs))) rf (get_fstn_args (Suc (Suc (length xs))) 
   697       [Cn (Suc (Suc (length xs))) rf (get_fstn_args (Suc (Suc (length xs))) 
   655        (length xs) @ [recf.id (Suc (Suc (length xs))) (Suc (length xs))])])))
   698        (length xs) @ [recf.id (Suc (Suc (length xs))) (Suc (length xs))])])))
   656       (xs @ [w]) =
   699       (xs @ [w]) =
   657        Minr (\<lambda>args. 0 < rec_eval rf args) xs w"
   700        Minr (\<lambda>args. 0 < rec_exec rf args) xs w"
   658 proof(induct w)
   701 proof(induct w)
   659   let ?rt = "(recf.id (Suc (length xs)) ((length xs)))"
   702   let ?rt = "(recf.id (Suc (length xs)) ((length xs)))"
   660   let ?rf = "(Cn (Suc (Suc (length xs))) 
   703   let ?rf = "(Cn (Suc (Suc (length xs))) 
   661     rec_not [Cn (Suc (Suc (length xs))) rf 
   704     rec_not [Cn (Suc (Suc (length xs))) rf 
   662     (get_fstn_args (Suc (Suc (length xs))) (length xs) @ 
   705     (get_fstn_args (Suc (Suc (length xs))) (length xs) @ 
   665   let ?rq = "(rec_all ?rt ?rf)"
   708   let ?rq = "(rec_all ?rt ?rf)"
   666   have prrf: "primerec ?rf (Suc (length (xs @ [0]))) \<and>
   709   have prrf: "primerec ?rf (Suc (length (xs @ [0]))) \<and>
   667         primerec ?rt (length (xs @ [0]))"
   710         primerec ?rt (length (xs @ [0]))"
   668     apply(auto simp: prrf nth_append)+
   711     apply(auto simp: prrf nth_append)+
   669     done
   712     done
   670   show "Sigma (rec_eval (rec_all ?rt ?rf)) (xs @ [0])
   713   show "Sigma (rec_exec (rec_all ?rt ?rf)) (xs @ [0])
   671        = Minr (\<lambda>args. 0 < rec_eval rf args) xs 0"
   714        = Minr (\<lambda>args. 0 < rec_exec rf args) xs 0"
   672     apply(simp add: Sigma.simps)
   715     apply(simp add: Sigma.simps)
   673     apply(simp only: prrf all_lemma,  
   716     apply(simp only: prrf all_lemma,  
   674           auto simp: rec_eval.simps get_fstn_args_take Minr.simps)
   717           auto simp: rec_exec.simps get_fstn_args_take Minr.simps)
   675     apply(rule_tac Min_eqI, auto)
   718     apply(rule_tac Min_eqI, auto)
   676     done
   719     done
   677 next
   720 next
   678   fix w
   721   fix w
   679   let ?rt = "(recf.id (Suc (length xs)) ((length xs)))"
   722   let ?rt = "(recf.id (Suc (length xs)) ((length xs)))"
   682     (get_fstn_args (Suc (Suc (length xs))) (length xs) @ 
   725     (get_fstn_args (Suc (Suc (length xs))) (length xs) @ 
   683                 [recf.id (Suc (Suc (length xs))) 
   726                 [recf.id (Suc (Suc (length xs))) 
   684     (Suc ((length xs)))])])"
   727     (Suc ((length xs)))])])"
   685   let ?rq = "(rec_all ?rt ?rf)"
   728   let ?rq = "(rec_all ?rt ?rf)"
   686   assume ind:
   729   assume ind:
   687     "Sigma (rec_eval (rec_all ?rt ?rf)) (xs @ [w]) = Minr (\<lambda>args. 0 < rec_eval rf args) xs w"
   730     "Sigma (rec_exec (rec_all ?rt ?rf)) (xs @ [w]) = Minr (\<lambda>args. 0 < rec_exec rf args) xs w"
   688   have prrf: "primerec ?rf (Suc (length (xs @ [Suc w]))) \<and>
   731   have prrf: "primerec ?rf (Suc (length (xs @ [Suc w]))) \<and>
   689         primerec ?rt (length (xs @ [Suc w]))"
   732         primerec ?rt (length (xs @ [Suc w]))"
   690     apply(auto simp: prrf nth_append)+
   733     apply(auto simp: prrf nth_append)+
   691     done
   734     done
   692   show "UF.Sigma (rec_eval (rec_all ?rt ?rf))
   735   show "UF.Sigma (rec_exec (rec_all ?rt ?rf))
   693          (xs @ [Suc w]) =
   736          (xs @ [Suc w]) =
   694         Minr (\<lambda>args. 0 < rec_eval rf args) xs (Suc w)"
   737         Minr (\<lambda>args. 0 < rec_exec rf args) xs (Suc w)"
   695     apply(auto simp: Sigma_Suc_simp_rewrite ind Minr_Suc_simp)
   738     apply(auto simp: Sigma_Suc_simp_rewrite ind Minr_Suc_simp)
   696     apply(simp_all only: prrf all_lemma)
   739     apply(simp_all only: prrf all_lemma)
   697     apply(auto simp: rec_eval.simps get_fstn_args_take Let_def Minr.simps split: if_splits)
   740     apply(auto simp: rec_exec.simps get_fstn_args_take Let_def Minr.simps split: if_splits)
   698     apply(drule_tac Min_false1, simp, simp, simp)
   741     apply(drule_tac Min_false1, simp, simp, simp)
   699     apply(case_tac "x = Suc w", simp, simp)
   742     apply(case_tac "x = Suc w", simp, simp)
   700     apply(drule_tac Min_false1, simp, simp, simp)
   743     apply(drule_tac Min_false1, simp, simp, simp)
   701     apply(drule_tac Min_false1, simp, simp, simp)
   744     apply(drule_tac Min_false1, simp, simp, simp)
   702     done
   745     done
   703 qed
   746 qed
   704 
   747 
   705 text {*
   748 text {*
   706   The correctness of @{text "rec_Minr"}.
   749   The correctness of @{text "rec_Minr"}.
   707   *}
   750   *}
   708 lemma Minr_lemma: 
   751 lemma Minr_lemma: "
   709   assumes h: "primerec rf (Suc (length xs))" 
   752   \<lbrakk>primerec rf (Suc (length xs))\<rbrakk> 
   710   shows "rec_eval (rec_Minr rf) (xs @ [w]) = Minr (\<lambda> args. (0 < rec_eval rf args)) xs w"
   753      \<Longrightarrow> rec_exec (rec_Minr rf) (xs @ [w]) = 
       
   754             Minr (\<lambda> args. (0 < rec_exec rf args)) xs w"
   711 proof -
   755 proof -
   712   let ?rt = "(recf.id (Suc (length xs)) ((length xs)))"
   756   let ?rt = "(recf.id (Suc (length xs)) ((length xs)))"
   713   let ?rf = "(Cn (Suc (Suc (length xs))) 
   757   let ?rf = "(Cn (Suc (Suc (length xs))) 
   714     rec_not [Cn (Suc (Suc (length xs))) rf 
   758     rec_not [Cn (Suc (Suc (length xs))) rf 
   715     (get_fstn_args (Suc (Suc (length xs))) (length xs) @ 
   759     (get_fstn_args (Suc (Suc (length xs))) (length xs) @ 
   716                 [recf.id (Suc (Suc (length xs))) 
   760                 [recf.id (Suc (Suc (length xs))) 
   717     (Suc ((length xs)))])])"
   761     (Suc ((length xs)))])])"
   718   let ?rq = "(rec_all ?rt ?rf)"
   762   let ?rq = "(rec_all ?rt ?rf)"
       
   763   assume h: "primerec rf (Suc (length xs))"
   719   have h1: "primerec ?rq (Suc (length xs))"
   764   have h1: "primerec ?rq (Suc (length xs))"
   720     apply(rule_tac primerec_all_iff)
   765     apply(rule_tac primerec_all_iff)
   721     apply(auto simp: h nth_append)+
   766     apply(auto simp: h nth_append)+
   722     done
   767     done
   723   moreover have "arity rf = Suc (length xs)"
   768   moreover have "arity rf = Suc (length xs)"
   724     using h by auto
   769     using h by auto
   725   ultimately show "rec_eval (rec_Minr rf) (xs @ [w]) = Minr (\<lambda> args. (0 < rec_eval rf args)) xs w"
   770   ultimately show "rec_exec (rec_Minr rf) (xs @ [w]) = 
   726     apply(simp add: rec_eval.simps rec_Minr.simps arity.simps Let_def 
   771     Minr (\<lambda> args. (0 < rec_exec rf args)) xs w"
       
   772     apply(simp add: rec_exec.simps rec_Minr.simps arity.simps Let_def 
   727                     sigma_lemma all_lemma)
   773                     sigma_lemma all_lemma)
   728     apply(rule_tac  sigma_minr_lemma)
   774     apply(rule_tac  sigma_minr_lemma)
   729     apply(simp add: h)
   775     apply(simp add: h)
   730     done
   776     done
   731 qed
   777 qed
   741 
   787 
   742 text {*
   788 text {*
   743   The correctness of @{text "rec_le"}.
   789   The correctness of @{text "rec_le"}.
   744   *}
   790   *}
   745 lemma le_lemma: 
   791 lemma le_lemma: 
   746   "rec_eval rec_le [x, y] = (if (x \<le> y) then 1 else 0)"
   792   "\<And>x y. rec_exec rec_le [x, y] = (if (x \<le> y) then 1 else 0)"
   747 by(auto simp: rec_le_def rec_eval.simps)
   793 by(auto simp: rec_le_def rec_exec.simps)
   748 
   794 
   749 text {*
   795 text {*
   750   Definition of @{text "Max[Rr]"} on page 77 of Boolos's book.
   796   Definition of @{text "Max[Rr]"} on page 77 of Boolos's book.
   751 *}
   797 *}
   752 
   798 
   763 fun rec_maxr :: "recf \<Rightarrow> recf"
   809 fun rec_maxr :: "recf \<Rightarrow> recf"
   764   where
   810   where
   765   "rec_maxr rr = (let vl = arity rr in 
   811   "rec_maxr rr = (let vl = arity rr in 
   766                   let rt = id (Suc vl) (vl - 1) in
   812                   let rt = id (Suc vl) (vl - 1) in
   767                   let rf1 = Cn (Suc (Suc vl)) rec_le 
   813                   let rf1 = Cn (Suc (Suc vl)) rec_le 
   768                     [id (Suc (Suc vl)) (Suc vl), id (Suc (Suc vl)) vl] in
   814                     [id (Suc (Suc vl)) 
       
   815                      ((Suc vl)), id (Suc (Suc vl)) (vl)] in
   769                   let rf2 = Cn (Suc (Suc vl)) rec_not 
   816                   let rf2 = Cn (Suc (Suc vl)) rec_not 
   770                       [Cn (Suc (Suc vl)) 
   817                       [Cn (Suc (Suc vl)) 
   771                            rr (get_fstn_args (Suc (Suc vl)) 
   818                            rr (get_fstn_args (Suc (Suc vl)) 
   772                             (vl - 1) @ 
   819                             (vl - 1) @ 
   773                              [id (Suc (Suc vl)) (Suc vl)])] in
   820                              [id (Suc (Suc vl)) ((Suc vl))])] in
   774                   let rf = Cn (Suc (Suc vl)) rec_disj [rf1, rf2] in
   821                   let rf = Cn (Suc (Suc vl)) rec_disj [rf1, rf2] in
   775                   let Qf = Cn (Suc vl) rec_not [rec_all rt rf]
   822                   let Qf = Cn (Suc vl) rec_not [rec_all rt rf]
   776                   in Cn vl (rec_sigma Qf) (get_fstn_args vl vl @
   823                   in Cn vl (rec_sigma Qf) (get_fstn_args vl vl @
   777                                                          [id vl (vl - 1)]))"
   824                                                          [id vl (vl - 1)]))"
   778 
   825 
   808   apply(rule_tac prime_cn, auto)
   855   apply(rule_tac prime_cn, auto)
   809   apply(case_tac i, auto)
   856   apply(case_tac i, auto)
   810   done
   857   done
   811 
   858 
   812 lemma [simp]:  
   859 lemma [simp]:  
   813   "length ys = Suc n \<Longrightarrow> (take n ys @ [ys ! n, ys ! n]) = ys @ [ys ! n]"
   860   "length ys = Suc n \<Longrightarrow> (take n ys @ [ys ! n, ys ! n]) =  
       
   861                                                   ys @ [ys ! n]"
   814 apply(simp)
   862 apply(simp)
   815 apply(subgoal_tac "\<exists> xs y. ys = xs @ [y]", auto)
   863 apply(subgoal_tac "\<exists> xs y. ys = xs @ [y]", auto)
   816 apply(rule_tac x = "butlast ys" in exI, rule_tac x = "last ys" in exI)
   864 apply(rule_tac x = "butlast ys" in exI, rule_tac x = "last ys" in exI)
   817 apply(case_tac "ys = []", simp_all)
   865 apply(case_tac "ys = []", simp_all)
   818 done
   866 done
   819 
   867 
   820 lemma Maxr_Suc_simp: 
   868 lemma Maxr_Suc_simp: 
   821   "Maxr Rr xs (Suc w) =(if Rr (xs @ [Suc w]) then Suc w else Maxr Rr xs w)"
   869   "Maxr Rr xs (Suc w) =(if Rr (xs @ [Suc w]) then Suc w
       
   870      else Maxr Rr xs w)"
   822 apply(auto simp: Maxr.simps Max.insert)
   871 apply(auto simp: Maxr.simps Max.insert)
   823 apply(rule_tac Max_eqI, auto)
   872 apply(rule_tac Max_eqI, auto)
   824 done
   873 done
   825 
   874 
   826 lemma [simp]: "min (Suc n) n = n" by simp
   875 lemma [simp]: "min (Suc n) n = n" by simp
   827 
   876 
   828 lemma Sigma_0: "\<forall> i \<le> n. (f (xs @ [i]) = 0) \<Longrightarrow> Sigma f (xs @ [n]) = 0"
   877 lemma Sigma_0: "\<forall> i \<le> n. (f (xs @ [i]) = 0) \<Longrightarrow> 
       
   878                               Sigma f (xs @ [n]) = 0"
   829 apply(induct n, simp add: Sigma.simps)
   879 apply(induct n, simp add: Sigma.simps)
   830 apply(simp add: Sigma_Suc_simp_rewrite)
   880 apply(simp add: Sigma_Suc_simp_rewrite)
   831 done
   881 done
   832   
   882   
   833 lemma [elim]: "\<forall>k<Suc w. f (xs @ [k]) = Suc 0 \<Longrightarrow> Sigma f (xs @ [w]) = Suc w"
   883 lemma [elim]: "\<forall>k<Suc w. f (xs @ [k]) = Suc 0
       
   884         \<Longrightarrow> Sigma f (xs @ [w]) = Suc w"
   834 apply(induct w)
   885 apply(induct w)
   835 apply(simp add: Sigma.simps, simp)
   886 apply(simp add: Sigma.simps, simp)
   836 apply(simp add: Sigma.simps)
   887 apply(simp add: Sigma.simps)
   837 done
   888 done
   838 
   889 
   845 apply(case_tac "ma = Suc w", auto)
   896 apply(case_tac "ma = Suc w", auto)
   846 done
   897 done
   847 
   898 
   848 lemma Sigma_Max_lemma: 
   899 lemma Sigma_Max_lemma: 
   849   assumes prrf: "primerec rf (Suc (length xs))"
   900   assumes prrf: "primerec rf (Suc (length xs))"
   850   shows "UF.Sigma (rec_eval (Cn (Suc (Suc (length xs))) rec_not
   901   shows "UF.Sigma (rec_exec (Cn (Suc (Suc (length xs))) rec_not
   851   [rec_all (recf.id (Suc (Suc (length xs))) (length xs))
   902   [rec_all (recf.id (Suc (Suc (length xs))) (length xs))
   852   (Cn (Suc (Suc (Suc (length xs)))) rec_disj
   903   (Cn (Suc (Suc (Suc (length xs)))) rec_disj
   853   [Cn (Suc (Suc (Suc (length xs)))) rec_le
   904   [Cn (Suc (Suc (Suc (length xs)))) rec_le
   854   [recf.id (Suc (Suc (Suc (length xs)))) (Suc (Suc (length xs))), 
   905   [recf.id (Suc (Suc (Suc (length xs)))) (Suc (Suc (length xs))), 
   855   recf.id (Suc (Suc (Suc (length xs)))) (Suc (length xs))],
   906   recf.id (Suc (Suc (Suc (length xs)))) (Suc (length xs))],
   856   Cn (Suc (Suc (Suc (length xs)))) rec_not
   907   Cn (Suc (Suc (Suc (length xs)))) rec_not
   857   [Cn (Suc (Suc (Suc (length xs)))) rf
   908   [Cn (Suc (Suc (Suc (length xs)))) rf
   858   (get_fstn_args (Suc (Suc (Suc (length xs)))) (length xs) @ 
   909   (get_fstn_args (Suc (Suc (Suc (length xs)))) (length xs) @ 
   859   [recf.id (Suc (Suc (Suc (length xs)))) (Suc (Suc (length xs)))])]])]))
   910   [recf.id (Suc (Suc (Suc (length xs)))) (Suc (Suc (length xs)))])]])]))
   860   ((xs @ [w]) @ [w]) =
   911   ((xs @ [w]) @ [w]) =
   861        Maxr (\<lambda>args. 0 < rec_eval rf args) xs w"
   912        Maxr (\<lambda>args. 0 < rec_exec rf args) xs w"
   862 proof -
   913 proof -
   863   let ?rt = "(recf.id (Suc (Suc (length xs))) ((length xs)))"
   914   let ?rt = "(recf.id (Suc (Suc (length xs))) ((length xs)))"
   864   let ?rf1 = "Cn (Suc (Suc (Suc (length xs))))
   915   let ?rf1 = "Cn (Suc (Suc (Suc (length xs))))
   865     rec_le [recf.id (Suc (Suc (Suc (length xs)))) 
   916     rec_le [recf.id (Suc (Suc (Suc (length xs)))) 
   866     ((Suc (Suc (length xs)))), recf.id 
   917     ((Suc (Suc (length xs)))), recf.id 
   874   let ?rf = "Cn (Suc (Suc (Suc (length xs)))) rec_disj [?rf1, ?rf3]"
   925   let ?rf = "Cn (Suc (Suc (Suc (length xs)))) rec_disj [?rf1, ?rf3]"
   875   let ?rq = "rec_all ?rt ?rf"
   926   let ?rq = "rec_all ?rt ?rf"
   876   let ?notrq = "Cn (Suc (Suc (length xs))) rec_not [?rq]"
   927   let ?notrq = "Cn (Suc (Suc (length xs))) rec_not [?rq]"
   877   show "?thesis"
   928   show "?thesis"
   878   proof(auto simp: Maxr.simps)
   929   proof(auto simp: Maxr.simps)
   879     assume h: "\<forall>x\<le>w. rec_eval rf (xs @ [x]) = 0"
   930     assume h: "\<forall>x\<le>w. rec_exec rf (xs @ [x]) = 0"
   880     have "primerec ?rf (Suc (length (xs @ [w, i]))) \<and> 
   931     have "primerec ?rf (Suc (length (xs @ [w, i]))) \<and> 
   881           primerec ?rt (length (xs @ [w, i]))"
   932           primerec ?rt (length (xs @ [w, i]))"
   882       using prrf
   933       using prrf
   883       apply(auto)
   934       apply(auto)
   884       apply(case_tac i, auto)
   935       apply(case_tac i, auto)
   885       apply(case_tac ia, auto simp: h nth_append)
   936       apply(case_tac ia, auto simp: h nth_append)
   886       done
   937       done
   887     hence "Sigma (rec_eval ?notrq) ((xs@[w])@[w]) = 0"
   938     hence "Sigma (rec_exec ?notrq) ((xs@[w])@[w]) = 0"
   888       apply(rule_tac Sigma_0)
   939       apply(rule_tac Sigma_0)
   889       apply(auto simp: rec_eval.simps all_lemma
   940       apply(auto simp: rec_exec.simps all_lemma
   890                        get_fstn_args_take nth_append h)
   941                        get_fstn_args_take nth_append h)
   891       done
   942       done
   892     thus "UF.Sigma (rec_eval ?notrq)
   943     thus "UF.Sigma (rec_exec ?notrq)
   893       (xs @ [w, w]) = 0"
   944       (xs @ [w, w]) = 0"
   894       by simp
   945       by simp
   895   next
   946   next
   896     fix x
   947     fix x
   897     assume h: "x \<le> w" "0 < rec_eval rf (xs @ [x])"
   948     assume h: "x \<le> w" "0 < rec_exec rf (xs @ [x])"
   898     hence "\<exists> ma. Max {y. y \<le> w \<and> 0 < rec_eval rf (xs @ [y])} = ma"
   949     hence "\<exists> ma. Max {y. y \<le> w \<and> 0 < rec_exec rf (xs @ [y])} = ma"
   899       by auto
   950       by auto
   900     from this obtain ma where k1: 
   951     from this obtain ma where k1: 
   901       "Max {y. y \<le> w \<and> 0 < rec_eval rf (xs @ [y])} = ma" ..
   952       "Max {y. y \<le> w \<and> 0 < rec_exec rf (xs @ [y])} = ma" ..
   902     hence k2: "ma \<le> w \<and> 0 < rec_eval rf (xs @ [ma])"
   953     hence k2: "ma \<le> w \<and> 0 < rec_exec rf (xs @ [ma])"
   903       using h
   954       using h
   904       apply(subgoal_tac
   955       apply(subgoal_tac
   905         "Max {y. y \<le> w \<and> 0 < rec_eval rf (xs @ [y])} \<in>  {y. y \<le> w \<and> 0 < rec_eval rf (xs @ [y])}")
   956         "Max {y. y \<le> w \<and> 0 < rec_exec rf (xs @ [y])} \<in>  {y. y \<le> w \<and> 0 < rec_exec rf (xs @ [y])}")
   906       apply(erule_tac CollectE, simp)
   957       apply(erule_tac CollectE, simp)
   907       apply(rule_tac Max_in, auto)
   958       apply(rule_tac Max_in, auto)
   908       done
   959       done
   909     hence k3: "\<forall> k < ma. (rec_eval ?notrq (xs @ [w, k]) = 1)"
   960     hence k3: "\<forall> k < ma. (rec_exec ?notrq (xs @ [w, k]) = 1)"
   910       apply(auto simp: nth_append)
   961       apply(auto simp: nth_append)
   911       apply(subgoal_tac "primerec ?rf (Suc (length (xs @ [w, k]))) \<and> 
   962       apply(subgoal_tac "primerec ?rf (Suc (length (xs @ [w, k]))) \<and> 
   912         primerec ?rt (length (xs @ [w, k]))")
   963         primerec ?rt (length (xs @ [w, k]))")
   913       apply(auto simp: rec_eval.simps all_lemma get_fstn_args_take nth_append)
   964       apply(auto simp: rec_exec.simps all_lemma get_fstn_args_take nth_append)
   914       using prrf
   965       using prrf
   915       apply(case_tac i, auto)
   966       apply(case_tac i, auto)
   916       apply(case_tac ia, auto simp: h nth_append)
   967       apply(case_tac ia, auto simp: h nth_append)
   917       done    
   968       done    
   918     have k4: "\<forall> k \<ge> ma. (rec_eval ?notrq (xs @ [w, k]) = 0)"
   969     have k4: "\<forall> k \<ge> ma. (rec_exec ?notrq (xs @ [w, k]) = 0)"
   919       apply(auto)
   970       apply(auto)
   920       apply(subgoal_tac "primerec ?rf (Suc (length (xs @ [w, k]))) \<and> 
   971       apply(subgoal_tac "primerec ?rf (Suc (length (xs @ [w, k]))) \<and> 
   921         primerec ?rt (length (xs @ [w, k]))")
   972         primerec ?rt (length (xs @ [w, k]))")
   922       apply(auto simp: rec_eval.simps all_lemma get_fstn_args_take nth_append)
   973       apply(auto simp: rec_exec.simps all_lemma get_fstn_args_take nth_append)
   923       apply(subgoal_tac "x \<le> Max {y. y \<le> w \<and> 0 < rec_eval rf (xs @ [y])}",
   974       apply(subgoal_tac "x \<le> Max {y. y \<le> w \<and> 0 < rec_exec rf (xs @ [y])}",
   924         simp add: k1)
   975         simp add: k1)
   925       apply(rule_tac Max_ge, auto)
   976       apply(rule_tac Max_ge, auto)
   926       using prrf
   977       using prrf
   927       apply(case_tac i, auto)
   978       apply(case_tac i, auto)
   928       apply(case_tac ia, auto simp: h nth_append)
   979       apply(case_tac ia, auto simp: h nth_append)
   929       done 
   980       done 
   930     from k3 k4 k1 have "Sigma (rec_eval ?notrq) ((xs @ [w]) @ [w]) = ma"
   981     from k3 k4 k1 have "Sigma (rec_exec ?notrq) ((xs @ [w]) @ [w]) = ma"
   931       apply(rule_tac Sigma_max_point, simp, simp, simp add: k2)
   982       apply(rule_tac Sigma_max_point, simp, simp, simp add: k2)
   932       done
   983       done
   933     from k1 and this show "Sigma (rec_eval ?notrq) (xs @ [w, w]) =
   984     from k1 and this show "Sigma (rec_exec ?notrq) (xs @ [w, w]) =
   934       Max {y. y \<le> w \<and> 0 < rec_eval rf (xs @ [y])}"
   985       Max {y. y \<le> w \<and> 0 < rec_exec rf (xs @ [y])}"
   935       by simp
   986       by simp
   936   qed  
   987   qed  
   937 qed
   988 qed
   938 
   989 
   939 text {*
   990 text {*
   940   The correctness of @{text "rec_maxr"}.
   991   The correctness of @{text "rec_maxr"}.
   941   *}
   992   *}
   942 lemma Maxr_lemma:
   993 lemma Maxr_lemma:
   943  assumes h: "primerec rf (Suc (length xs))"
   994  assumes h: "primerec rf (Suc (length xs))"
   944  shows   "rec_eval (rec_maxr rf) (xs @ [w]) = 
   995  shows   "rec_exec (rec_maxr rf) (xs @ [w]) = 
   945             Maxr (\<lambda> args. 0 < rec_eval rf args) xs w"
   996             Maxr (\<lambda> args. 0 < rec_exec rf args) xs w"
   946 proof -
   997 proof -
   947   from h have "arity rf = Suc (length xs)"
   998   from h have "arity rf = Suc (length xs)"
   948     by auto
   999     by auto
   949   thus "?thesis"
  1000   thus "?thesis"
   950   proof(simp add: rec_eval.simps rec_maxr.simps nth_append get_fstn_args_take)
  1001   proof(simp add: rec_exec.simps rec_maxr.simps nth_append get_fstn_args_take)
   951     let ?rt = "(recf.id (Suc (Suc (length xs))) ((length xs)))"
  1002     let ?rt = "(recf.id (Suc (Suc (length xs))) ((length xs)))"
   952     let ?rf1 = "Cn (Suc (Suc (Suc (length xs))))
  1003     let ?rf1 = "Cn (Suc (Suc (Suc (length xs))))
   953                      rec_le [recf.id (Suc (Suc (Suc (length xs)))) 
  1004                      rec_le [recf.id (Suc (Suc (Suc (length xs)))) 
   954               ((Suc (Suc (length xs)))), recf.id 
  1005               ((Suc (Suc (length xs)))), recf.id 
   955              (Suc (Suc (Suc (length xs)))) ((Suc (length xs)))]"
  1006              (Suc (Suc (Suc (length xs)))) ((Suc (length xs)))]"
   974     from prt and prrf have prrq: "primerec ?rq 
  1025     from prt and prrf have prrq: "primerec ?rq 
   975                                        (Suc (Suc (length xs)))"
  1026                                        (Suc (Suc (length xs)))"
   976       by(erule_tac primerec_all_iff, auto)
  1027       by(erule_tac primerec_all_iff, auto)
   977     hence prnotrp: "primerec ?notrq (Suc (length ((xs @ [w]))))"
  1028     hence prnotrp: "primerec ?notrq (Suc (length ((xs @ [w]))))"
   978       by(rule_tac prime_cn, auto)
  1029       by(rule_tac prime_cn, auto)
   979     have g1: "rec_eval (rec_sigma ?notrq) ((xs @ [w]) @ [w]) 
  1030     have g1: "rec_exec (rec_sigma ?notrq) ((xs @ [w]) @ [w]) 
   980       = Maxr (\<lambda>args. 0 < rec_eval rf args) xs w"
  1031       = Maxr (\<lambda>args. 0 < rec_exec rf args) xs w"
   981       using prnotrp
  1032       using prnotrp
   982       using sigma_lemma
  1033       using sigma_lemma
   983       apply(simp only: sigma_lemma)
  1034       apply(simp only: sigma_lemma)
   984       apply(rule_tac Sigma_Max_lemma)
  1035       apply(rule_tac Sigma_Max_lemma)
   985       apply(simp add: h)
  1036       apply(simp add: h)
   986       done
  1037       done
   987     thus "rec_eval (rec_sigma ?notrq)
  1038     thus "rec_exec (rec_sigma ?notrq)
   988      (xs @ [w, w]) =
  1039      (xs @ [w, w]) =
   989     Maxr (\<lambda>args. 0 < rec_eval rf args) xs w"
  1040     Maxr (\<lambda>args. 0 < rec_exec rf args) xs w"
   990       apply(simp)
  1041       apply(simp)
   991       done
  1042       done
   992   qed
  1043   qed
   993 qed
  1044 qed
   994       
  1045       
   995 text {* 
  1046 text {* 
   996   @{text "quo"} is the formal specification of division.
  1047   @{text "quo"} is the formal specification of division.
   997  *}
  1048  *}
   998 fun quo :: "nat list \<Rightarrow> nat"
  1049 fun quo :: "nat list \<Rightarrow> nat"
   999   where
  1050   where
  1000   "quo [x, y] = 
  1051   "quo [x, y] = (let Rr = 
  1001     (let Rr = (\<lambda> zs. ((zs ! (Suc 0) * zs ! (Suc (Suc 0)) \<le> zs ! 0) \<and> zs ! Suc 0 \<noteq> 0))
  1052                          (\<lambda> zs. ((zs ! (Suc 0) * zs ! (Suc (Suc 0))
  1002      in Maxr Rr [x, y] x)"
  1053                                  \<le> zs ! 0) \<and> zs ! Suc 0 \<noteq> (0::nat)))
       
  1054                  in Maxr Rr [x, y] x)"
  1003  
  1055  
  1004 declare quo.simps[simp del]
  1056 declare quo.simps[simp del]
  1005 
  1057 
  1006 text {*
  1058 text {*
  1007   The following lemmas shows more directly the menaing of @{text "quo"}:
  1059   The following lemmas shows more directly the menaing of @{text "quo"}:
  1034   two arguments are not equal.
  1086   two arguments are not equal.
  1035   *}
  1087   *}
  1036 definition rec_noteq:: "recf"
  1088 definition rec_noteq:: "recf"
  1037   where
  1089   where
  1038   "rec_noteq = Cn (Suc (Suc 0)) rec_not [Cn (Suc (Suc 0)) 
  1090   "rec_noteq = Cn (Suc (Suc 0)) rec_not [Cn (Suc (Suc 0)) 
  1039               rec_eq [id (Suc (Suc 0)) (0), id (Suc (Suc 0)) ((Suc 0))]]"
  1091               rec_eq [id (Suc (Suc 0)) (0), id (Suc (Suc 0)) 
       
  1092                                         ((Suc 0))]]"
  1040 
  1093 
  1041 text {*
  1094 text {*
  1042   The correctness of @{text "rec_noteq"}.
  1095   The correctness of @{text "rec_noteq"}.
  1043   *}
  1096   *}
  1044 lemma noteq_lemma: 
  1097 lemma noteq_lemma: 
  1045   "rec_eval rec_noteq [x, y] = (if x \<noteq> y then 1 else 0)"
  1098   "\<And> x y. rec_exec rec_noteq [x, y] = 
  1046 by(simp add: rec_eval.simps rec_noteq_def)
  1099                (if x \<noteq> y then 1 else 0)"
       
  1100 by(simp add: rec_exec.simps rec_noteq_def)
  1047 
  1101 
  1048 declare noteq_lemma[simp]
  1102 declare noteq_lemma[simp]
  1049 
  1103 
  1050 text {*
  1104 text {*
  1051   @{text "rec_quo"} is the recursive function used to implement @{text "quo"}
  1105   @{text "rec_quo"} is the recursive function used to implement @{text "quo"}
  1077 apply(rule_tac prime_cn, auto)+
  1131 apply(rule_tac prime_cn, auto)+
  1078 apply(case_tac i, auto intro: prime_id)
  1132 apply(case_tac i, auto intro: prime_id)
  1079 done
  1133 done
  1080 
  1134 
  1081 
  1135 
  1082 lemma quo_lemma1: "rec_eval rec_quo [x, y] = quo [x, y]"
  1136 lemma quo_lemma1: "rec_exec rec_quo [x, y] = quo [x, y]"
  1083 proof(simp add: rec_eval.simps rec_quo_def)
  1137 proof(simp add: rec_exec.simps rec_quo_def)
  1084   let ?rR = "(Cn (Suc (Suc (Suc 0))) rec_conj
  1138   let ?rR = "(Cn (Suc (Suc (Suc 0))) rec_conj
  1085                [Cn (Suc (Suc (Suc 0))) rec_le
  1139                [Cn (Suc (Suc (Suc 0))) rec_le
  1086                    [Cn (Suc (Suc (Suc 0))) rec_mult 
  1140                    [Cn (Suc (Suc (Suc 0))) rec_mult 
  1087                [recf.id (Suc (Suc (Suc 0))) (Suc (0)), 
  1141                [recf.id (Suc (Suc (Suc 0))) (Suc (0)), 
  1088                 recf.id (Suc (Suc (Suc 0))) (Suc (Suc (0)))],
  1142                 recf.id (Suc (Suc (Suc 0))) (Suc (Suc (0)))],
  1089                  recf.id (Suc (Suc (Suc 0))) (0)],  
  1143                  recf.id (Suc (Suc (Suc 0))) (0)],  
  1090           Cn (Suc (Suc (Suc 0))) rec_noteq 
  1144           Cn (Suc (Suc (Suc 0))) rec_noteq 
  1091                               [recf.id (Suc (Suc (Suc 0))) 
  1145                               [recf.id (Suc (Suc (Suc 0))) 
  1092              (Suc (0)), Cn (Suc (Suc (Suc 0))) (constn 0) 
  1146              (Suc (0)), Cn (Suc (Suc (Suc 0))) (constn 0) 
  1093                       [recf.id (Suc (Suc (Suc 0))) (0)]]])"
  1147                       [recf.id (Suc (Suc (Suc 0))) (0)]]])"
  1094   have "rec_eval (rec_maxr ?rR) ([x, y]@ [ x]) = Maxr (\<lambda> args. 0 < rec_eval ?rR args) [x, y] x"
  1148   have "rec_exec (rec_maxr ?rR) ([x, y]@ [ x]) = Maxr (\<lambda> args. 0 < rec_exec ?rR args) [x, y] x"
  1095   proof(rule_tac Maxr_lemma, simp)
  1149   proof(rule_tac Maxr_lemma, simp)
  1096     show "primerec ?rR (Suc (Suc (Suc 0)))"
  1150     show "primerec ?rR (Suc (Suc (Suc 0)))"
  1097       apply(auto)
  1151       apply(auto)
  1098       apply(case_tac i, auto)
  1152       apply(case_tac i, auto)
  1099       apply(case_tac [!] ia, auto)
  1153       apply(case_tac [!] ia, auto)
  1100       apply(case_tac i, auto)
  1154       apply(case_tac i, auto)
  1101       done
  1155       done
  1102   qed
  1156   qed
  1103   hence g1: "rec_eval (rec_maxr ?rR) ([x, y,  x]) =
  1157   hence g1: "rec_exec (rec_maxr ?rR) ([x, y,  x]) =
  1104              Maxr (\<lambda> args. if rec_eval ?rR args = 0 then False
  1158              Maxr (\<lambda> args. if rec_exec ?rR args = 0 then False
  1105                            else True) [x, y] x" 
  1159                            else True) [x, y] x" 
  1106     by simp
  1160     by simp
  1107   have g2: "Maxr (\<lambda> args. if rec_eval ?rR args = 0 then False
  1161   have g2: "Maxr (\<lambda> args. if rec_exec ?rR args = 0 then False
  1108                            else True) [x, y] x = quo [x, y]"
  1162                            else True) [x, y] x = quo [x, y]"
  1109     apply(simp add: rec_eval.simps)
  1163     apply(simp add: rec_exec.simps)
  1110     apply(simp add: Maxr.simps quo.simps, auto)
  1164     apply(simp add: Maxr.simps quo.simps, auto)
  1111     done
  1165     done
  1112   from g1 and g2 show 
  1166   from g1 and g2 show 
  1113     "rec_eval (rec_maxr ?rR) ([x, y,  x]) = quo [x, y]"
  1167     "rec_exec (rec_maxr ?rR) ([x, y,  x]) = quo [x, y]"
  1114     by simp
  1168     by simp
  1115 qed
  1169 qed
  1116 
  1170 
  1117 text {*
  1171 text {*
  1118   The correctness of @{text "quo"}.
  1172   The correctness of @{text "quo"}.
  1119   *}
  1173   *}
  1120 lemma quo_lemma2: "rec_eval rec_quo [x, y] = x div y"
  1174 lemma quo_lemma2: "rec_exec rec_quo [x, y] = x div y"
  1121   using quo_lemma1[of x y] quo_div[of x y]
  1175   using quo_lemma1[of x y] quo_div[of x y]
  1122   by simp
  1176   by simp
  1123  
  1177  
  1124 text {* 
  1178 text {* 
  1125   @{text "rec_mod"} is the recursive function used to implement 
  1179   @{text "rec_mod"} is the recursive function used to implement 
  1132                                                      (Suc (0))]]"
  1186                                                      (Suc (0))]]"
  1133 
  1187 
  1134 text {*
  1188 text {*
  1135   The correctness of @{text "rec_mod"}:
  1189   The correctness of @{text "rec_mod"}:
  1136   *}
  1190   *}
  1137 lemma mod_lemma: "rec_eval rec_mod [x, y] = (x mod y)"
  1191 lemma mod_lemma: "\<And> x y. rec_exec rec_mod [x, y] = (x mod y)"
  1138 proof(simp add: rec_eval.simps rec_mod_def quo_lemma2)
  1192 proof(simp add: rec_exec.simps rec_mod_def quo_lemma2)
  1139   fix x y
  1193   fix x y
  1140   show "x - x div y * y = x mod (y::nat)"
  1194   show "x - x div y * y = x mod (y::nat)"
  1141     using mod_div_equality2[of y x]
  1195     using mod_div_equality2[of y x]
  1142     apply(subgoal_tac "y * (x div y) = (x div y ) * y", arith, simp)
  1196     apply(subgoal_tac "y * (x div y) = (x div y ) * y", arith, simp)
  1143     done
  1197     done
  1144 qed
  1198 qed
  1145 
  1199 
  1146 section {* Embranch Function *}
  1200 text{* lemmas for embranch function*}
  1147 
       
  1148 type_synonym ftype = "nat list \<Rightarrow> nat"
  1201 type_synonym ftype = "nat list \<Rightarrow> nat"
  1149 type_synonym rtype = "nat list \<Rightarrow> bool"
  1202 type_synonym rtype = "nat list \<Rightarrow> bool"
  1150 
  1203 
  1151 text {*
  1204 text {*
  1152   The specifation of the mutli-way branching statement on
  1205   The specifation of the mutli-way branching statement on
  1176           rec_embranch' ((rg, rc) # rgcs) vl)"
  1229           rec_embranch' ((rg, rc) # rgcs) vl)"
  1177 
  1230 
  1178 declare Embranch.simps[simp del] rec_embranch.simps[simp del]
  1231 declare Embranch.simps[simp del] rec_embranch.simps[simp del]
  1179 
  1232 
  1180 lemma embranch_all0: 
  1233 lemma embranch_all0: 
  1181   "\<lbrakk>\<forall> j < length rcs. rec_eval (rcs ! j) xs = 0;
  1234   "\<lbrakk>\<forall> j < length rcs. rec_exec (rcs ! j) xs = 0;
  1182     length rgs = length rcs;  
  1235     length rgs = length rcs;  
  1183   rcs \<noteq> []; 
  1236   rcs \<noteq> []; 
  1184   list_all (\<lambda> rf. primerec rf (length xs)) (rgs @ rcs)\<rbrakk>  \<Longrightarrow> 
  1237   list_all (\<lambda> rf. primerec rf (length xs)) (rgs @ rcs)\<rbrakk>  \<Longrightarrow> 
  1185   rec_eval (rec_embranch (zip rgs rcs)) xs = 0"
  1238   rec_exec (rec_embranch (zip rgs rcs)) xs = 0"
  1186 proof(induct rcs arbitrary: rgs, simp, case_tac rgs, simp)
  1239 proof(induct rcs arbitrary: rgs, simp, case_tac rgs, simp)
  1187   fix a rcs rgs aa list
  1240   fix a rcs rgs aa list
  1188   assume ind: 
  1241   assume ind: 
  1189     "\<And>rgs. \<lbrakk>\<forall>j<length rcs. rec_eval (rcs ! j) xs = 0; 
  1242     "\<And>rgs. \<lbrakk>\<forall>j<length rcs. rec_exec (rcs ! j) xs = 0; 
  1190              length rgs = length rcs; rcs \<noteq> []; 
  1243              length rgs = length rcs; rcs \<noteq> []; 
  1191             list_all (\<lambda>rf. primerec rf (length xs)) (rgs @ rcs)\<rbrakk> \<Longrightarrow> 
  1244             list_all (\<lambda>rf. primerec rf (length xs)) (rgs @ rcs)\<rbrakk> \<Longrightarrow> 
  1192                       rec_eval (rec_embranch (zip rgs rcs)) xs = 0"
  1245                       rec_exec (rec_embranch (zip rgs rcs)) xs = 0"
  1193   and h:  "\<forall>j<length (a # rcs). rec_eval ((a # rcs) ! j) xs = 0"
  1246   and h:  "\<forall>j<length (a # rcs). rec_exec ((a # rcs) ! j) xs = 0"
  1194   "length rgs = length (a # rcs)" 
  1247   "length rgs = length (a # rcs)" 
  1195     "a # rcs \<noteq> []" 
  1248     "a # rcs \<noteq> []" 
  1196     "list_all (\<lambda>rf. primerec rf (length xs)) (rgs @ a # rcs)"
  1249     "list_all (\<lambda>rf. primerec rf (length xs)) (rgs @ a # rcs)"
  1197     "rgs = aa # list"
  1250     "rgs = aa # list"
  1198   have g: "rcs \<noteq> [] \<Longrightarrow> rec_eval (rec_embranch (zip list rcs)) xs = 0"
  1251   have g: "rcs \<noteq> [] \<Longrightarrow> rec_exec (rec_embranch (zip list rcs)) xs = 0"
  1199     using h
  1252     using h
  1200     by(rule_tac ind, auto)
  1253     by(rule_tac ind, auto)
  1201   show "rec_eval (rec_embranch (zip rgs (a # rcs))) xs = 0"
  1254   show "rec_exec (rec_embranch (zip rgs (a # rcs))) xs = 0"
  1202   proof(case_tac "rcs = []", simp)
  1255   proof(case_tac "rcs = []", simp)
  1203     show "rec_eval (rec_embranch (zip rgs [a])) xs = 0"
  1256     show "rec_exec (rec_embranch (zip rgs [a])) xs = 0"
  1204       using h
  1257       using h
  1205       apply(simp add: rec_embranch.simps rec_eval.simps)
  1258       apply(simp add: rec_embranch.simps rec_exec.simps)
  1206       apply(erule_tac x = 0 in allE, simp)
  1259       apply(erule_tac x = 0 in allE, simp)
  1207       done
  1260       done
  1208   next
  1261   next
  1209     assume "rcs \<noteq> []"
  1262     assume "rcs \<noteq> []"
  1210     hence "rec_eval (rec_embranch (zip list rcs)) xs = 0"
  1263     hence "rec_exec (rec_embranch (zip list rcs)) xs = 0"
  1211       using g by simp
  1264       using g by simp
  1212     thus "rec_eval (rec_embranch (zip rgs (a # rcs))) xs = 0"
  1265     thus "rec_exec (rec_embranch (zip rgs (a # rcs))) xs = 0"
  1213       using h
  1266       using h
  1214       apply(simp add: rec_embranch.simps rec_eval.simps)
  1267       apply(simp add: rec_embranch.simps rec_exec.simps)
  1215       apply(case_tac rcs,
  1268       apply(case_tac rcs,
  1216         auto simp: rec_eval.simps rec_embranch.simps)
  1269         auto simp: rec_exec.simps rec_embranch.simps)
  1217       apply(case_tac list,
  1270       apply(case_tac list,
  1218         auto simp: rec_eval.simps rec_embranch.simps)
  1271         auto simp: rec_exec.simps rec_embranch.simps)
  1219       done
  1272       done
  1220   qed
  1273   qed
  1221 qed     
  1274 qed     
  1222  
  1275  
  1223 
  1276 
  1224 lemma embranch_exec_0: "\<lbrakk>rec_eval aa xs = 0; zip rgs list \<noteq> []; 
  1277 lemma embranch_exec_0: "\<lbrakk>rec_exec aa xs = 0; zip rgs list \<noteq> []; 
  1225        list_all (\<lambda> rf. primerec rf (length xs)) ([a, aa] @ rgs @ list)\<rbrakk>
  1278        list_all (\<lambda> rf. primerec rf (length xs)) ([a, aa] @ rgs @ list)\<rbrakk>
  1226        \<Longrightarrow> rec_eval (rec_embranch ((a, aa) # zip rgs list)) xs
  1279        \<Longrightarrow> rec_exec (rec_embranch ((a, aa) # zip rgs list)) xs
  1227          = rec_eval (rec_embranch (zip rgs list)) xs"
  1280          = rec_exec (rec_embranch (zip rgs list)) xs"
  1228 apply(simp add: rec_eval.simps rec_embranch.simps)
  1281 apply(simp add: rec_exec.simps rec_embranch.simps)
  1229 apply(case_tac "zip rgs list", simp, case_tac ab, 
  1282 apply(case_tac "zip rgs list", simp, case_tac ab, 
  1230   simp add: rec_embranch.simps rec_eval.simps)
  1283   simp add: rec_embranch.simps rec_exec.simps)
  1231 apply(subgoal_tac "arity a = length xs", auto)
  1284 apply(subgoal_tac "arity a = length xs", auto)
  1232 apply(subgoal_tac "arity aaa = length xs", auto)
  1285 apply(subgoal_tac "arity aaa = length xs", auto)
  1233 apply(case_tac rgs, simp, case_tac list, simp, simp)
  1286 apply(case_tac rgs, simp, case_tac list, simp, simp)
  1234 done
  1287 done
  1235 
  1288 
  1242 apply(case_tac xs, simp, simp)
  1295 apply(case_tac xs, simp, simp)
  1243 done
  1296 done
  1244 
  1297 
  1245 lemma Embranch_0:  
  1298 lemma Embranch_0:  
  1246   "\<lbrakk>length rgs = k; length rcs = k; k > 0; 
  1299   "\<lbrakk>length rgs = k; length rcs = k; k > 0; 
  1247   \<forall> j < k. rec_eval (rcs ! j) xs = 0\<rbrakk> \<Longrightarrow>
  1300   \<forall> j < k. rec_exec (rcs ! j) xs = 0\<rbrakk> \<Longrightarrow>
  1248   Embranch (zip (map rec_eval rgs) (map (\<lambda>r args. 0 < rec_eval r args) rcs)) xs = 0"
  1301   Embranch (zip (map rec_exec rgs) (map (\<lambda>r args. 0 < rec_exec r args) rcs)) xs = 0"
  1249 proof(induct rgs arbitrary: rcs k, simp, simp)
  1302 proof(induct rgs arbitrary: rcs k, simp, simp)
  1250   fix a rgs rcs k
  1303   fix a rgs rcs k
  1251   assume ind: 
  1304   assume ind: 
  1252     "\<And>rcs k. \<lbrakk>length rgs = k; length rcs = k; 0 < k; \<forall>j<k. rec_eval (rcs ! j) xs = 0\<rbrakk> 
  1305     "\<And>rcs k. \<lbrakk>length rgs = k; length rcs = k; 0 < k; \<forall>j<k. rec_exec (rcs ! j) xs = 0\<rbrakk> 
  1253     \<Longrightarrow> Embranch (zip (map rec_eval rgs) (map (\<lambda>r args. 0 < rec_eval r args) rcs)) xs = 0"
  1306     \<Longrightarrow> Embranch (zip (map rec_exec rgs) (map (\<lambda>r args. 0 < rec_exec r args) rcs)) xs = 0"
  1254   and h: "Suc (length rgs) = k" "length rcs = k"
  1307   and h: "Suc (length rgs) = k" "length rcs = k"
  1255     "\<forall>j<k. rec_eval (rcs ! j) xs = 0"
  1308     "\<forall>j<k. rec_exec (rcs ! j) xs = 0"
  1256   from h show  
  1309   from h show  
  1257     "Embranch (zip (rec_eval a # map rec_eval rgs) 
  1310     "Embranch (zip (rec_exec a # map rec_exec rgs) 
  1258            (map (\<lambda>r args. 0 < rec_eval r args) rcs)) xs = 0"
  1311            (map (\<lambda>r args. 0 < rec_exec r args) rcs)) xs = 0"
  1259     apply(case_tac rcs, simp, case_tac "rgs = []", simp)
  1312     apply(case_tac rcs, simp, case_tac "rgs = []", simp)
  1260     apply(simp add: Embranch.simps)
  1313     apply(simp add: Embranch.simps)
  1261     apply(erule_tac x = 0 in allE, simp)
  1314     apply(erule_tac x = 0 in allE, simp)
  1262     apply(simp add: Embranch.simps)
  1315     apply(simp add: Embranch.simps)
  1263     apply(erule_tac x = 0 in all_dupE, simp)
  1316     apply(erule_tac x = 0 in all_dupE, simp)
  1271   *}
  1324   *}
  1272 lemma embranch_lemma:
  1325 lemma embranch_lemma:
  1273   assumes branch_num:
  1326   assumes branch_num:
  1274   "length rgs = n" "length rcs = n" "n > 0"
  1327   "length rgs = n" "length rcs = n" "n > 0"
  1275   and partition: 
  1328   and partition: 
  1276   "(\<exists> i < n. (rec_eval (rcs ! i) xs = 1 \<and> (\<forall> j < n. j \<noteq> i \<longrightarrow> 
  1329   "(\<exists> i < n. (rec_exec (rcs ! i) xs = 1 \<and> (\<forall> j < n. j \<noteq> i \<longrightarrow> 
  1277                                       rec_eval (rcs ! j) xs = 0)))"
  1330                                       rec_exec (rcs ! j) xs = 0)))"
  1278   and prime_all: "list_all (\<lambda> rf. primerec rf (length xs)) (rgs @ rcs)"
  1331   and prime_all: "list_all (\<lambda> rf. primerec rf (length xs)) (rgs @ rcs)"
  1279   shows "rec_eval (rec_embranch (zip rgs rcs)) xs =
  1332   shows "rec_exec (rec_embranch (zip rgs rcs)) xs =
  1280                   Embranch (zip (map rec_eval rgs) 
  1333                   Embranch (zip (map rec_exec rgs) 
  1281                      (map (\<lambda> r args. 0 < rec_eval r args) rcs)) xs"
  1334                      (map (\<lambda> r args. 0 < rec_exec r args) rcs)) xs"
  1282   using branch_num partition prime_all
  1335   using branch_num partition prime_all
  1283 proof(induct rgs arbitrary: rcs n, simp)
  1336 proof(induct rgs arbitrary: rcs n, simp)
  1284   fix a rgs rcs n
  1337   fix a rgs rcs n
  1285   assume ind: 
  1338   assume ind: 
  1286     "\<And>rcs n. \<lbrakk>length rgs = n; length rcs = n; 0 < n;
  1339     "\<And>rcs n. \<lbrakk>length rgs = n; length rcs = n; 0 < n;
  1287     \<exists>i<n. rec_eval (rcs ! i) xs = 1 \<and> (\<forall>j<n. j \<noteq> i \<longrightarrow> rec_eval (rcs ! j) xs = 0);
  1340     \<exists>i<n. rec_exec (rcs ! i) xs = 1 \<and> (\<forall>j<n. j \<noteq> i \<longrightarrow> rec_exec (rcs ! j) xs = 0);
  1288     list_all (\<lambda>rf. primerec rf (length xs)) (rgs @ rcs)\<rbrakk>
  1341     list_all (\<lambda>rf. primerec rf (length xs)) (rgs @ rcs)\<rbrakk>
  1289     \<Longrightarrow> rec_eval (rec_embranch (zip rgs rcs)) xs =
  1342     \<Longrightarrow> rec_exec (rec_embranch (zip rgs rcs)) xs =
  1290     Embranch (zip (map rec_eval rgs) (map (\<lambda>r args. 0 < rec_eval r args) rcs)) xs"
  1343     Embranch (zip (map rec_exec rgs) (map (\<lambda>r args. 0 < rec_exec r args) rcs)) xs"
  1291   and h: "length (a # rgs) = n" "length (rcs::recf list) = n" "0 < n"
  1344   and h: "length (a # rgs) = n" "length (rcs::recf list) = n" "0 < n"
  1292          " \<exists>i<n. rec_eval (rcs ! i) xs = 1 \<and> 
  1345          " \<exists>i<n. rec_exec (rcs ! i) xs = 1 \<and> 
  1293          (\<forall>j<n. j \<noteq> i \<longrightarrow> rec_eval (rcs ! j) xs = 0)" 
  1346          (\<forall>j<n. j \<noteq> i \<longrightarrow> rec_exec (rcs ! j) xs = 0)" 
  1294     "list_all (\<lambda>rf. primerec rf (length xs)) ((a # rgs) @ rcs)"
  1347     "list_all (\<lambda>rf. primerec rf (length xs)) ((a # rgs) @ rcs)"
  1295   from h show "rec_eval (rec_embranch (zip (a # rgs) rcs)) xs =
  1348   from h show "rec_exec (rec_embranch (zip (a # rgs) rcs)) xs =
  1296     Embranch (zip (map rec_eval (a # rgs)) (map (\<lambda>r args. 
  1349     Embranch (zip (map rec_exec (a # rgs)) (map (\<lambda>r args. 
  1297                 0 < rec_eval r args) rcs)) xs"
  1350                 0 < rec_exec r args) rcs)) xs"
  1298     apply(case_tac rcs, simp, simp)
  1351     apply(case_tac rcs, simp, simp)
  1299     apply(case_tac "rec_eval aa xs = 0")
  1352     apply(case_tac "rec_exec aa xs = 0")
  1300     apply(case_tac [!] "zip rgs list = []", simp)
  1353     apply(case_tac [!] "zip rgs list = []", simp)
  1301     apply(subgoal_tac "rgs = [] \<and> list = []", simp add: Embranch.simps rec_eval.simps rec_embranch.simps)
  1354     apply(subgoal_tac "rgs = [] \<and> list = []", simp add: Embranch.simps rec_exec.simps rec_embranch.simps)
  1302     apply(rule_tac  zip_null_iff, simp, simp, simp)
  1355     apply(rule_tac  zip_null_iff, simp, simp, simp)
  1303   proof -
  1356   proof -
  1304     fix aa list
  1357     fix aa list
  1305     assume g:
  1358     assume g:
  1306       "Suc (length rgs) = n" "Suc (length list) = n" 
  1359       "Suc (length rgs) = n" "Suc (length list) = n" 
  1307       "\<exists>i<n. rec_eval ((aa # list) ! i) xs = Suc 0 \<and> 
  1360       "\<exists>i<n. rec_exec ((aa # list) ! i) xs = Suc 0 \<and> 
  1308           (\<forall>j<n. j \<noteq> i \<longrightarrow> rec_eval ((aa # list) ! j) xs = 0)"
  1361           (\<forall>j<n. j \<noteq> i \<longrightarrow> rec_exec ((aa # list) ! j) xs = 0)"
  1309       "primerec a (length xs) \<and> 
  1362       "primerec a (length xs) \<and> 
  1310       list_all (\<lambda>rf. primerec rf (length xs)) rgs \<and>
  1363       list_all (\<lambda>rf. primerec rf (length xs)) rgs \<and>
  1311       primerec aa (length xs) \<and> 
  1364       primerec aa (length xs) \<and> 
  1312       list_all (\<lambda>rf. primerec rf (length xs)) list"
  1365       list_all (\<lambda>rf. primerec rf (length xs)) list"
  1313       "rec_eval aa xs = 0" "rcs = aa # list" "zip rgs list \<noteq> []"
  1366       "rec_exec aa xs = 0" "rcs = aa # list" "zip rgs list \<noteq> []"
  1314     have "rec_eval (rec_embranch ((a, aa) # zip rgs list)) xs
  1367     have "rec_exec (rec_embranch ((a, aa) # zip rgs list)) xs
  1315         = rec_eval (rec_embranch (zip rgs list)) xs"
  1368         = rec_exec (rec_embranch (zip rgs list)) xs"
  1316       apply(rule embranch_exec_0, simp_all add: g)
  1369       apply(rule embranch_exec_0, simp_all add: g)
  1317       done
  1370       done
  1318     from g and this show "rec_eval (rec_embranch ((a, aa) # zip rgs list)) xs =
  1371     from g and this show "rec_exec (rec_embranch ((a, aa) # zip rgs list)) xs =
  1319          Embranch ((rec_eval a, \<lambda>args. 0 < rec_eval aa args) # 
  1372          Embranch ((rec_exec a, \<lambda>args. 0 < rec_exec aa args) # 
  1320            zip (map rec_eval rgs) (map (\<lambda>r args. 0 < rec_eval r args) list)) xs"
  1373            zip (map rec_exec rgs) (map (\<lambda>r args. 0 < rec_exec r args) list)) xs"
  1321       apply(simp add: Embranch.simps)
  1374       apply(simp add: Embranch.simps)
  1322       apply(rule_tac n = "n - Suc 0" in ind)
  1375       apply(rule_tac n = "n - Suc 0" in ind)
  1323       apply(case_tac n, simp, simp)
  1376       apply(case_tac n, simp, simp)
  1324       apply(case_tac n, simp, simp)
  1377       apply(case_tac n, simp, simp)
  1325       apply(case_tac n, simp, simp add: zip_null_gr )
  1378       apply(case_tac n, simp, simp add: zip_null_gr )
  1329       apply(rule_tac allI, erule_tac x = "Suc j" in allE, simp)
  1382       apply(rule_tac allI, erule_tac x = "Suc j" in allE, simp)
  1330       done
  1383       done
  1331   next
  1384   next
  1332     fix aa list
  1385     fix aa list
  1333     assume g: "Suc (length rgs) = n" "Suc (length list) = n"
  1386     assume g: "Suc (length rgs) = n" "Suc (length list) = n"
  1334       "\<exists>i<n. rec_eval ((aa # list) ! i) xs = Suc 0 \<and> 
  1387       "\<exists>i<n. rec_exec ((aa # list) ! i) xs = Suc 0 \<and> 
  1335       (\<forall>j<n. j \<noteq> i \<longrightarrow> rec_eval ((aa # list) ! j) xs = 0)"
  1388       (\<forall>j<n. j \<noteq> i \<longrightarrow> rec_exec ((aa # list) ! j) xs = 0)"
  1336       "primerec a (length xs) \<and> list_all (\<lambda>rf. primerec rf (length xs)) rgs \<and>
  1389       "primerec a (length xs) \<and> list_all (\<lambda>rf. primerec rf (length xs)) rgs \<and>
  1337       primerec aa (length xs) \<and> list_all (\<lambda>rf. primerec rf (length xs)) list"
  1390       primerec aa (length xs) \<and> list_all (\<lambda>rf. primerec rf (length xs)) list"
  1338     "rcs = aa # list" "rec_eval aa xs \<noteq> 0" "zip rgs list = []"
  1391     "rcs = aa # list" "rec_exec aa xs \<noteq> 0" "zip rgs list = []"
  1339     thus "rec_eval (rec_embranch ((a, aa) # zip rgs list)) xs = 
  1392     thus "rec_exec (rec_embranch ((a, aa) # zip rgs list)) xs = 
  1340         Embranch ((rec_eval a, \<lambda>args. 0 < rec_eval aa args) # 
  1393         Embranch ((rec_exec a, \<lambda>args. 0 < rec_exec aa args) # 
  1341        zip (map rec_eval rgs) (map (\<lambda>r args. 0 < rec_eval r args) list)) xs"
  1394        zip (map rec_exec rgs) (map (\<lambda>r args. 0 < rec_exec r args) list)) xs"
  1342       apply(subgoal_tac "rgs = [] \<and> list = []", simp)
  1395       apply(subgoal_tac "rgs = [] \<and> list = []", simp)
  1343       prefer 2
  1396       prefer 2
  1344       apply(rule_tac zip_null_iff, simp, simp, simp)
  1397       apply(rule_tac zip_null_iff, simp, simp, simp)
  1345       apply(simp add: rec_eval.simps rec_embranch.simps Embranch.simps, auto)
  1398       apply(simp add: rec_exec.simps rec_embranch.simps Embranch.simps, auto)
  1346       done
  1399       done
  1347   next
  1400   next
  1348     fix aa list
  1401     fix aa list
  1349     assume g: "Suc (length rgs) = n" "Suc (length list) = n"
  1402     assume g: "Suc (length rgs) = n" "Suc (length list) = n"
  1350       "\<exists>i<n. rec_eval ((aa # list) ! i) xs = Suc 0 \<and>  
  1403       "\<exists>i<n. rec_exec ((aa # list) ! i) xs = Suc 0 \<and>  
  1351            (\<forall>j<n. j \<noteq> i \<longrightarrow> rec_eval ((aa # list) ! j) xs = 0)"
  1404            (\<forall>j<n. j \<noteq> i \<longrightarrow> rec_exec ((aa # list) ! j) xs = 0)"
  1352       "primerec a (length xs) \<and> list_all (\<lambda>rf. primerec rf (length xs)) rgs
  1405       "primerec a (length xs) \<and> list_all (\<lambda>rf. primerec rf (length xs)) rgs
  1353       \<and> primerec aa (length xs) \<and> list_all (\<lambda>rf. primerec rf (length xs)) list"
  1406       \<and> primerec aa (length xs) \<and> list_all (\<lambda>rf. primerec rf (length xs)) list"
  1354       "rcs = aa # list" "rec_eval aa xs \<noteq> 0" "zip rgs list \<noteq> []"
  1407       "rcs = aa # list" "rec_exec aa xs \<noteq> 0" "zip rgs list \<noteq> []"
  1355     have "rec_eval aa xs =  Suc 0"
  1408     have "rec_exec aa xs =  Suc 0"
  1356       using g
  1409       using g
  1357       apply(case_tac "rec_eval aa xs", simp, auto)
  1410       apply(case_tac "rec_exec aa xs", simp, auto)
  1358       done      
  1411       done      
  1359     moreover have "rec_eval (rec_embranch' (zip rgs list) (length xs)) xs = 0"
  1412     moreover have "rec_exec (rec_embranch' (zip rgs list) (length xs)) xs = 0"
  1360     proof -
  1413     proof -
  1361       have "rec_embranch' (zip rgs list) (length xs) = rec_embranch (zip rgs list)"
  1414       have "rec_embranch' (zip rgs list) (length xs) = rec_embranch (zip rgs list)"
  1362         using g
  1415         using g
  1363         apply(case_tac "zip rgs list", simp, case_tac ab)
  1416         apply(case_tac "zip rgs list", simp, case_tac ab)
  1364         apply(simp add: rec_embranch.simps)
  1417         apply(simp add: rec_embranch.simps)
  1365         apply(subgoal_tac "arity aaa = length xs", simp, auto)
  1418         apply(subgoal_tac "arity aaa = length xs", simp, auto)
  1366         apply(case_tac rgs, simp, simp, case_tac list, simp, simp)
  1419         apply(case_tac rgs, simp, simp, case_tac list, simp, simp)
  1367         done
  1420         done
  1368       moreover have "rec_eval (rec_embranch (zip rgs list)) xs = 0"
  1421       moreover have "rec_exec (rec_embranch (zip rgs list)) xs = 0"
  1369       proof(rule embranch_all0)
  1422       proof(rule embranch_all0)
  1370         show " \<forall>j<length list. rec_eval (list ! j) xs = 0"
  1423         show " \<forall>j<length list. rec_exec (list ! j) xs = 0"
  1371           using g
  1424           using g
  1372           apply(auto)
  1425           apply(auto)
  1373           apply(case_tac i, simp)
  1426           apply(case_tac i, simp)
  1374           apply(erule_tac x = "Suc j" in allE, simp)
  1427           apply(erule_tac x = "Suc j" in allE, simp)
  1375           apply(simp)
  1428           apply(simp)
  1389         show "list_all (\<lambda>rf. primerec rf (length xs)) (rgs @ list)"
  1442         show "list_all (\<lambda>rf. primerec rf (length xs)) (rgs @ list)"
  1390           using g
  1443           using g
  1391           apply auto
  1444           apply auto
  1392           done
  1445           done
  1393       qed
  1446       qed
  1394       ultimately show "rec_eval (rec_embranch' (zip rgs list) (length xs)) xs = 0"
  1447       ultimately show "rec_exec (rec_embranch' (zip rgs list) (length xs)) xs = 0"
  1395         by simp
  1448         by simp
  1396     qed
  1449     qed
  1397     moreover have 
  1450     moreover have 
  1398       "Embranch (zip (map rec_eval rgs) 
  1451       "Embranch (zip (map rec_exec rgs) 
  1399           (map (\<lambda>r args. 0 < rec_eval r args) list)) xs = 0"
  1452           (map (\<lambda>r args. 0 < rec_exec r args) list)) xs = 0"
  1400       using g
  1453       using g
  1401       apply(rule_tac k = "length rgs" in Embranch_0)
  1454       apply(rule_tac k = "length rgs" in Embranch_0)
  1402       apply(simp, case_tac n, simp, simp)
  1455       apply(simp, case_tac n, simp, simp)
  1403       apply(case_tac rgs, simp, simp)
  1456       apply(case_tac rgs, simp, simp)
  1404       apply(auto)
  1457       apply(auto)
  1409       done
  1462       done
  1410     moreover have "arity a = length xs"
  1463     moreover have "arity a = length xs"
  1411       using g
  1464       using g
  1412       apply(auto)
  1465       apply(auto)
  1413       done
  1466       done
  1414     ultimately show "rec_eval (rec_embranch ((a, aa) # zip rgs list)) xs = 
  1467     ultimately show "rec_exec (rec_embranch ((a, aa) # zip rgs list)) xs = 
  1415       Embranch ((rec_eval a, \<lambda>args. 0 < rec_eval aa args) #
  1468       Embranch ((rec_exec a, \<lambda>args. 0 < rec_exec aa args) #
  1416            zip (map rec_eval rgs) (map (\<lambda>r args. 0 < rec_eval r args) list)) xs"
  1469            zip (map rec_exec rgs) (map (\<lambda>r args. 0 < rec_exec r args) list)) xs"
  1417       apply(simp add: rec_eval.simps rec_embranch.simps Embranch.simps)
  1470       apply(simp add: rec_exec.simps rec_embranch.simps Embranch.simps)
  1418       done
  1471       done
  1419   qed
  1472   qed
  1420 qed
  1473 qed
  1421 
  1474 
       
  1475 text{* 
       
  1476   @{text "prime n"} means @{text "n"} is a prime number.
       
  1477 *}
       
  1478 fun Prime :: "nat \<Rightarrow> bool"
       
  1479   where
       
  1480   "Prime x = (1 < x \<and> (\<forall> u < x. (\<forall> v < x. u * v \<noteq> x)))"
       
  1481 
       
  1482 declare Prime.simps [simp del]
  1422 
  1483 
  1423 lemma primerec_all1: 
  1484 lemma primerec_all1: 
  1424   "primerec (rec_all rt rf) n \<Longrightarrow> primerec rt n"
  1485   "primerec (rec_all rt rf) n \<Longrightarrow> primerec rt n"
  1425   by (simp add: primerec_all)
  1486   by (simp add: primerec_all)
  1426 
  1487 
  1442        [Cn 3 rec_mult [id 3 1, id 3 2], id 3 0]))]"
  1503        [Cn 3 rec_mult [id 3 1, id 3 2], id 3 0]))]"
  1443 
  1504 
  1444 declare numeral_2_eq_2[simp del] numeral_3_eq_3[simp del]
  1505 declare numeral_2_eq_2[simp del] numeral_3_eq_3[simp del]
  1445 
  1506 
  1446 lemma exec_tmp: 
  1507 lemma exec_tmp: 
  1447   "rec_eval (rec_all (Cn 2 rec_minus [recf.id 2 0, Cn 2 (constn (Suc 0)) [recf.id 2 0]]) 
  1508   "rec_exec (rec_all (Cn 2 rec_minus [recf.id 2 0, Cn 2 (constn (Suc 0)) [recf.id 2 0]]) 
  1448   (Cn 3 rec_noteq [Cn 3 rec_mult [recf.id 3 (Suc 0), recf.id 3 2], recf.id 3 0]))  [x, k] = 
  1509   (Cn 3 rec_noteq [Cn 3 rec_mult [recf.id 3 (Suc 0), recf.id 3 2], recf.id 3 0]))  [x, k] = 
  1449   ((if (\<forall>w\<le>rec_eval (Cn 2 rec_minus [recf.id 2 0, Cn 2 (constn (Suc 0)) [recf.id 2 0]]) ([x, k]). 
  1510   ((if (\<forall>w\<le>rec_exec (Cn 2 rec_minus [recf.id 2 0, Cn 2 (constn (Suc 0)) [recf.id 2 0]]) ([x, k]). 
  1450   0 < rec_eval (Cn 3 rec_noteq [Cn 3 rec_mult [recf.id 3 (Suc 0), recf.id 3 2], recf.id 3 0])
  1511   0 < rec_exec (Cn 3 rec_noteq [Cn 3 rec_mult [recf.id 3 (Suc 0), recf.id 3 2], recf.id 3 0])
  1451   ([x, k] @ [w])) then 1 else 0))"
  1512   ([x, k] @ [w])) then 1 else 0))"
  1452 apply(rule_tac all_lemma)
  1513 apply(rule_tac all_lemma)
  1453 apply(auto)
  1514 apply(auto)
  1454 apply(case_tac [!] i, auto)
  1515 apply(case_tac [!] i, auto)
  1455 apply(case_tac ia, auto simp: numeral_3_eq_3 numeral_2_eq_2)
  1516 apply(case_tac ia, auto simp: numeral_3_eq_3 numeral_2_eq_2)
  1456 done
  1517 done
  1457 
  1518 
  1458 text {*
  1519 text {*
  1459   The correctness of @{text "Prime"}.
  1520   The correctness of @{text "Prime"}.
  1460   *}
  1521   *}
  1461 lemma prime_lemma: "rec_eval rec_prime [x] = (if prime x then 1 else 0)"
  1522 lemma prime_lemma: "rec_exec rec_prime [x] = (if Prime x then 1 else 0)"
  1462 proof(simp add: rec_eval.simps rec_prime_def)
  1523 proof(simp add: rec_exec.simps rec_prime_def)
  1463   let ?rt1 = "(Cn 2 rec_minus [recf.id 2 0, 
  1524   let ?rt1 = "(Cn 2 rec_minus [recf.id 2 0, 
  1464     Cn 2 (constn (Suc 0)) [recf.id 2 0]])"
  1525     Cn 2 (constn (Suc 0)) [recf.id 2 0]])"
  1465   let ?rf1 = "(Cn 3 rec_noteq [Cn 3 rec_mult 
  1526   let ?rf1 = "(Cn 3 rec_noteq [Cn 3 rec_mult 
  1466     [recf.id 3 (Suc 0), recf.id 3 2], recf.id 3 (0)])"
  1527     [recf.id 3 (Suc 0), recf.id 3 2], recf.id 3 (0)])"
  1467   let ?rt2 = "(Cn (Suc 0) rec_minus 
  1528   let ?rt2 = "(Cn (Suc 0) rec_minus 
  1468     [recf.id (Suc 0) 0, constn (Suc 0)])"
  1529     [recf.id (Suc 0) 0, constn (Suc 0)])"
  1469   let ?rf2 = "rec_all ?rt1 ?rf1"
  1530   let ?rf2 = "rec_all ?rt1 ?rf1"
  1470   have h1: "rec_eval (rec_all ?rt2 ?rf2) ([x]) = 
  1531   have h1: "rec_exec (rec_all ?rt2 ?rf2) ([x]) = 
  1471         (if (\<forall>k\<le>rec_eval ?rt2 ([x]). 0 < rec_eval ?rf2 ([x] @ [k])) then 1 else 0)"
  1532         (if (\<forall>k\<le>rec_exec ?rt2 ([x]). 0 < rec_exec ?rf2 ([x] @ [k])) then 1 else 0)"
  1472   proof(rule_tac all_lemma, simp_all)
  1533   proof(rule_tac all_lemma, simp_all)
  1473     show "primerec ?rf2 (Suc (Suc 0))"
  1534     show "primerec ?rf2 (Suc (Suc 0))"
  1474       apply(rule_tac primerec_all_iff)
  1535       apply(rule_tac primerec_all_iff)
  1475       apply(auto)
  1536       apply(auto)
  1476       apply(case_tac [!] i, auto simp: numeral_2_eq_2)
  1537       apply(case_tac [!] i, auto simp: numeral_2_eq_2)
  1482       apply(auto)
  1543       apply(auto)
  1483       apply(case_tac i, auto)
  1544       apply(case_tac i, auto)
  1484       done
  1545       done
  1485   qed
  1546   qed
  1486   from h1 show 
  1547   from h1 show 
  1487    "(Suc 0 < x \<longrightarrow>  (rec_eval (rec_all ?rt2 ?rf2) [x] = 0 \<longrightarrow> 
  1548    "(Suc 0 < x \<longrightarrow>  (rec_exec (rec_all ?rt2 ?rf2) [x] = 0 \<longrightarrow> 
  1488     \<not> prime x) \<and>
  1549     \<not> Prime x) \<and>
  1489      (0 < rec_eval (rec_all ?rt2 ?rf2) [x] \<longrightarrow> prime x)) \<and>
  1550      (0 < rec_exec (rec_all ?rt2 ?rf2) [x] \<longrightarrow> Prime x)) \<and>
  1490     (\<not> Suc 0 < x \<longrightarrow> \<not> prime x \<and> (rec_eval (rec_all ?rt2 ?rf2) [x] = 0
  1551     (\<not> Suc 0 < x \<longrightarrow> \<not> Prime x \<and> (rec_exec (rec_all ?rt2 ?rf2) [x] = 0
  1491     \<longrightarrow> \<not> prime x))"
  1552     \<longrightarrow> \<not> Prime x))"
  1492     apply(auto simp:rec_eval.simps)
  1553     apply(auto simp:rec_exec.simps)
  1493     apply(simp add: exec_tmp rec_eval.simps)
  1554     apply(simp add: exec_tmp rec_exec.simps)
  1494   proof -
  1555   proof -
  1495     assume "\<forall>k\<le>x - Suc 0. (0::nat) < (if \<forall>w\<le>x - Suc 0. 
  1556     assume "\<forall>k\<le>x - Suc 0. (0::nat) < (if \<forall>w\<le>x - Suc 0. 
  1496            0 < (if k * w \<noteq> x then 1 else (0 :: nat)) then 1 else 0)" "Suc 0 < x"
  1557            0 < (if k * w \<noteq> x then 1 else (0 :: nat)) then 1 else 0)" "Suc 0 < x"
  1497     thus "prime x"
  1558     thus "Prime x"
  1498       apply(simp add: rec_eval.simps split: if_splits)
  1559       apply(simp add: rec_exec.simps split: if_splits)
  1499       apply(simp add: prime_nat_def dvd_def, auto)
  1560       apply(simp add: Prime.simps, auto)
  1500       apply(erule_tac x = m in allE, auto)
  1561       apply(erule_tac x = u in allE, auto)
  1501       apply(case_tac m, simp, case_tac nat, simp, simp)
  1562       apply(case_tac u, simp, case_tac nat, simp, simp)
  1502       apply(case_tac k, simp, case_tac nat, simp, simp)
  1563       apply(case_tac v, simp, case_tac nat, simp, simp)
  1503       done
  1564       done
  1504   next
  1565   next
  1505     assume "\<not> Suc 0 < x" "prime x"
  1566     assume "\<not> Suc 0 < x" "Prime x"
  1506     thus "False"
  1567     thus "False"
  1507       by (simp add: prime_nat_def)
  1568       apply(simp add: Prime.simps)
       
  1569       done
  1508   next
  1570   next
  1509     fix k
  1571     fix k
  1510     assume "rec_eval (rec_all ?rt1 ?rf1)
  1572     assume "rec_exec (rec_all ?rt1 ?rf1)
  1511       [x, k] = 0" "k \<le> x - Suc 0" "prime x"
  1573       [x, k] = 0" "k \<le> x - Suc 0" "Prime x"
  1512     thus "False"
  1574     thus "False"
  1513       by (auto simp add: exec_tmp rec_eval.simps prime_nat_def dvd_def split: if_splits)
  1575       apply(simp add: exec_tmp rec_exec.simps Prime.simps split: if_splits)
       
  1576       done
  1514   next
  1577   next
  1515     fix k
  1578     fix k
  1516     assume "rec_eval (rec_all ?rt1 ?rf1)
  1579     assume "rec_exec (rec_all ?rt1 ?rf1)
  1517       [x, k] = 0" "k \<le> x - Suc 0" "prime x"
  1580       [x, k] = 0" "k \<le> x - Suc 0" "Prime x"
  1518     thus "False"
  1581     thus "False"
  1519       by(auto simp add: exec_tmp rec_eval.simps prime_nat_def dvd_def split: if_splits)
  1582       apply(simp add: exec_tmp rec_exec.simps Prime.simps split: if_splits)
       
  1583       done
  1520   qed
  1584   qed
  1521 qed
  1585 qed
  1522 
  1586 
  1523 definition rec_dummyfac :: "recf"
  1587 definition rec_dummyfac :: "recf"
  1524   where
  1588   where
  1525   "rec_dummyfac = Pr 1 (constn 1) (Cn 3 rec_mult [id 3 2, Cn 3 s [id 3 1]])"
  1589   "rec_dummyfac = Pr 1 (constn 1) 
       
  1590   (Cn 3 rec_mult [id 3 2, Cn 3 s [id 3 1]])"
  1526 
  1591 
  1527 text {*
  1592 text {*
  1528   The recursive function used to implment factorization.
  1593   The recursive function used to implment factorization.
  1529   *}
  1594   *}
  1530 definition rec_fac :: "recf"
  1595 definition rec_fac :: "recf"
  1537 fun fac :: "nat \<Rightarrow> nat"  ("_!" [100] 99)
  1602 fun fac :: "nat \<Rightarrow> nat"  ("_!" [100] 99)
  1538   where
  1603   where
  1539   "fac 0 = 1" |
  1604   "fac 0 = 1" |
  1540   "fac (Suc x) = (Suc x) * fac x"
  1605   "fac (Suc x) = (Suc x) * fac x"
  1541 
  1606 
  1542 lemma [simp]: "rec_eval rec_dummyfac [0, 0] = Suc 0"
  1607 lemma [simp]: "rec_exec rec_dummyfac [0, 0] = Suc 0"
  1543 by(simp add: rec_dummyfac_def rec_eval.simps)
  1608 by(simp add: rec_dummyfac_def rec_exec.simps)
  1544 
  1609 
  1545 lemma rec_cn_simp: 
  1610 lemma rec_cn_simp: "rec_exec (Cn n f gs) xs = 
  1546   "rec_eval (Cn n f gs) xs = (let rgs = map (\<lambda> g. rec_eval g xs) gs in rec_eval f rgs)"
  1611                 (let rgs = map (\<lambda> g. rec_exec g xs) gs in
  1547 by(simp add: rec_eval.simps)
  1612                  rec_exec f rgs)"
  1548 
  1613 by(simp add: rec_exec.simps)
  1549 lemma rec_id_simp: "rec_eval (id m n) xs = xs ! n" 
  1614 
  1550   by(simp add: rec_eval.simps)
  1615 lemma rec_id_simp: "rec_exec (id m n) xs = xs ! n" 
  1551 
  1616   by(simp add: rec_exec.simps)
  1552 lemma fac_dummy: "rec_eval rec_dummyfac [x, y] = y !"
  1617 
       
  1618 lemma fac_dummy: "rec_exec rec_dummyfac [x, y] = y !"
  1553 apply(induct y)
  1619 apply(induct y)
  1554 apply(auto simp: rec_dummyfac_def rec_eval.simps)
  1620 apply(auto simp: rec_dummyfac_def rec_exec.simps)
  1555 done
  1621 done
  1556 
  1622 
  1557 text {*
  1623 text {*
  1558   The correctness of @{text "rec_fac"}.
  1624   The correctness of @{text "rec_fac"}.
  1559   *}
  1625   *}
  1560 lemma fac_lemma: "rec_eval rec_fac [x] =  x!"
  1626 lemma fac_lemma: "rec_exec rec_fac [x] =  x!"
  1561 apply(simp add: rec_fac_def rec_eval.simps fac_dummy)
  1627 apply(simp add: rec_fac_def rec_exec.simps fac_dummy)
  1562 done
  1628 done
  1563 
  1629 
  1564 declare fac.simps[simp del]
  1630 declare fac.simps[simp del]
  1565 
  1631 
  1566 text {*
  1632 text {*
  1567   @{text "Np x"} returns the first prime number after @{text "x"}.
  1633   @{text "Np x"} returns the first prime number after @{text "x"}.
  1568   *}
  1634   *}
  1569 fun Np ::"nat \<Rightarrow> nat"
  1635 fun Np ::"nat \<Rightarrow> nat"
  1570   where
  1636   where
  1571   "Np x = Min {y. y \<le> Suc (x!) \<and> x < y \<and> prime y}"
  1637   "Np x = Min {y. y \<le> Suc (x!) \<and> x < y \<and> Prime y}"
  1572 
  1638 
  1573 declare Np.simps[simp del] rec_Minr.simps[simp del]
  1639 declare Np.simps[simp del] rec_Minr.simps[simp del]
  1574 
  1640 
  1575 text {*
  1641 text {*
  1576   @{text "rec_np"} is the recursive function used to implement
  1642   @{text "rec_np"} is the recursive function used to implement
  1587 apply(simp add: fac.simps)
  1653 apply(simp add: fac.simps)
  1588 apply(case_tac n, auto simp: fac.simps)
  1654 apply(case_tac n, auto simp: fac.simps)
  1589 done
  1655 done
  1590 
  1656 
  1591 lemma divsor_ex: 
  1657 lemma divsor_ex: 
  1592 "\<lbrakk>\<not> prime x; x > Suc 0\<rbrakk> \<Longrightarrow> (\<exists> u > Suc 0. (\<exists> v > Suc 0. u * v = x))"
  1658 "\<lbrakk>\<not> Prime x; x > Suc 0\<rbrakk> \<Longrightarrow> (\<exists> u > Suc 0. (\<exists> v > Suc 0. u * v = x))"
  1593  apply(auto simp: prime_nat_def dvd_def)
  1659  by(auto simp: Prime.simps)
  1594 by (metis Suc_lessI mult_0 nat_mult_commute neq0_conv)
  1660 
  1595 
  1661 lemma divsor_prime_ex: "\<lbrakk>\<not> Prime x; x > Suc 0\<rbrakk> \<Longrightarrow> 
  1596 
  1662   \<exists> p. Prime p \<and> p dvd x"
  1597 lemma divsor_prime_ex: "\<lbrakk>\<not> prime x; x > Suc 0\<rbrakk> \<Longrightarrow> 
       
  1598   \<exists> p. prime p \<and> p dvd x"
       
  1599 apply(induct x rule: wf_induct[where r = "measure (\<lambda> y. y)"], simp)
  1663 apply(induct x rule: wf_induct[where r = "measure (\<lambda> y. y)"], simp)
  1600 apply(drule_tac divsor_ex, simp, auto)
  1664 apply(drule_tac divsor_ex, simp, auto)
  1601 apply(erule_tac x = u in allE, simp)
  1665 apply(erule_tac x = u in allE, simp)
  1602 apply(case_tac "prime u", simp)
  1666 apply(case_tac "Prime u", simp)
  1603 apply(rule_tac x = u in exI, simp, auto)
  1667 apply(rule_tac x = u in exI, simp, auto)
  1604 done
  1668 done
  1605 
  1669 
  1606 lemma [intro]: "0 < n!"
  1670 lemma [intro]: "0 < n!"
  1607 apply(induct n)
  1671 apply(induct n)
  1635   from this obtain d where "d > 0 \<and> ka = d + k" ..
  1699   from this obtain d where "d > 0 \<and> ka = d + k" ..
  1636   from h2 and this and h1 show "False"
  1700   from h2 and this and h1 show "False"
  1637     by(simp add: add_mult_distrib2)
  1701     by(simp add: add_mult_distrib2)
  1638 qed
  1702 qed
  1639     
  1703     
  1640 lemma prime_ex: "\<exists> p. n < p \<and> p \<le> Suc (n!) \<and> prime p"
  1704 lemma prime_ex: "\<exists> p. n < p \<and> p \<le> Suc (n!) \<and> Prime p"
  1641 proof(cases "prime (n! + 1)")
  1705 proof(cases "Prime (n! + 1)")
  1642   case True thus "?thesis" 
  1706   case True thus "?thesis" 
  1643     by(rule_tac x = "Suc (n!)" in exI, simp)
  1707     by(rule_tac x = "Suc (n!)" in exI, simp)
  1644 next
  1708 next
  1645   assume h: "\<not> prime (n! + 1)"  
  1709   assume h: "\<not> Prime (n! + 1)"  
  1646   hence "\<exists> p. prime p \<and> p dvd (n! + 1)"
  1710   hence "\<exists> p. Prime p \<and> p dvd (n! + 1)"
  1647     by(erule_tac divsor_prime_ex, auto)
  1711     by(erule_tac divsor_prime_ex, auto)
  1648   from this obtain q where k: "prime q \<and> q dvd (n! + 1)" ..
  1712   from this obtain q where k: "Prime q \<and> q dvd (n! + 1)" ..
  1649   thus "?thesis"
  1713   thus "?thesis"
  1650   proof(cases "q > n")
  1714   proof(cases "q > n")
  1651     case True thus "?thesis"
  1715     case True thus "?thesis"
  1652       using k
  1716       using k
  1653       apply(rule_tac x = q in exI, auto)
  1717       apply(rule_tac x = q in exI, auto)
  1656   next
  1720   next
  1657     case False thus "?thesis"
  1721     case False thus "?thesis"
  1658     proof -
  1722     proof -
  1659       assume g: "\<not> n < q"
  1723       assume g: "\<not> n < q"
  1660       have j: "q > Suc 0"
  1724       have j: "q > Suc 0"
  1661         using k by(case_tac q, auto simp: prime_nat_def dvd_def)
  1725         using k by(case_tac q, auto simp: Prime.simps)
  1662       hence "q dvd n!"
  1726       hence "q dvd n!"
  1663         using g 
  1727         using g 
  1664         apply(rule_tac fac_dvd, auto)
  1728         apply(rule_tac fac_dvd, auto)
  1665         done
  1729         done
  1666       hence "\<not> q dvd Suc (n!)"
  1730       hence "\<not> q dvd Suc (n!)"
  1684 done
  1748 done
  1685 
  1749 
  1686 text {*
  1750 text {*
  1687   The correctness of @{text "rec_np"}.
  1751   The correctness of @{text "rec_np"}.
  1688   *}
  1752   *}
  1689 lemma np_lemma: "rec_eval rec_np [x] = Np x"
  1753 lemma np_lemma: "rec_exec rec_np [x] = Np x"
  1690 proof(auto simp: rec_np_def rec_eval.simps Let_def fac_lemma)
  1754 proof(auto simp: rec_np_def rec_exec.simps Let_def fac_lemma)
  1691   let ?rr = "(Cn 2 rec_conj [Cn 2 rec_less [recf.id 2 0,
  1755   let ?rr = "(Cn 2 rec_conj [Cn 2 rec_less [recf.id 2 0,
  1692     recf.id 2 (Suc 0)], Cn 2 rec_prime [recf.id 2 (Suc 0)]])"
  1756     recf.id 2 (Suc 0)], Cn 2 rec_prime [recf.id 2 (Suc 0)]])"
  1693   let ?R = "\<lambda> zs. zs ! 0 < zs ! 1 \<and> prime (zs ! 1)"
  1757   let ?R = "\<lambda> zs. zs ! 0 < zs ! 1 \<and> Prime (zs ! 1)"
  1694   have g1: "rec_eval (rec_Minr ?rr) ([x] @ [Suc (x!)]) = 
  1758   have g1: "rec_exec (rec_Minr ?rr) ([x] @ [Suc (x!)]) = 
  1695     Minr (\<lambda> args. 0 < rec_eval ?rr args) [x] (Suc (x!))"
  1759     Minr (\<lambda> args. 0 < rec_exec ?rr args) [x] (Suc (x!))"
  1696     by(rule_tac Minr_lemma, auto simp: rec_eval.simps
  1760     by(rule_tac Minr_lemma, auto simp: rec_exec.simps
  1697       prime_lemma, auto simp:  numeral_2_eq_2 numeral_3_eq_3)
  1761       prime_lemma, auto simp:  numeral_2_eq_2 numeral_3_eq_3)
  1698   have g2: "Minr (\<lambda> args. 0 < rec_eval ?rr args) [x] (Suc (x!)) = Np x"
  1762   have g2: "Minr (\<lambda> args. 0 < rec_exec ?rr args) [x] (Suc (x!)) = Np x"
  1699     using prime_ex[of x]
  1763     using prime_ex[of x]
  1700     apply(auto simp: Minr.simps Np.simps rec_eval.simps)
  1764     apply(auto simp: Minr.simps Np.simps rec_exec.simps)
  1701     apply(erule_tac x = p in allE, simp add: prime_lemma)
  1765     apply(erule_tac x = p in allE, simp add: prime_lemma)
  1702     apply(simp add: prime_lemma split: if_splits)
  1766     apply(simp add: prime_lemma split: if_splits)
  1703     apply(subgoal_tac
  1767     apply(subgoal_tac
  1704    "{uu. (prime uu \<longrightarrow> (x < uu \<longrightarrow> uu \<le> Suc (x!)) \<and> x < uu) \<and> prime uu}
  1768    "{uu. (Prime uu \<longrightarrow> (x < uu \<longrightarrow> uu \<le> Suc (x!)) \<and> x < uu) \<and> Prime uu}
  1705     = {y. y \<le> Suc (x!) \<and> x < y \<and> prime y}", auto)
  1769     = {y. y \<le> Suc (x!) \<and> x < y \<and> Prime y}", auto)
  1706     done
  1770     done
  1707   from g1 and g2 show "rec_eval (rec_Minr ?rr) ([x, Suc (x!)]) = Np x"
  1771   from g1 and g2 show "rec_exec (rec_Minr ?rr) ([x, Suc (x!)]) = Np x"
  1708     by simp
  1772     by simp
  1709 qed
  1773 qed
  1710 
  1774 
  1711 text {*
  1775 text {*
  1712   @{text "rec_power"} is the recursive function used to implement
  1776   @{text "rec_power"} is the recursive function used to implement
  1717   "rec_power = Pr 1 (constn 1) (Cn 3 rec_mult [id 3 0, id 3 2])"
  1781   "rec_power = Pr 1 (constn 1) (Cn 3 rec_mult [id 3 0, id 3 2])"
  1718 
  1782 
  1719 text {*
  1783 text {*
  1720   The correctness of @{text "rec_power"}.
  1784   The correctness of @{text "rec_power"}.
  1721   *}
  1785   *}
  1722 lemma power_lemma: "rec_eval rec_power [x, y] = x^y"
  1786 lemma power_lemma: "rec_exec rec_power [x, y] = x^y"
  1723   by(induct y, auto simp: rec_eval.simps rec_power_def)
  1787   by(induct y, auto simp: rec_exec.simps rec_power_def)
  1724 
  1788 
  1725 text{*
  1789 text{*
  1726   @{text "Pi k"} returns the @{text "k"}-th prime number.
  1790   @{text "Pi k"} returns the @{text "k"}-th prime number.
  1727   *}
  1791   *}
  1728 fun Pi :: "nat \<Rightarrow> nat"
  1792 fun Pi :: "nat \<Rightarrow> nat"
  1740   *}
  1804   *}
  1741 definition rec_pi :: "recf"
  1805 definition rec_pi :: "recf"
  1742   where
  1806   where
  1743   "rec_pi = Cn 1 rec_dummy_pi [id 1 0, id 1 0]"
  1807   "rec_pi = Cn 1 rec_dummy_pi [id 1 0, id 1 0]"
  1744 
  1808 
  1745 lemma pi_dummy_lemma: "rec_eval rec_dummy_pi [x, y] = Pi y"
  1809 lemma pi_dummy_lemma: "rec_exec rec_dummy_pi [x, y] = Pi y"
  1746 apply(induct y)
  1810 apply(induct y)
  1747 by(auto simp: rec_eval.simps rec_dummy_pi_def Pi.simps np_lemma)
  1811 by(auto simp: rec_exec.simps rec_dummy_pi_def Pi.simps np_lemma)
  1748 
  1812 
  1749 text {*
  1813 text {*
  1750   The correctness of @{text "rec_pi"}.
  1814   The correctness of @{text "rec_pi"}.
  1751   *}
  1815   *}
  1752 lemma pi_lemma: "rec_eval rec_pi [x] = Pi x"
  1816 lemma pi_lemma: "rec_exec rec_pi [x] = Pi x"
  1753 apply(simp add: rec_pi_def rec_eval.simps pi_dummy_lemma)
  1817 apply(simp add: rec_pi_def rec_exec.simps pi_dummy_lemma)
  1754 done
  1818 done
  1755 
  1819 
  1756 fun loR :: "nat list \<Rightarrow> bool"
  1820 fun loR :: "nat list \<Rightarrow> bool"
  1757   where
  1821   where
  1758   "loR [x, y, u] = (x mod (y^u) = 0)"
  1822   "loR [x, y, u] = (x mod (y^u) = 0)"
  1831              in Cn 2 rec_add [Cn 2 rec_mult [rb, rcond], 
  1895              in Cn 2 rec_add [Cn 2 rec_mult [rb, rcond], 
  1832                   Cn 2 rec_mult [Cn 2 (constn 0) [id 2 0], rcond2]])"
  1896                   Cn 2 rec_mult [Cn 2 (constn 0) [id 2 0], rcond2]])"
  1833 
  1897 
  1834 lemma rec_lo_Maxr_lor:
  1898 lemma rec_lo_Maxr_lor:
  1835   "\<lbrakk>Suc 0 < x; Suc 0 < y\<rbrakk> \<Longrightarrow>  
  1899   "\<lbrakk>Suc 0 < x; Suc 0 < y\<rbrakk> \<Longrightarrow>  
  1836         rec_eval rec_lo [x, y] = Maxr loR [x, y] x"
  1900         rec_exec rec_lo [x, y] = Maxr loR [x, y] x"
  1837 proof(auto simp: rec_eval.simps rec_lo_def Let_def 
  1901 proof(auto simp: rec_exec.simps rec_lo_def Let_def 
  1838     numeral_2_eq_2 numeral_3_eq_3)
  1902     numeral_2_eq_2 numeral_3_eq_3)
  1839   let ?rR = "(Cn (Suc (Suc (Suc 0))) rec_eq
  1903   let ?rR = "(Cn (Suc (Suc (Suc 0))) rec_eq
  1840      [Cn (Suc (Suc (Suc 0))) rec_mod [recf.id (Suc (Suc (Suc 0))) 0,
  1904      [Cn (Suc (Suc (Suc 0))) rec_mod [recf.id (Suc (Suc (Suc 0))) 0,
  1841      Cn (Suc (Suc (Suc 0))) rec_power [recf.id (Suc (Suc (Suc 0)))
  1905      Cn (Suc (Suc (Suc 0))) rec_power [recf.id (Suc (Suc (Suc 0)))
  1842      (Suc 0), recf.id (Suc (Suc (Suc 0))) (Suc (Suc 0))]],
  1906      (Suc 0), recf.id (Suc (Suc (Suc 0))) (Suc (Suc 0))]],
  1843      Cn (Suc (Suc (Suc 0))) (constn 0) [recf.id (Suc (Suc (Suc 0))) (Suc 0)]])"
  1907      Cn (Suc (Suc (Suc 0))) (constn 0) [recf.id (Suc (Suc (Suc 0))) (Suc 0)]])"
  1844   have h: "rec_eval (rec_maxr ?rR) ([x, y] @ [x]) =
  1908   have h: "rec_exec (rec_maxr ?rR) ([x, y] @ [x]) =
  1845     Maxr (\<lambda> args. 0 < rec_eval ?rR args) [x, y] x"
  1909     Maxr (\<lambda> args. 0 < rec_exec ?rR args) [x, y] x"
  1846     by(rule_tac Maxr_lemma, auto simp: rec_eval.simps
  1910     by(rule_tac Maxr_lemma, auto simp: rec_exec.simps
  1847       mod_lemma power_lemma, auto simp: numeral_2_eq_2 numeral_3_eq_3)
  1911       mod_lemma power_lemma, auto simp: numeral_2_eq_2 numeral_3_eq_3)
  1848   have "Maxr loR [x, y] x =  Maxr (\<lambda> args. 0 < rec_eval ?rR args) [x, y] x"
  1912   have "Maxr loR [x, y] x =  Maxr (\<lambda> args. 0 < rec_exec ?rR args) [x, y] x"
  1849     apply(simp add: rec_eval.simps mod_lemma power_lemma)
  1913     apply(simp add: rec_exec.simps mod_lemma power_lemma)
  1850     apply(simp add: Maxr.simps loR.simps)
  1914     apply(simp add: Maxr.simps loR.simps)
  1851     done
  1915     done
  1852   from h and this show "rec_eval (rec_maxr ?rR) [x, y, x] = 
  1916   from h and this show "rec_exec (rec_maxr ?rR) [x, y, x] = 
  1853     Maxr loR [x, y] x"
  1917     Maxr loR [x, y] x"
  1854     apply(simp)
  1918     apply(simp)
  1855     done
  1919     done
  1856 qed
  1920 qed
  1857 
  1921 
  1899 apply(simp add: Maxr.simps lo.simps, auto)
  1963 apply(simp add: Maxr.simps lo.simps, auto)
  1900 apply(erule_tac x = xa in allE, simp, simp add: uplimit_loR)
  1964 apply(erule_tac x = xa in allE, simp, simp add: uplimit_loR)
  1901 done
  1965 done
  1902 
  1966 
  1903 lemma lo_lemma': "\<lbrakk>Suc 0 < x; Suc 0 < y\<rbrakk> \<Longrightarrow> 
  1967 lemma lo_lemma': "\<lbrakk>Suc 0 < x; Suc 0 < y\<rbrakk> \<Longrightarrow> 
  1904   rec_eval rec_lo [x, y] = lo x y"
  1968   rec_exec rec_lo [x, y] = lo x y"
  1905 by(simp add: Maxr_lo  rec_lo_Maxr_lor)
  1969 by(simp add: Maxr_lo  rec_lo_Maxr_lor)
  1906 
  1970 
  1907 lemma lo_lemma'': "\<lbrakk>\<not> Suc 0 < x\<rbrakk> \<Longrightarrow> rec_eval rec_lo [x, y] = lo x y"
  1971 lemma lo_lemma'': "\<lbrakk>\<not> Suc 0 < x\<rbrakk> \<Longrightarrow> rec_exec rec_lo [x, y] = lo x y"
  1908 apply(case_tac x, auto simp: rec_eval.simps rec_lo_def 
  1972 apply(case_tac x, auto simp: rec_exec.simps rec_lo_def 
  1909   Let_def lo.simps)
  1973   Let_def lo.simps)
  1910 done
  1974 done
  1911   
  1975   
  1912 lemma lo_lemma''': "\<lbrakk>\<not> Suc 0 < y\<rbrakk> \<Longrightarrow> rec_eval rec_lo [x, y] = lo x y"
  1976 lemma lo_lemma''': "\<lbrakk>\<not> Suc 0 < y\<rbrakk> \<Longrightarrow> rec_exec rec_lo [x, y] = lo x y"
  1913 apply(case_tac y, auto simp: rec_eval.simps rec_lo_def 
  1977 apply(case_tac y, auto simp: rec_exec.simps rec_lo_def 
  1914   Let_def lo.simps)
  1978   Let_def lo.simps)
  1915 done
  1979 done
  1916 
  1980 
  1917 text {*
  1981 text {*
  1918   The correctness of @{text "rec_lo"}:
  1982   The correctness of @{text "rec_lo"}:
  1919 *}
  1983 *}
  1920 lemma lo_lemma: "rec_eval rec_lo [x, y] = lo x y" 
  1984 lemma lo_lemma: "rec_exec rec_lo [x, y] = lo x y" 
  1921 apply(case_tac "Suc 0 < x \<and> Suc 0 < y")
  1985 apply(case_tac "Suc 0 < x \<and> Suc 0 < y")
  1922 apply(auto simp: lo_lemma' lo_lemma'' lo_lemma''')
  1986 apply(auto simp: lo_lemma' lo_lemma'' lo_lemma''')
  1923 done
  1987 done
  1924 
  1988 
  1925 fun lgR :: "nat list \<Rightarrow> bool"
  1989 fun lgR :: "nat list \<Rightarrow> bool"
  1942 text {*
  2006 text {*
  1943   @{text "rec_lg"} is the recursive function used to implement @{text "lg"}.
  2007   @{text "rec_lg"} is the recursive function used to implement @{text "lg"}.
  1944   *}
  2008   *}
  1945 definition rec_lg :: "recf"
  2009 definition rec_lg :: "recf"
  1946   where
  2010   where
  1947   "rec_lg = 
  2011   "rec_lg = (let rec_lgR = Cn 3 rec_le
  1948   (let rec_lgR = Cn 3 rec_le [Cn 3 rec_power [id 3 1, id 3 2], id 3 0] in
  2012   [Cn 3 rec_power [id 3 1, id 3 2], id 3 0] in
  1949    let conR1 = Cn 2 rec_conj [Cn 2 rec_less [Cn 2 (constn 1) [id 2 0], id 2 0], 
  2013   let conR1 = Cn 2 rec_conj [Cn 2 rec_less 
  1950                               Cn 2 rec_less [Cn 2 (constn 1) [id 2 0], id 2 1]] in 
  2014                      [Cn 2 (constn 1) [id 2 0], id 2 0], 
  1951    let conR2 = Cn 2 rec_not [conR1] in 
  2015                             Cn 2 rec_less [Cn 2 (constn 1) 
  1952       Cn 2 rec_add [Cn 2 rec_mult [conR1, Cn 2 (rec_maxr rec_lgR)
  2016                                  [id 2 0], id 2 1]] in 
  1953                                                [id 2 0, id 2 1, id 2 0]], 
  2017   let conR2 = Cn 2 rec_not [conR1] in 
  1954                     Cn 2 rec_mult [conR2, Cn 2 (constn 0) [id 2 0]]])"
  2018         Cn 2 rec_add [Cn 2 rec_mult 
       
  2019               [conR1, Cn 2 (rec_maxr rec_lgR)
       
  2020                        [id 2 0, id 2 1, id 2 0]], 
       
  2021                        Cn 2 rec_mult [conR2, Cn 2 (constn 0) 
       
  2022                                 [id 2 0]]])"
  1955 
  2023 
  1956 lemma lg_maxr: "\<lbrakk>Suc 0 < x; Suc 0 < y\<rbrakk> \<Longrightarrow> 
  2024 lemma lg_maxr: "\<lbrakk>Suc 0 < x; Suc 0 < y\<rbrakk> \<Longrightarrow> 
  1957                       rec_eval rec_lg [x, y] = Maxr lgR [x, y] x"
  2025                       rec_exec rec_lg [x, y] = Maxr lgR [x, y] x"
  1958 proof(simp add: rec_eval.simps rec_lg_def Let_def)
  2026 proof(simp add: rec_exec.simps rec_lg_def Let_def)
  1959   assume h: "Suc 0 < x" "Suc 0 < y"
  2027   assume h: "Suc 0 < x" "Suc 0 < y"
  1960   let ?rR = "(Cn 3 rec_le [Cn 3 rec_power
  2028   let ?rR = "(Cn 3 rec_le [Cn 3 rec_power
  1961                [recf.id 3 (Suc 0), recf.id 3 2], recf.id 3 0])"
  2029                [recf.id 3 (Suc 0), recf.id 3 2], recf.id 3 0])"
  1962   have "rec_eval (rec_maxr ?rR) ([x, y] @ [x])
  2030   have "rec_exec (rec_maxr ?rR) ([x, y] @ [x])
  1963               = Maxr ((\<lambda> args. 0 < rec_eval ?rR args)) [x, y] x" 
  2031               = Maxr ((\<lambda> args. 0 < rec_exec ?rR args)) [x, y] x" 
  1964   proof(rule Maxr_lemma)
  2032   proof(rule Maxr_lemma)
  1965     show "primerec (Cn 3 rec_le [Cn 3 rec_power 
  2033     show "primerec (Cn 3 rec_le [Cn 3 rec_power 
  1966               [recf.id 3 (Suc 0), recf.id 3 2], recf.id 3 0]) (Suc (length [x, y]))"
  2034               [recf.id 3 (Suc 0), recf.id 3 2], recf.id 3 0]) (Suc (length [x, y]))"
  1967       apply(auto simp: numeral_3_eq_3)+
  2035       apply(auto simp: numeral_3_eq_3)+
  1968       done
  2036       done
  1969   qed
  2037   qed
  1970   moreover have "Maxr lgR [x, y] x = Maxr ((\<lambda> args. 0 < rec_eval ?rR args)) [x, y] x"
  2038   moreover have "Maxr lgR [x, y] x = Maxr ((\<lambda> args. 0 < rec_exec ?rR args)) [x, y] x"
  1971     apply(simp add: rec_eval.simps power_lemma)
  2039     apply(simp add: rec_exec.simps power_lemma)
  1972     apply(simp add: Maxr.simps lgR.simps)
  2040     apply(simp add: Maxr.simps lgR.simps)
  1973     done 
  2041     done 
  1974   ultimately show "rec_eval (rec_maxr ?rR) [x, y, x] = Maxr lgR [x, y] x"
  2042   ultimately show "rec_exec (rec_maxr ?rR) [x, y, x] = Maxr lgR [x, y] x"
  1975     by simp
  2043     by simp
  1976 qed
  2044 qed
  1977 
  2045 
  1978 lemma [simp]: "\<lbrakk>Suc 0 < y; lgR [x, y, xa]\<rbrakk> \<Longrightarrow> xa \<le> x"
  2046 lemma [simp]: "\<lbrakk>Suc 0 < y; lgR [x, y, xa]\<rbrakk> \<Longrightarrow> xa \<le> x"
  1979 apply(simp add: lgR.simps)
  2047 apply(simp add: lgR.simps)
  1989 lemma maxr_lg: "\<lbrakk>Suc 0 < x; Suc 0 < y\<rbrakk> \<Longrightarrow> Maxr lgR [x, y] x = lg x y"
  2057 lemma maxr_lg: "\<lbrakk>Suc 0 < x; Suc 0 < y\<rbrakk> \<Longrightarrow> Maxr lgR [x, y] x = lg x y"
  1990 apply(simp add: lg.simps Maxr.simps, auto)
  2058 apply(simp add: lg.simps Maxr.simps, auto)
  1991 apply(erule_tac x = xa in allE, simp)
  2059 apply(erule_tac x = xa in allE, simp)
  1992 done
  2060 done
  1993 
  2061 
  1994 lemma lg_lemma': "\<lbrakk>Suc 0 < x; Suc 0 < y\<rbrakk> \<Longrightarrow> rec_eval rec_lg [x, y] = lg x y"
  2062 lemma lg_lemma': "\<lbrakk>Suc 0 < x; Suc 0 < y\<rbrakk> \<Longrightarrow> rec_exec rec_lg [x, y] = lg x y"
  1995 apply(simp add: maxr_lg lg_maxr)
  2063 apply(simp add: maxr_lg lg_maxr)
  1996 done
  2064 done
  1997 
  2065 
  1998 lemma lg_lemma'': "\<not> Suc 0 < x \<Longrightarrow> rec_eval rec_lg [x, y] = lg x y"
  2066 lemma lg_lemma'': "\<not> Suc 0 < x \<Longrightarrow> rec_exec rec_lg [x, y] = lg x y"
  1999 apply(simp add: rec_eval.simps rec_lg_def Let_def lg.simps)
  2067 apply(simp add: rec_exec.simps rec_lg_def Let_def lg.simps)
  2000 done
  2068 done
  2001 
  2069 
  2002 lemma lg_lemma''': "\<not> Suc 0 < y \<Longrightarrow> rec_eval rec_lg [x, y] = lg x y"
  2070 lemma lg_lemma''': "\<not> Suc 0 < y \<Longrightarrow> rec_exec rec_lg [x, y] = lg x y"
  2003 apply(simp add: rec_eval.simps rec_lg_def Let_def lg.simps)
  2071 apply(simp add: rec_exec.simps rec_lg_def Let_def lg.simps)
  2004 done
  2072 done
  2005 
  2073 
  2006 text {*
  2074 text {*
  2007   The correctness of @{text "rec_lg"}.
  2075   The correctness of @{text "rec_lg"}.
  2008   *}
  2076   *}
  2009 lemma lg_lemma: "rec_eval rec_lg [x, y] = lg x y"
  2077 lemma lg_lemma: "rec_exec rec_lg [x, y] = lg x y"
  2010 apply(case_tac "Suc 0 < x \<and> Suc 0 < y", auto simp: 
  2078 apply(case_tac "Suc 0 < x \<and> Suc 0 < y", auto simp: 
  2011                             lg_lemma' lg_lemma'' lg_lemma''')
  2079                             lg_lemma' lg_lemma'' lg_lemma''')
  2012 done
  2080 done
  2013 
  2081 
  2014 text {*
  2082 text {*
  2030 declare Pi.simps[simp del]
  2098 declare Pi.simps[simp del]
  2031 
  2099 
  2032 text {*
  2100 text {*
  2033   The correctness of @{text "rec_entry"}.
  2101   The correctness of @{text "rec_entry"}.
  2034   *}
  2102   *}
  2035 lemma entry_lemma: "rec_eval rec_entry [str, i] = Entry str i"
  2103 lemma entry_lemma: "rec_exec rec_entry [str, i] = Entry str i"
  2036   by(simp add: rec_entry_def  rec_eval.simps lo_lemma pi_lemma)
  2104   by(simp add: rec_entry_def  rec_exec.simps lo_lemma pi_lemma)
  2037 
  2105 
  2038 
  2106 
  2039 subsection {* The construction of F *}
  2107 subsection {* The construction of F *}
  2040 
  2108 
  2041 text {*
  2109 text {*
  2055 | "rec_listsum2 vl (Suc n) = Cn vl rec_add [rec_listsum2 vl n, id vl n]"
  2123 | "rec_listsum2 vl (Suc n) = Cn vl rec_add [rec_listsum2 vl n, id vl n]"
  2056 
  2124 
  2057 declare listsum2.simps[simp del] rec_listsum2.simps[simp del]
  2125 declare listsum2.simps[simp del] rec_listsum2.simps[simp del]
  2058 
  2126 
  2059 lemma listsum2_lemma: "\<lbrakk>length xs = vl; n \<le> vl\<rbrakk> \<Longrightarrow> 
  2127 lemma listsum2_lemma: "\<lbrakk>length xs = vl; n \<le> vl\<rbrakk> \<Longrightarrow> 
  2060       rec_eval (rec_listsum2 vl n) xs = listsum2 xs n"
  2128       rec_exec (rec_listsum2 vl n) xs = listsum2 xs n"
  2061 apply(induct n, simp_all)
  2129 apply(induct n, simp_all)
  2062 apply(simp_all add: rec_eval.simps rec_listsum2.simps listsum2.simps)
  2130 apply(simp_all add: rec_exec.simps rec_listsum2.simps listsum2.simps)
  2063 done
  2131 done
  2064 
  2132 
  2065 fun strt' :: "nat list \<Rightarrow> nat \<Rightarrow> nat"
  2133 fun strt' :: "nat list \<Rightarrow> nat \<Rightarrow> nat"
  2066   where
  2134   where
  2067   "strt' xs 0 = 0"
  2135   "strt' xs 0 = 0"
  2079   Cn vl rec_power [Cn vl (constn 2) [id vl 0], rec_dbound]]])"
  2147   Cn vl rec_power [Cn vl (constn 2) [id vl 0], rec_dbound]]])"
  2080 
  2148 
  2081 declare strt'.simps[simp del] rec_strt'.simps[simp del]
  2149 declare strt'.simps[simp del] rec_strt'.simps[simp del]
  2082 
  2150 
  2083 lemma strt'_lemma: "\<lbrakk>length xs = vl; n \<le> vl\<rbrakk> \<Longrightarrow> 
  2151 lemma strt'_lemma: "\<lbrakk>length xs = vl; n \<le> vl\<rbrakk> \<Longrightarrow> 
  2084   rec_eval (rec_strt' vl n) xs = strt' xs n"
  2152   rec_exec (rec_strt' vl n) xs = strt' xs n"
  2085 apply(induct n)
  2153 apply(induct n)
  2086 apply(simp_all add: rec_eval.simps rec_strt'.simps strt'.simps
  2154 apply(simp_all add: rec_exec.simps rec_strt'.simps strt'.simps
  2087   Let_def power_lemma listsum2_lemma)
  2155   Let_def power_lemma listsum2_lemma)
  2088 done
  2156 done
  2089 
  2157 
  2090 text {*
  2158 text {*
  2091   @{text "strt"} corresponds to the @{text "strt"} function on page 90 of B book, but 
  2159   @{text "strt"} corresponds to the @{text "strt"} function on page 90 of B book, but 
  2106 fun rec_strt :: "nat \<Rightarrow> recf"
  2174 fun rec_strt :: "nat \<Rightarrow> recf"
  2107   where
  2175   where
  2108   "rec_strt vl = Cn vl (rec_strt' vl vl) (rec_map s vl)"
  2176   "rec_strt vl = Cn vl (rec_strt' vl vl) (rec_map s vl)"
  2109 
  2177 
  2110 lemma map_s_lemma: "length xs = vl \<Longrightarrow> 
  2178 lemma map_s_lemma: "length xs = vl \<Longrightarrow> 
  2111   map ((\<lambda>a. rec_eval a xs) \<circ> (\<lambda>i. Cn vl s [recf.id vl i]))
  2179   map ((\<lambda>a. rec_exec a xs) \<circ> (\<lambda>i. Cn vl s [recf.id vl i]))
  2112   [0..<vl]
  2180   [0..<vl]
  2113         = map Suc xs"
  2181         = map Suc xs"
  2114 apply(induct vl arbitrary: xs, simp, auto simp: rec_eval.simps)
  2182 apply(induct vl arbitrary: xs, simp, auto simp: rec_exec.simps)
  2115 apply(subgoal_tac "\<exists> ys y. xs = ys @ [y]", auto)
  2183 apply(subgoal_tac "\<exists> ys y. xs = ys @ [y]", auto)
  2116 proof -
  2184 proof -
  2117   fix ys y
  2185   fix ys y
  2118   assume ind: "\<And>xs. length xs = length (ys::nat list) \<Longrightarrow>
  2186   assume ind: "\<And>xs. length xs = length (ys::nat list) \<Longrightarrow>
  2119       map ((\<lambda>a. rec_eval a xs) \<circ> (\<lambda>i. Cn (length ys) s 
  2187       map ((\<lambda>a. rec_exec a xs) \<circ> (\<lambda>i. Cn (length ys) s 
  2120         [recf.id (length ys) (i)])) [0..<length ys] = map Suc xs"
  2188         [recf.id (length ys) (i)])) [0..<length ys] = map Suc xs"
  2121   show
  2189   show
  2122     "map ((\<lambda>a. rec_eval a (ys @ [y])) \<circ> (\<lambda>i. Cn (Suc (length ys)) s 
  2190     "map ((\<lambda>a. rec_exec a (ys @ [y])) \<circ> (\<lambda>i. Cn (Suc (length ys)) s 
  2123   [recf.id (Suc (length ys)) (i)])) [0..<length ys] = map Suc ys"
  2191   [recf.id (Suc (length ys)) (i)])) [0..<length ys] = map Suc ys"
  2124   proof -
  2192   proof -
  2125     have "map ((\<lambda>a. rec_eval a ys) \<circ> (\<lambda>i. Cn (length ys) s
  2193     have "map ((\<lambda>a. rec_exec a ys) \<circ> (\<lambda>i. Cn (length ys) s
  2126         [recf.id (length ys) (i)])) [0..<length ys] = map Suc ys"
  2194         [recf.id (length ys) (i)])) [0..<length ys] = map Suc ys"
  2127       apply(rule_tac ind, simp)
  2195       apply(rule_tac ind, simp)
  2128       done
  2196       done
  2129     moreover have
  2197     moreover have
  2130       "map ((\<lambda>a. rec_eval a (ys @ [y])) \<circ> (\<lambda>i. Cn (Suc (length ys)) s
  2198       "map ((\<lambda>a. rec_exec a (ys @ [y])) \<circ> (\<lambda>i. Cn (Suc (length ys)) s
  2131            [recf.id (Suc (length ys)) (i)])) [0..<length ys]
  2199            [recf.id (Suc (length ys)) (i)])) [0..<length ys]
  2132          = map ((\<lambda>a. rec_eval a ys) \<circ> (\<lambda>i. Cn (length ys) s 
  2200          = map ((\<lambda>a. rec_exec a ys) \<circ> (\<lambda>i. Cn (length ys) s 
  2133                  [recf.id (length ys) (i)])) [0..<length ys]"
  2201                  [recf.id (length ys) (i)])) [0..<length ys]"
  2134       apply(rule_tac map_ext, auto simp: rec_eval.simps nth_append)
  2202       apply(rule_tac map_ext, auto simp: rec_exec.simps nth_append)
  2135       done
  2203       done
  2136     ultimately show "?thesis"
  2204     ultimately show "?thesis"
  2137       by simp
  2205       by simp
  2138   qed
  2206   qed
  2139 next
  2207 next
  2147 
  2215 
  2148 text {*
  2216 text {*
  2149   The correctness of @{text "rec_strt"}.
  2217   The correctness of @{text "rec_strt"}.
  2150   *}
  2218   *}
  2151 lemma strt_lemma: "length xs = vl \<Longrightarrow> 
  2219 lemma strt_lemma: "length xs = vl \<Longrightarrow> 
  2152   rec_eval (rec_strt vl) xs = strt xs"
  2220   rec_exec (rec_strt vl) xs = strt xs"
  2153 apply(simp add: strt.simps rec_eval.simps strt'_lemma)
  2221 apply(simp add: strt.simps rec_exec.simps strt'_lemma)
  2154 apply(subgoal_tac "(map ((\<lambda>a. rec_eval a xs) \<circ> (\<lambda>i. Cn vl s [recf.id vl (i)])) [0..<vl])
  2222 apply(subgoal_tac "(map ((\<lambda>a. rec_exec a xs) \<circ> (\<lambda>i. Cn vl s [recf.id vl (i)])) [0..<vl])
  2155                   = map Suc xs", auto)
  2223                   = map Suc xs", auto)
  2156 apply(rule map_s_lemma, simp)
  2224 apply(rule map_s_lemma, simp)
  2157 done
  2225 done
  2158 
  2226 
  2159 text {*
  2227 text {*
  2170   where "rec_scan = Cn 1 rec_mod [id 1 0, constn 2]"
  2238   where "rec_scan = Cn 1 rec_mod [id 1 0, constn 2]"
  2171 
  2239 
  2172 text {*
  2240 text {*
  2173   The correctness of @{text "scan"}.
  2241   The correctness of @{text "scan"}.
  2174   *}
  2242   *}
  2175 lemma scan_lemma: "rec_eval rec_scan [r] = r mod 2"
  2243 lemma scan_lemma: "rec_exec rec_scan [r] = r mod 2"
  2176   by(simp add: rec_eval.simps rec_scan_def mod_lemma)
  2244   by(simp add: rec_exec.simps rec_scan_def mod_lemma)
  2177 
  2245 
  2178 fun newleft0 :: "nat list \<Rightarrow> nat"
  2246 fun newleft0 :: "nat list \<Rightarrow> nat"
  2179   where
  2247   where
  2180   "newleft0 [p, r] = p"
  2248   "newleft0 [p, r] = p"
  2181 
  2249 
  2293 
  2361 
  2294 text {*
  2362 text {*
  2295   The correctness of @{text "rec_newleft"}.
  2363   The correctness of @{text "rec_newleft"}.
  2296   *}
  2364   *}
  2297 lemma newleft_lemma: 
  2365 lemma newleft_lemma: 
  2298   "rec_eval rec_newleft [p, r, a] = newleft p r a"
  2366   "rec_exec rec_newleft [p, r, a] = newleft p r a"
  2299 proof(simp only: rec_newleft_def Let_def)
  2367 proof(simp only: rec_newleft_def Let_def)
  2300   let ?rgs = "[Cn 3 rec_newleft0 [recf.id 3 0, recf.id 3 1], Cn 3 rec_newleft2 
  2368   let ?rgs = "[Cn 3 rec_newleft0 [recf.id 3 0, recf.id 3 1], Cn 3 rec_newleft2 
  2301        [recf.id 3 0, recf.id 3 1], Cn 3 rec_newleft3 [recf.id 3 0, recf.id 3 1], recf.id 3 0]"
  2369        [recf.id 3 0, recf.id 3 1], Cn 3 rec_newleft3 [recf.id 3 0, recf.id 3 1], recf.id 3 0]"
  2302   let ?rrs = 
  2370   let ?rrs = 
  2303     "[Cn 3 rec_disj [Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 0) 
  2371     "[Cn 3 rec_disj [Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 0) 
  2304      [recf.id 3 0]], Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 1) [recf.id 3 0]]], 
  2372      [recf.id 3 0]], Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 1) [recf.id 3 0]]], 
  2305      Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 2) [recf.id 3 0]],
  2373      Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 2) [recf.id 3 0]],
  2306      Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 3) [recf.id 3 0]],
  2374      Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 3) [recf.id 3 0]],
  2307      Cn 3 rec_less [Cn 3 (constn 3) [recf.id 3 0], recf.id 3 2]]"
  2375      Cn 3 rec_less [Cn 3 (constn 3) [recf.id 3 0], recf.id 3 2]]"
  2308   have k1: "rec_eval (rec_embranch (zip ?rgs ?rrs)) [p, r, a]
  2376   have k1: "rec_exec (rec_embranch (zip ?rgs ?rrs)) [p, r, a]
  2309                          = Embranch (zip (map rec_eval ?rgs) (map (\<lambda>r args. 0 < rec_eval r args) ?rrs)) [p, r, a]"
  2377                          = Embranch (zip (map rec_exec ?rgs) (map (\<lambda>r args. 0 < rec_exec r args) ?rrs)) [p, r, a]"
  2310     apply(rule_tac embranch_lemma )
  2378     apply(rule_tac embranch_lemma )
  2311     apply(auto simp: numeral_3_eq_3 numeral_2_eq_2 rec_newleft0_def 
  2379     apply(auto simp: numeral_3_eq_3 numeral_2_eq_2 rec_newleft0_def 
  2312              rec_newleft1_def rec_newleft2_def rec_newleft3_def)+
  2380              rec_newleft1_def rec_newleft2_def rec_newleft3_def)+
  2313     apply(case_tac "a = 0 \<or> a = 1", rule_tac x = 0 in exI)
  2381     apply(case_tac "a = 0 \<or> a = 1", rule_tac x = 0 in exI)
  2314     prefer 2
  2382     prefer 2
  2315     apply(case_tac "a = 2", rule_tac x = "Suc 0" in exI)
  2383     apply(case_tac "a = 2", rule_tac x = "Suc 0" in exI)
  2316     prefer 2
  2384     prefer 2
  2317     apply(case_tac "a = 3", rule_tac x = "2" in exI)
  2385     apply(case_tac "a = 3", rule_tac x = "2" in exI)
  2318     prefer 2
  2386     prefer 2
  2319     apply(case_tac "a > 3", rule_tac x = "3" in exI, auto)
  2387     apply(case_tac "a > 3", rule_tac x = "3" in exI, auto)
  2320     apply(auto simp: rec_eval.simps)
  2388     apply(auto simp: rec_exec.simps)
  2321     apply(erule_tac [!] Suc_Suc_Suc_Suc_induct, auto simp: rec_eval.simps)
  2389     apply(erule_tac [!] Suc_Suc_Suc_Suc_induct, auto simp: rec_exec.simps)
  2322     done
  2390     done
  2323   have k2: "Embranch (zip (map rec_eval ?rgs) (map (\<lambda>r args. 0 < rec_eval r args) ?rrs)) [p, r, a] = newleft p r a"
  2391   have k2: "Embranch (zip (map rec_exec ?rgs) (map (\<lambda>r args. 0 < rec_exec r args) ?rrs)) [p, r, a] = newleft p r a"
  2324     apply(simp add: Embranch.simps)
  2392     apply(simp add: Embranch.simps)
  2325     apply(simp add: rec_eval.simps)
  2393     apply(simp add: rec_exec.simps)
  2326     apply(auto simp: newleft.simps rec_newleft0_def rec_eval.simps
  2394     apply(auto simp: newleft.simps rec_newleft0_def rec_exec.simps
  2327                      rec_newleft1_def rec_newleft2_def rec_newleft3_def)
  2395                      rec_newleft1_def rec_newleft2_def rec_newleft3_def)
  2328     done
  2396     done
  2329   from k1 and k2 show 
  2397   from k1 and k2 show 
  2330    "rec_eval (rec_embranch (zip ?rgs ?rrs)) [p, r, a] = newleft p r a"
  2398    "rec_exec (rec_embranch (zip ?rgs ?rrs)) [p, r, a] = newleft p r a"
  2331     by simp
  2399     by simp
  2332 qed
  2400 qed
  2333 
  2401 
  2334 text {* 
  2402 text {* 
  2335   The @{text "newrght"} function is one similar to @{text "newleft"}, but used to 
  2403   The @{text "newrght"} function is one similar to @{text "newleft"}, but used to 
  2382 done
  2450 done
  2383 
  2451 
  2384 text {*
  2452 text {*
  2385   The correctness of @{text "rec_newrght"}.
  2453   The correctness of @{text "rec_newrght"}.
  2386   *}
  2454   *}
  2387 lemma newrght_lemma: "rec_eval rec_newrght [p, r, a] = newrght p r a"
  2455 lemma newrght_lemma: "rec_exec rec_newrght [p, r, a] = newrght p r a"
  2388 proof(simp only: rec_newrght_def Let_def)
  2456 proof(simp only: rec_newrght_def Let_def)
  2389   let ?gs' = "[newrgt0, newrgt1, newrgt2, newrgt3, \<lambda> zs. zs ! 1]"
  2457   let ?gs' = "[newrgt0, newrgt1, newrgt2, newrgt3, \<lambda> zs. zs ! 1]"
  2390   let ?r0 = "\<lambda> zs. zs ! 2 = 0"
  2458   let ?r0 = "\<lambda> zs. zs ! 2 = 0"
  2391   let ?r1 = "\<lambda> zs. zs ! 2 = 1"
  2459   let ?r1 = "\<lambda> zs. zs ! 2 = 1"
  2392   let ?r2 = "\<lambda> zs. zs ! 2 = 2"
  2460   let ?r2 = "\<lambda> zs. zs ! 2 = 2"
  2403  "[Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 0) [recf.id 3 0]], Cn 3 rec_eq [recf.id 3 2, 
  2471  "[Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 0) [recf.id 3 0]], Cn 3 rec_eq [recf.id 3 2, 
  2404     Cn 3 (constn 1) [recf.id 3 0]], Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 2) [recf.id 3 0]],
  2472     Cn 3 (constn 1) [recf.id 3 0]], Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 2) [recf.id 3 0]],
  2405      Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 3) [recf.id 3 0]], 
  2473      Cn 3 rec_eq [recf.id 3 2, Cn 3 (constn 3) [recf.id 3 0]], 
  2406        Cn 3 rec_less [Cn 3 (constn 3) [recf.id 3 0], recf.id 3 2]]"
  2474        Cn 3 rec_less [Cn 3 (constn 3) [recf.id 3 0], recf.id 3 2]]"
  2407     
  2475     
  2408   have k1: "rec_eval (rec_embranch (zip ?rgs ?rrs)) [p, r, a]
  2476   have k1: "rec_exec (rec_embranch (zip ?rgs ?rrs)) [p, r, a]
  2409     = Embranch (zip (map rec_eval ?rgs) (map (\<lambda>r args. 0 < rec_eval r args) ?rrs)) [p, r, a]"
  2477     = Embranch (zip (map rec_exec ?rgs) (map (\<lambda>r args. 0 < rec_exec r args) ?rrs)) [p, r, a]"
  2410     apply(rule_tac embranch_lemma)
  2478     apply(rule_tac embranch_lemma)
  2411     apply(auto simp: numeral_3_eq_3 numeral_2_eq_2 rec_newrgt0_def 
  2479     apply(auto simp: numeral_3_eq_3 numeral_2_eq_2 rec_newrgt0_def 
  2412              rec_newrgt1_def rec_newrgt2_def rec_newrgt3_def)+
  2480              rec_newrgt1_def rec_newrgt2_def rec_newrgt3_def)+
  2413     apply(case_tac "a = 0", rule_tac x = 0 in exI)
  2481     apply(case_tac "a = 0", rule_tac x = 0 in exI)
  2414     prefer 2
  2482     prefer 2
  2416     prefer 2
  2484     prefer 2
  2417     apply(case_tac "a = 2", rule_tac x = "2" in exI)
  2485     apply(case_tac "a = 2", rule_tac x = "2" in exI)
  2418     prefer 2
  2486     prefer 2
  2419     apply(case_tac "a = 3", rule_tac x = "3" in exI)
  2487     apply(case_tac "a = 3", rule_tac x = "3" in exI)
  2420     prefer 2
  2488     prefer 2
  2421     apply(case_tac "a > 3", rule_tac x = "4" in exI, auto simp: rec_eval.simps)
  2489     apply(case_tac "a > 3", rule_tac x = "4" in exI, auto simp: rec_exec.simps)
  2422     apply(erule_tac [!] Suc_5_induct, auto simp: rec_eval.simps)
  2490     apply(erule_tac [!] Suc_5_induct, auto simp: rec_exec.simps)
  2423     done
  2491     done
  2424   have k2: "Embranch (zip (map rec_eval ?rgs)
  2492   have k2: "Embranch (zip (map rec_exec ?rgs)
  2425     (map (\<lambda>r args. 0 < rec_eval r args) ?rrs)) [p, r, a] = newrght p r a"
  2493     (map (\<lambda>r args. 0 < rec_exec r args) ?rrs)) [p, r, a] = newrght p r a"
  2426     apply(auto simp:Embranch.simps rec_eval.simps)
  2494     apply(auto simp:Embranch.simps rec_exec.simps)
  2427     apply(auto simp: newrght.simps rec_newrgt3_def rec_newrgt2_def
  2495     apply(auto simp: newrght.simps rec_newrgt3_def rec_newrgt2_def
  2428                      rec_newrgt1_def rec_newrgt0_def rec_eval.simps
  2496                      rec_newrgt1_def rec_newrgt0_def rec_exec.simps
  2429                      scan_lemma)
  2497                      scan_lemma)
  2430     done
  2498     done
  2431   from k1 and k2 show 
  2499   from k1 and k2 show 
  2432     "rec_eval (rec_embranch (zip ?rgs ?rrs)) [p, r, a] =      
  2500     "rec_exec (rec_embranch (zip ?rgs ?rrs)) [p, r, a] =      
  2433                                     newrght p r a" by simp
  2501                                     newrght p r a" by simp
  2434 qed
  2502 qed
  2435 
  2503 
  2436 declare Entry.simps[simp del]
  2504 declare Entry.simps[simp del]
  2437 
  2505 
  2464              Cn 3 rec_eq [id 3 1, Cn 3 (constn 0) [id 3 0]]]] "
  2532              Cn 3 rec_eq [id 3 1, Cn 3 (constn 0) [id 3 0]]]] "
  2465 
  2533 
  2466 text {*
  2534 text {*
  2467   The correctness of @{text "actn"}.
  2535   The correctness of @{text "actn"}.
  2468   *}
  2536   *}
  2469 lemma actn_lemma: "rec_eval rec_actn [m, q, r] = actn m q r"
  2537 lemma actn_lemma: "rec_exec rec_actn [m, q, r] = actn m q r"
  2470   by(auto simp: rec_actn_def rec_eval.simps entry_lemma scan_lemma)
  2538   by(auto simp: rec_actn_def rec_exec.simps entry_lemma scan_lemma)
  2471 
  2539 
  2472 fun newstat :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat"
  2540 fun newstat :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat"
  2473   where
  2541   where
  2474   "newstat m q r = (if q \<noteq> 0 then Entry m (4*(q - 1) + 2*scan r + 1)
  2542   "newstat m q r = (if q \<noteq> 0 then Entry m (4*(q - 1) + 2*scan r + 1)
  2475                     else 0)"
  2543                     else 0)"
  2484            Cn 3 rec_scan [id 3 2]], Cn 3 (constn 1) [id 3 0]]]], 
  2552            Cn 3 rec_scan [id 3 2]], Cn 3 (constn 1) [id 3 0]]]], 
  2485            Cn 3 rec_noteq [id 3 1, Cn 3 (constn 0) [id 3 0]]], 
  2553            Cn 3 rec_noteq [id 3 1, Cn 3 (constn 0) [id 3 0]]], 
  2486            Cn 3 rec_mult [Cn 3 (constn 0) [id 3 0], 
  2554            Cn 3 rec_mult [Cn 3 (constn 0) [id 3 0], 
  2487            Cn 3 rec_eq [id 3 1, Cn 3 (constn 0) [id 3 0]]]] "
  2555            Cn 3 rec_eq [id 3 1, Cn 3 (constn 0) [id 3 0]]]] "
  2488 
  2556 
  2489 lemma newstat_lemma: "rec_eval rec_newstat [m, q, r] = newstat m q r"
  2557 lemma newstat_lemma: "rec_exec rec_newstat [m, q, r] = newstat m q r"
  2490 by(auto simp:  rec_eval.simps entry_lemma scan_lemma rec_newstat_def)
  2558 by(auto simp:  rec_exec.simps entry_lemma scan_lemma rec_newstat_def)
  2491 
  2559 
  2492 declare newstat.simps[simp del] actn.simps[simp del]
  2560 declare newstat.simps[simp del] actn.simps[simp del]
  2493 
  2561 
  2494 text{*code the configuration*}
  2562 text{*code the configuration*}
  2495 
  2563 
  2502   "rec_trpl = Cn 3 rec_mult [Cn 3 rec_mult 
  2570   "rec_trpl = Cn 3 rec_mult [Cn 3 rec_mult 
  2503        [Cn 3 rec_power [Cn 3 (constn (Pi 0)) [id 3 0], id 3 0], 
  2571        [Cn 3 rec_power [Cn 3 (constn (Pi 0)) [id 3 0], id 3 0], 
  2504         Cn 3 rec_power [Cn 3 (constn (Pi 1)) [id 3 0], id 3 1]],
  2572         Cn 3 rec_power [Cn 3 (constn (Pi 1)) [id 3 0], id 3 1]],
  2505         Cn 3 rec_power [Cn 3 (constn (Pi 2)) [id 3 0], id 3 2]]"
  2573         Cn 3 rec_power [Cn 3 (constn (Pi 2)) [id 3 0], id 3 2]]"
  2506 declare trpl.simps[simp del]
  2574 declare trpl.simps[simp del]
  2507 lemma trpl_lemma: "rec_eval rec_trpl [p, q, r] = trpl p q r"
  2575 lemma trpl_lemma: "rec_exec rec_trpl [p, q, r] = trpl p q r"
  2508 by(auto simp: rec_trpl_def rec_eval.simps power_lemma trpl.simps)
  2576 by(auto simp: rec_trpl_def rec_exec.simps power_lemma trpl.simps)
  2509 
  2577 
  2510 text{*left, stat, rght: decode func*}
  2578 text{*left, stat, rght: decode func*}
  2511 fun left :: "nat \<Rightarrow> nat"
  2579 fun left :: "nat \<Rightarrow> nat"
  2512   where
  2580   where
  2513   "left c = lo c (Pi 0)"
  2581   "left c = lo c (Pi 0)"
  2553                   [Cn vl (constn 0) [id vl 0], 
  2621                   [Cn vl (constn 0) [id vl 0], 
  2554                    Cn vl (constn 1) [id vl 0], 
  2622                    Cn vl (constn 1) [id vl 0], 
  2555                    Cn vl (rec_strt (vl - 1)) 
  2623                    Cn vl (rec_strt (vl - 1)) 
  2556                         (map (\<lambda> i. id vl (i)) [1..<vl])]"
  2624                         (map (\<lambda> i. id vl (i)) [1..<vl])]"
  2557 
  2625 
  2558 lemma left_lemma: "rec_eval rec_left [c] = left c"
  2626 lemma left_lemma: "rec_exec rec_left [c] = left c"
  2559 by(simp add: rec_eval.simps rec_left_def left.simps lo_lemma)
  2627 by(simp add: rec_exec.simps rec_left_def left.simps lo_lemma)
  2560       
  2628       
  2561 lemma right_lemma: "rec_eval rec_right [c] = rght c"
  2629 lemma right_lemma: "rec_exec rec_right [c] = rght c"
  2562 by(simp add: rec_eval.simps rec_right_def rght.simps lo_lemma)
  2630 by(simp add: rec_exec.simps rec_right_def rght.simps lo_lemma)
  2563 
  2631 
  2564 lemma stat_lemma: "rec_eval rec_stat [c] = stat c"
  2632 lemma stat_lemma: "rec_exec rec_stat [c] = stat c"
  2565 by(simp add: rec_eval.simps rec_stat_def stat.simps lo_lemma)
  2633 by(simp add: rec_exec.simps rec_stat_def stat.simps lo_lemma)
  2566  
  2634  
  2567 declare rec_strt.simps[simp del] strt.simps[simp del]
  2635 declare rec_strt.simps[simp del] strt.simps[simp del]
  2568 
  2636 
  2569 lemma map_cons_eq: 
  2637 lemma map_cons_eq: 
  2570   "(map ((\<lambda>a. rec_eval a (m # xs)) \<circ> 
  2638   "(map ((\<lambda>a. rec_exec a (m # xs)) \<circ> 
  2571     (\<lambda>i. recf.id (Suc (length xs)) (i))) 
  2639     (\<lambda>i. recf.id (Suc (length xs)) (i))) 
  2572           [Suc 0..<Suc (length xs)])
  2640           [Suc 0..<Suc (length xs)])
  2573         = map (\<lambda> i. xs ! (i - 1)) [Suc 0..<Suc (length xs)]"
  2641         = map (\<lambda> i. xs ! (i - 1)) [Suc 0..<Suc (length xs)]"
  2574 apply(rule map_ext, auto)
  2642 apply(rule map_ext, auto)
  2575 apply(auto simp: rec_eval.simps nth_append nth_Cons split: nat.split)
  2643 apply(auto simp: rec_exec.simps nth_append nth_Cons split: nat.split)
  2576 done
  2644 done
  2577 
  2645 
  2578 lemma list_map_eq: 
  2646 lemma list_map_eq: 
  2579   "vl = length (xs::nat list) \<Longrightarrow> map (\<lambda> i. xs ! (i - 1))
  2647   "vl = length (xs::nat list) \<Longrightarrow> map (\<lambda> i. xs ! (i - 1))
  2580                                           [Suc 0..<Suc vl] = xs"
  2648                                           [Suc 0..<Suc vl] = xs"
  2614     done
  2682     done
  2615 qed
  2683 qed
  2616 
  2684 
  2617 lemma [elim]: 
  2685 lemma [elim]: 
  2618   "Suc 0 \<le> length xs \<Longrightarrow> 
  2686   "Suc 0 \<le> length xs \<Longrightarrow> 
  2619      (map ((\<lambda>a. rec_eval a (m # xs)) \<circ> 
  2687      (map ((\<lambda>a. rec_exec a (m # xs)) \<circ> 
  2620          (\<lambda>i. recf.id (Suc (length xs)) (i))) 
  2688          (\<lambda>i. recf.id (Suc (length xs)) (i))) 
  2621              [Suc 0..<length xs] @ [(m # xs) ! length xs]) = xs"
  2689              [Suc 0..<length xs] @ [(m # xs) ! length xs]) = xs"
  2622 using map_cons_eq[of m xs]
  2690 using map_cons_eq[of m xs]
  2623 apply(simp del: map_eq_conv add: rec_eval.simps)
  2691 apply(simp del: map_eq_conv add: rec_exec.simps)
  2624 using list_map_eq[of "length xs" xs]
  2692 using list_map_eq[of "length xs" xs]
  2625 apply(simp)
  2693 apply(simp)
  2626 done
  2694 done
  2627 
  2695 
  2628     
  2696     
  2629 lemma inpt_lemma:
  2697 lemma inpt_lemma:
  2630   "\<lbrakk>Suc (length xs) = vl\<rbrakk> \<Longrightarrow> 
  2698   "\<lbrakk>Suc (length xs) = vl\<rbrakk> \<Longrightarrow> 
  2631             rec_eval (rec_inpt vl) (m # xs) = inpt m xs"
  2699             rec_exec (rec_inpt vl) (m # xs) = inpt m xs"
  2632 apply(auto simp: rec_eval.simps rec_inpt_def 
  2700 apply(auto simp: rec_exec.simps rec_inpt_def 
  2633                  trpl_lemma inpt.simps strt_lemma)
  2701                  trpl_lemma inpt.simps strt_lemma)
  2634 apply(subgoal_tac
  2702 apply(subgoal_tac
  2635   "(map ((\<lambda>a. rec_eval a (m # xs)) \<circ> 
  2703   "(map ((\<lambda>a. rec_exec a (m # xs)) \<circ> 
  2636           (\<lambda>i. recf.id (Suc (length xs)) (i))) 
  2704           (\<lambda>i. recf.id (Suc (length xs)) (i))) 
  2637             [Suc 0..<length xs] @ [(m # xs) ! length xs]) = xs", simp)
  2705             [Suc 0..<length xs] @ [(m # xs) ! length xs]) = xs", simp)
  2638 apply(auto, case_tac xs, auto)
  2706 apply(auto, case_tac xs, auto)
  2639 done
  2707 done
  2640 
  2708 
  2654                              Cn 2 rec_right [id 2 1], 
  2722                              Cn 2 rec_right [id 2 1], 
  2655                              Cn 2 rec_actn [id 2 0, 
  2723                              Cn 2 rec_actn [id 2 0, 
  2656                                    Cn 2 rec_stat [id 2 1], 
  2724                                    Cn 2 rec_stat [id 2 1], 
  2657                              Cn 2 rec_right [id 2 1]]]]"
  2725                              Cn 2 rec_right [id 2 1]]]]"
  2658 
  2726 
  2659 lemma newconf_lemma: "rec_eval rec_newconf [m ,c] = newconf m c"
  2727 lemma newconf_lemma: "rec_exec rec_newconf [m ,c] = newconf m c"
  2660 by(auto simp: rec_newconf_def rec_eval.simps 
  2728 by(auto simp: rec_newconf_def rec_exec.simps 
  2661               trpl_lemma newleft_lemma left_lemma
  2729               trpl_lemma newleft_lemma left_lemma
  2662               right_lemma stat_lemma newrght_lemma actn_lemma 
  2730               right_lemma stat_lemma newrght_lemma actn_lemma 
  2663                newstat_lemma stat_lemma newconf.simps)
  2731                newstat_lemma stat_lemma newconf.simps)
  2664 
  2732 
  2665 declare newconf_lemma[simp]
  2733 declare newconf_lemma[simp]
  2683   where
  2751   where
  2684   "rec_conf = Pr 2 (Cn 2 rec_trpl [Cn 2 (constn 0) [id 2 0], Cn 2 (constn (Suc 0)) [id 2 0], id 2 1])
  2752   "rec_conf = Pr 2 (Cn 2 rec_trpl [Cn 2 (constn 0) [id 2 0], Cn 2 (constn (Suc 0)) [id 2 0], id 2 1])
  2685                   (Cn 4 rec_newconf [id 4 0, id 4 3])"
  2753                   (Cn 4 rec_newconf [id 4 0, id 4 3])"
  2686 
  2754 
  2687 lemma conf_step: 
  2755 lemma conf_step: 
  2688   "rec_eval rec_conf [m, r, Suc t] =
  2756   "rec_exec rec_conf [m, r, Suc t] =
  2689          rec_eval rec_newconf [m, rec_eval rec_conf [m, r, t]]"
  2757          rec_exec rec_newconf [m, rec_exec rec_conf [m, r, t]]"
  2690 proof -
  2758 proof -
  2691   have "rec_eval rec_conf ([m, r] @ [Suc t]) = 
  2759   have "rec_exec rec_conf ([m, r] @ [Suc t]) = 
  2692           rec_eval rec_newconf [m, rec_eval rec_conf [m, r, t]]"
  2760           rec_exec rec_newconf [m, rec_exec rec_conf [m, r, t]]"
  2693     by(simp only: rec_conf_def rec_pr_Suc_simp_rewrite,
  2761     by(simp only: rec_conf_def rec_pr_Suc_simp_rewrite,
  2694         simp add: rec_eval.simps)
  2762         simp add: rec_exec.simps)
  2695   thus "rec_eval rec_conf [m, r, Suc t] =
  2763   thus "rec_exec rec_conf [m, r, Suc t] =
  2696                 rec_eval rec_newconf [m, rec_eval rec_conf [m, r, t]]"
  2764                 rec_exec rec_newconf [m, rec_exec rec_conf [m, r, t]]"
  2697     by simp
  2765     by simp
  2698 qed
  2766 qed
  2699 
  2767 
  2700 text {*
  2768 text {*
  2701   The correctness of @{text "rec_conf"}.
  2769   The correctness of @{text "rec_conf"}.
  2702   *}
  2770   *}
  2703 lemma conf_lemma: 
  2771 lemma conf_lemma: 
  2704   "rec_eval rec_conf [m, r, t] = conf m r t"
  2772   "rec_exec rec_conf [m, r, t] = conf m r t"
  2705 apply(induct t)
  2773 apply(induct t)
  2706 apply(simp add: rec_conf_def rec_eval.simps conf.simps inpt_lemma trpl_lemma)
  2774 apply(simp add: rec_conf_def rec_exec.simps conf.simps inpt_lemma trpl_lemma)
  2707 apply(simp add: conf_step conf.simps)
  2775 apply(simp add: conf_step conf.simps)
  2708 done
  2776 done
  2709 
  2777 
  2710 text {*
  2778 text {*
  2711   @{text "NSTD c"} returns true if the configureation coded by @{text "c"} is no a stardard
  2779   @{text "NSTD c"} returns true if the configureation coded by @{text "c"} is no a stardard
  2733                                     [Cn 1 rec_add        
  2801                                     [Cn 1 rec_add        
  2734                                      [rec_right, constn 1], 
  2802                                      [rec_right, constn 1], 
  2735                                             constn 2]], constn 1]]],
  2803                                             constn 2]], constn 1]]],
  2736                Cn 1 rec_eq [rec_right, constn 0]]"
  2804                Cn 1 rec_eq [rec_right, constn 0]]"
  2737 
  2805 
  2738 lemma NSTD_lemma1: "rec_eval rec_NSTD [c] = Suc 0 \<or>
  2806 lemma NSTD_lemma1: "rec_exec rec_NSTD [c] = Suc 0 \<or>
  2739                    rec_eval rec_NSTD [c] = 0"
  2807                    rec_exec rec_NSTD [c] = 0"
  2740 by(simp add: rec_eval.simps rec_NSTD_def)
  2808 by(simp add: rec_exec.simps rec_NSTD_def)
  2741 
  2809 
  2742 declare NSTD.simps[simp del]
  2810 declare NSTD.simps[simp del]
  2743 lemma NSTD_lemma2': "(rec_eval rec_NSTD [c] = Suc 0) \<Longrightarrow> NSTD c"
  2811 lemma NSTD_lemma2': "(rec_exec rec_NSTD [c] = Suc 0) \<Longrightarrow> NSTD c"
  2744 apply(simp add: rec_eval.simps rec_NSTD_def stat_lemma left_lemma 
  2812 apply(simp add: rec_exec.simps rec_NSTD_def stat_lemma left_lemma 
  2745                 lg_lemma right_lemma power_lemma NSTD.simps eq_lemma)
  2813                 lg_lemma right_lemma power_lemma NSTD.simps eq_lemma)
  2746 apply(auto)
  2814 apply(auto)
  2747 apply(case_tac "0 < left c", simp, simp)
  2815 apply(case_tac "0 < left c", simp, simp)
  2748 done
  2816 done
  2749 
  2817 
  2750 lemma NSTD_lemma2'': 
  2818 lemma NSTD_lemma2'': 
  2751   "NSTD c \<Longrightarrow> (rec_eval rec_NSTD [c] = Suc 0)"
  2819   "NSTD c \<Longrightarrow> (rec_exec rec_NSTD [c] = Suc 0)"
  2752 apply(simp add: rec_eval.simps rec_NSTD_def stat_lemma 
  2820 apply(simp add: rec_exec.simps rec_NSTD_def stat_lemma 
  2753          left_lemma lg_lemma right_lemma power_lemma NSTD.simps)
  2821          left_lemma lg_lemma right_lemma power_lemma NSTD.simps)
  2754 apply(auto split: if_splits)
  2822 apply(auto split: if_splits)
  2755 done
  2823 done
  2756 
  2824 
  2757 text {*
  2825 text {*
  2758   The correctness of @{text "NSTD"}.
  2826   The correctness of @{text "NSTD"}.
  2759   *}
  2827   *}
  2760 lemma NSTD_lemma2: "(rec_eval rec_NSTD [c] = Suc 0) = NSTD c"
  2828 lemma NSTD_lemma2: "(rec_exec rec_NSTD [c] = Suc 0) = NSTD c"
  2761 using NSTD_lemma1
  2829 using NSTD_lemma1
  2762 apply(auto intro: NSTD_lemma2' NSTD_lemma2'')
  2830 apply(auto intro: NSTD_lemma2' NSTD_lemma2'')
  2763 done
  2831 done
  2764 
  2832 
  2765 fun nstd :: "nat \<Rightarrow> nat"
  2833 fun nstd :: "nat \<Rightarrow> nat"
  2766   where
  2834   where
  2767   "nstd c = (if NSTD c then 1 else 0)"
  2835   "nstd c = (if NSTD c then 1 else 0)"
  2768 
  2836 
  2769 lemma nstd_lemma: "rec_eval rec_NSTD [c] = nstd c"
  2837 lemma nstd_lemma: "rec_exec rec_NSTD [c] = nstd c"
  2770 using NSTD_lemma1
  2838 using NSTD_lemma1
  2771 apply(simp add: NSTD_lemma2, auto)
  2839 apply(simp add: NSTD_lemma2, auto)
  2772 done
  2840 done
  2773 
  2841 
  2774 text{* 
  2842 text{* 
  2788 
  2856 
  2789 text {*
  2857 text {*
  2790   The correctness of @{text "rec_nonstop"}.
  2858   The correctness of @{text "rec_nonstop"}.
  2791   *}
  2859   *}
  2792 lemma nonstop_lemma: 
  2860 lemma nonstop_lemma: 
  2793   "rec_eval rec_nonstop [m, r, t] = nonstop m r t"
  2861   "rec_exec rec_nonstop [m, r, t] = nonstop m r t"
  2794 apply(simp add: rec_eval.simps rec_nonstop_def nstd_lemma conf_lemma)
  2862 apply(simp add: rec_exec.simps rec_nonstop_def nstd_lemma conf_lemma)
  2795 done
  2863 done
  2796 
  2864 
  2797 text{*
  2865 text{*
  2798   @{text "rec_halt"} is the recursive function calculating the steps a TM needs to execute before
  2866   @{text "rec_halt"} is the recursive function calculating the steps a TM needs to execute before
  2799   to reach a stardard final configuration. This recursive function is the only one
  2867   to reach a stardard final configuration. This recursive function is the only one
  2983      rec_newstat_def)
  3051      rec_newstat_def)
  2984 apply(tactic {* resolve_tac [@{thm prime_cn}, 
  3052 apply(tactic {* resolve_tac [@{thm prime_cn}, 
  2985     @{thm prime_id}, @{thm prime_pr}] 1*}, auto+)+
  3053     @{thm prime_id}, @{thm prime_pr}] 1*}, auto+)+
  2986 done
  3054 done
  2987 
  3055 
  2988 lemma primerec_terminates: 
  3056 lemma primerec_terminate: 
  2989   "\<lbrakk>primerec f x; length xs = x\<rbrakk> \<Longrightarrow> terminates f xs"
  3057   "\<lbrakk>primerec f x; length xs = x\<rbrakk> \<Longrightarrow> terminate f xs"
  2990 proof(induct arbitrary: xs rule: primerec.induct)
  3058 proof(induct arbitrary: xs rule: primerec.induct)
  2991   fix xs
  3059   fix xs
  2992   assume "length (xs::nat list) = Suc 0"  thus "terminates z xs"
  3060   assume "length (xs::nat list) = Suc 0"  thus "terminate z xs"
  2993     by(case_tac xs, auto intro: termi_z)
  3061     by(case_tac xs, auto intro: termi_z)
  2994 next
  3062 next
  2995   fix xs
  3063   fix xs
  2996   assume "length (xs::nat list) = Suc 0" thus "terminates s xs"
  3064   assume "length (xs::nat list) = Suc 0" thus "terminate s xs"
  2997     by(case_tac xs, auto intro: termi_s)
  3065     by(case_tac xs, auto intro: termi_s)
  2998 next
  3066 next
  2999   fix n m xs
  3067   fix n m xs
  3000   assume "n < m" "length (xs::nat list) = m"  thus "terminates (id m n) xs"
  3068   assume "n < m" "length (xs::nat list) = m"  thus "terminate (id m n) xs"
  3001     by(erule_tac termi_id, simp)
  3069     by(erule_tac termi_id, simp)
  3002 next
  3070 next
  3003   fix f k gs m n xs
  3071   fix f k gs m n xs
  3004   assume ind: "\<forall>i<length gs. primerec (gs ! i) m \<and> (\<forall>x. length x = m \<longrightarrow> terminates (gs ! i) x)"
  3072   assume ind: "\<forall>i<length gs. primerec (gs ! i) m \<and> (\<forall>x. length x = m \<longrightarrow> terminate (gs ! i) x)"
  3005   and ind2: "\<And> xs. length xs = k \<Longrightarrow> terminates f xs"
  3073   and ind2: "\<And> xs. length xs = k \<Longrightarrow> terminate f xs"
  3006   and h: "primerec f k"  "length gs = k" "m = n" "length (xs::nat list) = m"
  3074   and h: "primerec f k"  "length gs = k" "m = n" "length (xs::nat list) = m"
  3007   have "terminates f (map (\<lambda>g. rec_eval g xs) gs)"
  3075   have "terminate f (map (\<lambda>g. rec_exec g xs) gs)"
  3008     using ind2[of "(map (\<lambda>g. rec_eval g xs) gs)"] h
  3076     using ind2[of "(map (\<lambda>g. rec_exec g xs) gs)"] h
  3009     by simp
  3077     by simp
  3010   moreover have "\<forall>g\<in>set gs. terminates g xs"
  3078   moreover have "\<forall>g\<in>set gs. terminate g xs"
  3011     using ind h
  3079     using ind h
  3012     by(auto simp: set_conv_nth)
  3080     by(auto simp: set_conv_nth)
  3013   ultimately show "terminates (Cn n f gs) xs"
  3081   ultimately show "terminate (Cn n f gs) xs"
  3014     using h
  3082     using h
  3015     by(rule_tac termi_cn, auto)
  3083     by(rule_tac termi_cn, auto)
  3016 next
  3084 next
  3017   fix f n g m xs
  3085   fix f n g m xs
  3018   assume ind1: "\<And>xs. length xs = n \<Longrightarrow> terminates f xs"
  3086   assume ind1: "\<And>xs. length xs = n \<Longrightarrow> terminate f xs"
  3019   and ind2: "\<And>xs. length xs = Suc (Suc n) \<Longrightarrow> terminates g xs"
  3087   and ind2: "\<And>xs. length xs = Suc (Suc n) \<Longrightarrow> terminate g xs"
  3020   and h: "primerec f n" " primerec g (Suc (Suc n))" " m = Suc n" "length (xs::nat list) = m"
  3088   and h: "primerec f n" " primerec g (Suc (Suc n))" " m = Suc n" "length (xs::nat list) = m"
  3021   have "\<forall>y<last xs. terminates g (butlast xs @ [y, rec_eval (Pr n f g) (butlast xs @ [y])])"
  3089   have "\<forall>y<last xs. terminate g (butlast xs @ [y, rec_exec (Pr n f g) (butlast xs @ [y])])"
  3022     using h
  3090     using h
  3023     apply(auto) 
  3091     apply(auto) 
  3024     by(rule_tac ind2, simp)
  3092     by(rule_tac ind2, simp)
  3025   moreover have "terminates f (butlast xs)"
  3093   moreover have "terminate f (butlast xs)"
  3026     using ind1[of "butlast xs"] h
  3094     using ind1[of "butlast xs"] h
  3027     by simp
  3095     by simp
  3028  moreover have "length (butlast xs) = n"
  3096  moreover have "length (butlast xs) = n"
  3029    using h by simp
  3097    using h by simp
  3030  ultimately have "terminates (Pr n f g) (butlast xs @ [last xs])"
  3098  ultimately have "terminate (Pr n f g) (butlast xs @ [last xs])"
  3031    by(rule_tac termi_pr, simp_all)
  3099    by(rule_tac termi_pr, simp_all)
  3032  thus "terminates (Pr n f g) xs"
  3100  thus "terminate (Pr n f g) xs"
  3033    using h
  3101    using h
  3034    by(case_tac "xs = []", auto)
  3102    by(case_tac "xs = []", auto)
  3035 qed
  3103 qed
  3036 
  3104 
  3037 text {*
  3105 text {*
  3038   The following lemma gives the correctness of @{text "rec_halt"}.
  3106   The following lemma gives the correctness of @{text "rec_halt"}.
  3039   It says: if @{text "rec_halt"} calculates that the TM coded by @{text "m"}
  3107   It says: if @{text "rec_halt"} calculates that the TM coded by @{text "m"}
  3040   will reach a standard final configuration after @{text "t"} steps of execution, 
  3108   will reach a standard final configuration after @{text "t"} steps of execution, then it is indeed so.
  3041   then it is indeed so.
       
  3042   *}
  3109   *}
  3043  
  3110  
  3044 
       
  3045 text {*F: universal machine*}
  3111 text {*F: universal machine*}
  3046 
  3112 
  3047 text {*
  3113 text {*
  3048   @{text "valu r"} extracts computing result out of the right number @{text "r"}.
  3114   @{text "valu r"} extracts computing result out of the right number @{text "r"}.
  3049   *}
  3115   *}
  3059   "rec_valu = Cn 1 rec_minus [Cn 1 rec_lg [s, constn 2], constn 1]"
  3125   "rec_valu = Cn 1 rec_minus [Cn 1 rec_lg [s, constn 2], constn 1]"
  3060 
  3126 
  3061 text {*
  3127 text {*
  3062   The correctness of @{text "rec_valu"}.
  3128   The correctness of @{text "rec_valu"}.
  3063 *}
  3129 *}
  3064 lemma value_lemma: "rec_eval rec_valu [r] = valu r"
  3130 lemma value_lemma: "rec_exec rec_valu [r] = valu r"
  3065 apply(simp add: rec_eval.simps rec_valu_def lg_lemma)
  3131 apply(simp add: rec_exec.simps rec_valu_def lg_lemma)
  3066 done
  3132 done
  3067 
  3133 
  3068 lemma [intro]: "primerec rec_valu (Suc 0)"
  3134 lemma [intro]: "primerec rec_valu (Suc 0)"
  3069 apply(simp add: rec_valu_def)
  3135 apply(simp add: rec_valu_def)
  3070 apply(rule_tac k = "Suc (Suc 0)" in prime_cn)
  3136 apply(rule_tac k = "Suc (Suc 0)" in prime_cn)
  3083   The definition of the universal function @{text "rec_F"}.
  3149   The definition of the universal function @{text "rec_F"}.
  3084   *}
  3150   *}
  3085 definition rec_F :: "recf"
  3151 definition rec_F :: "recf"
  3086   where
  3152   where
  3087   "rec_F = Cn (Suc (Suc 0)) rec_valu [Cn (Suc (Suc 0)) rec_right [Cn (Suc (Suc 0))
  3153   "rec_F = Cn (Suc (Suc 0)) rec_valu [Cn (Suc (Suc 0)) rec_right [Cn (Suc (Suc 0))
  3088    rec_conf ([id (Suc (Suc 0)) 0, id (Suc (Suc 0)) (Suc 0), rec_halt])]]"
  3154  rec_conf ([id (Suc (Suc 0)) 0, id (Suc (Suc 0)) (Suc 0), rec_halt])]]"
  3089 
  3155 
  3090 lemma get_fstn_args_nth: 
  3156 lemma get_fstn_args_nth: 
  3091   "k < n \<Longrightarrow> (get_fstn_args m n ! k) = id m (k)"
  3157   "k < n \<Longrightarrow> (get_fstn_args m n ! k) = id m (k)"
  3092 apply(induct n, simp)
  3158 apply(induct n, simp)
  3093 apply(case_tac "k = n", simp_all add: get_fstn_args.simps 
  3159 apply(case_tac "k = n", simp_all add: get_fstn_args.simps 
  3099   k < length ys\<rbrakk> \<Longrightarrow>
  3165   k < length ys\<rbrakk> \<Longrightarrow>
  3100   (get_fstn_args (length ys) (length ys) ! k) = 
  3166   (get_fstn_args (length ys) (length ys) ! k) = 
  3101                                   id (length ys) (k)"
  3167                                   id (length ys) (k)"
  3102 by(erule_tac get_fstn_args_nth)
  3168 by(erule_tac get_fstn_args_nth)
  3103 
  3169 
  3104 lemma terminates_halt_lemma: 
  3170 lemma terminate_halt_lemma: 
  3105   "\<lbrakk>rec_eval rec_nonstop ([m, r] @ [t]) = 0; 
  3171   "\<lbrakk>rec_exec rec_nonstop ([m, r] @ [t]) = 0; 
  3106      \<forall>i<t. 0 < rec_eval rec_nonstop ([m, r] @ [i])\<rbrakk> \<Longrightarrow> terminates rec_halt [m, r]"
  3172      \<forall>i<t. 0 < rec_exec rec_nonstop ([m, r] @ [i])\<rbrakk> \<Longrightarrow> terminate rec_halt [m, r]"
  3107 apply(simp add: rec_halt_def)
  3173 apply(simp add: rec_halt_def)
  3108 apply(rule_tac termi_mn, auto)
  3174 apply(rule_tac termi_mn, auto)
  3109 apply(rule_tac [!] primerec_terminates, auto)
  3175 apply(rule_tac [!] primerec_terminate, auto)
  3110 done
  3176 done
  3111 
  3177 
  3112 
  3178 
  3113 text {*
  3179 text {*
  3114   The correctness of @{text "rec_F"}, halt case.
  3180   The correctness of @{text "rec_F"}, halt case.
  3115   *}
  3181   *}
  3116 
  3182 
  3117 lemma F_lemma: "rec_eval rec_halt [m, r] = t \<Longrightarrow> rec_eval rec_F [m, r] = (valu (rght (conf m r t)))"
  3183 lemma F_lemma: "rec_exec rec_halt [m, r] = t \<Longrightarrow> rec_exec rec_F [m, r] = (valu (rght (conf m r t)))"
  3118 by(simp add: rec_F_def rec_eval.simps value_lemma right_lemma conf_lemma halt_lemma)
  3184 by(simp add: rec_F_def rec_exec.simps value_lemma right_lemma conf_lemma halt_lemma)
  3119 
  3185 
  3120 lemma terminates_F_lemma: "terminates rec_halt [m, r] \<Longrightarrow> terminates rec_F [m, r]"
  3186 lemma terminate_F_lemma: "terminate rec_halt [m, r] \<Longrightarrow> terminate rec_F [m, r]"
  3121 apply(simp add: rec_F_def)
  3187 apply(simp add: rec_F_def)
  3122 apply(rule_tac termi_cn, auto)
  3188 apply(rule_tac termi_cn, auto)
  3123 apply(rule_tac primerec_terminates, auto)
  3189 apply(rule_tac primerec_terminate, auto)
  3124 apply(rule_tac termi_cn, auto)
  3190 apply(rule_tac termi_cn, auto)
  3125 apply(rule_tac primerec_terminates, auto)
  3191 apply(rule_tac primerec_terminate, auto)
  3126 apply(rule_tac termi_cn, auto)
  3192 apply(rule_tac termi_cn, auto)
  3127 apply(rule_tac primerec_terminates, auto)
  3193 apply(rule_tac primerec_terminate, auto)
  3128 apply(rule_tac [!] termi_id, auto)
  3194 apply(rule_tac [!] termi_id, auto)
  3129 done
  3195 done
  3130 
  3196 
  3131 text {*
  3197 text {*
  3132   The correctness of @{text "rec_F"}, nonhalt case.
  3198   The correctness of @{text "rec_F"}, nonhalt case.
  3209 done
  3275 done
  3210 
  3276 
  3211 lemma Pi_gr_1[simp]: "Pi n > Suc 0"
  3277 lemma Pi_gr_1[simp]: "Pi n > Suc 0"
  3212 proof(induct n, auto simp: Pi.simps Np.simps)
  3278 proof(induct n, auto simp: Pi.simps Np.simps)
  3213   fix n
  3279   fix n
  3214   let ?setx = "{y. y \<le> Suc (Pi n!) \<and> Pi n < y \<and> prime y}"
  3280   let ?setx = "{y. y \<le> Suc (Pi n!) \<and> Pi n < y \<and> Prime y}"
  3215   have "finite ?setx" by auto
  3281   have "finite ?setx" by auto
  3216   moreover have "?setx \<noteq> {}"
  3282   moreover have "?setx \<noteq> {}"
  3217     using prime_ex[of "Pi n"]
  3283     using prime_ex[of "Pi n"]
  3218     apply(auto)
  3284     apply(auto)
  3219     done
  3285     done
  3220   ultimately show "Suc 0 < Min ?setx"
  3286   ultimately show "Suc 0 < Min ?setx"
  3221     apply(simp add: Min_gr_iff)
  3287     apply(simp add: Min_gr_iff)
  3222     apply(auto simp: prime_nat_def dvd_def)
  3288     apply(auto simp: Prime.simps)
  3223     done
  3289     done
  3224 qed
  3290 qed
  3225 
  3291 
  3226 lemma Pi_not_0[simp]: "Pi n > 0"
  3292 lemma Pi_not_0[simp]: "Pi n > 0"
  3227 using Pi_gr_1[of n]
  3293 using Pi_gr_1[of n]
  3246   "\<lbrakk>i < length nl; \<not> Suc 0 < godel_code nl\<rbrakk> \<Longrightarrow> nl ! i = 0"
  3312   "\<lbrakk>i < length nl; \<not> Suc 0 < godel_code nl\<rbrakk> \<Longrightarrow> nl ! i = 0"
  3247 using godel_code_great[of nl] godel_code_eq_1[of nl]
  3313 using godel_code_great[of nl] godel_code_eq_1[of nl]
  3248 apply(simp)
  3314 apply(simp)
  3249 done
  3315 done
  3250 
  3316 
       
  3317 lemma prime_coprime: "\<lbrakk>Prime x; Prime y; x\<noteq>y\<rbrakk> \<Longrightarrow> coprime x y"
       
  3318 proof(simp only: Prime.simps coprime_nat, auto simp: dvd_def,
       
  3319       rule_tac classical, simp)
       
  3320   fix d k ka
       
  3321   assume case_ka: "\<forall>u<d * ka. \<forall>v<d * ka. u * v \<noteq> d * ka" 
       
  3322     and case_k: "\<forall>u<d * k. \<forall>v<d * k. u * v \<noteq> d * k"
       
  3323     and h: "(0::nat) < d" "d \<noteq> Suc 0" "Suc 0 < d * ka" 
       
  3324            "ka \<noteq> k" "Suc 0 < d * k"
       
  3325   from h have "k > Suc 0 \<or> ka >Suc 0"
       
  3326     apply(auto)
       
  3327     apply(case_tac ka, simp, simp)
       
  3328     apply(case_tac k, simp, simp)
       
  3329     done
       
  3330   from this show "False"
       
  3331   proof(erule_tac disjE)
       
  3332     assume  "(Suc 0::nat) < k"
       
  3333     hence "k < d*k \<and> d < d*k"
       
  3334       using h
       
  3335       by(auto)
       
  3336     thus "?thesis"
       
  3337       using case_k
       
  3338       apply(erule_tac x = d in allE)
       
  3339       apply(simp)
       
  3340       apply(erule_tac x = k in allE)
       
  3341       apply(simp)
       
  3342       done
       
  3343   next
       
  3344     assume "(Suc 0::nat) < ka"
       
  3345     hence "ka < d * ka \<and> d < d*ka"
       
  3346       using h by auto
       
  3347     thus "?thesis"
       
  3348       using case_ka
       
  3349       apply(erule_tac x = d in allE)
       
  3350       apply(simp)
       
  3351       apply(erule_tac x = ka in allE)
       
  3352       apply(simp)
       
  3353       done
       
  3354   qed
       
  3355 qed
       
  3356 
  3251 lemma Pi_inc: "Pi (Suc i) > Pi i"
  3357 lemma Pi_inc: "Pi (Suc i) > Pi i"
  3252 proof(simp add: Pi.simps Np.simps)
  3358 proof(simp add: Pi.simps Np.simps)
  3253   let ?setx = "{y. y \<le> Suc (Pi i!) \<and> Pi i < y \<and> prime y}"
  3359   let ?setx = "{y. y \<le> Suc (Pi i!) \<and> Pi i < y \<and> Prime y}"
  3254   have "finite ?setx" by simp
  3360   have "finite ?setx" by simp
  3255   moreover have "?setx \<noteq> {}"
  3361   moreover have "?setx \<noteq> {}"
  3256     using prime_ex[of "Pi i"]
  3362     using prime_ex[of "Pi i"]
  3257     apply(auto)
  3363     apply(auto)
  3258     done
  3364     done
  3294 apply(simp)
  3400 apply(simp)
  3295 using Pi_inc_gr[of j i]
  3401 using Pi_inc_gr[of j i]
  3296 apply(simp)
  3402 apply(simp)
  3297 done
  3403 done
  3298 
  3404 
  3299 lemma [intro]: "prime (Suc (Suc 0))"
  3405 lemma [intro]: "Prime (Suc (Suc 0))"
  3300 apply(auto simp: prime_nat_def dvd_def)
  3406 apply(auto simp: Prime.simps)
  3301 apply(case_tac m, simp, case_tac k, simp, simp)
  3407 apply(case_tac u, simp, case_tac nat, simp, simp)
  3302 done
  3408 done
  3303 
  3409 
  3304 lemma Prime_Pi[intro]: "prime (Pi n)"
  3410 lemma Prime_Pi[intro]: "Prime (Pi n)"
  3305 proof(induct n, auto simp: Pi.simps Np.simps)
  3411 proof(induct n, auto simp: Pi.simps Np.simps)
  3306   fix n
  3412   fix n
  3307   let ?setx = "{y. y \<le> Suc (Pi n!) \<and> Pi n < y \<and> prime y}"
  3413   let ?setx = "{y. y \<le> Suc (Pi n!) \<and> Pi n < y \<and> Prime y}"
  3308   show "prime (Min ?setx)"
  3414   show "Prime (Min ?setx)"
  3309   proof -
  3415   proof -
  3310     have "finite ?setx" by simp
  3416     have "finite ?setx" by simp
  3311     moreover have "?setx \<noteq> {}" 
  3417     moreover have "?setx \<noteq> {}" 
  3312       using prime_ex[of "Pi n"]
  3418       using prime_ex[of "Pi n"]
  3313       apply(simp)
  3419       apply(simp)
  3319 qed
  3425 qed
  3320     
  3426     
  3321 lemma Pi_coprime: "i \<noteq> j \<Longrightarrow> coprime (Pi i) (Pi j)"
  3427 lemma Pi_coprime: "i \<noteq> j \<Longrightarrow> coprime (Pi i) (Pi j)"
  3322 using Prime_Pi[of i]
  3428 using Prime_Pi[of i]
  3323 using Prime_Pi[of j]
  3429 using Prime_Pi[of j]
  3324 apply(rule_tac primes_coprime_nat, simp_all add: Pi_notEq)
  3430 apply(rule_tac prime_coprime, simp_all add: Pi_notEq)
  3325 done
  3431 done
  3326 
  3432 
  3327 lemma Pi_power_coprime: "i \<noteq> j \<Longrightarrow> coprime ((Pi i)^m) ((Pi j)^n)"
  3433 lemma Pi_power_coprime: "i \<noteq> j \<Longrightarrow> coprime ((Pi i)^m) ((Pi j)^n)"
  3328 by(rule_tac coprime_exp2_nat, erule_tac Pi_coprime)
  3434 by(rule_tac coprime_exp2_nat, erule_tac Pi_coprime)
  3329 
  3435 
  3415   from g have 
  3521   from g have 
  3416     "coprime (Pi (Suc i)) (godel_code' (butlast ps) (Suc 0) *
  3522     "coprime (Pi (Suc i)) (godel_code' (butlast ps) (Suc 0) *
  3417                                         Pi (length ps)^(last ps)) "
  3523                                         Pi (length ps)^(last ps)) "
  3418   proof(rule_tac coprime_mult_nat, simp)
  3524   proof(rule_tac coprime_mult_nat, simp)
  3419     show "coprime (Pi (Suc i)) (Pi (length ps) ^ last ps)"
  3525     show "coprime (Pi (Suc i)) (Pi (length ps) ^ last ps)"
  3420       apply(rule_tac coprime_exp_nat, rule primes_coprime_nat, auto)
  3526       apply(rule_tac coprime_exp_nat, rule prime_coprime, auto)
  3421       using Pi_notEq[of "Suc i" "length ps"] h by simp
  3527       using Pi_notEq[of "Suc i" "length ps"] h by simp
  3422   qed
  3528   qed
  3423   from this and k show "coprime (Pi (Suc i)) (godel_code' ps (Suc 0))"
  3529   from this and k show "coprime (Pi (Suc i)) (godel_code' ps (Suc 0))"
  3424     by simp
  3530     by simp
  3425 qed
  3531 qed
  3999   The lemma relates the one step execution of TMs with the interpreter function @{text "rec_newconf"}.
  4105   The lemma relates the one step execution of TMs with the interpreter function @{text "rec_newconf"}.
  4000   *}
  4106   *}
  4001 lemma rec_t_eq_step: 
  4107 lemma rec_t_eq_step: 
  4002   "(\<lambda> (s, l, r). s \<le> length tp div 2) c \<Longrightarrow>
  4108   "(\<lambda> (s, l, r). s \<le> length tp div 2) c \<Longrightarrow>
  4003   trpl_code (step0 c tp) = 
  4109   trpl_code (step0 c tp) = 
  4004   rec_eval rec_newconf [code tp, trpl_code c]"
  4110   rec_exec rec_newconf [code tp, trpl_code c]"
  4005   apply(cases c, simp)
  4111   apply(cases c, simp)
  4006 proof(case_tac "fetch tp a (read ca)",
  4112 proof(case_tac "fetch tp a (read ca)",
  4007     simp add: newconf.simps trpl_code.simps step.simps)
  4113     simp add: newconf.simps trpl_code.simps step.simps)
  4008   fix a b ca aa ba
  4114   fix a b ca aa ba
  4009   assume h: "(a::nat) \<le> length tp div 2" 
  4115   assume h: "(a::nat) \<le> length tp div 2" 
  4161   apply(simp)
  4267   apply(simp)
  4162   done
  4268   done
  4163 
  4269 
  4164 lemma [simp]:
  4270 lemma [simp]:
  4165  "trpl_code (steps0 (Suc 0, Bk\<up>l, <lm>) tp 0) = 
  4271  "trpl_code (steps0 (Suc 0, Bk\<up>l, <lm>) tp 0) = 
  4166     rec_eval rec_conf [code tp, bl2wc (<lm>), 0]"
  4272     rec_exec rec_conf [code tp, bl2wc (<lm>), 0]"
  4167 apply(simp add: steps.simps rec_eval.simps conf_lemma  conf.simps 
  4273 apply(simp add: steps.simps rec_exec.simps conf_lemma  conf.simps 
  4168                 inpt.simps trpl_code.simps bl2wc.simps)
  4274                 inpt.simps trpl_code.simps bl2wc.simps)
  4169 done
  4275 done
  4170 
  4276 
  4171 text {*
  4277 text {*
  4172   The following lemma relates the multi-step interpreter function @{text "rec_conf"}
  4278   The following lemma relates the multi-step interpreter function @{text "rec_conf"}
  4210 qed
  4316 qed
  4211 
  4317 
  4212 lemma rec_t_eq_steps:
  4318 lemma rec_t_eq_steps:
  4213   "tm_wf (tp,0) \<Longrightarrow>
  4319   "tm_wf (tp,0) \<Longrightarrow>
  4214   trpl_code (steps0 (Suc 0, Bk\<up>l, <lm>) tp stp) = 
  4320   trpl_code (steps0 (Suc 0, Bk\<up>l, <lm>) tp stp) = 
  4215   rec_eval rec_conf [code tp, bl2wc (<lm>), stp]"
  4321   rec_exec rec_conf [code tp, bl2wc (<lm>), stp]"
  4216 proof(induct stp)
  4322 proof(induct stp)
  4217   case 0 thus "?case" by(simp)
  4323   case 0 thus "?case" by(simp)
  4218 next
  4324 next
  4219   case (Suc n) thus "?case"
  4325   case (Suc n) thus "?case"
  4220   proof -
  4326   proof -
  4221     assume ind: 
  4327     assume ind: 
  4222       "tm_wf (tp,0) \<Longrightarrow> trpl_code (steps0 (Suc 0, Bk\<up> l, <lm>) tp n) 
  4328       "tm_wf (tp,0) \<Longrightarrow> trpl_code (steps0 (Suc 0, Bk\<up> l, <lm>) tp n) 
  4223       = rec_eval rec_conf [code tp, bl2wc (<lm>), n]"
  4329       = rec_exec rec_conf [code tp, bl2wc (<lm>), n]"
  4224       and h: "tm_wf (tp, 0)"
  4330       and h: "tm_wf (tp, 0)"
  4225     show 
  4331     show 
  4226       "trpl_code (steps0 (Suc 0, Bk\<up> l, <lm>) tp (Suc n)) =
  4332       "trpl_code (steps0 (Suc 0, Bk\<up> l, <lm>) tp (Suc n)) =
  4227       rec_eval rec_conf [code tp, bl2wc (<lm>), Suc n]"
  4333       rec_exec rec_conf [code tp, bl2wc (<lm>), Suc n]"
  4228     proof(case_tac "steps0 (Suc 0, Bk\<up> l, <lm>) tp  n", 
  4334     proof(case_tac "steps0 (Suc 0, Bk\<up> l, <lm>) tp  n", 
  4229         simp only: step_red conf_lemma conf.simps)
  4335         simp only: step_red conf_lemma conf.simps)
  4230       fix a b c
  4336       fix a b c
  4231       assume g: "steps0 (Suc 0, Bk\<up> l, <lm>) tp n = (a, b, c) "
  4337       assume g: "steps0 (Suc 0, Bk\<up> l, <lm>) tp n = (a, b, c) "
  4232       hence "conf (code tp) (bl2wc (<lm>)) n= trpl_code (a, b, c)"
  4338       hence "conf (code tp) (bl2wc (<lm>)) n= trpl_code (a, b, c)"
  4233         using ind h
  4339         using ind h
  4234         apply(simp add: conf_lemma)
  4340         apply(simp add: conf_lemma)
  4235         done
  4341         done
  4236       moreover hence 
  4342       moreover hence 
  4237         "trpl_code (step0 (a, b, c) tp) = 
  4343         "trpl_code (step0 (a, b, c) tp) = 
  4238         rec_eval rec_newconf [code tp, trpl_code (a, b, c)]"
  4344         rec_exec rec_newconf [code tp, trpl_code (a, b, c)]"
  4239         apply(rule_tac rec_t_eq_step)
  4345         apply(rule_tac rec_t_eq_step)
  4240         using h g
  4346         using h g
  4241         apply(simp add: state_in_range)
  4347         apply(simp add: state_in_range)
  4242         done
  4348         done
  4243       ultimately show 
  4349       ultimately show 
  4291 
  4397 
  4292 lemma nonstop_t_eq: 
  4398 lemma nonstop_t_eq: 
  4293   "\<lbrakk>steps0 (Suc 0, Bk\<up>l, <lm>) tp stp = (0, Bk\<up> m, Oc\<up> rs @ Bk\<up> n); 
  4399   "\<lbrakk>steps0 (Suc 0, Bk\<up>l, <lm>) tp stp = (0, Bk\<up> m, Oc\<up> rs @ Bk\<up> n); 
  4294    tm_wf (tp, 0); 
  4400    tm_wf (tp, 0); 
  4295   rs > 0\<rbrakk> 
  4401   rs > 0\<rbrakk> 
  4296   \<Longrightarrow> rec_eval rec_nonstop [code tp, bl2wc (<lm>), stp] = 0"
  4402   \<Longrightarrow> rec_exec rec_nonstop [code tp, bl2wc (<lm>), stp] = 0"
  4297 proof(simp add: nonstop_lemma nonstop.simps nstd.simps)
  4403 proof(simp add: nonstop_lemma nonstop.simps nstd.simps)
  4298   assume h: "steps0 (Suc 0, Bk\<up>l, <lm>) tp stp = (0, Bk\<up> m, Oc\<up> rs @ Bk\<up> n)"
  4404   assume h: "steps0 (Suc 0, Bk\<up>l, <lm>) tp stp = (0, Bk\<up> m, Oc\<up> rs @ Bk\<up> n)"
  4299   and tc_t: "tm_wf (tp, 0)" "rs > 0"
  4405   and tc_t: "tm_wf (tp, 0)" "rs > 0"
  4300   have g: "rec_eval rec_conf [code tp,  bl2wc (<lm>), stp] =
  4406   have g: "rec_exec rec_conf [code tp,  bl2wc (<lm>), stp] =
  4301                                         trpl_code (0, Bk\<up> m, Oc\<up> rs@Bk\<up> n)"
  4407                                         trpl_code (0, Bk\<up> m, Oc\<up> rs@Bk\<up> n)"
  4302     using rec_t_eq_steps[of tp l lm stp] tc_t h
  4408     using rec_t_eq_steps[of tp l lm stp] tc_t h
  4303     by(simp)
  4409     by(simp)
  4304   thus "\<not> NSTD (conf (code tp) (bl2wc (<lm>)) stp)" 
  4410   thus "\<not> NSTD (conf (code tp) (bl2wc (<lm>)) stp)" 
  4305   proof(auto simp: NSTD.simps)
  4411   proof(auto simp: NSTD.simps)
  4485 text {*
  4591 text {*
  4486   The correntess of @{text "rec_F"} which relates the interpreter function @{text "rec_F"} with the
  4592   The correntess of @{text "rec_F"} which relates the interpreter function @{text "rec_F"} with the
  4487   execution of of TMs.
  4593   execution of of TMs.
  4488   *}
  4594   *}
  4489 
  4595 
  4490 lemma terminates_halt: 
  4596 lemma terminate_halt: 
  4491   "\<lbrakk>steps0 (Suc 0, Bk\<up>l, <lm>) tp stp = (0, Bk\<up>m, Oc\<up>rs@Bk\<up>n); 
  4597   "\<lbrakk>steps0 (Suc 0, Bk\<up>l, <lm>) tp stp = (0, Bk\<up>m, Oc\<up>rs@Bk\<up>n); 
  4492     tm_wf (tp,0); 0<rs\<rbrakk> \<Longrightarrow> terminates rec_halt [code tp, (bl2wc (<lm>))]"
  4598     tm_wf (tp,0); 0<rs\<rbrakk> \<Longrightarrow> terminate rec_halt [code tp, (bl2wc (<lm>))]"
  4493 apply(frule_tac halt_least_step, auto)
  4599 apply(frule_tac halt_least_step, auto)
  4494 thm terminates_halt_lemma
  4600 thm terminate_halt_lemma
  4495 apply(rule_tac t = stpa in terminates_halt_lemma)
  4601 apply(rule_tac t = stpa in terminate_halt_lemma)
  4496 apply(simp_all add: nonstop_lemma, auto)
  4602 apply(simp_all add: nonstop_lemma, auto)
  4497 done
  4603 done
  4498 
  4604 
  4499 lemma terminates_F: 
  4605 lemma terminate_F: 
  4500   "\<lbrakk>steps0 (Suc 0, Bk\<up>l, <lm>) tp stp = (0, Bk\<up>m, Oc\<up>rs@Bk\<up>n); 
  4606   "\<lbrakk>steps0 (Suc 0, Bk\<up>l, <lm>) tp stp = (0, Bk\<up>m, Oc\<up>rs@Bk\<up>n); 
  4501     tm_wf (tp,0); 0<rs\<rbrakk> \<Longrightarrow> terminates rec_F [code tp, (bl2wc (<lm>))]"
  4607     tm_wf (tp,0); 0<rs\<rbrakk> \<Longrightarrow> terminate rec_F [code tp, (bl2wc (<lm>))]"
  4502 apply(drule_tac terminates_halt, simp_all)
  4608 apply(drule_tac terminate_halt, simp_all)
  4503 apply(erule_tac terminates_F_lemma)
  4609 apply(erule_tac terminate_F_lemma)
  4504 done
  4610 done
  4505 
  4611 
  4506 lemma F_correct: 
  4612 lemma F_correct: 
  4507   "\<lbrakk>steps0 (Suc 0, Bk\<up>l, <lm>) tp stp = (0, Bk\<up>m, Oc\<up>rs@Bk\<up>n); 
  4613   "\<lbrakk>steps0 (Suc 0, Bk\<up>l, <lm>) tp stp = (0, Bk\<up>m, Oc\<up>rs@Bk\<up>n); 
  4508     tm_wf (tp,0); 0<rs\<rbrakk>
  4614     tm_wf (tp,0); 0<rs\<rbrakk>
  4509    \<Longrightarrow> rec_eval rec_F [code tp, (bl2wc (<lm>))] = (rs - Suc 0)"
  4615    \<Longrightarrow> rec_exec rec_F [code tp, (bl2wc (<lm>))] = (rs - Suc 0)"
  4510 apply(frule_tac halt_least_step, auto)
  4616 apply(frule_tac halt_least_step, auto)
  4511 apply(frule_tac  nonstop_t_eq, auto simp: nonstop_lemma)
  4617 apply(frule_tac  nonstop_t_eq, auto simp: nonstop_lemma)
  4512 using rec_t_eq_steps[of tp l lm stp]
  4618 using rec_t_eq_steps[of tp l lm stp]
  4513 apply(simp add: conf_lemma)
  4619 apply(simp add: conf_lemma)
  4514 proof -
  4620 proof -
  4521     "steps0 (Suc 0, Bk\<up> l, <lm>) tp stp = (0, Bk\<up> m, Oc\<up> rs @ Bk\<up> n)"
  4627     "steps0 (Suc 0, Bk\<up> l, <lm>) tp stp = (0, Bk\<up> m, Oc\<up> rs @ Bk\<up> n)"
  4522   hence g1: "conf (code tp) (bl2wc (<lm>)) stpa = trpl_code (0, Bk\<up> m, Oc\<up> rs @ Bk\<up>n)"
  4628   hence g1: "conf (code tp) (bl2wc (<lm>)) stpa = trpl_code (0, Bk\<up> m, Oc\<up> rs @ Bk\<up>n)"
  4523     using halt_state_keep[of "code tp" lm stpa stp]
  4629     using halt_state_keep[of "code tp" lm stpa stp]
  4524     by(simp)
  4630     by(simp)
  4525   moreover have g2:
  4631   moreover have g2:
  4526     "rec_eval rec_halt [code tp, (bl2wc (<lm>))] = stpa"
  4632     "rec_exec rec_halt [code tp, (bl2wc (<lm>))] = stpa"
  4527     using h
  4633     using h
  4528     by(auto simp: rec_eval.simps rec_halt_def nonstop_lemma intro!: Least_equality)
  4634     by(auto simp: rec_exec.simps rec_halt_def nonstop_lemma intro!: Least_equality)
  4529   show  
  4635   show  
  4530     "rec_eval rec_F [code tp, (bl2wc (<lm>))] = (rs - Suc 0)"
  4636     "rec_exec rec_F [code tp, (bl2wc (<lm>))] = (rs - Suc 0)"
  4531   proof -
  4637   proof -
  4532     have 
  4638     have 
  4533       "valu (rght (conf (code tp) (bl2wc (<lm>)) stpa)) = rs - Suc 0" 
  4639       "valu (rght (conf (code tp) (bl2wc (<lm>)) stpa)) = rs - Suc 0" 
  4534       using g1 
  4640       using g1 
  4535       apply(simp add: valu.simps trpl_code.simps 
  4641       apply(simp add: valu.simps trpl_code.simps 
  4536         bl2wc.simps  bl2nat_append lg_power)
  4642         bl2wc.simps  bl2nat_append lg_power)
  4537       done
  4643       done
  4538     thus "?thesis" 
  4644     thus "?thesis" 
  4539       by(simp add: rec_eval.simps F_lemma g2)
  4645       by(simp add: rec_exec.simps F_lemma g2)
  4540   qed
  4646   qed
  4541 qed
  4647 qed
  4542 
  4648 
  4543 end
  4649 end