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