1 theory turing_basic |
|
2 imports Main |
|
3 begin |
|
4 |
|
5 section {* Basic definitions of Turing machine *} |
|
6 |
|
7 (* Title: Turing machine's definition and its charater |
|
8 Author: Xu Jian <xujian817@hotmail.com> |
|
9 Maintainer: Xu Jian |
|
10 *) |
|
11 |
|
12 text {* |
|
13 Actions of Turing machine (Abbreviated TM in the following* ). |
|
14 *} |
|
15 |
|
16 datatype taction = |
|
17 -- {* Write zero *} |
|
18 W0 | |
|
19 -- {* Write one *} |
|
20 W1 | |
|
21 -- {* Move left *} |
|
22 L | |
|
23 -- {* Move right *} |
|
24 R | |
|
25 -- {* Do nothing *} |
|
26 Nop |
|
27 |
|
28 text {* |
|
29 Tape contents in every block. |
|
30 *} |
|
31 |
|
32 datatype block = |
|
33 -- {* Blank *} |
|
34 Bk | |
|
35 -- {* Occupied *} |
|
36 Oc |
|
37 |
|
38 text {* |
|
39 Tape is represented as a pair of lists $(L_{left}, L_{right})$, |
|
40 where $L_left$, named {\em left list}, is used to represent |
|
41 the tape to the left of RW-head and |
|
42 $L_{right}$, named {\em right list}, is used to represent the tape |
|
43 under and to the right of RW-head. |
|
44 *} |
|
45 |
|
46 type_synonym tape = "block list \<times> block list" |
|
47 |
|
48 text {* The state of turing machine.*} |
|
49 type_synonym tstate = nat |
|
50 |
|
51 text {* |
|
52 Turing machine instruction is represented as a |
|
53 pair @{text "(action, next_state)"}, |
|
54 where @{text "action"} is the action to take at the current state |
|
55 and @{text "next_state"} is the next state the machine is getting into |
|
56 after the action. |
|
57 *} |
|
58 type_synonym tinst = "taction \<times> tstate" |
|
59 |
|
60 text {* |
|
61 Program of Turing machine is represented as a list of Turing instructions |
|
62 and the execution of the program starts from the head of the list. |
|
63 *} |
|
64 type_synonym tprog = "tinst list" |
|
65 |
|
66 |
|
67 text {* |
|
68 Turing machine configuration, which consists of the current state |
|
69 and the tape. |
|
70 *} |
|
71 type_synonym t_conf = "tstate \<times> tape" |
|
72 |
|
73 fun nth_of :: "'a list \<Rightarrow> nat \<Rightarrow> 'a option" |
|
74 where |
|
75 "nth_of xs n = (if n < length xs then Some (xs!n) |
|
76 else None)" |
|
77 |
|
78 text {* |
|
79 The function used to fetech instruction out of Turing program. |
|
80 *} |
|
81 |
|
82 fun fetch :: "tprog \<Rightarrow> tstate \<Rightarrow> block \<Rightarrow> tinst" |
|
83 where |
|
84 "fetch p s b = (if s = 0 then (Nop, 0) else |
|
85 case b of |
|
86 Bk \<Rightarrow> case nth_of p (2 * (s - 1)) of |
|
87 Some i \<Rightarrow> i |
|
88 | None \<Rightarrow> (Nop, 0) |
|
89 | Oc \<Rightarrow> case nth_of p (2 * (s - 1) +1) of |
|
90 Some i \<Rightarrow> i |
|
91 | None \<Rightarrow> (Nop, 0))" |
|
92 |
|
93 |
|
94 fun new_tape :: "taction \<Rightarrow> tape \<Rightarrow> tape" |
|
95 where |
|
96 "new_tape action (leftn, rightn) = (case action of |
|
97 W0 \<Rightarrow> (leftn, Bk#(tl rightn)) | |
|
98 W1 \<Rightarrow> (leftn, Oc#(tl rightn)) | |
|
99 L \<Rightarrow> (if leftn = [] then (tl leftn, Bk#rightn) |
|
100 else (tl leftn, (hd leftn) # rightn)) | |
|
101 R \<Rightarrow> if rightn = [] then (Bk#leftn,tl rightn) |
|
102 else ((hd rightn)#leftn, tl rightn) | |
|
103 Nop \<Rightarrow> (leftn, rightn) |
|
104 )" |
|
105 |
|
106 text {* |
|
107 The one step function used to transfer Turing machine configuration. |
|
108 *} |
|
109 fun tstep :: "t_conf \<Rightarrow> tprog \<Rightarrow> t_conf" |
|
110 where |
|
111 "tstep c p = (let (s, l, r) = c in |
|
112 let (ac, ns) = (fetch p s (case r of [] \<Rightarrow> Bk | |
|
113 x # xs \<Rightarrow> x)) in |
|
114 (ns, new_tape ac (l, r)))" |
|
115 |
|
116 text {* |
|
117 The many-step function. |
|
118 *} |
|
119 fun steps :: "t_conf \<Rightarrow> tprog \<Rightarrow> nat \<Rightarrow> t_conf" |
|
120 where |
|
121 "steps c p 0 = c" | |
|
122 "steps c p (Suc n) = steps (tstep c p) p n" |
|
123 |
|
124 lemma tstep_red: "steps c p (Suc n) = tstep (steps c p n) p" |
|
125 proof(induct n arbitrary: c) |
|
126 fix c |
|
127 show "steps c p (Suc 0) = tstep (steps c p 0) p" by(simp add: steps.simps) |
|
128 next |
|
129 fix n c |
|
130 assume ind: "\<And> c. steps c p (Suc n) = tstep (steps c p n) p" |
|
131 have "steps (tstep c p) p (Suc n) = tstep (steps (tstep c p) p n) p" |
|
132 by(rule ind) |
|
133 thus "steps c p (Suc (Suc n)) = tstep (steps c p (Suc n)) p" by(simp add: steps.simps) |
|
134 qed |
|
135 |
|
136 declare Let_def[simp] option.split[split] |
|
137 |
|
138 definition |
|
139 "iseven n \<equiv> \<exists> x. n = 2 * x" |
|
140 |
|
141 |
|
142 text {* |
|
143 The following @{text "t_correct"} function is used to specify the wellformedness of Turing |
|
144 machine. |
|
145 *} |
|
146 fun t_correct :: "tprog \<Rightarrow> bool" |
|
147 where |
|
148 "t_correct p = (length p \<ge> 2 \<and> iseven (length p) \<and> |
|
149 list_all (\<lambda> (acn, s). s \<le> length p div 2) p)" |
|
150 |
|
151 declare t_correct.simps[simp del] |
|
152 |
|
153 lemma allimp: "\<lbrakk>\<forall>x. P x \<longrightarrow> Q x; \<forall>x. P x\<rbrakk> \<Longrightarrow> \<forall>x. Q x" |
|
154 by(auto elim: allE) |
|
155 |
|
156 lemma halt_lemma: "\<lbrakk>wf LE; \<forall> n. (\<not> P (f n) \<longrightarrow> (f (Suc n), (f n)) \<in> LE)\<rbrakk> \<Longrightarrow> \<exists> n. P (f n)" |
|
157 apply(rule exCI, drule allimp, auto) |
|
158 apply(drule_tac f = f in wf_inv_image, simp add: inv_image_def) |
|
159 apply(erule wf_induct, auto) |
|
160 done |
|
161 |
|
162 lemma steps_add: "steps c t (x + y) = steps (steps c t x) t y" |
|
163 by(induct x arbitrary: c, auto simp: steps.simps tstep_red) |
|
164 |
|
165 lemma listall_set: "list_all p t \<Longrightarrow> \<forall> a \<in> set t. p a" |
|
166 by(induct t, auto) |
|
167 |
|
168 lemma fetch_ex: "\<exists>b a. fetch T aa ab = (b, a)" |
|
169 by(simp add: fetch.simps) |
|
170 definition exponent :: "'a \<Rightarrow> nat \<Rightarrow> 'a list" ("_\<^bsup>_\<^esup>" [0, 0]100) |
|
171 where "exponent x n = replicate n x" |
|
172 |
|
173 text {* |
|
174 @{text "tinres l1 l2"} means left list @{text "l1"} is congruent with |
|
175 @{text "l2"} with respect to the execution of Turing machine. |
|
176 Appending Blank to the right of eigther one does not affect the |
|
177 outcome of excution. |
|
178 *} |
|
179 |
|
180 definition tinres :: "block list \<Rightarrow> block list \<Rightarrow> bool" |
|
181 where |
|
182 "tinres bx by = (\<exists> n. bx = by@Bk\<^bsup>n\<^esup> \<or> by = bx @ Bk\<^bsup>n\<^esup>)" |
|
183 |
|
184 lemma exp_zero: "a\<^bsup>0\<^esup> = []" |
|
185 by(simp add: exponent_def) |
|
186 lemma exp_ind_def: "a\<^bsup>Suc x \<^esup> = a # a\<^bsup>x\<^esup>" |
|
187 by(simp add: exponent_def) |
|
188 |
|
189 text {* |
|
190 The following lemma shows the meaning of @{text "tinres"} with respect to |
|
191 one step execution. |
|
192 *} |
|
193 lemma tinres_step: |
|
194 "\<lbrakk>tinres l l'; tstep (ss, l, r) t = (sa, la, ra); tstep (ss, l', r) t = (sb, lb, rb)\<rbrakk> |
|
195 \<Longrightarrow> tinres la lb \<and> ra = rb \<and> sa = sb" |
|
196 apply(auto simp: tstep.simps fetch.simps new_tape.simps |
|
197 split: if_splits taction.splits list.splits |
|
198 block.splits) |
|
199 apply(case_tac [!] "t ! (2 * (ss - Suc 0))", |
|
200 auto simp: exponent_def tinres_def split: if_splits taction.splits list.splits |
|
201 block.splits) |
|
202 apply(case_tac [!] "t ! (2 * (ss - Suc 0) + Suc 0)", |
|
203 auto simp: exponent_def tinres_def split: if_splits taction.splits list.splits |
|
204 block.splits) |
|
205 done |
|
206 |
|
207 declare tstep.simps[simp del] steps.simps[simp del] |
|
208 |
|
209 text {* |
|
210 The following lemma shows the meaning of @{text "tinres"} with respect to |
|
211 many step execution. |
|
212 *} |
|
213 lemma tinres_steps: |
|
214 "\<lbrakk>tinres l l'; steps (ss, l, r) t stp = (sa, la, ra); steps (ss, l', r) t stp = (sb, lb, rb)\<rbrakk> |
|
215 \<Longrightarrow> tinres la lb \<and> ra = rb \<and> sa = sb" |
|
216 apply(induct stp arbitrary: sa la ra sb lb rb, simp add: steps.simps) |
|
217 apply(simp add: tstep_red) |
|
218 apply(case_tac "(steps (ss, l, r) t stp)") |
|
219 apply(case_tac "(steps (ss, l', r) t stp)") |
|
220 proof - |
|
221 fix stp sa la ra sb lb rb a b c aa ba ca |
|
222 assume ind: "\<And>sa la ra sb lb rb. \<lbrakk>steps (ss, l, r) t stp = (sa, la, ra); |
|
223 steps (ss, l', r) t stp = (sb, lb, rb)\<rbrakk> \<Longrightarrow> tinres la lb \<and> ra = rb \<and> sa = sb" |
|
224 and h: " tinres l l'" "tstep (steps (ss, l, r) t stp) t = (sa, la, ra)" |
|
225 "tstep (steps (ss, l', r) t stp) t = (sb, lb, rb)" "steps (ss, l, r) t stp = (a, b, c)" |
|
226 "steps (ss, l', r) t stp = (aa, ba, ca)" |
|
227 have "tinres b ba \<and> c = ca \<and> a = aa" |
|
228 apply(rule_tac ind, simp_all add: h) |
|
229 done |
|
230 thus "tinres la lb \<and> ra = rb \<and> sa = sb" |
|
231 apply(rule_tac l = b and l' = ba and r = c and ss = a |
|
232 and t = t in tinres_step) |
|
233 using h |
|
234 apply(simp, simp, simp) |
|
235 done |
|
236 qed |
|
237 |
|
238 text {* |
|
239 The following function @{text "tshift tp n"} is used to shift Turing programs |
|
240 @{text "tp"} by @{text "n"} when it is going to be combined with others. |
|
241 *} |
|
242 |
|
243 fun tshift :: "tprog \<Rightarrow> nat \<Rightarrow> tprog" |
|
244 where |
|
245 "tshift tp off = (map (\<lambda> (action, state). (action, (if state = 0 then 0 |
|
246 else state + off))) tp)" |
|
247 |
|
248 text {* |
|
249 When two Turing programs are combined, the end state (state @{text "0"}) of the one |
|
250 at the prefix position needs to be connected to the start state |
|
251 of the one at postfix position. If @{text "tp"} is the Turing program |
|
252 to be at the prefix, @{text "change_termi_state tp"} is the transformed Turing program. |
|
253 *} |
|
254 fun change_termi_state :: "tprog \<Rightarrow> tprog" |
|
255 where |
|
256 "change_termi_state t = |
|
257 (map (\<lambda> (acn, ns). if ns = 0 then (acn, Suc ((length t) div 2)) else (acn, ns)) t)" |
|
258 |
|
259 text {* |
|
260 @{text "t_add tp1 tp2"} is the combined Truing program. |
|
261 *} |
|
262 |
|
263 fun t_add :: "tprog \<Rightarrow> tprog \<Rightarrow> tprog" ("_ |+| _" [0, 0] 100) |
|
264 where |
|
265 "t_add t1 t2 = ((change_termi_state t1) @ (tshift t2 ((length t1) div 2)))" |
|
266 |
|
267 text {* |
|
268 Tests whether the current configuration is at state @{text "0"}. |
|
269 *} |
|
270 definition isS0 :: "t_conf \<Rightarrow> bool" |
|
271 where |
|
272 "isS0 c = (let (s, l, r) = c in s = 0)" |
|
273 |
|
274 declare tstep.simps[simp del] steps.simps[simp del] |
|
275 t_add.simps[simp del] fetch.simps[simp del] |
|
276 new_tape.simps[simp del] |
|
277 |
|
278 |
|
279 text {* |
|
280 Single step execution starting from state @{text "0"} will not make any progress. |
|
281 *} |
|
282 lemma tstep_0: "tstep (0, tp) p = (0, tp)" |
|
283 apply(simp add: tstep.simps fetch.simps new_tape.simps) |
|
284 done |
|
285 |
|
286 |
|
287 text {* |
|
288 Many step executions starting from state @{text "0"} will not make any progress. |
|
289 *} |
|
290 |
|
291 lemma steps_0: "steps (0, tp) p stp = (0, tp)" |
|
292 apply(induct stp) |
|
293 apply(simp add: steps.simps) |
|
294 apply(simp add: tstep_red tstep_0) |
|
295 done |
|
296 |
|
297 lemma s_keep_step: "\<lbrakk>a \<le> length A div 2; tstep (a, b, c) A = (s, l, r); t_correct A\<rbrakk> |
|
298 \<Longrightarrow> s \<le> length A div 2" |
|
299 apply(simp add: tstep.simps fetch.simps t_correct.simps iseven_def |
|
300 split: if_splits block.splits list.splits) |
|
301 apply(case_tac [!] a, auto simp: list_all_length) |
|
302 apply(erule_tac x = "2 * nat" in allE, auto) |
|
303 apply(erule_tac x = "2 * nat" in allE, auto) |
|
304 apply(erule_tac x = "Suc (2 * nat)" in allE, auto) |
|
305 done |
|
306 |
|
307 lemma s_keep: "\<lbrakk>steps (Suc 0, tp) A stp = (s, l, r); t_correct A\<rbrakk> \<Longrightarrow> s \<le> length A div 2" |
|
308 proof(induct stp arbitrary: s l r) |
|
309 case 0 thus "?case" by(auto simp: t_correct.simps steps.simps) |
|
310 next |
|
311 fix stp s l r |
|
312 assume ind: "\<And>s l r. \<lbrakk>steps (Suc 0, tp) A stp = (s, l, r); t_correct A\<rbrakk> \<Longrightarrow> s \<le> length A div 2" |
|
313 and h1: "steps (Suc 0, tp) A (Suc stp) = (s, l, r)" |
|
314 and h2: "t_correct A" |
|
315 from h1 h2 show "s \<le> length A div 2" |
|
316 proof(simp add: tstep_red, cases "(steps (Suc 0, tp) A stp)", simp) |
|
317 fix a b c |
|
318 assume h3: "tstep (a, b, c) A = (s, l, r)" |
|
319 and h4: "steps (Suc 0, tp) A stp = (a, b, c)" |
|
320 have "a \<le> length A div 2" |
|
321 using h2 h4 |
|
322 by(rule_tac l = b and r = c in ind, auto) |
|
323 thus "?thesis" |
|
324 using h3 h2 |
|
325 by(simp add: s_keep_step) |
|
326 qed |
|
327 qed |
|
328 |
|
329 lemma t_merge_fetch_pre: |
|
330 "\<lbrakk>fetch A s b = (ac, ns); s \<le> length A div 2; t_correct A; s \<noteq> 0\<rbrakk> \<Longrightarrow> |
|
331 fetch (A |+| B) s b = (ac, if ns = 0 then Suc (length A div 2) else ns)" |
|
332 apply(subgoal_tac "2 * (s - Suc 0) < length A \<and> Suc (2 * (s - Suc 0)) < length A") |
|
333 apply(auto simp: fetch.simps t_add.simps split: if_splits block.splits) |
|
334 apply(simp_all add: nth_append change_termi_state.simps) |
|
335 done |
|
336 |
|
337 lemma [simp]: "\<lbrakk>\<not> a \<le> length A div 2; t_correct A\<rbrakk> \<Longrightarrow> fetch A a b = (Nop, 0)" |
|
338 apply(auto simp: fetch.simps del: nth_of.simps split: block.splits) |
|
339 apply(case_tac [!] a, auto simp: t_correct.simps iseven_def) |
|
340 done |
|
341 |
|
342 lemma [elim]: "\<lbrakk>t_correct A; \<not> isS0 (tstep (a, b, c) A)\<rbrakk> \<Longrightarrow> a \<le> length A div 2" |
|
343 apply(rule_tac classical, auto simp: tstep.simps new_tape.simps isS0_def) |
|
344 done |
|
345 |
|
346 lemma [elim]: "\<lbrakk>t_correct A; \<not> isS0 (tstep (a, b, c) A)\<rbrakk> \<Longrightarrow> 0 < a" |
|
347 apply(rule_tac classical, simp add: tstep_0 isS0_def) |
|
348 done |
|
349 |
|
350 |
|
351 lemma t_merge_pre_eq_step: "\<lbrakk>tstep (a, b, c) A = cf; t_correct A; \<not> isS0 cf\<rbrakk> |
|
352 \<Longrightarrow> tstep (a, b, c) (A |+| B) = cf" |
|
353 apply(subgoal_tac "a \<le> length A div 2 \<and> a \<noteq> 0") |
|
354 apply(simp add: tstep.simps) |
|
355 apply(case_tac "fetch A a (case c of [] \<Rightarrow> Bk | x # xs \<Rightarrow> x)", simp) |
|
356 apply(drule_tac B = B in t_merge_fetch_pre, simp, simp, simp, simp add: isS0_def, auto) |
|
357 done |
|
358 |
|
359 lemma t_merge_pre_eq: "\<lbrakk>steps (Suc 0, tp) A stp = cf; \<not> isS0 cf; t_correct A\<rbrakk> |
|
360 \<Longrightarrow> steps (Suc 0, tp) (A |+| B) stp = cf" |
|
361 proof(induct stp arbitrary: cf) |
|
362 case 0 thus "?case" by(simp add: steps.simps) |
|
363 next |
|
364 fix stp cf |
|
365 assume ind: "\<And>cf. \<lbrakk>steps (Suc 0, tp) A stp = cf; \<not> isS0 cf; t_correct A\<rbrakk> |
|
366 \<Longrightarrow> steps (Suc 0, tp) (A |+| B) stp = cf" |
|
367 and h1: "steps (Suc 0, tp) A (Suc stp) = cf" |
|
368 and h2: "\<not> isS0 cf" |
|
369 and h3: "t_correct A" |
|
370 from h1 h2 h3 show "steps (Suc 0, tp) (A |+| B) (Suc stp) = cf" |
|
371 proof(simp add: tstep_red, cases "steps (Suc 0, tp) (A) stp", simp) |
|
372 fix a b c |
|
373 assume h4: "tstep (a, b, c) A = cf" |
|
374 and h5: "steps (Suc 0, tp) A stp = (a, b, c)" |
|
375 have "steps (Suc 0, tp) (A |+| B) stp = (a, b, c)" |
|
376 proof(cases a) |
|
377 case 0 thus "?thesis" |
|
378 using h4 h2 |
|
379 apply(simp add: tstep_0, cases cf, simp add: isS0_def) |
|
380 done |
|
381 next |
|
382 case (Suc n) thus "?thesis" |
|
383 using h5 h3 |
|
384 apply(rule_tac ind, auto simp: isS0_def) |
|
385 done |
|
386 qed |
|
387 thus "tstep (steps (Suc 0, tp) (A |+| B) stp) (A |+| B) = cf" |
|
388 using h4 h5 h3 h2 |
|
389 apply(simp) |
|
390 apply(rule t_merge_pre_eq_step, auto) |
|
391 done |
|
392 qed |
|
393 qed |
|
394 |
|
395 declare nth.simps[simp del] tshift.simps[simp del] change_termi_state.simps[simp del] |
|
396 |
|
397 lemma [simp]: "length (change_termi_state A) = length A" |
|
398 by(simp add: change_termi_state.simps) |
|
399 |
|
400 lemma first_halt_point: "steps (Suc 0, tp) A stp = (0, tp') |
|
401 \<Longrightarrow> \<exists>stp. \<not> isS0 (steps (Suc 0, tp) A stp) \<and> steps (Suc 0, tp) A (Suc stp) = (0, tp')" |
|
402 proof(induct stp) |
|
403 case 0 thus "?case" by(simp add: steps.simps) |
|
404 next |
|
405 case (Suc n) |
|
406 fix stp |
|
407 assume ind: "steps (Suc 0, tp) A stp = (0, tp') \<Longrightarrow> |
|
408 \<exists>stp. \<not> isS0 (steps (Suc 0, tp) A stp) \<and> steps (Suc 0, tp) A (Suc stp) = (0, tp')" |
|
409 and h: "steps (Suc 0, tp) A (Suc stp) = (0, tp')" |
|
410 from h show "\<exists>stp. \<not> isS0 (steps (Suc 0, tp) A stp) \<and> steps (Suc 0, tp) A (Suc stp) = (0, tp')" |
|
411 proof(simp add: tstep_red, cases "steps (Suc 0, tp) A stp", simp, case_tac a) |
|
412 fix a b c |
|
413 assume g1: "a = (0::nat)" |
|
414 and g2: "tstep (a, b, c) A = (0, tp')" |
|
415 and g3: "steps (Suc 0, tp) A stp = (a, b, c)" |
|
416 have "steps (Suc 0, tp) A stp = (0, tp')" |
|
417 using g2 g1 g3 |
|
418 by(simp add: tstep_0) |
|
419 hence "\<exists> stp. \<not> isS0 (steps (Suc 0, tp) A stp) \<and> steps (Suc 0, tp) A (Suc stp) = (0, tp')" |
|
420 by(rule ind) |
|
421 thus "\<exists>stp. \<not> isS0 (steps (Suc 0, tp) A stp) \<and> tstep (steps (Suc 0, tp) A stp) A = (0, tp')" |
|
422 apply(simp add: tstep_red) |
|
423 done |
|
424 next |
|
425 fix a b c nat |
|
426 assume g1: "steps (Suc 0, tp) A stp = (a, b, c)" |
|
427 and g2: "steps (Suc 0, tp) A (Suc stp) = (0, tp')" "a= Suc nat" |
|
428 thus "\<exists>stp. \<not> isS0 (steps (Suc 0, tp) A stp) \<and> tstep (steps (Suc 0, tp) A stp) A = (0, tp')" |
|
429 apply(rule_tac x = stp in exI) |
|
430 apply(simp add: isS0_def tstep_red) |
|
431 done |
|
432 qed |
|
433 qed |
|
434 |
|
435 lemma t_merge_pre_halt_same': |
|
436 "\<lbrakk>\<not> isS0 (steps (Suc 0, tp) A stp) ; steps (Suc 0, tp) A (Suc stp) = (0, tp'); t_correct A\<rbrakk> |
|
437 \<Longrightarrow> steps (Suc 0, tp) (A |+| B) (Suc stp) = (Suc (length A div 2), tp')" |
|
438 proof(simp add: tstep_red, cases "steps (Suc 0, tp) A stp", simp) |
|
439 fix a b c |
|
440 assume h1: "\<not> isS0 (a, b, c)" |
|
441 and h2: "tstep (a, b, c) A = (0, tp')" |
|
442 and h3: "t_correct A" |
|
443 and h4: "steps (Suc 0, tp) A stp = (a, b, c)" |
|
444 have "steps (Suc 0, tp) (A |+| B) stp = (a, b, c)" |
|
445 using h1 h4 h3 |
|
446 apply(rule_tac t_merge_pre_eq, auto) |
|
447 done |
|
448 moreover have "tstep (a, b, c) (A |+| B) = (Suc (length A div 2), tp')" |
|
449 using h2 h3 h1 h4 |
|
450 apply(simp add: tstep.simps) |
|
451 apply(case_tac " fetch A a (case c of [] \<Rightarrow> Bk | x # xs \<Rightarrow> x)", simp) |
|
452 apply(drule_tac B = B in t_merge_fetch_pre, auto simp: isS0_def intro: s_keep) |
|
453 done |
|
454 ultimately show "tstep (steps (Suc 0, tp) (A |+| B) stp) (A |+| B) = (Suc (length A div 2), tp')" |
|
455 by(simp) |
|
456 qed |
|
457 |
|
458 text {* |
|
459 When Turing machine @{text "A"} and @{text "B"} are combined and the execution |
|
460 of @{text "A"} can termination within @{text "stp"} steps, |
|
461 the combined machine @{text "A |+| B"} will eventually get into the starting |
|
462 state of machine @{text "B"}. |
|
463 *} |
|
464 lemma t_merge_pre_halt_same: " |
|
465 \<lbrakk>steps (Suc 0, tp) A stp = (0, tp'); t_correct A; t_correct B\<rbrakk> |
|
466 \<Longrightarrow> \<exists> stp. steps (Suc 0, tp) (A |+| B) stp = (Suc (length A div 2), tp')" |
|
467 proof - |
|
468 assume a_wf: "t_correct A" |
|
469 and b_wf: "t_correct B" |
|
470 and a_ht: "steps (Suc 0, tp) A stp = (0, tp')" |
|
471 have halt_point: "\<exists> stp. \<not> isS0 (steps (Suc 0, tp) A stp) \<and> steps (Suc 0, tp) A (Suc stp) = (0, tp')" |
|
472 using a_ht |
|
473 by(erule_tac first_halt_point) |
|
474 then obtain stp' where "\<not> isS0 (steps (Suc 0, tp) A stp') \<and> steps (Suc 0, tp) A (Suc stp') = (0, tp')".. |
|
475 hence "steps (Suc 0, tp) (A |+| B) (Suc stp') = (Suc (length A div 2), tp')" |
|
476 using a_wf |
|
477 apply(rule_tac t_merge_pre_halt_same', auto) |
|
478 done |
|
479 thus "?thesis" .. |
|
480 qed |
|
481 |
|
482 lemma fetch_0: "fetch p 0 b = (Nop, 0)" |
|
483 by(simp add: fetch.simps) |
|
484 |
|
485 lemma [simp]: "length (tshift B x) = length B" |
|
486 by(simp add: tshift.simps) |
|
487 |
|
488 lemma [simp]: "t_correct A \<Longrightarrow> 2 * (length A div 2) = length A" |
|
489 apply(simp add: t_correct.simps iseven_def, auto) |
|
490 done |
|
491 |
|
492 lemma t_merge_fetch_snd: |
|
493 "\<lbrakk>fetch B a b = (ac, ns); t_correct A; t_correct B; a > 0 \<rbrakk> |
|
494 \<Longrightarrow> fetch (A |+| B) (a + length A div 2) b |
|
495 = (ac, if ns = 0 then 0 else ns + length A div 2)" |
|
496 apply(auto simp: fetch.simps t_add.simps split: if_splits block.splits) |
|
497 apply(case_tac [!] a, simp_all) |
|
498 apply(simp_all add: nth_append change_termi_state.simps tshift.simps) |
|
499 done |
|
500 |
|
501 lemma t_merge_snd_eq_step: |
|
502 "\<lbrakk>tstep (s, l, r) B = (s', l', r'); t_correct A; t_correct B; s > 0\<rbrakk> |
|
503 \<Longrightarrow> tstep (s + length A div 2, l, r) (A |+| B) = |
|
504 (if s' = 0 then 0 else s' + length A div 2, l' ,r') " |
|
505 apply(simp add: tstep.simps) |
|
506 apply(cases "fetch B s (case r of [] \<Rightarrow> Bk | x # xs \<Rightarrow> x)") |
|
507 apply(auto simp: t_merge_fetch_snd) |
|
508 apply(frule_tac [!] t_merge_fetch_snd, auto) |
|
509 done |
|
510 |
|
511 text {* |
|
512 Relates the executions of TM @{text "B"}, one is when @{text "B"} is executed alone, |
|
513 the other is the execution when @{text "B"} is in the combined TM. |
|
514 *} |
|
515 lemma t_merge_snd_eq_steps: |
|
516 "\<lbrakk>t_correct A; t_correct B; steps (s, l, r) B stp = (s', l', r'); s > 0\<rbrakk> |
|
517 \<Longrightarrow> steps (s + length A div 2, l, r) (A |+| B) stp = |
|
518 (if s' = 0 then 0 else s' + length A div 2, l', r')" |
|
519 proof(induct stp arbitrary: s' l' r') |
|
520 case 0 thus "?case" |
|
521 by(simp add: steps.simps) |
|
522 next |
|
523 fix stp s' l' r' |
|
524 assume ind: "\<And>s' l' r'. \<lbrakk>t_correct A; t_correct B; steps (s, l, r) B stp = (s', l', r'); 0 < s\<rbrakk> |
|
525 \<Longrightarrow> steps (s + length A div 2, l, r) (A |+| B) stp = |
|
526 (if s' = 0 then 0 else s' + length A div 2, l', r')" |
|
527 and h1: "steps (s, l, r) B (Suc stp) = (s', l', r')" |
|
528 and h2: "t_correct A" |
|
529 and h3: "t_correct B" |
|
530 and h4: "0 < s" |
|
531 from h1 show "steps (s + length A div 2, l, r) (A |+| B) (Suc stp) |
|
532 = (if s' = 0 then 0 else s' + length A div 2, l', r')" |
|
533 proof(simp only: tstep_red, cases "steps (s, l, r) B stp") |
|
534 fix a b c |
|
535 assume h5: "steps (s, l, r) B stp = (a, b, c)" "tstep (steps (s, l, r) B stp) B = (s', l', r')" |
|
536 hence h6: "(steps (s + length A div 2, l, r) (A |+| B) stp) = |
|
537 ((if a = 0 then 0 else a + length A div 2, b, c))" |
|
538 using h2 h3 h4 |
|
539 by(rule_tac ind, auto) |
|
540 thus "tstep (steps (s + length A div 2, l, r) (A |+| B) stp) (A |+| B) = |
|
541 (if s' = 0 then 0 else s'+ length A div 2, l', r')" |
|
542 using h5 |
|
543 proof(auto) |
|
544 assume "tstep (0, b, c) B = (0, l', r')" thus "tstep (0, b, c) (A |+| B) = (0, l', r')" |
|
545 by(simp add: tstep_0) |
|
546 next |
|
547 assume "tstep (0, b, c) B = (s', l', r')" "0 < s'" |
|
548 thus "tstep (0, b, c) (A |+| B) = (s' + length A div 2, l', r')" |
|
549 by(simp add: tstep_0) |
|
550 next |
|
551 assume "tstep (a, b, c) B = (0, l', r')" "0 < a" |
|
552 thus "tstep (a + length A div 2, b, c) (A |+| B) = (0, l', r')" |
|
553 using h2 h3 |
|
554 by(drule_tac t_merge_snd_eq_step, auto) |
|
555 next |
|
556 assume "tstep (a, b, c) B = (s', l', r')" "0 < a" "0 < s'" |
|
557 thus "tstep (a + length A div 2, b, c) (A |+| B) = (s' + length A div 2, l', r')" |
|
558 using h2 h3 |
|
559 by(drule_tac t_merge_snd_eq_step, auto) |
|
560 qed |
|
561 qed |
|
562 qed |
|
563 |
|
564 lemma t_merge_snd_halt_eq: |
|
565 "\<lbrakk>steps (Suc 0, tp) B stp = (0, tp'); t_correct A; t_correct B\<rbrakk> |
|
566 \<Longrightarrow> \<exists>stp. steps (Suc (length A div 2), tp) (A |+| B) stp = (0, tp')" |
|
567 apply(case_tac tp, cases tp', simp) |
|
568 apply(drule_tac s = "Suc 0" in t_merge_snd_eq_steps, auto) |
|
569 done |
|
570 |
|
571 lemma t_inj: "\<lbrakk>steps (Suc 0, tp) A stpa = (0, tp1); steps (Suc 0, tp) A stpb = (0, tp2)\<rbrakk> |
|
572 \<Longrightarrow> tp1 = tp2" |
|
573 proof - |
|
574 assume h1: "steps (Suc 0, tp) A stpa = (0, tp1)" |
|
575 and h2: "steps (Suc 0, tp) A stpb = (0, tp2)" |
|
576 thus "?thesis" |
|
577 proof(cases "stpa < stpb") |
|
578 case True thus "?thesis" |
|
579 using h1 h2 |
|
580 apply(drule_tac less_imp_Suc_add, auto) |
|
581 apply(simp del: add_Suc_right add_Suc add: add_Suc_right[THEN sym] steps_add steps_0) |
|
582 done |
|
583 next |
|
584 case False thus "?thesis" |
|
585 using h1 h2 |
|
586 apply(drule_tac leI) |
|
587 apply(case_tac "stpb = stpa", auto) |
|
588 apply(subgoal_tac "stpb < stpa") |
|
589 apply(drule_tac less_imp_Suc_add, auto) |
|
590 apply(simp del: add_Suc_right add_Suc add: add_Suc_right[THEN sym] steps_add steps_0) |
|
591 done |
|
592 qed |
|
593 qed |
|
594 |
|
595 type_synonym t_assert = "tape \<Rightarrow> bool" |
|
596 |
|
597 definition t_imply :: "t_assert \<Rightarrow> t_assert \<Rightarrow> bool" ("_ \<turnstile>-> _" [0, 0] 100) |
|
598 where |
|
599 "t_imply a1 a2 = (\<forall> tp. a1 tp \<longrightarrow> a2 tp)" |
|
600 |
|
601 |
|
602 locale turing_merge = |
|
603 fixes A :: "tprog" and B :: "tprog" and P1 :: "t_assert" |
|
604 and P2 :: "t_assert" |
|
605 and P3 :: "t_assert" |
|
606 and P4 :: "t_assert" |
|
607 and Q1:: "t_assert" |
|
608 and Q2 :: "t_assert" |
|
609 assumes |
|
610 A_wf : "t_correct A" |
|
611 and B_wf : "t_correct B" |
|
612 and A_halt : "P1 tp \<Longrightarrow> \<exists> stp. let (s, tp') = steps (Suc 0, tp) A stp in s = 0 \<and> Q1 tp'" |
|
613 and B_halt : "P2 tp \<Longrightarrow> \<exists> stp. let (s, tp') = steps (Suc 0, tp) B stp in s = 0 \<and> Q2 tp'" |
|
614 and A_uhalt : "P3 tp \<Longrightarrow> \<not> (\<exists> stp. isS0 (steps (Suc 0, tp) A stp))" |
|
615 and B_uhalt: "P4 tp \<Longrightarrow> \<not> (\<exists> stp. isS0 (steps (Suc 0, tp) B stp))" |
|
616 begin |
|
617 |
|
618 |
|
619 text {* |
|
620 The following lemma tries to derive the Hoare logic rule for sequentially combined TMs. |
|
621 It deals with the situtation when both @{text "A"} and @{text "B"} are terminated. |
|
622 *} |
|
623 |
|
624 lemma t_merge_halt: |
|
625 assumes aimpb: "Q1 \<turnstile>-> P2" |
|
626 shows "P1 \<turnstile>-> \<lambda> tp. (\<exists> stp tp'. steps (Suc 0, tp) (A |+| B) stp = (0, tp') \<and> Q2 tp')" |
|
627 proof(simp add: t_imply_def, auto) |
|
628 fix a b |
|
629 assume h: "P1 (a, b)" |
|
630 hence "\<exists> stp. let (s, tp') = steps (Suc 0, a, b) A stp in s = 0 \<and> Q1 tp'" |
|
631 using A_halt by simp |
|
632 from this obtain stp1 where "let (s, tp') = steps (Suc 0, a, b) A stp1 in s = 0 \<and> Q1 tp'" .. |
|
633 thus "\<exists>stp aa ba. steps (Suc 0, a, b) (A |+| B) stp = (0, aa, ba) \<and> Q2 (aa, ba)" |
|
634 proof(case_tac "steps (Suc 0, a, b) A stp1", simp, erule_tac conjE) |
|
635 fix aa ba c |
|
636 assume g1: "Q1 (ba, c)" |
|
637 and g2: "steps (Suc 0, a, b) A stp1 = (0, ba, c)" |
|
638 hence "P2 (ba, c)" |
|
639 using aimpb apply(simp add: t_imply_def) |
|
640 done |
|
641 hence "\<exists> stp. let (s, tp') = steps (Suc 0, ba, c) B stp in s = 0 \<and> Q2 tp'" |
|
642 using B_halt by simp |
|
643 from this obtain stp2 where "let (s, tp') = steps (Suc 0, ba, c) B stp2 in s = 0 \<and> Q2 tp'" .. |
|
644 thus "?thesis" |
|
645 proof(case_tac "steps (Suc 0, ba, c) B stp2", simp, erule_tac conjE) |
|
646 fix aa bb ca |
|
647 assume g3: " Q2 (bb, ca)" "steps (Suc 0, ba, c) B stp2 = (0, bb, ca)" |
|
648 have "\<exists> stp. steps (Suc 0, a, b) (A |+| B) stp = (Suc (length A div 2), ba , c)" |
|
649 using g2 A_wf B_wf |
|
650 by(rule_tac t_merge_pre_halt_same, auto) |
|
651 moreover have "\<exists> stp. steps (Suc (length A div 2), ba, c) (A |+| B) stp = (0, bb, ca)" |
|
652 using g3 A_wf B_wf |
|
653 apply(rule_tac t_merge_snd_halt_eq, auto) |
|
654 done |
|
655 ultimately show "\<exists>stp aa ba. steps (Suc 0, a, b) (A |+| B) stp = (0, aa, ba) \<and> Q2 (aa, ba)" |
|
656 apply(erule_tac exE, erule_tac exE) |
|
657 apply(rule_tac x = "stp + stpa" in exI, simp add: steps_add) |
|
658 using g3 by simp |
|
659 qed |
|
660 qed |
|
661 qed |
|
662 |
|
663 lemma t_merge_uhalt_tmp: |
|
664 assumes B_uh: "\<forall>stp. \<not> isS0 (steps (Suc 0, b, c) B stp)" |
|
665 and merge_ah: "steps (Suc 0, tp) (A |+| B) stpa = (Suc (length A div 2), b, c)" |
|
666 shows "\<forall> stp. \<not> isS0 (steps (Suc 0, tp) (A |+| B) stp)" |
|
667 using B_uh merge_ah |
|
668 apply(rule_tac allI) |
|
669 apply(case_tac "stp > stpa") |
|
670 apply(erule_tac x = "stp - stpa" in allE) |
|
671 apply(case_tac "(steps (Suc 0, b, c) B (stp - stpa))", simp) |
|
672 proof - |
|
673 fix stp a ba ca |
|
674 assume h1: "\<not> isS0 (a, ba, ca)" "stpa < stp" |
|
675 and h2: "steps (Suc 0, b, c) B (stp - stpa) = (a, ba, ca)" |
|
676 have "steps (Suc 0 + length A div 2, b, c) (A |+| B) (stp - stpa) = |
|
677 (if a = 0 then 0 else a + length A div 2, ba, ca)" |
|
678 using A_wf B_wf h2 |
|
679 by(rule_tac t_merge_snd_eq_steps, auto) |
|
680 moreover have "a > 0" using h1 by(simp add: isS0_def) |
|
681 moreover have "\<exists> stpb. stp = stpa + stpb" |
|
682 using h1 by(rule_tac x = "stp - stpa" in exI, simp) |
|
683 ultimately show "\<not> isS0 (steps (Suc 0, tp) (A |+| B) stp)" |
|
684 using merge_ah |
|
685 by(auto simp: steps_add isS0_def) |
|
686 next |
|
687 fix stp |
|
688 assume h: "steps (Suc 0, tp) (A |+| B) stpa = (Suc (length A div 2), b, c)" "\<not> stpa < stp" |
|
689 hence "\<exists> stpb. stpa = stp + stpb" apply(rule_tac x = "stpa - stp" in exI, auto) done |
|
690 thus "\<not> isS0 (steps (Suc 0, tp) (A |+| B) stp)" |
|
691 using h |
|
692 apply(auto) |
|
693 apply(cases "steps (Suc 0, tp) (A |+| B) stp", simp add: steps_add isS0_def steps_0) |
|
694 done |
|
695 qed |
|
696 |
|
697 text {* |
|
698 The following lemma deals with the situation when TM @{text "B"} can not terminate. |
|
699 *} |
|
700 |
|
701 lemma t_merge_uhalt: |
|
702 assumes aimpb: "Q1 \<turnstile>-> P4" |
|
703 shows "P1 \<turnstile>-> \<lambda> tp. \<not> (\<exists> stp. isS0 (steps (Suc 0, tp) (A |+| B) stp))" |
|
704 proof(simp only: t_imply_def, rule_tac allI, rule_tac impI) |
|
705 fix tp |
|
706 assume init_asst: "P1 tp" |
|
707 show "\<not> (\<exists>stp. isS0 (steps (Suc 0, tp) (A |+| B) stp))" |
|
708 proof - |
|
709 have "\<exists> stp. let (s, tp') = steps (Suc 0, tp) A stp in s = 0 \<and> Q1 tp'" |
|
710 using A_halt[of tp] init_asst |
|
711 by(simp) |
|
712 from this obtain stpx where "let (s, tp') = steps (Suc 0, tp) A stpx in s = 0 \<and> Q1 tp'" .. |
|
713 thus "?thesis" |
|
714 proof(cases "steps (Suc 0, tp) A stpx", simp, erule_tac conjE) |
|
715 fix a b c |
|
716 assume "Q1 (b, c)" |
|
717 and h3: "steps (Suc 0, tp) A stpx = (0, b, c)" |
|
718 hence h2: "P4 (b, c)" using aimpb |
|
719 by(simp add: t_imply_def) |
|
720 have "\<exists> stp. steps (Suc 0, tp) (A |+| B) stp = (Suc (length A div 2), b, c)" |
|
721 using h3 A_wf B_wf |
|
722 apply(rule_tac stp = stpx in t_merge_pre_halt_same, auto) |
|
723 done |
|
724 from this obtain stpa where h4:"steps (Suc 0, tp) (A |+| B) stpa = (Suc (length A div 2), b, c)" .. |
|
725 have " \<not> (\<exists> stp. isS0 (steps (Suc 0, b, c) B stp))" |
|
726 using B_uhalt [of "(b, c)"] h2 apply simp |
|
727 done |
|
728 from this and h4 show "\<forall>stp. \<not> isS0 (steps (Suc 0, tp) (A |+| B) stp)" |
|
729 by(rule_tac t_merge_uhalt_tmp, auto) |
|
730 qed |
|
731 qed |
|
732 qed |
|
733 end |
|
734 |
|
735 end |
|
736 |
|