1 (* include "turing/auxiliary_machines.ma". *)
3 include "turing/multi_to_mono/shift_trace.ma".
5 (******************************************************************************)
7 (* exec_move_R : before shifting the trace left - to simulate a right move of
8 the head - we must be sure we are not in rightoverflow, that is that the symbol
9 at the right of the head, if any, is not blank *)
11 (* a simple look-ahead machine *)
12 definition mtestR ≝ λsig,test.
14 (ifTM ? (test_char ? test)
15 (single_finalTM ? (move sig L))
16 (move sig L) tc_true).
19 definition RmtestR_true ≝ λsig,test.λt1,t2.
21 t1 = midtape sig ls c rs →
22 ∃c1,rs1. rs = c1::rs1 ∧ t1 = t2 ∧ (test c1 = true).
24 definition RmtestR_false ≝ λsig,test.λt1,t2.
26 t1 = midtape sig ls c (c1::rs) →
27 t1 = t2 ∧ (test c1 = false)) ∧
29 t1 = midtape sig ls c [] → t1 = t2).
31 definition mtestR_acc: ∀sig,test.states ? (mtestR sig test).
32 #sig #test @inr @inr @inl @inr @start_nop
35 lemma sem_mtestR: ∀sig,test.
38 RmtestR_true sig test, RmtestR_false sig test].
40 @(acc_sem_seq_app sig … (sem_move_single … )
42 (sem_test_char sig test)
44 (sem_move_single … )))
45 [#t1 #t2 #t3 whd in ⊢ (%→?); #Hmove * #tx * whd in ⊢ (%→?); * *
46 #cx * #Hcx #H1cx #Htx #Ht2 #ls #c #rs #Ht1
47 >Ht1 in Hmove; cases rs
48 [#H >H in Hcx; whd in ⊢ (??%?→?); #H1 destruct (H1)
49 |#c1 #rs1 #Ht3 %{c1} %{rs1} %
50 [% [//|>Htx in Ht2; >Ht3 whd in ⊢ (%→?); #H @sym_eq @H ]
51 |>Ht3 in Hcx; whd in ⊢ (??%?→?); #H1 destruct (H1) //
54 |#t1 #t2 #t3 whd in ⊢ (%→?); #Hmove * #tx * whd in ⊢ (%→?); *
57 >Ht1 in Hmove; whd in match (tape_move ???); #Ht3
58 >Ht3 in Hcx; #Hcx lapply (Hcx ? (refl ??))
59 #Htest % // >Heqtx in Htx; >Ht3 whd in ⊢ (%→?); #H @sym_eq @H
60 |#ls0 #c0 #Ht1 >Ht1 in Hmove; whd in match (tape_move ???);
61 <Heqtx #H1tx >H1tx in Htx; #H @sym_eq @H
66 definition guarded_M ≝ λsig,n,i,M.
67 (ifTM ? (test_char ? (not_blank sig n i))
69 (ifTM ? (mtestR ? (not_blank sig n i))
71 (nop ?) (mtestR_acc …)) tc_true).
73 definition R_guarded_M ≝ λsig,n,i,RM,t1,t2.
74 ∀ls,a,rs. t1 = midtape ? ls a rs →
75 (not_blank sig n i a = false →
76 not_blank sig n i (hd ? rs (all_blanks …)) = false → t1=t2) ∧
77 (not_blank sig n i a = true ∨
78 not_blank sig n i (hd ? rs (all_blanks …)) = true → RM t1 t2).
80 lemma sem_R_guarded: ∀sig,n,i,M,RM. M ⊨ RM →
81 guarded_M sig n i M ⊨ R_guarded_M sig n i RM.
83 @(sem_if_app … (sem_test_char … ) HM
84 (sem_if … (sem_mtestR … ) HM (sem_nop ?)))
86 [* * * #c * #Hc #H1c #Ht1 #Htout #ls #a #rs #Htin
87 >Htin in Hc; normalize in ⊢ (%→?); #H1 destruct (H1) %
88 [>H1c #H2 @False_ind destruct (H2)
89 |#H1 <Htin <Ht1 @Htout
91 |* * #Hc #Ht1 #H #ls #a #rs lapply (Hc a) <Ht1 -Ht1 #Ha #Ht1
93 [* #t2 * #Ht2 lapply (Ht2 … Ht1)
94 * #c1 * #rs1 * * #H1 #H2 #H3 #H4 % [2: //]
95 #Ha >H1 whd in match (hd ???); >H3 #H destruct (H)
96 |lapply Ht1 -Ht1 cases rs
97 [#Ht1 * #t2 * * #_ #Ht2 lapply (Ht2 … Ht1) -Ht2 #Ht2
98 whd in ⊢ (%→?); #Htout % [//] *
99 [>Ha [#H destruct (H)| >Ht1 %]
100 |whd in ⊢ (??%?→?); >blank_all_blanks whd in ⊢ (??%?→?);
103 |#c #rs1 #Ht1 * #t2 * * #Ht2 #_ lapply (Ht2 … Ht1) -Ht2 *
104 #Ht2 whd in ⊢ (??%?→?); #Hnb whd in ⊢ (%→?); #Htout % [//] *
105 [>Ha [#H destruct (H)| >Ht1 %]
106 |whd in ⊢ (??%?→?); whd in match (hd ???); >Hnb whd in ⊢ (??%?→?);
114 definition move_R_i ≝ λA,sig,n,i.
115 guarded_M ? (S n) i (mtiL A sig n i).
117 definition Rmove_R_i ≝ λA,sig,n,i.
118 R_guarded_M ? (S n) i (Rmtil A sig n i).
121 (*************************** readback of the tape *****************************)
123 definition opt_cur ≝ λsig,a.
124 if a == blank sig then None ? else Some ? a.
126 definition rb_trace ≝ λsig,ls,a,rs.
127 mk_tape ? (to_blank ? ls) (opt_cur sig a) (to_blank ? rs).
129 lemma rb_trace_def: ∀sig,ls,a,rs.
130 rb_trace sig ls a rs =
131 mk_tape ? (to_blank ? ls) (opt_cur sig a) (to_blank ? rs).
134 definition rb_trace_i ≝ λsig,n,ls,a,rs,i.
135 rb_trace sig (trace ? n i ls) (nth i ? a (blank ?)) (trace ? n i rs).
137 lemma rb_trace_i_def: ∀sig,n,ls,a,rs,i.
138 rb_trace_i sig n ls a rs i =
139 rb_trace sig (trace ? n i ls) (nth i ? a (blank ?)) (trace ? n i rs).
142 let rec readback sig n ls a rs i on i : Vector (tape (sig_ext sig)) i ≝
144 [ O ⇒ mk_Vector ? 0 (nil ?) (refl ??)
145 | S j ⇒ vec_cons ? (rb_trace_i sig n ls a rs j) j (readback sig n ls a rs j)
148 lemma orb_false_l: ∀b1,b2:bool.
149 (b1 ∨ b2) = false → (b1 = false) ∧ (b2 = false).
150 * * normalize /2/ qed.
152 lemma no_blank_true_to_not_blank: ∀sig,n,a,i.
153 (not_blank sig n i a = true) → nth i ? (vec … n a) (blank sig) ≠ blank ?.
154 #sig #n #a #i #H @(not_to_not … (eqnot_to_noteq … false H))
158 lemma rb_move_R : ∀A,sig,n,ls,a,rs,outt,i.
159 (∀i.regular_trace (TA A sig) (S n) a ls rs i) →
160 is_head ?? (nth n ? (vec … a) (blank ?)) = true →
163 Rmove_R_i … i (midtape ? ls a rs) outt →
165 outt = midtape ? ls1 a1 rs1 ∧
166 (∀i.regular_trace (TA A sig) (S n) a1 ls1 rs1 i) ∧
167 rb_trace_i ? (S n) ls1 (vec … a1) rs1 i =
168 tape_move ? (rb_trace_i ? (S n) ls (vec … a) rs i) R ∧
170 rb_trace_i ? (S n) ls1 (vec … a1) rs1 j =
171 rb_trace_i ? (S n) ls (vec … a) rs j.
172 #A #sig #n #ls #a #rs #outt #i #Hreg #Hha #Hhls #Hhrs #Hmove
173 lapply (Hmove … (refl …)) -Hmove * #HMove1 #HMove2
174 cases (true_or_false (not_blank ? (S n) i a ∨
175 not_blank ? (S n) i (hd ? rs (all_blanks ? (S n)))))
176 [2: #Hcase cases (orb_false_l … Hcase) -Hcase #Hb1 #Hb2
177 lapply (HMove1 … Hb1 Hb2) #Hout %{ls} %{a} %{rs}
178 %[%[%[@sym_eq @Hout |@Hreg]
180 cut (nth i ? (vec … a) (blank ?) = blank ?)
181 [@(\P ?) @injective_notb @Hb1] #Ha >Ha
182 >rb_trace_def whd in match (opt_cur ??);
183 cut (to_blank ? (trace ? (S n) i rs) = [])
185 cases (to_blank ? (trace ? (S n) i ls)) //
189 lapply(HMove2 (orb_true_l … Hb) … (refl …) Hha Hreg ? Hhls Hhrs) -HMove2
190 [#Hb1 lapply Hb -Hb whd in match (not_blank ? (S n) i a);
191 >Hb1 #H @no_blank_true_to_not_blank @H]
192 * #ls1 * #a1 * #rs1 * * * * * #H1 #H2 #H3 #H4 #H5 #H6
195 |(* either a is blank or not *)
196 cases (true_or_false (not_blank ? (S n) i a)) #Hba
198 >rb_trace_i_def >rb_trace_def <to_blank_i_def >H5 >to_blank_cons_nb
199 [2: @no_blank_true_to_not_blank //]
200 lapply H6 -H6 #Hrs >(rb_trace_i_def … rs i) >rb_trace_def
201 <(to_blank_i_def … rs) <Hrs
202 cut (opt_cur ? (nth i ? (vec … a) (blank ?)) =
203 Some ? (nth i ? (vec … a) (blank ?))) [@daemon] #Ha >Ha
204 (* either a1 is blank or not *)
205 cases (true_or_false (not_blank ? (S n) i a1)) #Hba1
206 [cut (opt_cur ? (nth i ? (vec … a1) (blank ?)) =
207 Some ? (nth i ? (vec … a1) (blank ?))) [@daemon] #Ha1 >Ha1
208 >to_blank_cons_nb [%|@(\Pf ?) @injective_notb @Hba1]
209 |cut (opt_cur ? (nth i ? (vec … a1) (blank ?)) = None ?) [@daemon]
211 cut (to_blank_i … i (a1::rs1) = [ ]) [@daemon] #Ha1rs1 >Ha1rs1
212 cut (to_blank_i … i rs1 = [ ]) [@daemon] #Hrs1 <to_blank_i_def >Hrs1 %
214 |>rb_trace_i_def >rb_trace_def <to_blank_i_def (* >H5 >to_blank_cons_nb *)
215 >rb_trace_i_def >rb_trace_def <(to_blank_i_def … rs) <H6 >H5
216 cut (to_blank_i … i (a::ls) = [ ]) [@daemon] #Hals >Hals
217 cut (to_blank_i … i ls = [ ]) [@daemon] #Hls <(to_blank_i_def … ls) >Hls
218 cut (opt_cur ? (nth i ? (vec … a) (blank ?)) = None ?) [@daemon] #Ha >Ha
219 cut (nth i ? (vec … a1) (blank ?) ≠ blank ?) [@daemon] #Ha1
220 >(to_blank_cons_nb … Ha1)
221 cut (opt_cur ? (nth i ? (vec … a1) (blank ?)) =
222 Some ? (nth i ? (vec … a1) (blank ?))) [@daemon] -Ha1 #Ha1 >Ha1 %
226 #j #Hle #Hneq >rb_trace_i_def >rb_trace_i_def >rb_trace_def >rb_trace_def
227 <(to_blank_i_def … rs) <(to_blank_i_def … rs1) >(H4 j Hle Hneq)
228 cut ((to_blank_i ? (S n) j ls1 = to_blank_i ? (S n) j ls) ∧
229 (opt_cur ? (nth j ? (vec … a1) (blank ?)) =
230 opt_cur ? (nth j ? (vec … a) (blank ?))))
231 [@daemon] * #H7 #H8 <to_blank_i_def >H7 >H8 //
236 definition Rmove_R_i_rb ≝ λA,sig,n,i,t1,t2.
238 t1 = midtape ? ls a rs →
239 (∀i.regular_trace (TA A sig) (S n) a ls rs i) →
240 is_head ?? (nth n ? (vec … a) (blank ?)) = true →
244 t2 = midtape (MTA A sig (S n)) ls1 a1 rs1 ∧
245 (∀i.regular_trace (TA A sig) (S n) a1 ls1 rs1 i) ∧
246 rb_trace_i ? (S n) ls1 (vec … a1) rs1 i =
247 tape_move ? (rb_trace_i ? (S n) ls (vec … a) rs i) R ∧
249 rb_trace_i ? (S n) ls1 (vec … a1) rs1 j =
250 rb_trace_i ? (S n) ls (vec … a) rs j.
252 lemma sem_move_R_i : ∀A,sig,n,i.i < n →
253 move_R_i A sig n i ⊨ Rmove_R_i_rb A sig n i.
254 #A #sig #n #i #ltin @(Realize_to_Realize … (Rmove_R_i A sig n i))
255 [#t1 #t2 #H #ls #a #rs #H1 #H2 #H3 #H4 #H5 @rb_move_R // <H1 //
256 |@sem_R_guarded @sem_Rmtil //
260 (* move_L_i axiomatized *)
262 axiom move_L_i : ∀A,sig.∀n,i:nat.TM (MTA A sig (S n)).
264 definition Rmove_L_i_rb ≝ λA,sig,n,i,t1,t2.
266 t1 = midtape ? ls a rs →
267 (∀i.regular_trace (TA A sig) (S n) a ls rs i) →
268 is_head ?? (nth n ? (vec … a) (blank ?)) = true →
272 t2 = midtape (MTA A sig (S n)) ls1 a1 rs1 ∧
273 (∀i.regular_trace ? (S n) a1 ls1 rs1 i) ∧
274 rb_trace_i ? (S n) ls1 (vec … a1) rs1 i =
275 tape_move ? (rb_trace_i ? (S n) ls (vec … a) rs i) L ∧
277 rb_trace_i ? (S n) ls1 (vec … a1) rs1 j =
278 rb_trace_i ? (S n) ls (vec … a) rs j.
280 axiom sem_move_L_i : ∀A,sig,n,i.i < n →
281 move_L_i A sig n i ⊨ Rmove_L_i_rb A sig n i.
284 lemma rb_move_L : ∀sig,n,ls,a,rs,outt,i.
285 (∀i.regular_trace sig n a ls rs i) →
286 nth n ? (vec … a) (blank ?) = head ? →
289 Rmove_L_i … i (midtape ? ls a rs) outt →
291 outt = midtape ? ls1 a1 rs1 ∧
292 rb_trace_i sig n ls1 (vec … a1) rs1 i =
293 tape_move ? (rb_trace_i sig n ls (vec … a) rs i) L ∧
295 rb_trace_i sig n ls1 (vec … a1) rs1 j =
296 rb_trace_i sig n ls (vec … a) rs j. *)
298 (* The following function read a move from a vector of moves and executes it *)
300 (* The head character contains a state and a sequence of moves *)
302 definition HC ≝ λQ,n.FinProd Q (FinVector FinMove n).
304 let rec all_N n on n : FinVector FinMove n ≝
306 [ O ⇒ Vector_of_list ? []
307 | S m ⇒ vec_cons ? N m (all_N m)
310 definition get_moves ≝ λQ,sig,n.λa:MTA (HC Q n) sig (S n).
311 match nth n ? (vec … a) (blank ?) with
313 | Some x ⇒ match x with
317 definition get_move ≝ λQ,sig,n.λa:MTA (HC Q n) sig (S n).λi.
318 nth i ? (vec … (get_moves Q sig n a)) N.
320 definition exec_move_i ≝ λQ,sig,n,i.
321 (ifTM ? (test_char ? (λa. (get_move Q sig n a i == R)))
322 (move_R_i (HC Q n) sig n i)
323 (ifTM ? (test_char ? (λa. (get_move Q sig n a i == L)))
324 (move_L_i (HC Q n) sig n i)
325 (nop ?) tc_true) tc_true).
327 definition R_exec_move_i ≝ λQ,sig,n,i,t1,t2.
329 t1 = midtape (MTA (HC Q n) sig (S n)) ls a rs →
330 (∀i.regular_trace ? (S n) a ls rs i) →
331 is_head ?? (nth n ? (vec … a) (blank ?)) = true →
335 t2 = midtape (MTA (HC Q n) sig (S n)) ls1 a1 rs1 ∧
336 (∀i.regular_trace ? (S n) a1 ls1 rs1 i) ∧
337 rb_trace_i ? (S n) ls1 (vec … a1) rs1 i =
338 tape_move ? (rb_trace_i ? (S n) ls (vec … a) rs i) (get_move Q sig n a i) ∧
340 rb_trace_i ? (S n) ls1 (vec … a1) rs1 j =
341 rb_trace_i ? (S n) ls (vec … a) rs j.
343 lemma tape_move_N: ∀A,t. tape_move A t N = t.
346 lemma sem_exec_move_i: ∀Q,sig,n,i. i < n →
347 exec_move_i Q sig n i ⊨ R_exec_move_i Q sig n i.
349 @(sem_if_app … (sem_test_char …)
350 (sem_move_R_i … ltin)
351 (sem_if … (sem_test_char …)
352 (sem_move_L_i … ltin) (sem_nop ?)))
354 [* * * #c * #Hc #HR #Ht1 #HMR
355 #a #ls #rs #Htin >Htin in Hc; whd in ⊢ (??%?→?); #H destruct (H)
356 >(\P HR) whd in HMR; @HMR >Ht1 @Htin
358 [* #t2 * * * #c * #Hc #HL #Ht2 #HML
359 #a #ls #rs #Htin >Htin in Hc; whd in ⊢ (??%?→?); #H destruct (H)
360 >(\P HL) @HML >Ht2 @Htin
361 |* #t2 * * #HL #Ht2 >Ht2 whd in ⊢ (%→?); #Htout >Htout
362 #a #ls #rs #Htin >Htin in HR; #HR >Htin in HL; #HL
363 cut (get_move Q sig n a i = N)
364 [lapply (HR a (refl … )) lapply (HL a (refl … ))
365 cases (get_move Q sig n a i) normalize
366 [#H destruct (H) |#_ #H destruct (H) | //]]
367 #HN >HN >tape_move_N #Hreg #_ #_ #_
369 %[%[%[%|@Hreg] |%] | //]
374 axiom reg_inv : ∀A,sig,n,a,ls,rs,a1,ls1,rs1.
375 regular_trace (TA A sig) (S n) a1 ls1 rs1 n →
376 (rb_trace_i ? (S n) ls1 (vec … a1) rs1 n =
377 rb_trace_i ? (S n) ls (vec … a) rs n) →
378 is_head ?? (nth n ? (vec … (S n) a) (blank ?)) = true →
381 is_head ?? (nth n ? (vec … a1) (blank ?)) = true ∧
382 no_head_in … ls1 ∧ no_head_in … rs1.