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