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