]> 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 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 ?) tc_true).
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 ?) tc_true) ????)
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 in ⊢ (%→?); #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,cur,rs.t1 = midtape STape ls cur rs → 
572   \fst cur ≠ grid ∧ 
573   (∀ls0,c,l1,l2,c1,l3,l4,rs0,n.
574    only_bits_or_nulls l1 → no_marks l1 (* → no_grids l2 *) → 
575    bit_or_null c = true → bit_or_null c1 = true →
576    only_bits_or_nulls l3 → S n = |l1| → |l1| = |l3| →
577    table_TM (S n) (l2@〈c1,false〉::l3@〈comma,false〉::l4) → 
578    ls = 〈grid,false〉::ls0 → cur = 〈c,true〉 → 
579    rs = l1@〈grid,false〉::l2@〈c1,true〉::l3@〈comma,false〉::l4@〈grid,false〉::rs0 → 
580    (* facciamo match *)
581    (〈c,false〉::l1 = 〈c1,false〉::l3 ∧
582    t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls0) 〈grid,false〉
583          (l2@〈c1,false〉::l3@〈comma,true〉::l4@〈grid,false〉::rs0))
584    ∨
585    (* non facciamo match e marchiamo la prossima tupla *)
586    (〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧
587     ∃c2,l5,l6.l4 = l5@〈bar,false〉::〈c2,false〉::l6 ∧
588     (* condizioni su l5 l6 l7 *)
589     t2 = midtape STape (〈grid,false〉::ls0) 〈c,true〉 
590           (l1@〈grid,false〉::l2@〈c1,false〉::l3@〈comma,false〉::
591            l5@〈bar,false〉::〈c2,true〉::l6@〈grid,false〉::rs0))
592    ∨  
593    (* non facciamo match e non c'è una prossima tupla:
594       non specifichiamo condizioni sul nastro di output, perché
595       non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
596    (〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧ no_bars l4 ∧ current ? t2 = Some ? 〈grid,true〉)).  
597   
598 definition R_match_tuple_step_false ≝ λt1,t2.
599   ∀ls,c,rs.t1 = midtape STape ls c rs → is_grid (\fst c) = true ∧ t2 = t1.
600   
601 include alias "basics/logic.ma". 
602
603 (*
604 lemma eq_f4: ∀A1,A2,A3,A4,B.∀f:A1 → A2 →A3 →A4 →B.
605   ∀x1,x2,x3,x4,y1,y2,y3,y4. x1 = y1 → x2 = y2 →x3=y3 →x4 = y4 →   
606     f x1 x2 x3 x4 = f y1 y2 y3 y4.
607 //
608 qed-. *)
609
610 lemma some_option_hd: ∀A.∀l:list A.∀a.∃b.
611   Some ? b = option_hd ? (l@[a]) .
612 #A #l #a cases l normalize /2/
613 qed.
614
615 axiom tech_split2 : ∀A,l1,l2,l3,l4,x. 
616   memb A x l1 = false → memb ? x l3 = false → 
617   l1@x::l2 = l3@x::l4 → l1 = l3 ∧ l2 = l4.
618   
619 axiom injective_append : ∀A,l.injective … (λx.append A x l).
620
621 lemma sem_match_tuple_step: 
622     accRealize ? match_tuple_step (inr … (inl … (inr … start_nop))) 
623     R_match_tuple_step_true R_match_tuple_step_false.
624 @(acc_sem_if_app … (sem_test_char ? (λc:STape.¬ is_grid (\fst c))) …
625   (sem_seq … sem_compare
626     (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
627       (sem_nop …)
628         (sem_seq … sem_mark_next_tuple 
629            (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
630              (sem_mark ?) (sem_seq … (sem_move_l …) (sem_init_current …))))))
631   (sem_nop ?) …)
632 [(* is_grid: termination case *)
633  2:#t1 #t2 #t3 whd in ⊢ (%→?); #H #H1 whd #ls #c #rs #Ht1
634   cases (H c ?) [2: >Ht1 %] #Hgrid #Heq %
635     [@injective_notb @Hgrid | <Heq @H1]
636 |#tapea #tapeout #tapeb whd in ⊢ (%→?); #Hcur
637  * #tapec * whd in ⊢ (%→?); #Hcompare #Hor 
638  #ls #cur #rs #Htapea >Htapea in Hcur; #Hcur cases (Hcur ? (refl ??)) 
639  -Hcur #Hcur #Htapeb %
640  [ % #Hfalse >Hfalse in Hcur; normalize #Hfalse1 destruct (Hfalse1)]
641  #ls0 #c #l1 #l2 #c1 #l3 #l4 #rs0 #n #Hl1bitnull #Hl1marks #Hc #Hc1 #Hl3 #eqn
642  #eqlen #Htable #Hls #Hcur #Hrs -Htapea >Hls in Htapeb; >Hcur >Hrs #Htapeb
643  cases (Hcompare … Htapeb) -Hcompare -Htapeb * #_ #_ #Hcompare
644  cases (Hcompare c c1 l1 l3 l2 (l4@〈grid,false〉::rs0) eqlen Hl1bitnull Hl3 Hl1marks … (refl …) Hc ?)  
645  -Hcompare 
646    [* #Htemp destruct (Htemp) #Htapec %1 % % [%]
647     >Htapec in Hor; -Htapec *
648      [2: * #t3 * whd in ⊢ (%→?); #H @False_ind
649       cases (H … (refl …)) whd in ⊢ ((??%?)→?); #H destruct (H)
650      |* #taped * whd in ⊢ (%→?); #Htaped cases (Htaped ? (refl …)) -Htaped *
651       #Htaped whd in ⊢ (%→?); #Htapeout >Htapeout >Htaped
652       %
653      ]
654    |* #la * #c' * #d' * #lb * #lc * * * #H1 #H2 #H3 #Htapec 
655     cut (〈c,false〉::l1 ≠ 〈c1,false〉::l3) 
656       [>H2 >H3 elim la
657         [@(not_to_not …H1) normalize #H destruct % 
658         |#x #tl @not_to_not normalize #H destruct // 
659         ]
660       ] #Hnoteq
661     cut (bit_or_null d' = true) 
662       [cases la in H3;
663         [normalize in ⊢ (%→?); #H destruct //
664         |#x #tl #H @(Hl3 〈d',false〉)
665          normalize in H; destruct @memb_append_l2 @memb_hd
666         ] 
667       ] #Hd'
668     >Htapec in Hor; -Htapec *
669      [* #taped * whd in ⊢ (%→?); #H @False_ind
670       cases (H … (refl …)) >(bit_or_null_not_grid ? Hd') #Htemp destruct (Htemp)
671      |* #taped * whd in ⊢ (%→?); #H cases (H … (refl …)) -H #_
672       #Htaped * #tapee * whd in ⊢ (%→?); #Htapee  
673       <(associative_append ? lc (〈comma,false〉::l4)) in Htaped; #Htaped
674       cases (Htapee … Htaped ???) -Htaped -Htapee 
675        [* #rs3 * * (* we proceed by cases on rs4 *) 
676          [(* rs4 is empty : the case is absurd since the tape
677             cannot end with a bar *)
678           * #d * #b * * * #Heq1 @False_ind 
679           cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut 
680           >Hcut in Htable; >H3 >associative_append
681           normalize >Heq1 <associative_append >Hcut
682           <associative_append #Htable @(absurd … Htable) 
683           @last_of_table
684          |(* rs4 not empty *)
685           * #d2 #b2 #rs3' * #d  * #b * * * #Heq1 #Hnobars
686           cut (memb STape 〈d2,b2〉 (l2@〈c1,false〉::l3@〈comma,false〉::l4) = true)
687             [@memb_append_l2
688              cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut
689              >Hcut >H3 >associative_append @memb_append_l2 
690              @memb_cons >Heq1 @memb_append_l2 @memb_cons @memb_hd] #d2intable
691           cut (is_grid d2 = false) 
692             [@(no_grids_in_table … Htable … 〈d2,b2〉 d2intable)] #Hd2
693           cut (b2 = false) 
694             [@(no_marks_in_table … Htable … 〈d2,b2〉 d2intable)] #Hb2 
695           >Hb2 in Heq1; #Heq1 -Hb2 -b2
696           whd in ⊢ ((???%)→?); #Htemp destruct (Htemp) #Htapee >Htapee -Htapee *
697            [(* we know current is not grid *)
698             * #tapef * whd in ⊢ (%→?); #Htapef 
699             cases (Htapef … (refl …)) >Hd2 #Htemp destruct (Htemp) 
700            |* #tapef * whd in ⊢ (%→?); #Htapef 
701             cases (Htapef … (refl …)) #_ -Htapef #Htapef
702             * #tapeg >Htapef -Htapef * 
703             (* move_l *)
704             whd in ⊢ (%→?); 
705             #H lapply (H … (refl …)) whd in ⊢ (???%→?); -H  #Htapeg
706             >Htapeg -Htapeg
707             (* init_current *)
708              whd in ⊢ (%→?); #Htapeout
709              cases (some_option_hd ? (reverse ? (reverse ? la)) 〈c',true〉)
710              * #c00 #b00 #Hoption
711              lapply 
712               (Htapeout (reverse ? rs3 @〈d',false〉::reverse ? la@reverse ? l2@(〈grid,false〉::reverse ? lb))
713               c' (reverse ? la) false ls0 bar (〈d2,true〉::rs3'@〈grid,false〉::rs0) c00 b00 ?????) -Htapeout
714                [whd in ⊢ (??(??%??)?); @eq_f3 [2:%|3: %]
715                 >associative_append 
716                  generalize in match (〈c',true〉::reverse ? la@〈grid,false〉::ls0); #l
717                 whd in ⊢ (???(???%)); >associative_append >associative_append % 
718                |>reverse_cons @Hoption
719                |cases la in H2; 
720                  [normalize in ⊢ (%→?); #Htemp destruct (Htemp) 
721                   @bit_or_null_not_grid @Hc
722                  |#x #tl normalize in ⊢ (%→?); #Htemp destruct (Htemp)
723                   @bit_or_null_not_grid @(Hl1bitnull 〈c',false〉) @memb_append_l2 @memb_hd
724                  ]
725                |cut (only_bits_or_nulls (la@(〈c',false〉::lb)))
726                  [<H2 whd #c0 #Hmemb cases (orb_true_l … Hmemb)
727                    [#eqc0 >(\P eqc0) @Hc |@Hl1bitnull]
728                  |#Hl1' #x #Hx @bit_or_null_not_grid @Hl1'
729                   @memb_append_l1 @daemon
730                  ]
731                |@daemon] #Htapeout % %2 % //
732             @(ex_intro … d2)
733             cut (∃rs32.rs3 = lc@〈comma,false〉::rs32) 
734                  [ (*cases (tech_split STape (λc.c == 〈bar,false〉) l4)
735                   [
736                   | * #l41 * * #cbar #bfalse * #l42 * * #Hbar #Hl4 #Hl41
737                     @(ex_intro ?? l41) >Hl4 in Heq1; #Heq1
738                 
739                 cut (sublist … lc l3)
740                   [ #x #Hx cases la in H3;
741                     [ normalize #H3 destruct (H3) @Hx
742                     | #p #la' normalize #Hla' destruct (Hla')
743                       @memb_append_l2 @memb_cons @Hx ] ] #Hsublist*)
744                 @daemon]
745                 * #rs32 #Hrs3
746                 (* cut 
747                 (〈c1,false〉::l3@〈comma,false〉::l4= la@〈d',false〉::rs3@〈bar,false〉::〈d2,b2〉::rs3')
748                 [@daemon] #Hcut *)
749                 cut (l4=rs32@〈bar,false〉::〈d2,false〉::rs3')
750                 [ >Hrs3 in Heq1; @daemon ] #Hl4
751                 @(ex_intro … rs32) @(ex_intro … rs3') % [@Hl4]
752                 >Htapeout @eq_f2
753                    [@daemon
754                    |(*>Hrs3 *)>append_cons
755                     > (?:l1@〈grid,false〉::l2@〈c1,false〉::l3@〈comma,false〉::rs32@〈bar,false〉::〈d2,true〉::rs3'@〈grid,false〉::rs
756                         = (l1@〈grid,false〉::l2@〈c1,false〉::l3@〈comma,false〉::rs32@[〈bar,false〉])@〈d2,true〉::rs3'@〈grid,false〉::rs)
757                     [|>associative_append normalize 
758                       >associative_append normalize
759                       >associative_append normalize
760                       >associative_append normalize
761                        % ]
762                     >reverse_append >reverse_append >reverse_cons
763                     >reverse_reverse >reverse_cons >reverse_reverse
764                     >reverse_append >reverse_append >reverse_cons
765                     >reverse_reverse >reverse_reverse >reverse_reverse
766                     >(?:(la@[〈c',false〉])@((((lb@[〈grid,false〉])@l2)@la)@[〈d',false〉])@rs3
767                        =((la@〈c',false〉::lb)@([〈grid,false〉]@l2@la@[〈d',false〉]@rs3)))
768                     [|>associative_append >associative_append 
769                       >associative_append >associative_append >associative_append
770                       >associative_append % ]
771                     <H2 normalize in ⊢ (??%?); >Hrs3
772                     >associative_append >associative_append normalize
773                     >associative_append >associative_append
774                     @eq_f @eq_f @eq_f
775                     >(?:la@(〈d',false〉::lc@〈comma,false〉::rs32)@〈bar,false〉::〈d2,true〉::rs3'@〈grid,false〉::rs0 = 
776                         (la@〈d',false〉::lc)@〈comma,false〉::rs32@〈bar,false〉::〈d2,true〉::rs3'@〈grid,false〉::rs0 )
777                     [| >associative_append normalize >associative_append % ]
778                     <H3 %
779                    ]
780                  ]
781               ]
782        |* #Hnobars #Htapee >Htapee -Htapee *
783          [whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
784           cases (Htapef … (refl …)) -Htapef #_ #Htapef >Htapef -Htapef
785           whd in ⊢ (%→?); #Htapeout %2 % 
786           [% [//] whd #x #Hx @Hnobars @memb_append_l2 @memb_cons //
787           | >(Htapeout … (refl …)) % ]
788          |whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
789           cases (Htapef … (refl …)) -Htapef 
790           whd in ⊢ ((??%?)→?); #Htemp destruct (Htemp) 
791          ]
792        |(* no marks in table *)
793         #x #membx @(no_marks_in_table … Htable) 
794         @memb_append_l2
795         cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut >Hcut
796         >H3 >associative_append @memb_append_l2 @memb_cons @membx
797        |(* no grids in table *)
798         #x #membx @(no_grids_in_table … Htable) 
799         @memb_append_l2
800         cut (∀A,l1,l2.∀a:A. a::l1@l2=(a::l1)@l2) [//] #Hcut >Hcut
801         >H3 >associative_append @memb_append_l2 @memb_cons @membx
802        |whd in ⊢ (??%?); >(bit_or_null_not_grid … Hd') >(bit_or_null_not_bar … Hd') %
803        ]
804      ]
805    |#x #membx @(no_marks_in_table … Htable) 
806     @memb_append_l2 @memb_cons @memb_append_l1 @membx 
807    |#x #membx @(no_marks_in_table … Htable) 
808     @memb_append_l1 @membx
809    |%
810    ]
811  ]
812 qed.
813
814 (* 
815   MATCH TUPLE
816
817   scrolls through the tuples in the transition table until one matching the
818   current configuration is found
819 *)
820
821 definition match_tuple ≝  whileTM ? match_tuple_step (inr … (inl … (inr … start_nop))).
822
823 lemma is_grid_true : ∀c.is_grid c = true → c = grid.
824 * normalize [ #b ] #H // destruct (H)
825 qed.
826
827 definition R_match_tuple ≝ λt1,t2.
828   ∀ls,c,l1,c1,l2,rs,n.
829   is_bit c = true → only_bits l1 → is_bit c1 = true → n = |l1| →
830   table_TM (S n) (〈c1,true〉::l2) → 
831   t1 = midtape STape (〈grid,false〉::ls) 〈c,true〉 
832          (l1@〈grid,false〉::〈c1,true〉::l2@〈grid,false〉::rs) → 
833   (* facciamo match *)
834   (∃l3,newc,mv,l4.
835    〈c1,false〉::l2 = l3@〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv@l4 ∧
836    t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls) 〈grid,false〉
837         (l3@〈c,false〉::l1@〈comma,true〉::newc@〈comma,false〉::mv@l4@〈grid,false〉::rs))
838   ∨
839   (* non facciamo match su nessuna tupla;
840      non specifichiamo condizioni sul nastro di output, perché
841      non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
842   (current ? t2 = Some ? 〈grid,true〉 ∧
843    ∀l3,newc,mv,l4.
844    〈c1,false〉::l2 ≠ l3@〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv@l4). 
845
846 definition weakR_match_tuple ≝ λt1,t2.
847   ∀ls,cur,rs.
848   t1 = midtape STape ls cur rs → 
849   (is_grid (\fst cur) = true → t2 = t1) ∧
850   (∀c,l1,c1,l2,l3,ls0,rs0,n.
851   ls = 〈grid,false〉::ls0 → 
852   cur = 〈c,true〉 → 
853   rs = l1@〈grid,false〉::l2@〈c1,true〉::l3@〈grid,false〉::rs0 → 
854   is_bit c = true → is_bit c1 = true → 
855   only_bits_or_nulls l1 → no_marks l1 → S n = |l1| →
856   table_TM (S n) (l2@〈c1,false〉::l3) → 
857   (* facciamo match *)
858   (∃l4,newc,mv,l5.
859    〈c1,false〉::l3 = l4@〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l5 ∧
860    t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls0) 〈grid,false〉
861         (l2@l4@〈c,false〉::l1@〈comma,true〉::newc@〈comma,false〉::mv::l5@
862         〈grid,false〉::rs0))
863   ∨
864   (* non facciamo match su nessuna tupla;
865      non specifichiamo condizioni sul nastro di output, perché
866      non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
867   (current ? t2 = Some ? 〈grid,true〉 ∧
868    ∀l4,newc,mv,l5.
869    〈c1,false〉::l3 ≠ l4@〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv::l5)).  
870
871 axiom table_bit_after_bar : 
872   ∀n,l1,c,l2.table_TM n (l1@〈bar,false〉::〈c,false〉::l2) → is_bit c = true.
873
874 lemma wsem_match_tuple : WRealize ? match_tuple weakR_match_tuple.
875 #intape #k #outc #Hloop 
876 lapply (sem_while … sem_match_tuple_step intape k outc Hloop) [%] -Hloop
877 * #ta * #Hstar @(star_ind_l ??????? Hstar)
878 [ #tb whd in ⊢ (%→?); #Hleft
879   #ls #cur #rs #Htb cases (Hleft … Htb) #Hgrid #Houtc %
880   [ #_ @Houtc 
881   | #c #l1 #c1 #l2 #l3 #ls0 #rs0 #n #Hls #Hcur #Hrs 
882     >Hcur in Hgrid; #Hgrid >(is_grid_true … Hgrid) normalize in ⊢ (%→?);
883     #Hc destruct (Hc)
884     
885   ]
886 | #tb #tc #td whd in ⊢ (%→?); #Htc
887   #Hstar1 #IH whd in ⊢ (%→?); #Hright lapply (IH Hright) -IH whd in ⊢ (%→?); #IH
888   #ls #cur #rs #Htb %
889   [ #Hcur cases (Htc … Htb) * #Hfalse @False_ind @Hfalse @(is_grid_true … Hcur)
890   |#c #l1 #c1 #l2 #l3 #ls0 #rs0 #n #Hls #Hcur #Hrs #Hc #Hc1 #Hl1bitnull #Hl1marks 
891    #Hl1len #Htable cases (Htc … Htb) -Htc -Htb * #_ #Htc
892    cut (∃la,lb,mv,lc.l3 = la@〈comma,false〉::lb@〈comma,false〉::mv::lc ∧
893          S n = |la| ∧ only_bits_or_nulls la)
894    [@daemon] * #la * #lb * #mv * #lc * * #Hl3 #Hlalen #Hlabitnull
895    >Hl3 in Htable;
896   >(?: 〈c1,true〉::(la@〈comma,false〉::lb@〈comma,false〉::mv::lc)@〈grid,false〉::rs =
897        〈c1,true〉::la@〈comma,false〉::(lb@〈comma,false〉::mv::lc)@〈grid,false〉::rs)
898   [#Htable cases (Htc ?? l1 l2 ??? rs0 ???????? Htable Hls Hcur ?)
899    [10: >Hrs >Hl3 >associative_append >associative_append
900     normalize >associative_append % ] //
901    [3: whd in ⊢ (??%?); >Hc %
902    |4: whd in ⊢ (??%?); >Hc1 %
903    |5: @Hl1len
904    |6: <Hl1len @Hlalen
905    | * 
906      [ * #Heq #Htc % %{[]} %{lb} %{mv} %{lc}
907        destruct (Heq) %
908        [ %
909        | cases (IH … Htc) #Houtc #_ >(Houtc (refl ??)) -Houtc
910          >Htc @eq_f normalize >associative_append %
911        ]
912      | * #Hdiff * #c2 * #l5 * #l6 * #Hnext #Htc
913        cases (IH … Htc) -IH #_ #IH >Hnext in Htable;
914        >(?:l2@〈c1,false〉::la@〈comma,false〉::l5@〈bar,false〉::〈c2,false〉::l6 =
915           (l2@〈c1,false〉::la@〈comma,false〉::l5@[〈bar,false〉])@〈c2,false〉::l6)
916        [|@daemon] #Htable
917        cases (IH ? l1 ? (l2@〈c1,false〉::la@〈comma,false〉::l5@[〈bar,false〉]) l6 ? rs0 ?
918                 (refl ??) (refl ??) … Htable) //
919        [3: >associative_append normalize >associative_append 
920            normalize >associative_append %
921        |4: @(table_bit_after_bar (S n) (l2@〈c1,false〉::la@〈comma,false〉::l5) ? l6)
922            >associative_append in Htable; normalize >associative_append normalize
923            >associative_append normalize >associative_append normalize
924            >associative_append normalize //
925        | * #l4 * #newc * #mv0 * #l50 * #Heq #Houtc %
926          @(ex_intro ?? (〈c1,false〉::la@〈comma,false〉::l5@〈bar,false〉::l4))
927          @(ex_intro ?? newc) @(ex_intro ?? mv0) @(ex_intro ?? l50) >Heq %
928          [ normalize >associative_append normalize >associative_append %
929          | >Houtc @eq_f normalize >associative_append normalize >associative_append
930            normalize >associative_append normalize >associative_append
931            normalize >associative_append %
932          ]
933        | * #Houtc #Hneq %2 % [@Houtc]
934          #l4 #newc #mv0 #l50
935          (* difficile (sempre che sia dimostrabile) 
936             dobbiamo veramente considerare di fare la table in modo più 
937             strutturato
938          *)
939          @daemon
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      | (* difficile (sempre che sia dimostrabile) 
950           dobbiamo veramente considerare di fare la table in modo più 
951           strutturato
952         *)
953         @daemon
954      ]
955    ]
956 | >associative_append %
957 ]
958 qed.
959