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