]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/multi_universal/compare.ma
9309156862befaa1c13832198f86fdfa1f59e49b
[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.
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 sig n〉
42    | Some ai ⇒ match nth j ? a (None ?) with 
43      [ None ⇒ 〈comp2,null_action ? n〉
44      | Some aj ⇒ if ai == aj 
45          then 〈comp1,change_vec ? (S n) 
46                       (change_vec ? (S n) (null_action ? n) (〈None ?,R〉) i)
47                         (〈None ?,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.
56   mk_mTM sig n compare_states (trans_compare_step i j sig n) 
57     comp0 (λq.q == comp1 ∨ q == comp2).
58
59 definition R_comp_step_true ≝ 
60   λi,j,sig,n.λint,outt: Vector (tape sig) (S n).
61   ∃x.
62    current ? (nth i ? int (niltape ?)) = Some ? x ∧
63    current ? (nth j ? int (niltape ?)) = Some ? x ∧
64    outt = change_vec ?? 
65             (change_vec ?? int
66               (tape_move_right ? (nth i ? int (niltape ?))) i)
67             (tape_move_right ? (nth j ? int (niltape ?))) j.
68
69 definition R_comp_step_false ≝ 
70   λi,j:nat.λsig,n.λint,outt: Vector (tape sig) (S n).
71    (current ? (nth i ? int (niltape ?)) ≠ current ? (nth j ? int (niltape ?)) ∨
72     current ? (nth i ? int (niltape ?)) = None ? ∨
73     current ? (nth j ? int (niltape ?)) = None ?) ∧ outt = int.
74
75 lemma comp_q0_q2_null :
76   ∀i,j,sig,n,v.i < S n → j < S n → 
77   (nth i ? (current_chars ?? v) (None ?) = None ? ∨
78    nth j ? (current_chars ?? v) (None ?) = None ?) → 
79   step sig n (compare_step i j sig n) (mk_mconfig ??? comp0 v) 
80   = mk_mconfig ??? comp2 v.
81 #i #j #sig #n #v #Hi #Hj
82 whd in ⊢ (? → ??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (?→??%?);
83 * #Hcurrent
84 [ @eq_f2
85   [ whd in ⊢ (??(???%)?); >Hcurrent %
86   | whd in ⊢ (??(????(???%))?); >Hcurrent @tape_move_null_action ]
87 | @eq_f2
88   [ whd in ⊢ (??(???%)?); >Hcurrent cases (nth i ?? (None sig)) //
89   | whd in ⊢ (??(????(???%))?); >Hcurrent
90     cases (nth i ?? (None sig)) [|#x] @tape_move_null_action ] ]
91 qed.
92
93 lemma comp_q0_q2_neq :
94   ∀i,j,sig,n,v.i < S n → j < S n → 
95   (nth i ? (current_chars ?? v) (None ?) ≠ nth j ? (current_chars ?? v) (None ?)) → 
96   step sig n (compare_step i j sig n) (mk_mconfig ??? comp0 v) 
97   = mk_mconfig ??? comp2 v.
98 #i #j #sig #n #v #Hi #Hj lapply (refl ? (nth i ?(current_chars ?? v)(None ?)))
99 cases (nth i ?? (None ?)) in ⊢ (???%→?);
100 [ #Hnth #_ @comp_q0_q2_null // % //
101 | #ai #Hai lapply (refl ? (nth j ?(current_chars ?? v)(None ?)))
102   cases (nth j ?? (None ?)) in ⊢ (???%→?);
103   [ #Hnth #_ @comp_q0_q2_null // %2 //
104   | #aj #Haj * #Hneq
105     whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
106     [ whd in match (trans ????); >Hai >Haj
107       whd in ⊢ (??(???%)?); cut ((ai==aj)=false)
108       [>(\bf ?) /2 by not_to_not/ % #Haiaj @Hneq
109        >Hai >Haj //
110       | #Haiaj >Haiaj % ]
111     | whd in match (trans ????); >Hai >Haj
112       whd in ⊢ (??(????(???%))?); cut ((ai==aj)=false)
113       [>(\bf ?) /2 by not_to_not/ % #Haiaj @Hneq
114        >Hai >Haj //
115       |#Hcut >Hcut @tape_move_null_action
116       ]
117     ]
118   ]
119 ]
120 qed.
121
122 axiom comp_q0_q1 :
123   ∀i,j,sig,n,v,a.i ≠ j → i < S n → j < S n → 
124   nth i ? (current_chars ?? v) (None ?) = Some ? a →
125   nth j ? (current_chars ?? v) (None ?) = Some ? a → 
126   step sig n (compare_step i j sig n) (mk_mconfig ??? comp0 v) =
127     mk_mconfig ??? comp1 
128      (change_vec ? (S n) 
129        (change_vec ?? v
130          (tape_move_right ? (nth i ? v (niltape ?))) i)
131        (tape_move_right ? (nth j ? v (niltape ?))) j).
132 (*
133 #i #j #sig #n #v #a #Heq #Hi #Hj #Ha1 #Ha2
134 whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
135 [ whd in match (trans ????);
136   >Ha1 >Ha2 whd in ⊢ (??(???%)?); >(\b ?) //
137 | whd in match (trans ????);
138   >Ha1 >Ha2 whd in ⊢ (??(????(???%))?); >(\b ?) //
139   change with (change_vec ?????) in ⊢ (??(????%)?);
140   <(change_vec_same … v j (niltape ?)) in ⊢ (??%?);
141   <(change_vec_same … v i (niltape ?)) in ⊢ (??%?);
142   >pmap_change >pmap_change >tape_move_null_action
143   @eq_f2 // @eq_f2 // >nth_change_vec_neq //
144 ]
145 qed.
146 *)
147
148 lemma sem_comp_step :
149   ∀i,j,sig,n.i ≠ j → i < S n → j < S n → 
150   compare_step i j sig n ⊨ 
151     [ comp1: R_comp_step_true i j sig n, 
152              R_comp_step_false i j sig n ].
153 #i #j #sig #n #Hneq #Hi #Hj #int
154 lapply (refl ? (current ? (nth i ? int (niltape ?))))
155 cases (current ? (nth i ? int (niltape ?))) in ⊢ (???%→?);
156 [ #Hcuri %{2} %
157   [| % [ %
158     [ whd in ⊢ (??%?); >comp_q0_q2_null /2/ % <Hcuri in ⊢ (???%); 
159       @sym_eq @nth_vec_map
160     | normalize in ⊢ (%→?); #H destruct (H) ]
161   | #_ % // % %2 // ] ]
162 | #a #Ha lapply (refl ? (current ? (nth j ? int (niltape ?))))
163   cases (current ? (nth j ? int (niltape ?))) in ⊢ (???%→?);
164   [ #Hcurj %{2} %
165     [| % [ %
166        [ whd in ⊢ (??%?); >comp_q0_q2_null /2/ %2 <Hcurj in ⊢ (???%); 
167          @sym_eq @nth_vec_map
168        | normalize in ⊢ (%→?); #H destruct (H) ]
169        | #_ % // >Ha >Hcurj % % % #H destruct (H) ] ]
170   | #b #Hb %{2} cases (true_or_false (a == b)) #Hab
171     [ %
172       [| % [ % 
173         [whd in ⊢  (??%?);  >(comp_q0_q1 … a Hneq Hi Hj) //
174           [>(\P Hab) <Hb @sym_eq @nth_vec_map
175           |<Ha @sym_eq @nth_vec_map ]
176         | #_ whd >(\P Hab) %{b} % // % // <(\P Hab) // ]
177         | * #H @False_ind @H %
178       ] ]
179     | %
180       [| % [ % 
181         [whd in ⊢  (??%?);  >comp_q0_q2_neq //
182          <(nth_vec_map ?? (current …) i ? int (niltape ?))
183          <(nth_vec_map ?? (current …) j ? int (niltape ?)) >Ha >Hb
184          @(not_to_not ??? (\Pf Hab)) #H destruct (H) %
185         | normalize in ⊢ (%→?); #H destruct (H) ]
186       | #_ % // % % >Ha >Hb @(not_to_not ??? (\Pf Hab)) #H destruct (H) % ] ]
187     ]
188   ]
189 ]
190 qed.
191
192 definition compare ≝ λi,j,sig,n.
193   whileTM … (compare_step i j sig n) comp1.
194
195 definition R_compare ≝ 
196   λi,j,sig,n.λint,outt: Vector (tape sig) (S n).
197   ((current ? (nth i ? int (niltape ?)) ≠ current ? (nth j ? int (niltape ?)) ∨
198     current ? (nth i ? int (niltape ?)) = None ? ∨
199     current ? (nth j ? int (niltape ?)) = None ?) → outt = int) ∧
200   (∀ls,x,xs,ci,rs,ls0,rs0. 
201     nth i ? int (niltape ?) = midtape sig ls x (xs@ci::rs) →
202     nth j ? int (niltape ?) = midtape sig ls0 x (xs@rs0) →
203     (rs0 = [ ] ∧
204      outt = change_vec ?? 
205            (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs) i)
206            (mk_tape sig (reverse ? xs@x::ls0) (None ?) []) j) ∨
207     ∃cj,rs1.rs0 = cj::rs1 ∧
208     (ci ≠ cj → 
209     outt = change_vec ?? 
210            (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs) i)
211            (midtape sig (reverse ? xs@x::ls0) cj rs1) j)).
212           
213 lemma wsem_compare : ∀i,j,sig,n.i ≠ j → i < S n → j < S n → 
214   compare i j sig n ⊫ R_compare i j sig n.
215 #i #j #sig #n #Hneq #Hi #Hj #ta #k #outc #Hloop
216 lapply (sem_while … (sem_comp_step i j sig n Hneq Hi Hj) … Hloop) //
217 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
218 [ whd in ⊢ (%→?); * * [ *
219  [ #Hcicj #Houtc % 
220    [ #_ @Houtc
221    | #ls #x #xs #ci #rs #ls0 #rs0 #Hnthi #Hnthj
222      >Hnthi in Hcicj; >Hnthj normalize in ⊢ (%→?); * #H @False_ind @H %
223    ]
224  | #Hci #Houtc %
225    [ #_ @Houtc
226    | #ls #x #xs #ci #rs #ls0 #rs0 #Hnthi >Hnthi in Hci;
227      normalize in ⊢ (%→?); #H destruct (H) ] ]
228  | #Hcj #Houtc %
229   [ #_ @Houtc
230   | #ls #x #xs #ci #rs #ls0 #rs0 #_ #Hnthj >Hnthj in Hcj;
231     normalize in ⊢ (%→?); #H destruct (H) ] ]
232 | #td #te * #x * * #Hci #Hcj #Hd #Hstar #IH #He lapply (IH He) -IH *
233   #IH1 #IH2 %
234   [ >Hci >Hcj * [ * 
235     [ * #H @False_ind @H % | #H destruct (H)] | #H destruct (H)] 
236   | #ls #c0 #xs #ci #rs #ls0 #rs0 cases xs
237     [ #Hnthi #Hnthj cases rs0 in Hnthj;
238       [ #Hnthj % % // >IH1
239         [ >Hd @eq_f3 //
240           [ @eq_f3 // >Hnthi %
241           | >Hnthj % ]
242         | >Hd %2 >nth_change_vec // >Hnthj % ]
243       | #r1 #rs1 #Hnthj %2 %{r1} %{rs1} % // #Hcir1 >IH1
244         [ >Hd @eq_f3 // 
245           [ @eq_f3 // >Hnthi %
246           | >Hnthj % ]
247         | >Hd >nth_change_vec // >nth_change_vec_neq [|@sym_not_eq //]
248           >nth_change_vec // >Hnthi >Hnthj normalize % %
249           @(not_to_not … Hcir1) #H destruct (H) %
250         ]
251       ]
252     | #x0 #xs0 #Hnthi #Hnthj 
253       cut (c0 = x) [ >Hnthi in Hci; normalize #H destruct (H) // ]
254       #Hcut destruct (Hcut) cases rs0 in Hnthj;
255       [ #Hnthj % % // 
256         cases (IH2 (x::ls) x0 xs0 ci rs (x::ls0) [ ] ??) -IH2
257         [ * #_ #IH2 >IH2 >Hd >change_vec_commute in ⊢ (??%?); //
258           >change_vec_change_vec >change_vec_commute in ⊢ (??%?); //
259           @sym_not_eq //
260         | * #cj * #rs1 * #H destruct (H)
261         | >Hd >nth_change_vec_neq [|@sym_not_eq //] >nth_change_vec //
262           >Hnthi %
263         | >Hd >nth_change_vec // >Hnthj % ]
264       | #r1 #rs1 #Hnthj %2 %{r1} %{rs1} % // #Hcir1
265         cases(IH2 (x::ls) x0 xs0 ci rs (x::ls0) (r1::rs1) ??)
266         [ * #H destruct (H)
267         | * #r1' * #rs1' * #H destruct (H) #Hc1r1 >Hc1r1 //
268           >Hd >change_vec_commute in ⊢ (??%?); //
269           >change_vec_change_vec >change_vec_commute in ⊢ (??%?); //
270             @sym_not_eq //
271         | >Hd >nth_change_vec_neq [|@sym_not_eq //] >nth_change_vec //
272           >Hnthi //
273         | >Hd >nth_change_vec // >Hnthi >Hnthj %
274 ]]]]]
275 qed.     
276  
277 lemma terminate_compare :  ∀i,j,sig,n,t.
278   i ≠ j → i < S n → j < S n → 
279   compare i j sig n ↓ t.
280 #i #j #sig #n #t #Hneq #Hi #Hj
281 @(terminate_while … (sem_comp_step …)) //
282 <(change_vec_same … t i (niltape ?))
283 cases (nth i (tape sig) t (niltape ?))
284 [ % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
285 |2,3: #a0 #al0 % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
286 | #ls #c #rs lapply c -c lapply ls -ls lapply t -t elim rs
287   [#t #ls #c % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?);
288    #H1 destruct (H1) #_ >change_vec_change_vec #Ht1 % 
289    #t2 * #x0 * * >Ht1 >nth_change_vec_neq [|@sym_not_eq //]
290    >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H)
291   |#r0 #rs0 #IH #t #ls #c % #t1 * #x * * >nth_change_vec //
292    normalize in ⊢ (%→?); #H destruct (H) #Hcur
293    >change_vec_change_vec >change_vec_commute // #Ht1 >Ht1 @IH
294   ]
295 ]
296 qed.
297
298 lemma sem_compare : ∀i,j,sig,n.
299   i ≠ j → i < S n → j < S n → 
300   compare i j sig n ⊨ R_compare i j sig n.
301 #i #j #sig #n #Hneq #Hi #Hj @WRealize_to_Realize /2/
302 qed.