]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/multi_universal/compare.ma
advancement in match
[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 (*    (∃rs'.rs = rs0@rs' ∧ current ? (nth j ? outt (niltape ?)) = None ?) ∨
193     (∃rs0'.rs0 = rs@rs0' ∧ 
194      outt = change_vec ?? 
195             (change_vec ?? int  
196               (mk_tape sig (reverse sig rs@x::ls) (None sig) []) i)
197             (mk_tape sig (reverse sig rs@x::ls0) (option_hd sig rs0')
198             (tail sig rs0')) j) ∨
199     (∃xs,ci,cj,rs',rs0'.ci ≠ cj ∧ rs = xs@ci::rs' ∧ rs0 = xs@cj::rs0' ∧
200      outt = change_vec ?? 
201             (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs') i)
202             (midtape sig (reverse ? xs@x::ls0) cj rs0') j)).*)
203 definition R_compare ≝ 
204   λi,j,sig,n.λint,outt: Vector (tape sig) (S n).
205   ((current ? (nth i ? int (niltape ?)) ≠ current ? (nth j ? int (niltape ?)) ∨
206     current ? (nth i ? int (niltape ?)) = None ? ∨
207     current ? (nth j ? int (niltape ?)) = None ?) → outt = int) ∧
208   (∀ls,x,rs,ls0,rs0. 
209 (*    nth i ? int (niltape ?) = midtape sig ls x (xs@ci::rs) → *)
210     nth i ? int (niltape ?) = midtape sig ls x rs →
211     nth j ? int (niltape ?) = midtape sig ls0 x rs0 →
212     (∃rs'.rs = rs0@rs' ∧ 
213      outt = change_vec ?? 
214             (change_vec ?? int  
215               (mk_tape sig (reverse sig rs0@x::ls) (option_hd sig rs') (tail ? rs')) i)
216             (mk_tape sig (reverse sig rs0@x::ls0) (None ?) [ ]) j) ∨
217     (∃rs0'.rs0 = rs@rs0' ∧ 
218      outt = change_vec ?? 
219             (change_vec ?? int  
220               (mk_tape sig (reverse sig rs@x::ls) (None sig) []) i)
221             (mk_tape sig (reverse sig rs@x::ls0) (option_hd sig rs0')
222             (tail sig rs0')) j) ∨
223     (∃xs,ci,cj,rs',rs0'.ci ≠ cj ∧ rs = xs@ci::rs' ∧ rs0 = xs@cj::rs0' ∧
224      outt = change_vec ?? 
225             (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs') i)
226             (midtape sig (reverse ? xs@x::ls0) cj rs0') j)).            
227           
228 lemma wsem_compare : ∀i,j,sig,n.i ≠ j → i < S n → j < S n → 
229   compare i j sig n ⊫ R_compare i j sig n.
230 #i #j #sig #n #Hneq #Hi #Hj #ta #k #outc #Hloop
231 lapply (sem_while … (sem_comp_step i j sig n Hneq Hi Hj) … Hloop) //
232 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
233 [ whd in ⊢ (%→?); * * [ *
234  [ #Hcicj #Houtc % 
235    [ #_ @Houtc
236    | #ls #x #rs #ls0 #rs0 #Hnthi #Hnthj
237      >Hnthi in Hcicj; >Hnthj normalize in ⊢ (%→?); * #H @False_ind @H %
238    ]
239  | #Hci #Houtc %
240    [ #_ @Houtc
241    | #ls #x #rs #ls0 #rs0 #Hnthi >Hnthi in Hci;
242      normalize in ⊢ (%→?); #H destruct (H) ] ]
243  | #Hcj #Houtc %
244   [ #_ @Houtc
245   | #ls #x #rs #ls0 #rs0 #_ #Hnthj >Hnthj in Hcj;
246     normalize in ⊢ (%→?); #H destruct (H) ] ]
247 | #td #te * #x * * #Hci #Hcj #Hd #Hstar #IH #He lapply (IH He) -IH *
248   #IH1 #IH2 %
249   [ >Hci >Hcj * [ * 
250     [ * #H @False_ind @H % | #H destruct (H)] | #H destruct (H)] 
251   | #ls #c0 #rs #ls0 #rs0 cases rs
252     [ -IH2 #Hnthi #Hnthj % %2 %{rs0} % [%]
253       >Hnthi in Hd; #Hd >Hd in IH1; #IH1 >IH1
254       [| % %2 >nth_change_vec_neq [|@sym_not_eq //] >nth_change_vec // % ]
255       >Hnthj cases rs0 [| #r1 #rs1 ] %
256     | #r1 #rs1 #Hnthi cases rs0
257       [ -IH2 #Hnthj % % %{(r1::rs1)} % [%]
258         >Hnthj in Hd; #Hd >Hd in IH1; #IH1 >IH1
259         [| %2 >nth_change_vec // ]
260         >Hnthi >Hnthj %
261       | #r2 #rs2 #Hnthj lapply IH2; >Hd in IH1; >Hnthi >Hnthj
262         >nth_change_vec //
263         >nth_change_vec_neq [| @sym_not_eq // ] >nth_change_vec //
264         cases (true_or_false (r1 == r2)) #Hr1r2
265         [ >(\P Hr1r2) #_ #IH2 cases (IH2 … (refl ??) (refl ??)) [ *
266           [ * #rs' * #Hrs1 #Hcurout_j % % %{rs'}
267             >Hrs1 % 
268             [ % 
269             | >Hcurout_j >change_vec_commute // >change_vec_change_vec
270               >change_vec_commute // @sym_not_eq // ]
271           | * #rs0' * #Hrs2 #Hcurout_i % %2 %{rs0'}
272             >Hrs2 >Hcurout_i % //
273             >change_vec_commute // >change_vec_change_vec
274             >change_vec_commute [|@sym_not_eq//] >change_vec_change_vec
275             >reverse_cons >associative_append >associative_append % ]
276           | * #xs * #ci * #cj * #rs' * #rs0' * * * #Hcicj #Hrs1 #Hrs2 
277             >change_vec_commute // >change_vec_change_vec 
278             >change_vec_commute [| @sym_not_eq ] // >change_vec_change_vec 
279             #Houtc %2 %{(r2::xs)} %{ci} %{cj} %{rs'} %{rs0'}
280             % [ % [ % [ // | >Hrs1 // ] | >Hrs2 // ] 
281               | >reverse_cons >associative_append >associative_append >Houtc % ] ]
282         | lapply (\Pf Hr1r2) -Hr1r2 #Hr1r2 #IH1 #_ %2
283           >IH1 [| % % normalize @(not_to_not … Hr1r2) #H destruct (H) % ]
284           %{[]} %{r1} %{r2} %{rs1} %{rs2} % [ % [ % /2/ | % ] | % ] ]]]]]
285 qed.
286  
287 lemma terminate_compare :  ∀i,j,sig,n,t.
288   i ≠ j → i < S n → j < S n → 
289   compare i j sig n ↓ t.
290 #i #j #sig #n #t #Hneq #Hi #Hj
291 @(terminate_while … (sem_comp_step …)) //
292 <(change_vec_same … t i (niltape ?))
293 cases (nth i (tape sig) t (niltape ?))
294 [ % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
295 |2,3: #a0 #al0 % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
296 | #ls #c #rs lapply c -c lapply ls -ls lapply t -t elim rs
297   [#t #ls #c % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?);
298    #H1 destruct (H1) #_ >change_vec_change_vec #Ht1 % 
299    #t2 * #x0 * * >Ht1 >nth_change_vec_neq [|@sym_not_eq //]
300    >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H)
301   |#r0 #rs0 #IH #t #ls #c % #t1 * #x * * >nth_change_vec //
302    normalize in ⊢ (%→?); #H destruct (H) #Hcur
303    >change_vec_change_vec >change_vec_commute // #Ht1 >Ht1 @IH
304   ]
305 ]
306 qed.
307
308 lemma sem_compare : ∀i,j,sig,n.
309   i ≠ j → i < S n → j < S n → 
310   compare i j sig n ⊨ R_compare i j sig n.
311 #i #j #sig #n #Hneq #Hi #Hj @WRealize_to_Realize 
312   [/2/| @wsem_compare // ]
313 qed.