]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/universal/tuples.ma
almost there
[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 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 definition tuple_TM : nat → list STape → Prop ≝ 
34  λn,t.∃qin,qout,mv,b1,b2.
35  only_bits qin ∧ only_bits qout ∧ only_bits mv ∧
36  |qin| = n ∧ |qout| = n (* ∧ |mv| = ? *) ∧ 
37  t = qin@〈comma,b1〉::qout@〈comma,b2〉::mv.
38  
39 inductive table_TM : nat → list STape → Prop ≝ 
40 | ttm_nil  : ∀n.table_TM n [] 
41 | ttm_cons : ∀n,t1,T,b.tuple_TM n t1 → table_TM n T → table_TM n (t1@〈bar,b〉::T).
42
43 axiom no_grids_in_table: ∀n.∀l.table_TM n l → no_grids l.
44 (*
45 l0 x* a l1 x0* a0 l2 ------> l0 x a* l1 x0 a0* l2
46    ^                               ^
47
48 if current (* x *) = #
49    then 
50    else if x = 0
51       then move_right; ----
52            adv_to_mark_r;
53            if current (* x0 *) = 0
54               then advance_mark ----
55                    adv_to_mark_l;
56                    advance_mark
57               else STOP
58       else x = 1 (* analogo *)
59
60 *)
61
62
63 (*
64    MARK NEXT TUPLE machine
65    (partially axiomatized)
66    
67    marks the first character after the first bar (rightwards)
68  *)
69  
70 definition bar_or_grid ≝ λc:STape.is_bar (\fst c) ∨ is_grid (\fst c).
71
72 definition mark_next_tuple ≝ 
73   seq ? (adv_to_mark_r ? bar_or_grid)
74      (ifTM ? (test_char ? (λc:STape.is_bar (\fst c)))
75        (move_right_and_mark ?) (nop ?) 1).
76
77 definition R_mark_next_tuple ≝ 
78   λt1,t2.
79     ∀ls,c,rs1,rs2.
80     (* c non può essere un separatore ... speriamo *)
81     t1 = midtape STape ls c (rs1@〈grid,false〉::rs2) → 
82     no_marks rs1 → no_grids rs1 → bar_or_grid c = false → 
83     (∃rs3,rs4,d,b.rs1 = rs3 @ 〈bar,false〉 :: rs4 ∧
84       no_bars rs3 ∧
85       Some ? 〈d,b〉 = option_hd ? (rs4@〈grid,false〉::rs2) ∧
86       t2 = midtape STape (〈bar,false〉::reverse ? rs3@c::ls) 〈d,true〉 (tail ? (rs4@〈grid,false〉::rs2)))
87     ∨
88     (no_bars rs1 ∧ t2 = midtape ? (reverse ? rs1@c::ls) 〈grid,false〉 rs2).
89      
90 axiom tech_split :
91   ∀A:DeqSet.∀f,l.
92    (∀x.memb A x l = true → f x = false) ∨
93    (∃l1,c,l2.f c = true ∧ l = l1@c::l2 ∧ ∀x.memb ? x l1 = true → f x = false).
94 (*#A #f #l elim l
95 [ % #x normalize #Hfalse *)
96      
97 theorem sem_mark_next_tuple :
98   Realize ? mark_next_tuple R_mark_next_tuple.
99 #intape 
100 lapply (sem_seq ? (adv_to_mark_r ? bar_or_grid)
101          (ifTM ? (test_char ? (λc:STape.is_bar (\fst c))) (move_right_and_mark ?) (nop ?) 1) ????)
102 [@sem_if [5: // |6: @sem_move_right_and_mark |7: // |*:skip]
103 | //
104 |||#Hif cases (Hif intape) -Hif
105    #j * #outc * #Hloop * #ta * #Hleft #Hright
106    @(ex_intro ?? j) @ex_intro [|% [@Hloop] ]
107    -Hloop
108    #ls #c #rs1 #rs2 #Hrs #Hrs1 #Hrs1' #Hc
109    cases (Hleft … Hrs)
110    [ * #Hfalse >Hfalse in Hc; #Htf destruct (Htf)
111    | * #_ #Hta cases (tech_split STape (λc.is_bar (\fst c)) rs1)
112      [ #H1 lapply (Hta rs1 〈grid,false〉 rs2 (refl ??) ? ?)
113        [ * #x #b #Hx whd in ⊢ (??%?); >(Hrs1' … Hx) >(H1 … Hx) %
114        | %
115        | -Hta #Hta cases Hright
116          [ * #tb * whd in ⊢ (%→?); #Hcurrent
117            @False_ind cases (Hcurrent 〈grid,false〉 ?)
118            [ normalize #Hfalse destruct (Hfalse)
119            | >Hta % ]
120          | * #tb * whd in ⊢ (%→?); #Hcurrent
121            cases (Hcurrent 〈grid,false〉 ?)
122            [  #_ #Htb whd in ⊢ (%→?); #Houtc
123              %2 %
124              [ @H1
125              | >Houtc >Htb >Hta % ]
126            | >Hta % ]
127          ]
128        ]
129     | * #rs3 * #c0 * #rs4 * * #Hc0 #Hsplit #Hrs3
130       % @(ex_intro ?? rs3) @(ex_intro ?? rs4)
131      lapply (Hta rs3 c0 (rs4@〈grid,false〉::rs2) ???)
132      [ #x #Hrs3' whd in ⊢ (??%?); >Hsplit in Hrs1;>Hsplit in Hrs3;
133        #Hrs3 #Hrs1 >(Hrs1 …) [| @memb_append_l1 @Hrs3'|]
134        >(Hrs3 … Hrs3') @Hrs1' >Hsplit @memb_append_l1 //
135      | whd in ⊢ (??%?); >Hc0 %
136      | >Hsplit >associative_append % ] -Hta #Hta
137        cases Hright
138        [ * #tb * whd in ⊢ (%→?); #Hta'
139          whd in ⊢ (%→?); #Htb
140          cases (Hta' c0 ?)
141          [ #_ #Htb' >Htb' in Htb; #Htb
142            generalize in match Hsplit; -Hsplit
143            cases rs4 in Hta;
144            [ #Hta #Hsplit >(Htb … Hta)
145              >(?:c0 = 〈bar,false〉)
146              [ @(ex_intro ?? grid) @(ex_intro ?? false)
147                % [ % [ % 
148                [(* Hsplit *) @daemon |(*Hrs3*) @daemon ] | % ] | % ] 
149                | (* Hc0 *) @daemon ]
150            | #r5 #rs5 >(eq_pair_fst_snd … r5)
151              #Hta #Hsplit >(Htb … Hta)
152              >(?:c0 = 〈bar,false〉)
153              [ @(ex_intro ?? (\fst r5)) @(ex_intro ?? (\snd r5))
154                % [ % [ % [ (* Hc0, Hsplit *) @daemon | (*Hrs3*) @daemon ] | % ]
155                      | % ] | (* Hc0 *) @daemon ] ] | >Hta % ]
156              | * #tb * whd in ⊢ (%→?); #Hta'
157                whd in ⊢ (%→?); #Htb
158                cases (Hta' c0 ?)
159                [ #Hfalse @False_ind >Hfalse in Hc0;
160                  #Hc0 destruct (Hc0)
161                | >Hta % ]
162 ]]]]
163 qed.
164
165 definition init_current ≝ 
166   seq ? (adv_to_mark_l ? (is_marked ?))
167     (seq ? (clear_mark ?)
168        (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
169           (seq ? (move_r ?) (mark ?)))).
170           
171 definition R_init_current ≝ λt1,t2.
172   ∀l1,c,l2,b,l3,c1,rs,c0,b0. no_marks l1 → no_grids l2 → is_grid c = false → 
173   Some ? 〈c0,b0〉 = option_hd ? (reverse ? (〈c,true〉::l2)) → 
174   t1 = midtape STape (l1@〈c,true〉::l2@〈grid,b〉::l3) 〈c1,false〉 rs → 
175   t2 = midtape STape (〈grid,b〉::l3) 〈c0,true〉
176         ((tail ? (reverse ? (l1@〈c,false〉::l2))@〈c1,false〉::rs)).
177
178 lemma sem_init_current : Realize ? init_current R_init_current.
179 #intape 
180 cases (sem_seq ????? (sem_adv_to_mark_l ? (is_marked ?))
181         (sem_seq ????? (sem_clear_mark ?)
182            (sem_seq ????? (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
183              (sem_seq ????? (sem_move_r ?) (sem_mark ?)))) intape)
184 #k * #outc * #Hloop #HR 
185 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop]
186 cases HR -HR #ta * whd in ⊢ (%→?); #Hta 
187 * #tb * whd in ⊢ (%→?); #Htb 
188 * #tc * whd in ⊢ (%→?); #Htc 
189 * #td * whd in ⊢ (%→%→?); #Htd #Houtc
190 #l1 #c #l2 #b #l3 #c1 #rs #c0 #b0 #Hl1 #Hl2 #Hc #Hc0 #Hintape
191 cases (Hta … Hintape) [ * #Hfalse normalize in Hfalse; destruct (Hfalse) ]
192 -Hta * #_ #Hta lapply (Hta l1 〈c,true〉 ? (refl ??) ??) [@Hl1|%]
193 -Hta #Hta lapply (Htb … Hta) -Htb #Htb cases (Htc … Htb) [ >Hc -Hc * #Hc destruct (Hc) ] 
194 -Htc * #_ #Htc lapply (Htc … (refl ??) (refl ??) ?) [@Hl2]
195 -Htc #Htc lapply (Htd … Htc) -Htd
196 >reverse_append >reverse_cons 
197 >reverse_cons in Hc0; cases (reverse … l2)
198 [ normalize in ⊢ (%→?); #Hc0 destruct (Hc0)
199   #Htd >(Houtc … Htd) %
200 | * #c2 #b2 #tl2 normalize in ⊢ (%→?);
201   #Hc0 #Htd >(Houtc … Htd)
202   whd in ⊢ (???%); destruct (Hc0)
203   >associative_append >associative_append %
204 ]
205 qed.
206
207 definition match_tuple_step ≝ 
208   ifTM ? (test_char ? (λc:STape.¬ is_grid (\fst c))) 
209    (single_finalTM ? 
210      (seq ? compare
211       (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
212         (nop ?)
213         (seq ? mark_next_tuple 
214            (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
215              (mark ?) (seq ? (move_l ?) init_current) tc_true)) tc_true)))
216     (nop ?) tc_true.
217
218 definition R_match_tuple_step_true ≝ λt1,t2.
219   ∀ls,c,l1,l2,c1,l3,l4,rs,n.
220   is_bit c = true → only_bits l1 → no_grids l2 → is_bit c1 = true →
221   only_bits l3 → n = |l1| → |l1| = |l3| →
222   table_TM (S n) (〈c1,true〉::l3@〈comma,false〉::l4) → 
223   t1 = midtape STape (〈grid,false〉::ls) 〈c,true〉 
224          (l1@〈grid,false〉::l2@〈bar,false〉::〈c1,true〉::l3@〈comma,false〉::l4@〈grid,false〉::rs) → 
225   (* facciamo match *)
226   (〈c,false〉::l1 = 〈c1,false〉::l3 ∧
227   t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls) 〈grid,false〉
228         (l2@〈bar,false〉::〈c1,false〉::l3@〈comma,true〉::l4@〈grid,false〉::rs))
229   ∨
230   (* non facciamo match e marchiamo la prossima tupla *)
231   ((〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧
232    ∃c2,l5,l6,l7.l4 = l5@〈bar,false〉::〈c2,false〉::l6@〈comma,false〉::l7 ∧
233    (* condizioni su l5 l6 l7 *)
234    t2 = midtape STape (〈grid,false〉::ls) 〈c,true〉 
235          (l1@〈grid,false〉::l2@〈bar,false〉::〈c1,true〉::l3@〈comma,false〉::
236           l5@〈bar,false〉::〈c2,true〉::l6@〈comma,false〉::l7))
237   ∨  
238   (* non facciamo match e non c'è una prossima tupla:
239      non specifichiamo condizioni sul nastro di output, perché
240      non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
241   (〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧ no_bars l4 ∧ current ? t2 = Some ? 〈grid,true〉)).  
242   
243 definition R_match_tuple_step_false ≝ λt1,t2.
244   ∀ls,c,rs.t1 = midtape STape ls c rs → is_grid (\fst c) = true ∧ t2 = t1.
245   
246 include alias "basics/logic.ma". 
247
248 (*
249 lemma eq_f4: ∀A1,A2,A3,A4,B.∀f:A1 → A2 →A3 →A4 →B.
250   ∀x1,x2,x3,x4,y1,y2,y3,y4. x1 = y1 → x2 = y2 →x3=y3 →x4 = y4 →   
251     f x1 x2 x3 x4 = f y1 y2 y3 y4.
252 //
253 qed-. *)
254
255 lemma some_option_hd: ∀A.∀l:list A.∀a.∃b.
256   Some ? b = option_hd ? (l@[a]) .
257 #A #l #a cases l normalize /2/
258 qed.
259
260 lemma bit_not_grid: ∀d. is_bit d = true → is_grid d = false.
261 * // normalize #H destruct
262 qed.
263
264 lemma bit_not_bar: ∀d. is_bit d = true → is_bar d = false.
265 * // normalize #H destruct
266 qed.
267
268 lemma sem_match_tuple_step: 
269     accRealize ? match_tuple_step (inr … (inl … (inr … 0))) 
270     R_match_tuple_step_true R_match_tuple_step_false.
271 @(acc_sem_if_app … (sem_test_char ? (λc:STape.¬ is_grid (\fst c))) …
272   (sem_seq … sem_compare
273     (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
274       (sem_nop …)
275         (sem_seq … sem_mark_next_tuple 
276            (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
277              (sem_mark ?) (sem_seq … (sem_move_l …) (sem_init_current …))))))
278   (sem_nop ?) …)
279 [(* is_grid: termination case *)
280  2:#t1 #t2 #t3 whd in ⊢ (%→?); #H #H1 whd #ls #c #rs #Ht1
281   cases (H c ?) [2: >Ht1 %] #Hgrid #Heq %
282     [@injective_notb @Hgrid | <Heq @H1]
283 |#tapea #tapeout #tapeb whd in ⊢ (%→?); #Htapea
284  * #tapec * #Hcompare #Hor 
285  #ls #c #l1 #l2 #c1 #l3 #l4 #rs #n #Hc #Hl1 #Hl2 #Hc1 #Hl3 #eqn
286  #eqlen #Htable #Htapea1 cases (Htapea 〈c,true〉 ?) >Htapea1 [2:%]
287  #notgridc -Htapea -Htapea1 -tapea #Htapeb  
288  cases (Hcompare … Htapeb) -Hcompare -Htapeb * #_ #_ #Hcompare 
289  cases (Hcompare c c1 l1 l3 (l2@[〈bar,false〉]) (l4@〈grid,false〉::rs) eqlen Hl1 … (refl …) Hc ?)  
290  -Hcompare 
291    [* #Htemp destruct (Htemp) #Htapec %1 % [%]
292     >Htapec in Hor; -Htapec *
293      [2: * #t3 * whd in ⊢ (%→?); #H @False_ind
294       cases (H … (refl …)) whd in ⊢ ((??%?)→?); #H destruct (H)
295      |* #taped * whd in ⊢ (%→?); #Htaped cases (Htaped ? (refl …)) -Htaped *
296       #Htaped whd in ⊢ (%→?); #Htapeout >Htapeout >Htaped >associative_append
297       %
298      ]
299    |* #la * #c' * #d' * #lb * #lc * * * #H1 #H2 #H3 #Htapec 
300     cut (〈c,false〉::l1 ≠ 〈c1,false〉::l3) 
301       [>H2 >H3 elim la
302         [@(not_to_not …H1) normalize #H destruct % 
303         |#x #tl @not_to_not normalize #H destruct // 
304         ]
305       ] #Hnoteq %2
306     cut (is_bit d' = true) 
307       [cases la in H3;
308         [normalize in ⊢ (%→?); #H destruct //
309         |#x #tl #H @(Hl3 〈d',false〉)
310          normalize in H; destruct @memb_append_l2 @memb_hd
311         ] 
312       ] #Hd'
313     >Htapec in Hor; -Htapec *
314      [* #taped * whd in ⊢ (%→?); #H @False_ind
315       cases (H … (refl …)) >(bit_not_grid ? Hd') #Htemp destruct (Htemp)
316      |* #taped * whd in ⊢ (%→?); #H cases (H … (refl …)) -H #_
317       #Htaped * #tapee * whd in ⊢ (%→?); #Htapee  
318       <(associative_append ? lc (〈comma,false〉::l4)) in Htaped; #Htaped
319       cases (Htapee … Htaped ???) -Htaped -Htapee 
320        [* #rs3 * * (* we proceed by cases on rs4 *) 
321          [(* rs4 is empty *)
322           * #d * #b * * * #Heq1 #Hnobars
323           whd in ⊢ ((???%)→?); #Htemp destruct (Htemp)
324           #Htapee * 
325            [* #tapef * whd in ⊢ (%→?); >Htapee -Htapee #Htapef 
326             cases (Htapef … (refl …)) -Htapef #_ #Htapef >Htapef -Htapef
327             whd in ⊢ (%→?); #H lapply (H … ???? (refl …)) #Htapeout
328             %2 % 
329              [% [@Hnoteq |@daemon]
330              |>Htapeout %
331              ]
332            |* #tapef * whd in ⊢ (%→?); >Htapee -Htapee #Htapef
333             cases (Htapef … (refl …)) whd in ⊢ ((??%?)→?); #Htemp destruct (Htemp)
334            ]
335          |(* rs4 not empty *)
336           * #d2 #b2 #rs3' * #d  * #b * * * #Heq1 #Hnobars
337           cut (is_grid d2 = false) [@daemon (* no grids in table *)] #Hd2
338           whd in ⊢ ((???%)→?); #Htemp destruct (Htemp) #Htapee >Htapee -Htapee *
339            [* #tapef * whd in ⊢ (%→?); #Htapef 
340             cases (Htapef … (refl …)) >Hd2 #Htemp destruct (Htemp) 
341            |* #tapef * whd in ⊢ (%→?); #Htapef 
342             cases (Htapef … (refl …)) #_ -Htapef #Htapef
343             * #tapeg >Htapef -Htapef * whd in ⊢ (%→?); 
344             #H lapply (H … (refl …)) whd in ⊢ (???%→?); -H  #Htapeg
345             >Htapeg -Htapeg whd in ⊢ (%→?); #Htapeout
346             %1 cases (some_option_hd ? (reverse ? (reverse ? la)) 〈c',true〉)
347             * #c00 #b00 #Hoption
348             lapply 
349              (Htapeout (reverse ? rs3 @〈d',false〉::reverse ? la@reverse ? (l2@[〈bar,false〉])@(〈grid,false〉::reverse ? lb))
350              c' (reverse ? la) false ls bar (〈d2,true〉::rs3'@〈grid,false〉::rs) c00 b00 ?????) -Htapeout
351               [whd in ⊢ (??(??%??)?); @eq_f3 [2:%|3: %]
352                >associative_append 
353                generalize in match (〈c',true〉::reverse ? la@〈grid,false〉::ls); #l
354                whd in ⊢ (???(???%)); >associative_append >associative_append % 
355               |>reverse_cons @Hoption
356               |cases la in H2; 
357                 [normalize in ⊢ (%→?); #Htemp destruct (Htemp) 
358                  @injective_notb @notgridc
359                 |#x #tl normalize in ⊢ (%→?); #Htemp destruct (Htemp)
360                  @bit_not_grid @(Hl1 〈c',false〉) @memb_append_l2 @memb_hd
361                 ]
362               |cut (only_bits (la@(〈c',false〉::lb)))
363                 [<H2 whd #c0 #Hmemb cases (orb_true_l … Hmemb)
364                   [#eqc0 >(\P eqc0) @Hc |@Hl1]
365                 |#Hl1' #x #Hx @bit_not_grid @Hl1'
366                  @memb_append_l1 @daemon
367                 ]
368               |@daemon
369               |@daemon
370               ]
371            ]
372          ]
373        |* #Hnobars #Htapee >Htapee -Htapee *
374          [whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
375           cases (Htapef … (refl …)) -Htapef #_ #Htapef >Htapef -Htapef
376           whd in ⊢ (%→?); #Htapeout %2
377           >(Htapeout … (refl …)) %
378            [ % 
379              [ @Hnoteq 
380              | whd #x #Hx @Hnobars @memb_append_l2 @memb_cons //
381              ]
382            | %
383            ] 
384          |whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
385           cases (Htapef … (refl …)) -Htapef 
386           whd in ⊢ ((??%?)→?); #Htemp destruct (Htemp) 
387          ]
388        |@daemon (* no marks in table *)
389        |(* no grids in table *)
390         #x #Hx cases (memb_append … Hx) 
391          [-Hx #Hx @bit_not_grid @Hl3 cases la in H3; normalize 
392            [#H3 destruct (H3) @Hx | #y #tl #H3 destruct (H3) 
393             @memb_append_l2 @memb_cons @Hx ]
394          |-Hx #Hx @(no_grids_in_table … Htable) 
395           @memb_cons @memb_append_l2 @Hx
396          ]
397        |whd in ⊢ (??%?); >(bit_not_grid … Hd') >(bit_not_bar … Hd') %
398        ]
399      ]
400    |@Hl3
401    |@daemon
402    |@daemon
403    |@daemon
404    |>associative_append %
405    ]
406  ]
407 qed.
408
409