]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/universal/copy.ma
Finalized copy sub-machine of the universal turing machine. Some new results
[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 include "turing/universal/tuples.ma".
14
15 definition write_states ≝ initN 2.
16
17 definition wr0 : write_states ≝ mk_Sig ?? 0 (leb_true_to_le 1 2 (refl …)).
18 definition wr1 : write_states ≝ mk_Sig ?? 1 (leb_true_to_le 2 2 (refl …)).
19
20 definition write ≝ λalpha,c.
21   mk_TM alpha write_states
22   (λp.let 〈q,a〉 ≝ p in
23     match pi1 … q with 
24     [ O ⇒ 〈wr1,Some ? 〈c,N〉〉
25     | S _ ⇒ 〈wr1,None ?〉 ])
26   wr0 (λx.x == wr1).
27   
28 definition R_write ≝ λalpha,c,t1,t2.
29   ∀ls,x,rs.t1 = midtape alpha ls x rs → t2 = midtape alpha ls c rs.
30   
31 axiom sem_write : ∀alpha,c.Realize ? (write alpha c) (R_write alpha c).
32
33 definition copy_step_subcase ≝
34   λalpha,c,elseM.ifTM ? (test_char ? (λx.x == 〈c,true〉))
35     (seq (FinProd alpha FinBool) (adv_mark_r …)
36       (seq ? (move_l …)
37         (seq ? (adv_to_mark_l … (is_marked alpha))
38           (seq ? (write ? 〈c,false〉)
39             (seq ? (move_r …)
40               (seq ? (mark …)
41                 (seq ? (move_r …) (adv_to_mark_r … (is_marked alpha)))))))))
42     elseM tc_true.
43
44 definition R_copy_step_subcase ≝ 
45   λalpha,c,RelseM,t1,t2.
46     ∀a,l1,x0,a0,l2,x,l3.
47     t1 = midtape (FinProd … alpha FinBool) (l1@〈a0,false〉::〈x0,true〉::l2) 
48          〈x,true〉 (〈a,false〉::l3) → 
49     (∀c.memb ? c l1 = true → is_marked ? c = false) →          
50     (x = c ∧ t2 = midtape ? (〈x,false〉::l1@〈a0,true〉::〈x,false〉::l2) 〈a,true〉 l3) ∨
51     (x ≠ c ∧ RelseM t1 t2).
52     
53 lemma sem_copy_step_subcase : 
54   ∀alpha,c,elseM,RelseM. Realize ? elseM RelseM → 
55   Realize ? (copy_step_subcase alpha c elseM) (R_copy_step_subcase alpha c RelseM).
56 #alpha #c #elseM #RelseM #HelseM #intape
57 cases (sem_if ? (test_char ? (λx. x == 〈c,true〉)) ?????? tc_true (sem_test_char ? (λx.x == 〈c,true〉))
58         (sem_seq ????? (sem_adv_mark_r alpha)
59           (sem_seq ????? (sem_move_l …)
60             (sem_seq ????? (sem_adv_to_mark_l … (is_marked alpha))
61               (sem_seq ????? (sem_write ? 〈c,false〉)
62                 (sem_seq ????? (sem_move_r …)
63                   (sem_seq ????? (sem_mark …)
64                     (sem_seq ????? (sem_move_r …) (sem_adv_to_mark_r … (is_marked alpha)))))))))
65         HelseM intape)
66 #k * #outc * #Hloop #HR %{k} %{outc} % [@Hloop] -Hloop
67 #a #l1 #x0 #a0 #l2 #x #l3 #Hintape #Hl1marks cases HR -HR
68 [ * #ta * whd in ⊢ (%→?); >Hintape #Hta cases (Hta … (refl ??)) -Hta #Hx #Hta
69   * #tb * whd in ⊢ (%→?); #Htb lapply (Htb … Hta) -Hta -Htb #Htb
70   * #tc * whd in ⊢ (%→?); #Htc lapply (Htc … Htb) -Htb -Htc #Htc
71   * #td * whd in ⊢ (%→?); #Htd cases (Htd … Htc) -Htd
72   [ >Htc * normalize in ⊢ (%→?); #Hfalse destruct (Hfalse) ]
73   * #_ #Htd lapply (Htd (l1@[〈a0,false〉]) 〈x0,true〉 l2 ? (refl ??) ?) -Htd
74   [ #x1 #Hx1 cases (memb_append … Hx1) -Hx1 #Hx1 [@(Hl1marks ? Hx1)|>(memb_single … Hx1) %]
75   | normalize >associative_append % ] #Htd
76   * #te * whd in ⊢ (%→?); #Hte lapply (Hte … Htd) -Hte -Htd -Htc #Hte
77   * #tf * whd in ⊢ (%→?); #Htf lapply (Htf … Hte) -Hte -Htf >reverse_append #Htf
78   * #tg * whd in ⊢ (%→?); #Htg lapply (Htg … Htf) -Htf -Htg >reverse_single #Htg
79   * #th * whd in ⊢ (%→?); #Hth lapply (Hth … Htg) -Htg -Hth
80   generalize in match Hl1marks; -Hl1marks @(list_elim_left … l1)
81   [ #Hl1marks #Hth whd in ⊢ (%→?); #Houtc cases (Houtc … Hth) -Houtc
82     [ * normalize in ⊢ (%→?); #Hfalse destruct (Hfalse) ]
83     * #_ #Houtc lapply (Houtc [] ?? (refl ??) (refl ??) Hl1marks) -Houtc
84     #Houtc lapply (\P Hx) -Hx #Hx destruct (Hx) % % [%] @Houtc
85   | -l1 #c1 #l1 #_ #Hl1marks >reverse_append >reverse_single
86     #Hth whd in ⊢ (%→?); #Houtc cases (Houtc … Hth) -Houtc
87     [ * >Hl1marks [ #Hfalse destruct (Hfalse) ] @memb_append_l2 @memb_hd ]
88     * #_ #Houtc lapply (Houtc (reverse ? l1@[〈x,false〉]) 〈a,true〉 l3 ? (refl ??) ?) -Houtc
89     [ #x1 #Hx1 cases (memb_append … Hx1) -Hx1 #Hx1 [ @Hl1marks @memb_append_l1 @daemon | >(memb_single … Hx1) % ]
90     | normalize >associative_append % ] 
91     #Houtc lapply (\P Hx) -Hx #Hx destruct (Hx) % % [%] >Houtc
92     >reverse_append >reverse_reverse >associative_append >associative_append % ]
93 | * #ta * whd in ⊢ (%→?); >Hintape #Hta cases (Hta ? (refl ??)) -Hta 
94   #Hxc #Hta >Hta #Houtc %2 % // lapply (\Pf Hxc) @not_to_not #Heq >Heq % ]
95 qed.
96     
97 (*
98 if current = 0,tt
99    then advance_mark_r;
100         move_l;
101         advance_to_mark_l;
102         write(0,ff)
103         move_r;
104         mark;
105         move_r;
106         advance_to_mark_r;
107 else if current = 1,tt
108    then advance_mark_r;
109         move_l;
110         advance_to_mark_l;
111         write(1,ff)
112         move_r;
113         mark;
114         move_r;
115         advance_to_mark_r;
116 else if current = null 
117    then advance_mark_r;
118         move_l;
119         advance_to_mark_l
120         adv_mark_r;
121         move_r;
122         advance_to_mark_r
123 *)
124
125 definition nocopy_subcase ≝
126   ifTM STape (test_char ? (λx:STape.x == 〈null,true〉))
127     (seq ? (adv_mark_r …)
128       (seq ? (move_l …)
129         (seq ? (adv_to_mark_l … (is_marked ?))
130           (seq ? (adv_mark_r …)
131             (seq ? (move_r …) (adv_to_mark_r … (is_marked ?)))))))
132     (nop ?) tc_true.
133
134 definition R_nocopy_subcase ≝ 
135   λt1,t2.
136     ∀a,l1,x0,a0,l2,x,l3.
137     t1 = midtape STape (l1@〈a0,false〉::〈x0,true〉::l2) 
138          〈x,true〉 (〈a,false〉::l3) → 
139     (∀c.memb ? c l1 = true → is_marked ? c = false) →          
140     (x = null ∧
141      t2 = midtape ? (〈x,false〉::l1@〈a0,true〉::〈x0,false〉::l2) 〈a,true〉 l3) ∨
142     (x ≠ null ∧ t2 = t1).
143     
144 lemma sem_nocopy_subcase : Realize ? nocopy_subcase R_nocopy_subcase.
145 #intape
146 cases (sem_if ? (test_char ? (λx:STape.x == 〈null,true〉)) ?????? tc_true
147         (sem_test_char ? (λx:STape.x == 〈null,true〉))
148           (sem_seq … (sem_adv_mark_r …)
149             (sem_seq … (sem_move_l …)
150               (sem_seq … (sem_adv_to_mark_l … (is_marked ?))
151                 (sem_seq … (sem_adv_mark_r …)
152                   (sem_seq … (sem_move_r …) 
153                     (sem_adv_to_mark_r … (is_marked ?))))))) (sem_nop ?) intape)
154 #k * #outc * #Hloop #HR @(ex_intro ?? k) @(ex_intro ?? outc)  % [@Hloop] -Hloop
155 #a #l1 #x0 #a0 #l2 #x #l3 #Hintape #Hl1marks cases HR -HR
156 [ * #ta * whd in ⊢ (%→?); >Hintape #Hta cases (Hta … (refl ??)) -Hta #Hx #Hta
157   * #tb * whd in ⊢ (%→?); #Htb lapply (Htb … Hta) -Hta -Htb #Htb
158   * #tc * whd in ⊢ (%→?); #Htc lapply (Htc … Htb) -Htb -Htc #Htc
159   * #td * whd in ⊢ (%→?); #Htd cases (Htd … Htc) -Htd
160   [ >Htc * normalize in ⊢ (%→?); #Hfalse destruct (Hfalse) ]
161   * #_ #Htd lapply (Htd (l1@[〈a0,false〉]) 〈x0,true〉 l2 ? (refl ??) ?) -Htd
162   [ #x1 #Hx1 cases (memb_append … Hx1) -Hx1 #Hx1 [@(Hl1marks ? Hx1)|>(memb_single … Hx1) %]
163   | normalize >associative_append % ] >reverse_append #Htd
164   * #te * whd in ⊢ (%→?); #Hte lapply (Hte … Htd) -Hte -Htd -Htc #Hte
165   * #tf * whd in ⊢ (%→?); #Htf lapply (Htf … Hte) -Hte -Htf
166   generalize in match Hl1marks; -Hl1marks @(list_elim_left … l1)
167   [ #Hl1marks #Hth whd in ⊢ (%→?); #Houtc cases (Houtc … Hth) -Houtc
168     [ * normalize in ⊢ (%→?); #Hfalse destruct (Hfalse) ]
169     * #_ #Houtc lapply (Houtc [] ?? (refl ??) (refl ??) Hl1marks) -Houtc
170     #Houtc lapply (\P Hx) -Hx #Hx destruct (Hx) % % [%] @Houtc
171   | -l1 #c1 #l1 #_ #Hl1marks >reverse_append >reverse_single
172     #Hth whd in ⊢ (%→?); #Houtc cases (Houtc … Hth) -Houtc
173     [ * >Hl1marks [ #Hfalse destruct (Hfalse) ] @memb_append_l2 @memb_hd ]
174     * #_ #Houtc lapply (Houtc (reverse ? l1@[〈x,false〉]) 〈a,true〉 l3 ? (refl ??) ?) -Houtc
175     [ #x1 #Hx1 cases (memb_append … Hx1) -Hx1 #Hx1 [ @Hl1marks @memb_append_l1 @daemon | >(memb_single … Hx1) % ]
176     | normalize >associative_append % ] 
177     #Houtc lapply (\P Hx) -Hx #Hx destruct (Hx) % % [%] >Houtc
178     >reverse_append >reverse_reverse >associative_append >associative_append % ]
179 | * #ta * whd in ⊢ (%→?); >Hintape #Hta cases (Hta ? (refl ??)) -Hta 
180   #Hxc #Hta >Hta whd in ⊢ (%→?); #Houtc %2 %
181   [ lapply (\Pf Hxc) @not_to_not #Heq >Heq %
182   | @Houtc ]
183 qed.
184
185 definition copy_step ≝
186   ifTM ? (test_char STape (λc.bit_or_null (\fst c)))
187   (single_finalTM ? (copy_step_subcase FSUnialpha (bit false)
188     (copy_step_subcase FSUnialpha (bit true) nocopy_subcase)))
189   (nop ?)
190   tc_true.
191   
192 definition R_copy_step_true ≝ 
193   λt1,t2.
194     ∀ls,c,rs. t1 = midtape STape ls 〈c,true〉 rs → 
195     bit_or_null c = true ∧
196     (∀a,l1,x0,a0,l2,l3.
197      ls = (l1@〈a0,false〉::〈x0,true〉::l2) → 
198      rs = (〈a,false〉::l3) → 
199      no_marks l1 →          
200      ((∃x. c = bit x ∧
201       t2 = midtape STape (〈bit x,false〉::l1@〈a0,true〉::〈bit x,false〉::l2) 〈a,true〉 l3) ∨
202       (c = null ∧
203       t2 = midtape ? (〈null,false〉::l1@〈a0,true〉::〈x0,false〉::l2) 〈a,true〉 l3))).
204      
205 definition R_copy_step_false ≝ 
206   λt1,t2.
207    ∀ls,c,rs.t1 = midtape (FinProd … FSUnialpha FinBool) ls c rs → 
208    bit_or_null (\fst c) = false ∧ t2 = t1.
209    
210 lemma sem_copy_step : 
211   accRealize ? copy_step (inr … (inl … (inr … start_nop))) R_copy_step_true R_copy_step_false.
212 #intape
213 @(acc_sem_if_app … (sem_test_char ? (λc:STape.bit_or_null (\fst c)))  …
214     (sem_copy_step_subcase FSUnialpha (bit false) …
215        (sem_copy_step_subcase FSUnialpha (bit true) … (sem_nocopy_subcase …)))
216           (sem_nop …))
217 [ #t1 #t2 #t3 whd in ⊢ (%→%→?); #H1 #H2 #ls #c #rs #Ht1 >Ht1 in H1; #H1
218   cases (H1 … (refl ??)) #Hc #Ht3 % [ @Hc ]
219   #a #l1 #x0 #a0 #l2 #l3 #Hls #Hrs #Hl1marks >Hls in Ht3; >Hrs #Ht3
220   cases (H2 … Ht3 ?)
221   [ * #Hc' #Ht2 % %{false} % // <Hc' @Ht2
222   | * #Hnotfalse whd in ⊢ (%→?); #Ht2 cases (Ht2 … Ht3 ?) -Ht2
223     [ * #Hc' #Ht2 % %{true} % // <Hc' @Ht2
224     |  * #Hnottrue whd in ⊢ (%→?); #Ht2 cases (Ht2 … Ht3 ?) -Ht2
225       [ * #Hc' #Ht2 %2 <Hc' % // @Ht2
226       | * #Hnotnull @False_ind
227         generalize in match Hnotnull;generalize in match Hnottrue;generalize in match Hnotfalse;
228         cases c in Hc; normalize
229         [ * [ #_ #_ * #Hfalse #_ | #_ * #Hfalse #_ #_ ]
230         | #_ #_ #_ * #Hfalse
231         |*: #Hfalse destruct (Hfalse) ] @Hfalse %
232       | @Hl1marks ]
233     | @Hl1marks ]
234   | @Hl1marks ]
235 | #t1 #t2 #t3 whd in ⊢ (%→%→?); #H1 #H2 #ls #c #rs #Ht1
236   >Ht1 in H1; #H1 cases (H1 … (refl ??)) #_ #Ht3 cases (H1 ? (refl ??)) -H1
237   #Hc #Ht3 % //
238 ]
239 qed.
240
241 (*
242 1) il primo carattere è marcato
243 2) l'ultimo carattere è l'unico che può essere null, gli altri sono bit
244 3) il terminatore non è né bit, né null
245 *)
246    
247 definition copy0 ≝ whileTM ? copy_step (inr … (inl … (inr … start_nop))).
248
249 let rec merge_config (l1,l2:list STape) ≝ 
250   match l1 with
251   [ nil ⇒ nil ?
252   | cons p1 l1' ⇒ match l2 with
253     [ nil ⇒ nil ? 
254     | cons p2 l2' ⇒ 
255            let 〈c1,b1〉 ≝ p1 in let 〈c2,b2〉 ≝ p2 in
256            match c2 with
257            [ null ⇒ p1
258            | _ ⇒ p2 ] :: merge_config l1' l2' ] ].
259            
260 lemma merge_config_append :
261  ∀l1,l2,l3,l4.|l1| = |l2| → 
262  merge_config (l1@l3) (l2@l4) = merge_config l1 l2@merge_config l3 l4.
263 #l1 #l2 #l3 #l4 #Hlen @(list_ind2 … Hlen)
264 [normalize //
265 | #t1 #t2 * #c1 #b1 * #c2 #b2 #IH whd in ⊢ (??%%); >IH % ]
266 qed.
267
268 definition R_copy0 ≝ λt1,t2.
269   ∀ls,c,c0,rs,l1,l3,l4.
270   t1 = midtape STape (l3@l4@〈c0,true〉::ls) 〈c,true〉 (l1@rs) → 
271   no_marks l1 → no_marks (l3@l4) → |l1| = |l4| → 
272   ∀l1',bv.〈c,false〉::l1 = l1'@[〈comma,bv〉] → only_bits_or_nulls l1' → 
273   ∀l4',bg.l4@[〈c0,false〉] = 〈grid,bg〉::l4' → only_bits_or_nulls l4' → 
274   (c = comma ∧ t2 = t1) ∨
275   (c ≠ comma ∧ 
276     t2 = midtape ? (reverse ? l1'@l3@〈grid,true〉::
277                   merge_config l4' (reverse ? l1')@ls) 
278      〈comma,true〉 rs).
279      
280 lemma inj_append_singleton_l1 :
281   ∀A.∀l1,l2:list A.∀a1,a2.l1@[a1] = l2@[a2] → l1 = l2.
282 #A #l1 #l2 #a1 #a2 #H lapply (eq_f … (reverse ?) … H)
283 >reverse_append >reverse_append normalize #H1 destruct
284 lapply (eq_f … (reverse ?) … e0) >reverse_reverse >reverse_reverse //
285 qed.
286
287 lemma inj_append_singleton_l2 :
288   ∀A.∀l1,l2:list A.∀a1,a2.l1@[a1] = l2@[a2] → a1 = a2.
289 #A #l1 #l2 #a1 #a2 #H lapply (eq_f … (reverse ?) … H)
290 >reverse_append >reverse_append normalize #H1 destruct %
291 qed.
292
293 lemma wsem_copy0 : WRealize ? copy0 R_copy0.
294 #intape #k #outc #Hloop 
295 lapply (sem_while … sem_copy_step intape k outc Hloop) [%] -Hloop
296 * #ta * #Hstar @(star_ind_l ??????? Hstar)
297 [ #tb whd in ⊢ (%→?); #Hleft
298   #ls #c #c0 #rs #l1 #l3 #l4 #Htb #Hl1nomarks #Hl3l4nomarks #Hlen #l1' #bv
299   #Hl1 #Hl1bits #l4' #bg #Hl4 #Hl4bits
300   cases (Hleft … Htb) -Hleft #Hc #Houtc % %
301   [ generalize in match Hl1bits; -Hl1bits cases l1' in Hl1;
302     [ normalize #Hl1 #c1 destruct (Hl1) %
303     | * #c' #b' #l0 #Heq normalize in Heq; destruct (Heq)
304       #Hl1bits lapply (Hl1bits 〈c',false〉 ?) [ @memb_hd ] 
305       >Hc #Hfalse destruct ]
306   | @Houtc ]
307 | #tb #tc #td whd in ⊢ (%→?→(?→%)→%→?); #Htc #Hstar1 #Hind #Htd
308   lapply (Hind Htd) -Hind #Hind
309   #ls #c #c0 #rs #l1 #l3 #l4 #Htb #Hl1nomarks #Hl3l4nomarks #Hlen #l1' #bv
310   #Hl1 #Hl1bits #l4' #bg #Hl4 #Hl4bits %2
311   cases (Htc … Htb) -Htc #Hcbitnull #Htc
312   % [ % #Hc' >Hc' in Hcbitnull; normalize #Hfalse destruct (Hfalse) ]
313   cut (|l1| = |reverse ? l4|) [@daemon] #Hlen1
314   @(list_cases2 … Hlen1)
315   [ (* case l1 = [] is discriminated because l1 contains at least comma *)
316     #Hl1nil @False_ind >Hl1nil in Hl1; cases l1' normalize
317     [ #Hl1 destruct normalize in Hcbitnull; destruct (Hcbitnull)
318     | #p0 #l0 normalize #Hfalse destruct (Hfalse) cases l0 in e0;
319       [ normalize #Hfalse1 destruct (Hfalse1)
320       | #p0' #l0' normalize #Hfalse1 destruct (Hfalse1) ] ]
321   | (* case c::l1 = c::a::l1'' *)
322     * #a #ba * #a0 #ba0 #l1'' #l4'' #Hl1cons #Hl4cons
323     lapply (eq_f ?? (reverse ?) ?? Hl4cons) >reverse_reverse >reverse_cons -Hl4cons #Hl4cons
324     cut (ba = false) 
325     [ >Hl1cons in Hl1nomarks; #Hl1nomarks lapply (Hl1nomarks 〈a,ba〉 ?)
326       [ @memb_hd | normalize // ] ] #Hba
327     cut (ba0 = false) 
328     [ >Hl4cons in Hl3l4nomarks; #Hl3l4nomarks lapply (Hl3l4nomarks 〈a0,ba0〉 ?)
329       [ @memb_append_l2 @memb_append_l2 @memb_hd | normalize // ] ] #Hba0
330     >Hba0 in Hl4cons; >Hba in Hl1cons; -Hba0 -Hba #Hl1cons #Hl4cons
331     >Hl4cons in Htc; >Hl1cons #Htc
332     lapply (Htc a (l3@reverse ? l4'') c0 a0 ls (l1''@rs) ? (refl ??) ?)
333     [ #x #Hx @Hl3l4nomarks >Hl4cons <associative_append
334       @memb_append_l1 @Hx
335     | >associative_append >associative_append %
336     | -Htc
337       cut (∃la.l1' = 〈c,false〉::la)
338       [ >Hl1cons in Hl1; cases l1'
339         [normalize #Hfalse destruct (Hfalse)
340         | #p #la normalize #Hla destruct (Hla) @(ex_intro ?? la) % ] ]
341       * #la #Hla
342       cut (∃lb.l4' = lb@[〈c0,false〉])
343       [ >Hl4cons in Hl4;
344         @(list_elim_left … l4')
345         [ #Heq lapply (eq_f … (length ?) … Heq)
346           >length_append >length_append 
347           >commutative_plus normalize >commutative_plus normalize
348           #Hfalse destruct
349         | #a1 #tl #_ #Heq 
350           >(inj_append_singleton_l2 ? (reverse ? l4''@[〈a0,false〉]) (〈grid,bg〉::tl) 〈c0,false〉 a1 Heq)
351           @ex_intro //
352       ] ] * #lb #Hlb
353       cut (|lb| = |reverse ? la|) 
354       [ >Hla in Hl1; >Hlb in Hl4; #Hl4 #Hl1
355         >(?:l1 = la@[〈comma,bv〉]) in Hlen;
356         [|normalize in Hl1; destruct (Hl1) %]
357         >(?:l4 = 〈grid,bg〉::lb)
358         [|@(inj_append_singleton_l1 ?? (〈grid,bg〉::lb) ?? Hl4) ]
359         >length_append >commutative_plus >length_reverse
360         normalize #Hlalb destruct (Hlalb) //
361       ] #Hlen2
362       *
363       (* by hyp on the first iteration step, 
364          we consider whether c = bit x or c = null *)
365       (* c = bit x *)
366       [ * #x * #Hx #Htc 
367         lapply (Hind (〈bit x,false〉::ls) a a0 rs l1'' 
368                 (〈bit x,false〉::l3) (reverse ? l4'') ????) 
369         [ >Hl1cons in Hlen; >Hl4cons >length_append >commutative_plus 
370           normalize #Hlen destruct (Hlen) //
371         | #x0 #Hx0 cases (orb_true_l … Hx0)
372           [ #Hx0eq >(\P Hx0eq) %
373           | -Hx0 #Hx0 @Hl3l4nomarks >Hl4cons
374             <associative_append @memb_append_l1 // ]
375         | #x0 #Hx0 @Hl1nomarks >Hl1cons @memb_cons //
376         | >Htc >associative_append % 
377         | -Hind 
378           <Hl1cons <Hl4cons #Hind lapply (Hind la bv ?? lb bg ??)
379           [ #x0 #Hx0 @Hl4bits >Hlb @memb_append_l1 //
380           | >Hlb in Hl4; normalize in ⊢ (%→?); #Hl4
381             @(inj_append_singleton_l1 ? l4 (〈grid,bg〉::lb) … Hl4)
382           | #x0 #Hx0 @Hl1bits >Hla @memb_cons //
383           | >Hla in Hl1; normalize in ⊢ (%→?); #Hl1
384             destruct (Hl1) // ] -Hind
385           (* by IH, we proceed by cases, whether a = comma 
386              (consequently several lists = []) or not *)          
387           *
388           [ * #Ha #Houtc1
389 (*           cut (l1 = [〈a,false〉])
390            [ cases l1'' in Hl1cons; // #y #ly #Hly
391              >Hly in Hl1; cases l1' in Hl1bits;
392              [ #_ normalize #Hfalse destruct (Hfalse)
393              | #p #lp #Hl1bits normalize #Heq destruct (Heq)
394                @False_ind lapply (Hl1bits 〈a,false〉 ?)
395                [ cases lp in e0;
396                  [ normalize #Hfalse destruct (Hfalse)
397                  | #p0 #lp0 normalize in ⊢ (%→?); #Heq destruct (Heq)
398                    @memb_cons @memb_hd ]
399                | >Ha normalize #Hfalse destruct (Hfalse) ]
400              ]
401            ] #Hl1a
402            cut (l4 = [〈a0,false〉])
403            [ generalize in match Hl4bits; cases l4' in Hl4;
404              [ >Hl4cons #Hfalse #_ 
405                lapply (inj_append_singleton_l1 ?? [] ?? Hfalse)
406                cases (reverse ? l4'') normalize
407                [ #Hfalse1 | #p0 #lp0 #Hfalse1 ] destruct (Hfalse1)
408              | #p #lp 
409            
410              cases l4'' in Hl4cons; // #y #ly #Hly
411              >Hly in Hl4; cases l4' in Hl4bits;
412              [ #_ >reverse_cons #Hfalse
413                lapply (inj_append_singleton_l1 ?? [] ?? Hfalse)
414                -Hfalse cases ly normalize
415                [ #Hfalse | #p #Hp #Hfalse ] destruct (Hfalse)
416                 
417              | #p #lp #Hl1bits normalize #Heq destruct (Heq)
418                @False_ind lapply (Hl1bits 〈a,false〉 ?)
419                [ cases lp in e0;
420                  [ normalize #Hfalse destruct (Hfalse)
421                  | #p0 #lp0 normalize in ⊢ (%→?); #Heq destruct (Heq)
422                    @memb_cons @memb_hd ]
423                | >Ha normalize #Hfalse destruct (Hfalse) ]
424              ]
425            ] #Hl1a
426              
427               >Hla normalize #Hl1 destruct (Hl1) lapply (inj_append_ @False_ind
428              
429            cut (l1'' = [] ∧ l4'' = [])
430            [ % [ >Hla in Hl1; normalize #Hl1 destruct (Hl1)
431            
432             cases l1'' in Hl1bits;
433                 
434                  [ #_ normalize #H *)
435            cut (la = [] ∧ lb = [] ∧ l1'' = [] ∧ l4'' = [])
436            [ @daemon ] * * * #Hla1 #Hlb1 #Hl1nil #Hl4nil
437            >Hl1cons in Hl1; >Hla
438            >Houtc1 >Htc #Hl1
439            >Hl4cons in Hl4; >Hlb #Hl4
440            >Hla1 >Hlb1 >Hl1nil >Hl4nil >Hx
441            cut (a0 = grid) [ @daemon ] #Ha0 <Ha <Ha0
442            normalize in ⊢ (??(??%?%)(??%?%)); >associative_append %
443           | * #Ha #Houtc1 >Houtc1 @eq_f3 //
444             >Hla >reverse_cons >associative_append @eq_f
445             >Hx whd in ⊢ (??%?); @eq_f whd in ⊢ (???%); @eq_f @eq_f
446             >Hlb >append_cons @eq_f2 // >(merge_config_append … Hlen2) %            
447           ]
448        ]
449     | (* c = null *)
450       * #Hc #Htc 
451       lapply (Hind (〈c0,false〉::ls) a a0 rs l1'' (〈null,false〉::l3) (reverse ? l4'') ????)
452       [  >Hl1cons in Hlen; >Hl4cons >length_append >commutative_plus normalize
453          #Hlen destruct (Hlen) @e0
454       | #x0 #Hx0 cases (memb_append STape ? [〈null,false〉] (l3@reverse ? l4'') … Hx0) -Hx0 #Hx0
455         [ >(memb_single … Hx0) %
456         | @Hl3l4nomarks cases (memb_append … Hx0) -Hx0 #Hx0
457           [ @memb_append_l1 //
458           | @memb_append_l2 >Hl4cons @memb_append_l1 // ]
459         ]
460       | >Hl1cons #x' #Hx0 @Hl1nomarks >Hl1cons @memb_cons //
461       | >Htc @eq_f3 // >associative_append % ] -Hind <Hl1cons <Hl4cons #Hind
462         lapply (Hind la bv ?? lb bg ??)
463           [ #x0 #Hx0 @Hl4bits >Hlb @memb_append_l1 //
464           | >Hlb in Hl4; normalize in ⊢ (%→?); #Hl4
465             @(inj_append_singleton_l1 ? l4 (〈grid,bg〉::lb) … Hl4)
466           | #x0 #Hx0 @Hl1bits >Hla @memb_cons //
467           | >Hla in Hl1; normalize in ⊢ (%→?); #Hl1
468             destruct (Hl1) // ] -Hind *
469           (* by IH, we proceed by cases, whether a = comma 
470              (consequently several lists = []) or not *)          
471           [ * #Ha #Houtc1 >Hl1cons in Hl1; >Hla
472            >Houtc1 >Htc #Hl1
473            >Hl4cons in Hl4; >Hlb #Hl4
474            cut (la = [] ∧ lb = [] ∧ l1'' = [] ∧ l4'' = []) 
475            [@daemon] * * * #Hla1 #Hlb1 #Hl1nil #Hl4nil
476            >Hla1 >Hlb1 >Hl1nil >Hl4nil >Hc
477            cut (a0 = grid) [ @daemon ] #Ha0 <Ha <Ha0
478            normalize in ⊢ (??(??%?%)(??%?%)); >associative_append %
479           | * #Ha #Houtc1 >Houtc1 @eq_f3 //
480             >Hla >reverse_cons >associative_append @eq_f
481             >Hc whd in ⊢ (??%?); @eq_f whd in ⊢ (???%); @eq_f @eq_f
482             >Hlb >append_cons @eq_f2 // >(merge_config_append … Hlen2) %
483           ]
484        ]
485 ]]]
486 qed.
487
488 definition merge_char ≝ λc1,c2.
489   match c2 with
490   [ null ⇒ c1
491   | _ ⇒ c2 ].
492   
493 lemma merge_cons : 
494   ∀c1,c2,conf1,conf2.
495   merge_config (〈c1,false〉::conf1) (〈c2,false〉::conf2) = 
496     〈merge_char c1 c2,false〉::merge_config conf1 conf2.
497 #c1 #c2 #conf1 #conf2 normalize @eq_f2 //
498 cases c2 /2/
499 qed.
500
501 lemma merge_bits : ∀l1,l2.|l1| = |l2| → only_bits l2 → merge_config l1 l2 = l2.
502 #l1 #l2 #Hlen @(list_ind2 … Hlen) //
503 #tl1 #tl2 #hd1 #hd2 #IH
504 >(eq_pair_fst_snd … hd1) >(eq_pair_fst_snd … hd2) #Hbits
505 change with (cons ???) in ⊢ (??%?); @eq_f2
506 [ cases (\fst hd2) in Hbits; 
507   [ #b #_ %
508   |*: #Hfalse lapply (Hfalse … (memb_hd …)) normalize #Hfalse1 destruct (Hfalse1) ]
509 | @IH #x #Hx @Hbits @memb_cons // ]
510 qed.
511
512 lemma merge_config_c_nil : 
513   ∀c.merge_config c [] = [].
514 #c cases c normalize //
515 qed.
516
517 lemma reverse_merge_config :
518   ∀c1,c2.|c1| = |c2| → reverse ? (merge_config c1 c2) = 
519     merge_config (reverse ? c1) (reverse ? c2).        
520 #c1 #c2 <(length_reverse ? c1) <(length_reverse ? c2) #Hlen
521 <(reverse_reverse ? c1) in ⊢ (??%?); <(reverse_reverse ? c2) in ⊢ (??%?);
522 generalize in match Hlen; @(list_ind2 … Hlen) -Hlen //
523 #tl1 #tl2 #hd1 #hd2 #IH whd in ⊢ (??%%→?); #Hlen destruct (Hlen) -Hlen
524 <(length_reverse ? tl1) in e0; <(length_reverse ? tl2) #Hlen
525 >reverse_cons >reverse_cons >(merge_config_append ???? Hlen)
526 >reverse_append >(eq_pair_fst_snd ?? hd1) >(eq_pair_fst_snd ?? hd2)
527 whd in ⊢ (??%%); @eq_f2 // @IH //
528 qed.
529
530 definition copy
531 ≝ 
532   seq STape copy0 (seq ? (move_l …) (seq ? (adv_to_mark_l … (is_marked ?))
533    (seq ? (clear_mark …) (seq ? (adv_to_mark_r … (is_marked ?)) (clear_mark …))))).
534
535 (*
536    s0, s1 = caratteri di testa dello stato
537    c0 = carattere corrente del nastro oggetto
538    c1 = carattere in scrittura sul nastro oggetto
539    
540    questa dimostrazione sfrutta il fatto che 
541    merge_config (l0@[c0]) (l1@[c1]) = l1@[merge_char c0 c1] 
542    se l0 e l1 non contengono null
543 *)
544
545 definition R_copy ≝ λt1,t2.
546   ∀ls,s0,s1,c0,c1,rs,l1,l3,l4.
547   t1 = midtape STape (l3@〈grid,false〉::〈c0,false〉::l4@〈s0,true〉::ls) 〈s1,true〉 (l1@〈c1,false〉::〈comma,false〉::rs) → 
548   no_marks l1 → no_marks l3 → no_marks l4 → |l1| = |l4| → 
549   only_bits (l4@[〈s0,false〉]) → only_bits (〈s1,false〉::l1) → 
550   bit_or_null c0 = true → bit_or_null c1 = true → 
551   t2 = midtape STape (〈c1,false〉::reverse ? l1@〈s1,false〉::l3@〈grid,false〉::
552                       〈merge_char c0 c1,false〉::reverse ? l1@〈s1,false〉::ls)
553        〈comma,false〉 rs.
554        
555 axiom sem_copy0 : Realize ? copy0 R_copy0.
556
557 definition option_cons ≝ λA.λa:option A.λl.
558   match a with
559   [ None ⇒ l
560   | Some a' ⇒ a'::l ].
561
562 lemma sem_copy : Realize ? copy R_copy.
563 #intape 
564 cases (sem_seq … (sem_copy0 …)
565         (sem_seq … (sem_move_l …)
566           (sem_seq … (sem_adv_to_mark_l … (is_marked ?))
567             (sem_seq … (sem_clear_mark …)
568               (sem_seq … (sem_adv_to_mark_r … (is_marked ?)) (sem_clear_mark …))))) intape)
569 #k * #outc * #Hloop #HR %{k} %{outc} % [@Hloop] -Hloop
570 #ls #s0 #s1 #c0 #c1 #rs #l1 #l2 #l3 #Hintape #Hl1marks #Hl2marks #Hl3marks #Hlen
571 #Hbits1 #Hbits2 #Hc0bits #Hc1bits
572 cases HR -HR #ta * whd in ⊢ (%→?); #Hta 
573 cut (ta = midtape STape (〈c1,false〉::reverse ? l1@〈s1,false〉::l2@〈grid,true〉::
574                       〈merge_char c0 c1,false〉::reverse ? l1@〈s1,false〉::ls)
575        〈comma,true〉 rs)
576 [lapply (Hta ls s1 s0 rs (l1@[〈c1,false〉;〈comma,false〉]) l2 (〈grid,false〉::〈c0,false〉::l3) ?)
577   [>associative_append in ⊢ (???(????%)); normalize in ⊢ (???(??%%%)); @Hintape ]
578  -Hta #Hta cases (Hta ??? (〈s1,false〉::l1@[〈c1,false〉]) false ? ? ?? (refl ??) ?)
579   [3: #x #Hx cases (memb_append … Hx) -Hx #Hx
580     [ @Hl1marks //
581     | cases (orb_true_l … Hx) -Hx #Hx [ >(\P Hx) % | >(memb_single … Hx) % ]] 
582   |4: #x #Hx cases (memb_append … Hx) -Hx #Hx
583     [ @Hl2marks //
584     | cases (orb_true_l … Hx) -Hx #Hx [ >(\P Hx) % | cases (orb_true_l … Hx) [-Hx #Hx >(\P Hx) % | @Hl3marks ] ] ]
585   |5: >length_append normalize >Hlen >commutative_plus %
586   |6: normalize >associative_append %
587   |7: #x #Hx cases (memb_append ?? (〈s1,false〉::l1) … Hx) -Hx #Hx
588     [ whd in ⊢ (??%?); >(Hbits2 … Hx) %
589     | >(memb_single … Hx) // ]
590   |8: #x #Hx cases (memb_append … Hx) -Hx #Hx
591     [ cases (orb_true_l … Hx) -Hx #Hx [ >(\P Hx) // | whd in ⊢ (??%?); >Hbits1 // @memb_append_l1 // ]
592     | >(memb_single … Hx) whd in ⊢ (??%?); >(Hbits1 〈s0,false〉) // @memb_append_l2 @memb_hd ]
593   | * #Hs1 @False_ind >Hs1 in Hbits2; #Hbits2 lapply (Hbits2 〈comma,false〉 ?) //
594     normalize #Hfalse destruct (Hfalse)
595   | * #Hs1 #Ht2 >Ht2 >reverse_cons >reverse_append >reverse_single @eq_f3 //
596     >merge_cons >merge_bits
597     [2: #x #Hx @Hbits2 cases (memb_append STape ? (reverse ? l1) ? Hx) -Hx #Hx
598       [@daemon | >(memb_single … Hx) @memb_hd ]
599     |3: >length_append >length_append >length_reverse >Hlen % ]
600     normalize >associative_append normalize >associative_append %
601   ]
602 ] -Hta #Hta * #tb * whd in ⊢ (%→?); #Htb
603 lapply (Htb … Hta) -Htb #Htb change with (midtape ????) in Htb:(???%);
604 * #tc * whd in ⊢ (%→?); #Htc 
605 cases (Htc … Htb)
606 [ * #Hfalse normalize in Hfalse; destruct (Hfalse) ]
607 * #_ #Htc 
608 lapply (Htc (reverse ? l1@〈s1,false〉::l2) 〈grid,true〉 
609           (〈merge_char c0 c1,false〉::reverse ? l1@〈s1,false〉::ls)???)
610 [ #x #Hx cases (memb_append … Hx) -Hx #Hx
611   [ @Hl1marks @daemon
612   | cases (orb_true_l … Hx) -Hx #Hx
613     [ >(\P Hx) % | @(Hl2marks … Hx) ] ]
614 | %
615 | whd in ⊢ (??%?); >associative_append % ] -Htc #Htc
616 * #td * whd in ⊢ (%→?); #Htd lapply (Htd … Htc) -Htd #Htd
617 * #te * whd in ⊢ (%→?); #Hte cases (Hte … Htd) -Hte -Htd
618 [ * #Hfalse normalize in Hfalse; destruct (Hfalse) ]
619 * #_ #Hte 
620 lapply (Hte (reverse ? (reverse ? l1@〈s1,false〉::l2)@[〈c1,false〉])
621          〈comma,true〉 rs ? (refl ??) ?) -Hte
622 [ >reverse_append >reverse_cons >reverse_reverse #x #Hx
623   cases (memb_append … Hx) -Hx #Hx
624   [ cases (memb_append … Hx) -Hx #Hx
625     [ cases (memb_append … Hx) -Hx #Hx
626       [ @daemon 
627       | lapply (memb_single … Hx) -Hx #Hx >Hx % ]
628     | @(Hl1marks … Hx) ]
629   | lapply (memb_single … Hx) -Hx #Hx >Hx % ]
630 | >reverse_append >reverse_reverse >reverse_cons
631   >associative_append >associative_append >associative_append
632   >associative_append >associative_append % ]
633 #Hte whd in ⊢ (%→?); #Houtc lapply (Houtc … Hte) -Houtc #Houtc >Houtc
634 @eq_f3 //
635 >reverse_append >reverse_append >reverse_single >reverse_cons
636 >reverse_append >reverse_append >reverse_reverse >reverse_reverse
637 >reverse_single >associative_append >associative_append %
638 qed.