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