879 "x > 0 \<Longrightarrow> {inv_end1 x} tcopy_end {inv_end0 x}" |
879 "x > 0 \<Longrightarrow> {inv_end1 x} tcopy_end {inv_end0 x}" |
880 proof(rule_tac Hoare_haltI) |
880 proof(rule_tac Hoare_haltI) |
881 fix l r |
881 fix l r |
882 assume h: "0 < x" |
882 assume h: "0 < x" |
883 "inv_end1 x (l, r)" |
883 "inv_end1 x (l, r)" |
884 hence "\<exists> stp. is_final (steps (Suc 0, l, r) (tcopy_end, 0) stp)" |
884 then have "\<exists> stp. is_final (steps0 (1, l, r) tcopy_end stp)" |
885 apply(rule_tac end_halt, simp_all add: inv_end.simps) |
885 by (simp add: end_halt inv_end.simps) |
886 done |
886 then obtain stp where "is_final (steps0 (1, l, r) tcopy_end stp)" .. |
887 then obtain stp where "is_final (steps (Suc 0, l, r) (tcopy_end, 0) stp)" .. |
887 moreover have "inv_end x (steps0 (1, l, r) tcopy_end stp)" |
888 moreover have "inv_end x (steps (Suc 0, l, r) (tcopy_end, 0) stp)" |
|
889 apply(rule_tac inv_end_steps) |
888 apply(rule_tac inv_end_steps) |
890 using h by(simp_all add: inv_end.simps) |
889 using h by(simp_all add: inv_end.simps) |
891 ultimately show |
890 ultimately show |
892 "\<exists>n. is_final (steps (1, l, r) (tcopy_end, 0) n) \<and> |
891 "\<exists>n. is_final (steps (1, l, r) (tcopy_end, 0) n) \<and> |
893 inv_end0 x holds_for steps (1, l, r) (tcopy_end, 0) n" |
892 inv_end0 x holds_for steps (1, l, r) (tcopy_end, 0) n" |
894 using h |
893 using h |
895 apply(rule_tac x = stp in exI) |
894 apply(rule_tac x = stp in exI) |
896 apply(case_tac "(steps (Suc 0, l, r) (tcopy_end, 0) stp)", |
895 apply(case_tac "(steps0 (1, l, r) tcopy_end stp)") |
897 simp add: inv_end.simps) |
896 apply(simp add: inv_end.simps) |
898 done |
897 done |
899 qed |
898 qed |
900 |
899 |
901 (* tcopy *) |
900 (* tcopy *) |
902 |
901 |
907 by (auto simp: tm_wf.simps tcopy_loop_def) |
906 by (auto simp: tm_wf.simps tcopy_loop_def) |
908 |
907 |
909 lemma [intro]: "tm_wf (tcopy_end, 0)" |
908 lemma [intro]: "tm_wf (tcopy_end, 0)" |
910 by (auto simp: tm_wf.simps tcopy_end_def) |
909 by (auto simp: tm_wf.simps tcopy_end_def) |
911 |
910 |
912 lemma [intro]: "\<lbrakk>tm_wf (A, 0); tm_wf (B, 0)\<rbrakk> \<Longrightarrow> tm_wf (A |+| B, 0)" |
|
913 by (auto simp: tm_wf.simps shift.simps adjust.simps tm_comp_length tm_comp.simps) |
|
914 |
|
915 lemma tcopy_correct1: |
911 lemma tcopy_correct1: |
916 "\<lbrakk>x > 0\<rbrakk> \<Longrightarrow> {inv_begin1 x} tcopy {inv_end0 x}" |
912 assumes "0 < x" |
917 proof(simp add: tcopy_def, rule_tac Hoare_plus_halt) |
913 shows "{inv_begin1 x} tcopy {inv_end0 x}" |
918 show "inv_loop0 x \<mapsto> inv_end1 x" |
914 proof - |
|
915 have "{inv_begin1 x} tcopy_begin {inv_begin0 x}" |
|
916 by (metis assms init_correct) |
|
917 moreover |
|
918 have "{inv_loop1 x} tcopy_loop {inv_loop0 x}" |
|
919 by (metis assms loop_correct) |
|
920 moreover |
|
921 have "inv_begin0 x \<mapsto> inv_loop1 x" |
|
922 by (auto simp: inv_begin0.simps inv_loop1.simps assert_imp_def) |
|
923 (rule_tac x = "Suc 0" in exI, auto) |
|
924 ultimately |
|
925 have "{inv_begin1 x} (tcopy_begin |+| tcopy_loop) {inv_loop0 x}" |
|
926 by (rule_tac Hoare_plus_halt) (auto) |
|
927 moreover |
|
928 have "{inv_end1 x} tcopy_end {inv_end0 x}" |
|
929 by (metis assms end_correct) |
|
930 moreover |
|
931 have "inv_loop0 x \<mapsto> inv_end1 x" |
919 by(auto simp: inv_end1.simps inv_loop1.simps assert_imp_def) |
932 by(auto simp: inv_end1.simps inv_loop1.simps assert_imp_def) |
920 next |
933 ultimately |
921 show "tm_wf (tcopy_begin |+| tcopy_loop, 0)" by auto |
934 show "{inv_begin1 x} tcopy {inv_end0 x}" |
922 next |
935 unfolding tcopy_def |
923 assume "0 < x" |
936 by (rule_tac Hoare_plus_halt) (auto) |
924 thus "{inv_begin1 x} (tcopy_begin |+| tcopy_loop) {inv_loop0 x}" |
|
925 proof(rule_tac Hoare_plus_halt) |
|
926 show "inv_begin0 x \<mapsto> inv_loop1 x" |
|
927 apply(auto simp: inv_begin0.simps inv_loop1.simps assert_imp_def) |
|
928 apply(rule_tac x = "Suc 0" in exI, auto) |
|
929 done |
|
930 next |
|
931 show "tm_wf (tcopy_begin, 0)" by auto |
|
932 next |
|
933 assume "0 < x" |
|
934 thus "{inv_begin1 x} tcopy_begin {inv_begin0 x}" |
|
935 by(erule_tac init_correct) |
|
936 next |
|
937 assume "0 < x" |
|
938 thus "{inv_loop1 x} tcopy_loop {inv_loop0 x}" |
|
939 by(erule_tac loop_correct) |
|
940 qed |
|
941 next |
|
942 assume "0 < x" |
|
943 thus "{inv_end1 x} tcopy_end {inv_end0 x}" |
|
944 by(erule_tac end_correct) |
|
945 qed |
937 qed |
946 |
938 |
947 abbreviation |
939 abbreviation |
948 "pre_tcopy n \<equiv> \<lambda>tp. tp = ([]::cell list, <[n::nat]>)" |
940 "pre_tcopy n \<equiv> \<lambda>tp. tp = ([]::cell list, <[n::nat]>)" |
949 abbreviation |
941 abbreviation |
950 "post_tcopy n \<equiv> \<lambda>tp. tp= ([Bk], <[n, n::nat]>)" |
942 "post_tcopy n \<equiv> \<lambda>tp. tp= ([Bk], <[n, n::nat]>)" |
951 |
943 |
952 lemma tcopy_correct2: |
944 lemma tcopy_correct: |
953 shows "{pre_tcopy n} tcopy {post_tcopy n}" |
945 shows "{pre_tcopy n} tcopy {post_tcopy n}" |
954 proof - |
946 proof - |
955 have "{inv_begin1 (Suc n)} tcopy {inv_end0 (Suc n)}" |
947 have "{inv_begin1 (Suc n)} tcopy {inv_end0 (Suc n)}" |
956 by (rule tcopy_correct1) (simp) |
948 by (rule tcopy_correct1) (simp) |
957 moreover |
949 moreover |
958 have "pre_tcopy n \<mapsto> inv_begin1 (Suc n)" |
950 have "pre_tcopy n = inv_begin1 (Suc n)" |
959 by (simp add: assert_imp_def tape_of_nl_abv) |
951 by (auto simp add: tape_of_nl_abv) |
960 moreover |
952 moreover |
961 have "inv_end0 (Suc n) \<mapsto> post_tcopy n" |
953 have "inv_end0 (Suc n) = post_tcopy n" |
962 by (simp add: assert_imp_def inv_end0.simps tape_of_nl_abv) |
954 by (auto simp add: inv_end0.simps tape_of_nl_abv) |
963 ultimately |
955 ultimately |
964 show "{pre_tcopy n} tcopy {post_tcopy n}" |
956 show "{pre_tcopy n} tcopy {post_tcopy n}" |
965 by (rule Hoare_weaken) |
957 by simp |
966 qed |
958 qed |
967 |
959 |
968 |
960 |
969 section {* The {\em Dithering} Turing Machine *} |
961 section {* The {\em Dithering} Turing Machine *} |
970 |
962 |
1020 and the final configuration is standard. |
1012 and the final configuration is standard. |
1021 *} |
1013 *} |
1022 |
1014 |
1023 definition haltP :: "tprog0 \<Rightarrow> nat list \<Rightarrow> bool" |
1015 definition haltP :: "tprog0 \<Rightarrow> nat list \<Rightarrow> bool" |
1024 where |
1016 where |
1025 "haltP p lm \<equiv> {(\<lambda>tp. tp = ([], <lm>))} p {(\<lambda>tp. (\<exists>k m. tp = (Bk \<up> k, <m::nat>)))}" |
1017 "haltP p lm \<equiv> {(\<lambda>tp. tp = ([], <lm>))} p {(\<lambda>tp. (\<exists>k n. tp = (Bk \<up> k, <n::nat>)))}" |
1026 |
1018 |
1027 lemma [intro, simp]: "tm_wf0 tcopy" |
1019 lemma [intro, simp]: "tm_wf0 tcopy" |
1028 by (auto simp: tcopy_def) |
1020 by (auto simp: tcopy_def) |
1029 |
1021 |
1030 lemma [intro, simp]: "tm_wf0 dither" |
1022 lemma [intro, simp]: "tm_wf0 dither" |
1104 |
1097 |
1105 (* {P1} (tcopy |+| H) {P3} *) |
1098 (* {P1} (tcopy |+| H) {P3} *) |
1106 have first: "{P1} (tcopy |+| H) {P3}" |
1099 have first: "{P1} (tcopy |+| H) {P3}" |
1107 proof (cases rule: Hoare_plus_halt_simple) |
1100 proof (cases rule: Hoare_plus_halt_simple) |
1108 case A_halt (* of tcopy *) |
1101 case A_halt (* of tcopy *) |
1109 show "{P1} tcopy {P2}" unfolding P1_def P2_def |
1102 show "{P1} tcopy {P2}" unfolding P1_def P2_def |
1110 by (rule tcopy_correct2) |
1103 by (rule tcopy_correct) |
1111 next |
1104 next |
1112 case B_halt (* of H *) |
1105 case B_halt (* of H *) |
1113 show "{P2} H {P3}" |
1106 show "{P2} H {P3}" |
1114 unfolding P2_def P3_def |
1107 unfolding P2_def P3_def |
1115 by (rule H_halt_inv[OF assms]) |
1108 by (rule H_halt_inv[OF assms]) |
1140 shows "False" |
1134 shows "False" |
1141 proof - |
1135 proof - |
1142 (* invariants *) |
1136 (* invariants *) |
1143 def P1 \<equiv> "\<lambda>tp. tp = ([]::cell list, <[code_tcontra]>)" |
1137 def P1 \<equiv> "\<lambda>tp. tp = ([]::cell list, <[code_tcontra]>)" |
1144 def P2 \<equiv> "\<lambda>tp. tp = ([Bk], <[code_tcontra, code_tcontra]>)" |
1138 def P2 \<equiv> "\<lambda>tp. tp = ([Bk], <[code_tcontra, code_tcontra]>)" |
1145 def P3 \<equiv> "\<lambda>tp. (\<exists>n. tp = (Bk \<up> n, <0::nat>))" |
1139 def Q3 \<equiv> "\<lambda>tp. (\<exists>n. tp = (Bk \<up> n, <0::nat>))" |
1146 |
1140 |
1147 (* |
1141 (* |
1148 {P1} tcopy {P2} {P2} H {P3} |
1142 {P1} tcopy {P2} {P2} H {Q3} |
1149 ---------------------------- |
1143 ---------------------------- |
1150 {P1} (tcopy |+| H) {P3} {P3} dither loops |
1144 {P1} (tcopy |+| H) {Q3} {Q3} dither loops |
1151 ------------------------------------------------ |
1145 ------------------------------------------------ |
1152 {P1} tcontra loops |
1146 {P1} tcontra loops |
1153 *) |
1147 *) |
1154 |
1148 |
1155 have H_wf: "tm_wf0 (tcopy |+| H)" by auto |
1149 have H_wf: "tm_wf0 (tcopy |+| H)" by auto |
1156 |
1150 |
1157 (* {P1} (tcopy |+| H) {P3} *) |
1151 (* {P1} (tcopy |+| H) {Q3} *) |
1158 have first: "{P1} (tcopy |+| H) {P3}" |
1152 have first: "{P1} (tcopy |+| H) {Q3}" |
1159 proof (cases rule: Hoare_plus_halt_simple) |
1153 proof (cases rule: Hoare_plus_halt_simple) |
1160 case A_halt (* of tcopy *) |
1154 case A_halt (* of tcopy *) |
1161 show "{P1} tcopy {P2}" unfolding P1_def P2_def |
1155 show "{P1} tcopy {P2}" unfolding P1_def P2_def |
1162 by (rule tcopy_correct2) |
1156 by (rule tcopy_correct) |
1163 next |
1157 next |
1164 case B_halt (* of H *) |
1158 case B_halt (* of H *) |
1165 then show "{P2} H {P3}" |
1159 then show "{P2} H {Q3}" |
1166 unfolding P2_def P3_def |
1160 unfolding P2_def Q3_def |
1167 by (rule H_unhalt_inv[OF assms]) |
1161 by (rule H_unhalt_inv[OF assms]) |
1168 qed (simp) |
1162 qed (simp) |
1169 |
1163 |
1170 (* {P3} dither loops *) |
1164 (* {P3} dither loops *) |
1171 have second: "{P3} dither \<up>" unfolding P3_def |
1165 have second: "{Q3} dither \<up>" unfolding Q3_def |
1172 by (rule dither_loops) |
1166 by (rule dither_loops) |
1173 |
1167 |
1174 (* {P1} tcontra loops *) |
1168 (* {P1} tcontra loops *) |
1175 have "{P1} tcontra \<up>" |
1169 have "{P1} tcontra \<up>" |
|
1170 unfolding tcontra_def |
1176 by (rule Hoare_plus_unhalt_simple[OF first second H_wf]) |
1171 by (rule Hoare_plus_unhalt_simple[OF first second H_wf]) |
1177 |
1172 |
1178 with assms show "False" |
1173 with assms show "False" |
1179 unfolding P1_def |
1174 unfolding P1_def |
1180 unfolding haltP_def |
1175 unfolding haltP_def |