thys/recursive.thy
changeset 70 2363eb91d9fd
child 126 0b302c0b449a
equal deleted inserted replaced
69:32ec97f94a07 70:2363eb91d9fd
       
     1 theory recursive
       
     2 imports Main rec_def abacus
       
     3 begin
       
     4 
       
     5 section {* 
       
     6   Compiling from recursive functions to Abacus machines
       
     7   *}
       
     8 
       
     9 text {*
       
    10   Some auxilliary Abacus machines used to construct the result Abacus machines.
       
    11 *}
       
    12 
       
    13 text {*
       
    14   @{text "get_paras_num recf"} returns the arity of recursive function @{text "recf"}.
       
    15 *}
       
    16 fun get_paras_num :: "recf \<Rightarrow> nat"
       
    17   where
       
    18   "get_paras_num z = 1" |
       
    19   "get_paras_num s = 1" |
       
    20   "get_paras_num (id m n) = m" |
       
    21   "get_paras_num (Cn n f gs) = n" |
       
    22   "get_paras_num (Pr n f g) = Suc n"  |
       
    23   "get_paras_num (Mn n f) = n"  
       
    24 
       
    25 fun addition :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> abc_prog"
       
    26   where
       
    27   "addition m n p = [Dec m 4, Inc n, Inc p, Goto 0, Dec p 7, 
       
    28                        Inc m, Goto 4]"
       
    29 
       
    30 fun mv_box :: "nat \<Rightarrow> nat \<Rightarrow> abc_prog"
       
    31   where
       
    32   "mv_box m n = [Dec m 3, Inc n, Goto 0]"
       
    33 
       
    34 fun abc_inst_shift :: "abc_inst \<Rightarrow> nat \<Rightarrow> abc_inst"
       
    35   where
       
    36   "abc_inst_shift (Inc m) n = Inc m" |
       
    37   "abc_inst_shift (Dec m e) n = Dec m (e + n)" |
       
    38   "abc_inst_shift (Goto m) n = Goto (m + n)"
       
    39 
       
    40 fun abc_shift :: "abc_inst list \<Rightarrow> nat \<Rightarrow> abc_inst list" 
       
    41   where
       
    42   "abc_shift xs n = map (\<lambda> x. abc_inst_shift x n) xs" 
       
    43 
       
    44 fun abc_append :: "abc_inst list \<Rightarrow> abc_inst list \<Rightarrow> 
       
    45                            abc_inst list" (infixl "[+]" 60)
       
    46   where
       
    47   "abc_append al bl = (let al_len = length al in 
       
    48                            al @ abc_shift bl al_len)"
       
    49 
       
    50 text {*
       
    51   The compilation of @{text "z"}-operator.
       
    52 *}
       
    53 definition rec_ci_z :: "abc_inst list"
       
    54   where
       
    55   "rec_ci_z \<equiv> [Goto 1]"
       
    56 
       
    57 text {*
       
    58   The compilation of @{text "s"}-operator.
       
    59 *}
       
    60 definition rec_ci_s :: "abc_inst list"
       
    61   where
       
    62   "rec_ci_s \<equiv> (addition 0 1 2 [+] [Inc 1])"
       
    63 
       
    64 
       
    65 text {*
       
    66   The compilation of @{text "id i j"}-operator
       
    67 *}
       
    68 
       
    69 fun rec_ci_id :: "nat \<Rightarrow> nat \<Rightarrow> abc_inst list"
       
    70   where
       
    71   "rec_ci_id i j = addition j i (i + 1)"
       
    72 
       
    73 fun mv_boxes :: "nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> abc_inst list"
       
    74   where
       
    75   "mv_boxes ab bb 0 = []" |
       
    76   "mv_boxes ab bb (Suc n) = mv_boxes ab bb n [+] mv_box (ab + n)
       
    77   (bb + n)"
       
    78 
       
    79 fun empty_boxes :: "nat \<Rightarrow> abc_inst list"
       
    80   where
       
    81   "empty_boxes 0 = []" |
       
    82   "empty_boxes (Suc n) = empty_boxes n [+] [Dec n 2, Goto 0]"
       
    83 
       
    84 fun cn_merge_gs ::
       
    85   "(abc_inst list \<times> nat \<times> nat) list \<Rightarrow> nat \<Rightarrow> abc_inst list"
       
    86   where
       
    87   "cn_merge_gs [] p = []" |
       
    88   "cn_merge_gs (g # gs) p = 
       
    89       (let (gprog, gpara, gn) = g in 
       
    90          gprog [+] mv_box gpara p [+] cn_merge_gs gs (Suc p))"
       
    91 
       
    92 
       
    93 text {*
       
    94   The compiler of recursive functions, where @{text "rec_ci recf"} return 
       
    95   @{text "(ap, arity, fp)"}, where @{text "ap"} is the Abacus program, @{text "arity"} is the 
       
    96   arity of the recursive function @{text "recf"}, 
       
    97 @{text "fp"} is the amount of memory which is going to be
       
    98   used by @{text "ap"} for its execution. 
       
    99 *}
       
   100 
       
   101 function rec_ci :: "recf \<Rightarrow> abc_inst list \<times> nat \<times> nat"
       
   102   where
       
   103   "rec_ci z = (rec_ci_z, 1, 2)" |
       
   104   "rec_ci s = (rec_ci_s, 1, 3)" |
       
   105   "rec_ci (id m n) = (rec_ci_id m n, m, m + 2)" |
       
   106   "rec_ci (Cn n f gs) = 
       
   107       (let cied_gs = map (\<lambda> g. rec_ci g) (f # gs) in
       
   108        let (fprog, fpara, fn) = hd cied_gs in 
       
   109        let pstr = 
       
   110         Max (set (Suc n # fn # (map (\<lambda> (aprog, p, n). n) cied_gs))) in
       
   111        let qstr = pstr + Suc (length gs) in 
       
   112        (cn_merge_gs (tl cied_gs) pstr [+] mv_boxes 0 qstr n [+] 
       
   113           mv_boxes pstr 0 (length gs) [+] fprog [+] 
       
   114             mv_box fpara pstr [+] empty_boxes (length gs) [+] 
       
   115              mv_box pstr n [+] mv_boxes qstr 0 n, n,  qstr + n))" |
       
   116   "rec_ci (Pr n f g) = 
       
   117          (let (fprog, fpara, fn) = rec_ci f in 
       
   118           let (gprog, gpara, gn) = rec_ci g in 
       
   119           let p = Max (set ([n + 3, fn, gn])) in 
       
   120           let e = length gprog + 7 in 
       
   121            (mv_box n p [+] fprog [+] mv_box n (Suc n) [+] 
       
   122                (([Dec p e] [+] gprog [+] 
       
   123                  [Inc n, Dec (Suc n) 3, Goto 1]) @
       
   124                      [Dec (Suc (Suc n)) 0, Inc (Suc n), Goto (length gprog + 4)]),
       
   125              Suc n, p + 1))" |
       
   126   "rec_ci (Mn n f) =
       
   127          (let (fprog, fpara, fn) = rec_ci f in 
       
   128           let len = length (fprog) in 
       
   129             (fprog @ [Dec (Suc n) (len + 5), Dec (Suc n) (len + 3),
       
   130              Goto (len + 1), Inc n, Goto 0], n, max (Suc n) fn) )"
       
   131   by pat_completeness auto
       
   132 termination 
       
   133 proof
       
   134 term size
       
   135   show "wf (measure size)" by auto
       
   136 next
       
   137   fix n f gs x
       
   138   assume "(x::recf) \<in> set (f # gs)" 
       
   139   thus "(x, Cn n f gs) \<in> measure size"
       
   140     by(induct gs, auto)
       
   141 next
       
   142   fix n f g
       
   143   show "(f, Pr n f g) \<in> measure size" by auto
       
   144 next
       
   145   fix n f g x xa y xb ya
       
   146   show "(g, Pr n f g) \<in> measure size" by auto
       
   147 next
       
   148   fix n f
       
   149   show "(f, Mn n f) \<in> measure size" by auto
       
   150 qed
       
   151 
       
   152 declare rec_ci.simps [simp del] rec_ci_s_def[simp del] 
       
   153         rec_ci_z_def[simp del] rec_ci_id.simps[simp del]
       
   154         mv_boxes.simps[simp del] abc_append.simps[simp del]
       
   155         mv_box.simps[simp del] addition.simps[simp del]
       
   156   
       
   157 thm rec_calc_rel.induct
       
   158 
       
   159 declare abc_steps_l.simps[simp del] abc_fetch.simps[simp del] 
       
   160         abc_step_l.simps[simp del] 
       
   161 
       
   162 lemma abc_steps_add: 
       
   163   "abc_steps_l (as, lm) ap (m + n) = 
       
   164          abc_steps_l (abc_steps_l (as, lm) ap m) ap n"
       
   165 apply(induct m arbitrary: n as lm, simp add: abc_steps_l.simps)
       
   166 proof -
       
   167   fix m n as lm
       
   168   assume ind: 
       
   169     "\<And>n as lm. abc_steps_l (as, lm) ap (m + n) = 
       
   170                    abc_steps_l (abc_steps_l (as, lm) ap m) ap n"
       
   171   show "abc_steps_l (as, lm) ap (Suc m + n) = 
       
   172              abc_steps_l (abc_steps_l (as, lm) ap (Suc m)) ap n"
       
   173     apply(insert ind[of as lm "Suc n"], simp)
       
   174     apply(insert ind[of as lm "Suc 0"], simp add: abc_steps_l.simps)
       
   175     apply(case_tac "(abc_steps_l (as, lm) ap m)", simp)
       
   176     apply(simp add: abc_steps_l.simps)
       
   177     apply(case_tac "abc_step_l (a, b) (abc_fetch a ap)", 
       
   178           simp add: abc_steps_l.simps)
       
   179     done
       
   180 qed
       
   181 
       
   182 (*lemmas: rec_ci and rec_calc_rel*)
       
   183 
       
   184 lemma rec_calc_inj_case_z: 
       
   185   "\<lbrakk>rec_calc_rel z l x; rec_calc_rel z l y\<rbrakk> \<Longrightarrow> x = y"
       
   186 apply(auto elim: calc_z_reverse)
       
   187 done
       
   188 
       
   189 lemma  rec_calc_inj_case_s: 
       
   190   "\<lbrakk>rec_calc_rel s l x; rec_calc_rel s l y\<rbrakk> \<Longrightarrow> x = y"
       
   191 apply(auto elim: calc_s_reverse)
       
   192 done
       
   193 
       
   194 lemma rec_calc_inj_case_id:
       
   195   "\<lbrakk>rec_calc_rel (recf.id nat1 nat2) l x;
       
   196     rec_calc_rel (recf.id nat1 nat2) l y\<rbrakk> \<Longrightarrow> x = y"
       
   197 apply(auto elim: calc_id_reverse)
       
   198 done
       
   199 
       
   200 lemma rec_calc_inj_case_mn:
       
   201   assumes ind: "\<And> l x y. \<lbrakk>rec_calc_rel f l x; rec_calc_rel f l y\<rbrakk> 
       
   202            \<Longrightarrow> x = y" 
       
   203   and h: "rec_calc_rel (Mn n f) l x" "rec_calc_rel (Mn n f) l y"
       
   204   shows "x = y"
       
   205   apply(insert h)
       
   206   apply(elim  calc_mn_reverse)
       
   207   apply(case_tac "x > y", simp)
       
   208   apply(erule_tac x = "y" in allE, auto)
       
   209 proof -
       
   210   fix v va
       
   211   assume "rec_calc_rel f (l @ [y]) 0" 
       
   212     "rec_calc_rel f (l @ [y]) v"  
       
   213     "0 < v"
       
   214   thus "False"
       
   215     apply(insert ind[of "l @ [y]" 0 v], simp)
       
   216     done
       
   217 next
       
   218   fix v va
       
   219   assume 
       
   220     "rec_calc_rel f (l @ [x]) 0" 
       
   221     "\<forall>x<y. \<exists>v. rec_calc_rel f (l @ [x]) v \<and> 0 < v" "\<not> y < x"
       
   222   thus "x = y"
       
   223     apply(erule_tac x = "x" in allE)
       
   224     apply(case_tac "x = y", auto)
       
   225     apply(drule_tac y = v in ind, simp, simp)
       
   226     done
       
   227 qed 
       
   228 
       
   229 lemma rec_calc_inj_case_pr: 
       
   230   assumes f_ind: 
       
   231   "\<And>l x y. \<lbrakk>rec_calc_rel f l x; rec_calc_rel f l y\<rbrakk> \<Longrightarrow> x = y"
       
   232   and g_ind:
       
   233   "\<And>x xa y xb ya l xc yb. 
       
   234   \<lbrakk>x = rec_ci f; (xa, y) = x; (xb, ya) = y; 
       
   235   rec_calc_rel g l xc; rec_calc_rel g l yb\<rbrakk> \<Longrightarrow> xc = yb"
       
   236   and h: "rec_calc_rel (Pr n f g) l x" "rec_calc_rel (Pr n f g) l y"  
       
   237   shows "x = y"
       
   238   apply(case_tac "rec_ci f")
       
   239 proof -
       
   240   fix a b c
       
   241   assume "rec_ci f = (a, b, c)"
       
   242   hence ng_ind: 
       
   243     "\<And> l xc yb. \<lbrakk>rec_calc_rel g l xc; rec_calc_rel g l yb\<rbrakk>
       
   244     \<Longrightarrow> xc = yb"
       
   245     apply(insert g_ind[of "(a, b, c)" "a" "(b, c)" b c], simp)
       
   246     done
       
   247   from h show "x = y"
       
   248     apply(erule_tac calc_pr_reverse, erule_tac calc_pr_reverse)
       
   249     apply(erule f_ind, simp, simp)
       
   250     apply(erule_tac calc_pr_reverse, simp, simp)
       
   251   proof -
       
   252     fix la ya ry laa yaa rya
       
   253     assume k1:  "rec_calc_rel g (la @ [ya, ry]) x" 
       
   254       "rec_calc_rel g (la @ [ya, rya]) y"
       
   255       and k2: "rec_calc_rel (Pr (length la) f g) (la @ [ya]) ry"
       
   256               "rec_calc_rel (Pr (length la) f g) (la @ [ya]) rya"
       
   257     from k2 have "ry = rya"
       
   258       apply(induct ya arbitrary: ry rya)
       
   259       apply(erule_tac calc_pr_reverse, 
       
   260         erule_tac calc_pr_reverse, simp)
       
   261       apply(erule f_ind, simp, simp, simp)
       
   262       apply(erule_tac calc_pr_reverse, simp)
       
   263       apply(erule_tac rSucy = rya in calc_pr_reverse, simp, simp)
       
   264     proof -
       
   265       fix ya ry rya l y ryb laa yb ryc
       
   266       assume ind:
       
   267         "\<And>ry rya. \<lbrakk>rec_calc_rel (Pr (length l) f g) (l @ [y]) ry; 
       
   268                    rec_calc_rel (Pr (length l) f g) (l @ [y]) rya\<rbrakk> \<Longrightarrow> ry = rya"
       
   269         and j: "rec_calc_rel (Pr (length l) f g) (l @ [y]) ryb"
       
   270         "rec_calc_rel g (l @ [y, ryb]) ry" 
       
   271         "rec_calc_rel (Pr (length l) f g) (l @ [y]) ryc" 
       
   272         "rec_calc_rel g (l @ [y, ryc]) rya"
       
   273       from j show "ry = rya"
       
   274 	apply(insert ind[of ryb ryc], simp)
       
   275 	apply(insert ng_ind[of "l @ [y, ryc]" ry rya], simp)
       
   276 	done
       
   277     qed 
       
   278     from k1 and this show "x = y"
       
   279       apply(simp)
       
   280       apply(insert ng_ind[of "la @ [ya, rya]" x y], simp)
       
   281       done
       
   282   qed  
       
   283 qed
       
   284 
       
   285 lemma Suc_nth_part_eq:
       
   286   "\<forall>k<Suc (length list). (a # xs) ! k = (aa # list) ! k
       
   287        \<Longrightarrow> \<forall>k<(length list). (xs) ! k = (list) ! k"
       
   288 apply(rule allI, rule impI)
       
   289 apply(erule_tac x = "Suc k" in allE, simp)
       
   290 done
       
   291 
       
   292 
       
   293 lemma list_eq_intro:  
       
   294   "\<lbrakk>length xs = length ys; \<forall> k < length xs. xs ! k = ys ! k\<rbrakk> 
       
   295   \<Longrightarrow> xs = ys"
       
   296 apply(induct xs arbitrary: ys, simp)
       
   297 apply(case_tac ys, simp, simp)
       
   298 proof -
       
   299   fix a xs ys aa list
       
   300   assume ind: 
       
   301     "\<And>ys. \<lbrakk>length list = length ys; \<forall>k<length ys. xs ! k = ys ! k\<rbrakk>
       
   302     \<Longrightarrow> xs = ys"
       
   303     and h: "length xs = length list" 
       
   304     "\<forall>k<Suc (length list). (a # xs) ! k = (aa # list) ! k"
       
   305   from h show "a = aa \<and> xs = list"
       
   306     apply(insert ind[of list], simp)
       
   307     apply(frule Suc_nth_part_eq, simp)
       
   308     apply(erule_tac x = "0" in allE, simp)
       
   309     done
       
   310 qed
       
   311 
       
   312 lemma rec_calc_inj_case_cn: 
       
   313   assumes ind: 
       
   314   "\<And>x l xa y.
       
   315   \<lbrakk>x = f \<or> x \<in> set gs; rec_calc_rel x l xa; rec_calc_rel x l y\<rbrakk>
       
   316   \<Longrightarrow> xa = y"
       
   317   and h: "rec_calc_rel (Cn n f gs) l x" 
       
   318          "rec_calc_rel (Cn n f gs) l y"
       
   319   shows "x = y"
       
   320   apply(insert h, elim  calc_cn_reverse)
       
   321   apply(subgoal_tac "rs = rsa")
       
   322   apply(rule_tac x = f and l = rsa and xa = x and y = y in ind, 
       
   323         simp, simp, simp)
       
   324   apply(intro list_eq_intro, simp, rule allI, rule impI)
       
   325   apply(erule_tac x = k in allE, rule_tac x = k in allE, simp, simp)
       
   326   apply(rule_tac x = "gs ! k" in ind, simp, simp, simp)
       
   327   done
       
   328 
       
   329 lemma rec_calc_inj:
       
   330   "\<lbrakk>rec_calc_rel f l x; 
       
   331     rec_calc_rel f l y\<rbrakk> \<Longrightarrow> x = y"
       
   332 apply(induct f arbitrary: l x y rule: rec_ci.induct)
       
   333 apply(simp add: rec_calc_inj_case_z)
       
   334 apply(simp add: rec_calc_inj_case_s)
       
   335 apply(simp add: rec_calc_inj_case_id, simp)
       
   336 apply(erule rec_calc_inj_case_cn,simp, simp)
       
   337 apply(erule rec_calc_inj_case_pr, auto)
       
   338 apply(erule rec_calc_inj_case_mn, auto)
       
   339 done
       
   340 
       
   341 
       
   342 lemma calc_rel_reverse_ind_step_ex: 
       
   343   "\<lbrakk>rec_calc_rel (Pr n f g) (lm @ [Suc x]) rs\<rbrakk> 
       
   344   \<Longrightarrow> \<exists> rs. rec_calc_rel (Pr n f g) (lm @ [x]) rs"
       
   345 apply(erule calc_pr_reverse, simp, simp)
       
   346 apply(rule_tac x = rk in exI, simp)
       
   347 done
       
   348 
       
   349 lemma [simp]: "Suc x \<le> y \<Longrightarrow> Suc (y - Suc x) = y - x"
       
   350 by arith
       
   351 
       
   352 lemma calc_pr_para_not_null: 
       
   353   "rec_calc_rel (Pr n f g) lm rs \<Longrightarrow> lm \<noteq> []"
       
   354 apply(erule calc_pr_reverse, simp, simp)
       
   355 done
       
   356 
       
   357 lemma calc_pr_less_ex: 
       
   358  "\<lbrakk>rec_calc_rel (Pr n f g) lm rs; x \<le> last lm\<rbrakk> \<Longrightarrow> 
       
   359  \<exists>rs. rec_calc_rel (Pr n f g) (butlast lm @ [last lm - x]) rs"
       
   360 apply(subgoal_tac "lm \<noteq> []")
       
   361 apply(induct x, rule_tac x = rs in exI, simp, simp, erule exE)
       
   362 apply(rule_tac rs = xa in calc_rel_reverse_ind_step_ex, simp)
       
   363 apply(simp add: calc_pr_para_not_null)
       
   364 done
       
   365 
       
   366 lemma calc_pr_zero_ex:
       
   367   "rec_calc_rel (Pr n f g) lm rs \<Longrightarrow> 
       
   368              \<exists>rs. rec_calc_rel f (butlast lm) rs"
       
   369 apply(drule_tac x = "last lm" in calc_pr_less_ex, simp,
       
   370       erule_tac exE, simp)
       
   371 apply(erule_tac calc_pr_reverse, simp)
       
   372 apply(rule_tac x = rs in exI, simp, simp)
       
   373 done
       
   374 
       
   375 
       
   376 lemma abc_steps_ind: 
       
   377   "abc_steps_l (as, am) ap (Suc stp) =
       
   378           abc_steps_l (abc_steps_l (as, am) ap stp) ap (Suc 0)"
       
   379 apply(insert abc_steps_add[of as am ap stp "Suc 0"], simp)
       
   380 done
       
   381 
       
   382 lemma abc_steps_zero: "abc_steps_l asm ap 0 = asm"
       
   383 apply(case_tac asm, simp add: abc_steps_l.simps)
       
   384 done
       
   385 
       
   386 lemma abc_append_nth: 
       
   387   "n < length ap + length bp \<Longrightarrow> 
       
   388        (ap [+] bp) ! n =
       
   389          (if n < length ap then ap ! n 
       
   390           else abc_inst_shift (bp ! (n - length ap)) (length ap))"
       
   391 apply(simp add: abc_append.simps nth_append map_nth split: if_splits)
       
   392 done
       
   393 
       
   394 lemma abc_state_keep:  
       
   395   "as \<ge> length bp \<Longrightarrow> abc_steps_l (as, lm) bp stp = (as, lm)"
       
   396 apply(induct stp, simp add: abc_steps_zero)
       
   397 apply(simp add: abc_steps_ind)
       
   398 apply(simp add: abc_steps_zero)
       
   399 apply(simp add: abc_steps_l.simps abc_fetch.simps abc_step_l.simps)
       
   400 done
       
   401 
       
   402 lemma abc_halt_equal: 
       
   403   "\<lbrakk>abc_steps_l (0, lm) bp stpa = (length bp, lm1); 
       
   404     abc_steps_l (0, lm) bp stpb = (length bp, lm2)\<rbrakk> \<Longrightarrow> lm1 = lm2"
       
   405 apply(case_tac "stpa - stpb > 0")
       
   406 apply(insert abc_steps_add[of 0 lm bp stpb "stpa - stpb"], simp)
       
   407 apply(insert abc_state_keep[of bp "length bp" lm2 "stpa - stpb"], 
       
   408       simp, simp add: abc_steps_zero)
       
   409 apply(insert abc_steps_add[of 0 lm bp stpa "stpb - stpa"], simp)
       
   410 apply(insert abc_state_keep[of bp "length bp" lm1 "stpb - stpa"], 
       
   411       simp)
       
   412 done  
       
   413 
       
   414 lemma abc_halt_point_ex: 
       
   415   "\<lbrakk>\<exists>stp. abc_steps_l (0, lm) bp stp = (bs, lm');
       
   416     bs = length bp; bp \<noteq> []\<rbrakk> 
       
   417   \<Longrightarrow> \<exists> stp. (\<lambda> (s, l). s < bs \<and> 
       
   418               (abc_steps_l (s, l) bp (Suc 0)) = (bs, lm')) 
       
   419       (abc_steps_l (0, lm) bp stp) "
       
   420 apply(erule_tac exE)
       
   421 proof -
       
   422   fix stp
       
   423   assume "bs = length bp" 
       
   424          "abc_steps_l (0, lm) bp stp = (bs, lm')" 
       
   425          "bp \<noteq> []"
       
   426   thus 
       
   427     "\<exists>stp. (\<lambda>(s, l). s < bs \<and> 
       
   428       abc_steps_l (s, l) bp (Suc 0) = (bs, lm')) 
       
   429                        (abc_steps_l (0, lm) bp stp)"
       
   430     apply(induct stp, simp add: abc_steps_zero, simp)
       
   431   proof -
       
   432     fix stpa
       
   433     assume ind: 
       
   434      "abc_steps_l (0, lm) bp stpa = (length bp, lm')
       
   435        \<Longrightarrow> \<exists>stp. (\<lambda>(s, l). s < length bp  \<and> abc_steps_l (s, l) bp 
       
   436              (Suc 0) = (length bp, lm')) (abc_steps_l (0, lm) bp stp)"
       
   437     and h: "abc_steps_l (0, lm) bp (Suc stpa) = (length bp, lm')" 
       
   438            "abc_steps_l (0, lm) bp stp = (length bp, lm')" 
       
   439            "bp \<noteq> []"
       
   440     from h show 
       
   441       "\<exists>stp. (\<lambda>(s, l). s < length bp \<and> abc_steps_l (s, l) bp (Suc 0)
       
   442                     = (length bp, lm')) (abc_steps_l (0, lm) bp stp)"
       
   443       apply(case_tac "abc_steps_l (0, lm) bp stpa", 
       
   444             case_tac "a = length bp")
       
   445       apply(insert ind, simp)
       
   446       apply(subgoal_tac "b = lm'", simp)
       
   447       apply(rule_tac abc_halt_equal, simp, simp)
       
   448       apply(rule_tac x = stpa in exI, simp add: abc_steps_ind)
       
   449       apply(simp add: abc_steps_zero)
       
   450       apply(rule classical, simp add: abc_steps_l.simps 
       
   451                              abc_fetch.simps abc_step_l.simps)
       
   452       done
       
   453   qed
       
   454 qed  
       
   455 
       
   456 
       
   457 lemma abc_append_empty_r[simp]: "[] [+] ab = ab"
       
   458 apply(simp add: abc_append.simps abc_inst_shift.simps)
       
   459 apply(induct ab, simp, simp)
       
   460 apply(case_tac a, simp_all add: abc_inst_shift.simps)
       
   461 done
       
   462 
       
   463 lemma abc_append_empty_l[simp]:  "ab [+] [] = ab"
       
   464 apply(simp add: abc_append.simps abc_inst_shift.simps)
       
   465 done
       
   466 
       
   467 
       
   468 lemma abc_append_length[simp]:  
       
   469   "length (ap [+] bp) = length ap + length bp"
       
   470 apply(simp add: abc_append.simps)
       
   471 done
       
   472 
       
   473 declare Let_def[simp]
       
   474 
       
   475 lemma abc_append_commute: "as [+] bs [+] cs = as [+] (bs [+] cs)"
       
   476 apply(simp add: abc_append.simps abc_shift.simps abc_inst_shift.simps)
       
   477 apply(induct cs, simp, simp)
       
   478 apply(case_tac a, auto simp: abc_inst_shift.simps Let_def)
       
   479 done
       
   480 
       
   481 lemma abc_halt_point_step[simp]: 
       
   482   "\<lbrakk>a < length bp; abc_steps_l (a, b) bp (Suc 0) = (length bp, lm')\<rbrakk>
       
   483   \<Longrightarrow> abc_steps_l (length ap + a, b) (ap [+] bp [+] cp) (Suc 0) = 
       
   484                                         (length ap + length bp, lm')"
       
   485 apply(simp add: abc_steps_l.simps abc_fetch.simps abc_append_nth)
       
   486 apply(case_tac "bp ! a", 
       
   487                       auto simp: abc_steps_l.simps abc_step_l.simps)
       
   488 done
       
   489 
       
   490 lemma abc_step_state_in:
       
   491   "\<lbrakk>bs < length bp;  abc_steps_l (a, b) bp (Suc 0) = (bs, l)\<rbrakk>
       
   492   \<Longrightarrow> a < length bp"
       
   493 apply(simp add: abc_steps_l.simps abc_fetch.simps)
       
   494 apply(rule_tac classical, 
       
   495       simp add: abc_step_l.simps abc_steps_l.simps)
       
   496 done
       
   497 
       
   498 
       
   499 lemma abc_append_state_in_exc: 
       
   500   "\<lbrakk>bs < length bp; abc_steps_l (0, lm) bp stpa = (bs, l)\<rbrakk>
       
   501  \<Longrightarrow> abc_steps_l (length ap, lm) (ap [+] bp [+] cp) stpa = 
       
   502                                              (length ap + bs, l)"
       
   503 apply(induct stpa arbitrary: bs l, simp add: abc_steps_zero)
       
   504 proof -
       
   505   fix stpa bs l
       
   506   assume ind: 
       
   507     "\<And>bs l. \<lbrakk>bs < length bp; abc_steps_l (0, lm) bp stpa = (bs, l)\<rbrakk>
       
   508     \<Longrightarrow> abc_steps_l (length ap, lm) (ap [+] bp [+] cp) stpa = 
       
   509                                                 (length ap + bs, l)"
       
   510     and h: "bs < length bp" 
       
   511            "abc_steps_l (0, lm) bp (Suc stpa) = (bs, l)"
       
   512   from h show 
       
   513     "abc_steps_l (length ap, lm) (ap [+] bp [+] cp) (Suc stpa) = 
       
   514                                                 (length ap + bs, l)"
       
   515     apply(simp add: abc_steps_ind)
       
   516     apply(case_tac "(abc_steps_l (0, lm) bp stpa)", simp)
       
   517   proof -
       
   518     fix a b
       
   519     assume g: "abc_steps_l (0, lm) bp stpa = (a, b)" 
       
   520               "abc_steps_l (a, b) bp (Suc 0) = (bs, l)"
       
   521     from h and g have k1: "a < length bp"
       
   522       apply(simp add: abc_step_state_in)
       
   523       done
       
   524     from h and g and k1 show 
       
   525    "abc_steps_l (abc_steps_l (length ap, lm) (ap [+] bp [+] cp) stpa) 
       
   526               (ap [+] bp [+] cp) (Suc 0) = (length ap + bs, l)"
       
   527       apply(insert ind[of a b], simp)
       
   528       apply(simp add: abc_steps_l.simps abc_fetch.simps 
       
   529                       abc_append_nth)
       
   530       apply(case_tac "bp ! a", auto simp: 
       
   531                                  abc_steps_l.simps abc_step_l.simps)
       
   532       done
       
   533   qed
       
   534 qed
       
   535 
       
   536 lemma [simp]: "abc_steps_l (0, am) [] stp = (0, am)"
       
   537 apply(induct stp, simp add: abc_steps_zero)
       
   538 apply(simp add: abc_steps_ind)
       
   539 apply(simp add: abc_steps_zero abc_steps_l.simps 
       
   540                 abc_fetch.simps abc_step_l.simps)
       
   541 done
       
   542 
       
   543 lemma abc_append_exc1:
       
   544   "\<lbrakk>\<exists> stp. abc_steps_l (0, lm) bp stp = (bs, lm');
       
   545     bs = length bp; 
       
   546     as = length ap\<rbrakk>
       
   547     \<Longrightarrow> \<exists> stp. abc_steps_l (as, lm) (ap [+] bp [+] cp) stp 
       
   548                                                  = (as + bs, lm')"
       
   549 apply(case_tac "bp = []", erule_tac exE, simp,
       
   550       rule_tac x = 0 in exI, simp add: abc_steps_zero)
       
   551 apply(frule_tac abc_halt_point_ex, simp, simp,
       
   552       erule_tac exE, erule_tac exE) 
       
   553 apply(rule_tac x = "stpa + Suc 0" in exI)
       
   554 apply(case_tac "(abc_steps_l (0, lm) bp stpa)", 
       
   555       simp add: abc_steps_ind)
       
   556 apply(subgoal_tac 
       
   557   "abc_steps_l (length ap, lm) (ap [+] bp [+] cp) stpa 
       
   558                                    = (length ap + a, b)", simp)
       
   559 apply(simp add: abc_steps_zero)
       
   560 apply(rule_tac abc_append_state_in_exc, simp, simp)
       
   561 done
       
   562 
       
   563 lemma abc_append_exc3: 
       
   564   "\<lbrakk>\<exists> stp. abc_steps_l (0, am) bp stp = (bs, bm); ss = length ap\<rbrakk>
       
   565    \<Longrightarrow>  \<exists> stp. abc_steps_l (ss, am) (ap [+] bp) stp = (bs + ss, bm)"
       
   566 apply(erule_tac exE)
       
   567 proof -
       
   568   fix stp
       
   569   assume h: "abc_steps_l (0, am) bp stp = (bs, bm)" "ss = length ap"
       
   570   thus " \<exists>stp. abc_steps_l (ss, am) (ap [+] bp) stp = (bs + ss, bm)"
       
   571   proof(induct stp arbitrary: bs bm)
       
   572     fix bs bm
       
   573     assume "abc_steps_l (0, am) bp 0 = (bs, bm)"
       
   574     thus "\<exists>stp. abc_steps_l (ss, am) (ap [+] bp) stp = (bs + ss, bm)"
       
   575       apply(rule_tac x = 0 in exI, simp add: abc_steps_l.simps)
       
   576       done
       
   577   next
       
   578     fix stp bs bm
       
   579     assume ind: 
       
   580       "\<And>bs bm. \<lbrakk>abc_steps_l (0, am) bp stp = (bs, bm);
       
   581                  ss = length ap\<rbrakk> \<Longrightarrow> 
       
   582           \<exists>stp. abc_steps_l (ss, am) (ap [+] bp) stp = (bs + ss, bm)"
       
   583     and g: "abc_steps_l (0, am) bp (Suc stp) = (bs, bm)"
       
   584     from g show 
       
   585       "\<exists>stp. abc_steps_l (ss, am) (ap [+] bp) stp = (bs + ss, bm)"
       
   586       apply(insert abc_steps_add[of 0 am bp stp "Suc 0"], simp)
       
   587       apply(case_tac "(abc_steps_l (0, am) bp stp)", simp)
       
   588     proof -
       
   589       fix a b
       
   590       assume "(bs, bm) = abc_steps_l (a, b) bp (Suc 0)" 
       
   591              "abc_steps_l (0, am) bp (Suc stp) = 
       
   592                        abc_steps_l (a, b) bp (Suc 0)" 
       
   593               "abc_steps_l (0, am) bp stp = (a, b)"
       
   594       thus "?thesis"
       
   595 	apply(insert ind[of a b], simp add: h, erule_tac exE)
       
   596 	apply(rule_tac x = "Suc stp" in exI)
       
   597 	apply(simp only: abc_steps_ind, simp add: abc_steps_zero)
       
   598       proof -
       
   599 	fix stp
       
   600 	assume "(bs, bm) = abc_steps_l (a, b) bp (Suc 0)"
       
   601 	thus "abc_steps_l (a + length ap, b) (ap [+] bp) (Suc 0)
       
   602                                               = (bs + length ap, bm)"
       
   603 	  apply(simp add: abc_steps_l.simps abc_steps_zero
       
   604                           abc_fetch.simps split: if_splits)
       
   605 	  apply(case_tac "bp ! a", 
       
   606                 simp_all add: abc_inst_shift.simps abc_append_nth
       
   607                    abc_steps_l.simps abc_steps_zero abc_step_l.simps)
       
   608 	  apply(auto)
       
   609 	  done
       
   610       qed
       
   611     qed
       
   612   qed
       
   613 qed
       
   614 
       
   615 lemma abc_add_equal:
       
   616   "\<lbrakk>ap \<noteq> []; 
       
   617     abc_steps_l (0, am) ap astp = (a, b);
       
   618     a < length ap\<rbrakk>
       
   619      \<Longrightarrow> (abc_steps_l (0, am) (ap @ bp) astp) = (a, b)"
       
   620 apply(induct astp arbitrary: a b, simp add: abc_steps_l.simps, simp)
       
   621 apply(simp add: abc_steps_ind)
       
   622 apply(case_tac "(abc_steps_l (0, am) ap astp)")
       
   623 proof -
       
   624   fix astp a b aa ba
       
   625   assume ind: 
       
   626     "\<And>a b. \<lbrakk>abc_steps_l (0, am) ap astp = (a, b); 
       
   627              a < length ap\<rbrakk> \<Longrightarrow> 
       
   628                   abc_steps_l (0, am) (ap @ bp) astp = (a, b)"
       
   629   and h: "abc_steps_l (abc_steps_l (0, am) ap astp) ap (Suc 0)
       
   630                                                             = (a, b)"
       
   631         "a < length ap" 
       
   632         "abc_steps_l (0, am) ap astp = (aa, ba)"
       
   633   from h show "abc_steps_l (abc_steps_l (0, am) (ap @ bp) astp)
       
   634                                         (ap @ bp) (Suc 0) = (a, b)"
       
   635     apply(insert ind[of aa ba], simp)
       
   636     apply(subgoal_tac "aa < length ap", simp)
       
   637     apply(simp add: abc_steps_l.simps abc_fetch.simps
       
   638                      nth_append abc_steps_zero)
       
   639     apply(rule abc_step_state_in, auto)
       
   640     done
       
   641 qed
       
   642 
       
   643 
       
   644 lemma abc_add_exc1: 
       
   645   "\<lbrakk>\<exists> astp. abc_steps_l (0, am) ap astp = (as, bm); as = length ap\<rbrakk>
       
   646   \<Longrightarrow> \<exists> stp. abc_steps_l (0, am) (ap @ bp) stp = (as, bm)"
       
   647 apply(case_tac "ap = []", simp, 
       
   648       rule_tac x = 0 in exI, simp add: abc_steps_zero)
       
   649 apply(drule_tac abc_halt_point_ex, simp, simp)
       
   650 apply(erule_tac exE, case_tac "(abc_steps_l (0, am) ap astp)", simp)
       
   651 apply(rule_tac x = "Suc astp" in exI, simp add: abc_steps_ind, auto)
       
   652 apply(frule_tac bp = bp in abc_add_equal, simp, simp, simp)
       
   653 apply(simp add: abc_steps_l.simps abc_steps_zero 
       
   654                 abc_fetch.simps nth_append)
       
   655 done
       
   656 
       
   657 declare abc_shift.simps[simp del] 
       
   658 
       
   659 lemma abc_append_exc2: 
       
   660   "\<lbrakk>\<exists> astp. abc_steps_l (0, am) ap astp = (as, bm); as = length ap; 
       
   661     \<exists> bstp. abc_steps_l (0, bm) bp bstp = (bs, bm'); bs = length bp;
       
   662     cs = as + bs; bp \<noteq> []\<rbrakk>
       
   663   \<Longrightarrow> \<exists> stp. abc_steps_l (0, am) (ap [+] bp) stp = (cs, bm')"
       
   664 apply(insert abc_append_exc1[of bm bp bs bm' as ap "[]"], simp)
       
   665 apply(drule_tac bp = "abc_shift bp (length ap)" in abc_add_exc1, simp)
       
   666 apply(subgoal_tac "ap @ abc_shift bp (length ap) = ap [+] bp", 
       
   667       simp, auto)
       
   668 apply(rule_tac x = "stpa + stp" in exI, simp add: abc_steps_add)
       
   669 apply(simp add: abc_append.simps)
       
   670 done
       
   671 lemma exponent_add_iff: "a\<up>b @ a\<up>c@ xs = a\<up>(b+c) @ xs"
       
   672 apply(auto simp: replicate_add)
       
   673 done
       
   674 
       
   675 lemma exponent_cons_iff: "a # a\<up>c @ xs = a\<up>(Suc c) @ xs"
       
   676 apply(auto simp: replicate_add)
       
   677 done
       
   678 
       
   679 lemma  [simp]: "length lm = n \<Longrightarrow>  
       
   680   abc_steps_l (Suc 0, lm @ Suc x # 0 # suf_lm) 
       
   681        [Inc n, Dec (Suc n) 3, Goto (Suc 0)] (Suc (Suc 0))
       
   682                                   = (3, lm @ Suc x # 0 # suf_lm)"
       
   683 apply(simp add: abc_steps_l.simps abc_fetch.simps 
       
   684                 abc_step_l.simps abc_lm_v.simps abc_lm_s.simps 
       
   685                 nth_append list_update_append)
       
   686 done
       
   687 
       
   688 lemma [simp]: 
       
   689   "length lm = n \<Longrightarrow> 
       
   690   abc_steps_l (Suc 0, lm @ Suc x # Suc y # suf_lm) 
       
   691      [Inc n, Dec (Suc n) 3, Goto (Suc 0)] (Suc (Suc 0))
       
   692   = (Suc 0, lm @ Suc x # y # suf_lm)"
       
   693 apply(simp add: abc_steps_l.simps abc_fetch.simps 
       
   694                 abc_step_l.simps abc_lm_v.simps abc_lm_s.simps 
       
   695                 nth_append list_update_append)
       
   696 done
       
   697 
       
   698 lemma pr_cycle_part_middle_inv: 
       
   699   "\<lbrakk>length lm = n\<rbrakk> \<Longrightarrow> 
       
   700   \<exists> stp. abc_steps_l (0, lm @ x # y # suf_lm) 
       
   701                          [Inc n, Dec (Suc n) 3, Goto (Suc 0)] stp 
       
   702   = (3, lm @ Suc x # 0 # suf_lm)"
       
   703 proof -
       
   704   assume h: "length lm = n"
       
   705   hence k1: "\<exists> stp. abc_steps_l (0, lm @ x # y # suf_lm) 
       
   706                            [Inc n, Dec (Suc n) 3, Goto (Suc 0)] stp 
       
   707     = (Suc 0, lm @ Suc x # y # suf_lm)"
       
   708     apply(rule_tac x = "Suc 0" in exI)
       
   709     apply(simp add: abc_steps_l.simps abc_step_l.simps 
       
   710                     abc_lm_v.simps abc_lm_s.simps nth_append 
       
   711                     list_update_append abc_fetch.simps)
       
   712     done
       
   713   from h have k2: 
       
   714     "\<exists> stp. abc_steps_l (Suc 0, lm @ Suc x # y # suf_lm)
       
   715                       [Inc n, Dec (Suc n) 3, Goto (Suc 0)] stp 
       
   716     = (3, lm @ Suc x # 0 # suf_lm)"
       
   717     apply(induct y)
       
   718     apply(rule_tac x = "Suc (Suc 0)" in exI, simp, simp, 
       
   719           erule_tac exE)
       
   720     apply(rule_tac x = "Suc (Suc 0) + stp" in exI, 
       
   721           simp only: abc_steps_add, simp)
       
   722     done      
       
   723   from k1 and k2 show 
       
   724     "\<exists> stp. abc_steps_l (0, lm @ x # y # suf_lm) 
       
   725                        [Inc n, Dec (Suc n) 3, Goto (Suc 0)] stp 
       
   726     = (3, lm @ Suc x # 0 # suf_lm)"
       
   727     apply(erule_tac exE, erule_tac exE)
       
   728     apply(rule_tac x = "stp + stpa" in exI, simp add: abc_steps_add)
       
   729     done
       
   730 qed
       
   731 
       
   732 lemma [simp]: 
       
   733   "length lm = Suc n \<Longrightarrow> 
       
   734   (abc_steps_l (length ap, lm @ x # Suc y # suf_lm) 
       
   735            (ap @ [Dec (Suc (Suc n)) 0, Inc (Suc n), Goto (length ap)]) 
       
   736                     (Suc (Suc (Suc 0))))
       
   737   = (length ap, lm @ Suc x # y # suf_lm)"
       
   738 apply(simp add: abc_steps_l.simps abc_fetch.simps abc_step_l.simps 
       
   739          abc_lm_v.simps list_update_append nth_append abc_lm_s.simps)
       
   740 done
       
   741 
       
   742 lemma switch_para_inv:
       
   743   assumes bp_def:"bp =  ap @ [Dec (Suc (Suc n)) 0, Inc (Suc n), Goto ss]"
       
   744   and h: "rec_ci (Pr n f g) = (aprog, rs_pos, a_md)" 
       
   745          "ss = length ap" 
       
   746          "length lm = Suc n"
       
   747   shows " \<exists>stp. abc_steps_l (ss, lm @ x # y # suf_lm) bp stp =
       
   748                                (0, lm @ (x + y) # 0 # suf_lm)"
       
   749 apply(induct y arbitrary: x)
       
   750 apply(rule_tac x = "Suc 0" in exI,
       
   751   simp add: bp_def mv_box.simps abc_steps_l.simps 
       
   752             abc_fetch.simps h abc_step_l.simps 
       
   753             abc_lm_v.simps list_update_append nth_append
       
   754             abc_lm_s.simps)
       
   755 proof -
       
   756   fix y x
       
   757   assume ind: 
       
   758     "\<And>x. \<exists>stp. abc_steps_l (ss, lm @ x # y # suf_lm) bp stp = 
       
   759                                      (0, lm @ (x + y) # 0 # suf_lm)"
       
   760   show "\<exists>stp. abc_steps_l (ss, lm @ x # Suc y # suf_lm) bp stp = 
       
   761                                   (0, lm @ (x + Suc y) # 0 # suf_lm)"
       
   762     apply(insert ind[of "Suc x"], erule_tac exE)
       
   763     apply(rule_tac x = "Suc (Suc (Suc 0)) + stp" in exI, 
       
   764           simp only: abc_steps_add bp_def h)
       
   765     apply(simp add: h)
       
   766     done
       
   767 qed
       
   768 
       
   769 lemma [simp]:
       
   770   "length lm = rs_pos \<and> Suc (Suc rs_pos) < a_md \<and> 0 < rs_pos \<Longrightarrow> 
       
   771       a_md - Suc 0 < Suc (Suc (Suc (a_md + length suf_lm - 
       
   772                                          Suc (Suc (Suc 0)))))"
       
   773 apply(arith)
       
   774 done
       
   775 
       
   776 lemma [simp]: 
       
   777   "Suc (Suc rs_pos) < a_md \<and> 0 < rs_pos \<Longrightarrow> 
       
   778                            \<not> a_md - Suc 0 < rs_pos - Suc 0"
       
   779 apply(arith)
       
   780 done
       
   781 
       
   782 lemma [simp]: 
       
   783   "Suc (Suc rs_pos) < a_md \<and> 0 < rs_pos \<Longrightarrow> 
       
   784            \<not> a_md - rs_pos < Suc (Suc (a_md - Suc (Suc rs_pos)))"
       
   785 apply(arith)
       
   786 done
       
   787 
       
   788 lemma butlast_append_last: "lm \<noteq> [] \<Longrightarrow> lm = butlast lm @ [last lm]"
       
   789 apply(auto)
       
   790 done
       
   791 
       
   792 lemma [simp]: "rec_ci (Pr n f g) = (aprog, rs_pos, a_md)
       
   793            \<Longrightarrow> (Suc (Suc rs_pos)) < a_md"
       
   794 apply(simp add: rec_ci.simps)
       
   795 apply(case_tac "rec_ci f", simp)
       
   796 apply(case_tac "rec_ci g", simp)
       
   797 apply(arith)
       
   798 done
       
   799 
       
   800 (*
       
   801 lemma pr_para_ge_suc0: "rec_calc_rel (Pr n f g) lm xs \<Longrightarrow> 0 < n"
       
   802 apply(erule calc_pr_reverse, simp, simp)
       
   803 done
       
   804 *)
       
   805 
       
   806 lemma ci_pr_para_eq: "rec_ci (Pr n f g) = (aprog, rs_pos, a_md)
       
   807                   \<Longrightarrow> rs_pos = Suc n"
       
   808 apply(simp add: rec_ci.simps)
       
   809 apply(case_tac "rec_ci g",  case_tac "rec_ci f", simp)
       
   810 done
       
   811 
       
   812 lemma [intro]:  
       
   813   "\<lbrakk>rec_ci z = (aprog, rs_pos, a_md); rec_calc_rel z lm xs\<rbrakk>
       
   814   \<Longrightarrow> length lm = rs_pos"
       
   815 apply(simp add: rec_ci.simps rec_ci_z_def)
       
   816 apply(erule_tac calc_z_reverse, simp)
       
   817 done
       
   818 
       
   819 lemma [intro]: 
       
   820   "\<lbrakk>rec_ci s = (aprog, rs_pos, a_md); rec_calc_rel s lm xs\<rbrakk>
       
   821   \<Longrightarrow> length lm = rs_pos"
       
   822 apply(simp add: rec_ci.simps rec_ci_s_def)
       
   823 apply(erule_tac calc_s_reverse, simp)
       
   824 done
       
   825 
       
   826 lemma [intro]: 
       
   827   "\<lbrakk>rec_ci (recf.id nat1 nat2) = (aprog, rs_pos, a_md); 
       
   828     rec_calc_rel (recf.id nat1 nat2) lm xs\<rbrakk> \<Longrightarrow> length lm = rs_pos"
       
   829 apply(simp add: rec_ci.simps rec_ci_id.simps)
       
   830 apply(erule_tac calc_id_reverse, simp)
       
   831 done
       
   832 
       
   833 lemma [intro]: 
       
   834   "\<lbrakk>rec_ci (Cn n f gs) = (aprog, rs_pos, a_md); 
       
   835     rec_calc_rel (Cn n f gs) lm xs\<rbrakk> \<Longrightarrow> length lm = rs_pos"
       
   836 apply(erule_tac calc_cn_reverse, simp)
       
   837 apply(simp add: rec_ci.simps)
       
   838 apply(case_tac "rec_ci f",  simp)
       
   839 done
       
   840 
       
   841 lemma [intro]:
       
   842   "\<lbrakk>rec_ci (Pr n f g) = (aprog, rs_pos, a_md); 
       
   843     rec_calc_rel (Pr n f g) lm xs\<rbrakk> \<Longrightarrow> length lm = rs_pos"
       
   844 apply(erule_tac  calc_pr_reverse, simp)
       
   845 apply(drule_tac ci_pr_para_eq, simp, simp)
       
   846 apply(drule_tac ci_pr_para_eq, simp)
       
   847 done
       
   848 
       
   849 lemma [intro]: 
       
   850   "\<lbrakk>rec_ci (Mn n f) = (aprog, rs_pos, a_md);
       
   851     rec_calc_rel (Mn n f) lm xs\<rbrakk> \<Longrightarrow> length lm = rs_pos"
       
   852 apply(erule_tac calc_mn_reverse)
       
   853 apply(simp add: rec_ci.simps)
       
   854 apply(case_tac "rec_ci f",  simp)
       
   855 done
       
   856 
       
   857 lemma para_pattern: 
       
   858   "\<lbrakk>rec_ci f = (aprog, rs_pos, a_md); rec_calc_rel f lm xs\<rbrakk>
       
   859   \<Longrightarrow> length lm = rs_pos"
       
   860 apply(case_tac f, auto)
       
   861 done
       
   862 
       
   863 lemma ci_pr_g_paras:
       
   864   "\<lbrakk>rec_ci (Pr n f g) = (aprog, rs_pos, a_md);
       
   865     rec_ci g = (a, aa, ba);
       
   866     rec_calc_rel (Pr n f g) (lm @ [x]) rs; x > 0\<rbrakk> \<Longrightarrow> 
       
   867     aa = Suc rs_pos "
       
   868 apply(erule calc_pr_reverse, simp)
       
   869 apply(subgoal_tac "length (args @ [k, rk]) = aa", simp)
       
   870 apply(subgoal_tac "rs_pos = Suc n", simp)
       
   871 apply(simp add: ci_pr_para_eq)
       
   872 apply(erule para_pattern, simp)
       
   873 done
       
   874 
       
   875 lemma ci_pr_g_md_less: 
       
   876   "\<lbrakk>rec_ci (Pr n f g) = (aprog, rs_pos, a_md); 
       
   877     rec_ci g = (a, aa, ba)\<rbrakk> \<Longrightarrow> ba < a_md"
       
   878 apply(simp add: rec_ci.simps)
       
   879 apply(case_tac "rec_ci f",  auto)
       
   880 done
       
   881 
       
   882 lemma [intro]: "rec_ci z = (ap, rp, ad) \<Longrightarrow> rp < ad"
       
   883   by(simp add: rec_ci.simps)
       
   884 
       
   885 lemma [intro]: "rec_ci s = (ap, rp, ad) \<Longrightarrow> rp < ad"
       
   886   by(simp add: rec_ci.simps)
       
   887 
       
   888 lemma [intro]: "rec_ci (recf.id nat1 nat2) = (ap, rp, ad) \<Longrightarrow> rp < ad"
       
   889   by(simp add: rec_ci.simps)
       
   890 
       
   891 lemma [intro]: "rec_ci (Cn n f gs) = (ap, rp, ad) \<Longrightarrow> rp < ad"
       
   892 apply(simp add: rec_ci.simps)
       
   893 apply(case_tac "rec_ci f",  simp)
       
   894 done
       
   895 
       
   896 lemma [intro]: "rec_ci (Pr n f g) = (ap, rp, ad) \<Longrightarrow> rp < ad"
       
   897 apply(simp add: rec_ci.simps)
       
   898 by(case_tac "rec_ci f", case_tac "rec_ci g",  auto)
       
   899 
       
   900 lemma [intro]: "rec_ci (Mn n f) = (ap, rp, ad) \<Longrightarrow> rp < ad"
       
   901 apply(simp add: rec_ci.simps)
       
   902 apply(case_tac "rec_ci f", simp)
       
   903 apply(arith)
       
   904 done
       
   905 
       
   906 lemma ci_ad_ge_paras: "rec_ci f = (ap, rp, ad) \<Longrightarrow> ad > rp"
       
   907 apply(case_tac f, auto)
       
   908 done
       
   909 
       
   910 lemma [elim]: "\<lbrakk>a [+] b = []; a \<noteq> [] \<or> b \<noteq> []\<rbrakk> \<Longrightarrow> RR"
       
   911 apply(auto simp: abc_append.simps abc_shift.simps)
       
   912 done
       
   913 
       
   914 lemma [intro]: "rec_ci z = ([], aa, ba) \<Longrightarrow> False"
       
   915 by(simp add: rec_ci.simps rec_ci_z_def)
       
   916 
       
   917 lemma [intro]: "rec_ci s = ([], aa, ba) \<Longrightarrow> False"
       
   918 by(auto simp: rec_ci.simps rec_ci_s_def addition.simps)
       
   919 
       
   920 lemma [intro]: "rec_ci (id m n) = ([], aa, ba) \<Longrightarrow> False"
       
   921 by(auto simp: rec_ci.simps rec_ci_id.simps addition.simps)
       
   922 
       
   923 lemma [intro]: "rec_ci (Cn n f gs) = ([], aa, ba) \<Longrightarrow> False"
       
   924 apply(case_tac "rec_ci f", auto simp: rec_ci.simps abc_append.simps)
       
   925 apply(simp add: abc_shift.simps mv_box.simps)
       
   926 done
       
   927 
       
   928 lemma [intro]: "rec_ci (Pr n f g) = ([], aa, ba) \<Longrightarrow> False"
       
   929 apply(simp add: rec_ci.simps)
       
   930 apply(case_tac "rec_ci f", case_tac "rec_ci g")
       
   931 by(auto)
       
   932 
       
   933 lemma [intro]: "rec_ci (Mn n f) = ([], aa, ba) \<Longrightarrow> False"
       
   934 apply(case_tac "rec_ci f", auto simp: rec_ci.simps)
       
   935 done
       
   936 
       
   937 lemma rec_ci_not_null:  "rec_ci g = (a, aa, ba) \<Longrightarrow> a \<noteq> []"
       
   938 by(case_tac g, auto)
       
   939 
       
   940 lemma calc_pr_g_def:
       
   941  "\<lbrakk>rec_calc_rel (Pr rs_pos f g) (lm @ [Suc x]) rsa;
       
   942    rec_calc_rel (Pr rs_pos f g) (lm @ [x]) rsxa\<rbrakk>
       
   943  \<Longrightarrow> rec_calc_rel g (lm @ [x, rsxa]) rsa"
       
   944 apply(erule_tac calc_pr_reverse, simp, simp)
       
   945 apply(subgoal_tac "rsxa = rk", simp)
       
   946 apply(erule_tac rec_calc_inj, auto)
       
   947 done
       
   948 
       
   949 lemma ci_pr_md_def: 
       
   950   "\<lbrakk>rec_ci (Pr n f g) = (aprog, rs_pos, a_md);
       
   951     rec_ci g = (a, aa, ba); rec_ci f = (ab, ac, bc)\<rbrakk>
       
   952   \<Longrightarrow> a_md = Suc (max (n + 3) (max bc ba))"
       
   953 by(simp add: rec_ci.simps)
       
   954 
       
   955 lemma  ci_pr_f_paras: 
       
   956   "\<lbrakk>rec_ci (Pr n f g) = (aprog, rs_pos, a_md);
       
   957     rec_calc_rel (Pr n f g) lm rs;
       
   958     rec_ci f = (ab, ac, bc)\<rbrakk>  \<Longrightarrow> ac = rs_pos - Suc 0"
       
   959 apply(subgoal_tac "\<exists>rs. rec_calc_rel f (butlast lm) rs", 
       
   960       erule_tac exE)
       
   961 apply(drule_tac f = f and lm = "butlast lm" in para_pattern, 
       
   962       simp, simp)
       
   963 apply(drule_tac para_pattern, simp)
       
   964 apply(subgoal_tac "lm \<noteq> []", simp)
       
   965 apply(erule_tac calc_pr_reverse, simp, simp)
       
   966 apply(erule calc_pr_zero_ex)
       
   967 done
       
   968 
       
   969 lemma ci_pr_md_ge_f:  "\<lbrakk>rec_ci (Pr n f g) = (aprog, rs_pos, a_md);
       
   970         rec_ci f = (ab, ac, bc)\<rbrakk> \<Longrightarrow> Suc bc \<le> a_md"
       
   971 apply(case_tac "rec_ci g")
       
   972 apply(simp add: rec_ci.simps, auto)
       
   973 done
       
   974 
       
   975 lemma ci_pr_md_ge_g:  "\<lbrakk>rec_ci (Pr n f g) = (aprog, rs_pos, a_md);
       
   976         rec_ci g = (ab, ac, bc)\<rbrakk> \<Longrightarrow> bc < a_md"
       
   977 apply(case_tac "rec_ci f")
       
   978 apply(simp add: rec_ci.simps, auto)
       
   979 done 
       
   980 
       
   981 lemma rec_calc_rel_def0: 
       
   982   "\<lbrakk>rec_calc_rel (Pr n f g) lm rs; rec_calc_rel f (butlast lm) rsa\<rbrakk>
       
   983   \<Longrightarrow> rec_calc_rel (Pr n f g) (butlast lm @ [0]) rsa"
       
   984   apply(rule_tac calc_pr_zero, simp)
       
   985 apply(erule_tac calc_pr_reverse, simp, simp, simp)
       
   986 done
       
   987 
       
   988 lemma [simp]:  "length (mv_box m n) = 3"
       
   989 by (auto simp: mv_box.simps)
       
   990 (*
       
   991 lemma
       
   992   "\<lbrakk>rec_ci (Pr n f g) = (aprog, rs_pos, a_md);
       
   993   rec_calc_rel (Pr n f g) lm rs;
       
   994   rec_ci g = (a, aa, ba);
       
   995   rec_ci f = (ab, ac, bc)\<rbrakk>
       
   996 \<Longrightarrow> \<exists>ap bp cp. aprog = ap [+] bp [+] cp \<and> length ap = 3 + length ab \<and> bp = recursive.mv_box (n - Suc 0) n 3"
       
   997 apply(simp add: rec_ci.simps)
       
   998 apply(rule_tac x = "recursive.mv_box (n - Suc 0) (max (Suc (Suc n)) (max bc ba)) 3 [+] ab" in exI, simp)
       
   999 apply(rule_tac x = "([Dec (max (Suc (Suc n)) (max bc ba)) (length a + 7)] [+] a [+] 
       
  1000   [Inc (n - Suc 0), Dec n 3, Goto (Suc 0)]) @ [Dec (Suc n) 0, Inc n, Goto (length a + 4)]" in exI, simp)
       
  1001 apply(auto simp: abc_append_commute)
       
  1002 done
       
  1003 
       
  1004 lemma  "\<lbrakk>rec_ci (Pr n f g) = (aprog, rs_pos, a_md);
       
  1005         rec_ci g = (a, aa, ba); rec_ci f = (ab, ac, bc)\<rbrakk>
       
  1006     \<Longrightarrow> \<exists>ap bp cp. aprog = ap [+] bp [+] cp \<and> length ap = 3 \<and> bp = ab"
       
  1007 apply(simp add: rec_ci.simps)
       
  1008 apply(rule_tac x = "recursive.mv_box (n - Suc 0) (max (Suc (Suc n)) (max bc ba)) 3" in exI, simp)
       
  1009 apply(rule_tac x = "recursive.mv_box (n - Suc 0) n 3 [+]
       
  1010      ([Dec (max (Suc (Suc n)) (max bc ba)) (length a + 7)] [+] a 
       
  1011   [+] [Inc (n - Suc 0), Dec n 3, Goto (Suc 0)]) @ [Dec (Suc n) 0, Inc n, Goto (length a + 4)]" in exI, auto)
       
  1012 apply(simp add: abc_append_commute)
       
  1013 done
       
  1014 *)
       
  1015 
       
  1016 lemma [simp]: "\<lbrakk>rec_ci (Pr n f g) = (aprog, rs_pos, a_md); rec_calc_rel (Pr n f g) lm rs\<rbrakk>
       
  1017     \<Longrightarrow> rs_pos = Suc n"
       
  1018 apply(simp add: ci_pr_para_eq)
       
  1019 done
       
  1020 
       
  1021 
       
  1022 lemma [simp]: "\<lbrakk>rec_ci (Pr n f g) = (aprog, rs_pos, a_md); rec_calc_rel (Pr n f g) lm rs\<rbrakk>
       
  1023     \<Longrightarrow> length lm = Suc n"
       
  1024 apply(subgoal_tac "rs_pos = Suc n", rule_tac para_pattern, simp, simp)
       
  1025 apply(case_tac "rec_ci f", case_tac "rec_ci g", simp add: rec_ci.simps)
       
  1026 done
       
  1027 
       
  1028 lemma [simp]: "rec_ci (Pr n f g) = (a, rs_pos, a_md) \<Longrightarrow> Suc (Suc n) < a_md"
       
  1029 apply(case_tac "rec_ci f", case_tac "rec_ci g", simp add: rec_ci.simps)
       
  1030 apply arith
       
  1031 done
       
  1032 
       
  1033 lemma [simp]: "rec_ci (Pr n f g) = (aprog, rs_pos, a_md) \<Longrightarrow> 0 < rs_pos"
       
  1034 apply(case_tac "rec_ci f", case_tac "rec_ci g")
       
  1035 apply(simp add: rec_ci.simps)
       
  1036 done
       
  1037 
       
  1038 lemma [simp]: "Suc (Suc rs_pos) < a_md \<Longrightarrow> 
       
  1039        butlast lm @ (last lm - xa) # (rsa::nat) # 0 # 0\<up>(a_md - Suc (Suc (Suc rs_pos))) @ xa # suf_lm =
       
  1040        butlast lm @ (last lm - xa) # rsa # 0\<up>(a_md - Suc (Suc rs_pos)) @ xa # suf_lm"
       
  1041 apply(simp add: replicate_Suc[THEN sym])
       
  1042 done
       
  1043 
       
  1044 lemma pr_cycle_part_ind: 
       
  1045   assumes g_ind: 
       
  1046   "\<And>lm rs suf_lm. rec_calc_rel g lm rs \<Longrightarrow> 
       
  1047   \<exists>stp. abc_steps_l (0, lm @ 0\<up>(ba - aa) @ suf_lm) a stp = 
       
  1048                     (length a, lm @ rs # 0\<up>(ba - Suc aa) @ suf_lm)"
       
  1049   and ap_def: 
       
  1050   "ap = ([Dec (a_md - Suc 0) (length a + 7)] [+]
       
  1051         (a [+] [Inc (rs_pos - Suc 0), Dec rs_pos 3, Goto (Suc 0)])) @
       
  1052          [Dec (Suc (Suc n)) 0, Inc (Suc n), Goto (length a + 4)]"
       
  1053   and h: "rec_ci (Pr n f g) = (aprog, rs_pos, a_md)" 
       
  1054          "rec_calc_rel (Pr n f g) 
       
  1055                    (butlast lm @ [last lm - Suc xa]) rsxa" 
       
  1056          "Suc xa \<le> last lm" 
       
  1057          "rec_ci g = (a, aa, ba)"
       
  1058          "rec_calc_rel (Pr n f g) (butlast lm @ [last lm - xa]) rsa"
       
  1059          "lm \<noteq> []"
       
  1060   shows 
       
  1061   "\<exists>stp. abc_steps_l 
       
  1062      (0, butlast lm @ (last lm - Suc xa) # rsxa # 
       
  1063                0\<up>(a_md - Suc (Suc rs_pos)) @ Suc xa # suf_lm) ap stp =
       
  1064      (0, butlast lm @ (last lm - xa) # rsa
       
  1065                  # 0\<up>(a_md - Suc (Suc rs_pos)) @ xa # suf_lm)"
       
  1066 proof -
       
  1067   have k1: "\<exists>stp. abc_steps_l (0, butlast lm @ (last lm - Suc xa) #
       
  1068     rsxa # 0\<up>(a_md - Suc (Suc rs_pos)) @ Suc xa # suf_lm) ap stp =
       
  1069          (length a + 4, butlast lm @ (last lm - xa) # 0 # rsa #
       
  1070                            0\<up>(a_md - Suc (Suc (Suc rs_pos))) @ xa # suf_lm)"
       
  1071     apply(simp add: ap_def, rule_tac abc_add_exc1)
       
  1072     apply(rule_tac as = "Suc 0" and 
       
  1073       bm = "butlast lm @ (last lm - Suc xa) # 
       
  1074       rsxa # 0\<up>(a_md - Suc (Suc rs_pos)) @ xa # suf_lm" in abc_append_exc2,
       
  1075       auto)
       
  1076   proof -
       
  1077     show 
       
  1078       "\<exists>astp. abc_steps_l (0, butlast lm @ (last lm - Suc xa) # rsxa 
       
  1079                    # 0\<up>(a_md - Suc (Suc rs_pos)) @ Suc xa # suf_lm) 
       
  1080               [Dec (a_md - Suc 0)(length a + 7)] astp =
       
  1081       (Suc 0, butlast lm @ (last lm - Suc xa) # 
       
  1082              rsxa # 0\<up>(a_md - Suc (Suc rs_pos)) @ xa # suf_lm)"
       
  1083       apply(rule_tac x = "Suc 0" in exI, 
       
  1084           simp add: abc_steps_l.simps abc_step_l.simps
       
  1085                      abc_fetch.simps)
       
  1086       apply(subgoal_tac "length lm = Suc n \<and> rs_pos = Suc n \<and>
       
  1087                               a_md > Suc (Suc rs_pos)")
       
  1088       apply(simp add: abc_lm_v.simps nth_append abc_lm_s.simps)
       
  1089       apply(insert nth_append[of 
       
  1090                  "(last lm - Suc xa) # rsxa # 0\<up>(a_md - Suc (Suc rs_pos))" 
       
  1091                  "Suc xa # suf_lm" "(a_md - rs_pos)"], simp)
       
  1092       apply(simp add: list_update_append del: list_update.simps)
       
  1093       apply(insert list_update_append[of "(last lm - Suc xa) # rsxa # 
       
  1094                                           0\<up>(a_md - Suc (Suc rs_pos))" 
       
  1095                     "Suc xa # suf_lm" "a_md - rs_pos" "xa"], simp)
       
  1096       apply(case_tac a_md, simp, simp)
       
  1097       apply(insert h, simp)
       
  1098       apply(insert para_pattern[of "Pr n f g" aprog rs_pos a_md 
       
  1099                     "(butlast lm @ [last lm - Suc xa])" rsxa], simp)
       
  1100       done
       
  1101   next
       
  1102     show "\<exists>bstp. abc_steps_l (0, butlast lm @ (last lm - Suc xa) # 
       
  1103            rsxa # 0\<up>(a_md - Suc (Suc rs_pos)) @ xa # suf_lm) (a [+] 
       
  1104             [Inc (rs_pos - Suc 0), Dec rs_pos 3, Goto (Suc 0)]) bstp =
       
  1105          (3 + length a, butlast lm @ (last lm - xa) # 0 # rsa #
       
  1106                           0\<up>(a_md - Suc (Suc (Suc rs_pos))) @ xa # suf_lm)"
       
  1107       apply(rule_tac as = "length a" and
       
  1108                bm = "butlast lm @ (last lm - Suc xa) # rsxa # rsa #
       
  1109                      0\<up>(a_md - Suc (Suc (Suc rs_pos))) @ xa # suf_lm" 
       
  1110         in abc_append_exc2, simp_all)
       
  1111     proof -
       
  1112       from h have j1: "aa = Suc rs_pos \<and> a_md > ba \<and> ba > Suc rs_pos"
       
  1113 	apply(insert h)
       
  1114 	apply(insert ci_pr_g_paras[of n f g aprog rs_pos
       
  1115                  a_md a aa ba "butlast lm" "last lm - xa" rsa], simp)
       
  1116 	apply(drule_tac ci_pr_md_ge_g, auto)
       
  1117 	apply(erule_tac ci_ad_ge_paras)
       
  1118 	done
       
  1119       from h have j2: "rec_calc_rel g (butlast lm @ 
       
  1120                                   [last lm - Suc xa, rsxa]) rsa"
       
  1121 	apply(rule_tac  calc_pr_g_def, simp, simp)
       
  1122 	done
       
  1123       from j1 and j2 show 
       
  1124         "\<exists>astp. abc_steps_l (0, butlast lm @ (last lm - Suc xa) #
       
  1125                 rsxa # 0\<up>(a_md - Suc (Suc rs_pos)) @ xa # suf_lm) a astp =
       
  1126         (length a, butlast lm @ (last lm - Suc xa) # rsxa # rsa 
       
  1127                          # 0\<up>(a_md - Suc (Suc (Suc rs_pos))) @ xa # suf_lm)"
       
  1128 	apply(insert g_ind[of
       
  1129           "butlast lm @ (last lm - Suc xa) # [rsxa]" rsa 
       
  1130           "0\<up>(a_md - ba - Suc 0) @ xa # suf_lm"], simp, auto)
       
  1131 	apply(simp add: exponent_add_iff)
       
  1132 	apply(rule_tac x = stp in exI, simp add: numeral_3_eq_3)
       
  1133 	done
       
  1134     next
       
  1135       from h have j3: "length lm = rs_pos \<and> rs_pos > 0"
       
  1136 	apply(rule_tac conjI)
       
  1137 	apply(drule_tac lm = "(butlast lm @ [last lm - Suc xa])"
       
  1138                           and xs = rsxa in para_pattern, simp, simp, simp)
       
  1139         done
       
  1140       from h have j4: "Suc (last lm - Suc xa) = last lm - xa"
       
  1141 	apply(case_tac "last lm", simp, simp)
       
  1142 	done
       
  1143       from j3 and j4 show
       
  1144       "\<exists>bstp. abc_steps_l (0, butlast lm @ (last lm - Suc xa) # rsxa #
       
  1145                      rsa # 0\<up>(a_md - Suc (Suc (Suc rs_pos))) @ xa # suf_lm)
       
  1146             [Inc (rs_pos - Suc 0), Dec rs_pos 3, Goto (Suc 0)] bstp =
       
  1147         (3, butlast lm @ (last lm - xa) # 0 # rsa #
       
  1148                        0\<up>(a_md - Suc (Suc (Suc rs_pos))) @ xa # suf_lm)"
       
  1149 	apply(insert pr_cycle_part_middle_inv[of "butlast lm" 
       
  1150           "rs_pos - Suc 0" "(last lm - Suc xa)" rsxa 
       
  1151           "rsa # 0\<up>(a_md - Suc (Suc (Suc rs_pos))) @ xa # suf_lm"], simp)
       
  1152 	done
       
  1153     qed
       
  1154   qed
       
  1155   from h have k2: 
       
  1156     "\<exists>stp. abc_steps_l (length a + 4, butlast lm @ (last lm - xa) # 0 
       
  1157            # rsa # 0\<up>(a_md - Suc (Suc (Suc rs_pos))) @ xa # suf_lm) ap stp =
       
  1158     (0, butlast lm @ (last lm - xa) # rsa # 0\<up>(a_md - Suc (Suc rs_pos)) @ xa # suf_lm)"
       
  1159     apply(insert switch_para_inv[of ap 
       
  1160       "([Dec (a_md - Suc 0) (length a + 7)] [+] 
       
  1161       (a [+] [Inc (rs_pos - Suc 0), Dec rs_pos 3, Goto (Suc 0)]))"
       
  1162       n "length a + 4" f g aprog rs_pos a_md 
       
  1163       "butlast lm @ [last lm - xa]" 0 rsa 
       
  1164       "0\<up>(a_md - Suc (Suc (Suc rs_pos))) @ xa # suf_lm"])
       
  1165     apply(simp add: h ap_def)
       
  1166     apply(subgoal_tac "length lm = Suc n \<and> Suc (Suc rs_pos) < a_md", 
       
  1167           simp)
       
  1168     apply(insert h, simp)
       
  1169     apply(frule_tac lm = "(butlast lm @ [last lm - Suc xa])" 
       
  1170       and xs = rsxa in para_pattern, simp, simp)
       
  1171     done   
       
  1172   from k1 and k2 show "?thesis"
       
  1173     apply(auto)
       
  1174     apply(rule_tac x = "stp + stpa" in exI, simp add: abc_steps_add)
       
  1175     done
       
  1176 qed
       
  1177 
       
  1178 lemma ci_pr_ex1: 
       
  1179   "\<lbrakk>rec_ci (Pr n f g) = (aprog, rs_pos, a_md);
       
  1180     rec_ci g = (a, aa, ba);
       
  1181     rec_ci f = (ab, ac, bc)\<rbrakk>
       
  1182 \<Longrightarrow> \<exists>ap bp. length ap = 6 + length ab \<and>
       
  1183     aprog = ap [+] bp \<and>
       
  1184     bp = ([Dec (a_md - Suc 0) (length a + 7)] [+] (a [+]
       
  1185          [Inc (rs_pos - Suc 0), Dec rs_pos 3, Goto (Suc 0)])) @ 
       
  1186          [Dec (Suc (Suc n)) 0, Inc (Suc n), Goto (length a + 4)]"
       
  1187 apply(simp add: rec_ci.simps)
       
  1188 apply(rule_tac x = "recursive.mv_box n (max (Suc (Suc (Suc n)))
       
  1189     (max bc ba)) [+] ab [+] recursive.mv_box n (Suc n)" in exI,
       
  1190      simp)
       
  1191 apply(auto simp add: abc_append_commute numeral_3_eq_3)
       
  1192 done
       
  1193 
       
  1194 lemma pr_cycle_part:
       
  1195   "\<lbrakk>\<And>lm rs suf_lm. rec_calc_rel g lm rs \<Longrightarrow>
       
  1196      \<exists>stp. abc_steps_l (0, lm @ 0\<up>(ba - aa) @ suf_lm) a stp = 
       
  1197                         (length a, lm @ rs # 0\<up>(ba - Suc aa) @ suf_lm);
       
  1198   rec_ci (Pr n f g) = (aprog, rs_pos, a_md); 
       
  1199   rec_calc_rel (Pr n f g) lm rs;
       
  1200   rec_ci g = (a, aa, ba);
       
  1201   rec_calc_rel (Pr n f g) (butlast lm @ [last lm - x]) rsx;
       
  1202   rec_ci f = (ab, ac, bc);
       
  1203   lm \<noteq> [];
       
  1204   x \<le> last lm\<rbrakk> \<Longrightarrow> 
       
  1205   \<exists>stp. abc_steps_l (6 + length ab, butlast lm @ (last lm - x) #
       
  1206               rsx # 0\<up>(a_md - Suc (Suc rs_pos)) @ x # suf_lm) aprog stp =
       
  1207   (6 + length ab, butlast lm @ last lm # rs #
       
  1208                                 0\<up>(a_md - Suc (Suc rs_pos)) @ 0 # suf_lm)"
       
  1209 proof -
       
  1210   assume g_ind:
       
  1211     "\<And>lm rs suf_lm. rec_calc_rel g lm rs \<Longrightarrow> 
       
  1212     \<exists>stp. abc_steps_l (0, lm @ 0\<up>(ba - aa) @ suf_lm) a stp =
       
  1213                       (length a, lm @ rs # 0\<up>(ba - Suc aa) @ suf_lm)"
       
  1214     and h: "rec_ci (Pr n f g) = (aprog, rs_pos, a_md)" 
       
  1215            "rec_calc_rel (Pr n f g) lm rs" 
       
  1216            "rec_ci g = (a, aa, ba)"
       
  1217            "rec_calc_rel (Pr n f g) (butlast lm @ [last lm - x]) rsx" 
       
  1218            "lm \<noteq> []"
       
  1219            "x \<le> last lm" 
       
  1220            "rec_ci f = (ab, ac, bc)" 
       
  1221   from h show 
       
  1222     "\<exists>stp. abc_steps_l (6 + length ab, butlast lm @ (last lm - x) # 
       
  1223             rsx # 0\<up>(a_md - Suc (Suc rs_pos)) @ x # suf_lm) aprog stp =
       
  1224     (6 + length ab, butlast lm @ last lm # rs #
       
  1225                                0\<up>(a_md - Suc (Suc rs_pos)) @ 0 # suf_lm)" 
       
  1226   proof(induct x arbitrary: rsx, simp_all)
       
  1227     fix rsxa
       
  1228     assume "rec_calc_rel (Pr n f g) lm rsxa" 
       
  1229            "rec_calc_rel (Pr n f g) lm rs"
       
  1230     from h and this have "rs = rsxa"
       
  1231       apply(subgoal_tac "lm \<noteq> [] \<and> rs_pos = Suc n", simp)
       
  1232       apply(rule_tac rec_calc_inj, simp, simp)
       
  1233       apply(simp)
       
  1234       done
       
  1235     thus "\<exists>stp. abc_steps_l (6 + length ab, butlast lm @  last lm # 
       
  1236              rsxa # 0\<up>(a_md - Suc (Suc rs_pos)) @ 0 # suf_lm) aprog stp =
       
  1237       (6 + length ab, butlast lm @ last lm # rs #
       
  1238                                0\<up>(a_md - Suc (Suc rs_pos)) @ 0 # suf_lm)"
       
  1239       by(rule_tac x = 0 in exI, simp add: abc_steps_l.simps)
       
  1240   next
       
  1241     fix xa rsxa
       
  1242     assume ind:
       
  1243    "\<And>rsx. rec_calc_rel (Pr n f g) (butlast lm @ [last lm - xa]) rsx 
       
  1244   \<Longrightarrow> \<exists>stp. abc_steps_l (6 + length ab, butlast lm @ (last lm - xa) #
       
  1245              rsx # 0\<up>(a_md - Suc (Suc rs_pos)) @ xa # suf_lm) aprog stp =
       
  1246       (6 + length ab, butlast lm @ last lm # rs # 
       
  1247                                0\<up>(a_md - Suc (Suc rs_pos)) @ 0 # suf_lm)"
       
  1248       and g: "rec_calc_rel (Pr n f g) 
       
  1249                       (butlast lm @ [last lm - Suc xa]) rsxa"
       
  1250       "Suc xa \<le> last lm"
       
  1251       "rec_ci (Pr n f g) = (aprog, rs_pos, a_md)" 
       
  1252       "rec_calc_rel (Pr n f g) lm rs"
       
  1253       "rec_ci g = (a, aa, ba)" 
       
  1254       "rec_ci f = (ab, ac, bc)" "lm \<noteq> []"
       
  1255     from g have k1: 
       
  1256       "\<exists> rs. rec_calc_rel (Pr n f g) (butlast lm @ [last lm - xa]) rs"
       
  1257       apply(rule_tac rs = rs in  calc_pr_less_ex, simp, simp)
       
  1258       done
       
  1259     from g and this show 
       
  1260       "\<exists>stp. abc_steps_l (6 + length ab, 
       
  1261            butlast lm @ (last lm - Suc xa) # rsxa # 
       
  1262               0\<up>(a_md - Suc (Suc rs_pos)) @ Suc xa # suf_lm) aprog stp =
       
  1263               (6 + length ab, butlast lm @ last lm # rs # 
       
  1264                                 0\<up>(a_md - Suc (Suc rs_pos)) @ 0 # suf_lm)"
       
  1265     proof(erule_tac exE)
       
  1266       fix rsa
       
  1267       assume k2: "rec_calc_rel (Pr n f g) 
       
  1268                            (butlast lm @ [last lm - xa]) rsa"
       
  1269       from g and k2 have
       
  1270       "\<exists>stp. abc_steps_l (6 + length ab, butlast lm @ 
       
  1271        (last lm - Suc xa) # rsxa # 
       
  1272                0\<up>(a_md - Suc (Suc rs_pos)) @ Suc xa # suf_lm) aprog stp
       
  1273         = (6 + length ab, butlast lm @ (last lm - xa) # rsa # 
       
  1274                                0\<up>(a_md - Suc (Suc rs_pos)) @ xa # suf_lm)"
       
  1275 	proof -
       
  1276 	  from g have k2_1: 
       
  1277             "\<exists> ap bp. length ap = 6 + length ab \<and>
       
  1278                    aprog = ap [+] bp \<and> 
       
  1279                    bp = ([Dec (a_md - Suc 0) (length a + 7)] [+]
       
  1280                   (a [+] [Inc (rs_pos - Suc 0), Dec rs_pos 3, 
       
  1281                   Goto (Suc 0)])) @
       
  1282                   [Dec (Suc (Suc n)) 0, Inc (Suc n), Goto (length a + 4)]"
       
  1283             apply(rule_tac ci_pr_ex1, auto)
       
  1284 	    done
       
  1285 	  from k2_1 and k2 and g show "?thesis"
       
  1286 	    proof(erule_tac exE, erule_tac exE)
       
  1287 	      fix ap bp
       
  1288 	      assume 
       
  1289                 "length ap = 6 + length ab \<and> 
       
  1290                  aprog = ap [+] bp \<and> bp =
       
  1291                 ([Dec (a_md - Suc 0) (length a + 7)] [+] 
       
  1292                 (a [+] [Inc (rs_pos - Suc 0), Dec rs_pos 3,
       
  1293                 Goto (Suc 0)])) @ 
       
  1294                 [Dec (Suc (Suc n)) 0, Inc (Suc n), Goto (length a + 4)]" 
       
  1295 	      from g and this and k2 and g_ind show "?thesis"
       
  1296 		apply(insert abc_append_exc3[of 
       
  1297                   "butlast lm @ (last lm - Suc xa) # rsxa #
       
  1298                   0\<up>(a_md - Suc (Suc rs_pos)) @ Suc xa # suf_lm" bp 0
       
  1299                   "butlast lm @ (last lm - xa) # rsa #
       
  1300                 0\<up>(a_md - Suc (Suc rs_pos)) @ xa # suf_lm" "length ap" ap],
       
  1301                  simp)
       
  1302 		apply(subgoal_tac 
       
  1303                 "\<exists>stp. abc_steps_l (0, butlast lm @ (last lm - Suc xa)
       
  1304                            # rsxa # 0\<up>(a_md - Suc (Suc rs_pos)) @ Suc xa # 
       
  1305                               suf_lm) bp stp =
       
  1306 	          (0, butlast lm @ (last lm - xa) # rsa #
       
  1307                            0\<up>(a_md - Suc (Suc rs_pos)) @ xa # suf_lm)",
       
  1308                       simp, erule_tac conjE, erule conjE)
       
  1309 		apply(erule pr_cycle_part_ind, auto)
       
  1310 		done
       
  1311 	    qed
       
  1312 	  qed  
       
  1313       from g and k2 and this show "?thesis"
       
  1314 	apply(erule_tac exE)
       
  1315 	apply(insert ind[of rsa], simp)
       
  1316 	apply(erule_tac exE)
       
  1317 	apply(rule_tac x = "stp + stpa" in exI, 
       
  1318               simp add: abc_steps_add)
       
  1319 	done
       
  1320     qed
       
  1321   qed
       
  1322 qed
       
  1323 
       
  1324 lemma ci_pr_length: 
       
  1325   "\<lbrakk>rec_ci (Pr n f g) = (aprog, rs_pos, a_md); 
       
  1326     rec_ci g = (a, aa, ba);  
       
  1327     rec_ci f = (ab, ac, bc)\<rbrakk>
       
  1328     \<Longrightarrow>  length aprog = 13 + length ab + length a"
       
  1329 apply(auto simp: rec_ci.simps)
       
  1330 done
       
  1331 
       
  1332 fun mv_box_inv :: "nat \<times> nat list \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat list \<Rightarrow> bool"
       
  1333   where
       
  1334   "mv_box_inv (as, lm) m n initlm = 
       
  1335          (let plus = initlm ! m + initlm ! n in
       
  1336            length initlm > max m n \<and> m \<noteq> n \<and> 
       
  1337               (if as = 0 then \<exists> k l. lm = initlm[m := k, n := l] \<and> 
       
  1338                     k + l = plus \<and> k \<le> initlm ! m 
       
  1339               else if as = 1 then \<exists> k l. lm = initlm[m := k, n := l]
       
  1340                              \<and> k + l + 1 = plus \<and> k < initlm ! m 
       
  1341               else if as = 2 then \<exists> k l. lm = initlm[m := k, n := l] 
       
  1342                               \<and> k + l = plus \<and> k \<le> initlm ! m
       
  1343               else if as = 3 then lm = initlm[m := 0, n := plus]
       
  1344               else False))"
       
  1345 
       
  1346 fun mv_box_stage1 :: "nat \<times> nat list \<Rightarrow> nat \<Rightarrow> nat"
       
  1347   where
       
  1348   "mv_box_stage1 (as, lm) m  = 
       
  1349             (if as = 3 then 0 
       
  1350              else 1)"
       
  1351 
       
  1352 fun mv_box_stage2 :: "nat \<times> nat list \<Rightarrow> nat \<Rightarrow> nat"
       
  1353   where
       
  1354   "mv_box_stage2 (as, lm) m = (lm ! m)"
       
  1355 
       
  1356 fun mv_box_stage3 :: "nat \<times> nat list \<Rightarrow> nat \<Rightarrow> nat"
       
  1357   where
       
  1358   "mv_box_stage3 (as, lm) m = (if as = 1 then 3 
       
  1359                                 else if as = 2 then 2
       
  1360                                 else if as = 0 then 1 
       
  1361                                 else 0)"
       
  1362  
       
  1363 fun mv_box_measure :: "((nat \<times> nat list) \<times> nat) \<Rightarrow> (nat \<times> nat \<times> nat)"
       
  1364   where
       
  1365   "mv_box_measure ((as, lm), m) = 
       
  1366      (mv_box_stage1 (as, lm) m, mv_box_stage2 (as, lm) m,
       
  1367       mv_box_stage3 (as, lm) m)"
       
  1368 
       
  1369 definition lex_pair :: "((nat \<times> nat) \<times> nat \<times> nat) set"
       
  1370   where
       
  1371   "lex_pair = less_than <*lex*> less_than"
       
  1372 
       
  1373 definition lex_triple :: 
       
  1374  "((nat \<times> (nat \<times> nat)) \<times> (nat \<times> (nat \<times> nat))) set"
       
  1375   where
       
  1376   "lex_triple \<equiv> less_than <*lex*> lex_pair"
       
  1377 
       
  1378 definition mv_box_LE :: 
       
  1379  "(((nat \<times> nat list) \<times> nat) \<times> ((nat \<times> nat list) \<times> nat)) set"
       
  1380   where 
       
  1381   "mv_box_LE \<equiv> (inv_image lex_triple mv_box_measure)"
       
  1382 
       
  1383 lemma wf_lex_triple: "wf lex_triple"
       
  1384   by (auto intro:wf_lex_prod simp:lex_triple_def lex_pair_def)
       
  1385 
       
  1386 lemma wf_mv_box_le[intro]: "wf mv_box_LE"
       
  1387 by(auto intro:wf_inv_image wf_lex_triple simp: mv_box_LE_def)
       
  1388 
       
  1389 declare mv_box_inv.simps[simp del]
       
  1390 
       
  1391 lemma mv_box_inv_init:  
       
  1392 "\<lbrakk>m < length initlm; n < length initlm; m \<noteq> n\<rbrakk> \<Longrightarrow> 
       
  1393   mv_box_inv (0, initlm) m n initlm"
       
  1394 apply(simp add: abc_steps_l.simps mv_box_inv.simps)
       
  1395 apply(rule_tac x = "initlm ! m" in exI, 
       
  1396       rule_tac x = "initlm ! n" in exI, simp)
       
  1397 done
       
  1398 
       
  1399 lemma [simp]: "abc_fetch 0 (recursive.mv_box m n) = Some (Dec m 3)"
       
  1400 apply(simp add: mv_box.simps abc_fetch.simps)
       
  1401 done
       
  1402 
       
  1403 lemma [simp]: "abc_fetch (Suc 0) (recursive.mv_box m n) =
       
  1404                Some (Inc n)"
       
  1405 apply(simp add: mv_box.simps abc_fetch.simps)
       
  1406 done
       
  1407 
       
  1408 lemma [simp]: "abc_fetch 2 (recursive.mv_box m n) = Some (Goto 0)"
       
  1409 apply(simp add: mv_box.simps abc_fetch.simps)
       
  1410 done
       
  1411 
       
  1412 lemma [simp]: "abc_fetch 3 (recursive.mv_box m n) = None"
       
  1413 apply(simp add: mv_box.simps abc_fetch.simps)
       
  1414 done
       
  1415 
       
  1416 lemma [simp]: 
       
  1417   "\<lbrakk>m \<noteq> n; m < length initlm; n < length initlm;
       
  1418     k + l = initlm ! m + initlm ! n; k \<le> initlm ! m; 0 < k\<rbrakk>
       
  1419  \<Longrightarrow> \<exists>ka la. initlm[m := k, n := l, m := k - Suc 0] = 
       
  1420      initlm[m := ka, n := la] \<and>
       
  1421      Suc (ka + la) = initlm ! m + initlm ! n \<and> 
       
  1422      ka < initlm ! m"
       
  1423 apply(rule_tac x = "k - Suc 0" in exI, rule_tac x = l in exI, 
       
  1424       simp, auto)
       
  1425 apply(subgoal_tac 
       
  1426       "initlm[m := k, n := l, m := k - Suc 0] = 
       
  1427        initlm[n := l, m := k, m := k - Suc 0]")
       
  1428 apply(simp add: list_update_overwrite )
       
  1429 apply(simp add: list_update_swap)
       
  1430 apply(simp add: list_update_swap)
       
  1431 done
       
  1432 
       
  1433 lemma [simp]:
       
  1434   "\<lbrakk>m \<noteq> n; m < length initlm; n < length initlm; 
       
  1435     Suc (k + l) = initlm ! m + initlm ! n;
       
  1436     k < initlm ! m\<rbrakk>
       
  1437     \<Longrightarrow> \<exists>ka la. initlm[m := k, n := l, n := Suc l] = 
       
  1438                 initlm[m := ka, n := la] \<and> 
       
  1439                 ka + la = initlm ! m + initlm ! n \<and> 
       
  1440                 ka \<le> initlm ! m"
       
  1441 apply(rule_tac x = k in exI, rule_tac x = "Suc l" in exI, auto)
       
  1442 done
       
  1443 
       
  1444 lemma [simp]: 
       
  1445   "\<lbrakk>length initlm > max m n; m \<noteq> n\<rbrakk> \<Longrightarrow> 
       
  1446    \<forall>na. \<not> (\<lambda>(as, lm) m. as = 3) 
       
  1447     (abc_steps_l (0, initlm) (recursive.mv_box m n) na) m \<and> 
       
  1448   mv_box_inv (abc_steps_l (0, initlm) 
       
  1449            (recursive.mv_box m n) na) m n initlm \<longrightarrow>
       
  1450   mv_box_inv (abc_steps_l (0, initlm) 
       
  1451            (recursive.mv_box m n) (Suc na)) m n initlm \<and>
       
  1452   ((abc_steps_l (0, initlm) (recursive.mv_box m n) (Suc na), m),
       
  1453    abc_steps_l (0, initlm) (recursive.mv_box m n) na, m) \<in> mv_box_LE"
       
  1454 apply(rule allI, rule impI, simp add: abc_steps_ind)
       
  1455 apply(case_tac "(abc_steps_l (0, initlm) (recursive.mv_box m n) na)",
       
  1456       simp)
       
  1457 apply(auto split:if_splits simp add:abc_steps_l.simps mv_box_inv.simps)
       
  1458 apply(auto simp add: mv_box_LE_def lex_triple_def lex_pair_def 
       
  1459                      abc_step_l.simps abc_steps_l.simps
       
  1460                      mv_box_inv.simps abc_lm_v.simps abc_lm_s.simps
       
  1461                 split: if_splits )
       
  1462 apply(rule_tac x = k in exI, rule_tac x = "Suc l" in exI, simp)
       
  1463 done
       
  1464 
       
  1465 lemma mv_box_inv_halt: 
       
  1466   "\<lbrakk>length initlm > max m n; m \<noteq> n\<rbrakk> \<Longrightarrow> 
       
  1467   \<exists> stp. (\<lambda> (as, lm). as = 3 \<and> 
       
  1468   mv_box_inv (as, lm) m n initlm) 
       
  1469              (abc_steps_l (0::nat, initlm) (mv_box m n) stp)"
       
  1470 thm halt_lemma2
       
  1471 apply(insert halt_lemma2[of mv_box_LE
       
  1472     "\<lambda> ((as, lm), m). mv_box_inv (as, lm) m n initlm"
       
  1473     "\<lambda> stp. (abc_steps_l (0, initlm) (recursive.mv_box m n) stp, m)"
       
  1474     "\<lambda> ((as, lm), m). as = (3::nat)"
       
  1475     ])
       
  1476 apply(insert wf_mv_box_le)
       
  1477 apply(simp add: mv_box_inv_init abc_steps_zero)
       
  1478 apply(erule_tac exE)
       
  1479 apply(rule_tac x = na in exI)
       
  1480 apply(case_tac "(abc_steps_l (0, initlm) (recursive.mv_box m n) na)",
       
  1481       simp, auto)
       
  1482 done
       
  1483 
       
  1484 lemma mv_box_halt_cond:
       
  1485   "\<lbrakk>m \<noteq> n; mv_box_inv (a, b) m n lm; a = 3\<rbrakk> \<Longrightarrow> 
       
  1486   b = lm[n := lm ! m + lm ! n, m := 0]"
       
  1487 apply(simp add: mv_box_inv.simps, auto)
       
  1488 apply(simp add: list_update_swap)
       
  1489 done
       
  1490 
       
  1491 lemma mv_box_ex:
       
  1492   "\<lbrakk>length lm > max m n; m \<noteq> n\<rbrakk> \<Longrightarrow> 
       
  1493   \<exists> stp. abc_steps_l (0::nat, lm) (mv_box m n) stp
       
  1494   = (3, (lm[n := (lm ! m + lm ! n)])[m := 0::nat])"
       
  1495 apply(drule mv_box_inv_halt, simp, erule_tac exE)
       
  1496 apply(rule_tac x = stp in exI)
       
  1497 apply(case_tac "abc_steps_l (0, lm) (recursive.mv_box m n) stp",
       
  1498       simp)
       
  1499 apply(erule_tac mv_box_halt_cond, auto)
       
  1500 done
       
  1501 
       
  1502 lemma [simp]: 
       
  1503   "\<lbrakk>a_md = Suc (max (Suc (Suc n)) (max bc ba)); 
       
  1504    length lm = rs_pos \<and> rs_pos = n \<and> n > 0\<rbrakk>
       
  1505   \<Longrightarrow> n - Suc 0 < length lm + 
       
  1506   (Suc (max (Suc (Suc n)) (max bc ba)) - rs_pos + length suf_lm) \<and>
       
  1507    Suc (Suc n) < length lm + (Suc (max (Suc (Suc n)) (max bc ba)) -
       
  1508   rs_pos + length suf_lm) \<and> bc < length lm + (Suc (max (Suc (Suc n)) 
       
  1509  (max bc ba)) - rs_pos + length suf_lm) \<and> ba < length lm + 
       
  1510   (Suc (max (Suc (Suc n)) (max bc ba)) - rs_pos + length suf_lm)"
       
  1511 apply(arith)
       
  1512 done
       
  1513 
       
  1514 lemma [simp]:
       
  1515   "\<lbrakk>a_md = Suc (max (Suc (Suc n)) (max bc ba)); 
       
  1516    length lm = rs_pos \<and> rs_pos = n \<and> n > 0\<rbrakk>
       
  1517  \<Longrightarrow> n - Suc 0 < Suc (length suf_lm + max (Suc (Suc n)) (max bc ba)) \<and>
       
  1518      Suc n < length suf_lm + max (Suc (Suc n)) (max bc ba) \<and> 
       
  1519      bc < Suc (length suf_lm + max (Suc (Suc n)) (max bc ba)) \<and> 
       
  1520      ba < Suc (length suf_lm + max (Suc (Suc n)) (max bc ba))"
       
  1521 apply(arith)
       
  1522 done
       
  1523 
       
  1524 lemma [simp]: "n - Suc 0 \<noteq> max (Suc (Suc n)) (max bc ba)"
       
  1525 apply(arith)
       
  1526 done
       
  1527 
       
  1528 lemma [simp]: 
       
  1529   "a_md \<ge> Suc bc \<and> rs_pos > 0 \<and> bc \<ge> rs_pos \<Longrightarrow> 
       
  1530  bc - (rs_pos - Suc 0) + a_md - Suc bc = Suc (a_md - rs_pos - Suc 0)"
       
  1531 apply(arith)
       
  1532 done
       
  1533 
       
  1534 lemma [simp]: "length lm = n \<and> rs_pos = n \<and> 0 < rs_pos \<and> 
       
  1535                                                   Suc rs_pos < a_md 
       
  1536        \<Longrightarrow> n - Suc 0 < Suc (Suc (a_md + length suf_lm - Suc (Suc 0))) 
       
  1537         \<and> n < Suc (Suc (a_md + length suf_lm - Suc (Suc 0)))"
       
  1538 apply(arith)
       
  1539 done
       
  1540      
       
  1541 lemma [simp]: "length lm = n \<and> rs_pos = n \<and> 0 < rs_pos \<and> 
       
  1542                Suc rs_pos < a_md \<Longrightarrow> n - Suc 0 \<noteq> n"
       
  1543 by arith
       
  1544 
       
  1545 lemma ci_pr_ex2: 
       
  1546   "\<lbrakk>rec_ci (Pr n f g) = (aprog, rs_pos, a_md);
       
  1547     rec_calc_rel (Pr n f g) lm rs; 
       
  1548     rec_ci g = (a, aa, ba); 
       
  1549     rec_ci f = (ab, ac, bc)\<rbrakk>
       
  1550   \<Longrightarrow> \<exists>ap bp. aprog = ap [+] bp \<and> 
       
  1551          ap = mv_box n (max (Suc (Suc (Suc n))) (max bc ba))"
       
  1552 apply(simp add: rec_ci.simps)
       
  1553 apply(rule_tac x = "(ab [+] (recursive.mv_box n (Suc n) [+]
       
  1554               ([Dec (max (n + 3) (max bc ba)) (length a + 7)] 
       
  1555       [+] (a [+] [Inc n, Dec (Suc n) 3, Goto (Suc 0)])) @ 
       
  1556       [Dec (Suc (Suc n)) 0, Inc (Suc n), Goto (length a + 4)]))" in exI, auto)
       
  1557 apply(simp add: abc_append_commute numeral_3_eq_3)
       
  1558 done
       
  1559 
       
  1560 lemma [simp]: 
       
  1561   "max (Suc (Suc (Suc n))) (max bc ba) - n < 
       
  1562      Suc (max (Suc (Suc (Suc n))) (max bc ba)) - n"
       
  1563 apply(arith)
       
  1564 done
       
  1565 
       
  1566 thm nth_replicate
       
  1567 (*
       
  1568 lemma exp_nth[simp]: "n < m \<Longrightarrow> a\<up>m ! n = a"
       
  1569 apply(sim)
       
  1570 done
       
  1571 *)
       
  1572 lemma [simp]: "length lm = n \<and> rs_pos = n \<and> 0 < n \<Longrightarrow> 
       
  1573                       lm[n - Suc 0 := 0::nat] = butlast lm @ [0]"
       
  1574 apply(auto)
       
  1575 apply(insert list_update_append[of "butlast lm" "[last lm]" 
       
  1576                                    "length lm - Suc 0" "0"], simp)
       
  1577 done
       
  1578 
       
  1579 lemma [simp]: "\<lbrakk>length lm = n; 0 < n\<rbrakk>  \<Longrightarrow> lm ! (n - Suc 0) = last lm"
       
  1580 apply(insert nth_append[of "butlast lm" "[last lm]" "n - Suc 0"],
       
  1581       simp)
       
  1582 apply(insert butlast_append_last[of lm], auto)
       
  1583 done
       
  1584 lemma exp_suc_iff: "a\<up>b @ [a] = a\<up>(b + Suc 0)"
       
  1585 apply(simp add: exp_ind del: replicate.simps)
       
  1586 done
       
  1587 
       
  1588 lemma less_not_less[simp]: "n > 0 \<Longrightarrow> \<not> n < n - Suc 0"
       
  1589 by auto
       
  1590 
       
  1591 lemma [simp]:
       
  1592   "Suc n < length suf_lm + max (Suc (Suc n)) (max bc ba) \<and> 
       
  1593   bc < Suc (length suf_lm + max (Suc (Suc n)) 
       
  1594   (max bc ba)) \<and> 
       
  1595   ba < Suc (length suf_lm + max (Suc (Suc n)) (max bc ba))"
       
  1596   by arith
       
  1597 
       
  1598 lemma [simp]: "length lm = n \<and> rs_pos = n \<and> n > 0 \<Longrightarrow> 
       
  1599 (lm @ 0\<up>(Suc (max (Suc (Suc n)) (max bc ba)) - n) @ suf_lm) 
       
  1600   [max (Suc (Suc n)) (max bc ba) :=
       
  1601    (lm @ 0\<up>(Suc (max (Suc (Suc n)) (max bc ba)) - n) @ suf_lm) ! (n - Suc 0) + 
       
  1602        (lm @ 0\<up>(Suc (max (Suc (Suc n)) (max bc ba)) - n) @ suf_lm) ! 
       
  1603                    max (Suc (Suc n)) (max bc ba), n - Suc 0 := 0::nat]
       
  1604  = butlast lm @ 0 # 0\<up>(max (Suc (Suc n)) (max bc ba) - n) @ last lm # suf_lm"
       
  1605 apply(simp add: nth_append nth_replicate list_update_append)
       
  1606 apply(insert list_update_append[of "0\<up>((max (Suc (Suc n)) (max bc ba)) - n)"
       
  1607          "[0]" "max (Suc (Suc n)) (max bc ba) - n" "last lm"], simp)
       
  1608 apply(simp add: exp_suc_iff Suc_diff_le del: list_update.simps)
       
  1609 done
       
  1610 
       
  1611 lemma exp_eq: "(a = b) = (c\<up>a = c\<up>b)"
       
  1612 apply(auto)
       
  1613 done
       
  1614 
       
  1615 lemma [simp]:
       
  1616   "\<lbrakk>length lm = n; 0 < n;  Suc n < a_md\<rbrakk> \<Longrightarrow> 
       
  1617    (butlast lm @ rsa # 0\<up>(a_md - Suc n) @ last lm # suf_lm)
       
  1618     [n := (butlast lm @ rsa # 0\<up>(a_md - Suc n) @ last lm # suf_lm) ! 
       
  1619         (n - Suc 0) + (butlast lm @ rsa # (0::nat)\<up>(a_md - Suc n) @ 
       
  1620                                 last lm # suf_lm) ! n, n - Suc 0 := 0]
       
  1621  = butlast lm @ 0 # rsa # 0\<up>(a_md - Suc (Suc n)) @ last lm # suf_lm"
       
  1622 apply(simp add: nth_append list_update_append)
       
  1623 apply(case_tac "a_md - Suc n", auto)
       
  1624 done
       
  1625 
       
  1626 lemma [simp]: 
       
  1627   "Suc (Suc rs_pos) \<le> a_md \<and> length lm = rs_pos \<and> 0 < rs_pos
       
  1628   \<Longrightarrow> a_md - Suc 0 < 
       
  1629           Suc (Suc (Suc (a_md + length suf_lm - Suc (Suc (Suc 0)))))"
       
  1630 by arith
       
  1631 
       
  1632 lemma [simp]: 
       
  1633   "Suc (Suc rs_pos) \<le> a_md \<and> length lm = rs_pos \<and> 0 < rs_pos \<Longrightarrow> 
       
  1634                                    \<not> a_md - Suc 0 < rs_pos - Suc 0"
       
  1635 by arith
       
  1636 
       
  1637 lemma [simp]: "Suc (Suc rs_pos) \<le> a_md \<Longrightarrow> 
       
  1638                                 \<not> a_md - Suc 0 < rs_pos - Suc 0"
       
  1639 by arith
       
  1640 
       
  1641 lemma [simp]: "\<lbrakk>Suc (Suc rs_pos) \<le> a_md\<rbrakk> \<Longrightarrow> 
       
  1642                \<not> a_md - rs_pos < Suc (Suc (a_md - Suc (Suc rs_pos)))"
       
  1643 by arith 
       
  1644 
       
  1645 lemma [simp]: 
       
  1646   "Suc (Suc rs_pos) \<le> a_md \<and> length lm = rs_pos \<and> 0 < rs_pos
       
  1647  \<Longrightarrow> (abc_lm_v (butlast lm @ last lm # rs # 0\<up>(a_md - Suc (Suc rs_pos)) @
       
  1648         0 # suf_lm) (a_md - Suc 0) = 0 \<longrightarrow>
       
  1649       abc_lm_s (butlast lm @ last lm # rs # 0\<up>(a_md - Suc (Suc rs_pos)) @ 
       
  1650         0 # suf_lm) (a_md - Suc 0) 0 = 
       
  1651          lm @ rs # 0\<up>(a_md - Suc rs_pos) @ suf_lm) \<and>
       
  1652      abc_lm_v (butlast lm @ last lm # rs # 0\<up>(a_md - Suc (Suc rs_pos)) @ 
       
  1653                0 # suf_lm) (a_md - Suc 0) = 0"
       
  1654 apply(simp add: abc_lm_v.simps nth_append abc_lm_s.simps)
       
  1655 apply(insert nth_append[of "last lm # rs # 0\<up>(a_md - Suc (Suc rs_pos))" 
       
  1656                "0 # suf_lm" "(a_md - rs_pos)"], auto)
       
  1657 apply(simp only: exp_suc_iff)
       
  1658 apply(subgoal_tac "a_md - Suc 0 < a_md + length suf_lm", simp)
       
  1659 apply(case_tac "lm = []", auto)
       
  1660 done
       
  1661 
       
  1662 lemma pr_prog_ex[simp]: "\<lbrakk>rec_ci (Pr n f g) = (aprog, rs_pos, a_md); 
       
  1663       rec_ci g = (a, aa, ba); rec_ci f = (ab, ac, bc)\<rbrakk>
       
  1664     \<Longrightarrow> \<exists>cp. aprog = recursive.mv_box n (max (n + 3) 
       
  1665                     (max bc ba)) [+] cp"
       
  1666 apply(simp add: rec_ci.simps)
       
  1667 apply(rule_tac x = "(ab [+] (recursive.mv_box n (Suc n) [+]
       
  1668               ([Dec (max (n + 3) (max bc ba)) (length a + 7)] 
       
  1669              [+] (a [+] [Inc n, Dec (Suc n) 3, Goto (Suc 0)]))
       
  1670              @ [Dec (Suc (Suc n)) 0, Inc (Suc n), Goto (length a + 4)]))" in exI)
       
  1671 apply(auto simp: abc_append_commute)
       
  1672 done
       
  1673 
       
  1674 lemma [simp]: "mv_box m n \<noteq> []"
       
  1675 by (simp add: mv_box.simps)
       
  1676 (*
       
  1677 lemma [simp]: "\<lbrakk>rs_pos = n; 0 < rs_pos ; Suc rs_pos < a_md\<rbrakk> \<Longrightarrow> 
       
  1678                         n - Suc 0 < a_md + length suf_lm"
       
  1679 by arith
       
  1680 *)
       
  1681 lemma [intro]: 
       
  1682   "\<lbrakk>rec_ci (Pr n f g) = (aprog, rs_pos, a_md); 
       
  1683     rec_ci f = (ab, ac, bc)\<rbrakk> \<Longrightarrow> 
       
  1684    \<exists>ap. (\<exists>cp. aprog = ap [+] ab [+] cp) \<and> length ap = 3"
       
  1685 apply(case_tac "rec_ci g", simp add: rec_ci.simps)
       
  1686 apply(rule_tac x = "mv_box n 
       
  1687               (max (n + 3) (max bc c))" in exI, simp)
       
  1688 apply(rule_tac x = "recursive.mv_box n (Suc n) [+]
       
  1689                  ([Dec (max (n + 3) (max bc c)) (length a + 7)]
       
  1690                  [+] a [+] [Inc n, Dec (Suc n) 3, Goto (Suc 0)])
       
  1691                @ [Dec (Suc (Suc n)) 0, Inc (Suc n), Goto (length a + 4)]" in exI, 
       
  1692       auto)
       
  1693 apply(simp add: abc_append_commute)
       
  1694 done
       
  1695 
       
  1696 lemma [intro]: 
       
  1697   "\<lbrakk>rec_ci (Pr n f g) = (aprog, rs_pos, a_md);
       
  1698     rec_ci g = (a, aa, ba); 
       
  1699     rec_ci f = (ab, ac, bc)\<rbrakk> \<Longrightarrow> 
       
  1700     \<exists>ap. (\<exists>cp. aprog = ap [+] recursive.mv_box n (Suc n) [+] cp)
       
  1701       \<and> length ap = 3 + length ab"
       
  1702 apply(simp add: rec_ci.simps)
       
  1703 apply(rule_tac x = "recursive.mv_box n (max (n + 3)
       
  1704                                 (max bc ba)) [+] ab" in exI, simp)
       
  1705 apply(rule_tac x = "([Dec (max (n + 3) (max bc ba))
       
  1706   (length a + 7)] [+] a [+] 
       
  1707   [Inc n, Dec (Suc n) 3, Goto (Suc 0)]) @ 
       
  1708   [Dec (Suc (Suc n)) 0, Inc (Suc n), Goto (length a + 4)]" in exI)
       
  1709 apply(auto simp: abc_append_commute)
       
  1710 done
       
  1711 
       
  1712 lemma [intro]: 
       
  1713   "\<lbrakk>rec_ci (Pr n f g) = (aprog, rs_pos, a_md);
       
  1714     rec_ci g = (a, aa, ba); 
       
  1715     rec_ci f = (ab, ac, bc)\<rbrakk>
       
  1716     \<Longrightarrow> \<exists>ap. (\<exists>cp. aprog = ap [+] ([Dec (a_md - Suc 0) (length a + 7)]
       
  1717              [+] (a [+] [Inc (rs_pos - Suc 0), Dec rs_pos 3, 
       
  1718              Goto (Suc 0)])) @ [Dec (Suc (Suc n)) 0, Inc (Suc n),
       
  1719              Goto (length a + 4)] [+] cp) \<and>
       
  1720              length ap = 6 + length ab"
       
  1721 apply(simp add: rec_ci.simps)
       
  1722 apply(rule_tac x = "recursive.mv_box n
       
  1723     (max (n + 3) (max bc ba)) [+] ab [+] 
       
  1724      recursive.mv_box n (Suc n)" in exI, simp)
       
  1725 apply(rule_tac x = "[]" in exI, auto)
       
  1726 apply(simp add: abc_append_commute)
       
  1727 done
       
  1728 
       
  1729 lemma [simp]: 
       
  1730   "n < Suc (max (n + 3) (max bc ba) + length suf_lm) \<and> 
       
  1731    Suc (Suc n) < max (n + 3) (max bc ba) + length suf_lm \<and> 
       
  1732    bc < Suc (max (n + 3) (max bc ba) + length suf_lm) \<and> 
       
  1733    ba < Suc (max (n + 3) (max bc ba) + length suf_lm)"
       
  1734 by arith
       
  1735 
       
  1736 lemma [simp]: "n \<noteq> max (n + (3::nat)) (max bc ba)"
       
  1737 by arith
       
  1738 
       
  1739 lemma [simp]:"length lm = Suc n \<Longrightarrow> lm[n := (0::nat)] = butlast lm @ [0]"
       
  1740 apply(subgoal_tac "\<exists> xs x. lm = xs @ [x]", auto simp: list_update_append)
       
  1741 apply(rule_tac x = "butlast lm" in exI, rule_tac x = "last lm" in exI)
       
  1742 apply(case_tac lm, auto)
       
  1743 done
       
  1744 
       
  1745 lemma [simp]:  "length lm = Suc n \<Longrightarrow> lm ! n =last lm"
       
  1746 apply(subgoal_tac "lm \<noteq> []")
       
  1747 apply(simp add: last_conv_nth, case_tac lm, simp_all)
       
  1748 done
       
  1749 
       
  1750 lemma [simp]: "length lm = Suc n \<Longrightarrow> 
       
  1751       (lm @ (0::nat)\<up>(max (n + 3) (max bc ba) - n) @ suf_lm)
       
  1752            [max (n + 3) (max bc ba) := (lm @ 0\<up>(max (n + 3) (max bc ba) - n) @ suf_lm) ! n + 
       
  1753                   (lm @ 0\<up>(max (n + 3) (max bc ba) - n) @ suf_lm) ! max (n + 3) (max bc ba), n := 0]
       
  1754        = butlast lm @ 0 # 0\<up>(max (n + 3) (max bc ba) - Suc n) @ last lm # suf_lm"
       
  1755 apply(auto simp: list_update_append nth_append)
       
  1756 apply(subgoal_tac "(0\<up>(max (n + 3) (max bc ba) - n)) = 0\<up>(max (n + 3) (max bc ba) - Suc n) @ [0::nat]")
       
  1757 apply(simp add: list_update_append)
       
  1758 apply(simp add: exp_suc_iff)
       
  1759 done
       
  1760 
       
  1761 lemma [simp]: "Suc (Suc n) < a_md \<Longrightarrow>  
       
  1762       n < Suc (Suc (a_md + length suf_lm - 2)) \<and>
       
  1763         n < Suc (a_md + length suf_lm - 2)"
       
  1764 by(arith)
       
  1765 
       
  1766 lemma [simp]: "\<lbrakk>length lm = Suc n; Suc (Suc n) < a_md\<rbrakk>
       
  1767         \<Longrightarrow>(butlast lm @ (rsa::nat) # 0\<up>(a_md - Suc (Suc n)) @ last lm # suf_lm)
       
  1768           [Suc n := (butlast lm @ rsa # 0\<up>(a_md - Suc (Suc n)) @ last lm # suf_lm) ! n +
       
  1769                   (butlast lm @ rsa # 0\<up>(a_md - Suc (Suc n)) @ last lm # suf_lm) ! Suc n, n := 0]
       
  1770     = butlast lm @ 0 # rsa # 0\<up>(a_md - Suc (Suc (Suc n))) @ last lm # suf_lm"
       
  1771 apply(auto simp: list_update_append)
       
  1772 apply(subgoal_tac "(0\<up>(a_md - Suc (Suc n))) = (0::nat) # (0\<up>(a_md - Suc (Suc (Suc n))))", simp add: nth_append)
       
  1773 apply(simp add: replicate_Suc[THEN sym])
       
  1774 done
       
  1775 
       
  1776 lemma pr_case:
       
  1777   assumes nf_ind:
       
  1778   "\<And> lm rs suf_lm. rec_calc_rel f lm rs \<Longrightarrow> 
       
  1779   \<exists>stp. abc_steps_l (0, lm @ 0\<up>(bc - ac) @ suf_lm) ab stp = 
       
  1780                 (length ab, lm @ rs # 0\<up>(bc - Suc ac) @ suf_lm)"
       
  1781   and ng_ind: "\<And> lm rs suf_lm. rec_calc_rel g lm rs \<Longrightarrow> 
       
  1782         \<exists>stp. abc_steps_l (0, lm @ 0\<up>(ba - aa) @ suf_lm) a stp = 
       
  1783                        (length a, lm @ rs # 0\<up>(ba - Suc aa) @ suf_lm)"
       
  1784     and h: "rec_ci (Pr n f g) = (aprog, rs_pos, a_md)"  "rec_calc_rel (Pr n f g) lm rs" 
       
  1785            "rec_ci g = (a, aa, ba)" "rec_ci f = (ab, ac, bc)" 
       
  1786   shows "\<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog stp = (length aprog, lm @ rs # 0\<up>(a_md - Suc rs_pos) @ suf_lm)"
       
  1787 proof -
       
  1788   from h have k1: "\<exists> stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog stp
       
  1789     = (3, butlast lm @ 0 # 0\<up>(a_md - rs_pos - 1) @ last lm # suf_lm)"
       
  1790   proof -
       
  1791     have "\<exists>bp cp. aprog = bp [+] cp \<and> bp = mv_box n 
       
  1792                  (max (n + 3) (max bc ba))"
       
  1793       apply(insert h, simp)
       
  1794       apply(erule pr_prog_ex, auto)
       
  1795       done
       
  1796     thus "?thesis"
       
  1797       apply(erule_tac exE, erule_tac exE, simp)
       
  1798       apply(subgoal_tac 
       
  1799            "\<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm)
       
  1800               ([] [+] recursive.mv_box n
       
  1801                   (max (n + 3) (max bc ba)) [+] cp) stp =
       
  1802              (0 + 3, butlast lm @ 0 # 0\<up>(a_md - Suc rs_pos) @ 
       
  1803                                         last lm # suf_lm)", simp)
       
  1804       apply(rule_tac abc_append_exc1, simp_all)
       
  1805       apply(insert mv_box_ex[of "n" "(max (n + 3) 
       
  1806                  (max bc ba))" "lm @ 0\<up>(a_md - rs_pos) @ suf_lm"], simp)
       
  1807       apply(subgoal_tac "a_md = Suc (max (n + 3) (max bc ba))",
       
  1808             simp)
       
  1809       apply(subgoal_tac "length lm = Suc n \<and> rs_pos = Suc n", simp)
       
  1810       apply(insert h)
       
  1811       apply(simp add: para_pattern ci_pr_para_eq)
       
  1812       apply(rule ci_pr_md_def, auto)
       
  1813       done
       
  1814   qed
       
  1815   from h have k2: 
       
  1816   "\<exists> stp. abc_steps_l (3,  butlast lm @ 0 # 0\<up>(a_md - rs_pos - 1) @ 
       
  1817              last lm # suf_lm) aprog stp 
       
  1818     = (length aprog, lm @ rs # 0\<up>(a_md - Suc rs_pos) @ suf_lm)"
       
  1819   proof -
       
  1820     from h have k2_1: "\<exists> rs. rec_calc_rel f (butlast lm) rs"
       
  1821       apply(erule_tac calc_pr_zero_ex)
       
  1822       done
       
  1823     thus "?thesis"
       
  1824     proof(erule_tac exE)
       
  1825       fix rsa
       
  1826       assume k2_2: "rec_calc_rel f (butlast lm) rsa"
       
  1827       from h and k2_2 have k2_2_1: 
       
  1828        "\<exists> stp. abc_steps_l (3, butlast lm @ 0 # 0\<up>(a_md - rs_pos - 1) 
       
  1829                  @ last lm # suf_lm) aprog stp
       
  1830         = (3 + length ab, butlast lm @ rsa # 0\<up>(a_md - rs_pos - 1) @ 
       
  1831                                              last lm # suf_lm)"
       
  1832       proof -
       
  1833 	from h have j1: "
       
  1834           \<exists> ap bp cp. aprog = ap [+] bp [+] cp \<and> length ap = 3 \<and> 
       
  1835               bp = ab"
       
  1836 	  apply(auto)
       
  1837 	  done
       
  1838 	from h have j2: "ac = rs_pos - 1"
       
  1839 	  apply(drule_tac ci_pr_f_paras, simp, auto)
       
  1840 	  done
       
  1841 	from h and j2 have j3: "a_md \<ge> Suc bc \<and> rs_pos > 0 \<and> bc \<ge> rs_pos"
       
  1842 	  apply(rule_tac conjI)
       
  1843 	  apply(erule_tac ab = ab and ac = ac in ci_pr_md_ge_f, simp)
       
  1844 	  apply(rule_tac context_conjI)
       
  1845           apply(simp_all add: rec_ci.simps)
       
  1846 	  apply(drule_tac ci_ad_ge_paras, drule_tac ci_ad_ge_paras)
       
  1847 	  apply(arith)
       
  1848 	  done	  
       
  1849 	from j1 and j2 show "?thesis"
       
  1850 	  apply(auto simp del: abc_append_commute)
       
  1851 	  apply(rule_tac abc_append_exc1, simp_all)
       
  1852 	  apply(insert nf_ind[of "butlast lm" "rsa" 
       
  1853                 "0\<up>(a_md - bc - Suc 0) @ last lm # suf_lm"], 
       
  1854                simp add: k2_2 j2, erule_tac exE)
       
  1855 	  apply(simp add: exponent_add_iff j3)
       
  1856 	  apply(rule_tac x = "stp" in exI, simp)
       
  1857 	  done
       
  1858       qed
       
  1859       from h have k2_2_2: 
       
  1860       "\<exists> stp. abc_steps_l (3 + length ab, butlast lm @ rsa # 
       
  1861                   0\<up>(a_md - rs_pos - 1) @ last lm # suf_lm) aprog stp
       
  1862         = (6 + length ab, butlast lm @ 0 # rsa # 
       
  1863                        0\<up>(a_md - rs_pos - 2) @ last lm # suf_lm)"
       
  1864       proof -	     
       
  1865 	from h have "\<exists> ap bp cp. aprog = ap [+] bp [+] cp \<and> 
       
  1866           length ap = 3 + length ab \<and> bp = recursive.mv_box n (Suc n)"
       
  1867 	  by auto
       
  1868 	thus "?thesis"
       
  1869 	proof(erule_tac exE, erule_tac exE, erule_tac exE, 
       
  1870               erule_tac exE)
       
  1871 	  fix ap cp bp apa
       
  1872 	  assume "aprog = ap [+] bp [+] cp \<and> length ap = 3 + 
       
  1873                     length ab \<and> bp = recursive.mv_box n (Suc n)"
       
  1874 	  thus "?thesis"
       
  1875 	    apply(simp del: abc_append_commute)
       
  1876 	    apply(subgoal_tac 
       
  1877               "\<exists>stp. abc_steps_l (3 + length ab, 
       
  1878                butlast lm @ rsa # 0\<up>(a_md - Suc rs_pos) @
       
  1879                  last lm # suf_lm) (ap [+] 
       
  1880                    recursive.mv_box n (Suc n) [+] cp) stp =
       
  1881               ((3 + length ab) + 3, butlast lm @ 0 # rsa # 
       
  1882                   0\<up>(a_md - Suc (Suc rs_pos)) @ last lm # suf_lm)", simp)
       
  1883 	    apply(rule_tac abc_append_exc1, simp_all)
       
  1884 	    apply(insert mv_box_ex[of n "Suc n" 
       
  1885                     "butlast lm @ rsa # 0\<up>(a_md - Suc rs_pos) @ 
       
  1886                           last lm # suf_lm"], simp)
       
  1887 	    apply(subgoal_tac "length lm = Suc n \<and> rs_pos = Suc n \<and> a_md > Suc (Suc n)", simp)
       
  1888 	    apply(insert h, simp)
       
  1889             done
       
  1890 	qed
       
  1891       qed
       
  1892       from h have k2_3: "lm \<noteq> []"
       
  1893 	apply(rule_tac calc_pr_para_not_null, simp)
       
  1894 	done
       
  1895       from h and k2_2 and k2_3 have k2_2_3: 
       
  1896       "\<exists> stp. abc_steps_l (6 + length ab, butlast lm @ 
       
  1897           (last lm - last lm) # rsa # 
       
  1898             0\<up>(a_md - (Suc (Suc rs_pos))) @ last lm # suf_lm) aprog stp
       
  1899         = (6 + length ab, butlast lm @ last lm # rs # 
       
  1900                         0\<up>(a_md - Suc (Suc (rs_pos))) @ 0 # suf_lm)"
       
  1901 	apply(rule_tac x = "last lm" and g = g in pr_cycle_part, auto)
       
  1902 	apply(rule_tac ng_ind, simp)
       
  1903 	apply(rule_tac rec_calc_rel_def0, simp, simp)
       
  1904 	done
       
  1905       from h  have k2_2_4: 
       
  1906        "\<exists> stp. abc_steps_l (6 + length ab,
       
  1907              butlast lm @ last lm # rs # 0\<up>(a_md - rs_pos - 2) @
       
  1908                   0 # suf_lm) aprog stp
       
  1909         = (13 + length ab + length a,
       
  1910                    lm @ rs # 0\<up>(a_md - rs_pos - 1) @ suf_lm)"
       
  1911       proof -
       
  1912 	from h have 
       
  1913         "\<exists> ap bp cp. aprog = ap [+] bp [+] cp \<and>
       
  1914                      length ap = 6 + length ab \<and> 
       
  1915                     bp = ([Dec (a_md - Suc 0) (length a + 7)] [+] 
       
  1916                          (a [+] [Inc (rs_pos - Suc 0), 
       
  1917                          Dec rs_pos 3, Goto (Suc 0)])) @ 
       
  1918                         [Dec (Suc (Suc n)) 0, Inc (Suc n), Goto (length a + 4)]"
       
  1919 	  by auto
       
  1920 	thus "?thesis"
       
  1921 	  apply(auto)
       
  1922 	  apply(subgoal_tac  
       
  1923             "\<exists>stp. abc_steps_l (6 + length ab, butlast lm @ 
       
  1924                 last lm # rs # 0\<up>(a_md - Suc (Suc rs_pos)) @ 0 # suf_lm)
       
  1925                 (ap [+] ([Dec (a_md - Suc 0) (length a + 7)] [+] 
       
  1926                 (a [+] [Inc (rs_pos - Suc 0), Dec rs_pos 3, 
       
  1927                 Goto (Suc 0)])) @ [Dec (Suc (Suc n)) 0, Inc (Suc n), 
       
  1928                 Goto (length a + 4)] [+] cp) stp =
       
  1929             (6 + length ab + (length a + 7) , 
       
  1930                  lm @ rs # 0\<up>(a_md - Suc rs_pos) @ suf_lm)", simp)
       
  1931 	  apply(subgoal_tac "13 + (length ab + length a) = 
       
  1932                               13 + length ab + length a", simp)
       
  1933 	  apply(arith)
       
  1934 	  apply(rule abc_append_exc1, simp_all)
       
  1935 	  apply(rule_tac x = "Suc 0" in exI, 
       
  1936                 simp add: abc_steps_l.simps abc_fetch.simps
       
  1937                          nth_append abc_append_nth abc_step_l.simps)
       
  1938 	  apply(subgoal_tac "a_md > Suc (Suc rs_pos) \<and> 
       
  1939                             length lm = rs_pos \<and> rs_pos > 0", simp)
       
  1940 	  apply(insert h, simp)
       
  1941 	  apply(subgoal_tac "rs_pos = Suc n", simp, simp)
       
  1942           done
       
  1943       qed
       
  1944       from h have k2_2_5: "length aprog = 13 + length ab + length a"
       
  1945 	apply(rule_tac ci_pr_length, simp_all)
       
  1946 	done
       
  1947       from k2_2_1 and k2_2_2 and k2_2_3 and k2_2_4 and k2_2_5 
       
  1948       show "?thesis"
       
  1949 	apply(auto)
       
  1950 	apply(rule_tac x = "stp + stpa + stpb + stpc" in exI, 
       
  1951               simp add: abc_steps_add)
       
  1952 	done
       
  1953     qed
       
  1954   qed	
       
  1955   from k1 and k2 show 
       
  1956     "\<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog stp 
       
  1957                = (length aprog, lm @ rs # 0\<up>(a_md - Suc rs_pos) @ suf_lm)"
       
  1958     apply(erule_tac exE)
       
  1959     apply(erule_tac exE)
       
  1960     apply(rule_tac x = "stp + stpa" in exI)
       
  1961     apply(simp add: abc_steps_add)
       
  1962     done
       
  1963 qed
       
  1964 
       
  1965 thm rec_calc_rel.induct
       
  1966 
       
  1967 lemma eq_switch: "x = y \<Longrightarrow> y = x"
       
  1968 by simp
       
  1969 
       
  1970 lemma [simp]: 
       
  1971   "\<lbrakk>rec_ci f = (a, aa, ba); 
       
  1972     rec_ci (Mn n f) = (aprog, rs_pos, a_md)\<rbrakk> \<Longrightarrow> \<exists>bp. aprog = a @ bp"
       
  1973 apply(simp add: rec_ci.simps)
       
  1974 apply(rule_tac x = "[Dec (Suc n) (length a + 5), 
       
  1975       Dec (Suc n) (length a + 3), Goto (Suc (length a)), 
       
  1976       Inc n, Goto 0]" in exI, auto)
       
  1977 done
       
  1978 
       
  1979 lemma ci_mn_para_eq[simp]: 
       
  1980   "rec_ci (Mn n f) = (aprog, rs_pos, a_md) \<Longrightarrow> rs_pos = n"
       
  1981 apply(case_tac "rec_ci f", simp add: rec_ci.simps)
       
  1982 done
       
  1983 (*
       
  1984 lemma [simp]: "\<lbrakk>rec_ci f = (a, aa, ba); rec_ci (Mn n f) = (aprog, rs_pos, a_md); rec_calc_rel (Mn n f) lm rs\<rbrakk> \<Longrightarrow> aa = Suc rs_pos"
       
  1985 apply(rule_tac calc_mn_reverse, simp)
       
  1986 apply(insert para_pattern [of f a aa ba "lm @ [rs]" 0], simp)
       
  1987 apply(subgoal_tac "rs_pos = length lm", simp)
       
  1988 apply(drule_tac ci_mn_para_eq, simp)
       
  1989 done
       
  1990 *)
       
  1991 lemma [simp]: "rec_ci f = (a, aa, ba) \<Longrightarrow> aa < ba"
       
  1992 apply(simp add: ci_ad_ge_paras)
       
  1993 done
       
  1994 
       
  1995 lemma [simp]: "\<lbrakk>rec_ci f = (a, aa, ba); 
       
  1996                 rec_ci (Mn n f) = (aprog, rs_pos, a_md)\<rbrakk>
       
  1997     \<Longrightarrow> ba \<le> a_md"
       
  1998 apply(simp add: rec_ci.simps)
       
  1999 by arith
       
  2000 
       
  2001 lemma mn_calc_f: 
       
  2002   assumes ind: 
       
  2003   "\<And>aprog a_md rs_pos rs suf_lm lm.
       
  2004   \<lbrakk>rec_ci f = (aprog, rs_pos, a_md); rec_calc_rel f lm rs\<rbrakk>  
       
  2005   \<Longrightarrow> \<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog stp    
       
  2006            = (length aprog, lm @ [rs] @ 0\<up>(a_md - rs_pos - 1) @ suf_lm)"
       
  2007   and h: "rec_ci f = (a, aa, ba)" 
       
  2008          "rec_ci (Mn n f) = (aprog, rs_pos, a_md)"  
       
  2009          "rec_calc_rel f (lm @ [x]) rsx" 
       
  2010          "aa = Suc n"
       
  2011   shows "\<exists>stp. abc_steps_l (0, lm @ x # 0\<up>(a_md - Suc rs_pos) @ suf_lm) 
       
  2012                   aprog stp = (length a, 
       
  2013                    lm @ x # rsx # 0\<up>(a_md - Suc (Suc rs_pos)) @ suf_lm)"
       
  2014 proof -
       
  2015   from h have k1: "\<exists> ap bp. aprog = ap @ bp \<and> ap = a"
       
  2016     by simp
       
  2017   from h have k2: "rs_pos = n"
       
  2018     apply(erule_tac ci_mn_para_eq)
       
  2019     done
       
  2020   from h and k1 and k2 show "?thesis"
       
  2021   
       
  2022   proof(erule_tac exE, erule_tac exE, simp, 
       
  2023         rule_tac abc_add_exc1, auto)
       
  2024     fix bp
       
  2025     show 
       
  2026       "\<exists>astp. abc_steps_l (0, lm @ x # 0\<up>(a_md - Suc n) @ suf_lm) a astp
       
  2027       = (length a, lm @ x # rsx # 0\<up>(a_md - Suc (Suc n)) @ suf_lm)"
       
  2028       apply(insert ind[of a "Suc n" ba  "lm @ [x]" rsx 
       
  2029              "0\<up>(a_md - ba) @ suf_lm"], simp add: exponent_add_iff h k2)
       
  2030       apply(subgoal_tac "ba > aa \<and> a_md \<ge> ba \<and> aa = Suc n", 
       
  2031             insert h, auto)
       
  2032       done
       
  2033   qed
       
  2034 qed
       
  2035 
       
  2036 fun mn_ind_inv ::
       
  2037   "nat \<times> nat list \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat list \<Rightarrow> nat list \<Rightarrow> bool"
       
  2038   where
       
  2039   "mn_ind_inv (as, lm') ss x rsx suf_lm lm = 
       
  2040            (if as = ss then lm' = lm @ x # rsx # suf_lm
       
  2041             else if as = ss + 1 then 
       
  2042                  \<exists>y. (lm' = lm @ x # y # suf_lm) \<and> y \<le> rsx
       
  2043             else if as = ss + 2 then 
       
  2044                  \<exists>y. (lm' = lm @ x # y # suf_lm) \<and> y \<le> rsx
       
  2045             else if as = ss + 3 then lm' = lm @ x # 0 # suf_lm
       
  2046             else if as = ss + 4 then lm' = lm @ Suc x # 0 # suf_lm
       
  2047             else if as = 0 then lm' = lm @ Suc x # 0 # suf_lm
       
  2048             else False
       
  2049 )"
       
  2050 
       
  2051 fun mn_stage1 :: "nat \<times> nat list \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat"
       
  2052   where
       
  2053   "mn_stage1 (as, lm) ss n = 
       
  2054             (if as = 0 then 0 
       
  2055              else if as = ss + 4 then 1
       
  2056              else if as = ss + 3 then 2
       
  2057              else if as = ss + 2 \<or> as = ss + 1 then 3
       
  2058              else if as = ss then 4
       
  2059              else 0
       
  2060 )"
       
  2061 
       
  2062 fun mn_stage2 :: "nat \<times> nat list \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat"
       
  2063   where
       
  2064   "mn_stage2 (as, lm) ss n = 
       
  2065             (if as = ss + 1 \<or> as = ss + 2 then (lm ! (Suc n))
       
  2066              else 0)"
       
  2067 
       
  2068 fun mn_stage3 :: "nat \<times> nat list \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat"
       
  2069   where
       
  2070   "mn_stage3 (as, lm) ss n = (if as = ss + 2 then 1 else 0)"
       
  2071 
       
  2072  
       
  2073 fun mn_measure :: "((nat \<times> nat list) \<times> nat \<times> nat) \<Rightarrow>
       
  2074                                                 (nat \<times> nat \<times> nat)"
       
  2075   where
       
  2076   "mn_measure ((as, lm), ss, n) = 
       
  2077      (mn_stage1 (as, lm) ss n, mn_stage2 (as, lm) ss n,
       
  2078                                        mn_stage3 (as, lm) ss n)"
       
  2079 
       
  2080 definition mn_LE :: "(((nat \<times> nat list) \<times> nat \<times> nat) \<times>
       
  2081                      ((nat \<times> nat list) \<times> nat \<times> nat)) set"
       
  2082   where "mn_LE \<equiv> (inv_image lex_triple mn_measure)"
       
  2083 
       
  2084 thm halt_lemma2
       
  2085 lemma wf_mn_le[intro]: "wf mn_LE"
       
  2086 by(auto intro:wf_inv_image wf_lex_triple simp: mn_LE_def)
       
  2087 
       
  2088 declare mn_ind_inv.simps[simp del]
       
  2089 
       
  2090 lemma mn_inv_init: 
       
  2091   "mn_ind_inv (abc_steps_l (length a, lm @ x # rsx # suf_lm) aprog 0)
       
  2092                                          (length a) x rsx suf_lm lm"
       
  2093 apply(simp add: mn_ind_inv.simps abc_steps_zero)
       
  2094 done
       
  2095 
       
  2096 lemma mn_halt_init: 
       
  2097   "rec_ci f = (a, aa, ba) \<Longrightarrow> 
       
  2098   \<not> (\<lambda>(as, lm') (ss, n). as = 0) 
       
  2099     (abc_steps_l (length a, lm @ x # rsx # suf_lm) aprog 0) 
       
  2100                                                        (length a, n)"
       
  2101 apply(simp add: abc_steps_zero)
       
  2102 apply(erule_tac rec_ci_not_null)
       
  2103 done
       
  2104 
       
  2105 thm rec_ci.simps
       
  2106 lemma [simp]: 
       
  2107   "\<lbrakk>rec_ci f = (a, aa, ba); 
       
  2108     rec_ci (Mn n f) = (aprog, rs_pos, a_md)\<rbrakk>
       
  2109     \<Longrightarrow> abc_fetch (length a) aprog =
       
  2110                       Some (Dec (Suc n) (length a + 5))"
       
  2111 apply(simp add: rec_ci.simps abc_fetch.simps, 
       
  2112                 erule_tac conjE, erule_tac conjE, simp)
       
  2113 apply(drule_tac eq_switch, drule_tac eq_switch, simp)
       
  2114 done
       
  2115 
       
  2116 lemma [simp]: "\<lbrakk>rec_ci f = (a, aa, ba); rec_ci (Mn n f) = (aprog, rs_pos, a_md)\<rbrakk>
       
  2117     \<Longrightarrow> abc_fetch (Suc (length a)) aprog = Some (Dec (Suc n) (length a + 3))"
       
  2118 apply(simp add: rec_ci.simps abc_fetch.simps, erule_tac conjE, erule_tac conjE, simp)
       
  2119 apply(drule_tac eq_switch, drule_tac eq_switch, simp add: nth_append)
       
  2120 done
       
  2121 
       
  2122 lemma [simp]:
       
  2123   "\<lbrakk>rec_ci f = (a, aa, ba);
       
  2124     rec_ci (Mn n f) = (aprog, rs_pos, a_md)\<rbrakk>
       
  2125     \<Longrightarrow> abc_fetch (Suc (Suc (length a))) aprog = 
       
  2126                                      Some (Goto (length a + 1))"
       
  2127 apply(simp add: rec_ci.simps abc_fetch.simps,
       
  2128       erule_tac conjE, erule_tac conjE, simp)
       
  2129 apply(drule_tac eq_switch, drule_tac eq_switch, simp add: nth_append)
       
  2130 done
       
  2131 
       
  2132 lemma [simp]: 
       
  2133   "\<lbrakk>rec_ci f = (a, aa, ba);
       
  2134     rec_ci (Mn n f) = (aprog, rs_pos, a_md)\<rbrakk>
       
  2135     \<Longrightarrow> abc_fetch (length a + 3) aprog = Some (Inc n)"
       
  2136 apply(simp add: rec_ci.simps abc_fetch.simps, 
       
  2137       erule_tac conjE, erule_tac conjE, simp)
       
  2138 apply(drule_tac eq_switch, drule_tac eq_switch, simp add: nth_append)
       
  2139 done
       
  2140 
       
  2141 lemma [simp]: "\<lbrakk>rec_ci f = (a, aa, ba); rec_ci (Mn n f) = (aprog, rs_pos, a_md)\<rbrakk>
       
  2142     \<Longrightarrow> abc_fetch (length a + 4) aprog = Some (Goto 0)"
       
  2143 apply(simp add: rec_ci.simps abc_fetch.simps, erule_tac conjE, erule_tac conjE, simp)
       
  2144 apply(drule_tac eq_switch, drule_tac eq_switch, simp add: nth_append)
       
  2145 done
       
  2146 
       
  2147 lemma [simp]: 
       
  2148   "0 < rsx
       
  2149    \<Longrightarrow> \<exists>y. (lm @ x # rsx # suf_lm)[Suc (length lm) := rsx - Suc 0]   
       
  2150     = lm @ x # y # suf_lm \<and> y \<le> rsx"
       
  2151 apply(case_tac rsx, simp, simp)
       
  2152 apply(rule_tac x = nat in exI, simp add: list_update_append)
       
  2153 done
       
  2154 
       
  2155 lemma [simp]: 
       
  2156   "\<lbrakk>y \<le> rsx; 0 < y\<rbrakk>
       
  2157    \<Longrightarrow> \<exists>ya. (lm @ x # y # suf_lm)[Suc (length lm) := y - Suc 0] 
       
  2158           = lm @ x # ya # suf_lm \<and> ya \<le> rsx"
       
  2159 apply(case_tac y, simp, simp)
       
  2160 apply(rule_tac x = nat in exI, simp add: list_update_append)
       
  2161 done
       
  2162 
       
  2163 lemma mn_halt_lemma: 
       
  2164   "\<lbrakk>rec_ci f = (a, aa, ba);
       
  2165     rec_ci (Mn n f) = (aprog, rs_pos, a_md);
       
  2166      0 < rsx; length lm = n\<rbrakk>
       
  2167     \<Longrightarrow>
       
  2168   \<forall>na. \<not> (\<lambda>(as, lm') (ss, n). as = 0)
       
  2169   (abc_steps_l (length a, lm @ x # rsx # suf_lm) aprog na) 
       
  2170                                                        (length a, n)
       
  2171  \<and> mn_ind_inv (abc_steps_l (length a, lm @ x # rsx # suf_lm)
       
  2172                        aprog na) (length a) x rsx suf_lm lm 
       
  2173 \<longrightarrow> mn_ind_inv (abc_steps_l (length a, lm @ x # rsx # suf_lm) aprog 
       
  2174                          (Suc na)) (length a) x rsx suf_lm lm
       
  2175  \<and> ((abc_steps_l (length a, lm @ x # rsx # suf_lm) aprog (Suc na), 
       
  2176                                                     length a, n), 
       
  2177     abc_steps_l (length a, lm @ x # rsx # suf_lm) aprog na,
       
  2178                               length a, n) \<in> mn_LE"
       
  2179 apply(rule allI, rule impI, simp add: abc_steps_ind)
       
  2180 apply(case_tac "(abc_steps_l (length a, lm @ x # rsx # suf_lm) 
       
  2181                                                    aprog na)", simp)
       
  2182 apply(auto split:if_splits simp add:abc_steps_l.simps 
       
  2183                            mn_ind_inv.simps abc_steps_zero)
       
  2184 apply(auto simp add: mn_LE_def lex_triple_def lex_pair_def 
       
  2185             abc_step_l.simps abc_steps_l.simps mn_ind_inv.simps
       
  2186             abc_lm_v.simps abc_lm_s.simps nth_append
       
  2187            split: if_splits)
       
  2188 apply(drule_tac  rec_ci_not_null, simp)
       
  2189 done
       
  2190 
       
  2191 lemma mn_halt:
       
  2192   "\<lbrakk>rec_ci f = (a, aa, ba);
       
  2193     rec_ci (Mn n f) = (aprog, rs_pos, a_md);
       
  2194     0 < rsx; length lm = n\<rbrakk>
       
  2195     \<Longrightarrow> \<exists> stp. (\<lambda> (as, lm'). (as = 0 \<and> 
       
  2196            mn_ind_inv (as, lm')  (length a) x rsx suf_lm lm))
       
  2197             (abc_steps_l (length a, lm @ x # rsx # suf_lm) aprog stp)"
       
  2198 apply(insert wf_mn_le)	  
       
  2199 apply(insert halt_lemma2[of mn_LE
       
  2200   "\<lambda> ((as, lm'), ss, n). mn_ind_inv (as, lm') ss x rsx suf_lm lm"
       
  2201   "\<lambda> stp. (abc_steps_l (length a, lm @ x # rsx # suf_lm) aprog stp, 
       
  2202   length a, n)"
       
  2203   "\<lambda> ((as, lm'), ss, n). as = 0"], 
       
  2204    simp)
       
  2205 apply(simp add: mn_halt_init mn_inv_init)
       
  2206 apply(drule_tac x = x and suf_lm = suf_lm in mn_halt_lemma, auto)
       
  2207 apply(rule_tac x = n in exI, 
       
  2208       case_tac "(abc_steps_l (length a, lm @ x # rsx # suf_lm)
       
  2209                               aprog n)", simp)
       
  2210 done
       
  2211 
       
  2212 lemma [simp]: "Suc rs_pos < a_md \<Longrightarrow> 
       
  2213                 Suc (a_md - Suc (Suc rs_pos)) = a_md - Suc rs_pos"
       
  2214 by arith
       
  2215 
       
  2216 term rec_ci
       
  2217 (*
       
  2218 lemma [simp]: "\<lbrakk>rec_ci (Mn n f) = (aprog, rs_pos, a_md); rec_calc_rel (Mn n f) lm rs\<rbrakk>  \<Longrightarrow> Suc rs_pos < a_md"
       
  2219 apply(case_tac "rec_ci f")
       
  2220 apply(subgoal_tac "c > b \<and> b = Suc rs_pos \<and> a_md \<ge> c")
       
  2221 apply(arith, auto)
       
  2222 done
       
  2223 *)
       
  2224 lemma mn_ind_step: 
       
  2225   assumes ind:  
       
  2226   "\<And>aprog a_md rs_pos rs suf_lm lm.
       
  2227   \<lbrakk>rec_ci f = (aprog, rs_pos, a_md);
       
  2228    rec_calc_rel f lm rs\<rbrakk> \<Longrightarrow> 
       
  2229   \<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog stp
       
  2230             = (length aprog, lm @ [rs] @ 0\<up>(a_md - rs_pos - 1) @ suf_lm)"
       
  2231   and h: "rec_ci f = (a, aa, ba)" 
       
  2232          "rec_ci (Mn n f) = (aprog, rs_pos, a_md)"  
       
  2233          "rec_calc_rel f (lm @ [x]) rsx" 
       
  2234          "rsx > 0" 
       
  2235          "Suc rs_pos < a_md" 
       
  2236          "aa = Suc rs_pos"
       
  2237   shows "\<exists>stp. abc_steps_l (0, lm @ x # 0\<up>(a_md - Suc rs_pos) @ suf_lm) 
       
  2238              aprog stp = (0, lm @ Suc x # 0\<up>(a_md - Suc rs_pos) @ suf_lm)"
       
  2239 thm abc_add_exc1
       
  2240 proof -
       
  2241   have k1: 
       
  2242     "\<exists> stp. abc_steps_l (0, lm @ x #  0\<up>(a_md - Suc (rs_pos)) @ suf_lm)
       
  2243          aprog stp = 
       
  2244        (length a, lm @ x # rsx # 0\<up>(a_md  - Suc (Suc rs_pos)) @ suf_lm)"
       
  2245     apply(insert h)
       
  2246     apply(auto intro: mn_calc_f ind)
       
  2247     done
       
  2248   from h have k2: "length lm = n"
       
  2249     apply(subgoal_tac "rs_pos = n")
       
  2250     apply(drule_tac  para_pattern, simp, simp, simp)
       
  2251     done
       
  2252   from h have k3: "a_md > (Suc rs_pos)"
       
  2253     apply(simp)
       
  2254     done
       
  2255   from k2 and h and k3 have k4: 
       
  2256     "\<exists> stp. abc_steps_l (length a,
       
  2257        lm @ x # rsx # 0\<up>(a_md  - Suc (Suc rs_pos)) @ suf_lm) aprog stp = 
       
  2258         (0, lm @ Suc x # 0\<up>(a_md - rs_pos - 1) @ suf_lm)"
       
  2259     apply(frule_tac x = x and 
       
  2260        suf_lm = "0\<up>(a_md - Suc (Suc rs_pos)) @ suf_lm" in mn_halt, auto)
       
  2261     apply(rule_tac x = "stp" in exI, 
       
  2262           simp add: mn_ind_inv.simps rec_ci_not_null)
       
  2263     apply(simp only: replicate.simps[THEN sym], simp)
       
  2264     done
       
  2265   from k1 and k4 show "?thesis"
       
  2266     apply(auto)
       
  2267     apply(rule_tac x = "stp + stpa" in exI, simp add: abc_steps_add)
       
  2268     done
       
  2269 qed
       
  2270 
       
  2271 lemma [simp]: 
       
  2272   "\<lbrakk>rec_ci f = (a, aa, ba); rec_ci (Mn n f) = (aprog, rs_pos, a_md);
       
  2273     rec_calc_rel (Mn n f) lm rs\<rbrakk> \<Longrightarrow> aa = Suc rs_pos"
       
  2274 apply(rule_tac calc_mn_reverse, simp)
       
  2275 apply(insert para_pattern [of f a aa ba "lm @ [rs]" 0], simp)
       
  2276 apply(subgoal_tac "rs_pos = length lm", simp)
       
  2277 apply(drule_tac ci_mn_para_eq, simp)
       
  2278 done
       
  2279 
       
  2280 lemma [simp]: "\<lbrakk>rec_ci (Mn n f) = (aprog, rs_pos, a_md);      
       
  2281                 rec_calc_rel (Mn n f) lm rs\<rbrakk>  \<Longrightarrow> Suc rs_pos < a_md"
       
  2282 apply(case_tac "rec_ci f")
       
  2283 apply(subgoal_tac "c > b \<and> b = Suc rs_pos \<and> a_md \<ge> c")
       
  2284 apply(arith, auto)
       
  2285 done
       
  2286 
       
  2287 lemma mn_ind_steps:  
       
  2288   assumes ind:
       
  2289   "\<And>aprog a_md rs_pos rs suf_lm lm. 
       
  2290   \<lbrakk>rec_ci f = (aprog, rs_pos, a_md); rec_calc_rel f lm rs\<rbrakk> \<Longrightarrow> 
       
  2291   \<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog stp = 
       
  2292               (length aprog, lm @ [rs] @ 0\<up>(a_md - rs_pos - 1) @ suf_lm)"
       
  2293   and h: "rec_ci f = (a, aa, ba)" 
       
  2294   "rec_ci (Mn n f) = (aprog, rs_pos, a_md)" 
       
  2295   "rec_calc_rel (Mn n f) lm rs"
       
  2296   "rec_calc_rel f (lm @ [rs]) 0" 
       
  2297   "\<forall>x<rs. (\<exists> v. rec_calc_rel f (lm @ [x]) v \<and> 0 < v)"
       
  2298   "n = length lm" 
       
  2299   "x \<le> rs"
       
  2300   shows "\<exists>stp. abc_steps_l (0, lm @ 0 # 0\<up>(a_md - Suc rs_pos) @ suf_lm)
       
  2301                  aprog stp = (0, lm @ x # 0\<up>(a_md - Suc rs_pos) @ suf_lm)"
       
  2302 apply(insert h, induct x, 
       
  2303       rule_tac x = 0 in exI, simp add: abc_steps_zero, simp)
       
  2304 proof -
       
  2305   fix x
       
  2306   assume k1: 
       
  2307     "\<exists>stp. abc_steps_l (0, lm @ 0 # 0\<up>(a_md - Suc rs_pos) @ suf_lm)
       
  2308                 aprog stp = (0, lm @ x # 0\<up>(a_md - Suc rs_pos) @ suf_lm)"
       
  2309   and k2: "rec_ci (Mn (length lm) f) = (aprog, rs_pos, a_md)" 
       
  2310           "rec_calc_rel (Mn (length lm) f) lm rs" 
       
  2311           "rec_calc_rel f (lm @ [rs]) 0" 
       
  2312           "\<forall>x<rs.(\<exists> v. rec_calc_rel f (lm @ [x]) v \<and> v > 0)" 
       
  2313           "n = length lm" 
       
  2314           "Suc x \<le> rs" 
       
  2315           "rec_ci f = (a, aa, ba)"
       
  2316   hence k2:
       
  2317     "\<exists>stp. abc_steps_l (0, lm @ x # 0\<up>(a_md - rs_pos - 1) @ suf_lm) aprog
       
  2318                stp = (0, lm @ Suc x # 0\<up>(a_md - rs_pos - 1) @ suf_lm)"
       
  2319     apply(erule_tac x = x in allE)
       
  2320     apply(auto)
       
  2321     apply(rule_tac  x = x in mn_ind_step)
       
  2322     apply(rule_tac ind, auto)      
       
  2323     done
       
  2324   from k1 and k2 show 
       
  2325     "\<exists>stp. abc_steps_l (0, lm @ 0 # 0\<up>(a_md - Suc rs_pos) @ suf_lm)
       
  2326           aprog stp = (0, lm @ Suc x # 0\<up>(a_md - Suc rs_pos) @ suf_lm)"
       
  2327     apply(auto)
       
  2328     apply(rule_tac x = "stp + stpa" in exI, simp only: abc_steps_add)
       
  2329     done
       
  2330 qed
       
  2331     
       
  2332 lemma [simp]: 
       
  2333 "\<lbrakk>rec_ci f = (a, aa, ba); 
       
  2334   rec_ci (Mn n f) = (aprog, rs_pos, a_md); 
       
  2335   rec_calc_rel (Mn n f) lm rs;
       
  2336   length lm = n\<rbrakk>
       
  2337  \<Longrightarrow> abc_lm_v (lm @ rs # 0\<up>(a_md - Suc rs_pos) @ suf_lm) (Suc n) = 0"
       
  2338 apply(auto simp: abc_lm_v.simps nth_append)
       
  2339 done
       
  2340 
       
  2341 lemma [simp]: 
       
  2342   "\<lbrakk>rec_ci f = (a, aa, ba); 
       
  2343     rec_ci (Mn n f) = (aprog, rs_pos, a_md); 
       
  2344     rec_calc_rel (Mn n f) lm rs;
       
  2345      length lm = n\<rbrakk>
       
  2346     \<Longrightarrow> abc_lm_s (lm @ rs # 0\<up>(a_md - Suc rs_pos) @ suf_lm) (Suc n) 0 =
       
  2347                            lm @ rs # 0\<up>(a_md - Suc rs_pos) @ suf_lm"
       
  2348 apply(auto simp: abc_lm_s.simps list_update_append)
       
  2349 done
       
  2350 
       
  2351 lemma mn_length: 
       
  2352   "\<lbrakk>rec_ci f = (a, aa, ba);
       
  2353     rec_ci (Mn n f) = (aprog, rs_pos, a_md)\<rbrakk>
       
  2354   \<Longrightarrow> length aprog = length a + 5"
       
  2355 apply(simp add: rec_ci.simps, erule_tac conjE)
       
  2356 apply(drule_tac eq_switch, drule_tac eq_switch, simp)
       
  2357 done
       
  2358 
       
  2359 lemma mn_final_step:
       
  2360   assumes ind:
       
  2361   "\<And>aprog a_md rs_pos rs suf_lm lm.
       
  2362   \<lbrakk>rec_ci f = (aprog, rs_pos, a_md); 
       
  2363   rec_calc_rel f lm rs\<rbrakk> \<Longrightarrow> 
       
  2364   \<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog stp =
       
  2365               (length aprog, lm @ [rs] @ 0\<up>(a_md - rs_pos - 1) @ suf_lm)"
       
  2366   and h: "rec_ci f = (a, aa, ba)" 
       
  2367          "rec_ci (Mn n f) = (aprog, rs_pos, a_md)" 
       
  2368          "rec_calc_rel (Mn n f) lm rs" 
       
  2369          "rec_calc_rel f (lm @ [rs]) 0" 
       
  2370   shows "\<exists>stp. abc_steps_l (0, lm @ rs # 0\<up>(a_md - Suc rs_pos) @ suf_lm) 
       
  2371      aprog stp = (length aprog, lm @ rs # 0\<up>(a_md - Suc rs_pos) @ suf_lm)"
       
  2372 proof -
       
  2373   from h and ind have k1:
       
  2374     "\<exists>stp.  abc_steps_l (0, lm @ rs # 0\<up>(a_md - Suc rs_pos) @ suf_lm) 
       
  2375         aprog stp = (length a,  lm @ rs # 0\<up>(a_md - Suc rs_pos) @ suf_lm)"
       
  2376     thm mn_calc_f
       
  2377     apply(insert mn_calc_f[of f a aa ba n aprog 
       
  2378                                rs_pos a_md lm rs 0 suf_lm], simp)
       
  2379     apply(subgoal_tac "aa = Suc n", simp add: exponent_cons_iff)
       
  2380     apply(subgoal_tac "rs_pos = n", simp, simp)
       
  2381     done
       
  2382   from h have k2: "length lm = n"
       
  2383     apply(subgoal_tac "rs_pos = n")
       
  2384     apply(drule_tac f = "Mn n f" in para_pattern, simp, simp, simp)
       
  2385     done
       
  2386   from h and k2 have k3: 
       
  2387   "\<exists>stp. abc_steps_l (length a, lm @ rs # 0\<up>(a_md - Suc rs_pos) @ suf_lm)
       
  2388     aprog stp = (length a + 5, lm @ rs # 0\<up>(a_md - Suc rs_pos) @ suf_lm)"
       
  2389     apply(rule_tac x = "Suc 0" in exI, 
       
  2390           simp add: abc_step_l.simps abc_steps_l.simps)
       
  2391     done
       
  2392   from h have k4: "length aprog = length a + 5"
       
  2393     apply(simp add: mn_length)
       
  2394     done
       
  2395   from k1 and k3 and k4 show "?thesis"
       
  2396     apply(auto)
       
  2397     apply(rule_tac x = "stp + stpa" in exI, simp add: abc_steps_add)
       
  2398     done
       
  2399 qed
       
  2400 
       
  2401 lemma mn_case: 
       
  2402   assumes ind: 
       
  2403   "\<And>aprog a_md rs_pos rs suf_lm lm.
       
  2404   \<lbrakk>rec_ci f = (aprog, rs_pos, a_md); rec_calc_rel f lm rs\<rbrakk> \<Longrightarrow> 
       
  2405   \<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog stp = 
       
  2406                (length aprog, lm @ [rs] @ 0\<up>(a_md - rs_pos - 1) @ suf_lm)"
       
  2407   and h: "rec_ci (Mn n f) = (aprog, rs_pos, a_md)" 
       
  2408          "rec_calc_rel (Mn n f) lm rs"
       
  2409   shows "\<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog stp 
       
  2410   = (length aprog, lm @ [rs] @ 0\<up>(a_md - rs_pos - 1) @ suf_lm)"
       
  2411 apply(case_tac "rec_ci f", simp)
       
  2412 apply(insert h, rule_tac calc_mn_reverse, simp)
       
  2413 proof -
       
  2414   fix a b c v
       
  2415   assume h: "rec_ci f = (a, b, c)" 
       
  2416             "rec_ci (Mn n f) = (aprog, rs_pos, a_md)" 
       
  2417             "rec_calc_rel (Mn n f) lm rs" 
       
  2418             "rec_calc_rel f (lm @ [rs]) 0" 
       
  2419             "\<forall>x<rs. \<exists>v. rec_calc_rel f (lm @ [x]) v \<and> 0 < v"
       
  2420             "n = length lm"
       
  2421   hence k1:
       
  2422     "\<exists>stp. abc_steps_l (0, lm @ 0 # 0\<up>(a_md - Suc rs_pos) @ suf_lm) aprog
       
  2423                   stp = (0, lm @ rs # 0\<up>(a_md - Suc rs_pos) @ suf_lm)"
       
  2424     thm mn_ind_steps
       
  2425     apply(auto intro: mn_ind_steps ind)
       
  2426     done
       
  2427   from h have k2: 
       
  2428     "\<exists>stp. abc_steps_l (0, lm @ rs # 0\<up>(a_md - Suc rs_pos) @ suf_lm) aprog
       
  2429          stp = (length aprog, lm @ rs # 0\<up>(a_md - Suc rs_pos) @ suf_lm)"
       
  2430     apply(auto intro: mn_final_step ind)
       
  2431     done
       
  2432   from k1 and k2 show 
       
  2433     "\<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog stp = 
       
  2434   (length aprog, lm @ rs # 0\<up>(a_md - Suc rs_pos) @ suf_lm)"
       
  2435     apply(auto, insert h)
       
  2436     apply(subgoal_tac "Suc rs_pos < a_md")
       
  2437     apply(rule_tac x = "stp + stpa" in exI, 
       
  2438       simp only: abc_steps_add exponent_cons_iff, simp, simp)
       
  2439     done
       
  2440 qed
       
  2441 
       
  2442 lemma z_rs: "rec_calc_rel z lm rs \<Longrightarrow> rs = 0"
       
  2443 apply(rule_tac calc_z_reverse, auto)
       
  2444 done
       
  2445 
       
  2446 lemma z_case:
       
  2447   "\<lbrakk>rec_ci z = (aprog, rs_pos, a_md); rec_calc_rel z lm rs\<rbrakk>
       
  2448   \<Longrightarrow> \<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog stp =
       
  2449            (length aprog, lm @ [rs] @ 0\<up>(a_md - rs_pos - 1) @ suf_lm)"
       
  2450 apply(simp add: rec_ci.simps rec_ci_z_def, auto)
       
  2451 apply(rule_tac x = "Suc 0" in exI, simp add: abc_steps_l.simps 
       
  2452                                abc_fetch.simps abc_step_l.simps z_rs)
       
  2453 done
       
  2454 
       
  2455 fun addition_inv :: "nat \<times> nat list \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow>     
       
  2456                      nat list \<Rightarrow> bool"
       
  2457   where
       
  2458   "addition_inv (as, lm') m n p lm = 
       
  2459         (let sn = lm ! n in
       
  2460          let sm = lm ! m in
       
  2461          lm ! p = 0 \<and>
       
  2462              (if as = 0 then \<exists> x. x \<le> lm ! m \<and> lm' = lm[m := x,
       
  2463                                     n := (sn + sm - x), p := (sm - x)]
       
  2464              else if as = 1 then \<exists> x. x < lm ! m \<and> lm' = lm[m := x,
       
  2465                             n := (sn + sm - x - 1), p := (sm - x - 1)]
       
  2466              else if as = 2 then \<exists> x. x < lm ! m \<and> lm' = lm[m := x, 
       
  2467                                n := (sn + sm - x), p := (sm - x - 1)]
       
  2468              else if as = 3 then \<exists> x. x < lm ! m \<and> lm' = lm[m := x,
       
  2469                                    n := (sn + sm - x), p := (sm - x)]
       
  2470              else if as = 4 then \<exists> x. x \<le> lm ! m \<and> lm' = lm[m := x,
       
  2471                                        n := (sn + sm), p := (sm - x)] 
       
  2472              else if as = 5 then \<exists> x. x < lm ! m \<and> lm' = lm[m := x, 
       
  2473                                   n := (sn + sm), p := (sm - x - 1)] 
       
  2474              else if as = 6 then \<exists> x. x < lm ! m \<and> lm' =
       
  2475                      lm[m := Suc x, n := (sn + sm), p := (sm - x - 1)]
       
  2476              else if as = 7 then lm' = lm[m := sm, n := (sn + sm)]
       
  2477              else False))"
       
  2478 
       
  2479 fun addition_stage1 :: "nat \<times> nat list \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat"
       
  2480   where
       
  2481   "addition_stage1 (as, lm) m p = 
       
  2482           (if as = 0 \<or> as = 1 \<or> as = 2 \<or> as = 3 then 2 
       
  2483            else if as = 4 \<or> as = 5 \<or> as = 6 then 1
       
  2484            else 0)"
       
  2485 
       
  2486 fun addition_stage2 :: "nat \<times> nat list \<Rightarrow> nat \<Rightarrow>  nat \<Rightarrow> nat"
       
  2487   where
       
  2488   "addition_stage2 (as, lm) m p = 
       
  2489               (if 0 \<le> as \<and> as \<le> 3 then lm ! m
       
  2490                else if 4 \<le> as \<and> as \<le> 6 then lm ! p
       
  2491                else 0)"
       
  2492 
       
  2493 fun addition_stage3 :: "nat \<times> nat list \<Rightarrow> nat \<Rightarrow> nat \<Rightarrow> nat"
       
  2494   where
       
  2495   "addition_stage3 (as, lm) m p = 
       
  2496              (if as = 1 then 4  
       
  2497               else if as = 2 then 3 
       
  2498               else if as = 3 then 2
       
  2499               else if as = 0 then 1 
       
  2500               else if as = 5 then 2
       
  2501               else if as = 6 then 1 
       
  2502               else if as = 4 then 0 
       
  2503               else 0)"
       
  2504 
       
  2505 fun addition_measure :: "((nat \<times> nat list) \<times> nat \<times> nat) \<Rightarrow> 
       
  2506                                                  (nat \<times> nat \<times> nat)"
       
  2507   where
       
  2508   "addition_measure ((as, lm), m, p) =
       
  2509      (addition_stage1 (as, lm) m p, 
       
  2510       addition_stage2 (as, lm) m p,
       
  2511       addition_stage3 (as, lm) m p)"
       
  2512 
       
  2513 definition addition_LE :: "(((nat \<times> nat list) \<times> nat \<times> nat) \<times> 
       
  2514                           ((nat \<times> nat list) \<times> nat \<times> nat)) set"
       
  2515   where "addition_LE \<equiv> (inv_image lex_triple addition_measure)"
       
  2516 
       
  2517 lemma [simp]: "wf addition_LE"
       
  2518 by(simp add: wf_inv_image wf_lex_triple addition_LE_def)
       
  2519 
       
  2520 declare addition_inv.simps[simp del]
       
  2521 
       
  2522 lemma addition_inv_init: 
       
  2523   "\<lbrakk>m \<noteq> n; max m n < p; length lm > p; lm ! p = 0\<rbrakk> \<Longrightarrow>
       
  2524                                    addition_inv (0, lm) m n p lm"
       
  2525 apply(simp add: addition_inv.simps)
       
  2526 apply(rule_tac x = "lm ! m" in exI, simp)
       
  2527 done
       
  2528 
       
  2529 thm addition.simps
       
  2530 
       
  2531 lemma [simp]: "abc_fetch 0 (addition m n p) = Some (Dec m 4)"
       
  2532 by(simp add: abc_fetch.simps addition.simps)
       
  2533 
       
  2534 lemma [simp]: "abc_fetch (Suc 0) (addition m n p) = Some (Inc n)"
       
  2535 by(simp add: abc_fetch.simps addition.simps)
       
  2536 
       
  2537 lemma [simp]: "abc_fetch 2 (addition m n p) = Some (Inc p)"
       
  2538 by(simp add: abc_fetch.simps addition.simps)
       
  2539 
       
  2540 lemma [simp]: "abc_fetch 3 (addition m n p) = Some (Goto 0)"
       
  2541 by(simp add: abc_fetch.simps addition.simps)
       
  2542 
       
  2543 lemma [simp]: "abc_fetch 4 (addition m n p) = Some (Dec p 7)"
       
  2544 by(simp add: abc_fetch.simps addition.simps)
       
  2545 
       
  2546 lemma [simp]: "abc_fetch 5 (addition m n p) = Some (Inc m)"
       
  2547 by(simp add: abc_fetch.simps addition.simps)
       
  2548 
       
  2549 lemma [simp]: "abc_fetch 6 (addition m n p) = Some (Goto 4)"
       
  2550 by(simp add: abc_fetch.simps addition.simps)
       
  2551 
       
  2552 lemma [simp]:
       
  2553   "\<lbrakk>m \<noteq> n; p < length lm; lm ! p = 0; m < p; n < p; x \<le> lm ! m; 0 < x\<rbrakk>
       
  2554  \<Longrightarrow> \<exists>xa<lm ! m. lm[m := x, n := lm ! n + lm ! m - x, 
       
  2555                     p := lm ! m - x, m := x - Suc 0] =
       
  2556                  lm[m := xa, n := lm ! n + lm ! m - Suc xa,
       
  2557                     p := lm ! m - Suc xa]"
       
  2558 apply(case_tac x, simp, simp)
       
  2559 apply(rule_tac x = nat in exI, simp add: list_update_swap 
       
  2560                                          list_update_overwrite)
       
  2561 done
       
  2562 
       
  2563 lemma [simp]:
       
  2564   "\<lbrakk>m \<noteq> n; p < length lm; lm ! p = 0; m < p; n < p; x < lm ! m\<rbrakk>
       
  2565    \<Longrightarrow> \<exists>xa<lm ! m. lm[m := x, n := lm ! n + lm ! m - Suc x,
       
  2566                       p := lm ! m - Suc x, n := lm ! n + lm ! m - x]
       
  2567                  = lm[m := xa, n := lm ! n + lm ! m - xa, 
       
  2568                       p := lm ! m - Suc xa]"
       
  2569 apply(rule_tac x = x in exI, 
       
  2570       simp add: list_update_swap list_update_overwrite)
       
  2571 done
       
  2572 
       
  2573 lemma [simp]: 
       
  2574   "\<lbrakk>m \<noteq> n; p < length lm; lm ! p = 0; m < p; n < p; x < lm ! m\<rbrakk>
       
  2575    \<Longrightarrow> \<exists>xa<lm ! m. lm[m := x, n := lm ! n + lm ! m - x, 
       
  2576                           p := lm ! m - Suc x, p := lm ! m - x]
       
  2577                  = lm[m := xa, n := lm ! n + lm ! m - xa, 
       
  2578                           p := lm ! m - xa]"
       
  2579 apply(rule_tac x = x in exI, simp add: list_update_overwrite)
       
  2580 done
       
  2581 
       
  2582 lemma [simp]: 
       
  2583   "\<lbrakk>m \<noteq> n; p < length lm; lm ! p = (0::nat); m < p; n < p; x < lm ! m\<rbrakk>
       
  2584   \<Longrightarrow> \<exists>xa\<le>lm ! m. lm[m := x, n := lm ! n + lm ! m - x,
       
  2585                                    p := lm ! m - x] = 
       
  2586                   lm[m := xa, n := lm ! n + lm ! m - xa, 
       
  2587                                    p := lm ! m - xa]"
       
  2588 apply(rule_tac x = x in exI, simp)
       
  2589 done
       
  2590 
       
  2591 lemma [simp]: 
       
  2592   "\<lbrakk>m \<noteq> n; p < length lm; lm ! p = 0; m < p; n < p;
       
  2593     x \<le> lm ! m; lm ! m \<noteq> x\<rbrakk>
       
  2594   \<Longrightarrow> \<exists>xa<lm ! m. lm[m := x, n := lm ! n + lm ! m, 
       
  2595                        p := lm ! m - x, p := lm ! m - Suc x] 
       
  2596                = lm[m := xa, n := lm ! n + lm ! m, 
       
  2597                        p := lm ! m - Suc xa]"
       
  2598 apply(rule_tac x = x in exI, simp add: list_update_overwrite)
       
  2599 done
       
  2600 
       
  2601 lemma [simp]:
       
  2602   "\<lbrakk>m \<noteq> n; p < length lm; lm ! p = 0; m < p; n < p; x < lm ! m\<rbrakk>
       
  2603   \<Longrightarrow> \<exists>xa<lm ! m. lm[m := x, n := lm ! n + lm ! m,
       
  2604                              p := lm ! m - Suc x, m := Suc x]
       
  2605                 = lm[m := Suc xa, n := lm ! n + lm ! m, 
       
  2606                              p := lm ! m - Suc xa]"
       
  2607 apply(rule_tac x = x in exI, 
       
  2608      simp add: list_update_swap list_update_overwrite)
       
  2609 done
       
  2610 
       
  2611 lemma [simp]: 
       
  2612   "\<lbrakk>m \<noteq> n; p < length lm; lm ! p = 0; m < p; n < p; x < lm ! m\<rbrakk>
       
  2613   \<Longrightarrow> \<exists>xa\<le>lm ! m. lm[m := Suc x, n := lm ! n + lm ! m, 
       
  2614                              p := lm ! m - Suc x] 
       
  2615                = lm[m := xa, n := lm ! n + lm ! m, p := lm ! m - xa]"
       
  2616 apply(rule_tac x = "Suc x" in exI, simp)
       
  2617 done
       
  2618 
       
  2619 lemma addition_halt_lemma: 
       
  2620   "\<lbrakk>m \<noteq> n; max m n < p; length lm > p; lm ! p = 0\<rbrakk> \<Longrightarrow>
       
  2621   \<forall>na. \<not> (\<lambda>(as, lm') (m, p). as = 7) 
       
  2622         (abc_steps_l (0, lm) (addition m n p) na) (m, p) \<and> 
       
  2623   addition_inv (abc_steps_l (0, lm) (addition m n p) na) m n p lm 
       
  2624 \<longrightarrow> addition_inv (abc_steps_l (0, lm) (addition m n p) 
       
  2625                                  (Suc na)) m n p lm 
       
  2626   \<and> ((abc_steps_l (0, lm) (addition m n p) (Suc na), m, p), 
       
  2627      abc_steps_l (0, lm) (addition m n p) na, m, p) \<in> addition_LE"
       
  2628 apply(rule allI, rule impI, simp add: abc_steps_ind)
       
  2629 apply(case_tac "(abc_steps_l (0, lm) (addition m n p) na)", simp)
       
  2630 apply(auto split:if_splits simp add: addition_inv.simps
       
  2631                                  abc_steps_zero)
       
  2632 apply(simp_all add: abc_steps_l.simps abc_steps_zero)
       
  2633 apply(auto simp add: addition_LE_def lex_triple_def lex_pair_def 
       
  2634                      abc_step_l.simps addition_inv.simps 
       
  2635                      abc_lm_v.simps abc_lm_s.simps nth_append
       
  2636                 split: if_splits)
       
  2637 apply(rule_tac x = x in exI, simp)
       
  2638 done
       
  2639 
       
  2640 lemma  addition_ex: 
       
  2641   "\<lbrakk>m \<noteq> n; max m n < p; length lm > p; lm ! p = 0\<rbrakk> \<Longrightarrow> 
       
  2642   \<exists> stp. (\<lambda> (as, lm'). as = 7 \<and> addition_inv (as, lm') m n p lm) 
       
  2643                         (abc_steps_l (0, lm) (addition m n p) stp)"
       
  2644 apply(insert halt_lemma2[of addition_LE
       
  2645   "\<lambda> ((as, lm'), m, p). addition_inv (as, lm') m n p lm"
       
  2646   "\<lambda> stp. (abc_steps_l (0, lm) (addition m n p) stp, m, p)"
       
  2647   "\<lambda> ((as, lm'), m, p). as = 7"], 
       
  2648   simp add: abc_steps_zero addition_inv_init)
       
  2649 apply(drule_tac addition_halt_lemma, simp, simp, simp,
       
  2650       simp, erule_tac exE)
       
  2651 apply(rule_tac x = na in exI, 
       
  2652       case_tac "(abc_steps_l (0, lm) (addition m n p) na)", auto)
       
  2653 done
       
  2654 
       
  2655 lemma [simp]: "length (addition m n p) = 7"
       
  2656 by (simp add: addition.simps)
       
  2657 
       
  2658 lemma [elim]: "addition 0 (Suc 0) 2 = [] \<Longrightarrow> RR"
       
  2659 by(simp add: addition.simps)
       
  2660 
       
  2661 lemma [simp]: "(0\<up>2)[0 := n] = [n, 0::nat]"
       
  2662 apply(subgoal_tac "2 = Suc 1", 
       
  2663       simp only: replicate.simps)
       
  2664 apply(auto)
       
  2665 done
       
  2666 
       
  2667 lemma [simp]: 
       
  2668   "\<exists>stp. abc_steps_l (0, n # 0\<up>2 @ suf_lm) 
       
  2669      (addition 0 (Suc 0) 2 [+] [Inc (Suc 0)]) stp = 
       
  2670                                       (8, n # Suc n # 0 # suf_lm)"
       
  2671 apply(rule_tac bm = "n # n # 0 # suf_lm" in abc_append_exc2, auto)
       
  2672 apply(insert addition_ex[of 0 "Suc 0" 2 "n # 0\<up>2 @ suf_lm"], 
       
  2673       simp add: nth_append numeral_2_eq_2, erule_tac exE)
       
  2674 apply(rule_tac x = stp in exI,
       
  2675       case_tac "(abc_steps_l (0, n # 0\<up>2 @ suf_lm)
       
  2676                       (addition 0 (Suc 0) 2) stp)", 
       
  2677       simp add: addition_inv.simps nth_append list_update_append numeral_2_eq_2)
       
  2678 apply(simp add: nth_append numeral_2_eq_2, erule_tac exE)
       
  2679 apply(rule_tac x = "Suc 0" in exI,
       
  2680       simp add: abc_steps_l.simps abc_fetch.simps 
       
  2681       abc_steps_zero abc_step_l.simps abc_lm_s.simps abc_lm_v.simps)
       
  2682 done
       
  2683 
       
  2684 lemma s_case:
       
  2685   "\<lbrakk>rec_ci s = (aprog, rs_pos, a_md); rec_calc_rel s lm rs\<rbrakk>
       
  2686   \<Longrightarrow> \<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog stp =
       
  2687                (length aprog, lm @ [rs] @ 0\<up>(a_md - rs_pos - 1) @ suf_lm)"
       
  2688 apply(simp add: rec_ci.simps rec_ci_s_def, auto)
       
  2689 apply(rule_tac calc_s_reverse, auto)
       
  2690 done
       
  2691 
       
  2692 lemma [simp]: 
       
  2693   "\<lbrakk>n < length lm; lm ! n = rs\<rbrakk>
       
  2694     \<Longrightarrow> \<exists>stp. abc_steps_l (0, lm @ 0 # 0 #suf_lm)
       
  2695                      (addition n (length lm) (Suc (length lm))) stp 
       
  2696              = (7, lm @ rs # 0 # suf_lm)"
       
  2697 apply(insert addition_ex[of n "length lm"
       
  2698                            "Suc (length lm)" "lm @ 0 # 0 # suf_lm"])
       
  2699 apply(simp add: nth_append, erule_tac exE)
       
  2700 apply(rule_tac x = stp in exI)
       
  2701 apply(case_tac "abc_steps_l (0, lm @ 0 # 0 # suf_lm) (addition n (length lm)
       
  2702                  (Suc (length lm))) stp", simp)
       
  2703 apply(simp add: addition_inv.simps)
       
  2704 apply(insert nth_append[of lm "0 # 0 # suf_lm" "n"], simp)
       
  2705 done
       
  2706 
       
  2707 lemma [simp]: "0\<up>2 = [0, 0::nat]"
       
  2708 apply(auto simp:numeral_2_eq_2)
       
  2709 done
       
  2710 
       
  2711 lemma id_case: 
       
  2712   "\<lbrakk>rec_ci (id m n) = (aprog, rs_pos, a_md); 
       
  2713     rec_calc_rel (id m n) lm rs\<rbrakk>
       
  2714   \<Longrightarrow> \<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog stp = 
       
  2715                (length aprog, lm @ [rs] @ 0\<up>(a_md - rs_pos - 1) @ suf_lm)"
       
  2716 apply(simp add: rec_ci.simps rec_ci_id.simps, auto)
       
  2717 apply(rule_tac calc_id_reverse, simp, simp)
       
  2718 done   
       
  2719 
       
  2720 lemma list_tl_induct:
       
  2721   "\<lbrakk>P []; \<And>a list. P list \<Longrightarrow> P (list @ [a::'a])\<rbrakk> \<Longrightarrow> 
       
  2722                                             P ((list::'a list))"
       
  2723 apply(case_tac "length list", simp)
       
  2724 proof -
       
  2725   fix nat
       
  2726   assume ind: "\<And>a list. P list \<Longrightarrow> P (list @ [a])"
       
  2727   and h: "length list = Suc nat" "P []"
       
  2728   from h show "P list"
       
  2729   proof(induct nat arbitrary: list, case_tac lista, simp, simp)
       
  2730     fix lista a listaa
       
  2731     from h show "P [a]"
       
  2732       by(insert ind[of "[]"], simp add: h)
       
  2733   next
       
  2734     fix nat list
       
  2735     assume nind: "\<And>list. \<lbrakk>length list = Suc nat; P []\<rbrakk> \<Longrightarrow> P list" 
       
  2736     and g: "length (list:: 'a list) = Suc (Suc nat)"
       
  2737     from g show "P (list::'a list)"
       
  2738       apply(insert nind[of "butlast list"], simp add: h)
       
  2739       apply(insert ind[of "butlast list" "last list"], simp)
       
  2740       apply(subgoal_tac "butlast list @ [last list] = list", simp)
       
  2741       apply(case_tac "list::'a list", simp, simp)
       
  2742       done
       
  2743   qed
       
  2744 qed      
       
  2745   
       
  2746 lemma nth_eq_butlast_nth: "\<lbrakk>length ys > Suc k\<rbrakk> \<Longrightarrow> 
       
  2747                                         ys ! k = butlast ys ! k"
       
  2748 apply(subgoal_tac "\<exists> xs y. ys = xs @ [y]", auto simp: nth_append)
       
  2749 apply(rule_tac x = "butlast ys" in exI, rule_tac x = "last ys" in exI)
       
  2750 apply(case_tac "ys = []", simp, simp)
       
  2751 done
       
  2752 
       
  2753 lemma [simp]: 
       
  2754 "\<lbrakk>\<forall>k<Suc (length list). rec_calc_rel ((list @ [a]) ! k) lm (ys ! k);
       
  2755   length ys = Suc (length list)\<rbrakk>
       
  2756    \<Longrightarrow> \<forall>k<length list. rec_calc_rel (list ! k) lm (butlast ys ! k)"
       
  2757 apply(rule allI, rule impI)
       
  2758 apply(erule_tac  x = k in allE, simp add: nth_append)
       
  2759 apply(subgoal_tac "ys ! k = butlast ys ! k", simp)
       
  2760 apply(rule_tac nth_eq_butlast_nth, arith)
       
  2761 done
       
  2762 
       
  2763 lemma cn_merge_gs_tl_app: 
       
  2764   "cn_merge_gs (gs @ [g]) pstr = 
       
  2765         cn_merge_gs gs pstr [+] cn_merge_gs [g] (pstr + length gs)"
       
  2766 apply(induct gs arbitrary: pstr, simp add: cn_merge_gs.simps, simp)
       
  2767 apply(case_tac a, simp add: abc_append_commute)
       
  2768 done
       
  2769 
       
  2770 lemma cn_merge_gs_length: 
       
  2771   "length (cn_merge_gs (map rec_ci list) pstr) = 
       
  2772       (\<Sum>(ap, pos, n)\<leftarrow>map rec_ci list. length ap) + 3 * length list "
       
  2773 apply(induct list arbitrary: pstr, simp, simp)
       
  2774 apply(case_tac "rec_ci a", simp)
       
  2775 done
       
  2776 
       
  2777 lemma [simp]: "Suc n \<le> pstr \<Longrightarrow> pstr + x - n > 0"
       
  2778 by arith
       
  2779 
       
  2780 lemma [simp]:
       
  2781   "\<lbrakk>Suc (pstr + length list) \<le> a_md; 
       
  2782     length ys = Suc (length list);
       
  2783     length lm = n;
       
  2784      Suc n \<le> pstr\<rbrakk>
       
  2785    \<Longrightarrow>  (ys ! length list # 0\<up>(pstr - Suc n) @ butlast ys @
       
  2786              0\<up>(a_md - (pstr + length list)) @ suf_lm) ! 
       
  2787                       (pstr + length list - n) = (0 :: nat)"
       
  2788 apply(insert nth_append[of "ys ! length list # 0\<up>(pstr - Suc n) @
       
  2789      butlast ys" "0\<up>(a_md - (pstr + length list)) @ suf_lm"
       
  2790       "(pstr + length list - n)"], simp add: nth_append)
       
  2791 done
       
  2792 
       
  2793 lemma [simp]:
       
  2794   "\<lbrakk>Suc (pstr + length list) \<le> a_md; 
       
  2795     length ys = Suc (length list);
       
  2796     length lm = n;
       
  2797      Suc n \<le> pstr\<rbrakk>
       
  2798     \<Longrightarrow> (lm @ last ys # 0\<up>(pstr - Suc n) @ butlast ys @
       
  2799          0\<up>(a_md - (pstr + length list)) @ suf_lm)[pstr + length list := 
       
  2800                                         last ys, n := 0] =
       
  2801         lm @ (0::nat)\<up>(pstr - n) @ ys @ 0\<up>(a_md - Suc (pstr + length list)) @ suf_lm"
       
  2802 apply(insert list_update_length[of 
       
  2803    "lm @ last ys # 0\<up>(pstr - Suc n) @ butlast ys" 0 
       
  2804    "0\<up>(a_md - Suc (pstr + length list)) @ suf_lm" "last ys"], simp)
       
  2805 apply(simp add: exponent_cons_iff)
       
  2806 apply(insert list_update_length[of "lm" 
       
  2807         "last ys" "0\<up>(pstr - Suc n) @ butlast ys @ 
       
  2808       last ys # 0\<up>(a_md - Suc (pstr + length list)) @ suf_lm" 0], simp)
       
  2809 apply(simp add: exponent_cons_iff)
       
  2810 apply(case_tac "ys = []", simp_all add: append_butlast_last_id)
       
  2811 done
       
  2812 
       
  2813 lemma cn_merge_gs_ex: 
       
  2814   "\<lbrakk>\<And>x aprog a_md rs_pos rs suf_lm lm.
       
  2815     \<lbrakk>x \<in> set gs; rec_ci x = (aprog, rs_pos, a_md);
       
  2816      rec_calc_rel x lm rs\<rbrakk>
       
  2817      \<Longrightarrow> \<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog stp 
       
  2818            = (length aprog, lm @ [rs] @ 0\<up>(a_md - rs_pos - 1) @ suf_lm); 
       
  2819    pstr + length gs\<le> a_md;
       
  2820    \<forall>k<length gs. rec_calc_rel (gs ! k) lm (ys ! k);
       
  2821    length ys = length gs; length lm = n;
       
  2822    pstr \<ge> Max (set (Suc n # map (\<lambda>(aprog, p, n). n) (map rec_ci gs)))\<rbrakk>
       
  2823   \<Longrightarrow> \<exists> stp. abc_steps_l (0, lm @ 0\<up>(a_md - n) @ suf_lm)
       
  2824                    (cn_merge_gs (map rec_ci gs) pstr) stp 
       
  2825    = (listsum (map ((\<lambda>(ap, pos, n). length ap) \<circ> rec_ci) gs) +
       
  2826   3 * length gs, lm @ 0\<up>(pstr - n) @ ys @ 0\<up>(a_md - (pstr + length gs)) @ suf_lm)"
       
  2827 apply(induct gs arbitrary: ys rule: list_tl_induct)
       
  2828 apply(simp add: exponent_add_iff, simp)
       
  2829 proof -
       
  2830   fix a list ys
       
  2831   assume ind: "\<And>x aprog a_md rs_pos rs suf_lm lm.
       
  2832     \<lbrakk>x = a \<or> x \<in> set list; rec_ci x = (aprog, rs_pos, a_md); 
       
  2833      rec_calc_rel x lm rs\<rbrakk>
       
  2834     \<Longrightarrow> \<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog stp =
       
  2835                 (length aprog, lm @ rs # 0\<up>(a_md - Suc rs_pos) @ suf_lm)"
       
  2836   and ind2: 
       
  2837     "\<And>ys. \<lbrakk>\<And>x aprog a_md rs_pos rs suf_lm lm.
       
  2838     \<lbrakk>x \<in> set list; rec_ci x = (aprog, rs_pos, a_md);
       
  2839      rec_calc_rel x lm rs\<rbrakk>
       
  2840     \<Longrightarrow> \<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog stp
       
  2841         = (length aprog, lm @ rs # 0\<up>(a_md - Suc rs_pos) @ suf_lm);
       
  2842     \<forall>k<length list. rec_calc_rel (list ! k) lm (ys ! k); 
       
  2843     length ys = length list\<rbrakk>
       
  2844     \<Longrightarrow> \<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - n) @ suf_lm) 
       
  2845                    (cn_merge_gs (map rec_ci list) pstr) stp =
       
  2846     (listsum (map ((\<lambda>(ap, pos, n). length ap) \<circ> rec_ci) list) +
       
  2847      3 * length list,
       
  2848                 lm @ 0\<up>(pstr - n) @ ys @ 0\<up>(a_md - (pstr + length list)) @ suf_lm)"
       
  2849     and h: "Suc (pstr + length list) \<le> a_md" 
       
  2850             "\<forall>k<Suc (length list). 
       
  2851                    rec_calc_rel ((list @ [a]) ! k) lm (ys ! k)" 
       
  2852             "length ys = Suc (length list)" 
       
  2853             "length lm = n"
       
  2854             "Suc n \<le> pstr \<and> (\<lambda>(aprog, p, n). n) (rec_ci a) \<le> pstr \<and> 
       
  2855             (\<forall>a\<in>set list. (\<lambda>(aprog, p, n). n) (rec_ci a) \<le> pstr)"
       
  2856   from h have k1: 
       
  2857     "\<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - n) @ suf_lm)
       
  2858                      (cn_merge_gs (map rec_ci list) pstr) stp =
       
  2859     (listsum (map ((\<lambda>(ap, pos, n). length ap) \<circ> rec_ci) list) +
       
  2860      3 * length list, lm @ 0\<up>(pstr - n) @ butlast ys @
       
  2861                                0\<up>(a_md - (pstr + length list)) @ suf_lm) "
       
  2862     apply(rule_tac ind2)
       
  2863     apply(rule_tac ind, auto)
       
  2864     done
       
  2865   from k1 and h show 
       
  2866     "\<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - n) @ suf_lm) 
       
  2867           (cn_merge_gs (map rec_ci list @ [rec_ci a]) pstr) stp =
       
  2868         (listsum (map ((\<lambda>(ap, pos, n). length ap) \<circ> rec_ci) list) + 
       
  2869         (\<lambda>(ap, pos, n). length ap) (rec_ci a) + (3 + 3 * length list),
       
  2870              lm @ 0\<up>(pstr - n) @ ys @ 0\<up>(a_md - Suc (pstr + length list)) @ suf_lm)"
       
  2871     apply(simp add: cn_merge_gs_tl_app)
       
  2872     thm abc_append_exc2
       
  2873     apply(rule_tac as = 
       
  2874   "(\<Sum>(ap, pos, n)\<leftarrow>map rec_ci list. length ap) + 3 * length list"    
       
  2875       and bm = "lm @ 0\<up>(pstr - n) @ butlast ys @ 
       
  2876                               0\<up>(a_md - (pstr + length list)) @ suf_lm" 
       
  2877       and bs = "(\<lambda>(ap, pos, n). length ap) (rec_ci a) + 3" 
       
  2878       and bm' = "lm @ 0\<up>(pstr - n) @ ys @ 0\<up>(a_md - Suc (pstr + length list)) @ 
       
  2879                                   suf_lm" in abc_append_exc2, simp)
       
  2880     apply(simp add: cn_merge_gs_length)
       
  2881   proof -
       
  2882     from h show 
       
  2883       "\<exists>bstp. abc_steps_l (0, lm @ 0\<up>(pstr - n) @ butlast ys @ 
       
  2884                                   0\<up>(a_md - (pstr + length list)) @ suf_lm) 
       
  2885               ((\<lambda>(gprog, gpara, gn). gprog [+] recursive.mv_box gpara 
       
  2886               (pstr + length list)) (rec_ci a)) bstp =
       
  2887               ((\<lambda>(ap, pos, n). length ap) (rec_ci a) + 3, 
       
  2888              lm @ 0\<up>(pstr - n) @ ys @ 0\<up>(a_md - Suc (pstr + length list)) @ suf_lm)"
       
  2889       apply(case_tac "rec_ci a", simp)
       
  2890       apply(rule_tac as = "length aa" and 
       
  2891                      bm = "lm @ (ys ! (length list)) # 
       
  2892           0\<up>(pstr - Suc n) @ butlast ys @ 0\<up>(a_md - (pstr + length list)) @ suf_lm" 
       
  2893         and bs = "3" and bm' = "lm @ 0\<up>(pstr - n) @ ys @
       
  2894              0\<up>(a_md - Suc (pstr + length list)) @ suf_lm" in abc_append_exc2)
       
  2895     proof -
       
  2896       fix aa b c
       
  2897       assume g: "rec_ci a = (aa, b, c)"
       
  2898       from h and g have k2: "b = n"
       
  2899 	apply(erule_tac x = "length list" in allE, simp)
       
  2900 	apply(subgoal_tac "length lm = b", simp)
       
  2901 	apply(rule para_pattern, simp, simp)
       
  2902 	done
       
  2903       from h and g and this show 
       
  2904         "\<exists>astp. abc_steps_l (0, lm @ 0\<up>(pstr - n) @ butlast ys @ 
       
  2905                          0\<up>(a_md - (pstr + length list)) @ suf_lm) aa astp =
       
  2906         (length aa, lm @ ys ! length list # 0\<up>(pstr - Suc n) @ 
       
  2907                        butlast ys @ 0\<up>(a_md - (pstr + length list)) @ suf_lm)"
       
  2908 	apply(subgoal_tac "c \<ge> Suc n")
       
  2909 	apply(insert ind[of a aa b c lm "ys ! length list" 
       
  2910           "0\<up>(pstr - c) @ butlast ys @ 0\<up>(a_md - (pstr + length list)) @ suf_lm"], simp)
       
  2911 	apply(erule_tac x = "length list" in allE, 
       
  2912               simp add: exponent_add_iff)
       
  2913 	apply(rule_tac Suc_leI, rule_tac ci_ad_ge_paras, simp)
       
  2914 	done
       
  2915     next
       
  2916       fix aa b c
       
  2917       show "length aa = length aa" by simp 
       
  2918     next
       
  2919       fix aa b c
       
  2920       assume "rec_ci a = (aa, b, c)"
       
  2921       from h and this show     
       
  2922       "\<exists>bstp. abc_steps_l (0, lm @ ys ! length list #
       
  2923           0\<up>(pstr - Suc n) @ butlast ys @ 0\<up>(a_md - (pstr + length list)) @ suf_lm)
       
  2924                  (recursive.mv_box b (pstr + length list)) bstp =
       
  2925        (3, lm @ 0\<up>(pstr - n) @ ys @ 0\<up>(a_md - Suc (pstr + length list)) @ suf_lm)"
       
  2926 	apply(insert mv_box_ex [of b "pstr + length list" 
       
  2927          "lm @ ys ! length list # 0\<up>(pstr - Suc n) @ butlast ys @ 
       
  2928          0\<up>(a_md - (pstr + length list)) @ suf_lm"], simp)
       
  2929         apply(subgoal_tac "b = n")
       
  2930 	apply(simp add: nth_append split: if_splits)
       
  2931 	apply(erule_tac x = "length list" in allE, simp)
       
  2932         apply(drule para_pattern, simp, simp)
       
  2933 	done
       
  2934     next
       
  2935       fix aa b c
       
  2936       show "3 = length (recursive.mv_box b (pstr + length list))" 
       
  2937         by simp
       
  2938     next
       
  2939       fix aa b aaa ba
       
  2940       show "length aa + 3 = length aa + 3" by simp
       
  2941     next
       
  2942       fix aa b c
       
  2943       show "mv_box b (pstr + length list) \<noteq> []" 
       
  2944         by(simp add: mv_box.simps)
       
  2945     qed
       
  2946   next
       
  2947     show "(\<lambda>(ap, pos, n). length ap) (rec_ci a) + 3 = 
       
  2948         length ((\<lambda>(gprog, gpara, gn). gprog [+]
       
  2949            recursive.mv_box gpara (pstr + length list)) (rec_ci a))"
       
  2950       by(case_tac "rec_ci a", simp)
       
  2951   next
       
  2952     show "listsum (map ((\<lambda>(ap, pos, n). length ap) \<circ> rec_ci) list) +
       
  2953       (\<lambda>(ap, pos, n). length ap) (rec_ci a) + (3 + 3 * length list)=
       
  2954       (\<Sum>(ap, pos, n)\<leftarrow>map rec_ci list. length ap) + 3 * length list + 
       
  2955                 ((\<lambda>(ap, pos, n). length ap) (rec_ci a) + 3)" by simp
       
  2956   next
       
  2957     show "(\<lambda>(gprog, gpara, gn). gprog [+] 
       
  2958       recursive.mv_box gpara (pstr + length list)) (rec_ci a) \<noteq> []"
       
  2959       by(case_tac "rec_ci a", 
       
  2960          simp add: abc_append.simps abc_shift.simps)
       
  2961   qed
       
  2962 qed
       
  2963  
       
  2964 lemma [simp]: "length (mv_boxes aa ba n) = 3*n"
       
  2965 by(induct n, auto simp: mv_boxes.simps)
       
  2966 
       
  2967 lemma exp_suc: "a\<up>Suc b = a\<up>b @ [a]"
       
  2968 by(simp add: exp_ind del: replicate.simps)
       
  2969 
       
  2970 lemma [simp]: 
       
  2971   "\<lbrakk>Suc n \<le> ba - aa;  length lm2 = Suc n;
       
  2972     length lm3 = ba - Suc (aa + n)\<rbrakk>
       
  2973   \<Longrightarrow> (last lm2 # lm3 @ butlast lm2 @ 0 # lm4) ! (ba - aa) = (0::nat)"
       
  2974 proof -
       
  2975   assume h: "Suc n \<le> ba - aa"
       
  2976   and g: "length lm2 = Suc n" "length lm3 = ba - Suc (aa + n)"
       
  2977   from h and g have k: "ba - aa = Suc (length lm3 + n)"
       
  2978     by arith
       
  2979   from  k show 
       
  2980     "(last lm2 # lm3 @ butlast lm2 @ 0 # lm4) ! (ba - aa) = 0"
       
  2981     apply(simp, insert g)
       
  2982     apply(simp add: nth_append)
       
  2983     done
       
  2984 qed
       
  2985 
       
  2986 lemma [simp]: "length lm1 = aa \<Longrightarrow>
       
  2987       (lm1 @ 0\<up>n @ last lm2 # lm3 @ butlast lm2 @ 0 # lm4) ! (aa + n) = last lm2"
       
  2988 apply(simp add: nth_append)
       
  2989 done
       
  2990 
       
  2991 lemma [simp]: "\<lbrakk>Suc n \<le> ba - aa; aa < ba\<rbrakk> \<Longrightarrow> 
       
  2992                     (ba < Suc (aa + (ba - Suc (aa + n) + n))) = False"
       
  2993 apply arith
       
  2994 done
       
  2995 
       
  2996 lemma [simp]: "\<lbrakk>Suc n \<le> ba - aa; aa < ba; length lm1 = aa; 
       
  2997        length lm2 = Suc n; length lm3 = ba - Suc (aa + n)\<rbrakk>
       
  2998      \<Longrightarrow> (lm1 @ 0\<up>n @ last lm2 # lm3 @ butlast lm2 @ 0 # lm4) ! (ba + n) = 0"
       
  2999 using nth_append[of "lm1 @ (0\<Colon>'a)\<up>n @ last lm2 # lm3 @ butlast lm2" 
       
  3000                      "(0\<Colon>'a) # lm4" "ba + n"]
       
  3001 apply(simp)
       
  3002 done
       
  3003 
       
  3004 lemma [simp]: 
       
  3005  "\<lbrakk>Suc n \<le> ba - aa; aa < ba; length lm1 = aa; length lm2 = Suc n;
       
  3006                  length lm3 = ba - Suc (aa + n)\<rbrakk>
       
  3007   \<Longrightarrow> (lm1 @ 0\<up>n @ last lm2 # lm3 @ butlast lm2 @ (0::nat) # lm4)
       
  3008   [ba + n := last lm2, aa + n := 0] = 
       
  3009   lm1 @ 0 # 0\<up>n @ lm3 @ lm2 @ lm4"
       
  3010 using list_update_append[of "lm1 @ 0\<up>n @ last lm2 # lm3 @ butlast lm2" "0 # lm4" 
       
  3011                             "ba + n" "last lm2"]
       
  3012 apply(simp)
       
  3013 apply(simp add: list_update_append)
       
  3014 apply(simp only: exponent_cons_iff exp_suc, simp)
       
  3015 apply(case_tac lm2, simp, simp)
       
  3016 done
       
  3017 
       
  3018 lemma mv_boxes_ex:
       
  3019   "\<lbrakk>n \<le> ba - aa; ba > aa; length lm1 = aa; 
       
  3020     length (lm2::nat list) = n; length lm3 = ba - aa - n\<rbrakk>
       
  3021      \<Longrightarrow> \<exists> stp. abc_steps_l (0, lm1 @ lm2 @ lm3 @ 0\<up>n @ lm4)
       
  3022        (mv_boxes aa ba n) stp = (3 * n, lm1 @ 0\<up>n @ lm3 @ lm2 @ lm4)"
       
  3023 apply(induct n arbitrary: lm2 lm3 lm4, simp)
       
  3024 apply(rule_tac x = 0 in exI, simp add: abc_steps_zero, 
       
  3025               simp add: mv_boxes.simps del: exp_suc_iff)
       
  3026 apply(rule_tac as = "3 *n" and bm = "lm1 @ 0\<up>n @ last lm2 # lm3 @
       
  3027                butlast lm2 @ 0 # lm4" in abc_append_exc2, simp_all)
       
  3028 apply(simp only: exponent_cons_iff, simp only: exp_suc, simp)
       
  3029 proof -
       
  3030   fix n lm2 lm3 lm4
       
  3031   assume ind:
       
  3032     "\<And>lm2 lm3 lm4. \<lbrakk>length lm2 = n; length lm3 = ba - (aa + n)\<rbrakk> \<Longrightarrow>
       
  3033     \<exists>stp. abc_steps_l (0, lm1 @ lm2 @ lm3 @ 0\<up>n @ lm4) 
       
  3034        (mv_boxes aa ba n) stp = (3 * n, lm1 @ 0\<up>n @ lm3 @ lm2 @ lm4)"
       
  3035   and h: "Suc n \<le> ba - aa" "aa < ba" "length (lm1::nat list) = aa" 
       
  3036          "length (lm2::nat list) = Suc n" 
       
  3037          "length (lm3::nat list) = ba - Suc (aa + n)"
       
  3038   from h show 
       
  3039     "\<exists>astp. abc_steps_l (0, lm1 @ lm2 @ lm3 @ 0\<up>n @ 0 # lm4) 
       
  3040                        (mv_boxes aa ba n) astp = 
       
  3041         (3 * n, lm1 @ 0\<up>n @ last lm2 # lm3 @ butlast lm2 @ 0 # lm4)"
       
  3042     apply(insert ind[of "butlast lm2" "last lm2 # lm3" "0 # lm4"], 
       
  3043           simp)
       
  3044     apply(subgoal_tac "lm1 @ butlast lm2 @ last lm2 # lm3 @ 0\<up>n @ 
       
  3045               0 # lm4 = lm1 @ lm2 @ lm3 @ 0\<up>n @ 0 # lm4", simp, simp)
       
  3046     apply(case_tac "lm2 = []", simp, simp)
       
  3047     done
       
  3048 next
       
  3049   fix n lm2 lm3 lm4
       
  3050   assume h: "Suc n \<le> ba - aa"
       
  3051             "aa < ba" 
       
  3052             "length (lm1::nat list) = aa" 
       
  3053             "length (lm2::nat list) = Suc n" 
       
  3054             "length (lm3::nat list) = ba - Suc (aa + n)"
       
  3055   thus " \<exists>bstp. abc_steps_l (0, lm1 @ 0\<up>n @ last lm2 # lm3 @
       
  3056                        butlast lm2 @ 0 # lm4) 
       
  3057                          (recursive.mv_box (aa + n) (ba + n)) bstp
       
  3058                = (3, lm1 @ 0 # 0\<up>n @ lm3 @ lm2 @ lm4)"
       
  3059     apply(insert mv_box_ex[of "aa + n" "ba + n" 
       
  3060        "lm1 @ 0\<up>n @ last lm2 # lm3 @ butlast lm2 @ 0 # lm4"], simp)
       
  3061     done
       
  3062 qed
       
  3063 (*    
       
  3064 lemma [simp]: "\<lbrakk>Suc n \<le> aa - ba; 
       
  3065                 ba < aa; 
       
  3066                length lm2 = aa - Suc (ba + n)\<rbrakk>
       
  3067       \<Longrightarrow> ((0::nat) # lm2 @ 0\<up>n @ last lm3 # lm4) ! (aa - ba)
       
  3068          = last lm3"
       
  3069 proof -
       
  3070   assume h: "Suc n \<le> aa - ba"
       
  3071     and g: " ba < aa" "length lm2 = aa - Suc (ba + n)"
       
  3072   from h and g have k: "aa - ba = Suc (length lm2 + n)"
       
  3073     by arith
       
  3074   thus "((0::nat) # lm2 @ 0\<up>n @ last lm3 # lm4) ! (aa - ba) = last lm3"
       
  3075     apply(simp,  simp add: nth_append)
       
  3076     done
       
  3077 qed
       
  3078 *)
       
  3079 
       
  3080 lemma [simp]: "\<lbrakk>Suc n \<le> aa - ba; ba < aa; length lm1 = ba; 
       
  3081         length lm2 = aa - Suc (ba + n); length lm3 = Suc n\<rbrakk>
       
  3082    \<Longrightarrow> (lm1 @ butlast lm3 @ 0 # lm2 @ 0\<up>n @ last lm3 # lm4) ! (aa + n) = last lm3"
       
  3083 using nth_append[of "lm1 @ butlast lm3 @ 0 # lm2 @ 0\<up>n" "last lm3 # lm4" "aa + n"]
       
  3084 apply(simp)
       
  3085 done
       
  3086 
       
  3087 lemma [simp]: "\<lbrakk>Suc n \<le> aa - ba; ba < aa; length lm1 = ba; 
       
  3088         length lm2 = aa - Suc (ba + n); length lm3 = Suc n\<rbrakk>
       
  3089      \<Longrightarrow> (lm1 @ butlast lm3 @ 0 # lm2 @ 0\<up>n @ last lm3 # lm4) ! (ba + n) = 0"
       
  3090 apply(simp add: nth_append)
       
  3091 done
       
  3092 
       
  3093 lemma [simp]: "\<lbrakk>Suc n \<le> aa - ba; ba < aa; length lm1 = ba; 
       
  3094         length lm2 = aa - Suc (ba + n); length lm3 = Suc n\<rbrakk> 
       
  3095      \<Longrightarrow> (lm1 @ butlast lm3 @ 0 # lm2 @ 0\<up>n @ last lm3 # lm4)[ba + n := last lm3, aa + n := 0]
       
  3096       = lm1 @ lm3 @ lm2 @ 0 # 0\<up>n @ lm4"
       
  3097 using list_update_append[of "lm1 @ butlast lm3" "(0\<Colon>'a) # lm2 @ (0\<Colon>'a)\<up>n @ last lm3 # lm4"]
       
  3098 apply(simp)
       
  3099 using list_update_append[of "lm1 @ butlast lm3 @ last lm3 # lm2 @ (0\<Colon>'a)\<up>n"
       
  3100                             "last lm3 # lm4" "aa + n" "0"]
       
  3101 apply(simp)
       
  3102 apply(simp only: replicate_Suc[THEN sym] exp_suc, simp)
       
  3103 apply(case_tac lm3, simp, simp)
       
  3104 done
       
  3105 
       
  3106 lemma mv_boxes_ex2:
       
  3107   "\<lbrakk>n \<le> aa - ba; 
       
  3108     ba < aa; 
       
  3109     length (lm1::nat list) = ba;
       
  3110     length (lm2::nat list) = aa - ba - n; 
       
  3111     length (lm3::nat list) = n\<rbrakk>
       
  3112      \<Longrightarrow> \<exists> stp. abc_steps_l (0, lm1 @ 0\<up>n @ lm2 @ lm3 @ lm4) 
       
  3113                 (mv_boxes aa ba n) stp =
       
  3114                     (3 * n, lm1 @ lm3 @ lm2 @ 0\<up>n @ lm4)"
       
  3115 apply(induct n arbitrary: lm2 lm3 lm4, simp)
       
  3116 apply(rule_tac x = 0 in exI, simp add: abc_steps_zero, 
       
  3117                    simp add: mv_boxes.simps del: exp_suc_iff)
       
  3118 apply(rule_tac as = "3 *n" and bm = "lm1 @ butlast lm3 @ 0 # lm2 @
       
  3119                   0\<up>n @ last lm3 # lm4" in abc_append_exc2, simp_all)
       
  3120 apply(simp only: exponent_cons_iff, simp only: exp_suc, simp)
       
  3121 proof -
       
  3122   fix n lm2 lm3 lm4
       
  3123   assume ind: 
       
  3124 "\<And>lm2 lm3 lm4. \<lbrakk>length lm2 = aa - (ba + n); length lm3 = n\<rbrakk> \<Longrightarrow> 
       
  3125   \<exists>stp. abc_steps_l (0, lm1 @ 0\<up>n @ lm2 @ lm3 @ lm4) 
       
  3126                  (mv_boxes aa ba n) stp = 
       
  3127                             (3 * n, lm1 @ lm3 @ lm2 @ 0\<up>n @ lm4)"
       
  3128   and h: "Suc n \<le> aa - ba" 
       
  3129          "ba < aa"  
       
  3130          "length (lm1::nat list) = ba" 
       
  3131          "length (lm2::nat list) = aa - Suc (ba + n)" 
       
  3132          "length (lm3::nat list) = Suc n"
       
  3133   from h show
       
  3134     "\<exists>astp. abc_steps_l (0, lm1 @ 0\<up>n @ 0 # lm2 @ lm3 @ lm4)
       
  3135         (mv_boxes aa ba n) astp = 
       
  3136           (3 * n, lm1 @ butlast lm3 @ 0 # lm2 @ 0\<up>n @ last lm3 # lm4)"
       
  3137     apply(insert ind[of "0 # lm2" "butlast lm3" "last lm3 # lm4"],
       
  3138           simp)
       
  3139     apply(subgoal_tac
       
  3140       "lm1 @ 0\<up>n @ 0 # lm2 @ butlast lm3 @ last lm3 # lm4 =
       
  3141            lm1 @ 0\<up>n @ 0 # lm2 @ lm3 @ lm4", simp, simp)
       
  3142     apply(case_tac "lm3 = []", simp, simp)
       
  3143     done
       
  3144 next
       
  3145   fix n lm2 lm3 lm4
       
  3146   assume h:
       
  3147     "Suc n \<le> aa - ba" 
       
  3148     "ba < aa"
       
  3149     "length lm1 = ba"
       
  3150     "length (lm2::nat list) = aa - Suc (ba + n)" 
       
  3151     "length (lm3::nat list) = Suc n"
       
  3152   thus
       
  3153     "\<exists>bstp. abc_steps_l (0, lm1 @ butlast lm3 @ 0 # lm2 @ 0\<up>n @ 
       
  3154                                last lm3 # lm4) 
       
  3155            (recursive.mv_box (aa + n) (ba + n)) bstp =
       
  3156                  (3, lm1 @ lm3 @ lm2 @ 0 # 0\<up>n @ lm4)"
       
  3157     apply(insert mv_box_ex[of "aa + n" "ba + n" "lm1 @ butlast lm3 @ 
       
  3158                           0 # lm2 @ 0\<up>n @ last lm3 # lm4"], simp)
       
  3159     done
       
  3160 qed
       
  3161 
       
  3162 lemma cn_merge_gs_len: 
       
  3163   "length (cn_merge_gs (map rec_ci gs) pstr) = 
       
  3164       (\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 3 * length gs"
       
  3165 apply(induct gs arbitrary: pstr, simp, simp)
       
  3166 apply(case_tac "rec_ci a", simp)
       
  3167 done
       
  3168 
       
  3169 lemma [simp]: "n < pstr \<Longrightarrow>
       
  3170      Suc (pstr + length ys - n) = Suc (pstr + length ys) - n"
       
  3171 by arith
       
  3172 
       
  3173 lemma save_paras':  
       
  3174   "\<lbrakk>length lm = n; pstr > n; a_md > pstr + length ys + n\<rbrakk>
       
  3175   \<Longrightarrow> \<exists>stp. abc_steps_l (0, lm @ 0\<up>(pstr - n) @ ys @
       
  3176                0\<up>(a_md - pstr - length ys) @ suf_lm) 
       
  3177                  (mv_boxes 0 (pstr + Suc (length ys)) n) stp
       
  3178         = (3 * n, 0\<up>pstr @ ys @ 0 # lm @ 0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm)"
       
  3179 thm mv_boxes_ex
       
  3180 apply(insert mv_boxes_ex[of n "pstr + Suc (length ys)" 0 "[]" "lm" 
       
  3181          "0\<up>(pstr - n) @ ys @ [0]" "0\<up>(a_md - pstr - length ys - n - Suc 0) @ suf_lm"], simp)
       
  3182 apply(erule_tac exE, rule_tac x = stp in exI,
       
  3183                             simp add: exponent_add_iff)
       
  3184 apply(simp only: exponent_cons_iff, simp)
       
  3185 done
       
  3186 
       
  3187 lemma [simp]:
       
  3188  "(max ba (Max (insert ba (((\<lambda>(aprog, p, n). n) o rec_ci) ` set gs))))
       
  3189  = (Max (insert ba (((\<lambda>(aprog, p, n). n) o rec_ci) ` set gs)))"
       
  3190 apply(rule min_max.sup_absorb2, auto)
       
  3191 done
       
  3192 
       
  3193 lemma [simp]:
       
  3194   "((\<lambda>(aprog, p, n). n) ` rec_ci ` set gs) = 
       
  3195                   (((\<lambda>(aprog, p, n). n) o rec_ci) ` set gs)"
       
  3196 apply(induct gs)
       
  3197 apply(simp, simp)
       
  3198 done
       
  3199 
       
  3200 lemma ci_cn_md_def:  
       
  3201   "\<lbrakk>rec_ci (Cn n f gs) = (aprog, rs_pos, a_md); 
       
  3202   rec_ci f = (a, aa, ba)\<rbrakk>
       
  3203     \<Longrightarrow> a_md = max (Suc n) (Max (insert ba (((\<lambda>(aprog, p, n). n) o 
       
  3204   rec_ci) ` set gs))) + Suc (length gs) + n"
       
  3205 apply(simp add: rec_ci.simps, auto)
       
  3206 done
       
  3207 
       
  3208 lemma save_paras_prog_ex:
       
  3209   "\<lbrakk>rec_ci (Cn n f gs) = (aprog, rs_pos, a_md); 
       
  3210     rec_ci f = (a, aa, ba); 
       
  3211     pstr = Max (set (Suc n # ba # map (\<lambda>(aprog, p, n). n)
       
  3212                                     (map rec_ci (f # gs))))\<rbrakk>
       
  3213     \<Longrightarrow> \<exists>ap bp cp. 
       
  3214       aprog = ap [+] bp [+] cp \<and>
       
  3215       length ap = (\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 
       
  3216               3 * length gs \<and> bp = mv_boxes 0 (pstr + Suc (length gs)) n"
       
  3217 apply(simp add: rec_ci.simps)
       
  3218 apply(rule_tac x = 
       
  3219   "cn_merge_gs (map rec_ci gs) (max (Suc n) (Max (insert ba 
       
  3220       (((\<lambda>(aprog, p, n). n) o rec_ci) ` set gs))))" in exI,
       
  3221       simp add: cn_merge_gs_len)
       
  3222 apply(rule_tac x = 
       
  3223   "mv_boxes (max (Suc n) (Max (insert ba (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs))))
       
  3224    0 (length gs) [+] a [+]recursive.mv_box aa (max (Suc n) 
       
  3225    (Max (insert ba (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) [+]
       
  3226    empty_boxes (length gs) [+] recursive.mv_box (max (Suc n) 
       
  3227   (Max (insert ba (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) n [+]
       
  3228    mv_boxes (Suc (max (Suc n) (Max (insert ba (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) 
       
  3229    ` set gs))) + length gs)) 0 n" in exI, auto)
       
  3230 apply(simp add: abc_append_commute)
       
  3231 done
       
  3232 
       
  3233 lemma save_paras: 
       
  3234   "\<lbrakk>rec_ci (Cn n f gs) = (aprog, rs_pos, a_md);
       
  3235     rs_pos = n;
       
  3236     \<forall>k<length gs. rec_calc_rel (gs ! k) lm (ys ! k);
       
  3237     length ys = length gs;
       
  3238     length lm = n;
       
  3239     rec_ci f = (a, aa, ba);
       
  3240     pstr = Max (set (Suc n # ba # map (\<lambda>(aprog, p, n). n)
       
  3241                                           (map rec_ci (f # gs))))\<rbrakk>
       
  3242   \<Longrightarrow> \<exists>stp. abc_steps_l ((\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) +
       
  3243           3 * length gs, lm @ 0\<up>(pstr - n) @ ys @
       
  3244                  0\<up>(a_md - pstr - length ys) @ suf_lm) aprog stp = 
       
  3245            ((\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) +
       
  3246                       3 * length gs + 3 * n, 
       
  3247              0\<up>pstr @ ys @ 0 # lm @ 0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm)"
       
  3248 proof -
       
  3249   assume h:
       
  3250     "rec_ci (Cn n f gs) = (aprog, rs_pos, a_md)"
       
  3251     "rs_pos = n" 
       
  3252     "\<forall>k<length gs. rec_calc_rel (gs ! k) lm (ys ! k)" 
       
  3253     "length ys = length gs"  
       
  3254     "length lm = n"    
       
  3255     "rec_ci f = (a, aa, ba)"
       
  3256     and g: "pstr = Max (set (Suc n # ba # map (\<lambda>(aprog, p, n). n) 
       
  3257                                         (map rec_ci (f # gs))))"
       
  3258   from h and g have k1: 
       
  3259     "\<exists> ap bp cp. aprog = ap [+] bp [+] cp \<and> 
       
  3260     length ap = (\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 
       
  3261                 3 *length gs \<and> bp = mv_boxes 0 (pstr + Suc (length ys)) n"
       
  3262     apply(drule_tac save_paras_prog_ex, auto)
       
  3263     done
       
  3264   from h have k2: 
       
  3265     "\<exists> stp. abc_steps_l (0, lm @ 0\<up>(pstr - n) @ ys @ 
       
  3266                          0\<up>(a_md - pstr - length ys) @ suf_lm)
       
  3267          (mv_boxes 0 (pstr + Suc (length ys)) n) stp = 
       
  3268         (3 * n, 0\<up>pstr @ ys @ 0 # lm @ 0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm)"
       
  3269     apply(rule_tac save_paras', simp, simp_all add: g)
       
  3270     apply(drule_tac a = a and aa = aa and ba = ba in 
       
  3271                                         ci_cn_md_def, simp, simp)
       
  3272     done
       
  3273   from k1 show 
       
  3274     "\<exists>stp. abc_steps_l ((\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 
       
  3275          3 * length gs, lm @ 0\<up>(pstr - n) @ ys @ 
       
  3276                  0\<up>(a_md - pstr - length ys) @ suf_lm) aprog stp =
       
  3277              ((\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 
       
  3278                3 * length gs + 3 * n, 
       
  3279                 0\<up> pstr @ ys @ 0 # lm @ 0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm)"
       
  3280   proof(erule_tac exE, erule_tac exE, erule_tac exE, erule_tac exE)
       
  3281     fix ap bp apa cp
       
  3282     assume "aprog = ap [+] bp [+] cp \<and> length ap = 
       
  3283             (\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 3 * length gs
       
  3284             \<and> bp = mv_boxes 0 (pstr + Suc (length ys)) n"
       
  3285     from this and k2 show "?thesis"
       
  3286       apply(simp)
       
  3287       apply(rule_tac abc_append_exc1, simp, simp, simp)
       
  3288       done
       
  3289   qed
       
  3290 qed
       
  3291  
       
  3292 lemma ci_cn_para_eq:
       
  3293   "rec_ci (Cn n f gs) = (aprog, rs_pos, a_md) \<Longrightarrow> rs_pos = n"
       
  3294 apply(simp add: rec_ci.simps, case_tac "rec_ci f", simp)
       
  3295 done
       
  3296 
       
  3297 lemma calc_gs_prog_ex: 
       
  3298   "\<lbrakk>rec_ci (Cn n f gs) = (aprog, rs_pos, a_md); 
       
  3299     rec_ci f = (a, aa, ba);
       
  3300     Max (set (Suc n # ba # map (\<lambda>(aprog, p, n). n) 
       
  3301                          (map rec_ci (f # gs)))) = pstr\<rbrakk>
       
  3302    \<Longrightarrow> \<exists>ap bp. aprog = ap [+] bp \<and> 
       
  3303                  ap = cn_merge_gs (map rec_ci gs) pstr"
       
  3304 apply(simp add: rec_ci.simps)
       
  3305 apply(rule_tac x = "mv_boxes 0 (Suc (max (Suc n)  
       
  3306    (Max (insert ba (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs))) + length gs)) n [+]
       
  3307    mv_boxes (max (Suc n) (Max (insert ba 
       
  3308   (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) 0 (length gs) [+]
       
  3309    a [+] recursive.mv_box aa (max (Suc n)
       
  3310     (Max (insert ba (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) [+]
       
  3311    empty_boxes (length gs) [+] recursive.mv_box (max (Suc n)
       
  3312     (Max (insert ba (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) n [+]
       
  3313     mv_boxes (Suc (max (Suc n) (Max 
       
  3314     (insert ba (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs))) + length gs)) 0 n"
       
  3315    in exI)
       
  3316 apply(auto simp: abc_append_commute)
       
  3317 done
       
  3318 
       
  3319 lemma cn_calc_gs: 
       
  3320   assumes ind: 
       
  3321   "\<And>x aprog a_md rs_pos rs suf_lm lm.
       
  3322   \<lbrakk>x \<in> set gs; 
       
  3323    rec_ci x = (aprog, rs_pos, a_md); 
       
  3324    rec_calc_rel x lm rs\<rbrakk>
       
  3325   \<Longrightarrow> \<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog stp = 
       
  3326      (length aprog, lm @ [rs] @ 0\<up>(a_md - rs_pos - 1) @ suf_lm)"
       
  3327   and h:  "rec_ci (Cn n f gs) = (aprog, rs_pos, a_md)"  
       
  3328           "\<forall>k<length gs. rec_calc_rel (gs ! k) lm (ys ! k)"
       
  3329           "length ys = length gs" 
       
  3330           "length lm = n" 
       
  3331           "rec_ci f = (a, aa, ba)" 
       
  3332           "Max (set (Suc n # ba # map (\<lambda>(aprog, p, n). n) 
       
  3333                                (map rec_ci (f # gs)))) = pstr"
       
  3334   shows  
       
  3335   "\<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog stp =
       
  3336   ((\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 3 * length gs, 
       
  3337    lm @ 0\<up>(pstr - n) @ ys @ 0\<up>(a_md -pstr - length ys) @ suf_lm) "
       
  3338 proof -
       
  3339   from h have k1:
       
  3340     "\<exists> ap bp. aprog = ap [+] bp \<and> ap = 
       
  3341                         cn_merge_gs (map rec_ci gs) pstr"
       
  3342     by(erule_tac calc_gs_prog_ex, auto)
       
  3343   from h have j1: "rs_pos = n"
       
  3344     by(simp add: ci_cn_para_eq)
       
  3345   from h have j2: "a_md \<ge> pstr"
       
  3346     by(drule_tac a = a and aa = aa and ba = ba in 
       
  3347                                 ci_cn_md_def, simp, simp)
       
  3348   from h have j3: "pstr > n"
       
  3349     by(auto)    
       
  3350   from j1 and j2 and j3 and h have k2:
       
  3351     "\<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) 
       
  3352                          (cn_merge_gs (map rec_ci gs) pstr) stp 
       
  3353     = ((\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 3 * length gs, 
       
  3354                   lm @ 0\<up>(pstr - n) @ ys @ 0\<up>(a_md - pstr - length ys) @ suf_lm)"
       
  3355     apply(simp)
       
  3356     apply(rule_tac cn_merge_gs_ex, rule_tac ind, simp, simp, auto)
       
  3357     apply(drule_tac a = a and aa = aa and ba = ba in 
       
  3358                                  ci_cn_md_def, simp, simp)
       
  3359     apply(rule min_max.le_supI2, auto)
       
  3360     done
       
  3361   from k1 show "?thesis"
       
  3362   proof(erule_tac exE, erule_tac exE, simp)
       
  3363     fix ap bp
       
  3364     from k2 show 
       
  3365       "\<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm)
       
  3366            (cn_merge_gs (map rec_ci gs) pstr [+] bp) stp =
       
  3367       (listsum (map ((\<lambda>(ap, pos, n). length ap) \<circ> rec_ci) gs) +
       
  3368          3 * length gs, 
       
  3369          lm @ 0\<up>(pstr - n) @ ys @ 0\<up>(a_md - (pstr + length ys)) @ suf_lm)"
       
  3370       apply(insert abc_append_exc1[of 
       
  3371         "lm @ 0\<up>(a_md - rs_pos) @ suf_lm" 
       
  3372         "(cn_merge_gs (map rec_ci gs) pstr)" 
       
  3373         "length (cn_merge_gs (map rec_ci gs) pstr)" 
       
  3374         "lm @ 0\<up>(pstr - n) @ ys @ 0\<up>(a_md - pstr - length ys) @ suf_lm" 0 
       
  3375         "[]" bp], simp add: cn_merge_gs_len)
       
  3376       done      
       
  3377   qed
       
  3378 qed
       
  3379 
       
  3380 lemma reset_new_paras': 
       
  3381   "\<lbrakk>length lm = n; 
       
  3382     pstr > 0; 
       
  3383     a_md \<ge> pstr + length ys + n;
       
  3384      pstr > length ys\<rbrakk> \<Longrightarrow>
       
  3385    \<exists>stp. abc_steps_l (0, 0\<up>pstr @ ys @ 0 # lm @  0\<up>(a_md - Suc (pstr + length ys + n)) @
       
  3386           suf_lm) (mv_boxes pstr 0 (length ys)) stp =
       
  3387   (3 * length ys, ys @ 0\<up>pstr @ 0 # lm @ 0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm)"
       
  3388 thm mv_boxes_ex2
       
  3389 apply(insert mv_boxes_ex2[of "length ys" "pstr" 0 "[]"
       
  3390      "0\<up>(pstr - length ys)" "ys" 
       
  3391      "0 # lm @ 0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm"], 
       
  3392      simp add: exponent_add_iff)
       
  3393 done
       
  3394 
       
  3395 lemma [simp]:  
       
  3396   "\<lbrakk>rec_ci (Cn n f gs) = (aprog, rs_pos, a_md);
       
  3397   rec_calc_rel f ys rs; rec_ci f = (a, aa, ba);
       
  3398   pstr = Max (set (Suc n # ba # map (\<lambda>(aprog, p, n). n) 
       
  3399                (map rec_ci (f # gs))))\<rbrakk>
       
  3400   \<Longrightarrow> length ys < pstr"
       
  3401 apply(subgoal_tac "length ys = aa", simp)
       
  3402 apply(subgoal_tac "aa < ba \<and> ba \<le> pstr", 
       
  3403       rule basic_trans_rules(22), auto)
       
  3404 apply(rule min_max.le_supI2)
       
  3405 apply(auto)
       
  3406 apply(erule_tac para_pattern, simp)
       
  3407 done
       
  3408 
       
  3409 lemma reset_new_paras_prog_ex: 
       
  3410   "\<lbrakk>rec_ci (Cn n f gs) = (aprog, rs_pos, a_md); 
       
  3411    rec_ci f = (a, aa, ba);
       
  3412    Max (set (Suc n # ba # map (\<lambda>(aprog, p, n). n) 
       
  3413   (map rec_ci (f # gs)))) = pstr\<rbrakk>
       
  3414   \<Longrightarrow> \<exists> ap bp cp. aprog = ap [+] bp [+] cp \<and> 
       
  3415   length ap = (\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) +
       
  3416            3 *length gs + 3 * n \<and> bp = mv_boxes pstr 0 (length gs)"
       
  3417 apply(simp add: rec_ci.simps)
       
  3418 apply(rule_tac x = "cn_merge_gs (map rec_ci gs) (max (Suc n) 
       
  3419           (Max (insert ba (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) [+] 
       
  3420           mv_boxes 0 (Suc (max (Suc n) (Max (insert ba 
       
  3421            (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs))) + length gs)) n" in exI, 
       
  3422        simp add: cn_merge_gs_len)
       
  3423 apply(rule_tac x = "a [+]
       
  3424      recursive.mv_box aa (max (Suc n) (Max (insert ba 
       
  3425      (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) [+]
       
  3426      empty_boxes (length gs) [+] recursive.mv_box 
       
  3427      (max (Suc n) (Max (insert ba (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) n
       
  3428       [+] mv_boxes (Suc (max (Suc n) (Max (insert ba 
       
  3429      (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs))) + length gs)) 0 n" in exI,
       
  3430        auto simp: abc_append_commute)
       
  3431 done
       
  3432 
       
  3433 lemma reset_new_paras:
       
  3434        "\<lbrakk>rec_ci (Cn n f gs) = (aprog, rs_pos, a_md);
       
  3435         rs_pos = n;
       
  3436         \<forall>k<length gs. rec_calc_rel (gs ! k) lm (ys ! k);
       
  3437         length ys = length gs;
       
  3438         length lm = n;
       
  3439         length ys = aa;
       
  3440         rec_ci f = (a, aa, ba);
       
  3441         pstr = Max (set (Suc n # ba # map (\<lambda>(aprog, p, n). n)
       
  3442                                     (map rec_ci (f # gs))))\<rbrakk>
       
  3443 \<Longrightarrow> \<exists>stp. abc_steps_l ((\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 
       
  3444                                                3 * length gs + 3 * n,
       
  3445         0\<up>pstr @ ys @ 0 # lm @ 0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm) aprog stp =
       
  3446   ((\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 6 * length gs + 3 * n,
       
  3447            ys @ 0\<up>pstr @ 0 # lm @ 0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm)"
       
  3448 proof -
       
  3449   assume h:
       
  3450     "rec_ci (Cn n f gs) = (aprog, rs_pos, a_md)"
       
  3451     "rs_pos = n" 
       
  3452     "length ys = aa"
       
  3453     "\<forall>k<length gs. rec_calc_rel (gs ! k) lm (ys ! k)"
       
  3454     "length ys = length gs"  "length lm = n"    
       
  3455     "rec_ci f = (a, aa, ba)"
       
  3456     and g: "pstr = Max (set (Suc n # ba # map (\<lambda>(aprog, p, n). n)
       
  3457                                          (map rec_ci (f # gs))))"
       
  3458   thm rec_ci.simps
       
  3459   from h and g have k1:
       
  3460     "\<exists> ap bp cp. aprog = ap [+] bp [+] cp \<and> length ap = 
       
  3461     (\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) +
       
  3462           3 *length gs + 3 * n \<and> bp = mv_boxes pstr 0 (length ys)"
       
  3463     by(drule_tac reset_new_paras_prog_ex, auto)
       
  3464   from h have k2:
       
  3465     "\<exists> stp. abc_steps_l (0, 0\<up>pstr @ ys @ 0 # lm @ 0\<up>(a_md - Suc (pstr + length ys + n)) @
       
  3466               suf_lm) (mv_boxes pstr 0 (length ys)) stp = 
       
  3467     (3 * (length ys), 
       
  3468      ys @ 0\<up>pstr @ 0 # lm @ 0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm)"
       
  3469     apply(rule_tac reset_new_paras', simp)
       
  3470     apply(simp add: g)
       
  3471     apply(drule_tac a = a and aa = aa and ba = ba in ci_cn_md_def,
       
  3472       simp, simp add: g, simp)
       
  3473     apply(subgoal_tac "length gs = aa \<and> aa < ba \<and> ba \<le> pstr", arith,
       
  3474           simp add: para_pattern)
       
  3475     apply(insert g, auto intro: min_max.le_supI2)
       
  3476     done
       
  3477   from k1 show 
       
  3478     "\<exists>stp. abc_steps_l ((\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 3
       
  3479     * length gs + 3 * n, 0\<up>pstr @ ys @ 0 # lm @ 0\<up>(a_md - Suc (pstr + length ys + n)) @ 
       
  3480      suf_lm) aprog stp =
       
  3481     ((\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 6 * length gs +
       
  3482       3 * n, ys @ 0\<up>pstr @ 0 # lm @ 0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm)"
       
  3483   proof(erule_tac exE, erule_tac exE, erule_tac exE, erule_tac exE)
       
  3484     fix ap bp apa cp
       
  3485     assume "aprog = ap [+] bp [+] cp \<and> length ap = 
       
  3486       (\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 3 * length gs +
       
  3487                   3 * n \<and> bp = mv_boxes pstr 0 (length ys)"
       
  3488     from this and k2 show "?thesis"
       
  3489       apply(simp)
       
  3490       apply(drule_tac as = 
       
  3491         "(\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 3 * length gs +
       
  3492         3 * n" and ap = ap and cp = cp in abc_append_exc1, auto)
       
  3493       apply(rule_tac x = stp in exI, simp add: h)
       
  3494       using h
       
  3495       apply(simp)
       
  3496       done
       
  3497   qed
       
  3498 qed
       
  3499 
       
  3500 thm rec_ci.simps 
       
  3501 
       
  3502 lemma calc_f_prog_ex: 
       
  3503   "\<lbrakk>rec_ci (Cn n f gs) = (aprog, rs_pos, a_md);
       
  3504     rec_ci f = (a, aa, ba);
       
  3505     Max (set (Suc n # ba # map (\<lambda>(aprog, p, n). n) 
       
  3506                    (map rec_ci (f # gs)))) = pstr\<rbrakk>
       
  3507    \<Longrightarrow> \<exists> ap bp cp. aprog = ap [+] bp [+] cp \<and>
       
  3508   length ap = (\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) +
       
  3509                                 6 *length gs + 3 * n \<and> bp = a"
       
  3510 apply(simp add: rec_ci.simps)
       
  3511 apply(rule_tac x = "cn_merge_gs (map rec_ci gs) (max (Suc n) (Max (insert ba
       
  3512      (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) [+] 
       
  3513      mv_boxes 0 (Suc (max (Suc n) (Max (insert ba 
       
  3514             (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs))) + length gs)) n [+]
       
  3515      mv_boxes (max (Suc n) (Max (insert ba 
       
  3516       (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) 0 (length gs)" in exI,
       
  3517      simp add: cn_merge_gs_len)
       
  3518 apply(rule_tac x = "recursive.mv_box aa (max (Suc n) (Max (insert ba 
       
  3519      (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) [+]
       
  3520      empty_boxes (length gs) [+] recursive.mv_box (max (Suc n) (
       
  3521      Max (insert ba (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) n [+]
       
  3522      mv_boxes (Suc (max (Suc n) (Max (insert ba 
       
  3523      (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs))) + length gs)) 0 n" in exI,
       
  3524   auto simp: abc_append_commute)
       
  3525 done
       
  3526 
       
  3527 lemma calc_cn_f:
       
  3528   assumes ind:
       
  3529   "\<And>x aprog a_md rs_pos rs suf_lm lm.
       
  3530   \<lbrakk>x \<in> set (f # gs);
       
  3531   rec_ci x = (aprog, rs_pos, a_md); 
       
  3532   rec_calc_rel x lm rs\<rbrakk>
       
  3533   \<Longrightarrow> \<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog stp =
       
  3534   (length aprog, lm @ [rs] @ 0\<up>(a_md - rs_pos - 1) @ suf_lm)"
       
  3535   and h: "rec_ci (Cn n f gs) = (aprog, rs_pos, a_md)"
       
  3536   "rec_calc_rel (Cn n f gs) lm rs"
       
  3537   "length ys = length gs"
       
  3538   "rec_calc_rel f ys rs"
       
  3539   "length lm = n"
       
  3540   "rec_ci f = (a, aa, ba)" 
       
  3541   and p: "pstr = Max (set (Suc n # ba # map (\<lambda>(aprog, p, n). n)
       
  3542                                 (map rec_ci (f # gs))))"
       
  3543   shows "\<exists>stp. abc_steps_l   
       
  3544   ((\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 6 * length gs + 3 * n,
       
  3545   ys @ 0\<up>pstr @ 0 # lm @ 0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm) aprog stp =
       
  3546   ((\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 6 * length gs + 
       
  3547                 3 * n + length a,
       
  3548   ys @ rs # 0\<up>pstr @ lm @ 0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm)"
       
  3549 proof -
       
  3550   from h have k1: 
       
  3551     "\<exists> ap bp cp. aprog = ap [+] bp [+] cp \<and> 
       
  3552     length ap = (\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 
       
  3553     6 *length gs + 3 * n \<and> bp = a"
       
  3554     by(drule_tac calc_f_prog_ex, auto)
       
  3555   from h and k1 show "?thesis"
       
  3556   proof (erule_tac exE, erule_tac exE, erule_tac exE, erule_tac exE)
       
  3557     fix ap bp apa cp
       
  3558     assume
       
  3559       "aprog = ap [+] bp [+] cp \<and> 
       
  3560       length ap = (\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 
       
  3561       6 * length gs + 3 * n \<and> bp = a"
       
  3562     from h and this show "?thesis"
       
  3563       apply(simp, rule_tac abc_append_exc1, simp_all)
       
  3564       apply(insert ind[of f "a" aa ba ys rs 
       
  3565         "0\<up>(pstr - ba + length gs) @ 0 # lm @ 
       
  3566         0\<up>(a_md - Suc (pstr + length gs + n)) @ suf_lm"], simp)
       
  3567       apply(subgoal_tac "ba > aa \<and> aa = length gs\<and> pstr \<ge> ba", simp)
       
  3568       apply(simp add: exponent_add_iff)
       
  3569       apply(case_tac pstr, simp add: p)
       
  3570       apply(simp only: exp_suc, simp)
       
  3571       apply(rule conjI, rule ci_ad_ge_paras, simp, rule conjI)
       
  3572       apply(subgoal_tac "length ys = aa", simp,
       
  3573         rule para_pattern, simp, simp)
       
  3574       apply(insert p, simp)
       
  3575       apply(auto intro: min_max.le_supI2)
       
  3576       done
       
  3577   qed
       
  3578 qed
       
  3579 (*
       
  3580 lemma [simp]: 
       
  3581   "\<lbrakk>pstr + length ys + n \<le> a_md; ys \<noteq> []\<rbrakk> \<Longrightarrow> 
       
  3582                           pstr < a_md + length suf_lm"
       
  3583 apply(case_tac "length ys", simp)
       
  3584 apply(arith)
       
  3585 done
       
  3586 *)
       
  3587 
       
  3588 lemma [simp]: 
       
  3589   "pstr > length ys 
       
  3590   \<Longrightarrow> (ys @ rs # 0\<up>pstr @ lm @
       
  3591   0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm) ! pstr = (0::nat)"
       
  3592 apply(simp add: nth_append)
       
  3593 done
       
  3594 
       
  3595 (*
       
  3596 lemma [simp]: "\<lbrakk>length ys < pstr; pstr - length ys = Suc x\<rbrakk>
       
  3597   \<Longrightarrow> pstr - Suc (length ys) = x"
       
  3598 by arith
       
  3599 *)
       
  3600 
       
  3601 lemma [simp]: "pstr > length ys \<Longrightarrow> 
       
  3602       (ys @ rs # 0\<up>pstr @ lm @ 0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm)
       
  3603                                          [pstr := rs, length ys := 0] =
       
  3604        ys @ 0\<up>(pstr - length ys) @ (rs::nat) # 0\<up>length ys @ lm @ 0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm"
       
  3605 apply(auto simp: list_update_append)
       
  3606 apply(case_tac "pstr - length ys",simp_all)
       
  3607 using list_update_length[of 
       
  3608   "0\<up>(pstr - Suc (length ys))" "0" "0\<up>length ys @ lm @ 
       
  3609   0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm" rs]
       
  3610 apply(simp only: exponent_cons_iff exponent_add_iff, simp)
       
  3611 apply(subgoal_tac "pstr - Suc (length ys) = nat", simp, simp)
       
  3612 done
       
  3613 
       
  3614 lemma save_rs': 
       
  3615   "\<lbrakk>pstr > length ys\<rbrakk>
       
  3616   \<Longrightarrow> \<exists>stp. abc_steps_l (0, ys @ rs # 0\<up>pstr @ lm @ 
       
  3617   0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm) 
       
  3618   (recursive.mv_box (length ys) pstr) stp =
       
  3619   (3, ys @ 0\<up>(pstr - (length ys)) @ rs # 
       
  3620   0\<up>length ys  @ lm @ 0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm)"
       
  3621 apply(insert mv_box_ex[of "length ys" pstr 
       
  3622   "ys @ rs # 0\<up>pstr @ lm @ 0\<up>(a_md - Suc(pstr + length ys + n)) @ suf_lm"], 
       
  3623   simp)
       
  3624 done
       
  3625 
       
  3626 
       
  3627 lemma save_rs_prog_ex:
       
  3628   "\<lbrakk>rec_ci (Cn n f gs) = (aprog, rs_pos, a_md);
       
  3629   rec_ci f = (a, aa, ba);
       
  3630   Max (set (Suc n # ba # map (\<lambda>(aprog, p, n). n) 
       
  3631                         (map rec_ci (f # gs)))) = pstr\<rbrakk>
       
  3632   \<Longrightarrow> \<exists> ap bp cp. aprog = ap [+] bp [+] cp \<and>
       
  3633   length ap = (\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) +
       
  3634               6 *length gs + 3 * n + length a
       
  3635   \<and> bp = mv_box aa pstr"
       
  3636 apply(simp add: rec_ci.simps)
       
  3637 apply(rule_tac x =
       
  3638   "cn_merge_gs (map rec_ci gs) (max (Suc n) (Max (insert ba 
       
  3639    (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs))))
       
  3640    [+] mv_boxes 0 (Suc (max (Suc n) (Max (insert ba 
       
  3641    (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs))) + length gs)) n [+]
       
  3642    mv_boxes (max (Suc n) (Max (insert ba (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs))))
       
  3643     0 (length gs) [+] a" 
       
  3644   in exI, simp add: cn_merge_gs_len)
       
  3645 apply(rule_tac x = 
       
  3646   "empty_boxes (length gs) [+]
       
  3647    recursive.mv_box (max (Suc n) (Max (insert ba 
       
  3648     (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) n [+]
       
  3649    mv_boxes (Suc (max (Suc n) (Max (insert ba (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))
       
  3650     + length gs)) 0 n" in exI, 
       
  3651   auto simp: abc_append_commute)
       
  3652 done
       
  3653 
       
  3654 lemma save_rs:  
       
  3655   assumes h: 
       
  3656   "rec_ci (Cn n f gs) = (aprog, rs_pos, a_md)" 
       
  3657   "rec_calc_rel (Cn n f gs) lm rs"
       
  3658   "\<forall>k<length gs. rec_calc_rel (gs ! k) lm (ys ! k)"
       
  3659   "length ys = length gs" 
       
  3660   "rec_calc_rel f ys rs" 
       
  3661   "rec_ci f = (a, aa, ba)"  
       
  3662   "length lm = n"
       
  3663   and pdef: "pstr = Max (set (Suc n # ba # map (\<lambda>(aprog, p, n). n) 
       
  3664                                             (map rec_ci (f # gs))))"
       
  3665   shows "\<exists>stp. abc_steps_l
       
  3666            ((\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 6 * length gs
       
  3667           + 3 * n + length a, ys @ rs # 0\<up>pstr @ lm @
       
  3668              0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm) aprog stp =
       
  3669   ((\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 6 * length gs 
       
  3670   + 3 * n + length a + 3,
       
  3671   ys @ 0\<up>(pstr - length ys) @ rs # 0\<up>length ys @ lm @ 
       
  3672                                0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm)"
       
  3673 proof -
       
  3674   thm rec_ci.simps
       
  3675   from h and pdef have k1: 
       
  3676     "\<exists> ap bp cp. aprog = ap [+] bp [+] cp \<and>
       
  3677     length ap = (\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) +
       
  3678     6 *length gs + 3 * n + length a \<and> bp = mv_box (length ys) pstr "
       
  3679     apply(subgoal_tac "length ys = aa")
       
  3680     apply(drule_tac a = a and aa = aa and ba = ba in save_rs_prog_ex, 
       
  3681       simp, simp, simp)
       
  3682     by(rule_tac para_pattern, simp, simp)
       
  3683   from k1 show 
       
  3684     "\<exists>stp. abc_steps_l
       
  3685     ((\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 6 * length gs + 3 * n
       
  3686     + length a, ys @ rs # 0\<up>pstr @ lm @ 0\<up>(a_md - Suc (pstr + length ys + n)) 
       
  3687     @ suf_lm) aprog stp =
       
  3688     ((\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 6 * length gs + 3 * n
       
  3689     + length a + 3, ys @ 0\<up>(pstr - length ys) @ rs # 
       
  3690     0\<up>length ys @ lm @ 0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm)"
       
  3691   proof (erule_tac exE, erule_tac exE, erule_tac exE, erule_tac exE)
       
  3692     fix ap bp apa cp
       
  3693     assume "aprog = ap [+] bp [+] cp \<and> length ap = 
       
  3694       (\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 6 * length gs + 
       
  3695       3 * n + length a \<and> bp = recursive.mv_box (length ys) pstr"
       
  3696     thus"?thesis"
       
  3697       apply(simp, rule_tac abc_append_exc1, simp_all)
       
  3698       apply(rule_tac save_rs', insert h)
       
  3699       apply(subgoal_tac "length gs = aa \<and> pstr \<ge> ba \<and> ba > aa",
       
  3700             arith)
       
  3701       apply(simp add: para_pattern, insert pdef, auto)
       
  3702       apply(rule_tac min_max.le_supI2, simp)
       
  3703       done
       
  3704   qed
       
  3705 qed
       
  3706 
       
  3707 lemma [simp]: "length (empty_boxes n) = 2*n"
       
  3708 apply(induct n, simp, simp)
       
  3709 done
       
  3710 
       
  3711 lemma mv_box_step_ex: "length lm = n \<Longrightarrow> 
       
  3712       \<exists>stp. abc_steps_l (0, lm @ Suc x # suf_lm) [Dec n 2, Goto 0] stp
       
  3713   = (0, lm @ x # suf_lm)"
       
  3714 apply(rule_tac x = "Suc (Suc 0)" in exI, 
       
  3715   simp add: abc_steps_l.simps abc_step_l.simps abc_fetch.simps 
       
  3716          abc_lm_v.simps abc_lm_s.simps nth_append list_update_append)
       
  3717 done
       
  3718 
       
  3719 lemma mv_box_ex': 
       
  3720   "\<lbrakk>length lm = n\<rbrakk> \<Longrightarrow> 
       
  3721   \<exists> stp. abc_steps_l (0, lm @ x # suf_lm) [Dec n 2, Goto 0] stp =
       
  3722   (Suc (Suc 0), lm @ 0 # suf_lm)"
       
  3723 apply(induct x)
       
  3724 apply(rule_tac x = "Suc 0" in exI, 
       
  3725   simp add: abc_steps_l.simps abc_fetch.simps abc_step_l.simps
       
  3726             abc_lm_v.simps nth_append abc_lm_s.simps, simp)
       
  3727 apply(drule_tac x = x and suf_lm = suf_lm in mv_box_step_ex, 
       
  3728       erule_tac exE, erule_tac exE)
       
  3729 apply(rule_tac x = "stpa + stp" in exI, simp add: abc_steps_add)
       
  3730 done
       
  3731 
       
  3732 lemma [simp]: "drop n lm = a # list \<Longrightarrow> list = drop (Suc n) lm"
       
  3733 apply(induct n arbitrary: lm a list, simp)
       
  3734 apply(case_tac "lm", simp, simp)
       
  3735 done
       
  3736 
       
  3737 lemma empty_boxes_ex: "\<lbrakk>length lm \<ge> n\<rbrakk>
       
  3738      \<Longrightarrow> \<exists>stp. abc_steps_l (0, lm) (empty_boxes n) stp = 
       
  3739                                           (2*n, 0\<up>n @ drop n lm)"
       
  3740 apply(induct n, simp, simp)
       
  3741 apply(rule_tac abc_append_exc2, auto)
       
  3742 apply(case_tac "drop n lm", simp, simp)
       
  3743 proof -
       
  3744   fix n stp a list
       
  3745   assume h: "Suc n \<le> length lm"  "drop n lm = a # list"
       
  3746   thus "\<exists>bstp. abc_steps_l (0, 0\<up>n @ a # list) [Dec n 2, Goto 0] bstp =
       
  3747                        (Suc (Suc 0), 0 # 0\<up>n @ drop (Suc n) lm)"
       
  3748     apply(insert mv_box_ex'[of "0\<up>n" n a list], simp, erule_tac exE)
       
  3749     apply(rule_tac x = stp in exI, simp, simp only: exponent_cons_iff)
       
  3750     apply(simp add:exp_ind del: replicate.simps)
       
  3751     done
       
  3752 qed
       
  3753 
       
  3754 
       
  3755 lemma mv_box_paras_prog_ex:
       
  3756   "\<lbrakk>rec_ci (Cn n f gs) = (aprog, rs_pos, a_md);
       
  3757   rec_ci f = (a, aa, ba); 
       
  3758   Max (set (Suc n # ba # map (\<lambda>(aprog, p, n). n) 
       
  3759                     (map rec_ci (f # gs)))) = pstr\<rbrakk>
       
  3760   \<Longrightarrow> \<exists> ap bp cp. aprog = ap [+] bp [+] cp \<and> 
       
  3761   length ap = (\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 
       
  3762   6 *length gs + 3 * n + length a + 3 \<and> bp = empty_boxes (length gs)"
       
  3763 apply(simp add: rec_ci.simps)
       
  3764 apply(rule_tac x = "cn_merge_gs (map rec_ci gs) (max (Suc n) 
       
  3765     (Max (insert ba (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) [+] 
       
  3766     mv_boxes 0 (Suc (max (Suc n) (Max 
       
  3767      (insert ba (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs))) + length gs)) n
       
  3768     [+] mv_boxes (max (Suc n) (Max (insert ba 
       
  3769     (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) 0 (length gs) [+]
       
  3770      a [+] recursive.mv_box aa (max (Suc n) 
       
  3771    (Max (insert ba (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs))))" 
       
  3772     in exI, simp add: cn_merge_gs_len)
       
  3773 apply(rule_tac x = " recursive.mv_box (max (Suc n) (Max (insert ba
       
  3774      (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) n [+]
       
  3775      mv_boxes (Suc (max (Suc n) (Max (insert ba 
       
  3776      (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs))) + length gs)) 0 n" in exI, 
       
  3777   auto simp: abc_append_commute)
       
  3778 done
       
  3779 
       
  3780 lemma mv_box_paras: 
       
  3781  assumes h: 
       
  3782   "rec_ci (Cn n f gs) = (aprog, rs_pos, a_md)" 
       
  3783   "rec_calc_rel (Cn n f gs) lm rs" 
       
  3784   "\<forall>k<length gs. rec_calc_rel (gs ! k) lm (ys ! k)"
       
  3785   "length ys = length gs" 
       
  3786   "rec_calc_rel f ys rs" 
       
  3787   "rec_ci f = (a, aa, ba)" 
       
  3788   and pdef: "pstr = Max (set (Suc n # ba # map (\<lambda>(aprog, p, n). n) 
       
  3789                                              (map rec_ci (f # gs))))"
       
  3790   and starts: "ss = (\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) +
       
  3791                               6 * length gs + 3 * n + length a + 3"
       
  3792   shows "\<exists>stp. abc_steps_l
       
  3793            (ss, ys @ 0\<up>(pstr - length ys) @ rs # 0\<up>length ys 
       
  3794                @ lm @ 0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm) aprog stp =
       
  3795    (ss + 2 * length gs, 0\<up>pstr @ rs # 0\<up>length ys  @ lm @ 
       
  3796                                 0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm)"
       
  3797 proof -
       
  3798   from h and pdef and starts have k1: 
       
  3799     "\<exists> ap bp cp. aprog = ap [+] bp [+] cp \<and>
       
  3800     length ap = (\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 
       
  3801                                6 *length gs + 3 * n + length a + 3
       
  3802     \<and> bp = empty_boxes (length ys)"
       
  3803     by(drule_tac mv_box_paras_prog_ex, auto)
       
  3804   from h have j1: "aa < ba"
       
  3805     by(simp add: ci_ad_ge_paras)
       
  3806   from h have j2: "length gs = aa"
       
  3807     by(drule_tac f = f in para_pattern, simp, simp)
       
  3808   from h and pdef have j3: "ba \<le> pstr"
       
  3809     apply simp 
       
  3810     apply(rule_tac min_max.le_supI2, simp)
       
  3811     done
       
  3812   from k1 show "?thesis"
       
  3813   proof (erule_tac exE, erule_tac exE, erule_tac exE, erule_tac exE)
       
  3814     fix ap bp apa cp
       
  3815     assume "aprog = ap [+] bp [+] cp \<and> 
       
  3816       length ap = (\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) +
       
  3817       6 * length gs + 3 * n + length a + 3 \<and> 
       
  3818       bp = empty_boxes (length ys)"
       
  3819     thus"?thesis"
       
  3820       apply(simp, rule_tac abc_append_exc1, simp_all add: starts h)
       
  3821       apply(insert empty_boxes_ex[of 
       
  3822         "length gs" "ys @ 0\<up>(pstr - (length gs)) @ rs #
       
  3823         0\<up>length gs @ lm @ 0\<up>(a_md - Suc (pstr + length gs + n)) @ suf_lm"], 
       
  3824         simp add: h)
       
  3825       apply(erule_tac exE, rule_tac x = stp in exI, 
       
  3826         simp add: replicate.simps[THEN sym]
       
  3827         replicate_add[THEN sym] del: replicate.simps)
       
  3828       apply(subgoal_tac "pstr >(length gs)", simp)
       
  3829       apply(subgoal_tac "ba > aa \<and> length gs = aa \<and> pstr \<ge> ba", simp)
       
  3830       apply(simp add: j1 j2 j3)
       
  3831       done     
       
  3832   qed
       
  3833 qed
       
  3834 
       
  3835 lemma restore_rs_prog_ex:
       
  3836   "\<lbrakk>rec_ci (Cn n f gs) = (aprog, rs_pos, a_md);
       
  3837   rec_ci f = (a, aa, ba);
       
  3838   Max (set (Suc n # ba # map (\<lambda>(aprog, p, n). n)
       
  3839   (map rec_ci (f # gs)))) = pstr;
       
  3840   ss = (\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) +
       
  3841   8 * length gs + 3 * n + length a + 3\<rbrakk>
       
  3842   \<Longrightarrow> \<exists> ap bp cp. aprog = ap [+] bp [+] cp \<and> length ap = ss \<and> 
       
  3843                                            bp = mv_box pstr n"
       
  3844 apply(simp add: rec_ci.simps)
       
  3845 apply(rule_tac x = "cn_merge_gs (map rec_ci gs) (max (Suc n) 
       
  3846       (Max (insert ba (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) [+] 
       
  3847       mv_boxes 0 (Suc (max (Suc n) (Max (insert ba (((\<lambda>(aprog, p, n). n)
       
  3848         \<circ> rec_ci) ` set gs))) + length gs)) n [+]
       
  3849      mv_boxes (max (Suc n) (Max (insert ba 
       
  3850       (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) 0 (length gs) [+]
       
  3851      a [+] recursive.mv_box aa (max (Suc n)
       
  3852        (Max (insert ba (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) [+]
       
  3853      empty_boxes (length gs)" in exI, simp add: cn_merge_gs_len)
       
  3854 apply(rule_tac x = "mv_boxes (Suc (max (Suc n) 
       
  3855        (Max (insert ba (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs))) 
       
  3856         + length gs)) 0 n" 
       
  3857   in exI, auto simp: abc_append_commute)
       
  3858 done
       
  3859 
       
  3860 lemma exp_add: "a\<up>(b+c) = a\<up>b @ a\<up>c"
       
  3861 apply(simp add:replicate_add)
       
  3862 done
       
  3863 
       
  3864 lemma [simp]: "n < pstr \<Longrightarrow> (0\<up>pstr)[n := rs] @ [0::nat] = 0\<up>n @ rs # 0\<up>(pstr - n)"
       
  3865 using list_update_length[of "0\<up>n" "0::nat" "0\<up>(pstr - Suc n)" rs]
       
  3866 apply(simp add: replicate_Suc[THEN sym] exp_add[THEN sym] exp_suc[THEN sym])
       
  3867 done
       
  3868 
       
  3869 lemma restore_rs:
       
  3870   assumes h: "rec_ci (Cn n f gs) = (aprog, rs_pos, a_md)" 
       
  3871   "rec_calc_rel (Cn n f gs) lm rs" 
       
  3872   "\<forall>k<length gs. rec_calc_rel (gs ! k) lm (ys ! k)"
       
  3873   "length ys = length gs"
       
  3874   "rec_calc_rel f ys rs" 
       
  3875   "rec_ci f = (a, aa, ba)" 
       
  3876   and pdef: "pstr = Max (set (Suc n # ba # map (\<lambda>(aprog, p, n). n)
       
  3877                                         (map rec_ci (f # gs))))"
       
  3878   and starts: "ss = (\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 
       
  3879                               8 * length gs + 3 * n + length a + 3" 
       
  3880   shows "\<exists>stp. abc_steps_l
       
  3881            (ss, 0\<up>pstr @ rs # 0\<up>length ys  @ lm @
       
  3882                     0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm) aprog stp =
       
  3883   (ss + 3, 0\<up>n @ rs # 0\<up>(pstr - n) @ 0\<up>length ys  @ lm @ 
       
  3884                                    0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm)"
       
  3885 proof -
       
  3886  from h and pdef and starts have k1:
       
  3887    "\<exists> ap bp cp. aprog = ap [+] bp [+] cp \<and> length ap = ss \<and> 
       
  3888                                             bp = mv_box pstr n"
       
  3889    by(drule_tac restore_rs_prog_ex, auto)
       
  3890  from k1 show "?thesis"
       
  3891  proof (erule_tac exE, erule_tac exE, erule_tac exE, erule_tac exE)
       
  3892    fix ap bp apa cp
       
  3893    assume "aprog = ap [+] bp [+] cp \<and> length ap = ss \<and> 
       
  3894                                  bp = recursive.mv_box pstr n"
       
  3895    thus"?thesis"
       
  3896      apply(simp, rule_tac abc_append_exc1, simp_all add: starts h)
       
  3897      apply(insert mv_box_ex[of pstr n "0\<up>pstr @ rs # 0\<up>length gs @
       
  3898                      lm @ 0\<up>(a_md - Suc (pstr + length gs + n)) @ suf_lm"], simp)
       
  3899      apply(subgoal_tac "pstr > n", simp)
       
  3900      apply(erule_tac exE, rule_tac x = stp in exI, 
       
  3901                          simp add: nth_append list_update_append)
       
  3902      apply(simp add: pdef)
       
  3903      done
       
  3904   qed
       
  3905 qed
       
  3906 
       
  3907 lemma [simp]:"xs \<noteq> [] \<Longrightarrow> length xs \<ge> Suc 0"
       
  3908 by(case_tac xs, auto)
       
  3909 
       
  3910 lemma  [simp]: "n < max (Suc n) (max ba (Max (((\<lambda>(aprog, p, n). n) o 
       
  3911                                                   rec_ci) ` set gs)))"
       
  3912 by(simp)
       
  3913 
       
  3914 lemma restore_paras_prog_ex: 
       
  3915   "\<lbrakk>rec_ci (Cn n f gs) = (aprog, rs_pos, a_md); 
       
  3916   rec_ci f = (a, aa, ba);
       
  3917   Max (set (Suc n # ba # map (\<lambda>(aprog, p, n). n)
       
  3918                           (map rec_ci (f # gs)))) = pstr;
       
  3919   ss = (\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) +
       
  3920                          8 * length gs + 3 * n + length a + 6\<rbrakk>
       
  3921   \<Longrightarrow> \<exists> ap bp cp. aprog = ap [+] bp [+] cp \<and> length ap = ss \<and> 
       
  3922                       bp = mv_boxes (pstr + Suc (length gs)) (0::nat) n"
       
  3923 apply(simp add: rec_ci.simps)
       
  3924 apply(rule_tac x = "cn_merge_gs (map rec_ci gs) (max (Suc n) 
       
  3925       (Max (insert ba (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs))))
       
  3926       [+] mv_boxes 0 (Suc (max (Suc n) 
       
  3927        (Max (insert ba (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs))) 
       
  3928      + length gs)) n [+] mv_boxes (max (Suc n) 
       
  3929     (Max (insert ba (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) 0 (length gs) [+]
       
  3930      a [+] recursive.mv_box aa (max (Suc n) 
       
  3931       (Max (insert ba (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) [+]
       
  3932      empty_boxes (length gs) [+]
       
  3933      recursive.mv_box (max (Suc n) (Max (insert ba 
       
  3934      (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) n" in exI, simp add: cn_merge_gs_len)
       
  3935 apply(rule_tac x = "[]" in exI, auto simp: abc_append_commute)
       
  3936 done
       
  3937 
       
  3938 lemma restore_paras: 
       
  3939   assumes h: "rec_ci (Cn n f gs) = (aprog, rs_pos, a_md)" 
       
  3940   "rec_calc_rel (Cn n f gs) lm rs" 
       
  3941   "\<forall>k<length gs. rec_calc_rel (gs ! k) lm (ys ! k)"
       
  3942   "length ys = length gs"
       
  3943   "rec_calc_rel f ys rs" 
       
  3944   "rec_ci f = (a, aa, ba)"
       
  3945   and pdef: 
       
  3946   "pstr = Max (set (Suc n # ba # map (\<lambda>(aprog, p, n). n) 
       
  3947                          (map rec_ci (f # gs))))"
       
  3948   and starts: "ss = (\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) + 
       
  3949                               8 * length gs + 3 * n + length a + 6" 
       
  3950   shows "\<exists>stp. abc_steps_l (ss, 0\<up>n @ rs # 0\<up>(pstr - n+ length ys) @
       
  3951                          lm @ 0\<up>(a_md - Suc (pstr + length ys + n)) @ suf_lm)
       
  3952   aprog stp = (ss + 3 * n, lm @ rs # 0\<up>(a_md - Suc n) @ suf_lm)"
       
  3953 proof -
       
  3954   thm rec_ci.simps
       
  3955   from h and pdef and starts have k1:
       
  3956     "\<exists> ap bp cp. aprog = ap [+] bp [+] cp \<and> length ap = ss \<and>
       
  3957                      bp = mv_boxes (pstr + Suc (length gs)) (0::nat) n"
       
  3958     by(drule_tac restore_paras_prog_ex, auto)
       
  3959   from k1 show "?thesis"
       
  3960   proof (erule_tac exE, erule_tac exE, erule_tac exE, erule_tac exE)
       
  3961     fix ap bp apa cp
       
  3962     assume "aprog = ap [+] bp [+] cp \<and> length ap = ss \<and> 
       
  3963                               bp = mv_boxes (pstr + Suc (length gs)) 0 n"
       
  3964     thus"?thesis"
       
  3965       apply(simp, rule_tac abc_append_exc1, simp_all add: starts h)
       
  3966       apply(insert mv_boxes_ex2[of n "pstr + Suc (length gs)" 0 "[]" 
       
  3967         "rs # 0\<up>(pstr - n + length gs)" "lm" 
       
  3968         "0\<up>(a_md - Suc (pstr + length gs + n)) @ suf_lm"], simp)
       
  3969       apply(subgoal_tac "pstr > n \<and> 
       
  3970         a_md > pstr + length gs + n \<and> length lm = n" , simp add: exponent_add_iff h)
       
  3971       using h pdef
       
  3972       apply(simp)     
       
  3973       apply(frule_tac a = a and 
       
  3974         aa = aa and ba = ba in ci_cn_md_def, simp, simp)
       
  3975       apply(subgoal_tac "length lm = rs_pos",
       
  3976         simp add: ci_cn_para_eq, erule_tac para_pattern, simp)
       
  3977       done
       
  3978   qed
       
  3979 qed
       
  3980 
       
  3981 lemma ci_cn_length:
       
  3982   "\<lbrakk>rec_ci (Cn n f gs) = (aprog, rs_pos, a_md); 
       
  3983   rec_calc_rel (Cn n f gs) lm rs;
       
  3984   rec_ci f = (a, aa, ba)\<rbrakk>
       
  3985   \<Longrightarrow> length aprog = (\<Sum>(ap, pos, n)\<leftarrow>map rec_ci gs. length ap) +
       
  3986                              8 * length gs + 6 * n + length a + 6"
       
  3987 apply(simp add: rec_ci.simps, auto simp: cn_merge_gs_len)
       
  3988 done
       
  3989 
       
  3990 lemma  cn_case: 
       
  3991   assumes ind:
       
  3992   "\<And>x aprog a_md rs_pos rs suf_lm lm.
       
  3993   \<lbrakk>x \<in> set (f # gs);
       
  3994   rec_ci x = (aprog, rs_pos, a_md);
       
  3995   rec_calc_rel x lm rs\<rbrakk>
       
  3996   \<Longrightarrow> \<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog stp = 
       
  3997                (length aprog, lm @ [rs] @ 0\<up>(a_md - rs_pos - 1) @ suf_lm)"
       
  3998   and h: "rec_ci (Cn n f gs) = (aprog, rs_pos, a_md)"
       
  3999          "rec_calc_rel (Cn n f gs) lm rs"
       
  4000   shows "\<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog stp 
       
  4001   = (length aprog, lm @ [rs] @ 0\<up>(a_md - rs_pos - 1) @ suf_lm)"
       
  4002 apply(insert h, case_tac "rec_ci f",  rule_tac calc_cn_reverse, simp)
       
  4003 proof -
       
  4004   fix a b c ys
       
  4005   let ?pstr = "Max (set (Suc n # c # (map (\<lambda>(aprog, p, n). n) 
       
  4006                                          (map rec_ci (f # gs)))))"  
       
  4007   let ?gs_len = "listsum (map (\<lambda> (ap, pos, n). length ap) 
       
  4008                                                 (map rec_ci (gs)))"
       
  4009   assume g: "rec_ci (Cn n f gs) = (aprog, rs_pos, a_md)"
       
  4010     "rec_calc_rel (Cn n f gs) lm rs"
       
  4011     "\<forall>k<length gs. rec_calc_rel (gs ! k) lm (ys ! k)" 
       
  4012     "length ys = length gs" 
       
  4013     "rec_calc_rel f ys rs"
       
  4014     "n = length lm"
       
  4015     "rec_ci f = (a, b, c)"  
       
  4016   hence k1:
       
  4017     "\<exists> stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog stp = 
       
  4018     (?gs_len + 3 * length gs, lm @ 0\<up>(?pstr - n) @ ys @
       
  4019                                0\<up>(a_md - ?pstr - length ys) @ suf_lm)"	
       
  4020     apply(rule_tac a = a and aa = b and ba = c in cn_calc_gs)
       
  4021     apply(rule_tac ind, auto)
       
  4022     done  
       
  4023   thm rec_ci.simps
       
  4024   from g have k2: 
       
  4025     "\<exists> stp. abc_steps_l (?gs_len + 3 * length gs,  lm @ 
       
  4026         0\<up>(?pstr - n) @ ys @ 0\<up>(a_md - ?pstr - length ys) @ suf_lm) aprog stp = 
       
  4027     (?gs_len + 3 * length gs + 3 * n, 0\<up>?pstr @ ys @ 0 # lm @ 
       
  4028                               0\<up>(a_md - Suc (?pstr + length ys + n)) @ suf_lm)"
       
  4029     thm save_paras
       
  4030     apply(erule_tac ba = c in save_paras, auto intro: ci_cn_para_eq)
       
  4031     done
       
  4032   from g have k3: 
       
  4033     "\<exists> stp. abc_steps_l (?gs_len + 3 * length gs + 3 * n,
       
  4034     0\<up>?pstr @ ys @ 0 # lm @ 0\<up>(a_md - Suc (?pstr + length ys + n)) @ suf_lm) aprog stp =
       
  4035     (?gs_len + 6 * length gs + 3 * n,  
       
  4036            ys @ 0\<up>?pstr @ 0 # lm @ 0\<up>(a_md - Suc (?pstr + length ys + n)) @ suf_lm)"
       
  4037     apply(erule_tac ba = c in reset_new_paras, 
       
  4038           auto intro: ci_cn_para_eq)
       
  4039     using para_pattern[of f a b c ys rs]
       
  4040     apply(simp)
       
  4041     done
       
  4042   from g have k4: 
       
  4043     "\<exists>stp. abc_steps_l  (?gs_len + 6 * length gs + 3 * n,  
       
  4044     ys @ 0\<up>?pstr @ 0 # lm @ 0\<up>(a_md - Suc (?pstr + length ys + n)) @ suf_lm) aprog stp =
       
  4045     (?gs_len + 6 * length gs + 3 * n + length a, 
       
  4046    ys @ rs # 0\<up>?pstr  @ lm @ 0\<up>(a_md - Suc (?pstr + length ys + n)) @ suf_lm)"
       
  4047     apply(rule_tac ba = c in calc_cn_f, rule_tac ind, auto)
       
  4048     done
       
  4049 thm rec_ci.simps
       
  4050   from g h have k5:
       
  4051     "\<exists> stp. abc_steps_l (?gs_len + 6 * length gs + 3 * n + length a,
       
  4052     ys @ rs # 0\<up>?pstr @ lm @ 0\<up>(a_md - Suc (?pstr + length ys + n)) @ suf_lm)
       
  4053     aprog stp =
       
  4054     (?gs_len + 6 * length gs + 3 * n + length a + 3,
       
  4055     ys @ 0\<up>(?pstr - length ys) @ rs # 0\<up>length ys @ lm @ 
       
  4056     0\<up>(a_md  - Suc (?pstr + length ys + n)) @ suf_lm)"
       
  4057     apply(rule_tac save_rs, auto simp: h)
       
  4058     done
       
  4059   from g have k6: 
       
  4060     "\<exists> stp. abc_steps_l (?gs_len + 6 * length gs + 3 * n + 
       
  4061     length a + 3, ys @ 0\<up>(?pstr - length ys) @ rs # 0\<up>length ys @ lm @ 
       
  4062     0\<up>(a_md  - Suc (?pstr + length ys + n)) @ suf_lm) 
       
  4063     aprog stp =
       
  4064     (?gs_len + 8 * length gs + 3 *n + length a + 3,
       
  4065     0\<up>?pstr @ rs # 0\<up>length ys @ lm @ 
       
  4066                         0\<up>(a_md -Suc (?pstr + length ys + n)) @ suf_lm)"
       
  4067     apply(drule_tac suf_lm = suf_lm in mv_box_paras, auto)
       
  4068     apply(rule_tac x = stp in exI, simp)
       
  4069     done
       
  4070   from g have k7: 
       
  4071     "\<exists> stp. abc_steps_l (?gs_len + 8 * length gs + 3 *n + 
       
  4072     length a + 3, 0\<up>?pstr  @ rs # 0\<up>length ys @ lm @ 
       
  4073     0\<up>(a_md -Suc (?pstr + length ys + n)) @ suf_lm) aprog stp =
       
  4074     (?gs_len + 8 * length gs + 3 * n + length a + 6, 
       
  4075     0\<up>n @ rs # 0\<up>(?pstr  - n) @ 0\<up>length ys @ lm @
       
  4076                         0\<up>(a_md -Suc (?pstr + length ys + n)) @ suf_lm)"
       
  4077     apply(drule_tac suf_lm = suf_lm in restore_rs, auto)
       
  4078     apply(rule_tac x = stp in exI, simp)
       
  4079     done
       
  4080   from g have k8: "\<exists> stp. abc_steps_l (?gs_len + 8 * length gs + 
       
  4081     3 * n + length a + 6,
       
  4082     0\<up>n @ rs # 0\<up>(?pstr  - n) @ 0\<up>length ys @ lm @
       
  4083                       0\<up>(a_md -Suc (?pstr + length ys + n)) @ suf_lm) aprog stp =
       
  4084     (?gs_len + 8 * length gs + 6 * n + length a + 6,
       
  4085                            lm @ rs # 0\<up>(a_md - Suc n) @ suf_lm)"
       
  4086     apply(drule_tac suf_lm = suf_lm in restore_paras, auto)
       
  4087     apply(simp add: exponent_add_iff)
       
  4088     apply(rule_tac x = stp in exI, simp)
       
  4089     done
       
  4090   from g have j1: 
       
  4091     "length aprog = ?gs_len + 8 * length gs + 6 * n + length a + 6"
       
  4092     by(drule_tac a = a and aa = b and ba = c in ci_cn_length,
       
  4093       simp, simp, simp)
       
  4094   from g have j2: "rs_pos = n"
       
  4095     by(simp add: ci_cn_para_eq)
       
  4096   from k1 and k2 and k3 and k4 and k5 and k6 and k7 and k8
       
  4097     and j1 and j2 show 
       
  4098     "\<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog stp = 
       
  4099     (length aprog, lm @ [rs] @ 0\<up>(a_md - rs_pos - 1) @ suf_lm)"
       
  4100     apply(auto)
       
  4101     apply(rule_tac x = "stp + stpa + stpb + stpc +
       
  4102       stpd + stpe + stpf + stpg" in exI, simp add: abc_steps_add)
       
  4103     done
       
  4104 qed
       
  4105 
       
  4106 text {*
       
  4107   Correctness of the complier (terminate case), which says if the execution of 
       
  4108   a recursive function @{text "recf"} terminates and gives result, then 
       
  4109   the Abacus program compiled from @{text "recf"} termintes and gives the same result.
       
  4110   Additionally, to facilitate induction proof, we append @{text "anything"} to the
       
  4111   end of Abacus memory.
       
  4112 *}
       
  4113 
       
  4114 lemma recursive_compile_correct:
       
  4115   "\<lbrakk>rec_ci recf = (ap, arity, fp);
       
  4116     rec_calc_rel recf args r\<rbrakk>
       
  4117   \<Longrightarrow> (\<exists> stp. (abc_steps_l (0, args @ 0\<up>(fp - arity) @ anything) ap stp) = 
       
  4118               (length ap, args@[r]@0\<up>(fp - arity - 1) @ anything))"
       
  4119 apply(induct arbitrary: ap fp arity r anything args
       
  4120   rule: rec_ci.induct)
       
  4121 prefer 5
       
  4122 proof(case_tac "rec_ci g", case_tac "rec_ci f", simp)
       
  4123   fix n f g ap fp arity r anything args  a b c aa ba ca
       
  4124   assume f_ind:
       
  4125     "\<And>ap fp arity r anything args.
       
  4126     \<lbrakk>aa = ap \<and> ba = arity \<and> ca = fp; rec_calc_rel f args r\<rbrakk> \<Longrightarrow> 
       
  4127     \<exists>stp. abc_steps_l (0, args @ 0\<up>(fp - arity) @ anything) ap stp =
       
  4128     (length ap, args @ r # 0\<up>(fp - Suc arity) @ anything)"
       
  4129     and g_ind:
       
  4130     "\<And>x xa y xb ya ap fp arity r anything args.
       
  4131     \<lbrakk>x = (aa, ba, ca); xa = aa \<and> y = (ba, ca); xb = ba \<and> ya = ca; 
       
  4132     a = ap \<and> b = arity \<and> c = fp; rec_calc_rel g args r\<rbrakk>
       
  4133     \<Longrightarrow> \<exists>stp. abc_steps_l (0, args @ 0\<up>(fp - arity) @ anything) ap stp =
       
  4134     (length ap, args @ r # 0\<up>(fp - Suc arity) @ anything)"
       
  4135     and h: "rec_ci (Pr n f g) = (ap, arity, fp)" 
       
  4136     "rec_calc_rel (Pr n f g) args r" 
       
  4137     "rec_ci g = (a, b, c)" 
       
  4138     "rec_ci f = (aa, ba, ca)"
       
  4139   from h have nf_ind: 
       
  4140     "\<And> args r anything. rec_calc_rel f args r \<Longrightarrow> 
       
  4141     \<exists>stp. abc_steps_l (0, args @ 0\<up>(ca - ba) @ anything) aa stp = 
       
  4142     (length aa, args @ r # 0\<up>(ca - Suc ba) @ anything)"
       
  4143     and ng_ind: 
       
  4144     "\<And> args r anything. rec_calc_rel g args r \<Longrightarrow> 
       
  4145     \<exists>stp. abc_steps_l (0, args @ 0\<up>(c - b) @ anything) a stp = 
       
  4146          (length a, args @ r # 0\<up>(c - Suc b)  @ anything)"
       
  4147     apply(insert f_ind[of aa ba ca], simp)
       
  4148     apply(insert g_ind[of "(aa, ba, ca)" aa "(ba, ca)" ba ca a b c],
       
  4149       simp)
       
  4150     done
       
  4151   from nf_ind and ng_ind and h show 
       
  4152     "\<exists>stp. abc_steps_l (0, args @ 0\<up>(fp - arity) @ anything) ap stp = 
       
  4153     (length ap, args @ r # 0\<up>(fp - Suc arity) @ anything)"
       
  4154     apply(auto intro: nf_ind ng_ind pr_case)
       
  4155     done
       
  4156 next
       
  4157   fix ap fp arity r anything args
       
  4158   assume h:
       
  4159     "rec_ci z = (ap, arity, fp)" "rec_calc_rel z args r"
       
  4160   thus "\<exists>stp. abc_steps_l (0, args @ 0\<up>(fp - arity) @ anything) ap stp =
       
  4161     (length ap, args @ [r] @ 0\<up>(fp - arity - 1) @ anything)"
       
  4162     by (rule_tac z_case)    
       
  4163 next
       
  4164   fix ap fp arity r anything args
       
  4165   assume h: 
       
  4166     "rec_ci s = (ap, arity, fp)" 
       
  4167     "rec_calc_rel s args r"
       
  4168   thus 
       
  4169     "\<exists>stp. abc_steps_l (0, args @ 0\<up>(fp - arity) @ anything) ap stp =
       
  4170     (length ap, args @ [r] @ 0\<up>(fp - arity - 1) @ anything)"
       
  4171     by(erule_tac s_case, simp)
       
  4172 next
       
  4173   fix m n ap fp arity r anything args
       
  4174   assume h: "rec_ci (id m n) = (ap, arity, fp)" 
       
  4175     "rec_calc_rel (id m n) args r"
       
  4176   thus "\<exists>stp. abc_steps_l (0, args @ 0\<up>(fp - arity) @ anything) ap stp 
       
  4177     = (length ap, args @ [r] @ 0\<up>(fp - arity - 1) @ anything)"
       
  4178     by(erule_tac id_case)
       
  4179 next
       
  4180   fix n f gs ap fp arity r anything args
       
  4181   assume ind: "\<And>x ap fp arity r anything args.
       
  4182     \<lbrakk>x \<in> set (f # gs); 
       
  4183     rec_ci x = (ap, arity, fp); 
       
  4184     rec_calc_rel x args r\<rbrakk>
       
  4185     \<Longrightarrow> \<exists>stp. abc_steps_l (0, args @ 0\<up>(fp - arity) @ anything) ap stp =
       
  4186     (length ap, args @ [r] @ 0\<up>(fp - arity - 1) @ anything)"
       
  4187   and h: "rec_ci (Cn n f gs) = (ap, arity, fp)" 
       
  4188     "rec_calc_rel (Cn n f gs) args r"
       
  4189   from h show
       
  4190     "\<exists>stp. abc_steps_l (0, args @ 0\<up>(fp - arity) @ anything) 
       
  4191        ap stp = (length ap, args @ [r] @ 0\<up>(fp - arity - 1) @ anything)"
       
  4192     apply(rule_tac cn_case, rule_tac ind, auto)
       
  4193     done
       
  4194 next
       
  4195   fix n f ap fp arity r anything args
       
  4196   assume ind:
       
  4197     "\<And>ap fp arity r anything args.
       
  4198     \<lbrakk>rec_ci f = (ap, arity, fp); rec_calc_rel f args r\<rbrakk> \<Longrightarrow> 
       
  4199     \<exists>stp. abc_steps_l (0, args @ 0\<up>(fp - arity) @ anything) ap stp =
       
  4200     (length ap, args @ [r] @ 0\<up>(fp - arity - 1) @ anything)"
       
  4201   and h: "rec_ci (Mn n f) = (ap, arity, fp)" 
       
  4202     "rec_calc_rel (Mn n f) args r"
       
  4203   from h show 
       
  4204     "\<exists>stp. abc_steps_l (0, args @ 0\<up>(fp - arity) @ anything) ap stp = 
       
  4205               (length ap, args @ [r] @ 0\<up>(fp - arity - 1) @ anything)"
       
  4206     apply(rule_tac mn_case, rule_tac ind, auto)
       
  4207     done    
       
  4208 qed
       
  4209 
       
  4210 lemma abc_append_uhalt1:
       
  4211   "\<lbrakk>\<forall> stp. (\<lambda> (ss, e). ss < length bp) (abc_steps_l (0, lm) bp stp);
       
  4212     p = ap [+] bp [+] cp\<rbrakk>
       
  4213   \<Longrightarrow> \<forall> stp. (\<lambda> (ss, e). ss < length p) 
       
  4214                      (abc_steps_l (length ap, lm) p stp)"
       
  4215 apply(auto)
       
  4216 apply(erule_tac x = stp in allE, auto)
       
  4217 apply(frule_tac ap = ap and cp = cp in abc_append_state_in_exc, auto)
       
  4218 done
       
  4219 
       
  4220 
       
  4221 lemma abc_append_unhalt2:
       
  4222   "\<lbrakk>abc_steps_l (0, am) ap stp = (length ap, lm); bp \<noteq> [];
       
  4223   \<forall> stp. (\<lambda> (ss, e). ss < length bp) (abc_steps_l (0, lm) bp stp);
       
  4224   p = ap [+] bp [+] cp\<rbrakk>
       
  4225   \<Longrightarrow> \<forall> stp. (\<lambda> (ss, e). ss < length p) (abc_steps_l (0, am) p stp)"
       
  4226 proof -
       
  4227   assume h: 
       
  4228     "abc_steps_l (0, am) ap stp = (length ap, lm)" 
       
  4229     "bp \<noteq> []"
       
  4230     "\<forall> stp. (\<lambda> (ss, e). ss < length bp) (abc_steps_l (0, lm) bp stp)"
       
  4231     "p = ap [+] bp [+] cp"
       
  4232   have "\<exists> stp. (abc_steps_l (0, am) p stp) = (length ap, lm)"
       
  4233     using h
       
  4234     thm abc_add_exc1
       
  4235     apply(simp add: abc_append.simps)
       
  4236     apply(rule_tac abc_add_exc1, auto)
       
  4237     done
       
  4238   from this obtain stpa where g1: 
       
  4239     "(abc_steps_l (0, am) p stpa) = (length ap, lm)" ..
       
  4240   moreover have g2: "\<forall> stp. (\<lambda> (ss, e). ss < length p) 
       
  4241                           (abc_steps_l (length ap, lm) p stp)"
       
  4242     using h
       
  4243     apply(erule_tac abc_append_uhalt1, simp)
       
  4244     done
       
  4245   moreover from g1 and g2 have
       
  4246     "\<forall> stp. (\<lambda> (ss, e). ss < length p) 
       
  4247                     (abc_steps_l (0, am) p (stpa + stp))"
       
  4248     apply(simp add: abc_steps_add)
       
  4249     done
       
  4250   thus "\<forall> stp. (\<lambda> (ss, e). ss < length p) 
       
  4251                            (abc_steps_l (0, am) p stp)"
       
  4252     apply(rule_tac allI, auto)
       
  4253     apply(case_tac "stp \<ge>  stpa")
       
  4254     apply(erule_tac x = "stp - stpa" in allE, simp)
       
  4255   proof - 	
       
  4256     fix stp a b
       
  4257     assume g3:  "abc_steps_l (0, am) p stp = (a, b)" 
       
  4258                 "\<not> stpa \<le> stp"
       
  4259     thus "a < length p"
       
  4260       using g1 h
       
  4261       apply(case_tac "a < length p", simp, simp)
       
  4262       apply(subgoal_tac "\<exists> d. stpa = stp + d")
       
  4263       using  abc_state_keep[of p a b "stpa - stp"]
       
  4264       apply(erule_tac exE, simp add: abc_steps_add)
       
  4265       apply(rule_tac x = "stpa - stp" in exI, simp)
       
  4266       done
       
  4267   qed
       
  4268 qed
       
  4269 
       
  4270 text {*
       
  4271   Correctness of the complier (non-terminating case for Mn). There are many cases when a 
       
  4272   recursive function does not terminate. For the purpose of Uiversal Turing Machine, we only 
       
  4273   need to prove the case for @{text "Mn"} and @{text "Cn"}.
       
  4274   This lemma is for @{text "Mn"}. For @{text "Mn n f"}, this lemma describes what 
       
  4275   happens when @{text "f"} always terminates but always does not return zero, so that
       
  4276   @{text "Mn"} has to loop forever.
       
  4277   *}
       
  4278 
       
  4279 lemma Mn_unhalt:
       
  4280   assumes mn_rf: "rf = Mn n f"
       
  4281   and compiled_mnrf: "rec_ci rf = (aprog, rs_pos, a_md)"
       
  4282   and compiled_f: "rec_ci f = (aprog', rs_pos', a_md')"
       
  4283   and args: "length lm = n"
       
  4284   and unhalt_condition: "\<forall> y. (\<exists> rs. rec_calc_rel f (lm @ [y]) rs \<and> rs \<noteq> 0)"
       
  4285   shows "\<forall> stp. case abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm)
       
  4286                aprog stp of (ss, e) \<Rightarrow> ss < length aprog"
       
  4287   using mn_rf compiled_mnrf compiled_f args unhalt_condition
       
  4288 proof(rule_tac allI)
       
  4289   fix stp
       
  4290   assume h: "rf = Mn n f" 
       
  4291             "rec_ci rf = (aprog, rs_pos, a_md)"
       
  4292             "rec_ci f = (aprog', rs_pos', a_md')" 
       
  4293             "\<forall>y. \<exists>rs. rec_calc_rel f (lm @ [y]) rs \<and> rs \<noteq> 0" "length lm = n"
       
  4294   thm mn_ind_step
       
  4295   have "\<exists>stpa \<ge> stp. abc_steps_l (0, lm @ 0 # 0\<up>(a_md - Suc rs_pos) @ suf_lm) aprog stpa 
       
  4296          = (0, lm @ stp # 0\<up>(a_md - Suc rs_pos) @ suf_lm)"
       
  4297   proof(induct stp, auto)
       
  4298     show "\<exists>stpa. abc_steps_l (0, lm @ 0 # 0\<up>(a_md - Suc rs_pos) @ suf_lm) 
       
  4299           aprog stpa = (0, lm @ 0 # 0\<up>(a_md - Suc rs_pos) @ suf_lm)"
       
  4300       apply(rule_tac x = 0 in exI, simp add: abc_steps_l.simps)
       
  4301       done
       
  4302   next
       
  4303     fix stp stpa
       
  4304     assume g1: "stp \<le> stpa"
       
  4305       and g2: "abc_steps_l (0, lm @ 0 # 0\<up>(a_md - Suc rs_pos) @ suf_lm)
       
  4306                             aprog stpa
       
  4307                = (0, lm @ stp # 0\<up>(a_md - Suc rs_pos) @ suf_lm)"
       
  4308     have "\<exists>rs. rec_calc_rel f (lm @ [stp]) rs \<and> rs \<noteq> 0"
       
  4309       using h
       
  4310       apply(erule_tac x = stp in allE, simp)
       
  4311       done
       
  4312     from this obtain rs where g3:
       
  4313       "rec_calc_rel f (lm @ [stp]) rs \<and> rs \<noteq> 0" ..
       
  4314     hence "\<exists> stpb. abc_steps_l (0, lm @ stp # 0\<up>(a_md - Suc rs_pos) @
       
  4315                      suf_lm) aprog stpb 
       
  4316       = (0, lm @ Suc stp # 0\<up>(a_md - Suc rs_pos) @ suf_lm)"
       
  4317       using h
       
  4318       apply(rule_tac mn_ind_step)
       
  4319       apply(rule_tac recursive_compile_correct, simp, simp)
       
  4320     proof -
       
  4321       show "rec_ci f = ((aprog', rs_pos', a_md'))" using h by simp
       
  4322     next
       
  4323       show "rec_ci (Mn n f) = (aprog, rs_pos, a_md)" using h by simp
       
  4324     next
       
  4325       show "rec_calc_rel f (lm @ [stp]) rs" using g3 by simp
       
  4326     next
       
  4327       show "0 < rs" using g3 by simp
       
  4328     next
       
  4329       show "Suc rs_pos < a_md"
       
  4330         using g3 h
       
  4331         apply(auto)
       
  4332         apply(frule_tac f = f in para_pattern, simp, simp)
       
  4333         apply(simp add: rec_ci.simps, auto)
       
  4334         apply(subgoal_tac "Suc (length lm) < a_md'")
       
  4335         apply(arith)
       
  4336         apply(simp add: ci_ad_ge_paras)
       
  4337         done
       
  4338     next
       
  4339       show "rs_pos' = Suc rs_pos"
       
  4340         using g3 h
       
  4341         apply(auto)
       
  4342         apply(frule_tac f = f in para_pattern, simp, simp)
       
  4343         apply(simp add: rec_ci.simps)
       
  4344         done
       
  4345     qed
       
  4346     thus "\<exists>stpa\<ge>Suc stp. abc_steps_l (0, lm @ 0 # 0\<up>(a_md - Suc rs_pos) @
       
  4347                  suf_lm) aprog stpa 
       
  4348       = (0, lm @ Suc stp # 0\<up>(a_md - Suc rs_pos) @ suf_lm)"
       
  4349       using g2
       
  4350       apply(erule_tac exE)
       
  4351       apply(case_tac "stpb = 0", simp add: abc_steps_l.simps)
       
  4352       apply(rule_tac x = "stpa + stpb" in exI, simp add:
       
  4353         abc_steps_add)
       
  4354       using g1
       
  4355       apply(arith)
       
  4356       done
       
  4357   qed
       
  4358   from this obtain stpa where 
       
  4359     "stp \<le> stpa \<and> abc_steps_l (0, lm @ 0 # 0\<up>(a_md - Suc rs_pos) @ suf_lm)
       
  4360          aprog stpa = (0, lm @ stp # 0\<up>(a_md - Suc rs_pos) @ suf_lm)" ..
       
  4361   thus "case abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog stp
       
  4362     of (ss, e) \<Rightarrow> ss < length aprog"
       
  4363     apply(case_tac "abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suf_lm) aprog
       
  4364       stp", simp, case_tac "a \<ge> length aprog", 
       
  4365         simp, simp)
       
  4366     apply(subgoal_tac "\<exists> d. stpa = stp + d", erule_tac exE)
       
  4367     apply(subgoal_tac "lm @ 0\<up>(a_md - rs_pos) @ suf_lm = lm @ 0 # 
       
  4368              0\<up>(a_md - Suc rs_pos) @ suf_lm", simp add: abc_steps_add)
       
  4369     apply(frule_tac as = a and lm = b and stp = d in abc_state_keep, 
       
  4370           simp)
       
  4371     using h  
       
  4372     apply(simp add: rec_ci.simps, simp, 
       
  4373               simp only: replicate_Suc[THEN sym])
       
  4374     apply(case_tac rs_pos, simp, simp)
       
  4375     apply(rule_tac x = "stpa - stp" in exI, simp, simp)
       
  4376     done
       
  4377 qed   
       
  4378 
       
  4379 lemma abc_append_cons_eq[intro!]: 
       
  4380   "\<lbrakk>ap = bp; cp = dp\<rbrakk> \<Longrightarrow> ap [+] cp = bp [+] dp"
       
  4381 by simp 
       
  4382 
       
  4383 lemma cn_merge_gs_split: 
       
  4384   "\<lbrakk>i < length gs; rec_ci (gs!i) = (ga, gb, gc)\<rbrakk> \<Longrightarrow> 
       
  4385      cn_merge_gs (map rec_ci gs) p = 
       
  4386         cn_merge_gs (map rec_ci (take i gs)) p [+] ga [+] 
       
  4387        mv_box gb (p + i) [+] 
       
  4388       cn_merge_gs (map rec_ci (drop (Suc i) gs)) (p + Suc i)"
       
  4389 apply(induct i arbitrary: gs p, case_tac gs, simp, simp)
       
  4390 apply(case_tac gs, simp, case_tac "rec_ci a", 
       
  4391        simp add: abc_append_commute[THEN sym])
       
  4392 done
       
  4393 
       
  4394 text {*
       
  4395   Correctness of the complier (non-terminating case for Mn). There are many cases when a 
       
  4396   recursive function does not terminate. For the purpose of Uiversal Turing Machine, we only 
       
  4397   need to prove the case for @{text "Mn"} and @{text "Cn"}.
       
  4398   This lemma is for @{text "Cn"}. For @{text "Cn f g1 g2 \<dots>gi, gi+1, \<dots> gn"}, this lemma describes what 
       
  4399   happens when every one of @{text "g1, g2, \<dots> gi"} terminates, but 
       
  4400   @{text "gi+1"} does not terminate, so that whole function @{text "Cn f g1 g2 \<dots>gi, gi+1, \<dots> gn"}
       
  4401   does not terminate.
       
  4402   *}
       
  4403 
       
  4404 lemma cn_gi_uhalt: 
       
  4405   assumes cn_recf: "rf = Cn n f gs"
       
  4406   and compiled_cn_recf: "rec_ci rf = (aprog, rs_pos, a_md)"
       
  4407   and args_length: "length lm = n"
       
  4408   and exist_unhalt_recf: "i < length gs" "gi = gs ! i"
       
  4409   and complied_unhalt_recf: "rec_ci gi = (ga, gb, gc)"  "gb = n"
       
  4410   and all_halt_before_gi: "\<forall> j < i. (\<exists> rs. rec_calc_rel (gs!j) lm rs)" 
       
  4411   and unhalt_condition: "\<And> slm. \<forall> stp. case abc_steps_l (0, lm @ 0\<up>(gc - gb) @ slm) 
       
  4412      ga stp of (se, e) \<Rightarrow> se < length ga"
       
  4413   shows " \<forall> stp. case abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suflm) aprog
       
  4414   stp of (ss, e) \<Rightarrow> ss < length aprog"
       
  4415   using cn_recf compiled_cn_recf args_length exist_unhalt_recf complied_unhalt_recf
       
  4416         all_halt_before_gi unhalt_condition
       
  4417 proof(case_tac "rec_ci f", simp)
       
  4418   fix a b c
       
  4419   assume h1: "rf = Cn n f gs" 
       
  4420     "rec_ci (Cn n f gs) = (aprog, rs_pos, a_md)" 
       
  4421     "length lm = n" 
       
  4422     "gi = gs ! i" 
       
  4423     "rec_ci (gs!i) = (ga, n, gc)" 
       
  4424     "gb = n" "rec_ci f = (a, b, c)"
       
  4425     and h2: "\<forall>j<i. \<exists>rs. rec_calc_rel (gs ! j) lm rs"
       
  4426     "i < length gs"
       
  4427   and ind:
       
  4428     "\<And> slm. \<forall> stp. case abc_steps_l (0, lm @ 0\<up>(gc - n) @ slm) ga stp of (se, e) \<Rightarrow> se < length ga"
       
  4429   have h3: "rs_pos = n"
       
  4430     using h1
       
  4431     by(rule_tac ci_cn_para_eq, simp)
       
  4432   let ?ggs = "take i gs"
       
  4433   have "\<exists> ys. (length ys = i \<and> 
       
  4434     (\<forall> k < i. rec_calc_rel (?ggs ! k) lm (ys ! k)))"
       
  4435     using h2
       
  4436     apply(induct i, simp, simp)
       
  4437     apply(erule_tac exE)
       
  4438     apply(erule_tac x = ia in allE, simp)
       
  4439     apply(erule_tac exE)
       
  4440     apply(rule_tac x = "ys @ [x]" in exI, simp add: nth_append, auto)
       
  4441     apply(subgoal_tac "k = length ys", simp, simp)
       
  4442     done
       
  4443   from this obtain ys where g1:
       
  4444     "(length ys = i \<and> (\<forall> k < i. rec_calc_rel (?ggs ! k)
       
  4445                         lm (ys ! k)))" ..
       
  4446   let ?pstr = "Max (set (Suc n # c # map (\<lambda>(aprog, p, n). n)
       
  4447     (map rec_ci (f # gs))))"
       
  4448   have "\<exists>stp. abc_steps_l (0, lm @ 0\<up>(a_md - n) @ suflm) 
       
  4449     (cn_merge_gs (map rec_ci ?ggs) ?pstr) stp =
       
  4450     (listsum (map ((\<lambda>(ap, pos, n). length ap) \<circ> rec_ci) ?ggs) +
       
  4451     3 * length ?ggs, lm @ 0\<up>(?pstr - n) @ ys @ 0\<up>(a_md -(?pstr + length ?ggs)) @
       
  4452     suflm) "
       
  4453     apply(rule_tac  cn_merge_gs_ex)
       
  4454     apply(rule_tac  recursive_compile_correct, simp, simp)
       
  4455     using h1
       
  4456     apply(simp add: rec_ci.simps, auto)
       
  4457     using g1
       
  4458     apply(simp)
       
  4459     using h2 g1
       
  4460     apply(simp)
       
  4461     apply(rule_tac min_max.le_supI2)
       
  4462     apply(rule_tac Max_ge, simp, simp, rule_tac disjI2)
       
  4463     apply(subgoal_tac "aa \<in> set gs", simp)
       
  4464     using h2
       
  4465     apply(rule_tac A = "set (take i gs)" in subsetD, 
       
  4466       simp add: set_take_subset, simp)
       
  4467     done
       
  4468   thm cn_merge_gs.simps
       
  4469   from this obtain stpa where g2: 
       
  4470     "abc_steps_l (0, lm @ 0\<up>(a_md - n) @ suflm) 
       
  4471     (cn_merge_gs (map rec_ci ?ggs) ?pstr) stpa =
       
  4472     (listsum (map ((\<lambda>(ap, pos, n). length ap) \<circ> rec_ci) ?ggs) +
       
  4473     3 * length ?ggs, lm @ 0\<up>(?pstr - n) @ ys @ 0\<up>(a_md -(?pstr + length ?ggs)) @
       
  4474     suflm)" ..
       
  4475   moreover have 
       
  4476     "\<exists> cp. aprog = (cn_merge_gs
       
  4477     (map rec_ci ?ggs) ?pstr) [+] ga [+] cp"
       
  4478     using h1
       
  4479     apply(simp add: rec_ci.simps)
       
  4480     apply(rule_tac x = "mv_box n (?pstr + i) [+] 
       
  4481       (cn_merge_gs (map rec_ci (drop (Suc i) gs)) (?pstr + Suc i))
       
  4482       [+]mv_boxes 0 (Suc (max (Suc n) (Max (insert c 
       
  4483      (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs))) +
       
  4484       length gs)) n [+] mv_boxes (max (Suc n) (Max (insert c 
       
  4485       (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) 0 (length gs) [+]
       
  4486       a [+] recursive.mv_box b (max (Suc n) 
       
  4487       (Max (insert c (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) [+]
       
  4488      empty_boxes (length gs) [+] recursive.mv_box (max (Suc n) 
       
  4489       (Max (insert c (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs)))) n [+]
       
  4490       mv_boxes (Suc (max (Suc n) (Max (insert c 
       
  4491     (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs))) + length gs)) 0 n" in exI)
       
  4492     apply(simp add: abc_append_commute [THEN sym])
       
  4493     apply(auto)
       
  4494     using cn_merge_gs_split[of i gs ga "length lm" gc 
       
  4495       "(max (Suc (length lm))
       
  4496        (Max (insert c (((\<lambda>(aprog, p, n). n) \<circ> rec_ci) ` set gs))))"] 
       
  4497       h2
       
  4498     apply(simp)
       
  4499     done
       
  4500   from this obtain cp where g3: 
       
  4501     "aprog = (cn_merge_gs (map rec_ci ?ggs) ?pstr) [+] ga [+] cp" ..
       
  4502   show "\<forall> stp. case abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suflm) 
       
  4503     aprog stp of (ss, e) \<Rightarrow> ss < length aprog"
       
  4504   proof(rule_tac abc_append_unhalt2)
       
  4505     show "abc_steps_l (0, lm @ 0\<up>(a_md - rs_pos) @ suflm) (
       
  4506       cn_merge_gs (map rec_ci ?ggs) ?pstr) stpa =
       
  4507          (length ((cn_merge_gs (map rec_ci ?ggs) ?pstr)),  
       
  4508           lm @ 0\<up>(?pstr - n) @ ys @ 0\<up>(a_md -(?pstr + length ?ggs)) @ suflm)"
       
  4509       using h3 g2
       
  4510       apply(simp add: cn_merge_gs_length)
       
  4511       done
       
  4512   next
       
  4513     show "ga \<noteq> []"
       
  4514       using h1
       
  4515       apply(simp add: rec_ci_not_null)
       
  4516       done
       
  4517   next
       
  4518     show "\<forall>stp. case abc_steps_l (0, lm @ 0\<up>(?pstr - n) @ ys
       
  4519       @ 0\<up>(a_md - (?pstr + length (take i gs))) @ suflm) ga  stp of
       
  4520           (ss, e) \<Rightarrow> ss < length ga"
       
  4521       using ind[of "0\<up>(?pstr - gc) @ ys @ 0\<up>(a_md - (?pstr + length (take i gs)))
       
  4522         @ suflm"]
       
  4523       apply(subgoal_tac "lm @ 0\<up>(?pstr - n) @ ys
       
  4524         @ 0\<up>(a_md - (?pstr + length (take i gs))) @ suflm
       
  4525                        = lm @ 0\<up>(gc - n) @ 
       
  4526         0\<up>(?pstr - gc) @ ys @ 0\<up>(a_md - (?pstr + length (take i gs))) @ suflm", simp)
       
  4527       apply(simp add: replicate_add[THEN sym])
       
  4528       apply(subgoal_tac "gc > n \<and> ?pstr \<ge> gc")
       
  4529       apply(erule_tac conjE)
       
  4530       apply(simp add: h1)
       
  4531       using h1
       
  4532       apply(auto)
       
  4533       apply(rule_tac min_max.le_supI2)
       
  4534       apply(rule_tac Max_ge, simp, simp)
       
  4535       apply(rule_tac disjI2)
       
  4536       using h2
       
  4537       thm rev_image_eqI
       
  4538       apply(rule_tac x = "gs!i" in rev_image_eqI, simp, simp)
       
  4539       done
       
  4540   next
       
  4541     show "aprog = cn_merge_gs (map rec_ci (take i gs)) 
       
  4542               ?pstr [+] ga [+] cp"
       
  4543       using g3 by simp
       
  4544   qed
       
  4545 qed
       
  4546 
       
  4547 lemma recursive_compile_correct_spec: 
       
  4548   "\<lbrakk>rec_ci re = (ap, ary, fp); 
       
  4549     rec_calc_rel re args r\<rbrakk>
       
  4550   \<Longrightarrow> (\<exists> stp. (abc_steps_l (0, args @ 0\<up>(fp - ary)) ap stp) = 
       
  4551                      (length ap, args@[r]@0\<up>(fp - ary - 1)))"
       
  4552 using recursive_compile_correct[of re ap ary fp args r "[]"]
       
  4553 by simp
       
  4554 
       
  4555 definition dummy_abc :: "nat \<Rightarrow> abc_inst list"
       
  4556 where
       
  4557 "dummy_abc k = [Inc k, Dec k 0, Goto 3]"
       
  4558 
       
  4559 definition abc_list_crsp:: "nat list \<Rightarrow> nat list \<Rightarrow> bool"
       
  4560   where
       
  4561   "abc_list_crsp xs ys = (\<exists> n. xs = ys @ 0\<up>n \<or> ys = xs @ 0\<up>n)"
       
  4562 
       
  4563 lemma [intro]: "abc_list_crsp (lm @ 0\<up>m) lm"
       
  4564 apply(auto simp: abc_list_crsp_def)
       
  4565 done
       
  4566 
       
  4567 lemma abc_list_crsp_lm_v: 
       
  4568   "abc_list_crsp lma lmb \<Longrightarrow> abc_lm_v lma n = abc_lm_v lmb n"
       
  4569 apply(auto simp: abc_list_crsp_def abc_lm_v.simps 
       
  4570                  nth_append)
       
  4571 done
       
  4572 
       
  4573 lemma  rep_app_cons_iff: 
       
  4574   "k < n \<Longrightarrow> replicate n a[k:=b] = 
       
  4575           replicate k a @ b # replicate (n - k - 1) a"
       
  4576 apply(induct n arbitrary: k, simp)
       
  4577 apply(simp split:nat.splits)
       
  4578 done
       
  4579 
       
  4580 lemma abc_list_crsp_lm_s: 
       
  4581   "abc_list_crsp lma lmb \<Longrightarrow> 
       
  4582       abc_list_crsp (abc_lm_s lma m n) (abc_lm_s lmb m n)"
       
  4583 apply(auto simp: abc_list_crsp_def abc_lm_v.simps abc_lm_s.simps)
       
  4584 apply(simp_all add: list_update_append, auto)
       
  4585 proof -
       
  4586   fix na
       
  4587   assume h: "m < length lmb + na" " \<not> m < length lmb"
       
  4588   hence "m - length lmb < na" by simp
       
  4589   hence "replicate na 0[(m- length lmb):= n] = 
       
  4590            replicate (m - length lmb) 0 @ n # 
       
  4591               replicate (na - (m - length lmb) - 1) 0"
       
  4592     apply(erule_tac rep_app_cons_iff)
       
  4593     done
       
  4594   thus "\<exists>nb. replicate na 0[m - length lmb := n] =
       
  4595                  replicate (m - length lmb) 0 @ n # replicate nb 0 \<or>
       
  4596                  replicate (m - length lmb) 0 @ [n] =
       
  4597                  replicate na 0[m - length lmb := n] @ replicate nb 0"
       
  4598     apply(auto)
       
  4599     done
       
  4600 next
       
  4601   fix na
       
  4602   assume h: "\<not> m < length lmb + na"
       
  4603   show 
       
  4604     "\<exists>nb. replicate na 0 @ replicate (m - (length lmb + na)) 0 @ [n] =
       
  4605            replicate (m - length lmb) 0 @ n # replicate nb 0 \<or>
       
  4606           replicate (m - length lmb) 0 @ [n] =
       
  4607             replicate na 0 @
       
  4608             replicate (m - (length lmb + na)) 0 @ n # replicate nb 0"
       
  4609     apply(rule_tac x = 0 in exI, simp, auto)
       
  4610     using h
       
  4611     apply(simp add: replicate_add[THEN sym])
       
  4612     done
       
  4613 next
       
  4614   fix na
       
  4615   assume h: "\<not> m < length lma" "m < length lma + na"
       
  4616   hence "m - length lma < na" by simp
       
  4617   hence 
       
  4618     "replicate na 0[(m- length lma):= n] = replicate (m - length lma) 
       
  4619                   0 @ n # replicate (na - (m - length lma) - 1) 0"
       
  4620     apply(erule_tac rep_app_cons_iff)
       
  4621     done
       
  4622   thus "\<exists>nb. replicate (m - length lma) 0 @ [n] =
       
  4623                  replicate na 0[m - length lma := n] @ replicate nb 0 
       
  4624            \<or> replicate na 0[m - length lma := n] =
       
  4625                  replicate (m - length lma) 0 @ n # replicate nb 0"
       
  4626     apply(auto)
       
  4627     done
       
  4628 next
       
  4629   fix na
       
  4630   assume "\<not> m < length lma + na"
       
  4631   thus " \<exists>nb. replicate (m - length lma) 0 @ [n] =
       
  4632             replicate na 0 @
       
  4633             replicate (m - (length lma + na)) 0 @ n # replicate nb 0 
       
  4634         \<or>   replicate na 0 @ 
       
  4635                replicate (m - (length lma + na)) 0 @ [n] =
       
  4636             replicate (m - length lma) 0 @ n # replicate nb 0"
       
  4637     apply(rule_tac x = 0 in exI, simp, auto)
       
  4638     apply(simp add: replicate_add[THEN sym])
       
  4639     done
       
  4640 qed
       
  4641 
       
  4642 lemma abc_list_crsp_step: 
       
  4643   "\<lbrakk>abc_list_crsp lma lmb; abc_step_l (aa, lma) i = (a, lma'); 
       
  4644     abc_step_l (aa, lmb) i = (a', lmb')\<rbrakk>
       
  4645     \<Longrightarrow> a' = a \<and> abc_list_crsp lma' lmb'"
       
  4646 apply(case_tac i, auto simp: abc_step_l.simps 
       
  4647        abc_list_crsp_lm_s abc_list_crsp_lm_v Let_def 
       
  4648                        split: abc_inst.splits if_splits)
       
  4649 done
       
  4650 
       
  4651 lemma abc_list_crsp_steps: 
       
  4652   "\<lbrakk>abc_steps_l (0, lm @ 0\<up>m) aprog stp = (a, lm'); aprog \<noteq> []\<rbrakk> 
       
  4653       \<Longrightarrow> \<exists> lma. abc_steps_l (0, lm) aprog stp = (a, lma) \<and> 
       
  4654                                           abc_list_crsp lm' lma"
       
  4655 apply(induct stp arbitrary: a lm', simp add: abc_steps_l.simps, auto)
       
  4656 apply(case_tac "abc_steps_l (0, lm @ 0\<up>m) aprog stp", 
       
  4657       simp add: abc_step_red)
       
  4658 proof -
       
  4659   fix stp a lm' aa b
       
  4660   assume ind:
       
  4661     "\<And>a lm'. aa = a \<and> b = lm' \<Longrightarrow> 
       
  4662      \<exists>lma. abc_steps_l (0, lm) aprog stp = (a, lma) \<and>
       
  4663                                           abc_list_crsp lm' lma"
       
  4664     and h: "abc_steps_l (0, lm @ 0\<up>m) aprog (Suc stp) = (a, lm')" 
       
  4665            "abc_steps_l (0, lm @ 0\<up>m) aprog stp = (aa, b)" 
       
  4666            "aprog \<noteq> []"
       
  4667   hence g1: "abc_steps_l (0, lm @ 0\<up>m) aprog (Suc stp)
       
  4668           = abc_step_l (aa, b) (abc_fetch aa aprog)"
       
  4669     apply(rule_tac abc_step_red, simp)
       
  4670     done
       
  4671   have "\<exists>lma. abc_steps_l (0, lm) aprog stp = (aa, lma) \<and> 
       
  4672               abc_list_crsp b lma"
       
  4673     apply(rule_tac ind, simp)
       
  4674     done
       
  4675   from this obtain lma where g2: 
       
  4676     "abc_steps_l (0, lm) aprog stp = (aa, lma) \<and> 
       
  4677      abc_list_crsp b lma"   ..
       
  4678   hence g3: "abc_steps_l (0, lm) aprog (Suc stp)
       
  4679           = abc_step_l (aa, lma) (abc_fetch aa aprog)"
       
  4680     apply(rule_tac abc_step_red, simp)
       
  4681     done
       
  4682   show "\<exists>lma. abc_steps_l (0, lm) aprog (Suc stp) = (a, lma) \<and> abc_list_crsp lm' lma"
       
  4683     using g1 g2 g3 h
       
  4684     apply(auto)
       
  4685     apply(case_tac "abc_step_l (aa, b) (abc_fetch aa aprog)",
       
  4686           case_tac "abc_step_l (aa, lma) (abc_fetch aa aprog)", simp)
       
  4687     apply(rule_tac abc_list_crsp_step, auto)
       
  4688     done
       
  4689 qed
       
  4690 
       
  4691 lemma recursive_compile_correct_norm: 
       
  4692   "\<lbrakk>rec_ci re = (aprog, rs_pos, a_md);  
       
  4693    rec_calc_rel re lm rs\<rbrakk>
       
  4694   \<Longrightarrow> (\<exists> stp lm' m. (abc_steps_l (0, lm) aprog stp) = 
       
  4695   (length aprog, lm') \<and> abc_list_crsp lm' (lm @ rs # 0\<up>m))"
       
  4696 apply(frule_tac recursive_compile_correct_spec, auto)
       
  4697 apply(drule_tac abc_list_crsp_steps)
       
  4698 apply(rule_tac rec_ci_not_null, simp)
       
  4699 apply(erule_tac exE, rule_tac x = stp in exI, 
       
  4700   auto simp: abc_list_crsp_def)
       
  4701 done
       
  4702 
       
  4703 lemma [simp]: "length (dummy_abc (length lm)) = 3"
       
  4704 apply(simp add: dummy_abc_def)
       
  4705 done
       
  4706 
       
  4707 lemma [simp]: "dummy_abc (length lm) \<noteq> []"
       
  4708 apply(simp add: dummy_abc_def)
       
  4709 done
       
  4710 
       
  4711 lemma dummy_abc_steps_ex: 
       
  4712   "\<exists>bstp. abc_steps_l (0, lm') (dummy_abc (length lm)) bstp = 
       
  4713   ((Suc (Suc (Suc 0))), abc_lm_s lm' (length lm) (abc_lm_v lm' (length lm)))"
       
  4714 apply(rule_tac x = "Suc (Suc (Suc 0))" in exI)
       
  4715 apply(auto simp: abc_steps_l.simps abc_step_l.simps 
       
  4716   dummy_abc_def abc_fetch.simps)
       
  4717 apply(auto simp: abc_lm_s.simps abc_lm_v.simps nth_append)
       
  4718 apply(simp add: butlast_append)
       
  4719 done
       
  4720 
       
  4721 lemma [simp]: 
       
  4722   "\<lbrakk>Suc (length lm) - length lm' \<le> n; \<not> length lm < length lm'; lm @ rs # 0 \<up> m = lm' @ 0 \<up> n\<rbrakk> 
       
  4723   \<Longrightarrow> lm' @ 0 \<up> Suc (length lm - length lm') = lm @ [rs]"
       
  4724 apply(subgoal_tac "n > m")
       
  4725 apply(subgoal_tac "\<exists> d. n = d + m", erule_tac exE)
       
  4726 apply(simp add: replicate_add)
       
  4727 apply(drule_tac length_equal, simp)
       
  4728 apply(simp add: replicate_Suc[THEN sym] del: replicate_Suc)
       
  4729 apply(rule_tac x = "n - m" in exI, simp)
       
  4730 apply(drule_tac length_equal, simp)
       
  4731 done
       
  4732 
       
  4733 lemma [elim]: 
       
  4734   "lm @ rs # 0\<up>m = lm' @ 0\<up>n \<Longrightarrow> 
       
  4735   \<exists>m. abc_lm_s lm' (length lm) (abc_lm_v lm' (length lm)) = 
       
  4736                             lm @ rs # 0\<up>m"
       
  4737 proof(cases "length lm' > length lm")
       
  4738   case True 
       
  4739   assume h: "lm @ rs # 0\<up>m = lm' @ 0\<up>n" "length lm < length lm'"
       
  4740   hence "m \<ge> n"
       
  4741     apply(drule_tac length_equal)
       
  4742     apply(simp)
       
  4743     done
       
  4744   hence "\<exists> d. m = d + n"
       
  4745     apply(rule_tac x = "m - n" in exI, simp)
       
  4746     done
       
  4747   from this obtain d where "m = d + n" ..
       
  4748   from h and this show "?thesis"
       
  4749     apply(auto simp: abc_lm_s.simps abc_lm_v.simps 
       
  4750                      replicate_add)
       
  4751     done
       
  4752 next
       
  4753   case False
       
  4754   assume h:"lm @ rs # 0\<up>m = lm' @ 0\<up>n" 
       
  4755     and    g: "\<not> length lm < length lm'"
       
  4756   have "take (Suc (length lm)) (lm @ rs # 0\<up>m) = 
       
  4757                         take (Suc (length lm)) (lm' @ 0\<up>n)"
       
  4758     using h by simp
       
  4759   moreover have "n \<ge> (Suc (length lm) - length lm')"
       
  4760     using h g
       
  4761     apply(drule_tac length_equal)
       
  4762     apply(simp)
       
  4763     done
       
  4764   ultimately show 
       
  4765     "\<exists>m. abc_lm_s lm' (length lm) (abc_lm_v lm' (length lm)) =
       
  4766                                                        lm @ rs # 0\<up>m"
       
  4767     using g h
       
  4768     apply(simp add: abc_lm_s.simps abc_lm_v.simps min_def)
       
  4769     apply(rule_tac x = 0 in exI, 
       
  4770       simp add:replicate_append_same replicate_Suc[THEN sym]
       
  4771                                       del:replicate_Suc)
       
  4772     done    
       
  4773 qed
       
  4774 
       
  4775 lemma [elim]: 
       
  4776   "abc_list_crsp lm' (lm @ rs # 0\<up>m)
       
  4777   \<Longrightarrow> \<exists>m. abc_lm_s lm' (length lm) (abc_lm_v lm' (length lm)) 
       
  4778              = lm @ rs # 0\<up>m"
       
  4779 apply(auto simp: abc_list_crsp_def)
       
  4780 apply(simp add: abc_lm_v.simps abc_lm_s.simps)
       
  4781 apply(rule_tac x =  "m + n" in exI, 
       
  4782       simp add: replicate_add)
       
  4783 done
       
  4784 
       
  4785 lemma abc_append_dummy_complie:
       
  4786   "\<lbrakk>rec_ci recf = (ap, ary, fp);  
       
  4787     rec_calc_rel recf args r; 
       
  4788     length args = k\<rbrakk>
       
  4789   \<Longrightarrow> (\<exists> stp m. (abc_steps_l (0, args) (ap [+] dummy_abc k) stp) = 
       
  4790                   (length ap + 3, args @ r # 0\<up>m))"
       
  4791 apply(drule_tac recursive_compile_correct_norm, auto simp: numeral_3_eq_3)
       
  4792 proof -
       
  4793   fix stp lm' m
       
  4794   assume h: "rec_calc_rel recf args r"  
       
  4795     "abc_steps_l (0, args) ap stp = (length ap, lm')" 
       
  4796     "abc_list_crsp lm' (args @ r # 0\<up>m)"
       
  4797   thm abc_append_exc2
       
  4798   thm abc_lm_s.simps
       
  4799   have "\<exists>stp. abc_steps_l (0, args) (ap [+] 
       
  4800     (dummy_abc (length args))) stp = (length ap + 3, 
       
  4801     abc_lm_s lm' (length args) (abc_lm_v lm' (length args)))"
       
  4802     using h
       
  4803     apply(rule_tac bm = lm' in abc_append_exc2,
       
  4804           auto intro: dummy_abc_steps_ex simp: numeral_3_eq_3)
       
  4805     done
       
  4806   thus "\<exists>stp m. abc_steps_l (0, args) (ap [+] 
       
  4807     dummy_abc (length args)) stp = (Suc (Suc (Suc (length ap))), args @ r # 0\<up>m)"
       
  4808     using h
       
  4809     apply(erule_tac exE)
       
  4810     apply(rule_tac x = stpa in exI, auto)
       
  4811     done
       
  4812 qed
       
  4813 
       
  4814 lemma [simp]: "length (dummy_abc k) = 3"
       
  4815 apply(simp add: dummy_abc_def)
       
  4816 done
       
  4817 
       
  4818 lemma [simp]: "length args = k \<Longrightarrow> abc_lm_v (args @ r # 0\<up>m) k = r "
       
  4819 apply(simp add: abc_lm_v.simps nth_append)
       
  4820 done
       
  4821 
       
  4822 lemma [simp]: "crsp (layout_of (ap [+] dummy_abc k)) (0, args)
       
  4823   (Suc 0, Bk # Bk # ires, <args> @ Bk \<up> rn) ires"
       
  4824 apply(auto simp: crsp.simps start_of.simps)
       
  4825 done
       
  4826 
       
  4827 lemma recursive_compile_to_tm_correct: 
       
  4828   "\<lbrakk>rec_ci recf = (ap, ary, fp); 
       
  4829     rec_calc_rel recf args r;
       
  4830     length args = k;
       
  4831     ly = layout_of (ap [+] dummy_abc k);
       
  4832     tp = tm_of (ap [+] dummy_abc k)\<rbrakk>
       
  4833   \<Longrightarrow> \<exists> stp m l. steps0 (Suc 0, Bk # Bk # ires, <args> @ Bk\<up>rn)
       
  4834   (tp @ shift (mopup k) (length tp div 2)) stp
       
  4835   = (0, Bk\<up>m @ Bk # Bk # ires, Oc\<up>Suc r @ Bk\<up>l)"
       
  4836   using abc_append_dummy_complie[of recf ap ary fp args r k]
       
  4837 apply(simp)
       
  4838 apply(erule_tac exE)+
       
  4839 apply(frule_tac tp = tp and n = k 
       
  4840                and ires = ires in compile_correct_halt, simp_all add: length_append)
       
  4841 apply(simp_all add: length_append)
       
  4842 done
       
  4843 
       
  4844 lemma [simp]:
       
  4845   "list_all (\<lambda>(acn, s). s \<le> Suc (Suc (Suc (Suc (Suc (Suc (2 * n))))))) xs \<Longrightarrow>
       
  4846   list_all (\<lambda>(acn, s). s \<le> Suc (Suc (Suc (Suc (Suc (Suc (Suc (Suc (2 * n))))))))) xs"
       
  4847 apply(induct xs, simp, simp)
       
  4848 apply(case_tac a, simp)
       
  4849 done
       
  4850 
       
  4851 lemma shift_append: "shift (xs @ ys) n = shift xs n @ shift ys n"
       
  4852 apply(simp add: shift.simps)
       
  4853 done
       
  4854 
       
  4855 lemma [simp]: "length (shift (mopup n) ss) = 4 * n + 12"
       
  4856 apply(auto simp: mopup.simps shift_append mopup_b_def)
       
  4857 done
       
  4858 
       
  4859 lemma length_tm_even[intro]: "length (tm_of ap) mod 2 = 0"
       
  4860 apply(simp add: tm_of.simps)
       
  4861 done
       
  4862 
       
  4863 lemma [simp]: "k < length ap \<Longrightarrow> tms_of ap ! k  = 
       
  4864  ci (layout_of ap) (start_of (layout_of ap) k) (ap ! k)"
       
  4865 apply(simp add: tms_of.simps tpairs_of.simps)
       
  4866 done
       
  4867 
       
  4868 lemma start_of_suc_inc:
       
  4869   "\<lbrakk>k < length ap; ap ! k = Inc n\<rbrakk> \<Longrightarrow> start_of (layout_of ap) (Suc k) =
       
  4870                         start_of (layout_of ap) k + 2 * n + 9"
       
  4871 apply(rule_tac start_of_Suc1, auto simp: abc_fetch.simps)
       
  4872 done
       
  4873 
       
  4874 lemma start_of_suc_dec:
       
  4875   "\<lbrakk>k < length ap; ap ! k = (Dec n e)\<rbrakk> \<Longrightarrow> start_of (layout_of ap) (Suc k) =
       
  4876                         start_of (layout_of ap) k + 2 * n + 16"
       
  4877 apply(rule_tac start_of_Suc2, auto simp: abc_fetch.simps)
       
  4878 done
       
  4879 
       
  4880 lemma inc_state_all_le:
       
  4881   "\<lbrakk>k < length ap; ap ! k = Inc n; 
       
  4882        (a, b) \<in> set (shift (shift tinc_b (2 * n)) 
       
  4883                             (start_of (layout_of ap) k - Suc 0))\<rbrakk>
       
  4884        \<Longrightarrow> b \<le> start_of (layout_of ap) (length ap)"
       
  4885 apply(subgoal_tac "b \<le> start_of (layout_of ap) (Suc k)")
       
  4886 apply(subgoal_tac "start_of (layout_of ap) (Suc k) \<le> start_of (layout_of ap) (length ap) ")
       
  4887 apply(arith)
       
  4888 apply(case_tac "Suc k = length ap", simp)
       
  4889 apply(rule_tac start_of_less, simp)
       
  4890 apply(auto simp: tinc_b_def shift.simps start_of_suc_inc length_of.simps startof_not0)
       
  4891 done
       
  4892 
       
  4893 lemma findnth_le[elim]: 
       
  4894   "(a, b) \<in> set (shift (findnth n) (start_of (layout_of ap) k - Suc 0))
       
  4895   \<Longrightarrow> b \<le> Suc (start_of (layout_of ap) k + 2 * n)"
       
  4896 apply(induct n, simp add: findnth.simps shift.simps)
       
  4897 apply(simp add: findnth.simps shift_append, auto)
       
  4898 apply(auto simp: shift.simps)
       
  4899 done
       
  4900 
       
  4901 lemma findnth_state_all_le1:
       
  4902   "\<lbrakk>k < length ap; ap ! k = Inc n;
       
  4903   (a, b) \<in> 
       
  4904   set (shift (findnth n) (start_of (layout_of ap) k - Suc 0))\<rbrakk> 
       
  4905   \<Longrightarrow> b \<le> start_of (layout_of ap) (length ap)"
       
  4906 apply(subgoal_tac "b \<le> start_of (layout_of ap) (Suc k)")
       
  4907 apply(subgoal_tac "start_of (layout_of ap) (Suc k) \<le> start_of (layout_of ap) (length ap) ")
       
  4908 apply(arith)
       
  4909 apply(case_tac "Suc k = length ap", simp)
       
  4910 apply(rule_tac start_of_less, simp)
       
  4911 apply(subgoal_tac "b \<le> start_of (layout_of ap) k + 2*n + 1 \<and> 
       
  4912      start_of (layout_of ap) k + 2*n + 1 \<le>  start_of (layout_of ap) (Suc k)", auto)
       
  4913 apply(auto simp: tinc_b_def shift.simps length_of.simps startof_not0 start_of_suc_inc)
       
  4914 done
       
  4915 
       
  4916 lemma start_of_eq: "length ap < as \<Longrightarrow> start_of (layout_of ap) as = start_of (layout_of ap) (length ap)"
       
  4917 apply(induct as, simp)
       
  4918 apply(case_tac "length ap < as", simp add: start_of.simps)
       
  4919 apply(subgoal_tac "as = length ap")
       
  4920 apply(simp add: start_of.simps)
       
  4921 apply arith
       
  4922 done
       
  4923 
       
  4924 lemma start_of_all_le: "start_of (layout_of ap) as \<le> start_of (layout_of ap) (length ap)"
       
  4925 apply(subgoal_tac "as > length ap \<or> as = length ap \<or> as < length ap", 
       
  4926       auto simp: start_of_eq start_of_less)
       
  4927 done
       
  4928 
       
  4929 lemma findnth_state_all_le2: 
       
  4930   "\<lbrakk>k < length ap; 
       
  4931   ap ! k = Dec n e;
       
  4932   (a, b) \<in> set (shift (findnth n) (start_of (layout_of ap) k - Suc 0))\<rbrakk>
       
  4933   \<Longrightarrow> b \<le> start_of (layout_of ap) (length ap)"
       
  4934 apply(subgoal_tac "b \<le> start_of (layout_of ap) k + 2*n + 1 \<and> 
       
  4935      start_of (layout_of ap) k + 2*n + 1 \<le>  start_of (layout_of ap) (Suc k) \<and>
       
  4936       start_of (layout_of ap) (Suc k) \<le> start_of (layout_of ap) (length ap)", auto)
       
  4937 apply(subgoal_tac "start_of (layout_of ap) (Suc k) = 
       
  4938   start_of  (layout_of ap)  k + 2*n + 16", simp)
       
  4939 apply(simp add: start_of_suc_dec)
       
  4940 apply(rule_tac start_of_all_le)
       
  4941 done
       
  4942 
       
  4943 lemma dec_state_all_le[simp]:
       
  4944   "\<lbrakk>k < length ap; ap ! k = Dec n e; 
       
  4945   (a, b) \<in> set (shift (shift tdec_b (2 * n))
       
  4946   (start_of (layout_of ap) k - Suc 0))\<rbrakk>
       
  4947        \<Longrightarrow> b \<le> start_of (layout_of ap) (length ap)"
       
  4948 apply(subgoal_tac "2*n + start_of (layout_of ap) k + 16 \<le> start_of (layout_of ap) (length ap) \<and> start_of (layout_of ap) k > 0")
       
  4949 prefer 2
       
  4950 apply(subgoal_tac "start_of (layout_of ap) (Suc k) = start_of (layout_of ap) k + 2*n + 16
       
  4951                  \<and> start_of (layout_of ap) (Suc k) \<le> start_of (layout_of ap) (length ap)")
       
  4952 apply(simp add: startof_not0, rule_tac conjI)
       
  4953 apply(simp add: start_of_suc_dec)
       
  4954 apply(rule_tac start_of_all_le)
       
  4955 apply(auto simp: tdec_b_def shift.simps)
       
  4956 done
       
  4957 
       
  4958 lemma tms_any_less: 
       
  4959   "\<lbrakk>k < length ap; (a, b) \<in> set (tms_of ap ! k)\<rbrakk> \<Longrightarrow> 
       
  4960   b \<le> start_of (layout_of ap) (length ap)"
       
  4961 apply(case_tac "ap!k", auto simp: tms_of.simps tpairs_of.simps ci.simps shift_append sete.simps)
       
  4962 apply(erule_tac findnth_state_all_le1, simp_all)
       
  4963 apply(erule_tac inc_state_all_le, simp_all)
       
  4964 apply(erule_tac findnth_state_all_le2, simp_all)
       
  4965 apply(rule_tac start_of_all_le)
       
  4966 apply(rule_tac dec_state_all_le, simp_all)
       
  4967 apply(rule_tac start_of_all_le)
       
  4968 done
       
  4969 
       
  4970 lemma concat_in: "i < length (concat xs) \<Longrightarrow> \<exists>k < length xs. concat xs ! i \<in> set (xs ! k)"
       
  4971 apply(induct xs rule: list_tl_induct, simp, simp)
       
  4972 apply(case_tac "i < length (concat list)", simp)
       
  4973 apply(erule_tac exE, rule_tac x = k in exI)
       
  4974 apply(simp add: nth_append)
       
  4975 apply(rule_tac x = "length list" in exI, simp)
       
  4976 apply(simp add: nth_append)
       
  4977 done 
       
  4978 
       
  4979 lemma [simp]: "length (tms_of ap) = length ap"
       
  4980 apply(simp add: tms_of.simps tpairs_of.simps)
       
  4981 done
       
  4982 
       
  4983 declare length_concat[simp]
       
  4984 
       
  4985 lemma in_tms: "i < length (tm_of ap) \<Longrightarrow> \<exists> k < length ap. (tm_of ap ! i) \<in> set (tms_of ap ! k)"
       
  4986 apply(simp only: tm_of.simps)
       
  4987 using concat_in[of i "tms_of ap"]
       
  4988 apply(auto)
       
  4989 done
       
  4990 
       
  4991 lemma all_le_start_of: "list_all (\<lambda>(acn, s). 
       
  4992   s \<le> start_of (layout_of ap) (length ap)) (tm_of ap)"
       
  4993 apply(simp only: list_all_length)
       
  4994 apply(rule_tac allI, rule_tac impI)
       
  4995 apply(drule_tac in_tms, auto elim: tms_any_less)
       
  4996 done
       
  4997 
       
  4998 lemma length_ci: 
       
  4999 "\<lbrakk>k < length ap; length (ci ly y (ap ! k)) = 2 * qa\<rbrakk>
       
  5000       \<Longrightarrow> layout_of ap ! k = qa"
       
  5001 apply(case_tac "ap ! k")
       
  5002 apply(auto simp: layout_of.simps ci.simps 
       
  5003   length_of.simps tinc_b_def tdec_b_def length_findnth sete.simps)
       
  5004 done
       
  5005 
       
  5006 lemma [intro]: "length (ci ly y i) mod 2 = 0"
       
  5007 apply(case_tac i, auto simp: ci.simps length_findnth
       
  5008   tinc_b_def sete.simps tdec_b_def)
       
  5009 done
       
  5010 
       
  5011 lemma [intro]: "listsum (map (length \<circ> (\<lambda>(x, y). ci ly x y)) zs) mod 2 = 0"
       
  5012 apply(induct zs rule: list_tl_induct, simp)
       
  5013 apply(case_tac a, simp)
       
  5014 apply(subgoal_tac "length (ci ly aa b) mod 2 = 0")
       
  5015 apply(auto)
       
  5016 done
       
  5017 
       
  5018 lemma zip_pre:
       
  5019   "(length ys) \<le> length ap \<Longrightarrow>
       
  5020   zip ys ap = zip ys (take (length ys) (ap::'a list))"
       
  5021 proof(induct ys arbitrary: ap, simp, case_tac ap, simp)
       
  5022   fix a ys ap aa list
       
  5023   assume ind: "\<And>(ap::'a list). length ys \<le> length ap \<Longrightarrow> 
       
  5024     zip ys ap = zip ys (take (length ys) ap)"
       
  5025   and h: "length (a # ys) \<le> length ap" "(ap::'a list) = aa # (list::'a list)"
       
  5026   from h show "zip (a # ys) ap = zip (a # ys) (take (length (a # ys)) ap)"
       
  5027     using ind[of list]
       
  5028     apply(simp)
       
  5029     done
       
  5030 qed
       
  5031 
       
  5032 lemma length_start_of_tm: "start_of (layout_of ap) (length ap) = Suc (length (tm_of ap)  div 2)"
       
  5033 using tpa_states[of "tm_of ap"  "length ap" ap]
       
  5034 apply(simp add: tm_of.simps)
       
  5035 done
       
  5036 
       
  5037 lemma [elim]: "list_all (\<lambda>(acn, s). s \<le> Suc q) xs
       
  5038         \<Longrightarrow> list_all (\<lambda>(acn, s). s \<le> q + (2 * n + 6)) xs"
       
  5039 apply(simp add: list_all_length)
       
  5040 apply(auto)
       
  5041 done
       
  5042 
       
  5043 lemma [simp]: "length mopup_b = 12"
       
  5044 apply(simp add: mopup_b_def)
       
  5045 done
       
  5046 (*
       
  5047 lemma [elim]: "\<lbrakk>na < 4 * n; tshift (mop_bef n) q ! na = (a, b)\<rbrakk> \<Longrightarrow> 
       
  5048   b \<le> q + (2 * n + 6)"
       
  5049 apply(induct n, simp, simp add: mop_bef.simps nth_append tshift_append shift_length)
       
  5050 apply(case_tac "na < 4*n", simp, simp)
       
  5051 apply(subgoal_tac "na = 4*n \<or> na = 1 + 4*n \<or> na = 2 + 4*n \<or> na = 3 + 4*n",
       
  5052   auto simp: shift_length)
       
  5053 apply(simp_all add: tshift.simps)
       
  5054 done
       
  5055 *)
       
  5056 
       
  5057 lemma mp_up_all_le: "list_all  (\<lambda>(acn, s). s \<le> q + (2 * n + 6)) 
       
  5058   [(R, Suc (Suc (2 * n + q))), (R, Suc (2 * n + q)), 
       
  5059   (L, 5 + 2 * n + q), (W0, Suc (Suc (Suc (2 * n + q)))), (R, 4 + 2 * n + q),
       
  5060   (W0, Suc (Suc (Suc (2 * n + q)))), (R, Suc (Suc (2 * n + q))),
       
  5061   (W0, Suc (Suc (Suc (2 * n + q)))), (L, 5 + 2 * n + q),
       
  5062   (L, 6 + 2 * n + q), (R, 0),  (L, 6 + 2 * n + q)]"
       
  5063 apply(auto)
       
  5064 done
       
  5065 
       
  5066 lemma [simp]: "(a, b) \<in> set (mopup_a n) \<Longrightarrow> b \<le> 2 * n + 6"
       
  5067 apply(induct n, auto simp: mopup_a.simps)
       
  5068 done
       
  5069 
       
  5070 lemma [simp]: "(a, b) \<in> set (shift (mopup n) (listsum (layout_of ap)))
       
  5071   \<Longrightarrow> b \<le> (2 * listsum (layout_of ap) + length (mopup n)) div 2"
       
  5072 apply(auto simp: mopup.simps shift_append shift.simps)
       
  5073 apply(auto simp: mopup_a.simps mopup_b_def)
       
  5074 done
       
  5075 
       
  5076 lemma [intro]: " 2 \<le> 2 * listsum (layout_of ap) + length (mopup n)"
       
  5077 apply(simp add: mopup.simps)
       
  5078 done
       
  5079 
       
  5080 lemma [intro]: " (2 * listsum (layout_of ap) + length (mopup n)) mod 2 = 0"
       
  5081 apply(auto simp: mopup.simps)
       
  5082 apply arith
       
  5083 done
       
  5084 
       
  5085 lemma [simp]: "b \<le> Suc x
       
  5086           \<Longrightarrow> b \<le> (2 * x + length (mopup n)) div 2"
       
  5087 apply(auto simp: mopup.simps)
       
  5088 done
       
  5089 
       
  5090 lemma t_compiled_correct: 
       
  5091   "\<lbrakk>tp = tm_of ap; ly = layout_of ap; mop_ss = start_of ly (length ap)\<rbrakk> \<Longrightarrow> 
       
  5092     tm_wf (tp @ shift( mopup n) (length tp div 2), 0)"
       
  5093   using length_start_of_tm[of ap] all_le_start_of[of ap]
       
  5094 apply(auto simp: tm_wf.simps List.list_all_iff)
       
  5095 done
       
  5096 
       
  5097 end
       
  5098 
       
  5099     
       
  5100   
       
  5101 
       
  5102 
       
  5103   
       
  5104