]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/mono.ma
Added weak realizability.
[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 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. p b = false → q b = false) →
77  ∀k1,k2,a1,a2,a3,a4.
78    loop A k1 f p a1 = Some ? a2 → 
79      f a2 = a3 → q a2 = false → 
80        loop A k2 f q a3 = Some ? a4 →
81          loop A (k1+k2) f q a1 = Some ? a4.
82 #Sig #f #p #q #Hpq #k1 elim k1 
83   [normalize #k2 #a1 #a2 #a3 #a4 #H destruct
84   |#k1' #Hind #k2 #a1 #a2 #a3 #a4 normalize in ⊢ (%→?);
85    cases (true_or_false (p a1)) #pa1 >pa1 normalize in ⊢ (%→?);
86    [#eqa1a2 destruct #eqa2a3 #Hqa2 #H
87     whd in ⊢ (??(??%???)?); >plus_n_Sm @loop_incr
88     whd in ⊢ (??%?); >Hqa2 >eqa2a3 @H
89    |normalize >(Hpq … pa1) normalize 
90     #H1 #H2 #H3 @(Hind … H2) //
91    ]
92  ]
93 qed.
94
95 (*
96 lemma loop_split : ∀A,f,p,q.(∀b. p b = false → q b = false) →
97  ∀k1,k2,a1,a2,a3.
98    loop A k1 f p a1 = Some ? a2 → 
99      loop A k2 f q a2 = Some ? a3 →
100        loop A (k1+k2) f q a1 = Some ? a3.
101 #Sig #f #p #q #Hpq #k1 elim k1 
102   [normalize #k2 #a1 #a2 #a3 #H destruct
103   |#k1' #Hind #k2 #a1 #a2 #a3 normalize in ⊢ (%→?→?);
104    cases (true_or_false (p a1)) #pa1 >pa1 normalize in ⊢ (%→?);
105    [#eqa1a2 destruct #H @loop_incr //
106    |normalize >(Hpq … pa1) normalize 
107     #H1 #H2 @(Hind … H2) //
108    ]
109  ]
110 qed.
111 *)
112
113 definition initc ≝ λsig.λM:TM sig.λt.
114   mk_config sig (states sig M) (start sig M) t.
115
116 definition Realize ≝ λsig.λM:TM sig.λR:relation (tape sig).
117 ∀t.∃i.∃outc.
118   loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc ∧
119   R t (ctape ?? outc).
120
121 definition WRealize ≝ λsig.λM:TM sig.λR:relation (tape sig).
122 ∀t,i,outc.
123   loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) (initc sig M t) = Some ? outc → 
124   R t (ctape ?? outc).
125   
126 lemma loop_eq : ∀sig,f,q,i,j,a,x,y. 
127   loop sig i f q a = Some ? x → loop sig j f q a = Some ? y → x = y.
128 #sig #f #q #i #j @(nat_elim2 … i j)
129 [ #n #a #x #y normalize #Hfalse destruct (Hfalse)
130 | #n #a #x #y #H1 normalize #Hfalse destruct (Hfalse)
131 | #n1 #n2 #IH #a #x #y normalize cases (q a) normalize
132   [ #H1 #H2 destruct %
133   | /2/ ]
134 ]
135 qed.
136
137 theorem Realize_to_WRealize : ∀sig,M,R.Realize sig M R → WRealize sig M R.
138 #sig #M #R #H1 #inc #i #outc #Hloop
139 cases (H1 inc) #k * #outc1 * #Hloop1 #HR
140 >(loop_eq … Hloop Hloop1) //
141 qed.
142
143 definition accRealize ≝ λsig.λM:TM sig.λacc:states sig M.λRtrue,Rfalse: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   (cstate ?? outc = acc → Rtrue t (ctape ?? outc)) ∧ 
147   (cstate ?? outc ≠ acc → Rfalse t (ctape ?? outc)).
148
149 (* Compositions *)
150
151 definition seq_trans ≝ λsig. λM1,M2 : TM sig. 
152 λp. let 〈s,a〉 ≝ p in
153   match s with 
154   [ inl s1 ⇒ 
155       if halt sig M1 s1 then 〈inr … (start sig M2), None ?〉
156       else 
157       let 〈news1,m〉 ≝ trans sig M1 〈s1,a〉 in
158       〈inl … news1,m〉
159   | inr s2 ⇒ 
160       let 〈news2,m〉 ≝ trans sig M2 〈s2,a〉 in
161       〈inr … news2,m〉
162   ].
163  
164 definition seq ≝ λsig. λM1,M2 : TM sig. 
165   mk_TM sig 
166     (FinSum (states sig M1) (states sig M2))
167     (seq_trans sig M1 M2) 
168     (inl … (start sig M1))
169     (λs.match s with
170       [ inl _ ⇒ false | inr s2 ⇒ halt sig M2 s2]). 
171
172 definition Rcomp ≝ λA.λR1,R2:relation A.λa1,a2.
173   ∃am.R1 a1 am ∧ R2 am a2.
174
175 (*
176 definition injectRl ≝ λsig.λM1.λM2.λR.
177    λc1,c2. ∃c11,c12. 
178      inl … (cstate sig M1 c11) = cstate sig (seq sig M1 M2) c1 ∧ 
179      inl … (cstate sig M1 c12) = cstate sig (seq sig M1 M2) c2 ∧
180      ctape sig M1 c11 = ctape sig (seq sig M1 M2) c1 ∧ 
181      ctape sig M1 c12 = ctape sig (seq sig M1 M2) c2 ∧ 
182      R c11 c12.
183
184 definition injectRr ≝ λsig.λM1.λM2.λR.
185    λc1,c2. ∃c21,c22. 
186      inr … (cstate sig M2 c21) = cstate sig (seq sig M1 M2) c1 ∧ 
187      inr … (cstate sig M2 c22) = cstate sig (seq sig M1 M2) c2 ∧
188      ctape sig M2 c21 = ctape sig (seq sig M1 M2) c1 ∧ 
189      ctape sig M2 c22 = ctape sig (seq sig M1 M2) c2 ∧ 
190      R c21 c22.
191      
192 definition Rlink ≝ λsig.λM1,M2.λc1,c2.
193   ctape sig (seq sig M1 M2) c1 = ctape sig (seq sig M1 M2) c2 ∧
194   cstate sig (seq sig M1 M2) c1 = inl … (halt sig M1) ∧
195   cstate sig (seq sig M1 M2) c2 = inr … (start sig M2). *)
196   
197 interpretation "relation composition" 'compose R1 R2 = (Rcomp ? R1 R2).
198
199 definition lift_confL ≝ 
200   λsig,S1,S2,c.match c with 
201   [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inl … s) t ].
202   
203 definition lift_confR ≝ 
204   λsig,S1,S2,c.match c with
205   [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inr … s) t ].
206   
207 definition halt_liftL ≝ 
208   λS1,S2,halt.λs:FinSum S1 S2.
209   match s with
210   [ inl s1 ⇒ halt s1
211   | inr _ ⇒ true ]. (* should be vacuous in all cases we use halt_liftL *)
212
213 definition halt_liftR ≝ 
214   λS1,S2,halt.λs:FinSum S1 S2.
215   match s with
216   [ inl _ ⇒ false 
217   | inr s2 ⇒ halt s2 ].
218       
219 lemma p_halt_liftL : ∀sig,S1,S2,halt,c.
220   halt (cstate sig S1 c) =
221      halt_liftL S1 S2 halt (cstate … (lift_confL … c)).
222 #sig #S1 #S2 #halt #c cases c #s #t %
223 qed.
224
225 lemma trans_liftL : ∀sig,M1,M2,s,a,news,move.
226   halt ? M1 s = false → 
227   trans sig M1 〈s,a〉 = 〈news,move〉 → 
228   trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inl … news,move〉.
229 #sig (*#M1*) * #Q1 #T1 #init1 #halt1 #M2 #s #a #news #move
230 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
231 qed.
232
233 lemma trans_liftR : ∀sig,M1,M2,s,a,news,move.
234   halt ? M2 s = false → 
235   trans sig M2 〈s,a〉 = 〈news,move〉 → 
236   trans sig (seq sig M1 M2) 〈inr … s,a〉 = 〈inr … news,move〉.
237 #sig #M1 * #Q2 #T2 #init2 #halt2 #s #a #news #move
238 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
239 qed.
240
241 lemma config_eq : 
242   ∀sig,M,c1,c2.
243   cstate sig M c1 = cstate sig M c2 → 
244   ctape sig M c1 = ctape sig M c2 →  c1 = c2.
245 #sig #M1 * #s1 #t1 * #s2 #t2 //
246 qed.
247
248 lemma step_lift_confR : ∀sig,M1,M2,c0.
249  halt ? M2 (cstate ?? c0) = false → 
250  step sig (seq sig M1 M2) (lift_confR sig (states ? M1) (states ? M2) c0) =
251  lift_confR sig (states ? M1) (states ? M2) (step sig M2 c0).
252 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s * #lt
253 #rs #Hhalt
254 whd in ⊢ (???(????%));whd in ⊢ (???%);
255 lapply (refl ? (trans ?? 〈s,option_hd sig rs〉))
256 cases (trans ?? 〈s,option_hd sig rs〉) in ⊢ (???% → %);
257 #s0 #m0 #Heq whd in ⊢ (???%);
258 whd in ⊢ (??(???%)?); whd in ⊢ (??%?);
259 >(trans_liftR … Heq)
260 [% | //]
261 qed.
262
263 lemma step_lift_confL : ∀sig,M1,M2,c0.
264  halt ? M1 (cstate ?? c0) = false → 
265  step sig (seq sig M1 M2) (lift_confL sig (states ? M1) (states ? M2) c0) =
266  lift_confL sig ?? (step sig M1 c0).
267 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s * #lt
268 #rs #Hhalt
269 whd in ⊢ (???(????%));whd in ⊢ (???%);
270 lapply (refl ? (trans ?? 〈s,option_hd sig rs〉))
271 cases (trans ?? 〈s,option_hd sig rs〉) in ⊢ (???% → %);
272 #s0 #m0 #Heq whd in ⊢ (???%);
273 whd in ⊢ (??(???%)?); whd in ⊢ (??%?);
274 >(trans_liftL … Heq)
275 [% | //]
276 qed.
277
278 lemma loop_lift : ∀A,B,k,lift,f,g,h,hlift,c1,c2.
279   (∀x.hlift (lift x) = h x) → 
280   (∀x.h x = false → lift (f x) = g (lift x)) → 
281   loop A k f h c1 = Some ? c2 → 
282   loop B k g hlift (lift c1) = Some ? (lift … c2).
283 #A #B #k #lift #f #g #h #hlift #c1 #c2 #Hfg #Hhlift
284 generalize in match c1; elim k
285 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
286 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
287  cases (true_or_false (h c0)) #Hc0 >Hfg >Hc0
288  [ normalize #Heq destruct (Heq) %
289  | normalize <Hhlift // @IH ]
290 qed.
291
292 (* 
293 lemma loop_liftL : ∀sig,k,M1,M2,c1,c2.
294   loop ? k (step sig M1) (λc.halt sig M1 (cstate ?? c)) c1 = Some ? c2 →
295     loop ? k (step sig (seq sig M1 M2)) 
296       (λc.halt_liftL ?? (halt sig M1) (cstate ?? c)) (lift_confL … c1) = 
297     Some ? (lift_confL … c2).
298 #sig #k #M1 #M2 #c1 #c2 generalize in match c1;
299 elim k
300 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
301 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
302  cases (true_or_false (halt ?? (cstate sig (states ? M1) c0))) #Hc0 >Hc0
303  [ >(?: halt_liftL ?? (halt sig M1) (cstate sig ? (lift_confL … c0)) = true)
304    [ whd in ⊢ (??%? → ??%?); #Hc2 destruct (Hc2) %
305    | <Hc0 cases c0 // ]
306  | >(?: halt_liftL ?? (halt sig M1) (cstate ?? (lift_confL … c0)) = false)
307    [whd in ⊢ (??%? → ??%?); #Hc2 <(IH ? Hc2) @eq_f
308     @step_lift_confL //
309    | <Hc0 cases c0 // ]
310 qed.
311
312 lemma loop_liftR : ∀sig,k,M1,M2,c1,c2.
313   loop ? k (step sig M2) (λc.halt sig M2 (cstate ?? c)) c1 = Some ? c2 →
314     loop ? k (step sig (seq sig M1 M2)) 
315       (λc.halt sig (seq sig M1 M2) (cstate ?? c)) (lift_confR … c1) = 
316     Some ? (lift_confR … c2).
317 #sig #k #M1 #M2 #c1 #c2 generalize in match c1;
318 elim k
319 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
320 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
321  cases (true_or_false (halt ?? (cstate sig ? c0))) #Hc0 >Hc0
322  [ >(?: halt ? (seq sig M1 M2) (cstate sig ? (lift_confR … c0)) = true)
323    [ whd in ⊢ (??%? → ??%?); #Hc2 destruct (Hc2) %
324    | <Hc0 cases c0 // ]
325  | >(?: halt ? (seq sig M1 M2) (cstate sig ? (lift_confR … c0)) = false)
326    [whd in ⊢ (??%? → ??%?); #Hc2 <(IH ? Hc2) @eq_f
327     @step_lift_confR //
328    | <Hc0 cases c0 // ]
329  ]
330 qed.  
331
332 *)
333     
334 lemma loop_Some : 
335   ∀A,k,f,p,a,b.loop A k f p a = Some ? b → p b = true.
336 #A #k #f #p elim k 
337 [#a #b normalize #Hfalse destruct
338 |#k0 #IH #a #b whd in ⊢ (??%? → ?); cases (true_or_false (p a)) #Hpa
339  [ >Hpa normalize #H1 destruct //
340  | >Hpa normalize @IH
341  ]
342 ]
343 qed. 
344
345 lemma trans_liftL_true : ∀sig,M1,M2,s,a.
346   halt ? M1 s = true → 
347   trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inr … (start ? M2),None ?〉.
348 #sig #M1 #M2 #s #a
349 #Hhalt whd in ⊢ (??%?); >Hhalt %
350 qed.
351
352 lemma eq_ctape_lift_conf_L : ∀sig,S1,S2,outc.
353   ctape sig (FinSum S1 S2) (lift_confL … outc) = ctape … outc.
354 #sig #S1 #S2 #outc cases outc #s #t %
355 qed.
356   
357 lemma eq_ctape_lift_conf_R : ∀sig,S1,S2,outc.
358   ctape sig (FinSum S1 S2) (lift_confR … outc) = ctape … outc.
359 #sig #S1 #S2 #outc cases outc #s #t %
360 qed.
361
362 theorem sem_seq: ∀sig,M1,M2,R1,R2.
363   Realize sig M1 R1 → Realize sig M2 R2 → 
364     Realize sig (seq sig M1 M2) (R1 ∘ R2).
365 #sig #M1 #M2 #R1 #R2 #HR1 #HR2 #t 
366 cases (HR1 t) #k1 * #outc1 * #Hloop1 #HM1
367 cases (HR2 (ctape sig (states ? M1) outc1)) #k2 * #outc2 * #Hloop2 #HM2
368 @(ex_intro … (k1+k2)) @(ex_intro … (lift_confR … outc2))
369 %
370 [@(loop_split ??????????? 
371    (loop_lift ??? (lift_confL sig (states sig M1) (states sig M2))
372    (step sig M1) (step sig (seq sig M1 M2)) 
373    (λc.halt sig M1 (cstate … c)) 
374    (λc.halt_liftL ?? (halt sig M1) (cstate … c)) … Hloop1))
375   [ * *
376    [ #sl #tl whd in ⊢ (??%? → ?); #Hl %
377    | #sr #tr whd in ⊢ (??%? → ?); #Hr destruct (Hr) ]
378   || #c0 #Hhalt <step_lift_confL //
379   | #x <p_halt_liftL %
380   |6:cases outc1 #s1 #t1 %
381   |7:@(loop_lift … (initc ?? (ctape … outc1)) … Hloop2) 
382     [ * #s2 #t2 %
383     | #c0 #Hhalt <step_lift_confR // ]
384   |whd in ⊢ (??(???%)?);whd in ⊢ (??%?);
385    generalize in match Hloop1; cases outc1 #sc1 #tc1 #Hloop10 
386    >(trans_liftL_true sig M1 M2 ??) 
387     [ whd in ⊢ (??%?); whd in ⊢ (???%);
388       @config_eq whd in ⊢ (???%); //
389     | @(loop_Some ?????? Hloop10) ]
390  ]
391 | @(ex_intro … (ctape ? (FinSum (states ? M1) (states ? M2)) (lift_confL … outc1)))
392   % // >eq_ctape_lift_conf_L >eq_ctape_lift_conf_R //
393 ]
394 qed.
395