]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/multi_universal/compare.ma
232a4962b03571a77edb9fca6c7012c6ee9e0ed7
[helm.git] / matita / matita / lib / turing / multi_universal / compare.ma
1 (**************************************************************************)
2 (*       ___                                                              *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||         The HELM team.                                      *)
8 (*      ||A||         http://helm.cs.unibo.it                             *)
9 (*      \   /                                                             *)
10 (*       \ /        This file is distributed under the terms of the       *)
11 (*        v         GNU General Public License Version 2                  *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 include "turing/multi_universal/moves.ma".
16 include "turing/if_multi.ma".
17 include "turing/inject.ma".
18 include "turing/basic_machines.ma".
19
20 definition compare_states ≝ initN 3.
21
22 definition comp0 : compare_states ≝ mk_Sig ?? 0 (leb_true_to_le 1 3 (refl …)).
23 definition comp1 : compare_states ≝ mk_Sig ?? 1 (leb_true_to_le 2 3 (refl …)).
24 definition comp2 : compare_states ≝ mk_Sig ?? 2 (leb_true_to_le 3 3 (refl …)).
25
26 (*
27
28 0) (x,x) → (x,x)(R,R) → 1
29    (x,y≠x) → None 2
30 1) (_,_) → None 1
31 2) (_,_) → None 2
32
33 *)
34
35 definition trans_compare_step ≝ 
36  λi,j.λsig:FinSet.λn.λis_endc.
37  λp:compare_states × (Vector (option sig) (S n)).
38  let 〈q,a〉 ≝ p in
39  match pi1 … q with
40  [ O ⇒ match nth i ? a (None ?) with
41    [ None ⇒ 〈comp2,null_action ? n〉
42    | Some ai ⇒ match nth j ? a (None ?) with 
43      [ None ⇒ 〈comp2,null_action ? n〉
44      | Some aj ⇒ if notb (is_endc ai) ∧ ai == aj 
45          then 〈comp1,change_vec ? (S n) 
46                       (change_vec ? (S n) (null_action ? n) (Some ? 〈ai,R〉) i)
47                         (Some ? 〈aj,R〉) j〉
48          else 〈comp2,null_action ? n〉 ]
49    ]
50  | S q ⇒ match q with 
51    [ O ⇒ (* 1 *) 〈comp1,null_action ? n〉
52    | S _ ⇒ (* 2 *) 〈comp2,null_action ? n〉 ] ].
53
54 definition compare_step ≝ 
55   λi,j,sig,n,is_endc.
56   mk_mTM sig n compare_states (trans_compare_step i j sig n is_endc) 
57     comp0 (λq.q == comp1 ∨ q == comp2).
58
59 definition R_comp_step_true ≝ 
60   λi,j,sig,n,is_endc.λint,outt: Vector (tape sig) (S n).
61   ∃x.
62    is_endc x = false ∧
63    current ? (nth i ? int (niltape ?)) = Some ? x ∧
64    current ? (nth j ? int (niltape ?)) = Some ? x ∧
65    outt = change_vec ?? 
66             (change_vec ?? int
67               (tape_move ? (nth i ? int (niltape ?)) (Some ? 〈x,R〉)) i)
68             (tape_move ? (nth j ? int (niltape ?)) (Some ? 〈x,R〉)) j.
69
70 definition R_comp_step_false ≝ 
71   λi,j:nat.λsig,n,is_endc.λint,outt: Vector (tape sig) (S n).
72    ((∃x.current ? (nth i ? int (niltape ?)) = Some ? x ∧ is_endc x = true) ∨
73    current ? (nth i ? int (niltape ?)) ≠ current ? (nth j ? int (niltape ?)) ∨
74    current ? (nth i ? int (niltape ?)) = None ? ∨
75    current ? (nth j ? int (niltape ?)) = None ?) ∧ outt = int.
76
77 lemma comp_q0_q2_null :
78   ∀i,j,sig,n,is_endc,v.i < S n → j < S n → 
79   (nth i ? (current_chars ?? v) (None ?) = None ? ∨
80    nth j ? (current_chars ?? v) (None ?) = None ?) → 
81   step sig n (compare_step i j sig n is_endc) (mk_mconfig ??? comp0 v) 
82   = mk_mconfig ??? comp2 v.
83 #i #j #sig #n #is_endc #v #Hi #Hj
84 whd in ⊢ (? → ??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (?→??%?);
85 * #Hcurrent
86 [ @eq_f2
87   [ whd in ⊢ (??(???%)?); >Hcurrent %
88   | whd in ⊢ (??(???????(???%))?); >Hcurrent @tape_move_null_action ]
89 | @eq_f2
90   [ whd in ⊢ (??(???%)?); >Hcurrent cases (nth i ?? (None sig)) //
91   | whd in ⊢ (??(???????(???%))?); >Hcurrent
92     cases (nth i ?? (None sig)) [|#x] @tape_move_null_action ] ]
93 qed.
94
95 lemma comp_q0_q2_neq :
96   ∀i,j,sig,n,is_endc,v.i < S n → j < S n → 
97   ((∃x.nth i ? (current_chars ?? v) (None ?) = Some ? x ∧ is_endc x = true) ∨ 
98     nth i ? (current_chars ?? v) (None ?) ≠ nth j ? (current_chars ?? v) (None ?)) → 
99   step sig n (compare_step i j sig n is_endc) (mk_mconfig ??? comp0 v) 
100   = mk_mconfig ??? comp2 v.
101 #i #j #sig #n #is_endc #v #Hi #Hj lapply (refl ? (nth i ?(current_chars ?? v)(None ?)))
102 cases (nth i ?? (None ?)) in ⊢ (???%→?);
103 [ #Hnth #_ @comp_q0_q2_null // % //
104 | #ai #Hai lapply (refl ? (nth j ?(current_chars ?? v)(None ?)))
105   cases (nth j ?? (None ?)) in ⊢ (???%→?);
106   [ #Hnth #_ @comp_q0_q2_null // %2 //
107   | #aj #Haj *
108     [ * #c * >Hai #Heq #Hendc whd in ⊢ (??%?); 
109       >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
110       [ whd in match (trans ????); >Hai >Haj destruct (Heq) 
111         whd in ⊢ (??(???%)?); >Hendc // 
112       | whd in match (trans ????); >Hai >Haj destruct (Heq) 
113         whd in ⊢ (??(???????(???%))?); >Hendc @tape_move_null_action
114       ]
115     | #Hneq
116       whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
117       [ whd in match (trans ????); >Hai >Haj
118         whd in ⊢ (??(???%)?); cut ((¬is_endc ai∧ai==aj)=false)
119         [>(\bf ?) /2 by not_to_not/ cases (is_endc ai) // |#Hcut >Hcut //]
120         | whd in match (trans ????); >Hai >Haj
121           whd in ⊢ (??(???????(???%))?); cut ((¬is_endc ai∧ai==aj)=false)
122           [>(\bf ?) /2 by not_to_not/ cases (is_endc ai) // 
123           |#Hcut >Hcut @tape_move_null_action
124           ]
125         ]
126       ]
127     ]
128 ]
129 qed.
130
131 lemma comp_q0_q1 :
132   ∀i,j,sig,n,is_endc,v,a.i ≠ j → i < S n → j < S n → 
133   nth i ? (current_chars ?? v) (None ?) = Some ? a → is_endc a = false →
134   nth j ? (current_chars ?? v) (None ?) = Some ? a → 
135   step sig n (compare_step i j sig n is_endc) (mk_mconfig ??? comp0 v) =
136     mk_mconfig ??? comp1 
137      (change_vec ? (S n) 
138        (change_vec ?? v
139          (tape_move ? (nth i ? v (niltape ?)) (Some ? 〈a,R〉)) i)
140        (tape_move ? (nth j ? v (niltape ?)) (Some ? 〈a,R〉)) j).
141 #i #j #sig #n #is_endc #v #a #Heq #Hi #Hj #Ha1 #Hnotendc #Ha2
142 whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
143 [ whd in match (trans ????);
144   >Ha1 >Ha2 whd in ⊢ (??(???%)?); >Hnotendc >(\b ?) //
145 | whd in match (trans ????);
146   >Ha1 >Ha2 whd in ⊢ (??(???????(???%))?); >Hnotendc >(\b ?) //
147   change with (change_vec ?????) in ⊢ (??(???????%)?);
148   <(change_vec_same … v j (niltape ?)) in ⊢ (??%?);
149   <(change_vec_same … v i (niltape ?)) in ⊢ (??%?);
150   >pmap_change >pmap_change >tape_move_null_action
151   @eq_f2 // @eq_f2 // >nth_change_vec_neq //
152 ]
153 qed.
154
155 lemma sem_comp_step :
156   ∀i,j,sig,n,is_endc.i ≠ j → i < S n → j < S n → 
157   compare_step i j sig n is_endc ⊨ 
158     [ comp1: R_comp_step_true i j sig n is_endc, 
159              R_comp_step_false i j sig n is_endc ].
160 #i #j #sig #n #is_endc #Hneq #Hi #Hj #int
161 lapply (refl ? (current ? (nth i ? int (niltape ?))))
162 cases (current ? (nth i ? int (niltape ?))) in ⊢ (???%→?);
163 [ #Hcuri %{2} %
164   [| % [ %
165     [ whd in ⊢ (??%?); >comp_q0_q2_null /2/ % <Hcuri in ⊢ (???%); 
166       @sym_eq @nth_vec_map
167     | normalize in ⊢ (%→?); #H destruct (H) ]
168   | #_ % // % %2 // ] ]
169 | #a #Ha lapply (refl ? (current ? (nth j ? int (niltape ?))))
170   cases (current ? (nth j ? int (niltape ?))) in ⊢ (???%→?);
171   [ #Hcurj %{2} %
172     [| % [ %
173        [ whd in ⊢ (??%?); >comp_q0_q2_null /2/ %2 <Hcurj in ⊢ (???%); 
174          @sym_eq @nth_vec_map
175        | normalize in ⊢ (%→?); #H destruct (H) ]
176        | #_ % // >Ha >Hcurj % % %2 % #H destruct (H) ] ]
177   | #b #Hb %{2} 
178    cases (true_or_false (is_endc a)) #Haendc
179     [ %
180       [| % [ % 
181         [whd in ⊢  (??%?);  >comp_q0_q2_neq //
182          % %{a} % // <Ha @sym_eq @nth_vec_map
183         | normalize in ⊢ (%→?); #H destruct (H) ]
184       | #_ % // % % % >Ha %{a} % // ]
185       ]
186     |cases (true_or_false (a == b)) #Hab
187       [ %
188         [| % [ % 
189           [whd in ⊢  (??%?);  >(comp_q0_q1 … a Hneq Hi Hj) //
190             [>(\P Hab) <Hb @sym_eq @nth_vec_map
191             |<Ha @sym_eq @nth_vec_map ]
192           | #_ whd >(\P Hab) %{b} % // % // <(\P Hab) % // ]
193           | * #H @False_ind @H %
194         ] ]
195       | %
196         [| % [ % 
197           [whd in ⊢  (??%?);  >comp_q0_q2_neq //
198            <(nth_vec_map ?? (current …) i ? int (niltape ?))
199            <(nth_vec_map ?? (current …) j ? int (niltape ?)) %2 >Ha >Hb
200            @(not_to_not ??? (\Pf Hab)) #H destruct (H) %
201           | normalize in ⊢ (%→?); #H destruct (H) ]
202         | #_ % // % % %2 >Ha >Hb @(not_to_not ??? (\Pf Hab)) #H destruct (H) % ] ]
203       ]
204     ]
205   ]
206 ]
207 qed.
208
209 definition compare ≝ λi,j,sig,n,is_endc.
210   whileTM … (compare_step i j sig n is_endc) comp1.
211
212 definition R_compare ≝ 
213   λi,j,sig,n,is_endc.λint,outt: Vector (tape sig) (S n).
214   ((∃x.current ? (nth i ? int (niltape ?)) = Some ? x ∧ is_endc x = true) ∨
215    (current ? (nth i ? int (niltape ?)) ≠ current ? (nth j ? int (niltape ?)) ∨
216     current ? (nth i ? int (niltape ?)) = None ? ∨
217     current ? (nth j ? int (niltape ?)) = None ?) → outt = int) ∧
218   (∀ls,x,xs,ci,rs,ls0,rs0. 
219     nth i ? int (niltape ?) = midtape sig ls x (xs@ci::rs) →
220     nth j ? int (niltape ?) = midtape sig ls0 x (xs@rs0) →
221     (∀c0. memb ? c0 (x::xs) = true → is_endc c0 = false) → 
222     (rs0 = [ ] ∧
223      outt = change_vec ?? 
224            (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs) i)
225            (mk_tape sig (reverse ? xs@x::ls0) (None ?) []) j) ∨
226     ∃cj,rs1.rs0 = cj::rs1 ∧
227     ((is_endc ci = true ∨ ci ≠ cj) → 
228     outt = change_vec ?? 
229            (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs) i)
230            (midtape sig (reverse ? xs@x::ls0) cj rs1) j)).
231           
232 lemma wsem_compare : ∀i,j,sig,n,is_endc.i ≠ j → i < S n → j < S n → 
233   compare i j sig n is_endc ⊫ R_compare i j sig n is_endc.
234 #i #j #sig #n #is_endc #Hneq #Hi #Hj #ta #k #outc #Hloop
235 lapply (sem_while … (sem_comp_step i j sig n is_endc Hneq Hi Hj) … Hloop) //
236 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
237 [ #tc whd in ⊢ (%→?); * * [ * [ *
238   [* #curi * #Hcuri #Hendi #Houtc %
239     [ #_ @Houtc  
240     | #ls #x #xs #ci #rs #ls0 #rs0 #Hnthi #Hnthj #Hnotendc 
241       @False_ind
242       >Hnthi in Hcuri; normalize in ⊢ (%→?); #H destruct (H)
243       >(Hnotendc ? (memb_hd … )) in Hendi; #H destruct (H)
244     ]
245   |#Hcicj #Houtc % 
246     [ #_ @Houtc
247     | #ls #x #xs #ci #rs #ls0 #rs0 #Hnthi #Hnthj
248       >Hnthi in Hcicj; >Hnthj normalize in ⊢ (%→?); * #H @False_ind @H %
249     ]]
250   | #Hci #Houtc %
251     [ #_ @Houtc
252     | #ls #x #xs #ci #rs #ls0 #rs0 #Hnthi >Hnthi in Hci;
253       normalize in ⊢ (%→?); #H destruct (H) ] ]
254   | #Hcj #Houtc %
255     [ #_ @Houtc
256     | #ls #x #xs #ci #rs #ls0 #rs0 #_ #Hnthj >Hnthj in Hcj;
257       normalize in ⊢ (%→?); #H destruct (H) ] ]
258   | #tc #td #te * #x * * * #Hendcx #Hci #Hcj #Hd #Hstar #IH #He lapply (IH He) -IH *
259     #IH1 #IH2 %
260     [ >Hci >Hcj * [* #x0 * #H destruct (H) >Hendcx #H destruct (H) 
261     |* [* #H @False_ind [cases H -H #H @H % | destruct (H)] | #H destruct (H)]] 
262     | #ls #c0 #xs #ci #rs #ls0 #rs0 cases xs
263       [ #Hnthi #Hnthj #Hnotendc cases rs0 in Hnthj;
264         [ #Hnthj % % // >IH1
265           [ >Hd @eq_f3 //
266             [ @eq_f3 // >(?:c0=x) [ >Hnthi % ]
267               >Hnthi in Hci;normalize #H destruct (H) %
268             | >(?:c0=x) [ >Hnthj % ]
269               >Hnthi in Hci;normalize #H destruct (H) % ]
270           | >Hd %2 %2 >nth_change_vec // >Hnthj % ]
271         | #r1 #rs1 #Hnthj %2 %{r1} %{rs1} % // *
272           [ #Hendci >IH1
273             [ >Hd @eq_f3 // 
274               [ @eq_f3 // >(?:c0=x) [ >Hnthi % ]
275              >Hnthi in Hci;normalize #H destruct (H) %
276             | >(?:c0=x) [ >Hnthj % ]
277             >Hnthi in Hci;normalize #H destruct (H) % ]
278         | >Hd >nth_change_vec // >nth_change_vec_neq [|@sym_not_eq //]
279           >nth_change_vec // >Hnthi >Hnthj normalize % %{ci} % //
280         ]
281       |#Hcir1 >IH1
282         [>Hd @eq_f3 // 
283           [ @eq_f3 // >(?:c0=x) [ >Hnthi % ]
284             >Hnthi in Hci;normalize #H destruct (H) %
285           | >(?:c0=x) [ >Hnthj % ]
286             >Hnthi in Hci;normalize #H destruct (H) % ]
287         | >Hd %2 % % >nth_change_vec //
288           >nth_change_vec_neq [|@sym_not_eq //]
289           >nth_change_vec // >Hnthi >Hnthj normalize @(not_to_not … Hcir1)
290           #H destruct (H) % ]
291       ]
292     ]
293   |#x0 #xs0 #Hnthi #Hnthj #Hnotendc 
294    cut (c0 = x) [ >Hnthi in Hci; normalize #H destruct (H) // ]
295    #Hcut destruct (Hcut) cases rs0 in Hnthj;
296     [ #Hnthj % % // 
297       cases (IH2 (x::ls) x0 xs0 ci rs (x::ls0) [ ] ???) -IH2
298       [ * #_ #IH2 >IH2 >Hd >change_vec_commute in ⊢ (??%?); //
299         >change_vec_change_vec >change_vec_commute in ⊢ (??%?); //
300         @sym_not_eq //
301       | * #cj * #rs1 * #H destruct (H)
302       | >Hd >nth_change_vec_neq [|@sym_not_eq //] >nth_change_vec //
303         >Hnthi %
304       | >Hd >nth_change_vec // >Hnthj %
305       | #c0 #Hc0 @Hnotendc @memb_cons @Hc0 ]
306     | #r1 #rs1 #Hnthj %2 %{r1} %{rs1} % // #Hcir1
307       cases(IH2 (x::ls) x0 xs0 ci rs (x::ls0) (r1::rs1) ???)
308       [ * #H destruct (H)
309       | * #r1' * #rs1' * #H destruct (H) #Hc1r1 >Hc1r1 //
310         >Hd >change_vec_commute in ⊢ (??%?); //
311         >change_vec_change_vec >change_vec_commute in ⊢ (??%?); //
312           @sym_not_eq //
313       | >Hd >nth_change_vec_neq [|@sym_not_eq //] >nth_change_vec //
314         >Hnthi //
315       | >Hd >nth_change_vec // >Hnthi >Hnthj %
316       | #c0 #Hc0 @Hnotendc @memb_cons @Hc0
317 ]]]]]
318 qed.      
319  
320 lemma terminate_compare :  ∀i,j,sig,n,is_endc,t.
321   i ≠ j → i < S n → j < S n → 
322   compare i j sig n is_endc ↓ t.
323 #i #j #sig #n #is_endc #t #Hneq #Hi #Hj
324 @(terminate_while … (sem_comp_step …)) //
325 <(change_vec_same … t i (niltape ?))
326 cases (nth i (tape sig) t (niltape ?))
327 [ % #t1 * #x * * * #_ >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct 
328 |2,3: #a0 #al0 % #t1 * #x * * * #_ >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
329 | #ls #c #rs lapply c -c lapply ls -ls lapply t -t elim rs
330   [#t #ls #c % #t1 * #x * * * #Hendcx >nth_change_vec // normalize in ⊢ (%→?);
331    #H1 destruct (H1) #Hxsep >change_vec_change_vec #Ht1 % 
332    #t2 * #x0 * * * #Hendcx0 >Ht1 >nth_change_vec_neq [|@sym_not_eq //]
333    >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H)
334   |#r0 #rs0 #IH #t #ls #c % #t1 * #x * * >nth_change_vec //
335    normalize in ⊢ (%→?); #H destruct (H) #Hcur
336    >change_vec_change_vec >change_vec_commute // #Ht1 >Ht1 @IH
337   ]
338 ]
339 qed.
340
341 lemma sem_compare : ∀i,j,sig,n,is_endc.
342   i ≠ j → i < S n → j < S n → 
343   compare i j sig n is_endc ⊨ R_compare i j sig n is_endc.
344 #i #j #sig #n #is_endc #Hneq #Hi #Hj @WRealize_to_Realize /2/
345 qed.