thys/UTM.thy
changeset 288 a9003e6d0463
parent 285 447b433b67fa
child 292 293e9c6f22e1
equal deleted inserted replaced
287:d5a0e25c4742 288:a9003e6d0463
     1 (* Title: thys/UTM.thy
     1 (* Title: thys/UTM.thy
     2    Author: Jian Xu, Xingyuan Zhang, and Christian Urban
     2    Author: Jian Xu, Xingyuan Zhang, and Christian Urban
     3 *)
     3 *)
     4 
     4 
     5 header {* Construction of a Universal Turing Machine *}
     5 chapter {* Construction of a Universal Turing Machine *}
     6 
     6 
     7 theory UTM
     7 theory UTM
     8 imports Recursive Abacus UF GCD Turing_Hoare
     8 imports Recursive Abacus UF HOL.GCD Turing_Hoare
     9 begin
     9 begin
    10 
    10 
    11 section {* Wang coding of input arguments *}
    11 section {* Wang coding of input arguments *}
    12 
    12 
    13 text {*
    13 text {*
   682   "wcode_backto_standard_pos ires rs (l, r) = (wcode_backto_standard_pos_B ires rs (l, r) \<or>
   682   "wcode_backto_standard_pos ires rs (l, r) = (wcode_backto_standard_pos_B ires rs (l, r) \<or>
   683                                             wcode_backto_standard_pos_O ires rs (l, r))"
   683                                             wcode_backto_standard_pos_O ires rs (l, r))"
   684 
   684 
   685 declare wcode_backto_standard_pos.simps[simp del]
   685 declare wcode_backto_standard_pos.simps[simp del]
   686 
   686 
   687 lemma [simp]: "<0::nat> = [Oc]"
       
   688 apply(simp add: tape_of_nat_abv tape_of_nat_list.simps)
       
   689 done
       
   690 
       
   691 lemma tape_of_Suc_nat: "<Suc (a ::nat)> = replicate a Oc @ [Oc, Oc]"
       
   692 apply(simp only: tape_of_nat_abv exp_ind, simp)
       
   693 done
       
   694 
       
   695 lemma [simp]: "length (<a::nat>) = Suc a"
       
   696 apply(simp add: tape_of_nat_abv tape_of_nat_list.simps)
       
   697 done
       
   698 
       
   699 lemma [simp]: "<[a::nat]> = <a>"
       
   700 apply(simp add: tape_of_nat_abv tape_of_nl_abv
       
   701   tape_of_nat_list.simps)
       
   702 done
       
   703 
       
   704 lemma bin_wc_eq: "bl_bin xs = bl2wc xs"
   687 lemma bin_wc_eq: "bl_bin xs = bl2wc xs"
   705 proof(induct xs)
   688 proof(induct xs)
   706   show " bl_bin [] = bl2wc []" 
   689   show " bl_bin [] = bl2wc []" 
   707     apply(simp add: bl_bin.simps)
   690     apply(simp add: bl_bin.simps)
   708     done
   691     done
   713     apply(case_tac a, simp_all add: bl_bin.simps bl2wc.simps)
   696     apply(case_tac a, simp_all add: bl_bin.simps bl2wc.simps)
   714     apply(simp_all add: bl2nat.simps bl2nat_double)
   697     apply(simp_all add: bl2nat.simps bl2nat_double)
   715     done
   698     done
   716 qed
   699 qed
   717 
   700 
   718 lemma bl_bin_nat_Suc:  
       
   719   "bl_bin (<Suc a>) = bl_bin (<a>) + 2^(Suc a)"
       
   720 apply(simp add: tape_of_nat_abv bl_bin.simps)
       
   721 apply(induct a, auto simp: bl_bin.simps)
       
   722 done
       
   723 
       
   724 lemma [simp]: " rev (a\<up>(aa)) = a\<up>(aa)"
       
   725 apply(simp)
       
   726 done
       
   727 
       
   728 lemma tape_of_nl_append_one: "lm \<noteq> [] \<Longrightarrow>  <lm @ [a]> = <lm> @ Bk # Oc\<up>Suc a"
   701 lemma tape_of_nl_append_one: "lm \<noteq> [] \<Longrightarrow>  <lm @ [a]> = <lm> @ Bk # Oc\<up>Suc a"
   729 apply(induct lm, auto simp: tape_of_nl_cons split:if_splits)
   702 apply(induct lm, auto simp: tape_of_nl_cons split:if_splits)
   730 done
   703 done
   731 
   704 
   732 lemma tape_of_nl_rev: "rev (<lm::nat list>) = (<rev lm>)"
   705 lemma tape_of_nl_rev: "rev (<lm::nat list>) = (<rev lm>)"
   733 apply(induct lm, simp, auto)
   706 apply(induct lm, simp, auto)
   734 apply(auto simp: tape_of_nl_cons tape_of_nl_append_one split: if_splits)
   707 apply(auto simp: tape_of_nl_cons tape_of_nl_append_one split: if_splits)
   735 apply(simp add: exp_ind[THEN sym])
   708 apply(simp add: exp_ind[THEN sym])
   736 done
   709 done
   737 
   710 
   738 lemma [simp]: "a\<up>(Suc 0) = [a]" 
   711 lemma exp_1[simp]: "a\<up>(Suc 0) = [a]" 
   739 by(simp)
   712 by(simp)
   740 
   713 
   741 lemma tape_of_nl_cons_app1: "(<a # xs @ [b]>) = (Oc\<up>(Suc a) @ Bk # (<xs@ [b]>))"
   714 lemma tape_of_nl_cons_app1: "(<a # xs @ [b]>) = (Oc\<up>(Suc a) @ Bk # (<xs@ [b]>))"
   742 apply(case_tac xs, simp add: tape_of_nl_abv tape_of_nat_list.simps tape_of_nat_abv)
   715 apply(case_tac xs; simp add: tape_of_list_def tape_of_nat_list.simps tape_of_nat_def)
   743 apply(simp add: tape_of_nl_abv  tape_of_nat_list.simps tape_of_nat_abv)
       
   744 done
   716 done
   745 
   717 
   746 lemma bl_bin_bk_oc[simp]:
   718 lemma bl_bin_bk_oc[simp]:
   747   "bl_bin (xs @ [Bk, Oc]) = 
   719   "bl_bin (xs @ [Bk, Oc]) = 
   748   bl_bin xs + 2*2^(length xs)"
   720   bl_bin xs + 2*2^(length xs)"
   750 using bl2nat_cons_oc[of "xs @ [Bk]"]
   722 using bl2nat_cons_oc[of "xs @ [Bk]"]
   751 apply(simp add: bl2nat_cons_bk bl2wc.simps)
   723 apply(simp add: bl2nat_cons_bk bl2wc.simps)
   752 done
   724 done
   753 
   725 
   754 lemma tape_of_nat[simp]: "(<a::nat>) = Oc\<up>(Suc a)"
   726 lemma tape_of_nat[simp]: "(<a::nat>) = Oc\<up>(Suc a)"
   755 apply(simp add: tape_of_nat_abv)
   727 apply(simp add: tape_of_nat_def)
   756 done
   728 done
   757 
   729 
   758 lemma tape_of_nl_cons_app2: "(<c # xs @ [b]>) = (<c # xs> @ Bk # Oc\<up>(Suc b))"
   730 lemma tape_of_nl_cons_app2: "(<c # xs @ [b]>) = (<c # xs> @ Bk # Oc\<up>(Suc b))"
   759 proof(induct "length xs" arbitrary: xs c,
   731 proof(induct "length xs" arbitrary: xs c, simp add: tape_of_list_def)
   760   simp add: tape_of_nl_abv  tape_of_nat_list.simps)
       
   761   fix x xs c
   732   fix x xs c
   762   assume ind: "\<And>xs c. x = length xs \<Longrightarrow> <c # xs @ [b]> = 
   733   assume ind: "\<And>xs c. x = length xs \<Longrightarrow> <c # xs @ [b]> = 
   763     <c # xs> @ Bk # Oc\<up>(Suc b)"
   734     <c # xs> @ Bk # Oc\<up>(Suc b)"
   764     and h: "Suc x = length (xs::nat list)" 
   735     and h: "Suc x = length (xs::nat list)" 
   765   show "<c # xs @ [b]> = <c # xs> @ Bk # Oc\<up>(Suc b)"
   736   show "<c # xs @ [b]> = <c # xs> @ Bk # Oc\<up>(Suc b)"
   766   proof(case_tac xs, simp add: tape_of_nl_abv  tape_of_nat_list.simps)
   737   proof(case_tac xs, simp add: tape_of_list_def  tape_of_nat_list.simps)
   767     fix a list
   738     fix a list
   768     assume g: "xs = a # list"
   739     assume g: "xs = a # list"
   769     hence k: "<a # list @ [b]> =  <a # list> @ Bk # Oc\<up>(Suc b)"
   740     hence k: "<a # list @ [b]> =  <a # list> @ Bk # Oc\<up>(Suc b)"
   770       apply(rule_tac ind)
   741       apply(rule_tac ind)
   771       using h
   742       using h
   772       apply(simp)
   743       apply(simp)
   773       done
   744       done
   774     from g and k show "<c # xs @ [b]> = <c # xs> @ Bk # Oc\<up>(Suc b)"
   745     from g and k show "<c # xs @ [b]> = <c # xs> @ Bk # Oc\<up>(Suc b)"
   775       apply(simp add: tape_of_nl_abv tape_of_nat_list.simps)
   746       apply(simp add: tape_of_list_def tape_of_nat_list.simps)
   776       done
   747       done
   777   qed
   748   qed
   778 qed
   749 qed
   779 
   750 
   780 lemma [simp]: "length (<aa # a # list>) = Suc (Suc aa) + length (<a # list>)"
   751 lemma length_2_elems[simp]: "length (<aa # a # list>) = Suc (Suc aa) + length (<a # list>)"
   781 apply(simp add: tape_of_nl_abv tape_of_nat_list.simps)
   752 apply(simp add: tape_of_list_def tape_of_nat_list.simps)
   782 done
   753 done
   783 
   754 
   784 lemma [simp]: "bl_bin (Oc\<up>(Suc aa) @ Bk # tape_of_nat_list (a # lista) @ [Bk, Oc]) =
   755 lemma bl_bin_addition[simp]: "bl_bin (Oc\<up>(Suc aa) @ Bk # tape_of_nat_list (a # lista) @ [Bk, Oc]) =
   785               bl_bin (Oc\<up>(Suc aa) @ Bk # tape_of_nat_list (a # lista)) + 
   756               bl_bin (Oc\<up>(Suc aa) @ Bk # tape_of_nat_list (a # lista)) + 
   786               2* 2^(length (Oc\<up>(Suc aa) @ Bk # tape_of_nat_list (a # lista)))"
   757               2* 2^(length (Oc\<up>(Suc aa) @ Bk # tape_of_nat_list (a # lista)))"
   787 using bl_bin_bk_oc[of "Oc\<up>(Suc aa) @ Bk # tape_of_nat_list (a # lista)"]
   758 using bl_bin_bk_oc[of "Oc\<up>(Suc aa) @ Bk # tape_of_nat_list (a # lista)"]
   788 apply(simp)
   759 apply(simp)
   789 done
   760 done
   790 
   761 
   791 declare replicate_Suc[simp del]
   762 declare replicate_Suc[simp del]
   792 
   763 
   793 lemma [simp]: 
   764 lemma bl_bin_2[simp]: 
   794   "bl_bin (<aa # list>) + (4 * rs + 4) * 2 ^ (length (<aa # list>) - Suc 0)
   765   "bl_bin (<aa # list>) + (4 * rs + 4) * 2 ^ (length (<aa # list>) - Suc 0)
   795   = bl_bin (Oc\<up>(Suc aa) @ Bk # <list @ [0]>) + rs * (2 * 2 ^ (aa + length (<list @ [0]>)))"
   766   = bl_bin (Oc\<up>(Suc aa) @ Bk # <list @ [0]>) + rs * (2 * 2 ^ (aa + length (<list @ [0]>)))"
   796 
       
   797 apply(case_tac "list", simp add: add_mult_distrib)
   767 apply(case_tac "list", simp add: add_mult_distrib)
   798 apply(simp add: tape_of_nl_cons_app2 add_mult_distrib)
   768 apply(simp add: tape_of_nl_cons_app2 add_mult_distrib)
   799 apply(simp add: tape_of_nl_abv tape_of_nat_list.simps)
   769 apply(simp add: tape_of_list_def tape_of_nat_list.simps)
   800 done
   770 done
   801 
   771 
   802 lemma tape_of_nl_app_Suc: "((<list @ [Suc ab]>)) = (<list @ [ab]>) @ [Oc]"
   772 lemma tape_of_nl_app_Suc: "((<list @ [Suc ab]>)) = (<list @ [ab]>) @ [Oc]"
   803 apply(induct list)
   773 apply(induct list)
   804 apply(simp add: tape_of_nl_abv tape_of_nat_list.simps exp_ind)
   774 apply(simp add: tape_of_list_def tape_of_nat_list.simps exp_ind)
   805 apply(case_tac list)
   775 apply(case_tac list)
   806 apply(simp_all add:tape_of_nl_abv tape_of_nat_list.simps exp_ind)
   776 apply(simp_all add:tape_of_list_def tape_of_nat_list.simps exp_ind)
   807 done
   777 done
   808 
   778 
   809 lemma [simp]: "bl_bin (Oc # Oc\<up>(aa) @ Bk # <list @ [ab]> @ [Oc])
   779 lemma bl_bin_3[simp]: "bl_bin (Oc # Oc\<up>(aa) @ Bk # <list @ [ab]> @ [Oc])
   810               = bl_bin (Oc # Oc\<up>(aa) @ Bk # <list @ [ab]>) +
   780               = bl_bin (Oc # Oc\<up>(aa) @ Bk # <list @ [ab]>) +
   811               2^(length (Oc # Oc\<up>(aa) @ Bk # <list @ [ab]>))"
   781               2^(length (Oc # Oc\<up>(aa) @ Bk # <list @ [ab]>))"
   812 apply(simp add: bin_wc_eq)
   782 apply(simp add: bin_wc_eq)
   813 apply(simp add: bl2nat_cons_oc bl2wc.simps)
   783 apply(simp add: bl2nat_cons_oc bl2wc.simps)
   814 using bl2nat_cons_oc[of "Oc # Oc\<up>(aa) @ Bk # <list @ [ab]>"]
   784 using bl2nat_cons_oc[of "Oc # Oc\<up>(aa) @ Bk # <list @ [ab]>"]
   815 apply(simp)
   785 apply(simp)
   816 done
   786 done
   817 lemma [simp]: "bl_bin (Oc # Oc\<up>(aa) @ Bk # <list @ [ab]>) + (4 * 2 ^ (aa + length (<list @ [ab]>)) +
   787 lemma bl_bin_4[simp]: "bl_bin (Oc # Oc\<up>(aa) @ Bk # <list @ [ab]>) + (4 * 2 ^ (aa + length (<list @ [ab]>)) +
   818          4 * (rs * 2 ^ (aa + length (<list @ [ab]>)))) =
   788          4 * (rs * 2 ^ (aa + length (<list @ [ab]>)))) =
   819        bl_bin (Oc # Oc\<up>(aa) @ Bk # <list @ [Suc ab]>) +
   789        bl_bin (Oc # Oc\<up>(aa) @ Bk # <list @ [Suc ab]>) +
   820          rs * (2 * 2 ^ (aa + length (<list @ [Suc ab]>)))"
   790          rs * (2 * 2 ^ (aa + length (<list @ [Suc ab]>)))"
   821 apply(simp add: tape_of_nl_app_Suc)
   791 apply(simp add: tape_of_nl_app_Suc)
   822 done
   792 done
   861       wcode_double_case_step (st, l, r))"
   831       wcode_double_case_step (st, l, r))"
   862 
   832 
   863 definition wcode_double_case_le :: "(config \<times> config) set"
   833 definition wcode_double_case_le :: "(config \<times> config) set"
   864   where "wcode_double_case_le \<equiv> (inv_image lex_pair wcode_double_case_measure)"
   834   where "wcode_double_case_le \<equiv> (inv_image lex_pair wcode_double_case_measure)"
   865 
   835 
   866 lemma [intro]: "wf lex_pair"
   836 lemma wf_lex_pair[intro]: "wf lex_pair"
   867 by(auto intro:wf_lex_prod simp:lex_pair_def)
   837 by(auto intro:wf_lex_prod simp:lex_pair_def)
   868 
   838 
   869 lemma wf_wcode_double_case_le[intro]: "wf wcode_double_case_le"
   839 lemma wf_wcode_double_case_le[intro]: "wf wcode_double_case_le"
   870 by(auto intro:wf_inv_image simp: wcode_double_case_le_def )
   840 by(auto intro:wf_inv_image simp: wcode_double_case_le_def )
   871 
   841 
   872 lemma [simp]: "fetch t_wcode_main (Suc 0) Bk = (L, Suc 0)"
   842 lemma fetch_t_wcode_main[simp]:
   873 apply(simp add: t_wcode_main_def t_wcode_main_first_part_def
   843   "fetch t_wcode_main (Suc 0) Bk = (L, Suc 0)"
   874                 fetch.simps nth_of.simps)
   844   "fetch t_wcode_main (Suc 0) Oc = (L, Suc (Suc 0))"
   875 done
   845   "fetch t_wcode_main (Suc (Suc 0)) Oc = (R, 3)"
   876 
   846   "fetch t_wcode_main (Suc (Suc 0)) Bk = (L, 7)"
   877 lemma [simp]: "fetch t_wcode_main (Suc 0) Oc = (L, Suc (Suc 0))"
   847   "fetch t_wcode_main (Suc (Suc (Suc 0))) Bk = (R, 4)"
   878 apply(simp add: t_wcode_main_def t_wcode_main_first_part_def
   848   "fetch t_wcode_main (Suc (Suc (Suc 0))) Oc = (W0, 3)"
   879                 fetch.simps nth_of.simps)
   849   "fetch t_wcode_main 4 Bk = (R, 4)"
   880 done
   850   "fetch t_wcode_main 4 Oc = (R, 5)"
   881 
   851   "fetch t_wcode_main 5 Oc = (R, 5)"
   882 lemma [simp]: "fetch t_wcode_main (Suc (Suc 0)) Oc = (R, 3)"
   852   "fetch t_wcode_main 5 Bk = (W1, 6)"
   883 apply(simp add: t_wcode_main_def t_wcode_main_first_part_def
   853   "fetch t_wcode_main 6 Bk = (R, 13)"
   884                 fetch.simps nth_of.simps)
   854   "fetch t_wcode_main 6 Oc = (L, 6)"
   885 done
   855   "fetch t_wcode_main 7 Oc = (R, 8)"
   886 
   856   "fetch t_wcode_main 7 Bk = (R, 0)"
   887 lemma [simp]: "fetch t_wcode_main (Suc (Suc (Suc 0))) Bk = (R, 4)"
   857   "fetch t_wcode_main 8 Bk = (R, 9)"
   888 apply(simp add: t_wcode_main_def t_wcode_main_first_part_def
   858   "fetch t_wcode_main 9 Bk = (R, 10)"
   889                 fetch.simps nth_of.simps)
   859   "fetch t_wcode_main 9 Oc = (W0, 9)"
   890 done 
   860   "fetch t_wcode_main 10 Bk = (R, 10)"
   891 
   861   "fetch t_wcode_main 10 Oc = (R, 11)"
   892 lemma [simp]: "fetch t_wcode_main (Suc (Suc (Suc 0))) Oc = (W0, 3)"
   862   "fetch t_wcode_main 11 Bk = (W1, 12)"
   893 apply(simp add: t_wcode_main_def t_wcode_main_first_part_def
   863   "fetch t_wcode_main 11 Oc = (R, 11)"
   894                 fetch.simps nth_of.simps)
   864   "fetch t_wcode_main 12 Oc = (L, 12)"
   895 done
   865   "fetch t_wcode_main 12 Bk = (R, t_twice_len + 14)"
   896 
   866 by(auto simp: t_wcode_main_def t_wcode_main_first_part_def fetch.simps numeral)
   897 lemma [simp]: "fetch t_wcode_main 4 Bk = (R, 4)"
       
   898 apply(subgoal_tac "4 = Suc 3")
       
   899 apply(simp only: t_wcode_main_def t_wcode_main_first_part_def
       
   900                 fetch.simps nth_of.simps, auto)
       
   901 done
       
   902 
       
   903 lemma [simp]: "fetch t_wcode_main 4 Oc = (R, 5)"
       
   904 apply(subgoal_tac "4 = Suc 3")
       
   905 apply(simp only: t_wcode_main_def t_wcode_main_first_part_def
       
   906                 fetch.simps nth_of.simps, auto)
       
   907 done
       
   908 
       
   909 lemma [simp]: "fetch t_wcode_main 5 Oc = (R, 5)"
       
   910 apply(subgoal_tac "5 = Suc 4")
       
   911 apply(simp only: t_wcode_main_def t_wcode_main_first_part_def
       
   912                 fetch.simps nth_of.simps, auto)
       
   913 done
       
   914 
       
   915 lemma [simp]: "fetch t_wcode_main 5 Bk = (W1, 6)"
       
   916 apply(subgoal_tac "5 = Suc 4")
       
   917 apply(simp only: t_wcode_main_def t_wcode_main_first_part_def
       
   918                 fetch.simps nth_of.simps, auto)
       
   919 done
       
   920 
       
   921 lemma [simp]: "fetch t_wcode_main 6 Bk = (R, 13)"
       
   922 apply(subgoal_tac "6 = Suc 5")
       
   923 apply(simp only: t_wcode_main_def t_wcode_main_first_part_def
       
   924                 fetch.simps nth_of.simps, auto)
       
   925 done
       
   926   
       
   927 lemma [simp]: "fetch t_wcode_main 6 Oc = (L, 6)"
       
   928 apply(subgoal_tac "6 = Suc 5")
       
   929 apply(simp only: t_wcode_main_def t_wcode_main_first_part_def
       
   930                 fetch.simps nth_of.simps, auto)
       
   931 done
       
   932 
       
   933 lemma [elim]: "Bk\<up>(mr) = [] \<Longrightarrow> mr = 0"
       
   934 apply(case_tac mr, auto)
       
   935 done
       
   936 
       
   937 lemma [simp]: "wcode_on_left_moving_1 ires rs (b, []) = False"
       
   938 apply(simp add: wcode_on_left_moving_1.simps wcode_on_left_moving_1_B.simps
       
   939                 wcode_on_left_moving_1_O.simps) 
       
   940 done                                           
       
   941 
       
   942 
   867 
   943 declare wcode_on_checking_1.simps[simp del]
   868 declare wcode_on_checking_1.simps[simp del]
   944 
   869 
   945 lemmas wcode_double_case_inv_simps = 
   870 lemmas wcode_double_case_inv_simps = 
   946   wcode_on_left_moving_1.simps wcode_on_left_moving_1_O.simps
   871   wcode_on_left_moving_1.simps wcode_on_left_moving_1_O.simps
   947   wcode_on_left_moving_1_B.simps wcode_on_checking_1.simps
   872   wcode_on_left_moving_1_B.simps wcode_on_checking_1.simps
   948   wcode_erase1.simps wcode_on_right_moving_1.simps
   873   wcode_erase1.simps wcode_on_right_moving_1.simps
   949   wcode_goon_right_moving_1.simps wcode_backto_standard_pos.simps
   874   wcode_goon_right_moving_1.simps wcode_backto_standard_pos.simps
   950 
   875 
   951 
   876 
   952 lemma [simp]: "wcode_on_left_moving_1 ires rs (b, r) \<Longrightarrow> b \<noteq> []"
   877 lemma wcode_on_left_moving_1[simp]:
   953 apply(simp add: wcode_double_case_inv_simps, auto)
   878   "wcode_on_left_moving_1 ires rs (b, []) = False"
   954 done
   879   "wcode_on_left_moving_1 ires rs (b, r) \<Longrightarrow> b \<noteq> []"
   955 
   880 by(auto simp: wcode_on_left_moving_1.simps wcode_on_left_moving_1_B.simps
   956 
   881                 wcode_on_left_moving_1_O.simps)
   957 lemma [elim]: "\<lbrakk>wcode_on_left_moving_1 ires rs (b, Bk # list);
   882 
       
   883 lemma wcode_on_left_moving_1E[elim]: "\<lbrakk>wcode_on_left_moving_1 ires rs (b, Bk # list);
   958                 tl b = aa \<and> hd b # Bk # list = ba\<rbrakk> \<Longrightarrow> 
   884                 tl b = aa \<and> hd b # Bk # list = ba\<rbrakk> \<Longrightarrow> 
   959                wcode_on_left_moving_1 ires rs (aa, ba)"
   885                wcode_on_left_moving_1 ires rs (aa, ba)"
   960 apply(simp only: wcode_on_left_moving_1.simps wcode_on_left_moving_1_O.simps
   886 apply(simp only: wcode_on_left_moving_1.simps wcode_on_left_moving_1_O.simps
   961                 wcode_on_left_moving_1_B.simps)
   887                 wcode_on_left_moving_1_B.simps)
   962 apply(erule_tac disjE)
   888 apply(erule_tac disjE)
   971 apply(simp)
   897 apply(simp)
   972 done
   898 done
   973 
   899 
   974 declare replicate_Suc[simp]
   900 declare replicate_Suc[simp]
   975 
   901 
   976 lemma [elim]: 
   902 lemma wcode_on_moving_1_Elim[elim]: 
   977   "\<lbrakk>wcode_on_left_moving_1 ires rs (b, Oc # list); tl b = aa \<and> hd b # Oc # list = ba\<rbrakk> 
   903   "\<lbrakk>wcode_on_left_moving_1 ires rs (b, Oc # list); tl b = aa \<and> hd b # Oc # list = ba\<rbrakk> 
   978     \<Longrightarrow> wcode_on_checking_1 ires rs (aa, ba)"
   904     \<Longrightarrow> wcode_on_checking_1 ires rs (aa, ba)"
   979 apply(simp only: wcode_double_case_inv_simps)
   905 apply(simp only: wcode_double_case_inv_simps)
   980 apply(erule_tac disjE)
   906 apply(erule_tac disjE)
   981 apply(erule_tac [!] exE)+
   907 apply(erule_tac [!] exE)+
   982 apply(case_tac mr, simp, auto)
   908 apply(case_tac mr, simp, auto)
   983 done
   909 done
   984 
   910 
   985 lemma [simp]: "wcode_on_checking_1 ires rs (b, []) = False" 
   911 lemma wcode_on_checking_1_Elim[elim]: "\<lbrakk>wcode_on_checking_1 ires rs (b, Oc # ba);Oc # b = aa \<and> list = ba\<rbrakk>
   986 apply(auto simp: wcode_double_case_inv_simps)
       
   987 done         
       
   988  
       
   989 lemma [simp]: "wcode_on_checking_1 ires rs (b, Bk # list) = False"
       
   990 apply(auto simp: wcode_double_case_inv_simps)
       
   991 done         
       
   992   
       
   993 lemma [elim]: "\<lbrakk>wcode_on_checking_1 ires rs (b, Oc # ba);Oc # b = aa \<and> list = ba\<rbrakk>
       
   994   \<Longrightarrow> wcode_erase1 ires rs (aa, ba)"
   912   \<Longrightarrow> wcode_erase1 ires rs (aa, ba)"
   995 apply(simp only: wcode_double_case_inv_simps)
   913 apply(simp only: wcode_double_case_inv_simps)
   996 apply(erule_tac exE)+
   914 apply(erule_tac exE)+
   997 apply(rule_tac x = ln in exI, rule_tac x = rn in exI, simp)
   915 apply(rule_tac x = ln in exI, rule_tac x = rn in exI, simp)
   998 done
   916 done
   999 
   917 
  1000 
   918 lemma wcode_on_checking_1_simp[simp]:
  1001 lemma [simp]: "wcode_on_checking_1 ires rs (b, []) = False"
   919   "wcode_on_checking_1 ires rs (b, []) = False" 
       
   920   "wcode_on_checking_1 ires rs (b, Bk # list) = False"
       
   921 by(auto simp: wcode_double_case_inv_simps)
       
   922   
       
   923 lemma wcode_erase1_nonempty_snd[simp]: "wcode_erase1 ires rs (b, []) = False"
  1002 apply(simp add: wcode_double_case_inv_simps)
   924 apply(simp add: wcode_double_case_inv_simps)
  1003 done
   925 done
  1004 
   926 
  1005 lemma [simp]: "wcode_on_checking_1 ires rs ([], Bk # list) = False"
   927 lemma wcode_on_right_moving_1_nonempty_snd[simp]: "wcode_on_right_moving_1 ires rs (b, []) = False"
  1006 apply(simp add: wcode_double_case_inv_simps)
   928 apply(simp add: wcode_double_case_inv_simps)
  1007 done
   929 done
  1008 
   930 
  1009 lemma [simp]: "wcode_erase1 ires rs (b, []) = False"
   931 lemma wcode_on_right_moving_1_BkE[elim]:
  1010 apply(simp add: wcode_double_case_inv_simps)
   932  "\<lbrakk>wcode_on_right_moving_1 ires rs (b, Bk # ba);  Bk # b = aa \<and> list = b\<rbrakk> \<Longrightarrow> 
  1011 done
       
  1012 
       
  1013 lemma [simp]: "wcode_on_right_moving_1 ires rs (b, []) = False"
       
  1014 apply(simp add: wcode_double_case_inv_simps)
       
  1015 done
       
  1016 
       
  1017 lemma [simp]: "wcode_on_right_moving_1 ires rs (b, []) = False"
       
  1018 apply(simp add: wcode_double_case_inv_simps)
       
  1019 done
       
  1020 
       
  1021 lemma [elim]: "\<lbrakk>wcode_on_right_moving_1 ires rs (b, Bk # ba);  Bk # b = aa \<and> list = b\<rbrakk> \<Longrightarrow> 
       
  1022   wcode_on_right_moving_1 ires rs (aa, ba)"
   933   wcode_on_right_moving_1 ires rs (aa, ba)"
  1023 apply(simp only: wcode_double_case_inv_simps)
   934 apply(simp only: wcode_double_case_inv_simps)
  1024 apply(erule_tac exE)+
   935 apply(erule_tac exE)+
  1025 apply(rule_tac x = "Suc ml" in exI, rule_tac x = "mr - Suc 0" in exI,
   936 apply(rule_tac x = "Suc ml" in exI, rule_tac x = "mr - Suc 0" in exI,
  1026       rule_tac x = rn in exI)
   937       rule_tac x = rn in exI)
  1027 apply(simp)
   938 apply(simp)
  1028 apply(case_tac mr, simp, simp)
   939 apply(case_tac mr, simp, simp)
  1029 done
   940 done
  1030 
   941 
  1031 lemma [elim]: 
   942 lemma wcode_on_right_moving_1_OcE[elim]: 
  1032   "\<lbrakk>wcode_on_right_moving_1 ires rs (b, Oc # ba); Oc # b = aa \<and> list = ba\<rbrakk> 
   943   "\<lbrakk>wcode_on_right_moving_1 ires rs (b, Oc # ba); Oc # b = aa \<and> list = ba\<rbrakk> 
  1033   \<Longrightarrow> wcode_goon_right_moving_1 ires rs (aa, ba)"
   944   \<Longrightarrow> wcode_goon_right_moving_1 ires rs (aa, ba)"
  1034 apply(simp only: wcode_double_case_inv_simps)
   945 apply(simp only: wcode_double_case_inv_simps)
  1035 apply(erule_tac exE)+
   946 apply(erule_tac exE)+
  1036 apply(rule_tac x = "Suc 0" in exI, rule_tac x = "rs" in exI,
   947 apply(rule_tac x = "Suc 0" in exI, rule_tac x = "rs" in exI,
  1037       rule_tac x = "ml - Suc (Suc 0)" in exI, rule_tac x = rn in exI)
   948       rule_tac x = "ml - Suc (Suc 0)" in exI, rule_tac x = rn in exI)
  1038 apply(case_tac mr, simp_all)
   949 apply(case_tac mr, simp_all)
  1039 apply(case_tac ml, simp, case_tac nat, simp, simp)
   950 apply(case_tac ml, simp, case_tac nat, simp, simp)
  1040 done
   951 done
  1041 
   952 
  1042 lemma [simp]: 
   953 lemma wcode_erase1_BkE[elim]: "\<lbrakk>wcode_erase1 ires rs (b, Bk # ba); Bk # b = aa \<and> list = ba; c = Bk # ba\<rbrakk> 
  1043   "wcode_on_right_moving_1 ires rs (b, []) \<Longrightarrow> False"
       
  1044 apply(simp add: wcode_double_case_inv_simps)
       
  1045 done
       
  1046 
       
  1047 lemma [elim]: "\<lbrakk>wcode_erase1 ires rs (b, Bk # ba); Bk # b = aa \<and> list = ba; c = Bk # ba\<rbrakk> 
       
  1048   \<Longrightarrow> wcode_on_right_moving_1 ires rs (aa, ba)"
   954   \<Longrightarrow> wcode_on_right_moving_1 ires rs (aa, ba)"
  1049 apply(simp only: wcode_double_case_inv_simps)
   955 apply(simp only: wcode_double_case_inv_simps)
  1050 apply(erule_tac exE)+
   956 apply(erule_tac exE)+
  1051 apply(rule_tac x = "Suc 0" in exI, rule_tac x = "Suc (Suc ln)" in exI, 
   957 apply(rule_tac x = "Suc 0" in exI, rule_tac x = "Suc (Suc ln)" in exI, 
  1052       rule_tac x = rn in exI, simp add: exp_ind del: replicate_Suc)
   958       rule_tac x = rn in exI, simp add: exp_ind del: replicate_Suc)
  1053 done
   959 done
  1054 
   960 
  1055 lemma [elim]: "\<lbrakk>wcode_erase1 ires rs (aa, Oc # list);  b = aa \<and> Bk # list = ba\<rbrakk> \<Longrightarrow> 
   961 lemma wcode_erase1_OcE[elim]: "\<lbrakk>wcode_erase1 ires rs (aa, Oc # list);  b = aa \<and> Bk # list = ba\<rbrakk> \<Longrightarrow> 
  1056   wcode_erase1 ires rs (aa, ba)"
   962   wcode_erase1 ires rs (aa, ba)"
  1057 apply(simp only: wcode_double_case_inv_simps)
   963 apply(simp only: wcode_double_case_inv_simps)
  1058 apply(erule_tac exE)+
   964 apply(erule_tac exE)+
  1059 apply(rule_tac x = ln in exI, rule_tac x = rn in exI, auto)
   965 apply(rule_tac x = ln in exI, rule_tac x = rn in exI, auto)
  1060 done
   966 done
  1061 
   967 
  1062 lemma [elim]: "\<lbrakk>wcode_goon_right_moving_1 ires rs (aa, []); b = aa \<and> [Oc] = ba\<rbrakk> 
   968 lemma wcode_goon_right_moving_1_emptyE[elim]: "\<lbrakk>wcode_goon_right_moving_1 ires rs (aa, []); b = aa \<and> [Oc] = ba\<rbrakk> 
  1063               \<Longrightarrow> wcode_backto_standard_pos ires rs (aa, ba)"
   969               \<Longrightarrow> wcode_backto_standard_pos ires rs (aa, ba)"
  1064 apply(simp only: wcode_double_case_inv_simps)
   970 apply(simp only: wcode_double_case_inv_simps)
  1065 apply(erule_tac exE)+
   971 apply(erule_tac exE)+
  1066 apply(rule_tac disjI2)
   972 apply(rule_tac disjI2)
  1067 apply(simp only:wcode_backto_standard_pos_O.simps)
   973 apply(simp only:wcode_backto_standard_pos_O.simps)
  1068 apply(rule_tac x = ml in exI, rule_tac x = "Suc 0" in exI, rule_tac x = ln in exI,
   974 apply(rule_tac x = ml in exI, rule_tac x = "Suc 0" in exI, rule_tac x = ln in exI,
  1069       rule_tac x = rn in exI, simp)
   975       rule_tac x = rn in exI, simp)
  1070 done
   976 done
  1071 
   977 
  1072 lemma [elim]: 
   978 lemma wcode_goon_right_moving_1_BkE[elim]: 
  1073   "\<lbrakk>wcode_goon_right_moving_1 ires rs (aa, Bk # list);  b = aa \<and> Oc # list = ba\<rbrakk>
   979   "\<lbrakk>wcode_goon_right_moving_1 ires rs (aa, Bk # list);  b = aa \<and> Oc # list = ba\<rbrakk>
  1074   \<Longrightarrow> wcode_backto_standard_pos ires rs (aa, ba)"
   980   \<Longrightarrow> wcode_backto_standard_pos ires rs (aa, ba)"
  1075 apply(simp only: wcode_double_case_inv_simps)
   981 apply(simp only: wcode_double_case_inv_simps)
  1076 apply(erule_tac exE)+
   982 apply(erule_tac exE)+
  1077 apply(rule_tac disjI2)
   983 apply(rule_tac disjI2)
  1079 apply(rule_tac x = ml in exI, rule_tac x = "Suc 0" in exI, rule_tac x = ln in exI,
   985 apply(rule_tac x = ml in exI, rule_tac x = "Suc 0" in exI, rule_tac x = ln in exI,
  1080       rule_tac x = "rn - Suc 0" in exI, simp)
   986       rule_tac x = "rn - Suc 0" in exI, simp)
  1081 apply(case_tac mr, simp, case_tac rn, simp, simp_all)
   987 apply(case_tac mr, simp, case_tac rn, simp, simp_all)
  1082 done
   988 done
  1083 
   989 
  1084 lemma [elim]: "\<lbrakk>wcode_goon_right_moving_1 ires rs (b, Oc # ba);  Oc # b = aa \<and> list = ba\<rbrakk> 
   990 lemma wcode_goon_right_moving_1_OcE[elim]: "\<lbrakk>wcode_goon_right_moving_1 ires rs (b, Oc # ba);  Oc # b = aa \<and> list = ba\<rbrakk> 
  1085   \<Longrightarrow> wcode_goon_right_moving_1 ires rs (aa, ba)"
   991   \<Longrightarrow> wcode_goon_right_moving_1 ires rs (aa, ba)"
  1086 apply(simp only: wcode_double_case_inv_simps)
   992 apply(simp only: wcode_double_case_inv_simps)
  1087 apply(erule_tac exE)+
   993 apply(erule_tac exE)+
  1088 apply(rule_tac x = "Suc ml" in exI, rule_tac x = "mr - Suc 0" in exI, 
   994 apply(rule_tac x = "Suc ml" in exI, rule_tac x = "mr - Suc 0" in exI, 
  1089       rule_tac x = ln in exI, rule_tac x = rn in exI)
   995       rule_tac x = ln in exI, rule_tac x = rn in exI)
  1090 apply(simp)
   996 apply(simp)
  1091 apply(case_tac mr, simp, case_tac rn, simp_all)
   997 apply(case_tac mr, simp, case_tac rn, simp_all)
  1092 done
   998 done
  1093 
   999 
  1094 lemma [elim]: "\<lbrakk>wcode_backto_standard_pos ires rs (b, []);  Bk # b = aa\<rbrakk> \<Longrightarrow> False"
  1000 
  1095 apply(auto simp: wcode_double_case_inv_simps wcode_backto_standard_pos_O.simps
  1001 lemma wcode_backto_standard_pos_BkE[elim]: "\<lbrakk>wcode_backto_standard_pos ires rs (b, Bk # ba); Bk # b = aa \<and> list = ba\<rbrakk> 
  1096                  wcode_backto_standard_pos_B.simps)
       
  1097 done
       
  1098 
       
  1099 lemma [elim]: "\<lbrakk>wcode_backto_standard_pos ires rs (b, Bk # ba); Bk # b = aa \<and> list = ba\<rbrakk> 
       
  1100   \<Longrightarrow> wcode_before_double ires rs (aa, ba)"
  1002   \<Longrightarrow> wcode_before_double ires rs (aa, ba)"
  1101 apply(simp only: wcode_double_case_inv_simps wcode_backto_standard_pos_B.simps
  1003 apply(simp only: wcode_double_case_inv_simps wcode_backto_standard_pos_B.simps
  1102                  wcode_backto_standard_pos_O.simps wcode_before_double.simps)
  1004                  wcode_backto_standard_pos_O.simps wcode_before_double.simps)
  1103 apply(erule_tac disjE)
  1005 apply(erule_tac disjE)
  1104 apply(erule_tac exE)+
  1006 apply(erule_tac exE)+
  1105 apply(rule_tac x = ln in exI, rule_tac x = rn in exI, simp)
  1007 apply(rule_tac x = ln in exI, rule_tac x = rn in exI, simp)
  1106 apply(auto)
  1008 apply(auto)
  1107 apply(case_tac [!] mr, simp_all)
  1009 apply(case_tac [!] mr, simp_all)
  1108 done
  1010 done
  1109 
  1011 
  1110 lemma [simp]: "wcode_backto_standard_pos ires rs ([], Oc # list) = False"
  1012 lemma wcode_backto_standard_pos_no_Oc[simp]: "wcode_backto_standard_pos ires rs ([], Oc # list) = False"
  1111 apply(auto simp: wcode_backto_standard_pos.simps wcode_backto_standard_pos_B.simps
  1013 apply(auto simp: wcode_backto_standard_pos.simps wcode_backto_standard_pos_B.simps
  1112                  wcode_backto_standard_pos_O.simps)
  1014                  wcode_backto_standard_pos_O.simps)
  1113 done
  1015 done
  1114 
  1016 
  1115 lemma [simp]: "wcode_backto_standard_pos ires rs (b, []) = False"
  1017 lemma wcode_backto_standard_pos_nonempty_snd[simp]: "wcode_backto_standard_pos ires rs (b, []) = False"
  1116 apply(auto simp: wcode_backto_standard_pos.simps wcode_backto_standard_pos_B.simps
  1018 apply(auto simp: wcode_backto_standard_pos.simps wcode_backto_standard_pos_B.simps
  1117                  wcode_backto_standard_pos_O.simps)
  1019                  wcode_backto_standard_pos_O.simps)
  1118 done
  1020 done
  1119 
  1021 
  1120 lemma [elim]: "\<lbrakk>wcode_backto_standard_pos ires rs (b, Oc # list); tl b = aa; hd b # Oc # list =  ba\<rbrakk>
  1022 lemma wcode_backto_standard_pos_OcE[elim]: "\<lbrakk>wcode_backto_standard_pos ires rs (b, Oc # list); tl b = aa; hd b # Oc # list =  ba\<rbrakk>
  1121        \<Longrightarrow> wcode_backto_standard_pos ires rs (aa, ba)"
  1023        \<Longrightarrow> wcode_backto_standard_pos ires rs (aa, ba)"
  1122 apply(simp only:  wcode_backto_standard_pos.simps wcode_backto_standard_pos_B.simps
  1024 apply(simp only:  wcode_backto_standard_pos.simps wcode_backto_standard_pos_B.simps
  1123                  wcode_backto_standard_pos_O.simps)
  1025                  wcode_backto_standard_pos_O.simps)
  1124 apply(erule_tac disjE)
  1026 apply(erule_tac disjE)
  1125 apply(simp)
  1027 apply(simp)
  1208     done
  1110     done
  1209   thus "?thesis"
  1111   thus "?thesis"
  1210     by simp
  1112     by simp
  1211 qed 
  1113 qed 
  1212 
  1114 
  1213 lemma t_twice_len_ge: "Suc 0 \<le> length t_twice div 2"
       
  1214 apply(simp add: t_twice_def mopup.simps t_twice_compile_def)
       
  1215 done
       
  1216 
       
  1217 declare start_of.simps[simp del]
  1115 declare start_of.simps[simp del]
  1218 
  1116 
  1219 lemma twice_lemma: "rec_exec rec_twice [rs] = 2*rs"
  1117 lemma twice_lemma: "rec_exec rec_twice [rs] = 2*rs"
  1220 by(auto simp: rec_twice_def rec_exec.simps)
  1118 by(auto simp: rec_twice_def rec_exec.simps)
  1221 
  1119 
  1240     show "tm_of abc_twice = tm_of (a [+] dummy_abc (length [rs]))"
  1138     show "tm_of abc_twice = tm_of (a [+] dummy_abc (length [rs]))"
  1241       using h
  1139       using h
  1242       by(simp add: abc_twice_def)
  1140       by(simp add: abc_twice_def)
  1243   qed
  1141   qed
  1244   thus "?thesis"
  1142   thus "?thesis"
  1245     apply(simp add: tape_of_nl_abv tape_of_nat_list.simps tape_of_nat_abv rec_exec.simps twice_lemma)
  1143     apply(simp add: tape_of_list_def tape_of_nat_def rec_exec.simps twice_lemma)
  1246     done
  1144     done
  1247 qed
  1145 qed
  1248 
  1146 
  1249 declare adjust.simps[simp]
  1147 declare adjust.simps[simp]
  1250 
  1148 
  1366     done
  1264     done
  1367 qed    
  1265 qed    
  1368    
  1266    
  1369 declare tm_wf.simps[simp del]
  1267 declare tm_wf.simps[simp del]
  1370 
  1268 
  1371 lemma [simp]: " tm_wf (t_twice_compile, 0)"
  1269 lemma tm_wf_t_twice_compile [simp]: "tm_wf (t_twice_compile, 0)"
  1372 apply(simp only: t_twice_compile_def)
  1270 apply(simp only: t_twice_compile_def)
  1373 apply(rule_tac wf_tm_from_abacus, simp)
  1271 apply(rule_tac wf_tm_from_abacus, simp)
  1374 done
  1272 done
  1375 
  1273 
  1376 lemma t_twice_change_term_state:
  1274 lemma t_twice_change_term_state:
  1397   thus "?thesis"
  1295   thus "?thesis"
  1398     apply(simp add: t_twice_def t_twice_len_def)
  1296     apply(simp add: t_twice_def t_twice_len_def)
  1399     by metis
  1297     by metis
  1400 qed
  1298 qed
  1401 
  1299 
  1402 lemma [intro]: "length t_wcode_main_first_part mod 2 = 0"
  1300 lemma length_t_wcode_main_first_part_even[intro]: "length t_wcode_main_first_part mod 2 = 0"
  1403 apply(auto simp: t_wcode_main_first_part_def)
  1301 apply(auto simp: t_wcode_main_first_part_def)
  1404 done
  1302 done
  1405 
  1303 
  1406 lemma t_twice_append_pre:
  1304 lemma t_twice_append_pre:
  1407   "steps0 (Suc 0, Bk # Bk # ires, Oc\<up>(Suc rs) @ Bk\<up>(n)) t_twice stp
  1305   "steps0 (Suc 0, Bk # Bk # ires, Oc\<up>(Suc rs) @ Bk\<up>(n)) t_twice stp
  1426   apply(rule_tac x = stp in exI, rule_tac x = ln in exI, rule_tac x = rn in exI)
  1324   apply(rule_tac x = stp in exI, rule_tac x = ln in exI, rule_tac x = rn in exI)
  1427   apply(simp)
  1325   apply(simp)
  1428   done
  1326   done
  1429   
  1327   
  1430 lemma mopup_mod2: "length (mopup k) mod 2  = 0"
  1328 lemma mopup_mod2: "length (mopup k) mod 2  = 0"
  1431 apply(auto simp: mopup.simps)
  1329   by(auto simp: mopup.simps)
  1432 by arith
  1330 
  1433 
  1331 lemma fetch_t_wcode_main_Oc[simp]: "fetch t_wcode_main (Suc (t_twice_len + length t_wcode_main_first_part div 2)) Oc
  1434 lemma [simp]: "fetch t_wcode_main (Suc (t_twice_len + length t_wcode_main_first_part div 2)) Oc
       
  1435      = (L, Suc 0)"
  1332      = (L, Suc 0)"
  1436 apply(subgoal_tac "length (t_twice) mod 2 = 0")
  1333 apply(subgoal_tac "length (t_twice) mod 2 = 0")
  1437 apply(simp add: t_wcode_main_def nth_append fetch.simps t_wcode_main_first_part_def 
  1334 apply(simp add: t_wcode_main_def nth_append fetch.simps t_wcode_main_first_part_def 
  1438   nth_of.simps t_twice_len_def, auto)
  1335   nth_of.simps t_twice_len_def, auto)
  1439 apply(simp add: t_twice_def t_twice_compile_def)
  1336 apply(simp add: t_twice_def t_twice_compile_def)
  1450 apply(simp add: steps.simps step.simps exp_ind)
  1347 apply(simp add: steps.simps step.simps exp_ind)
  1451 apply(case_tac m, simp_all)
  1348 apply(case_tac m, simp_all)
  1452 apply(simp add: exp_ind[THEN sym])
  1349 apply(simp add: exp_ind[THEN sym])
  1453 done
  1350 done
  1454 
  1351 
  1455 lemma wcode_main_first_part_len:
  1352 lemma wcode_main_first_part_len[simp]:
  1456   "length t_wcode_main_first_part = 24"
  1353   "length t_wcode_main_first_part = 24"
  1457   apply(simp add: t_wcode_main_first_part_def)
  1354   apply(simp add: t_wcode_main_first_part_def)
  1458   done
  1355   done
  1459 
  1356 
  1460 lemma wcode_double_case: 
  1357 lemma wcode_double_case: 
  1657 
  1554 
  1658 definition wcode_fourtimes_case_le :: "(config \<times> config) set"
  1555 definition wcode_fourtimes_case_le :: "(config \<times> config) set"
  1659   where "wcode_fourtimes_case_le \<equiv> (inv_image lex_pair wcode_fourtimes_case_measure)"
  1556   where "wcode_fourtimes_case_le \<equiv> (inv_image lex_pair wcode_fourtimes_case_measure)"
  1660 
  1557 
  1661 lemma wf_wcode_fourtimes_case_le[intro]: "wf wcode_fourtimes_case_le"
  1558 lemma wf_wcode_fourtimes_case_le[intro]: "wf wcode_fourtimes_case_le"
  1662 by(auto intro:wf_inv_image simp: wcode_fourtimes_case_le_def)
  1559 by(auto simp: wcode_fourtimes_case_le_def)
  1663 
  1560 
  1664 lemma [simp]: "fetch t_wcode_main (Suc (Suc 0)) Bk = (L, 7)"
  1561 lemma nonempty_snd [simp]:
  1665 apply(simp add: t_wcode_main_def fetch.simps 
  1562   "wcode_on_left_moving_2 ires rs (b, []) = False"
  1666   t_wcode_main_first_part_def nth_of.simps)
  1563   "wcode_on_checking_2 ires rs (b, []) = False"
  1667 done
  1564   "wcode_goon_checking ires rs (b, []) = False"
  1668 
  1565   "wcode_right_move ires rs (b, []) = False"
  1669 lemma [simp]: "fetch t_wcode_main 7 Oc = (R, 8)"
  1566   "wcode_erase2 ires rs (b, []) = False"
  1670 apply(subgoal_tac "7 = Suc 6")
  1567   "wcode_on_right_moving_2 ires rs (b, []) = False"
  1671 apply(simp only: t_wcode_main_def fetch.simps 
  1568   "wcode_backto_standard_pos_2 ires rs (b, []) = False"
  1672   t_wcode_main_first_part_def nth_of.simps)
  1569   "wcode_on_checking_2 ires rs (b, Oc # list) = False"
  1673 apply(auto)
  1570   by(auto simp: wcode_fourtimes_invs) 
  1674 done
  1571 
  1675  
  1572 lemma wcode_on_left_moving_2[simp]:
  1676 lemma [simp]: "fetch t_wcode_main 8 Bk = (R, 9)"
  1573  "wcode_on_left_moving_2 ires rs (b, Bk # list) \<Longrightarrow>  wcode_on_left_moving_2 ires rs (tl b, hd b # Bk # list)"
  1677 apply(subgoal_tac "8 = Suc 7")
       
  1678 apply(simp only: t_wcode_main_def fetch.simps 
       
  1679   t_wcode_main_first_part_def nth_of.simps)
       
  1680 apply(auto)
       
  1681 done
       
  1682 
       
  1683 
       
  1684 lemma [simp]: "fetch t_wcode_main 9 Bk = (R, 10)"
       
  1685 apply(subgoal_tac "9 = Suc 8")
       
  1686 apply(simp only: t_wcode_main_def fetch.simps 
       
  1687   t_wcode_main_first_part_def nth_of.simps)
       
  1688 apply(auto)
       
  1689 done
       
  1690 
       
  1691 lemma [simp]: "fetch t_wcode_main 9 Oc = (W0, 9)"
       
  1692 apply(subgoal_tac "9 = Suc 8")
       
  1693 apply(simp only: t_wcode_main_def fetch.simps 
       
  1694   t_wcode_main_first_part_def nth_of.simps)
       
  1695 apply(auto)
       
  1696 done
       
  1697 
       
  1698 lemma [simp]: "fetch t_wcode_main 10 Bk = (R, 10)"
       
  1699 apply(subgoal_tac "10 = Suc 9")
       
  1700 apply(simp only: t_wcode_main_def fetch.simps 
       
  1701   t_wcode_main_first_part_def nth_of.simps)
       
  1702 apply(auto)
       
  1703 done
       
  1704 
       
  1705 lemma [simp]: "fetch t_wcode_main 10 Oc = (R, 11)"
       
  1706 apply(subgoal_tac "10 = Suc 9")
       
  1707 apply(simp only: t_wcode_main_def fetch.simps 
       
  1708   t_wcode_main_first_part_def nth_of.simps)
       
  1709 apply(auto)
       
  1710 done
       
  1711 
       
  1712 lemma [simp]: "fetch t_wcode_main 11 Bk = (W1, 12)"
       
  1713 apply(subgoal_tac "11 = Suc 10")
       
  1714 apply(simp only: t_wcode_main_def fetch.simps 
       
  1715   t_wcode_main_first_part_def nth_of.simps)
       
  1716 apply(auto)
       
  1717 done
       
  1718 
       
  1719 lemma [simp]: "fetch t_wcode_main 11 Oc = (R, 11)"
       
  1720 apply(subgoal_tac "11 = Suc 10")
       
  1721 apply(simp only: t_wcode_main_def fetch.simps 
       
  1722   t_wcode_main_first_part_def nth_of.simps)
       
  1723 apply(auto)
       
  1724 done
       
  1725 
       
  1726 lemma [simp]: "fetch t_wcode_main 12 Oc = (L, 12)"
       
  1727 apply(subgoal_tac "12 = Suc 11")
       
  1728 apply(simp only: t_wcode_main_def fetch.simps 
       
  1729   t_wcode_main_first_part_def nth_of.simps)
       
  1730 apply(auto)
       
  1731 done
       
  1732 
       
  1733 lemma [simp]: "fetch t_wcode_main 12 Bk = (R, t_twice_len + 14)"
       
  1734 apply(subgoal_tac "12 = Suc 11")
       
  1735 apply(simp only: t_wcode_main_def fetch.simps 
       
  1736   t_wcode_main_first_part_def nth_of.simps)
       
  1737 apply(auto)
       
  1738 done
       
  1739 
       
  1740 lemma [simp]: "wcode_on_left_moving_2 ires rs (b, []) = False"
       
  1741 apply(auto simp: wcode_fourtimes_invs)
       
  1742 done
       
  1743 
       
  1744 lemma [simp]: "wcode_on_checking_2 ires rs (b, []) = False"
       
  1745 apply(auto simp: wcode_fourtimes_invs)
       
  1746 done          
       
  1747 
       
  1748 lemma [simp]: "wcode_goon_checking ires rs (b, []) = False"
       
  1749 apply(auto simp: wcode_fourtimes_invs)
       
  1750 done
       
  1751 
       
  1752 lemma [simp]: "wcode_right_move ires rs (b, []) = False"
       
  1753 apply(auto simp: wcode_fourtimes_invs)
       
  1754 done
       
  1755 
       
  1756 lemma [simp]: "wcode_erase2 ires rs (b, []) = False"
       
  1757 apply(auto simp: wcode_fourtimes_invs)
       
  1758 done
       
  1759 
       
  1760 lemma [simp]: "wcode_on_right_moving_2 ires rs (b, []) = False"
       
  1761 apply(auto simp: wcode_fourtimes_invs)
       
  1762 done
       
  1763 
       
  1764 lemma [simp]: "wcode_backto_standard_pos_2 ires rs (b, []) = False"
       
  1765 apply(auto simp: wcode_fourtimes_invs)
       
  1766 done
       
  1767     
       
  1768 lemma [simp]: "wcode_on_left_moving_2 ires rs (b, Bk # list) \<Longrightarrow> b \<noteq> []"
       
  1769 apply(simp add: wcode_fourtimes_invs, auto)
       
  1770 done     
       
  1771 
       
  1772 lemma [simp]: "wcode_on_left_moving_2 ires rs (b, Bk # list) \<Longrightarrow>  wcode_on_left_moving_2 ires rs (tl b, hd b # Bk # list)"
       
  1773 apply(simp only: wcode_fourtimes_invs)
  1574 apply(simp only: wcode_fourtimes_invs)
  1774 apply(erule_tac disjE)
  1575 apply(erule_tac disjE)
  1775 apply(erule_tac exE)+
  1576 apply(erule_tac exE)+
  1776 apply(case_tac ml, simp)
  1577 apply(case_tac ml, simp)
  1777 apply(rule_tac x = "mr - (Suc (Suc 0))" in exI, rule_tac x = rn in exI, simp)
  1578 apply(rule_tac x = "mr - (Suc (Suc 0))" in exI, rule_tac x = rn in exI, simp)
  1780 apply(rule_tac x = nat in exI, rule_tac x = "Suc mr" in exI, rule_tac x = rn in exI,
  1581 apply(rule_tac x = nat in exI, rule_tac x = "Suc mr" in exI, rule_tac x = rn in exI,
  1781       simp add: replicate_Suc)
  1582       simp add: replicate_Suc)
  1782 apply(simp)
  1583 apply(simp)
  1783 done
  1584 done
  1784 
  1585 
  1785 lemma [simp]: "wcode_on_checking_2 ires rs (b, Bk # list) \<Longrightarrow> b \<noteq> []"
  1586 lemma wcode_goon_checking_via_2 [simp]: "wcode_on_checking_2 ires rs (b, Bk # list)
  1786 apply(auto simp: wcode_fourtimes_invs)
       
  1787 done
       
  1788 
       
  1789 lemma  [simp]: "wcode_on_checking_2 ires rs (b, Bk # list)
       
  1790        \<Longrightarrow>   wcode_goon_checking ires rs (tl b, hd b # Bk # list)"
  1587        \<Longrightarrow>   wcode_goon_checking ires rs (tl b, hd b # Bk # list)"
  1791 apply(simp only: wcode_fourtimes_invs)
  1588 apply(simp only: wcode_fourtimes_invs)
  1792 apply(auto)
  1589 apply(auto)
  1793 done
  1590   done
  1794 
  1591 
  1795 lemma [simp]: "wcode_goon_checking ires rs (b, Bk # list) = False"
  1592 lemma wcode_erase2_via_move [simp]: "wcode_right_move ires rs (b, Bk # list) \<Longrightarrow>  wcode_erase2 ires rs (Bk # b, list)"
  1796 apply(simp add: wcode_fourtimes_invs)
       
  1797 done
       
  1798 
       
  1799 lemma [simp]: " wcode_right_move ires rs (b, Bk # list) \<Longrightarrow> b\<noteq> []" 
       
  1800 apply(simp add: wcode_fourtimes_invs)
       
  1801 done
       
  1802 
       
  1803 lemma [simp]: "wcode_right_move ires rs (b, Bk # list) \<Longrightarrow>  wcode_erase2 ires rs (Bk # b, list)"
       
  1804 apply(auto simp:wcode_fourtimes_invs )
  1593 apply(auto simp:wcode_fourtimes_invs )
  1805 apply(rule_tac x = ln in exI, rule_tac x = rn in exI, simp)
  1594 apply(rule_tac x = ln in exI, rule_tac x = rn in exI, simp)
  1806 done
  1595   done
  1807 
  1596 lemma wcode_on_right_moving_2_via_erase2[simp]:
  1808 lemma [simp]: "wcode_erase2 ires rs (b, Bk # list) \<Longrightarrow> b \<noteq> []"
  1597  "wcode_erase2 ires rs (b, Bk # list) \<Longrightarrow> wcode_on_right_moving_2 ires rs (Bk # b, list)"
  1809 apply(auto simp: wcode_fourtimes_invs)
       
  1810 done
       
  1811 
       
  1812 lemma [simp]: "wcode_erase2 ires rs (b, Bk # list) \<Longrightarrow> wcode_on_right_moving_2 ires rs (Bk # b, list)"
       
  1813 apply(auto simp:wcode_fourtimes_invs )
  1598 apply(auto simp:wcode_fourtimes_invs )
  1814 apply(rule_tac x = "Suc (Suc 0)" in exI, simp add: exp_ind)
  1599 apply(rule_tac x = "Suc (Suc 0)" in exI, simp add: exp_ind)
  1815 apply(rule_tac x =  "Suc (Suc ln)" in exI, simp add: exp_ind del: replicate_Suc)
  1600 apply(rule_tac x =  "Suc (Suc ln)" in exI, simp add: exp_ind del: replicate_Suc)
  1816 done
  1601 done
  1817 
  1602 
  1818 lemma [simp]: "wcode_on_right_moving_2 ires rs (b, Bk # list) \<Longrightarrow> b \<noteq> []"
  1603 lemma wcode_on_right_moving_2_move_Bk[simp]: "wcode_on_right_moving_2 ires rs (b, Bk # list)
  1819 apply(auto simp:wcode_fourtimes_invs )
       
  1820 done
       
  1821 
       
  1822 lemma [simp]: "wcode_on_right_moving_2 ires rs (b, Bk # list)
       
  1823        \<Longrightarrow> wcode_on_right_moving_2 ires rs (Bk # b, list)"
  1604        \<Longrightarrow> wcode_on_right_moving_2 ires rs (Bk # b, list)"
  1824 apply(auto simp: wcode_fourtimes_invs)
  1605 apply(auto simp: wcode_fourtimes_invs)
  1825 apply(rule_tac x = "Suc ml" in exI, simp)
  1606 apply(rule_tac x = "Suc ml" in exI, simp)
  1826 apply(rule_tac x = "mr - 1" in exI, case_tac mr,auto)
  1607 apply(rule_tac x = "mr - 1" in exI, case_tac mr,auto)
  1827 done
  1608   done
  1828 
  1609 
  1829 lemma [simp]: "wcode_goon_right_moving_2 ires rs (b, Bk # list) \<Longrightarrow> b \<noteq> []"
  1610 lemma wcode_backto_standard_pos_2_via_right[simp]:
  1830 apply(auto simp: wcode_fourtimes_invs)
  1611  "wcode_goon_right_moving_2 ires rs (b, Bk # list) \<Longrightarrow> 
  1831 done
       
  1832 
       
  1833 lemma [simp]: "wcode_goon_right_moving_2 ires rs (b, Bk # list) \<Longrightarrow> 
       
  1834                  wcode_backto_standard_pos_2 ires rs (b, Oc # list)"
  1612                  wcode_backto_standard_pos_2 ires rs (b, Oc # list)"
  1835 apply(simp add: wcode_fourtimes_invs, auto)
  1613 apply(simp add: wcode_fourtimes_invs, auto)
  1836 apply(rule_tac x = ml in exI, auto)
  1614 apply(rule_tac x = ml in exI, auto)
  1837 apply(rule_tac x = "Suc 0" in exI, simp)
  1615 apply(rule_tac x = "Suc 0" in exI, simp)
  1838 apply(case_tac mr, simp_all)
  1616 apply(case_tac mr, simp_all)
  1839 apply(rule_tac x = "rn - 1" in exI, simp)
  1617 apply(rule_tac x = "rn - 1" in exI, simp)
  1840 apply(case_tac rn, simp, simp)
  1618 apply(case_tac rn, simp, simp)
  1841 done
  1619   done
  1842    
  1620 
  1843 lemma  [simp]: "wcode_backto_standard_pos_2 ires rs (b, Bk # list) \<Longrightarrow>  b \<noteq> []"
  1621 lemma wcode_on_checking_2_via_left[simp]: "wcode_on_left_moving_2 ires rs (b, Oc # list) \<Longrightarrow> 
  1844 apply(simp add: wcode_fourtimes_invs, auto)
       
  1845 done
       
  1846 
       
  1847 lemma [simp]: "wcode_on_left_moving_2 ires rs (b, Oc # list) \<Longrightarrow> b \<noteq> []"
       
  1848 apply(simp add: wcode_fourtimes_invs, auto)
       
  1849 done
       
  1850 
       
  1851 lemma [simp]: "wcode_on_left_moving_2 ires rs (b, Oc # list) \<Longrightarrow> 
       
  1852                      wcode_on_checking_2 ires rs (tl b, hd b # Oc # list)"
  1622                      wcode_on_checking_2 ires rs (tl b, hd b # Oc # list)"
  1853 apply(auto simp: wcode_fourtimes_invs)
  1623 apply(auto simp: wcode_fourtimes_invs)
  1854 apply(case_tac [!] mr, simp_all)
  1624 apply(case_tac [!] mr, simp_all)
  1855 done
  1625   done
  1856 
  1626 
  1857 lemma [simp]: "wcode_goon_right_moving_2 ires rs (b, []) \<Longrightarrow> b \<noteq> []"
  1627 lemma wcode_backto_standard_pos_2_empty_via_right[simp]:
  1858 apply(auto simp: wcode_fourtimes_invs)
  1628  "wcode_goon_right_moving_2 ires rs (b, []) \<Longrightarrow>
  1859 done
       
  1860 
       
  1861 lemma [simp]: "wcode_goon_right_moving_2 ires rs (b, []) \<Longrightarrow>
       
  1862               wcode_backto_standard_pos_2 ires rs (b, [Oc])"
  1629               wcode_backto_standard_pos_2 ires rs (b, [Oc])"
  1863 apply(simp only: wcode_fourtimes_invs)
  1630 apply(simp only: wcode_fourtimes_invs)
  1864 apply(erule_tac exE)+
  1631 apply(erule_tac exE)+
  1865 apply(rule_tac disjI1)
  1632 apply(rule_tac disjI1)
  1866 apply(rule_tac x = ml in exI, rule_tac x = "Suc 0" in exI, 
  1633 apply(rule_tac x = ml in exI, rule_tac x = "Suc 0" in exI, 
  1867       rule_tac x = ln in exI, rule_tac x = rn in exI, simp)
  1634       rule_tac x = ln in exI, rule_tac x = rn in exI, simp)
  1868 done
  1635 done
  1869 
  1636 
  1870 lemma "wcode_backto_standard_pos_2 ires rs (b, Bk # list)
  1637 lemma wcode_goon_checking_cases[simp]: "wcode_goon_checking ires rs (b, Oc # list) \<Longrightarrow>
  1871        \<Longrightarrow> (\<exists>ln. b = Bk # Bk\<up>(ln) @ Oc # ires) \<and> (\<exists>rn. list = Oc\<up>(Suc (Suc rs)) @ Bk\<up>(rn))"
       
  1872 apply(auto simp: wcode_fourtimes_invs)
       
  1873 apply(case_tac [!] mr, auto)
       
  1874 done
       
  1875 
       
  1876 
       
  1877 lemma [simp]: "wcode_on_checking_2 ires rs (b, Oc # list) \<Longrightarrow> False"
       
  1878 apply(simp add: wcode_fourtimes_invs)
       
  1879 done
       
  1880 
       
  1881 lemma [simp]: "wcode_goon_checking ires rs (b, Oc # list) \<Longrightarrow>
       
  1882   (b = [] \<longrightarrow> wcode_right_move ires rs ([Oc], list)) \<and>
  1638   (b = [] \<longrightarrow> wcode_right_move ires rs ([Oc], list)) \<and>
  1883   (b \<noteq> [] \<longrightarrow> wcode_right_move ires rs (Oc # b, list))"
  1639   (b \<noteq> [] \<longrightarrow> wcode_right_move ires rs (Oc # b, list))"
  1884 apply(simp only: wcode_fourtimes_invs)
  1640 apply(simp only: wcode_fourtimes_invs)
  1885 apply(erule_tac exE)+
  1641 apply(erule_tac exE)+
  1886 apply(auto)
  1642 apply(auto)
  1887 done
  1643 done
  1888 
  1644 
  1889 lemma [simp]: "wcode_right_move ires rs (b, Oc # list) = False"
  1645 lemma wcode_right_move_no_Oc[simp]: "wcode_right_move ires rs (b, Oc # list) = False"
  1890 apply(auto simp: wcode_fourtimes_invs)
  1646 apply(auto simp: wcode_fourtimes_invs)
  1891 done
  1647 done
  1892 
  1648 
  1893 lemma [simp]: " wcode_erase2 ires rs (b, Oc # list) \<Longrightarrow> b \<noteq> []"
  1649 lemma wcode_erase2_Bk_via_Oc[simp]: "wcode_erase2 ires rs (b, Oc # list)
  1894 apply(simp add: wcode_fourtimes_invs)
       
  1895 done
       
  1896 
       
  1897 lemma [simp]: "wcode_erase2 ires rs (b, Oc # list)
       
  1898        \<Longrightarrow> wcode_erase2 ires rs (b, Bk # list)"
  1650        \<Longrightarrow> wcode_erase2 ires rs (b, Bk # list)"
  1899 apply(auto simp: wcode_fourtimes_invs)
  1651 apply(auto simp: wcode_fourtimes_invs)
  1900 done
  1652 done
  1901 
  1653 
  1902 lemma [simp]: "wcode_on_right_moving_2 ires rs (b, Oc # list) \<Longrightarrow> b \<noteq> []"
  1654 lemma wcode_goon_right_moving_2_Oc_move[simp]:
  1903 apply(simp only: wcode_fourtimes_invs)
  1655  "wcode_on_right_moving_2 ires rs (b, Oc # list)
  1904 apply(auto)
       
  1905 done
       
  1906 
       
  1907 lemma [simp]: "wcode_on_right_moving_2 ires rs (b, Oc # list)
       
  1908        \<Longrightarrow> wcode_goon_right_moving_2 ires rs (Oc # b, list)"
  1656        \<Longrightarrow> wcode_goon_right_moving_2 ires rs (Oc # b, list)"
  1909 apply(auto simp: wcode_fourtimes_invs)
  1657 apply(auto simp: wcode_fourtimes_invs)
  1910 apply(case_tac mr, simp_all)
  1658 apply(case_tac mr, simp_all)
  1911 apply(rule_tac x = "Suc 0" in exI, auto)
  1659 apply(rule_tac x = "Suc 0" in exI, auto)
  1912 apply(rule_tac x = "ml - 2" in exI)
  1660 apply(rule_tac x = "ml - 2" in exI)
  1913 apply(case_tac ml, simp, case_tac nat, simp_all)
  1661 apply(case_tac ml, simp, case_tac nat, simp_all)
  1914 done
  1662 done
  1915 
  1663 
  1916 lemma [simp]: "wcode_goon_right_moving_2 ires rs (b, Oc # list) \<Longrightarrow> b \<noteq> []"
  1664 lemma wcode_backto_standard_pos_2_exists[simp]: "wcode_backto_standard_pos_2 ires rs (b, Bk # list)
  1917 apply(simp only:wcode_fourtimes_invs, auto)
       
  1918 done
       
  1919 
       
  1920 lemma [simp]: "wcode_backto_standard_pos_2 ires rs (b, Bk # list)
       
  1921        \<Longrightarrow> (\<exists>ln. b = Bk # Bk\<up>(ln) @ Oc # ires) \<and> (\<exists>rn. list = Oc\<up>(Suc (Suc rs)) @ Bk\<up>(rn))"
  1665        \<Longrightarrow> (\<exists>ln. b = Bk # Bk\<up>(ln) @ Oc # ires) \<and> (\<exists>rn. list = Oc\<up>(Suc (Suc rs)) @ Bk\<up>(rn))"
  1922 apply(simp add: wcode_fourtimes_invs, auto)
  1666 apply(simp add: wcode_fourtimes_invs, auto)
  1923 apply(case_tac [!] mr, simp_all)
  1667 apply(case_tac [!] mr, simp_all)
  1924 done
  1668 done
  1925 
  1669 
  1926 lemma [simp]: "wcode_on_checking_2 ires rs (b, Oc # list) = False"
  1670 lemma wcode_goon_right_moving_2_move_Oc[simp]: "wcode_goon_right_moving_2 ires rs (b, Oc # list) \<Longrightarrow>
  1927 apply(simp add: wcode_fourtimes_invs)
       
  1928 done
       
  1929 
       
  1930 lemma [simp]: "wcode_goon_right_moving_2 ires rs (b, Oc # list) \<Longrightarrow>
       
  1931        wcode_goon_right_moving_2 ires rs (Oc # b, list)"
  1671        wcode_goon_right_moving_2 ires rs (Oc # b, list)"
  1932 apply(simp only:wcode_fourtimes_invs, auto)
  1672 apply(simp only:wcode_fourtimes_invs, auto)
  1933 apply(rule_tac x = "Suc ml" in exI, simp)
  1673 apply(rule_tac x = "Suc ml" in exI, simp)
  1934 apply(rule_tac x = "mr - 1" in exI)
  1674 apply(rule_tac x = "mr - 1" in exI)
  1935 apply(case_tac mr, case_tac rn, auto)
  1675 apply(case_tac mr, case_tac rn, auto)
  1936 done
  1676   done
  1937 
  1677 
  1938 lemma [simp]: "wcode_backto_standard_pos_2 ires rs (b, Oc # list) \<Longrightarrow> b \<noteq> []"
  1678 
  1939 apply(simp only: wcode_fourtimes_invs, auto)
  1679 lemma wcode_backto_standard_pos_2_Oc_mv_hd[simp]:
  1940 done
  1680  "wcode_backto_standard_pos_2 ires rs (b, Oc # list)    
  1941  
       
  1942 lemma [simp]: "wcode_backto_standard_pos_2 ires rs (b, Oc # list)    
       
  1943             \<Longrightarrow> wcode_backto_standard_pos_2 ires rs (tl b, hd b # Oc # list)"
  1681             \<Longrightarrow> wcode_backto_standard_pos_2 ires rs (tl b, hd b # Oc # list)"
  1944 apply(simp only: wcode_fourtimes_invs)
  1682 apply(simp only: wcode_fourtimes_invs)
  1945 apply(erule_tac disjE)
  1683 apply(erule_tac disjE)
  1946 apply(erule_tac exE)+
  1684 apply(erule_tac exE)+
  1947 apply(case_tac ml, auto)
  1685 apply(case_tac ml, auto)
  1948 apply(rule_tac x = nat in exI, auto)
  1686 apply(rule_tac x = nat in exI, auto)
  1949 apply(rule_tac x = "Suc mr" in exI, simp)
  1687 apply(rule_tac x = "Suc mr" in exI, simp)
  1950 done
  1688 done
       
  1689 
       
  1690 lemma nonempty_fst[simp]:
       
  1691   "wcode_on_left_moving_2 ires rs (b, Bk # list) \<Longrightarrow> b \<noteq> []"
       
  1692   "wcode_on_checking_2 ires rs (b, Bk # list) \<Longrightarrow> b \<noteq> []"
       
  1693   "wcode_goon_checking ires rs (b, Bk # list) = False"
       
  1694   "wcode_right_move ires rs (b, Bk # list) \<Longrightarrow> b\<noteq> []" 
       
  1695   "wcode_erase2 ires rs (b, Bk # list) \<Longrightarrow> b \<noteq> []"
       
  1696   "wcode_on_right_moving_2 ires rs (b, Bk # list) \<Longrightarrow> b \<noteq> []"
       
  1697   "wcode_goon_right_moving_2 ires rs (b, Bk # list) \<Longrightarrow> b \<noteq> []"
       
  1698   "wcode_backto_standard_pos_2 ires rs (b, Bk # list) \<Longrightarrow>  b \<noteq> []"
       
  1699   "wcode_on_left_moving_2 ires rs (b, Oc # list) \<Longrightarrow> b \<noteq> []"
       
  1700   "wcode_goon_right_moving_2 ires rs (b, []) \<Longrightarrow> b \<noteq> []"
       
  1701   "wcode_erase2 ires rs (b, Oc # list) \<Longrightarrow> b \<noteq> []"
       
  1702   "wcode_on_right_moving_2 ires rs (b, Oc # list) \<Longrightarrow> b \<noteq> []"
       
  1703   "wcode_goon_right_moving_2 ires rs (b, Oc # list) \<Longrightarrow> b \<noteq> []"
       
  1704   "wcode_backto_standard_pos_2 ires rs (b, Oc # list) \<Longrightarrow> b \<noteq> []"
       
  1705   by(auto simp: wcode_fourtimes_invs)
       
  1706 
  1951 
  1707 
  1952 lemma wcode_fourtimes_case_first_correctness:
  1708 lemma wcode_fourtimes_case_first_correctness:
  1953  shows "let P = (\<lambda> (st, l, r). st = t_twice_len + 14) in 
  1709  shows "let P = (\<lambda> (st, l, r). st = t_twice_len + 14) in 
  1954   let Q = (\<lambda> (st, l, r). wcode_fourtimes_case_inv st ires rs (l, r)) in 
  1710   let Q = (\<lambda> (st, l, r). wcode_fourtimes_case_inv st ires rs (l, r)) in 
  1955   let f = (\<lambda> stp. steps0 (Suc 0, Bk # Bk\<up>(m) @ Oc # Bk # Oc # ires, Bk # Oc\<up>(Suc rs) @ Bk\<up>(n)) t_wcode_main stp) in
  1711   let f = (\<lambda> stp. steps0 (Suc 0, Bk # Bk\<up>(m) @ Oc # Bk # Oc # ires, Bk # Oc\<up>(Suc rs) @ Bk\<up>(n)) t_wcode_main stp) in
  1995 
  1751 
  1996 definition t_fourtimes_len :: "nat"
  1752 definition t_fourtimes_len :: "nat"
  1997   where
  1753   where
  1998   "t_fourtimes_len = (length t_fourtimes div 2)"
  1754   "t_fourtimes_len = (length t_fourtimes div 2)"
  1999 
  1755 
  2000 lemma t_fourtimes_len_gr:  "t_fourtimes_len > 0"
  1756 lemma primerec_rec_fourtimes_1[intro]: "primerec rec_fourtimes (Suc 0)"
  2001 apply(simp add: t_fourtimes_len_def t_fourtimes_def mopup.simps t_fourtimes_compile_def)
       
  2002 done
       
  2003 
       
  2004 lemma [intro]: "primerec rec_fourtimes (Suc 0)"
       
  2005 apply(auto simp: rec_fourtimes_def numeral_4_eq_4 constn.simps)
  1757 apply(auto simp: rec_fourtimes_def numeral_4_eq_4 constn.simps)
  2006 by auto
  1758 by auto
  2007 
  1759 
  2008 lemma fourtimes_lemma: "rec_exec rec_fourtimes [rs] = 4 * rs"
  1760 lemma fourtimes_lemma: "rec_exec rec_fourtimes [rs] = 4 * rs"
  2009 by(simp add: rec_exec.simps rec_fourtimes_def)
  1761 by(simp add: rec_exec.simps rec_fourtimes_def)
  2010 
       
  2011 
  1762 
  2012 lemma t_fourtimes_correct: 
  1763 lemma t_fourtimes_correct: 
  2013   "\<exists>stp ln rn. steps0 (Suc 0, Bk # Bk # ires, Oc\<up>(Suc rs) @ Bk\<up>(n)) 
  1764   "\<exists>stp ln rn. steps0 (Suc 0, Bk # Bk # ires, Oc\<up>(Suc rs) @ Bk\<up>(n)) 
  2014     (tm_of abc_fourtimes @ shift (mopup 1) (length (tm_of abc_fourtimes) div 2)) stp =
  1765     (tm_of abc_fourtimes @ shift (mopup 1) (length (tm_of abc_fourtimes) div 2)) stp =
  2015        (0, Bk\<up>(ln) @ Bk # Bk # ires, Oc\<up>(Suc (4 * rs)) @ Bk\<up>(rn))"
  1766        (0, Bk\<up>(ln) @ Bk # Bk # ires, Oc\<up>(Suc (4 * rs)) @ Bk\<up>(rn))"
  2029     show "tm_of abc_fourtimes = tm_of (a [+] dummy_abc (length [rs]))"
  1780     show "tm_of abc_fourtimes = tm_of (a [+] dummy_abc (length [rs]))"
  2030       using h
  1781       using h
  2031       by(simp add: abc_fourtimes_def)
  1782       by(simp add: abc_fourtimes_def)
  2032   qed
  1783   qed
  2033   thus "?thesis"
  1784   thus "?thesis"
  2034     apply(simp add: tape_of_nl_abv tape_of_nat_list.simps tape_of_nat_abv fourtimes_lemma)
  1785     apply(simp add: tape_of_list_def tape_of_nat_def fourtimes_lemma)
  2035     done
  1786     done
  2036 qed
  1787 qed
  2037 
  1788 
  2038 lemma wf_fourtimes[intro]: "tm_wf (t_fourtimes_compile, 0)"
  1789 lemma wf_fourtimes[intro]: "tm_wf (t_fourtimes_compile, 0)"
  2039 apply(simp only: t_fourtimes_compile_def)
  1790 apply(simp only: t_fourtimes_compile_def)
  2065   thus "?thesis"
  1816   thus "?thesis"
  2066     apply(simp add: t_fourtimes_def t_fourtimes_len_def)
  1817     apply(simp add: t_fourtimes_def t_fourtimes_len_def)
  2067     by metis
  1818     by metis
  2068 qed
  1819 qed
  2069 
  1820 
  2070 lemma [intro]: "length t_twice mod 2 = 0"
  1821 lemma length_t_twice_even[intro]: "is_even (length t_twice)"
  2071 apply(auto simp: t_twice_def t_twice_compile_def)
  1822   by(auto simp: t_twice_def t_twice_compile_def intro!:mopup_mod2)
  2072 by (metis mopup_mod2)
       
  2073 
  1823 
  2074 lemma t_fourtimes_append_pre:
  1824 lemma t_fourtimes_append_pre:
  2075   "steps0 (Suc 0, Bk # Bk # ires, Oc\<up>(Suc rs) @ Bk\<up>(n)) t_fourtimes stp
  1825   "steps0 (Suc 0, Bk # Bk # ires, Oc\<up>(Suc rs) @ Bk\<up>(n)) t_fourtimes stp
  2076   = (Suc t_fourtimes_len, Bk\<up>(ln) @ Bk # Bk # ires, Oc\<up>(Suc (4 * rs)) @ Bk\<up>(rn))
  1826   = (Suc t_fourtimes_len, Bk\<up>(ln) @ Bk # Bk # ires, Oc\<up>(Suc (4 * rs)) @ Bk\<up>(rn))
  2077    \<Longrightarrow> steps0 (Suc 0 + length (t_wcode_main_first_part @ 
  1827    \<Longrightarrow> steps0 (Suc 0 + length (t_wcode_main_first_part @ 
  2082   shift t_fourtimes (length (t_wcode_main_first_part @ 
  1832   shift t_fourtimes (length (t_wcode_main_first_part @ 
  2083   shift t_twice (length t_wcode_main_first_part div 2) @ [(L, 1), (L, 1)]) div 2) @ ([(L, 1), (L, 1)])) stp 
  1833   shift t_twice (length t_wcode_main_first_part div 2) @ [(L, 1), (L, 1)]) div 2) @ ([(L, 1), (L, 1)])) stp 
  2084   = ((Suc t_fourtimes_len) + length (t_wcode_main_first_part @ 
  1834   = ((Suc t_fourtimes_len) + length (t_wcode_main_first_part @ 
  2085   shift t_twice (length t_wcode_main_first_part div 2) @ [(L, 1), (L, 1)]) div 2,
  1835   shift t_twice (length t_wcode_main_first_part div 2) @ [(L, 1), (L, 1)]) div 2,
  2086   Bk\<up>(ln) @ Bk # Bk # ires, Oc\<up>(Suc (4 * rs)) @ Bk\<up>(rn))"
  1836   Bk\<up>(ln) @ Bk # Bk # ires, Oc\<up>(Suc (4 * rs)) @ Bk\<up>(rn))"
  2087 apply(rule_tac tm_append_shift_append_steps, simp_all)
  1837   using length_t_twice_even
  2088 apply(auto simp: t_wcode_main_first_part_def)
  1838   by(intro tm_append_shift_append_steps, auto)
  2089 done
  1839 
  2090 
  1840 lemma split_26_even[simp]: "(26 + l::nat) div 2 = l div 2 + 13" by(simp)
  2091 
  1841 
  2092 lemma [simp]: "length t_wcode_main_first_part = 24"
  1842 lemma t_twice_len_plust_14[simp]: "t_twice_len + 14 =  14 + length (shift t_twice 12) div 2"
  2093 apply(simp add: t_wcode_main_first_part_def)
       
  2094 done
       
  2095 
       
  2096 lemma [simp]: "(26 + length t_twice) div 2 = (length t_twice) div 2 + 13"
       
  2097 apply(simp add: t_twice_def t_twice_def)
       
  2098 done
       
  2099 
       
  2100 lemma [simp]: "((26 + length (shift t_twice 12)) div 2)
       
  2101              = (length (shift t_twice 12) div 2 + 13)"
       
  2102 apply(simp add: t_twice_def)
       
  2103 done 
       
  2104 
       
  2105 lemma [simp]: "t_twice_len + 14 =  14 + length (shift t_twice 12) div 2"
       
  2106 apply(simp add: t_twice_def t_twice_len_def)
  1843 apply(simp add: t_twice_def t_twice_len_def)
  2107 done
  1844 done
  2108 
  1845 
  2109 lemma t_fourtimes_append:
  1846 lemma t_fourtimes_append:
  2110   "\<exists> stp ln rn. 
  1847   "\<exists> stp ln rn. 
  2123   apply(drule_tac t_fourtimes_append_pre)
  1860   apply(drule_tac t_fourtimes_append_pre)
  2124   apply(rule_tac x = stp in exI, rule_tac x = ln in exI, rule_tac x = rn in exI)
  1861   apply(rule_tac x = stp in exI, rule_tac x = ln in exI, rule_tac x = rn in exI)
  2125   apply(simp add: t_twice_len_def)
  1862   apply(simp add: t_twice_len_def)
  2126   done
  1863   done
  2127 
  1864 
  2128 lemma t_wcode_main_len: "length t_wcode_main = length t_twice + length t_fourtimes + 28"
       
  2129 apply(simp add: t_wcode_main_def)
       
  2130 done
       
  2131 
       
  2132 lemma even_twice_len: "length t_twice mod 2 = 0"
       
  2133 apply(auto simp: t_twice_def t_twice_compile_def)
       
  2134 by (metis mopup_mod2)
       
  2135 
       
  2136 lemma even_fourtimes_len: "length t_fourtimes mod 2 = 0"
  1865 lemma even_fourtimes_len: "length t_fourtimes mod 2 = 0"
  2137 apply(auto simp: t_fourtimes_def t_fourtimes_compile_def)
  1866 apply(auto simp: t_fourtimes_def t_fourtimes_compile_def)
  2138 by (metis mopup_mod2)
  1867 by (metis mopup_mod2)
  2139 
  1868 
  2140 lemma [simp]: "2 * (length t_twice div 2) = length t_twice"
  1869 lemma t_twice_even[simp]: "2 * (length t_twice div 2) = length t_twice"
  2141 using even_twice_len
  1870 using length_t_twice_even by arith
  2142 by arith
  1871 
  2143 
  1872 lemma t_fourtimes_even[simp]: "2 * (length t_fourtimes div 2) = length t_fourtimes"
  2144 lemma [simp]: "2 * (length t_fourtimes div 2) = length t_fourtimes"
       
  2145 using even_fourtimes_len
  1873 using even_fourtimes_len
  2146 by arith
  1874 by arith
  2147 
  1875 
  2148 lemma [simp]: "fetch t_wcode_main (14 + length t_twice div 2 + t_fourtimes_len) Oc
  1876 lemma fetch_t_wcode_14_Oc: "fetch t_wcode_main (14 + length t_twice div 2 + t_fourtimes_len) Oc
  2149              = (L, Suc 0)" 
  1877              = (L, Suc 0)" 
  2150 apply(subgoal_tac "14 = Suc 13")
  1878 apply(subgoal_tac "14 = Suc 13")
  2151 apply(simp only: fetch.simps add_Suc nth_of.simps t_wcode_main_def)
  1879 apply(simp only: fetch.simps add_Suc nth_of.simps t_wcode_main_def)
  2152 apply(simp add:length_append length_shift Parity.two_times_even_div_two even_twice_len t_fourtimes_len_def nth_append)
  1880 apply(simp add:length_t_twice_even t_fourtimes_len_def nth_append)
  2153 by arith
  1881 by arith
  2154 
  1882 
  2155 lemma [simp]: "fetch t_wcode_main (14 + length t_twice div 2 + t_fourtimes_len) Bk
  1883 lemma fetch_t_wcode_14_Bk: "fetch t_wcode_main (14 + length t_twice div 2 + t_fourtimes_len) Bk
  2156              = (L, Suc 0)"
  1884              = (L, Suc 0)"
  2157 apply(subgoal_tac "14 = Suc 13")
  1885 apply(subgoal_tac "14 = Suc 13")
  2158 apply(simp only: fetch.simps add_Suc nth_of.simps t_wcode_main_def)
  1886 apply(simp only: fetch.simps add_Suc nth_of.simps t_wcode_main_def)
  2159 apply(simp add:length_append length_shift Parity.two_times_even_div_two even_twice_len t_fourtimes_len_def nth_append)
  1887 apply(simp add:length_t_twice_even t_fourtimes_len_def nth_append)
  2160 by arith
  1888 by arith
  2161 
  1889 
  2162 lemma [simp]: "fetch t_wcode_main (14 + length t_twice div 2 + t_fourtimes_len) b
  1890 lemma fetch_t_wcode_14 [simp]: "fetch t_wcode_main (14 + length t_twice div 2 + t_fourtimes_len) b
  2163              = (L, Suc 0)"
  1891              = (L, Suc 0)"
  2164 apply(case_tac b, simp_all)
  1892 apply(case_tac b, simp_all add:fetch_t_wcode_14_Bk fetch_t_wcode_14_Oc)
  2165 done
  1893 done
  2166 
  1894 
  2167 lemma wcode_jump2: 
  1895 lemma wcode_jump2: 
  2168   "\<exists> stp ln rn. steps0 (t_twice_len + 14 + t_fourtimes_len
  1896   "\<exists> stp ln rn. steps0 (t_twice_len + 14 + t_fourtimes_len
  2169   , Bk # Bk # Bk\<up>(lnb) @ Oc # ires, Oc\<up>(Suc (4 * rs + 4)) @ Bk\<up>(rnb)) t_wcode_main stp =
  1897   , Bk # Bk # Bk\<up>(lnb) @ Oc # ires, Oc\<up>(Suc (4 * rs + 4)) @ Bk\<up>(rnb)) t_wcode_main stp =
  2222     (Suc 0, Bk # Bk\<up>(lnc) @ Oc # ires, Bk # Oc\<up>(Suc (4*rs + 4)) @ Bk\<up>(rnc))"
  1950     (Suc 0, Bk # Bk\<up>(lnc) @ Oc # ires, Bk # Oc\<up>(Suc (4*rs + 4)) @ Bk\<up>(rnc))"
  2223     by blast
  1951     by blast
  2224   from stp1 stp2 stp3 show "?thesis"
  1952   from stp1 stp2 stp3 show "?thesis"
  2225     apply(rule_tac x = "stpa + stpb + stpc" in exI,
  1953     apply(rule_tac x = "stpa + stpb + stpc" in exI,
  2226           rule_tac x = lnc in exI, rule_tac x = rnc in exI)
  1954           rule_tac x = lnc in exI, rule_tac x = rnc in exI)
  2227     apply(simp add: steps_add)
  1955     apply(simp)
  2228     done
  1956     done
  2229 qed
  1957 qed
  2230 
  1958 
  2231 fun wcode_on_left_moving_3_B :: "bin_inv_t"
  1959 fun wcode_on_left_moving_3_B :: "bin_inv_t"
  2232   where
  1960   where
  2307 lemmas wcode_halt_invs = 
  2035 lemmas wcode_halt_invs = 
  2308   wcode_on_left_moving_3_B.simps wcode_on_left_moving_3_O.simps
  2036   wcode_on_left_moving_3_B.simps wcode_on_left_moving_3_O.simps
  2309   wcode_on_checking_3.simps wcode_goon_checking_3.simps 
  2037   wcode_on_checking_3.simps wcode_goon_checking_3.simps 
  2310   wcode_on_left_moving_3.simps wcode_stop.simps
  2038   wcode_on_left_moving_3.simps wcode_stop.simps
  2311 
  2039 
  2312 lemma [simp]: "fetch t_wcode_main 7 Bk = (R, 0)"
  2040 
  2313 apply(subgoal_tac "7 = Suc 6")
  2041 lemma wcode_on_left_moving_3_mv_Bk[simp]: "wcode_on_left_moving_3 ires rs (b, Bk # list)
  2314 apply(simp only: fetch.simps t_wcode_main_def nth_append nth_of.simps
       
  2315                 t_wcode_main_first_part_def)
       
  2316 apply(auto)
       
  2317 done
       
  2318 
       
  2319 lemma [simp]: "wcode_on_left_moving_3 ires rs (b, [])  = False"
       
  2320 apply(simp only: wcode_halt_invs)
       
  2321 apply(simp)
       
  2322 done    
       
  2323 
       
  2324 lemma [simp]: "wcode_on_checking_3 ires rs (b, []) = False"
       
  2325 apply(simp add: wcode_halt_invs)
       
  2326 done
       
  2327               
       
  2328 lemma [simp]: "wcode_goon_checking_3 ires rs (b, []) = False"
       
  2329 apply(simp add: wcode_halt_invs)
       
  2330 done 
       
  2331 
       
  2332 lemma [simp]: "wcode_on_left_moving_3 ires rs (b, Bk # list)
       
  2333  \<Longrightarrow> wcode_on_left_moving_3 ires rs (tl b, hd b # Bk # list)"
  2042  \<Longrightarrow> wcode_on_left_moving_3 ires rs (tl b, hd b # Bk # list)"
  2334 apply(simp only: wcode_halt_invs)
  2043 apply(simp only: wcode_halt_invs)
  2335 apply(erule_tac disjE)
  2044 apply(erule_tac disjE)
  2336 apply(erule_tac exE)+
  2045 apply(erule_tac exE)+
  2337 apply(case_tac ml, simp)
  2046 apply(case_tac ml, simp)
  2342 apply(rule_tac x = nat in exI, rule_tac x = "Suc mr" in exI, 
  2051 apply(rule_tac x = nat in exI, rule_tac x = "Suc mr" in exI, 
  2343       rule_tac x = rn in exI, simp)
  2052       rule_tac x = rn in exI, simp)
  2344 apply(simp)
  2053 apply(simp)
  2345 done
  2054 done
  2346 
  2055 
  2347 lemma [simp]: "wcode_goon_checking_3 ires rs (b, Bk # list) \<Longrightarrow> 
  2056 lemma wcode_goon_checking_3_cases[simp]: "wcode_goon_checking_3 ires rs (b, Bk # list) \<Longrightarrow> 
  2348   (b = [] \<longrightarrow> wcode_stop ires rs ([Bk], list)) \<and>
  2057   (b = [] \<longrightarrow> wcode_stop ires rs ([Bk], list)) \<and>
  2349   (b \<noteq> [] \<longrightarrow> wcode_stop ires rs (Bk # b, list))"
  2058   (b \<noteq> [] \<longrightarrow> wcode_stop ires rs (Bk # b, list))"
  2350 apply(auto simp: wcode_halt_invs)
  2059 apply(auto simp: wcode_halt_invs)
  2351 done
  2060 done
  2352 
  2061 
  2353 lemma [simp]: "wcode_on_left_moving_3 ires rs (b, Oc # list) \<Longrightarrow> b \<noteq> []"
  2062 lemma wcode_on_checking_3_mv_Oc[simp]: "wcode_on_left_moving_3 ires rs (b, Oc # list) \<Longrightarrow> 
  2354 apply(auto simp: wcode_halt_invs)
       
  2355 done
       
  2356 
       
  2357 lemma [simp]: "wcode_on_left_moving_3 ires rs (b, Oc # list) \<Longrightarrow> 
       
  2358                wcode_on_checking_3 ires rs (tl b, hd b # Oc # list)"
  2063                wcode_on_checking_3 ires rs (tl b, hd b # Oc # list)"
  2359 apply(simp add:wcode_halt_invs, auto)
  2064 apply(simp add:wcode_halt_invs, auto)
  2360 apply(case_tac [!] mr, simp_all)
  2065 apply(case_tac [!] mr, simp_all)
  2361 done     
  2066 done     
  2362 
  2067 
  2363 lemma [simp]: "wcode_on_checking_3 ires rs (b, Oc # list) = False"
  2068 lemma wcode_3_nonempty[simp]:
  2364 apply(auto simp: wcode_halt_invs)
  2069   "wcode_on_left_moving_3 ires rs (b, []) = False"
  2365 done
  2070   "wcode_on_checking_3 ires rs (b, []) = False"
  2366 
  2071   "wcode_goon_checking_3 ires rs (b, []) = False"
  2367 lemma [simp]: "wcode_on_left_moving_3 ires rs (b, Bk # list) \<Longrightarrow> b \<noteq> []"
  2072   "wcode_on_left_moving_3 ires rs (b, Oc # list) \<Longrightarrow> b \<noteq> []"
  2368 apply(simp add: wcode_halt_invs, auto)
  2073   "wcode_on_checking_3 ires rs (b, Oc # list) = False"
  2369 done
  2074   "wcode_on_left_moving_3 ires rs (b, Bk # list) \<Longrightarrow> b \<noteq> []"
  2370 
  2075   "wcode_on_checking_3 ires rs (b, Bk # list) \<Longrightarrow> b \<noteq> []"
  2371 lemma [simp]: "wcode_on_checking_3 ires rs (b, Bk # list) \<Longrightarrow> b \<noteq> []"
  2076   "wcode_goon_checking_3 ires rs (b, Oc # list) = False"
  2372 apply(auto simp: wcode_halt_invs)
  2077   by(auto simp: wcode_halt_invs)
  2373 done
  2078 
  2374 
  2079 lemma wcode_goon_checking_3_mv_Bk[simp]: "wcode_on_checking_3 ires rs (b, Bk # list) \<Longrightarrow> 
  2375 lemma [simp]: "wcode_on_checking_3 ires rs (b, Bk # list) \<Longrightarrow> 
       
  2376   wcode_goon_checking_3 ires rs (tl b, hd b # Bk # list)"
  2080   wcode_goon_checking_3 ires rs (tl b, hd b # Bk # list)"
  2377 apply(auto simp: wcode_halt_invs)
  2081 apply(auto simp: wcode_halt_invs)
  2378 done
       
  2379 
       
  2380 lemma [simp]: "wcode_goon_checking_3 ires rs (b, Oc # list) = False"
       
  2381 apply(simp add: wcode_goon_checking_3.simps)
       
  2382 done
  2082 done
  2383 
  2083 
  2384 lemma t_halt_case_correctness: 
  2084 lemma t_halt_case_correctness: 
  2385 shows "let P = (\<lambda> (st, l, r). st = 0) in 
  2085 shows "let P = (\<lambda> (st, l, r). st = 0) in 
  2386        let Q = (\<lambda> (st, l, r). wcode_halt_case_inv st ires rs (l, r)) in 
  2086        let Q = (\<lambda> (st, l, r). wcode_halt_case_inv st ires rs (l, r)) in 
  2416     apply(auto)
  2116     apply(auto)
  2417     done
  2117     done
  2418 qed
  2118 qed
  2419 
  2119 
  2420 declare wcode_halt_case_inv.simps[simp del]
  2120 declare wcode_halt_case_inv.simps[simp del]
  2421 lemma [intro]: "\<exists> xs. (<rev list @ [aa::nat]> :: cell list) = Oc # xs"
  2121 lemma leading_Oc[intro]: "\<exists> xs. (<rev list @ [aa::nat]> :: cell list) = Oc # xs"
  2422 apply(case_tac "rev list", simp)
  2122 apply(case_tac "rev list", simp)
  2423 apply(simp add: tape_of_nl_cons)
  2123 apply(simp add: tape_of_nl_cons)
  2424 done
  2124 done
  2425 
  2125 
  2426 lemma wcode_halt_case:
  2126 lemma wcode_halt_case:
  2434 apply(auto simp: wcode_halt_case_inv.simps wcode_stop.simps)
  2134 apply(auto simp: wcode_halt_case_inv.simps wcode_stop.simps)
  2435 apply(rule_tac x = na in exI, rule_tac x = ln in exI, 
  2135 apply(rule_tac x = na in exI, rule_tac x = ln in exI, 
  2436       rule_tac x = rn in exI, simp)
  2136       rule_tac x = rn in exI, simp)
  2437 done
  2137 done
  2438 
  2138 
  2439 lemma bl_bin_one: "bl_bin [Oc] =  Suc 0"
  2139 lemma bl_bin_one[simp]: "bl_bin [Oc] = 1"
  2440 apply(simp add: bl_bin.simps)
  2140 apply(simp add: bl_bin.simps)
  2441 done
  2141 done
  2442 
  2142 
  2443 lemma [simp]: "bl_bin [Oc] = 1"
  2143 lemma twice_power[intro]: "2 * 2 ^ a = Suc (Suc (2 * bl_bin (Oc \<up> a)))"
  2444 apply(simp add: bl_bin.simps)
       
  2445 done
       
  2446 
       
  2447 lemma [intro]: "2 * 2 ^ a = Suc (Suc (2 * bl_bin (Oc \<up> a)))"
       
  2448 apply(induct a, auto simp: bl_bin.simps)
  2144 apply(induct a, auto simp: bl_bin.simps)
  2449 done
  2145 done
  2450 declare replicate_Suc[simp del]
  2146 declare replicate_Suc[simp del]
  2451 
  2147 
  2452 lemma t_wcode_main_lemma_pre:
  2148 lemma t_wcode_main_lemma_pre:
  2506           (Suc 0, Bk # Bk\<up>(lna) @ rev (<a>) @ Bk # Bk # ires, Bk # Oc\<up>(Suc (2 * rs + 2)) @ Bk\<up>(rna))" by blast
  2202           (Suc 0, Bk # Bk\<up>(lna) @ rev (<a>) @ Bk # Bk # ires, Bk # Oc\<up>(Suc (2 * rs + 2)) @ Bk\<up>(rna))" by blast
  2507         moreover have 
  2203         moreover have 
  2508           "\<exists>stp ln rn.
  2204           "\<exists>stp ln rn.
  2509           steps0 (Suc 0,  Bk # Bk\<up>(lna) @ rev (<a::nat>) @ Bk # Bk # ires, Bk # Oc\<up>(Suc (2 * rs + 2)) @ Bk\<up>(rna)) t_wcode_main stp =
  2205           steps0 (Suc 0,  Bk # Bk\<up>(lna) @ rev (<a::nat>) @ Bk # Bk # ires, Bk # Oc\<up>(Suc (2 * rs + 2)) @ Bk\<up>(rna)) t_wcode_main stp =
  2510           (0, Bk # ires, Bk # Oc # Bk\<up>(ln) @ Bk # Bk # Oc\<up>(bl_bin (<a>) + (2*rs + 2)  * 2 ^ a) @ Bk\<up>(rn))"
  2206           (0, Bk # ires, Bk # Oc # Bk\<up>(ln) @ Bk # Bk # Oc\<up>(bl_bin (<a>) + (2*rs + 2)  * 2 ^ a) @ Bk\<up>(rn))"
  2511           using ind2[of lna ires "2*rs + 2" rna] by(simp add: tape_of_nl_abv tape_of_nat_abv)   
  2207           using ind2[of lna ires "2*rs + 2" rna] by(simp add: tape_of_list_def tape_of_nat_def)   
  2512         from this obtain stpb lnb rnb where stp2:  
  2208         from this obtain stpb lnb rnb where stp2:  
  2513           "steps0 (Suc 0,  Bk # Bk\<up>(lna) @ rev (<a>) @ Bk # Bk # ires, Bk # Oc\<up>(Suc (2 * rs + 2)) @ Bk\<up>(rna)) t_wcode_main stpb =
  2209           "steps0 (Suc 0,  Bk # Bk\<up>(lna) @ rev (<a>) @ Bk # Bk # ires, Bk # Oc\<up>(Suc (2 * rs + 2)) @ Bk\<up>(rna)) t_wcode_main stpb =
  2514           (0, Bk # ires, Bk # Oc # Bk\<up>(lnb) @ Bk # Bk # Oc\<up>(bl_bin (<a>) + (2*rs + 2)  * 2 ^ a) @ Bk\<up>(rnb))"
  2210           (0, Bk # ires, Bk # Oc # Bk\<up>(lnb) @ Bk # Bk # Oc\<up>(bl_bin (<a>) + (2*rs + 2)  * 2 ^ a) @ Bk\<up>(rnb))"
  2515           by blast
  2211           by blast
  2516         from stp1 and stp2 show "?thesis"
  2212         from stp1 and stp2 show "?thesis"
  2517           apply(rule_tac x = "stpa + stpb" in exI,
  2213           apply(rule_tac x = "stpa + stpb" in exI,
  2518             rule_tac x = lnb in exI, rule_tac x = rnb in exI, simp add: tape_of_nat_abv)
  2214             rule_tac x = lnb in exI, rule_tac x = rnb in exI, simp add: tape_of_nat_def)
  2519           apply(simp add:  bl_bin.simps replicate_Suc)
  2215           apply(simp add:  bl_bin.simps replicate_Suc)
  2520           apply(auto)
  2216           apply(auto)
  2521           done
  2217           done
  2522       qed
  2218       qed
  2523     qed
  2219     qed
  2779       wprepare_step (st, l, r))"
  2475       wprepare_step (st, l, r))"
  2780 
  2476 
  2781 definition wcode_prepare_le :: "(config \<times> config) set"
  2477 definition wcode_prepare_le :: "(config \<times> config) set"
  2782   where "wcode_prepare_le \<equiv> (inv_image lex_triple wcode_prepare_measure)"
  2478   where "wcode_prepare_le \<equiv> (inv_image lex_triple wcode_prepare_measure)"
  2783 
  2479 
  2784 lemma [intro]: "wf lex_pair"
       
  2785 by(auto intro:wf_lex_prod simp:lex_pair_def)
       
  2786 
       
  2787 lemma wf_wcode_prepare_le[intro]: "wf wcode_prepare_le"
  2480 lemma wf_wcode_prepare_le[intro]: "wf wcode_prepare_le"
  2788 by(auto intro:wf_inv_image simp: wcode_prepare_le_def 
  2481 by(auto intro:wf_inv_image simp: wcode_prepare_le_def 
  2789            lex_triple_def)
  2482            lex_triple_def)
  2790 
  2483 
  2791 declare wprepare_add_one.simps[simp del] wprepare_goto_first_end.simps[simp del]
  2484 declare wprepare_add_one.simps[simp del] wprepare_goto_first_end.simps[simp del]
  2797         wprepare_erase.simps wprepare_goto_start_pos.simps
  2490         wprepare_erase.simps wprepare_goto_start_pos.simps
  2798         wprepare_loop_start.simps wprepare_loop_goon.simps
  2491         wprepare_loop_start.simps wprepare_loop_goon.simps
  2799         wprepare_add_one2.simps
  2492         wprepare_add_one2.simps
  2800 
  2493 
  2801 declare wprepare_inv.simps[simp del]
  2494 declare wprepare_inv.simps[simp del]
  2802 lemma [simp]: "fetch t_wcode_prepare (Suc 0) Bk = (W1, 2)"
  2495 
  2803 apply(simp add: fetch.simps t_wcode_prepare_def nth_of.simps)
  2496 lemma fetch_t_wcode_prepare[simp]:
  2804 done
  2497   "fetch t_wcode_prepare (Suc 0) Bk = (W1, 2)"
  2805 
  2498   "fetch t_wcode_prepare (Suc 0) Oc = (L, 1)"
  2806 lemma [simp]: "fetch t_wcode_prepare (Suc 0) Oc = (L, 1)"
  2499   "fetch t_wcode_prepare (Suc (Suc 0)) Bk = (L, 3)"
  2807 apply(simp add: fetch.simps t_wcode_prepare_def nth_of.simps)
  2500   "fetch t_wcode_prepare (Suc (Suc 0)) Oc = (R, 2)"
  2808 done
  2501   "fetch t_wcode_prepare (Suc (Suc (Suc 0))) Bk = (R, 4)"
  2809 
  2502   "fetch t_wcode_prepare (Suc (Suc (Suc 0))) Oc = (W0, 3)"
  2810 lemma [simp]: "fetch t_wcode_prepare (Suc (Suc 0)) Bk = (L, 3)"
  2503   "fetch t_wcode_prepare 4 Bk = (R, 4)"
  2811 apply(simp add: fetch.simps t_wcode_prepare_def nth_of.simps)
  2504   "fetch t_wcode_prepare 4 Oc = (R, 5)"
  2812 done
  2505   "fetch t_wcode_prepare 5 Oc = (R, 5)"
  2813 
  2506   "fetch t_wcode_prepare 5 Bk = (R, 6)"
  2814 lemma [simp]: "fetch t_wcode_prepare (Suc (Suc 0)) Oc = (R, 2)"
  2507   "fetch t_wcode_prepare 6 Oc = (R, 5)"
  2815 apply(simp add: fetch.simps t_wcode_prepare_def nth_of.simps)
  2508   "fetch t_wcode_prepare 6 Bk = (R, 7)"
  2816 done
  2509   "fetch t_wcode_prepare 7 Oc = (L, 0)"
  2817 
  2510   "fetch t_wcode_prepare 7 Bk = (W1, 7)"
  2818 lemma [simp]: "fetch t_wcode_prepare (Suc (Suc (Suc 0))) Bk = (R, 4)"
  2511   unfolding fetch.simps t_wcode_prepare_def nth_of.simps
  2819 apply(simp add: fetch.simps t_wcode_prepare_def nth_of.simps)
  2512   numeral by auto
  2820 done
  2513 
  2821 
  2514 lemma wprepare_add_one_nonempty_snd[simp]: "lm \<noteq> [] \<Longrightarrow> wprepare_add_one m lm (b, []) = False"
  2822 lemma [simp]: "fetch t_wcode_prepare (Suc (Suc (Suc 0))) Oc = (W0, 3)"
       
  2823 apply(simp add: fetch.simps t_wcode_prepare_def nth_of.simps)
       
  2824 done
       
  2825 
       
  2826 lemma [simp]: "fetch t_wcode_prepare 4 Bk = (R, 4)"
       
  2827 apply(subgoal_tac "4 = Suc 3")
       
  2828 apply(simp_all only: fetch.simps t_wcode_prepare_def nth_of.simps)
       
  2829 apply(auto)
       
  2830 done
       
  2831 
       
  2832 lemma [simp]: "fetch t_wcode_prepare 4 Oc = (R, 5)"
       
  2833 apply(subgoal_tac "4 = Suc 3")
       
  2834 apply(simp_all only: fetch.simps t_wcode_prepare_def nth_of.simps)
       
  2835 apply(auto)
       
  2836 done
       
  2837 
       
  2838 
       
  2839 lemma [simp]: "fetch t_wcode_prepare 5 Oc = (R, 5)"
       
  2840 apply(subgoal_tac "5 = Suc 4")
       
  2841 apply(simp_all only: fetch.simps t_wcode_prepare_def nth_of.simps)
       
  2842 apply(auto)
       
  2843 done
       
  2844 
       
  2845 lemma [simp]: "fetch t_wcode_prepare 5 Bk = (R, 6)"
       
  2846 apply(subgoal_tac "5 = Suc 4")
       
  2847 apply(simp_all only: fetch.simps t_wcode_prepare_def nth_of.simps)
       
  2848 apply(auto)
       
  2849 done
       
  2850 
       
  2851 lemma [simp]: "fetch t_wcode_prepare 6 Oc = (R, 5)"
       
  2852 apply(subgoal_tac "6 = Suc 5")
       
  2853 apply(simp_all only: fetch.simps t_wcode_prepare_def nth_of.simps)
       
  2854 apply(auto)
       
  2855 done
       
  2856 
       
  2857 lemma [simp]: "fetch t_wcode_prepare 6 Bk = (R, 7)"
       
  2858 apply(subgoal_tac "6 = Suc 5")
       
  2859 apply(simp_all only: fetch.simps t_wcode_prepare_def nth_of.simps)
       
  2860 apply(auto)
       
  2861 done
       
  2862 
       
  2863 lemma [simp]: "fetch t_wcode_prepare 7 Oc = (L, 0)"
       
  2864 apply(subgoal_tac "7 = Suc 6")
       
  2865 apply(simp_all only: fetch.simps t_wcode_prepare_def nth_of.simps)
       
  2866 apply(auto)
       
  2867 done
       
  2868 
       
  2869 lemma [simp]: "fetch t_wcode_prepare 7 Bk = (W1, 7)"
       
  2870 apply(subgoal_tac "7 = Suc 6")
       
  2871 apply(simp_all only: fetch.simps t_wcode_prepare_def nth_of.simps)
       
  2872 apply(auto)
       
  2873 done
       
  2874 
       
  2875 lemma [simp]: "lm \<noteq> [] \<Longrightarrow> wprepare_add_one m lm (b, []) = False"
       
  2876 apply(simp add: wprepare_invs)
  2515 apply(simp add: wprepare_invs)
  2877 done
  2516 done
  2878 
  2517 
  2879 lemma [simp]: "lm \<noteq> [] \<Longrightarrow> wprepare_goto_first_end m lm (b, []) = False"
  2518 lemma wprepare_goto_first_end_nonempty_snd[simp]: "lm \<noteq> [] \<Longrightarrow> wprepare_goto_first_end m lm (b, []) = False"
  2880 apply(simp add: wprepare_invs)
  2519 apply(simp add: wprepare_invs)
  2881 done
  2520 done
  2882 
  2521 
  2883 lemma [simp]: "lm \<noteq> [] \<Longrightarrow> wprepare_erase m lm (b, []) = False"
  2522 lemma wprepare_erase_nonempty_snd[simp]: "lm \<noteq> [] \<Longrightarrow> wprepare_erase m lm (b, []) = False"
  2884 apply(simp add: wprepare_invs)
  2523 apply(simp add: wprepare_invs)
  2885 done
  2524 done
  2886 
  2525 
  2887 lemma [simp]: "lm \<noteq> [] \<Longrightarrow> wprepare_goto_start_pos m lm (b, []) = False"
  2526 lemma wprepare_goto_start_pos_nonempty_snd[simp]: "lm \<noteq> [] \<Longrightarrow> wprepare_goto_start_pos m lm (b, []) = False"
  2888 apply(simp add: wprepare_invs)
  2527 apply(simp add: wprepare_invs)
  2889 done
  2528 done
  2890 
  2529 
  2891 lemma [simp]: "\<lbrakk>lm \<noteq> []; wprepare_loop_start m lm (b, [])\<rbrakk> \<Longrightarrow> b \<noteq> []"
  2530 lemma wprepare_loop_start_empty_nonempty_fst[simp]: "\<lbrakk>lm \<noteq> []; wprepare_loop_start m lm (b, [])\<rbrakk> \<Longrightarrow> b \<noteq> []"
  2892 apply(simp add: wprepare_invs)
  2531 apply(simp add: wprepare_invs)
  2893 done
  2532 done
  2894 
  2533 
  2895 lemma rev_eq: "rev xs = rev ys \<Longrightarrow> xs = ys"
  2534 lemma rev_eq: "rev xs = rev ys \<Longrightarrow> xs = ys" by auto
  2896 by auto
  2535 
  2897 
  2536 lemma wprepare_loop_goon_Bk_empty_snd[simp]: "\<lbrakk>lm \<noteq> []; wprepare_loop_start m lm (b, [])\<rbrakk> \<Longrightarrow> 
  2898 lemma [simp]: "\<lbrakk>lm \<noteq> []; wprepare_loop_start m lm (b, [])\<rbrakk> \<Longrightarrow> 
       
  2899                                   wprepare_loop_goon m lm (Bk # b, [])"
  2537                                   wprepare_loop_goon m lm (Bk # b, [])"
  2900 apply(simp only: wprepare_invs)
  2538 apply(simp only: wprepare_invs)
  2901 apply(erule_tac disjE)
  2539 apply(erule_tac disjE)
  2902 apply(rule_tac disjI2)
  2540 apply(rule_tac disjI2)
  2903 apply(simp add: wprepare_loop_start_on_rightmost.simps
  2541 apply(simp add: wprepare_loop_start_on_rightmost.simps
  2904                 wprepare_loop_goon_on_rightmost.simps, auto)
  2542                 wprepare_loop_goon_on_rightmost.simps, auto)
  2905 apply(rule_tac rev_eq, simp add: tape_of_nl_rev)
  2543 apply(rule_tac rev_eq, simp add: tape_of_nl_rev)
  2906 done
  2544 done
  2907 
  2545 
  2908 lemma [simp]: "\<lbrakk>lm \<noteq> []; wprepare_loop_goon m lm (b, [])\<rbrakk> \<Longrightarrow> b \<noteq> []"
  2546 lemma wprepare_loop_goon_nonempty_fst[simp]: "\<lbrakk>lm \<noteq> []; wprepare_loop_goon m lm (b, [])\<rbrakk> \<Longrightarrow> b \<noteq> []"
  2909 apply(simp only: wprepare_invs, auto)
  2547 apply(simp only: wprepare_invs, auto)
  2910 done
  2548 done
  2911 
  2549 
  2912 lemma [simp]:"\<lbrakk>lm \<noteq> []; wprepare_loop_goon m lm (b, [])\<rbrakk> \<Longrightarrow> 
  2550 lemma wprepare_add_one2_Bk_empty[simp]:"\<lbrakk>lm \<noteq> []; wprepare_loop_goon m lm (b, [])\<rbrakk> \<Longrightarrow> 
  2913   wprepare_add_one2 m lm (Bk # b, [])"
  2551   wprepare_add_one2 m lm (Bk # b, [])"
  2914 apply(simp only: wprepare_invs, auto split: if_splits)
  2552 apply(simp only: wprepare_invs, auto split: if_splits)
  2915 done
  2553 done
  2916 
  2554 
  2917 lemma [simp]: "wprepare_add_one2 m lm (b, []) \<Longrightarrow> b \<noteq> []"
  2555 lemma wprepare_add_one2_nonempty_fst[simp]: "wprepare_add_one2 m lm (b, []) \<Longrightarrow> b \<noteq> []"
  2918 apply(simp only: wprepare_invs, auto)
  2556 apply(simp only: wprepare_invs, auto)
  2919 done
  2557 done
  2920 
  2558 
  2921 lemma [simp]: "wprepare_add_one2 m lm (b, []) \<Longrightarrow> wprepare_add_one2 m lm (b, [Oc])"
  2559 lemma wprepare_add_one2_Oc[simp]: "wprepare_add_one2 m lm (b, []) \<Longrightarrow> wprepare_add_one2 m lm (b, [Oc])"
  2922 apply(simp only: wprepare_invs, auto)
  2560 apply(simp only: wprepare_invs, auto)
  2923 done
  2561 done
  2924 
  2562 
  2925 lemma [simp]: "Bk # list = <(m::nat) # lm> @ ys = False"
  2563 lemma Bk_not_tape_start[simp]: "(Bk # list = <(m::nat) # lm> @ ys) = False"
  2926 apply(case_tac lm, auto simp: tape_of_nl_cons replicate_Suc)
  2564 apply(case_tac lm, auto simp: tape_of_nl_cons replicate_Suc)
  2927 done
  2565 done
  2928 
  2566 
  2929 lemma [simp]: "\<lbrakk>lm \<noteq> []; wprepare_add_one m lm (b, Bk # list)\<rbrakk>
  2567 lemma wprepare_goto_first_end_cases[simp]:
       
  2568  "\<lbrakk>lm \<noteq> []; wprepare_add_one m lm (b, Bk # list)\<rbrakk>
  2930        \<Longrightarrow> (b = [] \<longrightarrow> wprepare_goto_first_end m lm ([], Oc # list)) \<and> 
  2569        \<Longrightarrow> (b = [] \<longrightarrow> wprepare_goto_first_end m lm ([], Oc # list)) \<and> 
  2931            (b \<noteq> [] \<longrightarrow> wprepare_goto_first_end m lm (b, Oc # list))"
  2570            (b \<noteq> [] \<longrightarrow> wprepare_goto_first_end m lm (b, Oc # list))"
  2932 apply(simp only: wprepare_invs)
  2571 apply(simp only: wprepare_invs)
  2933 apply(auto simp: tape_of_nl_cons split: if_splits)
  2572 apply(auto simp: tape_of_nl_cons split: if_splits)
  2934 apply(rule_tac x = 0 in exI, simp add: replicate_Suc)
  2573 apply(rule_tac x = 0 in exI, simp add: replicate_Suc)
  2935 apply(case_tac lm, simp, simp add: tape_of_nl_abv tape_of_nat_list.simps replicate_Suc)
  2574 apply(case_tac lm, simp, simp add: tape_of_list_def tape_of_nat_list.simps replicate_Suc)
  2936 done
  2575 done
  2937 
  2576 
  2938 lemma [simp]: "wprepare_goto_first_end m lm (b, Bk # list) \<Longrightarrow> b \<noteq> []"
  2577 lemma wprepare_goto_first_end_Bk_nonempty_fst[simp]:
       
  2578   "wprepare_goto_first_end m lm (b, Bk # list) \<Longrightarrow> b \<noteq> []"
  2939 apply(simp only: wprepare_invs , auto simp: replicate_Suc)
  2579 apply(simp only: wprepare_invs , auto simp: replicate_Suc)
  2940 done
  2580 done
  2941 
  2581 
  2942 declare replicate_Suc[simp]
  2582 declare replicate_Suc[simp]
  2943 
  2583 
  2944 lemma [simp]: "wprepare_goto_first_end m lm (b, Bk # list) \<Longrightarrow>
  2584 lemma wprepare_erase_elem_Bk_rest[simp]: "wprepare_goto_first_end m lm (b, Bk # list) \<Longrightarrow>
  2945                           wprepare_erase m lm (tl b, hd b # Bk # list)"
  2585                           wprepare_erase m lm (tl b, hd b # Bk # list)"
  2946 apply(simp only: wprepare_invs, auto)
  2586 apply(simp only: wprepare_invs, auto)
  2947 apply(case_tac mr, simp_all)
  2587 apply(case_tac mr, simp_all)
  2948 apply(case_tac mr, auto)
  2588 apply(case_tac mr, auto)
  2949 done
  2589 done
  2950 
  2590 
  2951 lemma [simp]: "wprepare_erase m lm (b, Bk # list) \<Longrightarrow> b \<noteq> []"
  2591 lemma wprepare_erase_Bk_nonempty_fst[simp]: "wprepare_erase m lm (b, Bk # list) \<Longrightarrow> b \<noteq> []"
  2952 apply(simp only: wprepare_invs, auto)
  2592 apply(simp only: wprepare_invs, auto)
  2953 done
  2593 done
  2954 
  2594 
  2955 lemma [simp]: "wprepare_erase m lm (b, Bk # list) \<Longrightarrow> 
  2595 lemma wprepare_goto_start_pos_Bk[simp]: "wprepare_erase m lm (b, Bk # list) \<Longrightarrow> 
  2956                            wprepare_goto_start_pos m lm (Bk # b, list)"
  2596                            wprepare_goto_start_pos m lm (Bk # b, list)"
  2957 apply(simp only: wprepare_invs, auto)
  2597 apply(simp only: wprepare_invs, auto)
  2958 done
  2598 done
  2959 
  2599 
  2960 lemma [simp]: "\<lbrakk>wprepare_add_one m lm (b, Bk # list)\<rbrakk> \<Longrightarrow> list \<noteq> []"
  2600 lemma wprepare_add_one_Bk_nonempty_snd[simp]: "\<lbrakk>wprepare_add_one m lm (b, Bk # list)\<rbrakk> \<Longrightarrow> list \<noteq> []"
  2961 apply(simp only: wprepare_invs)
  2601 apply(simp only: wprepare_invs)
  2962 apply(case_tac lm, simp_all add: tape_of_nl_abv 
  2602 apply(case_tac lm, simp_all add: tape_of_list_def tape_of_nat_def, auto)
  2963                          tape_of_nat_list.simps tape_of_nat_abv, auto)
       
  2964 done
  2603 done
  2965     
  2604     
  2966 lemma [simp]: "\<lbrakk>lm \<noteq> [];  wprepare_goto_first_end m lm (b, Bk # list)\<rbrakk> \<Longrightarrow> list \<noteq> []"
  2605 lemma wprepare_goto_first_end_nonempty_snd_tl[simp]:
       
  2606  "\<lbrakk>lm \<noteq> [];  wprepare_goto_first_end m lm (b, Bk # list)\<rbrakk> \<Longrightarrow> list \<noteq> []"
  2967 apply(simp only: wprepare_invs, auto)
  2607 apply(simp only: wprepare_invs, auto)
  2968 apply(case_tac mr, simp_all)
  2608 apply(case_tac mr, simp_all)
  2969 done
  2609   done
  2970      
  2610 
  2971 lemma [simp]: "\<lbrakk>lm \<noteq> [];  wprepare_goto_first_end m lm (b, Bk # list)\<rbrakk> \<Longrightarrow> b \<noteq> []"
  2611 lemma wprepare_erase_Bk_nonempty_list[simp]: "\<lbrakk>lm \<noteq> []; wprepare_erase m lm (b, Bk # list)\<rbrakk> \<Longrightarrow> list \<noteq> []"
  2972 apply(simp only: wprepare_invs, auto)
  2612 apply(simp only: wprepare_invs, auto)
  2973 done
  2613 done
  2974 
  2614 
  2975 lemma [simp]: "\<lbrakk>lm \<noteq> []; wprepare_erase m lm (b, Bk # list)\<rbrakk> \<Longrightarrow> list \<noteq> []"
  2615 
  2976 apply(simp only: wprepare_invs, auto)
  2616 lemma wprepare_goto_start_pos_Bk_nonempty[simp]: "\<lbrakk>lm \<noteq> [];  wprepare_goto_start_pos m lm (b, Bk # list)\<rbrakk> \<Longrightarrow> list \<noteq> []"
  2977 done
  2617   by(cases lm;cases list;simp only: wprepare_invs, auto)
  2978 
  2618 
  2979 lemma [simp]: "\<lbrakk>lm \<noteq> []; wprepare_erase m lm (b, Bk # list)\<rbrakk> \<Longrightarrow> b \<noteq> []"
  2619 lemma wprepare_goto_start_pos_Bk_nonempty_fst[simp]: "\<lbrakk>lm \<noteq> [];  wprepare_goto_start_pos m lm (b, Bk # list)\<rbrakk> \<Longrightarrow> b \<noteq> []"
  2980 apply(simp only: wprepare_invs, auto)
       
  2981 done
       
  2982 
       
  2983 lemma [simp]: "\<lbrakk>lm \<noteq> [];  wprepare_goto_start_pos m lm (b, Bk # list)\<rbrakk> \<Longrightarrow> list \<noteq> []"
       
  2984 apply(simp only: wprepare_invs, auto)
       
  2985 apply(case_tac lm, simp, case_tac list)
       
  2986 apply(simp_all add: tape_of_nl_abv tape_of_nat_list.simps tape_of_nat_abv)
       
  2987 done
       
  2988 
       
  2989 lemma [simp]: "\<lbrakk>lm \<noteq> [];  wprepare_goto_start_pos m lm (b, Bk # list)\<rbrakk> \<Longrightarrow> b \<noteq> []"
       
  2990 apply(simp only: wprepare_invs)
  2620 apply(simp only: wprepare_invs)
  2991 apply(auto)
  2621 apply(auto)
  2992 done
  2622 done
  2993 
  2623 
  2994 lemma [simp]: "\<lbrakk>lm \<noteq> []; wprepare_loop_goon m lm (b, Bk # list)\<rbrakk> \<Longrightarrow> b \<noteq> []"
  2624 lemma wprepare_loop_goon_Bk_nonempty[simp]: "\<lbrakk>lm \<noteq> []; wprepare_loop_goon m lm (b, Bk # list)\<rbrakk> \<Longrightarrow> b \<noteq> []"
  2995 apply(simp only: wprepare_invs, auto)
  2625 apply(simp only: wprepare_invs, auto)
  2996 done
  2626 done
  2997 
  2627 
  2998 lemma [simp]: "\<lbrakk>lm \<noteq> []; wprepare_loop_goon m lm (b, Bk # list)\<rbrakk> \<Longrightarrow> 
  2628 lemma wprepare_loop_goon_wprepare_add_one2_cases[simp]: "\<lbrakk>lm \<noteq> []; wprepare_loop_goon m lm (b, Bk # list)\<rbrakk> \<Longrightarrow> 
  2999   (list = [] \<longrightarrow> wprepare_add_one2 m lm (Bk # b, [])) \<and> 
  2629   (list = [] \<longrightarrow> wprepare_add_one2 m lm (Bk # b, [])) \<and> 
  3000   (list \<noteq> [] \<longrightarrow> wprepare_add_one2 m lm (Bk # b, list))"
  2630   (list \<noteq> [] \<longrightarrow> wprepare_add_one2 m lm (Bk # b, list))"
  3001 apply(simp only: wprepare_invs, simp)
  2631   unfolding wprepare_invs
  3002 apply(case_tac list, simp_all split: if_splits, auto)
  2632 apply(cases list;auto split:nat.split if_splits)
  3003 apply(case_tac [1-3] mr, simp_all add: )
  2633 apply(case_tac [1-3] mr, simp_all add: )
  3004 apply(case_tac mr, simp_all)
  2634 apply(case_tac mr, simp_all)
  3005 apply(case_tac [1-2] mr, simp_all add: )
  2635 apply(case_tac [1-2] mr, simp_all add: )
  3006 apply(case_tac rn, simp, case_tac nat, auto simp: )
  2636 apply(case_tac rn, force, case_tac nat, auto simp: )
  3007 done
  2637 done
  3008 
  2638 
  3009 lemma [simp]: "wprepare_add_one2 m lm (b, Bk # list) \<Longrightarrow> b \<noteq> []"
  2639 lemma wprepare_add_one2_nonempty[simp]: "wprepare_add_one2 m lm (b, Bk # list) \<Longrightarrow> b \<noteq> []"
  3010 apply(simp only: wprepare_invs, simp)
  2640 apply(simp only: wprepare_invs, simp)
  3011 done
  2641 done
  3012 
  2642 
  3013 lemma [simp]: "wprepare_add_one2 m lm (b, Bk # list) \<Longrightarrow> 
  2643 lemma wprepare_add_one2_cases[simp]: "wprepare_add_one2 m lm (b, Bk # list) \<Longrightarrow> 
  3014       (list = [] \<longrightarrow> wprepare_add_one2 m lm (b, [Oc])) \<and> 
  2644       (list = [] \<longrightarrow> wprepare_add_one2 m lm (b, [Oc])) \<and> 
  3015       (list \<noteq> [] \<longrightarrow> wprepare_add_one2 m lm (b, Oc # list))"
  2645       (list \<noteq> [] \<longrightarrow> wprepare_add_one2 m lm (b, Oc # list))"
  3016 apply(simp only:  wprepare_invs, auto)
  2646 apply(simp only:  wprepare_invs, auto)
  3017 done
  2647 done
  3018 
  2648 
  3019 lemma [simp]: "wprepare_goto_first_end m lm (b, Oc # list)
  2649 lemma wprepare_goto_first_end_cases_Oc[simp]: "wprepare_goto_first_end m lm (b, Oc # list)
  3020        \<Longrightarrow> (b = [] \<longrightarrow> wprepare_goto_first_end m lm ([Oc], list)) \<and> 
  2650        \<Longrightarrow> (b = [] \<longrightarrow> wprepare_goto_first_end m lm ([Oc], list)) \<and> 
  3021            (b \<noteq> [] \<longrightarrow> wprepare_goto_first_end m lm (Oc # b, list))"
  2651            (b \<noteq> [] \<longrightarrow> wprepare_goto_first_end m lm (Oc # b, list))"
  3022 apply(simp only:  wprepare_invs, auto)
  2652 apply(simp only:  wprepare_invs, auto)
  3023 apply(rule_tac x = 1 in exI, auto)
  2653 apply(rule_tac x = 1 in exI, auto)
  3024 apply(case_tac mr, simp_all add: )
  2654 apply(case_tac mr, simp_all add: )
  3025 apply(case_tac ml, simp_all add: )
  2655 apply(case_tac ml, simp_all add: )
  3026 apply(rule_tac x = "Suc ml" in exI, simp_all add: )
  2656 apply(rule_tac x = "Suc ml" in exI, simp_all add: )
  3027 apply(rule_tac x = "mr - 1" in exI, simp)
  2657 apply(rule_tac x = "mr - 1" in exI, simp)
  3028 done
  2658 done
  3029 
  2659 
  3030 lemma [simp]: "wprepare_erase m lm (b, Oc # list) \<Longrightarrow> b \<noteq> []"
  2660 lemma wprepare_erase_nonempty[simp]: "wprepare_erase m lm (b, Oc # list) \<Longrightarrow> b \<noteq> []"
  3031 apply(simp only: wprepare_invs, auto simp: )
  2661 apply(simp only: wprepare_invs, auto simp: )
  3032 done
  2662 done
  3033 
  2663 
  3034 lemma [simp]: "wprepare_erase m lm (b, Oc # list)
  2664 lemma wprepare_erase_Bk[simp]: "wprepare_erase m lm (b, Oc # list)
  3035   \<Longrightarrow> wprepare_erase m lm (b, Bk # list)"
  2665   \<Longrightarrow> wprepare_erase m lm (b, Bk # list)"
  3036 apply(simp  only:wprepare_invs, auto simp: )
  2666 apply(simp  only:wprepare_invs, auto simp: )
  3037 done
  2667 done
  3038 
  2668 
  3039 lemma [simp]: "\<lbrakk>lm \<noteq> []; wprepare_goto_start_pos m lm (b, Bk # list)\<rbrakk>
  2669 lemma wprepare_goto_start_pos_Bk_move[simp]: "\<lbrakk>lm \<noteq> []; wprepare_goto_start_pos m lm (b, Bk # list)\<rbrakk>
  3040        \<Longrightarrow> wprepare_goto_start_pos m lm (Bk # b, list)"
  2670        \<Longrightarrow> wprepare_goto_start_pos m lm (Bk # b, list)"
  3041 apply(simp only:wprepare_invs, auto)
  2671 apply(simp only:wprepare_invs, auto)
  3042 apply(case_tac [!] lm, simp, simp_all)
  2672 apply(case_tac [!] lm, simp, simp_all)
  3043 done
  2673 done
  3044 
  2674 
  3045 lemma [simp]: "wprepare_loop_start m lm (b, aa) \<Longrightarrow> b \<noteq> []"
  2675 lemma wprepare_loop_start_b_nonempty[simp]: "wprepare_loop_start m lm (b, aa) \<Longrightarrow> b \<noteq> []"
  3046 apply(simp only:wprepare_invs, auto)
  2676 apply(simp only:wprepare_invs, auto)
  3047 done
  2677 done
  3048 lemma [elim]: "Bk # list = Oc\<up>(mr) @ Bk\<up>(rn)  \<Longrightarrow> \<exists>rn. list = Bk\<up>(rn)"
  2678 lemma exists_exp_of_Bk[elim]: "Bk # list = Oc\<up>(mr) @ Bk\<up>(rn)  \<Longrightarrow> \<exists>rn. list = Bk\<up>(rn)"
  3049 apply(case_tac mr, simp_all)
  2679 apply(case_tac mr, simp_all)
  3050 apply(case_tac rn, simp_all)
  2680 apply(case_tac rn, simp_all)
  3051 done
  2681 done
  3052 
  2682 
  3053 lemma rev_equal_iff: "x = y \<Longrightarrow> rev x = rev y"
  2683 lemma wprepare_loop_start_in_middle_Bk_False[simp]: "wprepare_loop_start_in_middle m lm (b, [Bk]) = False"
  3054 by simp
       
  3055 
       
  3056 lemma tape_of_nl_false1:
       
  3057   "lm \<noteq> [] \<Longrightarrow> rev b @ [Bk] \<noteq> Bk\<up>(ln) @ Oc # Oc\<up>(m) @ Bk # Bk # <lm::nat list>"
       
  3058 apply(auto)
       
  3059 apply(drule_tac rev_equal_iff, simp add: tape_of_nl_rev)
       
  3060 apply(case_tac "rev lm")
       
  3061 apply(case_tac [2] list, auto simp: tape_of_nl_abv tape_of_nat_list.simps tape_of_nat_abv)
       
  3062 done
       
  3063 
       
  3064 lemma [simp]: "wprepare_loop_start_in_middle m lm (b, [Bk]) = False"
       
  3065 apply(simp add: wprepare_loop_start_in_middle.simps, auto)
  2684 apply(simp add: wprepare_loop_start_in_middle.simps, auto)
  3066 apply(case_tac mr, simp_all add: )
  2685 apply(case_tac mr, simp_all add: )
  3067 done
  2686 done
  3068 
  2687 
  3069 declare wprepare_loop_start_in_middle.simps[simp del]
  2688 declare wprepare_loop_start_in_middle.simps[simp del]
  3070 
  2689 
  3071 declare wprepare_loop_start_on_rightmost.simps[simp del] 
  2690 declare wprepare_loop_start_on_rightmost.simps[simp del] 
  3072         wprepare_loop_goon_in_middle.simps[simp del]
  2691         wprepare_loop_goon_in_middle.simps[simp del]
  3073         wprepare_loop_goon_on_rightmost.simps[simp del]
  2692         wprepare_loop_goon_on_rightmost.simps[simp del]
  3074 
  2693 
  3075 lemma [simp]: "wprepare_loop_goon_in_middle m lm (Bk # b, []) = False"
  2694 lemma wprepare_loop_goon_in_middle_Bk_False[simp]: "wprepare_loop_goon_in_middle m lm (Bk # b, []) = False"
  3076 apply(simp add: wprepare_loop_goon_in_middle.simps, auto)
  2695 apply(simp add: wprepare_loop_goon_in_middle.simps, auto)
  3077 done
  2696 done
  3078 
  2697 
  3079 lemma [simp]: "\<lbrakk>lm \<noteq> []; wprepare_loop_start m lm (b, [Bk])\<rbrakk> \<Longrightarrow>
  2698 lemma wprepare_loop_goon_Bk[simp]: "\<lbrakk>lm \<noteq> []; wprepare_loop_start m lm (b, [Bk])\<rbrakk> \<Longrightarrow>
  3080   wprepare_loop_goon m lm (Bk # b, [])"
  2699   wprepare_loop_goon m lm (Bk # b, [])"
  3081 apply(simp only: wprepare_invs, simp)
  2700 apply(simp only: wprepare_invs, simp)
  3082 apply(simp add: wprepare_loop_goon_on_rightmost.simps 
  2701 apply(simp add: wprepare_loop_goon_on_rightmost.simps 
  3083   wprepare_loop_start_on_rightmost.simps, auto)
  2702   wprepare_loop_start_on_rightmost.simps, auto)
  3084 apply(case_tac mr, simp_all add: )
  2703 apply(case_tac mr, simp_all add: )
  3085 apply(rule_tac rev_eq)
  2704 apply(rule_tac rev_eq)
  3086 apply(simp add: tape_of_nl_rev)
  2705 apply(simp add: tape_of_nl_rev)
  3087 apply(simp add: exp_ind replicate_Suc[THEN sym] del: replicate_Suc)
  2706 apply(simp add: exp_ind replicate_Suc[THEN sym] del: replicate_Suc)
  3088 done
  2707 done
  3089 
  2708 
  3090 lemma [simp]: "wprepare_loop_start_on_rightmost m lm (b, Bk # a # lista)
  2709 lemma wprepare_loop_goon_in_middle_Bk_False2[simp]: "wprepare_loop_start_on_rightmost m lm (b, Bk # a # lista)
  3091  \<Longrightarrow> wprepare_loop_goon_in_middle m lm (Bk # b, a # lista) = False"
  2710  \<Longrightarrow> wprepare_loop_goon_in_middle m lm (Bk # b, a # lista) = False"
  3092 apply(auto simp: wprepare_loop_start_on_rightmost.simps
  2711 apply(auto simp: wprepare_loop_start_on_rightmost.simps
  3093                  wprepare_loop_goon_in_middle.simps)
  2712                  wprepare_loop_goon_in_middle.simps)
  3094 done
  2713 done
  3095 
  2714 
  3096 lemma [simp]: "\<lbrakk>lm \<noteq> []; wprepare_loop_start_on_rightmost m lm (b, Bk # a # lista)\<rbrakk>
  2715 lemma wprepare_loop_goon_on_rightbmost_Bk_False[simp]: "\<lbrakk>lm \<noteq> []; wprepare_loop_start_on_rightmost m lm (b, Bk # a # lista)\<rbrakk>
  3097     \<Longrightarrow> wprepare_loop_goon_on_rightmost m lm (Bk # b, a # lista)"
  2716     \<Longrightarrow> wprepare_loop_goon_on_rightmost m lm (Bk # b, a # lista)"
  3098 apply(simp only: wprepare_loop_start_on_rightmost.simps
  2717 apply(simp only: wprepare_loop_start_on_rightmost.simps
  3099                  wprepare_loop_goon_on_rightmost.simps, auto)
  2718                  wprepare_loop_goon_on_rightmost.simps, auto)
  3100 apply(case_tac mr, simp_all add: )
  2719 apply(case_tac mr, simp_all add: )
  3101 apply(simp add: tape_of_nl_rev)
  2720 apply(simp add: tape_of_nl_rev)
  3102 apply(simp add: replicate_Suc[THEN sym] exp_ind del: replicate_Suc)
  2721 apply(simp add: replicate_Suc[THEN sym] exp_ind del: replicate_Suc)
  3103 done
  2722 done
  3104 
  2723 
  3105 lemma [simp]: "\<lbrakk>lm \<noteq> []; wprepare_loop_start_in_middle m lm (b, Bk # a # lista)\<rbrakk>
  2724 lemma wprepare_loop_goon_on_rightbmost_Bk_False_2[simp]: "\<lbrakk>lm \<noteq> []; wprepare_loop_start_in_middle m lm (b, Bk # a # lista)\<rbrakk>
  3106   \<Longrightarrow> wprepare_loop_goon_on_rightmost m lm (Bk # b, a # lista) = False"
  2725   \<Longrightarrow> wprepare_loop_goon_on_rightmost m lm (Bk # b, a # lista) = False"
  3107 apply(simp add: wprepare_loop_start_in_middle.simps
  2726 apply(simp add: wprepare_loop_start_in_middle.simps
  3108                 wprepare_loop_goon_on_rightmost.simps, auto)
  2727                 wprepare_loop_goon_on_rightmost.simps, auto)
  3109 apply(case_tac mr, simp_all add: )
  2728 apply(case_tac mr, simp_all add: )
  3110 apply(case_tac  "lm1::nat list", simp_all, case_tac  list, simp)
  2729 apply(case_tac  "lm1::nat list", simp_all, case_tac  list, simp)
  3111 apply(simp add: tape_of_nl_abv tape_of_nat_list.simps tape_of_nat_abv )
  2730 apply(simp add: tape_of_list_def tape_of_nat_list.simps tape_of_nat_def )
  3112 apply(case_tac [!] rna, simp_all add: )
  2731 apply(case_tac [!] rna, simp_all add: )
  3113 apply(case_tac mr, simp_all add: )
  2732 apply(case_tac mr, simp_all add: )
  3114 apply(case_tac lm1, simp, case_tac list, simp)
  2733 apply(case_tac lm1, simp, case_tac list, simp)
  3115 apply(simp_all add: tape_of_nl_abv tape_of_nat_list.simps  tape_of_nat_abv)
  2734 apply(simp_all add: tape_of_list_def tape_of_nat_list.simps  tape_of_nat_def)
  3116 done
  2735 done
  3117 
  2736 
  3118 lemma [simp]: 
  2737 lemma wprepare_loop_goon_in_middle_Bk_False3[simp]: 
  3119   "\<lbrakk>lm \<noteq> []; wprepare_loop_start_in_middle m lm (b, Bk # a # lista)\<rbrakk> 
  2738   "\<lbrakk>lm \<noteq> []; wprepare_loop_start_in_middle m lm (b, Bk # a # lista)\<rbrakk> 
  3120   \<Longrightarrow> wprepare_loop_goon_in_middle m lm (Bk # b, a # lista)"
  2739   \<Longrightarrow> wprepare_loop_goon_in_middle m lm (Bk # b, a # lista)"
  3121 apply(simp add: wprepare_loop_start_in_middle.simps
  2740 apply(simp add: wprepare_loop_start_in_middle.simps
  3122                wprepare_loop_goon_in_middle.simps, auto)
  2741                wprepare_loop_goon_in_middle.simps, auto)
  3123 apply(rule_tac x = rn in exI, simp)
  2742 apply(rule_tac x = rn in exI, simp)
  3124 apply(case_tac mr, simp_all add: )
  2743 apply(case_tac mr, simp_all add: )
  3125 apply(case_tac lm1, simp)
  2744 apply(case_tac lm1, simp)
  3126 apply(rule_tac x = "Suc aa" in exI, simp)
  2745 apply(rule_tac x = "Suc aa" in exI, simp)
  3127 apply(rule_tac x = list in exI)
  2746 apply(rule_tac x = list in exI)
  3128 apply(case_tac list, simp_all add: tape_of_nl_abv tape_of_nat_list.simps tape_of_nat_abv)
  2747 apply(case_tac list, simp_all add: tape_of_list_def  tape_of_nat_def)
  3129 done
  2748 done
  3130 
  2749 
  3131 lemma [simp]: "\<lbrakk>lm \<noteq> []; wprepare_loop_start m lm (b, Bk # a # lista)\<rbrakk> \<Longrightarrow> 
  2750 lemma wprepare_loop_goon_Bk2[simp]: "\<lbrakk>lm \<noteq> []; wprepare_loop_start m lm (b, Bk # a # lista)\<rbrakk> \<Longrightarrow> 
  3132   wprepare_loop_goon m lm (Bk # b, a # lista)"
  2751   wprepare_loop_goon m lm (Bk # b, a # lista)"
  3133 apply(simp add: wprepare_loop_start.simps 
  2752 apply(simp add: wprepare_loop_start.simps 
  3134                 wprepare_loop_goon.simps)
  2753                 wprepare_loop_goon.simps)
  3135 apply(erule_tac disjE, simp, auto)
  2754 apply(erule_tac disjE, simp, auto)
  3136 done
  2755 done
  3146   \<Longrightarrow> (hd b = Oc \<longrightarrow> (b = [] \<longrightarrow> wprepare_add_one m lm ([], Bk # Oc # list)) \<and>
  2765   \<Longrightarrow> (hd b = Oc \<longrightarrow> (b = [] \<longrightarrow> wprepare_add_one m lm ([], Bk # Oc # list)) \<and>
  3147                      (b \<noteq> [] \<longrightarrow> wprepare_add_one m lm (tl b, Oc # Oc # list))) \<and>
  2766                      (b \<noteq> [] \<longrightarrow> wprepare_add_one m lm (tl b, Oc # Oc # list))) \<and>
  3148   (hd b \<noteq> Oc \<longrightarrow> (b = [] \<longrightarrow> wprepare_add_one m lm ([], Bk # Oc # list)) \<and>
  2767   (hd b \<noteq> Oc \<longrightarrow> (b = [] \<longrightarrow> wprepare_add_one m lm ([], Bk # Oc # list)) \<and>
  3149                  (b \<noteq> [] \<longrightarrow> wprepare_add_one m lm (tl b, hd b # Oc # list)))"
  2768                  (b \<noteq> [] \<longrightarrow> wprepare_add_one m lm (tl b, hd b # Oc # list)))"
  3150 apply(simp only: wprepare_add_one.simps, auto)
  2769 apply(simp only: wprepare_add_one.simps, auto)
  3151 done
  2770   done
  3152 
  2771 
  3153 lemma [simp]: "wprepare_loop_start m lm (b, Oc # list) \<Longrightarrow> b \<noteq> []"
  2772 lemma wprepare_loop_start_on_rightmost_Oc[simp]: "wprepare_loop_start_on_rightmost m lm (b, Oc # list) \<Longrightarrow> 
  3154 apply(simp)
       
  3155 done
       
  3156 
       
  3157 lemma [simp]: "wprepare_loop_start_on_rightmost m lm (b, Oc # list) \<Longrightarrow> 
       
  3158   wprepare_loop_start_on_rightmost m lm (Oc # b, list)"
  2773   wprepare_loop_start_on_rightmost m lm (Oc # b, list)"
  3159 apply(simp add: wprepare_loop_start_on_rightmost.simps, auto)
  2774 apply(simp add: wprepare_loop_start_on_rightmost.simps, auto)
  3160 apply(rule_tac x = rn in exI, auto)
  2775 apply(rule_tac x = rn in exI, auto)
  3161 apply(case_tac mr, simp_all add: )
  2776 apply(case_tac mr, simp_all add: )
  3162 done
  2777 done
  3163 
  2778 
  3164 lemma [simp]: "wprepare_loop_start_in_middle m lm (b, Oc # list) \<Longrightarrow> 
  2779 lemma wprepare_loop_start_in_middle_Oc[simp]: "wprepare_loop_start_in_middle m lm (b, Oc # list) \<Longrightarrow> 
  3165                 wprepare_loop_start_in_middle m lm (Oc # b, list)"
  2780                 wprepare_loop_start_in_middle m lm (Oc # b, list)"
  3166 apply(simp add: wprepare_loop_start_in_middle.simps, auto)
  2781 apply(simp add: wprepare_loop_start_in_middle.simps, auto)
  3167 apply(rule_tac x = rn in exI, auto)
  2782 apply(rule_tac x = rn in exI, auto)
  3168 apply(case_tac mr, simp, simp add: )
  2783 apply(case_tac mr, simp, simp add: )
  3169 apply(rule_tac x = nat in exI, simp)
  2784 apply(rule_tac x = nat in exI, simp)
  3174        wprepare_loop_start m lm (Oc # b, list)"
  2789        wprepare_loop_start m lm (Oc # b, list)"
  3175 apply(simp add: wprepare_loop_start.simps)
  2790 apply(simp add: wprepare_loop_start.simps)
  3176 apply(erule_tac disjE, simp_all )
  2791 apply(erule_tac disjE, simp_all )
  3177 done
  2792 done
  3178 
  2793 
  3179 lemma [simp]: "wprepare_loop_goon m lm (b, Oc # list) \<Longrightarrow> b \<noteq> []"
  2794 lemma wprepare_loop_goon_Oc_nonempty[simp]: "wprepare_loop_goon m lm (b, Oc # list) \<Longrightarrow> b \<noteq> []"
  3180 apply(simp add: wprepare_loop_goon.simps     
  2795 apply(simp add: wprepare_loop_goon.simps     
  3181                 wprepare_loop_goon_in_middle.simps 
  2796                 wprepare_loop_goon_in_middle.simps 
  3182                 wprepare_loop_goon_on_rightmost.simps)
  2797                 wprepare_loop_goon_on_rightmost.simps)
  3183 apply(auto)
  2798 apply(auto)
  3184 done
  2799 done
  3185 
  2800 
  3186 lemma [simp]: "wprepare_goto_start_pos m lm (b, Oc # list) \<Longrightarrow> b \<noteq> []"
  2801 lemma wprepare_goto_start_pos_Oc_nonempty[simp]: "wprepare_goto_start_pos m lm (b, Oc # list) \<Longrightarrow> b \<noteq> []"
  3187 apply(simp add: wprepare_goto_start_pos.simps)
  2802 apply(simp add: wprepare_goto_start_pos.simps)
  3188 done
  2803 done
  3189 
  2804 
  3190 lemma [simp]: "wprepare_loop_goon_on_rightmost m lm (b, Oc # list) = False"
  2805 lemma wprepare_loop_goon_on_rightmost_Oc_False[simp]: "wprepare_loop_goon_on_rightmost m lm (b, Oc # list) = False"
  3191 apply(simp add: wprepare_loop_goon_on_rightmost.simps)
  2806 apply(simp add: wprepare_loop_goon_on_rightmost.simps)
  3192 done
  2807 done
  3193 
  2808 
  3194 lemma wprepare_loop1: "\<lbrakk>rev b @ Oc\<up>(mr) =  Oc\<up>(Suc m) @ Bk # Bk # <lm>; 
  2809 lemma wprepare_loop1: "\<lbrakk>rev b @ Oc\<up>(mr) =  Oc\<up>(Suc m) @ Bk # Bk # <lm>; 
  3195          b \<noteq> []; 0 < mr; Oc # list = Oc\<up>(mr) @ Bk\<up>(rn)\<rbrakk>
  2810          b \<noteq> []; 0 < mr; Oc # list = Oc\<up>(mr) @ Bk\<up>(rn)\<rbrakk>
  3207 apply(case_tac mr, simp_all add: )
  2822 apply(case_tac mr, simp_all add: )
  3208 apply(rule_tac x = nat in exI, simp)
  2823 apply(rule_tac x = nat in exI, simp)
  3209 apply(rule_tac x = "a#lista" in exI, simp)
  2824 apply(rule_tac x = "a#lista" in exI, simp)
  3210 done
  2825 done
  3211 
  2826 
  3212 lemma [simp]: "wprepare_loop_goon_in_middle m lm (b, Oc # list) \<Longrightarrow>
  2827 lemma wprepare_loop_goon_in_middle_cases[simp]: "wprepare_loop_goon_in_middle m lm (b, Oc # list) \<Longrightarrow>
  3213                 wprepare_loop_start_on_rightmost m lm (Oc # b, list) \<or>
  2828                 wprepare_loop_start_on_rightmost m lm (Oc # b, list) \<or>
  3214                 wprepare_loop_start_in_middle m lm (Oc # b, list)"
  2829                 wprepare_loop_start_in_middle m lm (Oc # b, list)"
  3215 apply(simp add: wprepare_loop_goon_in_middle.simps split: if_splits)
  2830 apply(simp add: wprepare_loop_goon_in_middle.simps split: if_splits)
  3216 apply(case_tac lm1, simp_all add: wprepare_loop1 wprepare_loop2)
  2831 apply(case_tac lm1, simp_all add: wprepare_loop1 wprepare_loop2)
  3217 done
  2832 done
  3218 
  2833 
  3219 lemma [simp]: "wprepare_loop_goon m lm (b, Oc # list)
  2834 lemma wprepare_loop_start_Oc[simp]: "wprepare_loop_goon m lm (b, Oc # list)
  3220   \<Longrightarrow>  wprepare_loop_start m lm (Oc # b, list)"
  2835   \<Longrightarrow>  wprepare_loop_start m lm (Oc # b, list)"
  3221 apply(simp add: wprepare_loop_goon.simps
  2836 apply(simp add: wprepare_loop_goon.simps
  3222                 wprepare_loop_start.simps)
  2837                 wprepare_loop_start.simps)
  3223 done
  2838 done
  3224 
  2839 
  3225 lemma [simp]: "wprepare_add_one m lm (b, Oc # list)
  2840 lemma wprepare_add_one_b[simp]: "wprepare_add_one m lm (b, Oc # list)
  3226        \<Longrightarrow> b = [] \<longrightarrow> wprepare_add_one m lm ([], Bk # Oc # list)"
  2841        \<Longrightarrow> b = [] \<longrightarrow> wprepare_add_one m lm ([], Bk # Oc # list)"
  3227 apply(auto)
  2842 apply(auto)
  3228 apply(simp add: wprepare_add_one.simps)
  2843 apply(simp add: wprepare_add_one.simps)
  3229 done
  2844 done
  3230 
  2845 
  3231 lemma [simp]: "wprepare_goto_start_pos m [a] (b, Oc # list)
  2846 lemma wprepare_loop_start_on_rightmost_Oc2[simp]: "wprepare_goto_start_pos m [a] (b, Oc # list)
  3232               \<Longrightarrow> wprepare_loop_start_on_rightmost m [a] (Oc # b, list) "
  2847               \<Longrightarrow> wprepare_loop_start_on_rightmost m [a] (Oc # b, list) "
  3233 apply(auto simp: wprepare_goto_start_pos.simps 
  2848 apply(auto simp: wprepare_goto_start_pos.simps 
  3234                  wprepare_loop_start_on_rightmost.simps)
  2849                  wprepare_loop_start_on_rightmost.simps)
  3235 apply(rule_tac x = rn in exI, simp)
  2850 apply(rule_tac x = rn in exI, simp)
  3236 apply(simp add: replicate_Suc[THEN sym] exp_ind del: replicate_Suc)
  2851 apply(simp add: replicate_Suc[THEN sym] exp_ind del: replicate_Suc)
  3237 done
  2852 done
  3238 
  2853 
  3239 lemma [simp]:  "wprepare_goto_start_pos m (a # aa # listaa) (b, Oc # list)
  2854 lemma wprepare_loop_start_in_middle_2_Oc[simp]:  "wprepare_goto_start_pos m (a # aa # listaa) (b, Oc # list)
  3240        \<Longrightarrow>wprepare_loop_start_in_middle m (a # aa # listaa) (Oc # b, list)"
  2855        \<Longrightarrow>wprepare_loop_start_in_middle m (a # aa # listaa) (Oc # b, list)"
  3241 apply(auto simp: wprepare_goto_start_pos.simps
  2856 apply(auto simp: wprepare_goto_start_pos.simps
  3242                  wprepare_loop_start_in_middle.simps)
  2857                  wprepare_loop_start_in_middle.simps)
  3243 apply(rule_tac x = rn in exI, simp)
  2858 apply(rule_tac x = rn in exI, simp)
  3244 apply(simp add: exp_ind[THEN sym])
  2859 apply(simp add: exp_ind[THEN sym])
  3245 apply(rule_tac x = a in exI, rule_tac x = "aa#listaa" in exI, simp)
  2860 apply(rule_tac x = a in exI, rule_tac x = "aa#listaa" in exI, simp)
  3246 apply(simp add: tape_of_nl_cons)
  2861 apply(simp add: tape_of_nl_cons)
  3247 done
  2862 done
  3248 
  2863 
  3249 lemma [simp]: "\<lbrakk>lm \<noteq> []; wprepare_goto_start_pos m lm (b, Oc # list)\<rbrakk>
  2864 lemma wprepare_loop_start_Oc2[simp]: "\<lbrakk>lm \<noteq> []; wprepare_goto_start_pos m lm (b, Oc # list)\<rbrakk>
  3250        \<Longrightarrow> wprepare_loop_start m lm (Oc # b, list)"
  2865        \<Longrightarrow> wprepare_loop_start m lm (Oc # b, list)"
  3251 apply(case_tac lm, simp_all)
  2866 apply(case_tac lm, simp_all)
  3252 apply(case_tac lista, simp_all add: wprepare_loop_start.simps)
  2867 apply(case_tac lista, simp_all add: wprepare_loop_start.simps)
  3253 done
  2868 done
  3254 
  2869 
  3255 lemma [simp]: "wprepare_add_one2 m lm (b, Oc # list) \<Longrightarrow> b \<noteq> []"
  2870 lemma wprepare_add_one2_Oc_nonempty[simp]: "wprepare_add_one2 m lm (b, Oc # list) \<Longrightarrow> b \<noteq> []"
  3256 apply(auto simp: wprepare_add_one2.simps)
  2871 apply(auto simp: wprepare_add_one2.simps)
  3257 done
  2872 done
  3258 
  2873 
  3259 lemma add_one_2_stop:
  2874 lemma add_one_2_stop:
  3260   "wprepare_add_one2 m lm (b, Oc # list)      
  2875   "wprepare_add_one2 m lm (b, Oc # list)      
  3261   \<Longrightarrow>  wprepare_stop m lm (tl b, hd b # Oc # list)"
  2876   \<Longrightarrow>  wprepare_stop m lm (tl b, hd b # Oc # list)"
  3262 apply(simp add: wprepare_stop.simps wprepare_add_one2.simps)
  2877 apply(simp add: wprepare_add_one2.simps)
  3263 done
  2878 done
  3264 
  2879 
  3265 declare wprepare_stop.simps[simp del]
  2880 declare wprepare_stop.simps[simp del]
  3266 
  2881 
  3267 lemma wprepare_correctness:
  2882 lemma wprepare_correctness:
  3283       using h
  2898       using h
  3284       apply(rule_tac allI, rule_tac impI, case_tac "?f n", 
  2899       apply(rule_tac allI, rule_tac impI, case_tac "?f n", 
  3285             simp add: step_red step.simps)
  2900             simp add: step_red step.simps)
  3286       apply(case_tac c, simp, case_tac [2] aa)
  2901       apply(case_tac c, simp, case_tac [2] aa)
  3287       apply(simp_all add: wprepare_inv.simps wcode_prepare_le_def lex_triple_def lex_pair_def
  2902       apply(simp_all add: wprepare_inv.simps wcode_prepare_le_def lex_triple_def lex_pair_def
  3288                  split: if_splits)
  2903                  split: if_splits) (* slow *)
  3289       apply(simp_all add: start_2_goon  start_2_start
  2904       apply(simp_all add: start_2_goon  start_2_start
  3290                            add_one_2_add_one add_one_2_stop)
  2905                            add_one_2_add_one add_one_2_stop)
  3291       apply(auto simp: wprepare_add_one2.simps)
  2906       apply(auto simp: wprepare_add_one2.simps)
  3292       done   
  2907       done   
  3293   next
  2908   next
  3302   thus "?thesis"
  2917   thus "?thesis"
  3303     apply(auto)
  2918     apply(auto)
  3304     done
  2919     done
  3305 qed
  2920 qed
  3306 
  2921 
  3307 lemma [intro]: "tm_wf (t_wcode_prepare, 0)"
  2922 lemma tm_wf_t_wcode_prepare[intro]: "tm_wf (t_wcode_prepare, 0)"
  3308 apply(simp add:tm_wf.simps t_wcode_prepare_def)
  2923 apply(simp add:tm_wf.simps t_wcode_prepare_def)
  3309 done
  2924   done
  3310    
  2925 
  3311 lemma t_correct_shift:
  2926 lemma is_28_even[intro]: "(28 + (length t_twice_compile + length t_fourtimes_compile)) mod 2 = 0"
  3312          "list_all (\<lambda>(acn, st). (st \<le> y)) tp \<Longrightarrow>
  2927   by(auto simp: t_twice_compile_def t_fourtimes_compile_def)
  3313           list_all (\<lambda>(acn, st). (st \<le> y + off)) (shift tp off) "
  2928 
  3314 apply(auto simp: List.list_all_length)
  2929 lemma b_le_28[elim]: "(a, b) \<in> set t_wcode_main_first_part \<Longrightarrow>
  3315 apply(erule_tac x = n in allE, simp add: length_shift)
       
  3316 apply(case_tac "tp!n", auto simp: shift.simps)
       
  3317 done
       
  3318 
       
  3319 lemma [intro]: "(28 + (length t_twice_compile + length t_fourtimes_compile)) mod 2 = 0"
       
  3320 apply(auto simp: t_twice_compile_def t_fourtimes_compile_def)
       
  3321 by arith
       
  3322 
       
  3323 lemma [elim]: "(a, b) \<in> set t_wcode_main_first_part \<Longrightarrow>
       
  3324   b \<le> (28 + (length t_twice_compile + length t_fourtimes_compile)) div 2"
  2930   b \<le> (28 + (length t_twice_compile + length t_fourtimes_compile)) div 2"
  3325 apply(auto simp: t_wcode_main_first_part_def t_twice_def)
  2931 apply(auto simp: t_wcode_main_first_part_def t_twice_def)
  3326 done
  2932 done
  3327 
  2933 
  3328 
  2934 
  3329 
  2935 
  3330 lemma tm_wf_change_termi: "tm_wf (tp, 0) \<Longrightarrow> 
  2936 lemma tm_wf_change_termi:
  3331       list_all (\<lambda>(acn, st). (st \<le> Suc (length tp div 2))) (adjust0 tp)"
  2937   assumes "tm_wf (tp, 0)"
  3332 apply(auto simp: tm_wf.simps List.list_all_length)
  2938   shows "list_all (\<lambda>(acn, st). (st \<le> Suc (length tp div 2))) (adjust0 tp)"
  3333 apply(case_tac "tp!n", auto simp: adjust.simps split: if_splits)
  2939 proof -
  3334 apply(erule_tac x = "(a, b)" in ballE, auto)
  2940   { fix acn st n
  3335 by (metis in_set_conv_nth)
  2941     assume "tp ! n = (acn, st)" "n < length tp" "0 < st"
       
  2942     hence "(acn, st)\<in>set tp" by (metis nth_mem)
       
  2943     with assms tm_wf.simps have "st \<le> length tp div 2 + 0" by auto
       
  2944     hence "st \<le> Suc (length tp div 2)" by auto
       
  2945   }
       
  2946   thus ?thesis
       
  2947     by(auto simp: tm_wf.simps List.list_all_length adjust.simps split: if_splits prod.split)
       
  2948 qed
  3336 
  2949 
  3337 lemma tm_wf_shift:
  2950 lemma tm_wf_shift:
  3338          "list_all (\<lambda>(acn, st). (st \<le> y)) tp \<Longrightarrow>
  2951          "list_all (\<lambda>(acn, st). (st \<le> y)) tp \<Longrightarrow>
  3339           list_all (\<lambda>(acn, st). (st \<le> y + off)) (shift tp off) "
  2952           list_all (\<lambda>(acn, st). (st \<le> y + off)) (shift tp off) "
  3340 apply(auto simp: tm_wf.simps List.list_all_length)
  2953 apply(auto simp: tm_wf.simps List.list_all_length)
  3342 apply(case_tac "tp!n", auto simp: shift.simps)
  2955 apply(case_tac "tp!n", auto simp: shift.simps)
  3343 done
  2956 done
  3344 
  2957 
  3345 declare length_tp'[simp del]
  2958 declare length_tp'[simp del]
  3346 
  2959 
  3347 lemma [simp]: "length (mopup (Suc 0)) = 16"
  2960 lemma length_mopup_1[simp]: "length (mopup (Suc 0)) = 16"
  3348 apply(auto simp: mopup.simps)
  2961 apply(auto simp: mopup.simps)
  3349 done
  2962 done
  3350 
  2963 
  3351 lemma [elim]: "(a, b) \<in> set (shift (adjust0 t_twice_compile) 12) \<Longrightarrow> 
  2964 lemma twice_plus_28_elim[elim]: "(a, b) \<in> set (shift (adjust0 t_twice_compile) 12) \<Longrightarrow> 
  3352   b \<le> (28 + (length t_twice_compile + length t_fourtimes_compile)) div 2"
  2965   b \<le> (28 + (length t_twice_compile + length t_fourtimes_compile)) div 2"
  3353 apply(simp add: t_twice_compile_def t_fourtimes_compile_def)
  2966 apply(simp add: t_twice_compile_def t_fourtimes_compile_def)
  3354 proof -
  2967 proof -
  3355   assume g: "(a, b)
  2968   assume g: "(a, b)
  3356     \<in> set (shift
  2969     \<in> set (shift
  3362   moreover have "length (tm_of abc_twice) mod 2 = 0" by auto
  2975   moreover have "length (tm_of abc_twice) mod 2 = 0" by auto
  3363   moreover have "length (tm_of abc_fourtimes) mod 2 = 0" by auto
  2976   moreover have "length (tm_of abc_fourtimes) mod 2 = 0" by auto
  3364   ultimately have "list_all (\<lambda>(acn, st). (st \<le> (60 + (length (tm_of abc_twice) + length (tm_of abc_fourtimes))) div 2)) 
  2977   ultimately have "list_all (\<lambda>(acn, st). (st \<le> (60 + (length (tm_of abc_twice) + length (tm_of abc_fourtimes))) div 2)) 
  3365     (shift (adjust0 t_twice_compile) 12)"
  2978     (shift (adjust0 t_twice_compile) 12)"
  3366   proof(auto simp add: mod_ex1 del: adjust.simps)
  2979   proof(auto simp add: mod_ex1 del: adjust.simps)
  3367     fix q qa
  2980     assume "even (length (tm_of abc_twice))"
  3368     assume h: "length (tm_of abc_twice) = 2 * q" "length (tm_of abc_fourtimes) = 2 * qa"
  2981     then obtain q where q:"length (tm_of abc_twice) = 2 * q" by auto
       
  2982     assume "even (length (tm_of abc_fourtimes))"
       
  2983     then obtain qa where qa:"length (tm_of abc_fourtimes) = 2 * qa" by auto
       
  2984     note h = q qa
  3369     hence "list_all (\<lambda>(acn, st). st \<le> (18 + (q + qa)) + 12) (shift (adjust0 t_twice_compile) 12)"
  2985     hence "list_all (\<lambda>(acn, st). st \<le> (18 + (q + qa)) + 12) (shift (adjust0 t_twice_compile) 12)"
  3370     proof(rule_tac tm_wf_shift t_twice_compile_def)
  2986     proof(rule_tac tm_wf_shift t_twice_compile_def)
  3371       have "list_all (\<lambda>(acn, st). st \<le> Suc (length t_twice_compile div 2)) (adjust0 t_twice_compile)"
  2987       have "list_all (\<lambda>(acn, st). st \<le> Suc (length t_twice_compile div 2)) (adjust0 t_twice_compile)"
  3372         by(rule_tac tm_wf_change_termi, auto)
  2988         by(rule_tac tm_wf_change_termi, auto)
  3373       thus "list_all (\<lambda>(acn, st). st \<le> 18 + (q + qa)) (adjust0 t_twice_compile)"
  2989       thus "list_all (\<lambda>(acn, st). st \<le> 18 + (q + qa)) (adjust0 t_twice_compile)"
  3374         using h
  2990         using h
  3375         apply(simp add: t_twice_compile_def, auto simp: List.list_all_length)
  2991         apply(simp add: t_twice_compile_def, auto simp: List.list_all_length)
  3376         done
  2992         done
  3377     qed
  2993     qed
  3378     thus "list_all (\<lambda>(acn, st). st \<le> 30 + (q + qa))
  2994     thus "list_all (\<lambda>(acn, st). st \<le> 30 + (length (tm_of abc_twice) div 2 + length (tm_of abc_fourtimes) div 2))
  3379            (shift
  2995      (shift (adjust0 t_twice_compile) 12)" using h
  3380              (adjust t_twice_compile (Suc (length t_twice_compile div 2))) 12)"
       
  3381       by simp
  2996       by simp
  3382   qed
  2997   qed
  3383   thus "b \<le> (60 + (length (tm_of abc_twice) + length (tm_of abc_fourtimes))) div 2"
  2998   thus "b \<le> (60 + (length (tm_of abc_twice) + length (tm_of abc_fourtimes))) div 2"
  3384     using g
  2999     using g
  3385     apply(auto simp:t_twice_compile_def)
  3000     apply(auto simp:t_twice_compile_def)
  3386     apply(simp add: Ball_set[THEN sym])
  3001     apply(simp add: Ball_set[THEN sym])
  3387     apply(erule_tac x = "(a, b)" in ballE, simp, simp)
  3002     apply(erule_tac x = "(a, b)" in ballE, simp, simp)
  3388     done
  3003     done
  3389 qed 
  3004 qed 
  3390 
  3005 
  3391 lemma [elim]: "(a, b) \<in> set (shift (adjust0 t_fourtimes_compile) (t_twice_len + 13)) 
  3006 lemma length_plus_28_elim2[elim]: "(a, b) \<in> set (shift (adjust0 t_fourtimes_compile) (t_twice_len + 13)) 
  3392   \<Longrightarrow> b \<le> (28 + (length t_twice_compile + length t_fourtimes_compile)) div 2"
  3007   \<Longrightarrow> b \<le> (28 + (length t_twice_compile + length t_fourtimes_compile)) div 2"
  3393 apply(simp add: t_twice_compile_def t_fourtimes_compile_def t_twice_len_def)
  3008 apply(simp add: t_twice_compile_def t_fourtimes_compile_def t_twice_len_def)
  3394 proof -
  3009 proof -
  3395   assume g: "(a, b)
  3010   assume g: "(a, b)
  3396     \<in> set (shift
  3011     \<in> set (shift
  3397             (adjust
  3012              (adjust (tm_of abc_fourtimes @ shift (mopup (Suc 0)) (length (tm_of abc_fourtimes) div 2))
  3398               (tm_of abc_fourtimes @
  3013                (Suc ((length (tm_of abc_fourtimes) + 16) div 2)))
  3399                shift (mopup (Suc 0)) (length (tm_of abc_fourtimes) div 2))
  3014              (length t_twice div 2 + 13))"
  3400               (Suc ((length (tm_of abc_fourtimes) + 16) div 2)))
       
  3401             (length t_twice div 2 + 13))"
       
  3402   moreover have "length (tm_of abc_twice) mod 2 = 0" by auto
  3015   moreover have "length (tm_of abc_twice) mod 2 = 0" by auto
  3403   moreover have "length (tm_of abc_fourtimes) mod 2 = 0" by auto
  3016   moreover have "length (tm_of abc_fourtimes) mod 2 = 0" by auto
  3404   ultimately have "list_all (\<lambda>(acn, st). (st \<le> (60 + (length (tm_of abc_twice) + length (tm_of abc_fourtimes))) div 2)) 
  3017   ultimately have "list_all (\<lambda>(acn, st). (st \<le> (60 + (length (tm_of abc_twice) + length (tm_of abc_fourtimes))) div 2)) 
  3405     (shift (adjust0 (tm_of abc_fourtimes @ shift (mopup (Suc 0))
  3018     (shift (adjust0 (tm_of abc_fourtimes @ shift (mopup (Suc 0))
  3406     (length (tm_of abc_fourtimes) div 2))) (length t_twice div 2 + 13))"
  3019     (length (tm_of abc_fourtimes) div 2))) (length t_twice div 2 + 13))"
  3407   proof(auto simp: mod_ex1 t_twice_def t_twice_compile_def)
  3020   proof(auto simp: mod_ex1 t_twice_def t_twice_compile_def)
  3408     fix q qa
  3021     assume "even (length (tm_of abc_twice))"
  3409     assume h: "length (tm_of abc_twice) = 2 * q" "length (tm_of abc_fourtimes) = 2 * qa"
  3022     then obtain q where q:"length (tm_of abc_twice) = 2 * q" by auto
       
  3023     assume "even (length (tm_of abc_fourtimes))"
       
  3024     then obtain qa where qa:"length (tm_of abc_fourtimes) = 2 * qa" by auto
       
  3025     note h = q qa
  3410     hence "list_all (\<lambda>(acn, st). st \<le> (9 + qa + (21 + q)))
  3026     hence "list_all (\<lambda>(acn, st). st \<le> (9 + qa + (21 + q)))
  3411       (shift (adjust0 (tm_of abc_fourtimes @ shift (mopup (Suc 0)) qa)) (21 + q))"
  3027       (shift (adjust0 (tm_of abc_fourtimes @ shift (mopup (Suc 0)) qa)) (21 + q))"
  3412     proof(rule_tac tm_wf_shift t_twice_compile_def)
  3028     proof(rule_tac tm_wf_shift t_twice_compile_def)
  3413       have "list_all (\<lambda>(acn, st). st \<le> Suc (length (tm_of abc_fourtimes @ shift 
  3029       have "list_all (\<lambda>(acn, st). st \<le> Suc (length (tm_of abc_fourtimes @ shift 
  3414         (mopup (Suc 0)) qa) div 2)) (adjust0 (tm_of abc_fourtimes @ shift (mopup (Suc 0)) qa))"
  3030         (mopup (Suc 0)) qa) div 2)) (adjust0 (tm_of abc_fourtimes @ shift (mopup (Suc 0)) qa))"
  3422                 2)))"
  3038                 2)))"
  3423         using h
  3039         using h
  3424         apply(simp)
  3040         apply(simp)
  3425         done
  3041         done
  3426     qed
  3042     qed
  3427     thus "list_all (\<lambda>(acn, st). st \<le> 30 + (q + qa))
  3043     thus "list_all
  3428            (shift
  3044      (\<lambda>(acn, st). st \<le> 30 + (length (tm_of abc_twice) div 2 + length (tm_of abc_fourtimes) div 2))
  3429              (adjust (tm_of abc_fourtimes @ shift (mopup (Suc 0)) qa)
  3045      (shift
  3430                (9 + qa))
  3046        (adjust (tm_of abc_fourtimes @ shift (mopup (Suc 0)) (length (tm_of abc_fourtimes) div 2))
  3431              (21 + q))"
  3047          (9 + length (tm_of abc_fourtimes) div 2))
       
  3048        (21 + length (tm_of abc_twice) div 2))"
  3432       apply(subgoal_tac "qa + q = q + qa")
  3049       apply(subgoal_tac "qa + q = q + qa")
  3433       apply(simp add: h)
  3050       apply(simp add: h)
  3434       apply(simp)
  3051       apply(simp)
  3435       done
  3052       done
  3436   qed
  3053   qed
  3439     apply(simp add: Ball_set[THEN sym])
  3056     apply(simp add: Ball_set[THEN sym])
  3440     apply(erule_tac x = "(a, b)" in ballE, simp, simp)
  3057     apply(erule_tac x = "(a, b)" in ballE, simp, simp)
  3441     done
  3058     done
  3442 qed
  3059 qed
  3443 
  3060 
  3444 lemma [intro]: "tm_wf (t_wcode_main, 0)"
  3061 lemma tm_wf_t_wcode_main[intro]: "tm_wf (t_wcode_main, 0)"
  3445 apply(auto simp: t_wcode_main_def tm_wf.simps
  3062 by(auto simp: t_wcode_main_def tm_wf.simps
  3446                  t_twice_def t_fourtimes_def del: List.list_all_iff)
  3063                  t_twice_def t_fourtimes_def del: List.list_all_iff)
  3447 done
       
  3448 
  3064 
  3449 declare tm_comp.simps[simp del]
  3065 declare tm_comp.simps[simp del]
  3450 lemma tm_wf_comp: "\<lbrakk>tm_wf (A, 0); tm_wf (B, 0)\<rbrakk> \<Longrightarrow> tm_wf (A |+| B, 0)"
  3066 lemma tm_wf_comp: "\<lbrakk>tm_wf (A, 0); tm_wf (B, 0)\<rbrakk> \<Longrightarrow> tm_wf (A |+| B, 0)"
  3451 apply(auto simp: tm_wf.simps shift.simps adjust.simps tm_comp_length
  3067  by(auto simp: tm_wf.simps shift.simps adjust.simps tm_comp_length
  3452                  tm_comp.simps)
  3068                  tm_comp.simps) auto
  3453 done
       
  3454 
       
  3455 lemma [intro]: "tm_wf (t_wcode_prepare |+| t_wcode_main, 0)"
       
  3456 apply(rule_tac tm_wf_comp, auto)
       
  3457 done
       
  3458 
  3069 
  3459 lemma prepare_mainpart_lemma:
  3070 lemma prepare_mainpart_lemma:
  3460   "args \<noteq> [] \<Longrightarrow> 
  3071   "args \<noteq> [] \<Longrightarrow> 
  3461   \<exists> stp ln rn. steps0 (Suc 0, [], <m # args>) (t_wcode_prepare |+| t_wcode_main) stp
  3072   \<exists> stp ln rn. steps0 (Suc 0, [], <m # args>) (t_wcode_prepare |+| t_wcode_main) stp
  3462               = (0,  Bk # Oc\<up>(Suc m), Bk # Oc # Bk\<up>(ln) @ Bk # Bk # Oc\<up>(bl_bin (<args>)) @ Bk\<up>(rn))"
  3073               = (0,  Bk # Oc\<up>(Suc m), Bk # Oc # Bk\<up>(ln) @ Bk # Bk # Oc\<up>(bl_bin (<args>)) @ Bk\<up>(rn))"
  3515 
  3126 
  3516 definition tinres :: "cell list \<Rightarrow> cell list \<Rightarrow> bool"
  3127 definition tinres :: "cell list \<Rightarrow> cell list \<Rightarrow> bool"
  3517   where
  3128   where
  3518   "tinres xs ys = (\<exists>n. xs = ys @ Bk \<up> n \<or> ys = xs @ Bk \<up> n)"
  3129   "tinres xs ys = (\<exists>n. xs = ys @ Bk \<up> n \<or> ys = xs @ Bk \<up> n)"
  3519    
  3130    
  3520 lemma [simp]:  "tinres r r' \<Longrightarrow> 
  3131 lemma tinres_fetch_congr[simp]:  "tinres r r' \<Longrightarrow> 
  3521   fetch t ss (read r) = 
  3132   fetch t ss (read r) = 
  3522   fetch t ss (read r')"
  3133   fetch t ss (read r')"
  3523 apply(simp add: fetch.simps, auto split: if_splits simp: tinres_def)
  3134 apply(simp add: fetch.simps, auto split: if_splits simp: tinres_def)
  3524 apply(case_tac [!] n, simp_all)
  3135 apply(case_tac [!] n, simp_all)
  3525 done
  3136 done
  3526 
  3137 
  3527 lemma [intro]: "\<exists> n. (a::cell)\<up>(n) = []"
  3138 lemma nonempty_hd_tinres[simp]: "\<lbrakk>tinres r r'; r \<noteq> []; r' \<noteq> []\<rbrakk> \<Longrightarrow> hd r = hd r'"
  3528 by auto
       
  3529 
       
  3530 lemma [simp]: "\<lbrakk>tinres r r'; r \<noteq> []; r' \<noteq> []\<rbrakk> \<Longrightarrow> hd r = hd r'"
       
  3531 apply(auto simp: tinres_def)
  3139 apply(auto simp: tinres_def)
  3532 done
  3140 done
  3533 
  3141 
  3534 lemma [intro]: "hd (Bk\<up>(Suc n)) = Bk"
  3142 lemma tinres_nonempty[simp]:
  3535 apply(simp add: )
  3143   "\<lbrakk>tinres r []; r \<noteq> []\<rbrakk> \<Longrightarrow> hd r = Bk"
  3536 done
  3144   "\<lbrakk>tinres [] r'; r' \<noteq> []\<rbrakk> \<Longrightarrow> hd r' = Bk"
  3537 
  3145   "\<lbrakk>tinres r [];  r \<noteq> []\<rbrakk> \<Longrightarrow> tinres (tl r) []"
  3538 lemma [simp]: "\<lbrakk>tinres r []; r \<noteq> []\<rbrakk> \<Longrightarrow> hd r = Bk"
  3146   "tinres r r' \<Longrightarrow> tinres (b # r) (b # r')"
  3539 apply(auto simp: tinres_def)
  3147   by(auto simp: tinres_def)
  3540 done
  3148 
  3541 
  3149 lemma ex_move_tl[intro]: "\<exists>na. tl r = tl (r @ Bk\<up>(n)) @ Bk\<up>(na) \<or> tl (r @ Bk\<up>(n)) = tl r @ Bk\<up>(na)"
  3542 lemma [simp]: "\<lbrakk>tinres [] r'; r' \<noteq> []\<rbrakk> \<Longrightarrow> hd r' = Bk"
       
  3543 apply(auto simp: tinres_def)
       
  3544 done
       
  3545 
       
  3546 lemma [intro]: "\<exists>na. tl r = tl (r @ Bk\<up>(n)) @ Bk\<up>(na) \<or> tl (r @ Bk\<up>(n)) = tl r @ Bk\<up>(na)"
       
  3547 apply(case_tac r, simp)
  3150 apply(case_tac r, simp)
  3548 apply(case_tac n, simp, simp)
  3151 apply(case_tac n, simp, simp)
  3549 apply(rule_tac x = nat in exI, simp)
  3152 apply(rule_tac x = nat in exI, simp)
  3550 apply(rule_tac x = n in exI, simp)
  3153 apply(rule_tac x = n in exI, simp)
  3551 done
  3154 done
  3552 
  3155 
  3553 lemma [simp]: "tinres r r' \<Longrightarrow> tinres (tl r) (tl r')"
  3156 lemma tinres_tails[simp]: "tinres r r' \<Longrightarrow> tinres (tl r) (tl r')"
  3554 apply(auto simp: tinres_def)
  3157 apply(auto simp: tinres_def)
  3555 apply(case_tac r', simp)
  3158 apply(case_tac r', simp)
  3556 apply(case_tac n, simp_all)
  3159 apply(case_tac n, simp_all)
  3557 apply(rule_tac x = nat in exI, simp)
  3160 apply(rule_tac x = nat in exI, simp)
  3558 apply(rule_tac x = n in exI, simp)
  3161 apply(rule_tac x = n in exI, simp)
  3559 done
  3162 done
  3560 
  3163 
  3561 lemma [simp]: "\<lbrakk>tinres r [];  r \<noteq> []\<rbrakk> \<Longrightarrow> tinres (tl r) []"
  3164 lemma tinres_empty[simp]: 
  3562 apply(case_tac r, auto simp: tinres_def)
  3165   "\<lbrakk>tinres [] r'\<rbrakk> \<Longrightarrow> tinres [] (tl r')"
  3563 apply(case_tac n, simp_all add: )
  3166   "tinres r [] \<Longrightarrow> tinres (Bk # tl r) [Bk]"
  3564 apply(rule_tac x = nat in exI, simp)
  3167   "tinres r [] \<Longrightarrow> tinres (Oc # tl r) [Oc]"
  3565 done
  3168   by(auto simp: tinres_def)
  3566 
  3169 
  3567 lemma [simp]: "\<lbrakk>tinres [] r'\<rbrakk> \<Longrightarrow> tinres [] (tl r')"
  3170 lemma tinres_step2:
  3568 apply(case_tac r', auto simp: tinres_def)
  3171   assumes "tinres r r'" "step0 (ss, l, r) t = (sa, la, ra)" "step0 (ss, l, r') t = (sb, lb, rb)"
  3569 apply(case_tac n, simp_all add: )
  3172   shows "la = lb \<and> tinres ra rb \<and> sa = sb"
  3570 apply(rule_tac x = nat in exI, simp)
  3173 proof (cases "fetch t ss (read r')")
  3571 done
  3174   case (Pair a b)
  3572 
  3175   have sa:"sa = sb" using assms Pair by(force simp: step.simps)
  3573 lemma [simp]: "tinres r r' \<Longrightarrow> tinres (b # r) (b # r')"
  3176   have "la = lb \<and> tinres ra rb" using assms Pair
  3574 apply(auto simp: tinres_def)
  3177     by(cases a, auto simp: step.simps split: if_splits)
  3575 done
  3178   thus ?thesis using sa by auto
  3576 
  3179 qed
  3577 lemma [simp]: "tinres r [] \<Longrightarrow> tinres (Bk # tl r) [Bk]"
       
  3578 apply(auto simp: tinres_def)
       
  3579 done
       
  3580 
       
  3581 lemma [simp]: "tinres r [] \<Longrightarrow> tinres (Oc # tl r) [Oc]"
       
  3582 apply(auto simp: tinres_def)
       
  3583 done
       
  3584 
       
  3585 lemma tinres_step2: 
       
  3586   "\<lbrakk>tinres r r'; step0 (ss, l, r) t = (sa, la, ra); step0 (ss, l, r') t = (sb, lb, rb)\<rbrakk>
       
  3587     \<Longrightarrow> la = lb \<and> tinres ra rb \<and> sa = sb"
       
  3588 apply(case_tac "ss = 0", simp add: step_0)
       
  3589 apply(simp add: step.simps [simp del], auto)
       
  3590 apply(case_tac [!] "fetch t ss (read r')", simp)
       
  3591 apply(auto simp: update.simps)
       
  3592 apply(case_tac [!] a, auto split: if_splits)
       
  3593 done
       
  3594 
  3180 
  3595 lemma tinres_steps2: 
  3181 lemma tinres_steps2: 
  3596   "\<lbrakk>tinres r r'; steps0 (ss, l, r) t stp = (sa, la, ra); steps0 (ss, l, r') t stp = (sb, lb, rb)\<rbrakk>
  3182   "\<lbrakk>tinres r r'; steps0 (ss, l, r) t stp = (sa, la, ra); steps0 (ss, l, r') t stp = (sb, lb, rb)\<rbrakk>
  3597     \<Longrightarrow> la = lb \<and> tinres ra rb \<and> sa = sb"
  3183     \<Longrightarrow> la = lb \<and> tinres ra rb \<and> sa = sb"
  3598 apply(induct stp arbitrary: sa la ra sb lb rb, simp add: steps.simps)
  3184 apply(induct stp arbitrary: sa la ra sb lb rb, simp add: steps.simps)
  3622   "t_wcode_adjust = [(W1, 1), (R, 2), (Nop, 2), (R, 3), (R, 3), (R, 4), 
  3208   "t_wcode_adjust = [(W1, 1), (R, 2), (Nop, 2), (R, 3), (R, 3), (R, 4), 
  3623                    (L, 8), (L, 5), (L, 6), (W0, 5), (L, 6), (R, 7), 
  3209                    (L, 8), (L, 5), (L, 6), (W0, 5), (L, 6), (R, 7), 
  3624                    (W1, 2), (Nop, 7), (L, 9), (W0, 8), (L, 9), (L, 10), 
  3210                    (W1, 2), (Nop, 7), (L, 9), (W0, 8), (L, 9), (L, 10), 
  3625                     (L, 11), (L, 10), (R, 0), (L, 11)]"
  3211                     (L, 11), (L, 10), (R, 0), (L, 11)]"
  3626                  
  3212                  
  3627 lemma [simp]: "fetch t_wcode_adjust (Suc 0) Bk = (W1, 1)"
  3213 lemma fetch_t_wcode_adjust[simp]:
  3628 apply(simp add: fetch.simps t_wcode_adjust_def nth_of.simps)
  3214   "fetch t_wcode_adjust (Suc 0) Bk = (W1, 1)"
  3629 done
  3215   "fetch t_wcode_adjust (Suc 0) Oc = (R, 2)"
  3630 
  3216   "fetch t_wcode_adjust (Suc (Suc 0)) Oc = (R, 3)"
  3631 lemma [simp]: "fetch t_wcode_adjust (Suc 0) Oc = (R, 2)"
  3217   "fetch t_wcode_adjust (Suc (Suc (Suc 0))) Oc = (R, 4)"
  3632 apply(simp add: fetch.simps t_wcode_adjust_def nth_of.simps)
  3218   "fetch t_wcode_adjust  (Suc (Suc (Suc 0))) Bk = (R, 3)"
  3633 done
  3219   "fetch t_wcode_adjust 4 Bk = (L, 8)"
  3634 
  3220   "fetch t_wcode_adjust 4 Oc = (L, 5)"
  3635 lemma [simp]: "fetch t_wcode_adjust (Suc (Suc 0)) Oc = (R, 3)"
  3221   "fetch t_wcode_adjust 5 Oc = (W0, 5)"
  3636 apply(simp add: fetch.simps t_wcode_adjust_def nth_of.simps)
  3222   "fetch t_wcode_adjust 5 Bk = (L, 6)"
  3637 done
  3223   "fetch t_wcode_adjust 6 Oc = (R, 7)"
  3638 
  3224   "fetch t_wcode_adjust 6 Bk = (L, 6)"
  3639 lemma [simp]: "fetch t_wcode_adjust (Suc (Suc (Suc 0))) Oc = (R, 4)"
  3225   "fetch t_wcode_adjust 7 Bk = (W1, 2)"
  3640 apply(simp add: fetch.simps t_wcode_adjust_def nth_of.simps)
  3226   "fetch t_wcode_adjust 8 Bk = (L, 9)"
  3641 done
  3227   "fetch t_wcode_adjust 8 Oc = (W0, 8)"
  3642 
  3228   "fetch t_wcode_adjust 9 Oc = (L, 10)"
  3643 lemma [simp]: "fetch t_wcode_adjust  (Suc (Suc (Suc 0))) Bk = (R, 3)"
  3229   "fetch t_wcode_adjust 9 Bk = (L, 9)"
  3644 apply(simp add: fetch.simps t_wcode_adjust_def nth_of.simps)
  3230   "fetch t_wcode_adjust 10 Bk = (L, 11)"
  3645 done
  3231   "fetch t_wcode_adjust 10 Oc = (L, 10)"
  3646 
  3232   "fetch t_wcode_adjust 11 Oc = (L, 11)"
  3647 lemma [simp]: "fetch t_wcode_adjust 4 Bk = (L, 8)"
  3233   "fetch t_wcode_adjust 11 Bk = (R, 0)"
  3648 apply(simp add: fetch.simps t_wcode_adjust_def nth_of.simps numeral_4_eq_4)
  3234 by(auto simp: fetch.simps t_wcode_adjust_def nth_of.simps numeral)
  3649 done
  3235 
  3650 
       
  3651 lemma [simp]: "fetch t_wcode_adjust 4 Oc = (L, 5)"
       
  3652 apply(simp add: fetch.simps t_wcode_adjust_def nth_of.simps numeral_4_eq_4)
       
  3653 done
       
  3654 
       
  3655 lemma [simp]: "fetch t_wcode_adjust 5 Oc = (W0, 5)"
       
  3656 apply(simp only: fetch.simps t_wcode_adjust_def nth_of.simps numeral_5_eq_5, simp)
       
  3657 done
       
  3658 
       
  3659 lemma [simp]: "fetch t_wcode_adjust 5 Bk = (L, 6)"
       
  3660 apply(simp only: fetch.simps t_wcode_adjust_def nth_of.simps numeral_5_eq_5, auto)
       
  3661 done
       
  3662 
       
  3663 lemma [simp]: "fetch t_wcode_adjust 6 Oc = (R, 7)"
       
  3664 apply(simp add: fetch.simps t_wcode_adjust_def nth_of.simps numeral_6_eq_6)
       
  3665 done
       
  3666 
       
  3667 lemma [simp]: "fetch t_wcode_adjust 6 Bk = (L, 6)"
       
  3668 apply(simp add: fetch.simps t_wcode_adjust_def nth_of.simps numeral_6_eq_6)
       
  3669 done
       
  3670 
       
  3671 lemma [simp]: "fetch t_wcode_adjust 7 Bk = (W1, 2)"
       
  3672 apply(simp add: fetch.simps t_wcode_adjust_def nth_of.simps numeral_7_eq_7)
       
  3673 done
       
  3674 
       
  3675 lemma [simp]: "fetch t_wcode_adjust 8 Bk = (L, 9)"
       
  3676 apply(simp add: fetch.simps t_wcode_adjust_def nth_of.simps numeral_8_eq_8)
       
  3677 done
       
  3678 
       
  3679 lemma [simp]: "fetch t_wcode_adjust 8 Oc = (W0, 8)"
       
  3680 apply(simp add: fetch.simps t_wcode_adjust_def nth_of.simps numeral_8_eq_8)
       
  3681 done
       
  3682 
       
  3683 lemma [simp]: "fetch t_wcode_adjust 9 Oc = (L, 10)"
       
  3684 apply(simp add: fetch.simps t_wcode_adjust_def nth_of.simps numeral_9_eq_9)
       
  3685 done
       
  3686 
       
  3687 lemma [simp]: "fetch t_wcode_adjust 9 Bk = (L, 9)"
       
  3688 apply(simp add: fetch.simps t_wcode_adjust_def nth_of.simps numeral_9_eq_9)
       
  3689 done
       
  3690 
       
  3691 lemma [simp]: "fetch t_wcode_adjust 10 Bk = (L, 11)"
       
  3692 apply(simp add: fetch.simps t_wcode_adjust_def nth_of.simps  numeral_10_eq_10)
       
  3693 done
       
  3694 
       
  3695 lemma [simp]: "fetch t_wcode_adjust 10 Oc = (L, 10)"
       
  3696 apply(simp add: fetch.simps t_wcode_adjust_def nth_of.simps eval_nat_numeral)
       
  3697 done
       
  3698 
       
  3699 lemma [simp]: "fetch t_wcode_adjust 11 Oc = (L, 11)"
       
  3700 apply(simp add: fetch.simps t_wcode_adjust_def nth_of.simps eval_nat_numeral)
       
  3701 done
       
  3702 
       
  3703 lemma [simp]: "fetch t_wcode_adjust 11 Bk = (R, 0)"
       
  3704 apply(simp add: fetch.simps t_wcode_adjust_def nth_of.simps eval_nat_numeral)
       
  3705 done
       
  3706 
  3236 
  3707 fun wadjust_start :: "nat \<Rightarrow> nat \<Rightarrow> tape \<Rightarrow> bool"
  3237 fun wadjust_start :: "nat \<Rightarrow> nat \<Rightarrow> tape \<Rightarrow> bool"
  3708   where
  3238   where
  3709   "wadjust_start m rs (l, r) = 
  3239   "wadjust_start m rs (l, r) = 
  3710          (\<exists> ln rn. l = Bk # Oc\<up>(Suc m) \<and>
  3240          (\<exists> ln rn. l = Bk # Oc\<up>(Suc m) \<and>
  3911       wadjust_step rs (st, l, r))"
  3441       wadjust_step rs (st, l, r))"
  3912 
  3442 
  3913 definition wadjust_le :: "((nat \<times> config) \<times> nat \<times> config) set"
  3443 definition wadjust_le :: "((nat \<times> config) \<times> nat \<times> config) set"
  3914   where "wadjust_le \<equiv> (inv_image lex_square wadjust_measure)"
  3444   where "wadjust_le \<equiv> (inv_image lex_square wadjust_measure)"
  3915 
  3445 
  3916 lemma [intro]: "wf lex_square"
  3446 lemma wf_lex_square[intro]: "wf lex_square"
  3917 by(auto intro:wf_lex_prod simp: Abacus.lex_pair_def lex_square_def 
  3447 by(auto intro:wf_lex_prod simp: Abacus.lex_pair_def lex_square_def 
  3918   Abacus.lex_triple_def)
  3448   Abacus.lex_triple_def)
  3919 
  3449 
  3920 lemma wf_wadjust_le[intro]: "wf wadjust_le"
  3450 lemma wf_wadjust_le[intro]: "wf wadjust_le"
  3921 by(auto intro:wf_inv_image simp: wadjust_le_def
  3451 by(auto intro:wf_inv_image simp: wadjust_le_def
  3922            Abacus.lex_triple_def Abacus.lex_pair_def)
  3452            Abacus.lex_triple_def Abacus.lex_pair_def)
  3923 
  3453 
  3924 lemma [simp]: "wadjust_start m rs (c, []) = False"
  3454 lemma wadjust_start_snd_nonempty[simp]: "wadjust_start m rs (c, []) = False"
  3925 apply(auto simp: wadjust_start.simps)
  3455 apply(auto simp: wadjust_start.simps)
  3926 done
  3456 done
  3927 
  3457 
  3928 lemma [simp]: "wadjust_loop_right_move m rs (c, []) \<Longrightarrow> c \<noteq> []"
  3458 lemma wadjust_loop_right_move_fst_nonempty[simp]: "wadjust_loop_right_move m rs (c, []) \<Longrightarrow> c \<noteq> []"
  3929 apply(auto simp: wadjust_loop_right_move.simps)
  3459 apply(auto simp: wadjust_loop_right_move.simps)
  3930 done
  3460 done
  3931 
  3461 
  3932 lemma [simp]: "wadjust_loop_right_move m rs (c, [])
  3462 lemma wadjust_loop_check_fst_nonempty[simp]: "wadjust_loop_check m rs (c, []) \<Longrightarrow> c \<noteq> []"
  3933         \<Longrightarrow>  wadjust_loop_check m rs (Bk # c, [])"
       
  3934 apply(simp only: wadjust_loop_right_move.simps wadjust_loop_check.simps)
       
  3935 apply(auto)
       
  3936 done
       
  3937 
       
  3938 lemma [simp]: "wadjust_loop_check m rs (c, []) \<Longrightarrow> c \<noteq> []"
       
  3939 apply(simp only: wadjust_loop_check.simps, auto)
  3463 apply(simp only: wadjust_loop_check.simps, auto)
  3940 done
  3464 done
  3941  
  3465  
  3942 lemma [simp]: "wadjust_loop_start m rs (c, []) = False"
  3466 lemma wadjust_loop_start_snd_nonempty[simp]: "wadjust_loop_start m rs (c, []) = False"
  3943 apply(simp add: wadjust_loop_start.simps)
  3467 apply(simp add: wadjust_loop_start.simps)
  3944 done
  3468 done
  3945 
  3469 
  3946 lemma [simp]: "wadjust_loop_right_move m rs (c, []) \<Longrightarrow> 
  3470 lemma wadjust_erase2_singleton[simp]: "wadjust_loop_check m rs (c, []) \<Longrightarrow> wadjust_erase2 m rs (tl c, [hd c])"
  3947   wadjust_loop_right_move m rs (Bk # c, [])"
       
  3948 apply(simp only: wadjust_loop_right_move.simps)
       
  3949 apply(erule_tac exE)+
       
  3950 apply(auto)
       
  3951 done
       
  3952 
       
  3953 lemma [simp]: "wadjust_loop_check m rs (c, []) \<Longrightarrow> wadjust_erase2 m rs (tl c, [hd c])"
       
  3954 apply(simp only: wadjust_loop_check.simps wadjust_erase2.simps, auto)
  3471 apply(simp only: wadjust_loop_check.simps wadjust_erase2.simps, auto)
  3955 done
  3472 done
  3956 
  3473 
  3957 lemma [simp]: " wadjust_loop_erase m rs (c, [])
  3474 lemma wadjust_loop_on_left_moving_snd_nonempty[simp]:
  3958     \<Longrightarrow> (c = [] \<longrightarrow> wadjust_loop_on_left_moving m rs ([], [Bk])) \<and>
  3475   "wadjust_loop_on_left_moving m rs (c, []) = False"
  3959         (c \<noteq> [] \<longrightarrow> wadjust_loop_on_left_moving m rs (tl c, [hd c]))"
  3476 "wadjust_loop_right_move2 m rs (c, []) = False"
  3960 apply(simp add: wadjust_loop_erase.simps)
  3477 "wadjust_erase2 m rs ([], []) = False"
  3961 done
  3478   by(auto simp: wadjust_loop_on_left_moving.simps
  3962 
  3479         wadjust_loop_right_move2.simps
  3963 lemma [simp]: "wadjust_loop_on_left_moving m rs (c, []) = False"
  3480         wadjust_erase2.simps)
  3964 apply(auto simp: wadjust_loop_on_left_moving.simps)
  3481 
  3965 done
  3482 lemma wadjust_on_left_moving_B_Bk1[simp]: "wadjust_on_left_moving_B m rs 
  3966 
       
  3967 
       
  3968 lemma [simp]: "wadjust_loop_right_move2 m rs (c, []) = False"
       
  3969 apply(auto simp: wadjust_loop_right_move2.simps)
       
  3970 done
       
  3971    
       
  3972 lemma [simp]: "wadjust_erase2 m rs ([], []) = False"
       
  3973 apply(auto simp: wadjust_erase2.simps)
       
  3974 done
       
  3975 
       
  3976 lemma [simp]: "wadjust_on_left_moving_B m rs 
       
  3977                  (Oc # Oc # Oc\<up>(rs) @ Bk # Oc # Oc\<up>(m), [Bk])"
  3483                  (Oc # Oc # Oc\<up>(rs) @ Bk # Oc # Oc\<up>(m), [Bk])"
  3978 apply(simp add: wadjust_on_left_moving_B.simps, auto)
  3484 apply(simp add: wadjust_on_left_moving_B.simps, auto)
  3979 done
  3485 done
  3980 
  3486 
  3981 lemma [simp]: "wadjust_on_left_moving_B m rs 
  3487 lemma wadjust_on_left_moving_B_Bk2[simp]: "wadjust_on_left_moving_B m rs 
  3982                  (Bk\<up>(n) @ Bk # Oc # Oc # Oc\<up>(rs) @ Bk # Oc # Oc\<up>(m), [Bk])"
  3488                  (Bk\<up>(n) @ Bk # Oc # Oc # Oc\<up>(rs) @ Bk # Oc # Oc\<up>(m), [Bk])"
  3983 apply(simp add: wadjust_on_left_moving_B.simps , auto)
  3489 apply(simp add: wadjust_on_left_moving_B.simps , auto)
  3984 apply(rule_tac x = "Suc n" in exI, simp add: exp_ind del: replicate_Suc)
  3490 apply(rule_tac x = "Suc n" in exI, simp add: exp_ind del: replicate_Suc)
  3985 done
  3491 done
  3986 
  3492 
  3987 lemma [simp]: "\<lbrakk>wadjust_erase2 m rs (c, []); c \<noteq> []\<rbrakk> \<Longrightarrow>
  3493 lemma wadjust_on_left_moving_singleton[simp]: "\<lbrakk>wadjust_erase2 m rs (c, []); c \<noteq> []\<rbrakk> \<Longrightarrow>
  3988             wadjust_on_left_moving m rs (tl c, [hd c])"
  3494             wadjust_on_left_moving m rs (tl c, [hd c])"
  3989 apply(simp only: wadjust_erase2.simps)
  3495 apply(simp only: wadjust_erase2.simps)
  3990 apply(erule_tac exE)+
  3496 apply(erule_tac exE)+
  3991 apply(case_tac ln, simp_all add:  wadjust_on_left_moving.simps)
  3497 apply(case_tac ln, simp_all add:  wadjust_on_left_moving.simps)
  3992 done
  3498 done
  3993 
  3499 
  3994 lemma [simp]: "wadjust_erase2 m rs (c, [])
  3500 lemma wadjust_erase2_cases[simp]: "wadjust_erase2 m rs (c, [])
  3995     \<Longrightarrow> (c = [] \<longrightarrow> wadjust_on_left_moving m rs ([], [Bk])) \<and> 
  3501     \<Longrightarrow> (c = [] \<longrightarrow> wadjust_on_left_moving m rs ([], [Bk])) \<and> 
  3996        (c \<noteq> [] \<longrightarrow> wadjust_on_left_moving m rs (tl c, [hd c]))"
  3502        (c \<noteq> [] \<longrightarrow> wadjust_on_left_moving m rs (tl c, [hd c]))"
  3997 apply(auto)
  3503 apply(auto)
  3998 done
  3504 done
  3999 
  3505 
  4000 lemma [simp]: "wadjust_on_left_moving m rs ([], []) = False"
  3506 lemma wadjust_on_left_moving_nonempty[simp]:
  4001 apply(simp add: wadjust_on_left_moving.simps 
  3507    "wadjust_on_left_moving m rs ([], []) = False"
       
  3508    "wadjust_on_left_moving_O m rs (c, []) = False"
       
  3509 apply(auto simp: wadjust_on_left_moving.simps 
  4002   wadjust_on_left_moving_O.simps wadjust_on_left_moving_B.simps)
  3510   wadjust_on_left_moving_O.simps wadjust_on_left_moving_B.simps)
  4003 done
  3511 done
  4004 
  3512 
  4005 lemma [simp]: "wadjust_on_left_moving_O m rs (c, []) = False"
  3513 lemma wadjust_on_left_moving_B_singleton_Bk[simp]:
  4006 apply(simp add: wadjust_on_left_moving_O.simps)
  3514   " \<lbrakk>wadjust_on_left_moving_B m rs (c, []); c \<noteq> []; hd c = Bk\<rbrakk> \<Longrightarrow>
  4007 done
       
  4008 
       
  4009 lemma [simp]: " \<lbrakk>wadjust_on_left_moving_B m rs (c, []); c \<noteq> []; hd c = Bk\<rbrakk> \<Longrightarrow>
       
  4010                                       wadjust_on_left_moving_B m rs (tl c, [Bk])"
  3515                                       wadjust_on_left_moving_B m rs (tl c, [Bk])"
  4011 apply(simp add: wadjust_on_left_moving_B.simps, auto)
  3516 apply(simp add: wadjust_on_left_moving_B.simps, auto)
  4012 apply(case_tac [!] ln, simp_all)
  3517 apply(case_tac [!] ln, simp_all)
  4013 done
  3518 done
  4014 
  3519 
  4015 lemma [simp]: "\<lbrakk>wadjust_on_left_moving_B m rs (c, []); c \<noteq> []; hd c = Oc\<rbrakk> \<Longrightarrow>
  3520 lemma wadjust_on_left_moving_B_singleton_Oc[simp]:
       
  3521  "\<lbrakk>wadjust_on_left_moving_B m rs (c, []); c \<noteq> []; hd c = Oc\<rbrakk> \<Longrightarrow>
  4016                                   wadjust_on_left_moving_O m rs (tl c, [Oc])"
  3522                                   wadjust_on_left_moving_O m rs (tl c, [Oc])"
  4017 apply(simp add: wadjust_on_left_moving_B.simps wadjust_on_left_moving_O.simps, auto)
  3523 apply(simp add: wadjust_on_left_moving_B.simps wadjust_on_left_moving_O.simps, auto)
  4018 apply(case_tac [!] ln, simp_all add: )
  3524 apply(case_tac [!] ln, simp_all add: )
  4019 done
  3525   done
  4020 
  3526 
  4021 lemma [simp]: "\<lbrakk>wadjust_on_left_moving m rs (c, []); c \<noteq> []\<rbrakk> \<Longrightarrow> 
  3527 lemma wadjust_on_left_moving_singleton2[simp]:
       
  3528  "\<lbrakk>wadjust_on_left_moving m rs (c, []); c \<noteq> []\<rbrakk> \<Longrightarrow> 
  4022   wadjust_on_left_moving m rs (tl c, [hd c])"
  3529   wadjust_on_left_moving m rs (tl c, [hd c])"
  4023 apply(simp add: wadjust_on_left_moving.simps)
  3530 apply(simp add: wadjust_on_left_moving.simps)
  4024 apply(case_tac "hd c", simp_all)
  3531 apply(case_tac "hd c", simp_all)
  4025 done
  3532 done
  4026 
  3533 
  4027 lemma [simp]: "wadjust_on_left_moving m rs (c, [])
  3534 lemma wadjust_nonempty[simp]: "wadjust_goon_left_moving m rs (c, []) = False"
  4028     \<Longrightarrow> (c = [] \<longrightarrow> wadjust_on_left_moving m rs ([], [Bk])) \<and> 
  3535 "wadjust_backto_standard_pos m rs (c, []) = False"
  4029        (c \<noteq> [] \<longrightarrow> wadjust_on_left_moving m rs (tl c, [hd c]))"
  3536   by(auto simp: wadjust_goon_left_moving.simps wadjust_goon_left_moving_B.simps
  4030 apply(auto)
  3537                  wadjust_goon_left_moving_O.simps wadjust_backto_standard_pos.simps
  4031 done
       
  4032 
       
  4033 lemma [simp]: "wadjust_goon_left_moving m rs (c, []) = False"
       
  4034 apply(auto simp: wadjust_goon_left_moving.simps wadjust_goon_left_moving_B.simps
       
  4035                  wadjust_goon_left_moving_O.simps)
       
  4036 done
       
  4037 
       
  4038 lemma [simp]: "wadjust_backto_standard_pos m rs (c, []) = False"
       
  4039 apply(auto simp: wadjust_backto_standard_pos.simps
       
  4040  wadjust_backto_standard_pos_B.simps wadjust_backto_standard_pos_O.simps)
  3538  wadjust_backto_standard_pos_B.simps wadjust_backto_standard_pos_O.simps)
  4041 done
  3539 
  4042 
  3540 lemma wadjust_loop_start_no_Bk[simp]: "wadjust_loop_start m rs (c, Bk # list) = False"
  4043 lemma [simp]:
       
  4044   "wadjust_start m rs (c, Bk # list) \<Longrightarrow> 
       
  4045   (c = [] \<longrightarrow> wadjust_start m rs ([], Oc # list)) \<and> 
       
  4046   (c \<noteq> [] \<longrightarrow> wadjust_start m rs (c, Oc # list))"
       
  4047 apply(auto simp: wadjust_start.simps)
       
  4048 done
       
  4049 
       
  4050 lemma [simp]: "wadjust_loop_start m rs (c, Bk # list) = False"
       
  4051 apply(auto simp: wadjust_loop_start.simps)
  3541 apply(auto simp: wadjust_loop_start.simps)
  4052 done
  3542 done
  4053 
  3543 
  4054 lemma [simp]: "wadjust_loop_right_move m rs (c, b) \<Longrightarrow> c \<noteq> []"
  3544 lemma wadjust_loop_right_move_Bk[simp]: "wadjust_loop_right_move m rs (c, Bk # list)
  4055 apply(simp only: wadjust_loop_right_move.simps, auto)
       
  4056 done
       
  4057 
       
  4058 lemma [simp]: "wadjust_loop_right_move m rs (c, Bk # list)
       
  4059     \<Longrightarrow> wadjust_loop_right_move m rs (Bk # c, list)"
  3545     \<Longrightarrow> wadjust_loop_right_move m rs (Bk # c, list)"
  4060 apply(simp only: wadjust_loop_right_move.simps)
  3546 apply(simp only: wadjust_loop_right_move.simps)
  4061 apply(erule_tac exE)+
  3547 apply(erule_tac exE)+
  4062 apply(rule_tac x = ml in exI, simp)
  3548 apply(rule_tac x = ml in exI, simp)
  4063 apply(rule_tac x = mr in exI, simp)
  3549 apply(rule_tac x = mr in exI, simp)
  4064 apply(rule_tac x = "Suc nl" in exI, simp add: )
  3550 apply(rule_tac x = "Suc nl" in exI, simp add: )
  4065 apply(case_tac nr, simp, case_tac mr, simp_all add: )
  3551 apply(case_tac nr, simp, case_tac mr, simp_all add: )
  4066 apply(rule_tac x = nat in exI, auto)
  3552 apply(rule_tac x = nat in exI, auto)
  4067 done
  3553 done
  4068 
  3554 
  4069 lemma [simp]: "wadjust_loop_check m rs (c, b) \<Longrightarrow> c \<noteq> []"
  3555 lemma wadjust_loop_check_nonempty[simp]: "wadjust_loop_check m rs (c, b) \<Longrightarrow> c \<noteq> []"
  4070 apply(simp only: wadjust_loop_check.simps, auto)
  3556 apply(simp only: wadjust_loop_check.simps, auto)
  4071 done
  3557 done
  4072 
  3558 
  4073 lemma [simp]: "wadjust_loop_check m rs (c, Bk # list)
  3559 lemma wadjust_erase2_via_loop_check_Bk[simp]: "wadjust_loop_check m rs (c, Bk # list)
  4074               \<Longrightarrow>  wadjust_erase2 m rs (tl c, hd c # Bk # list)"
  3560               \<Longrightarrow>  wadjust_erase2 m rs (tl c, hd c # Bk # list)"
  4075 apply(auto simp: wadjust_loop_check.simps wadjust_erase2.simps)
  3561 apply(auto simp: wadjust_loop_check.simps wadjust_erase2.simps)
  4076 apply(case_tac [!] mr, simp_all)
  3562 apply(case_tac [!] mr, simp_all)
  4077 done
  3563 done
  4078 
  3564 
  4079 lemma [simp]: "wadjust_loop_erase m rs (c, b) \<Longrightarrow> c \<noteq> []"
       
  4080 apply(simp only: wadjust_loop_erase.simps, auto)
       
  4081 done
       
  4082 
       
  4083 declare wadjust_loop_on_left_moving_O.simps[simp del]
  3565 declare wadjust_loop_on_left_moving_O.simps[simp del]
  4084         wadjust_loop_on_left_moving_B.simps[simp del]
  3566         wadjust_loop_on_left_moving_B.simps[simp del]
  4085 
  3567 
  4086 lemma [simp]: "\<lbrakk>wadjust_loop_erase m rs (c, Bk # list); hd c = Bk\<rbrakk>
  3568 lemma wadjust_loop_on_left_moving_B_via_erase[simp]: "\<lbrakk>wadjust_loop_erase m rs (c, Bk # list); hd c = Bk\<rbrakk>
  4087     \<Longrightarrow> wadjust_loop_on_left_moving_B m rs (tl c, Bk # Bk # list)"
  3569     \<Longrightarrow> wadjust_loop_on_left_moving_B m rs (tl c, Bk # Bk # list)"
  4088 apply(simp only: wadjust_loop_erase.simps 
  3570 apply(simp only: wadjust_loop_erase.simps 
  4089   wadjust_loop_on_left_moving_B.simps)
  3571   wadjust_loop_on_left_moving_B.simps)
  4090 apply(erule_tac exE)+
  3572 apply(erule_tac exE)+
  4091 apply(rule_tac x = ml in exI, rule_tac x = mr in exI, 
  3573 apply(rule_tac x = ml in exI, rule_tac x = mr in exI, 
  4092       rule_tac x = ln in exI, rule_tac x = 0 in exI, simp)
  3574       rule_tac x = ln in exI, rule_tac x = 0 in exI, simp)
  4093 apply(case_tac ln, simp_all add: , auto)
  3575 apply(case_tac ln, simp_all add: , auto)
  4094 apply(simp add: exp_ind [THEN sym])
  3576 apply(simp add: exp_ind [THEN sym])
  4095 done
  3577 done
  4096 
  3578 
  4097 lemma [simp]: "\<lbrakk>wadjust_loop_erase m rs (c, Bk # list); c \<noteq> []; hd c = Oc\<rbrakk> \<Longrightarrow>
  3579 lemma wadjust_loop_on_left_moving_O_Bk_via_erase[simp]:
       
  3580  "\<lbrakk>wadjust_loop_erase m rs (c, Bk # list); c \<noteq> []; hd c = Oc\<rbrakk> \<Longrightarrow>
  4098              wadjust_loop_on_left_moving_O m rs (tl c, Oc # Bk # list)"
  3581              wadjust_loop_on_left_moving_O m rs (tl c, Oc # Bk # list)"
  4099 apply(simp only: wadjust_loop_erase.simps wadjust_loop_on_left_moving_O.simps,
  3582 apply(simp only: wadjust_loop_erase.simps wadjust_loop_on_left_moving_O.simps,
  4100        auto)
  3583        auto)
  4101 apply(case_tac [!] ln, simp_all add: )
  3584 apply(case_tac [!] ln, simp_all add: )
  4102 done
  3585 done
  4103 
  3586 
  4104 lemma [simp]: "\<lbrakk>wadjust_loop_erase m rs (c, Bk # list); c \<noteq> []\<rbrakk> \<Longrightarrow> 
  3587 lemma wadjust_loop_on_left_moving_Bk_via_erase[simp]: "\<lbrakk>wadjust_loop_erase m rs (c, Bk # list); c \<noteq> []\<rbrakk> \<Longrightarrow> 
  4105                 wadjust_loop_on_left_moving m rs (tl c, hd c # Bk # list)"
  3588                 wadjust_loop_on_left_moving m rs (tl c, hd c # Bk # list)"
  4106 apply(case_tac "hd c", simp_all add:wadjust_loop_on_left_moving.simps)
  3589 apply(case_tac "hd c", simp_all add:wadjust_loop_on_left_moving.simps)
  4107 done
  3590 done
  4108 
  3591 
  4109 lemma [simp]: "wadjust_loop_on_left_moving m rs (c, b) \<Longrightarrow> c \<noteq> []"
  3592 
  4110 apply(simp add: wadjust_loop_on_left_moving.simps 
  3593 lemma wadjust_loop_on_left_moving_B_Bk_move[simp]:
  4111 wadjust_loop_on_left_moving_O.simps wadjust_loop_on_left_moving_B.simps, auto)
  3594  "\<lbrakk>wadjust_loop_on_left_moving_B m rs (c, Bk # list); hd c = Bk\<rbrakk>
  4112 done
       
  4113 
       
  4114 lemma [simp]: "wadjust_loop_on_left_moving_O m rs (c, Bk # list) = False"
       
  4115 apply(simp add: wadjust_loop_on_left_moving_O.simps)
       
  4116 done
       
  4117 
       
  4118 lemma [simp]: "\<lbrakk>wadjust_loop_on_left_moving_B m rs (c, Bk # list); hd c = Bk\<rbrakk>
       
  4119     \<Longrightarrow>  wadjust_loop_on_left_moving_B m rs (tl c, Bk # Bk # list)"
  3595     \<Longrightarrow>  wadjust_loop_on_left_moving_B m rs (tl c, Bk # Bk # list)"
  4120 apply(simp only: wadjust_loop_on_left_moving_B.simps)
  3596 apply(simp only: wadjust_loop_on_left_moving_B.simps)
  4121 apply(erule_tac exE)+
  3597 apply(erule_tac exE)+
  4122 apply(rule_tac x = ml in exI, rule_tac x = mr in exI)
  3598 apply(rule_tac x = ml in exI, rule_tac x = mr in exI)
  4123 apply(case_tac nl, simp_all add: , auto)
  3599 apply(case_tac nl, simp_all add: , auto)
  4124 apply(rule_tac x = "Suc nr" in exI, auto simp: )
  3600 apply(rule_tac x = "Suc nr" in exI, auto simp: )
  4125 done
  3601 done
  4126 
  3602 
  4127 lemma [simp]: "\<lbrakk>wadjust_loop_on_left_moving_B m rs (c, Bk # list); hd c = Oc\<rbrakk>
  3603 lemma wadjust_loop_on_left_moving_O_Oc_move[simp]:
       
  3604  "\<lbrakk>wadjust_loop_on_left_moving_B m rs (c, Bk # list); hd c = Oc\<rbrakk>
  4128     \<Longrightarrow> wadjust_loop_on_left_moving_O m rs (tl c, Oc # Bk # list)"
  3605     \<Longrightarrow> wadjust_loop_on_left_moving_O m rs (tl c, Oc # Bk # list)"
  4129 apply(simp only: wadjust_loop_on_left_moving_O.simps 
  3606 apply(simp only: wadjust_loop_on_left_moving_O.simps 
  4130                  wadjust_loop_on_left_moving_B.simps)
  3607                  wadjust_loop_on_left_moving_B.simps)
  4131 apply(erule_tac exE)+
  3608 apply(erule_tac exE)+
  4132 apply(rule_tac x = ml in exI, rule_tac x = mr in exI)
  3609 apply(rule_tac x = ml in exI, rule_tac x = mr in exI)
  4133 apply(case_tac nl, simp_all add: , auto)
  3610 apply(case_tac nl, simp_all add: , auto)
  4134 done
  3611 done
  4135 
  3612 
  4136 lemma [simp]: "wadjust_loop_on_left_moving m rs (c, Bk # list)
  3613 lemma wadjust_loop_on_left_moving_O_noBk[simp]:
       
  3614  "wadjust_loop_on_left_moving_O m rs (c, Bk # list) = False"
       
  3615 apply(simp add: wadjust_loop_on_left_moving_O.simps)
       
  3616 done
       
  3617 lemma wadjust_loop_on_left_moving_Bk_move[simp]:
       
  3618  "wadjust_loop_on_left_moving m rs (c, Bk # list)
  4137             \<Longrightarrow> wadjust_loop_on_left_moving m rs (tl c, hd c # Bk # list)"
  3619             \<Longrightarrow> wadjust_loop_on_left_moving m rs (tl c, hd c # Bk # list)"
  4138 apply(simp add: wadjust_loop_on_left_moving.simps)
  3620 apply(simp add: wadjust_loop_on_left_moving.simps)
  4139 apply(case_tac "hd c", simp_all)
  3621 apply(case_tac "hd c", simp_all)
  4140 done
  3622 done
  4141 
  3623 
  4142 lemma [simp]: "wadjust_loop_right_move2 m rs (c, b) \<Longrightarrow> c \<noteq> []"
  3624 lemma wadjust_loop_erase_nonempty[simp]: "wadjust_loop_erase m rs (c, b) \<Longrightarrow> c \<noteq> []"
  4143 apply(simp only: wadjust_loop_right_move2.simps, auto)
  3625 "wadjust_loop_on_left_moving m rs (c, b) \<Longrightarrow> c \<noteq> []"
  4144 done
  3626 "wadjust_loop_right_move2 m rs (c, b) \<Longrightarrow> c \<noteq> []"
  4145 
  3627 "wadjust_erase2 m rs (c, Bk # list) \<Longrightarrow> c \<noteq> []"
  4146 lemma [simp]: "wadjust_loop_right_move2 m rs (c, Bk # list) \<Longrightarrow>  wadjust_loop_start m rs (c, Oc # list)"
  3628 "wadjust_on_left_moving m rs (c,b) \<Longrightarrow> c \<noteq> []"
       
  3629 "wadjust_on_left_moving_O m rs (c, Bk # list) = False"
       
  3630 "wadjust_goon_left_moving m rs (c, b) \<Longrightarrow> c \<noteq> []"
       
  3631   by(auto simp: wadjust_loop_erase.simps wadjust_loop_on_left_moving.simps 
       
  3632     wadjust_loop_on_left_moving_O.simps wadjust_loop_on_left_moving_B.simps
       
  3633     wadjust_loop_right_move2.simps wadjust_erase2.simps
       
  3634     wadjust_on_left_moving.simps
       
  3635     wadjust_on_left_moving_O.simps
       
  3636     wadjust_on_left_moving_B.simps wadjust_goon_left_moving.simps
       
  3637                 wadjust_goon_left_moving_B.simps
       
  3638                 wadjust_goon_left_moving_O.simps)
       
  3639 
       
  3640 lemma wadjust_loop_start_Oc_via_Bk_move[simp]: 
       
  3641 "wadjust_loop_right_move2 m rs (c, Bk # list) \<Longrightarrow>  wadjust_loop_start m rs (c, Oc # list)"
  4147 apply(auto simp: wadjust_loop_right_move2.simps wadjust_loop_start.simps)
  3642 apply(auto simp: wadjust_loop_right_move2.simps wadjust_loop_start.simps)
  4148 apply(case_tac ln, simp_all add: )
  3643 apply(case_tac ln, simp_all add: )
  4149 apply(rule_tac x = 0 in exI, simp)
  3644 apply(rule_tac x = 0 in exI, simp)
  4150 apply(rule_tac x = rn in exI, simp)
  3645 apply(rule_tac x = rn in exI, simp)
  4151 apply(rule_tac x = "Suc ml" in exI, simp add: , auto)
  3646 apply(rule_tac x = "Suc ml" in exI, simp add: , auto)
  4152 apply(rule_tac x = "Suc nat" in exI, simp add: exp_ind del: replicate_Suc)
  3647 apply(rule_tac x = "Suc nat" in exI, simp add: exp_ind del: replicate_Suc)
  4153 apply(rule_tac x = rn in exI, auto)
  3648 apply(rule_tac x = rn in exI, auto)
  4154 apply(rule_tac x = "Suc ml" in exI, auto )
  3649 apply(rule_tac x = "Suc ml" in exI, auto )
  4155 done
  3650   done
  4156 
  3651 
  4157 lemma [simp]: "wadjust_erase2 m rs (c, Bk # list) \<Longrightarrow> c \<noteq> []"
  3652 lemma wadjust_on_left_moving_Bk_via_erase[simp]: "wadjust_erase2 m rs (c, Bk # list) \<Longrightarrow> 
  4158 apply(auto simp:wadjust_erase2.simps )
       
  4159 done
       
  4160 
       
  4161 lemma [simp]: "wadjust_erase2 m rs (c, Bk # list) \<Longrightarrow> 
       
  4162                  wadjust_on_left_moving m rs (tl c, hd c # Bk # list)"
  3653                  wadjust_on_left_moving m rs (tl c, hd c # Bk # list)"
  4163 apply(auto simp: wadjust_erase2.simps)
  3654 apply(auto simp: wadjust_erase2.simps)
  4164 apply(case_tac ln, simp_all add:  wadjust_on_left_moving.simps 
  3655 apply(case_tac ln, simp_all add:  wadjust_on_left_moving.simps 
  4165         wadjust_on_left_moving_O.simps wadjust_on_left_moving_B.simps)
  3656         wadjust_on_left_moving_O.simps wadjust_on_left_moving_B.simps)
  4166 apply(auto)
  3657 apply(auto)
  4167 apply(rule_tac x = "(Suc (Suc rn))" in exI, simp add: )
  3658 apply(rule_tac x = "(Suc (Suc rn))" in exI, simp add: )
  4168 apply(rule_tac x = "Suc nat" in exI, simp add: exp_ind del: replicate_Suc)
  3659 apply(rule_tac x = "Suc nat" in exI, simp add: exp_ind del: replicate_Suc)
  4169 apply(rule_tac x = "(Suc (Suc rn))" in exI, simp add: )
  3660 apply(rule_tac x = "(Suc (Suc rn))" in exI, simp add: )
  4170 done
  3661   done
  4171 
  3662 
  4172 lemma [simp]: "wadjust_on_left_moving m rs (c,b) \<Longrightarrow> c \<noteq> []"
  3663 
  4173 apply(simp only:wadjust_on_left_moving.simps
  3664 lemma wadjust_on_left_moving_B_Bk_drop_one: "\<lbrakk>wadjust_on_left_moving_B m rs (c, Bk # list); hd c = Bk\<rbrakk>
  4174                 wadjust_on_left_moving_O.simps
       
  4175                 wadjust_on_left_moving_B.simps
       
  4176              , auto)
       
  4177 done
       
  4178 
       
  4179 lemma [simp]: "wadjust_on_left_moving_O m rs (c, Bk # list) = False"
       
  4180 apply(simp add: wadjust_on_left_moving_O.simps)
       
  4181 done
       
  4182 
       
  4183 lemma [simp]: "\<lbrakk>wadjust_on_left_moving_B m rs (c, Bk # list); hd c = Bk\<rbrakk>
       
  4184     \<Longrightarrow> wadjust_on_left_moving_B m rs (tl c, Bk # Bk # list)"
  3665     \<Longrightarrow> wadjust_on_left_moving_B m rs (tl c, Bk # Bk # list)"
  4185 apply(auto simp: wadjust_on_left_moving_B.simps)
  3666 apply(auto simp: wadjust_on_left_moving_B.simps)
  4186 apply(case_tac ln, simp_all)
  3667 apply(case_tac ln, simp_all)
  4187 done
  3668 done
  4188 
  3669 
  4189 lemma [simp]: "\<lbrakk>wadjust_on_left_moving_B m rs (c, Bk # list); hd c = Oc\<rbrakk>
  3670 lemma wadjust_on_left_moving_B_Bk_drop_Oc: "\<lbrakk>wadjust_on_left_moving_B m rs (c, Bk # list); hd c = Oc\<rbrakk>
  4190     \<Longrightarrow> wadjust_on_left_moving_O m rs (tl c, Oc # Bk # list)"
  3671     \<Longrightarrow> wadjust_on_left_moving_O m rs (tl c, Oc # Bk # list)"
  4191 apply(auto simp: wadjust_on_left_moving_O.simps
  3672 apply(auto simp: wadjust_on_left_moving_O.simps
  4192                  wadjust_on_left_moving_B.simps)
  3673                  wadjust_on_left_moving_B.simps)
  4193 apply(case_tac ln, simp_all add: )
  3674 apply(case_tac ln, simp_all add: )
  4194 done
  3675 done
  4195 
  3676 
  4196 lemma [simp]: "wadjust_on_left_moving  m rs (c, Bk # list) \<Longrightarrow>  
  3677 lemma wadjust_on_left_moving_B_drop[simp]: "wadjust_on_left_moving  m rs (c, Bk # list) \<Longrightarrow>  
  4197                   wadjust_on_left_moving m rs (tl c, hd c # Bk # list)"
  3678                   wadjust_on_left_moving m rs (tl c, hd c # Bk # list)"
  4198 apply(simp add: wadjust_on_left_moving.simps)
  3679   by(cases "hd c", auto simp:wadjust_on_left_moving.simps wadjust_on_left_moving_B_Bk_drop_one
  4199 apply(case_tac "hd c", simp_all)
  3680       wadjust_on_left_moving_B_Bk_drop_Oc)
  4200 done
  3681 
  4201 
  3682 lemma wadjust_goon_left_moving_O_no_Bk[simp]: "wadjust_goon_left_moving_O m rs (c, Bk # list) = False"
  4202 lemma [simp]: "wadjust_goon_left_moving m rs (c, b) \<Longrightarrow> c \<noteq> []"
       
  4203 apply(simp add: wadjust_goon_left_moving.simps
       
  4204                 wadjust_goon_left_moving_B.simps
       
  4205                 wadjust_goon_left_moving_O.simps , auto)
       
  4206 done
       
  4207 
       
  4208 lemma [simp]: "wadjust_goon_left_moving_O m rs (c, Bk # list) = False"
       
  4209 apply(simp add: wadjust_goon_left_moving_O.simps, auto)
  3683 apply(simp add: wadjust_goon_left_moving_O.simps, auto)
  4210 apply(case_tac mr, simp_all add: )
  3684 apply(case_tac mr, simp_all add: )
  4211 done
  3685 done
  4212 
  3686 
  4213 lemma [simp]: "\<lbrakk>wadjust_goon_left_moving_B m rs (c, Bk # list); hd c = Bk\<rbrakk>
  3687 lemma wadjust_backto_standard_pos_via_left_Bk[simp]:
  4214     \<Longrightarrow> wadjust_backto_standard_pos_B m rs (tl c, Bk # Bk # list)"
  3688  "wadjust_goon_left_moving m rs (c, Bk # list) \<Longrightarrow>
  4215 apply(auto simp: wadjust_goon_left_moving_B.simps 
       
  4216                  wadjust_backto_standard_pos_B.simps )
       
  4217 done
       
  4218 
       
  4219 lemma [simp]: "\<lbrakk>wadjust_goon_left_moving_B m rs (c, Bk # list); hd c = Oc\<rbrakk>
       
  4220     \<Longrightarrow> wadjust_backto_standard_pos_O m rs (tl c, Oc # Bk # list)"
       
  4221 apply(auto simp: wadjust_goon_left_moving_B.simps 
       
  4222                  wadjust_backto_standard_pos_O.simps)
       
  4223 done
       
  4224 
       
  4225 lemma [simp]: "wadjust_goon_left_moving m rs (c, Bk # list) \<Longrightarrow>
       
  4226   wadjust_backto_standard_pos m rs (tl c, hd c # Bk # list)"
  3689   wadjust_backto_standard_pos m rs (tl c, hd c # Bk # list)"
  4227 apply(case_tac "hd c", simp_all add: wadjust_backto_standard_pos.simps 
  3690 by(case_tac "hd c", simp_all add: wadjust_backto_standard_pos.simps wadjust_goon_left_moving.simps
  4228                                      wadjust_goon_left_moving.simps)
  3691                     wadjust_goon_left_moving_B.simps wadjust_backto_standard_pos_O.simps)
  4229 done
  3692 
  4230 
  3693 lemma wadjust_loop_right_move_Oc[simp]: "wadjust_loop_start m rs (c, Oc # list)
  4231 lemma [simp]: "wadjust_backto_standard_pos m rs (c, Bk # list) \<Longrightarrow>
       
  4232   (c = [] \<longrightarrow> wadjust_stop m rs ([Bk], list)) \<and> (c \<noteq> [] \<longrightarrow> wadjust_stop m rs (Bk # c, list))"
       
  4233 apply(auto simp: wadjust_backto_standard_pos.simps 
       
  4234                  wadjust_backto_standard_pos_B.simps
       
  4235                  wadjust_backto_standard_pos_O.simps wadjust_stop.simps)
       
  4236 apply(case_tac [!] mr, simp_all add: )
       
  4237 done
       
  4238 
       
  4239 lemma [simp]: "wadjust_start m rs (c, Oc # list)
       
  4240               \<Longrightarrow> (c = [] \<longrightarrow> wadjust_loop_start m rs ([Oc], list)) \<and>
       
  4241                 (c \<noteq> [] \<longrightarrow> wadjust_loop_start m rs (Oc # c, list))"
       
  4242 apply(auto simp:wadjust_loop_start.simps wadjust_start.simps )
       
  4243 apply(rule_tac x = ln in exI, rule_tac x = rn in exI,
       
  4244       rule_tac x = "Suc 0" in exI, simp)
       
  4245 done
       
  4246 
       
  4247 lemma [simp]: "wadjust_loop_start m rs (c, b) \<Longrightarrow> c \<noteq> []"
       
  4248 apply(simp add: wadjust_loop_start.simps, auto)
       
  4249 done
       
  4250 
       
  4251 lemma [simp]: "wadjust_loop_start m rs (c, Oc # list)
       
  4252               \<Longrightarrow> wadjust_loop_right_move m rs (Oc # c, list)"
  3694               \<Longrightarrow> wadjust_loop_right_move m rs (Oc # c, list)"
  4253 apply(simp add: wadjust_loop_start.simps wadjust_loop_right_move.simps, auto)
  3695 apply(simp add: wadjust_loop_start.simps wadjust_loop_right_move.simps, auto)
  4254 apply(rule_tac x = ml in exI, rule_tac x = mr in exI, 
  3696 apply(rule_tac x = ml in exI, rule_tac x = mr in exI, 
  4255       rule_tac x = 0 in exI, simp)
  3697       rule_tac x = 0 in exI, simp)
  4256 apply(rule_tac x = "Suc ln" in exI, simp add: exp_ind del: replicate_Suc)
  3698 apply(rule_tac x = "Suc ln" in exI, simp add: exp_ind del: replicate_Suc)
  4257 done
  3699 done
  4258 
  3700 
  4259 lemma [simp]: "wadjust_loop_right_move m rs (c, Oc # list) \<Longrightarrow> 
  3701 lemma wadjust_loop_check_Oc[simp]: "wadjust_loop_right_move m rs (c, Oc # list) \<Longrightarrow> 
  4260                        wadjust_loop_check m rs (Oc # c, list)"
  3702                        wadjust_loop_check m rs (Oc # c, list)"
  4261 apply(simp add: wadjust_loop_right_move.simps  
  3703 apply(simp add: wadjust_loop_right_move.simps  
  4262                  wadjust_loop_check.simps, auto)
  3704                  wadjust_loop_check.simps, auto)
  4263 apply(rule_tac [!] x = ml in exI, simp_all add: exp_ind del: replicate_Suc, auto)
  3705 apply(rule_tac [!] x = ml in exI, simp_all add: exp_ind del: replicate_Suc, auto)
  4264 apply(case_tac nl, simp_all add: exp_ind del: replicate_Suc)
  3706 apply(case_tac nl, simp_all add: exp_ind del: replicate_Suc)
  4265 apply(rule_tac x = "mr - 1" in exI, case_tac mr, simp_all add: )
  3707 apply(rule_tac x = "mr - 1" in exI, case_tac mr, simp_all add: )
  4266 apply(case_tac [!] nr, simp_all)
  3708 apply(case_tac [!] nr, simp_all)
  4267 done
  3709 done
  4268 
  3710 
  4269 lemma [simp]: "wadjust_loop_check m rs (c, Oc # list) \<Longrightarrow> 
  3711 lemma wadjust_loop_erase_move_Oc[simp]: "wadjust_loop_check m rs (c, Oc # list) \<Longrightarrow> 
  4270                wadjust_loop_erase m rs (tl c, hd c # Oc # list)"
  3712                wadjust_loop_erase m rs (tl c, hd c # Oc # list)"
  4271 apply(simp only: wadjust_loop_check.simps wadjust_loop_erase.simps)
  3713 apply(simp only: wadjust_loop_check.simps wadjust_loop_erase.simps)
  4272 apply(erule_tac exE)+
  3714 apply(erule_tac exE)+
  4273 apply(rule_tac x = ml in exI, rule_tac x = mr in exI, auto)
  3715 apply(rule_tac x = ml in exI, rule_tac x = mr in exI, auto)
  4274 apply(case_tac mr, simp_all add: )
  3716 apply(case_tac mr, simp_all add: )
  4275 done
  3717 done
  4276 
  3718 
  4277 lemma [simp]: "wadjust_loop_erase m rs (c, Oc # list) \<Longrightarrow> 
  3719 lemma wadjust_loop_erase_Bk_via_Oc[simp]: "wadjust_loop_erase m rs (c, Oc # list) \<Longrightarrow> 
  4278                 wadjust_loop_erase m rs (c, Bk # list)"
  3720                 wadjust_loop_erase m rs (c, Bk # list)"
  4279 apply(auto simp: wadjust_loop_erase.simps)
  3721 apply(auto simp: wadjust_loop_erase.simps)
  4280 done
  3722 done
  4281 
  3723 
  4282 lemma [simp]: "wadjust_loop_on_left_moving_B m rs (c, Oc # list) = False"
  3724 lemma wadjust_loop_on_left_moving_B_no_Oc[simp]: "wadjust_loop_on_left_moving_B m rs (c, Oc # list) = False"
  4283 apply(auto simp: wadjust_loop_on_left_moving_B.simps)
  3725 apply(auto simp: wadjust_loop_on_left_moving_B.simps)
  4284 apply(case_tac nr, simp_all add: )
  3726 apply(case_tac nr, simp_all add: )
  4285 done
  3727 done
  4286 
  3728 
  4287 lemma [simp]: "wadjust_loop_on_left_moving m rs (c, Oc # list)
  3729 lemma wadjust_loop_right_move2_via_left_moving[simp]:
       
  3730  "wadjust_loop_on_left_moving m rs (c, Oc # list)
  4288            \<Longrightarrow> wadjust_loop_right_move2 m rs (Oc # c, list)"
  3731            \<Longrightarrow> wadjust_loop_right_move2 m rs (Oc # c, list)"
  4289 apply(simp add:wadjust_loop_on_left_moving.simps)
  3732 apply(simp add:wadjust_loop_on_left_moving.simps)
  4290 apply(auto simp: wadjust_loop_on_left_moving_O.simps
  3733 apply(auto simp: wadjust_loop_on_left_moving_O.simps
  4291                  wadjust_loop_right_move2.simps)
  3734                  wadjust_loop_right_move2.simps)
  4292 done
  3735 done
  4293 
  3736 
  4294 lemma [simp]: "wadjust_loop_right_move2 m rs (c, Oc # list) = False"
  3737 lemma wadjust_loop_right_move2_no_Oc[simp]: "wadjust_loop_right_move2 m rs (c, Oc # list) = False"
  4295 apply(auto simp: wadjust_loop_right_move2.simps )
  3738 apply(auto simp: wadjust_loop_right_move2.simps )
  4296 apply(case_tac ln, simp_all add: )
  3739 apply(case_tac ln, simp_all add: )
  4297 done
  3740 done
  4298 
  3741 
  4299 lemma [simp]: "wadjust_erase2 m rs (c, Oc # list)
  3742 lemma wadjust_on_left_moving_B_no_Oc[simp]:
  4300               \<Longrightarrow> (c = [] \<longrightarrow> wadjust_erase2 m rs ([], Bk # list))
  3743  "wadjust_on_left_moving_B m rs (c, Oc # list) = False"
  4301                \<and> (c \<noteq> [] \<longrightarrow> wadjust_erase2 m rs (c, Bk # list))"
       
  4302 apply(auto simp: wadjust_erase2.simps )
       
  4303 done
       
  4304 
       
  4305 lemma [simp]: "wadjust_on_left_moving_B m rs (c, Oc # list) = False"
       
  4306 apply(auto simp: wadjust_on_left_moving_B.simps)
  3744 apply(auto simp: wadjust_on_left_moving_B.simps)
  4307 done
  3745 done
  4308 
  3746 
  4309 lemma [simp]: "\<lbrakk>wadjust_on_left_moving_O m rs (c, Oc # list); hd c = Bk\<rbrakk> \<Longrightarrow> 
  3747 lemma wadjust_goon_left_moving_B_Bk_Oc: "\<lbrakk>wadjust_on_left_moving_O m rs (c, Oc # list); hd c = Bk\<rbrakk> \<Longrightarrow> 
  4310          wadjust_goon_left_moving_B m rs (tl c, Bk # Oc # list)"
  3748          wadjust_goon_left_moving_B m rs (tl c, Bk # Oc # list)"
  4311 apply(auto simp: wadjust_on_left_moving_O.simps 
  3749 apply(auto simp: wadjust_on_left_moving_O.simps 
  4312      wadjust_goon_left_moving_B.simps )
  3750      wadjust_goon_left_moving_B.simps )
  4313 done
  3751 done
  4314 
  3752 
  4315 lemma [simp]: "\<lbrakk>wadjust_on_left_moving_O m rs (c, Oc # list); hd c = Oc\<rbrakk>
  3753 lemma wadjust_goon_left_moving_O_Oc_Oc: "\<lbrakk>wadjust_on_left_moving_O m rs (c, Oc # list); hd c = Oc\<rbrakk>
  4316     \<Longrightarrow> wadjust_goon_left_moving_O m rs (tl c, Oc # Oc # list)"
  3754     \<Longrightarrow> wadjust_goon_left_moving_O m rs (tl c, Oc # Oc # list)"
  4317 apply(auto simp: wadjust_on_left_moving_O.simps 
  3755 apply(auto simp: wadjust_on_left_moving_O.simps 
  4318                  wadjust_goon_left_moving_O.simps )
  3756                  wadjust_goon_left_moving_O.simps )
  4319 apply(auto simp:  numeral_2_eq_2)
  3757 apply(auto simp:  numeral_2_eq_2)
  4320 done
  3758 done
  4321 
  3759 
  4322 
  3760 
  4323 lemma [simp]: "wadjust_on_left_moving m rs (c, Oc # list) \<Longrightarrow> 
  3761 lemma wadjust_goon_left_moving_Oc[simp]: "wadjust_on_left_moving m rs (c, Oc # list) \<Longrightarrow> 
  4324               wadjust_goon_left_moving m rs (tl c, hd c # Oc # list)"
  3762               wadjust_goon_left_moving m rs (tl c, hd c # Oc # list)"
  4325 apply(simp add: wadjust_on_left_moving.simps   
  3763   by(cases "hd c"; force simp: wadjust_on_left_moving.simps wadjust_goon_left_moving.simps
  4326                  wadjust_goon_left_moving.simps)
  3764      wadjust_goon_left_moving_B_Bk_Oc wadjust_goon_left_moving_O_Oc_Oc)+
  4327 apply(case_tac "hd c", simp_all)
  3765 
  4328 done
  3766 lemma left_moving_Bk_Oc[simp]: "\<lbrakk>wadjust_goon_left_moving_O m rs (c, Oc # list); hd c = Bk\<rbrakk> 
  4329 
       
  4330 lemma [simp]: "wadjust_on_left_moving m rs (c, Oc # list) \<Longrightarrow> 
       
  4331   wadjust_goon_left_moving m rs (tl c, hd c # Oc # list)"
       
  4332 apply(simp add: wadjust_on_left_moving.simps 
       
  4333   wadjust_goon_left_moving.simps)
       
  4334 apply(case_tac "hd c", simp_all)
       
  4335 done
       
  4336 
       
  4337 lemma [simp]: "wadjust_goon_left_moving_B m rs (c, Oc # list) = False"
       
  4338 apply(auto simp: wadjust_goon_left_moving_B.simps)
       
  4339 done
       
  4340 
       
  4341 lemma [simp]: "\<lbrakk>wadjust_goon_left_moving_O m rs (c, Oc # list); hd c = Bk\<rbrakk> 
       
  4342                \<Longrightarrow> wadjust_goon_left_moving_B m rs (tl c, Bk # Oc # list)"
  3767                \<Longrightarrow> wadjust_goon_left_moving_B m rs (tl c, Bk # Oc # list)"
  4343 apply(auto simp: wadjust_goon_left_moving_O.simps wadjust_goon_left_moving_B.simps)
  3768 apply(auto simp: wadjust_goon_left_moving_O.simps wadjust_goon_left_moving_B.simps)
  4344 apply(case_tac [!] ml, auto simp: )
  3769 apply(case_tac [!] ml, auto simp: )
  4345 done
  3770 done
  4346 
  3771 
  4347 lemma  [simp]: "\<lbrakk>wadjust_goon_left_moving_O m rs (c, Oc # list); hd c = Oc\<rbrakk> \<Longrightarrow> 
  3772 lemma  left_moving_Oc_Oc[simp]: "\<lbrakk>wadjust_goon_left_moving_O m rs (c, Oc # list); hd c = Oc\<rbrakk> \<Longrightarrow> 
  4348   wadjust_goon_left_moving_O m rs (tl c, Oc # Oc # list)"
  3773   wadjust_goon_left_moving_O m rs (tl c, Oc # Oc # list)"
  4349 apply(auto simp: wadjust_goon_left_moving_O.simps wadjust_goon_left_moving_B.simps)
  3774 apply(auto simp: wadjust_goon_left_moving_O.simps wadjust_goon_left_moving_B.simps)
  4350 apply(rule_tac x = "ml - 1" in exI, simp)
  3775 apply(rule_tac x = "ml - 1" in exI, simp)
  4351 apply(case_tac ml, simp_all add: )
  3776 apply(case_tac ml, simp_all add: )
  4352 apply(rule_tac x = "Suc mr" in exI, auto simp: )
  3777 apply(rule_tac x = "Suc mr" in exI, auto simp: )
  4353 done
  3778 done
  4354 
  3779 
  4355 lemma [simp]: "wadjust_goon_left_moving m rs (c, Oc # list) \<Longrightarrow> 
  3780 lemma wadjust_goon_left_moving_B_no_Oc[simp]:
       
  3781   "wadjust_goon_left_moving_B m rs (c, Oc # list) = False"
       
  3782 apply(auto simp: wadjust_goon_left_moving_B.simps)
       
  3783   done
       
  3784 
       
  3785 lemma wadjust_goon_left_moving_Oc_move[simp]: "wadjust_goon_left_moving m rs (c, Oc # list) \<Longrightarrow> 
  4356   wadjust_goon_left_moving m rs (tl c, hd c # Oc # list)"
  3786   wadjust_goon_left_moving m rs (tl c, hd c # Oc # list)"
  4357 apply(simp add: wadjust_goon_left_moving.simps)
  3787 by(cases "hd c",auto simp: wadjust_goon_left_moving.simps)
  4358 apply(case_tac "hd c", simp_all)
  3788 
  4359 done
  3789 lemma wadjust_backto_standard_pos_B_no_Oc[simp]:
  4360 
  3790   "wadjust_backto_standard_pos_B m rs (c, Oc # list) = False"
  4361 lemma [simp]: "wadjust_backto_standard_pos_B m rs (c, Oc # list) = False"
       
  4362 apply(simp add: wadjust_backto_standard_pos_B.simps)
  3791 apply(simp add: wadjust_backto_standard_pos_B.simps)
  4363 done
  3792 done
  4364 
  3793 
  4365 lemma [simp]: "wadjust_backto_standard_pos_O m rs (c, Bk # xs) = False"
  3794 lemma wadjust_backto_standard_pos_O_no_Bk[simp]:
       
  3795   "wadjust_backto_standard_pos_O m rs (c, Bk # xs) = False"
  4366 apply(simp add: wadjust_backto_standard_pos_O.simps, auto)
  3796 apply(simp add: wadjust_backto_standard_pos_O.simps, auto)
  4367 apply(case_tac mr, simp_all add: )
  3797 apply(case_tac mr, simp_all add: )
  4368 done
  3798 done
  4369 
  3799 
  4370 lemma [simp]: "wadjust_backto_standard_pos_O m rs ([], Oc # list) \<Longrightarrow> 
  3800 lemma wadjust_backto_standard_pos_B_Bk_Oc[simp]:
       
  3801  "wadjust_backto_standard_pos_O m rs ([], Oc # list) \<Longrightarrow> 
  4371   wadjust_backto_standard_pos_B m rs ([], Bk # Oc # list)"
  3802   wadjust_backto_standard_pos_B m rs ([], Bk # Oc # list)"
  4372 apply(auto simp: wadjust_backto_standard_pos_O.simps
  3803 apply(auto simp: wadjust_backto_standard_pos_O.simps
  4373                  wadjust_backto_standard_pos_B.simps)
  3804                  wadjust_backto_standard_pos_B.simps)
  4374 done
  3805 done
  4375 
  3806 
  4376 lemma [simp]: 
  3807 lemma wadjust_backto_standard_pos_B_Bk_Oc_via_O[simp]: 
  4377   "\<lbrakk>wadjust_backto_standard_pos_O m rs (c, Oc # list); c \<noteq> []; hd c = Bk\<rbrakk>
  3808   "\<lbrakk>wadjust_backto_standard_pos_O m rs (c, Oc # list); c \<noteq> []; hd c = Bk\<rbrakk>
  4378   \<Longrightarrow> wadjust_backto_standard_pos_B m rs (tl c, Bk # Oc # list)"
  3809   \<Longrightarrow> wadjust_backto_standard_pos_B m rs (tl c, Bk # Oc # list)"
  4379 apply(simp add:wadjust_backto_standard_pos_O.simps 
  3810 apply(simp add:wadjust_backto_standard_pos_O.simps 
  4380         wadjust_backto_standard_pos_B.simps, auto)
  3811         wadjust_backto_standard_pos_B.simps, auto)
  4381 done 
  3812 done 
  4382 
  3813 
  4383 lemma [simp]: "\<lbrakk>wadjust_backto_standard_pos_O m rs (c, Oc # list); c \<noteq> []; hd c = Oc\<rbrakk>
  3814 lemma wadjust_backto_standard_pos_B_Oc_Oc_via_O[simp]: "\<lbrakk>wadjust_backto_standard_pos_O m rs (c, Oc # list); c \<noteq> []; hd c = Oc\<rbrakk>
  4384           \<Longrightarrow>  wadjust_backto_standard_pos_O m rs (tl c, Oc # Oc # list)"
  3815           \<Longrightarrow>  wadjust_backto_standard_pos_O m rs (tl c, Oc # Oc # list)"
  4385 apply(simp add: wadjust_backto_standard_pos_O.simps, auto)
  3816 apply(simp add: wadjust_backto_standard_pos_O.simps, auto)
  4386 apply(case_tac ml, simp_all add: , auto)
  3817 apply(case_tac ml, simp_all add: , auto)
  4387 done
  3818 done
  4388 
  3819 
  4389 lemma [simp]: "wadjust_backto_standard_pos m rs (c, Oc # list)
  3820 lemma wadjust_backto_standard_pos_cases[simp]: "wadjust_backto_standard_pos m rs (c, Oc # list)
  4390   \<Longrightarrow> (c = [] \<longrightarrow> wadjust_backto_standard_pos m rs ([], Bk # Oc # list)) \<and> 
  3821   \<Longrightarrow> (c = [] \<longrightarrow> wadjust_backto_standard_pos m rs ([], Bk # Oc # list)) \<and> 
  4391  (c \<noteq> [] \<longrightarrow> wadjust_backto_standard_pos m rs (tl c, hd c # Oc # list))"
  3822  (c \<noteq> [] \<longrightarrow> wadjust_backto_standard_pos m rs (tl c, hd c # Oc # list))"
  4392 apply(auto simp: wadjust_backto_standard_pos.simps)
  3823 apply(auto simp: wadjust_backto_standard_pos.simps)
  4393 apply(case_tac "hd c", simp_all)
  3824 apply(case_tac "hd c", simp_all)
  4394 done
  3825 done
  4395 
  3826 
  4396 lemma [simp]: "wadjust_loop_right_move m rs (c, []) = False"
  3827 lemma wadjust_loop_right_move_nonempty_snd[simp]: "wadjust_loop_right_move m rs (c, []) = False"
  4397 apply(simp only: wadjust_loop_right_move.simps)
  3828 apply(simp only: wadjust_loop_right_move.simps)
  4398 apply(rule_tac iffI)
  3829 apply(rule_tac iffI)
  4399 apply(erule_tac exE)+
  3830 apply(erule_tac exE)+
  4400 apply(case_tac nr, simp_all add: )
  3831 apply(case_tac nr, simp_all add: )
  4401 apply(case_tac mr, simp_all add: )
  3832 apply(case_tac mr, simp_all add: )
  4402 done
  3833 done
  4403 
  3834 
  4404 lemma [simp]: "wadjust_loop_erase m rs (c, []) = False"
  3835 lemma wadjust_loop_erase_nonempty_snd[simp]: "wadjust_loop_erase m rs (c, []) = False"
  4405 apply(simp only: wadjust_loop_erase.simps, auto)
  3836 apply(simp only: wadjust_loop_erase.simps, auto)
  4406 done
  3837 done
  4407 
  3838 
  4408 lemma [simp]: "\<lbrakk>Suc (Suc rs) = a;  wadjust_loop_erase m rs (c, Bk # list)\<rbrakk>
  3839 lemma wadjust_loop_erase_cases2[simp]: "\<lbrakk>Suc (Suc rs) = a;  wadjust_loop_erase m rs (c, Bk # list)\<rbrakk>
  4409   \<Longrightarrow> a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev (tl c) @ hd c # Bk # list))))
  3840   \<Longrightarrow> a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev (tl c) @ hd c # Bk # list))))
  4410   < a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev c @ Bk # list)))) \<or>
  3841   < a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev c @ Bk # list)))) \<or>
  4411   a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev (tl c) @ hd c # Bk # list)))) =
  3842   a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev (tl c) @ hd c # Bk # list)))) =
  4412   a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev c @ Bk # list))))"
  3843   a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev c @ Bk # list))))"
  4413 apply(simp only: wadjust_loop_erase.simps)
  3844 apply(simp only: wadjust_loop_erase.simps)
  4414 apply(rule_tac disjI2)
  3845 apply(rule_tac disjI2)
  4415 apply(case_tac c, simp, simp)
  3846 apply(case_tac c, simp, simp)
  4416 done
  3847   done
  4417 
  3848 
  4418 lemma [simp]:
  3849 lemma dropWhile_exp1: "dropWhile (\<lambda>a. a = Oc) (Oc\<up>(n) @ xs) = dropWhile (\<lambda>a. a = Oc) xs"
       
  3850 apply(induct n, simp_all add: )
       
  3851 done
       
  3852 lemma takeWhile_exp1: "takeWhile (\<lambda>a. a = Oc) (Oc\<up>(n) @ xs) = Oc\<up>(n) @ takeWhile (\<lambda>a. a = Oc) xs"
       
  3853 apply(induct n, simp_all add: )
       
  3854 done
       
  3855 
       
  3856 lemma wadjust_correctness_helper_1:
       
  3857   assumes "Suc (Suc rs) = a" " wadjust_loop_right_move2 m rs (c, Bk # list)"
       
  3858   shows "a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev c @ Oc # list))))
       
  3859                  < a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev c @ Bk # list))))"
       
  3860 proof -
       
  3861   have "ml + mr = Suc rs \<Longrightarrow> 0 < mr \<Longrightarrow>
       
  3862        rs - (ml + length (takeWhile (\<lambda>a. a = Oc) list))
       
  3863        < Suc rs -
       
  3864          (ml +
       
  3865           length
       
  3866            (takeWhile (\<lambda>a. a = Oc)
       
  3867              (Bk \<up> ln @ Bk # Bk # Oc \<up> mr @ Bk \<up> rn))) "
       
  3868     for ml mr ln rn
       
  3869   by(cases ln, auto)
       
  3870   thus ?thesis using assms
       
  3871     by (auto simp: wadjust_loop_right_move2.simps dropWhile_exp1 takeWhile_exp1)
       
  3872 qed
       
  3873 
       
  3874 lemma wadjust_correctness_helper_2:
  4419   "\<lbrakk>Suc (Suc rs) = a;  wadjust_loop_on_left_moving m rs (c, Bk # list)\<rbrakk>
  3875   "\<lbrakk>Suc (Suc rs) = a;  wadjust_loop_on_left_moving m rs (c, Bk # list)\<rbrakk>
  4420   \<Longrightarrow> a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev (tl c) @ hd c # Bk # list))))
  3876   \<Longrightarrow> a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev (tl c) @ hd c # Bk # list))))
  4421   < a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev c @ Bk # list)))) \<or>
  3877   < a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev c @ Bk # list)))) \<or>
  4422   a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev (tl c) @ hd c # Bk # list)))) =
  3878   a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev (tl c) @ hd c # Bk # list)))) =
  4423   a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev c @ Bk # list))))"
  3879   a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev c @ Bk # list))))"
  4424 apply(subgoal_tac "c \<noteq> []")
  3880   apply(subgoal_tac "c \<noteq> []")
  4425 apply(case_tac c, simp_all)
  3881   apply(case_tac c, simp_all)
  4426 done
  3882   done
  4427 
  3883 
  4428 lemma dropWhile_exp1: "dropWhile (\<lambda>a. a = Oc) (Oc\<up>(n) @ xs) = dropWhile (\<lambda>a. a = Oc) xs"
  3884 lemma wadjust_loop_check_empty_false[simp]: "wadjust_loop_check m rs ([], b) = False"
  4429 apply(induct n, simp_all add: )
       
  4430 done
       
  4431 lemma takeWhile_exp1: "takeWhile (\<lambda>a. a = Oc) (Oc\<up>(n) @ xs) = Oc\<up>(n) @ takeWhile (\<lambda>a. a = Oc) xs"
       
  4432 apply(induct n, simp_all add: )
       
  4433 done
       
  4434 
       
  4435 lemma [simp]: "\<lbrakk>Suc (Suc rs) = a;  wadjust_loop_right_move2 m rs (c, Bk # list)\<rbrakk>
       
  4436               \<Longrightarrow> a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev c @ Oc # list))))
       
  4437                  < a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev c @ Bk # list))))"
       
  4438 apply(simp add: wadjust_loop_right_move2.simps, auto)
       
  4439 apply(simp add: dropWhile_exp1 takeWhile_exp1)
       
  4440 apply(case_tac ln, simp, simp add: )
       
  4441 done
       
  4442 
       
  4443 lemma [simp]: "wadjust_loop_check m rs ([], b) = False"
       
  4444 apply(simp add: wadjust_loop_check.simps)
  3885 apply(simp add: wadjust_loop_check.simps)
  4445 done
  3886 done
  4446 
  3887 
  4447 lemma [simp]: "\<lbrakk>Suc (Suc rs) = a;  wadjust_loop_check m rs (c, Oc # list)\<rbrakk>
  3888 lemma wadjust_loop_check_cases: "\<lbrakk>Suc (Suc rs) = a;  wadjust_loop_check m rs (c, Oc # list)\<rbrakk>
  4448   \<Longrightarrow> a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev (tl c) @ hd c # Oc # list))))
  3889   \<Longrightarrow> a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev (tl c) @ hd c # Oc # list))))
  4449   < a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev c @ Oc # list)))) \<or>
  3890   < a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev c @ Oc # list)))) \<or>
  4450   a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev (tl c) @ hd c # Oc # list)))) =
  3891   a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev (tl c) @ hd c # Oc # list)))) =
  4451   a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev c @ Oc # list))))"
  3892   a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev c @ Oc # list))))"
  4452 apply(case_tac "c", simp_all)
  3893 apply(case_tac "c", simp_all)
  4453 done
  3894 done
  4454 
  3895 
  4455 lemma [simp]: 
  3896 lemma wadjust_loop_erase_cases_or: 
  4456   "\<lbrakk>Suc (Suc rs) = a;  wadjust_loop_erase m rs (c, Oc # list)\<rbrakk>
  3897   "\<lbrakk>Suc (Suc rs) = a;  wadjust_loop_erase m rs (c, Oc # list)\<rbrakk>
  4457   \<Longrightarrow> a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev c @ Bk # list))))
  3898   \<Longrightarrow> a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev c @ Bk # list))))
  4458   < a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev c @ Oc # list)))) \<or>
  3899   < a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev c @ Oc # list)))) \<or>
  4459   a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev c @ Bk # list)))) =
  3900   a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev c @ Bk # list)))) =
  4460   a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev c @ Oc # list))))"
  3901   a - length (takeWhile (\<lambda>a. a = Oc) (tl (dropWhile (\<lambda>a. a = Oc) (rev c @ Oc # list))))"
  4462 apply(rule_tac disjI2)
  3903 apply(rule_tac disjI2)
  4463 apply(auto)
  3904 apply(auto)
  4464 apply(simp add: dropWhile_exp1 takeWhile_exp1)
  3905 apply(simp add: dropWhile_exp1 takeWhile_exp1)
  4465 done
  3906 done
  4466 
  3907 
       
  3908 lemmas wadjust_correctness_helpers = wadjust_correctness_helper_2 wadjust_correctness_helper_1 wadjust_loop_erase_cases_or wadjust_loop_check_cases
       
  3909 
  4467 declare numeral_2_eq_2[simp del]
  3910 declare numeral_2_eq_2[simp del]
  4468 
  3911 
  4469 lemma [simp]: "wadjust_start m rs (c, Bk # list)
  3912 lemma wadjust_start_Oc[simp]: "wadjust_start m rs (c, Bk # list)
  4470        \<Longrightarrow> wadjust_start m rs (c, Oc # list)"
  3913        \<Longrightarrow> wadjust_start m rs (c, Oc # list)"
  4471 apply(auto simp: wadjust_start.simps)
  3914 apply(auto simp: wadjust_start.simps)
  4472 done
  3915 done
  4473 
  3916 
  4474 lemma [simp]: "wadjust_backto_standard_pos m rs (c, Bk # list)
  3917 lemma wadjust_stop_Bk[simp]: "wadjust_backto_standard_pos m rs (c, Bk # list)
  4475        \<Longrightarrow> wadjust_stop m rs (Bk # c, list)"
  3918        \<Longrightarrow> wadjust_stop m rs (Bk # c, list)"
  4476 apply(auto simp: wadjust_backto_standard_pos.simps 
  3919 apply(auto simp: wadjust_backto_standard_pos.simps 
  4477 wadjust_stop.simps wadjust_backto_standard_pos_B.simps)
  3920 wadjust_stop.simps wadjust_backto_standard_pos_B.simps)
  4478 done
  3921 done
  4479 
  3922 
  4480 lemma [simp]: "wadjust_start m rs (c, Oc # list)
  3923 lemma wadjust_loop_start_Oc[simp]: "wadjust_start m rs (c, Oc # list)
  4481        \<Longrightarrow> wadjust_loop_start m rs (Oc # c, list)"
  3924        \<Longrightarrow> wadjust_loop_start m rs (Oc # c, list)"
  4482 apply(auto simp: wadjust_start.simps wadjust_loop_start.simps)
  3925 apply(auto simp: wadjust_start.simps wadjust_loop_start.simps)
  4483 apply(rule_tac x = ln in exI, simp)
  3926 apply(rule_tac x = ln in exI, simp)
  4484 apply(rule_tac x = "rn" in exI, simp)
  3927 apply(rule_tac x = "rn" in exI, simp)
  4485 apply(rule_tac x = 1 in exI, simp)
  3928 apply(rule_tac x = 1 in exI, simp)
  4486 done
  3929 done
  4487 
  3930 
  4488 lemma [simp]:" wadjust_erase2 m rs (c, Oc # list)
  3931 lemma erase2_Bk_if_Oc[simp]:" wadjust_erase2 m rs (c, Oc # list)
  4489        \<Longrightarrow> wadjust_erase2 m rs (c, Bk # list)"
  3932        \<Longrightarrow> wadjust_erase2 m rs (c, Bk # list)"
  4490 apply(auto simp: wadjust_erase2.simps)
  3933 apply(auto simp: wadjust_erase2.simps)
  4491 done
  3934 done
  4492 
  3935 
  4493 lemma wadjust_correctness:
  3936 lemma wadjust_correctness:
  4505   proof(rule_tac halt_lemma2)
  3948   proof(rule_tac halt_lemma2)
  4506     show "wf wadjust_le" by auto
  3949     show "wf wadjust_le" by auto
  4507   next
  3950   next
  4508     show "\<forall> n. \<not> ?P (?f n) \<and> ?Q (?f n) \<longrightarrow> 
  3951     show "\<forall> n. \<not> ?P (?f n) \<and> ?Q (?f n) \<longrightarrow> 
  4509                  ?Q (?f (Suc n)) \<and> (?f (Suc n), ?f n) \<in> wadjust_le"
  3952                  ?Q (?f (Suc n)) \<and> (?f (Suc n), ?f n) \<in> wadjust_le"
  4510       apply(rule_tac allI, rule_tac impI, case_tac "?f n", simp)
  3953       apply(rule_tac allI, rule_tac impI, case_tac "?f n")
  4511       apply(simp add: step.simps)
  3954       apply((case_tac d, case_tac [2] aa); simp add: step.simps)
  4512       apply(case_tac d, case_tac [2] aa, simp_all)
       
  4513       apply(simp_all only: wadjust_inv.simps split: if_splits)
  3955       apply(simp_all only: wadjust_inv.simps split: if_splits)
  4514       apply(simp_all)
  3956       apply(simp_all)
  4515       apply(simp_all add: wadjust_inv.simps wadjust_le_def
  3957       apply(simp_all add: wadjust_inv.simps wadjust_le_def
       
  3958             wadjust_correctness_helpers
  4516             Abacus.lex_triple_def Abacus.lex_pair_def lex_square_def  split: if_splits)
  3959             Abacus.lex_triple_def Abacus.lex_pair_def lex_square_def  split: if_splits)
       
  3960       
  4517       done
  3961       done
  4518   next
  3962   next
  4519     show "?Q (?f 0)"
  3963     show "?Q (?f 0)"
  4520       apply(simp add: steps.simps wadjust_inv.simps wadjust_start.simps, auto)
  3964       apply(simp add: steps.simps wadjust_inv.simps wadjust_start.simps, auto)
  4521       done
  3965       done
  4527   thus"?thesis"
  3971   thus"?thesis"
  4528     apply(simp)
  3972     apply(simp)
  4529     done
  3973     done
  4530 qed
  3974 qed
  4531 
  3975 
  4532 lemma [intro]: "tm_wf (t_wcode_adjust, 0)"
  3976 lemma tm_wf_t_wcode_adjust[intro]: "tm_wf (t_wcode_adjust, 0)"
  4533 apply(auto simp: t_wcode_adjust_def tm_wf.simps)
  3977 apply(auto simp: t_wcode_adjust_def tm_wf.simps)
  4534 done
  3978 done
  4535 
  3979 
  4536 declare tm_comp.simps[simp del]
  3980 lemma bl_bin_nonzero[simp]: "args \<noteq> [] \<Longrightarrow> bl_bin (<args::nat list>) > 0"
  4537 
  3981   by(cases args)
  4538 lemma [simp]: "args \<noteq> [] \<Longrightarrow> bl_bin (<args::nat list>) > 0"
  3982     (auto simp: tape_of_nl_cons bl_bin.simps)
  4539 apply(case_tac args)
       
  4540 apply(auto simp: tape_of_nl_cons bl_bin.simps split: if_splits)
       
  4541 done
       
  4542 
  3983 
  4543 lemma wcode_lemma_pre':
  3984 lemma wcode_lemma_pre':
  4544   "args \<noteq> [] \<Longrightarrow> 
  3985   "args \<noteq> [] \<Longrightarrow> 
  4545   \<exists> stp rn. steps0 (Suc 0, [], <m # args>) 
  3986   \<exists> stp rn. steps0 (Suc 0, [], <m # args>) 
  4546               ((t_wcode_prepare |+| t_wcode_main) |+| t_wcode_adjust) stp
  3987               ((t_wcode_prepare |+| t_wcode_main) |+| t_wcode_adjust) stp
  4623 lemma wcode_lemma: 
  4064 lemma wcode_lemma: 
  4624   "args \<noteq> [] \<Longrightarrow> 
  4065   "args \<noteq> [] \<Longrightarrow> 
  4625   \<exists> stp ln rn. steps0 (Suc 0, [], <m # args>)  (t_wcode) stp = 
  4066   \<exists> stp ln rn. steps0 (Suc 0, [], <m # args>)  (t_wcode) stp = 
  4626               (0,  [Bk],  <[m ,bl_bin (<args>)]> @ Bk\<up>(rn))"
  4067               (0,  [Bk],  <[m ,bl_bin (<args>)]> @ Bk\<up>(rn))"
  4627 using wcode_lemma_1[of args m]
  4068 using wcode_lemma_1[of args m]
  4628 apply(simp add: t_wcode_def tape_of_nl_abv tape_of_nat_list.simps tape_of_nat_abv)
  4069 apply(simp add: t_wcode_def tape_of_list_def tape_of_nat_list.simps tape_of_nat_def)
  4629 done
  4070 done
  4630 
  4071 
  4631 section {* The universal TM *}
  4072 section {* The universal TM *}
  4632 
  4073 
  4633 text {*
  4074 text {*
  4659 definition UTM_pre :: "instr list"
  4100 definition UTM_pre :: "instr list"
  4660   where
  4101   where
  4661   "UTM_pre = t_wcode |+| t_utm"
  4102   "UTM_pre = t_wcode |+| t_utm"
  4662 
  4103 
  4663 lemma tinres_step1: 
  4104 lemma tinres_step1: 
  4664   "\<lbrakk>tinres l l'; step (ss, l, r) (t, 0) = (sa, la, ra); 
  4105   assumes "tinres l l'" "step (ss, l, r) (t, 0) = (sa, la, ra)" 
  4665                  step (ss, l', r) (t, 0) = (sb, lb, rb)\<rbrakk>
  4106           "step (ss, l', r) (t, 0) = (sb, lb, rb)"
  4666     \<Longrightarrow> tinres la lb \<and> ra = rb \<and> sa = sb"
  4107   shows "tinres la lb \<and> ra = rb \<and> sa = sb"
  4667 apply(case_tac ss, case_tac [!]r, case_tac [!] "a::cell")
  4108 proof(cases "r")
  4668 apply(auto simp: step.simps fetch.simps nth_of.simps
  4109   case Nil
  4669         split: if_splits )
  4110   then show ?thesis using assms
  4670 apply(case_tac [!] "t ! (2 * nat)", 
  4111     by (cases "(fetch t ss Bk)";cases "fst (fetch t ss Bk)";auto simp:step.simps split:if_splits)
  4671      auto simp: tinres_def split: if_splits)
  4112 next
  4672 apply(case_tac [1-8] a, auto split: if_splits)
  4113   case (Cons a list)
  4673 apply(case_tac [!] "t ! (2 * nat)", 
  4114   then show ?thesis using assms
  4674      auto simp: tinres_def split: if_splits)
  4115     by (cases "(fetch t ss a)";cases "fst (fetch t ss a)";auto simp:step.simps split:if_splits)
  4675 apply(case_tac [1-4] a, auto split: if_splits)
  4116 qed
  4676 apply(case_tac [!] "t ! Suc (2 * nat)", 
       
  4677      auto simp: if_splits)
       
  4678 apply(case_tac [!] aa, auto split: if_splits)
       
  4679 done
       
  4680 
  4117 
  4681 lemma tinres_steps1: 
  4118 lemma tinres_steps1: 
  4682   "\<lbrakk>tinres l l'; steps (ss, l, r) (t, 0) stp = (sa, la, ra); 
  4119   "\<lbrakk>tinres l l'; steps (ss, l, r) (t, 0) stp = (sa, la, ra); 
  4683                  steps (ss, l', r) (t, 0) stp = (sb, lb, rb)\<rbrakk>
  4120                  steps (ss, l', r) (t, 0) stp = (sb, lb, rb)\<rbrakk>
  4684     \<Longrightarrow> tinres la lb \<and> ra = rb \<and> sa = sb"
  4121     \<Longrightarrow> tinres la lb \<and> ra = rb \<and> sa = sb"
  4692           steps (ss, l', r) (t, 0) stp = (sb, lb, rb)\<rbrakk> \<Longrightarrow> tinres la lb \<and> ra = rb \<and> sa = sb"
  4129           steps (ss, l', r) (t, 0) stp = (sb, lb, rb)\<rbrakk> \<Longrightarrow> tinres la lb \<and> ra = rb \<and> sa = sb"
  4693   and h: " tinres l l'" "step (steps (ss, l, r) (t, 0) stp) (t, 0) = (sa, la, ra)"
  4130   and h: " tinres l l'" "step (steps (ss, l, r) (t, 0) stp) (t, 0) = (sa, la, ra)"
  4694          "step (steps (ss, l', r) (t, 0) stp) (t, 0) = (sb, lb, rb)" "steps (ss, l, r) (t, 0) stp = (a, b, c)" 
  4131          "step (steps (ss, l', r) (t, 0) stp) (t, 0) = (sb, lb, rb)" "steps (ss, l, r) (t, 0) stp = (a, b, c)" 
  4695          "steps (ss, l', r) (t, 0) stp = (aa, ba, ca)"
  4132          "steps (ss, l', r) (t, 0) stp = (aa, ba, ca)"
  4696   have "tinres b ba \<and> c = ca \<and> a = aa"
  4133   have "tinres b ba \<and> c = ca \<and> a = aa"
  4697     apply(rule_tac ind, simp_all add: h)
  4134     using ind h by metis
  4698     done
       
  4699   thus "tinres la lb \<and> ra = rb \<and> sa = sb"
  4135   thus "tinres la lb \<and> ra = rb \<and> sa = sb"
  4700     apply(rule_tac l = b and l' = ba and r = c  and ss = a   
  4136     using tinres_step1 h by metis
  4701             and t = t in tinres_step1)
       
  4702     using h
       
  4703     apply(simp, simp, simp)
       
  4704     done
       
  4705 qed
  4137 qed
  4706 
  4138 
  4707 lemma [simp]: 
  4139 lemma tinres_some_exp[simp]: 
  4708   "tinres (Bk \<up> m @ [Bk, Bk]) la \<Longrightarrow> \<exists>m. la = Bk \<up> m"
  4140   "tinres (Bk \<up> m @ [Bk, Bk]) la \<Longrightarrow> \<exists>m. la = Bk \<up> m"
  4709 apply(auto simp: tinres_def)
  4141 apply(auto simp: tinres_def)
  4710 apply(case_tac n, simp add: exp_ind)
  4142 apply(case_tac n, simp add: exp_ind)
  4711 apply(rule_tac  x ="Suc (Suc m)" in exI, simp only: exp_ind, simp)
  4143 apply(rule_tac  x ="Suc (Suc m)" in exI, simp only: exp_ind, simp)
  4712 apply(simp add: exp_ind del: replicate_Suc)
  4144 apply(simp add: exp_ind del: replicate_Suc)
  4778     using assms
  4210     using assms
  4779     apply(case_tac rs, simp_all add: numeral_2_eq_2)
  4211     apply(case_tac rs, simp_all add: numeral_2_eq_2)
  4780     done
  4212     done
  4781 qed
  4213 qed
  4782 
  4214 
  4783 lemma [intro]: "tm_wf (t_wcode, 0)"
  4215 lemma tm_wf_t_wcode[intro]: "tm_wf (t_wcode, 0)"
  4784 apply(simp add: t_wcode_def)
  4216   apply(simp add: t_wcode_def)
  4785 apply(rule_tac tm_wf_comp)
  4217   apply(rule_tac tm_wf_comp)
  4786 apply(rule_tac tm_wf_comp, auto)
  4218   apply(rule_tac tm_wf_comp, auto)
  4787 done
  4219   done
  4788       
       
  4789 lemma [intro]: "tm_wf (t_utm, 0)"
       
  4790 apply(simp only: t_utm_def F_tprog_def)
       
  4791 apply(rule_tac wf_tm_from_abacus, auto)
       
  4792 done 
       
  4793 
  4220 
  4794 lemma UTM_halt_lemma_pre: 
  4221 lemma UTM_halt_lemma_pre: 
  4795   assumes wf_tm: "tm_wf (tp, 0)"
  4222   assumes wf_tm: "tm_wf (tp, 0)"
  4796   and result: "0 < rs"
  4223   and result: "0 < rs"
  4797   and args: "args \<noteq> []"
  4224   and args: "args \<noteq> []"
  4823         (\<lambda>(l, r). (\<exists>ln. l = Bk \<up> ln) \<and>
  4250         (\<lambda>(l, r). (\<exists>ln. l = Bk \<up> ln) \<and>
  4824         (\<exists>rn. r = Oc \<up> rs @ Bk \<up> rn)) holds_for steps0 (Suc 0, [Bk],
  4251         (\<exists>rn. r = Oc \<up> rs @ Bk \<up> rn)) holds_for steps0 (Suc 0, [Bk],
  4825         Oc # Oc \<up> code tp @ Bk # Oc # Oc \<up> bl_bin (<args>) @ Bk \<up> rn) t_utm n"
  4252         Oc # Oc \<up> code tp @ Bk # Oc # Oc \<up> bl_bin (<args>) @ Bk \<up> rn) t_utm n"
  4826         using t_utm_halt_eq[of tp i "args" stp m rs k rn] assms
  4253         using t_utm_halt_eq[of tp i "args" stp m rs k rn] assms
  4827       apply(auto simp: bin_wc_eq)
  4254       apply(auto simp: bin_wc_eq)
  4828       apply(rule_tac x = stpa in exI, simp add: tape_of_nl_abv tape_of_nat_abv)
  4255       apply(rule_tac x = stpa in exI, simp add: tape_of_list_def tape_of_nat_def)
  4829       done
  4256       done
  4830     qed
  4257     qed
  4831   qed
  4258   qed
  4832   thus "?thesis"
  4259   thus "?thesis"
  4833     apply(auto simp: Hoare_halt_def UTM_pre_def)
  4260     apply(auto simp: Hoare_halt_def UTM_pre_def)
  4858 
  4285 
  4859 lemma nstd_case1: "0 < a \<Longrightarrow> NSTD (trpl_code (a, b, c))"
  4286 lemma nstd_case1: "0 < a \<Longrightarrow> NSTD (trpl_code (a, b, c))"
  4860 apply(simp add: NSTD.simps trpl_code.simps)
  4287 apply(simp add: NSTD.simps trpl_code.simps)
  4861 done
  4288 done
  4862 
  4289 
  4863 lemma [simp]: "\<forall>m. b \<noteq> Bk\<up>(m) \<Longrightarrow> 0 < bl2wc b"
  4290 lemma nonzero_bl2wc[simp]: "\<forall>m. b \<noteq> Bk\<up>(m) \<Longrightarrow> 0 < bl2wc b"
  4864 apply(rule classical, simp)
  4291 apply(rule classical, simp)
  4865 apply(induct b, erule_tac x = 0 in allE, simp)
  4292 apply(induct b, erule_tac x = 0 in allE, simp)
  4866 apply(simp add: bl2wc.simps, case_tac a, simp_all 
  4293 apply(simp add: bl2wc.simps, case_tac a, simp_all 
  4867   add: bl2nat.simps bl2nat_double)
  4294   add: bl2nat.simps bl2nat_double)
  4868 apply(case_tac "\<exists> m. b = Bk\<up>(m)",  erule exE)
  4295 apply(case_tac "\<exists> m. b = Bk\<up>(m)",  erule exE)
  4871 
  4298 
  4872 lemma nstd_case2: "\<forall>m. b \<noteq> Bk\<up>(m) \<Longrightarrow> NSTD (trpl_code (a, b, c))"
  4299 lemma nstd_case2: "\<forall>m. b \<noteq> Bk\<up>(m) \<Longrightarrow> NSTD (trpl_code (a, b, c))"
  4873 apply(simp add: NSTD.simps trpl_code.simps)
  4300 apply(simp add: NSTD.simps trpl_code.simps)
  4874 done
  4301 done
  4875 
  4302 
  4876 lemma [elim]: "Suc (2 * x) = 2 * y \<Longrightarrow> RR"
  4303 lemma even_not_odd[elim]: "Suc (2 * x) = 2 * y \<Longrightarrow> RR"
  4877 apply(induct x arbitrary: y, simp, simp)
  4304 apply(induct x arbitrary: y, simp, simp)
  4878 apply(case_tac y, simp, simp)
  4305 apply(case_tac y, simp, simp)
  4879 done
  4306 done
  4880 
  4307 
  4881 declare replicate_Suc[simp del]
  4308 declare replicate_Suc[simp del]
  5198         apply(rule_tac tutm_uhalt, simp_all)
  4625         apply(rule_tac tutm_uhalt, simp_all)
  5199         done
  4626         done
  5200       thus "False"
  4627       thus "False"
  5201         using h
  4628         using h
  5202         apply(erule_tac x = n in allE)
  4629         apply(erule_tac x = n in allE)
  5203         apply(simp add: tape_of_nl_abv bin_wc_eq tape_of_nat_abv)
  4630         apply(simp add: tape_of_list_def bin_wc_eq tape_of_nat_def)
  5204         done
  4631         done
  5205     qed
  4632     qed
  5206   qed
  4633   qed
  5207   thus "?thesis"
  4634   thus "?thesis"
  5208     apply(simp add: Hoare_unhalt_def UTM_pre_def)
  4635     apply(simp add: Hoare_unhalt_def UTM_pre_def)
  5256       show "\<exists>n. is_final (steps0 (Suc 0, [Bk], Oc \<up> Suc (code p) @ Bk # Oc \<up> Suc (bl_bin (<args>)) @ Bk \<up> rn) t_utm n) \<and>
  4683       show "\<exists>n. is_final (steps0 (Suc 0, [Bk], Oc \<up> Suc (code p) @ Bk # Oc \<up> Suc (bl_bin (<args>)) @ Bk \<up> rn) t_utm n) \<and>
  5257              (\<lambda>(l, r). (\<exists>m. l = Bk \<up> m) \<and> (\<exists>n. r = Oc \<up> rs @ Bk \<up> n)) holds_for steps0 
  4684              (\<lambda>(l, r). (\<exists>m. l = Bk \<up> m) \<and> (\<exists>n. r = Oc \<up> rs @ Bk \<up> n)) holds_for steps0 
  5258          (Suc 0, [Bk], Oc \<up> Suc (code p) @ Bk # Oc \<up> Suc (bl_bin (<args>)) @ Bk \<up> rn) t_utm n"      
  4685          (Suc 0, [Bk], Oc \<up> Suc (code p) @ Bk # Oc \<up> Suc (bl_bin (<args>)) @ Bk \<up> rn) t_utm n"      
  5259         using t_utm_halt_eq[of p i "args" stp m rs k rn] assms k
  4686         using t_utm_halt_eq[of p i "args" stp m rs k rn] assms k
  5260         apply(auto simp: bin_wc_eq, auto)        
  4687         apply(auto simp: bin_wc_eq, auto)        
  5261         apply(rule_tac x = stpa in exI, simp add: tape_of_nl_abv tape_of_nat_abv)
  4688         apply(rule_tac x = stpa in exI, simp add: tape_of_list_def tape_of_nat_def)
  5262         done
  4689         done
  5263     qed
  4690     qed
  5264   next
  4691   next
  5265     show "tm_wf0 t_wcode" by auto
  4692     show "tm_wf0 t_wcode" by auto
  5266   qed
  4693   qed
  5284   and args: "(args::nat list) \<noteq> []"
  4711   and args: "(args::nat list) \<noteq> []"
  5285   and exec: "{(\<lambda>tp. tp = ([], <args>))} p {(\<lambda>tp. tp = (Bk\<up>m, <(n::nat)> @ Bk\<up>k))}" 
  4712   and exec: "{(\<lambda>tp. tp = ([], <args>))} p {(\<lambda>tp. tp = (Bk\<up>m, <(n::nat)> @ Bk\<up>k))}" 
  5286   shows "{(\<lambda>tp. tp = ([], <code p # args>))} UTM {(\<lambda>tp. (\<exists> m k. tp = (Bk\<up>m, <n> @ Bk\<up>k)))}"
  4713   shows "{(\<lambda>tp. tp = ([], <code p # args>))} UTM {(\<lambda>tp. (\<exists> m k. tp = (Bk\<up>m, <n> @ Bk\<up>k)))}"
  5287 using UTM_halt_lemma[OF assms(1) _ assms(2), where i="0"]
  4714 using UTM_halt_lemma[OF assms(1) _ assms(2), where i="0"]
  5288 using assms(3)
  4715 using assms(3)
  5289 apply(simp add: tape_of_nat_abv)
  4716 apply(simp add: tape_of_nat_def)
  5290 done
  4717 done
  5291 
  4718 
  5292     
  4719     
  5293 lemma UTM_unhalt_lemma: 
  4720 lemma UTM_unhalt_lemma: 
  5294   assumes tm_wf: "tm_wf (p, 0)"
  4721   assumes tm_wf: "tm_wf (p, 0)"
  5316   and unhalt: "{(\<lambda>tp. tp = ([], <args>))} p \<up>"
  4743   and unhalt: "{(\<lambda>tp. tp = ([], <args>))} p \<up>"
  5317   and args: "args \<noteq> []"
  4744   and args: "args \<noteq> []"
  5318   shows "{(\<lambda>tp. tp = ([], <code p # args>))} UTM \<up>"
  4745   shows "{(\<lambda>tp. tp = ([], <code p # args>))} UTM \<up>"
  5319 using UTM_unhalt_lemma[OF assms(1), where i="0"]
  4746 using UTM_unhalt_lemma[OF assms(1), where i="0"]
  5320 using assms(2-3)
  4747 using assms(2-3)
  5321 apply(simp add: tape_of_nat_abv)
  4748 apply(simp add: tape_of_nat_def)
  5322 done
  4749 done
  5323 
  4750 
  5324 end                               
  4751 end