thys/Recursive.thy
changeset 288 a9003e6d0463
parent 285 447b433b67fa
child 290 6e1c03614d36
equal deleted inserted replaced
287:d5a0e25c4742 288:a9003e6d0463
   163 
   163 
   164 definition addition_LE :: "(((nat \<times> nat list) \<times> nat \<times> nat) \<times> 
   164 definition addition_LE :: "(((nat \<times> nat list) \<times> nat \<times> nat) \<times> 
   165                           ((nat \<times> nat list) \<times> nat \<times> nat)) set"
   165                           ((nat \<times> nat list) \<times> nat \<times> nat)) set"
   166   where "addition_LE \<equiv> (inv_image lex_triple addition_measure)"
   166   where "addition_LE \<equiv> (inv_image lex_triple addition_measure)"
   167 
   167 
   168 lemma [simp]: "wf addition_LE"
   168 lemma wf_additon_LE[simp]: "wf addition_LE"
   169 by(auto simp: wf_inv_image addition_LE_def lex_triple_def
   169   by(auto simp: addition_LE_def lex_triple_def lex_pair_def)
   170              lex_pair_def)
       
   171 
   170 
   172 declare addition_inv.simps[simp del]
   171 declare addition_inv.simps[simp del]
   173 
   172 
   174 lemma addition_inv_init: 
   173 lemma addition_inv_init: 
   175   "\<lbrakk>m \<noteq> n; max m n < p; length lm > p; lm ! p = 0\<rbrakk> \<Longrightarrow>
   174   "\<lbrakk>m \<noteq> n; max m n < p; length lm > p; lm ! p = 0\<rbrakk> \<Longrightarrow>
   176                                    addition_inv (0, lm) m n p lm"
   175                                    addition_inv (0, lm) m n p lm"
   177 apply(simp add: addition_inv.simps Let_def)
   176 apply(simp add: addition_inv.simps Let_def)
   178 apply(rule_tac x = "lm ! m" in exI, simp)
   177 apply(rule_tac x = "lm ! m" in exI, simp)
   179 done
   178 done
   180 
   179 
   181 lemma [simp]: "abc_fetch 0 (addition m n p) = Some (Dec m 4)"
   180 lemma abs_fetch_0[simp]: "abc_fetch 0 (addition m n p) = Some (Dec m 4)"
   182 by(simp add: abc_fetch.simps addition.simps)
   181 by(simp add: abc_fetch.simps addition.simps)
   183 
   182 
   184 lemma [simp]: "abc_fetch (Suc 0) (addition m n p) = Some (Inc n)"
   183 lemma abs_fetch_1[simp]: "abc_fetch (Suc 0) (addition m n p) = Some (Inc n)"
   185 by(simp add: abc_fetch.simps addition.simps)
   184 by(simp add: abc_fetch.simps addition.simps)
   186 
   185 
   187 lemma [simp]: "abc_fetch 2 (addition m n p) = Some (Inc p)"
   186 lemma abs_fetch_2[simp]: "abc_fetch 2 (addition m n p) = Some (Inc p)"
   188 by(simp add: abc_fetch.simps addition.simps)
   187 by(simp add: abc_fetch.simps addition.simps)
   189 
   188 
   190 lemma [simp]: "abc_fetch 3 (addition m n p) = Some (Goto 0)"
   189 lemma abs_fetch_3[simp]: "abc_fetch 3 (addition m n p) = Some (Goto 0)"
   191 by(simp add: abc_fetch.simps addition.simps)
   190 by(simp add: abc_fetch.simps addition.simps)
   192 
   191 
   193 lemma [simp]: "abc_fetch 4 (addition m n p) = Some (Dec p 7)"
   192 lemma abs_fetch_4[simp]: "abc_fetch 4 (addition m n p) = Some (Dec p 7)"
   194 by(simp add: abc_fetch.simps addition.simps)
   193 by(simp add: abc_fetch.simps addition.simps)
   195 
   194 
   196 lemma [simp]: "abc_fetch 5 (addition m n p) = Some (Inc m)"
   195 lemma abs_fetch_5[simp]: "abc_fetch 5 (addition m n p) = Some (Inc m)"
   197 by(simp add: abc_fetch.simps addition.simps)
   196 by(simp add: abc_fetch.simps addition.simps)
   198 
   197 
   199 lemma [simp]: "abc_fetch 6 (addition m n p) = Some (Goto 4)"
   198 lemma abs_fetch_6[simp]: "abc_fetch 6 (addition m n p) = Some (Goto 4)"
   200 by(simp add: abc_fetch.simps addition.simps)
   199 by(simp add: abc_fetch.simps addition.simps)
   201 
   200 
   202 lemma [simp]:
   201 lemma exists_small_list_elem1[simp]:
   203   "\<lbrakk>m \<noteq> n; p < length lm; lm ! p = 0; m < p; n < p; x \<le> lm ! m; 0 < x\<rbrakk>
   202   "\<lbrakk>m \<noteq> n; p < length lm; lm ! p = 0; m < p; n < p; x \<le> lm ! m; 0 < x\<rbrakk>
   204  \<Longrightarrow> \<exists>xa<lm ! m. lm[m := x, n := lm ! n + lm ! m - x, 
   203  \<Longrightarrow> \<exists>xa<lm ! m. lm[m := x, n := lm ! n + lm ! m - x, 
   205                     p := lm ! m - x, m := x - Suc 0] =
   204                     p := lm ! m - x, m := x - Suc 0] =
   206                  lm[m := xa, n := lm ! n + lm ! m - Suc xa,
   205                  lm[m := xa, n := lm ! n + lm ! m - Suc xa,
   207                     p := lm ! m - Suc xa]"
   206                     p := lm ! m - Suc xa]"
   208 apply(case_tac x, simp, simp)
   207 apply(case_tac x, simp, simp)
   209 apply(rule_tac x = nat in exI, simp add: list_update_swap 
   208 apply(rule_tac x = nat in exI, simp add: list_update_swap 
   210                                          list_update_overwrite)
   209                                          list_update_overwrite)
   211 done
   210 done
   212 
   211 
   213 lemma [simp]:
   212 lemma exists_small_list_elem2[simp]:
   214   "\<lbrakk>m \<noteq> n; p < length lm; lm ! p = 0; m < p; n < p; x < lm ! m\<rbrakk>
   213   "\<lbrakk>m \<noteq> n; p < length lm; lm ! p = 0; m < p; n < p; x < lm ! m\<rbrakk>
   215    \<Longrightarrow> \<exists>xa<lm ! m. lm[m := x, n := lm ! n + lm ! m - Suc x,
   214    \<Longrightarrow> \<exists>xa<lm ! m. lm[m := x, n := lm ! n + lm ! m - Suc x,
   216                       p := lm ! m - Suc x, n := lm ! n + lm ! m - x]
   215                       p := lm ! m - Suc x, n := lm ! n + lm ! m - x]
   217                  = lm[m := xa, n := lm ! n + lm ! m - xa, 
   216                  = lm[m := xa, n := lm ! n + lm ! m - xa, 
   218                       p := lm ! m - Suc xa]"
   217                       p := lm ! m - Suc xa]"
   219 apply(rule_tac x = x in exI, 
   218 apply(rule_tac x = x in exI, 
   220       simp add: list_update_swap list_update_overwrite)
   219       simp add: list_update_swap list_update_overwrite)
   221 done
   220 done
   222 
   221 
   223 lemma [simp]: 
   222 lemma exists_small_list_elem3[simp]: 
   224   "\<lbrakk>m \<noteq> n; p < length lm; lm ! p = 0; m < p; n < p; x < lm ! m\<rbrakk>
   223   "\<lbrakk>m \<noteq> n; p < length lm; lm ! p = 0; m < p; n < p; x < lm ! m\<rbrakk>
   225    \<Longrightarrow> \<exists>xa<lm ! m. lm[m := x, n := lm ! n + lm ! m - x, 
   224    \<Longrightarrow> \<exists>xa<lm ! m. lm[m := x, n := lm ! n + lm ! m - x, 
   226                           p := lm ! m - Suc x, p := lm ! m - x]
   225                           p := lm ! m - Suc x, p := lm ! m - x]
   227                  = lm[m := xa, n := lm ! n + lm ! m - xa, 
   226                  = lm[m := xa, n := lm ! n + lm ! m - xa, 
   228                           p := lm ! m - xa]"
   227                           p := lm ! m - xa]"
   229 apply(rule_tac x = x in exI, simp add: list_update_overwrite)
   228 apply(rule_tac x = x in exI, simp add: list_update_overwrite)
   230 done
   229 done
   231 
   230 
   232 lemma [simp]: 
   231 lemma exists_small_list_elem4[simp]: 
   233   "\<lbrakk>m \<noteq> n; p < length lm; lm ! p = (0::nat); m < p; n < p; x < lm ! m\<rbrakk>
   232   "\<lbrakk>m \<noteq> n; p < length lm; lm ! p = (0::nat); m < p; n < p; x < lm ! m\<rbrakk>
   234   \<Longrightarrow> \<exists>xa\<le>lm ! m. lm[m := x, n := lm ! n + lm ! m - x,
   233   \<Longrightarrow> \<exists>xa\<le>lm ! m. lm[m := x, n := lm ! n + lm ! m - x,
   235                                    p := lm ! m - x] = 
   234                                    p := lm ! m - x] = 
   236                   lm[m := xa, n := lm ! n + lm ! m - xa, 
   235                   lm[m := xa, n := lm ! n + lm ! m - xa, 
   237                                    p := lm ! m - xa]"
   236                                    p := lm ! m - xa]"
   238 apply(rule_tac x = x in exI, simp)
   237 apply(rule_tac x = x in exI, simp)
   239 done
   238 done
   240 
   239 
   241 lemma [simp]: 
   240 lemma exists_small_list_elem5[simp]: 
   242   "\<lbrakk>m \<noteq> n; p < length lm; lm ! p = 0; m < p; n < p;
   241   "\<lbrakk>m \<noteq> n; p < length lm; lm ! p = 0; m < p; n < p;
   243     x \<le> lm ! m; lm ! m \<noteq> x\<rbrakk>
   242     x \<le> lm ! m; lm ! m \<noteq> x\<rbrakk>
   244   \<Longrightarrow> \<exists>xa<lm ! m. lm[m := x, n := lm ! n + lm ! m, 
   243   \<Longrightarrow> \<exists>xa<lm ! m. lm[m := x, n := lm ! n + lm ! m, 
   245                        p := lm ! m - x, p := lm ! m - Suc x] 
   244                        p := lm ! m - x, p := lm ! m - Suc x] 
   246                = lm[m := xa, n := lm ! n + lm ! m, 
   245                = lm[m := xa, n := lm ! n + lm ! m, 
   247                        p := lm ! m - Suc xa]"
   246                        p := lm ! m - Suc xa]"
   248 apply(rule_tac x = x in exI, simp add: list_update_overwrite)
   247 apply(rule_tac x = x in exI, simp add: list_update_overwrite)
   249 done
   248 done
   250 
   249 
   251 lemma [simp]:
   250 lemma exists_small_list_elem6[simp]:
   252   "\<lbrakk>m \<noteq> n; p < length lm; lm ! p = 0; m < p; n < p; x < lm ! m\<rbrakk>
   251   "\<lbrakk>m \<noteq> n; p < length lm; lm ! p = 0; m < p; n < p; x < lm ! m\<rbrakk>
   253   \<Longrightarrow> \<exists>xa<lm ! m. lm[m := x, n := lm ! n + lm ! m,
   252   \<Longrightarrow> \<exists>xa<lm ! m. lm[m := x, n := lm ! n + lm ! m,
   254                              p := lm ! m - Suc x, m := Suc x]
   253                              p := lm ! m - Suc x, m := Suc x]
   255                 = lm[m := Suc xa, n := lm ! n + lm ! m, 
   254                 = lm[m := Suc xa, n := lm ! n + lm ! m, 
   256                              p := lm ! m - Suc xa]"
   255                              p := lm ! m - Suc xa]"
   257 apply(rule_tac x = x in exI, 
   256 apply(rule_tac x = x in exI, 
   258      simp add: list_update_swap list_update_overwrite)
   257      simp add: list_update_swap list_update_overwrite)
   259 done
   258 done
   260 
   259 
   261 lemma [simp]: 
   260 lemma exists_small_list_elem7[simp]: 
   262   "\<lbrakk>m \<noteq> n; p < length lm; lm ! p = 0; m < p; n < p; x < lm ! m\<rbrakk>
   261   "\<lbrakk>m \<noteq> n; p < length lm; lm ! p = 0; m < p; n < p; x < lm ! m\<rbrakk>
   263   \<Longrightarrow> \<exists>xa\<le>lm ! m. lm[m := Suc x, n := lm ! n + lm ! m, 
   262   \<Longrightarrow> \<exists>xa\<le>lm ! m. lm[m := Suc x, n := lm ! n + lm ! m, 
   264                              p := lm ! m - Suc x] 
   263                              p := lm ! m - Suc x] 
   265                = lm[m := xa, n := lm ! n + lm ! m, p := lm ! m - xa]"
   264                = lm[m := xa, n := lm ! n + lm ! m, p := lm ! m - xa]"
   266 apply(rule_tac x = "Suc x" in exI, simp)
   265 apply(rule_tac x = "Suc x" in exI, simp)
   309 
   308 
   310 lemma length_addition[simp]: "length (addition a b c) = 7"
   309 lemma length_addition[simp]: "length (addition a b c) = 7"
   311 by(auto simp: addition.simps)
   310 by(auto simp: addition.simps)
   312 
   311 
   313 lemma addition_correct:
   312 lemma addition_correct:
   314   "\<lbrakk>m \<noteq> n; max m n < p; length lm > p; lm ! p = 0\<rbrakk>
   313   assumes "m \<noteq> n" "max m n < p" "length lm > p" "lm ! p = 0"
   315    \<Longrightarrow> {\<lambda> a. a = lm} (addition m n p) {\<lambda> nl. addition_inv (7, nl) m n p lm}"
   314   shows "{\<lambda> a. a = lm} (addition m n p) {\<lambda> nl. addition_inv (7, nl) m n p lm}"
   316 using assms
   315 using assms
   317 proof(rule_tac abc_Hoare_haltI, simp)
   316 proof(rule_tac abc_Hoare_haltI, simp)
   318   fix lma
   317   fix lma
   319   assume "m \<noteq> n" "m < p \<and> n < p" "p < length lm" "lm ! p = 0"
   318   assume "m \<noteq> n" "m < p \<and> n < p" "p < length lm" "lm ! p = 0"
   320   then have "\<exists> stp. (\<lambda> (as, lm'). as = 7 \<and> addition_inv (as, lm') m n p lm) 
   319   then have "\<exists> stp. (\<lambda> (as, lm'). as = 7 \<and> addition_inv (as, lm') m n p lm) 
   478 apply(simp add: abc_steps_l.simps mv_box_inv.simps)
   477 apply(simp add: abc_steps_l.simps mv_box_inv.simps)
   479 apply(rule_tac x = "initlm ! m" in exI, 
   478 apply(rule_tac x = "initlm ! m" in exI, 
   480       rule_tac x = "initlm ! n" in exI, simp)
   479       rule_tac x = "initlm ! n" in exI, simp)
   481 done
   480 done
   482 
   481 
   483 lemma [simp]: "abc_fetch 0 (mv_box m n) = Some (Dec m 3)"
   482 lemma abc_fetch_0[simp]: "abc_fetch 0 (mv_box m n) = Some (Dec m 3)"
   484 apply(simp add: mv_box.simps abc_fetch.simps)
   483 apply(simp add: mv_box.simps abc_fetch.simps)
   485 done
   484 done
   486 
   485 
   487 lemma [simp]: "abc_fetch (Suc 0) (mv_box m n) =
   486 lemma abc_fetch_1[simp]: "abc_fetch (Suc 0) (mv_box m n) =
   488                Some (Inc n)"
   487                Some (Inc n)"
   489 apply(simp add: mv_box.simps abc_fetch.simps)
   488 apply(simp add: mv_box.simps abc_fetch.simps)
   490 done
   489 done
   491 
   490 
   492 lemma [simp]: "abc_fetch 2 (mv_box m n) = Some (Goto 0)"
   491 lemma abc_fetch_2[simp]: "abc_fetch 2 (mv_box m n) = Some (Goto 0)"
   493 apply(simp add: mv_box.simps abc_fetch.simps)
   492 apply(simp add: mv_box.simps abc_fetch.simps)
   494 done
   493 done
   495 
   494 
   496 lemma [simp]: "abc_fetch 3 (mv_box m n) = None"
   495 lemma abc_fetch_3[simp]: "abc_fetch 3 (mv_box m n) = None"
   497 apply(simp add: mv_box.simps abc_fetch.simps)
   496 apply(simp add: mv_box.simps abc_fetch.simps)
   498 done
   497 done
   499 
   498 
   500 lemma replicate_Suc_iff_anywhere: "x # x\<up>b @ ys = x\<up>(Suc b) @ ys"
   499 lemma replicate_Suc_iff_anywhere: "x # x\<up>b @ ys = x\<up>(Suc b) @ ys"
   501 by simp
   500 by simp
   502 
   501 
   503 lemma [simp]: 
   502 lemma exists_smaller_in_list0[simp]: 
   504   "\<lbrakk>m \<noteq> n; m < length initlm; n < length initlm;
   503   "\<lbrakk>m \<noteq> n; m < length initlm; n < length initlm;
   505     k + l = initlm ! m + initlm ! n; k \<le> initlm ! m; 0 < k\<rbrakk>
   504     k + l = initlm ! m + initlm ! n; k \<le> initlm ! m; 0 < k\<rbrakk>
   506  \<Longrightarrow> \<exists>ka la. initlm[m := k, n := l, m := k - Suc 0] = 
   505  \<Longrightarrow> \<exists>ka la. initlm[m := k, n := l, m := k - Suc 0] = 
   507      initlm[m := ka, n := la] \<and>
   506      initlm[m := ka, n := la] \<and>
   508      Suc (ka + la) = initlm ! m + initlm ! n \<and> 
   507      Suc (ka + la) = initlm ! m + initlm ! n \<and> 
   515 apply(simp add: list_update_overwrite )
   514 apply(simp add: list_update_overwrite )
   516 apply(simp add: list_update_swap)
   515 apply(simp add: list_update_swap)
   517 apply(simp add: list_update_swap)
   516 apply(simp add: list_update_swap)
   518 done
   517 done
   519 
   518 
   520 lemma [simp]:
   519 lemma exists_smaller_in_list1[simp]:
   521   "\<lbrakk>m \<noteq> n; m < length initlm; n < length initlm; 
   520   "\<lbrakk>m \<noteq> n; m < length initlm; n < length initlm; 
   522     Suc (k + l) = initlm ! m + initlm ! n;
   521     Suc (k + l) = initlm ! m + initlm ! n;
   523     k < initlm ! m\<rbrakk>
   522     k < initlm ! m\<rbrakk>
   524     \<Longrightarrow> \<exists>ka la. initlm[m := k, n := l, n := Suc l] = 
   523     \<Longrightarrow> \<exists>ka la. initlm[m := k, n := l, n := Suc l] = 
   525                 initlm[m := ka, n := la] \<and> 
   524                 initlm[m := ka, n := la] \<and> 
   526                 ka + la = initlm ! m + initlm ! n \<and> 
   525                 ka + la = initlm ! m + initlm ! n \<and> 
   527                 ka \<le> initlm ! m"
   526                 ka \<le> initlm ! m"
   528 apply(rule_tac x = k in exI, rule_tac x = "Suc l" in exI, auto)
   527 apply(rule_tac x = k in exI, rule_tac x = "Suc l" in exI, auto)
   529 done
   528 done
   530 
   529 
   531 lemma [simp]: 
   530 lemma abc_steps_prop[simp]: 
   532   "\<lbrakk>length initlm > max m n; m \<noteq> n\<rbrakk> \<Longrightarrow> 
   531   "\<lbrakk>length initlm > max m n; m \<noteq> n\<rbrakk> \<Longrightarrow> 
   533    \<forall>na. \<not> (\<lambda>(as, lm) m. as = 3) 
   532    \<forall>na. \<not> (\<lambda>(as, lm) m. as = 3) 
   534     (abc_steps_l (0, initlm) (mv_box m n) na) m \<and> 
   533     (abc_steps_l (0, initlm) (mv_box m n) na) m \<and> 
   535   mv_box_inv (abc_steps_l (0, initlm) 
   534   mv_box_inv (abc_steps_l (0, initlm) 
   536            (mv_box m n) na) m n initlm \<longrightarrow>
   535            (mv_box m n) na) m n initlm \<longrightarrow>
   596 apply(rule_tac x = stp in exI, simp)
   595 apply(rule_tac x = stp in exI, simp)
   597 done
   596 done
   598 
   597 
   599 declare list_update.simps(2)[simp del]
   598 declare list_update.simps(2)[simp del]
   600 
   599 
   601 lemma [simp]:
   600 lemma zero_case_rec_exec[simp]:
   602   "\<lbrakk>length xs < gf; gf \<le> ft; n < length gs\<rbrakk>
   601   "\<lbrakk>length xs < gf; gf \<le> ft; n < length gs\<rbrakk>
   603   \<Longrightarrow> (rec_exec (gs ! n) xs # 0 \<up> (ft - Suc (length xs)) @ map (\<lambda>i. rec_exec i xs) (take n gs) @ 0 \<up> (length gs - n) @ 0 # 0 \<up> length xs @ anything)
   602   \<Longrightarrow> (rec_exec (gs ! n) xs # 0 \<up> (ft - Suc (length xs)) @ map (\<lambda>i. rec_exec i xs) (take n gs) @ 0 \<up> (length gs - n) @ 0 # 0 \<up> length xs @ anything)
   604   [ft + n - length xs := rec_exec (gs ! n) xs, 0 := 0] =
   603   [ft + n - length xs := rec_exec (gs ! n) xs, 0 := 0] =
   605   0 \<up> (ft - length xs) @ map (\<lambda>i. rec_exec i xs) (take n gs) @ rec_exec (gs ! n) xs # 0 \<up> (length gs - Suc n) @ 0 # 0 \<up> length xs @ anything"
   604   0 \<up> (ft - length xs) @ map (\<lambda>i. rec_exec i xs) (take n gs) @ rec_exec (gs ! n) xs # 0 \<up> (length gs - Suc n) @ 0 # 0 \<up> length xs @ anything"
   606 using list_update_append[of "rec_exec (gs ! n) xs # 0 \<up> (ft - Suc (length xs)) @ map (\<lambda>i. rec_exec i xs) (take n gs)"
   605 using list_update_append[of "rec_exec (gs ! n) xs # 0 \<up> (ft - Suc (length xs)) @ map (\<lambda>i. rec_exec i xs) (take n gs)"
   674           using a g_newcond h x
   673           using a g_newcond h x
   675           by(erule_tac x = "gs!n" in ballE, simp_all)
   674           by(erule_tac x = "gs!n" in ballE, simp_all)
   676         hence c: "length xs = ga"
   675         hence c: "length xs = ga"
   677           using a param_pattern by metis  
   676           using a param_pattern by metis  
   678         have d: "gf > ga" using footprint_ge a by simp
   677         have d: "gf > ga" using footprint_ge a by simp
   679         have e: "ft \<ge> gf" using ft a h
   678         have e: "ft \<ge> gf"
   680           by(simp,  rule_tac min_max.le_supI2, rule_tac Max_ge, simp, 
   679           using ft a h Max_ge image_eqI
       
   680           by(simp, rule_tac max.coboundedI2, rule_tac Max_ge, simp, 
   681             rule_tac insertI2,  
   681             rule_tac insertI2,  
   682             rule_tac f = "(\<lambda>(aprog, p, n). n)" and x = "rec_ci (gs!n)" in image_eqI, simp, 
   682             rule_tac f = "(\<lambda>(aprog, p, n). n)" and x = "rec_ci (gs!n)" in image_eqI, simp, 
   683             rule_tac x = "gs!n" in image_eqI, simp, simp)
   683             rule_tac x = "gs!n" in image_eqI, simp, simp)
   684         show "{\<lambda>nl. nl = xs @ 0 \<up> (ft - length xs) @ 
   684         show "{\<lambda>nl. nl = xs @ 0 \<up> (ft - length xs) @ 
   685           map (\<lambda>i. rec_exec i xs) (take n gs) @ 0 \<up> (length gs - n) @ 0 \<up> Suc (length xs) @ anything} gp [+] mv_box ga (ft + n)
   685           map (\<lambda>i. rec_exec i xs) (take n gs) @ 0 \<up> (length gs - n) @ 0 \<up> Suc (length xs) @ anything} gp [+] mv_box ga (ft + n)
   778 by(induct n, auto simp: mv_boxes.simps)
   778 by(induct n, auto simp: mv_boxes.simps)
   779 
   779 
   780 lemma exp_suc: "a\<up>Suc b = a\<up>b @ [a]"
   780 lemma exp_suc: "a\<up>Suc b = a\<up>b @ [a]"
   781 by(simp add: exp_ind del: replicate.simps)
   781 by(simp add: exp_ind del: replicate.simps)
   782 
   782 
   783 lemma [simp]: 
   783 lemma last_0[simp]: 
   784   "\<lbrakk>Suc n \<le> ba - aa;  length lm2 = Suc n;
   784   "\<lbrakk>Suc n \<le> ba - aa;  length lm2 = Suc n;
   785     length lm3 = ba - Suc (aa + n)\<rbrakk>
   785     length lm3 = ba - Suc (aa + n)\<rbrakk>
   786   \<Longrightarrow> (last lm2 # lm3 @ butlast lm2 @ 0 # lm4) ! (ba - aa) = (0::nat)"
   786   \<Longrightarrow> (last lm2 # lm3 @ butlast lm2 @ 0 # lm4) ! (ba - aa) = (0::nat)"
   787 proof -
   787 proof -
   788   assume h: "Suc n \<le> ba - aa"
   788   assume h: "Suc n \<le> ba - aa"
   794     apply(simp, insert g)
   794     apply(simp, insert g)
   795     apply(simp add: nth_append)
   795     apply(simp add: nth_append)
   796     done
   796     done
   797 qed
   797 qed
   798 
   798 
   799 lemma [simp]: "length lm1 = aa \<Longrightarrow>
   799 lemma butlast_last[simp]: "length lm1 = aa \<Longrightarrow>
   800       (lm1 @ 0\<up>n @ last lm2 # lm3 @ butlast lm2 @ 0 # lm4) ! (aa + n) = last lm2"
   800       (lm1 @ 0\<up>n @ last lm2 # lm3 @ butlast lm2 @ 0 # lm4) ! (aa + n) = last lm2"
   801 apply(simp add: nth_append)
   801 apply(simp add: nth_append)
   802 done
   802 done
   803 
   803 
   804 lemma [simp]: "\<lbrakk>Suc n \<le> ba - aa; aa < ba\<rbrakk> \<Longrightarrow> 
   804 lemma arith_as_simp[simp]: "\<lbrakk>Suc n \<le> ba - aa; aa < ba\<rbrakk> \<Longrightarrow> 
   805                     (ba < Suc (aa + (ba - Suc (aa + n) + n))) = False"
   805                     (ba < Suc (aa + (ba - Suc (aa + n) + n))) = False"
   806 apply arith
   806 apply arith
   807 done
   807 done
   808 
   808 
   809 lemma [simp]: "\<lbrakk>Suc n \<le> ba - aa; aa < ba; length lm1 = aa; 
   809 lemma butlast_elem[simp]: "\<lbrakk>Suc n \<le> ba - aa; aa < ba; length lm1 = aa; 
   810        length lm2 = Suc n; length lm3 = ba - Suc (aa + n)\<rbrakk>
   810        length lm2 = Suc n; length lm3 = ba - Suc (aa + n)\<rbrakk>
   811      \<Longrightarrow> (lm1 @ 0\<up>n @ last lm2 # lm3 @ butlast lm2 @ 0 # lm4) ! (ba + n) = 0"
   811      \<Longrightarrow> (lm1 @ 0\<up>n @ last lm2 # lm3 @ butlast lm2 @ 0 # lm4) ! (ba + n) = 0"
   812 using nth_append[of "lm1 @ (0\<Colon>'a)\<up>n @ last lm2 # lm3 @ butlast lm2" 
   812 using nth_append[of "lm1 @ (0::'a)\<up>n @ last lm2 # lm3 @ butlast lm2" 
   813                      "(0\<Colon>'a) # lm4" "ba + n"]
   813                      "(0::'a) # lm4" "ba + n"]
   814 apply(simp)
   814 apply(simp)
   815 done
   815 done
   816 
   816 
   817 lemma [simp]: 
   817 lemma update_butlast_eq0[simp]: 
   818  "\<lbrakk>Suc n \<le> ba - aa; aa < ba; length lm1 = aa; length lm2 = Suc n;
   818  "\<lbrakk>Suc n \<le> ba - aa; aa < ba; length lm1 = aa; length lm2 = Suc n;
   819                  length lm3 = ba - Suc (aa + n)\<rbrakk>
   819                  length lm3 = ba - Suc (aa + n)\<rbrakk>
   820   \<Longrightarrow> (lm1 @ 0\<up>n @ last lm2 # lm3 @ butlast lm2 @ (0::nat) # lm4)
   820   \<Longrightarrow> (lm1 @ 0\<up>n @ last lm2 # lm3 @ butlast lm2 @ (0::nat) # lm4)
   821   [ba + n := last lm2, aa + n := 0] = 
   821   [ba + n := last lm2, aa + n := 0] = 
   822   lm1 @ 0 # 0\<up>n @ lm3 @ lm2 @ lm4"
   822   lm1 @ 0 # 0\<up>n @ lm3 @ lm2 @ lm4"
   823 using list_update_append[of "lm1 @ 0\<up>n @ last lm2 # lm3 @ butlast lm2" "0 # lm4" 
   823 using list_update_append[of "lm1 @ 0\<up>n @ last lm2 # lm3 @ butlast lm2" "0 # lm4" 
   824                             "ba + n" "last lm2"]
   824                             "ba + n" "last lm2"]
   825 apply(simp)
   825 apply(simp add: list_update_append list_update.simps(2-) replicate_Suc_iff_anywhere exp_suc
   826 apply(simp add: list_update_append list_update.simps replicate_Suc_iff_anywhere exp_suc
   826            del: replicate_Suc)
   827   del: replicate_Suc)
       
   828 apply(case_tac lm2, simp, simp)
   827 apply(case_tac lm2, simp, simp)
   829 done
   828 done
   830 
   829 
   831 lemma [simp]:
   830 lemma update_butlast_eq1[simp]:
   832   "\<lbrakk>Suc (length lm1 + n) \<le> ba; length lm2 = Suc n; length lm3 = ba - Suc (length lm1 + n); 
   831   "\<lbrakk>Suc (length lm1 + n) \<le> ba; length lm2 = Suc n; length lm3 = ba - Suc (length lm1 + n); 
   833   \<not> ba - Suc (length lm1) < ba - Suc (length lm1 + n); \<not> ba + n - length lm1 < n\<rbrakk>
   832   \<not> ba - Suc (length lm1) < ba - Suc (length lm1 + n); \<not> ba + n - length lm1 < n\<rbrakk>
   834     \<Longrightarrow> (0::nat) \<up> n @ (last lm2 # lm3 @ butlast lm2 @ 0 # lm4)[ba - length lm1 := last lm2, 0 := 0] =
   833     \<Longrightarrow> (0::nat) \<up> n @ (last lm2 # lm3 @ butlast lm2 @ 0 # lm4)[ba - length lm1 := last lm2, 0 := 0] =
   835   0 # 0 \<up> n @ lm3 @ lm2 @ lm4"
   834   0 # 0 \<up> n @ lm3 @ lm2 @ lm4"
   836 apply(subgoal_tac "ba - length lm1 = Suc n + length lm3", simp add: list_update.simps list_update_append)
   835 apply(subgoal_tac "ba - length lm1 = Suc n + length lm3", simp add: list_update.simps(2-) list_update_append)
   837 apply(simp add: replicate_Suc_iff_anywhere exp_suc del: replicate_Suc)
   836 apply(simp add: replicate_Suc_iff_anywhere exp_suc del: replicate_Suc)
   838 apply(case_tac lm2, simp, simp)
   837 apply(case_tac lm2, simp, simp)
   839 apply(auto)
   838 apply(auto)
   840 done
   839 done
   841 
   840 
   890  qed
   889  qed
   891  thus "?case"
   890  thus "?case"
   892    by(simp add: mv_boxes.simps)
   891    by(simp add: mv_boxes.simps)
   893 qed
   892 qed
   894     
   893     
   895 lemma [simp]:
   894 lemma update_butlast_eq2[simp]:
   896   "\<lbrakk>Suc n \<le> aa - length lm1; length lm1 < aa; 
   895   "\<lbrakk>Suc n \<le> aa - length lm1; length lm1 < aa; 
   897   length lm2 = aa - Suc (length lm1 + n); 
   896   length lm2 = aa - Suc (length lm1 + n); 
   898   length lm3 = Suc n; 
   897   length lm3 = Suc n; 
   899   \<not> aa - Suc (length lm1) < aa - Suc (length lm1 + n);
   898   \<not> aa - Suc (length lm1) < aa - Suc (length lm1 + n);
   900   \<not> aa + n - length lm1 < n\<rbrakk>
   899   \<not> aa + n - length lm1 < n\<rbrakk>
   978     by(rule_tac mv_boxes_correct, auto)
   977     by(rule_tac mv_boxes_correct, auto)
   979   thus "?thesis"
   978   thus "?thesis"
   980     by(simp add: replicate_merge_anywhere)
   979     by(simp add: replicate_merge_anywhere)
   981 qed
   980 qed
   982 
   981 
   983 lemma [intro]: 
   982 lemma length_le_max_insert_rec_ci[intro]: 
   984   "length gs \<le> ffp \<Longrightarrow> length gs \<le> max (Suc (length xs)) (Max (insert ffp ((\<lambda>(aprog, p, n). n) ` rec_ci ` set gs)))"
   983   "length gs \<le> ffp \<Longrightarrow> length gs \<le> max x1 (Max (insert ffp (x2 ` x3 ` set gs)))"
   985  apply(rule_tac min_max.le_supI2)
   984  apply(rule_tac max.coboundedI2)
   986  apply(simp add: Max_ge_iff)
   985  apply(simp add: Max_ge_iff)
   987 done
   986 done
   988 
   987 
   989 lemma restore_new_paras:
   988 lemma restore_new_paras:
   990   "ffp \<ge> length gs 
   989   "ffp \<ge> length gs 
  1004   ultimately show "?thesis"
  1003   ultimately show "?thesis"
  1005     using j
  1004     using j
  1006     by(simp add: replicate_merge_anywhere le_add_diff_inverse)
  1005     by(simp add: replicate_merge_anywhere le_add_diff_inverse)
  1007 qed
  1006 qed
  1008    
  1007    
  1009 lemma [intro]: "ffp \<le> max (Suc (length xs)) (Max (insert ffp ((\<lambda>(aprog, p, n). n) ` rec_ci ` set gs)))"
  1008 lemma le_max_insert[intro]: "ffp \<le> max x0 (Max (insert ffp (x1 ` x2 ` set gs)))"
  1010 apply(rule_tac min_max.le_supI2)
  1009   by (rule max.coboundedI2) auto
  1011 apply(rule_tac Max_ge, auto)
       
  1012 done
       
  1013 
  1010 
  1014 declare max_less_iff_conj[simp del]
  1011 declare max_less_iff_conj[simp del]
  1015 
  1012 
  1016 lemma save_rs:
  1013 lemma save_rs:
  1017   "\<lbrakk>far = length gs;
  1014   "\<lbrakk>far = length gs;
  1044     by(simp add: list_update_append list_update.simps replicate_Suc_iff_anywhere del: replicate_Suc)
  1041     by(simp add: list_update_append list_update.simps replicate_Suc_iff_anywhere del: replicate_Suc)
  1045   ultimately show "?thesis"
  1042   ultimately show "?thesis"
  1046     by(simp)
  1043     by(simp)
  1047 qed
  1044 qed
  1048 
  1045 
  1049 lemma [simp]: "length (empty_boxes n) = 2*n"
  1046 lemma length_empty_boxes[simp]: "length (empty_boxes n) = 2*n"
  1050 apply(induct n, simp, simp)
  1047 apply(induct n, simp, simp)
  1051 done
  1048 done
  1052 
  1049 
  1053 lemma empty_one_box_correct:
  1050 lemma empty_one_box_correct:
  1054   "{\<lambda>nl. nl = 0 \<up> n @ x # lm} [Dec n 2, Goto 0] {\<lambda>nl. nl = 0 # 0 \<up> n @ lm}"
  1051   "{\<lambda>nl. nl = 0 \<up> n @ x # lm} [Dec n 2, Goto 0] {\<lambda>nl. nl = 0 # 0 \<up> n @ lm}"
  1099       by(rule_tac ind, simp)
  1096       by(rule_tac ind, simp)
  1100   next
  1097   next
  1101     show "{\<lambda>nl. nl = 0 \<up> n @ drop n lm} [Dec n 2, Goto 0] {\<lambda>nl. nl = 0 # 0 \<up> n @ drop (Suc n) lm}"
  1098     show "{\<lambda>nl. nl = 0 \<up> n @ drop n lm} [Dec n 2, Goto 0] {\<lambda>nl. nl = 0 # 0 \<up> n @ drop (Suc n) lm}"
  1102       using empty_one_box_correct[of n "lm ! n" "drop (Suc n) lm"]
  1099       using empty_one_box_correct[of n "lm ! n" "drop (Suc n) lm"]
  1103       using h
  1100       using h
  1104       by(simp add: nth_drop')
  1101       by(simp add: Cons_nth_drop_Suc)
  1105   qed
  1102   qed
  1106   thus "?case"
  1103   thus "?case"
  1107     by(simp add: empty_boxes.simps)
  1104     by(simp add: empty_boxes.simps)
  1108 qed
  1105 qed
  1109 
  1106 
  1110 lemma [simp]: "length gs \<le> ffp \<Longrightarrow>
  1107 lemma insert_dominated[simp]: "length gs \<le> ffp \<Longrightarrow>
  1111     length gs + (max (Suc (length xs)) (Max (insert ffp ((\<lambda>(aprog, p, n). n) ` rec_ci ` set gs))) - length gs) =
  1108     length gs + (max xs (Max (insert ffp (x1 ` x2 ` set gs))) - length gs) =
  1112     max (Suc (length xs)) (Max (insert ffp ((\<lambda>(aprog, p, n). n) ` rec_ci ` set gs)))"
  1109     max xs (Max (insert ffp (x1 ` x2 ` set gs)))"
  1113 apply(rule_tac le_add_diff_inverse)
  1110 apply(rule_tac le_add_diff_inverse)
  1114 apply(rule_tac min_max.le_supI2)
  1111 apply(rule_tac max.coboundedI2)
  1115 apply(simp add: Max_ge_iff)
  1112 apply(simp add: Max_ge_iff)
  1116 done
  1113 done
  1117 
  1114 
  1118 
  1115 
  1119 lemma clean_paras: 
  1116 lemma clean_paras: 
  1315     apply(auto simp: rec_ci.simps abc_comp_commute)
  1312     apply(auto simp: rec_ci.simps abc_comp_commute)
  1316     apply(rule_tac compile_cn_correct', simp_all)
  1313     apply(rule_tac compile_cn_correct', simp_all)
  1317     done
  1314     done
  1318 qed
  1315 qed
  1319 
  1316 
  1320 lemma [simp]: 
  1317 lemma mv_box_correct_simp[simp]: 
  1321   "\<lbrakk>length xs = n; ft = max (n+3) (max fft gft)\<rbrakk> 
  1318   "\<lbrakk>length xs = n; ft = max (n+3) (max fft gft)\<rbrakk> 
  1322  \<Longrightarrow> {\<lambda>nl. nl = xs @ 0 # 0 \<up> (ft - n) @ anything} mv_box n ft 
  1319  \<Longrightarrow> {\<lambda>nl. nl = xs @ 0 # 0 \<up> (ft - n) @ anything} mv_box n ft 
  1323        {\<lambda>nl. nl = xs @ 0 # 0 \<up> (ft - n) @ anything}"
  1320        {\<lambda>nl. nl = xs @ 0 # 0 \<up> (ft - n) @ anything}"
  1324 using mv_box_correct[of n ft "xs @ 0 # 0 \<up> (ft - n) @ anything"]
  1321 using mv_box_correct[of n ft "xs @ 0 # 0 \<up> (ft - n) @ anything"]
  1325 by(auto)
  1322 by(auto)
  1326 
  1323 
  1327 lemma [simp]: "length xs < max (length xs + 3) (max fft gft)"
  1324 lemma length_under_max[simp]: "length xs < max (length xs + 3) fft"
  1328 by auto
  1325 by auto
  1329 
  1326 
  1330 lemma save_init_rs: 
  1327 lemma save_init_rs: 
  1331   "\<lbrakk>length xs = n; ft = max (n+3) (max fft gft)\<rbrakk> 
  1328   "\<lbrakk>length xs = n; ft = max (n+3) (max fft gft)\<rbrakk> 
  1332      \<Longrightarrow>  {\<lambda>nl. nl = xs @ rec_exec f xs # 0 \<up> (ft - n) @ anything} mv_box n (Suc n) 
  1329      \<Longrightarrow>  {\<lambda>nl. nl = xs @ rec_exec f xs # 0 \<up> (ft - n) @ anything} mv_box n (Suc n) 
  1334 using mv_box_correct[of n "Suc n" "xs @ rec_exec f xs # 0 \<up> (ft - n) @ anything"]
  1331 using mv_box_correct[of n "Suc n" "xs @ rec_exec f xs # 0 \<up> (ft - n) @ anything"]
  1335 apply(auto simp: list_update_append list_update.simps nth_append split: if_splits)
  1332 apply(auto simp: list_update_append list_update.simps nth_append split: if_splits)
  1336 apply(case_tac "(max (length xs + 3) (max fft gft))", simp_all add: list_update.simps Suc_diff_le)
  1333 apply(case_tac "(max (length xs + 3) (max fft gft))", simp_all add: list_update.simps Suc_diff_le)
  1337 done
  1334 done
  1338 
  1335 
  1339 lemma [simp]: "n + (2::nat) < max (n + 3) (max fft gft)"
  1336 lemma less_then_max_plus2[simp]: "n + (2::nat) < max (n + 3) x"
  1340 by auto
  1337 by auto
  1341 
  1338 
  1342 lemma [simp]: "n < max (n + (3::nat)) (max fft gft)"
  1339 lemma less_then_max_plus3[simp]: "n < max (n + (3::nat)) x"
  1343 by auto
  1340 by auto
  1344 
  1341 
  1345 lemma [simp]:
  1342 lemma mv_box_max_plus_3_correct[simp]:
  1346   "length xs = n \<Longrightarrow> 
  1343   "length xs = n \<Longrightarrow> 
  1347   {\<lambda>nl. nl = xs @ x # 0 \<up> (max (n + (3::nat)) (max fft gft) - n) @ anything} mv_box n (max (n + 3) (max fft gft))
  1344   {\<lambda>nl. nl = xs @ x # 0 \<up> (max (n + (3::nat)) (max fft gft) - n) @ anything} mv_box n (max (n + 3) (max fft gft))
  1348   {\<lambda>nl. nl = xs @ 0 \<up> (max (n + 3) (max fft gft) - n) @ x # anything}"
  1345   {\<lambda>nl. nl = xs @ 0 \<up> (max (n + 3) (max fft gft) - n) @ x # anything}"
  1349 proof -
  1346 proof -
  1350   assume h: "length xs = n"
  1347   assume h: "length xs = n"
  1371     apply(simp)
  1368     apply(simp)
  1372     using b
  1369     using b
  1373     by simp
  1370     by simp
  1374 qed
  1371 qed
  1375 
  1372 
  1376 lemma [simp]: "max n (Suc n) < Suc (Suc (max (n + 3) (max fft gft) + length anything - Suc 0))"
  1373 lemma max_less_suc_suc[simp]: "max n (Suc n) < Suc (Suc (max (n + 3) x + anything - Suc 0))"
  1377 by arith    
  1374 by arith    
  1378 
  1375 
  1379 lemma [simp]: "Suc n < max (n + 3) (max fft gft)"
  1376 lemma suc_less_plus_3[simp]: "Suc n < max (n + 3) x"
  1380 by arith
  1377 by arith
  1381 
  1378 
  1382 lemma [simp]:
  1379 lemma mv_box_ok_suc_simp[simp]:
  1383   "length xs = n
  1380   "length xs = n
  1384  \<Longrightarrow> {\<lambda>nl. nl = xs @ rec_exec f xs # 0 \<up> (max (n + 3) (max fft gft) - Suc n) @ x # anything} mv_box n (Suc n)
  1381  \<Longrightarrow> {\<lambda>nl. nl = xs @ rec_exec f xs # 0 \<up> (max (n + 3) (max fft gft) - Suc n) @ x # anything} mv_box n (Suc n)
  1385     {\<lambda>nl. nl = xs @ 0 # rec_exec f xs # 0 \<up> (max (n + 3) (max fft gft) - Suc (Suc n)) @ x # anything}"
  1382     {\<lambda>nl. nl = xs @ 0 # rec_exec f xs # 0 \<up> (max (n + 3) (max fft gft) - Suc (Suc n)) @ x # anything}"
  1386 using mv_box_correct[of n "Suc n" "xs @ rec_exec f xs # 0 \<up> (max (n + 3) (max fft gft) - Suc n) @ x # anything"]
  1383 using mv_box_correct[of n "Suc n" "xs @ rec_exec f xs # 0 \<up> (max (n + 3) (max fft gft) - Suc n) @ x # anything"]
  1387 apply(simp add: nth_append list_update_append list_update.simps)
  1384 apply(simp add: nth_append list_update_append list_update.simps)
  1460 apply(case_tac "A = []")
  1457 apply(case_tac "A = []")
  1461 apply(rule_tac x = 0 in exI, simp add: abc_steps_l.simps abc_exec_null)
  1458 apply(rule_tac x = 0 in exI, simp add: abc_steps_l.simps abc_exec_null)
  1462 apply(rule_tac abc_append_frist_steps_halt_eq', simp_all)
  1459 apply(rule_tac abc_append_frist_steps_halt_eq', simp_all)
  1463 done
  1460 done
  1464 
  1461 
  1465 lemma [simp]: "Suc (Suc (max (length xs + 3) (max fft gft) - Suc (Suc (length xs))))
  1462 lemma suc_suc_max_simp[simp]: "Suc (Suc (max (xs + 3) fft - Suc (Suc ( xs))))
  1466            = max (length xs + 3) (max fft gft) - (length xs)"
  1463            = max ( xs + 3) fft - ( xs)"
  1467 by arith
  1464 by arith
  1468 
  1465 
  1469 lemma [simp]: "\<lbrakk>ft = max (n + 3) (max fft gft); length xs = n\<rbrakk> \<Longrightarrow>
  1466 lemma contract_dec_ft_length_plus_7[simp]: "\<lbrakk>ft = max (n + 3) (max fft gft); length xs = n\<rbrakk> \<Longrightarrow>
  1470      {\<lambda>nl. nl = xs @ (x - Suc y) # rec_exec (Pr n f g) (xs @ [x - Suc y]) # 0 \<up> (ft - Suc (Suc n)) @ Suc y # anything}
  1467      {\<lambda>nl. nl = xs @ (x - Suc y) # rec_exec (Pr n f g) (xs @ [x - Suc y]) # 0 \<up> (ft - Suc (Suc n)) @ Suc y # anything}
  1471      [Dec ft (length gap + 7)] 
  1468      [Dec ft (length gap + 7)] 
  1472      {\<lambda>nl. nl = xs @ (x - Suc y) # rec_exec (Pr n f g) (xs @ [x - Suc y]) # 0 \<up> (ft - Suc (Suc n)) @ y # anything}"
  1469      {\<lambda>nl. nl = xs @ (x - Suc y) # rec_exec (Pr n f g) (xs @ [x - Suc y]) # 0 \<up> (ft - Suc (Suc n)) @ y # anything}"
  1473 apply(simp add: abc_Hoare_halt_def)
  1470 apply(simp add: abc_Hoare_halt_def)
  1474 apply(rule_tac x = 1 in exI)
  1471 apply(rule_tac x = 1 in exI)
  1504     have "length xs = n \<Longrightarrow> 
  1501     have "length xs = n \<Longrightarrow> 
  1505       {\<lambda>nl. nl = xs @ Suc x # y # anything} [Dec (Suc n) 2, Goto 0] {\<lambda>nl. nl = xs @ Suc x # 0 # anything}" by fact
  1502       {\<lambda>nl. nl = xs @ Suc x # y # anything} [Dec (Suc n) 2, Goto 0] {\<lambda>nl. nl = xs @ Suc x # 0 # anything}" by fact
  1506     then obtain stp where 
  1503     then obtain stp where 
  1507       "abc_steps_l (0, xs @ Suc x # y # anything) [Dec (Suc n) 2, Goto 0] stp = (2, xs @ Suc x # 0 # anything)"
  1504       "abc_steps_l (0, xs @ Suc x # y # anything) [Dec (Suc n) 2, Goto 0] stp = (2, xs @ Suc x # 0 # anything)"
  1508       using h
  1505       using h
  1509       apply(auto simp: abc_Hoare_halt_def)
  1506       apply(auto simp: abc_Hoare_halt_def numeral_2_eq_2)
  1510       by(case_tac "abc_steps_l (0, xs @ Suc x # y # anything) [Dec (Suc (length xs)) 2, Goto 0] n",   
  1507       by (metis (mono_tags, lifting) abc_final.simps abc_holds_for.elims(2) length_Cons list.size(3))
  1511             simp_all add: numeral_2_eq_2)   
       
  1512     moreover have "abc_steps_l (0, xs @ Suc x # Suc y # anything) [Dec (Suc n) 2, Goto 0] 2 = 
  1508     moreover have "abc_steps_l (0, xs @ Suc x # Suc y # anything) [Dec (Suc n) 2, Goto 0] 2 = 
  1513                  (0, xs @ Suc x # y # anything)"
  1509                  (0, xs @ Suc x # y # anything)"
  1514       using h
  1510       using h
  1515       by(simp add: abc_steps_l.simps numeral_2_eq_2 abc_step_l.simps abc_fetch.simps
  1511       by(simp add: abc_steps_l.simps numeral_2_eq_2 abc_step_l.simps abc_fetch.simps
  1516         abc_lm_v.simps abc_lm_s.simps nth_append list_update_append list_update.simps)
  1512         abc_lm_v.simps abc_lm_s.simps nth_append list_update_append list_update.simps)
  1524   "length xs = n \<Longrightarrow> {\<lambda>nl. nl = xs @ x # y # anything}  [Inc n, Dec (Suc n) 3, Goto (Suc 0)]
  1520   "length xs = n \<Longrightarrow> {\<lambda>nl. nl = xs @ x # y # anything}  [Inc n, Dec (Suc n) 3, Goto (Suc 0)]
  1525        {\<lambda>nl. nl = xs @ Suc x # 0 # anything}"
  1521        {\<lambda>nl. nl = xs @ Suc x # 0 # anything}"
  1526 using adjust_paras'[of xs n x y anything]
  1522 using adjust_paras'[of xs n x y anything]
  1527 by(simp add: abc_comp.simps abc_shift.simps numeral_2_eq_2 numeral_3_eq_3)
  1523 by(simp add: abc_comp.simps abc_shift.simps numeral_2_eq_2 numeral_3_eq_3)
  1528 
  1524 
  1529 lemma [simp]: "\<lbrakk>rec_ci g = (gap, gar, gft); \<forall>y<x. terminate g (xs @ [y, rec_exec (Pr n f g) (xs @ [y])]);
  1525 lemma rec_ci_SucSuc_n[simp]: "\<lbrakk>rec_ci g = (gap, gar, gft); \<forall>y<x. terminate g (xs @ [y, rec_exec (Pr n f g) (xs @ [y])]);
  1530         length xs = n; Suc y\<le>x\<rbrakk> \<Longrightarrow> gar = Suc (Suc n)"
  1526         length xs = n; Suc y\<le>x\<rbrakk> \<Longrightarrow> gar = Suc (Suc n)"
  1531   apply(erule_tac x = y in allE, simp)
  1527   apply(erule_tac x = y in allE, simp)
  1532   apply(drule_tac param_pattern, auto)
  1528   apply(drule_tac param_pattern, auto)
  1533   done
  1529   done
  1534 
  1530 
  1581 apply(induct y)
  1577 apply(induct y)
  1582 apply(simp add: rec_exec.simps)
  1578 apply(simp add: rec_exec.simps)
  1583 apply(simp add: rec_exec.simps)
  1579 apply(simp add: rec_exec.simps)
  1584 done
  1580 done
  1585 
  1581 
  1586 lemma [simp]: "Suc (max (n + 3) (max fft gft) - Suc (Suc (Suc n))) = max (n + 3) (max fft gft) - Suc (Suc n)"
  1582 lemma suc_max_simp[simp]: "Suc (max (n + 3) fft - Suc (Suc (Suc n))) = max (n + 3) fft - Suc (Suc n)"
  1587 by arith
  1583 by arith
  1588 
  1584 
  1589 lemma pr_loop:
  1585 lemma pr_loop:
  1590   assumes code: "code = ([Dec (max (n + 3) (max fft gft)) (length gap + 7)] [+] (gap [+] [Inc n, Dec (Suc n) 3, Goto (Suc 0)])) @
  1586   assumes code: "code = ([Dec (max (n + 3) (max fft gft)) (length gap + 7)] [+] (gap [+] [Inc n, Dec (Suc n) 3, Goto (Suc 0)])) @
  1591     [Dec (Suc (Suc n)) 0, Inc (Suc n), Goto (length gap + 4)]"
  1587     [Dec (Suc (Suc n)) 0, Inc (Suc n), Goto (length gap + 4)]"
  1689   ultimately show "?thesis"
  1685   ultimately show "?thesis"
  1690     using code ft
  1686     using code ft
  1691     by(auto, rule_tac x = "stp + stpa" in exI, simp add: abc_steps_add replicate_Suc_iff_anywhere del: replicate_Suc)
  1687     by(auto, rule_tac x = "stp + stpa" in exI, simp add: abc_steps_add replicate_Suc_iff_anywhere del: replicate_Suc)
  1692 qed
  1688 qed
  1693 
  1689 
  1694 lemma [simp]: 
  1690 lemma abc_lm_s_simp0[simp]: 
  1695   "length xs = n \<Longrightarrow> abc_lm_s (xs @ x # rec_exec (Pr n f g) (xs @ [x]) # 0 \<up> (max (n + 3) 
  1691   "length xs = n \<Longrightarrow> abc_lm_s (xs @ x # rec_exec (Pr n f g) (xs @ [x]) # 0 \<up> (max (n + 3) 
  1696   (max fft gft) - Suc (Suc n)) @ 0 # anything) (max (n + 3) (max fft gft)) 0 =
  1692   (max fft gft) - Suc (Suc n)) @ 0 # anything) (max (n + 3) (max fft gft)) 0 =
  1697     xs @ x # rec_exec (Pr n f g) (xs @ [x]) # 0 \<up> (max (n + 3) (max fft gft) - Suc n) @ anything"
  1693     xs @ x # rec_exec (Pr n f g) (xs @ [x]) # 0 \<up> (max (n + 3) (max fft gft) - Suc n) @ anything"
  1698 apply(simp add: abc_lm_s.simps)
  1694 apply(simp add: abc_lm_s.simps)
  1699 using list_update_length[of "xs @ x # rec_exec (Pr n f g) (xs @ [x]) # 0 \<up> (max (n + 3) (max fft gft) - Suc (Suc n))"
  1695 using list_update_length[of "xs @ x # rec_exec (Pr n f g) (xs @ [x]) # 0 \<up> (max (n + 3) (max fft gft) - Suc (Suc n))"
  1700                         0 anything 0]
  1696                         0 anything 0]
  1701 apply(auto simp: Suc_diff_Suc)
  1697 apply(auto simp: Suc_diff_Suc)
  1702 apply(simp add: exp_suc[THEN sym] Suc_diff_Suc  del: replicate_Suc)
  1698 apply(simp add: exp_suc[THEN sym] Suc_diff_Suc  del: replicate_Suc)
  1703 done
  1699 done
  1704 
  1700 
  1705 lemma [simp]:
  1701 lemma index_at_zero_elem[simp]:
  1706   "(xs @ x # rec_exec (Pr (length xs) f g) (xs @ [x]) # 0 \<up> (max (length xs + 3)
  1702   "(xs @ x # re # 0 \<up> (max (length xs + 3)
  1707   (max fft gft) - Suc (Suc (length xs))) @ 0 # anything) !
  1703   (max fft gft) - Suc (Suc (length xs))) @ 0 # anything) !
  1708     max (length xs + 3) (max fft gft) = 0"
  1704     max (length xs + 3) (max fft gft) = 0"
  1709 using nth_append_length[of "xs @ x # rec_exec (Pr (length xs) f g) (xs @ [x]) #
  1705 using nth_append_length[of "xs @ x # re #
  1710   0 \<up> (max (length xs + 3) (max fft gft) - Suc (Suc (length xs)))" 0  anything]
  1706   0 \<up> (max (length xs + 3) (max fft gft) - Suc (Suc (length xs)))" 0  anything]
  1711 by(simp)
  1707 by(simp)
  1712 
  1708 
  1713 lemma pr_loop_correct:
  1709 lemma pr_loop_correct:
  1714   assumes less: "y \<le> x" 
  1710   assumes less: "y \<le> x" 
  1918 lemma wf_mn_le[intro]: "wf mn_LE"
  1914 lemma wf_mn_le[intro]: "wf mn_LE"
  1919 by(auto intro:wf_inv_image wf_lex_triple simp: mn_LE_def)
  1915 by(auto intro:wf_inv_image wf_lex_triple simp: mn_LE_def)
  1920 
  1916 
  1921 declare mn_ind_inv.simps[simp del]
  1917 declare mn_ind_inv.simps[simp del]
  1922 
  1918 
  1923 lemma [simp]: 
  1919 lemma put_in_tape_small_enough0[simp]: 
  1924   "0 < rsx \<Longrightarrow> 
  1920   "0 < rsx \<Longrightarrow> 
  1925  \<exists>y. (xs @ x # rsx # anything)[Suc (length xs) := rsx - Suc 0] = xs @ x # y # anything \<and> y \<le> rsx"
  1921  \<exists>y. (xs @ x # rsx # anything)[Suc (length xs) := rsx - Suc 0] = xs @ x # y # anything \<and> y \<le> rsx"
  1926 apply(rule_tac x = "rsx - 1" in exI)
  1922 apply(rule_tac x = "rsx - 1" in exI)
  1927 apply(simp add: list_update_append list_update.simps)
  1923 apply(simp add: list_update_append list_update.simps)
  1928 done
  1924 done
  1929 
  1925 
  1930 lemma [simp]: 
  1926 lemma put_in_tape_small_enough1[simp]: 
  1931   "\<lbrakk>y \<le> rsx; 0 < y\<rbrakk>
  1927   "\<lbrakk>y \<le> rsx; 0 < y\<rbrakk>
  1932             \<Longrightarrow> \<exists>ya. (xs @ x # y # anything)[Suc (length xs) := y - Suc 0] = xs @ x # ya # anything \<and> ya \<le> rsx"
  1928             \<Longrightarrow> \<exists>ya. (xs @ x # y # anything)[Suc (length xs) := y - Suc 0] = xs @ x # ya # anything \<and> ya \<le> rsx"
  1933 apply(rule_tac x = "y - 1" in exI)
  1929 apply(rule_tac x = "y - 1" in exI)
  1934 apply(simp add: list_update_append list_update.simps)
  1930 apply(simp add: list_update_append list_update.simps)
  1935 done
  1931 done
  1937 lemma abc_comp_null[simp]: "(A [+] B = []) = (A = [] \<and> B = [])"
  1933 lemma abc_comp_null[simp]: "(A [+] B = []) = (A = [] \<and> B = [])"
  1938 by(auto simp: abc_comp.simps abc_shift.simps)
  1934 by(auto simp: abc_comp.simps abc_shift.simps)
  1939 
  1935 
  1940 lemma rec_ci_not_null[simp]: "(rec_ci f \<noteq> ([], a, b))"
  1936 lemma rec_ci_not_null[simp]: "(rec_ci f \<noteq> ([], a, b))"
  1941 apply(case_tac f, auto simp: rec_ci_z_def rec_ci_s_def rec_ci.simps addition.simps rec_ci_id.simps)
  1937 apply(case_tac f, auto simp: rec_ci_z_def rec_ci_s_def rec_ci.simps addition.simps rec_ci_id.simps)
  1942 apply(case_tac "rec_ci recf", auto simp: mv_box.simps)
  1938 apply(case_tac "rec_ci x42", auto simp: mv_box.simps)
  1943 apply(case_tac "rec_ci recf1", case_tac "rec_ci recf2", simp)
  1939 apply(case_tac "rec_ci x52", case_tac "rec_ci x53", simp)
  1944 apply(case_tac "rec_ci recf", simp)
  1940 apply(case_tac "rec_ci x62", simp)
  1945 done
  1941 done
  1946 
  1942 
  1947 lemma mn_correct:
  1943 lemma mn_correct:
  1948   assumes compile: "rec_ci rf = (fap, far, fft)"
  1944   assumes compile: "rec_ci rf = (fap, far, fft)"
  1949   and ge: "0 < rsx"
  1945   and ge: "0 < rsx"
  2229 
  2225 
  2230 lemma abc_list_crsp_elim: 
  2226 lemma abc_list_crsp_elim: 
  2231   "\<lbrakk>abc_list_crsp lma lmb; \<exists> n. lma = lmb @ 0\<up>n \<or> lmb = lma @ 0\<up>n \<Longrightarrow> P \<rbrakk> \<Longrightarrow> P"
  2227   "\<lbrakk>abc_list_crsp lma lmb; \<exists> n. lma = lmb @ 0\<up>n \<or> lmb = lma @ 0\<up>n \<Longrightarrow> P \<rbrakk> \<Longrightarrow> P"
  2232 by(auto simp: abc_list_crsp_def)
  2228 by(auto simp: abc_list_crsp_def)
  2233 
  2229 
  2234 lemma [simp]: 
  2230 lemma abc_list_crsp_simp[simp]: 
  2235   "\<lbrakk>abc_list_crsp lma lmb; m < length lma; m < length lmb\<rbrakk> \<Longrightarrow>
  2231   "\<lbrakk>abc_list_crsp lma lmb; m < length lma; m < length lmb\<rbrakk> \<Longrightarrow>
  2236           abc_list_crsp (lma[m := n]) (lmb[m := n])"
  2232           abc_list_crsp (lma[m := n]) (lmb[m := n])"
  2237 by(auto simp: abc_list_crsp_def list_update_append)
  2233 by(auto simp: abc_list_crsp_def list_update_append)
  2238 
  2234 
  2239 lemma [simp]: 
  2235 lemma abc_list_crsp_simp2[simp]: 
  2240   "\<lbrakk>abc_list_crsp lma lmb; m < length lma; \<not> m < length lmb\<rbrakk> \<Longrightarrow> 
  2236   "\<lbrakk>abc_list_crsp lma lmb; m < length lma; \<not> m < length lmb\<rbrakk> \<Longrightarrow> 
  2241   abc_list_crsp (lma[m := n]) (lmb @ 0 \<up> (m - length lmb) @ [n])"
  2237   abc_list_crsp (lma[m := n]) (lmb @ 0 \<up> (m - length lmb) @ [n])"
  2242 apply(auto simp: abc_list_crsp_def list_update_append)
  2238 apply(auto simp: abc_list_crsp_def list_update_append)
  2243 apply(rule_tac x = "na + length lmb - Suc m" in exI)
  2239 apply(rule_tac x = "na + length lmb - Suc m" in exI)
  2244 apply(rule_tac disjI1)
  2240 apply(rule_tac disjI1)
  2245 apply(simp add: upd_conv_take_nth_drop min_absorb1)
  2241 apply(simp add: upd_conv_take_nth_drop min_absorb1)
  2246 done
  2242 done
  2247 
  2243 
  2248 lemma [simp]:
  2244 lemma abc_list_crsp_simp3[simp]:
  2249   "\<lbrakk>abc_list_crsp lma lmb; \<not> m < length lma; m < length lmb\<rbrakk> \<Longrightarrow> 
  2245   "\<lbrakk>abc_list_crsp lma lmb; \<not> m < length lma; m < length lmb\<rbrakk> \<Longrightarrow> 
  2250   abc_list_crsp (lma @ 0 \<up> (m - length lma) @ [n]) (lmb[m := n])"
  2246   abc_list_crsp (lma @ 0 \<up> (m - length lma) @ [n]) (lmb[m := n])"
  2251 apply(auto simp: abc_list_crsp_def list_update_append)
  2247 apply(auto simp: abc_list_crsp_def list_update_append)
  2252 apply(rule_tac x = "na + length lma - Suc m" in exI)
  2248 apply(rule_tac x = "na + length lma - Suc m" in exI)
  2253 apply(rule_tac disjI2)
  2249 apply(rule_tac disjI2)
  2254 apply(simp add: upd_conv_take_nth_drop min_absorb1)
  2250 apply(simp add: upd_conv_take_nth_drop min_absorb1)
  2255 done
  2251 done
  2256 
  2252 
  2257 lemma [simp]: "\<lbrakk>abc_list_crsp lma lmb; \<not> m < length lma; \<not> m < length lmb\<rbrakk> \<Longrightarrow> 
  2253 lemma abc_list_crsp_simp4[simp]: "\<lbrakk>abc_list_crsp lma lmb; \<not> m < length lma; \<not> m < length lmb\<rbrakk> \<Longrightarrow> 
  2258   abc_list_crsp (lma @ 0 \<up> (m - length lma) @ [n]) (lmb @ 0 \<up> (m - length lmb) @ [n])"
  2254   abc_list_crsp (lma @ 0 \<up> (m - length lma) @ [n]) (lmb @ 0 \<up> (m - length lmb) @ [n])"
  2259   by(auto simp: abc_list_crsp_def list_update_append replicate_merge_anywhere)
  2255   by(auto simp: abc_list_crsp_def list_update_append replicate_merge_anywhere)
  2260 
  2256 
  2261 lemma abc_list_crsp_lm_s: 
  2257 lemma abc_list_crsp_lm_s: 
  2262   "abc_list_crsp lma lmb \<Longrightarrow> 
  2258   "abc_list_crsp lma lmb \<Longrightarrow> 
  2283   fix stp a lm' aa b
  2279   fix stp a lm' aa b
  2284   assume ind:
  2280   assume ind:
  2285     "\<And>a lm'. aa = a \<and> b = lm' \<Longrightarrow> 
  2281     "\<And>a lm'. aa = a \<and> b = lm' \<Longrightarrow> 
  2286      \<exists>lma. abc_steps_l (0, lm) aprog stp = (a, lma) \<and>
  2282      \<exists>lma. abc_steps_l (0, lm) aprog stp = (a, lma) \<and>
  2287                                           abc_list_crsp lm' lma"
  2283                                           abc_list_crsp lm' lma"
  2288     and h: "abc_steps_l (0, lm @ 0\<up>m) aprog (Suc stp) = (a, lm')" 
  2284     and h: "abc_step_l (aa, b) (abc_fetch aa aprog) = (a, lm')" 
  2289            "abc_steps_l (0, lm @ 0\<up>m) aprog stp = (aa, b)" 
  2285            "abc_steps_l (0, lm @ 0\<up>m) aprog stp = (aa, b)" 
  2290            "aprog \<noteq> []"
  2286            "aprog \<noteq> []"
  2291   hence g1: "abc_steps_l (0, lm @ 0\<up>m) aprog (Suc stp)
       
  2292           = abc_step_l (aa, b) (abc_fetch aa aprog)"
       
  2293     apply(rule_tac abc_step_red, simp)
       
  2294     done
       
  2295   have "\<exists>lma. abc_steps_l (0, lm) aprog stp = (aa, lma) \<and> 
  2287   have "\<exists>lma. abc_steps_l (0, lm) aprog stp = (aa, lma) \<and> 
  2296               abc_list_crsp b lma"
  2288               abc_list_crsp b lma"
  2297     apply(rule_tac ind, simp)
  2289     apply(rule_tac ind, simp)
  2298     done
  2290     done
  2299   from this obtain lma where g2: 
  2291   from this obtain lma where g2: 
  2302   hence g3: "abc_steps_l (0, lm) aprog (Suc stp)
  2294   hence g3: "abc_steps_l (0, lm) aprog (Suc stp)
  2303           = abc_step_l (aa, lma) (abc_fetch aa aprog)"
  2295           = abc_step_l (aa, lma) (abc_fetch aa aprog)"
  2304     apply(rule_tac abc_step_red, simp)
  2296     apply(rule_tac abc_step_red, simp)
  2305     done
  2297     done
  2306   show "\<exists>lma. abc_steps_l (0, lm) aprog (Suc stp) = (a, lma) \<and> abc_list_crsp lm' lma"
  2298   show "\<exists>lma. abc_steps_l (0, lm) aprog (Suc stp) = (a, lma) \<and> abc_list_crsp lm' lma"
  2307     using g1 g2 g3 h
  2299     using g2 g3 h
  2308     apply(auto)
  2300     apply(auto)
  2309     apply(case_tac "abc_step_l (aa, b) (abc_fetch aa aprog)",
  2301     apply(case_tac "abc_step_l (aa, b) (abc_fetch aa aprog)",
  2310           case_tac "abc_step_l (aa, lma) (abc_fetch aa aprog)", simp)
  2302           case_tac "abc_step_l (aa, lma) (abc_fetch aa aprog)", simp)
  2311     apply(rule_tac abc_list_crsp_step, auto)
  2303     apply(rule_tac abc_list_crsp_step, auto)
  2312     done
  2304     done
  2341 apply(case_tac "abc_steps_l (0, args @ 0 \<up> (ft - arity)) ap n", auto)
  2333 apply(case_tac "abc_steps_l (0, args @ 0 \<up> (ft - arity)) ap n", auto)
  2342 apply(drule_tac abc_list_crsp_steps, auto)
  2334 apply(drule_tac abc_list_crsp_steps, auto)
  2343 apply(rule_tac list_crsp_simp2, auto)
  2335 apply(rule_tac list_crsp_simp2, auto)
  2344 done
  2336 done
  2345 
  2337 
  2346 lemma [simp]:
  2338 lemma find_exponent_rec_exec[simp]:
  2347   assumes a: "args @ [rec_exec f args] = lm @ 0 \<up> n"
  2339   assumes a: "args @ [rec_exec f args] = lm @ 0 \<up> n"
  2348   and b: "length args < length lm"
  2340   and b: "length args < length lm"
  2349   shows "\<exists>m. lm = args @ rec_exec f args # 0 \<up> m"
  2341   shows "\<exists>m. lm = args @ rec_exec f args # 0 \<up> m"
  2350 using assms
  2342 using assms
  2351 apply(case_tac n, simp)
  2343 apply(case_tac n, simp)
  2352 apply(rule_tac x = 0 in exI, simp)
  2344 apply(rule_tac x = 0 in exI, simp)
  2353 apply(drule_tac length_equal, simp)
  2345 apply(drule_tac length_equal, simp)
  2354 done
  2346 done
  2355 
  2347 
  2356 lemma [simp]: 
  2348 lemma find_exponent_complex[simp]: 
  2357 "\<lbrakk>args @ [rec_exec f args] = lm @ 0 \<up> n; \<not> length args < length lm\<rbrakk>
  2349 "\<lbrakk>args @ [rec_exec f args] = lm @ 0 \<up> n; \<not> length args < length lm\<rbrakk>
  2358   \<Longrightarrow> \<exists>m. (lm @ 0 \<up> (length args - length lm) @ [Suc 0])[length args := 0] =
  2350   \<Longrightarrow> \<exists>m. (lm @ 0 \<up> (length args - length lm) @ [Suc 0])[length args := 0] =
  2359   args @ rec_exec f args # 0 \<up> m"
  2351   args @ rec_exec f args # 0 \<up> m"
  2360 apply(case_tac n, simp_all add: exp_suc list_update_append list_update.simps del: replicate_Suc)
  2352 apply(case_tac n, simp_all add: exp_suc list_update_append list_update.simps del: replicate_Suc)
  2361 apply(subgoal_tac "length args = Suc (length lm)", simp)
  2353 apply(subgoal_tac "length args = Suc (length lm)", simp)
  2419       using g_unhalt[of "0 \<up> (?ft - (length args) - (gft - gar)) @
  2411       using g_unhalt[of "0 \<up> (?ft - (length args) - (gft - gar)) @
  2420         map (\<lambda>i. rec_exec i args) (take i gs) @ 0 \<up> (length gs - i) @ 0 \<up> Suc (length args) @ anything"]
  2412         map (\<lambda>i. rec_exec i args) (take i gs) @ 0 \<up> (length gs - i) @ 0 \<up> Suc (length args) @ anything"]
  2421       by simp
  2413       by simp
  2422     moreover have "?ft \<ge> gft"
  2414     moreover have "?ft \<ge> gft"
  2423       using g compile2
  2415       using g compile2
  2424       apply(rule_tac min_max.le_supI2, rule_tac Max_ge, simp, rule_tac insertI2)
  2416       apply(rule_tac max.coboundedI2, rule_tac Max_ge, simp, rule_tac insertI2)
  2425       apply(rule_tac  x = "rec_ci (gs ! i)" in image_eqI, simp)
  2417       apply(rule_tac  x = "rec_ci (gs ! i)" in image_eqI, simp)
  2426       by(rule_tac x = "gs!i"  in image_eqI, simp, simp)
  2418       by(rule_tac x = "gs!i"  in image_eqI, simp, simp)
  2427     then have b:"?Q_tmp = ?Q"
  2419     then have b:"?Q_tmp = ?Q"
  2428       using compile2
  2420       using compile2
  2429       apply(rule_tac arg_cong)
  2421       apply(rule_tac arg_cong)
  2524                      (0, Bk\<up>Suc (Suc m), Oc\<up>Suc (rec_exec recf args) @ Bk\<up>l)"
  2516                      (0, Bk\<up>Suc (Suc m), Oc\<up>Suc (rec_exec recf args) @ Bk\<up>l)"
  2525 proof(case_tac "rec_ci recf", simp add: tm_of_rec.simps)
  2517 proof(case_tac "rec_ci recf", simp add: tm_of_rec.simps)
  2526   fix ap ar fp
  2518   fix ap ar fp
  2527   assume "rec_ci recf = (ap, ar, fp)"
  2519   assume "rec_ci recf = (ap, ar, fp)"
  2528   thus "\<exists>stp m l. steps0 (Suc 0, [Bk, Bk], <args>) 
  2520   thus "\<exists>stp m l. steps0 (Suc 0, [Bk, Bk], <args>) 
  2529     (tm_of (ap [+] dummy_abc ar) @ shift (mopup ar) (listsum (layout_of (ap [+] dummy_abc ar)))) stp =
  2521     (tm_of (ap [+] dummy_abc ar) @ shift (mopup ar) (sum_list (layout_of (ap [+] dummy_abc ar)))) stp =
  2530     (0, Bk # Bk # Bk \<up> m, Oc # Oc \<up> rec_exec recf args @ Bk \<up> l)"
  2522     (0, Bk # Bk # Bk \<up> m, Oc # Oc \<up> rec_exec recf args @ Bk \<up> l)"
  2531     using recursive_compile_to_tm_correct1[of recf ap ar fp args "tm_of (ap [+] dummy_abc (length args))" "[]" 0]
  2523     using recursive_compile_to_tm_correct1[of recf ap ar fp args "tm_of (ap [+] dummy_abc (length args))" "[]" 0]
  2532       assms param_pattern[of recf args ap ar fp]
  2524       assms param_pattern[of recf args ap ar fp]
  2533     by(simp add: replicate_Suc[THEN sym] replicate_Suc_iff_anywhere del: replicate_Suc tm_of_rec_def, 
  2525     by(simp add: replicate_Suc[THEN sym] replicate_Suc_iff_anywhere del: replicate_Suc, 
  2534       simp add: exp_suc del: replicate_Suc)
  2526        simp add: exp_suc del: replicate_Suc)
  2535 qed
  2527 qed
  2536 
  2528 
  2537 lemma recursive_compile_to_tm_correct3: 
  2529 lemma recursive_compile_to_tm_correct3: 
  2538   assumes termi: "terminate recf args"
  2530   assumes termi: "terminate recf args"
  2539   shows "{\<lambda> tp. tp =([Bk, Bk], <args>)} (tm_of_rec recf) 
  2531   shows "{\<lambda> tp. tp =([Bk, Bk], <args>)} (tm_of_rec recf) 
  2540          {\<lambda> tp. \<exists> k l. tp = (Bk\<up> k, <rec_exec recf args> @ Bk \<up> l)}"
  2532          {\<lambda> tp. \<exists> k l. tp = (Bk\<up> k, <rec_exec recf args> @ Bk \<up> l)}"
  2541 using recursive_compile_to_tm_correct2[OF assms]
  2533 using recursive_compile_to_tm_correct2[OF assms]
  2542 apply(auto simp add: Hoare_halt_def)
  2534 apply(auto simp add: Hoare_halt_def)
  2543 apply(rule_tac x = stp in exI)
  2535 apply(rule_tac x = stp in exI)
  2544 apply(auto simp add: tape_of_nat_abv)
  2536 apply(auto simp add: tape_of_nat_def)
  2545 apply(rule_tac x = "Suc (Suc m)" in exI)
  2537 apply(rule_tac x = "Suc (Suc m)" in exI)
  2546 apply(simp)
  2538 apply(simp)
  2547 done 
  2539 done 
  2548 
  2540 
  2549 lemma [simp]:
  2541 lemma list_all_suc_many[simp]:
  2550   "list_all (\<lambda>(acn, s). s \<le> Suc (Suc (Suc (Suc (Suc (Suc (2 * n))))))) xs \<Longrightarrow>
  2542   "list_all (\<lambda>(acn, s). s \<le> Suc (Suc (Suc (Suc (Suc (Suc (2 * n))))))) xs \<Longrightarrow>
  2551   list_all (\<lambda>(acn, s). s \<le> Suc (Suc (Suc (Suc (Suc (Suc (Suc (Suc (2 * n))))))))) xs"
  2543   list_all (\<lambda>(acn, s). s \<le> Suc (Suc (Suc (Suc (Suc (Suc (Suc (Suc (2 * n))))))))) xs"
  2552 apply(induct xs, simp, simp)
  2544 apply(induct xs, simp, simp)
  2553 apply(case_tac a, simp)
  2545 apply(case_tac a, simp)
  2554 done
  2546 done
  2555 
  2547 
  2556 lemma shift_append: "shift (xs @ ys) n = shift xs n @ shift ys n"
  2548 lemma shift_append: "shift (xs @ ys) n = shift xs n @ shift ys n"
  2557 apply(simp add: shift.simps)
  2549 apply(simp add: shift.simps)
  2558 done
  2550 done
  2559 
  2551 
  2560 lemma [simp]: "length (shift (mopup n) ss) = 4 * n + 12"
  2552 lemma length_shift_mopup[simp]: "length (shift (mopup n) ss) = 4 * n + 12"
  2561 apply(auto simp: mopup.simps shift_append mopup_b_def)
  2553 apply(auto simp: mopup.simps shift_append mopup_b_def)
  2562 done
  2554 done
  2563 
  2555 
  2564 lemma length_tm_even[intro]: "length (tm_of ap) mod 2 = 0"
  2556 lemma length_tm_even[intro]: "length (tm_of ap) mod 2 = 0"
  2565 apply(simp add: tm_of.simps)
  2557 apply(simp add: tm_of.simps)
  2566 done
  2558 done
  2567 
  2559 
  2568 lemma [simp]: "k < length ap \<Longrightarrow> tms_of ap ! k  = 
  2560 lemma tms_of_at_index[simp]: "k < length ap \<Longrightarrow> tms_of ap ! k  = 
  2569  ci (layout_of ap) (start_of (layout_of ap) k) (ap ! k)"
  2561  ci (layout_of ap) (start_of (layout_of ap) k) (ap ! k)"
  2570 apply(simp add: tms_of.simps tpairs_of.simps)
  2562 apply(simp add: tms_of.simps tpairs_of.simps)
  2571 done
  2563 done
  2572 
  2564 
  2573 lemma start_of_suc_inc:
  2565 lemma start_of_suc_inc:
  2666 apply(case_tac "ap!k", auto simp: tms_of.simps tpairs_of.simps ci.simps shift_append adjust.simps)
  2658 apply(case_tac "ap!k", auto simp: tms_of.simps tpairs_of.simps ci.simps shift_append adjust.simps)
  2667 apply(erule_tac findnth_state_all_le1, simp_all)
  2659 apply(erule_tac findnth_state_all_le1, simp_all)
  2668 apply(erule_tac inc_state_all_le, simp_all)
  2660 apply(erule_tac inc_state_all_le, simp_all)
  2669 apply(erule_tac findnth_state_all_le2, simp_all)
  2661 apply(erule_tac findnth_state_all_le2, simp_all)
  2670 apply(rule_tac start_of_all_le)
  2662 apply(rule_tac start_of_all_le)
  2671 apply(rule_tac dec_state_all_le, simp_all)
       
  2672 apply(rule_tac start_of_all_le)
  2663 apply(rule_tac start_of_all_le)
  2673 done
  2664 done
  2674 
  2665 
  2675 lemma concat_in: "i < length (concat xs) \<Longrightarrow> 
  2666 lemma concat_in: "i < length (concat xs) \<Longrightarrow> 
  2676   \<exists>k < length xs. concat xs ! i \<in> set (xs ! k)"
  2667   \<exists>k < length xs. concat xs ! i \<in> set (xs ! k)"
  2680 apply(simp add: nth_append)
  2671 apply(simp add: nth_append)
  2681 apply(rule_tac x = "length xs" in exI, simp)
  2672 apply(rule_tac x = "length xs" in exI, simp)
  2682 apply(simp add: nth_append)
  2673 apply(simp add: nth_append)
  2683 done 
  2674 done 
  2684 
  2675 
  2685 lemma [simp]: "length (tms_of ap) = length ap"
  2676 lemma length_tms_of[simp]: "length (tms_of ap) = length ap"
  2686 apply(simp add: tms_of.simps tpairs_of.simps)
  2677 apply(simp add: tms_of.simps tpairs_of.simps)
  2687 done
  2678 done
  2688 
  2679 
  2689 declare length_concat[simp]
  2680 declare length_concat[simp]
  2690 
  2681 
  2707 apply(case_tac "ap ! k")
  2698 apply(case_tac "ap ! k")
  2708 apply(auto simp: layout_of.simps ci.simps 
  2699 apply(auto simp: layout_of.simps ci.simps 
  2709   length_of.simps tinc_b_def tdec_b_def length_findnth adjust.simps)
  2700   length_of.simps tinc_b_def tdec_b_def length_findnth adjust.simps)
  2710 done
  2701 done
  2711 
  2702 
  2712 lemma [intro]: "length (ci ly y i) mod 2 = 0"
  2703 lemma ci_even[intro]: "length (ci ly y i) mod 2 = 0"
  2713 apply(case_tac i, auto simp: ci.simps length_findnth
  2704 apply(case_tac i, auto simp: ci.simps length_findnth
  2714   tinc_b_def adjust.simps tdec_b_def)
  2705   tinc_b_def adjust.simps tdec_b_def)
  2715 done
  2706 done
  2716 
  2707 
  2717 lemma [intro]: "listsum (map (length \<circ> (\<lambda>(x, y). ci ly x y)) zs) mod 2 = 0"
  2708 lemma sum_list_ci_even[intro]: "sum_list (map (length \<circ> (\<lambda>(x, y). ci ly x y)) zs) mod 2 = 0"
  2718 apply(induct zs rule: rev_induct, simp)
  2709 apply(induct zs rule: rev_induct, simp)
  2719 apply(case_tac x, simp)
  2710 apply(case_tac x, simp)
  2720 apply(subgoal_tac "length (ci ly a b) mod 2 = 0")
  2711 apply(subgoal_tac "length (ci ly a b) mod 2 = 0")
  2721 apply(auto)
  2712 apply(auto)
  2722 done
  2713 done
  2738 lemma length_start_of_tm: "start_of (layout_of ap) (length ap) = Suc (length (tm_of ap)  div 2)"
  2729 lemma length_start_of_tm: "start_of (layout_of ap) (length ap) = Suc (length (tm_of ap)  div 2)"
  2739 using tpa_states[of "tm_of ap"  "length ap" ap]
  2730 using tpa_states[of "tm_of ap"  "length ap" ap]
  2740 apply(simp add: tm_of.simps)
  2731 apply(simp add: tm_of.simps)
  2741 done
  2732 done
  2742 
  2733 
  2743 lemma [elim]: "list_all (\<lambda>(acn, s). s \<le> Suc q) xs
  2734 lemma list_all_add_6E[elim]: "list_all (\<lambda>(acn, s). s \<le> Suc q) xs
  2744         \<Longrightarrow> list_all (\<lambda>(acn, s). s \<le> q + (2 * n + 6)) xs"
  2735         \<Longrightarrow> list_all (\<lambda>(acn, s). s \<le> q + (2 * n + 6)) xs"
  2745 apply(simp add: list_all_length)
  2736 by(auto simp: list_all_length)
  2746 apply(auto)
  2737 
  2747 done
  2738 lemma mopup_b_12[simp]: "length mopup_b = 12"
  2748 
       
  2749 lemma [simp]: "length mopup_b = 12"
       
  2750 apply(simp add: mopup_b_def)
  2739 apply(simp add: mopup_b_def)
  2751 done
  2740 done
  2752 
  2741 
  2753 lemma mp_up_all_le: "list_all  (\<lambda>(acn, s). s \<le> q + (2 * n + 6)) 
  2742 lemma mp_up_all_le: "list_all  (\<lambda>(acn, s). s \<le> q + (2 * n + 6)) 
  2754   [(R, Suc (Suc (2 * n + q))), (R, Suc (2 * n + q)), 
  2743   [(R, Suc (Suc (2 * n + q))), (R, Suc (2 * n + q)), 
  2755   (L, 5 + 2 * n + q), (W0, Suc (Suc (Suc (2 * n + q)))), (R, 4 + 2 * n + q),
  2744   (L, 5 + 2 * n + q), (W0, Suc (Suc (Suc (2 * n + q)))), (R, 4 + 2 * n + q),
  2756   (W0, Suc (Suc (Suc (2 * n + q)))), (R, Suc (Suc (2 * n + q))),
  2745   (W0, Suc (Suc (Suc (2 * n + q)))), (R, Suc (Suc (2 * n + q))),
  2757   (W0, Suc (Suc (Suc (2 * n + q)))), (L, 5 + 2 * n + q),
  2746   (W0, Suc (Suc (Suc (2 * n + q)))), (L, 5 + 2 * n + q),
  2758   (L, 6 + 2 * n + q), (R, 0),  (L, 6 + 2 * n + q)]"
  2747   (L, 6 + 2 * n + q), (R, 0),  (L, 6 + 2 * n + q)]"
  2759 apply(auto)
  2748 by(auto)
  2760 done
  2749 
  2761 
  2750 lemma mopup_le6[simp]: "(a, b) \<in> set (mopup_a n) \<Longrightarrow> b \<le> 2 * n + 6"
  2762 lemma [simp]: "(a, b) \<in> set (mopup_a n) \<Longrightarrow> b \<le> 2 * n + 6"
       
  2763 apply(induct n, auto simp: mopup_a.simps)
  2751 apply(induct n, auto simp: mopup_a.simps)
  2764 done
  2752 done
  2765 
  2753 
  2766 lemma [simp]: "(a, b) \<in> set (shift (mopup n) (listsum (layout_of ap)))
  2754 lemma shift_le2[simp]: "(a, b) \<in> set (shift (mopup n) x)
  2767   \<Longrightarrow> b \<le> (2 * listsum (layout_of ap) + length (mopup n)) div 2"
  2755   \<Longrightarrow> b \<le> (2 * x + length (mopup n)) div 2"
  2768 apply(auto simp: mopup.simps shift_append shift.simps)
  2756 apply(auto simp: mopup.simps shift_append shift.simps)
  2769 apply(auto simp: mopup_a.simps mopup_b_def)
  2757 apply(auto simp: mopup_b_def)
  2770 done
  2758 done
  2771 
  2759 
  2772 lemma [intro]: " 2 \<le> 2 * listsum (layout_of ap) + length (mopup n)"
  2760 lemma mopup_ge2[intro]: " 2 \<le> x + length (mopup n)"
  2773 apply(simp add: mopup.simps)
  2761 apply(simp add: mopup.simps)
  2774 done
  2762 done
  2775 
  2763 
  2776 lemma [intro]: " (2 * listsum (layout_of ap) + length (mopup n)) mod 2 = 0"
  2764 lemma mopup_even[intro]: " (2 * x + length (mopup n)) mod 2 = 0"
  2777 apply(auto simp: mopup.simps)
  2765 by(auto simp: mopup.simps)
  2778 apply arith
  2766 
  2779 done
  2767 lemma mopup_div_2[simp]: "b \<le> Suc x
  2780 
       
  2781 lemma [simp]: "b \<le> Suc x
       
  2782           \<Longrightarrow> b \<le> (2 * x + length (mopup n)) div 2"
  2768           \<Longrightarrow> b \<le> (2 * x + length (mopup n)) div 2"
  2783 apply(auto simp: mopup.simps)
  2769 by(auto simp: mopup.simps)
  2784 done
  2770 
  2785 
  2771 lemma wf_tm_from_abacus: assumes "tp = tm_of ap"
  2786 lemma wf_tm_from_abacus: 
  2772   shows "tm_wf0 (tp @ shift (mopup n) (length tp div 2))"
  2787   "tp = tm_of ap \<Longrightarrow> 
  2773 proof -
  2788     tm_wf (tp @ shift( mopup n) (length tp div 2), 0)"
  2774   have "is_even (length (mopup n))" for n using tm_wf.simps by blast
  2789  using length_start_of_tm[of ap] all_le_start_of[of ap]
  2775   moreover have "(aa, ba) \<in> set (mopup n) \<Longrightarrow> ba \<le> length (mopup n) div 2" for aa ba
  2790 apply(auto simp: tm_wf.simps List.list_all_iff)
  2776     by (metis (no_types, lifting) add_cancel_left_right case_prodD tm_wf.simps wf_mopup)
  2791 apply(case_tac n)
  2777   moreover have "(\<forall>x\<in>set (tm_of ap). case x of (acn, s) \<Rightarrow> s \<le> Suc (sum_list (layout_of ap))) \<Longrightarrow>
  2792 apply(simp add: mopup.simps)
  2778            (a, b) \<in> set (tm_of ap) \<Longrightarrow> b \<le> sum_list (layout_of ap) + length (mopup n) div 2"
  2793 apply(simp add: mopup.simps)
  2779     for a b s
  2794 apply (metis mod_mult_right_eq mod_mult_self2 mod_mult_self2_is_0 mult_2_right nat_mult_commute numeral_Bit0)
  2780     by (metis (no_types, lifting) add_Suc add_cancel_left_right case_prodD div_mult_mod_eq le_SucE mult_2_right not_numeral_le_zero tm_wf.simps trans_le_add1 wf_mopup)
  2795 sorry
  2781   ultimately show ?thesis unfolding assms
       
  2782     using length_start_of_tm[of ap] all_le_start_of[of ap] tm_wf.simps 
       
  2783   by(auto simp: List.list_all_iff shift.simps)
       
  2784 qed
  2796 
  2785 
  2797 lemma wf_tm_from_recf:
  2786 lemma wf_tm_from_recf:
  2798   assumes compile: "tp = tm_of_rec recf"
  2787   assumes compile: "tp = tm_of_rec recf"
  2799   shows "tm_wf0 tp"
  2788   shows "tm_wf0 tp"
  2800 proof -
  2789 proof -