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