]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/universal/tuples.ma
Porting to the new defintion of finset
[helm.git] / matita / matita / lib / turing / universal / tuples.ma
1 (*
2     ||M||  This file is part of HELM, an Hypertextual, Electronic   
3     ||A||  Library of Mathematics, developed at the Computer Science 
4     ||T||  Department of the University of Bologna, Italy.           
5     ||I||                                                            
6     ||T||  
7     ||A||  
8     \   /  This file is distributed under the terms of the       
9      \ /   GNU General Public License Version 2   
10       V_____________________________________________________________*)
11
12
13 (* COMPARE BIT
14
15 *)
16
17 include "turing/universal/marks.ma".
18
19 definition STape ≝ FinProd … FSUnialpha FinBool.
20
21 definition only_bits ≝ λl.
22   ∀c.memb STape c l = true → is_bit (\fst c) = true.
23
24 definition only_bits_or_nulls ≝ λl.
25   ∀c.memb STape c l = true → bit_or_null (\fst c) = true.
26   
27 definition no_grids ≝ λl.
28   ∀c.memb STape c l = true → is_grid (\fst c) = false.
29
30 definition no_bars ≝ λl.
31   ∀c.memb STape c l = true → is_bar (\fst c) = false.
32
33 definition no_marks ≝ λl.
34   ∀c.memb STape c l = true → is_marked ? c = false.
35
36 lemma bit_not_grid: ∀d. is_bit d = true → is_grid d = false.
37 * // normalize #H destruct
38 qed.
39
40 lemma bit_or_null_not_grid: ∀d. bit_or_null d = true → is_grid d = false.
41 * // normalize #H destruct
42 qed.
43
44 lemma bit_not_bar: ∀d. is_bit d = true → is_bar d = false.
45 * // normalize #H destruct
46 qed.
47
48 lemma bit_or_null_not_bar: ∀d. bit_or_null d = true → is_bar d = false.
49 * // normalize #H destruct
50 qed.
51
52 definition mk_tuple ≝ λqin,cin,qout,cout,mv.
53   qin @ cin :: 〈comma,false〉:: qout @ cout :: 〈comma,false〉 :: [mv].
54
55 axiom num_of_state : ∀state:FinSet. state → list (unialpha × bool).
56
57 definition tuple_of_pair ≝ λstates: FinSet.
58   λhalt:states →bool. 
59   λp: (states × (option FinBool)) × (states × (option (FinBool × move))).
60   let 〈inp,outp〉 ≝ p in
61   let 〈q,a〉 ≝ inp in
62   let cin ≝ match a with [ None ⇒ null | Some b ⇒ bit b ] in
63   let 〈qn,action〉 ≝ outp in
64   let 〈cout,mv〉 ≝ 
65     match action with 
66     [ None ⇒ 〈null,null〉
67     | Some act ⇒ let 〈na,m〉 ≝ act in 
68       match m with 
69       [ R ⇒ 〈bit na,bit true〉
70       | L ⇒ 〈bit na,bit false〉
71       | N ⇒ 〈bit na,null〉]
72     ] in
73   let qin ≝ num_of_state states q in
74   let qout ≝ num_of_state states qn in
75   mk_tuple qin 〈cin,false〉 qout 〈cout,false〉 〈mv,false〉.
76
77
78   
79   
80
81 (* by definition, a tuple is not marked *)
82 definition tuple_TM : nat → list STape → Prop ≝ 
83  λn,t.∃qin,cin,qout,cout,mv.
84  no_marks qin ∧ no_marks qout ∧
85  only_bits qin ∧ only_bits qout ∧ 
86  bit_or_null cin = true ∧ bit_or_null cout = true ∧ bit_or_null mv = true ∧
87  (cout = null → mv = null) ∧
88  |qin| = n ∧ |qout| = n ∧
89  t = mk_tuple qin 〈cin,false〉 qout 〈cout,false〉 〈mv,false〉.
90  
91 inductive table_TM (n:nat) : list STape → Prop ≝ 
92 | ttm_nil  : table_TM n [] 
93 | ttm_cons : ∀t1,T.tuple_TM n t1 → table_TM n T → table_TM n (t1@〈bar,false〉::T).
94
95 inductive match_in_table (n:nat) (qin:list STape) (cin: STape) 
96                          (qout:list STape) (cout:STape) (mv:STape) 
97 : list STape → Prop ≝ 
98 | mit_hd : 
99    ∀tb.
100    tuple_TM n (mk_tuple qin cin qout cout mv) → 
101    match_in_table n qin cin qout cout mv 
102      (mk_tuple qin cin qout cout mv @〈bar,false〉::tb)
103 | mit_tl :
104    ∀qin0,cin0,qout0,cout0,mv0,tb.
105    tuple_TM n (mk_tuple qin0 cin0 qout0 cout0 mv0) → 
106    match_in_table n qin cin qout cout mv tb → 
107    match_in_table n qin cin qout cout mv  
108      (mk_tuple qin0 cin0 qout0 cout0 mv0@〈bar,false〉::tb).
109      
110 axiom append_l1_injective : 
111   ∀A.∀l1,l2,l3,l4:list A. |l1| = |l2| → l1@l3 = l2@l4 → l1 = l2.
112 axiom append_l2_injective : 
113   ∀A.∀l1,l2,l3,l4:list A. |l1| = |l2| → l1@l3 = l2@l4 → l3 = l4.
114 axiom cons_injective_l : ∀A.∀a1,a2:A.∀l1,l2.a1::l1 = a2::l2 → a1 = a2.
115 axiom cons_injective_r : ∀A.∀a1,a2:A.∀l1,l2.a1::l1 = a2::l2 → l1 = l2.
116 axiom tuple_len : ∀n,t.tuple_TM n t → |t| = 2*n+5.
117 axiom append_eq_tech1 :
118   ∀A,l1,l2,l3,l4,a.l1@a::l2 = l3@l4 → |l1| < |l3| → ∃la:list A.l1@a::la = l3.
119 axiom append_eq_tech2 :
120   ∀A,l1,l2,l3,l4,a.l1@a::l2 = l3@l4 → memb A a l4 = false → ∃la:list A.l3 = l1@a::la.
121 (*axiom list_decompose_cases : 
122   ∀A,l1,l2,l3,l4,a.l1@a::l2 = l3@l4 → ∃la,lb:list A.l3 = la@a::lb ∨ l4 = la@a::lb.
123 axiom list_decompose_l :
124   ∀A,l1,l2,l3,l4,a.l1@a::l2 = l3@l4 → memb A a l4 = false → 
125   ∃la,lb.l2 = la@lb ∧ l3 = l1@a::la.
126 axiom list_decompose_r :
127   ∀A,l1,l2,l3,l4,a.l1@a::l2 = l3@l4 → memb A a l3 = false → 
128   ∃la,lb.l1 = la@lb ∧ l4 = lb@a::l2.
129 axiom list_decompose_memb :
130   ∀A,l1,l2,l3,l4,a.l1@a::l2 = l3@l4 → |l1| < |l3| → memb A a l3 = true.*)
131
132 lemma table_invert_r : ∀n,t,T.
133   tuple_TM n t → table_TM n (t@〈bar,false〉::T) → table_TM n T.
134 #n #t #T #Htuple #Htable inversion Htable
135 [ cases t normalize [ #Hfalse | #p #t0 #Hfalse ] destruct (Hfalse)
136 | #t0 #T0 #Htuple0 #Htable0 #_ #Heq lapply (append_l2_injective ?????? Heq)
137   [ >(tuple_len … Htuple) >(tuple_len … Htuple0) % ]
138   -Heq #Heq destruct (Heq) // ]
139 qed.
140
141 lemma match_in_table_to_tuple :
142   ∀n,T,qin,cin,qout,cout,mv.
143   match_in_table n qin cin qout cout mv T → table_TM n T → 
144   tuple_TM n (mk_tuple qin cin qout cout mv).
145 #n #T #qin #cin #qout #cout #mv #Hmatch elim Hmatch
146 [ //
147 | #qin0 #cin0 #qout0 #cout0 #mv0 #tb #Htuple #Hmatch #IH #Htable
148   @IH @(table_invert_r ???? Htable) @Htuple
149 ]
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@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 [ #t1 #t2 <associative_append cases (t1@qin) normalize in ⊢ (%→?);
165   [ #Hfalse destruct (Hfalse) | #c0 #t0 #Hfalse whd 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 >Htuple in H1; #H1 
170   lapply (ttm_cons … T0 H1 Htable0) #Htable
171   cases t1 in HT;
172   [ >Htuple normalize in ⊢ (??%%→?);
173     >associative_append >associative_append #HT
174     cut (qin0 = qin ∧ (〈cin0,false〉 = cin ∧ (qout0 = qout ∧ 
175          (〈cout0,false〉 = cout ∧ (〈mv0,false〉 = mv ∧ 〈bar,false〉::T0 = t2)))))
176     [ lapply (append_l1_injective … HT) [ >Hlenqin @Hlenqin0 ]
177       #Hqin % [ @Hqin ] -Hqin
178       lapply (append_l2_injective … HT) [ >Hlenqin @Hlenqin0 ] -HT #HT
179       lapply (cons_injective_l ????? HT) #Hcin % [ @Hcin ] -Hcin
180       lapply (cons_injective_r ????? HT) -HT #HT 
181       lapply (cons_injective_r ????? HT) -HT
182       >associative_append >associative_append #HT
183       lapply (append_l1_injective … HT) [ >Hlenqout @Hlenqout0 ]
184       #Hqout % [ @Hqout ] -Hqout
185       lapply (append_l2_injective … HT) [ >Hlenqout @Hlenqout0 ] -HT normalize #HT
186       lapply (cons_injective_l ????? HT) #Hcout % [ @Hcout ] -Hcout
187       lapply (cons_injective_r ????? HT) -HT #HT 
188       lapply (cons_injective_r ????? HT) -HT #HT
189       lapply (cons_injective_l ????? HT) #Hmv % [ @Hmv ] -Hmv
190       @(cons_injective_r ????? HT) ]
191     -HT * #Hqin * #Hcin * #Hqout * #Hcout * #Hmv #HT0
192     >(?:qin0@(〈cin0,false〉::〈comma,false〉::qout0@[〈cout0,false〉;〈comma,false〉;〈mv0,false〉])@〈bar,false〉::T0
193         = mk_tuple qin cin qout cout mv@〈bar,false〉::T0)
194     [|>Hqin >Hqout >Hcin >Hcout >Hmv normalize >associative_append >associative_append
195        normalize >associative_append % ]
196     % %{qin0} %{cin0} %{qout0} %{cout0} %{mv0} % // % [|@Hlenqout0] % // % 
197         [ | @Hcout0mv0 ] % // % // % // % // % // % // %
198   | #c0 #cs0 #HT cut (∃cs1.c0::cs0 = tuple@〈bar,false〉::cs1)
199     [ cases (append_eq_tech1 ?????? HT ?)
200       [ -HT #ta #Hta cases (append_eq_tech2 … Hta ?)
201         [ -Hta #tb #Htb %{tb} @Htb 
202         | @daemon ]
203       | @le_S_S >length_append >(plus_n_O (|tuple|)) >commutative_plus @le_plus
204         [ @le_O_n
205         | >Htuple normalize >length_append >length_append @le_plus [ >Hlenqin >Hlenqin0 % ]
206           @le_S_S @le_S_S >length_append >length_append @le_plus [ >Hlenqout >Hlenqout0 % ] %] ] 
207     ]
208     * #cs1 #Hcs1 >Hcs1 in HT; >associative_append >associative_append #HT
209     lapply (append_l2_injective … HT) // -HT #HT
210     lapply (cons_injective_r ????? HT) -HT
211     <associative_append #HT >Htuple %2 // @(IH … HT)
212   ]
213 ]
214 qed.
215
216 (*
217 lemma table_invert_l : ∀n,T0,qin,cin,qout,cout,mv.
218   table_TM n (mk_tuple qin cin qout cout mv@〈bar,false〉::T0) → 
219   tuple_TM n (mk_tuple qin cin qout cout mv).
220 #n #T #qin #cin #qout #cout #mv #HT inversion HT
221 [ change with (append ???) in ⊢ (??(??%?)?→?);cases qin [ #Hfalse | #t0 #ts0 #Hfalse] normalize in Hfalse; destruct (Hfalse)
222 | #t0 #T0 #Ht0 #HT0 #_
223
224   
225 lemma table_invert_r : ∀n,T0,qin,cin,qout,cout,mv.
226   table n (mk_tuple qin cin qout cout mv@〈bar,false〉::T0) → table n T0. 
227 *)
228
229 lemma no_grids_in_tuple : ∀n,l.tuple_TM n l → no_grids l.
230 #n #l * #qin * #cin * #qout * #cout * #mv * * * * * * * * * *
231 #_ #_ #Hqin #Hqout #Hcin #Hcout #Hmv #_ #_ #_ #Hl >Hl
232 #c #Hc normalize in Hc; cases (memb_append … Hc) -Hc #Hc
233 [ @bit_not_grid @(Hqin … Hc)
234 | cases (orb_true_l … Hc) -Hc #Hc 
235 [ change with (c == 〈cin,false〉 = true) in Hc; >(\P Hc) @bit_or_null_not_grid //
236 | cases (orb_true_l … Hc) -Hc #Hc 
237 [ change with (c == 〈comma,false〉 = true) in Hc; >(\P Hc) %
238 | cases (memb_append …Hc) -Hc #Hc
239 [ @bit_not_grid @(Hqout … Hc)
240 | cases (orb_true_l … Hc) -Hc #Hc 
241 [ change with (c == 〈cout,false〉 = true) in Hc; >(\P Hc) @bit_or_null_not_grid //
242 | cases (orb_true_l … Hc) -Hc #Hc 
243 [ change with (c == 〈comma,false〉 = true) in Hc; >(\P Hc) %
244 | >(memb_single … Hc) @bit_or_null_not_grid @Hmv
245 ]]]]]]
246 qed.
247
248 lemma no_marks_in_tuple : ∀n,l.tuple_TM n l → no_marks l.
249 #n #l * #qin * #cin * #qout * #cout * #mv * * * * * * * * * *
250 #Hqin #Hqout #_ #_ #_ #_ #_ #_ #_ #_ #Hl >Hl
251 #c #Hc normalize in Hc; cases (memb_append … Hc) -Hc #Hc
252 [ @(Hqin … Hc)
253 | cases (orb_true_l … Hc) -Hc #Hc 
254 [ change with (c == 〈cin,false〉 = true) in Hc; >(\P Hc) %
255 | cases (orb_true_l … Hc) -Hc #Hc 
256 [ change with (c == 〈comma,false〉 = true) in Hc; >(\P Hc) %
257 | cases (memb_append … Hc) -Hc #Hc
258 [ @(Hqout … Hc)
259 | cases (orb_true_l … Hc) -Hc #Hc 
260 [ change with (c == 〈cout,false〉 = true) in Hc; >(\P Hc) %
261 | cases (orb_true_l … Hc) -Hc #Hc 
262
263 [ change with (c == 〈comma,false〉 = true) in Hc; >(\P Hc) %
264 | >(memb_single … Hc) %
265 ]]]]]]
266 qed.
267
268 lemma no_grids_in_table: ∀n.∀l.table_TM n l → no_grids l.
269 #n #l #t elim t   
270   [normalize #c #H destruct
271   |#t1 #t2 #Ht1 #Ht2 #IH lapply (no_grids_in_tuple … Ht1) -Ht1 #Ht1 #x #Hx
272    cases (memb_append … Hx) -Hx #Hx
273    [ @(Ht1 … Hx)
274    | cases (orb_true_l … Hx) -Hx #Hx
275      [ >(\P Hx) %
276      | @(IH … Hx) ] ] ]
277 qed.
278
279 lemma no_marks_in_table: ∀n.∀l.table_TM n l → no_marks l.
280 #n #l #t elim t   
281   [normalize #c #H destruct
282   |#t1 #t2 #Ht1 #Ht2 #IH lapply (no_marks_in_tuple … Ht1) -Ht1 #Ht1 #x #Hx
283    cases (memb_append … Hx) -Hx #Hx
284    [ @(Ht1 … Hx)
285    | cases (orb_true_l … Hx) -Hx #Hx
286      [ >(\P Hx) %
287      | @(IH … Hx) ] ] ]
288 qed.      
289           
290 axiom last_of_table: ∀n,l,b.¬ table_TM n (l@[〈bar,b〉]).
291    
292 (*
293 l0 x* a l1 x0* a0 l2 ------> l0 x a* l1 x0 a0* l2
294    ^                               ^
295
296 if current (* x *) = #
297    then 
298    else if x = 0
299       then move_right; ----
300            adv_to_mark_r;
301            if current (* x0 *) = 0
302               then advance_mark ----
303                    adv_to_mark_l;
304                    advance_mark
305               else STOP
306       else x = 1 (* analogo *)
307
308 *)
309
310
311 (*
312    MARK NEXT TUPLE machine
313    (partially axiomatized)
314    
315    marks the first character after the first bar (rightwards)
316  *)
317  
318 definition bar_or_grid ≝ λc:STape.is_bar (\fst c) ∨ is_grid (\fst c).
319
320 definition mark_next_tuple ≝ 
321   seq ? (adv_to_mark_r ? bar_or_grid)
322      (ifTM ? (test_char ? (λc:STape.is_bar (\fst c)))
323        (move_right_and_mark ?) (nop ?) 1).
324
325 definition R_mark_next_tuple ≝ 
326   λt1,t2.
327     ∀ls,c,rs1,rs2.
328     (* c non può essere un separatore ... speriamo *)
329     t1 = midtape STape ls c (rs1@〈grid,false〉::rs2) → 
330     no_marks rs1 → no_grids rs1 → bar_or_grid c = false → 
331     (∃rs3,rs4,d,b.rs1 = rs3 @ 〈bar,false〉 :: rs4 ∧
332       no_bars rs3 ∧
333       Some ? 〈d,b〉 = option_hd ? (rs4@〈grid,false〉::rs2) ∧
334       t2 = midtape STape (〈bar,false〉::reverse ? rs3@c::ls) 〈d,true〉 (tail ? (rs4@〈grid,false〉::rs2)))
335     ∨
336     (no_bars rs1 ∧ t2 = midtape ? (reverse ? rs1@c::ls) 〈grid,false〉 rs2).
337      
338 axiom tech_split :
339   ∀A:DeqSet.∀f,l.
340    (∀x.memb A x l = true → f x = false) ∨
341    (∃l1,c,l2.f c = true ∧ l = l1@c::l2 ∧ ∀x.memb ? x l1 = true → f x = false).
342 (*#A #f #l elim l
343 [ % #x normalize #Hfalse *)
344      
345 theorem sem_mark_next_tuple :
346   Realize ? mark_next_tuple R_mark_next_tuple.
347 #intape 
348 lapply (sem_seq ? (adv_to_mark_r ? bar_or_grid)
349          (ifTM ? (test_char ? (λc:STape.is_bar (\fst c))) (move_right_and_mark ?) (nop ?) 1) ????)
350 [@sem_if [5: // |6: @sem_move_right_and_mark |7: // |*:skip]
351 | //
352 |||#Hif cases (Hif intape) -Hif
353    #j * #outc * #Hloop * #ta * #Hleft #Hright
354    @(ex_intro ?? j) @ex_intro [|% [@Hloop] ]
355    -Hloop
356    #ls #c #rs1 #rs2 #Hrs #Hrs1 #Hrs1' #Hc
357    cases (Hleft … Hrs)
358    [ * #Hfalse >Hfalse in Hc; #Htf destruct (Htf)
359    | * #_ #Hta cases (tech_split STape (λc.is_bar (\fst c)) rs1)
360      [ #H1 lapply (Hta rs1 〈grid,false〉 rs2 (refl ??) ? ?)
361        [ * #x #b #Hx whd in ⊢ (??%?); >(Hrs1' … Hx) >(H1 … Hx) %
362        | %
363        | -Hta #Hta cases Hright
364          [ * #tb * whd in ⊢ (%→?); #Hcurrent
365            @False_ind cases (Hcurrent 〈grid,false〉 ?)
366            [ normalize #Hfalse destruct (Hfalse)
367            | >Hta % ]
368          | * #tb * whd in ⊢ (%→?); #Hcurrent
369            cases (Hcurrent 〈grid,false〉 ?)
370            [  #_ #Htb whd in ⊢ (%→?); #Houtc
371              %2 %
372              [ @H1
373              | >Houtc >Htb >Hta % ]
374            | >Hta % ]
375          ]
376        ]
377     | * #rs3 * #c0 * #rs4 * * #Hc0 #Hsplit #Hrs3
378       % @(ex_intro ?? rs3) @(ex_intro ?? rs4)
379      lapply (Hta rs3 c0 (rs4@〈grid,false〉::rs2) ???)
380      [ #x #Hrs3' whd in ⊢ (??%?); >Hsplit in Hrs1;>Hsplit in Hrs3;
381        #Hrs3 #Hrs1 >(Hrs1 …) [| @memb_append_l1 @Hrs3'|]
382        >(Hrs3 … Hrs3') @Hrs1' >Hsplit @memb_append_l1 //
383      | whd in ⊢ (??%?); >Hc0 %
384      | >Hsplit >associative_append % ] -Hta #Hta
385        cases Hright
386        [ * #tb * whd in ⊢ (%→?); #Hta'
387          whd in ⊢ (%→?); #Htb
388          cases (Hta' c0 ?)
389          [ #_ #Htb' >Htb' in Htb; #Htb
390            generalize in match Hsplit; -Hsplit
391            cases rs4 in Hta;
392            [ #Hta #Hsplit >(Htb … Hta)
393              >(?:c0 = 〈bar,false〉)
394              [ @(ex_intro ?? grid) @(ex_intro ?? false)
395                % [ % [ % 
396                [(* Hsplit *) @daemon |(*Hrs3*) @daemon ] | % ] | % ] 
397                | (* Hc0 *) @daemon ]
398            | #r5 #rs5 >(eq_pair_fst_snd … r5)
399              #Hta #Hsplit >(Htb … Hta)
400              >(?:c0 = 〈bar,false〉)
401              [ @(ex_intro ?? (\fst r5)) @(ex_intro ?? (\snd r5))
402                % [ % [ % [ (* Hc0, Hsplit *) @daemon | (*Hrs3*) @daemon ] | % ]
403                      | % ] | (* Hc0 *) @daemon ] ] | >Hta % ]
404              | * #tb * whd in ⊢ (%→?); #Hta'
405                whd in ⊢ (%→?); #Htb
406                cases (Hta' c0 ?)
407                [ #Hfalse @False_ind >Hfalse in Hc0;
408                  #Hc0 destruct (Hc0)
409                | >Hta % ]
410 ]]]]
411 qed.
412
413 definition init_current_on_match ≝ 
414   (seq ? (move_l ?)
415     (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
416       (seq ? (move_r ?) (mark ?)))).
417           
418 definition R_init_current_on_match ≝ λt1,t2.
419   ∀l1,l2,c,rs. no_grids l1 → is_grid c = false → 
420   t1 = midtape STape (l1@〈c,false〉::〈grid,false〉::l2)  〈grid,false〉 rs → 
421   t2 = midtape STape (〈grid,false〉::l2) 〈c,true〉 ((reverse ? l1)@〈grid,false〉::rs).
422
423 lemma sem_init_current_on_match : 
424   Realize ? init_current_on_match R_init_current_on_match.
425 #intape 
426 cases (sem_seq ????? (sem_move_l ?)
427         (sem_seq ????? (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
428            (sem_seq ????? (sem_move_r ?) (sem_mark ?))) intape)
429 #k * #outc * #Hloop #HR 
430 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop] -Hloop
431 #l1 #l2 #c #rs #Hl1 #Hc #Hintape
432 cases HR -HR #ta * whd in ⊢ (%→?); #Hta lapply (Hta … Hintape) -Hta -Hintape 
433 generalize in match Hl1; cases l1
434   [#Hl1 whd in ⊢ ((???(??%%%))→?); #Hta
435    * #tb * whd in ⊢ (%→?); #Htb cases (Htb … Hta) -Hta
436     [* >Hc #Htemp destruct (Htemp) ]
437    * #_ #Htc lapply (Htc [ ] 〈grid,false〉 ? (refl ??) (refl …) Hl1) 
438    whd in ⊢ ((???(??%%%))→?); -Htc #Htc
439    * #td * whd in ⊢ (%→?); #Htd lapply (Htd … Htc) -Htc -Htd 
440    whd in ⊢ ((???(??%%%))→?); #Htd
441    whd in ⊢ (%→?); #Houtc lapply (Houtc … Htd) -Houtc #Houtc
442    >Houtc % 
443   |#d #tl #Htl whd in ⊢ ((???(??%%%))→?); #Hta
444    * #tb * whd in ⊢ (%→?); #Htb cases (Htb … Hta) -Htb
445     [* >(Htl … (memb_hd …)) #Htemp destruct (Htemp)]    
446    * #Hd >append_cons #Htb lapply (Htb … (refl ??) (refl …) ?)
447     [#x #membx cases (memb_append … membx) -membx #membx
448       [@Htl @memb_cons @membx | >(memb_single … membx) @Hc]]-Htb  #Htb
449    * #tc * whd in ⊢ (%→?); #Htc lapply (Htc … Htb) -Htb -Htc 
450    >reverse_append >associative_append whd in ⊢ ((???(??%%%))→?); #Htc
451    whd in ⊢ (%→?); #Houtc lapply (Houtc … Htc) -Houtc #Houtc 
452    >Houtc >reverse_cons >associative_append % 
453   ]
454 qed.   
455
456 (*
457 definition init_current_gen ≝ 
458   seq ? (adv_to_mark_l ? (is_marked ?))
459     (seq ? (clear_mark ?)
460        (seq ? (move_l ?)
461          (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
462             (seq ? (move_r ?) (mark ?))))).
463           
464 definition R_init_current_gen ≝ λt1,t2.
465   ∀l1,c,l2,b,l3,c1,rs,c0,b0. no_marks l1 → no_grids l2 →
466   Some ? 〈c0,b0〉 = option_hd ? (reverse ? (〈c,true〉::l2)) → 
467   t1 = midtape STape (l1@〈c,true〉::l2@〈grid,b〉::l3) 〈c1,false〉 rs → 
468   t2 = midtape STape (〈grid,b〉::l3) 〈c0,true〉
469         ((tail ? (reverse ? (l1@〈c,false〉::l2))@〈c1,false〉::rs)).
470
471 lemma sem_init_current_gen : Realize ? init_current_gen R_init_current_gen.
472 #intape 
473 cases (sem_seq ????? (sem_adv_to_mark_l ? (is_marked ?))
474         (sem_seq ????? (sem_clear_mark ?)
475           (sem_seq ????? (sem_move_l ?)
476             (sem_seq ????? (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
477               (sem_seq ????? (sem_move_r ?) (sem_mark ?))))) intape)
478 #k * #outc * #Hloop #HR 
479 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop] -Hloop
480 #l1 #c #l2 #b #l3 #c1 #rs #c0 #b0 #Hl1 #Hl2 #Hc #Hintape
481 cases HR -HR #ta * whd in ⊢ (%→?); #Hta cases (Hta … Hintape) -Hta -Hintape
482   [ * #Hfalse normalize in Hfalse; destruct (Hfalse) ]
483 * #_ #Hta lapply (Hta l1 〈c,true〉 ? (refl ??) ??) [@Hl1|%] -Hta #Hta
484 * #tb * whd in ⊢ (%→?); #Htb lapply (Htb … Hta) -Htb -Hta #Htb 
485 * #tc * whd in ⊢ (%→?); #Htc lapply (Htc … Htb) -Htc -Htb 
486 generalize in match Hc; generalize in match Hl2; cases l2
487   [#_ whd in ⊢ ((???%)→?); #Htemp destruct (Htemp) 
488    whd in ⊢ ((???(??%%%))→?); #Htc
489    * #td * whd in ⊢ (%→?); #Htd cases (Htd … Htc) -Htd
490     [2: * whd in ⊢ (??%?→?); #Htemp destruct (Htemp) ]
491    * #_ #Htd >Htd in Htc; -Htd #Htd
492    * #te * whd in ⊢ (%→?); #Hte lapply (Hte … Htd) -Htd
493    >reverse_append >reverse_cons 
494    whd in ⊢ ((???(??%%%))→?); #Hte
495    whd in ⊢ (%→?); #Houtc lapply (Houtc … Hte) -Houtc -Hte #Houtc
496    >Houtc %
497   |#d #tl #Htl #Hc0 whd in ⊢ ((???(??%%%))→?); #Htc
498    * #td * whd in ⊢ (%→?); #Htd cases (Htd … Htc) -Htd
499     [* >(Htl … (memb_hd …)) whd in ⊢ (??%?→?); #Htemp destruct (Htemp)]    
500    * #Hd #Htd lapply (Htd … (refl ??) (refl ??) ?)
501     [#x #membx @Htl @memb_cons @membx] -Htd #Htd
502    * #te * whd in ⊢ (%→?); #Hte lapply (Hte … Htd) -Htd
503    >reverse_append >reverse_cons >reverse_cons
504    >reverse_cons in Hc0; >reverse_cons cases (reverse ? tl)
505      [normalize in ⊢ (%→?); #Hc0 destruct (Hc0) #Hte 
506       whd in ⊢ (%→?); #Houtc lapply (Houtc … Hte) -Houtc -Hte #Houtc
507       >Houtc %
508      |* #c2 #b2 #tl2 normalize in ⊢ (%→?); #Hc0 destruct (Hc0)  
509       whd in ⊢ ((???(??%%%))→?); #Hte 
510       whd in ⊢ (%→?); #Houtc lapply (Houtc … Hte) -Houtc -Hte #Houtc
511       >Houtc >associative_append >associative_append >associative_append %
512      ]
513    ]
514 qed.
515 *)
516
517 definition init_current ≝ 
518   seq ? (adv_to_mark_l ? (is_marked ?))
519     (seq ? (clear_mark ?)
520        (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
521           (seq ? (move_r ?) (mark ?)))).
522           
523 definition R_init_current ≝ λt1,t2.
524   ∀l1,c,l2,b,l3,c1,rs,c0,b0. no_marks l1 → no_grids l2 → is_grid c = false → 
525   Some ? 〈c0,b0〉 = option_hd ? (reverse ? (〈c,true〉::l2)) → 
526   t1 = midtape STape (l1@〈c,true〉::l2@〈grid,b〉::l3) 〈c1,false〉 rs → 
527   t2 = midtape STape (〈grid,b〉::l3) 〈c0,true〉
528         ((tail ? (reverse ? (l1@〈c,false〉::l2))@〈c1,false〉::rs)).
529
530 lemma sem_init_current : Realize ? init_current R_init_current.
531 #intape 
532 cases (sem_seq ????? (sem_adv_to_mark_l ? (is_marked ?))
533         (sem_seq ????? (sem_clear_mark ?)
534            (sem_seq ????? (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
535              (sem_seq ????? (sem_move_r ?) (sem_mark ?)))) intape)
536 #k * #outc * #Hloop #HR 
537 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop]
538 cases HR -HR #ta * whd in ⊢ (%→?); #Hta 
539 * #tb * whd in ⊢ (%→?); #Htb 
540 * #tc * whd in ⊢ (%→?); #Htc 
541 * #td * whd in ⊢ (%→%→?); #Htd #Houtc
542 #l1 #c #l2 #b #l3 #c1 #rs #c0 #b0 #Hl1 #Hl2 #Hc #Hc0 #Hintape
543 cases (Hta … Hintape) [ * #Hfalse normalize in Hfalse; destruct (Hfalse) ]
544 -Hta * #_ #Hta lapply (Hta l1 〈c,true〉 ? (refl ??) ??) [@Hl1|%]
545 -Hta #Hta lapply (Htb … Hta) -Htb #Htb cases (Htc … Htb) [ >Hc -Hc * #Hc destruct (Hc) ] 
546 -Htc * #_ #Htc lapply (Htc … (refl ??) (refl ??) ?) [@Hl2]
547 -Htc #Htc lapply (Htd … Htc) -Htd
548 >reverse_append >reverse_cons 
549 >reverse_cons in Hc0; cases (reverse … l2)
550 [ normalize in ⊢ (%→?); #Hc0 destruct (Hc0)
551   #Htd >(Houtc … Htd) %
552 | * #c2 #b2 #tl2 normalize in ⊢ (%→?);
553   #Hc0 #Htd >(Houtc … Htd)
554   whd in ⊢ (???%); destruct (Hc0)
555   >associative_append >associative_append %
556 ]
557 qed.
558
559 definition match_tuple_step ≝ 
560   ifTM ? (test_char ? (λc:STape.¬ is_grid (\fst c))) 
561    (single_finalTM ? 
562      (seq ? compare
563       (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
564         (nop ?)
565         (seq ? mark_next_tuple 
566            (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
567              (mark ?) (seq ? (move_l ?) init_current) tc_true)) tc_true)))
568     (nop ?) tc_true.
569
570 definition R_match_tuple_step_true ≝ λt1,t2.
571   ∀ls,c,l1,l2,c1,l3,l4,rs,n.
572   bit_or_null c = true → only_bits_or_nulls l1 → no_marks l1 (* → no_grids l2 *) → bit_or_null c1 = true →
573   only_bits_or_nulls l3 → n = |l1| → |l1| = |l3| →
574   table_TM (S n) (l2@〈bar,false〉::〈c1,false〉::l3@〈comma,false〉::l4) → 
575   t1 = midtape STape (〈grid,false〉::ls) 〈c,true〉 
576          (l1@〈grid,false〉::l2@〈bar,false〉::〈c1,true〉::l3@〈comma,false〉::l4@〈grid,false〉::rs) → 
577   (* facciamo match *)
578   (〈c,false〉::l1 = 〈c1,false〉::l3 ∧
579   t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls) 〈grid,false〉
580         (l2@〈bar,false〉::〈c1,false〉::l3@〈comma,true〉::l4@〈grid,false〉::rs))
581   ∨
582   (* non facciamo match e marchiamo la prossima tupla *)
583   ((〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧
584    ∃c2,l5,l6.l4 = l5@〈bar,false〉::〈c2,false〉::l6 ∧
585    (* condizioni su l5 l6 l7 *)
586    t2 = midtape STape (〈grid,false〉::ls) 〈c,true〉 
587          (l1@〈grid,false〉::l2@〈bar,false〉::〈c1,false〉::l3@〈comma,false〉::
588           l5@〈bar,false〉::〈c2,true〉::l6@〈grid,false〉::rs))
589   ∨  
590   (* non facciamo match e non c'è una prossima tupla:
591      non specifichiamo condizioni sul nastro di output, perché
592      non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
593   (〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧ no_bars l4 ∧ current ? t2 = Some ? 〈grid,true〉)).  
594   
595 definition R_match_tuple_step_false ≝ λt1,t2.
596   ∀ls,c,rs.t1 = midtape STape ls c rs → is_grid (\fst c) = true ∧ t2 = t1.
597   
598 include alias "basics/logic.ma". 
599
600 (*
601 lemma eq_f4: ∀A1,A2,A3,A4,B.∀f:A1 → A2 →A3 →A4 →B.
602   ∀x1,x2,x3,x4,y1,y2,y3,y4. x1 = y1 → x2 = y2 →x3=y3 →x4 = y4 →   
603     f x1 x2 x3 x4 = f y1 y2 y3 y4.
604 //
605 qed-. *)
606
607 lemma some_option_hd: ∀A.∀l:list A.∀a.∃b.
608   Some ? b = option_hd ? (l@[a]) .
609 #A #l #a cases l normalize /2/
610 qed.
611
612 axiom tech_split2 : ∀A,l1,l2,l3,l4,x. 
613   memb A x l1 = false → memb ? x l3 = false → 
614   l1@x::l2 = l3@x::l4 → l1 = l3 ∧ l2 = l4.
615   
616 axiom injective_append : ∀A,l.injective … (λx.append A x l).
617
618 lemma sem_match_tuple_step: 
619     accRealize ? match_tuple_step (inr … (inl … (inr … 0))) 
620     R_match_tuple_step_true R_match_tuple_step_false.
621 @(acc_sem_if_app … (sem_test_char ? (λc:STape.¬ is_grid (\fst c))) …
622   (sem_seq … sem_compare
623     (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
624       (sem_nop …)
625         (sem_seq … sem_mark_next_tuple 
626            (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
627              (sem_mark ?) (sem_seq … (sem_move_l …) (sem_init_current …))))))
628   (sem_nop ?) …)
629 [(* is_grid: termination case *)
630  2:#t1 #t2 #t3 whd in ⊢ (%→?); #H #H1 whd #ls #c #rs #Ht1
631   cases (H c ?) [2: >Ht1 %] #Hgrid #Heq %
632     [@injective_notb @Hgrid | <Heq @H1]
633 |#tapea #tapeout #tapeb whd in ⊢ (%→?); #Htapea
634  * #tapec * #Hcompare #Hor 
635  #ls #c #l1 #l2 #c1 #l3 #l4 #rs #n #Hc #Hl1bars #Hl1marks #Hc1 #Hl3 #eqn
636  #eqlen #Htable #Htapea1 cases (Htapea 〈c,true〉 ?) >Htapea1 [2:%]
637  #notgridc -Htapea -Htapea1 -tapea #Htapeb  
638  cases (Hcompare … Htapeb) -Hcompare -Htapeb * #_ #_ #Hcompare 
639  cases (Hcompare c c1 l1 l3 (l2@[〈bar,false〉]) (l4@〈grid,false〉::rs) eqlen Hl1bars Hl3 Hl1marks … (refl …) Hc ?)  
640  -Hcompare 
641    [* #Htemp destruct (Htemp) #Htapec %1 % [%]
642     >Htapec in Hor; -Htapec *
643      [2: * #t3 * whd in ⊢ (%→?); #H @False_ind
644       cases (H … (refl …)) whd in ⊢ ((??%?)→?); #H destruct (H)
645      |* #taped * whd in ⊢ (%→?); #Htaped cases (Htaped ? (refl …)) -Htaped *
646       #Htaped whd in ⊢ (%→?); #Htapeout >Htapeout >Htaped >associative_append
647       %
648      ]
649    |* #la * #c' * #d' * #lb * #lc * * * #H1 #H2 #H3 #Htapec 
650     cut (〈c,false〉::l1 ≠ 〈c1,false〉::l3) 
651       [>H2 >H3 elim la
652         [@(not_to_not …H1) normalize #H destruct % 
653         |#x #tl @not_to_not normalize #H destruct // 
654         ]
655       ] #Hnoteq %2
656     cut (bit_or_null d' = true) 
657       [cases la in H3;
658         [normalize in ⊢ (%→?); #H destruct //
659         |#x #tl #H @(Hl3 〈d',false〉)
660          normalize in H; destruct @memb_append_l2 @memb_hd
661         ] 
662       ] #Hd'
663     >Htapec in Hor; -Htapec *
664      [* #taped * whd in ⊢ (%→?); #H @False_ind
665       cases (H … (refl …)) >(bit_or_null_not_grid ? Hd') #Htemp destruct (Htemp)
666      |* #taped * whd in ⊢ (%→?); #H cases (H … (refl …)) -H #_
667       #Htaped * #tapee * whd in ⊢ (%→?); #Htapee  
668       <(associative_append ? lc (〈comma,false〉::l4)) in Htaped; #Htaped
669       cases (Htapee … Htaped ???) -Htaped -Htapee 
670        [* #rs3 * * (* we proceed by cases on rs4 *) 
671          [(* rs4 is empty : the case is absurd since the tape
672             cannot end with a bar *)
673           * #d * #b * * * #Heq1 @False_ind 
674           cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut 
675           >Hcut in Htable; >H3 >associative_append
676           normalize >Heq1 >Hcut <associative_append >Hcut 
677           <associative_append #Htable @(absurd … Htable) 
678           @last_of_table    
679          |(* rs4 not empty *)
680           * #d2 #b2 #rs3' * #d  * #b * * * #Heq1 #Hnobars
681           cut (memb STape 〈d2,b2〉 (l2@〈bar,false〉::〈c1,false〉::l3@〈comma,false〉::l4) = true)
682             [@memb_append_l2 @memb_cons
683              cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut
684              >Hcut >H3 >associative_append @memb_append_l2 
685              @memb_cons >Heq1 @memb_append_l2 @memb_cons @memb_hd] #d2intable
686           cut (is_grid d2 = false) 
687             [@(no_grids_in_table … Htable … 〈d2,b2〉 d2intable)] #Hd2
688           cut (b2 = false) 
689             [@(no_marks_in_table … Htable … 〈d2,b2〉 d2intable)] #Hb2 
690           >Hb2 in Heq1; #Heq1 -Hb2 -b2
691           whd in ⊢ ((???%)→?); #Htemp destruct (Htemp) #Htapee >Htapee -Htapee *
692            [(* we know current is not grid *)
693             * #tapef * whd in ⊢ (%→?); #Htapef 
694             cases (Htapef … (refl …)) >Hd2 #Htemp destruct (Htemp) 
695            |* #tapef * whd in ⊢ (%→?); #Htapef 
696             cases (Htapef … (refl …)) #_ -Htapef #Htapef
697             * #tapeg >Htapef -Htapef * 
698             (* move_l *)
699             whd in ⊢ (%→?); 
700             #H lapply (H … (refl …)) whd in ⊢ (???%→?); -H  #Htapeg
701             >Htapeg -Htapeg
702             (* init_current *)
703              whd in ⊢ (%→?); #Htapeout
704             %1 cases (some_option_hd ? (reverse ? (reverse ? la)) 〈c',true〉)
705             * #c00 #b00 #Hoption
706             lapply 
707              (Htapeout (reverse ? rs3 @〈d',false〉::reverse ? la@reverse ? (l2@[〈bar,false〉])@(〈grid,false〉::reverse ? lb))
708              c' (reverse ? la) false ls bar (〈d2,true〉::rs3'@〈grid,false〉::rs) c00 b00 ?????) -Htapeout
709               [whd in ⊢ (??(??%??)?); @eq_f3 [2:%|3: %]
710                >associative_append 
711                generalize in match (〈c',true〉::reverse ? la@〈grid,false〉::ls); #l
712                whd in ⊢ (???(???%)); >associative_append >associative_append % 
713               |>reverse_cons @Hoption
714               |cases la in H2; 
715                 [normalize in ⊢ (%→?); #Htemp destruct (Htemp) 
716                  @injective_notb @notgridc
717                 |#x #tl normalize in ⊢ (%→?); #Htemp destruct (Htemp)
718                  @bit_or_null_not_grid @(Hl1bars 〈c',false〉) @memb_append_l2 @memb_hd
719                 ]
720               |cut (only_bits_or_nulls (la@(〈c',false〉::lb)))
721                 [<H2 whd #c0 #Hmemb cases (orb_true_l … Hmemb)
722                   [#eqc0 >(\P eqc0) @Hc |@Hl1bars]
723                 |#Hl1' #x #Hx @bit_or_null_not_grid @Hl1'
724                  @memb_append_l1 @daemon
725                 ]
726               |@daemon
727               |>reverse_append >reverse_cons >reverse_reverse
728                >reverse_append >reverse_reverse
729                >reverse_cons >reverse_append >reverse_reverse
730                >reverse_append >reverse_cons >reverse_reverse
731                >reverse_reverse
732                #Htapeout % [@Hnoteq]
733                @(ex_intro … d2)
734                cut (∃rs32.rs3 = lc@〈comma,false〉::rs32) 
735                [ (*cases (tech_split STape (λc.c == 〈bar,false〉) l4)
736                  [
737                  | * #l41 * * #cbar #bfalse * #l42 * * #Hbar #Hl4 #Hl41
738                    @(ex_intro ?? l41) >Hl4 in Heq1; #Heq1
739                
740                cut (sublist … lc l3)
741                  [ #x #Hx cases la in H3;
742                    [ normalize #H3 destruct (H3) @Hx
743                    | #p #la' normalize #Hla' destruct (Hla')
744                      @memb_append_l2 @memb_cons @Hx ] ] #Hsublist*)
745                @daemon]
746                * #rs32 #Hrs3
747                (* cut 
748                (〈c1,false〉::l3@〈comma,false〉::l4= la@〈d',false〉::rs3@〈bar,false〉::〈d2,b2〉::rs3')
749                [@daemon] #Hcut *)
750                cut (l4=rs32@〈bar,false〉::〈d2,false〉::rs3')
751                [ >Hrs3 in Heq1; @daemon ] #Hl4
752                @(ex_intro … rs32) @(ex_intro … rs3') %
753                  [@Hl4
754                  |>Htapeout @eq_f2
755                    [@daemon
756                    |(*>Hrs3 *)>append_cons
757                     > (?:l1@〈grid,false〉::l2@〈bar,false〉::〈c1,false〉::l3@〈comma,false〉::rs32@〈bar,false〉::〈d2,true〉::rs3'@〈grid,false〉::rs
758                         = (l1@〈grid,false〉::l2@〈bar,false〉::〈c1,false〉::l3@〈comma,false〉::rs32@[〈bar,false〉])@〈d2,true〉::rs3'@〈grid,false〉::rs)
759                     [|>associative_append normalize 
760                       >associative_append normalize
761                       >associative_append normalize
762                       >associative_append normalize
763                        % ]
764                     @eq_f2 [|%]
765                     @(injective_append … (〈d2,false〉::rs3'))
766                     >(?:(la@[〈c',false〉])@((((lb@[〈grid,false〉])@l2@[〈bar,false〉])@la)@[〈d',false〉])@rs3
767                        =((la@〈c',false〉::lb)@([〈grid,false〉]@l2@[〈bar,false〉]@la@[〈d',false〉]@rs3)))
768                     [|>associative_append >associative_append 
769                       >associative_append >associative_append >associative_append
770                       >associative_append >associative_append % ]
771                     <H2 normalize (* <Hrs3 *)
772                     >associative_append >associative_append >associative_append
773                     @eq_f normalize @eq_f >associative_append
774                     >associative_append @eq_f normalize @eq_f
775                     >(append_cons ? 〈d',false〉) >associative_append
776                     <Heq1 >Hl4 <associative_append <append_cons
777                     <H3
778                     >associative_append normalize
779                     >associative_append normalize %
780                    ]
781                  ]
782               ]
783            ]
784          ]
785        |* #Hnobars #Htapee >Htapee -Htapee *
786          [whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
787           cases (Htapef … (refl …)) -Htapef #_ #Htapef >Htapef -Htapef
788           whd in ⊢ (%→?); #Htapeout %2
789           >(Htapeout … (refl …)) %
790            [ % 
791              [ @Hnoteq 
792              | whd #x #Hx @Hnobars @memb_append_l2 @memb_cons //
793              ]
794            | %
795            ] 
796          |whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
797           cases (Htapef … (refl …)) -Htapef 
798           whd in ⊢ ((??%?)→?); #Htemp destruct (Htemp) 
799          ]
800        |(* no marks in table *)
801         #x #membx @(no_marks_in_table … Htable) 
802         @memb_append_l2 @memb_cons 
803         cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut >Hcut
804         >H3 >associative_append @memb_append_l2 @memb_cons @membx
805        |(* no grids in table *)
806         #x #membx @(no_grids_in_table … Htable) 
807         @memb_append_l2 @memb_cons 
808         cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut >Hcut
809         >H3 >associative_append @memb_append_l2 @memb_cons @membx
810        |whd in ⊢ (??%?); >(bit_or_null_not_grid … Hd') >(bit_or_null_not_bar … Hd') %
811        ]
812      ]
813    |#x #membx @(no_marks_in_table … Htable) 
814     @memb_append_l2 @memb_cons @memb_cons @memb_append_l1 @membx 
815    |#x #membx @(no_marks_in_table … Htable) 
816     cases (memb_append … membx) -membx #membx
817      [@memb_append_l1 @membx | @memb_append_l2 >(memb_single … membx) @memb_hd]
818    |>associative_append %
819    ]
820  ]
821 qed.
822
823
824 (* 
825   MATCH TUPLE
826
827   scrolls through the tuples in the transition table until one matching the
828   current configuration is found
829 *)
830
831 definition match_tuple ≝  whileTM ? match_tuple_step (inr … (inl … (inr … 0))).
832
833 definition R_match_tuple ≝ λt1,t2.
834   ∀ls,c,l1,c1,l2,rs,n.
835   is_bit c = true → only_bits_or_nulls l1 → is_bit c1 = true → n = |l1| →
836   table_TM (S n) (〈c1,false〉::l2) → 
837   t1 = midtape STape (〈grid,false〉::ls) 〈c,true〉 
838          (l1@〈grid,false〉::〈c1,true〉::l2@〈grid,false〉::rs) → 
839   (* facciamo match *)
840   (∃l3,newc,mv,l4.
841    〈c1,false〉::l2 = l3@〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l4 ∧
842    t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls) 〈grid,false〉
843         (l3@〈c,false〉::l1@〈comma,true〉::newc@〈comma,false〉::mv::l4@〈grid,false〉::rs))
844   ∨
845   (* non facciamo match su nessuna tupla;
846      non specifichiamo condizioni sul nastro di output, perché
847      non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
848   (current ? t2 = Some ? 〈grid,true〉 ∧
849    ∀l3,newc,mv,l4.
850    〈c1,false〉::l2 ≠ l3@〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l4).