]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/multi_universal/copy.ma
9e1d1e365e98104b795fab2b90bf6acd1cdb3783
[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 lemma wsem_copy : ∀src,dst,sig,n.src ≠ dst → src < S n → dst < S n → 
162   copy src dst sig n ⊫ R_copy src dst sig n.
163 #src #dst #sig #n #Hneq #Hsrc #Hdst #ta #k #outc #Hloop
164 lapply (sem_while … (sem_copy_step src dst sig n Hneq Hsrc Hdst) … Hloop) //
165 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
166 [ whd in ⊢ (%→?); * #Hnone #Hout %
167   [#_ @Hout
168   |#ls #x #x0 #rs #ls0 #rs0 #Hsrc1 #Hdst1 @False_ind cases Hnone
169     [>Hsrc1 normalize #H destruct (H) | >Hdst1 normalize #H destruct (H)]
170   ]
171 |#tc #td * #x * #y * * #Hcx #Hcy #Htd #Hstar #IH #He lapply (IH He) -IH *
172  #IH1 #IH2 %
173   [* [>Hcx #H destruct (H) | >Hcy #H destruct (H)]
174   |#ls #x' #y' #rs #ls0 #rs0 #Hnth_src #Hnth_dst
175    >Hnth_src in Hcx; whd in ⊢ (??%?→?); #H destruct (H)
176    >Hnth_dst in Hcy; whd in ⊢ (??%?→?); #H destruct (H)
177    >Hnth_src in Htd; >Hnth_dst -Hnth_src -Hnth_dst
178    cases rs
179     [(* the source tape is empty after the move *)
180      #Htd lapply (IH1 ?) 
181       [%1 >Htd >nth_change_vec_neq [2:@(not_to_not … Hneq) //] >nth_change_vec //]
182      #Hout (* whd in match (tape_move ???); *) %1 %{([])} %{rs0} % 
183       [% [// | // ] 
184       |whd in match (reverse ??); whd in match (reverse ??);
185        >Hout >Htd @eq_f2 // cases rs0 //
186       ]
187     |#c1 #tl1 cases rs0
188       [(* the dst tape is empty after the move *)
189        #Htd lapply (IH1 ?) [%2 >Htd >nth_change_vec //] 
190        #Hout (* whd in match (tape_move ???); *) %2 %{[ ]} %{(c1::tl1)} % 
191         [% [// | // ] 
192         |whd in match (reverse ??); whd in match (reverse ??);
193          >Hout >Htd @eq_f2 // 
194         ]
195       |#c2 #tl2 whd in match (tape_move_mono ???); whd in match (tape_move_mono ???);
196        #Htd
197        cut (nth src (tape sig) td (niltape sig)=midtape sig (x::ls) c1 tl1)
198          [>Htd >nth_change_vec_neq [2:@(not_to_not … Hneq) //] @nth_change_vec //]
199        #Hsrc_td
200        cut (nth dst (tape sig) td (niltape sig)=midtape sig (x::ls0) c2 tl2)
201          [>Htd @nth_change_vec //]
202        #Hdst_td cases (IH2 … Hsrc_td Hdst_td) -Hsrc_td -Hdst_td
203         [* #rs01 * #rs02 * * #H1 #H2 #H3 %1
204          %{(c2::rs01)} %{rs02} % [% [@eq_f //|normalize @eq_f @H2]]
205          >Htd in H3; >change_vec_commute // >change_vec_change_vec
206          >change_vec_commute [2:@(not_to_not … Hneq) //] >change_vec_change_vec 
207          #H >reverse_cons >associative_append >associative_append @H 
208         |* #rs11 * #rs12 * * #H1 #H2 #H3 %2
209          %{(c1::rs11)} %{rs12} % [% [@eq_f //|normalize @eq_f @H2]]
210          >Htd in H3; >change_vec_commute // >change_vec_change_vec
211          >change_vec_commute [2:@(not_to_not … Hneq) //] >change_vec_change_vec 
212          #H >reverse_cons >associative_append >associative_append @H 
213         ]
214       ]
215     ]
216   ]
217 qed.
218      
219  
220 lemma terminate_copy :  ∀src,dst,sig,n,t.
221   src ≠ dst → src < S n → dst < S n → copy src dst sig n ↓ t.
222 #src #dst #sig #n #t #Hneq #Hsrc #Hdts
223 @(terminate_while … (sem_copy_step …)) //
224 <(change_vec_same … t src (niltape ?))
225 cases (nth src (tape sig) t (niltape ?))
226 [ % #t1 * #x * #y * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
227 |2,3: #a0 #al0 % #t1 * #x * #y * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
228 | #ls #c #rs lapply c -c lapply ls -ls lapply t -t elim rs
229   [#t #ls #c % #t1 * #x * #y * * >nth_change_vec // normalize in ⊢ (%→?);
230    #H1 destruct (H1) #_ >change_vec_change_vec #Ht1 % 
231    #t2 * #x0 * #y0 * * >Ht1 >nth_change_vec_neq [|@sym_not_eq //]
232    >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H)
233   |#r0 #rs0 #IH #t #ls #c % #t1 * #x * #y * * >nth_change_vec //
234    normalize in ⊢ (%→?); #H destruct (H) #Hcur
235    >change_vec_change_vec >change_vec_commute // #Ht1 >Ht1 @IH
236   ]
237 ]
238 qed.
239
240 lemma sem_copy : ∀src,dst,sig,n.
241   src ≠ dst → src < S n → dst < S n → 
242   copy src dst sig n ⊨ R_copy src dst sig n.
243 #i #j #sig #n #Hneq #Hi #Hj @WRealize_to_Realize [/2/| @wsem_copy // ]
244 qed.