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_____________________________________________________________*)
17 include "turing/universal/tuples.ma".
19 definition write_states ≝ initN 2.
21 definition write ≝ λalpha,c.
22 mk_TM alpha write_states
25 [ O ⇒ 〈1,Some ? 〈c,N〉〉
29 definition R_write ≝ λalpha,c,t1,t2.
30 ∀ls,x,rs.t1 = midtape alpha ls x rs → t2 = midtape alpha ls c rs.
32 axiom sem_write : ∀alpha,c.Realize ? (write alpha c) (R_write alpha c).
34 definition copy_step_subcase ≝
35 λalpha,c,elseM.ifTM ? (test_char ? (λx.x == 〈c,true〉))
36 (seq (FinProd alpha FinBool) (adv_mark_r …)
38 (seq ? (adv_to_mark_l … (is_marked alpha))
39 (seq ? (write ? 〈c,false〉)
42 (seq ? (move_r …) (adv_to_mark_r … (is_marked alpha)))))))))
45 definition R_copy_step_subcase ≝
46 λalpha,c,RelseM,t1,t2.
48 t1 = midtape (FinProd … alpha FinBool) (l1@〈a0,false〉::〈x0,true〉::l2)
49 〈x,true〉 (〈a,false〉::l3) →
50 (∀c.memb ? c l1 = true → is_marked ? c = false) →
51 (x = c ∧ t2 = midtape ? (〈x,false〉::l1@〈a0,true〉::〈x,false〉::l2) 〈a,true〉 l3) ∨
52 (x ≠ c ∧ RelseM t1 t2).
54 axiom sem_copy_step_subcase :
55 ∀alpha,c,elseM,RelseM.
56 Realize ? (copy_step_subcase alpha c elseM) (R_copy_step_subcase alpha c RelseM).
68 else if current = 1,tt
77 else if current = null
86 definition nocopy_subcase ≝
87 ifTM STape (test_char ? (λx:STape.x == 〈null,true〉))
90 (seq ? (adv_to_mark_l … (is_marked ?))
92 (seq ? (move_r …) (adv_to_mark_r … (is_marked ?)))))))
95 definition R_nocopy_subcase ≝
98 t1 = midtape STape (l1@〈a0,false〉::〈x0,true〉::l2)
99 〈x,true〉 (〈a,false〉::l3) →
100 (∀c.memb ? c l1 = true → is_marked ? c = false) →
102 t2 = midtape ? (〈x,false〉::l1@〈a0,true〉::〈x0,false〉::l2) 〈a,true〉 l3) ∨
103 (x ≠ null ∧ t2 = t1).
105 axiom sem_nocopy_subcase : Realize ? nocopy_subcase R_nocopy_subcase.
107 cases (sem_if ? (test_char ? (λx:STape.x == 〈null,true〉)) ?????? tc_true
108 (sem_test_char ? (λx:STape.x == 〈null,true〉))
109 (sem_seq … (sem_adv_mark_r …)
110 (sem_seq … (sem_move_l …)
111 (sem_seq … (sem_adv_to_mark_l … (is_marked ?))
112 (sem_seq … (sem_adv_mark_r …)
113 (sem_seq … (sem_move_r …) (sem_adv_to_mark_r … (is_marked ?))
114 ))))) (sem_nop ?) intape)
115 #k * #outc * #Hloop #HR @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop] -Hloop
117 [| * #ta * whd in ⊢ (%→%→?); #Hta #Houtc
118 #ls #x #rs #Hintape %2 >Hintape in Hta; #Hta cases (Hta ? (refl ??)) -Hta #Hx #Hta %
119 [ lapply (\Pf Hx) @not_to_not #Hx' >Hx' %
124 definition copy_step ≝
125 ifTM ? (test_char STape (λc.is_bit (\fst c)))
126 (single_finalTM ? (copy_step_subcase FSUnialpha (bit false)
127 (copy_step_subcase FSUnialpha (bit true) nocopy_subcase)))
131 definition R_copy_step_true ≝
133 ∀ls,c,rs. t1 = midtape STape ls 〈c,true〉 rs →
134 bit_or_null c = true ∧
136 ls = (l1@〈a0,false〉::〈x0,true〉::l2) →
137 rs = (〈a,false〉::l3) →
140 t2 = midtape STape (〈bit x,false〉::l1@〈a0,true〉::〈bit x,false〉::l2) 〈a,true〉 l3) ∨
142 t2 = midtape ? (〈null,false〉::l1@〈a0,true〉::〈x0,false〉::l2) 〈a,true〉 l3))).
144 definition R_copy_step_false ≝
146 ∀ls,c,rs.t1 = midtape (FinProd … FSUnialpha FinBool) ls c rs →
147 bit_or_null (\fst c) = false ∧ t2 = t1.
149 axiom sem_copy_step :
150 accRealize ? copy_step (inr … (inl … (inr … 0))) R_copy_step_true R_copy_step_false.
153 1) il primo carattere è marcato
154 2) l'ultimo carattere è l'unico che può essere null, gli altri sono bit
155 3) il terminatore non è né bit, né null
158 definition copy0 ≝ whileTM ? copy_step (inr … (inl … (inr … 0))).
160 let rec merge_config (l1,l2:list STape) ≝
163 | cons p1 l1' ⇒ match l2 with
166 let 〈c1,b1〉 ≝ p1 in let 〈c2,b2〉 ≝ p2 in
168 [ null ⇒ p1 :: merge_config l1' l2'
169 | _ ⇒ p2 :: merge_config l1' l2' ] ] ].
171 definition R_copy0 ≝ λt1,t2.
172 ∀ls,c,c0,rs,l1,l3,l4.
173 t1 = midtape STape (l3@l4@〈c0,true〉::ls) 〈c,true〉 (l1@rs) →
174 no_marks l1 → no_marks (l3@l4) → |l1| = |l4| →
175 ∀l1',bv.〈c,false〉::l1 = l1'@[〈comma,bv〉] → only_bits_or_nulls l1' →
176 ∀l4',bg.l4@[〈c0,false〉] = 〈grid,bg〉::l4' → only_bits_or_nulls l4' →
177 (c = comma ∧ t2 = t1) ∨
179 t2 = midtape ? (reverse ? l1'@l3@〈grid,true〉::
180 merge_config l4' (reverse ? l1')@ls)
183 lemma wsem_copy0 : WRealize ? copy0 R_copy0.
184 #intape #k #outc #Hloop
185 lapply (sem_while … sem_copy_step intape k outc Hloop) [%] -Hloop
186 * #ta * #Hstar @(star_ind_l ??????? Hstar)
187 [ #tb whd in ⊢ (%→?); #Hleft
188 #ls #c #c0 #rs #l1 #l3 #l4 #Htb #Hl1nomarks #Hl3l4nomarks #Hlen #l1' #bv
189 #Hl1 #Hl1bits #l4' #bg #Hl4 #Hl4bits
190 cases (Hleft … Htb) -Hleft #Hc #Houtc % %
191 [ generalize in match Hl1bits; -Hl1bits cases l1' in Hl1;
192 [ normalize #Hl1 #c1 destruct (Hl1) %
193 | * #c' #b' #l0 #Heq normalize in Heq; destruct (Heq)
194 #Hl1bits lapply (Hl1bits 〈c',false〉 ?) [ @memb_hd ]
195 >Hc #Hfalse destruct ]
197 | #tb #tc #td whd in ⊢ (%→?→(?→%)→%→?); #Htc #Hstar1 #Hind #Htd
198 lapply (Hind Htd) -Hind #Hind
199 #ls #c #c0 #rs #l1 #l3 #l4 #Htb #Hl1nomarks #Hl3l4nomarks #Hlen #l1' #bv
200 #Hl1 #Hl1bits #l4' #bg #Hl4 #Hl4bits %2
201 cases (Htc … Htb) -Htc #Hcbitnull #Htc
202 % [ % #Hc' >Hc' in Hcbitnull; normalize #Hfalse destruct (Hfalse) ]
203 cut (|l1| = |reverse ? l4|) [@daemon] #Hlen1
204 @(list_cases_2 … Hlen1)
205 [ #Hl1nil @False_ind >Hl1nil in Hl1; cases l1' normalize
206 [ #Hl1 destruct normalize in Hcbitnull; destruct (Hcbitnull)
207 | #p0 #l0 normalize #Hfalse destruct (Hfalse) cases l0 in e0;
208 [ normalize #Hfalse1 destruct (Hfalse1)
209 | #p0' #l0' normalize #Hfalse1 destruct (Hfalse1) ] ]
210 | * #a #ba * #a0 #ba0 #l1'' #l4'' #Hl1cons #Hl4cons
211 lapply (eq_f ?? (reverse ?) ?? Hl4cons) >reverse_reverse >reverse_cons -Hl4cons #Hl4cons
212 cut (ba = false) [ @daemon ] #Hba
213 cut (ba0 = false) [ @daemon ] #Hba0
214 >Hba0 in Hl4cons; >Hba in Hl1cons; -Hba0 -Hba #Hl1cons #Hl4cons
215 >Hl4cons in Htc; >Hl1cons #Htc
216 lapply (Htc a (l3@reverse ? l4'') c0 a0 ls (l1''@rs) ? (refl ??) ?)
217 [ #x #Hx @Hl3l4nomarks >Hl4cons <associative_append
219 | >associative_append >associative_append %
222 lapply (Hind (〈bit x,false〉::ls) a a0 rs l1''
223 (〈bit x,false〉::l3) (reverse ? l4'') ????)
224 [ >Hl1cons in Hlen; >Hl4cons >length_append >commutative_plus
225 normalize #Hlen destruct (Hlen) //
226 | #x0 #Hx0 cases (orb_true_l … Hx0)
227 [ #Hx0eq >(\P Hx0eq) %
228 | -Hx0 #Hx0 @Hl3l4nomarks >Hl4cons
229 <associative_append @memb_append_l1 // ]
230 | #x0 #Hx0 @Hl1nomarks >Hl1cons @memb_cons //
231 | >Htc >associative_append %
233 cut (∃la.l1' = 〈c,false〉::la)
234 [ >Hl1cons in Hl1; cases l1'
235 [normalize #Hfalse destruct (Hfalse)
236 | #p #la normalize #Hla destruct (Hla) @(ex_intro ?? la) % ] ]
238 cut (∃lb.l4' = lb@[〈c0,false〉])
240 @(list_elim_left … l4')
241 (* si usa l'iniettività del "cons destro"
248 @(list_elim_left … l4')
249 <Hl1cons <Hl4cons #Hind lapply (Hind ?? Hl1 ??? Hl4 ?)
253 seq STape (move_l …) (seq ? (adv_to_mark_l … (is_marked ?))
254 (seq ? (clear_mark …) (seq ? (adv_to_mark_r … (is_marked ?)) (clear_mark …)))).
256 definition R_copy ≝ λt1,t2.
257 ∀ls,c,c0,rs,l1,l3,l4.
258 t1 = midtape STape (l3@〈grid,false〉::l4@〈c0,true〉::ls) 〈c,true〉 (l1@〈comma,false〉::rs) →
259 no_marks l1 → no_marks l3 → no_marks l4 → |l1| = |l4| →
260 only_bits_or_nulls (〈c0,true〉::l4) → only_bits_or_nulls (〈c,true〉::l1) →
261 t2 = midtape STape (reverse ? l1@l3@〈grid,false〉::
262 merge_config (l4@[〈c0,false〉]) (reverse ? (〈c,false〉::l1))@ls)
265 axiom sem_copy : Realize ? copy R_copy.