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