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/turing.ma".
16 include "turing/inject.ma".
17 include "turing/while_multi.ma".
19 definition compare_states ≝ initN 3.
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 …)).
27 0) (x,x) → (x,x)(R,R) → 1
34 definition trans_compare_step ≝
36 λp:compare_states × (Vector (option sig) (S n)).
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)
47 else 〈comp2,null_action ? n〉 ]
50 [ O ⇒ (* 1 *) 〈comp1,null_action ? n〉
51 | S _ ⇒ (* 2 *) 〈comp2,null_action ? n〉 ] ].
53 definition compare_step ≝
55 mk_mTM sig n compare_states (trans_compare_step i j sig n)
56 comp0 (λq.q == comp1 ∨ q == comp2).
58 definition R_comp_step_true ≝
59 λi,j,sig,n.λint,outt: Vector (tape sig) (S n).
61 current ? (nth i ? int (niltape ?)) = Some ? x ∧
62 current ? (nth j ? int (niltape ?)) = Some ? x ∧
65 (tape_move ? (nth i ? int (niltape ?)) (Some ? 〈x,R〉)) i)
66 (tape_move ? (nth j ? int (niltape ?)) (Some ? 〈x,R〉)) j.
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.
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 ⊢ (?→??%?);
84 [ whd in ⊢ (??(???%)?); >Hcurrent %
85 | whd in ⊢ (??(???????(???%))?); >Hcurrent @tape_move_null_action ]
87 [ whd in ⊢ (??(???%)?); >Hcurrent cases (nth i ?? (None sig)) //
88 | whd in ⊢ (??(???????(???%))?); >Hcurrent
89 cases (nth i ?? (None sig)) [|#x] @tape_move_null_action ] ]
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 //
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
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) =
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 //
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 ⊢ (???%→?);
147 [ whd in ⊢ (??%?); >comp_q0_q2_null /2/ % <Hcuri in ⊢ (???%);
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 ⊢ (???%→?);
155 [ whd in ⊢ (??%?); >comp_q0_q2_null /2/ %2 <Hcurj in ⊢ (???%);
157 | normalize in ⊢ (%→?); #H destruct (H) ]
158 | #_ % >Ha >Hcurj % % % #H destruct (H) ] ]
159 | #b #Hb %{2} cases (true_or_false (a == b)) #Hab
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 %
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) % ] ]
181 definition compare ≝ λi,j,sig,n.
182 whileTM … (compare_step i j sig n) comp1.
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)) →
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 →
193 (change_vec ?? int (midtape sig (reverse ? xs@x::ls) ci rs) i)
194 (midtape sig (reverse ? xs@x::ls0) cj rs0) j).
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 ⊢ (%→?); * * [ *
204 | #ls #x #xs #ci #rs #ls0 #cj #rs0 #Hnthi #Hnthj
205 >Hnthi in Hcicj; >Hnthj normalize in ⊢ (%→?); * #H @False_ind @H %
209 | #ls #x #xs #ci #rs #ls0 #cj #rs0 #Hnthi >Hnthi in Hci;
210 normalize in ⊢ (%→?); #H destruct (H) ] ]
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 *
217 [ >Hci >Hcj * #H @False_ind @H %
218 | #ls #c0 #xs #ci #rs #ls0 #cj #rs0 cases xs
219 [ #Hnthi #Hnthj #Hcicj >IH1
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)
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 ⊢ (??%?); //
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) %
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
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/