1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| The HELM team. *)
8 (* ||A|| http://helm.cs.unibo.it *)
10 (* \ / This file is distributed under the terms of the *)
11 (* v GNU General Public License Version 2 *)
13 (**************************************************************************)
15 include "turing/multi_universal/moves.ma".
16 include "turing/if_multi.ma".
17 include "turing/inject.ma".
18 include "turing/basic_machines.ma".
20 definition compare_states ≝ initN 3.
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 …)).
28 0) (x,x) → (x,x)(R,R) → 1
35 definition trans_compare_step ≝
37 λp:compare_states × (Vector (option sig) (S n)).
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)
48 else 〈comp2,null_action ? n〉 ]
51 [ O ⇒ (* 1 *) 〈comp1,null_action ? n〉
52 | S _ ⇒ (* 2 *) 〈comp2,null_action ? n〉 ] ].
54 definition compare_step ≝
56 mk_mTM sig n compare_states (trans_compare_step i j sig n)
57 comp0 (λq.q == comp1 ∨ q == comp2).
59 definition R_comp_step_true ≝
60 λi,j,sig,n.λint,outt: Vector (tape sig) (S n).
62 current ? (nth i ? int (niltape ?)) = Some ? x ∧
63 current ? (nth j ? int (niltape ?)) = Some ? x ∧
66 (tape_move_right ? (nth i ? int (niltape ?))) i)
67 (tape_move_right ? (nth j ? int (niltape ?))) j.
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.
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 ⊢ (?→??%?);
85 [ whd in ⊢ (??(???%)?); >Hcurrent %
86 | whd in ⊢ (??(????(???%))?); >Hcurrent @tape_move_null_action ]
88 [ whd in ⊢ (??(???%)?); >Hcurrent cases (nth i ?? (None sig)) //
89 | whd in ⊢ (??(????(???%))?); >Hcurrent
90 cases (nth i ?? (None sig)) [|#x] @tape_move_null_action ] ]
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 //
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
111 | whd in match (trans ????); >Hai >Haj
112 whd in ⊢ (??(????(???%))?); cut ((ai==aj)=false)
113 [>(\bf ?) /2 by not_to_not/ % #Haiaj @Hneq
115 |#Hcut >Hcut @tape_move_null_action
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) =
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 ⊢ (??%?);
142 >pmap_change >pmap_change <tape_move_multi_def
143 >tape_move_null_action
144 @eq_f2 // >nth_change_vec_neq //
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 ⊢ (???%→?);
158 [ whd in ⊢ (??%?); >comp_q0_q2_null /2/ % <Hcuri in ⊢ (???%);
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 ⊢ (???%→?);
166 [ whd in ⊢ (??%?); >comp_q0_q2_null /2/ %2 <Hcurj in ⊢ (???%);
168 | normalize in ⊢ (%→?); #H destruct (H) ]
169 | #_ % // >Ha >Hcurj % % % #H destruct (H) ] ]
170 | #b #Hb %{2} cases (true_or_false (a == b)) #Hab
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 %
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) % ] ]
192 definition compare ≝ λi,j,sig,n.
193 whileTM … (compare_step i j sig n) comp1.
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) ∧
201 (* nth i ? int (niltape ?) = midtape sig ls x (xs@ci::rs) → *)
202 nth i ? int (niltape ?) = midtape sig ls x rs →
203 nth j ? int (niltape ?) = midtape sig ls0 x rs0 →
204 (∃rs'.rs = rs0@rs' ∧ current ? (nth j ? outt (niltape ?)) = None ?) ∨
205 (∃rs0'.rs0 = rs@rs0' ∧
208 (mk_tape sig (reverse sig rs@x::ls) (None sig) []) i)
209 (mk_tape sig (reverse sig rs@x::ls0) (option_hd sig rs0')
210 (tail sig rs0')) j) ∨
211 (∃xs,ci,cj,rs',rs0'.ci ≠ cj ∧ rs = xs@ci::rs' ∧ rs0 = xs@cj::rs0' ∧
213 (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs') i)
214 (midtape sig (reverse ? xs@x::ls0) cj rs0') j)).
216 lemma wsem_compare : ∀i,j,sig,n.i ≠ j → i < S n → j < S n →
217 compare i j sig n ⊫ R_compare i j sig n.
218 #i #j #sig #n #Hneq #Hi #Hj #ta #k #outc #Hloop
219 lapply (sem_while … (sem_comp_step i j sig n Hneq Hi Hj) … Hloop) //
220 -Hloop * #tb * #Hstar @(star_ind_l ??????? Hstar) -Hstar
221 [ whd in ⊢ (%→?); * * [ *
224 | #ls #x #rs #ls0 #rs0 #Hnthi #Hnthj
225 >Hnthi in Hcicj; >Hnthj normalize in ⊢ (%→?); * #H @False_ind @H %
229 | #ls #x #rs #ls0 #rs0 #Hnthi >Hnthi in Hci;
230 normalize in ⊢ (%→?); #H destruct (H) ] ]
233 | #ls #x #rs #ls0 #rs0 #_ #Hnthj >Hnthj in Hcj;
234 normalize in ⊢ (%→?); #H destruct (H) ] ]
235 | #td #te * #x * * #Hci #Hcj #Hd #Hstar #IH #He lapply (IH He) -IH *
238 [ * #H @False_ind @H % | #H destruct (H)] | #H destruct (H)]
239 | #ls #c0 #rs #ls0 #rs0 cases rs
240 [ -IH2 #Hnthi #Hnthj % %2 %{rs0} % [%]
241 >Hnthi in Hd; #Hd >Hd in IH1; #IH1 >IH1
242 [| % %2 >nth_change_vec_neq [|@sym_not_eq //] >nth_change_vec // % ]
243 >Hnthj cases rs0 [| #r1 #rs1 ] %
244 | #r1 #rs1 #Hnthi cases rs0
245 [ -IH2 #Hnthj % % %{(r1::rs1)} % [%]
246 >Hnthj in Hd; #Hd >Hd in IH1; #IH1 >IH1
247 [| %2 >nth_change_vec // ]
249 | #r2 #rs2 #Hnthj lapply IH2; >Hd in IH1; >Hnthi >Hnthj
251 >nth_change_vec_neq [| @sym_not_eq // ] >nth_change_vec //
252 cases (true_or_false (r1 == r2)) #Hr1r2
253 [ >(\P Hr1r2) #_ #IH2 cases (IH2 … (refl ??) (refl ??)) [ *
254 [ * #rs' * #Hrs1 #Hcurout_j % % %{rs'}
255 >Hrs1 >Hcurout_j normalize % //
256 | * #rs0' * #Hrs2 #Hcurout_i % %2 %{rs0'}
257 >Hrs2 >Hcurout_i % //
258 >change_vec_commute // >change_vec_change_vec
259 >change_vec_commute [|@sym_not_eq//] >change_vec_change_vec
260 >reverse_cons >associative_append >associative_append % ]
261 | * #xs * #ci * #cj * #rs' * #rs0' * * * #Hcicj #Hrs1 #Hrs2
262 >change_vec_commute // >change_vec_change_vec
263 >change_vec_commute [| @sym_not_eq ] // >change_vec_change_vec
264 #Houtc %2 %{(r2::xs)} %{ci} %{cj} %{rs'} %{rs0'}
265 % [ % [ % [ // | >Hrs1 // ] | >Hrs2 // ]
266 | >reverse_cons >associative_append >associative_append >Houtc % ] ]
267 | lapply (\Pf Hr1r2) -Hr1r2 #Hr1r2 #IH1 #_ %2
268 >IH1 [| % % normalize @(not_to_not … Hr1r2) #H destruct (H) % ]
269 %{[]} %{r1} %{r2} %{rs1} %{rs2} % [ % [ % /2/ | % ] | % ] ]]]]]
272 lemma terminate_compare : ∀i,j,sig,n,t.
273 i ≠ j → i < S n → j < S n →
274 compare i j sig n ↓ t.
275 #i #j #sig #n #t #Hneq #Hi #Hj
276 @(terminate_while … (sem_comp_step …)) //
277 <(change_vec_same … t i (niltape ?))
278 cases (nth i (tape sig) t (niltape ?))
279 [ % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
280 |2,3: #a0 #al0 % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?); #Hx destruct
281 | #ls #c #rs lapply c -c lapply ls -ls lapply t -t elim rs
282 [#t #ls #c % #t1 * #x * * >nth_change_vec // normalize in ⊢ (%→?);
283 #H1 destruct (H1) #_ >change_vec_change_vec #Ht1 %
284 #t2 * #x0 * * >Ht1 >nth_change_vec_neq [|@sym_not_eq //]
285 >nth_change_vec // normalize in ⊢ (%→?); #H destruct (H)
286 |#r0 #rs0 #IH #t #ls #c % #t1 * #x * * >nth_change_vec //
287 normalize in ⊢ (%→?); #H destruct (H) #Hcur
288 >change_vec_change_vec >change_vec_commute // #Ht1 >Ht1 @IH
293 lemma sem_compare : ∀i,j,sig,n.
294 i ≠ j → i < S n → j < S n →
295 compare i j sig n ⊨ R_compare i j sig n.
296 #i #j #sig #n #Hneq #Hi #Hj @WRealize_to_Realize /2/