thys/UTM.thy
changeset 163 67063c5365e1
parent 145 38d8e0e37b7d
child 166 99a180fd4194
equal deleted inserted replaced
162:a63c3f8d7234 163:67063c5365e1
     1 theory UTM
     1 theory UTM
     2 imports Main recursive abacus UF GCD turing_hoare
     2 imports Recursive Abacus UF GCD Turing_Hoare
     3 begin
     3 begin
     4 
     4 
     5 section {* Wang coding of input arguments *}
     5 section {* Wang coding of input arguments *}
     6 
     6 
     7 text {*
     7 text {*
  1245 apply(case_tac [!] a, auto simp: fetch.simps nth_of.simps)
  1245 apply(case_tac [!] a, auto simp: fetch.simps nth_of.simps)
  1246 done
  1246 done
  1247 
  1247 
  1248 lemma adjust_fetch_norm: 
  1248 lemma adjust_fetch_norm: 
  1249   "\<lbrakk>st > 0;  st \<le> length tp div 2; fetch ap st b = (aa, ns); ns \<noteq> 0\<rbrakk>
  1249   "\<lbrakk>st > 0;  st \<le> length tp div 2; fetch ap st b = (aa, ns); ns \<noteq> 0\<rbrakk>
  1250  \<Longrightarrow>  fetch (turing_basic.adjust ap) st b = (aa, ns)"
  1250  \<Longrightarrow>  fetch (Turing.adjust ap) st b = (aa, ns)"
  1251  apply(case_tac b, auto simp: fetch.simps nth_of.simps nth_map
  1251  apply(case_tac b, auto simp: fetch.simps nth_of.simps nth_map
  1252                        split: if_splits)
  1252                        split: if_splits)
  1253 apply(case_tac [!] st, auto simp: fetch.simps nth_of.simps)
  1253 apply(case_tac [!] st, auto simp: fetch.simps nth_of.simps)
  1254 done
  1254 done
  1255 
  1255 
  1297   thus "?case"
  1297   thus "?case"
  1298     by(simp add: steps.simps)
  1298     by(simp add: steps.simps)
  1299 next
  1299 next
  1300   case (Suc stp st' l' r')
  1300   case (Suc stp st' l' r')
  1301   have ind: "\<And>st' l' r'. \<lbrakk>steps0 (st, l, r) ap stp = (st', l', r'); 0 < st'\<rbrakk> 
  1301   have ind: "\<And>st' l' r'. \<lbrakk>steps0 (st, l, r) ap stp = (st', l', r'); 0 < st'\<rbrakk> 
  1302     \<Longrightarrow> steps0 (st, l, r) (turing_basic.adjust ap) stp = (st', l', r')" by fact
  1302     \<Longrightarrow> steps0 (st, l, r) (Turing.adjust ap) stp = (st', l', r')" by fact
  1303   have h: "steps0 (st, l, r) ap (Suc stp) = (st', l', r')" by fact
  1303   have h: "steps0 (st, l, r) ap (Suc stp) = (st', l', r')" by fact
  1304   have g:   "0 < st'" by fact
  1304   have g:   "0 < st'" by fact
  1305   obtain st'' l'' r'' where a: "steps0 (st, l, r) ap stp = (st'', l'', r'')"
  1305   obtain st'' l'' r'' where a: "steps0 (st, l, r) ap stp = (st'', l'', r'')"
  1306     by (metis prod_cases3)
  1306     by (metis prod_cases3)
  1307   hence c:"0 < st''"
  1307   hence c:"0 < st''"
  1308     using h g
  1308     using h g
  1309     apply(simp add: step_red)
  1309     apply(simp add: step_red)
  1310     apply(case_tac st'', auto)
  1310     apply(case_tac st'', auto)
  1311     done
  1311     done
  1312   hence b: "steps0 (st, l, r) (turing_basic.adjust ap) stp = (st'', l'', r'')"
  1312   hence b: "steps0 (st, l, r) (Turing.adjust ap) stp = (st'', l'', r'')"
  1313     using a
  1313     using a
  1314     by(rule_tac ind, simp_all)
  1314     by(rule_tac ind, simp_all)
  1315   thus "?case"
  1315   thus "?case"
  1316     using assms a b h g
  1316     using assms a b h g
  1317     apply(simp add: step_red) 
  1317     apply(simp add: step_red) 
  3371 
  3371 
  3372 lemma [simp]: "length (mopup (Suc 0)) = 16"
  3372 lemma [simp]: "length (mopup (Suc 0)) = 16"
  3373 apply(auto simp: mopup.simps)
  3373 apply(auto simp: mopup.simps)
  3374 done
  3374 done
  3375 
  3375 
  3376 lemma [elim]: "(a, b) \<in> set (shift (turing_basic.adjust t_twice_compile) 12) \<Longrightarrow> 
  3376 lemma [elim]: "(a, b) \<in> set (shift (Turing.adjust t_twice_compile) 12) \<Longrightarrow> 
  3377   b \<le> (28 + (length t_twice_compile + length t_fourtimes_compile)) div 2"
  3377   b \<le> (28 + (length t_twice_compile + length t_fourtimes_compile)) div 2"
  3378 apply(simp add: t_twice_compile_def t_fourtimes_compile_def)
  3378 apply(simp add: t_twice_compile_def t_fourtimes_compile_def)
  3379 proof -
  3379 proof -
  3380   assume g: "(a, b) \<in> set (shift (turing_basic.adjust (tm_of abc_twice @ shift (mopup (Suc 0)) (length (tm_of abc_twice) div 2))) 12)"
  3380   assume g: "(a, b) \<in> set (shift (Turing.adjust (tm_of abc_twice @ shift (mopup (Suc 0)) (length (tm_of abc_twice) div 2))) 12)"
  3381   moreover have "length (tm_of abc_twice) mod 2 = 0" by auto
  3381   moreover have "length (tm_of abc_twice) mod 2 = 0" by auto
  3382   moreover have "length (tm_of abc_fourtimes) mod 2 = 0" by auto
  3382   moreover have "length (tm_of abc_fourtimes) mod 2 = 0" by auto
  3383   ultimately have "list_all (\<lambda>(acn, st). (st \<le> (60 + (length (tm_of abc_twice) + length (tm_of abc_fourtimes))) div 2)) 
  3383   ultimately have "list_all (\<lambda>(acn, st). (st \<le> (60 + (length (tm_of abc_twice) + length (tm_of abc_fourtimes))) div 2)) 
  3384     (shift (turing_basic.adjust t_twice_compile) 12)"
  3384     (shift (Turing.adjust t_twice_compile) 12)"
  3385   proof(auto simp: mod_ex1)
  3385   proof(auto simp: mod_ex1)
  3386     fix q qa
  3386     fix q qa
  3387     assume h: "length (tm_of abc_twice) = 2 * q" "length (tm_of abc_fourtimes) = 2 * qa"
  3387     assume h: "length (tm_of abc_twice) = 2 * q" "length (tm_of abc_fourtimes) = 2 * qa"
  3388     hence "list_all (\<lambda>(acn, st). st \<le> (18 + (q + qa)) + 12) (shift (turing_basic.adjust t_twice_compile) 12)"
  3388     hence "list_all (\<lambda>(acn, st). st \<le> (18 + (q + qa)) + 12) (shift (Turing.adjust t_twice_compile) 12)"
  3389     proof(rule_tac tm_wf_shift t_twice_compile_def)
  3389     proof(rule_tac tm_wf_shift t_twice_compile_def)
  3390       have "list_all (\<lambda>(acn, st). st \<le> Suc (length t_twice_compile div 2)) (adjust t_twice_compile)"
  3390       have "list_all (\<lambda>(acn, st). st \<le> Suc (length t_twice_compile div 2)) (adjust t_twice_compile)"
  3391         by(rule_tac tm_wf_change_termi, auto)
  3391         by(rule_tac tm_wf_change_termi, auto)
  3392       thus "list_all (\<lambda>(acn, st). st \<le> 18 + (q + qa)) (turing_basic.adjust t_twice_compile)"
  3392       thus "list_all (\<lambda>(acn, st). st \<le> 18 + (q + qa)) (Turing.adjust t_twice_compile)"
  3393         using h
  3393         using h
  3394         apply(simp add: t_twice_compile_def, auto simp: List.list_all_length)
  3394         apply(simp add: t_twice_compile_def, auto simp: List.list_all_length)
  3395         done
  3395         done
  3396     qed
  3396     qed
  3397     thus "list_all (\<lambda>(acn, st). st \<le> 30 + (q + qa)) (shift (turing_basic.adjust t_twice_compile) 12)"
  3397     thus "list_all (\<lambda>(acn, st). st \<le> 30 + (q + qa)) (shift (Turing.adjust t_twice_compile) 12)"
  3398       by simp
  3398       by simp
  3399   qed
  3399   qed
  3400   thus "b \<le> (60 + (length (tm_of abc_twice) + length (tm_of abc_fourtimes))) div 2"
  3400   thus "b \<le> (60 + (length (tm_of abc_twice) + length (tm_of abc_fourtimes))) div 2"
  3401     using g
  3401     using g
  3402     apply(auto simp:t_twice_compile_def)
  3402     apply(auto simp:t_twice_compile_def)
  3403     apply(simp add: Ball_set[THEN sym])
  3403     apply(simp add: Ball_set[THEN sym])
  3404     apply(erule_tac x = "(a, b)" in ballE, simp, simp)
  3404     apply(erule_tac x = "(a, b)" in ballE, simp, simp)
  3405     done
  3405     done
  3406 qed 
  3406 qed 
  3407 
  3407 
  3408 lemma [elim]: "(a, b) \<in> set (shift (turing_basic.adjust t_fourtimes_compile) (t_twice_len + 13)) 
  3408 lemma [elim]: "(a, b) \<in> set (shift (Turing.adjust t_fourtimes_compile) (t_twice_len + 13)) 
  3409   \<Longrightarrow> b \<le> (28 + (length t_twice_compile + length t_fourtimes_compile)) div 2"
  3409   \<Longrightarrow> b \<le> (28 + (length t_twice_compile + length t_fourtimes_compile)) div 2"
  3410 apply(simp add: t_twice_compile_def t_fourtimes_compile_def t_twice_len_def)
  3410 apply(simp add: t_twice_compile_def t_fourtimes_compile_def t_twice_len_def)
  3411 proof -
  3411 proof -
  3412   assume g: "(a, b) \<in> set (shift (turing_basic.adjust (tm_of abc_fourtimes @ shift (mopup (Suc 0))
  3412   assume g: "(a, b) \<in> set (shift (Turing.adjust (tm_of abc_fourtimes @ shift (mopup (Suc 0))
  3413     (length (tm_of abc_fourtimes) div 2))) (length t_twice div 2 + 13))"
  3413     (length (tm_of abc_fourtimes) div 2))) (length t_twice div 2 + 13))"
  3414   moreover have "length (tm_of abc_twice) mod 2 = 0" by auto
  3414   moreover have "length (tm_of abc_twice) mod 2 = 0" by auto
  3415   moreover have "length (tm_of abc_fourtimes) mod 2 = 0" by auto
  3415   moreover have "length (tm_of abc_fourtimes) mod 2 = 0" by auto
  3416   ultimately have "list_all (\<lambda>(acn, st). (st \<le> (60 + (length (tm_of abc_twice) + length (tm_of abc_fourtimes))) div 2)) 
  3416   ultimately have "list_all (\<lambda>(acn, st). (st \<le> (60 + (length (tm_of abc_twice) + length (tm_of abc_fourtimes))) div 2)) 
  3417     (shift (turing_basic.adjust (tm_of abc_fourtimes @ shift (mopup (Suc 0))
  3417     (shift (Turing.adjust (tm_of abc_fourtimes @ shift (mopup (Suc 0))
  3418     (length (tm_of abc_fourtimes) div 2))) (length t_twice div 2 + 13))"
  3418     (length (tm_of abc_fourtimes) div 2))) (length t_twice div 2 + 13))"
  3419   proof(auto simp: mod_ex1 t_twice_def t_twice_compile_def)
  3419   proof(auto simp: mod_ex1 t_twice_def t_twice_compile_def)
  3420     fix q qa
  3420     fix q qa
  3421     assume h: "length (tm_of abc_twice) = 2 * q" "length (tm_of abc_fourtimes) = 2 * qa"
  3421     assume h: "length (tm_of abc_twice) = 2 * q" "length (tm_of abc_fourtimes) = 2 * qa"
  3422     hence "list_all (\<lambda>(acn, st). st \<le> (9 + qa + (21 + q)))
  3422     hence "list_all (\<lambda>(acn, st). st \<le> (9 + qa + (21 + q)))
  3423       (shift (turing_basic.adjust (tm_of abc_fourtimes @ shift (mopup (Suc 0)) qa)) (21 + q))"
  3423       (shift (Turing.adjust (tm_of abc_fourtimes @ shift (mopup (Suc 0)) qa)) (21 + q))"
  3424     proof(rule_tac tm_wf_shift t_twice_compile_def)
  3424     proof(rule_tac tm_wf_shift t_twice_compile_def)
  3425       have "list_all (\<lambda>(acn, st). st \<le> Suc (length (tm_of abc_fourtimes @ shift 
  3425       have "list_all (\<lambda>(acn, st). st \<le> Suc (length (tm_of abc_fourtimes @ shift 
  3426         (mopup (Suc 0)) qa) div 2)) (adjust (tm_of abc_fourtimes @ shift (mopup (Suc 0)) qa))"
  3426         (mopup (Suc 0)) qa) div 2)) (adjust (tm_of abc_fourtimes @ shift (mopup (Suc 0)) qa))"
  3427         apply(rule_tac tm_wf_change_termi)
  3427         apply(rule_tac tm_wf_change_termi)
  3428         using wf_fourtimes h
  3428         using wf_fourtimes h
  3429         apply(simp add: t_fourtimes_compile_def)
  3429         apply(simp add: t_fourtimes_compile_def)
  3430         done        
  3430         done        
  3431       thus "list_all (\<lambda>(acn, st). st \<le> 9 + qa) ((turing_basic.adjust (tm_of abc_fourtimes @ shift (mopup (Suc 0)) qa)))"
  3431       thus "list_all (\<lambda>(acn, st). st \<le> 9 + qa) ((Turing.adjust (tm_of abc_fourtimes @ shift (mopup (Suc 0)) qa)))"
  3432         using h
  3432         using h
  3433         apply(simp)
  3433         apply(simp)
  3434         done
  3434         done
  3435     qed
  3435     qed
  3436     thus "list_all (\<lambda>(acn, st). st \<le> 30 + (q + qa)) (shift (turing_basic.adjust (tm_of abc_fourtimes @ shift (mopup (Suc 0)) qa)) (21 + q))"
  3436     thus "list_all (\<lambda>(acn, st). st \<le> 30 + (q + qa)) (shift (Turing.adjust (tm_of abc_fourtimes @ shift (mopup (Suc 0)) qa)) (21 + q))"
  3437       apply(subgoal_tac "qa + q = q + qa")
  3437       apply(subgoal_tac "qa + q = q + qa")
  3438       apply(simp, simp)
  3438       apply(simp, simp)
  3439       done
  3439       done
  3440   qed
  3440   qed
  3441   thus "b \<le> (60 + (length (tm_of abc_twice) + length (tm_of abc_fourtimes))) div 2"
  3441   thus "b \<le> (60 + (length (tm_of abc_twice) + length (tm_of abc_fourtimes))) div 2"
  3510   qed
  3510   qed
  3511   thus "?thesis"
  3511   thus "?thesis"
  3512     apply(auto simp: Hoare_halt_def)
  3512     apply(auto simp: Hoare_halt_def)
  3513     apply(rule_tac x = n in exI)
  3513     apply(rule_tac x = n in exI)
  3514     apply(case_tac "(steps0 (Suc 0, [], <m # args>)
  3514     apply(case_tac "(steps0 (Suc 0, [], <m # args>)
  3515       (turing_basic.adjust t_wcode_prepare @ shift t_wcode_main (length t_wcode_prepare div 2)) n)")
  3515       (Turing.adjust t_wcode_prepare @ shift t_wcode_main (length t_wcode_prepare div 2)) n)")
  3516     apply(auto simp: tm_comp.simps)
  3516     apply(auto simp: tm_comp.simps)
  3517     done
  3517     done
  3518 qed
  3518 qed
  3519 
  3519 
  3520 definition tinres :: "cell list \<Rightarrow> cell list \<Rightarrow> bool"
  3520 definition tinres :: "cell list \<Rightarrow> cell list \<Rightarrow> bool"
  3917 
  3917 
  3918 definition wadjust_le :: "((nat \<times> config) \<times> nat \<times> config) set"
  3918 definition wadjust_le :: "((nat \<times> config) \<times> nat \<times> config) set"
  3919   where "wadjust_le \<equiv> (inv_image lex_square wadjust_measure)"
  3919   where "wadjust_le \<equiv> (inv_image lex_square wadjust_measure)"
  3920 
  3920 
  3921 lemma [intro]: "wf lex_square"
  3921 lemma [intro]: "wf lex_square"
  3922 by(auto intro:wf_lex_prod simp: abacus.lex_pair_def lex_square_def 
  3922 by(auto intro:wf_lex_prod simp: Abacus.lex_pair_def lex_square_def 
  3923   abacus.lex_triple_def)
  3923   Abacus.lex_triple_def)
  3924 
  3924 
  3925 lemma wf_wadjust_le[intro]: "wf wadjust_le"
  3925 lemma wf_wadjust_le[intro]: "wf wadjust_le"
  3926 by(auto intro:wf_inv_image simp: wadjust_le_def
  3926 by(auto intro:wf_inv_image simp: wadjust_le_def
  3927            abacus.lex_triple_def abacus.lex_pair_def)
  3927            Abacus.lex_triple_def Abacus.lex_pair_def)
  3928 
  3928 
  3929 lemma [simp]: "wadjust_start m rs (c, []) = False"
  3929 lemma [simp]: "wadjust_start m rs (c, []) = False"
  3930 apply(auto simp: wadjust_start.simps)
  3930 apply(auto simp: wadjust_start.simps)
  3931 done
  3931 done
  3932 
  3932 
  4516       apply(simp add: step.simps)
  4516       apply(simp add: step.simps)
  4517       apply(case_tac d, case_tac [2] aa, simp_all)
  4517       apply(case_tac d, case_tac [2] aa, simp_all)
  4518       apply(simp_all only: wadjust_inv.simps split: if_splits)
  4518       apply(simp_all only: wadjust_inv.simps split: if_splits)
  4519       apply(simp_all)
  4519       apply(simp_all)
  4520       apply(simp_all add: wadjust_inv.simps wadjust_le_def
  4520       apply(simp_all add: wadjust_inv.simps wadjust_le_def
  4521             abacus.lex_triple_def abacus.lex_pair_def lex_square_def  split: if_splits)
  4521             Abacus.lex_triple_def Abacus.lex_pair_def lex_square_def  split: if_splits)
  4522       done
  4522       done
  4523   next
  4523   next
  4524     show "?Q (?f 0)"
  4524     show "?Q (?f 0)"
  4525       apply(simp add: steps.simps wadjust_inv.simps wadjust_start.simps, auto)
  4525       apply(simp add: steps.simps wadjust_inv.simps wadjust_start.simps, auto)
  4526       done
  4526       done
  4665   where
  4665   where
  4666   "UTM_pre = t_wcode |+| t_utm"
  4666   "UTM_pre = t_wcode |+| t_utm"
  4667 
  4667 
  4668 (*
  4668 (*
  4669 lemma F_abc_halt_eq:
  4669 lemma F_abc_halt_eq:
  4670   "\<lbrakk>turing_basic.t_correct tp; 
  4670   "\<lbrakk>Turing.t_correct tp; 
  4671     length lm = k;
  4671     length lm = k;
  4672     steps (Suc 0, Bk\<up>(l), <lm>) tp stp = (0, Bk\<up>(m), Oc\<up>(rs)@Bk\<up>(n));
  4672     steps (Suc 0, Bk\<up>(l), <lm>) tp stp = (0, Bk\<up>(m), Oc\<up>(rs)@Bk\<up>(n));
  4673     rs > 0\<rbrakk>
  4673     rs > 0\<rbrakk>
  4674     \<Longrightarrow> \<exists> stp m. abc_steps_l (0, [code tp, bl2wc (<lm>)]) (F_aprog) stp =
  4674     \<Longrightarrow> \<exists> stp m. abc_steps_l (0, [code tp, bl2wc (<lm>)]) (F_aprog) stp =
  4675                        (length (F_aprog), code tp # bl2wc (<lm>) # (rs - 1) # 0\<up>(m))"
  4675                        (length (F_aprog), code tp # bl2wc (<lm>) # (rs - 1) # 0\<up>(m))"
  4699 done
  4699 done
  4700 
  4700 
  4701 declare tape_of_nl_abv_cons[simp del]
  4701 declare tape_of_nl_abv_cons[simp del]
  4702 
  4702 
  4703 lemma t_utm_halt_eq': 
  4703 lemma t_utm_halt_eq': 
  4704   "\<lbrakk>turing_basic.t_correct tp;
  4704   "\<lbrakk>Turing.t_correct tp;
  4705    0 < rs;
  4705    0 < rs;
  4706   steps (Suc 0, Bk\<up>(l), <lm::nat list>) tp stp = (0, Bk\<up>(m), Oc\<up>(rs)@Bk\<up>(n))\<rbrakk>
  4706   steps (Suc 0, Bk\<up>(l), <lm::nat list>) tp stp = (0, Bk\<up>(m), Oc\<up>(rs)@Bk\<up>(n))\<rbrakk>
  4707   \<Longrightarrow>  \<exists>stp m n. steps (Suc 0, [Bk, Bk], <[code tp, bl2wc (<lm>)]>) t_utm stp = 
  4707   \<Longrightarrow>  \<exists>stp m n. steps (Suc 0, [Bk, Bk], <[code tp, bl2wc (<lm>)]>) t_utm stp = 
  4708                                                 (0, Bk\<up>(m), Oc\<up>(rs) @ Bk\<up>(n))"
  4708                                                 (0, Bk\<up>(m), Oc\<up>(rs) @ Bk\<up>(n))"
  4709 apply(drule_tac  l = l in F_abc_halt_eq, simp, simp, simp)
  4709 apply(drule_tac  l = l in F_abc_halt_eq, simp, simp, simp)
  4727 apply(rule_tac x = "n - na" in exI, simp)
  4727 apply(rule_tac x = "n - na" in exI, simp)
  4728 done
  4728 done
  4729 *)
  4729 *)
  4730 (*
  4730 (*
  4731 lemma t_utm_halt_eq'': 
  4731 lemma t_utm_halt_eq'': 
  4732   "\<lbrakk>turing_basic.t_correct tp;
  4732   "\<lbrakk>Turing.t_correct tp;
  4733    0 < rs;
  4733    0 < rs;
  4734    steps (Suc 0, Bk\<up>(l), <lm::nat list>) tp stp = (0, Bk\<up>(m), Oc\<up>(rs)@Bk\<up>(n))\<rbrakk>
  4734    steps (Suc 0, Bk\<up>(l), <lm::nat list>) tp stp = (0, Bk\<up>(m), Oc\<up>(rs)@Bk\<up>(n))\<rbrakk>
  4735   \<Longrightarrow>  \<exists>stp m n. steps (Suc 0, [Bk, Bk], <[code tp, bl2wc (<lm>)]> @ Bk\<up>(i)) t_utm stp = 
  4735   \<Longrightarrow>  \<exists>stp m n. steps (Suc 0, [Bk, Bk], <[code tp, bl2wc (<lm>)]> @ Bk\<up>(i)) t_utm stp = 
  4736                                                 (0, Bk\<up>(m), Oc\<up>(rs) @ Bk\<up>(n))"
  4736                                                 (0, Bk\<up>(m), Oc\<up>(rs) @ Bk\<up>(n))"
  4737 apply(drule_tac t_utm_halt_eq', simp_all)
  4737 apply(drule_tac t_utm_halt_eq', simp_all)