]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/universal/copy.ma
Added copy machine (mockup)
[helm.git] / matita / matita / lib / turing / universal / copy.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/tuples.ma".
18
19 definition write_states ≝ initN 2.
20
21 definition write ≝ λalpha,c.
22   mk_TM alpha write_states
23   (λp.let 〈q,a〉 ≝ p in
24     match q with 
25     [ O ⇒ 〈1,Some ? 〈c,N〉〉
26     | S _ ⇒ 〈1,None ?〉 ])
27   O (λx.x == 1).
28   
29 definition R_write ≝ λalpha,c,t1,t2.
30   ∀ls,x,rs.t1 = midtape alpha ls x rs → t2 = midtape alpha ls c rs.
31   
32 axiom sem_write : ∀alpha,c.Realize ? (write alpha c) (R_write alpha c).
33
34 definition copy_step_subcase ≝
35   λalpha,c,elseM.ifTM ? (test_char ? (λx.x == 〈c,true〉))
36     (seq (FinProd alpha FinBool) (adv_mark_r …)
37       (seq ? (move_l …)
38         (seq ? (adv_to_mark_l … (is_marked alpha))
39           (seq ? (write ? 〈c,false〉)
40             (seq ? (move_r …)
41               (seq ? (mark …)
42                 (seq ? (move_r …) (adv_to_mark_r … (is_marked alpha)))))))))
43     elseM tc_true.
44
45 definition R_copy_step_subcase ≝ 
46   λalpha,c,RelseM,t1,t2.
47     ∀ls,x,rs.t1 = midtape (FinProd … alpha FinBool) ls 〈x,true〉 rs → 
48     (x = c ∧
49      ∀a,l1,x0,a0,l2,l3. (∀c.memb ? c l1 = true → is_marked ? c = false) → 
50      ls = l1@〈a0,false〉::〈x0,true〉::l2 → 
51      rs = 〈a,false〉::l3 → 
52      t2 = midtape ? (〈x,false〉::l1@〈a0,true〉::〈x,false〉::l2) 〈a,true〉 l3) ∨
53     (x ≠ c ∧ RelseM t1 t2).
54     
55 axiom sem_copy_step_subcase : 
56   ∀alpha,c,elseM,RelseM.
57   Realize ? (copy_step_subcase alpha c elseM) (R_copy_step_subcase alpha c RelseM).
58     
59 (*
60 if current = 0,tt
61    then advance_mark_r;
62         move_l;
63         advance_to_mark_l;
64         write(0,ff)
65         move_r;
66         mark;
67         move_r;
68         advance_to_mark_r;
69 else if current = 1,tt
70    then advance_mark_r;
71         move_l;
72         advance_to_mark_l;
73         write(1,ff)
74         move_r;
75         mark;
76         move_r;
77         advance_to_mark_r;
78 else nop
79 *)
80
81 definition copy_step ≝
82   ifTM ? (test_char STape (λc.is_bit (\fst c)))
83   (single_finalTM ? (copy_step_subcase FSUnialpha (bit false)
84     (copy_step_subcase FSUnialpha (bit true) (nop ?))))
85   (nop ?)
86   tc_true.
87   
88 definition R_copy_step_true ≝ 
89   λt1,t2.
90     ∀ls,c,rs.t1 = midtape (FinProd … FSUnialpha FinBool) ls 〈c,true〉 rs → 
91     ∃x. c = bit x ∧
92     (∀a,l1,c0,a0,l2,l3. (∀y.memb ? y l1 = true → is_marked ? y = false) → 
93      ls = l1@〈a0,false〉::〈c0,true〉::l2 → 
94      rs = 〈a,false〉::l3 → 
95      t2 = midtape STape (〈bit x,false〉::l1@〈a0,true〉::〈bit x,false〉::l2) 〈a,true〉 l3).
96
97 definition R_copy_step_false ≝ 
98   λt1,t2.
99    ∀ls,c,rs.t1 = midtape (FinProd … FSUnialpha FinBool) ls c rs → 
100    is_bit (\fst c) = false ∧ t2 = t1.
101
102 axiom sem_comp_step : 
103   accRealize ? copy_step (inr … (inl … (inr … 0))) R_copy_step_true R_copy_step_false.
104    
105 definition copy ≝ whileTM ? copy_step (inr … (inl … (inr … 0))).
106
107 definition R_copy ≝ λt1,t2.
108   ∀ls,c,rs.t1 = midtape ? ls 〈c,true〉 rs → 
109   (∀l1,d,l2,l3,l4.
110    〈c,false〉::rs = l1@〈d,false〉::l2 → only_bits l1 → is_bit d = false → 
111    ls = l3@l4@〈c0,true〉::l5 → |l4| = |l1@[〈d,false〉]|
112   
113   
114
115 axiom no_grids_in_table: ∀n.∀l.table_TM n l → no_grids l.
116 (*
117 l0 x* a l1 x0* a0 l2 ------> l0 x a* l1 x0 a0* l2
118    ^                               ^
119
120 if current (* x *) = #
121    then 
122    else if x = 0
123       then move_right; ----
124            adv_to_mark_r;
125            if current (* x0 *) = 0
126               then advance_mark ----
127                    adv_to_mark_l;
128                    advance_mark
129               else STOP
130       else x = 1 (* analogo *)
131
132 *)
133
134
135 (*
136    MARK NEXT TUPLE machine
137    (partially axiomatized)
138    
139    marks the first character after the first bar (rightwards)
140  *)
141  
142 definition bar_or_grid ≝ λc:STape.is_bar (\fst c) ∨ is_grid (\fst c).
143
144 definition mark_next_tuple ≝ 
145   seq ? (adv_to_mark_r ? bar_or_grid)
146      (ifTM ? (test_char ? (λc:STape.is_bar (\fst c)))
147        (move_right_and_mark ?) (nop ?) 1).
148
149 definition R_mark_next_tuple ≝ 
150   λt1,t2.
151     ∀ls,c,rs1,rs2.
152     (* c non può essere un separatore ... speriamo *)
153     t1 = midtape STape ls c (rs1@〈grid,false〉::rs2) → 
154     no_marks rs1 → no_grids rs1 → bar_or_grid c = false → 
155     (∃rs3,rs4,d,b.rs1 = rs3 @ 〈bar,false〉 :: rs4 ∧
156       no_bars rs3 ∧
157       Some ? 〈d,b〉 = option_hd ? (rs4@〈grid,false〉::rs2) ∧
158       t2 = midtape STape (〈bar,false〉::reverse ? rs3@c::ls) 〈d,true〉 (tail ? (rs4@〈grid,false〉::rs2)))
159     ∨
160     (no_bars rs1 ∧ t2 = midtape ? (reverse ? rs1@c::ls) 〈grid,false〉 rs2).
161      
162 axiom tech_split :
163   ∀A:DeqSet.∀f,l.
164    (∀x.memb A x l = true → f x = false) ∨
165    (∃l1,c,l2.f c = true ∧ l = l1@c::l2 ∧ ∀x.memb ? x l1 = true → f x = false).
166 (*#A #f #l elim l
167 [ % #x normalize #Hfalse *)
168      
169 theorem sem_mark_next_tuple :
170   Realize ? mark_next_tuple R_mark_next_tuple.
171 #intape 
172 lapply (sem_seq ? (adv_to_mark_r ? bar_or_grid)
173          (ifTM ? (test_char ? (λc:STape.is_bar (\fst c))) (move_right_and_mark ?) (nop ?) 1) ????)
174 [@sem_if [5: // |6: @sem_move_right_and_mark |7: // |*:skip]
175 | //
176 |||#Hif cases (Hif intape) -Hif
177    #j * #outc * #Hloop * #ta * #Hleft #Hright
178    @(ex_intro ?? j) @ex_intro [|% [@Hloop] ]
179    -Hloop
180    #ls #c #rs1 #rs2 #Hrs #Hrs1 #Hrs1' #Hc
181    cases (Hleft … Hrs)
182    [ * #Hfalse >Hfalse in Hc; #Htf destruct (Htf)
183    | * #_ #Hta cases (tech_split STape (λc.is_bar (\fst c)) rs1)
184      [ #H1 lapply (Hta rs1 〈grid,false〉 rs2 (refl ??) ? ?)
185        [ * #x #b #Hx whd in ⊢ (??%?); >(Hrs1' … Hx) >(H1 … Hx) %
186        | %
187        | -Hta #Hta cases Hright
188          [ * #tb * whd in ⊢ (%→?); #Hcurrent
189            @False_ind cases (Hcurrent 〈grid,false〉 ?)
190            [ normalize #Hfalse destruct (Hfalse)
191            | >Hta % ]
192          | * #tb * whd in ⊢ (%→?); #Hcurrent
193            cases (Hcurrent 〈grid,false〉 ?)
194            [  #_ #Htb whd in ⊢ (%→?); #Houtc
195              %2 %
196              [ @H1
197              | >Houtc >Htb >Hta % ]
198            | >Hta % ]
199          ]
200        ]
201     | * #rs3 * #c0 * #rs4 * * #Hc0 #Hsplit #Hrs3
202       % @(ex_intro ?? rs3) @(ex_intro ?? rs4)
203      lapply (Hta rs3 c0 (rs4@〈grid,false〉::rs2) ???)
204      [ #x #Hrs3' whd in ⊢ (??%?); >Hsplit in Hrs1;>Hsplit in Hrs3;
205        #Hrs3 #Hrs1 >(Hrs1 …) [| @memb_append_l1 @Hrs3'|]
206        >(Hrs3 … Hrs3') @Hrs1' >Hsplit @memb_append_l1 //
207      | whd in ⊢ (??%?); >Hc0 %
208      | >Hsplit >associative_append % ] -Hta #Hta
209        cases Hright
210        [ * #tb * whd in ⊢ (%→?); #Hta'
211          whd in ⊢ (%→?); #Htb
212          cases (Hta' c0 ?)
213          [ #_ #Htb' >Htb' in Htb; #Htb
214            generalize in match Hsplit; -Hsplit
215            cases rs4 in Hta;
216            [ #Hta #Hsplit >(Htb … Hta)
217              >(?:c0 = 〈bar,false〉)
218              [ @(ex_intro ?? grid) @(ex_intro ?? false)
219                % [ % [ % 
220                [(* Hsplit *) @daemon |(*Hrs3*) @daemon ] | % ] | % ] 
221                | (* Hc0 *) @daemon ]
222            | #r5 #rs5 >(eq_pair_fst_snd … r5)
223              #Hta #Hsplit >(Htb … Hta)
224              >(?:c0 = 〈bar,false〉)
225              [ @(ex_intro ?? (\fst r5)) @(ex_intro ?? (\snd r5))
226                % [ % [ % [ (* Hc0, Hsplit *) @daemon | (*Hrs3*) @daemon ] | % ]
227                      | % ] | (* Hc0 *) @daemon ] ] | >Hta % ]
228              | * #tb * whd in ⊢ (%→?); #Hta'
229                whd in ⊢ (%→?); #Htb
230                cases (Hta' c0 ?)
231                [ #Hfalse @False_ind >Hfalse in Hc0;
232                  #Hc0 destruct (Hc0)
233                | >Hta % ]
234 ]]]]
235 qed.
236
237 definition init_current ≝ 
238   seq ? (adv_to_mark_l ? (is_marked ?))
239     (seq ? (clear_mark ?)
240        (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
241           (seq ? (move_r ?) (mark ?)))).
242           
243 definition R_init_current ≝ λt1,t2.
244   ∀l1,c,l2,b,l3,c1,rs,c0,b0. no_marks l1 → no_grids l2 → is_grid c = false → 
245   Some ? 〈c0,b0〉 = option_hd ? (reverse ? (〈c,true〉::l2)) → 
246   t1 = midtape STape (l1@〈c,true〉::l2@〈grid,b〉::l3) 〈c1,false〉 rs → 
247   t2 = midtape STape (〈grid,b〉::l3) 〈c0,true〉
248         ((tail ? (reverse ? (l1@〈c,false〉::l2))@〈c1,false〉::rs)).
249
250 lemma sem_init_current : Realize ? init_current R_init_current.
251 #intape 
252 cases (sem_seq ????? (sem_adv_to_mark_l ? (is_marked ?))
253         (sem_seq ????? (sem_clear_mark ?)
254            (sem_seq ????? (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
255              (sem_seq ????? (sem_move_r ?) (sem_mark ?)))) intape)
256 #k * #outc * #Hloop #HR 
257 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop]
258 cases HR -HR #ta * whd in ⊢ (%→?); #Hta 
259 * #tb * whd in ⊢ (%→?); #Htb 
260 * #tc * whd in ⊢ (%→?); #Htc 
261 * #td * whd in ⊢ (%→%→?); #Htd #Houtc
262 #l1 #c #l2 #b #l3 #c1 #rs #c0 #b0 #Hl1 #Hl2 #Hc #Hc0 #Hintape
263 cases (Hta … Hintape) [ * #Hfalse normalize in Hfalse; destruct (Hfalse) ]
264 -Hta * #_ #Hta lapply (Hta l1 〈c,true〉 ? (refl ??) ??) [@Hl1|%]
265 -Hta #Hta lapply (Htb … Hta) -Htb #Htb cases (Htc … Htb) [ >Hc -Hc * #Hc destruct (Hc) ] 
266 -Htc * #_ #Htc lapply (Htc … (refl ??) (refl ??) ?) [@Hl2]
267 -Htc #Htc lapply (Htd … Htc) -Htd
268 >reverse_append >reverse_cons 
269 >reverse_cons in Hc0; cases (reverse … l2)
270 [ normalize in ⊢ (%→?); #Hc0 destruct (Hc0)
271   #Htd >(Houtc … Htd) %
272 | * #c2 #b2 #tl2 normalize in ⊢ (%→?);
273   #Hc0 #Htd >(Houtc … Htd)
274   whd in ⊢ (???%); destruct (Hc0)
275   >associative_append >associative_append %
276 ]
277 qed.
278
279 definition match_tuple_step ≝ 
280   ifTM ? (test_char ? (λc:STape.¬ is_grid (\fst c))) 
281    (single_finalTM ? 
282      (seq ? compare
283       (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
284         (nop ?)
285         (seq ? mark_next_tuple 
286            (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
287              (mark ?) (seq ? (move_l ?) init_current) tc_true)) tc_true)))
288     (nop ?) tc_true.
289
290 definition R_match_tuple_step_true ≝ λt1,t2.
291   ∀ls,c,l1,l2,c1,l3,l4,rs,n.
292   is_bit c = true → only_bits l1 → no_grids l2 → is_bit c1 = true →
293   only_bits l3 → n = |l1| → |l1| = |l3| →
294   table_TM (S n) (〈c1,true〉::l3@〈comma,false〉::l4) → 
295   t1 = midtape STape (〈grid,false〉::ls) 〈c,true〉 
296          (l1@〈grid,false〉::l2@〈bar,false〉::〈c1,true〉::l3@〈comma,false〉::l4@〈grid,false〉::rs) → 
297   (* facciamo match *)
298   (〈c,false〉::l1 = 〈c1,false〉::l3 ∧
299   t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls) 〈grid,false〉
300         (l2@〈bar,false〉::〈c1,false〉::l3@〈comma,true〉::l4@〈grid,false〉::rs))
301   ∨
302   (* non facciamo match e marchiamo la prossima tupla *)
303   ((〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧
304    ∃c2,l5,l6,l7.l4 = l5@〈bar,false〉::〈c2,false〉::l6@〈comma,false〉::l7 ∧
305    (* condizioni su l5 l6 l7 *)
306    t2 = midtape STape (〈grid,false〉::ls) 〈c,true〉 
307          (l1@〈grid,false〉::l2@〈bar,false〉::〈c1,true〉::l3@〈comma,false〉::
308           l5@〈bar,false〉::〈c2,true〉::l6@〈comma,false〉::l7))
309   ∨  
310   (* non facciamo match e non c'è una prossima tupla:
311      non specifichiamo condizioni sul nastro di output, perché
312      non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
313   (〈c,false〉::l1 ≠ 〈c1,false〉::l3 ∧ no_bars l4 ∧ current ? t2 = Some ? 〈grid,true〉)).  
314   
315 definition R_match_tuple_step_false ≝ λt1,t2.
316   ∀ls,c,rs.t1 = midtape STape ls c rs → is_grid (\fst c) = true ∧ t2 = t1.
317   
318 include alias "basics/logic.ma". 
319
320 (*
321 lemma eq_f4: ∀A1,A2,A3,A4,B.∀f:A1 → A2 →A3 →A4 →B.
322   ∀x1,x2,x3,x4,y1,y2,y3,y4. x1 = y1 → x2 = y2 →x3=y3 →x4 = y4 →   
323     f x1 x2 x3 x4 = f y1 y2 y3 y4.
324 //
325 qed-. *)
326
327 lemma some_option_hd: ∀A.∀l:list A.∀a.∃b.
328   Some ? b = option_hd ? (l@[a]) .
329 #A #l #a cases l normalize /2/
330 qed.
331
332 lemma bit_not_grid: ∀d. is_bit d = true → is_grid d = false.
333 * // normalize #H destruct
334 qed.
335
336 lemma bit_not_bar: ∀d. is_bit d = true → is_bar d = false.
337 * // normalize #H destruct
338 qed.
339
340 axiom sem_match_tuple_step: 
341     accRealize ? match_tuple_step (inr … (inl … (inr … 0))) 
342     R_match_tuple_step_true R_match_tuple_step_false.
343 (* @(acc_sem_if_app … (sem_test_char ? (λc:STape.¬ is_grid (\fst c))) …
344   (sem_seq … sem_compare
345     (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
346       (sem_nop …)
347         (sem_seq … sem_mark_next_tuple 
348            (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
349              (sem_mark ?) (sem_seq … (sem_move_l …) (sem_init_current …))))))
350   (sem_nop ?) …)
351 [(* is_grid: termination case *)
352  2:#t1 #t2 #t3 whd in ⊢ (%→?); #H #H1 whd #ls #c #rs #Ht1
353   cases (H c ?) [2: >Ht1 %] #Hgrid #Heq %
354     [@injective_notb @Hgrid | <Heq @H1]
355 |#tapea #tapeout #tapeb whd in ⊢ (%→?); #Htapea
356  * #tapec * #Hcompare #Hor 
357  #ls #c #l1 #l2 #c1 #l3 #l4 #rs #n #Hc #Hl1 #Hl2 #Hc1 #Hl3 #eqn
358  #eqlen #Htable #Htapea1 cases (Htapea 〈c,true〉 ?) >Htapea1 [2:%]
359  #notgridc -Htapea -Htapea1 -tapea #Htapeb  
360  cases (Hcompare … Htapeb) -Hcompare -Htapeb * #_ #_ #Hcompare 
361  cases (Hcompare c c1 l1 l3 (l2@[〈bar,false〉]) (l4@〈grid,false〉::rs) eqlen … (refl …) Hc ?)  
362  -Hcompare 
363    [* #Htemp destruct (Htemp) #Htapec %1 % [%]
364     >Htapec in Hor; -Htapec *
365      [2: * #t3 * whd in ⊢ (%→?); #H @False_ind
366       cases (H … (refl …)) whd in ⊢ ((??%?)→?); #H destruct (H)
367      |* #taped * whd in ⊢ (%→?); #Htaped cases (Htaped ? (refl …)) -Htaped *
368       #Htaped whd in ⊢ (%→?); #Htapeout >Htapeout >Htaped >associative_append
369       %
370      ]
371    |* #la * #c' * #d' * #lb * #lc * * * #H1 #H2 #H3 #Htapec 
372     cut (〈c,false〉::l1 ≠ 〈c1,false〉::l3) 
373       [>H2 >H3 elim la
374         [@(not_to_not …H1) normalize #H destruct % 
375         |#x #tl @not_to_not normalize #H destruct // 
376         ]
377       ] #Hnoteq %2
378     cut (is_bit d' = true) 
379       [cases la in H3;
380         [normalize in ⊢ (%→?); #H destruct //
381         |#x #tl #H @(Hl3 〈d',false〉)
382          normalize in H; destruct @memb_append_l2 @memb_hd
383         ] 
384       ] #Hd'
385     >Htapec in Hor; -Htapec *
386      [* #taped * whd in ⊢ (%→?); #H @False_ind
387       cases (H … (refl …)) >Hd' #Htemp destruct (Htemp)
388      |* #taped * whd in ⊢ (%→?); #H cases (H … (refl …)) -H #_
389       #Htaped * #tapee * whd in ⊢ (%→?); #Htapee  
390       <(associative_append ? lc (〈comma,false〉::l4)) in Htaped; #Htaped
391       lapply (Htapee … Htaped ???) -Htaped -Htapee 
392        [whd in ⊢ (??%?); >(bit_not_grid … Hd') >(bit_not_bar … Hd') %
393        |#x #Hx cases (memb_append … Hx) 
394          [-Hx #Hx @bit_not_grid @Hl3 cases la in H3; normalize 
395            [#H3 destruct (H3) @Hx | #y #tl #H3 destruct (H3) 
396             @memb_append_l2 @memb_cons @Hx ]
397          |-Hx #Hx @(no_grids_in_table … Htable) 
398           @memb_cons @memb_append_l2 @Hx
399          ]
400        |@daemon (* TODO *)
401        |* 
402          [* #rs3 * * (* we proceed by cases on rs4 *) 
403            [* #d * #b * * * #Heq1 #Hnobars
404             whd in ⊢ ((???%)→?); #Htemp destruct (Htemp)
405             #Htapee * 
406              [* #tapef * whd in ⊢ (%→?); >Htapee -Htapee #Htapef 
407               cases (Htapef … (refl …)) -Htapef #_ #Htapef >Htapef -Htapef
408               whd in ⊢ (%→?); #H lapply (H … ???? (refl …)) #Htapeout
409               %1 %
410               [ //| @daemon]
411               | >Htapeout %
412               ]
413            |* #tapef * whd in ⊢ (%→?); >Htapee -Htapee #Htapef
414             cases (Htapef … (refl …)) whd in ⊢ ((??%?)→?); #Htemp destruct (Htemp)
415            ]
416          |* #d2 #b2 #rs3' * #d  * #b * * * #Heq1 #Hnobars
417           cut (is_grid d2 = false) [@daemon (* ??? *)] #Hd2
418           whd in ⊢ ((???%)→?); #Htemp destruct (Htemp) #Htapee >Htapee -Htapee *
419            [* #tapef * whd in ⊢ (%→?); #Htapef 
420             cases (Htapef … (refl …)) >Hd2 #Htemp destruct (Htemp) 
421            |* #tapef * whd in ⊢ (%→?); #Htapef 
422             cases (Htapef … (refl …)) #_ -Htapef #Htapef
423             * #tapeg >Htapef -Htapef * whd in ⊢ (%→?); 
424             #H lapply (H … (refl …)) whd in ⊢ (???%→?); -H  #Htapeg
425             >Htapeg -Htapeg whd in ⊢ (%→?); #Htapeout
426             %1 cases (some_option_hd ? (reverse ? (reverse ? la)) 〈c',true〉)
427             * #c00 #b00 #Hoption
428             lapply 
429              (Htapeout (reverse ? rs3 @〈d',false〉::reverse ? la@reverse ? (l2@[〈bar,false〉])@(〈grid,false〉::reverse ? lb))
430              c' (reverse ? la) false ls bar (〈d2,true〉::rs3'@〈grid,false〉::rs) c00 b00 ?????) -Htapeout
431               [whd in ⊢ (??(??%??)?); @eq_f3 [2:%|3: %]
432                >associative_append 
433                generalize in match (〈c',true〉::reverse ? la@〈grid,false〉::ls); #l
434                whd in ⊢ (???(???%)); >associative_append >associative_append 
435                % 
436               |@daemon 
437               |@daemon
438               |@daemon
439               |@daemon
440               |@daemon
441               ]
442            ]
443          ]
444        |* #Hnobars #Htapee >Htapee -Htapee *
445          [whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
446           cases (Htapef … (refl …)) -Htapef #_ #Htapef >Htapef -Htapef
447           whd in ⊢ (%→?); #Htapeout %2
448           >(Htapeout … (refl …)) %
449            [ % 
450              [ @daemon 
451              | @daemon
452              ]
453            | %
454            ] 
455          |whd in ⊢ (%→?); * #tapef * whd in ⊢ (%→?); #Htapef
456           cases (Htapef … (refl …)) -Htapef 
457           whd in ⊢ ((??%?)→?); #Htemp destruct (Htemp) 
458          ]
459        |
460            
461            
462       
463       
464    
465   
466   ????? (refl …) Hc ?) -Hcompare 
467  #Hcompare 
468   is_bit c = true → only_bits l1 → no_grids l2 → is_bit c1 = true →
469   only_bits l3 → n = |l2| → |l2| = |l3| →
470   table_TM (S n) (〈c1,true〉::l3@〈comma,false〉::l4) →#x
471
472   #intape
473 cases 
474   (acc_sem_if … (sem_test_char ? (λc:STape.¬ is_grid (\fst c))) 
475     (sem_seq … sem_compare
476       (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
477         (sem_nop …)
478         (sem_seq … sem_mark_next_tuple 
479            (sem_if … (sem_test_char ? (λc:STape.is_grid (\fst c)))
480              (sem_mark ?) (sem_seq … (sem_move_l …) (sem_init_current …))))))
481     (sem_nop ?) intape)
482 #k * #outc * * #Hloop #H1 #H2
483 @(ex_intro ?? k) @(ex_intro ?? outc) %
484 [ % [@Hloop ] ] -Hloop
485   *)
486
487 (* 
488   MATCH TUPLE
489
490   scrolls through the tuples in the transition table until one matching the
491   current configuration is found
492 *)
493
494 definition match_tuple ≝  whileTM ? match_tuple_step (inr … (inl … (inr … 0))).
495
496 definition R_match_tuple ≝ λt1,t2.
497   ∀ls,c,l1,c1,l2,rs,n.
498   is_bit c = true → only_bits l1 → is_bit c1 = true → n = |l1| →
499   table_TM (S n) (〈c1,true〉::l2) → 
500   t1 = midtape STape (〈grid,false〉::ls) 〈c,true〉 
501          (l1@〈grid,false〉::〈c1,true〉::l2@〈grid,false〉::rs) → 
502   (* facciamo match *)
503   (∃l3,newc,mv,l4.
504    〈c1,false〉::l2 = l3@〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv@l4 ∧
505    t2 = midtape ? (reverse ? l1@〈c,false〉::〈grid,false〉::ls) 〈grid,false〉
506         (l3@〈c,false〉::l1@〈comma,true〉::newc@〈comma,false〉::mv@l4@〈grid,false〉::rs))
507   ∨
508   (* non facciamo match su nessuna tupla;
509      non specifichiamo condizioni sul nastro di output, perché
510      non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
511   (current ? t2 = Some ? 〈grid,true〉 ∧
512    ∀l3,newc,mv,l4.
513    〈c1,false〉::l2 ≠ l3@〈c,false〉::l1@〈comma,false〉::newc@〈comma,false〉::mv@l4).