]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/multi_universal/copy.ma
copy.ma
[helm.git] / matita / matita / lib / turing / multi_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 include "turing/multi_universal/moves.ma".
13 include "turing/if_multi.ma".
14 include "turing/inject.ma".
15 include "turing/basic_machines.ma".
16
17 definition copy_states ≝ initN 3.
18
19 definition copy0 : copy_states ≝ mk_Sig ?? 0 (leb_true_to_le 1 3 (refl …)).
20 definition copy1 : copy_states ≝ mk_Sig ?? 1 (leb_true_to_le 2 3 (refl …)).
21 definition copy2 : copy_states ≝ mk_Sig ?? 2 (leb_true_to_le 3 3 (refl …)).
22
23
24 definition trans_copy_step ≝ 
25  λsrc,dst.λsig:FinSet.λn.
26  λp:copy_states × (Vector (option sig) (S n)).
27  let 〈q,a〉 ≝ p in
28  match pi1 … q with
29  [ O ⇒ match nth src ? a (None ?) with
30    [ None ⇒ 〈copy2,null_action sig n〉
31    | Some ai ⇒ match nth dst ? a (None ?) with 
32      [ None ⇒ 〈copy2,null_action ? n〉
33      | Some aj ⇒ 
34          〈copy1,change_vec ? (S n) 
35            (change_vec ? (S n) (null_action ? n) (〈None ?,R〉) src)
36            (〈Some ? ai,R〉) dst〉
37      ]
38    ]
39  | S q ⇒ match q with 
40    [ O ⇒ (* 1 *) 〈copy1,null_action ? n〉
41    | S _ ⇒ (* 2 *) 〈copy2,null_action ? n〉 ] ].
42
43 definition copy_step ≝ 
44   λsrc,dst,sig,n.
45   mk_mTM sig n copy_states (trans_copy_step src dst sig n) 
46     copy0 (λq.q == copy1 ∨ q == copy2).
47
48 definition R_comp_step_true ≝ 
49   λsrc,dst,sig,n.λint,outt: Vector (tape sig) (S n).
50   ∃x,y.
51    current ? (nth src ? int (niltape ?)) = Some ? x ∧
52    current ? (nth dst ? int (niltape ?)) = Some ? y ∧
53    outt = change_vec ?? 
54             (change_vec ?? int
55               (tape_move_mono ? (nth src ? int (niltape ?)) 〈None ?, R〉) src)
56             (tape_move_mono ? (nth dst ? int (niltape ?)) 〈Some ? x, R〉) dst.
57
58 definition R_comp_step_false ≝ 
59   λsrc,dst:nat.λsig,n.λint,outt: Vector (tape sig) (S n).
60     (current ? (nth src ? int (niltape ?)) = None ? ∨
61      current ? (nth dst ? int (niltape ?)) = None ?) ∧ outt = int.
62
63 lemma copy_q0_q2_null :
64   ∀src,dst,sig,n,v.src < S n → dst < S n → 
65   (nth src ? (current_chars ?? v) (None ?) = None ? ∨
66    nth dst ? (current_chars ?? v) (None ?) = None ?) → 
67   step sig n (copy_step src dst sig n) (mk_mconfig ??? copy0 v) 
68   = mk_mconfig ??? copy2 v.
69 #src #dst #sig #n #v #Hi #Hj
70 whd in ⊢ (? → ??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (?→??%?);
71 * #Hcurrent
72 [ @eq_f2
73   [ whd in ⊢ (??(???%)?); >Hcurrent %
74   | whd in ⊢ (??(????(???%))?); >Hcurrent @tape_move_null_action ]
75 | @eq_f2
76   [ whd in ⊢ (??(???%)?); >Hcurrent cases (nth src ?? (None sig)) //
77   | whd in ⊢ (??(????(???%))?); >Hcurrent
78     cases (nth src ?? (None sig)) [|#x] @tape_move_null_action ] ]
79 qed.
80
81 lemma copy_q0_q1 :
82   ∀src,dst,sig,n,v,a,b.src ≠ dst → src < S n → dst < S n → 
83   nth src ? (current_chars ?? v) (None ?) = Some ? a →
84   nth dst ? (current_chars ?? v) (None ?) = Some ? b → 
85   step sig n (copy_step src dst sig n) (mk_mconfig ??? copy0 v) =
86     mk_mconfig ??? copy1 
87      (change_vec ? (S n) 
88        (change_vec ?? v
89          (tape_move_mono ? (nth src ? v (niltape ?)) 〈None ?, R〉) src)
90             (tape_move_mono ? (nth dst ? v (niltape ?)) 〈Some ? a, R〉) dst).
91 #src #dst #sig #n #v #a #b #Heq #Hsrc #Hdst #Ha1 #Ha2
92 whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
93 [ whd in match (trans ????);
94   >Ha1 >Ha2 whd in ⊢ (??(???%)?); >(\b ?) //
95 | whd in match (trans ????);
96   >Ha1 >Ha2 whd in ⊢ (??(????(???%))?); >(\b ?) //
97   change with (change_vec ?????) in ⊢ (??(????%)?);
98   <(change_vec_same … v dst (niltape ?)) in ⊢ (??%?);
99   <(change_vec_same … v src (niltape ?)) in ⊢ (??%?);
100   >tape_move_multi_def 
101   >pmap_change >pmap_change <tape_move_multi_def
102   >tape_move_null_action
103   @eq_f2 // >nth_change_vec_neq //
104 ]
105 qed.
106
107 lemma sem_copy_step :
108   ∀src,dst,sig,n.src ≠ dst → src < S n → dst < S n → 
109   copy_step src dst sig n ⊨ 
110     [ copy1: R_comp_step_true src dst sig n, 
111             R_comp_step_false src dst sig n ].
112 #src #dst #sig #n #Hneq #Hsrc #Hdst #int
113 lapply (refl ? (current ? (nth src ? int (niltape ?))))
114 cases (current ? (nth src ? int (niltape ?))) in ⊢ (???%→?);
115 [ #Hcur_src %{2} %
116   [| % [ %
117     [ whd in ⊢ (??%?); >copy_q0_q2_null /2/ 
118     | normalize in ⊢ (%→?); #H destruct (H) ]
119   | #_ % // % // ] ]
120 | #a #Ha lapply (refl ? (current ? (nth dst ? int (niltape ?))))
121   cases (current ? (nth dst ? int (niltape ?))) in ⊢ (???%→?);
122   [ #Hcur_dst %{2} %
123     [| % [ %
124        [ whd in ⊢ (??%?); >copy_q0_q2_null /2/ 
125        | normalize in ⊢ (%→?); #H destruct (H) ]
126        | #_ % // %2 >Hcur_dst % ] ]
127   | #b #Hb %{2} %
128     [| % [ % 
129       [whd in ⊢  (??%?);  >(copy_q0_q1 … a b Hneq Hsrc Hdst) //
130       | #_ %{a} %{b} % // % //]
131       | * #H @False_ind @H %
132       ]
133     ]
134   ]
135 ]
136 qed.
137
138 definition copy ≝ λsrc,dst,sig,n.
139   whileTM … (copy_step src dst sig n) copy1.
140
141 definition R_copy ≝ 
142   λsrc,dst,sig,n.λint,outt: Vector (tape sig) (S n).
143   ((current ? (nth src ? int (niltape ?)) = None ? ∨
144     current ? (nth dst ? int (niltape ?)) = None ?) → outt = int) ∧
145   (∀ls,x,x0,rs,ls0,rs0. 
146     nth src ? int (niltape ?) = midtape sig ls x rs →
147     nth dst ? int (niltape ?) = midtape sig ls0 x0 rs0 →
148     (∃rs01,rs02.rs0 = rs01@rs02 ∧ |rs01| = |rs| ∧
149      outt = change_vec ?? 
150             (change_vec ?? int  
151               (mk_tape sig (reverse sig rs@x::ls) (None sig) []) src)
152             (mk_tape sig (reverse sig rs@x::ls0) (option_hd sig rs02)
153             (tail sig rs02)) dst) ∨
154     (∃rs1,rs2.rs = rs1@rs2 ∧ |rs1| = |rs0| ∧
155      outt = change_vec ?? 
156             (change_vec ?? int  
157               (mk_tape sig (reverse sig rs1@x::ls) (option_hd sig rs2)
158             (tail sig rs2)) src)
159             (mk_tape sig (reverse sig rs1@x::ls0) (None sig) []) dst)).
160           
161 axiom daemon : ∀P:Prop.P.
162
163 lemma wsem_copy : ∀src,dst,sig,n.src ≠ dst → src < S n → dst < S n → 
164   copy src dst sig n ⊫ R_copy src dst sig n.
165 #src #dst #sig #n #Hneq #Hsrc #Hdst #ta #k #outc #Hloop
166 lapply (sem_while … (sem_copy_step src dst sig n Hneq Hsrc Hdst) … Hloop) //
167 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
168 [ whd in ⊢ (%→?); * #Hnone #Hout %
169   [#_ @Hout
170   |#ls #x #x0 #rs #ls0 #rs0 #Hsrc1 #Hdst1 @False_ind cases Hnone
171     [>Hsrc1 normalize #H destruct (H) | >Hdst1 normalize #H destruct (H)]
172   ]
173 |#tc #td * #x * #y * * #Hcx #Hcy #Htd #Hstar #IH #He lapply (IH He) -IH *
174  #IH1 #IH2 %
175   [* [>Hcx #H destruct (H) | >Hcy #H destruct (H)]
176   |#ls #x' #y' #rs #ls0 #rs0 #Hnth_src #Hnth_dst
177    >Hnth_src in Hcx; whd in ⊢ (??%?→?); #H destruct (H)
178    >Hnth_dst in Hcy; whd in ⊢ (??%?→?); #H destruct (H)
179    >Hnth_src in Htd; >Hnth_dst -Hnth_src -Hnth_dst
180    cases rs
181     [(* the source tape is empty after the move *)
182      lapply (IH1 ?) [@daemon]
183      #Hout (* whd in match (tape_move ???); *) #Htemp %1 %{([])} %{rs0} % 
184       [% [// | // ] 
185       |whd in match (reverse ??); whd in match (reverse ??);
186        >Hout >Htemp @eq_f2 // cases rs0 //
187       ]
188     |#c1 #tl1 cases rs0
189       [(* the dst tape is empty after the move *)
190        lapply (IH1 ?) [@daemon]
191        #Hout (* whd in match (tape_move ???); *) #Htemp %2 %{[ ]} %{(c1::tl1)} % 
192         [% [// | // ] 
193         |whd in match (reverse ??); whd in match (reverse ??);
194          >Hout >Htemp @eq_f2 // 
195         ]
196       |#c2 #tl2 whd in match (tape_move_mono ???); whd in match (tape_move_mono ???);
197        #Htd
198     
199
200   
201   [ >Hci >Hcj * [ * 
202     [ * #H @False_ind @H % | #H destruct (H)] | #H destruct (H)] 
203   | #ls #c0 #rs #ls0 #rs0 cases rs
204     [ -IH2 #Hnthi #Hnthj % %2 %{rs0} % [%]
205       >Hnthi in Hd; #Hd >Hd in IH1; #IH1 >IH1
206       [| % %2 >nth_change_vec_neq [|@sym_not_eq //] >nth_change_vec // % ]
207       >Hnthj cases rs0 [| #r1 #rs1 ] %
208     | #r1 #rs1 #Hnthi cases rs0
209       [ -IH2 #Hnthj % % %{(r1::rs1)} % [%]
210         >Hnthj in Hd; #Hd >Hd in IH1; #IH1 >IH1
211         [| %2 >nth_change_vec // ]
212         >nth_change_vec //
213       | #r2 #rs2 #Hnthj lapply IH2; >Hd in IH1; >Hnthi >Hnthj
214         >nth_change_vec //
215         >nth_change_vec_neq [| @sym_not_eq // ] >nth_change_vec //
216         cases (true_or_false (r1 == r2)) #Hr1r2
217         [ >(\P Hr1r2) #_ #IH2 cases (IH2 … (refl ??) (refl ??)) [ *
218           [ * #rs' * #Hrs1 #Hcurout_j % % %{rs'}
219             >Hrs1 >Hcurout_j normalize % //
220           | * #rs0' * #Hrs2 #Hcurout_i % %2 %{rs0'}
221             >Hrs2 >Hcurout_i % //
222             >change_vec_commute // >change_vec_change_vec
223             >change_vec_commute [|@sym_not_eq//] >change_vec_change_vec
224             >reverse_cons >associative_append >associative_append % ]
225           | * #xs * #ci * #cj * #rs' * #rs0' * * * #Hcicj #Hrs1 #Hrs2 
226             >change_vec_commute // >change_vec_change_vec 
227             >change_vec_commute [| @sym_not_eq ] // >change_vec_change_vec 
228             #Houtc %2 %{(r2::xs)} %{ci} %{cj} %{rs'} %{rs0'}
229             % [ % [ % [ // | >Hrs1 // ] | >Hrs2 // ] 
230               | >reverse_cons >associative_append >associative_append >Houtc % ] ]
231         | lapply (\Pf Hr1r2) -Hr1r2 #Hr1r2 #IH1 #_ %2
232           >IH1 [| % % normalize @(not_to_not … Hr1r2) #H destruct (H) % ]
233           %{[]} %{r1} %{r2} %{rs1} %{rs2} % [ % [ % /2/ | % ] | % ] ]]]]]
234 qed.
235  
236 lemma terminate_compare :  ∀i,j,sig,n,t.
237   i ≠ j → i < S n → j < S n → 
238   compare i j sig n ↓ t.
239 #i #j #sig #n #t #Hneq #Hi #Hj
240 @(terminate_while … (sem_comp_step …)) //
241 <(change_vec_same … t i (niltape ?))
242 cases (nth i (tape sig) t (niltape ?))
243 [ % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
244 |2,3: #a0 #al0 % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
245 | #ls #c #rs lapply c -c lapply ls -ls lapply t -t elim rs
246   [#t #ls #c % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?);
247    #H1 destruct (H1) #_ >change_vec_change_vec #Ht1 % 
248    #t2 * #x0 * * >Ht1 >nth_change_vec_neq [|@sym_not_eq //]
249    >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H)
250   |#r0 #rs0 #IH #t #ls #c % #t1 * #x * * >nth_change_vec //
251    normalize in ⊢ (%→?); #H destruct (H) #Hcur
252    >change_vec_change_vec >change_vec_commute // #Ht1 >Ht1 @IH
253   ]
254 ]
255 qed.
256
257 lemma sem_compare : ∀i,j,sig,n.
258   i ≠ j → i < S n → j < S n → 
259   compare i j sig n ⊨ R_compare i j sig n.
260 #i #j #sig #n #Hneq #Hi #Hj @WRealize_to_Realize 
261   [/2/| @wsem_compare // ]
262 qed.