]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/universal/tuples.ma
Progress.
[helm.git] / matita / matita / lib / turing / universal / tuples.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 (* COMPARE BIT
14
15 *)
16
17 include "turing/universal/marks.ma".
18
19 definition STape ≝ FinProd … FSUnialpha FinBool.
20
21 definition only_bits ≝ λl.
22   ∀c.memb STape c l = true → is_bit (\fst c) = true.
23
24 definition only_bits_or_nulls ≝ λl.
25   ∀c.memb STape c l = true → bit_or_null (\fst c) = true.
26   
27 definition no_grids ≝ λl.
28   ∀c.memb STape c l = true → is_grid (\fst c) = false.
29
30 definition no_bars ≝ λl.
31   ∀c.memb STape c l = true → is_bar (\fst c) = false.
32
33 definition no_marks ≝ λl.
34   ∀c.memb STape c l = true → is_marked ? c = false.
35
36 lemma bit_not_grid: ∀d. is_bit d = true → is_grid d = false.
37 * // normalize #H destruct
38 qed.
39
40 lemma bit_or_null_not_grid: ∀d. bit_or_null d = true → is_grid d = false.
41 * // normalize #H destruct
42 qed.
43
44 lemma bit_not_bar: ∀d. is_bit d = true → is_bar d = false.
45 * // normalize #H destruct
46 qed.
47
48 lemma bit_or_null_not_bar: ∀d. bit_or_null d = true → is_bar d = false.
49 * // normalize #H destruct
50 qed.
51
52 definition mk_tuple ≝ λqin,cin,qout,cout,mv.
53   〈bar,false〉 :: qin @ cin :: 〈comma,false〉:: qout @ cout :: 〈comma,false〉 :: [mv].
54
55 (* by definition, a tuple is not marked *)
56 definition tuple_TM : nat → list STape → Prop ≝ 
57  λn,t.∃qin,cin,qout,cout,mv.
58  no_marks qin ∧ no_marks qout ∧
59  only_bits qin ∧ only_bits qout ∧ 
60  bit_or_null cin = true ∧ bit_or_null cout = true ∧ bit_or_null mv = true ∧
61  (cout = null → mv = null) ∧
62  |qin| = n ∧ |qout| = n ∧
63  t = mk_tuple qin 〈cin,false〉 qout 〈cout,false〉 〈mv,false〉.
64  
65 inductive table_TM (n:nat) : list STape → Prop ≝ 
66 | ttm_nil  : table_TM n [] 
67 | ttm_cons : ∀t1,T.tuple_TM n t1 → table_TM n T → table_TM n (t1@T).
68
69 inductive match_in_table (n:nat) (qin:list STape) (cin: STape) 
70                          (qout:list STape) (cout:STape) (mv:STape) 
71 : list STape → Prop ≝ 
72 | mit_hd : 
73    ∀tb.
74    tuple_TM n (mk_tuple qin cin qout cout mv) → 
75    match_in_table n qin cin qout cout mv 
76      (mk_tuple qin cin qout cout mv @tb)
77 | mit_tl :
78    ∀qin0,cin0,qout0,cout0,mv0,tb.
79    tuple_TM n (mk_tuple qin0 cin0 qout0 cout0 mv0) → 
80    match_in_table n qin cin qout cout mv tb → 
81    match_in_table n qin cin qout cout mv  
82      (mk_tuple qin0 cin0 qout0 cout0 mv0@tb).
83      
84 axiom append_l1_injective : 
85   ∀A.∀l1,l2,l3,l4:list A. |l1| = |l2| → l1@l3 = l2@l4 → l1 = l2.
86 axiom append_l2_injective : 
87   ∀A.∀l1,l2,l3,l4:list A. |l1| = |l2| → l1@l3 = l2@l4 → l3 = l4.
88 axiom append_l1_injective_r :
89   ∀A.∀l1,l2,l3,l4:list A. |l3| = |l4| → l1@l3 = l2@l4 → l1 = l2.
90 axiom append_l2_injective_r : 
91   ∀A.∀l1,l2,l3,l4:list A. |l3| = |l4| → l1@l3 = l2@l4 → l3 = l4.
92 axiom cons_injective_l : ∀A.∀a1,a2:A.∀l1,l2.a1::l1 = a2::l2 → a1 = a2.
93 axiom cons_injective_r : ∀A.∀a1,a2:A.∀l1,l2.a1::l1 = a2::l2 → l1 = l2.
94 axiom tuple_len : ∀n,t.tuple_TM n t → |t| = 2*n+6.
95 axiom append_eq_tech1 :
96   ∀A,l1,l2,l3,l4.l1@l2 = l3@l4 → |l1| < |l3| → ∃la:list A.l1@la = l3.
97 axiom append_eq_tech2 :
98   ∀A,l1,l2,l3,l4,a.l1@a::l2 = l3@l4 → memb A a l4 = false → ∃la:list A.l3 = l1@a::la.
99 (*axiom list_decompose_cases : 
100   ∀A,l1,l2,l3,l4,a.l1@a::l2 = l3@l4 → ∃la,lb:list A.l3 = la@a::lb ∨ l4 = la@a::lb.
101 axiom list_decompose_l :
102   ∀A,l1,l2,l3,l4,a.l1@a::l2 = l3@l4 → memb A a l4 = false → 
103   ∃la,lb.l2 = la@lb ∧ l3 = l1@a::la.*)
104 axiom list_decompose_r :
105   ∀A,l1,l2,l3,l4,a.l1@a::l2 = l3@l4 → memb A a l3 = false → 
106   ∃la,lb.l1 = la@lb ∧ l4 = lb@a::l2.
107 (*axiom list_decompose_memb :
108   ∀A,l1,l2,l3,l4,a.l1@a::l2 = l3@l4 → |l1| < |l3| → memb A a l3 = true.*)
109
110 lemma table_invert_r : ∀n,t,T.
111   tuple_TM n t → table_TM n (t@T) → table_TM n T.
112 #n #t #T #Htuple #Htable inversion Htable
113 [ cases Htuple #qin * #cin * #qout * #cout * #mv * #_ #Ht >Ht
114   normalize #Hfalse destruct (Hfalse)
115 | #t0 #T0 #Htuple0 #Htable0 #_ #Heq 
116   lapply (append_l2_injective ?????? Heq)
117   [ >(tuple_len … Htuple) >(tuple_len … Htuple0) % ]
118   -Heq #Heq destruct (Heq) // ]
119 qed.
120
121 lemma match_in_table_to_tuple :
122   ∀n,T,qin,cin,qout,cout,mv.
123   match_in_table n qin cin qout cout mv T → table_TM n T → 
124   tuple_TM n (mk_tuple qin cin qout cout mv).
125 #n #T #qin #cin #qout #cout #mv #Hmatch elim Hmatch
126 [ //
127 | #qin0 #cin0 #qout0 #cout0 #mv0 #tb #Htuple #Hmatch #IH #Htable
128   @IH @(table_invert_r ???? Htable) @Htuple
129 ]
130 qed.
131
132 lemma match_in_table_append :
133   ∀n,T,qin,cin,qout,cout,mv,t.
134   tuple_TM n t → 
135   match_in_table n qin cin qout cout mv (t@T) → 
136   t = mk_tuple qin cin qout cout mv ∨ match_in_table n qin cin qout cout mv T.
137 #n #T #qin #cin #qout #cout #mv #t #Ht #Hmatch inversion Hmatch
138 [ #T0 #H #H1 % >(append_l1_injective … H1) //
139   >(tuple_len … Ht) >(tuple_len … H) %
140 | #qin0 #cin0 #qout0 #cout0 #mv0 #T0 #H #H1 #_ #H2 %2
141   >(append_l2_injective … H2) // >(tuple_len … Ht) >(tuple_len … H) %
142 ]
143 qed.
144
145 lemma generic_match_to_match_in_table_tech : 
146   ∀n,t,T0,T1,T2.tuple_TM n t → table_TM n (T1@〈bar,false〉::T2) → 
147    t@T0 = T1@〈bar,false〉::T2 → T1 = [] ∨ ∃T3.T1 = t@T3.
148 #n #t #T0 #T1 #T2 #Ht cases T1
149 [ #_ #_ % %
150 | normalize #c #T1c #Htable #Heq %2
151   cases Ht in Heq; #qin * #cin * #qout * #cout * #mv **********
152   #Hqin1 #Hqout1 #Hqin2 #Hqout2 #Hcin #Hcout #Hmv #Hcoutmv #Hqinlen #Hqoutlen
153   #Heqt >Heqt whd in ⊢ (??%%→?); #Ht lapply (cons_injective_r ????? Ht)
154   #Ht' cases (list_decompose_r STape … (sym_eq … Ht') ?)
155   [ #la * #lb * #HT1c #HT0 %{lb} >HT1c @(eq_f2 ??? (append ?) (c::la)) //
156     >HT0 in Ht'; >HT1c >associative_append in ⊢ (???%→?); #Ht'
157     <(append_l1_injective_r … Ht') // <(cons_injective_l ????? Ht) %
158   |@daemon ]
159 qed.
160     
161 lemma generic_match_to_match_in_table :
162   ∀n,T.table_TM n T → 
163   ∀qin,cin,qout,cout,mv.|qin| = n → |qout| = n → 
164   only_bits qin → only_bits qout → 
165   bit_or_null (\fst cin) = true → bit_or_null (\fst cout) = true → 
166   bit_or_null (\fst mv) = true →  
167   ∀t1,t2.
168   T = (t1@〈bar,false〉::qin@cin::〈comma,false〉::qout@cout::〈comma,false〉::[mv])@t2 → 
169   match_in_table n qin cin qout cout mv T.
170 #n #T #Htable #qin #cin #qout #cout #mv #Hlenqin #Hlenqout
171 #Hqinbits #Hqoutbits #Hcin #Hcout #Hmv
172 elim Htable
173 [ * [ #t2 normalize in ⊢ (%→?); #Hfalse destruct (Hfalse)
174     | #c0 #t1 #t2 normalize in ⊢ (%→?); #Hfalse destruct (Hfalse) ]
175 | #tuple #T0 #H1 #Htable0#IH #t1 #t2 #HT cases H1 #qin0 * #cin0 * #qout0 * #cout0 * #mv0
176   * * * * * * * * * *
177   #Hqin0marks #Hqout0marks #Hqin0bits #Hqout0bits #Hcin0 #Hcout0 #Hmv0 #Hcout0mv0
178   #Hlenqin0 #Hlenqout0 #Htuple 
179   lapply (generic_match_to_match_in_table_tech n ? T0 t1 
180            (qin@cin::〈comma,false〉::qout@[cout;〈comma,false〉;mv]@t2) H1) #Htmp
181   >Htuple in H1; #H1 
182   lapply (ttm_cons … T0 H1 Htable0) <Htuple in ⊢ (%→?); >HT
183   >associative_append normalize >associative_append normalize
184   >associative_append #Htable cases (Htmp Htable ?)
185   [ #Ht1 >Htuple in HT; >Ht1 normalize in ⊢ (??%%→?);
186     >associative_append >associative_append #HT
187     cut (qin0 = qin ∧ (〈cin0,false〉 = cin ∧ (qout0 = qout ∧ 
188          (〈cout0,false〉 = cout ∧ (〈mv0,false〉 = mv ∧ T0 = t2)))))
189     [ lapply (cons_injective_r ????? HT) -HT #HT 
190       lapply (append_l1_injective … HT) [ >Hlenqin @Hlenqin0 ]
191       #Hqin % [ @Hqin ] -Hqin
192       lapply (append_l2_injective … HT) [ >Hlenqin @Hlenqin0 ] -HT #HT
193       lapply (cons_injective_l ????? HT) #Hcin % [ @Hcin ] -Hcin
194       lapply (cons_injective_r ????? HT) -HT #HT 
195       lapply (cons_injective_r ????? HT) -HT
196       >associative_append >associative_append #HT
197       lapply (append_l1_injective … HT) [ >Hlenqout @Hlenqout0 ]
198       #Hqout % [ @Hqout ] -Hqout
199       lapply (append_l2_injective … HT) [ >Hlenqout @Hlenqout0 ] -HT normalize #HT
200       lapply (cons_injective_l ????? HT) #Hcout % [ @Hcout ] -Hcout
201       lapply (cons_injective_r ????? HT) -HT #HT 
202       lapply (cons_injective_r ????? HT) -HT #HT
203       lapply (cons_injective_l ????? HT) #Hmv % [ @Hmv ] -Hmv
204       @(cons_injective_r ????? HT) ]
205     -HT * #Hqin * #Hcin * #Hqout * #Hcout * #Hmv #HT0
206     >(?:〈bar,false〉::qin0@(〈cin0,false〉::〈comma,false〉::qout0@
207         [〈cout0,false〉;〈comma,false〉;〈mv0,false〉])@T0 = tuple@T0)
208     [ >Htuple >Hqin >Hqout >Hcin >Hcout >Hmv % //
209     | >Htuple normalize >associative_append normalize >associative_append
210       normalize >associative_append % ]
211   | * #T3 #HT3 >HT3 in HT; >associative_append; >associative_append #HT
212     lapply (append_l2_injective … HT) // -HT #HT %2 //
213     @(IH T3 t2) >HT >associative_append %
214   |>HT >associative_append normalize >associative_append normalize
215    >associative_append % ]
216 ]
217 qed.
218
219 (*
220 lemma table_invert_l : ∀n,T0,qin,cin,qout,cout,mv.
221   table_TM n (mk_tuple qin cin qout cout mv@〈bar,false〉::T0) → 
222   tuple_TM n (mk_tuple qin cin qout cout mv).
223 #n #T #qin #cin #qout #cout #mv #HT inversion HT
224 [ change with (append ???) in ⊢ (??(??%?)?→?);cases qin [ #Hfalse | #t0 #ts0 #Hfalse] normalize in Hfalse; destruct (Hfalse)
225 | #t0 #T0 #Ht0 #HT0 #_
226
227   
228 lemma table_invert_r : ∀n,T0,qin,cin,qout,cout,mv.
229   table n (mk_tuple qin cin qout cout mv@〈bar,false〉::T0) → table n T0. 
230 *)
231
232 lemma no_grids_in_tuple : ∀n,l.tuple_TM n l → no_grids l.
233 #n #l * #qin * #cin * #qout * #cout * #mv * * * * * * * * * *
234 #_ #_ #Hqin #Hqout #Hcin #Hcout #Hmv #_ #_ #_ #Hl >Hl
235 #c #Hc cases (orb_true_l … Hc) -Hc #Hc
236 [ >(\P Hc) %
237 | cases (memb_append … Hc) -Hc #Hc
238 [ @bit_not_grid @(Hqin … Hc)
239 | cases (orb_true_l … Hc) -Hc #Hc 
240 [ change with (c == 〈cin,false〉 = true) in Hc; >(\P Hc) @bit_or_null_not_grid //
241 | cases (orb_true_l … Hc) -Hc #Hc 
242 [ change with (c == 〈comma,false〉 = true) in Hc; >(\P Hc) %
243 | cases (memb_append …Hc) -Hc #Hc
244 [ @bit_not_grid @(Hqout … Hc)
245 | cases (orb_true_l … Hc) -Hc #Hc 
246 [ change with (c == 〈cout,false〉 = true) in Hc; >(\P Hc) @bit_or_null_not_grid //
247 | cases (orb_true_l … Hc) -Hc #Hc 
248 [ change with (c == 〈comma,false〉 = true) in Hc; >(\P Hc) %
249 | >(memb_single … Hc) @bit_or_null_not_grid @Hmv
250 ]]]]]]
251 qed.
252
253 lemma no_marks_in_tuple : ∀n,l.tuple_TM n l → no_marks l.
254 #n #l * #qin * #cin * #qout * #cout * #mv * * * * * * * * * *
255 #Hqin #Hqout #_ #_ #_ #_ #_ #_ #_ #_ #Hl >Hl
256 #c #Hc cases (orb_true_l … Hc) -Hc #Hc
257 [ >(\P Hc) %
258 | cases (memb_append … Hc) -Hc #Hc
259 [ @(Hqin … Hc)
260 | cases (orb_true_l … Hc) -Hc #Hc 
261 [ change with (c == 〈cin,false〉 = true) in Hc; >(\P Hc) %
262 | cases (orb_true_l … Hc) -Hc #Hc 
263 [ change with (c == 〈comma,false〉 = true) in Hc; >(\P Hc) %
264 | cases (memb_append … Hc) -Hc #Hc
265 [ @(Hqout … Hc)
266 | cases (orb_true_l … Hc) -Hc #Hc 
267 [ change with (c == 〈cout,false〉 = true) in Hc; >(\P Hc) %
268 | cases (orb_true_l … Hc) -Hc #Hc 
269 [ change with (c == 〈comma,false〉 = true) in Hc; >(\P Hc) %
270 | >(memb_single … Hc) %
271 ]]]]]]
272 qed.
273
274 lemma no_grids_in_table: ∀n.∀l.table_TM n l → no_grids l.
275 #n #l #t elim t   
276   [normalize #c #H destruct
277   |#t1 #t2 #Ht1 #Ht2 #IH lapply (no_grids_in_tuple … Ht1) -Ht1 #Ht1 #x #Hx
278    cases (memb_append … Hx) -Hx #Hx
279    [ @(Ht1 … Hx)
280    | @(IH … Hx) ] ]
281 qed.
282
283 lemma no_marks_in_table: ∀n.∀l.table_TM n l → no_marks l.
284 #n #l #t elim t   
285   [normalize #c #H destruct
286   |#t1 #t2 #Ht1 #Ht2 #IH lapply (no_marks_in_tuple … Ht1) -Ht1 #Ht1 #x #Hx
287    cases (memb_append … Hx) -Hx #Hx
288    [ @(Ht1 … Hx)
289    | @(IH … Hx) ] ] 
290 qed.      
291           
292 axiom last_of_table: ∀n,l,b.¬ table_TM n (l@[〈bar,b〉]).
293    
294 (*
295 l0 x* a l1 x0* a0 l2 ------> l0 x a* l1 x0 a0* l2
296    ^                               ^
297
298 if current (* x *) = #
299    then 
300    else if x = 0
301       then move_right; ----
302            adv_to_mark_r;
303            if current (* x0 *) = 0
304               then advance_mark ----
305                    adv_to_mark_l;
306                    advance_mark
307               else STOP
308       else x = 1 (* analogo *)
309
310 *)
311
312
313 (*
314    MARK NEXT TUPLE machine
315    (partially axiomatized)
316    
317    marks the first character after the first bar (rightwards)
318  *)
319  
320 definition bar_or_grid ≝ λc:STape.is_bar (\fst c) ∨ is_grid (\fst c).
321
322 definition mark_next_tuple ≝ 
323   seq ? (adv_to_mark_r ? bar_or_grid)
324      (ifTM ? (test_char ? (λc:STape.is_bar (\fst c)))
325        (move_right_and_mark ?) (nop ?) tc_true).
326
327 definition R_mark_next_tuple ≝ 
328   λt1,t2.
329     ∀ls,c,rs1,rs2.
330     (* c non può essere un separatore ... speriamo *)
331     t1 = midtape STape ls c (rs1@〈grid,false〉::rs2) → 
332     no_marks rs1 → no_grids rs1 → bar_or_grid c = false → 
333     (∃rs3,rs4,d,b.rs1 = rs3 @ 〈bar,false〉 :: rs4 ∧
334       no_bars rs3 ∧
335       Some ? 〈d,b〉 = option_hd ? (rs4@〈grid,false〉::rs2) ∧
336       t2 = midtape STape (〈bar,false〉::reverse ? rs3@c::ls) 〈d,true〉 (tail ? (rs4@〈grid,false〉::rs2)))
337     ∨
338     (no_bars rs1 ∧ t2 = midtape ? (reverse ? rs1@c::ls) 〈grid,false〉 rs2).
339      
340 axiom tech_split :
341   ∀A:DeqSet.∀f,l.
342    (∀x.memb A x l = true → f x = false) ∨
343    (∃l1,c,l2.f c = true ∧ l = l1@c::l2 ∧ ∀x.memb ? x l1 = true → f x = false).
344 (*#A #f #l elim l
345 [ % #x normalize #Hfalse *)
346      
347 theorem sem_mark_next_tuple :
348   Realize ? mark_next_tuple R_mark_next_tuple.
349 #intape 
350 lapply (sem_seq ? (adv_to_mark_r ? bar_or_grid)
351          (ifTM ? (test_char ? (λc:STape.is_bar (\fst c))) (move_right_and_mark ?) (nop ?) tc_true) ????)
352 [@sem_if [5: // |6: @sem_move_right_and_mark |7: // |*:skip]
353 | //
354 |||#Hif cases (Hif intape) -Hif
355    #j * #outc * #Hloop * #ta * #Hleft #Hright
356    @(ex_intro ?? j) @ex_intro [|% [@Hloop] ]
357    -Hloop
358    #ls #c #rs1 #rs2 #Hrs #Hrs1 #Hrs1' #Hc
359    cases (Hleft … Hrs)
360    [ * #Hfalse >Hfalse in Hc; #Htf destruct (Htf)
361    | * #_ #Hta cases (tech_split STape (λc.is_bar (\fst c)) rs1)
362      [ #H1 lapply (Hta rs1 〈grid,false〉 rs2 (refl ??) ? ?)
363        [ * #x #b #Hx whd in ⊢ (??%?); >(Hrs1' … Hx) >(H1 … Hx) %
364        | %
365        | -Hta #Hta cases Hright
366          [ * #tb * whd in ⊢ (%→?); #Hcurrent
367            @False_ind cases (Hcurrent 〈grid,false〉 ?)
368            [ normalize in ⊢ (%→?); #Hfalse destruct (Hfalse)
369            | >Hta % ]
370          | * #tb * whd in ⊢ (%→?); #Hcurrent
371            cases (Hcurrent 〈grid,false〉 ?)
372            [  #_ #Htb whd in ⊢ (%→?); #Houtc
373              %2 %
374              [ @H1
375              | >Houtc >Htb >Hta % ]
376            | >Hta % ]
377          ]
378        ]
379     | * #rs3 * #c0 * #rs4 * * #Hc0 #Hsplit #Hrs3
380       % @(ex_intro ?? rs3) @(ex_intro ?? rs4)
381      lapply (Hta rs3 c0 (rs4@〈grid,false〉::rs2) ???)
382      [ #x #Hrs3' whd in ⊢ (??%?); >Hsplit in Hrs1;>Hsplit in Hrs3;
383        #Hrs3 #Hrs1 >(Hrs1 …) [| @memb_append_l1 @Hrs3'|]
384        >(Hrs3 … Hrs3') @Hrs1' >Hsplit @memb_append_l1 //
385      | whd in ⊢ (??%?); >Hc0 %
386      | >Hsplit >associative_append % ] -Hta #Hta
387        cases Hright
388        [ * #tb * whd in ⊢ (%→?); #Hta'
389          whd in ⊢ (%→?); #Htb
390          cases (Hta' c0 ?)
391          [ #_ #Htb' >Htb' in Htb; #Htb
392            generalize in match Hsplit; -Hsplit
393            cases rs4 in Hta;
394            [ #Hta #Hsplit >(Htb … Hta)
395              >(?:c0 = 〈bar,false〉)
396              [ @(ex_intro ?? grid) @(ex_intro ?? false)
397                % [ % [ % 
398                [(* Hsplit *) @daemon |(*Hrs3*) @daemon ] | % ] | % ] 
399                | (* Hc0 *) @daemon ]
400            | #r5 #rs5 >(eq_pair_fst_snd … r5)
401              #Hta #Hsplit >(Htb … Hta)
402              >(?:c0 = 〈bar,false〉)
403              [ @(ex_intro ?? (\fst r5)) @(ex_intro ?? (\snd r5))
404                % [ % [ % [ (* Hc0, Hsplit *) @daemon | (*Hrs3*) @daemon ] | % ]
405                      | % ] | (* Hc0 *) @daemon ] ] | >Hta % ]
406              | * #tb * whd in ⊢ (%→?); #Hta'
407                whd in ⊢ (%→?); #Htb
408                cases (Hta' c0 ?)
409                [ #Hfalse @False_ind >Hfalse in Hc0;
410                  #Hc0 destruct (Hc0)
411                | >Hta % ]
412 ]]]]
413 qed.
414
415 definition init_current_on_match ≝ 
416   (seq ? (move_l ?)
417     (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
418       (seq ? (move_r ?) (mark ?)))).
419           
420 definition R_init_current_on_match ≝ λt1,t2.
421   ∀l1,l2,c,rs. no_grids l1 → is_grid c = false → 
422   t1 = midtape STape (l1@〈c,false〉::〈grid,false〉::l2)  〈grid,false〉 rs → 
423   t2 = midtape STape (〈grid,false〉::l2) 〈c,true〉 ((reverse ? l1)@〈grid,false〉::rs).
424
425 lemma sem_init_current_on_match : 
426   Realize ? init_current_on_match R_init_current_on_match.
427 #intape 
428 cases (sem_seq ????? (sem_move_l ?)
429         (sem_seq ????? (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
430            (sem_seq ????? (sem_move_r ?) (sem_mark ?))) intape)
431 #k * #outc * #Hloop #HR 
432 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop] -Hloop
433 #l1 #l2 #c #rs #Hl1 #Hc #Hintape
434 cases HR -HR #ta * whd in ⊢ (%→?); #Hta lapply (Hta … Hintape) -Hta -Hintape 
435 generalize in match Hl1; cases l1
436   [#Hl1 whd in ⊢ ((???(??%%%))→?); #Hta
437    * #tb * whd in ⊢ (%→?); #Htb cases (Htb … Hta) -Hta
438     [* >Hc #Htemp destruct (Htemp) ]
439    * #_ #Htc lapply (Htc [ ] 〈grid,false〉 ? (refl ??) (refl …) Hl1) 
440    whd in ⊢ ((???(??%%%))→?); -Htc #Htc
441    * #td * whd in ⊢ (%→?); #Htd lapply (Htd … Htc) -Htc -Htd 
442    whd in ⊢ ((???(??%%%))→?); #Htd
443    whd in ⊢ (%→?); #Houtc lapply (Houtc … Htd) -Houtc #Houtc
444    >Houtc % 
445   |#d #tl #Htl whd in ⊢ ((???(??%%%))→?); #Hta
446    * #tb * whd in ⊢ (%→?); #Htb cases (Htb … Hta) -Htb
447     [* >(Htl … (memb_hd …)) #Htemp destruct (Htemp)]    
448    * #Hd >append_cons #Htb lapply (Htb … (refl ??) (refl …) ?)
449     [#x #membx cases (memb_append … membx) -membx #membx
450       [@Htl @memb_cons @membx | >(memb_single … membx) @Hc]]-Htb  #Htb
451    * #tc * whd in ⊢ (%→?); #Htc lapply (Htc … Htb) -Htb -Htc 
452    >reverse_append >associative_append whd in ⊢ ((???(??%%%))→?); #Htc
453    whd in ⊢ (%→?); #Houtc lapply (Houtc … Htc) -Houtc #Houtc 
454    >Houtc >reverse_cons >associative_append % 
455   ]
456 qed.   
457
458 (*
459 definition init_current_gen ≝ 
460   seq ? (adv_to_mark_l ? (is_marked ?))
461     (seq ? (clear_mark ?)
462        (seq ? (move_l ?)
463          (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
464             (seq ? (move_r ?) (mark ?))))).
465           
466 definition R_init_current_gen ≝ λt1,t2.
467   ∀l1,c,l2,b,l3,c1,rs,c0,b0. no_marks l1 → no_grids l2 →
468   Some ? 〈c0,b0〉 = option_hd ? (reverse ? (〈c,true〉::l2)) → 
469   t1 = midtape STape (l1@〈c,true〉::l2@〈grid,b〉::l3) 〈c1,false〉 rs → 
470   t2 = midtape STape (〈grid,b〉::l3) 〈c0,true〉
471         ((tail ? (reverse ? (l1@〈c,false〉::l2))@〈c1,false〉::rs)).
472
473 lemma sem_init_current_gen : Realize ? init_current_gen R_init_current_gen.
474 #intape 
475 cases (sem_seq ????? (sem_adv_to_mark_l ? (is_marked ?))
476         (sem_seq ????? (sem_clear_mark ?)
477           (sem_seq ????? (sem_move_l ?)
478             (sem_seq ????? (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
479               (sem_seq ????? (sem_move_r ?) (sem_mark ?))))) intape)
480 #k * #outc * #Hloop #HR 
481 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop] -Hloop
482 #l1 #c #l2 #b #l3 #c1 #rs #c0 #b0 #Hl1 #Hl2 #Hc #Hintape
483 cases HR -HR #ta * whd in ⊢ (%→?); #Hta cases (Hta … Hintape) -Hta -Hintape
484   [ * #Hfalse normalize in Hfalse; destruct (Hfalse) ]
485 * #_ #Hta lapply (Hta l1 〈c,true〉 ? (refl ??) ??) [@Hl1|%] -Hta #Hta
486 * #tb * whd in ⊢ (%→?); #Htb lapply (Htb … Hta) -Htb -Hta #Htb 
487 * #tc * whd in ⊢ (%→?); #Htc lapply (Htc … Htb) -Htc -Htb 
488 generalize in match Hc; generalize in match Hl2; cases l2
489   [#_ whd in ⊢ ((???%)→?); #Htemp destruct (Htemp) 
490    whd in ⊢ ((???(??%%%))→?); #Htc
491    * #td * whd in ⊢ (%→?); #Htd cases (Htd … Htc) -Htd
492     [2: * whd in ⊢ (??%?→?); #Htemp destruct (Htemp) ]
493    * #_ #Htd >Htd in Htc; -Htd #Htd
494    * #te * whd in ⊢ (%→?); #Hte lapply (Hte … Htd) -Htd
495    >reverse_append >reverse_cons 
496    whd in ⊢ ((???(??%%%))→?); #Hte
497    whd in ⊢ (%→?); #Houtc lapply (Houtc … Hte) -Houtc -Hte #Houtc
498    >Houtc %
499   |#d #tl #Htl #Hc0 whd in ⊢ ((???(??%%%))→?); #Htc
500    * #td * whd in ⊢ (%→?); #Htd cases (Htd … Htc) -Htd
501     [* >(Htl … (memb_hd …)) whd in ⊢ (??%?→?); #Htemp destruct (Htemp)]    
502    * #Hd #Htd lapply (Htd … (refl ??) (refl ??) ?)
503     [#x #membx @Htl @memb_cons @membx] -Htd #Htd
504    * #te * whd in ⊢ (%→?); #Hte lapply (Hte … Htd) -Htd
505    >reverse_append >reverse_cons >reverse_cons
506    >reverse_cons in Hc0; >reverse_cons cases (reverse ? tl)
507      [normalize in ⊢ (%→?); #Hc0 destruct (Hc0) #Hte 
508       whd in ⊢ (%→?); #Houtc lapply (Houtc … Hte) -Houtc -Hte #Houtc
509       >Houtc %
510      |* #c2 #b2 #tl2 normalize in ⊢ (%→?); #Hc0 destruct (Hc0)  
511       whd in ⊢ ((???(??%%%))→?); #Hte 
512       whd in ⊢ (%→?); #Houtc lapply (Houtc … Hte) -Houtc -Hte #Houtc
513       >Houtc >associative_append >associative_append >associative_append %
514      ]
515    ]
516 qed.
517 *)
518
519 definition init_current ≝ 
520   seq ? (adv_to_mark_l ? (is_marked ?))
521     (seq ? (clear_mark ?)
522        (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
523           (seq ? (move_r ?) (mark ?)))).
524           
525 definition R_init_current ≝ λt1,t2.
526   ∀l1,c,l2,b,l3,c1,rs,c0,b0. no_marks l1 → no_grids l2 → is_grid c = false → 
527   Some ? 〈c0,b0〉 = option_hd ? (reverse ? (〈c,true〉::l2)) → 
528   t1 = midtape STape (l1@〈c,true〉::l2@〈grid,b〉::l3) 〈c1,false〉 rs → 
529   t2 = midtape STape (〈grid,b〉::l3) 〈c0,true〉
530         ((tail ? (reverse ? (l1@〈c,false〉::l2))@〈c1,false〉::rs)).
531
532 lemma sem_init_current : Realize ? init_current R_init_current.
533 #intape 
534 cases (sem_seq ????? (sem_adv_to_mark_l ? (is_marked ?))
535         (sem_seq ????? (sem_clear_mark ?)
536            (sem_seq ????? (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
537              (sem_seq ????? (sem_move_r ?) (sem_mark ?)))) intape)
538 #k * #outc * #Hloop #HR 
539 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop]
540 cases HR -HR #ta * whd in ⊢ (%→?); #Hta 
541 * #tb * whd in ⊢ (%→?); #Htb 
542 * #tc * whd in ⊢ (%→?); #Htc 
543 * #td * whd in ⊢ (%→%→?); #Htd #Houtc
544 #l1 #c #l2 #b #l3 #c1 #rs #c0 #b0 #Hl1 #Hl2 #Hc #Hc0 #Hintape
545 cases (Hta … Hintape) [ * #Hfalse normalize in Hfalse; destruct (Hfalse) ]
546 -Hta * #_ #Hta lapply (Hta l1 〈c,true〉 ? (refl ??) ??) [@Hl1|%]
547 -Hta #Hta lapply (Htb … Hta) -Htb #Htb cases (Htc … Htb) [ >Hc -Hc * #Hc destruct (Hc) ] 
548 -Htc * #_ #Htc lapply (Htc … (refl ??) (refl ??) ?) [@Hl2]
549 -Htc #Htc lapply (Htd … Htc) -Htd
550 >reverse_append >reverse_cons 
551 >reverse_cons in Hc0; cases (reverse … l2)
552 [ normalize in ⊢ (%→?); #Hc0 destruct (Hc0)
553   #Htd >(Houtc … Htd) %
554 | * #c2 #b2 #tl2 normalize in ⊢ (%→?);
555   #Hc0 #Htd >(Houtc … Htd)
556   whd in ⊢ (???%); destruct (Hc0)
557   >associative_append >associative_append %
558 ]
559 qed.
560
561 definition match_tuple_step ≝ 
562   ifTM ? (test_char ? (λc:STape.¬ is_grid (\fst c))) 
563    (single_finalTM ? 
564      (seq ? compare
565       (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
566         (nop ?)
567         (seq ? mark_next_tuple 
568            (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
569              (mark ?) (seq ? (move_l ?) init_current) tc_true)) tc_true)))
570     (nop ?) tc_true.
571
572 definition R_match_tuple_step_true ≝ λt1,t2.
573   ∀ls,cur,rs.t1 = midtape STape ls cur rs → 
574   \fst cur ≠ grid ∧ 
575   (∀ls0,c,l1,l2,c1,l3,l4,rs0,n.
576    only_bits_or_nulls l1 → no_marks l1 (* → no_grids l2 *) → 
577    bit_or_null c = true → bit_or_null c1 = true →
578    only_bits_or_nulls l3 → S n = |l1| → |l1| = |l3| →
579    table_TM (S n) (l2@〈c1,false〉::l3@〈comma,false〉::l4) → 
580    ls = 〈grid,false〉::ls0 → cur = 〈c,true〉 → 
581    rs = l1@〈grid,false〉::l2@〈c1,true〉::l3@〈comma,false〉::l4@〈grid,false〉::rs0 → 
582    (* facciamo match *)
583    (〈c,false〉::l1 = 〈c1,false〉::l3 ∧
584    t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls0) 〈grid,false〉
585          (l2@〈c1,false〉::l3@〈comma,true〉::l4@〈grid,false〉::rs0))
586    ∨
587    (* non facciamo match e marchiamo la prossima tupla *)
588    (〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧
589     ∃c2,l5,l6.l4 = l5@〈bar,false〉::〈c2,false〉::l6 ∧
590     (* condizioni su l5 l6 l7 *)
591     t2 = midtape STape (〈grid,false〉::ls0) 〈c,true〉 
592           (l1@〈grid,false〉::l2@〈c1,false〉::l3@〈comma,false〉::
593            l5@〈bar,false〉::〈c2,true〉::l6@〈grid,false〉::rs0))
594    ∨  
595    (* non facciamo match e non c'è una prossima tupla:
596       non specifichiamo condizioni sul nastro di output, perché
597       non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
598    (〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧ no_bars l4 ∧ current ? t2 = Some ? 〈grid,true〉)).  
599   
600 definition R_match_tuple_step_false ≝ λt1,t2.
601   ∀ls,c,rs.t1 = midtape STape ls c rs → is_grid (\fst c) = true ∧ t2 = t1.
602   
603 include alias "basics/logic.ma". 
604
605 (*
606 lemma eq_f4: ∀A1,A2,A3,A4,B.∀f:A1 → A2 →A3 →A4 →B.
607   ∀x1,x2,x3,x4,y1,y2,y3,y4. x1 = y1 → x2 = y2 →x3=y3 →x4 = y4 →   
608     f x1 x2 x3 x4 = f y1 y2 y3 y4.
609 //
610 qed-. *)
611
612 lemma some_option_hd: ∀A.∀l:list A.∀a.∃b.
613   Some ? b = option_hd ? (l@[a]) .
614 #A #l #a cases l normalize /2/
615 qed.
616
617 axiom tech_split2 : ∀A,l1,l2,l3,l4,x. 
618   memb A x l1 = false → memb ? x l3 = false → 
619   l1@x::l2 = l3@x::l4 → l1 = l3 ∧ l2 = l4.
620   
621 axiom injective_append : ∀A,l.injective … (λx.append A x l).
622
623 lemma sem_match_tuple_step: 
624     accRealize ? match_tuple_step (inr … (inl … (inr … start_nop))) 
625     R_match_tuple_step_true R_match_tuple_step_false.
626 @(acc_sem_if_app … (sem_test_char ? (λc:STape.¬ is_grid (\fst c))) …
627   (sem_seq … sem_compare
628     (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
629       (sem_nop …)
630         (sem_seq … sem_mark_next_tuple 
631            (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
632              (sem_mark ?) (sem_seq … (sem_move_l …) (sem_init_current …))))))
633   (sem_nop ?) …)
634 [(* is_grid: termination case *)
635  2:#t1 #t2 #t3 whd in ⊢ (%→?); #H #H1 whd #ls #c #rs #Ht1
636   cases (H c ?) [2: >Ht1 %] #Hgrid #Heq %
637     [@injective_notb @Hgrid | <Heq @H1]
638 |#tapea #tapeout #tapeb whd in ⊢ (%→?); #Hcur
639  * #tapec * whd in ⊢ (%→?); #Hcompare #Hor 
640  #ls #cur #rs #Htapea >Htapea in Hcur; #Hcur cases (Hcur ? (refl ??)) 
641  -Hcur #Hcur #Htapeb %
642  [ % #Hfalse >Hfalse in Hcur; normalize #Hfalse1 destruct (Hfalse1)]
643  #ls0 #c #l1 #l2 #c1 #l3 #l4 #rs0 #n #Hl1bitnull #Hl1marks #Hc #Hc1 #Hl3 #eqn
644  #eqlen #Htable #Hls #Hcur #Hrs -Htapea >Hls in Htapeb; >Hcur >Hrs #Htapeb
645  cases (Hcompare … Htapeb) -Hcompare -Htapeb * #_ #_ #Hcompare
646  cases (Hcompare c c1 l1 l3 l2 (l4@〈grid,false〉::rs0) eqlen Hl1bitnull Hl3 Hl1marks … (refl …) Hc ?)  
647  -Hcompare 
648    [* #Htemp destruct (Htemp) #Htapec %1 % % [%]
649     >Htapec in Hor; -Htapec *
650      [2: * #t3 * whd in ⊢ (%→?); #H @False_ind
651       cases (H … (refl …)) whd in ⊢ ((??%?)→?); #H destruct (H)
652      |* #taped * whd in ⊢ (%→?); #Htaped cases (Htaped ? (refl …)) -Htaped *
653       #Htaped whd in ⊢ (%→?); #Htapeout >Htapeout >Htaped
654       %
655      ]
656    |* #la * #c' * #d' * #lb * #lc * * * #H1 #H2 #H3 #Htapec 
657     cut (〈c,false〉::l1 ≠ 〈c1,false〉::l3) 
658       [>H2 >H3 elim la
659         [@(not_to_not …H1) normalize #H destruct % 
660         |#x #tl @not_to_not normalize #H destruct // 
661         ]
662       ] #Hnoteq
663     cut (bit_or_null d' = true) 
664       [cases la in H3;
665         [normalize in ⊢ (%→?); #H destruct //
666         |#x #tl #H @(Hl3 〈d',false〉)
667          normalize in H; destruct @memb_append_l2 @memb_hd
668         ] 
669       ] #Hd'
670     >Htapec in Hor; -Htapec *
671      [* #taped * whd in ⊢ (%→?); #H @False_ind
672       cases (H … (refl …)) >(bit_or_null_not_grid ? Hd') #Htemp destruct (Htemp)
673      |* #taped * whd in ⊢ (%→?); #H cases (H … (refl …)) -H #_
674       #Htaped * #tapee * whd in ⊢ (%→?); #Htapee  
675       <(associative_append ? lc (〈comma,false〉::l4)) in Htaped; #Htaped
676       cases (Htapee … Htaped ???) -Htaped -Htapee 
677        [* #rs3 * * (* we proceed by cases on rs4 *) 
678          [(* rs4 is empty : the case is absurd since the tape
679             cannot end with a bar *)
680           * #d * #b * * * #Heq1 @False_ind 
681           cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut 
682           >Hcut in Htable; >H3 >associative_append
683           normalize >Heq1 <associative_append >Hcut
684           <associative_append #Htable @(absurd … Htable) 
685           @last_of_table
686          |(* rs4 not empty *)
687           * #d2 #b2 #rs3' * #d  * #b * * * #Heq1 #Hnobars
688           cut (memb STape 〈d2,b2〉 (l2@〈c1,false〉::l3@〈comma,false〉::l4) = true)
689             [@memb_append_l2
690              cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut
691              >Hcut >H3 >associative_append @memb_append_l2 
692              @memb_cons >Heq1 @memb_append_l2 @memb_cons @memb_hd] #d2intable
693           cut (is_grid d2 = false) 
694             [@(no_grids_in_table … Htable … 〈d2,b2〉 d2intable)] #Hd2
695           cut (b2 = false) 
696             [@(no_marks_in_table … Htable … 〈d2,b2〉 d2intable)] #Hb2 
697           >Hb2 in Heq1; #Heq1 -Hb2 -b2
698           whd in ⊢ ((???%)→?); #Htemp destruct (Htemp) #Htapee >Htapee -Htapee *
699            [(* we know current is not grid *)
700             * #tapef * whd in ⊢ (%→?); #Htapef 
701             cases (Htapef … (refl …)) >Hd2 #Htemp destruct (Htemp) 
702            |* #tapef * whd in ⊢ (%→?); #Htapef 
703             cases (Htapef … (refl …)) #_ -Htapef #Htapef
704             * #tapeg >Htapef -Htapef * 
705             (* move_l *)
706             whd in ⊢ (%→?); 
707             #H lapply (H … (refl …)) whd in ⊢ (???%→?); -H  #Htapeg
708             >Htapeg -Htapeg
709             (* init_current *)
710              whd in ⊢ (%→?); #Htapeout
711              cases (some_option_hd ? (reverse ? (reverse ? la)) 〈c',true〉)
712              * #c00 #b00 #Hoption
713              lapply 
714               (Htapeout (reverse ? rs3 @〈d',false〉::reverse ? la@reverse ? l2@(〈grid,false〉::reverse ? lb))
715               c' (reverse ? la) false ls0 bar (〈d2,true〉::rs3'@〈grid,false〉::rs0) c00 b00 ?????) -Htapeout
716                [whd in ⊢ (??(??%??)?); @eq_f3 [2:%|3: %]
717                 >associative_append 
718                  generalize in match (〈c',true〉::reverse ? la@〈grid,false〉::ls0); #l
719                 whd in ⊢ (???(???%)); >associative_append >associative_append % 
720                |>reverse_cons @Hoption
721                |cases la in H2; 
722                  [normalize in ⊢ (%→?); #Htemp destruct (Htemp) 
723                   @bit_or_null_not_grid @Hc
724                  |#x #tl normalize in ⊢ (%→?); #Htemp destruct (Htemp)
725                   @bit_or_null_not_grid @(Hl1bitnull 〈c',false〉) @memb_append_l2 @memb_hd
726                  ]
727                |cut (only_bits_or_nulls (la@(〈c',false〉::lb)))
728                  [<H2 whd #c0 #Hmemb cases (orb_true_l … Hmemb)
729                    [#eqc0 >(\P eqc0) @Hc |@Hl1bitnull]
730                  |#Hl1' #x #Hx @bit_or_null_not_grid @Hl1'
731                   @memb_append_l1 @daemon
732                  ]
733                |@daemon] #Htapeout % %2 % //
734             @(ex_intro … d2)
735             cut (∃rs32.rs3 = lc@〈comma,false〉::rs32) 
736                  [ (*cases (tech_split STape (λc.c == 〈bar,false〉) l4)
737                   [
738                   | * #l41 * * #cbar #bfalse * #l42 * * #Hbar #Hl4 #Hl41
739                     @(ex_intro ?? l41) >Hl4 in Heq1; #Heq1
740                 
741                 cut (sublist … lc l3)
742                   [ #x #Hx cases la in H3;
743                     [ normalize #H3 destruct (H3) @Hx
744                     | #p #la' normalize #Hla' destruct (Hla')
745                       @memb_append_l2 @memb_cons @Hx ] ] #Hsublist*)
746                 @daemon]
747                 * #rs32 #Hrs3
748                 (* cut 
749                 (〈c1,false〉::l3@〈comma,false〉::l4= la@〈d',false〉::rs3@〈bar,false〉::〈d2,b2〉::rs3')
750                 [@daemon] #Hcut *)
751                 cut (l4=rs32@〈bar,false〉::〈d2,false〉::rs3')
752                 [ >Hrs3 in Heq1; @daemon ] #Hl4
753                 @(ex_intro … rs32) @(ex_intro … rs3') % [@Hl4]
754                 >Htapeout @eq_f2
755                    [(* by Hoption, H2 *) @daemon
756                    |(*>Hrs3 *)>append_cons
757                     > (?:l1@〈grid,false〉::l2@〈c1,false〉::l3@〈comma,false〉::rs32@〈bar,false〉::〈d2,true〉::rs3'@〈grid,false〉::rs
758                         = (l1@〈grid,false〉::l2@〈c1,false〉::l3@〈comma,false〉::rs32@[〈bar,false〉])@〈d2,true〉::rs3'@〈grid,false〉::rs)
759                     [|>associative_append normalize 
760                       >associative_append normalize
761                       >associative_append normalize
762                       >associative_append normalize
763                        % ]
764                     >reverse_append >reverse_append >reverse_cons
765                     >reverse_reverse >reverse_cons >reverse_reverse
766                     >reverse_append >reverse_append >reverse_cons
767                     >reverse_reverse >reverse_reverse >reverse_reverse
768                     >(?:(la@[〈c',false〉])@((((lb@[〈grid,false〉])@l2)@la)@[〈d',false〉])@rs3
769                        =((la@〈c',false〉::lb)@([〈grid,false〉]@l2@la@[〈d',false〉]@rs3)))
770                     [|>associative_append >associative_append 
771                       >associative_append >associative_append >associative_append
772                       >associative_append % ]
773                     <H2 normalize in ⊢ (??%?); >Hrs3
774                     >associative_append >associative_append normalize
775                     >associative_append >associative_append
776                     @eq_f @eq_f @eq_f
777                     >(?:la@(〈d',false〉::lc@〈comma,false〉::rs32)@〈bar,false〉::〈d2,true〉::rs3'@〈grid,false〉::rs0 = 
778                         (la@〈d',false〉::lc)@〈comma,false〉::rs32@〈bar,false〉::〈d2,true〉::rs3'@〈grid,false〉::rs0 )
779                     [| >associative_append normalize >associative_append % ]
780                     <H3 %
781                    ]
782                  ]
783               ]
784        |* #Hnobars #Htapee >Htapee -Htapee *
785          [whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
786           cases (Htapef … (refl …)) -Htapef #_ #Htapef >Htapef -Htapef
787           whd in ⊢ (%→?); #Htapeout %2 % 
788           [% [//] whd #x #Hx @Hnobars @memb_append_l2 @memb_cons //
789           | >(Htapeout … (refl …)) % ]
790          |whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
791           cases (Htapef … (refl …)) -Htapef 
792           whd in ⊢ ((??%?)→?); #Htemp destruct (Htemp) 
793          ]
794        |(* no marks in table *)
795         #x #membx @(no_marks_in_table … Htable) 
796         @memb_append_l2
797         cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut >Hcut
798         >H3 >associative_append @memb_append_l2 @memb_cons @membx
799        |(* no grids in table *)
800         #x #membx @(no_grids_in_table … Htable) 
801         @memb_append_l2
802         cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut >Hcut
803         >H3 >associative_append @memb_append_l2 @memb_cons @membx
804        |whd in ⊢ (??%?); >(bit_or_null_not_grid … Hd') >(bit_or_null_not_bar … Hd') %
805        ]
806      ]
807    |#x #membx @(no_marks_in_table … Htable) 
808     @memb_append_l2 @memb_cons @memb_append_l1 @membx 
809    |#x #membx @(no_marks_in_table … Htable) 
810     @memb_append_l1 @membx
811    |%
812    ]
813  ]
814 qed.
815
816 (* 
817   MATCH TUPLE
818
819   scrolls through the tuples in the transition table until one matching the
820   current configuration is found
821 *)
822
823 definition match_tuple ≝  whileTM ? match_tuple_step (inr … (inl … (inr … start_nop))).
824
825 lemma is_grid_true : ∀c.is_grid c = true → c = grid.
826 * normalize [ #b ] #H // destruct (H)
827 qed.
828
829 (* possible variante ? 
830 definition weakR_match_tuple ≝ λt1,t2.
831   (∀ls,cur,rs,b. t1 = midtape STape ls 〈grid,b〉 rs → t2 = t1) ∧
832   (∀c,l1,c1,l2,l3,ls0,rs0,n.
833   t1 = midtape STape (〈grid,false〉::ls0) 〈bit c,true〉 rs 
834     (l1@〈grid,false〉::l2@〈bit c1,true〉::l3@〈grid,false〉::rs0) → 
835   only_bits_or_nulls l1 → no_marks l1 → S n = |l1| →
836   table_TM (S n) (l2@〈c1,false〉::l3) → 
837   (* facciamo match *)
838   (∃l4,newc,mv,l5.
839    〈c1,false〉::l3 = l4@〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l5 ∧
840    t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls0) 〈grid,false〉
841         (l2@l4@〈c,false〉::l1@〈comma,true〉::newc@〈comma,false〉::mv::l5@
842         〈grid,false〉::rs0))
843   ∨
844   (* non facciamo match su nessuna tupla;
845      non specifichiamo condizioni sul nastro di output, perché
846      non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
847   (current ? t2 = Some ? 〈grid,true〉 ∧
848    ∀l4,newc,mv,l5.
849    〈c1,false〉::l3 ≠ l4@〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l5)).  
850 *) 
851
852 definition R_match_tuple0 ≝ λt1,t2.
853   ∀ls,cur,rs.
854   t1 = midtape STape ls cur rs → 
855   (is_grid (\fst cur) = true → t2 = t1) ∧
856   (∀c,l1,c1,l2,l3,ls0,rs0,n.
857   ls = 〈grid,false〉::ls0 → 
858   cur = 〈c,true〉 → 
859   rs = l1@〈grid,false〉::l2@〈bar,false〉::〈c1,true〉::l3@〈grid,false〉::rs0 → 
860   is_bit c = true → is_bit c1 = true → 
861   only_bits_or_nulls l1 → no_marks l1 → S n = |l1| →
862   table_TM (S n) (l2@〈bar,false〉::〈c1,false〉::l3) → 
863   (* facciamo match *)
864   (∃l4,newc,mv,l5.
865    〈bar,false〉::〈c1,false〉::l3 = l4@〈bar,false〉::〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l5 ∧
866    t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls0) 〈grid,false〉
867         (l2@l4@〈bar,false〉::〈c,false〉::l1@〈comma,true〉::newc@〈comma,false〉::mv::l5@
868         〈grid,false〉::rs0))
869   ∨
870   (* non facciamo match su nessuna tupla;
871      non specifichiamo condizioni sul nastro di output, perché
872      non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
873   (current ? t2 = Some ? 〈grid,true〉 ∧
874    ∀l4,newc,mv,l5.
875    〈bar,false〉::〈c1,false〉::l3 ≠ l4@〈bar,false〉::〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l5)).  
876
877 axiom table_bit_after_bar : 
878   ∀n,l1,c,l2.table_TM n (l1@〈bar,false〉::〈c,false〉::l2) → is_bit c = true.
879
880 lemma wsem_match_tuple : WRealize ? match_tuple R_match_tuple0.
881 #intape #k #outc #Hloop 
882 lapply (sem_while … sem_match_tuple_step intape k outc Hloop) [%] -Hloop
883 * #ta * #Hstar @(star_ind_l ??????? Hstar)
884 [ #tb whd in ⊢ (%→?); #Hleft
885   #ls #cur #rs #Htb cases (Hleft … Htb) #Hgrid #Houtc %
886   [ #_ @Houtc 
887   | #c #l1 #c1 #l2 #l3 #ls0 #rs0 #n #Hls #Hcur #Hrs 
888     >Hcur in Hgrid; #Hgrid >(is_grid_true … Hgrid) normalize in ⊢ (%→?);
889     #Hc destruct (Hc)
890   ]
891 | (* in the interesting case, we execute a true iteration, then we restart the
892      while cycle, finally we end with a false iteration *)
893   #tb #tc #td whd in ⊢ (%→?); #Htc
894   #Hstar1 #IH whd in ⊢ (%→?); #Hright lapply (IH Hright) -IH whd in ⊢ (%→?); #IH
895   #ls #cur #rs #Htb %
896   [ (* cur can't be true because we assume at least one iteration *)
897     #Hcur cases (Htc … Htb) * #Hfalse @False_ind @Hfalse @(is_grid_true … Hcur)
898   | (* current and a tuple are marked *)
899    #c #l1 #c1 #l2 #l3 #ls0 #rs0 #n #Hls #Hcur #Hrs #Hc #Hc1 #Hl1bitnull #Hl1marks 
900    #Hl1len #Htable cases (Htc … Htb) -Htc -Htb * #_ #Htc
901    (* expose the marked tuple in table *)
902    cut (∃la,lb,mv,lc.l3 = la@〈comma,false〉::lb@〈comma,false〉::mv::lc ∧
903          S n = |la| ∧ only_bits_or_nulls la)
904    [@daemon] * #la * #lb * #mv * #lc * * #Hl3 #Hlalen #Hlabitnull
905    >Hl3 in Htable; >append_cons #Htable
906    >(?: l2@〈bar,false〉::〈c1,true〉::l3@〈grid,false〉::rs0
907       = (l2@[〈bar,false〉])@〈c1,true〉::la@〈comma,false〉::(lb@〈comma,false〉::mv::
908          lc)@〈grid,false〉::rs0) in Hrs;
909    [| >associative_append normalize >Hl3
910       >associative_append normalize % ] #Hrs
911    cases (Htc ????????? Hl1bitnull Hl1marks ?? Hlabitnull Hl1len ? Htable Hls Hcur Hrs)
912    [5: <Hl1len @Hlalen
913    |4: whd in ⊢ (??%?); >Hc1 %
914    |3: whd in ⊢ (??%?); >Hc %
915    |-Htc *
916      [ (* case 1: match successful *)
917        * #Heq #Htc % %{[]} %{lb} %{mv} %{lc} destruct (Heq) %
918        [%
919        | cases (IH … Htc) -IH #Houtc #_ >(Houtc (refl ??)) 
920          >Htc @eq_f normalize >associative_append normalize
921          >associative_append normalize %
922        ]     
923      | (* case 2: tuples don't match, we still have other tuples to try *)
924        * #Hdiff * #c2 * #l5 * #l6 * #Heqlblc #Htc
925        cases (IH ??? … Htc) -IH #_ #IH 
926        (* by induction hypothesis *)
927        lapply (IH ? l1 c2 (l2@〈bar,false〉::〈c1,false〉::la@〈comma,false〉::l5) l6 ? rs0 n (refl ??) (refl ??) ???????)
928        [ generalize in match Htable;
929          >associative_append normalize 
930          >associative_append normalize >Heqlblc
931          >associative_append normalize //
932        | @Hl1len
933        | @Hl1marks
934        | @Hl1bitnull
935        | (*???*) @daemon
936        | @Hc
937        | >associative_append normalize 
938          >associative_append normalize
939          >associative_append %
940        |-IH * 
941          [ (* the while finally matches a tuple *)
942            * #l7 * #newc * #mv0 * #l8 * #Hl7l8 #Houtc %
943            >Heqlblc @(ex_intro ?? (〈bar,false〉::〈c1,false〉::la@〈comma,false〉::l5@l7))
944            %{newc} %{mv0} %{l8} %
945            [ normalize >Hl7l8 >associative_append normalize 
946              >associative_append %
947            | >Houtc @eq_f >associative_append normalize
948              >associative_append normalize >associative_append 
949              normalize >associative_append %
950            ]
951          | (* the while fails finding a tuple: there are no matches in the whole table *)
952            * #Houtc #Hdiff1 %2 %
953            [ @Houtc
954            | #l50 #newc #mv0 #l51 >Heqlblc 
955              @daemon
956            ]
957          ]
958        ]
959      ]
960    | (* match failed and there is no next tuple: the next while cycle will just exit *)
961      * * #Hdiff #Hnobars generalize in match (refl ? tc);
962      cases tc in ⊢ (???% → %);
963      [ #_ normalize in ⊢ (??%?→?); #Hfalse destruct (Hfalse)
964      |2,3: #x #xs #_ normalize in ⊢ (??%?→?); #Hfalse destruct (Hfalse) ]
965      #ls1 #cur1 #rs1 #Htc normalize in ⊢ (??%?→?); #Hcur1
966      cases (IH … Htc) -IH #IH #_ %2 %
967      [ destruct (Hcur1) >IH [ >Htc % | % ]
968      | #l4 #newc #mv0 #l5
969        (* no_bars except the first one, where the tuple does not match ⇒ 
970           no match *)
971         @daemon
972      ]
973    ]
974  ]
975 qed.
976
977 definition R_match_tuple ≝ λt1,t2.
978   ∀ls,c,l1,c1,l2,rs,n.
979   is_bit c = true → is_bit c1 = true →
980   only_bits_or_nulls l1 → no_marks l1 → S n = |l1| → 
981   table_TM (S n) (〈bar,false〉::〈c1,false〉::l2) → 
982   t1 = midtape STape (〈grid,false〉::ls) 〈c,true〉 
983          (l1@〈grid,false〉::〈bar,false〉::〈c1,true〉::l2@〈grid,false〉::rs) → 
984   (* facciamo match *)
985   (∃l3,newc,mv,l4.
986    〈bar,false〉::〈c1,false〉::l2 = l3@〈bar,false〉::〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l4 ∧
987    t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls) 〈grid,false〉
988         (l3@〈bar,false〉::〈c,false〉::l1@〈comma,true〉::newc@〈comma,false〉::mv::l4@〈grid,false〉::rs))
989   ∨
990   (* non facciamo match su nessuna tupla;
991      non specifichiamo condizioni sul nastro di output, perché
992      non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
993   (current ? t2 = Some ? 〈grid,true〉 ∧
994    ∀l3,newc,mv,l4.
995    〈bar,false〉::〈c1,false〉::l2 ≠ l3@〈bar,false〉::〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l4). 
996
997 (* we still haven't proved termination *)
998 axiom sem_match_tuple0 : Realize ? match_tuple R_match_tuple0.
999
1000 axiom Realize_to_Realize : 
1001   ∀alpha,M,R1,R2.(∀t1,t2.R1 t1 t2 → R2 t1 t2) → Realize alpha M R1 → Realize alpha M R2.
1002
1003 lemma sem_match_tuple : Realize ? match_tuple R_match_tuple.
1004 generalize in match sem_match_tuple0; @Realize_to_Realize
1005 #t1 #t2 #HR #ls #c #l1 #c1 #l2 #rs #n #Hc #Hc1 #Hl1bitsnulls #Hl1marks #Hl1len #Htable #Ht1
1006 cases (HR … Ht1) -HR #_ #HR
1007 @(HR ??? [] … (refl ??) (refl ??) (refl ??) Hc Hc1 Hl1bitsnulls Hl1marks
1008           Hl1len  Htable)
1009 qed.