]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/universal/match_machines.ma
adding match_machines and removing trans_to_tuples
[helm.git] / matita / matita / lib / turing / universal / match_machines.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
13 include "turing/universal/tuples.ma".
14 include "turing/universal/marks.ma".
15
16 (*
17 l0 x* a l1 x0* a0 l2 ------> l0 x a* l1 x0 a0* l2
18    ^                               ^
19
20 if current (* x *) = #
21    then 
22    else if x = 0
23       then move_right; ----
24            adv_to_mark_r;
25            if current (* x0 *) = 0
26               then advance_mark ----
27                    adv_to_mark_l;
28                    advance_mark
29               else STOP
30       else x = 1 (* analogo *)
31
32 *)
33
34
35 (*
36    MARK NEXT TUPLE machine
37    (partially axiomatized)
38    
39    marks the first character after the first bar (rightwards)
40  *)
41  
42 definition mark_next_tuple ≝ 
43   seq ? (adv_to_mark_r ? bar_or_grid)
44      (ifTM ? (test_char ? (λc:STape.is_bar (\fst c)))
45        (move_right_and_mark ?) (nop ?) tc_true).
46
47 definition R_mark_next_tuple ≝ 
48   λt1,t2.
49     ∀ls,c,rs1,rs2.
50     (* c non può essere un separatore ... speriamo *)
51     t1 = midtape STape ls c (rs1@〈grid,false〉::rs2) → 
52     no_marks rs1 → no_grids rs1 → bar_or_grid c = false → 
53     (∃rs3,rs4,d,b.rs1 = rs3 @ 〈bar,false〉 :: rs4 ∧
54       no_bars rs3 ∧
55       Some ? 〈d,b〉 = option_hd ? (rs4@〈grid,false〉::rs2) ∧
56       t2 = midtape STape (〈bar,false〉::reverse ? rs3@c::ls) 〈d,true〉 (tail ? (rs4@〈grid,false〉::rs2)))
57     ∨
58     (no_bars rs1 ∧ t2 = midtape ? (reverse ? rs1@c::ls) 〈grid,false〉 rs2).
59      
60 axiom daemon :∀P:Prop.P.
61
62 axiom tech_split :
63   ∀A:DeqSet.∀f,l.
64    (∀x.memb A x l = true → f x = false) ∨
65    (∃l1,c,l2.f c = true ∧ l = l1@c::l2 ∧ ∀x.memb ? x l1 = true → f x = false).
66 (*#A #f #l elim l
67 [ % #x normalize #Hfalse *)
68      
69 theorem sem_mark_next_tuple :
70   Realize ? mark_next_tuple R_mark_next_tuple.
71 #intape 
72 lapply (sem_seq ? (adv_to_mark_r ? bar_or_grid)
73          (ifTM ? (test_char ? (λc:STape.is_bar (\fst c))) (move_right_and_mark ?) (nop ?) tc_true) ????)
74 [@sem_if [5: // |6: @sem_move_right_and_mark |7: // |*:skip]
75 | //
76 |||#Hif cases (Hif intape) -Hif
77    #j * #outc * #Hloop * #ta * #Hleft #Hright
78    @(ex_intro ?? j) @ex_intro [|% [@Hloop] ]
79    -Hloop
80    #ls #c #rs1 #rs2 #Hrs #Hrs1 #Hrs1' #Hc
81    cases (Hleft … Hrs)
82    [ * #Hfalse >Hfalse in Hc; #Htf destruct (Htf)
83    | * #_ #Hta cases (tech_split STape (λc.is_bar (\fst c)) rs1)
84      [ #H1 lapply (Hta rs1 〈grid,false〉 rs2 (refl ??) ? ?)
85        [ * #x #b #Hx whd in ⊢ (??%?); >(Hrs1' … Hx) >(H1 … Hx) %
86        | %
87        | -Hta #Hta cases Hright
88          [ * #tb * whd in ⊢ (%→?); #Hcurrent
89            @False_ind cases (Hcurrent 〈grid,false〉 ?)
90            [ normalize in ⊢ (%→?); #Hfalse destruct (Hfalse)
91            | >Hta % ]
92          | * #tb * whd in ⊢ (%→?); #Hcurrent
93            cases (Hcurrent 〈grid,false〉 ?)
94            [  #_ #Htb whd in ⊢ (%→?); #Houtc
95              %2 %
96              [ @H1
97              | >Houtc >Htb >Hta % ]
98            | >Hta % ]
99          ]
100        ]
101     | * #rs3 * #c0 * #rs4 * * #Hc0 #Hsplit #Hrs3
102       % @(ex_intro ?? rs3) @(ex_intro ?? rs4)
103      lapply (Hta rs3 c0 (rs4@〈grid,false〉::rs2) ???)
104      [ #x #Hrs3' whd in ⊢ (??%?); >Hsplit in Hrs1;>Hsplit in Hrs3;
105        #Hrs3 #Hrs1 >(Hrs1 …) [| @memb_append_l1 @Hrs3'|]
106        >(Hrs3 … Hrs3') @Hrs1' >Hsplit @memb_append_l1 //
107      | whd in ⊢ (??%?); >Hc0 %
108      | >Hsplit >associative_append % ] -Hta #Hta
109        cases Hright
110        [ * #tb * whd in ⊢ (%→?); #Hta'
111          whd in ⊢ (%→?); #Htb
112          cases (Hta' c0 ?)
113          [ #_ #Htb' >Htb' in Htb; #Htb
114            generalize in match Hsplit; -Hsplit
115            cases rs4 in Hta;
116            [ #Hta #Hsplit >(Htb … Hta)
117              >(?:c0 = 〈bar,false〉)
118              [ @(ex_intro ?? grid) @(ex_intro ?? false)
119                % [ % [ % 
120                [(* Hsplit *) @daemon |(*Hrs3*) @daemon ] | % ] | % ] 
121                | (* Hc0 *) @daemon ]
122            | #r5 #rs5 >(eq_pair_fst_snd … r5)
123              #Hta #Hsplit >(Htb … Hta)
124              >(?:c0 = 〈bar,false〉)
125              [ @(ex_intro ?? (\fst r5)) @(ex_intro ?? (\snd r5))
126                % [ % [ % [ (* Hc0, Hsplit *) @daemon | (*Hrs3*) @daemon ] | % ]
127                      | % ] | (* Hc0 *) @daemon ] ] | >Hta % ]
128              | * #tb * whd in ⊢ (%→?); #Hta'
129                whd in ⊢ (%→?); #Htb
130                cases (Hta' c0 ?)
131                [ #Hfalse @False_ind >Hfalse in Hc0;
132                  #Hc0 destruct (Hc0)
133                | >Hta % ]
134 ]]]]
135 qed.
136
137 definition init_current_on_match ≝ 
138   (seq ? (move_l ?)
139     (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
140       (seq ? (move_r ?) (mark ?)))).
141           
142 definition R_init_current_on_match ≝ λt1,t2.
143   ∀l1,l2,c,rs. no_grids l1 → is_grid c = false → 
144   t1 = midtape STape (l1@〈c,false〉::〈grid,false〉::l2)  〈grid,false〉 rs → 
145   t2 = midtape STape (〈grid,false〉::l2) 〈c,true〉 ((reverse ? l1)@〈grid,false〉::rs).
146
147 lemma sem_init_current_on_match : 
148   Realize ? init_current_on_match R_init_current_on_match.
149 #intape 
150 cases (sem_seq ????? (sem_move_l ?)
151         (sem_seq ????? (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
152            (sem_seq ????? (sem_move_r ?) (sem_mark ?))) intape)
153 #k * #outc * #Hloop #HR 
154 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop] -Hloop
155 #l1 #l2 #c #rs #Hl1 #Hc #Hintape
156 cases HR -HR #ta * whd in ⊢ (%→?); #Hta lapply (Hta … Hintape) -Hta -Hintape 
157 generalize in match Hl1; cases l1
158   [#Hl1 whd in ⊢ ((???(??%%%))→?); #Hta
159    * #tb * whd in ⊢ (%→?); #Htb cases (Htb … Hta) -Hta
160     [* >Hc #Htemp destruct (Htemp) ]
161    * #_ #Htc lapply (Htc [ ] 〈grid,false〉 ? (refl ??) (refl …) Hl1) 
162    whd in ⊢ ((???(??%%%))→?); -Htc #Htc
163    * #td * whd in ⊢ (%→?); #Htd lapply (Htd … Htc) -Htc -Htd 
164    whd in ⊢ ((???(??%%%))→?); #Htd
165    whd in ⊢ (%→?); #Houtc lapply (Houtc … Htd) -Houtc #Houtc
166    >Houtc % 
167   |#d #tl #Htl whd in ⊢ ((???(??%%%))→?); #Hta
168    * #tb * whd in ⊢ (%→?); #Htb cases (Htb … Hta) -Htb
169     [* >(Htl … (memb_hd …)) #Htemp destruct (Htemp)]    
170    * #Hd >append_cons #Htb lapply (Htb … (refl ??) (refl …) ?)
171     [#x #membx cases (memb_append … membx) -membx #membx
172       [@Htl @memb_cons @membx | >(memb_single … membx) @Hc]]-Htb  #Htb
173    * #tc * whd in ⊢ (%→?); #Htc lapply (Htc … Htb) -Htb -Htc 
174    >reverse_append >associative_append whd in ⊢ ((???(??%%%))→?); #Htc
175    whd in ⊢ (%→?); #Houtc lapply (Houtc … Htc) -Houtc #Houtc 
176    >Houtc >reverse_cons >associative_append % 
177   ]
178 qed.   
179
180 (*
181 definition init_current_gen ≝ 
182   seq ? (adv_to_mark_l ? (is_marked ?))
183     (seq ? (clear_mark ?)
184        (seq ? (move_l ?)
185          (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
186             (seq ? (move_r ?) (mark ?))))).
187           
188 definition R_init_current_gen ≝ λt1,t2.
189   ∀l1,c,l2,b,l3,c1,rs,c0,b0. no_marks l1 → no_grids l2 →
190   Some ? 〈c0,b0〉 = option_hd ? (reverse ? (〈c,true〉::l2)) → 
191   t1 = midtape STape (l1@〈c,true〉::l2@〈grid,b〉::l3) 〈c1,false〉 rs → 
192   t2 = midtape STape (〈grid,b〉::l3) 〈c0,true〉
193         ((tail ? (reverse ? (l1@〈c,false〉::l2))@〈c1,false〉::rs)).
194
195 lemma sem_init_current_gen : Realize ? init_current_gen R_init_current_gen.
196 #intape 
197 cases (sem_seq ????? (sem_adv_to_mark_l ? (is_marked ?))
198         (sem_seq ????? (sem_clear_mark ?)
199           (sem_seq ????? (sem_move_l ?)
200             (sem_seq ????? (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
201               (sem_seq ????? (sem_move_r ?) (sem_mark ?))))) intape)
202 #k * #outc * #Hloop #HR 
203 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop] -Hloop
204 #l1 #c #l2 #b #l3 #c1 #rs #c0 #b0 #Hl1 #Hl2 #Hc #Hintape
205 cases HR -HR #ta * whd in ⊢ (%→?); #Hta cases (Hta … Hintape) -Hta -Hintape
206   [ * #Hfalse normalize in Hfalse; destruct (Hfalse) ]
207 * #_ #Hta lapply (Hta l1 〈c,true〉 ? (refl ??) ??) [@Hl1|%] -Hta #Hta
208 * #tb * whd in ⊢ (%→?); #Htb lapply (Htb … Hta) -Htb -Hta #Htb 
209 * #tc * whd in ⊢ (%→?); #Htc lapply (Htc … Htb) -Htc -Htb 
210 generalize in match Hc; generalize in match Hl2; cases l2
211   [#_ whd in ⊢ ((???%)→?); #Htemp destruct (Htemp) 
212    whd in ⊢ ((???(??%%%))→?); #Htc
213    * #td * whd in ⊢ (%→?); #Htd cases (Htd … Htc) -Htd
214     [2: * whd in ⊢ (??%?→?); #Htemp destruct (Htemp) ]
215    * #_ #Htd >Htd in Htc; -Htd #Htd
216    * #te * whd in ⊢ (%→?); #Hte lapply (Hte … Htd) -Htd
217    >reverse_append >reverse_cons 
218    whd in ⊢ ((???(??%%%))→?); #Hte
219    whd in ⊢ (%→?); #Houtc lapply (Houtc … Hte) -Houtc -Hte #Houtc
220    >Houtc %
221   |#d #tl #Htl #Hc0 whd in ⊢ ((???(??%%%))→?); #Htc
222    * #td * whd in ⊢ (%→?); #Htd cases (Htd … Htc) -Htd
223     [* >(Htl … (memb_hd …)) whd in ⊢ (??%?→?); #Htemp destruct (Htemp)]    
224    * #Hd #Htd lapply (Htd … (refl ??) (refl ??) ?)
225     [#x #membx @Htl @memb_cons @membx] -Htd #Htd
226    * #te * whd in ⊢ (%→?); #Hte lapply (Hte … Htd) -Htd
227    >reverse_append >reverse_cons >reverse_cons
228    >reverse_cons in Hc0; >reverse_cons cases (reverse ? tl)
229      [normalize in ⊢ (%→?); #Hc0 destruct (Hc0) #Hte 
230       whd in ⊢ (%→?); #Houtc lapply (Houtc … Hte) -Houtc -Hte #Houtc
231       >Houtc %
232      |* #c2 #b2 #tl2 normalize in ⊢ (%→?); #Hc0 destruct (Hc0)  
233       whd in ⊢ ((???(??%%%))→?); #Hte 
234       whd in ⊢ (%→?); #Houtc lapply (Houtc … Hte) -Houtc -Hte #Houtc
235       >Houtc >associative_append >associative_append >associative_append %
236      ]
237    ]
238 qed.
239 *)
240
241 definition init_current ≝ 
242   seq ? (adv_to_mark_l ? (is_marked ?))
243     (seq ? (clear_mark ?)
244        (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
245           (seq ? (move_r ?) (mark ?)))).
246           
247 definition R_init_current ≝ λt1,t2.
248   ∀l1,c,l2,b,l3,c1,rs,c0,b0. no_marks l1 → no_grids l2 → is_grid c = false → 
249   Some ? 〈c0,b0〉 = option_hd ? (reverse ? (〈c,true〉::l2)) → 
250   t1 = midtape STape (l1@〈c,true〉::l2@〈grid,b〉::l3) 〈c1,false〉 rs → 
251   t2 = midtape STape (〈grid,b〉::l3) 〈c0,true〉
252         ((tail ? (reverse ? (l1@〈c,false〉::l2))@〈c1,false〉::rs)).
253
254 lemma sem_init_current : Realize ? init_current R_init_current.
255 #intape 
256 cases (sem_seq ????? (sem_adv_to_mark_l ? (is_marked ?))
257         (sem_seq ????? (sem_clear_mark ?)
258            (sem_seq ????? (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
259              (sem_seq ????? (sem_move_r ?) (sem_mark ?)))) intape)
260 #k * #outc * #Hloop #HR 
261 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop]
262 cases HR -HR #ta * whd in ⊢ (%→?); #Hta 
263 * #tb * whd in ⊢ (%→?); #Htb 
264 * #tc * whd in ⊢ (%→?); #Htc 
265 * #td * whd in ⊢ (%→%→?); #Htd #Houtc
266 #l1 #c #l2 #b #l3 #c1 #rs #c0 #b0 #Hl1 #Hl2 #Hc #Hc0 #Hintape
267 cases (Hta … Hintape) [ * #Hfalse normalize in Hfalse; destruct (Hfalse) ]
268 -Hta * #_ #Hta lapply (Hta l1 〈c,true〉 ? (refl ??) ??) [@Hl1|%]
269 -Hta #Hta lapply (Htb … Hta) -Htb #Htb cases (Htc … Htb) [ >Hc -Hc * #Hc destruct (Hc) ] 
270 -Htc * #_ #Htc lapply (Htc … (refl ??) (refl ??) ?) [@Hl2]
271 -Htc #Htc lapply (Htd … Htc) -Htd
272 >reverse_append >reverse_cons 
273 >reverse_cons in Hc0; cases (reverse … l2)
274 [ normalize in ⊢ (%→?); #Hc0 destruct (Hc0)
275   #Htd >(Houtc … Htd) %
276 | * #c2 #b2 #tl2 normalize in ⊢ (%→?);
277   #Hc0 #Htd >(Houtc … Htd)
278   whd in ⊢ (???%); destruct (Hc0)
279   >associative_append >associative_append %
280 ]
281 qed.
282
283 definition match_tuple_step ≝ 
284   ifTM ? (test_char ? (λc:STape.¬ is_grid (\fst c))) 
285    (single_finalTM ? 
286      (seq ? compare
287       (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
288         (nop ?)
289         (seq ? mark_next_tuple 
290            (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
291              (mark ?) (seq ? (move_l ?) init_current) tc_true)) tc_true)))
292     (nop ?) tc_true.
293
294 definition R_match_tuple_step_true ≝ λt1,t2.
295   ∀ls,cur,rs.t1 = midtape STape ls cur rs → 
296   \fst cur ≠ grid ∧ 
297   (∀ls0,c,l1,l2,c1,l3,l4,rs0,n.
298    only_bits_or_nulls l1 → no_marks l1 (* → no_grids l2 *) → 
299    bit_or_null c = true → bit_or_null c1 = true →
300    only_bits_or_nulls l3 → S n = |l1| → |l1| = |l3| →
301    table_TM (S n) (l2@〈c1,false〉::l3@〈comma,false〉::l4) → 
302    ls = 〈grid,false〉::ls0 → cur = 〈c,true〉 → 
303    rs = l1@〈grid,false〉::l2@〈c1,true〉::l3@〈comma,false〉::l4@〈grid,false〉::rs0 → 
304    (* facciamo match *)
305    (〈c,false〉::l1 = 〈c1,false〉::l3 ∧
306    t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls0) 〈grid,false〉
307          (l2@〈c1,false〉::l3@〈comma,true〉::l4@〈grid,false〉::rs0))
308    ∨
309    (* non facciamo match e marchiamo la prossima tupla *)
310    (〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧
311     ∃c2,l5,l6.l4 = l5@〈bar,false〉::〈c2,false〉::l6 ∧
312     (* condizioni su l5 l6 l7 *)
313     t2 = midtape STape (〈grid,false〉::ls0) 〈c,true〉 
314           (l1@〈grid,false〉::l2@〈c1,false〉::l3@〈comma,false〉::
315            l5@〈bar,false〉::〈c2,true〉::l6@〈grid,false〉::rs0))
316    ∨  
317    (* non facciamo match e non c'è una prossima tupla:
318       non specifichiamo condizioni sul nastro di output, perché
319       non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
320    (〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧ no_bars l4 ∧ current ? t2 = Some ? 〈grid,true〉)).  
321   
322 definition R_match_tuple_step_false ≝ λt1,t2.
323   ∀ls,c,rs.t1 = midtape STape ls c rs → is_grid (\fst c) = true ∧ t2 = t1.
324   
325 include alias "basics/logic.ma". 
326
327 (*
328 lemma eq_f4: ∀A1,A2,A3,A4,B.∀f:A1 → A2 →A3 →A4 →B.
329   ∀x1,x2,x3,x4,y1,y2,y3,y4. x1 = y1 → x2 = y2 →x3=y3 →x4 = y4 →   
330     f x1 x2 x3 x4 = f y1 y2 y3 y4.
331 //
332 qed-. *)
333
334 lemma some_option_hd: ∀A.∀l:list A.∀a.∃b.
335   Some ? b = option_hd ? (l@[a]) .
336 #A #l #a cases l normalize /2/
337 qed.
338
339 axiom tech_split2 : ∀A,l1,l2,l3,l4,x. 
340   memb A x l1 = false → memb ? x l3 = false → 
341   l1@x::l2 = l3@x::l4 → l1 = l3 ∧ l2 = l4.
342   
343 axiom injective_append : ∀A,l.injective … (λx.append A x l).
344
345 lemma sem_match_tuple_step: 
346     accRealize ? match_tuple_step (inr … (inl … (inr … start_nop))) 
347     R_match_tuple_step_true R_match_tuple_step_false.
348 @(acc_sem_if_app … (sem_test_char ? (λc:STape.¬ is_grid (\fst c))) …
349   (sem_seq … sem_compare
350     (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
351       (sem_nop …)
352         (sem_seq … sem_mark_next_tuple 
353            (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
354              (sem_mark ?) (sem_seq … (sem_move_l …) (sem_init_current …))))))
355   (sem_nop ?) …)
356 [(* is_grid: termination case *)
357  2:#t1 #t2 #t3 whd in ⊢ (%→?); #H #H1 whd #ls #c #rs #Ht1
358   cases (H c ?) [2: >Ht1 %] #Hgrid #Heq %
359     [@injective_notb @Hgrid | <Heq @H1]
360 |#tapea #tapeout #tapeb whd in ⊢ (%→?); #Hcur
361  * #tapec * whd in ⊢ (%→?); #Hcompare #Hor 
362  #ls #cur #rs #Htapea >Htapea in Hcur; #Hcur cases (Hcur ? (refl ??)) 
363  -Hcur #Hcur #Htapeb %
364  [ % #Hfalse >Hfalse in Hcur; normalize #Hfalse1 destruct (Hfalse1)]
365  #ls0 #c #l1 #l2 #c1 #l3 #l4 #rs0 #n #Hl1bitnull #Hl1marks #Hc #Hc1 #Hl3 #eqn
366  #eqlen #Htable #Hls #Hcur #Hrs -Htapea >Hls in Htapeb; >Hcur >Hrs #Htapeb
367  cases (Hcompare … Htapeb) -Hcompare -Htapeb * #_ #_ #Hcompare
368  cases (Hcompare c c1 l1 l3 l2 (l4@〈grid,false〉::rs0) eqlen Hl1bitnull Hl3 Hl1marks … (refl …) Hc ?)  
369  -Hcompare 
370    [* #Htemp destruct (Htemp) #Htapec %1 % % [%]
371     >Htapec in Hor; -Htapec *
372      [2: * #t3 * whd in ⊢ (%→?); #H @False_ind
373       cases (H … (refl …)) whd in ⊢ ((??%?)→?); #H destruct (H)
374      |* #taped * whd in ⊢ (%→?); #Htaped cases (Htaped ? (refl …)) -Htaped *
375       #Htaped whd in ⊢ (%→?); #Htapeout >Htapeout >Htaped
376       %
377      ]
378    |* #la * #c' * #d' * #lb * #lc * * * #H1 #H2 #H3 #Htapec 
379     cut (〈c,false〉::l1 ≠ 〈c1,false〉::l3) 
380       [>H2 >H3 elim la
381         [@(not_to_not …H1) normalize #H destruct % 
382         |#x #tl @not_to_not normalize #H destruct // 
383         ]
384       ] #Hnoteq
385     cut (bit_or_null d' = true) 
386       [cases la in H3;
387         [normalize in ⊢ (%→?); #H destruct //
388         |#x #tl #H @(Hl3 〈d',false〉)
389          normalize in H; destruct @memb_append_l2 @memb_hd
390         ] 
391       ] #Hd'
392     >Htapec in Hor; -Htapec *
393      [* #taped * whd in ⊢ (%→?); #H @False_ind
394       cases (H … (refl …)) >(bit_or_null_not_grid ? Hd') #Htemp destruct (Htemp)
395      |* #taped * whd in ⊢ (%→?); #H cases (H … (refl …)) -H #_
396       #Htaped * #tapee * whd in ⊢ (%→?); #Htapee  
397       <(associative_append ? lc (〈comma,false〉::l4)) in Htaped; #Htaped
398       cases (Htapee … Htaped ???) -Htaped -Htapee 
399        [* #rs3 * * (* we proceed by cases on rs4 *) 
400          [(* rs4 is empty : the case is absurd since the tape
401             cannot end with a bar *)
402           * #d * #b * * * #Heq1 @False_ind 
403           cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut 
404           >Hcut in Htable; >H3 >associative_append
405           normalize >Heq1 <associative_append >Hcut
406           <associative_append #Htable @(absurd … Htable) 
407           @last_of_table
408          |(* rs4 not empty *)
409           * #d2 #b2 #rs3' * #d  * #b * * * #Heq1 #Hnobars
410           cut (memb STape 〈d2,b2〉 (l2@〈c1,false〉::l3@〈comma,false〉::l4) = true)
411             [@memb_append_l2
412              cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut
413              >Hcut >H3 >associative_append @memb_append_l2 
414              @memb_cons >Heq1 @memb_append_l2 @memb_cons @memb_hd] #d2intable
415           cut (is_grid d2 = false) 
416             [@(no_grids_in_table … Htable … 〈d2,b2〉 d2intable)] #Hd2
417           cut (b2 = false) 
418             [@(no_marks_in_table … Htable … 〈d2,b2〉 d2intable)] #Hb2 
419           >Hb2 in Heq1; #Heq1 -Hb2 -b2
420           whd in ⊢ ((???%)→?); #Htemp destruct (Htemp) #Htapee >Htapee -Htapee *
421            [(* we know current is not grid *)
422             * #tapef * whd in ⊢ (%→?); #Htapef 
423             cases (Htapef … (refl …)) >Hd2 #Htemp destruct (Htemp) 
424            |* #tapef * whd in ⊢ (%→?); #Htapef 
425             cases (Htapef … (refl …)) #_ -Htapef #Htapef
426             * #tapeg >Htapef -Htapef * 
427             (* move_l *)
428             whd in ⊢ (%→?); 
429             #H lapply (H … (refl …)) whd in ⊢ (???%→?); -H  #Htapeg
430             >Htapeg -Htapeg
431             (* init_current *)
432              whd in ⊢ (%→?); #Htapeout
433              cases (some_option_hd ? (reverse ? (reverse ? la)) 〈c',true〉)
434              * #c00 #b00 #Hoption
435              lapply 
436               (Htapeout (reverse ? rs3 @〈d',false〉::reverse ? la@reverse ? l2@(〈grid,false〉::reverse ? lb))
437               c' (reverse ? la) false ls0 bar (〈d2,true〉::rs3'@〈grid,false〉::rs0) c00 b00 ?????) -Htapeout
438                [whd in ⊢ (??(??%??)?); @eq_f3 [2:%|3: %]
439                 >associative_append 
440                  generalize in match (〈c',true〉::reverse ? la@〈grid,false〉::ls0); #l
441                 whd in ⊢ (???(???%)); >associative_append >associative_append % 
442                |>reverse_cons @Hoption
443                |cases la in H2; 
444                  [normalize in ⊢ (%→?); #Htemp destruct (Htemp) 
445                   @bit_or_null_not_grid @Hc
446                  |#x #tl normalize in ⊢ (%→?); #Htemp destruct (Htemp)
447                   @bit_or_null_not_grid @(Hl1bitnull 〈c',false〉) @memb_append_l2 @memb_hd
448                  ]
449                |cut (only_bits_or_nulls (la@(〈c',false〉::lb)))
450                  [<H2 whd #c0 #Hmemb cases (orb_true_l … Hmemb)
451                    [#eqc0 >(\P eqc0) @Hc |@Hl1bitnull]
452                  |#Hl1' #x #Hx @bit_or_null_not_grid @Hl1'
453                   @memb_append_l1 @daemon
454                  ]
455                |@daemon] #Htapeout % %2 % //
456             @(ex_intro … d2)
457             cut (∃rs32.rs3 = lc@〈comma,false〉::rs32) 
458                  [ (*cases (tech_split STape (λc.c == 〈bar,false〉) l4)
459                   [
460                   | * #l41 * * #cbar #bfalse * #l42 * * #Hbar #Hl4 #Hl41
461                     @(ex_intro ?? l41) >Hl4 in Heq1; #Heq1
462                 
463                 cut (sublist … lc l3)
464                   [ #x #Hx cases la in H3;
465                     [ normalize #H3 destruct (H3) @Hx
466                     | #p #la' normalize #Hla' destruct (Hla')
467                       @memb_append_l2 @memb_cons @Hx ] ] #Hsublist*)
468                 @daemon]
469                 * #rs32 #Hrs3
470                 (* cut 
471                 (〈c1,false〉::l3@〈comma,false〉::l4= la@〈d',false〉::rs3@〈bar,false〉::〈d2,b2〉::rs3')
472                 [@daemon] #Hcut *)
473                 cut (l4=rs32@〈bar,false〉::〈d2,false〉::rs3')
474                 [ >Hrs3 in Heq1; @daemon ] #Hl4
475                 @(ex_intro … rs32) @(ex_intro … rs3') % [@Hl4]
476                 >Htapeout @eq_f2
477                    [(* by Hoption, H2 *) @daemon
478                    |(*>Hrs3 *)>append_cons
479                     > (?:l1@〈grid,false〉::l2@〈c1,false〉::l3@〈comma,false〉::rs32@〈bar,false〉::〈d2,true〉::rs3'@〈grid,false〉::rs
480                         = (l1@〈grid,false〉::l2@〈c1,false〉::l3@〈comma,false〉::rs32@[〈bar,false〉])@〈d2,true〉::rs3'@〈grid,false〉::rs)
481                     [|>associative_append normalize 
482                       >associative_append normalize
483                       >associative_append normalize
484                       >associative_append normalize
485                        % ]
486                     >reverse_append >reverse_append >reverse_cons
487                     >reverse_reverse >reverse_cons >reverse_reverse
488                     >reverse_append >reverse_append >reverse_cons
489                     >reverse_reverse >reverse_reverse >reverse_reverse
490                     >(?:(la@[〈c',false〉])@((((lb@[〈grid,false〉])@l2)@la)@[〈d',false〉])@rs3
491                        =((la@〈c',false〉::lb)@([〈grid,false〉]@l2@la@[〈d',false〉]@rs3)))
492                     [|>associative_append >associative_append 
493                       >associative_append >associative_append >associative_append
494                       >associative_append % ]
495                     <H2 normalize in ⊢ (??%?); >Hrs3
496                     >associative_append >associative_append normalize
497                     >associative_append >associative_append
498                     @eq_f @eq_f @eq_f
499                     >(?:la@(〈d',false〉::lc@〈comma,false〉::rs32)@〈bar,false〉::〈d2,true〉::rs3'@〈grid,false〉::rs0 = 
500                         (la@〈d',false〉::lc)@〈comma,false〉::rs32@〈bar,false〉::〈d2,true〉::rs3'@〈grid,false〉::rs0 )
501                     [| >associative_append normalize >associative_append % ]
502                     <H3 %
503                    ]
504                  ]
505               ]
506        |* #Hnobars #Htapee >Htapee -Htapee *
507          [whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
508           cases (Htapef … (refl …)) -Htapef #_ #Htapef >Htapef -Htapef
509           whd in ⊢ (%→?); #Htapeout %2 % 
510           [% [//] whd #x #Hx @Hnobars @memb_append_l2 @memb_cons //
511           | >(Htapeout … (refl …)) % ]
512          |whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
513           cases (Htapef … (refl …)) -Htapef 
514           whd in ⊢ ((??%?)→?); #Htemp destruct (Htemp) 
515          ]
516        |(* no marks in table *)
517         #x #membx @(no_marks_in_table … Htable) 
518         @memb_append_l2
519         cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut >Hcut
520         >H3 >associative_append @memb_append_l2 @memb_cons @membx
521        |(* no grids in table *)
522         #x #membx @(no_grids_in_table … Htable) 
523         @memb_append_l2
524         cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut >Hcut
525         >H3 >associative_append @memb_append_l2 @memb_cons @membx
526        |whd in ⊢ (??%?); >(bit_or_null_not_grid … Hd') >(bit_or_null_not_bar … Hd') %
527        ]
528      ]
529    |#x #membx @(no_marks_in_table … Htable) 
530     @memb_append_l2 @memb_cons @memb_append_l1 @membx 
531    |#x #membx @(no_marks_in_table … Htable) 
532     @memb_append_l1 @membx
533    |%
534    ]
535  ]
536 qed.
537
538 (* 
539   MATCH TUPLE
540
541   scrolls through the tuples in the transition table until one matching the
542   current configuration is found
543 *)
544
545 definition match_tuple ≝  whileTM ? match_tuple_step (inr … (inl … (inr … start_nop))).
546
547 lemma is_grid_true : ∀c.is_grid c = true → c = grid.
548 * normalize [ #b ] #H // destruct (H)
549 qed.
550
551 (* possible variante ? 
552 definition weakR_match_tuple ≝ λt1,t2.
553   (∀ls,cur,rs,b. t1 = midtape STape ls 〈grid,b〉 rs → t2 = t1) ∧
554   (∀c,l1,c1,l2,l3,ls0,rs0,n.
555   t1 = midtape STape (〈grid,false〉::ls0) 〈bit c,true〉 rs 
556     (l1@〈grid,false〉::l2@〈bit c1,true〉::l3@〈grid,false〉::rs0) → 
557   only_bits_or_nulls l1 → no_marks l1 → S n = |l1| →
558   table_TM (S n) (l2@〈c1,false〉::l3) → 
559   (* facciamo match *)
560   (∃l4,newc,mv,l5.
561    〈c1,false〉::l3 = l4@〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l5 ∧
562    t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls0) 〈grid,false〉
563         (l2@l4@〈c,false〉::l1@〈comma,true〉::newc@〈comma,false〉::mv::l5@
564         〈grid,false〉::rs0))
565   ∨
566   (* non facciamo match su nessuna tupla;
567      non specifichiamo condizioni sul nastro di output, perché
568      non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
569   (current ? t2 = Some ? 〈grid,true〉 ∧
570    ∀l4,newc,mv,l5.
571    〈c1,false〉::l3 ≠ l4@〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l5)).  
572 *) 
573
574 definition R_match_tuple0 ≝ λt1,t2.
575   ∀ls,cur,rs.
576   t1 = midtape STape ls cur rs → 
577   (is_grid (\fst cur) = true → t2 = t1) ∧
578   (∀c,l1,c1,l2,l3,ls0,rs0,n.
579   ls = 〈grid,false〉::ls0 → 
580   cur = 〈c,true〉 → 
581   rs = l1@〈grid,false〉::l2@〈bar,false〉::〈c1,true〉::l3@〈grid,false〉::rs0 → 
582   is_bit c = true → is_bit c1 = true → 
583   only_bits_or_nulls l1 → no_marks l1 → S n = |l1| →
584   table_TM (S n) (l2@〈bar,false〉::〈c1,false〉::l3) → 
585   (* facciamo match *)
586   (∃l4,newc,mv,l5.
587    〈bar,false〉::〈c1,false〉::l3 = l4@〈bar,false〉::〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l5 ∧
588    t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls0) 〈grid,false〉
589         (l2@l4@〈bar,false〉::〈c,false〉::l1@〈comma,true〉::newc@〈comma,false〉::mv::l5@
590         〈grid,false〉::rs0))
591   ∨
592   (* non facciamo match su nessuna tupla;
593      non specifichiamo condizioni sul nastro di output, perché
594      non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
595   (current ? t2 = Some ? 〈grid,true〉 ∧
596    ∀l4,newc,mv,l5.
597    〈bar,false〉::〈c1,false〉::l3 ≠ l4@〈bar,false〉::〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l5)).  
598
599 axiom table_bit_after_bar : 
600   ∀n,l1,c,l2.table_TM n (l1@〈bar,false〉::〈c,false〉::l2) → is_bit c = true.
601
602 lemma wsem_match_tuple : WRealize ? match_tuple R_match_tuple0.
603 #intape #k #outc #Hloop 
604 lapply (sem_while … sem_match_tuple_step intape k outc Hloop) [%] -Hloop
605 * #ta * #Hstar @(star_ind_l ??????? Hstar)
606 [ #tb whd in ⊢ (%→?); #Hleft
607   #ls #cur #rs #Htb cases (Hleft … Htb) #Hgrid #Houtc %
608   [ #_ @Houtc 
609   | #c #l1 #c1 #l2 #l3 #ls0 #rs0 #n #Hls #Hcur #Hrs 
610     >Hcur in Hgrid; #Hgrid >(is_grid_true … Hgrid) normalize in ⊢ (%→?);
611     #Hc destruct (Hc)
612   ]
613 | (* in the interesting case, we execute a true iteration, then we restart the
614      while cycle, finally we end with a false iteration *)
615   #tb #tc #td whd in ⊢ (%→?); #Htc
616   #Hstar1 #IH whd in ⊢ (%→?); #Hright lapply (IH Hright) -IH whd in ⊢ (%→?); #IH
617   #ls #cur #rs #Htb %
618   [ (* cur can't be true because we assume at least one iteration *)
619     #Hcur cases (Htc … Htb) * #Hfalse @False_ind @Hfalse @(is_grid_true … Hcur)
620   | (* current and a tuple are marked *)
621    #c #l1 #c1 #l2 #l3 #ls0 #rs0 #n #Hls #Hcur #Hrs #Hc #Hc1 #Hl1bitnull #Hl1marks 
622    #Hl1len #Htable cases (Htc … Htb) -Htc -Htb * #_ #Htc
623    (* expose the marked tuple in table *)
624    cut (∃la,lb,mv,lc.l3 = la@〈comma,false〉::lb@〈comma,false〉::mv::lc ∧
625          S n = |la| ∧ only_bits_or_nulls la)
626    [@daemon] * #la * #lb * #mv * #lc * * #Hl3 #Hlalen #Hlabitnull
627    >Hl3 in Htable; >append_cons #Htable
628    >(?: l2@〈bar,false〉::〈c1,true〉::l3@〈grid,false〉::rs0
629       = (l2@[〈bar,false〉])@〈c1,true〉::la@〈comma,false〉::(lb@〈comma,false〉::mv::
630          lc)@〈grid,false〉::rs0) in Hrs;
631    [| >associative_append normalize >Hl3
632       >associative_append normalize % ] #Hrs
633    cases (Htc ????????? Hl1bitnull Hl1marks ?? Hlabitnull Hl1len ? Htable Hls Hcur Hrs)
634    [5: <Hl1len @Hlalen
635    |4: whd in ⊢ (??%?); >Hc1 %
636    |3: whd in ⊢ (??%?); >Hc %
637    |-Htc *
638      [ (* case 1: match successful *)
639        * #Heq #Htc % %{[]} %{lb} %{mv} %{lc} destruct (Heq) %
640        [%
641        | cases (IH … Htc) -IH #Houtc #_ >(Houtc (refl ??)) 
642          >Htc @eq_f normalize >associative_append normalize
643          >associative_append normalize %
644        ]     
645      | (* case 2: tuples don't match, we still have other tuples to try *)
646        * #Hdiff * #c2 * #l5 * #l6 * #Heqlblc #Htc
647        cases (IH ??? … Htc) -IH #_ #IH 
648        (* by induction hypothesis *)
649        lapply (IH ? l1 c2 (l2@〈bar,false〉::〈c1,false〉::la@〈comma,false〉::l5) l6 ? rs0 n (refl ??) (refl ??) ???????)
650        [ generalize in match Htable;
651          >associative_append normalize 
652          >associative_append normalize >Heqlblc
653          >associative_append normalize //
654        | @Hl1len
655        | @Hl1marks
656        | @Hl1bitnull
657        | (*???*) @daemon
658        | @Hc
659        | >associative_append normalize 
660          >associative_append normalize
661          >associative_append %
662        |-IH * 
663          [ (* the while finally matches a tuple *)
664            * #l7 * #newc * #mv0 * #l8 * #Hl7l8 #Houtc %
665            >Heqlblc @(ex_intro ?? (〈bar,false〉::〈c1,false〉::la@〈comma,false〉::l5@l7))
666            %{newc} %{mv0} %{l8} %
667            [ normalize >Hl7l8 >associative_append normalize 
668              >associative_append %
669            | >Houtc @eq_f >associative_append normalize
670              >associative_append normalize >associative_append 
671              normalize >associative_append %
672            ]
673          | (* the while fails finding a tuple: there are no matches in the whole table *)
674            * #Houtc #Hdiff1 %2 %
675            [ @Houtc
676            | #l50 #newc #mv0 #l51 >Heqlblc 
677              @daemon
678            ]
679          ]
680        ]
681      ]
682    | (* match failed and there is no next tuple: the next while cycle will just exit *)
683      * * #Hdiff #Hnobars generalize in match (refl ? tc);
684      cases tc in ⊢ (???% → %);
685      [ #_ normalize in ⊢ (??%?→?); #Hfalse destruct (Hfalse)
686      |2,3: #x #xs #_ normalize in ⊢ (??%?→?); #Hfalse destruct (Hfalse) ]
687      #ls1 #cur1 #rs1 #Htc normalize in ⊢ (??%?→?); #Hcur1
688      cases (IH … Htc) -IH #IH #_ %2 %
689      [ destruct (Hcur1) >IH [ >Htc % | % ]
690      | #l4 #newc #mv0 #l5
691        (* no_bars except the first one, where the tuple does not match ⇒ 
692           no match *)
693         @daemon
694      ]
695    ]
696  ]
697 qed.
698
699 definition R_match_tuple ≝ λt1,t2.
700   ∀ls,c,l1,c1,l2,rs,n.
701   is_bit c = true → is_bit c1 = true →
702   only_bits_or_nulls l1 → no_marks l1 → S n = |l1| → 
703   table_TM (S n) (〈bar,false〉::〈c1,false〉::l2) → 
704   t1 = midtape STape (〈grid,false〉::ls) 〈c,true〉 
705          (l1@〈grid,false〉::〈bar,false〉::〈c1,true〉::l2@〈grid,false〉::rs) → 
706   (* facciamo match *)
707   (∃l3,newc,mv,l4.
708    〈bar,false〉::〈c1,false〉::l2 = l3@〈bar,false〉::〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l4 ∧
709    t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls) 〈grid,false〉
710         (l3@〈bar,false〉::〈c,false〉::l1@〈comma,true〉::newc@〈comma,false〉::mv::l4@〈grid,false〉::rs))
711   ∨
712   (* non facciamo match su nessuna tupla;
713      non specifichiamo condizioni sul nastro di output, perché
714      non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
715   (current ? t2 = Some ? 〈grid,true〉 ∧
716    ∀l3,newc,mv,l4.
717    〈bar,false〉::〈c1,false〉::l2 ≠ l3@〈bar,false〉::〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l4). 
718
719 (* we still haven't proved termination *)
720 axiom sem_match_tuple0 : Realize ? match_tuple R_match_tuple0.
721
722 lemma sem_match_tuple : Realize ? match_tuple R_match_tuple.
723 generalize in match sem_match_tuple0; @Realize_to_Realize
724 #t1 #t2 #HR #ls #c #l1 #c1 #l2 #rs #n #Hc #Hc1 #Hl1bitsnulls #Hl1marks #Hl1len #Htable #Ht1
725 cases (HR … Ht1) -HR #_ #HR
726 @(HR ??? [] … (refl ??) (refl ??) (refl ??) Hc Hc1 Hl1bitsnulls Hl1marks
727           Hl1len  Htable)
728 qed.