]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/universal/move_tape.ma
more typos
[helm.git] / matita / matita / lib / turing / universal / move_tape.ma
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic   
3     ||A||  Library of Mathematics, developed at the Computer Science 
4     ||T||  Department of the University of Bologna, Italy.           
5     ||I||                                                            
6     ||T||  
7     ||A||  
8     \   /  This file is distributed under the terms of the       
9      \ /   GNU General Public License Version 2   
10       V_____________________________________________________________*)
11
12 include "turing/universal/move_char_c.ma".
13 include "turing/universal/move_char_l.ma".
14 include "turing/universal/tuples.ma".
15
16 definition init_cell_states ≝ initN 2.
17
18 definition init_cell ≝ 
19  mk_TM STape init_cell_states
20  (λp.let 〈q,a〉 ≝ p in
21   match q with
22   [ O ⇒ match a with
23     [ None ⇒ 〈1, Some ? 〈〈null,false〉,N〉〉
24     | Some _ ⇒ 〈1, None ?〉 ]
25   | S _ ⇒ 〈1,None ?〉 ])
26  O (λq.q == 1).
27  
28 definition R_init_cell ≝ λt1,t2.
29  (∃c.current STape t1 = Some ? c ∧ t2 = t1) ∨
30  (current STape t1 = None ? ∧ t2 = midtape ? (left ? t1) 〈null,false〉 (right ? t1)).
31  
32 axiom sem_init_cell : Realize ? init_cell R_init_cell.
33
34 definition swap_states : FinSet → FinSet ≝ λalpha:FinSet.FinProd (initN 4) alpha.
35
36 definition swap ≝ 
37  λalpha:FinSet.λd:alpha.
38  mk_TM alpha (mcl_states alpha)
39  (λp.let 〈q,a〉 ≝ p in
40   let 〈q',b〉 ≝ q in
41   match a with 
42   [ None ⇒ 〈〈3,d〉,None ?〉 
43   | Some a' ⇒ 
44   match q' with
45   [ O ⇒ (* qinit *)
46      〈〈1,a'〉,Some ? 〈a',R〉〉
47   | S q' ⇒ match q' with
48     [ O ⇒ (* q1 *)
49       〈〈2,a'〉,Some ? 〈b,L〉〉
50     | S q' ⇒ match q' with
51       [ O ⇒ (* q2 *)
52         〈〈3,d〉,Some ? 〈b,N〉〉
53       | S _⇒ (* qacc *)
54           〈〈3,d〉,None ?〉 ] ] ] ])
55   〈0,d〉
56   (λq.let 〈q',a〉 ≝ q in q' == 3).
57   
58 definition R_swap ≝ 
59   λalpha,t1,t2.
60    ∀a,b,ls,rs.  
61     t1 = midtape alpha ls b (a::rs) → 
62     t2 = midtape alpha ls a (b::rs).
63
64 (*
65 lemma swap_q0_q1 : 
66   ∀alpha:FinSet.∀d,a,ls,a0,rs.
67   step alpha (swap alpha d)
68     (mk_config ?? 〈0,a〉 (mk_tape … ls (Some ? a0) rs)) =
69   mk_config alpha (states ? (swap alpha d)) 〈1,a0〉
70     (tape_move_right alpha ls a0 rs).
71 #alpha #d #a *
72 [ #a0 #rs %
73 | #a1 #ls #a0 #rs %
74 ]
75 qed.
76     
77 lemma swap_q1_q2 :
78   ∀alpha:FinSet.∀d,a,ls,a0,rs.
79   step alpha (swap alpha d) 
80     (mk_config ?? 〈1,a〉 (mk_tape … ls (Some ? a0) rs)) = 
81   mk_config alpha (states ? (swap alpha d)) 〈2,a0〉 
82     (tape_move_left alpha ls a rs).
83 #alpha #sep #a #ls #a0 * //
84 qed.
85
86 lemma swap_q2_q3 :
87   ∀alpha:FinSet.∀d,a,ls,a0,rs.
88   step alpha (swap alpha d) 
89     (mk_config ?? 〈2,a〉 (mk_tape … ls (Some ? a0) rs)) = 
90   mk_config alpha (states ? (swap alpha d)) 〈3,d〉 
91     (tape_move_left alpha ls a rs).
92 #alpha #sep #a #ls #a0 * //
93 qed.
94 *)
95
96 lemma sem_swap :
97   ∀alpha,d.
98   Realize alpha (swap alpha d) (R_swap alpha).
99 #alpha #d #tapein @(ex_intro ?? 4) cases tapein
100 [ @ex_intro [| % [ % | #a #b #ls #rs #Hfalse destruct (Hfalse) ] ]
101 | #a #al @ex_intro [| % [ % | #a #b #ls #rs #Hfalse destruct (Hfalse) ] ]
102 | #a #al @ex_intro [| % [ % | #a #b #ls #rs #Hfalse destruct (Hfalse) ] ]
103 | #ls #c #rs cases rs
104   [ @ex_intro [| % [ % | #a #b #ls0 #rs0 #Hfalse destruct (Hfalse) ] ]
105   | -rs #r #rs @ex_intro 
106     [|% 
107      [%
108      | #r0 #c0 #ls0 #rs0 #Htape destruct (Htape) normalize cases ls0 
109        [% | #l1 #ls1 %] ] ] ] ]
110 qed.
111
112 axiom ssem_move_char_l :
113   ∀alpha,sep.
114   Realize alpha (move_char_l alpha sep) (R_move_char_l alpha sep).
115
116 (*
117 MOVE TAPE RIGHT:
118
119   ls # current c # table # d? rs
120                      ^
121   ls # current c # table # d? rs init
122                          ^
123   ls # current c # table # d? rs
124                            ^
125   ls # current c # table # d rs ----------------------
126                            ^     move_l
127   ls # current c # table # d rs
128                          ^       swap
129   ls # current c # table d # rs --------------------
130                          ^
131   ls # current c # table d # rs
132                        ^
133   ls # current c # d table # rs  sub1
134                    ^
135   ls # current c # d table # rs
136                  ^
137   ls # current c d # table # rs -------------------
138                  ^               move_l
139   ls # current c d # table # rs -------------------
140                ^
141   ls # current c d # table # rs
142              ^
143   ls # c current d # table # rs  sub1
144        ^
145   ls # c current d # table # rs
146      ^
147   ls c # current d # table # rs ------------------
148      ^
149
150 (move_to_grid_r;)
151 move_r;
152 init_cell;
153 move_l;
154 swap;
155
156 move_l;
157 move_char_l;
158 ---------move_l;
159 swap;
160
161 move_l;
162
163 move_l;
164 move_char_l;
165 ---------move_l;
166 swap
167 *)
168
169 (* l1 # l2 r  ---> l1 r # l2 
170            ^          ^
171  *)
172 definition mtr_aux ≝ 
173   seq ? (move_l …) (seq ? (move_char_l STape 〈grid,false〉)
174    (swap STape 〈grid,false〉)).
175 definition R_mtr_aux ≝ λt1,t2.
176   ∀l1,l2,l3,r. t1 = midtape STape (l2@〈grid,false〉::l1) r l3 → no_grids l2 → 
177   t2 = midtape STape l1 r (〈grid,false〉::reverse ? l2@l3).
178
179 lemma sem_mtr_aux : Realize ? mtr_aux R_mtr_aux.
180 #intape 
181 cases (sem_seq … (sem_move_l …) (sem_seq … (ssem_move_char_l STape 〈grid,false〉)
182         (sem_swap STape 〈grid,false〉)) intape)
183 #k * #outc * #Hloop #HR @(ex_intro ?? k) @(ex_intro ?? outc)  % [@Hloop] -Hloop
184 #l1 #l2 #l3  #r #Hintape #Hl2
185 cases HR -HR #ta * whd in ⊢ (%→?); #Hta lapply (Hta … Hintape) -Hta #Hta
186 * #tb * whd in ⊢(%→?); generalize in match Hta; -Hta cases l2 in Hl2;
187 [ #_ #Hta #Htb lapply (Htb … Hta) -Htb * #Htb lapply (Htb (refl ??)) -Htb #Htb #_
188   whd in ⊢(%→?); >Htb #Houtc lapply (Houtc … Hta) -Houtc #Houtc @Houtc
189 | #c0 #l0 #Hnogrids #Hta #Htb lapply (Htb … Hta) -Htb * #_ #Htb
190     lapply (Htb … (refl ??) ??)
191     [ cases (true_or_false (memb STape 〈grid,false〉 l0)) #Hmemb
192       [ @False_ind lapply (Hnogrids 〈grid,false〉 ?)
193         [ @memb_cons // | normalize #Hfalse destruct (Hfalse) ]
194       | @Hmemb ]
195     | % #Hc0 lapply (Hnogrids c0 ?)
196       [ @memb_hd | >Hc0 normalize #Hfalse destruct (Hfalse) ]
197     | #Htb whd in ⊢(%→?); >Htb #Houtc lapply (Houtc … (refl ??)) -Houtc #Houtc
198       >reverse_cons >associative_append @Houtc
199 ]]
200 qed.
201
202 definition move_tape_r ≝ 
203   seq ? (move_r …) (seq ? init_cell (seq ? (move_l …) 
204    (seq ? (swap STape 〈grid,false〉) 
205      (seq ? mtr_aux (seq ? (move_l …) (seq ? mtr_aux (move_r …))))))).
206
207 definition R_move_tape_r ≝ λt1,t2.
208   ∀rs,n,table,c0,bc0,curconfig,ls0.
209   bit_or_null c0 = true → only_bits_or_nulls curconfig → table_TM n (reverse ? table) → 
210   t1 = midtape STape (table@〈grid,false〉::〈c0,bc0〉::curconfig@〈grid,false〉::ls0) 
211          〈grid,false〉 rs →
212   (rs = [] ∧
213    t2 = midtape STape (〈c0,bc0〉::ls0) 〈grid,false〉 (reverse STape curconfig@〈null,false〉::
214                              〈grid,false〉::reverse STape table@[〈grid,false〉])) ∨
215   (∃r0,rs0.rs = r0::rs0 ∧
216    t2 = midtape STape (〈c0,bc0〉::ls0) 〈grid,false〉 (reverse STape curconfig@r0::
217                              〈grid,false〉::reverse STape table@〈grid,false〉::rs0)).
218
219 lemma sem_move_tape_r : Realize ? move_tape_r R_move_tape_r.
220 #tapein 
221 cases (sem_seq …(sem_move_r …) (sem_seq … sem_init_cell (sem_seq … (sem_move_l …)
222    (sem_seq … (sem_swap STape 〈grid,false〉) (sem_seq … sem_mtr_aux
223      (sem_seq … (sem_move_l …) (sem_seq … sem_mtr_aux (sem_move_r …))))))) tapein)
224 #k * #outc * #Hloop #HR @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop] -Hloop
225 #rs #n #table #c0 #bc0 #curconfig #ls0 #Hbitnullc0 #Hbitnullcc #Htable #Htapein
226 cases HR -HR #ta * whd in ⊢ (%→?); #Hta lapply (Hta … Htapein) -Hta #Hta
227 * #tb * whd in ⊢ (%→?); *
228 [ * #r0 *
229   generalize in match Hta; generalize in match Htapein; -Htapein -Hta cases rs
230   [ #_ #Hta >Hta normalize in ⊢ (%→?); #Hfalse destruct (Hfalse) ]
231   #r1 #rs1 #Htapein #Hta change with (midtape ?? r1 rs1) in Hta:(???%); >Hta 
232   #Hr0 whd in Hr0:(??%?); #Htb * #tc * whd in ⊢ (%→?); #Htc lapply (Htc … Htb) -Htc #Htc
233   * #td * whd in ⊢ (%→?); #Htd lapply (Htd … Htc) -Htd #Htd
234   * #te * whd in ⊢ (%→?); #Hte lapply (Hte … Htd ?) [ (*memb_reverse @(no_grids_in_table … Htable)*) @daemon ] -Hte #Hte
235   * #tf * whd in ⊢ (%→?); #Htf lapply (Htf … Hte) -Htf #Htf
236   * #tg * whd in ⊢ (%→?); #Htg lapply (Htg … Htf ?) [ #x #Hx @bit_or_null_not_grid @Hbitnullcc // ] -Htg #Htg
237   whd in ⊢ (%→?); #Houtc lapply (Houtc … Htg) -Houtc #Houtc
238   %2 @(ex_intro ?? r1) @(ex_intro ?? rs1) % [%] @Houtc 
239 | * generalize in match Hta; generalize in match Htapein; -Htapein -Hta cases rs
240   [|#r1 #rs1 #_ #Hta >Hta normalize in ⊢ (%→?); #Hfalse destruct (Hfalse) ]
241   #Htapein #Hta change with (rightof ???) in Hta:(???%); >Hta 
242   #Hr0 whd in Hr0:(??%?); #Htb * #tc * whd in ⊢ (%→?); #Htc lapply (Htc … Htb) -Htc #Htc
243   * #td * whd in ⊢ (%→?); #Htd lapply (Htd … Htc) -Htd #Htd
244   * #te * whd in ⊢ (%→?); #Hte lapply (Hte … Htd ?) [(*same as above @(no_grids_in_table … Htable) *) @daemon ] -Hte #Hte
245   * #tf * whd in ⊢ (%→?); #Htf lapply (Htf … Hte) -Htf #Htf
246   * #tg * whd in ⊢ (%→?); #Htg lapply (Htg … Htf ?) [ #x #Hx @bit_or_null_not_grid @Hbitnullcc // ] -Htg #Htg
247   whd in ⊢ (%→?); #Houtc lapply (Houtc … Htg) -Houtc #Houtc
248   % % [% | @Houtc ]
249 qed.
250
251 (*
252 MOVE TAPE LEFT:
253
254   ls # current c # table # d rs
255                      ^
256   ls # current c # table # d rs
257                          ^
258   ls # current c # table d # rs
259                        ^
260   ls # current c # d table # rs
261                    ^
262   ls # current c # d table # rs
263                  ^
264   ls # current c d # table # rs
265                ^
266   ls # current c d # table # rs
267              ^
268   ls # c current c # table # rs
269        ^
270   ls # c current c # table # rs
271      ^
272   ls c # current c # table # rs
273      ^
274
275 move_to_grid_r;
276 swap;
277 move_char_l;
278 move_l;
279 swap;
280 move_l;
281 move_char_l;
282 move_l;
283 swap
284 *)
285 axiom move_tape_l : TM STape.
286 (*  seq ? (move_r …) (seq ? init_cell (seq ? (move_l …) 
287    (seq ? (swap STape 〈grid,false〉) 
288      (seq ? mtr_aux (seq ? (move_l …) mtr_aux))))). *)
289
290 definition R_move_tape_l ≝ λt1,t2.
291   ∀rs,n,table,c0,bc0,curconfig,ls0.
292   bit_or_null c0 = true → only_bits_or_nulls curconfig → table_TM n (reverse ? table) → 
293   t1 = midtape STape (table@〈grid,false〉::〈c0,bc0〉::curconfig@〈grid,false〉::ls0) 
294          〈grid,false〉 rs →
295   (ls0 = [] ∧
296    t2 = midtape STape [] 〈grid,false〉 
297          (reverse ? curconfig@〈null,false〉::〈grid,false〉::reverse ? table@〈grid,false〉::〈c0,bc0〉::rs)) ∨
298   (∃l1,ls1. ls0 = l1::ls1 ∧
299    t2 = midtape STape ls1 〈grid,false〉
300          (reverse ? curconfig@l1::〈grid,false〉::reverse ? table@〈grid,false〉::〈c0,bc0〉::rs)).
301    
302 axiom sem_move_tape_l : Realize ? move_tape_l R_move_tape_l.
303
304 (*
305            by cases on current: 
306              case bit false: move_tape_l
307              case bit true: move_tape_r
308              case null: adv_to_grid_l; move_l; adv_to_grid_l;
309 *)
310
311 definition lift_tape ≝ λls,c,rs.
312   let 〈c0,b〉 ≝ c in
313   let c' ≝ match c0 with
314   [ null ⇒ None ?
315   | _ ⇒ Some ? c ]
316   in
317   mk_tape STape ls c' rs.
318   
319 definition sim_current_of_tape ≝ λt.
320   match current STape t with
321   [ None ⇒ 〈null,false〉
322   | Some c0 ⇒ c0 ].
323
324
325 definition move_of_unialpha ≝ 
326   λc.match c with
327   [ bit x ⇒ match x with [ true ⇒ R | false ⇒ L ]
328   | _ ⇒ N ].
329
330 definition R_uni_step ≝ λt1,t2.
331   ∀n,table,c,c1,ls,rs,curs,curc,news,newc,mv.
332   table_TM n table → 
333   match_in_table n (〈c,false〉::curs) 〈curc,false〉 
334     (〈c1,false〉::news) 〈newc,false〉 〈mv,false〉 table → 
335   t1 = midtape STape (〈grid,false〉::ls) 〈c,false〉 
336     (curs@〈curc,false〉::〈grid,false〉::table@〈grid,false〉::rs) → 
337   ∀t1',ls1,rs1.t1' = lift_tape ls 〈curc,false〉 rs → 
338   (t2 = midtape STape (〈grid,false〉::ls1) 〈c1,false〉 
339     (news@〈newc,false〉::〈grid,false〉::table@〈grid,false〉::rs1) ∧
340    lift_tape ls1 〈newc,false〉 rs1 = 
341    tape_move STape t1' (Some ? 〈〈newc,false〉,move_of_unialpha mv〉)).
342
343 definition no_nulls ≝ 
344  λl:list STape.∀x.memb ? x l = true → is_null (\fst x) = false.
345  
346 definition current_of_alpha ≝ λc:STape.
347   match \fst c with [ null ⇒ None ? | _ ⇒ Some ? c ].
348
349 (* 
350    no_marks (c::ls@rs) 
351    only_bits (ls@rs)
352    bit_or_null c
353    
354 *)
355 definition legal_tape ≝ λls,c,rs.
356  no_marks (c::ls@rs) ∧ only_bits (ls@rs) ∧ bit_or_null (\fst c) = true ∧
357  (\fst c ≠ null ∨ ls = [] ∨ rs = []).
358  
359 lemma legal_tape_left :
360   ∀ls,c,rs.legal_tape ls c rs → 
361   left ? (mk_tape STape ls (current_of_alpha c) rs) = ls.
362 #ls * #c #bc #rs * * * #_ #_ #_ *
363 [ *
364   [ cases c
365     [ #c' #_ %
366     | * #Hfalse @False_ind /2/
367     |*: #_ % ]
368   | #Hls >Hls cases c // cases rs //
369   ]
370 | #Hrs >Hrs cases c // cases ls //
371 ]
372 qed.
373
374 axiom legal_tape_current :
375   ∀ls,c,rs.legal_tape ls c rs → 
376   current ? (mk_tape STape ls (current_of_alpha c) rs) = current_of_alpha c.
377
378 axiom legal_tape_right :
379   ∀ls,c,rs.legal_tape ls c rs → 
380   right ? (mk_tape STape ls (current_of_alpha c) rs) = rs.
381
382 (*
383 lemma legal_tape_cases : 
384   ∀ls,c,rs.legal_tape ls c rs → 
385   \fst c ≠ null ∨ (\fst c = null ∧ (ls = [] ∨ rs = [])).
386 #ls #c #rs cases c #c0 #bc0 cases c0
387 [ #c1 normalize #_ % % #Hfalse destruct (Hfalse)
388 | cases ls
389   [ #_ %2 % // % %
390   | #l0 #ls0 cases rs
391     [ #_ %2 % // %2 %
392     | #r0 #rs0 normalize * * #_ #Hrs destruct (Hrs) ]
393   ]
394 |*: #_ % % #Hfalse destruct (Hfalse) ]
395 qed.
396
397 axiom legal_tape_conditions : 
398   ∀ls,c,rs.(\fst c ≠ null ∨ ls = [] ∨ rs = []) → legal_tape ls c rs.
399 (*#ls #c #rs *
400 [ *
401   [ >(eq_pair_fst_snd ?? c) cases (\fst c)
402     [ #c0 #Hc % % %
403     | * #Hfalse @False_ind /2/
404     |*: #Hc % % %
405     ]
406   | cases ls [ * #Hfalse @False_ind /2/ ]
407     #l0 #ls0 
408   
409   #Hc
410 *)
411 *)
412  
413 definition R_move_tape_r_abstract ≝ λt1,t2.
414   ∀rs,n,table,curc,curconfig,ls.
415   is_bit curc = true → only_bits_or_nulls curconfig → table_TM n (reverse ? table) → 
416   t1 = midtape STape (table@〈grid,false〉::〈curc,false〉::curconfig@〈grid,false〉::ls) 
417          〈grid,false〉 rs →
418   legal_tape ls 〈curc,false〉 rs → 
419   ∀t1'.t1' = lift_tape ls 〈curc,false〉 rs → 
420   ∃ls1,rs1,newc.
421   (t2 = midtape STape ls1 〈grid,false〉 (reverse ? curconfig@〈newc,false〉::
422     〈grid,false〉::reverse ? table@〈grid,false〉::rs1) ∧
423    lift_tape ls1 〈newc,false〉 rs1 = 
424    tape_move_right STape ls 〈curc,false〉 rs ∧ legal_tape ls1 〈newc,false〉 rs1).
425    
426 lemma lift_tape_not_null :
427   ∀ls,c,rs. is_null (\fst c) = false → 
428   lift_tape ls c rs = mk_tape STape ls (Some ? c) rs.
429 #ls * #c0 #bc0 #rs cases c0
430 [|normalize in ⊢ (%→?); #Hfalse destruct (Hfalse) ]
431 //
432 qed.
433
434 axiom bit_not_null :  ∀d.is_bit d = true → is_null d = false.
435  
436 lemma mtr_concrete_to_abstract :
437   ∀t1,t2.R_move_tape_r t1 t2 → R_move_tape_r_abstract t1 t2.
438 #t1 #t2 whd in ⊢(%→?); #Hconcrete
439 #rs #n #table #curc #curconfig #ls #Hbitcurc #Hcurconfig #Htable #Ht1
440 * * * #Hnomarks #Hbits #Hcurc #Hlegal #t1' #Ht1'
441 cases (Hconcrete … Htable Ht1) //
442 [ * #Hrs #Ht2
443   @(ex_intro ?? (〈curc,false〉::ls)) @(ex_intro ?? [])
444   @(ex_intro ?? null) %
445   [ %
446     [ >Ht2 %
447     | >Hrs % ]
448   | % [ % [ %
449     [ >append_nil #x #Hx cases (orb_true_l … Hx) #Hx'
450       [ >(\P Hx') % 
451       | @Hnomarks @(memb_append_l1 … Hx') ]
452     | >append_nil #x #Hx cases (orb_true_l … Hx) #Hx'
453       [ >(\P Hx') //
454       | @Hbits @(memb_append_l1 … Hx') ]]
455     | % ]
456     | %2 % ]
457   ]
458 | * * #r0 #br0 * #rs0 * #Hrs 
459   cut (br0 = false) 
460   [ @(Hnomarks 〈r0,br0〉) @memb_cons @memb_append_l2 >Hrs @memb_hd]
461   #Hbr0 >Hbr0 in Hrs; #Hrs #Ht2
462   @(ex_intro ?? (〈curc,false〉::ls)) @(ex_intro ?? rs0)
463   @(ex_intro ?? r0) %
464   [ %
465     [ >Ht2  //
466     | >Hrs >lift_tape_not_null
467       [ %
468       | @bit_not_null @(Hbits 〈r0,false〉) >Hrs @memb_append_l2 @memb_hd ] ]
469   | % [ % [ %
470     [ #x #Hx cases (orb_true_l … Hx) #Hx'
471       [ >(\P Hx') % 
472       | cases (memb_append … Hx') #Hx'' @Hnomarks 
473         [ @(memb_append_l1 … Hx'') 
474         | >Hrs @memb_cons @memb_append_l2 @(memb_cons … Hx'') ]
475       ]
476     | whd in ⊢ (?%); #x #Hx cases (orb_true_l … Hx) #Hx'
477       [ >(\P Hx') //
478       | cases (memb_append … Hx') #Hx'' @Hbits
479         [ @(memb_append_l1 … Hx'') | >Hrs @memb_append_l2 @(memb_cons … Hx'') ]
480       ]]
481     | whd in ⊢ (??%?); >(Hbits 〈r0,false〉) //
482       @memb_append_l2 >Hrs @memb_hd ]
483     | % % % #Hr0 lapply (Hbits 〈r0,false〉?) 
484       [ @memb_append_l2 >Hrs @memb_hd
485       | >Hr0 normalize #Hfalse destruct (Hfalse)
486       ] ] ] ]
487 qed.
488
489 definition R_move_tape_l_abstract ≝ λt1,t2.
490   ∀rs,n,table,curc,curconfig,ls.
491   is_bit curc = true → only_bits_or_nulls curconfig → table_TM n (reverse ? table) → 
492   t1 = midtape STape (table@〈grid,false〉::〈curc,false〉::curconfig@〈grid,false〉::ls) 
493          〈grid,false〉 rs →
494   legal_tape ls 〈curc,false〉 rs → 
495   ∀t1'.t1' = lift_tape ls 〈curc,false〉 rs → 
496   ∃ls1,rs1,newc.
497   (t2 = midtape STape ls1 〈grid,false〉 (reverse ? curconfig@〈newc,false〉::
498     〈grid,false〉::reverse ? table@〈grid,false〉::rs1) ∧
499    lift_tape ls1 〈newc,false〉 rs1 = 
500    tape_move_left STape ls 〈curc,false〉 rs ∧ legal_tape ls1 〈newc,false〉 rs1).
501
502 lemma mtl_concrete_to_abstract :
503   ∀t1,t2.R_move_tape_l t1 t2 → R_move_tape_l_abstract t1 t2.
504 #t1 #t2 whd in ⊢(%→?); #Hconcrete
505 #rs #n #table #curc #curconfig #ls #Hcurc #Hcurconfig #Htable #Ht1
506 * * * #Hnomarks #Hbits #Hcurc #Hlegal #t1' #Ht1'
507 cases (Hconcrete … Htable Ht1) //
508 [ * #Hls #Ht2
509   @(ex_intro ?? [])
510   @(ex_intro ?? (〈curc,false〉::rs)) 
511   @(ex_intro ?? null) %
512   [ %
513     [ >Ht2 %
514     | >Hls % ]
515   |  % [ % [ %
516     [ #x #Hx cases (orb_true_l … Hx) #Hx'
517       [ >(\P Hx') % 
518       | @Hnomarks >Hls @Hx' ]
519     | #x #Hx cases (orb_true_l … Hx) #Hx'
520       [ >(\P Hx') //
521       | @Hbits >Hls @Hx' ]]
522     | % ]
523     | % %2 % ]
524   ]
525 | * * #l0 #bl0 * #ls0 * #Hls 
526   cut (bl0 = false) 
527   [ @(Hnomarks 〈l0,bl0〉) @memb_cons @memb_append_l1 >Hls @memb_hd]
528   #Hbl0 >Hbl0 in Hls; #Hls #Ht2
529   @(ex_intro ?? ls0) @(ex_intro ?? (〈curc,false〉::rs))
530   @(ex_intro ?? l0) %
531   [ % 
532     [ >Ht2 %
533     | >Hls >lift_tape_not_null
534       [ %
535       | @bit_not_null @(Hbits 〈l0,false〉) >Hls @memb_append_l1 @memb_hd ] ]
536   | % [ % [ %
537     [ #x #Hx cases (orb_true_l … Hx) #Hx'
538       [ >(\P Hx') % 
539       | cases (memb_append … Hx') #Hx'' @Hnomarks 
540         [ >Hls @memb_cons @memb_cons @(memb_append_l1 … Hx'') 
541         | cases (orb_true_l … Hx'') #Hx'''
542           [ >(\P Hx''') @memb_hd
543           | @memb_cons @(memb_append_l2 … Hx''')]
544         ]
545       ]
546     | whd in ⊢ (?%); #x #Hx cases (memb_append … Hx) #Hx'
547       [ @Hbits >Hls @memb_cons @(memb_append_l1 … Hx')
548       | cases (orb_true_l … Hx') #Hx''
549         [ >(\P Hx'') //
550         | @Hbits @(memb_append_l2 … Hx'')
551         ]]]
552     | whd in ⊢ (??%?); >(Hbits 〈l0,false〉) //
553       @memb_append_l1 >Hls @memb_hd ]
554     | % % % #Hl0 lapply (Hbits 〈l0,false〉?) 
555       [ @memb_append_l1 >Hls @memb_hd
556       | >Hl0 normalize #Hfalse destruct (Hfalse)
557       ] ] ] ]
558 qed. 
559   
560 lemma Realize_to_Realize : 
561   ∀alpha,M,R1,R2.(∀t1,t2.R1 t1 t2 → R2 t1 t2) → Realize alpha M R1 → Realize alpha M R2.
562 #alpha #M #R1 #R2 #Himpl #HR1 #intape
563 cases (HR1 intape) -HR1 #k * #outc * #Hloop #HR1
564 @(ex_intro ?? k) @(ex_intro ?? outc) % /2/
565 qed.
566
567 lemma sem_move_tape_l_abstract : Realize … move_tape_l R_move_tape_l_abstract.
568 @(Realize_to_Realize … mtl_concrete_to_abstract) //
569 qed.
570
571 lemma sem_move_tape_r_abstract : Realize … move_tape_r R_move_tape_r_abstract.
572 @(Realize_to_Realize … mtr_concrete_to_abstract) //
573 qed.
574
575 (*
576  t1 =  ls # cs c # table # rs  
577  
578  let simt ≝ lift_tape ls c rs in
579  let simt' ≝ move_left simt' in
580  
581  t2 = left simt'# cs (sim_current_of_tape simt') # table # right simt'
582 *)
583           
584 (*
585 definition R_move
586
587 definition R_exec_move ≝ λt1,t2.
588   ∀ls,current,table1,newcurrent,table2,rs.
589   t1 = midtape STape (current@〈grid,false〉::ls) 〈grid,false〉
590        (table1@〈comma,true〉::newcurrent@〈comma,false〉::move::table2@
591         〈grid,false〉::rs) → 
592   table_TM (table1@〈comma,false〉::newcurrent@〈comma,false〉::move::table2) →
593   t2 = midtape
594 *)
595
596 (*
597
598 step :
599
600 if is_true(current) (* current state is final *)
601    then nop
602    else 
603    init_match;
604    match_tuple;
605    if is_marked(current) = false (* match ok *)
606       then exec_move; 
607       else sink;
608         
609 *)
610
611
612 definition move_tape ≝ 
613   ifTM ? (test_char ? (λc:STape.c == 〈bit false,false〉)) 
614     (* spostamento a sinistra: verificare se per caso non conviene spostarsi 
615        sulla prima grid invece dell'ultima *)
616     (seq ? (adv_to_mark_r ? (λc:STape.is_grid (\fst c))) move_tape_l)
617     (ifTM ? (test_char ? (λc:STape.c == 〈bit true,false〉)) 
618        (seq ? (adv_to_mark_r ? (λc:STape.is_grid (\fst c))) move_tape_r)
619        (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
620           (seq ? (move_l …) (adv_to_mark_l ? (λc:STape.is_grid (\fst c))))) 
621        tc_true) tc_true.
622            
623 definition R_move_tape ≝ λt1,t2.
624   ∀rs,n,table1,mv,table2,curc,curconfig,ls.
625   bit_or_null mv = true → only_bits_or_nulls curconfig → 
626   (is_bit mv = true → is_bit curc = true) → 
627   table_TM n (reverse ? table1@〈mv,false〉::table2) → 
628   t1 = midtape STape (table1@〈grid,false〉::〈curc,false〉::curconfig@〈grid,false〉::ls) 
629          〈mv,false〉 (table2@〈grid,false〉::rs) →
630   legal_tape ls 〈curc,false〉 rs → 
631   ∀t1'.t1' = lift_tape ls 〈curc,false〉 rs → 
632   ∃ls1,rs1,newc.
633   legal_tape ls1 〈newc,false〉 rs1 ∧
634   (t2 = midtape STape ls1 〈grid,false〉 (reverse ? curconfig@〈newc,false〉::
635     〈grid,false〉::reverse ? table1@〈mv,false〉::table2@〈grid,false〉::rs1) ∧
636    ((mv = bit false ∧ lift_tape ls1 〈newc,false〉 rs1 = tape_move_left STape ls 〈curc,false〉 rs) ∨
637     (mv = bit true ∧ lift_tape ls1 〈newc,false〉 rs1 = tape_move_right STape ls 〈curc,false〉 rs) ∨
638     (mv = null ∧ ls1 = ls ∧ rs1 = rs ∧ curc = newc))).
639      
640 lemma sem_move_tape : Realize ? move_tape R_move_tape.
641 #intape 
642 cases (sem_if ? (test_char ??) … tc_true (sem_test_char ? (λc:STape.c == 〈bit false,false〉))
643         (sem_seq … (sem_adv_to_mark_r ? (λc:STape.is_grid (\fst c))) sem_move_tape_l_abstract)
644         (sem_if ? (test_char ??) … tc_true (sem_test_char ? (λc:STape.c == 〈bit true,false〉))
645         (sem_seq … (sem_adv_to_mark_r ? (λc:STape.is_grid (\fst c))) sem_move_tape_r_abstract)
646         (sem_seq … (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
647           (sem_seq … (sem_move_l …) (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))))) intape)
648 #k * #outc * #Hloop #HR
649 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop] -Hloop
650 #rs #n #table1 #mv #table2 #curc #curconfig #ls
651 #Hmv #Hcurconfig #Hmvcurc #Htable #Hintape #Htape #t1' #Ht1'
652 generalize in match HR; -HR *
653 [ * #ta * whd in ⊢ (%→?); #Hta cases (Hta 〈mv,false〉 ?)
654   [| >Hintape % ] -Hta #Hceq #Hta lapply (\P Hceq) -Hceq #Hceq destruct (Hta Hceq)
655   * #tb * whd in ⊢ (%→?); #Htb cases (Htb … Hintape) -Htb -Hintape
656   [ * normalize in ⊢ (%→?); #Hfalse destruct (Hfalse) ]
657   * #_ #Htb lapply (Htb … (refl ??) (refl ??) ?)
658   [ @daemon ] -Htb >append_cons <associative_append #Htb
659   whd in ⊢ (%→?); #Houtc lapply (Houtc … Htb … Ht1') //
660   [ >reverse_append >reverse_append >reverse_reverse @Htable 
661   | /2/
662   ||]
663   -Houtc -Htb * #ls1 * #rs1 * #newc * * #Houtc #Hnewtape #Hnewtapelegal
664   @(ex_intro ?? ls1) @(ex_intro ?? rs1) @(ex_intro ?? newc) % 
665   [ //
666   | % 
667     [ >Houtc >reverse_append >reverse_append >reverse_reverse
668       >associative_append >associative_append % 
669     | % % % // ]
670   ]
671 | * #ta * whd in ⊢ (%→?); #Hta cases (Hta 〈mv,false〉 ?) 
672   [| >Hintape % ] -Hta #Hcneq cut (mv ≠ bit false) 
673   [ lapply (\Pf Hcneq) @not_to_not #Heq >Heq % ] -Hcneq #Hcneq #Hta destruct (Hta)
674     *
675     [ * #tb * whd in ⊢ (%→?);#Htb cases (Htb 〈mv,false〉 ?) 
676       [| >Hintape % ] -Htb #Hceq #Htb lapply (\P Hceq) -Hceq #Hceq destruct (Htb Hceq)
677       * #tc * whd in ⊢ (%→?); #Htc cases (Htc … Hintape) -Htc -Hintape
678       [ * normalize in ⊢ (%→?); #Hfalse destruct (Hfalse) ]
679     * #_ #Htc lapply (Htc … (refl ??) (refl ??) ?)
680     [ @daemon ] -Htc >append_cons <associative_append #Htc
681     whd in ⊢ (%→?); #Houtc lapply (Houtc … Htc … Ht1') //
682     [ >reverse_append >reverse_append >reverse_reverse @Htable 
683     | /2/ |]
684     -Houtc -Htc * #ls1 * #rs1 * #newc * * #Houtc #Hnewtape #Hnewtapelegal
685     @(ex_intro ?? ls1) @(ex_intro ?? rs1) @(ex_intro ?? newc) % 
686     [ //
687     | %
688       [ >Houtc >reverse_append >reverse_append >reverse_reverse
689         >associative_append >associative_append % 
690       | % %2 % // ]
691     ]
692   | * #tb * whd in ⊢ (%→?); #Htb cases (Htb 〈mv,false〉 ?) 
693     [| >Hintape % ] -Htb #Hcneq' cut (mv ≠ bit true) 
694     [ lapply (\Pf Hcneq') @not_to_not #Heq >Heq % ] -Hcneq' #Hcneq' #Htb destruct (Htb)
695     * #tc * whd in ⊢ (%→?); #Htc cases (Htc … Hintape)
696     [ *  >(bit_or_null_not_grid … Hmv) #Hfalse destruct (Hfalse) ] -Htc
697     * #_ #Htc lapply (Htc … (refl ??) (refl ??) ?) [@daemon] -Htc #Htc
698     * #td * whd in ⊢ (%→?); #Htd lapply (Htd … Htc) -Htd -Htc
699     whd in ⊢ (???%→?); #Htd whd in ⊢ (%→?); #Houtc lapply (Houtc … Htd) -Houtc *
700     [ * cases Htape * * #_ #_ #Hcurc #_
701       >(bit_or_null_not_grid … Hcurc) #Hfalse destruct (Hfalse) ]
702     * #_ #Houtc lapply (Houtc … (refl ??) (refl ??) ?) [@daemon] -Houtc #Houtc
703     @(ex_intro ?? ls) @(ex_intro ?? rs) @(ex_intro ?? curc) %
704     [ //
705     | %
706       [ @Houtc
707       | %2 % // % // % // 
708         generalize in match Hcneq; generalize in match Hcneq'; 
709         cases mv in Hmv; normalize //
710         [ * #_ normalize [ #Hfalse @False_ind cases Hfalse /2/ | #_ #Hfalse @False_ind cases Hfalse /2/ ] 
711         |*: #Hfalse destruct (Hfalse) ]
712       ]
713     ]
714   ]
715 ]
716 qed.