thys/UTM.thy
changeset 190 f1ecb4a68a54
parent 170 eccd79a974ae
child 229 d8e6f0798e23
equal deleted inserted replaced
189:5974111de158 190:f1ecb4a68a54
   541 where
   541 where
   542   "t_twice_compile= (tm_of abc_twice @ (shift (mopup 1) (length (tm_of abc_twice) div 2)))"
   542   "t_twice_compile= (tm_of abc_twice @ (shift (mopup 1) (length (tm_of abc_twice) div 2)))"
   543 
   543 
   544 definition t_twice :: "instr list"
   544 definition t_twice :: "instr list"
   545   where
   545   where
   546   "t_twice = adjust t_twice_compile"
   546   "t_twice = adjust0 t_twice_compile"
   547 
   547 
   548 definition t_fourtimes_compile :: "instr list"
   548 definition t_fourtimes_compile :: "instr list"
   549 where
   549 where
   550   "t_fourtimes_compile= (tm_of abc_fourtimes @ (shift (mopup 1) (length (tm_of abc_fourtimes) div 2)))"
   550   "t_fourtimes_compile= (tm_of abc_fourtimes @ (shift (mopup 1) (length (tm_of abc_fourtimes) div 2)))"
   551 
   551 
   552 definition t_fourtimes :: "instr list"
   552 definition t_fourtimes :: "instr list"
   553   where
   553   where
   554   "t_fourtimes = adjust t_fourtimes_compile"
   554   "t_fourtimes = adjust0 t_fourtimes_compile"
   555 
   555 
   556 definition t_twice_len :: "nat"
   556 definition t_twice_len :: "nat"
   557   where
   557   where
   558   "t_twice_len = length t_twice div 2"
   558   "t_twice_len = length t_twice div 2"
   559 
   559 
  1264 
  1264 
  1265 declare adjust.simps[simp]
  1265 declare adjust.simps[simp]
  1266 
  1266 
  1267 lemma adjust_fetch0: 
  1267 lemma adjust_fetch0: 
  1268   "\<lbrakk>0 < a; a \<le> length ap div 2;  fetch ap a b = (aa, 0)\<rbrakk>
  1268   "\<lbrakk>0 < a; a \<le> length ap div 2;  fetch ap a b = (aa, 0)\<rbrakk>
  1269   \<Longrightarrow> fetch (adjust ap) a b = (aa, Suc (length ap div 2))"
  1269   \<Longrightarrow> fetch (adjust0 ap) a b = (aa, Suc (length ap div 2))"
  1270 apply(case_tac b, auto simp: fetch.simps nth_of.simps nth_map
  1270 apply(case_tac b, auto simp: fetch.simps nth_of.simps nth_map
  1271                        split: if_splits)
  1271                        split: if_splits)
  1272 apply(case_tac [!] a, auto simp: fetch.simps nth_of.simps)
  1272 apply(case_tac [!] a, auto simp: fetch.simps nth_of.simps)
  1273 done
  1273 done
  1274 
  1274 
  1275 lemma adjust_fetch_norm: 
  1275 lemma adjust_fetch_norm: 
  1276   "\<lbrakk>st > 0;  st \<le> length tp div 2; fetch ap st b = (aa, ns); ns \<noteq> 0\<rbrakk>
  1276   "\<lbrakk>st > 0;  st \<le> length tp div 2; fetch ap st b = (aa, ns); ns \<noteq> 0\<rbrakk>
  1277  \<Longrightarrow>  fetch (Turing.adjust ap) st b = (aa, ns)"
  1277  \<Longrightarrow>  fetch (adjust0 ap) st b = (aa, ns)"
  1278  apply(case_tac b, auto simp: fetch.simps nth_of.simps nth_map
  1278  apply(case_tac b, auto simp: fetch.simps nth_of.simps nth_map
  1279                        split: if_splits)
  1279                        split: if_splits)
  1280 apply(case_tac [!] st, auto simp: fetch.simps nth_of.simps)
  1280 apply(case_tac [!] st, auto simp: fetch.simps nth_of.simps)
  1281 done
  1281 done
  1282 
  1282 
  1284 
  1284 
  1285 lemma adjust_step_eq: 
  1285 lemma adjust_step_eq: 
  1286   assumes exec: "step0 (st,l,r) ap = (st', l', r')"
  1286   assumes exec: "step0 (st,l,r) ap = (st', l', r')"
  1287   and wf_tm: "tm_wf (ap, 0)"
  1287   and wf_tm: "tm_wf (ap, 0)"
  1288   and notfinal: "st' > 0"
  1288   and notfinal: "st' > 0"
  1289   shows "step0 (st, l, r) (adjust ap) = (st', l', r')"
  1289   shows "step0 (st, l, r) (adjust0 ap) = (st', l', r')"
  1290   using assms
  1290   using assms
  1291 proof -
  1291 proof -
  1292   have "st > 0"
  1292   have "st > 0"
  1293     using assms
  1293     using assms
  1294     by(case_tac st, simp_all add: step.simps fetch.simps)
  1294     by(case_tac st, simp_all add: step.simps fetch.simps)
  1298     apply(case_tac st, auto simp: step.simps fetch.simps)
  1298     apply(case_tac st, auto simp: step.simps fetch.simps)
  1299     apply(case_tac "read r", simp_all add: fetch.simps 
  1299     apply(case_tac "read r", simp_all add: fetch.simps 
  1300       nth_of.simps adjust.simps tm_wf.simps split: if_splits)
  1300       nth_of.simps adjust.simps tm_wf.simps split: if_splits)
  1301     apply(auto simp: mod_ex2)
  1301     apply(auto simp: mod_ex2)
  1302     done    
  1302     done    
  1303   ultimately have "fetch (adjust ap) st (read r) = fetch ap st (read r)"
  1303   ultimately have "fetch (adjust0 ap) st (read r) = fetch ap st (read r)"
  1304     using assms
  1304     using assms
  1305     apply(case_tac "fetch ap st (read r)")
  1305     apply(case_tac "fetch ap st (read r)")
  1306     apply(drule_tac adjust_fetch_norm, simp_all)
  1306     apply(drule_tac adjust_fetch_norm, simp_all)
  1307     apply(simp add: step.simps)
  1307     apply(simp add: step.simps)
  1308     done
  1308     done
  1315 
  1315 
  1316 lemma adjust_steps_eq: 
  1316 lemma adjust_steps_eq: 
  1317   assumes exec: "steps0 (st,l,r) ap stp = (st', l', r')"
  1317   assumes exec: "steps0 (st,l,r) ap stp = (st', l', r')"
  1318   and wf_tm: "tm_wf (ap, 0)"
  1318   and wf_tm: "tm_wf (ap, 0)"
  1319   and notfinal: "st' > 0"
  1319   and notfinal: "st' > 0"
  1320   shows "steps0 (st, l, r) (adjust ap) stp = (st', l', r')"
  1320   shows "steps0 (st, l, r) (adjust0 ap) stp = (st', l', r')"
  1321   using exec notfinal
  1321   using exec notfinal
  1322 proof(induct stp arbitrary: st' l' r')
  1322 proof(induct stp arbitrary: st' l' r')
  1323   case 0
  1323   case 0
  1324   thus "?case"
  1324   thus "?case"
  1325     by(simp add: steps.simps)
  1325     by(simp add: steps.simps)
  1326 next
  1326 next
  1327   case (Suc stp st' l' r')
  1327   case (Suc stp st' l' r')
  1328   have ind: "\<And>st' l' r'. \<lbrakk>steps0 (st, l, r) ap stp = (st', l', r'); 0 < st'\<rbrakk> 
  1328   have ind: "\<And>st' l' r'. \<lbrakk>steps0 (st, l, r) ap stp = (st', l', r'); 0 < st'\<rbrakk> 
  1329     \<Longrightarrow> steps0 (st, l, r) (Turing.adjust ap) stp = (st', l', r')" by fact
  1329     \<Longrightarrow> steps0 (st, l, r) (adjust0 ap) stp = (st', l', r')" by fact
  1330   have h: "steps0 (st, l, r) ap (Suc stp) = (st', l', r')" by fact
  1330   have h: "steps0 (st, l, r) ap (Suc stp) = (st', l', r')" by fact
  1331   have g:   "0 < st'" by fact
  1331   have g:   "0 < st'" by fact
  1332   obtain st'' l'' r'' where a: "steps0 (st, l, r) ap stp = (st'', l'', r'')"
  1332   obtain st'' l'' r'' where a: "steps0 (st, l, r) ap stp = (st'', l'', r'')"
  1333     by (metis prod_cases3)
  1333     by (metis prod_cases3)
  1334   hence c:"0 < st''"
  1334   hence c:"0 < st''"
  1335     using h g
  1335     using h g
  1336     apply(simp add: step_red)
  1336     apply(simp add: step_red)
  1337     apply(case_tac st'', auto)
  1337     apply(case_tac st'', auto)
  1338     done
  1338     done
  1339   hence b: "steps0 (st, l, r) (Turing.adjust ap) stp = (st'', l'', r'')"
  1339   hence b: "steps0 (st, l, r) (adjust0 ap) stp = (st'', l'', r'')"
  1340     using a
  1340     using a
  1341     by(rule_tac ind, simp_all)
  1341     by(rule_tac ind, simp_all)
  1342   thus "?case"
  1342   thus "?case"
  1343     using assms a b h g
  1343     using assms a b h g
  1344     apply(simp add: step_red) 
  1344     apply(simp add: step_red) 
  1347 qed 
  1347 qed 
  1348 
  1348 
  1349 lemma adjust_halt_eq:
  1349 lemma adjust_halt_eq:
  1350   assumes exec: "steps0 (1, l, r) ap stp = (0, l', r')"
  1350   assumes exec: "steps0 (1, l, r) ap stp = (0, l', r')"
  1351   and tm_wf: "tm_wf (ap, 0)" 
  1351   and tm_wf: "tm_wf (ap, 0)" 
  1352   shows "\<exists> stp. steps0 (Suc 0, l, r) (adjust ap) stp = 
  1352   shows "\<exists> stp. steps0 (Suc 0, l, r) (adjust0 ap) stp = 
  1353         (Suc (length ap div 2), l', r')"
  1353         (Suc (length ap div 2), l', r')"
  1354 proof -
  1354 proof -
  1355   have "\<exists> stp. \<not> is_final (steps0 (1, l, r) ap stp) \<and> (steps0 (1, l, r) ap (Suc stp) = (0, l', r'))"
  1355   have "\<exists> stp. \<not> is_final (steps0 (1, l, r) ap stp) \<and> (steps0 (1, l, r) ap (Suc stp) = (0, l', r'))"
  1356     using exec
  1356     using exec
  1357     by(erule_tac before_final)
  1357     by(erule_tac before_final)
  1358   then obtain stpa where a: 
  1358   then obtain stpa where a: 
  1359     "\<not> is_final (steps0 (1, l, r) ap stpa) \<and> (steps0 (1, l, r) ap (Suc stpa) = (0, l', r'))" ..
  1359     "\<not> is_final (steps0 (1, l, r) ap stpa) \<and> (steps0 (1, l, r) ap (Suc stpa) = (0, l', r'))" ..
  1360   obtain sa la ra where b:"steps0 (1, l, r) ap stpa = (sa, la, ra)"  by (metis prod_cases3)
  1360   obtain sa la ra where b:"steps0 (1, l, r) ap stpa = (sa, la, ra)"  by (metis prod_cases3)
  1361   hence c: "steps0 (Suc 0, l, r) (adjust ap) stpa = (sa, la, ra)"
  1361   hence c: "steps0 (Suc 0, l, r) (adjust0 ap) stpa = (sa, la, ra)"
  1362     using assms a
  1362     using assms a
  1363     apply(rule_tac adjust_steps_eq, simp_all)
  1363     apply(rule_tac adjust_steps_eq, simp_all)
  1364     done
  1364     done
  1365   have d: "sa \<le> length ap div 2"
  1365   have d: "sa \<le> length ap div 2"
  1366     using steps_in_range[of  "(l, r)" ap stpa] a tm_wf b
  1366     using steps_in_range[of  "(l, r)" ap stpa] a tm_wf b
  1369     by (metis prod.exhaust)
  1369     by (metis prod.exhaust)
  1370   hence f: "ns = 0"
  1370   hence f: "ns = 0"
  1371     using b a
  1371     using b a
  1372     apply(simp add: step_red step.simps)
  1372     apply(simp add: step_red step.simps)
  1373     done
  1373     done
  1374   have k: "fetch (adjust ap) sa (read ra) = (ac, Suc (length ap div 2))"
  1374   have k: "fetch (adjust0 ap) sa (read ra) = (ac, Suc (length ap div 2))"
  1375     using a b c d e f
  1375     using a b c d e f
  1376     apply(rule_tac adjust_fetch0, simp_all)
  1376     apply(rule_tac adjust_fetch0, simp_all)
  1377     done
  1377     done
  1378   from a b e f k and c show "?thesis"
  1378   from a b e f k and c show "?thesis"
  1379     apply(rule_tac x = "Suc stpa" in exI)
  1379     apply(rule_tac x = "Suc stpa" in exI)
  1400     by(rule_tac t_twice_correct)
  1400     by(rule_tac t_twice_correct)
  1401   then obtain stp ln rn where " steps0 (Suc 0, Bk # Bk # ires, Oc\<up>(Suc rs) @ Bk\<up>(n)) 
  1401   then obtain stp ln rn where " steps0 (Suc 0, Bk # Bk # ires, Oc\<up>(Suc rs) @ Bk\<up>(n)) 
  1402     (tm_of abc_twice @ shift (mopup (Suc 0)) ((length (tm_of abc_twice) div 2))) stp =
  1402     (tm_of abc_twice @ shift (mopup (Suc 0)) ((length (tm_of abc_twice) div 2))) stp =
  1403     (0, Bk\<up>(ln) @ Bk # Bk # ires, Oc\<up>(Suc (2 * rs)) @ Bk\<up>(rn))" by blast
  1403     (0, Bk\<up>(ln) @ Bk # Bk # ires, Oc\<up>(Suc (2 * rs)) @ Bk\<up>(rn))" by blast
  1404   hence "\<exists> stp. steps0 (Suc 0, Bk # Bk # ires, Oc\<up>(Suc rs) @ Bk\<up>(n))
  1404   hence "\<exists> stp. steps0 (Suc 0, Bk # Bk # ires, Oc\<up>(Suc rs) @ Bk\<up>(n))
  1405     (adjust t_twice_compile) stp
  1405     (adjust0 t_twice_compile) stp
  1406      = (Suc (length t_twice_compile div 2), Bk\<up>(ln) @ Bk # Bk # ires, Oc\<up>(Suc (2 * rs)) @ Bk\<up>(rn))"
  1406      = (Suc (length t_twice_compile div 2), Bk\<up>(ln) @ Bk # Bk # ires, Oc\<up>(Suc (2 * rs)) @ Bk\<up>(rn))"
  1407     apply(rule_tac stp = stp in adjust_halt_eq)
  1407     apply(rule_tac stp = stp in adjust_halt_eq)
  1408     apply(simp add: t_twice_compile_def, auto)
  1408     apply(simp add: t_twice_compile_def, auto)
  1409     done
  1409     done
  1410   then obtain stpb where 
  1410   then obtain stpb where 
  1411     "steps0 (Suc 0, Bk # Bk # ires, Oc\<up>(Suc rs) @ Bk\<up>(n))
  1411     "steps0 (Suc 0, Bk # Bk # ires, Oc\<up>(Suc rs) @ Bk\<up>(n))
  1412     (adjust t_twice_compile) stpb
  1412     (adjust0 t_twice_compile) stpb
  1413      = (Suc (length t_twice_compile div 2), Bk\<up>(ln) @ Bk # Bk # ires, Oc\<up>(Suc (2 * rs)) @ Bk\<up>(rn))" ..
  1413      = (Suc (length t_twice_compile div 2), Bk\<up>(ln) @ Bk # Bk # ires, Oc\<up>(Suc (2 * rs)) @ Bk\<up>(rn))" ..
  1414   thus "?thesis"
  1414   thus "?thesis"
  1415     apply(simp add: t_twice_def t_twice_len_def)
  1415     apply(simp add: t_twice_def t_twice_len_def)
  1416     by metis
  1416     by metis
  1417 qed
  1417 qed
  2077   then obtain stp ln rn where 
  2077   then obtain stp ln rn where 
  2078     "steps0 (Suc 0, Bk # Bk # ires, Oc\<up>(Suc rs) @ Bk\<up>(n)) 
  2078     "steps0 (Suc 0, Bk # Bk # ires, Oc\<up>(Suc rs) @ Bk\<up>(n)) 
  2079     (tm_of abc_fourtimes @ shift (mopup 1) ((length (tm_of abc_fourtimes) div 2))) stp =
  2079     (tm_of abc_fourtimes @ shift (mopup 1) ((length (tm_of abc_fourtimes) div 2))) stp =
  2080     (0, Bk\<up>(ln) @ Bk # Bk # ires, Oc\<up>(Suc (4 * rs)) @ Bk\<up>(rn))" by blast
  2080     (0, Bk\<up>(ln) @ Bk # Bk # ires, Oc\<up>(Suc (4 * rs)) @ Bk\<up>(rn))" by blast
  2081   hence "\<exists> stp. steps0 (Suc 0, Bk # Bk # ires, Oc\<up>(Suc rs) @ Bk\<up>(n))
  2081   hence "\<exists> stp. steps0 (Suc 0, Bk # Bk # ires, Oc\<up>(Suc rs) @ Bk\<up>(n))
  2082     (adjust t_fourtimes_compile) stp
  2082     (adjust0 t_fourtimes_compile) stp
  2083      = (Suc (length t_fourtimes_compile div 2), Bk\<up>(ln) @ Bk # Bk # ires, Oc\<up>(Suc (4 * rs)) @ Bk\<up>(rn))"
  2083      = (Suc (length t_fourtimes_compile div 2), Bk\<up>(ln) @ Bk # Bk # ires, Oc\<up>(Suc (4 * rs)) @ Bk\<up>(rn))"
  2084     apply(rule_tac stp = stp in adjust_halt_eq)
  2084     apply(rule_tac stp = stp in adjust_halt_eq)
  2085     apply(simp add: t_fourtimes_compile_def, auto)
  2085     apply(simp add: t_fourtimes_compile_def, auto)
  2086     done
  2086     done
  2087   then obtain stpb where 
  2087   then obtain stpb where 
  2088     "steps0 (Suc 0, Bk # Bk # ires, Oc\<up>(Suc rs) @ Bk\<up>(n))
  2088     "steps0 (Suc 0, Bk # Bk # ires, Oc\<up>(Suc rs) @ Bk\<up>(n))
  2089     (adjust t_fourtimes_compile) stpb
  2089     (adjust0 t_fourtimes_compile) stpb
  2090      = (Suc (length t_fourtimes_compile div 2), Bk\<up>(ln) @ Bk # Bk # ires, Oc\<up>(Suc (4 * rs)) @ Bk\<up>(rn))" ..
  2090      = (Suc (length t_fourtimes_compile div 2), Bk\<up>(ln) @ Bk # Bk # ires, Oc\<up>(Suc (4 * rs)) @ Bk\<up>(rn))" ..
  2091   thus "?thesis"
  2091   thus "?thesis"
  2092     apply(simp add: t_fourtimes_def t_fourtimes_len_def)
  2092     apply(simp add: t_fourtimes_def t_fourtimes_len_def)
  2093     by metis
  2093     by metis
  2094 qed
  2094 qed
  3354 done
  3354 done
  3355 
  3355 
  3356 
  3356 
  3357 
  3357 
  3358 lemma tm_wf_change_termi: "tm_wf (tp, 0) \<Longrightarrow> 
  3358 lemma tm_wf_change_termi: "tm_wf (tp, 0) \<Longrightarrow> 
  3359       list_all (\<lambda>(acn, st). (st \<le> Suc (length tp div 2))) (adjust tp)"
  3359       list_all (\<lambda>(acn, st). (st \<le> Suc (length tp div 2))) (adjust0 tp)"
  3360 apply(auto simp: tm_wf.simps List.list_all_length)
  3360 apply(auto simp: tm_wf.simps List.list_all_length)
  3361 apply(case_tac "tp!n", auto simp: adjust.simps split: if_splits)
  3361 apply(case_tac "tp!n", auto simp: adjust.simps split: if_splits)
  3362 apply(erule_tac x = "(a, b)" in ballE, auto)
  3362 apply(erule_tac x = "(a, b)" in ballE, auto)
  3363 by (metis in_set_conv_nth)
  3363 by (metis in_set_conv_nth)
  3364 
  3364 
  3374 
  3374 
  3375 lemma [simp]: "length (mopup (Suc 0)) = 16"
  3375 lemma [simp]: "length (mopup (Suc 0)) = 16"
  3376 apply(auto simp: mopup.simps)
  3376 apply(auto simp: mopup.simps)
  3377 done
  3377 done
  3378 
  3378 
  3379 lemma [elim]: "(a, b) \<in> set (shift (Turing.adjust t_twice_compile) 12) \<Longrightarrow> 
  3379 lemma [elim]: "(a, b) \<in> set (shift (adjust0 t_twice_compile) 12) \<Longrightarrow> 
  3380   b \<le> (28 + (length t_twice_compile + length t_fourtimes_compile)) div 2"
  3380   b \<le> (28 + (length t_twice_compile + length t_fourtimes_compile)) div 2"
  3381 apply(simp add: t_twice_compile_def t_fourtimes_compile_def)
  3381 apply(simp add: t_twice_compile_def t_fourtimes_compile_def)
  3382 proof -
  3382 proof -
  3383   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)"
  3383   assume g: "(a, b)
       
  3384     \<in> set (shift
       
  3385             (adjust
       
  3386               (tm_of abc_twice @
       
  3387                shift (mopup (Suc 0)) (length (tm_of abc_twice) div 2))
       
  3388               (Suc ((length (tm_of abc_twice) + 16) div 2)))
       
  3389             12)"
  3384   moreover have "length (tm_of abc_twice) mod 2 = 0" by auto
  3390   moreover have "length (tm_of abc_twice) mod 2 = 0" by auto
  3385   moreover have "length (tm_of abc_fourtimes) mod 2 = 0" by auto
  3391   moreover have "length (tm_of abc_fourtimes) mod 2 = 0" by auto
  3386   ultimately have "list_all (\<lambda>(acn, st). (st \<le> (60 + (length (tm_of abc_twice) + length (tm_of abc_fourtimes))) div 2)) 
  3392   ultimately have "list_all (\<lambda>(acn, st). (st \<le> (60 + (length (tm_of abc_twice) + length (tm_of abc_fourtimes))) div 2)) 
  3387     (shift (Turing.adjust t_twice_compile) 12)"
  3393     (shift (adjust0 t_twice_compile) 12)"
  3388   proof(auto simp: mod_ex1)
  3394   proof(auto simp add: mod_ex1 del: adjust.simps)
  3389     fix q qa
  3395     fix q qa
  3390     assume h: "length (tm_of abc_twice) = 2 * q" "length (tm_of abc_fourtimes) = 2 * qa"
  3396     assume h: "length (tm_of abc_twice) = 2 * q" "length (tm_of abc_fourtimes) = 2 * qa"
  3391     hence "list_all (\<lambda>(acn, st). st \<le> (18 + (q + qa)) + 12) (shift (Turing.adjust t_twice_compile) 12)"
  3397     hence "list_all (\<lambda>(acn, st). st \<le> (18 + (q + qa)) + 12) (shift (adjust0 t_twice_compile) 12)"
  3392     proof(rule_tac tm_wf_shift t_twice_compile_def)
  3398     proof(rule_tac tm_wf_shift t_twice_compile_def)
  3393       have "list_all (\<lambda>(acn, st). st \<le> Suc (length t_twice_compile div 2)) (adjust t_twice_compile)"
  3399       have "list_all (\<lambda>(acn, st). st \<le> Suc (length t_twice_compile div 2)) (adjust0 t_twice_compile)"
  3394         by(rule_tac tm_wf_change_termi, auto)
  3400         by(rule_tac tm_wf_change_termi, auto)
  3395       thus "list_all (\<lambda>(acn, st). st \<le> 18 + (q + qa)) (Turing.adjust t_twice_compile)"
  3401       thus "list_all (\<lambda>(acn, st). st \<le> 18 + (q + qa)) (adjust0 t_twice_compile)"
  3396         using h
  3402         using h
  3397         apply(simp add: t_twice_compile_def, auto simp: List.list_all_length)
  3403         apply(simp add: t_twice_compile_def, auto simp: List.list_all_length)
  3398         done
  3404         done
  3399     qed
  3405     qed
  3400     thus "list_all (\<lambda>(acn, st). st \<le> 30 + (q + qa)) (shift (Turing.adjust t_twice_compile) 12)"
  3406     thus "list_all (\<lambda>(acn, st). st \<le> 30 + (q + qa))
       
  3407            (shift
       
  3408              (adjust t_twice_compile (Suc (length t_twice_compile div 2))) 12)"
  3401       by simp
  3409       by simp
  3402   qed
  3410   qed
  3403   thus "b \<le> (60 + (length (tm_of abc_twice) + length (tm_of abc_fourtimes))) div 2"
  3411   thus "b \<le> (60 + (length (tm_of abc_twice) + length (tm_of abc_fourtimes))) div 2"
  3404     using g
  3412     using g
  3405     apply(auto simp:t_twice_compile_def)
  3413     apply(auto simp:t_twice_compile_def)
  3406     apply(simp add: Ball_set[THEN sym])
  3414     apply(simp add: Ball_set[THEN sym])
  3407     apply(erule_tac x = "(a, b)" in ballE, simp, simp)
  3415     apply(erule_tac x = "(a, b)" in ballE, simp, simp)
  3408     done
  3416     done
  3409 qed 
  3417 qed 
  3410 
  3418 
  3411 lemma [elim]: "(a, b) \<in> set (shift (Turing.adjust t_fourtimes_compile) (t_twice_len + 13)) 
  3419 lemma [elim]: "(a, b) \<in> set (shift (adjust0 t_fourtimes_compile) (t_twice_len + 13)) 
  3412   \<Longrightarrow> b \<le> (28 + (length t_twice_compile + length t_fourtimes_compile)) div 2"
  3420   \<Longrightarrow> b \<le> (28 + (length t_twice_compile + length t_fourtimes_compile)) div 2"
  3413 apply(simp add: t_twice_compile_def t_fourtimes_compile_def t_twice_len_def)
  3421 apply(simp add: t_twice_compile_def t_fourtimes_compile_def t_twice_len_def)
  3414 proof -
  3422 proof -
  3415   assume g: "(a, b) \<in> set (shift (Turing.adjust (tm_of abc_fourtimes @ shift (mopup (Suc 0))
  3423   assume g: "(a, b)
  3416     (length (tm_of abc_fourtimes) div 2))) (length t_twice div 2 + 13))"
  3424     \<in> set (shift
       
  3425             (adjust
       
  3426               (tm_of abc_fourtimes @
       
  3427                shift (mopup (Suc 0)) (length (tm_of abc_fourtimes) div 2))
       
  3428               (Suc ((length (tm_of abc_fourtimes) + 16) div 2)))
       
  3429             (length t_twice div 2 + 13))"
  3417   moreover have "length (tm_of abc_twice) mod 2 = 0" by auto
  3430   moreover have "length (tm_of abc_twice) mod 2 = 0" by auto
  3418   moreover have "length (tm_of abc_fourtimes) mod 2 = 0" by auto
  3431   moreover have "length (tm_of abc_fourtimes) mod 2 = 0" by auto
  3419   ultimately have "list_all (\<lambda>(acn, st). (st \<le> (60 + (length (tm_of abc_twice) + length (tm_of abc_fourtimes))) div 2)) 
  3432   ultimately have "list_all (\<lambda>(acn, st). (st \<le> (60 + (length (tm_of abc_twice) + length (tm_of abc_fourtimes))) div 2)) 
  3420     (shift (Turing.adjust (tm_of abc_fourtimes @ shift (mopup (Suc 0))
  3433     (shift (adjust0 (tm_of abc_fourtimes @ shift (mopup (Suc 0))
  3421     (length (tm_of abc_fourtimes) div 2))) (length t_twice div 2 + 13))"
  3434     (length (tm_of abc_fourtimes) div 2))) (length t_twice div 2 + 13))"
  3422   proof(auto simp: mod_ex1 t_twice_def t_twice_compile_def)
  3435   proof(auto simp: mod_ex1 t_twice_def t_twice_compile_def)
  3423     fix q qa
  3436     fix q qa
  3424     assume h: "length (tm_of abc_twice) = 2 * q" "length (tm_of abc_fourtimes) = 2 * qa"
  3437     assume h: "length (tm_of abc_twice) = 2 * q" "length (tm_of abc_fourtimes) = 2 * qa"
  3425     hence "list_all (\<lambda>(acn, st). st \<le> (9 + qa + (21 + q)))
  3438     hence "list_all (\<lambda>(acn, st). st \<le> (9 + qa + (21 + q)))
  3426       (shift (Turing.adjust (tm_of abc_fourtimes @ shift (mopup (Suc 0)) qa)) (21 + q))"
  3439       (shift (adjust0 (tm_of abc_fourtimes @ shift (mopup (Suc 0)) qa)) (21 + q))"
  3427     proof(rule_tac tm_wf_shift t_twice_compile_def)
  3440     proof(rule_tac tm_wf_shift t_twice_compile_def)
  3428       have "list_all (\<lambda>(acn, st). st \<le> Suc (length (tm_of abc_fourtimes @ shift 
  3441       have "list_all (\<lambda>(acn, st). st \<le> Suc (length (tm_of abc_fourtimes @ shift 
  3429         (mopup (Suc 0)) qa) div 2)) (adjust (tm_of abc_fourtimes @ shift (mopup (Suc 0)) qa))"
  3442         (mopup (Suc 0)) qa) div 2)) (adjust0 (tm_of abc_fourtimes @ shift (mopup (Suc 0)) qa))"
  3430         apply(rule_tac tm_wf_change_termi)
  3443         apply(rule_tac tm_wf_change_termi)
  3431         using wf_fourtimes h
  3444         using wf_fourtimes h
  3432         apply(simp add: t_fourtimes_compile_def)
  3445         apply(simp add: t_fourtimes_compile_def)
  3433         done        
  3446         done
  3434       thus "list_all (\<lambda>(acn, st). st \<le> 9 + qa) ((Turing.adjust (tm_of abc_fourtimes @ shift (mopup (Suc 0)) qa)))"
  3447       thus "list_all (\<lambda>(acn, st). st \<le> 9 + qa)
       
  3448         (adjust (tm_of abc_fourtimes @ shift (mopup (Suc 0)) qa)
       
  3449           (Suc (length (tm_of abc_fourtimes @ shift (mopup (Suc 0)) qa) div
       
  3450                 2)))"
  3435         using h
  3451         using h
  3436         apply(simp)
  3452         apply(simp)
  3437         done
  3453         done
  3438     qed
  3454     qed
  3439     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))"
  3455     thus "list_all (\<lambda>(acn, st). st \<le> 30 + (q + qa))
       
  3456            (shift
       
  3457              (adjust (tm_of abc_fourtimes @ shift (mopup (Suc 0)) qa)
       
  3458                (9 + qa))
       
  3459              (21 + q))"
  3440       apply(subgoal_tac "qa + q = q + qa")
  3460       apply(subgoal_tac "qa + q = q + qa")
  3441       apply(simp, simp)
  3461       apply(simp add: h)
       
  3462       apply(simp)
  3442       done
  3463       done
  3443   qed
  3464   qed
  3444   thus "b \<le> (60 + (length (tm_of abc_twice) + length (tm_of abc_fourtimes))) div 2"
  3465   thus "b \<le> (60 + (length (tm_of abc_twice) + length (tm_of abc_fourtimes))) div 2"
  3445     using g
  3466     using g
  3446     apply(simp add: Ball_set[THEN sym])
  3467     apply(simp add: Ball_set[THEN sym])
  3513   qed
  3534   qed
  3514   thus "?thesis"
  3535   thus "?thesis"
  3515     apply(auto simp: Hoare_halt_def)
  3536     apply(auto simp: Hoare_halt_def)
  3516     apply(rule_tac x = n in exI)
  3537     apply(rule_tac x = n in exI)
  3517     apply(case_tac "(steps0 (Suc 0, [], <m # args>)
  3538     apply(case_tac "(steps0 (Suc 0, [], <m # args>)
  3518       (Turing.adjust t_wcode_prepare @ shift t_wcode_main (length t_wcode_prepare div 2)) n)")
  3539       (adjust0 t_wcode_prepare @ shift t_wcode_main (length t_wcode_prepare div 2)) n)")
  3519     apply(auto simp: tm_comp.simps)
  3540     apply(auto simp: tm_comp.simps)
  3520     done
  3541     done
  3521 qed
  3542 qed
  3522 
  3543 
  3523 definition tinres :: "cell list \<Rightarrow> cell list \<Rightarrow> bool"
  3544 definition tinres :: "cell list \<Rightarrow> cell list \<Rightarrow> bool"
  4611 text {*
  4632 text {*
  4612   The initialization TM @{text "t_wcode"}.
  4633   The initialization TM @{text "t_wcode"}.
  4613   *}
  4634   *}
  4614 definition t_wcode :: "instr list"
  4635 definition t_wcode :: "instr list"
  4615   where
  4636   where
  4616   "t_wcode = (t_wcode_prepare |+| t_wcode_main) |+| t_wcode_adjust"
  4637   "t_wcode = (t_wcode_prepare |+| t_wcode_main) |+| t_wcode_adjust        "
  4617 
  4638 
  4618 
  4639 
  4619 text {*
  4640 text {*
  4620   The correctness of @{text "t_wcode"}.
  4641   The correctness of @{text "t_wcode"}.
  4621   *}
  4642   *}