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