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