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