]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/mono.ma
comments
[helm.git] / matita / matita / lib / turing / mono.ma
1 (*
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.           
5     ||I||                                                            
6     ||T||  
7     ||A||  
8     \   /  This file is distributed under the terms of the       
9      \ /   GNU General Public License Version 2   
10       V_____________________________________________________________*)
11
12 include "basics/vectors.ma".
13 (* include "basics/relations.ma". *)
14
15 (******************************** tape ****************************************)
16
17 (* A tape is essentially a triple 〈left,current,right〉 where however the current 
18 symbol could be missing. This may happen for three different reasons: both tapes 
19 are empty; we are on the left extremity of a non-empty tape (left overflow), or 
20 we are on the right extremity of a non-empty tape (right overflow). *)
21
22 inductive tape (sig:FinSet) : Type[0] ≝ 
23 | niltape : tape sig
24 | leftof  : sig → list sig → tape sig
25 | rightof : sig → list sig → tape sig
26 | midtape : list sig → sig → list sig → tape sig.
27
28 definition left ≝ 
29  λsig.λt:tape sig.match t with
30  [ niltape ⇒ [] | leftof _ _ ⇒ [] | rightof s l ⇒ s::l | midtape l _ _ ⇒ l ].
31
32 definition right ≝ 
33  λsig.λt:tape sig.match t with
34  [ niltape ⇒ [] | leftof s r ⇒ s::r | rightof _ _ ⇒ []| midtape _ _ r ⇒ r ].
35  
36 definition current ≝ 
37  λsig.λt:tape sig.match t with
38  [ midtape _ c _ ⇒ Some ? c | _ ⇒ None ? ].
39  
40 definition mk_tape : 
41   ∀sig:FinSet.list sig → option sig → list sig → tape sig ≝ 
42   λsig,lt,c,rt.match c with
43   [ Some c' ⇒ midtape sig lt c' rt
44   | None ⇒ match lt with 
45     [ nil ⇒ match rt with
46       [ nil ⇒ niltape ?
47       | cons r0 rs0 ⇒ leftof ? r0 rs0 ]
48     | cons l0 ls0 ⇒ rightof ? l0 ls0 ] ].
49
50 inductive move : Type[0] ≝
51   | L : move | R : move | N : move.
52
53 (********************************** machine ***********************************)
54
55 record TM (sig:FinSet): Type[1] ≝ 
56 { states : FinSet;
57   trans : states × (option sig) → states × (option (sig × move));
58   start: states;
59   halt : states → bool
60 }.
61
62 definition tape_move_left ≝ λsig:FinSet.λlt:list sig.λc:sig.λrt:list sig.
63   match lt with
64   [ nil ⇒ leftof sig c rt
65   | cons c0 lt0 ⇒ midtape sig lt0 c0 (c::rt) ].
66   
67 definition tape_move_right ≝ λsig:FinSet.λlt:list sig.λc:sig.λrt:list sig.
68   match rt with
69   [ nil ⇒ rightof sig c lt
70   | cons c0 rt0 ⇒ midtape sig (c::lt) c0 rt0 ].
71
72 definition tape_move ≝ λsig.λt: tape sig.λm:option (sig × move).
73   match m with
74   [ None ⇒ t
75   | Some m' ⇒ 
76     let 〈s,m1〉 ≝ m' in 
77     match m1 with
78       [ R ⇒ tape_move_right ? (left ? t) s (right ? t)
79       | L ⇒ tape_move_left ? (left ? t) s (right ? t)
80       | N ⇒ midtape ? (left ? t) s (right ? t)
81       ] ].
82
83 record config (sig,states:FinSet): Type[0] ≝ 
84 { cstate : states;
85   ctape: tape sig
86 }.
87   
88 definition step ≝ λsig.λM:TM sig.λc:config sig (states sig M).
89   let current_char ≝ current ? (ctape ?? c) in
90   let 〈news,mv〉 ≝ trans sig M 〈cstate ?? c,current_char〉 in
91   mk_config ?? news (tape_move sig (ctape ?? c) mv).
92
93 (******************************** loop ****************************************)
94 let rec loop (A:Type[0]) n (f:A→A) p a on n ≝
95   match n with 
96   [ O ⇒ None ?
97   | S m ⇒ if p a then (Some ? a) else loop A m f p (f a)
98   ].
99   
100 lemma loop_S_true : 
101   ∀A,n,f,p,a. p a = true → 
102     loop A (S n) f p a = Some ? a.
103 #A #n #f #p #a #pa normalize >pa //
104 qed.
105
106 lemma loop_S_false : 
107   ∀A,n,f,p,a.  p a = false → 
108     loop A (S n) f p a = loop A n f p (f a).
109 normalize #A #n #f #p #a #Hpa >Hpa %
110 qed.  
111   
112 lemma loop_incr : ∀A,f,p,k1,k2,a1,a2. 
113   loop A k1 f p a1 = Some ? a2 → 
114     loop A (k2+k1) f p a1 = Some ? a2.
115 #A #f #p #k1 #k2 #a1 #a2 generalize in match a1; elim k1
116 [normalize #a0 #Hfalse destruct
117 |#k1' #IH #a0 <plus_n_Sm whd in ⊢ (??%? → ??%?);
118  cases (true_or_false (p a0)) #Hpa0 >Hpa0 whd in ⊢ (??%? → ??%?); // @IH
119 ]
120 qed.
121
122 lemma loop_merge : ∀A,f,p,q.(∀b. p b = false → q b = false) →
123  ∀k1,k2,a1,a2,a3,a4.
124    loop A k1 f p a1 = Some ? a2 → 
125      f a2 = a3 → q a2 = false → 
126        loop A k2 f q a3 = Some ? a4 →
127          loop A (k1+k2) f q a1 = Some ? a4.
128 #Sig #f #p #q #Hpq #k1 elim k1 
129   [normalize #k2 #a1 #a2 #a3 #a4 #H destruct
130   |#k1' #Hind #k2 #a1 #a2 #a3 #a4 normalize in ⊢ (%→?);
131    cases (true_or_false (p a1)) #pa1 >pa1 normalize in ⊢ (%→?);
132    [#eqa1a2 destruct #eqa2a3 #Hqa2 #H
133     whd in ⊢ (??(??%???)?); >plus_n_Sm @loop_incr
134     whd in ⊢ (??%?); >Hqa2 >eqa2a3 @H
135    |normalize >(Hpq … pa1) normalize #H1 #H2 #H3 @(Hind … H2) //
136    ]
137  ]
138 qed.
139
140 lemma loop_split : ∀A,f,p,q.(∀b. q b = true → p b = true) →
141  ∀k,a1,a2.
142    loop A k f q a1 = Some ? a2 → 
143    ∃k1,a3.
144     loop A k1 f p a1 = Some ? a3 ∧ 
145       loop A (S(k-k1)) f q a3 = Some ? a2.
146 #A #f #p #q #Hpq #k elim k
147   [#a1 #a2 normalize #Heq destruct
148   |#i #Hind #a1 #a2 normalize 
149    cases (true_or_false (q a1)) #Hqa1 >Hqa1 normalize
150     [ #Ha1a2 destruct
151      @(ex_intro … 1) @(ex_intro … a2) % 
152        [normalize >(Hpq …Hqa1) // |>Hqa1 //]
153     |#Hloop cases (true_or_false (p a1)) #Hpa1 
154        [@(ex_intro … 1) @(ex_intro … a1) % 
155          [normalize >Hpa1 // |>Hqa1 <Hloop normalize //]
156        |cases (Hind …Hloop) #k2 * #a3 * #Hloop1 #Hloop2
157         @(ex_intro … (S k2)) @(ex_intro … a3) %
158          [normalize >Hpa1 normalize // | @Hloop2 ]
159        ]
160     ]
161   ]
162 qed.
163
164 lemma loop_eq : ∀sig,f,q,i,j,a,x,y. 
165   loop sig i f q a = Some ? x → loop sig j f q a = Some ? y → x = y.
166 #sig #f #q #i #j @(nat_elim2 … i j)
167 [ #n #a #x #y normalize #Hfalse destruct (Hfalse)
168 | #n #a #x #y #H1 normalize #Hfalse destruct (Hfalse)
169 | #n1 #n2 #IH #a #x #y normalize cases (q a) normalize
170   [ #H1 #H2 destruct %
171   | /2/ ]
172 ]
173 qed.
174
175 (************************** Realizability *************************************)
176 definition loopM ≝ λsig,M,i,cin.
177   loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) cin.
178
179 definition initc ≝ λsig.λM:TM sig.λt.
180   mk_config sig (states sig M) (start sig M) t.
181
182 definition Realize ≝ λsig.λM:TM sig.λR:relation (tape sig).
183 ∀t.∃i.∃outc.
184   loopM sig M i (initc sig M t) = Some ? outc ∧ R t (ctape ?? outc).
185
186 definition WRealize ≝ λsig.λM:TM sig.λR:relation (tape sig).
187 ∀t,i,outc.
188   loopM sig M i (initc sig M t) = Some ? outc → R t (ctape ?? outc).
189
190 definition Terminate ≝ λsig.λM:TM sig.λt. ∃i,outc.
191   loopM sig M i (initc sig M t) = Some ? outc.
192
193 lemma WRealize_to_Realize : ∀sig.∀M: TM sig.∀R.
194   (∀t.Terminate sig M t) → WRealize sig M R → Realize sig M R.
195 #sig #M #R #HT #HW #t cases (HT … t) #i * #outc #Hloop 
196 @(ex_intro … i) @(ex_intro … outc) % // @(HW … i) //
197 qed.
198
199 theorem Realize_to_WRealize : ∀sig,M,R.
200   Realize sig M R → WRealize sig M R.
201 #sig #M #R #H1 #inc #i #outc #Hloop 
202 cases (H1 inc) #k * #outc1 * #Hloop1 #HR >(loop_eq … Hloop Hloop1) //
203 qed.
204
205 definition accRealize ≝ λsig.λM:TM sig.λacc:states sig M.λRtrue,Rfalse.
206 ∀t.∃i.∃outc.
207   loopM sig M i (initc sig M t) = Some ? outc ∧
208     (cstate ?? outc = acc → Rtrue t (ctape ?? outc)) ∧ 
209     (cstate ?? outc ≠ acc → Rfalse t (ctape ?? outc)).
210
211 (******************************** NOP Machine *********************************)
212
213 (* NO OPERATION
214    t1 = t2 *)
215   
216 definition nop_states ≝ initN 1.
217 definition start_nop : initN 1 ≝ mk_Sig ?? 0 (le_n … 1).
218
219 definition nop ≝ 
220   λalpha:FinSet.mk_TM alpha nop_states
221   (λp.let 〈q,a〉 ≝ p in 〈q,None ?〉)
222   start_nop (λ_.true).
223   
224 definition R_nop ≝ λalpha.λt1,t2:tape alpha.t2 = t1.
225
226 lemma sem_nop :
227   ∀alpha.Realize alpha (nop alpha) (R_nop alpha).
228 #alpha #intape @(ex_intro ?? 1) 
229 @(ex_intro … (mk_config ?? start_nop intape)) % % 
230 qed.
231
232 (************************** Sequential Composition ****************************)
233
234 definition seq_trans ≝ λsig. λM1,M2 : TM sig. 
235 λp. let 〈s,a〉 ≝ p in
236   match s with 
237   [ inl s1 ⇒ 
238       if halt sig M1 s1 then 〈inr … (start sig M2), None ?〉
239       else 
240       let 〈news1,m〉 ≝ trans sig M1 〈s1,a〉 in
241       〈inl … news1,m〉
242   | inr s2 ⇒ 
243       let 〈news2,m〉 ≝ trans sig M2 〈s2,a〉 in
244       〈inr … news2,m〉
245   ].
246  
247 definition seq ≝ λsig. λM1,M2 : TM sig. 
248   mk_TM sig 
249     (FinSum (states sig M1) (states sig M2))
250     (seq_trans sig M1 M2) 
251     (inl … (start sig M1))
252     (λs.match s with
253       [ inl _ ⇒ false | inr s2 ⇒ halt sig M2 s2]). 
254
255 definition Rcomp ≝ λA.λR1,R2:relation A.λa1,a2.
256   ∃am.R1 a1 am ∧ R2 am a2.
257   
258 interpretation "relation composition" 'compose R1 R2 = (Rcomp ? R1 R2).
259
260 definition lift_confL ≝ 
261   λsig,S1,S2,c.match c with 
262   [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inl … s) t ].
263   
264 definition lift_confR ≝ 
265   λsig,S1,S2,c.match c with
266   [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inr … s) t ].
267   
268 definition halt_liftL ≝ 
269   λS1,S2,halt.λs:FinSum S1 S2.
270   match s with
271   [ inl s1 ⇒ halt s1
272   | inr _ ⇒ true ]. (* should be vacuous in all cases we use halt_liftL *)
273
274 definition halt_liftR ≝ 
275   λS1,S2,halt.λs:FinSum S1 S2.
276   match s with
277   [ inl _ ⇒ false 
278   | inr s2 ⇒ halt s2 ].
279       
280 lemma p_halt_liftL : ∀sig,S1,S2,halt,c.
281   halt (cstate sig S1 c) =
282      halt_liftL S1 S2 halt (cstate … (lift_confL … c)).
283 #sig #S1 #S2 #halt #c cases c #s #t %
284 qed.
285
286 lemma trans_liftL : ∀sig,M1,M2,s,a,news,move.
287   halt ? M1 s = false → 
288   trans sig M1 〈s,a〉 = 〈news,move〉 → 
289   trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inl … news,move〉.
290 #sig (*#M1*) * #Q1 #T1 #init1 #halt1 #M2 #s #a #news #move
291 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
292 qed.
293
294 lemma trans_liftR : ∀sig,M1,M2,s,a,news,move.
295   halt ? M2 s = false → 
296   trans sig M2 〈s,a〉 = 〈news,move〉 → 
297   trans sig (seq sig M1 M2) 〈inr … s,a〉 = 〈inr … news,move〉.
298 #sig #M1 * #Q2 #T2 #init2 #halt2 #s #a #news #move
299 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
300 qed.
301
302 lemma config_eq : 
303   ∀sig,M,c1,c2.
304   cstate sig M c1 = cstate sig M c2 → 
305   ctape sig M c1 = ctape sig M c2 →  c1 = c2.
306 #sig #M1 * #s1 #t1 * #s2 #t2 //
307 qed.
308
309 lemma step_lift_confR : ∀sig,M1,M2,c0.
310  halt ? M2 (cstate ?? c0) = false → 
311  step sig (seq sig M1 M2) (lift_confR sig (states ? M1) (states ? M2) c0) =
312  lift_confR sig (states ? M1) (states ? M2) (step sig M2 c0).
313 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s #t
314   lapply (refl ? (trans ?? 〈s,current sig t〉))
315   cases (trans ?? 〈s,current sig t〉) in ⊢ (???% → %);
316   #s0 #m0 cases t
317   [ #Heq #Hhalt
318   | 2,3: #s1 #l1 #Heq #Hhalt 
319   |#ls #s1 #rs #Heq #Hhalt ]
320   whd in ⊢ (???(????%)); >Heq
321   whd in ⊢ (???%);
322   whd in ⊢ (??(???%)?); whd in ⊢ (??%?);
323   >(trans_liftR … Heq) //
324 qed.
325
326 lemma step_lift_confL : ∀sig,M1,M2,c0.
327  halt ? M1 (cstate ?? c0) = false → 
328  step sig (seq sig M1 M2) (lift_confL sig (states ? M1) (states ? M2) c0) =
329  lift_confL sig ?? (step sig M1 c0).
330 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s #t
331   lapply (refl ? (trans ?? 〈s,current sig t〉))
332   cases (trans ?? 〈s,current sig t〉) in ⊢ (???% → %);
333   #s0 #m0 cases t
334   [ #Heq #Hhalt
335   | 2,3: #s1 #l1 #Heq #Hhalt 
336   |#ls #s1 #rs #Heq #Hhalt ]
337   whd in ⊢ (???(????%)); >Heq
338   whd in ⊢ (???%);
339   whd in ⊢ (??(???%)?); whd in ⊢ (??%?);
340   >(trans_liftL … Heq) //
341 qed.
342
343 lemma loop_lift : ∀A,B,k,lift,f,g,h,hlift,c1,c2.
344   (∀x.hlift (lift x) = h x) → 
345   (∀x.h x = false → lift (f x) = g (lift x)) → 
346   loop A k f h c1 = Some ? c2 → 
347   loop B k g hlift (lift c1) = Some ? (lift … c2).
348 #A #B #k #lift #f #g #h #hlift #c1 #c2 #Hfg #Hhlift
349 generalize in match c1; elim k
350 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
351 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
352  cases (true_or_false (h c0)) #Hc0 >Hfg >Hc0
353  [ normalize #Heq destruct (Heq) %
354  | normalize <Hhlift // @IH ]
355 qed.
356
357 (* 
358 lemma loop_liftL : ∀sig,k,M1,M2,c1,c2.
359   loop ? k (step sig M1) (λc.halt sig M1 (cstate ?? c)) c1 = Some ? c2 →
360     loop ? k (step sig (seq sig M1 M2)) 
361       (λc.halt_liftL ?? (halt sig M1) (cstate ?? c)) (lift_confL … c1) = 
362     Some ? (lift_confL … c2).
363 #sig #k #M1 #M2 #c1 #c2 generalize in match c1;
364 elim k
365 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
366 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
367  cases (true_or_false (halt ?? (cstate sig (states ? M1) c0))) #Hc0 >Hc0
368  [ >(?: halt_liftL ?? (halt sig M1) (cstate sig ? (lift_confL … c0)) = true)
369    [ whd in ⊢ (??%? → ??%?); #Hc2 destruct (Hc2) %
370    | <Hc0 cases c0 // ]
371  | >(?: halt_liftL ?? (halt sig M1) (cstate ?? (lift_confL … c0)) = false)
372    [whd in ⊢ (??%? → ??%?); #Hc2 <(IH ? Hc2) @eq_f
373     @step_lift_confL //
374    | <Hc0 cases c0 // ]
375 qed.
376
377 lemma loop_liftR : ∀sig,k,M1,M2,c1,c2.
378   loop ? k (step sig M2) (λc.halt sig M2 (cstate ?? c)) c1 = Some ? c2 →
379     loop ? k (step sig (seq sig M1 M2)) 
380       (λc.halt sig (seq sig M1 M2) (cstate ?? c)) (lift_confR … c1) = 
381     Some ? (lift_confR … c2).
382 #sig #k #M1 #M2 #c1 #c2 generalize in match c1;
383 elim k
384 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
385 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
386  cases (true_or_false (halt ?? (cstate sig ? c0))) #Hc0 >Hc0
387  [ >(?: halt ? (seq sig M1 M2) (cstate sig ? (lift_confR … c0)) = true)
388    [ whd in ⊢ (??%? → ??%?); #Hc2 destruct (Hc2) %
389    | <Hc0 cases c0 // ]
390  | >(?: halt ? (seq sig M1 M2) (cstate sig ? (lift_confR … c0)) = false)
391    [whd in ⊢ (??%? → ??%?); #Hc2 <(IH ? Hc2) @eq_f
392     @step_lift_confR //
393    | <Hc0 cases c0 // ]
394  ]
395 qed.  
396
397 *)
398     
399 lemma loop_Some : 
400   ∀A,k,f,p,a,b.loop A k f p a = Some ? b → p b = true.
401 #A #k #f #p elim k 
402 [#a #b normalize #Hfalse destruct
403 |#k0 #IH #a #b whd in ⊢ (??%? → ?); cases (true_or_false (p a)) #Hpa
404  [ >Hpa normalize #H1 destruct //
405  | >Hpa normalize @IH
406  ]
407 ]
408 qed. 
409
410 lemma trans_liftL_true : ∀sig,M1,M2,s,a.
411   halt ? M1 s = true → 
412   trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inr … (start ? M2),None ?〉.
413 #sig #M1 #M2 #s #a
414 #Hhalt whd in ⊢ (??%?); >Hhalt %
415 qed.
416
417 lemma eq_ctape_lift_conf_L : ∀sig,S1,S2,outc.
418   ctape sig (FinSum S1 S2) (lift_confL … outc) = ctape … outc.
419 #sig #S1 #S2 #outc cases outc #s #t %
420 qed.
421   
422 lemma eq_ctape_lift_conf_R : ∀sig,S1,S2,outc.
423   ctape sig (FinSum S1 S2) (lift_confR … outc) = ctape … outc.
424 #sig #S1 #S2 #outc cases outc #s #t %
425 qed.
426
427 theorem sem_seq: ∀sig,M1,M2,R1,R2.
428   Realize sig M1 R1 → Realize sig M2 R2 → 
429     Realize sig (seq sig M1 M2) (R1 ∘ R2).
430 #sig #M1 #M2 #R1 #R2 #HR1 #HR2 #t 
431 cases (HR1 t) #k1 * #outc1 * #Hloop1 #HM1
432 cases (HR2 (ctape sig (states ? M1) outc1)) #k2 * #outc2 * #Hloop2 #HM2
433 @(ex_intro … (k1+k2)) @(ex_intro … (lift_confR … outc2))
434 %
435 [@(loop_merge ??????????? 
436    (loop_lift ??? (lift_confL sig (states sig M1) (states sig M2))
437    (step sig M1) (step sig (seq sig M1 M2)) 
438    (λc.halt sig M1 (cstate … c)) 
439    (λc.halt_liftL ?? (halt sig M1) (cstate … c)) … Hloop1))
440   [ * *
441    [ #sl #tl whd in ⊢ (??%? → ?); #Hl %
442    | #sr #tr whd in ⊢ (??%? → ?); #Hr destruct (Hr) ]
443   || #c0 #Hhalt <step_lift_confL //
444   | #x <p_halt_liftL %
445   |6:cases outc1 #s1 #t1 %
446   |7:@(loop_lift … (initc ?? (ctape … outc1)) … Hloop2) 
447     [ * #s2 #t2 %
448     | #c0 #Hhalt <step_lift_confR // ]
449   |whd in ⊢ (??(???%)?);whd in ⊢ (??%?);
450    generalize in match Hloop1; cases outc1 #sc1 #tc1 #Hloop10 
451    >(trans_liftL_true sig M1 M2 ??) 
452     [ whd in ⊢ (??%?); whd in ⊢ (???%);
453       @config_eq whd in ⊢ (???%); //
454     | @(loop_Some ?????? Hloop10) ]
455  ]
456 | @(ex_intro … (ctape ? (FinSum (states ? M1) (states ? M2)) (lift_confL … outc1)))
457   % // >eq_ctape_lift_conf_L >eq_ctape_lift_conf_R //
458 ]
459 qed.
460