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