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