]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/multi_universal/compare.ma
501b7bc77369b75b82cf4e2742bb2e494ff12b71
[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 lemma 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 #i #j #sig #n #v #a #Heq #Hi #Hj #Ha1 #Ha2
133 whd in ⊢ (??%?); >(eq_pair_fst_snd … (trans ????)) whd in ⊢ (??%?); @eq_f2
134 [ whd in match (trans ????);
135   >Ha1 >Ha2 whd in ⊢ (??(???%)?); >(\b ?) //
136 | whd in match (trans ????);
137   >Ha1 >Ha2 whd in ⊢ (??(????(???%))?); >(\b ?) //
138   change with (change_vec ?????) in ⊢ (??(????%)?);
139   <(change_vec_same … v j (niltape ?)) in ⊢ (??%?);
140   <(change_vec_same … v i (niltape ?)) in ⊢ (??%?);
141   >tape_move_multi_def 
142   >pmap_change >pmap_change <tape_move_multi_def
143   >tape_move_null_action
144   @eq_f2 // >nth_change_vec_neq //
145 ]
146 qed.
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/ 
159     | normalize in ⊢ (%→?); #H destruct (H) ]
160   | #_ % // % %2 // ] ]
161 | #a #Ha lapply (refl ? (current ? (nth j ? int (niltape ?))))
162   cases (current ? (nth j ? int (niltape ?))) in ⊢ (???%→?);
163   [ #Hcurj %{2} %
164     [| % [ %
165        [ whd in ⊢ (??%?); >comp_q0_q2_null /2/ %2
166        | normalize in ⊢ (%→?); #H destruct (H) ]
167        | #_ % // >Ha >Hcurj % % % #H destruct (H) ] ]
168   | #b #Hb %{2} cases (true_or_false (a == b)) #Hab
169     [ %
170       [| % [ % 
171         [whd in ⊢  (??%?);  >(comp_q0_q1 … a Hneq Hi Hj) //
172          >(\P Hab) <Hb @sym_eq @nth_vec_map
173         | #_ whd >(\P Hab) %{b} % // % // <(\P Hab) // ]
174         | * #H @False_ind @H %
175       ] ]
176     | %
177       [| % [ % 
178         [whd in ⊢  (??%?);  >comp_q0_q2_neq //
179          <(nth_vec_map ?? (current …) i ? int (niltape ?))
180          <(nth_vec_map ?? (current …) j ? int (niltape ?)) >Ha >Hb
181          @(not_to_not ??? (\Pf Hab)) #H destruct (H) %
182         | normalize in ⊢ (%→?); #H destruct (H) ]
183       | #_ % // % % >Ha >Hb @(not_to_not ??? (\Pf Hab)) #H destruct (H) % ] ]
184     ]
185   ]
186 ]
187 qed.
188
189 definition compare ≝ λi,j,sig,n.
190   whileTM … (compare_step i j sig n) comp1.
191
192 definition R_compare ≝ 
193   λi,j,sig,n.λint,outt: Vector (tape sig) (S n).
194   ((current ? (nth i ? int (niltape ?)) ≠ current ? (nth j ? int (niltape ?)) ∨
195     current ? (nth i ? int (niltape ?)) = None ? ∨
196     current ? (nth j ? int (niltape ?)) = None ?) → outt = int) ∧
197   (∀ls,x,rs,ls0,rs0. 
198 (*    nth i ? int (niltape ?) = midtape sig ls x (xs@ci::rs) → *)
199     nth i ? int (niltape ?) = midtape sig ls x rs →
200     nth j ? int (niltape ?) = midtape sig ls0 x rs0 →
201     (∃rs'.rs = rs0@rs' ∧ current ? (nth j ? outt (niltape ?)) = None ?) ∨
202     (∃rs0'.rs0 = rs@rs0' ∧ 
203      outt = change_vec ?? 
204             (change_vec ?? int  
205               (mk_tape sig (reverse sig rs@x::ls) (None sig) []) i)
206             (mk_tape sig (reverse sig rs@x::ls0) (option_hd sig rs0')
207             (tail sig rs0')) j) ∨
208     (∃xs,ci,cj,rs',rs0'.ci ≠ cj ∧ rs = xs@ci::rs' ∧ rs0 = xs@cj::rs0' ∧
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 rs0') 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 #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 #rs #ls0 #rs0 #Hnthi >Hnthi in Hci;
227      normalize in ⊢ (%→?); #H destruct (H) ] ]
228  | #Hcj #Houtc %
229   [ #_ @Houtc
230   | #ls #x #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 #rs #ls0 #rs0 cases rs
237     [ -IH2 #Hnthi #Hnthj % %2 %{rs0} % [%]
238       >Hnthi in Hd; #Hd >Hd in IH1; #IH1 >IH1
239       [| % %2 >nth_change_vec_neq [|@sym_not_eq //] >nth_change_vec // % ]
240       >Hnthj cases rs0 [| #r1 #rs1 ] %
241     | #r1 #rs1 #Hnthi cases rs0
242       [ -IH2 #Hnthj % % %{(r1::rs1)} % [%]
243         >Hnthj in Hd; #Hd >Hd in IH1; #IH1 >IH1
244         [| %2 >nth_change_vec // ]
245         >nth_change_vec //
246       | #r2 #rs2 #Hnthj lapply IH2; >Hd in IH1; >Hnthi >Hnthj
247         >nth_change_vec //
248         >nth_change_vec_neq [| @sym_not_eq // ] >nth_change_vec //
249         cases (true_or_false (r1 == r2)) #Hr1r2
250         [ >(\P Hr1r2) #_ #IH2 cases (IH2 … (refl ??) (refl ??)) [ *
251           [ * #rs' * #Hrs1 #Hcurout_j % % %{rs'}
252             >Hrs1 >Hcurout_j normalize % //
253           | * #rs0' * #Hrs2 #Hcurout_i % %2 %{rs0'}
254             >Hrs2 >Hcurout_i % //
255             >change_vec_commute // >change_vec_change_vec
256             >change_vec_commute [|@sym_not_eq//] >change_vec_change_vec
257             >reverse_cons >associative_append >associative_append % ]
258           | * #xs * #ci * #cj * #rs' * #rs0' * * * #Hcicj #Hrs1 #Hrs2 
259             >change_vec_commute // >change_vec_change_vec 
260             >change_vec_commute [| @sym_not_eq ] // >change_vec_change_vec 
261             #Houtc %2 %{(r2::xs)} %{ci} %{cj} %{rs'} %{rs0'}
262             % [ % [ % [ // | >Hrs1 // ] | >Hrs2 // ] 
263               | >reverse_cons >associative_append >associative_append >Houtc % ] ]
264         | lapply (\Pf Hr1r2) -Hr1r2 #Hr1r2 #IH1 #_ %2
265           >IH1 [| % % normalize @(not_to_not … Hr1r2) #H destruct (H) % ]
266           %{[]} %{r1} %{r2} %{rs1} %{rs2} % [ % [ % /2/ | % ] | % ] ]]]]]
267 qed.
268  
269 lemma terminate_compare :  ∀i,j,sig,n,t.
270   i ≠ j → i < S n → j < S n → 
271   compare i j sig n ↓ t.
272 #i #j #sig #n #t #Hneq #Hi #Hj
273 @(terminate_while … (sem_comp_step …)) //
274 <(change_vec_same … t i (niltape ?))
275 cases (nth i (tape sig) t (niltape ?))
276 [ % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
277 |2,3: #a0 #al0 % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
278 | #ls #c #rs lapply c -c lapply ls -ls lapply t -t elim rs
279   [#t #ls #c % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?);
280    #H1 destruct (H1) #_ >change_vec_change_vec #Ht1 % 
281    #t2 * #x0 * * >Ht1 >nth_change_vec_neq [|@sym_not_eq //]
282    >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H)
283   |#r0 #rs0 #IH #t #ls #c % #t1 * #x * * >nth_change_vec //
284    normalize in ⊢ (%→?); #H destruct (H) #Hcur
285    >change_vec_change_vec >change_vec_commute // #Ht1 >Ht1 @IH
286   ]
287 ]
288 qed.
289
290 lemma sem_compare : ∀i,j,sig,n.
291   i ≠ j → i < S n → j < S n → 
292   compare i j sig n ⊨ R_compare i j sig n.
293 #i #j #sig #n #Hneq #Hi #Hj @WRealize_to_Realize 
294   [/2/| @wsem_compare // ]
295 qed.