]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/universal/match_machines.ma
49f1b69238a630ef6bfc63d655be1977427c8a04
[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   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 (proj2 ?? Hleft … Hrs)
82    [ * #Hfalse >Hfalse in Hc; #Htf destruct (Htf)
83    | * * #_ #Hta #_ cases (tech_split STape (λc.is_bar (\fst c)) rs1)
84      [ #H1 %2 % [@H1]
85       lapply (Hta rs1 〈grid,false〉 rs2 (refl ??) ? ?)
86        [ * #x #b #Hx whd in ⊢ (??%?); >(Hrs1' … Hx) >(H1 … Hx) %
87        | %
88        | -Hta #Hta cases Hright
89          [ * #tb * whd in ⊢ (%→?); * * #c1 * >Hta 
90           whd in ⊢ ((??%?)→?); #H destruct (H) whd in ⊢ ((??%?)→?); #H destruct
91          | * #tb * whd in ⊢ (%→?); * #_ #Htb >Htb >Hta 
92            whd in ⊢ (%→?); #H @H
93          ]
94        ]
95     |* #rs3 * #c0 * #rs4 * * #Hc0 #Hsplit #Hrs3
96      % @(ex_intro ?? rs3) @(ex_intro ?? rs4)
97      lapply (Hta rs3 c0 (rs4@〈grid,false〉::rs2) ???)
98       [#x #Hrs3' whd in ⊢ (??%?); >Hsplit in Hrs1;>Hsplit in Hrs3;
99        #Hrs3 #Hrs1 >(Hrs1 …) [| @memb_append_l1 @Hrs3'|]
100        >(Hrs3 … Hrs3') @Hrs1' >Hsplit @memb_append_l1 //
101       |whd in ⊢ (??%?); >Hc0 %
102       |>Hsplit >associative_append % 
103       ]-Hta #Hta
104      cases Hright -Hright 
105       [* whd in ⊢ (%→?); #tb * * * #c1 * >Hta -Hta 
106        whd in ⊢ (??%?→?); #H destruct (H) #Hc1 #Htb
107        whd in ⊢ (%→?); #Houtc 
108        cut (c1=〈bar,false〉)
109         [lapply Hc1 lapply Hsplit cases c1 #c1l #c1r #Hsplit
110          cases c1l normalize 
111           [#b #H destruct |2,3,5:#H destruct] 
112            #_ @eq_f @(Hrs1 … 〈c1l,c1r〉) >Hsplit @memb_append_l2 @memb_hd]
113        #Hcut lapply Hsplit -Hsplit
114        cases rs4 in Htb;
115         [#Htb lapply(Houtc … Htb) -Houtc #Houtc #Hsplit
116          @(ex_intro ?? grid) @(ex_intro ?? false) % 
117           [% [ % [<Hcut @Hsplit |@Hrs3 ] | % ] 
118           |>Houtc >Hcut % 
119           ]
120         |* #r5l #r5r #rs5 #Htb
121          lapply(Houtc … Htb) -Houtc #Houtc #Hsplit
122          @(ex_intro ?? r5l) @(ex_intro ?? r5r) % 
123           [%[%[<Hcut @Hsplit| @Hrs3] | % ]
124           |>Houtc >Hcut % 
125           ]
126         ]
127       |* whd in ⊢ (%→?); #tb * * 
128        #H @False_ind >Hta in H; #H lapply(H c0 (refl …))
129        >Hc0 #H destruct
130       ]
131     ]
132   ]
133 ]
134 qed.
135
136 definition init_current_on_match ≝ 
137   move_l ? · adv_to_mark_l ? (λc:STape.is_grid (\fst c)) · move_r ? · mark ?.
138           
139 definition R_init_current_on_match ≝ λt1,t2.
140   ∀l1,l2,c,rs. no_grids l1 → is_grid c = false → 
141   t1 = midtape STape (l1@〈c,false〉::〈grid,false〉::l2)  〈grid,false〉 rs → 
142   t2 = midtape STape (〈grid,false〉::l2) 〈c,true〉 ((reverse ? l1)@〈grid,false〉::rs).
143
144 lemma sem_init_current_on_match : 
145   Realize ? init_current_on_match R_init_current_on_match.
146 #intape 
147 cases (sem_seq ????? (sem_move_l ?)
148         (sem_seq ????? (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
149            (sem_seq ????? (sem_move_r ?) (sem_mark ?))) intape)
150 #k * #outc * #Hloop #HR 
151 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop] -Hloop
152 #l1 #l2 #c #rs #Hl1 #Hc #Hintape
153 cases HR -HR #ta * whd in ⊢ (%→?); * #_ #Hta lapply (Hta … Hintape) -Hta -Hintape 
154 generalize in match Hl1; cases l1
155   [#Hl1 whd in ⊢ ((???(??%%%))→?); #Hta
156    * #tb * whd in ⊢ (%→?); * #_ #Htb cases (Htb … Hta) -Htb -Hta #_
157    (* [* >Hc #Htemp destruct (Htemp) ] *)
158    #Htb cases (Htb … Hc) -Htb #Htb #_ 
159    lapply (Htb [ ] 〈grid,false〉 ? (refl ??) (refl …) Hl1) 
160    whd in ⊢ ((???(??%%%))→?); -Htb #Htb
161    * #tc * whd in ⊢ (%→?); * #_ #Htc lapply (Htc … Htb) -Htb -Htc 
162    whd in ⊢ ((???(??%%%))→?); #Htc
163    whd in ⊢ (%→?); * #Houtc #_ lapply (Houtc … Htc) -Houtc #Houtc
164    >Houtc % 
165   |#d #tl #Htl whd in ⊢ ((???(??%%%))→?); #Hta
166    * #tb * whd in ⊢ (%→?); * #_ #Htb cases (Htb … Hta) -Htb
167    #_ (* [* >(Htl … (memb_hd …)) #Htemp destruct (Htemp)]  *)
168    #Htb cases (Htb ?) -Htb [2: @Htl @memb_hd]
169    >append_cons #Htb #_ lapply (Htb … (refl ??) (refl …) ?)
170     [#x #membx cases (memb_append … membx) -membx #membx
171       [@Htl @memb_cons @membx | >(memb_single … membx) @Hc]]-Htb  #Htb
172    * #tc * whd in ⊢ (%→?); * #_ #Htc lapply (Htc … Htb) -Htb -Htc 
173    >reverse_append >associative_append whd in ⊢ ((???(??%%%))→?); #Htc
174    whd in ⊢ (%→?); * #Houtc #_ lapply (Houtc … Htc) -Houtc #Houtc 
175    >Houtc >reverse_cons >associative_append % 
176   ]
177 qed.   
178
179 (*
180 definition init_current_gen ≝ 
181   seq ? (adv_to_mark_l ? (is_marked ?))
182     (seq ? (clear_mark ?)
183        (seq ? (move_l ?)
184          (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
185             (seq ? (move_r ?) (mark ?))))).
186           
187 definition R_init_current_gen ≝ λt1,t2.
188   ∀l1,c,l2,b,l3,c1,rs,c0,b0. no_marks l1 → no_grids l2 →
189   Some ? 〈c0,b0〉 = option_hd ? (reverse ? (〈c,true〉::l2)) → 
190   t1 = midtape STape (l1@〈c,true〉::l2@〈grid,b〉::l3) 〈c1,false〉 rs → 
191   t2 = midtape STape (〈grid,b〉::l3) 〈c0,true〉
192         ((tail ? (reverse ? (l1@〈c,false〉::l2))@〈c1,false〉::rs)).
193
194 lemma sem_init_current_gen : Realize ? init_current_gen R_init_current_gen.
195 #intape 
196 cases (sem_seq ????? (sem_adv_to_mark_l ? (is_marked ?))
197         (sem_seq ????? (sem_clear_mark ?)
198           (sem_seq ????? (sem_move_l ?)
199             (sem_seq ????? (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
200               (sem_seq ????? (sem_move_r ?) (sem_mark ?))))) intape)
201 #k * #outc * #Hloop #HR 
202 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop] -Hloop
203 #l1 #c #l2 #b #l3 #c1 #rs #c0 #b0 #Hl1 #Hl2 #Hc #Hintape
204 cases HR -HR #ta * whd in ⊢ (%→?); #Hta cases (Hta … Hintape) -Hta -Hintape
205   [ * #Hfalse normalize in Hfalse; destruct (Hfalse) ]
206 * #_ #Hta lapply (Hta l1 〈c,true〉 ? (refl ??) ??) [@Hl1|%] -Hta #Hta
207 * #tb * whd in ⊢ (%→?); #Htb lapply (Htb … Hta) -Htb -Hta #Htb 
208 * #tc * whd in ⊢ (%→?); #Htc lapply (Htc … Htb) -Htc -Htb 
209 generalize in match Hc; generalize in match Hl2; cases l2
210   [#_ whd in ⊢ ((???%)→?); #Htemp destruct (Htemp) 
211    whd in ⊢ ((???(??%%%))→?); #Htc
212    * #td * whd in ⊢ (%→?); #Htd cases (Htd … Htc) -Htd
213     [2: * whd in ⊢ (??%?→?); #Htemp destruct (Htemp) ]
214    * #_ #Htd >Htd in Htc; -Htd #Htd
215    * #te * whd in ⊢ (%→?); #Hte lapply (Hte … Htd) -Htd
216    >reverse_append >reverse_cons 
217    whd in ⊢ ((???(??%%%))→?); #Hte
218    whd in ⊢ (%→?); #Houtc lapply (Houtc … Hte) -Houtc -Hte #Houtc
219    >Houtc %
220   |#d #tl #Htl #Hc0 whd in ⊢ ((???(??%%%))→?); #Htc
221    * #td * whd in ⊢ (%→?); #Htd cases (Htd … Htc) -Htd
222     [* >(Htl … (memb_hd …)) whd in ⊢ (??%?→?); #Htemp destruct (Htemp)]    
223    * #Hd #Htd lapply (Htd … (refl ??) (refl ??) ?)
224     [#x #membx @Htl @memb_cons @membx] -Htd #Htd
225    * #te * whd in ⊢ (%→?); #Hte lapply (Hte … Htd) -Htd
226    >reverse_append >reverse_cons >reverse_cons
227    >reverse_cons in Hc0; >reverse_cons cases (reverse ? tl)
228      [normalize in ⊢ (%→?); #Hc0 destruct (Hc0) #Hte 
229       whd in ⊢ (%→?); #Houtc lapply (Houtc … Hte) -Houtc -Hte #Houtc
230       >Houtc %
231      |* #c2 #b2 #tl2 normalize in ⊢ (%→?); #Hc0 destruct (Hc0)  
232       whd in ⊢ ((???(??%%%))→?); #Hte 
233       whd in ⊢ (%→?); #Houtc lapply (Houtc … Hte) -Houtc -Hte #Houtc
234       >Houtc >associative_append >associative_append >associative_append %
235      ]
236    ]
237 qed.
238 *)
239
240 definition init_current ≝ 
241   adv_to_mark_l ? (is_marked ?) ·clear_mark ? ·
242     adv_to_mark_l ? (λc:STape.is_grid (\fst c)) · move_r ? · mark ?.
243           
244 definition R_init_current ≝ λt1,t2.
245   ∀l1,c,l2,b,l3,c1,rs,c0,b0. no_marks l1 → no_grids l2 → is_grid c = false → 
246   Some ? 〈c0,b0〉 = option_hd ? (reverse ? (〈c,true〉::l2)) → 
247   t1 = midtape STape (l1@〈c,true〉::l2@〈grid,b〉::l3) 〈c1,false〉 rs → 
248   t2 = midtape STape (〈grid,b〉::l3) 〈c0,true〉
249         ((tail ? (reverse ? (l1@〈c,false〉::l2))@〈c1,false〉::rs)).
250
251 lemma sem_init_current : Realize ? init_current R_init_current.
252 #intape 
253 cases (sem_seq ????? (sem_adv_to_mark_l ? (is_marked ?))
254         (sem_seq ????? (sem_clear_mark ?)
255            (sem_seq ????? (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
256              (sem_seq ????? (sem_move_r ?) (sem_mark ?)))) intape)
257 #k * #outc * #Hloop #HR 
258 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop]
259 cases HR -HR #ta * whd in ⊢ (%→?); * #_ #Hta 
260 * #tb * whd in ⊢ (%→?); * #_ #Htb 
261 * #tc * whd in ⊢ (%→?); * #_ #Htc 
262 * #td * whd in ⊢ (%→%→?); * #_ #Htd * #Houtc #_
263 #l1 #c #l2 #b #l3 #c1 #rs #c0 #b0 #Hl1 #Hl2 #Hc #Hc0 #Hintape
264 cases (Hta … Hintape) #_ -Hta #Hta cases (Hta (refl …)) -Hta 
265 #Hta #_ lapply (Hta l1 〈c,true〉 ? (refl ??) ??) [@Hl1|%]
266 -Hta #Hta lapply (Htb … Hta) -Htb #Htb cases (Htc … Htb) #_ #Htc
267 cases (Htc Hc) -Htc #Htc #_ lapply (Htc … (refl ??) (refl ??) ?) [@Hl2]
268 -Htc #Htc lapply (Htd … Htc) -Htd
269 >reverse_append >reverse_cons 
270 >reverse_cons in Hc0; cases (reverse … l2)
271 [ normalize in ⊢ (%→?); #Hc0 destruct (Hc0)
272   #Htd >(Houtc … Htd) %
273 | * #c2 #b2 #tl2 normalize in ⊢ (%→?);
274   #Hc0 #Htd >(Houtc … Htd)
275   whd in ⊢ (???%); destruct (Hc0)
276   >associative_append >associative_append %
277 ]
278 qed.
279
280 definition match_tuple_step ≝ 
281   ifTM ? (test_char ? (λc:STape.¬ is_grid (\fst c))) 
282    (single_finalTM ? 
283      (compare ·
284       (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
285         (nop ?)
286         (mark_next_tuple ·
287            (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
288              (mark ?) (move_l ? · init_current) tc_true)) tc_true)))
289     (nop ?) tc_true.
290
291 (* universal version 
292 definition R_match_tuple_step_true ≝ λt1,t2.
293   ∀ls,cur,rs.t1 = midtape STape ls cur rs → 
294   \fst cur ≠ grid ∧ 
295   (∀ls0,c,l1,l2,c1,l3,l4,rs0,n.
296    only_bits_or_nulls l1 → no_marks l1 (* → no_grids l2 *) → 
297    bit_or_null c = true → bit_or_null c1 = true →
298    only_bits_or_nulls l3 → S n = |l1| → |l1| = |l3| →
299    table_TM (S n) (l2@〈c1,false〉::l3@〈comma,false〉::l4) → 
300    ls = 〈grid,false〉::ls0 → cur = 〈c,true〉 → 
301    rs = l1@〈grid,false〉::l2@〈c1,true〉::l3@〈comma,false〉::l4@〈grid,false〉::rs0 → 
302    (* facciamo match *)
303    (〈c,false〉::l1 = 〈c1,false〉::l3 ∧
304    t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls0) 〈grid,false〉
305          (l2@〈c1,false〉::l3@〈comma,true〉::l4@〈grid,false〉::rs0))
306    ∨
307    (* non facciamo match e marchiamo la prossima tupla *)
308    (〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧
309     ∃c2,l5,l6.l4 = l5@〈bar,false〉::〈c2,false〉::l6 ∧
310     (* condizioni su l5 l6 l7 *)
311     t2 = midtape STape (〈grid,false〉::ls0) 〈c,true〉 
312           (l1@〈grid,false〉::l2@〈c1,false〉::l3@〈comma,false〉::
313            l5@〈bar,false〉::〈c2,true〉::l6@〈grid,false〉::rs0))
314    ∨  
315    (* non facciamo match e non c'è una prossima tupla:
316       non specifichiamo condizioni sul nastro di output, perché
317       non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
318    (〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧ no_bars l4 ∧ current ? t2 = Some ? 〈grid,true〉)).  
319 *)
320
321 definition R_match_tuple_step_true ≝ λt1,t2.
322   ∃ls,cur,rs.t1 = midtape STape ls cur rs \wedge 
323   \fst cur ≠ grid ∧ 
324   (∀ls0,c,l1,l2,c1,l3,l4,rs0,n.
325    only_bits_or_nulls l1 → no_marks l1 (* → no_grids l2 *) → 
326    bit_or_null c = true → bit_or_null c1 = true →
327    only_bits_or_nulls l3 → S n = |l1| → |l1| = |l3| →
328    table_TM (S n) (l2@〈c1,false〉::l3@〈comma,false〉::l4) → 
329    ls = 〈grid,false〉::ls0 → cur = 〈c,true〉 → 
330    rs = l1@〈grid,false〉::l2@〈c1,true〉::l3@〈comma,false〉::l4@〈grid,false〉::rs0 → 
331    (* facciamo match *)
332    (〈c,false〉::l1 = 〈c1,false〉::l3 ∧
333    t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls0) 〈grid,false〉
334          (l2@〈c1,false〉::l3@〈comma,true〉::l4@〈grid,false〉::rs0))
335    ∨
336    (* non facciamo match e marchiamo la prossima tupla *)
337    (〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧
338     ∃c2,l5,l6.l4 = l5@〈bar,false〉::〈c2,false〉::l6 ∧
339     (* condizioni su l5 l6 l7 *)
340     t2 = midtape STape (〈grid,false〉::ls0) 〈c,true〉 
341           (l1@〈grid,false〉::l2@〈c1,false〉::l3@〈comma,false〉::
342            l5@〈bar,false〉::〈c2,true〉::l6@〈grid,false〉::rs0))
343    ∨  
344    (* non facciamo match e non c'è una prossima tupla:
345       non specifichiamo condizioni sul nastro di output, perché
346       non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
347    (〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧ no_bars l4 ∧ current ? t2 = Some ? 〈grid,true〉)).  
348
349 definition R_match_tuple_step_false ≝ λt1,t2.
350   ∀ls,c,rs.t1 = midtape STape ls c rs → is_grid (\fst c) = true ∧ t2 = t1.
351   
352 include alias "basics/logic.ma". 
353
354 (*
355 lemma eq_f4: ∀A1,A2,A3,A4,B.∀f:A1 → A2 →A3 →A4 →B.
356   ∀x1,x2,x3,x4,y1,y2,y3,y4. x1 = y1 → x2 = y2 →x3=y3 →x4 = y4 →   
357     f x1 x2 x3 x4 = f y1 y2 y3 y4.
358 //
359 qed-. *)
360
361 lemma some_option_hd: ∀A.∀l:list A.∀a.∃b.
362   Some ? b = option_hd ? (l@[a]) .
363 #A #l #a cases l normalize /2/
364 qed.
365
366 axiom tech_split2 : ∀A,l1,l2,l3,l4,x. 
367   memb A x l1 = false → memb ? x l3 = false → 
368   l1@x::l2 = l3@x::l4 → l1 = l3 ∧ l2 = l4.
369   
370 axiom injective_append : ∀A,l.injective … (λx.append A x l).
371
372 lemma sem_match_tuple_step: 
373     accRealize ? match_tuple_step (inr … (inl … (inr … start_nop))) 
374     R_match_tuple_step_true R_match_tuple_step_false.
375 @(acc_sem_if_app … (sem_test_char ? (λc:STape.¬ is_grid (\fst c))) …
376   (sem_seq … sem_compare
377     (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
378       (sem_nop …)
379         (sem_seq … sem_mark_next_tuple 
380            (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
381              (sem_mark ?) (sem_seq … (sem_move_l …) (sem_init_current …))))))
382   (sem_nop ?) …)
383 [(* is_grid: termination case *)
384  2:#t1 #t2 #t3 whd in ⊢ (%→?); * #Hc #H #H1 whd #ls #c #rs #Ht1 %
385   [lapply(Hc c ?) [>Ht1 %] #Hgrid @injective_notb @Hgrid |>H1 @H]
386 |#tapea #tapeout #tapeb whd in ⊢ (%→?); #Hcur
387  * #tapec * whd in ⊢ (%→?); #Hcompare #Hor 
388  cases Hcur * #c * -Hcur #Hcur #Hgrid #Htapeb cases (current_to_midtape … Hcur)
389  #ls * #rs #Htapea @(ex_intro … ls) @(ex_intro … c) @(ex_intro … rs) %
390   [%[@Htapea | cases (true_or_false (\fst c == grid))
391     [#eqc @False_ind >(\P eqc) in Hgrid; normalize #H destruct |#eqc @(\Pf eqc)]]] 
392  #ls0 #cur #l1 #l2 #c1 #l3 #l4 #rs0 #n #Hl1bitnull #Hl1marks #Hc #Hc1 #Hl3 #eqn
393  #eqlen #Htable #Hls -Hcur #Hcur #Hrs >Htapea in Htapeb; >Hls >Hcur >Hrs #Htapeb
394  cases (Hcompare … Htapeb) -Hcompare -Htapeb * #_ #_ #Hcompare
395  cases (Hcompare cur c1 l1 l3 l2 (l4@〈grid,false〉::rs0) eqlen Hl1bitnull Hl3 Hl1marks … (refl …) Hc ?)  
396  -Hcompare 
397    [* #Htemp destruct (Htemp) #Htapec %1 % % [%]
398     >Htapec in Hor; -Htapec *
399      [2: * #t3 * whd in ⊢ (%→?); * #H #_ @False_ind
400       lapply (H … (refl …)) whd in ⊢ ((??%?)→?); #H destruct (H)
401      |* #taped * whd in ⊢ (%→?); * #_ 
402       #Htaped whd in ⊢ (%→?); #Htapeout >Htapeout >Htaped
403       %
404      ]
405    |* #la * #c' * #d' * #lb * #lc * * * #H1 #H2 #H3 #Htapec 
406     cut (〈cur,false〉::l1 ≠ 〈c1,false〉::l3) 
407       [>H2 >H3 elim la
408         [@(not_to_not …H1) normalize #H destruct (H) % 
409         |#x #tl @not_to_not normalize #H destruct // 
410         ]
411       ] #Hnoteq
412     cut (bit_or_null d' = true) 
413       [cases la in H3;
414         [normalize in ⊢ (%→?); #H destruct //
415         |#x #tl #H @(Hl3 〈d',false〉)
416          normalize in H; destruct @memb_append_l2 @memb_hd
417         ] 
418       ] #Hd'
419     >Htapec in Hor; -Htapec *
420      [* #taped * whd in ⊢ (%→?); * * #c0 * normalize in ⊢ (%→?); 
421       #Hdes destruct (Hdes) >(bit_or_null_not_grid ? Hd') #Htemp destruct (Htemp)
422      |* #taped * whd in ⊢ (%→?); * #_ (* * #_ #H cases (H … (refl …)) -H #_ *)
423       #Htaped * #tapee * whd in ⊢ (%→?); #Htapee  
424       <(associative_append ? lc (〈comma,false〉::l4)) in Htaped; #Htaped
425       cases (Htapee … Htaped ???) -Htaped -Htapee 
426        [* #rs3 * * (* we proceed by cases on rs4 *) 
427          [(* rs4 is empty : the case is absurd since the tape
428             cannot end with a bar *)
429           * #d * #b * * * #Heq1 @False_ind 
430           cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut 
431           >Hcut in Htable; >H3 >associative_append
432           normalize >Heq1 <associative_append >Hcut
433           <associative_append #Htable @(absurd … Htable) 
434           @last_of_table
435          |(* rs4 not empty *)
436           * #d2 #b2 #rs3' * #d  * #b * * * #Heq1 #Hnobars
437           cut (memb STape 〈d2,b2〉 (l2@〈c1,false〉::l3@〈comma,false〉::l4) = true)
438             [@memb_append_l2
439              cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut
440              >Hcut >H3 >associative_append @memb_append_l2 
441              @memb_cons >Heq1 @memb_append_l2 @memb_cons @memb_hd] #d2intable
442           cut (is_grid d2 = false) 
443             [@(no_grids_in_table … Htable … 〈d2,b2〉 d2intable)] #Hd2
444           cut (b2 = false) 
445             [@(no_marks_in_table … Htable … 〈d2,b2〉 d2intable)] #Hb2 
446           >Hb2 in Heq1; #Heq1 -Hb2 -b2
447           whd in ⊢ ((???%)→?); #Htemp destruct (Htemp) #Htapee >Htapee -Htapee *
448            [(* we know current is not grid *)
449             * #tapef * whd in ⊢ (%→?); * * #c0 *
450             normalize in ⊢ (%→?); #Hdes destruct (Hdes) >Hd2 
451             #Htemp destruct (Htemp) 
452            |* #tapef * whd in ⊢ (%→?); * #_ #Htapef 
453             * #tapeg >Htapef -Htapef * 
454             (* move_l *)
455             whd in ⊢ (%→?); * #_ #H lapply (H … (refl …)) whd in ⊢ (???%→?); -H  #Htapeg
456             >Htapeg -Htapeg
457             (* init_current *)
458              whd in ⊢ (%→?); #Htapeout
459              cases (some_option_hd ? (reverse ? (reverse ? la)) 〈c',true〉)
460              * #c00 #b00 #Hoption
461              lapply 
462               (Htapeout (reverse ? rs3 @〈d',false〉::reverse ? la@reverse ? l2@(〈grid,false〉::reverse ? lb))
463               c' (reverse ? la) false ls0 bar (〈d2,true〉::rs3'@〈grid,false〉::rs0) c00 b00 ?????) -Htapeout
464                [whd in ⊢ (??(??%??)?); @eq_f3 [2:%|3: %]
465                 >associative_append 
466                  generalize in match (〈c',true〉::reverse ? la@〈grid,false〉::ls0); #l
467                 whd in ⊢ (???(???%)); >associative_append >associative_append % 
468                |>reverse_cons @Hoption
469                |cases la in H2; 
470                  [normalize in ⊢ (%→?); #Htemp destruct (Htemp) 
471                   @bit_or_null_not_grid @Hc
472                  |#x #tl normalize in ⊢ (%→?); #Htemp destruct (Htemp)
473                   @bit_or_null_not_grid @(Hl1bitnull 〈c',false〉) @memb_append_l2 @memb_hd
474                  ]
475                |cut (only_bits_or_nulls (la@(〈c',false〉::lb)))
476                  [<H2 whd #c0 #Hmemb cases (orb_true_l … Hmemb)
477                    [#eqc0 >(\P eqc0) @Hc |@Hl1bitnull]
478                  |#Hl1' #x #Hx @bit_or_null_not_grid @Hl1'
479                   @memb_append_l1 @daemon
480                  ]
481                |@daemon] #Htapeout % %2 % //
482             @(ex_intro … d2)
483             cut (∃rs32.rs3 = lc@〈comma,false〉::rs32) 
484                  [ (*cases (tech_split STape (λc.c == 〈bar,false〉) l4)
485                   [
486                   | * #l41 * * #cbar #bfalse * #l42 * * #Hbar #Hl4 #Hl41
487                     @(ex_intro ?? l41) >Hl4 in Heq1; #Heq1
488                 
489                 cut (sublist … lc l3)
490                   [ #x #Hx cases la in H3;
491                     [ normalize #H3 destruct (H3) @Hx
492                     | #p #la' normalize #Hla' destruct (Hla')
493                       @memb_append_l2 @memb_cons @Hx ] ] #Hsublist*)
494                 @daemon]
495                 * #rs32 #Hrs3
496                 (* cut 
497                 (〈c1,false〉::l3@〈comma,false〉::l4= la@〈d',false〉::rs3@〈bar,false〉::〈d2,b2〉::rs3')
498                 [@daemon] #Hcut *)
499                 cut (l4=rs32@〈bar,false〉::〈d2,false〉::rs3')
500                 [ >Hrs3 in Heq1; @daemon ] #Hl4
501                 @(ex_intro … rs32) @(ex_intro … rs3') % [@Hl4]
502                 >Htapeout @eq_f2
503                    [(* by Hoption, H2 *) @daemon
504                    |(*>Hrs3 *)>append_cons
505                     > (?:l1@〈grid,false〉::l2@〈c1,false〉::l3@〈comma,false〉::rs32@〈bar,false〉::〈d2,true〉::rs3'@〈grid,false〉::rs
506                         = (l1@〈grid,false〉::l2@〈c1,false〉::l3@〈comma,false〉::rs32@[〈bar,false〉])@〈d2,true〉::rs3'@〈grid,false〉::rs)
507                     [|>associative_append normalize 
508                       >associative_append normalize
509                       >associative_append normalize
510                       >associative_append normalize
511                        % ]
512                     >reverse_append >reverse_append >reverse_cons
513                     >reverse_reverse >reverse_cons >reverse_reverse
514                     >reverse_append >reverse_append >reverse_cons
515                     >reverse_reverse >reverse_reverse >reverse_reverse
516                     >(?:(la@[〈c',false〉])@((((lb@[〈grid,false〉])@l2)@la)@[〈d',false〉])@rs3
517                        =((la@〈c',false〉::lb)@([〈grid,false〉]@l2@la@[〈d',false〉]@rs3)))
518                     [|>associative_append >associative_append 
519                       >associative_append >associative_append >associative_append
520                       >associative_append % ]
521                     <H2 normalize in ⊢ (??%?); >Hrs3
522                     >associative_append >associative_append normalize
523                     >associative_append >associative_append
524                     @eq_f @eq_f @eq_f
525                     >(?:la@(〈d',false〉::lc@〈comma,false〉::rs32)@〈bar,false〉::〈d2,true〉::rs3'@〈grid,false〉::rs0 = 
526                         (la@〈d',false〉::lc)@〈comma,false〉::rs32@〈bar,false〉::〈d2,true〉::rs3'@〈grid,false〉::rs0 )
527                     [| >associative_append normalize >associative_append % ]
528                     <H3 %
529                    ]
530                  ]
531               ]
532        |* #Hnobars #Htapee >Htapee -Htapee *
533          [whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); * #_ 
534           #Htapef >Htapef -Htapef
535           whd in ⊢ (%→?); * #Htapeout #_ %2 % 
536           [% [//] whd #x #Hx @Hnobars @memb_append_l2 @memb_cons //
537           | >(Htapeout … (refl …)) % ]
538          |whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); 
539           * #Hc0 lapply(Hc0 … (refl … )) normalize in ⊢ (%→?); #Htemp destruct (Htemp) 
540          ]
541        |(* no marks in table *)
542         #x #membx @(no_marks_in_table … Htable) 
543         @memb_append_l2
544         cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut >Hcut
545         >H3 >associative_append @memb_append_l2 @memb_cons @membx
546        |(* no grids in table *)
547         #x #membx @(no_grids_in_table … Htable) 
548         @memb_append_l2
549         cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut >Hcut
550         >H3 >associative_append @memb_append_l2 @memb_cons @membx
551        |whd in ⊢ (??%?); >(bit_or_null_not_grid … Hd') >(bit_or_null_not_bar … Hd') %
552        ]
553      ]
554    |#x #membx @(no_marks_in_table … Htable) 
555     @memb_append_l2 @memb_cons @memb_append_l1 @membx 
556    |#x #membx @(no_marks_in_table … Htable) 
557     @memb_append_l1 @membx
558    |%
559    ]
560  ]
561 qed.
562
563 (* 
564   MATCH TUPLE
565
566   scrolls through the tuples in the transition table until one matching the
567   current configuration is found
568 *)
569
570 definition match_tuple ≝  whileTM ? match_tuple_step (inr … (inl … (inr … start_nop))).
571
572 lemma is_grid_true : ∀c.is_grid c = true → c = grid.
573 * normalize [ #b ] #H // destruct (H)
574 qed.
575
576 (* possible variante ? 
577 definition weakR_match_tuple ≝ λt1,t2.
578   (∀ls,cur,rs,b. t1 = midtape STape ls 〈grid,b〉 rs → t2 = t1) ∧
579   (∀c,l1,c1,l2,l3,ls0,rs0,n.
580   t1 = midtape STape (〈grid,false〉::ls0) 〈bit c,true〉 rs 
581     (l1@〈grid,false〉::l2@〈bit c1,true〉::l3@〈grid,false〉::rs0) → 
582   only_bits_or_nulls l1 → no_marks l1 → S n = |l1| →
583   table_TM (S n) (l2@〈c1,false〉::l3) → 
584   (* facciamo match *)
585   (∃l4,newc,mv,l5.
586    〈c1,false〉::l3 = l4@〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l5 ∧
587    t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls0) 〈grid,false〉
588         (l2@l4@〈c,false〉::l1@〈comma,true〉::newc@〈comma,false〉::mv::l5@
589         〈grid,false〉::rs0))
590   ∨
591   (* non facciamo match su nessuna tupla;
592      non specifichiamo condizioni sul nastro di output, perché
593      non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
594   (current ? t2 = Some ? 〈grid,true〉 ∧
595    ∀l4,newc,mv,l5.
596    〈c1,false〉::l3 ≠ l4@〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l5)).  
597 *) 
598
599 definition R_match_tuple0 ≝ λt1,t2.
600   ∀ls,cur,rs.
601   t1 = midtape STape ls cur rs → 
602   (is_grid (\fst cur) = true → t2 = t1) ∧
603   (∀c,l1,c1,l2,l3,ls0,rs0,n.
604   ls = 〈grid,false〉::ls0 → 
605   cur = 〈c,true〉 → 
606   rs = l1@〈grid,false〉::l2@〈bar,false〉::〈c1,true〉::l3@〈grid,false〉::rs0 → 
607   is_bit c = true → is_bit c1 = true → 
608   only_bits_or_nulls l1 → no_marks l1 → S n = |l1| →
609   table_TM (S n) (l2@〈bar,false〉::〈c1,false〉::l3) → 
610   (* facciamo match *)
611   (∃l4,newc,mv,l5.
612    〈bar,false〉::〈c1,false〉::l3 = l4@〈bar,false〉::〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l5 ∧
613    t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls0) 〈grid,false〉
614         (l2@l4@〈bar,false〉::〈c,false〉::l1@〈comma,true〉::newc@〈comma,false〉::mv::l5@
615         〈grid,false〉::rs0))
616   ∨
617   (* non facciamo match su nessuna tupla;
618      non specifichiamo condizioni sul nastro di output, perché
619      non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
620   (current ? t2 = Some ? 〈grid,true〉 ∧
621    ∀l4,newc,mv,l5.
622    〈bar,false〉::〈c1,false〉::l3 ≠ l4@〈bar,false〉::〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l5)).  
623
624 axiom table_bit_after_bar : 
625   ∀n,l1,c,l2.table_TM n (l1@〈bar,false〉::〈c,false〉::l2) → is_bit c = true.
626
627 lemma wsem_match_tuple : WRealize ? match_tuple R_match_tuple0.
628 #intape #k #outc #Hloop 
629 lapply (sem_while … sem_match_tuple_step intape k outc Hloop) [%] -Hloop
630 * #ta * #Hstar @(star_ind_l ??????? Hstar)
631 [ #tb whd in ⊢ (%→?); #Hleft
632   #ls #cur #rs #Htb cases (Hleft … Htb) #Hgrid #Houtc %
633   [ #_ @Houtc 
634   | #c #l1 #c1 #l2 #l3 #ls0 #rs0 #n #Hls #Hcur #Hrs 
635     >Hcur in Hgrid; #Hgrid >(is_grid_true … Hgrid) normalize in ⊢ (%→?);
636     #Hc destruct (Hc)
637   ]
638 | (* in the interesting case, we execute a true iteration, then we restart the
639      while cycle, finally we end with a false iteration *)
640   #tb #tc #td whd in ⊢ (%→?); #Htc
641   #Hstar1 #IH whd in ⊢ (%→?); #Hright lapply (IH Hright) -IH whd in ⊢ (%→?); #IH
642   #ls #cur #rs #Htb %
643   [ (* cur can't be true because we assume at least one iteration *)
644     #Hcur cases Htc #ls' * #c' * #rs' * * >Htb #Hdes destruct (Hdes) 
645     #Hfalse @False_ind @(absurd … (is_grid_true … Hcur) Hfalse)
646   | (* current and a tuple are marked *)
647    #c #l1 #c1 #l2 #l3 #ls0 #rs0 #n #Hls #Hcur #Hrs #Hc #Hc1 #Hl1bitnull #Hl1marks 
648    #Hl1len #Htable 
649    cases Htc #ls' * #c' * #rs' * * >Htb #Hdes destruct (Hdes)
650    -Htb * #_ #Htc
651    (* expose the marked tuple in table *)
652    cut (∃la,lb,mv,lc.l3 = la@〈comma,false〉::lb@〈comma,false〉::mv::lc ∧
653          S n = |la| ∧ only_bits_or_nulls la)
654    [@daemon] * #la * #lb * #mv * #lc * * #Hl3 #Hlalen #Hlabitnull
655    >Hl3 in Htable; >append_cons #Htable
656    >(?: l2@〈bar,false〉::〈c1,true〉::l3@〈grid,false〉::rs0
657       = (l2@[〈bar,false〉])@〈c1,true〉::la@〈comma,false〉::(lb@〈comma,false〉::mv::
658          lc)@〈grid,false〉::rs0) in Hrs;
659    [| >associative_append normalize >Hl3
660       >associative_append normalize % ] #Hrs
661    cases (Htc ????????? Hl1bitnull Hl1marks ?? Hlabitnull Hl1len ? Htable Hls Hcur Hrs)
662    [5: <Hl1len @Hlalen
663    |4: whd in ⊢ (??%?); >Hc1 %
664    |3: whd in ⊢ (??%?); >Hc %
665    |-Htc *
666      [ (* case 1: match successful *)
667        * #Heq #Htc % %{[]} %{lb} %{mv} %{lc} destruct (Heq) %
668        [%
669        | cases (IH … Htc) -IH #Houtc #_ >(Houtc (refl ??)) 
670          >Htc @eq_f normalize >associative_append normalize
671          >associative_append normalize %
672        ]     
673      | (* case 2: tuples don't match, we still have other tuples to try *)
674        * #Hdiff * #c2 * #l5 * #l6 * #Heqlblc #Htc
675        cases (IH ??? … Htc) -IH #_ #IH 
676        (* by induction hypothesis *)
677        lapply (IH ? l1 c2 (l2@〈bar,false〉::〈c1,false〉::la@〈comma,false〉::l5) l6 ? rs0 n (refl ??) (refl ??) ???????)
678        [ generalize in match Htable;
679          >associative_append normalize 
680          >associative_append normalize >Heqlblc
681          >associative_append normalize //
682        | @Hl1len
683        | @Hl1marks
684        | @Hl1bitnull
685        | (*???*) @daemon
686        | @Hc
687        | >associative_append normalize 
688          >associative_append normalize
689          >associative_append %
690        |-IH * 
691          [ (* the while finally matches a tuple *)
692            * #l7 * #newc * #mv0 * #l8 * #Hl7l8 #Houtc %
693            >Heqlblc @(ex_intro ?? (〈bar,false〉::〈c1,false〉::la@〈comma,false〉::l5@l7))
694            %{newc} %{mv0} %{l8} %
695            [ normalize >Hl7l8 >associative_append normalize 
696              >associative_append %
697            | >Houtc @eq_f >associative_append normalize
698              >associative_append normalize >associative_append 
699              normalize >associative_append %
700            ]
701          | (* the while fails finding a tuple: there are no matches in the whole table *)
702            * #Houtc #Hdiff1 %2 %
703            [ @Houtc
704            | #l50 #newc #mv0 #l51 >Heqlblc 
705              @daemon
706            ]
707          ]
708        ]
709      ]
710    | (* match failed and there is no next tuple: the next while cycle will just exit *)
711      * * #Hdiff #Hnobars generalize in match (refl ? tc);
712      cases tc in ⊢ (???% → %);
713      [ #_ normalize in ⊢ (??%?→?); #Hfalse destruct (Hfalse)
714      |2,3: #x #xs #_ normalize in ⊢ (??%?→?); #Hfalse destruct (Hfalse) ]
715      #ls1 #cur1 #rs1 #Htc normalize in ⊢ (??%?→?); #Hcur1
716      cases (IH … Htc) -IH #IH #_ %2 %
717      [ destruct (Hcur1) >IH [ >Htc % | % ]
718      | #l4 #newc #mv0 #l5
719        (* no_bars except the first one, where the tuple does not match ⇒ 
720           no match *)
721         @daemon
722      ]
723    ]
724  ]
725 qed.
726
727 (* termination *) 
728 lemma WF_mts_niltape:
729   WF ? (inv ? R_match_tuple_step_true) (niltape (FinProd FSUnialpha FinBool)).
730 @wf #t1 whd in ⊢ (%→?); * #ls * #c * #rs * * #H destruct 
731 qed.
732
733 lemma WF_mts_rightof:
734   ∀a,ls. WF ? (inv ? R_match_tuple_step_true) (rightof (FinProd FSUnialpha FinBool) a ls).
735 #a #ls @wf #t1 whd in ⊢ (%→?); * #ls * #c * #rs * * #H destruct 
736 qed.
737
738 lemma WF_mts_leftof:
739   ∀a,ls. WF ? (inv ? R_match_tuple_step_true) (leftof (FinProd FSUnialpha FinBool) a ls).
740 #a #ls @wf #t1 whd in ⊢ (%→?); * #ls * #c * #rs * * #H destruct 
741 qed.
742
743 lemma WF_cst_midtape_grid:
744   ∀ls,b,rs. WF ? (inv ? R_match_tuple_step_true) 
745     (midtape (FinProd … FSUnialpha FinBool) ls 〈grid,b〉 rs).
746 #ls #b #rs @wf #t1 whd in ⊢ (%→?); * #ls' * #c' * #rs' * * #H destruct
747 * #Hfalse @False_ind @Hfalse % 
748 qed.
749          
750 definition Pre_match_tuple ≝ λt.
751   ∃ls,cur,rs. t = midtape STape ls cur rs ∧
752   (is_grid (\fst cur) = true ∨
753   (∃ls0,c,l1,l2,c1,l3,l4,rs0,n.
754    only_bits_or_nulls l1 ∧ no_marks l1 ∧
755    bit_or_null c = true ∧ bit_or_null c1 = true ∧
756    only_bits_or_nulls l3 ∧ S n = |l1| ∧|l1| = |l3| ∧
757    table_TM (S n) (l2@〈c1,false〉::l3@〈comma,false〉::l4) ∧ 
758    ls = 〈grid,false〉::ls0 ∧ cur = 〈c,true〉 ∧
759    rs = l1@〈grid,false〉::l2@〈c1,true〉::l3@〈comma,false〉::l4@〈grid,false〉::rs0)). 
760
761 lemma acc_Realize_to_acc_GRealize: ∀sig,M.∀q:states sig M.∀P,R1,R2. 
762   M ⊨ [q:R1,R2] → accGRealize sig M q P R1 R2.
763 #alpha #M #q #Pre #R1 #R2 #HR #t #HPre
764 cases (HR t) -HR #k * #outc * * #Hloop #HRtrue #HRfalse 
765 @(ex_intro ?? k) @(ex_intro ?? outc) % 
766   [ % [@Hloop] @HRtrue | @HRfalse]
767 qed.
768
769
770 lemma terminate_match_tuple: 
771   ∀t. Pre_match_tuple t → Terminate ? match_tuple t.
772 #t #HPre
773 @(terminate_while_guarded ??? 
774    Pre_match_tuple … 
775    (acc_Realize_to_acc_GRealize ??? Pre_match_tuple … sem_match_tuple_step)
776    … HPre) [%] 
777   [-HPre -t #t1 #t2 #HPre cases HPre #ls * * #curl #curr * #rs * #Ht1 * 
778     [(* absurd case *)
779      #Hgrid * #ls1 * #cur1 * #rs1 * * >Ht1 #Hdes destruct (Hdes) 
780      #Habs @False_ind @(absurd ?? Habs) @(is_grid_true … Hgrid)
781     |* #ls0 * #c * #l1 * #l2 * #c1 * #l3 * #l4 * #rs0 * #n
782      * * * * * * * * * *
783      #Hl1 #Hmarksl1 #Hc #Hc1 #Hl3 #lenl1 #eqlen #Htable #Hls #Hcur #Hrs
784      * #ls1 * #cur1 * #rs1 * * >Ht1 #Hdes destruct (Hdes) #Hdes #H
785      lapply (H … Hl1 Hmarksl1 Hc Hc1 Hl3 lenl1 eqlen Htable Hls Hcur Hrs)
786      -H *
787       [* [ * #Hdes #Ht2 >Ht2 
788           @ex_intro [2:@ex_intro [2: @ex_intro [2: % [%]|]|]|]
789           %1 %
790          |* #test * #c2 * #l5 * #l6 * #Hl4 #Ht2 
791           cut (∃l7,l8. l6 = l7@〈comma,false 〉::l8 ∧ |l7| = |l1|) [@daemon]
792           * #l7 * #l8 * #Hl6 #eqlen1 
793           @ex_intro [2:@ex_intro [2: @ex_intro [2: % [@Ht2]|]|]|] %2
794           @(ex_intro … ls0) @(ex_intro … c) @(ex_intro … l1)
795           @(ex_intro … (l2@〈c1,false〉::l3@〈comma,false〉::l5@[〈bar,false〉])) 
796           @(ex_intro … c2) @(ex_intro … l7) @(ex_intro … l8) 
797           @(ex_intro … rs0) @(ex_intro … n) 
798           % [2: >Hl6 >associative_append >associative_append @eq_f @eq_f @eq_f
799            @eq_f >associative_append @eq_f @eq_f >associative_append % ]
800           % [2: %] % [2: %] % [2:@daemon] % [2: @sym_eq @eqlen1]
801           % [2: @lenl1] % [2: #x #memx @daemon] 
802           % [2: @daemon] % [2: @Hc] % [2: @Hmarksl1] @Hl1
803          ]
804      |* * #_ #_ #H cases (current_to_midtape … H) #ls * #rs #Ht1
805       >Ht1  @ex_intro [2:@ex_intro [2: @ex_intro [2: % [%]|]|]|] %1 %
806      ]
807    ]
808   |cases HPre -HPre #ls * * #curl #curr * #rs * #Ht *
809   [#Hgrid >Ht >(is_grid_true … Hgrid) @WF_cst_midtape_grid
810   |* #ls0 * #c * #l1 * #l2 * #c1 * #l3 * #l4
811    cut (∃len. |l4| = len) [/2/] * #lenl4 
812    lapply l4 lapply l3 lapply c1 lapply l2 lapply l1 lapply c lapply ls0 lapply Ht
813    lapply curr lapply curl lapply ls lapply rs lapply t -l4 -l3 -l2 -l1 -c1 -curr -curl -ls -t
814    -c -ls0 -rs
815    (* by induction on the length of l4 *)
816    @(nat_elim1 lenl4)
817    #len #Hind #t #rs #ls #cl #cr #Ht #ls0 #c #l1 #l2 #c1 #l3 #l4 #Hlen
818    * #rs0 * #n * * * * * * * * * *
819    #Hl1 #Hmarksl1 #Hc #Hc1 #Hl3 #lenl1 #eqlen #Htable #Hls #Hcur #Hrs
820    % #t1 >Ht whd in ⊢ (%→?); * #ls1 * #cur *  #rs1 * * #Hdes destruct (Hdes) 
821    #Hgrid #H lapply (H … Hl1 Hmarksl1 Hc Hc1 Hl3 lenl1 eqlen Htable Hls Hcur Hrs)
822    -H *
823     [* [ * #Hdes destruct (Hdes) #Ht1 >Ht1 @WF_cst_midtape_grid
824        | * #_ * #c2 * #l5 * #l6 * #Hl4 #Ht1 
825         cut (∃l7,l8. l6 = l7@〈comma,false 〉::l8 ∧ |l7| = |l1|) [@daemon]
826         * #l7 * #l8 * #Hl6 #eqlen1 
827         @(Hind … Ht1 ls0 c l1 (l2@〈c1,false〉::l3@〈comma,false〉::l5@[〈bar,false〉]) c2 l7 l8 … (refl …))
828          [<Hlen >Hl4 >Hl6 >length_append normalize in match (length … (cons …));
829           >length_append normalize in match (length … (cons …)); <plus_n_Sm
830           @le_S_S @daemon
831          |@(ex_intro … rs0) @(ex_intro … n) % 
832           [2: >Hl6 >associative_append >associative_append @eq_f @eq_f @eq_f
833            @eq_f >associative_append @eq_f @eq_f >associative_append % ]
834           % [2: %] % [2: %] % [2:@daemon] % [2: @sym_eq @eqlen1]
835           % [2: @lenl1] % [2: #x #memx @daemon] 
836           % [2: @daemon] % [2: @Hc] % [2: @Hmarksl1] @Hl1
837          ]
838        ]
839      |* * #_ #_ #H cases (current_to_midtape … H) #ls * #rs #Ht1
840       >Ht1 // 
841      ]
842    ]
843 qed.
844      
845 definition R_match_tuple ≝ λt1,t2.
846   ∀ls,c,l1,c1,l2,rs,n.
847   is_bit c = true → is_bit c1 = true →
848   only_bits_or_nulls l1 → no_marks l1 → S n = |l1| → 
849   table_TM (S n) (〈bar,false〉::〈c1,false〉::l2) → 
850   t1 = midtape STape (〈grid,false〉::ls) 〈c,true〉 
851          (l1@〈grid,false〉::〈bar,false〉::〈c1,true〉::l2@〈grid,false〉::rs) → 
852   (* facciamo match *)
853   (∃l3,newc,mv,l4.
854    〈bar,false〉::〈c1,false〉::l2 = l3@〈bar,false〉::〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l4 ∧
855    t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls) 〈grid,false〉
856         (l3@〈bar,false〉::〈c,false〉::l1@〈comma,true〉::newc@〈comma,false〉::mv::l4@〈grid,false〉::rs))
857   ∨
858   (* non facciamo match su nessuna tupla;
859      non specifichiamo condizioni sul nastro di output, perché
860      non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
861   (current ? t2 = Some ? 〈grid,true〉 ∧
862    ∀l3,newc,mv,l4.
863    〈bar,false〉::〈c1,false〉::l2 ≠ l3@〈bar,false〉::〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l4). 
864
865 lemma sem_match_tuple0 : GRealize ? match_tuple Pre_match_tuple R_match_tuple0.
866 @WRealize_to_GRealize [@terminate_match_tuple | @wsem_match_tuple]
867 qed.
868
869 lemma sem_match_tuple : GRealize ? match_tuple Pre_match_tuple R_match_tuple.
870 generalize in match sem_match_tuple0; @GRealize_to_GRealize
871 #t1 #t2 #HR #ls #c #l1 #c1 #l2 #rs #n #Hc #Hc1 #Hl1bitsnulls #Hl1marks #Hl1len #Htable #Ht1
872 cases (HR … Ht1) -HR #_ #HR
873 @(HR ??? [] … (refl ??) (refl ??) (refl ??) Hc Hc1 Hl1bitsnulls Hl1marks
874           Hl1len  Htable)
875 qed.