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