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