thys/uncomputable.thy
changeset 66 4a3cd7d70ec2
parent 65 0349fa7f5595
child 67 140489a4020e
equal deleted inserted replaced
65:0349fa7f5595 66:4a3cd7d70ec2
  1035 
  1035 
  1036 text {*
  1036 text {*
  1037   The {\em Dithering} TM, when the input is @{text "1"}, it will loop forever, otherwise, it will
  1037   The {\em Dithering} TM, when the input is @{text "1"}, it will loop forever, otherwise, it will
  1038   terminate.
  1038   terminate.
  1039 *}
  1039 *}
       
  1040 
  1040 definition dither :: "instr list"
  1041 definition dither :: "instr list"
  1041   where
  1042   where
  1042   "dither \<equiv> [(W0, 1), (R, 2), (L, 1), (L, 0)] "
  1043   "dither \<equiv> [(W0, 1), (R, 2), (L, 1), (L, 0)] "
  1043 
  1044 
  1044 lemma dither_halt_rs: 
       
  1045   "\<exists> stp. steps (Suc 0, Bk\<up>m, [Oc, Oc]) (dither, 0) stp = 
       
  1046                                  (0, Bk\<up>m, [Oc, Oc])"
       
  1047 apply(rule_tac x = "Suc (Suc (Suc 0))" in exI)
       
  1048 apply(simp add: dither_def steps.simps 
       
  1049                 step.simps fetch.simps numeral)
       
  1050 done
       
  1051 
       
  1052 lemma dither_unhalt_state: 
  1045 lemma dither_unhalt_state: 
  1053   "(steps (Suc 0, Bk\<up>m, [Oc]) (dither, 0) stp = 
  1046   "(steps0 (1, Bk \<up> m, [Oc]) dither stp = (1, Bk \<up> m, [Oc])) \<or> 
  1054    (Suc 0, Bk\<up>m, [Oc])) \<or> 
  1047    (steps0 (1, Bk \<up> m, [Oc]) dither stp = (2, Oc # Bk \<up> m, []))"
  1055    (steps (Suc 0, Bk\<up>m, [Oc]) (dither, 0) stp = (2, Oc # Bk\<up>m, []))"
  1048   apply(induct stp)
  1056   apply(induct stp, simp add: steps.simps)
  1049   apply(simp add: steps.simps)
  1057   apply(simp add: step_red, auto)
  1050   apply(simp add: step_red)
  1058   apply(auto simp: step.simps fetch.simps dither_def numeral)
  1051   apply(auto simp: step.simps fetch.simps dither_def numeral)
  1059   done
  1052   done
  1060 
  1053 
  1061 lemma dither_unhalt_rs: 
  1054 lemma dither_unhalt_rs: 
  1062   "\<not> is_final (steps (Suc 0, Bk\<up>m, [Oc]) (dither,0) stp)"
  1055   shows "\<not> is_final (steps0 (1, Bk \<up> m, [Oc]) dither stp)"
  1063 using dither_unhalt_state[of m stp]
  1056 using dither_unhalt_state[of m stp]
       
  1057 by auto
       
  1058 
       
  1059 lemma dither_loops:
       
  1060   shows "{(\<lambda>(l, r). (\<exists>nd. l = Bk \<up> nd) \<and> r = [Oc])} dither \<up>" 
       
  1061 apply(rule Hoare_unhaltI)
       
  1062 using dither_unhalt_rs
  1064 apply(auto)
  1063 apply(auto)
  1065 done
  1064 done
       
  1065 
       
  1066 lemma dither_halt_rs: 
       
  1067   "\<exists>stp. steps0 (Suc 0, Bk \<up> m, [Oc, Oc]) dither stp = (0, Bk \<up> m, [Oc, Oc])"
       
  1068 unfolding dither_def
       
  1069 apply(rule_tac x = "3" in exI)
       
  1070 apply(simp add: steps.simps step.simps fetch.simps numeral)
       
  1071 done 
       
  1072 
       
  1073 definition
       
  1074   "dither_halt_inv \<equiv> (\<lambda>(l, r). (\<exists>nd. l = Bk \<up> nd) \<and> r = [Oc, Oc])"
       
  1075 
       
  1076 lemma dither_halts:
       
  1077   shows "{dither_halt_inv} dither {dither_halt_inv}" 
       
  1078 unfolding dither_halt_inv_def
       
  1079 apply(rule HoareI)
       
  1080 using dither_halt_rs
       
  1081 apply(auto)
       
  1082 by (metis (lifting, mono_tags) holds_for.simps is_final_eq prod.cases)
       
  1083 
       
  1084 
  1066 
  1085 
  1067 section {*
  1086 section {*
  1068   The final diagnal arguments to show the undecidability of Halting problem.
  1087   The final diagnal arguments to show the undecidability of Halting problem.
  1069 *}
  1088 *}
  1070 
  1089 
  1248     thus "a = 0 \<and> (\<exists>nb. b =  Bk\<up>nb) \<and> c = [Oc, Oc]"
  1267     thus "a = 0 \<and> (\<exists>nb. b =  Bk\<up>nb) \<and> c = [Oc, Oc]"
  1249       by(auto elim: tinres_ex1)
  1268       by(auto elim: tinres_ex1)
  1250   qed
  1269   qed
  1251 qed
  1270 qed
  1252    
  1271    
       
  1272 (* TM that produces the contradiction *)
  1253 definition tcontra :: "instr list \<Rightarrow> instr list"
  1273 definition tcontra :: "instr list \<Rightarrow> instr list"
  1254   where
  1274   where
  1255   "tcontra h \<equiv> (tcopy |+| h) |+| dither"
  1275   "tcontra h \<equiv> (tcopy |+| h) |+| dither"
  1256 
  1276 
  1257 declare replicate_Suc[simp del]
       
  1258 
       
  1259 lemma dither_loops:
       
  1260   shows "{(\<lambda>(l, r). (\<exists>nd. l = Bk \<up> nd) \<and> r = [Oc])} dither \<up>" 
       
  1261 apply(rule Hoare_unhaltI)
       
  1262 apply(auto intro!: dither_unhalt_rs)
       
  1263 done
       
  1264 
       
  1265 lemma dither_halts:
       
  1266   shows 
       
  1267   "{(\<lambda>(l, r). (\<exists>nd. l = Bk \<up> nd) \<and> r = [Oc, Oc])} dither {(\<lambda>(l, r). (\<exists>nd. l = Bk \<up> nd) \<and> r = [Oc, Oc])}" 
       
  1268 apply(rule HoareI)
       
  1269 using dither_halt_rs
       
  1270 apply(auto)
       
  1271 by (metis (lifting, mono_tags) holds_for.simps is_final_eq prod.cases)
       
  1272 
  1277 
  1273 lemma uh_h: 
  1278 lemma uh_h: 
  1274   assumes "\<not> haltP0 (tcontra H) [code (tcontra H)]"
  1279   assumes "\<not> haltP0 (tcontra H) [code (tcontra H)]"
  1275   shows "haltP0 (tcontra H) [code (tcontra H)]"
  1280   shows "False"
  1276 proof -
  1281 proof -
  1277    (* code of tcontra *)
  1282    (* code of tcontra *)
  1278   def code_tcontra \<equiv> "(code (tcontra H))"
  1283   def code_tcontra \<equiv> "(code (tcontra H))"
  1279   have assms': "\<not> haltP0 (tcontra H) [code_tcontra]" 
  1284   have assms': "\<not> haltP0 (tcontra H) [code_tcontra]" 
  1280     using assms by (simp add: code_tcontra_def)
  1285     using assms by (simp add: code_tcontra_def)
  1281 
  1286 
  1282   (* invariants *)
  1287   (* invariants *)
  1283   def P1 \<equiv> "\<lambda>(l::cell list, r::cell list). (l = [] \<and> r = <[code_tcontra]>)"
  1288   def P1 \<equiv> "\<lambda>(l::cell list, r::cell list). (l = [] \<and> r = <[code_tcontra]>)"
  1284   def P2 \<equiv> "\<lambda>(l::cell list, r::cell list). (l = [Bk] \<and> r = <[code_tcontra, code_tcontra]>)"
  1289   def P2 \<equiv> "\<lambda>(l::cell list, r::cell list). (l = [Bk] \<and> r = <[code_tcontra, code_tcontra]>)"
  1285   def P3 \<equiv> "\<lambda>(l::cell list, r::cell list). (\<exists>nd. l = Bk \<up> nd) \<and> r = [Oc, Oc]"
  1290   def P3 \<equiv> dither_halt_inv
  1286 
  1291 
  1287   (*
  1292   (*
  1288   {P1} tcopy {P2}  {P2} H {P3} 
  1293   {P1} tcopy {P2}  {P2} H {P3} 
  1289   ----------------------------
  1294   ----------------------------
  1290      {P1} (tcopy |+| H) {P3}     {P3} dither {P3}
  1295      {P1} (tcopy |+| H) {P3}     {P3} dither {P3}
  1314     obtain n i
  1319     obtain n i
  1315       where "steps0 (1, Bk \<up> 1, <[code_tcontra, code_tcontra]>) H n = (0, Bk \<up> i, [Oc, Oc])"
  1320       where "steps0 (1, Bk \<up> 1, <[code_tcontra, code_tcontra]>) H n = (0, Bk \<up> i, [Oc, Oc])"
  1316       using nh_newcase[of "tcontra H" "[code_tcontra]" 1, OF assms', folded code_tcontra_def]
  1321       using nh_newcase[of "tcontra H" "[code_tcontra]" 1, OF assms', folded code_tcontra_def]
  1317       by (auto)
  1322       by (auto)
  1318     then show "{P2} H {P3}"
  1323     then show "{P2} H {P3}"
  1319       unfolding P2_def P3_def
  1324       unfolding P2_def P3_def dither_halt_inv_def
  1320       unfolding Hoare_def
  1325       unfolding Hoare_def
  1321       apply(auto)
  1326       apply(auto)
  1322       apply(rule_tac x = n in exI)
  1327       apply(rule_tac x = n in exI)
  1323       apply(simp add: replicate_Suc)
  1328       apply(simp)
  1324       done
  1329       done
  1325   qed (simp)
  1330   qed (simp)
  1326 
  1331 
  1327   (* {P3} dither {P3} *)
  1332   (* {P3} dither {P3} *)
  1328   have second: "{P3} dither {P3}" unfolding P3_def 
  1333   have second: "{P3} dither {P3}" unfolding P3_def 
  1330   
  1335   
  1331   (* {P1} tcontra {P3} *)
  1336   (* {P1} tcontra {P3} *)
  1332   have "{P1} (tcontra H) {P3}" unfolding tcontra_def
  1337   have "{P1} (tcontra H) {P3}" unfolding tcontra_def
  1333     by (rule Hoare_plus_halt_simple[OF first second H_wf])
  1338     by (rule Hoare_plus_halt_simple[OF first second H_wf])
  1334 
  1339 
  1335   with assms' have "False"
  1340   with assms' show "False"
  1336     unfolding P1_def P3_def code_tcontra_def
  1341     unfolding P1_def P3_def dither_halt_inv_def code_tcontra_def
  1337     unfolding haltP_def
  1342     unfolding haltP_def
  1338     unfolding Hoare_def 
  1343     unfolding Hoare_def 
  1339     apply(auto)
  1344     apply(auto)
  1340     apply(erule_tac x = n in allE)
  1345     apply(erule_tac x = n in allE)
  1341     apply(case_tac "steps0 (Suc 0, [], <[code (tcontra H)]>) (tcontra H) n")
  1346     apply(case_tac "steps0 (Suc 0, [], <[code (tcontra H)]>) (tcontra H) n")
  1342     apply(simp, auto)
  1347     apply(simp, auto)
  1343     apply(erule_tac x = 2 in allE, erule_tac x = 0 in allE)
  1348     apply(erule_tac x = 2 in allE, erule_tac x = 0 in allE)
  1344     apply(simp)
  1349     apply(simp)
  1345     by (smt replicate_0 replicate_Suc)
  1350     by (smt replicate_0 replicate_Suc)
  1346   then show "haltP0 (tcontra H) [code (tcontra H)]"
       
  1347     by blast
       
  1348 qed
  1351 qed
  1349 
  1352 
  1350 lemma h_uh: 
  1353 lemma h_uh: 
  1351   assumes "haltP0 (tcontra H) [code (tcontra H)]"
  1354   assumes "haltP0 (tcontra H) [code (tcontra H)]"
  1352   shows "\<not> haltP0 (tcontra H) [code (tcontra H)]"
  1355   shows "False"
  1353 proof - 
  1356 proof - 
  1354   (* code of tcontra *)
  1357   (* code of tcontra *)
  1355   def code_tcontra \<equiv> "(code (tcontra H))"
  1358   def code_tcontra \<equiv> "(code (tcontra H))"
  1356   have assms': "haltP0 (tcontra H) [code_tcontra]" 
  1359   have assms': "haltP0 (tcontra H) [code_tcontra]" 
  1357     using assms by (simp add: code_tcontra_def)
  1360     using assms by (simp add: code_tcontra_def)
  1395     then show "{P2} H {P3}"
  1398     then show "{P2} H {P3}"
  1396       unfolding P2_def P3_def
  1399       unfolding P2_def P3_def
  1397       unfolding Hoare_def
  1400       unfolding Hoare_def
  1398       apply(auto)
  1401       apply(auto)
  1399       apply(rule_tac x = n in exI)
  1402       apply(rule_tac x = n in exI)
  1400       apply(simp add: replicate_Suc)
  1403       apply(simp)
  1401       done
  1404       done
  1402   qed (simp)
  1405   qed (simp)
  1403 
  1406 
  1404   (* {P3} dither loops *)
  1407   (* {P3} dither loops *)
  1405   have second: "{P3} dither \<up>" unfolding P3_def 
  1408   have second: "{P3} dither \<up>" unfolding P3_def 
  1407   
  1410   
  1408   (* {P1} tcontra loops *)
  1411   (* {P1} tcontra loops *)
  1409   have "{P1} (tcontra H) \<up>" unfolding tcontra_def
  1412   have "{P1} (tcontra H) \<up>" unfolding tcontra_def
  1410     by (rule Hoare_plus_unhalt_simple[OF first second H_wf])
  1413     by (rule Hoare_plus_unhalt_simple[OF first second H_wf])
  1411 
  1414 
  1412   with assms have "False"
  1415   with assms show "False"
  1413     unfolding haltP_def
  1416     unfolding haltP_def
  1414     unfolding P1_def code_tcontra_def
  1417     unfolding P1_def code_tcontra_def
  1415     unfolding Hoare_unhalt_def
  1418     unfolding Hoare_unhalt_def
  1416     by (auto, metis is_final_eq)  
  1419     by (auto, metis is_final_eq)  
  1417   then show "\<not> haltP0 (tcontra H) [code_tcontra]"
       
  1418     by blast
       
  1419 qed
  1420 qed
  1420 
  1421 
  1421 
  1422 
  1422 
  1423 
  1423       
  1424       
  1424 text {*
  1425 text {*
  1425   @{text "False"} can finally derived.
  1426   @{text "False"} can finally derived.
  1426 *}
  1427 *}
  1427 
  1428 
  1428 lemma false: "False"
  1429 lemma false: "False"
  1429 using uh_h h_uh
  1430 using uh_h h_uh by auto
  1430 by auto
       
  1431 
  1431 
  1432 end
  1432 end
  1433 
  1433 
       
  1434 declare replicate_Suc[simp del]
       
  1435 
       
  1436 
  1434 end
  1437 end
  1435 
  1438