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).
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 //
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) ∧
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) →
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 ∧
210 (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs) i)
211 (midtape sig (reverse ? xs@x::ls0) cj rs1) j)).
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 ⊢ (%→?); * * [ *
221 | #ls #x #xs #ci #rs #ls0 #rs0 #Hnthi #Hnthj
222 >Hnthi in Hcicj; >Hnthj normalize in ⊢ (%→?); * #H @False_ind @H %
226 | #ls #x #xs #ci #rs #ls0 #rs0 #Hnthi >Hnthi in Hci;
227 normalize in ⊢ (%→?); #H destruct (H) ] ]
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 *
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;
242 | >Hd %2 >nth_change_vec // >Hnthj % ]
243 | #r1 #rs1 #Hnthj %2 %{r1} %{rs1} % // #Hcir1 >IH1
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) %
252 | #x0 #xs0 #Hnthi #Hnthj
253 cut (c0 = x) [ >Hnthi in Hci; normalize #H destruct (H) // ]
254 #Hcut destruct (Hcut) cases rs0 in 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 ⊢ (??%?); //
260 | * #cj * #rs1 * #H destruct (H)
261 | >Hd >nth_change_vec_neq [|@sym_not_eq //] >nth_change_vec //
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) ??)
267 | * #r1' * #rs1' * #H destruct (H) #Hc1r1 >Hc1r1 //
268 >Hd >change_vec_commute in ⊢ (??%?); //
269 >change_vec_change_vec >change_vec_commute in ⊢ (??%?); //
271 | >Hd >nth_change_vec_neq [|@sym_not_eq //] >nth_change_vec //
273 | >Hd >nth_change_vec // >Hnthi >Hnthj %
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
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/