]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/wmono.ma
decentralizing core notation
[helm.git] / matita / matita / lib / turing / wmono.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/finset.ma".
14 include "basics/core_notation/compose_2.ma".
15 (* include "basics/relations.ma". *)
16
17 record tape (sig:FinSet): Type[0] ≝ 
18 { left : list sig;
19   right: list sig
20 }.
21
22 inductive move : Type[0] ≝
23 | L : move 
24 | R : move
25 .
26
27 (* We do not distinuish an input tape *)
28
29 record TM (sig:FinSet): Type[1] ≝ 
30 { states : FinSet;
31   trans : states × (option sig) → states × (option (sig × move));
32   start: states;
33   halt : states → bool
34 }.
35
36 record config (sig,states:FinSet): Type[0] ≝ 
37 { cstate : states;
38   ctape: tape sig
39 }.
40
41 definition option_hd ≝ λA.λl:list A.
42   match l with
43   [nil ⇒ None ?
44   |cons a _ ⇒ Some ? a
45   ].
46
47 definition tape_move ≝ λsig.λt: tape sig.λm:option (sig × move).
48   match m with 
49   [ None ⇒ t
50   | Some m1 ⇒ 
51     match \snd m1 with
52     [ R ⇒ mk_tape sig ((\fst m1)::(left ? t)) (tail ? (right ? t))
53     | L ⇒ mk_tape sig (tail ? (left ? t)) ((\fst m1)::(right ? t))
54     ]
55   ].
56
57 definition step ≝ λsig.λM:TM sig.λc:config sig (states sig M).
58   let current_char ≝ option_hd ? (right ? (ctape ?? c)) in
59   let 〈news,mv〉 ≝ trans sig M 〈cstate ?? c,current_char〉 in
60   mk_config ?? news (tape_move sig (ctape ?? c) mv).
61   
62 let rec loop (A:Type[0]) n (f:A→A) p a on n ≝
63   match n with 
64   [ O ⇒ None ?
65   | S m ⇒ if p a then (Some ? a) else loop A m f p (f a)
66   ].
67   
68 lemma loop_incr : ∀A,f,p,k1,k2,a1,a2. 
69   loop A k1 f p a1 = Some ? a2 → 
70     loop A (k2+k1) f p a1 = Some ? a2.
71 #A #f #p #k1 #k2 #a1 #a2 generalize in match a1; elim k1
72 [normalize #a0 #Hfalse destruct
73 |#k1' #IH #a0 <plus_n_Sm whd in ⊢ (??%? → ??%?);
74  cases (true_or_false (p a0)) #Hpa0 >Hpa0 whd in ⊢ (??%? → ??%?); // @IH
75 ]
76 qed.
77
78 lemma loop_split : ∀A,f,p,q.(∀b. q b = true → p b = true) →
79  ∀k,a1,a2.
80    loop A k f q a1 = Some ? a2 → 
81    ∃k1,a3.
82     loop A k1 f p a1 = Some ? a3 ∧ 
83       loop A (S(k-k1)) f q a3 = Some ? a2.
84 #A #f #p #q #Hpq #k elim k
85   [#a1 #a2 normalize #Heq destruct
86   |#i #Hind #a1 #a2 normalize 
87    cases (true_or_false (q a1)) #Hqa1 >Hqa1 normalize
88     [ #Ha1a2 destruct
89      @(ex_intro … 1) @(ex_intro … a2) % 
90        [normalize >(Hpq …Hqa1) // |>Hqa1 //]
91     |#Hloop cases (true_or_false (p a1)) #Hpa1 
92        [@(ex_intro … 1) @(ex_intro … a1) % 
93          [normalize >Hpa1 // |>Hqa1 <Hloop normalize //]
94        |cases (Hind …Hloop) #k2 * #a3 * #Hloop1 #Hloop2
95         @(ex_intro … (S k2)) @(ex_intro … a3) %
96          [normalize >Hpa1 normalize // | @Hloop2 ]
97        ]
98     ]
99   ]
100 qed.
101    
102 (* 
103 lemma loop_split : ∀A,f,p,q.(∀b. p b = false → q b = false) →
104  ∀k1,k2,a1,a2,a3,a4.
105    loop A k1 f p a1 = Some ? a2 → 
106      f a2 = a3 → q a2 = false → 
107        loop A k2 f q a3 = Some ? a4 →
108          loop A (k1+k2) f q a1 = Some ? a4.
109 #Sig #f #p #q #Hpq #k1 elim k1 
110   [normalize #k2 #a1 #a2 #a3 #a4 #H destruct
111   |#k1' #Hind #k2 #a1 #a2 #a3 #a4 normalize in ⊢ (%→?);
112    cases (true_or_false (p a1)) #pa1 >pa1 normalize in ⊢ (%→?);
113    [#eqa1a2 destruct #eqa2a3 #Hqa2 #H
114     whd in ⊢ (??(??%???)?); >plus_n_Sm @loop_incr
115     whd in ⊢ (??%?); >Hqa2 >eqa2a3 @H
116    |normalize >(Hpq … pa1) normalize 
117     #H1 #H2 #H3 @(Hind … H2) //
118    ]
119  ]
120 qed. *)
121
122 (*
123 lemma loop_split : ∀A,f,p,q.(∀b. p b = false → q b = false) →
124  ∀k1,k2,a1,a2,a3.
125    loop A k1 f p a1 = Some ? a2 → 
126      loop A k2 f q a2 = Some ? a3 →
127        loop A (k1+k2) f q a1 = Some ? a3.
128 #Sig #f #p #q #Hpq #k1 elim k1 
129   [normalize #k2 #a1 #a2 #a3 #H destruct
130   |#k1' #Hind #k2 #a1 #a2 #a3 normalize in ⊢ (%→?→?);
131    cases (true_or_false (p a1)) #pa1 >pa1 normalize in ⊢ (%→?);
132    [#eqa1a2 destruct #H @loop_incr //
133    |normalize >(Hpq … pa1) normalize 
134     #H1 #H2 @(Hind … H2) //
135    ]
136  ]
137 qed.
138 *)
139
140 definition initc ≝ λsig.λM:TM sig.λt.
141   mk_config sig (states sig M) (start sig M) t.
142
143 definition Realize ≝ λsig.λM:TM sig.λR:relation (tape sig).
144 ∀t.∀i.∀outc.
145   loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc →
146   R t (ctape ?? outc).
147
148
149 definition accRealize ≝ λsig.λM:TM sig.λacc:states sig M.λRtrue,Rfalse:relation (tape sig).
150 ∀t.∃i.∃outc.
151   loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc ∧
152   (cstate ?? outc = acc → Rtrue t (ctape ?? outc)) ∧ 
153   (cstate ?? outc ≠ acc → Rfalse t (ctape ?? outc)).
154
155 (* Compositions *)
156
157 definition seq_trans ≝ λsig. λM1,M2 : TM sig. 
158 λp. let 〈s,a〉 ≝ p in
159   match s with 
160   [ inl s1 ⇒ 
161       if halt sig M1 s1 then 〈inr … (start sig M2), None ?〉
162       else 
163       let 〈news1,m〉 ≝ trans sig M1 〈s1,a〉 in
164       〈inl … news1,m〉
165   | inr s2 ⇒ 
166       let 〈news2,m〉 ≝ trans sig M2 〈s2,a〉 in
167       〈inr … news2,m〉
168   ].
169  
170 definition seq ≝ λsig. λM1,M2 : TM sig. 
171   mk_TM sig 
172     (FinSum (states sig M1) (states sig M2))
173     (seq_trans sig M1 M2) 
174     (inl … (start sig M1))
175     (λs.match s with
176       [ inl _ ⇒ false | inr s2 ⇒ halt sig M2 s2]). 
177
178 definition Rcomp ≝ λA.λR1,R2:relation A.λa1,a2.
179   ∃am.R1 a1 am ∧ R2 am a2.
180
181 (*
182 definition injectRl ≝ λsig.λM1.λM2.λR.
183    λc1,c2. ∃c11,c12. 
184      inl … (cstate sig M1 c11) = cstate sig (seq sig M1 M2) c1 ∧ 
185      inl … (cstate sig M1 c12) = cstate sig (seq sig M1 M2) c2 ∧
186      ctape sig M1 c11 = ctape sig (seq sig M1 M2) c1 ∧ 
187      ctape sig M1 c12 = ctape sig (seq sig M1 M2) c2 ∧ 
188      R c11 c12.
189
190 definition injectRr ≝ λsig.λM1.λM2.λR.
191    λc1,c2. ∃c21,c22. 
192      inr … (cstate sig M2 c21) = cstate sig (seq sig M1 M2) c1 ∧ 
193      inr … (cstate sig M2 c22) = cstate sig (seq sig M1 M2) c2 ∧
194      ctape sig M2 c21 = ctape sig (seq sig M1 M2) c1 ∧ 
195      ctape sig M2 c22 = ctape sig (seq sig M1 M2) c2 ∧ 
196      R c21 c22.
197      
198 definition Rlink ≝ λsig.λM1,M2.λc1,c2.
199   ctape sig (seq sig M1 M2) c1 = ctape sig (seq sig M1 M2) c2 ∧
200   cstate sig (seq sig M1 M2) c1 = inl … (halt sig M1) ∧
201   cstate sig (seq sig M1 M2) c2 = inr … (start sig M2). *)
202   
203 interpretation "relation composition" 'compose R1 R2 = (Rcomp ? R1 R2).
204
205 definition lift_confL ≝ 
206   λsig,S1,S2,c.match c with 
207   [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inl … s) t ].
208   
209 definition lift_confR ≝ 
210   λsig,S1,S2,c.match c with
211   [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inr … s) t ].
212   
213 definition halt_liftL ≝ 
214   λS1,S2,halt.λs:FinSum S1 S2.
215   match s with
216   [ inl s1 ⇒ halt s1
217   | inr _ ⇒ true ]. (* should be vacuous in all cases we use halt_liftL *)
218
219 definition halt_liftR ≝ 
220   λS1,S2,halt.λs:FinSum S1 S2.
221   match s with
222   [ inl _ ⇒ false 
223   | inr s2 ⇒ halt s2 ].
224       
225 lemma p_halt_liftL : ∀sig,S1,S2,halt,c.
226   halt (cstate sig S1 c) =
227      halt_liftL S1 S2 halt (cstate … (lift_confL … c)).
228 #sig #S1 #S2 #halt #c cases c #s #t %
229 qed.
230
231 lemma trans_liftL : ∀sig,M1,M2,s,a,news,move.
232   halt ? M1 s = false → 
233   trans sig M1 〈s,a〉 = 〈news,move〉 → 
234   trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inl … news,move〉.
235 #sig (*#M1*) * #Q1 #T1 #init1 #halt1 #M2 #s #a #news #move
236 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
237 qed.
238
239 lemma trans_liftR : ∀sig,M1,M2,s,a,news,move.
240   halt ? M2 s = false → 
241   trans sig M2 〈s,a〉 = 〈news,move〉 → 
242   trans sig (seq sig M1 M2) 〈inr … s,a〉 = 〈inr … news,move〉.
243 #sig #M1 * #Q2 #T2 #init2 #halt2 #s #a #news #move
244 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
245 qed.
246
247 lemma config_eq : 
248   ∀sig,M,c1,c2.
249   cstate sig M c1 = cstate sig M c2 → 
250   ctape sig M c1 = ctape sig M c2 →  c1 = c2.
251 #sig #M1 * #s1 #t1 * #s2 #t2 //
252 qed.
253
254 lemma step_lift_confR : ∀sig,M1,M2,c0.
255  halt ? M2 (cstate ?? c0) = false → 
256  step sig (seq sig M1 M2) (lift_confR sig (states ? M1) (states ? M2) c0) =
257  lift_confR sig (states ? M1) (states ? M2) (step sig M2 c0).
258 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s * #lt
259 #rs #Hhalt
260 whd in ⊢ (???(????%));whd in ⊢ (???%);
261 lapply (refl ? (trans ?? 〈s,option_hd sig rs〉))
262 cases (trans ?? 〈s,option_hd sig rs〉) in ⊢ (???% → %);
263 #s0 #m0 #Heq whd in ⊢ (???%);
264 whd in ⊢ (??(???%)?); whd in ⊢ (??%?);
265 >(trans_liftR … Heq)
266 [% | //]
267 qed.
268
269 lemma step_lift_confL : ∀sig,M1,M2,c0.
270  halt ? M1 (cstate ?? c0) = false → 
271  step sig (seq sig M1 M2) (lift_confL sig (states ? M1) (states ? M2) c0) =
272  lift_confL sig ?? (step sig M1 c0).
273 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s * #lt
274 #rs #Hhalt
275 whd in ⊢ (???(????%));whd in ⊢ (???%);
276 lapply (refl ? (trans ?? 〈s,option_hd sig rs〉))
277 cases (trans ?? 〈s,option_hd sig rs〉) in ⊢ (???% → %);
278 #s0 #m0 #Heq whd in ⊢ (???%);
279 whd in ⊢ (??(???%)?); whd in ⊢ (??%?);
280 >(trans_liftL … Heq)
281 [% | //]
282 qed.
283
284 lemma loop_lift : ∀A,B,k,lift,f,g,h,hlift,c1,c2.
285   (∀x.hlift (lift x) = h x) → 
286   (∀x.h x = false → lift (f x) = g (lift x)) → 
287   loop A k f h c1 = Some ? c2 → 
288   loop B k g hlift (lift c1) = Some ? (lift … c2).
289 #A #B #k #lift #f #g #h #hlift #c1 #c2 #Hfg #Hhlift
290 generalize in match c1; elim k
291 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
292 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
293  cases (true_or_false (h c0)) #Hc0 >Hfg >Hc0
294  [ normalize #Heq destruct (Heq) %
295  | normalize <Hhlift // @IH ]
296 qed.
297
298 (* 
299 lemma loop_liftL : ∀sig,k,M1,M2,c1,c2.
300   loop ? k (step sig M1) (λc.halt sig M1 (cstate ?? c)) c1 = Some ? c2 →
301     loop ? k (step sig (seq sig M1 M2)) 
302       (λc.halt_liftL ?? (halt sig M1) (cstate ?? c)) (lift_confL … c1) = 
303     Some ? (lift_confL … c2).
304 #sig #k #M1 #M2 #c1 #c2 generalize in match c1;
305 elim k
306 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
307 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
308  cases (true_or_false (halt ?? (cstate sig (states ? M1) c0))) #Hc0 >Hc0
309  [ >(?: halt_liftL ?? (halt sig M1) (cstate sig ? (lift_confL … c0)) = true)
310    [ whd in ⊢ (??%? → ??%?); #Hc2 destruct (Hc2) %
311    | <Hc0 cases c0 // ]
312  | >(?: halt_liftL ?? (halt sig M1) (cstate ?? (lift_confL … c0)) = false)
313    [whd in ⊢ (??%? → ??%?); #Hc2 <(IH ? Hc2) @eq_f
314     @step_lift_confL //
315    | <Hc0 cases c0 // ]
316 qed.
317
318 lemma loop_liftR : ∀sig,k,M1,M2,c1,c2.
319   loop ? k (step sig M2) (λc.halt sig M2 (cstate ?? c)) c1 = Some ? c2 →
320     loop ? k (step sig (seq sig M1 M2)) 
321       (λc.halt sig (seq sig M1 M2) (cstate ?? c)) (lift_confR … c1) = 
322     Some ? (lift_confR … c2).
323 #sig #k #M1 #M2 #c1 #c2 generalize in match c1;
324 elim k
325 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
326 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
327  cases (true_or_false (halt ?? (cstate sig ? c0))) #Hc0 >Hc0
328  [ >(?: halt ? (seq sig M1 M2) (cstate sig ? (lift_confR … c0)) = true)
329    [ whd in ⊢ (??%? → ??%?); #Hc2 destruct (Hc2) %
330    | <Hc0 cases c0 // ]
331  | >(?: halt ? (seq sig M1 M2) (cstate sig ? (lift_confR … c0)) = false)
332    [whd in ⊢ (??%? → ??%?); #Hc2 <(IH ? Hc2) @eq_f
333     @step_lift_confR //
334    | <Hc0 cases c0 // ]
335  ]
336 qed.  
337
338 *)
339     
340 lemma loop_Some : 
341   ∀A,k,f,p,a,b.loop A k f p a = Some ? b → p b = true.
342 #A #k #f #p elim k 
343 [#a #b normalize #Hfalse destruct
344 |#k0 #IH #a #b whd in ⊢ (??%? → ?); cases (true_or_false (p a)) #Hpa
345  [ >Hpa normalize #H1 destruct //
346  | >Hpa normalize @IH
347  ]
348 ]
349 qed. 
350
351 lemma trans_liftL_true : ∀sig,M1,M2,s,a.
352   halt ? M1 s = true → 
353   trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inr … (start ? M2),None ?〉.
354 #sig #M1 #M2 #s #a
355 #Hhalt whd in ⊢ (??%?); >Hhalt %
356 qed.
357
358 lemma eq_ctape_lift_conf_L : ∀sig,S1,S2,outc.
359   ctape sig (FinSum S1 S2) (lift_confL … outc) = ctape … outc.
360 #sig #S1 #S2 #outc cases outc #s #t %
361 qed.
362   
363 lemma eq_ctape_lift_conf_R : ∀sig,S1,S2,outc.
364   ctape sig (FinSum S1 S2) (lift_confR … outc) = ctape … outc.
365 #sig #S1 #S2 #outc cases outc #s #t %
366 qed.
367
368 axiom daemon :∀P:Prop.P.
369
370 theorem sem_seq: ∀sig,M1,M2,R1,R2.
371   Realize sig M1 R1 → Realize sig M2 R2 → 
372     Realize sig (seq sig M1 M2) (R1 ∘ R2).
373 @daemon (* this no longer works: TODO *) (*
374 #sig #M1 #M2 #R1 #R2 #HR1 #HR2 #t #i #outc #Hloop
375 cases (HR1 t) #k1 * #outc1 * #Hloop1 #HM1
376 cases (HR2 (ctape sig (states ? M1) outc1)) #k2 * #outc2 * #Hloop2 #HM2
377 @(ex_intro … (k1+k2)) @(ex_intro … (lift_confR … outc2))
378 %
379 [@(loop_split ??????????? 
380    (loop_lift ??? (lift_confL sig (states sig M1) (states sig M2))
381    (step sig M1) (step sig (seq sig M1 M2)) 
382    (λc.halt sig M1 (cstate … c)) 
383    (λc.halt_liftL ?? (halt sig M1) (cstate … c)) … Hloop1))
384   [ * *
385    [ #sl #tl whd in ⊢ (??%? → ?); #Hl %
386    | #sr #tr whd in ⊢ (??%? → ?); #Hr destruct (Hr) ]
387   || #c0 #Hhalt <step_lift_confL //
388   | #x <p_halt_liftL %
389   |6:cases outc1 #s1 #t1 %
390   |7:@(loop_lift … (initc ?? (ctape … outc1)) … Hloop2) 
391     [ * #s2 #t2 %
392     | #c0 #Hhalt <step_lift_confR // ]
393   |whd in ⊢ (??(???%)?);whd in ⊢ (??%?);
394    generalize in match Hloop1; cases outc1 #sc1 #tc1 #Hloop10 
395    >(trans_liftL_true sig M1 M2 ??) 
396     [ whd in ⊢ (??%?); whd in ⊢ (???%);
397       @config_eq whd in ⊢ (???%); //
398     | @(loop_Some ?????? Hloop10) ]
399  ]
400 | @(ex_intro … (ctape ? (FinSum (states ? M1) (states ? M2)) (lift_confL … outc1)))
401   % // >eq_ctape_lift_conf_L >eq_ctape_lift_conf_R //
402 ]
403 *)
404 qed.
405