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