2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department of the University of Bologna, Italy.
8 \ / This file is distributed under the terms of the
9 \ / GNU General Public License Version 2
10 V_____________________________________________________________*)
12 include "turing/while_machine.ma".
14 (******************* write a given symbol under the head **********************)
15 definition write_states ≝ initN 2.
17 definition wr0 : write_states ≝ mk_Sig ?? 0 (leb_true_to_le 1 2 (refl …)).
18 definition wr1 : write_states ≝ mk_Sig ?? 1 (leb_true_to_le 2 2 (refl …)).
20 definition write ≝ λalpha,c.
21 mk_TM alpha write_states
24 [ O ⇒ 〈wr1,Some ? 〈c,N〉〉
25 | S _ ⇒ 〈wr1,None ?〉 ])
28 definition R_write ≝ λalpha,c,t1,t2.
29 ∀ls,x,rs.t1 = midtape alpha ls x rs → t2 = midtape alpha ls c rs.
31 lemma sem_write : ∀alpha,c.Realize ? (write alpha c) (R_write alpha c).
32 #alpha #c #t @(ex_intro … 2) @ex_intro
33 [|% [% |#ls #c #rs #Ht >Ht % ] ]
36 (******************** moves the head one step to the right ********************)
38 definition move_states ≝ initN 2.
39 definition move0 : move_states ≝ mk_Sig ?? 0 (leb_true_to_le 1 2 (refl …)).
40 definition move1 : move_states ≝ mk_Sig ?? 1 (leb_true_to_le 2 2 (refl …)).
43 λalpha:FinSet.mk_TM alpha move_states
46 [ None ⇒ 〈move1,None ?〉
47 | Some a' ⇒ match (pi1 … q) with
48 [ O ⇒ 〈move1,Some ? 〈a',R〉〉
49 | S q ⇒ 〈move1,None ?〉 ] ])
50 move0 (λq.q == move1).
52 definition R_move_r ≝ λalpha,t1,t2.
53 (current ? t1 = None ? → t1 = t2) ∧
55 t1 = midtape alpha ls c rs →
56 t2 = mk_tape ? (c::ls) (option_hd ? rs) (tail ? rs).
58 (current ? t1 = None ? ∧ t1 = t2) ∨
60 t1 = midtape alpha ls c rs ∧
61 t2 = mk_tape ? (c::ls) (option_hd ? rs) (tail ? rs).*)
64 ∀alpha.Realize ? (move_r alpha) (R_move_r alpha).
65 #alpha #intape @(ex_intro ?? 2) cases intape
67 [| % [ % | % // #ls #c #rs #H destruct ] ]
69 [| % [ % | % // #ls #c #rs #H destruct ] ]
71 [| % [ % | % // #ls #c #rs #H destruct ] ]
73 @ex_intro [| % [ % | % [whd in ⊢ ((??%?)→?); #H destruct]
74 #ls1 #c1 #rs1 #H destruct cases rs1 // ] ] ]
77 (******************** moves the head one step to the left *********************)
80 λalpha:FinSet.mk_TM alpha move_states
83 [ None ⇒ 〈move1,None ?〉
84 | Some a' ⇒ match pi1 … q with
85 [ O ⇒ 〈move1,Some ? 〈a',L〉〉
86 | S q ⇒ 〈move1,None ?〉 ] ])
87 move0 (λq.q == move1).
89 definition R_move_l ≝ λalpha,t1,t2.
90 (current ? t1 = None ? → t1 = t2) ∧
92 t1 = midtape alpha ls c rs →
93 t2 = mk_tape ? (tail ? ls) (option_hd ? ls) (c::rs).
96 ∀alpha.Realize ? (move_l alpha) (R_move_l alpha).
97 #alpha #intape @(ex_intro ?? 2) cases intape
99 [| % [ % | % // #ls #c #rs #H destruct ] ]
101 [| % [ % | % // #ls #c #rs #H destruct ] ]
103 [| % [ % | % // #ls #c #rs #H destruct ] ]
105 @ex_intro [| % [ % | % [whd in ⊢ ((??%?)→?); #H destruct]
106 #ls1 #c1 #rs1 #H destruct cases ls1 // ] ] ]
109 (********************************* test char **********************************)
111 (* the test_char machine ends up in two different states q1 and q2 wether or not
112 the current character satisfies a boolean test function passed as a parameter to
114 The machine ends up in q1 also in case there is no current character.
117 definition tc_states ≝ initN 3.
119 definition tc_start : tc_states ≝ mk_Sig ?? 0 (leb_true_to_le 1 3 (refl …)).
120 definition tc_true : tc_states ≝ mk_Sig ?? 1 (leb_true_to_le 2 3 (refl …)).
121 definition tc_false : tc_states ≝ mk_Sig ?? 2 (leb_true_to_le 3 3 (refl …)).
123 definition test_char ≝
124 λalpha:FinSet.λtest:alpha→bool.
125 mk_TM alpha tc_states
128 [ None ⇒ 〈tc_false, None ?〉
131 [ true ⇒ 〈tc_true,None ?〉
132 | false ⇒ 〈tc_false,None ?〉 ]])
133 tc_start (λx.notb (x == tc_start)).
135 definition Rtc_true ≝
137 (∃c. current alpha t1 = Some ? c ∧ test c = true) ∧ t2 = t1.
139 definition Rtc_false ≝
141 (∀c. current alpha t1 = Some ? c → test c = false) ∧ t2 = t1.
144 ∀alpha,test,ls,a0,rs. test a0 = true →
145 step alpha (test_char alpha test)
146 (mk_config ?? tc_start (midtape … ls a0 rs)) =
147 mk_config alpha (states ? (test_char alpha test)) tc_true
148 (midtape … ls a0 rs).
149 #alpha #test #ls #a0 #ts #Htest whd in ⊢ (??%?);
150 whd in match (trans … 〈?,?〉); >Htest %
154 ∀alpha,test,ls,a0,rs. test a0 = false →
155 step alpha (test_char alpha test)
156 (mk_config ?? tc_start (midtape … ls a0 rs)) =
157 mk_config alpha (states ? (test_char alpha test)) tc_false
158 (midtape … ls a0 rs).
159 #alpha #test #ls #a0 #ts #Htest whd in ⊢ (??%?);
160 whd in match (trans … 〈?,?〉); >Htest %
163 lemma sem_test_char :
165 accRealize alpha (test_char alpha test)
166 tc_true (Rtc_true alpha test) (Rtc_false alpha test).
169 @(ex_intro ?? (mk_config ?? tc_false (niltape ?))) %
170 [ % // normalize #Hfalse destruct | #_ normalize % // #c #Hfalse destruct (Hfalse) ]
171 | #a #al @(ex_intro ?? 2) @(ex_intro ?? (mk_config ?? tc_false (leftof ? a al)))
172 % [ % // normalize #Hfalse destruct | #_ normalize % // #c #Hfalse destruct (Hfalse) ]
173 | #a #al @(ex_intro ?? 2) @(ex_intro ?? (mk_config ?? tc_false (rightof ? a al)))
174 % [ % // normalize #Hfalse destruct | #_ normalize % // #c #Hfalse destruct (Hfalse) ]
175 | #ls #c #rs @(ex_intro ?? 2)
176 cases (true_or_false (test c)) #Htest
177 [ @(ex_intro ?? (mk_config ?? tc_true ?))
180 [ whd in ⊢ (??%?); >tc_q0_q1 //
181 | #_ % // @(ex_intro … c) /2/]
182 | * #Hfalse @False_ind @Hfalse % ]
184 | @(ex_intro ?? (mk_config ?? tc_false (midtape ? ls c rs)))
187 [ whd in ⊢ (??%?); >tc_q0_q2 //
188 | whd in ⊢ ((??%%)→?); #Hfalse destruct (Hfalse) ]
189 | #_ % // #c0 whd in ⊢ ((??%?)→?); #Hc0 destruct (Hc0) //
195 lemma test_char_inv :
196 ∀sig.∀P:tape sig → Prop.∀f,t,t0.P t → Rtc_true sig f t t0 → P t0.
197 #sig #P #f #t #t0 #HPt * #_ //
200 (************************************* swap ***********************************)
201 definition swap_states : FinSet → FinSet ≝
202 λalpha:FinSet.FinProd (initN 4) alpha.
204 definition swap0 : initN 4 ≝ mk_Sig ?? 0 (leb_true_to_le 1 4 (refl …)).
205 definition swap1 : initN 4 ≝ mk_Sig ?? 1 (leb_true_to_le 2 4 (refl …)).
206 definition swap2 : initN 4 ≝ mk_Sig ?? 2 (leb_true_to_le 3 4 (refl …)).
207 definition swap3 : initN 4 ≝ mk_Sig ?? 3 (leb_true_to_le 4 4 (refl …)).
210 λalpha:FinSet.λfoo:alpha.
211 mk_TM alpha (swap_states alpha)
214 let q' ≝ pi1 nat (λi.i<4) q' in
216 [ None ⇒ 〈〈swap3,foo〉,None ?〉 (* if tape is empty then stop *)
219 [ O ⇒ (* q0 *) 〈〈swap1,a'〉,Some ? 〈a',R〉〉 (* save in register and move R *)
220 | S q' ⇒ match q' with
221 [ O ⇒ (* q1 *) 〈〈swap2,a'〉,Some ? 〈b,L〉〉 (* swap with register and move L *)
222 | S q' ⇒ match q' with
223 [ O ⇒ (* q2 *) 〈〈swap3,foo〉,Some ? 〈b,N〉〉 (* copy from register and stay *)
224 | S q' ⇒ (* q3 *) 〈〈swap3,foo〉,None ?〉 (* final state *)
229 (λq.\fst q == swap3).
234 t1 = midtape alpha ls b [ ] →
235 t2 = rightof ? b ls) ∧
237 t1 = midtape alpha ls b (a::rs) →
238 t2 = midtape alpha ls a (b::rs)).
240 lemma sem_swap_r : ∀alpha,foo.
241 swap_r alpha foo ⊨ Rswap_r alpha.
243 [@(ex_intro ?? 2) @(ex_intro … (mk_config ?? 〈swap3,foo〉 (niltape ?)))
244 % [% |% [#b #ls | #a #b #ls #rs] #H destruct]
245 |#l0 #lt0 @(ex_intro ?? 2) @(ex_intro … (mk_config ?? 〈swap3,foo〉 (leftof ? l0 lt0)))
246 % [% | % [#b #ls | #a #b #ls #rs] #H destruct]
247 |#r0 #rt0 @(ex_intro ?? 2) @(ex_intro … (mk_config ?? 〈swap3,foo〉 (rightof ? r0 rt0)))
248 % [% |% [#b #ls | #a #b #ls #rs] #H destruct]
249 | #lt #c #rt @(ex_intro ?? 4) cases rt
250 [@ex_intro [|% [ % | %
251 [#b #ls #H destruct normalize // |#a #b #ls #rs #H destruct]]]
252 |#r0 #rt0 @ex_intro [| % [ % | %
253 [#b #ls #H destruct | #a #b #ls #rs #H destruct normalize //
259 λalpha:FinSet.λfoo:alpha.
260 mk_TM alpha (swap_states alpha)
263 let q' ≝ pi1 nat (λi.i<4) q' in
265 [ None ⇒ 〈〈swap3,foo〉,None ?〉 (* if tape is empty then stop *)
268 [ O ⇒ (* q0 *) 〈〈swap1,a'〉,Some ? 〈a',L〉〉 (* save in register and move L *)
269 | S q' ⇒ match q' with
270 [ O ⇒ (* q1 *) 〈〈swap2,a'〉,Some ? 〈b,R〉〉 (* swap with register and move R *)
271 | S q' ⇒ match q' with
272 [ O ⇒ (* q2 *) 〈〈swap3,foo〉,Some ? 〈b,N〉〉 (* copy from register and stay *)
273 | S q' ⇒ (* q3 *) 〈〈swap3,foo〉,None ?〉 (* final state *)
278 (λq.\fst q == swap3).
283 t1 = midtape alpha [ ] b rs →
284 t2 = leftof ? b rs) ∧
286 t1 = midtape alpha (a::ls) b rs →
287 t2 = midtape alpha (b::ls) a rs).
289 lemma sem_swap_l : ∀alpha,foo.
290 swap_l alpha foo ⊨ Rswap_l alpha.
292 [@(ex_intro ?? 2) @(ex_intro … (mk_config ?? 〈swap3,foo〉 (niltape ?)))
293 % [% |% [#b #rs | #a #b #ls #rs] #H destruct]
294 |#l0 #lt0 @(ex_intro ?? 2) @(ex_intro … (mk_config ?? 〈swap3,foo〉 (leftof ? l0 lt0)))
295 % [% | % [#b #rs | #a #b #ls #rs] #H destruct]
296 |#r0 #rt0 @(ex_intro ?? 2) @(ex_intro … (mk_config ?? 〈swap3,foo〉 (rightof ? r0 rt0)))
297 % [% |% [#b #rs | #a #b #ls #rs] #H destruct]
298 | #lt #c #rt @(ex_intro ?? 4) cases lt
299 [@ex_intro [|% [ % | %
300 [#b #rs #H destruct normalize // |#a #b #ls #rs #H destruct]]]
301 |#r0 #rt0 @ex_intro [| % [ % | %
302 [#b #rs #H destruct | #a #b #ls #rs #H destruct normalize //