]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/mono.ma
80e3d4aaaaf650d4420628dc631b33a1e6cfde6d
[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 lemma config_expand: ∀sig,Q,c. 
89   c = mk_config sig Q (cstate ?? c) (ctape ?? c).
90 #sig #Q * // 
91 qed.
92   
93 lemma config_eq : ∀sig,M,c1,c2.
94   cstate sig M c1 = cstate sig M c2 → 
95     ctape sig M c1 = ctape sig M c2 →  c1 = c2.
96 #sig #M1 * #s1 #t1 * #s2 #t2 //
97 qed.
98
99 definition step ≝ λsig.λM:TM sig.λc:config sig (states sig M).
100   let current_char ≝ current ? (ctape ?? c) in
101   let 〈news,mv〉 ≝ trans sig M 〈cstate ?? c,current_char〉 in
102   mk_config ?? news (tape_move sig (ctape ?? c) mv).
103
104 (******************************** loop ****************************************)
105 let rec loop (A:Type[0]) n (f:A→A) p a on n ≝
106   match n with 
107   [ O ⇒ None ?
108   | S m ⇒ if p a then (Some ? a) else loop A m f p (f a)
109   ].
110   
111 lemma loop_S_true : 
112   ∀A,n,f,p,a. p a = true → 
113     loop A (S n) f p a = Some ? a.
114 #A #n #f #p #a #pa normalize >pa //
115 qed.
116
117 lemma loop_S_false : 
118   ∀A,n,f,p,a.  p a = false → 
119     loop A (S n) f p a = loop A n f p (f a).
120 normalize #A #n #f #p #a #Hpa >Hpa %
121 qed.  
122   
123 lemma loop_incr : ∀A,f,p,k1,k2,a1,a2. 
124   loop A k1 f p a1 = Some ? a2 → 
125     loop A (k2+k1) f p a1 = Some ? a2.
126 #A #f #p #k1 #k2 #a1 #a2 generalize in match a1; elim k1
127 [normalize #a0 #Hfalse destruct
128 |#k1' #IH #a0 <plus_n_Sm whd in ⊢ (??%? → ??%?);
129  cases (true_or_false (p a0)) #Hpa0 >Hpa0 whd in ⊢ (??%? → ??%?); // @IH
130 ]
131 qed.
132
133 lemma loop_merge : ∀A,f,p,q.(∀b. p b = false → q b = false) →
134  ∀k1,k2,a1,a2,a3,a4.
135    loop A k1 f p a1 = Some ? a2 → 
136      f a2 = a3 → q a2 = false → 
137        loop A k2 f q a3 = Some ? a4 →
138          loop A (k1+k2) f q a1 = Some ? a4.
139 #Sig #f #p #q #Hpq #k1 elim k1 
140   [normalize #k2 #a1 #a2 #a3 #a4 #H destruct
141   |#k1' #Hind #k2 #a1 #a2 #a3 #a4 normalize in ⊢ (%→?);
142    cases (true_or_false (p a1)) #pa1 >pa1 normalize in ⊢ (%→?);
143    [#eqa1a2 destruct #eqa2a3 #Hqa2 #H
144     whd in ⊢ (??(??%???)?); >plus_n_Sm @loop_incr
145     whd in ⊢ (??%?); >Hqa2 >eqa2a3 @H
146    |normalize >(Hpq … pa1) normalize #H1 #H2 #H3 @(Hind … H2) //
147    ]
148  ]
149 qed.
150
151 lemma loop_split : ∀A,f,p,q.(∀b. q b = true → p b = true) →
152  ∀k,a1,a2.
153    loop A k f q a1 = Some ? a2 → 
154    ∃k1,a3.
155     loop A k1 f p a1 = Some ? a3 ∧ 
156       loop A (S(k-k1)) f q a3 = Some ? a2.
157 #A #f #p #q #Hpq #k elim k
158   [#a1 #a2 normalize #Heq destruct
159   |#i #Hind #a1 #a2 normalize 
160    cases (true_or_false (q a1)) #Hqa1 >Hqa1 normalize
161     [ #Ha1a2 destruct
162      @(ex_intro … 1) @(ex_intro … a2) % 
163        [normalize >(Hpq …Hqa1) // |>Hqa1 //]
164     |#Hloop cases (true_or_false (p a1)) #Hpa1 
165        [@(ex_intro … 1) @(ex_intro … a1) % 
166          [normalize >Hpa1 // |>Hqa1 <Hloop normalize //]
167        |cases (Hind …Hloop) #k2 * #a3 * #Hloop1 #Hloop2
168         @(ex_intro … (S k2)) @(ex_intro … a3) %
169          [normalize >Hpa1 normalize // | @Hloop2 ]
170        ]
171     ]
172   ]
173 qed.
174
175 lemma loop_eq : ∀sig,f,q,i,j,a,x,y. 
176   loop sig i f q a = Some ? x → loop sig j f q a = Some ? y → x = y.
177 #sig #f #q #i #j @(nat_elim2 … i j)
178 [ #n #a #x #y normalize #Hfalse destruct (Hfalse)
179 | #n #a #x #y #H1 normalize #Hfalse destruct (Hfalse)
180 | #n1 #n2 #IH #a #x #y normalize cases (q a) normalize
181   [ #H1 #H2 destruct %
182   | /2/ ]
183 ]
184 qed.
185
186 lemma loop_Some : 
187   ∀A,k,f,p,a,b.loop A k f p a = Some ? b → p b = true.
188 #A #k #f #p elim k 
189   [#a #b normalize #Hfalse destruct
190   |#k0 #IH #a #b whd in ⊢ (??%? → ?); cases (true_or_false (p a)) #Hpa
191     [ >Hpa normalize #H1 destruct // | >Hpa normalize @IH ]
192   ]
193 qed. 
194
195 lemma loop_lift : ∀A,B,k,lift,f,g,h,hlift,c1,c2.
196   (∀x.hlift (lift x) = h x) → 
197   (∀x.h x = false → lift (f x) = g (lift x)) → 
198   loop A k f h c1 = Some ? c2 → 
199   loop B k g hlift (lift c1) = Some ? (lift … c2).
200 #A #B #k #lift #f #g #h #hlift #c1 #c2 #Hfg #Hhlift
201 generalize in match c1; elim k
202 [#c0 normalize in ⊢ (??%? → ?); #Hfalse destruct (Hfalse)
203 |#k0 #IH #c0 whd in ⊢ (??%? → ??%?);
204  cases (true_or_false (h c0)) #Hc0 >Hfg >Hc0 normalize
205  [ #Heq destruct (Heq) % | <Hhlift // @IH ]
206 qed.
207
208 (************************** Realizability *************************************)
209 definition loopM ≝ λsig,M,i,cin.
210   loop ? i (step sig M) (λc.halt sig M (cstate ?? c)) cin.
211
212 definition initc ≝ λsig.λM:TM sig.λt.
213   mk_config sig (states sig M) (start sig M) t.
214
215 definition Realize ≝ λsig.λM:TM sig.λR:relation (tape sig).
216 ∀t.∃i.∃outc.
217   loopM sig M i (initc sig M t) = Some ? outc ∧ R t (ctape ?? outc).
218
219 definition WRealize ≝ λsig.λM:TM sig.λR:relation (tape sig).
220 ∀t,i,outc.
221   loopM sig M i (initc sig M t) = Some ? outc → R t (ctape ?? outc).
222
223 definition Terminate ≝ λsig.λM:TM sig.λt. ∃i,outc.
224   loopM sig M i (initc sig M t) = Some ? outc.
225   
226 notation "M \vDash R" non associative with precedence 45 for @{ 'models $M $R}.
227 interpretation "realizability" 'models M R = (Realize ? M R).
228
229 notation "M \VDash R" non associative with precedence 45 for @{ 'wmodels $M $R}.
230 interpretation "weak realizability" 'wmodels M R = (WRealize ? M R).
231
232 interpretation "termination" 'fintersects M t = (Terminate ? M t).
233
234 lemma WRealize_to_Realize : ∀sig.∀M: TM sig.∀R.
235   (∀t.M ↓ t) → M ⊫ R → M ⊨ R.
236 #sig #M #R #HT #HW #t cases (HT … t) #i * #outc #Hloop 
237 @(ex_intro … i) @(ex_intro … outc) % // @(HW … i) //
238 qed.
239
240 theorem Realize_to_WRealize : ∀sig.∀M:TM sig.∀R.
241   M ⊨ R → M ⊫ R.
242 #sig #M #R #H1 #inc #i #outc #Hloop 
243 cases (H1 inc) #k * #outc1 * #Hloop1 #HR >(loop_eq … Hloop Hloop1) //
244 qed.
245
246 definition accRealize ≝ λsig.λM:TM sig.λacc:states sig M.λRtrue,Rfalse.
247 ∀t.∃i.∃outc.
248   loopM sig M i (initc sig M t) = Some ? outc ∧
249     (cstate ?? outc = acc → Rtrue t (ctape ?? outc)) ∧ 
250     (cstate ?? outc ≠ acc → Rfalse t (ctape ?? outc)).
251
252 (******************************** NOP Machine *********************************)
253
254 (* NO OPERATION
255    t1 = t2 *)
256   
257 definition nop_states ≝ initN 1.
258 definition start_nop : initN 1 ≝ mk_Sig ?? 0 (le_n … 1).
259
260 definition nop ≝ 
261   λalpha:FinSet.mk_TM alpha nop_states
262   (λp.let 〈q,a〉 ≝ p in 〈q,None ?〉)
263   start_nop (λ_.true).
264   
265 definition R_nop ≝ λalpha.λt1,t2:tape alpha.t2 = t1.
266
267 lemma sem_nop :
268   ∀alpha.nop alpha ⊨ R_nop alpha.
269 #alpha #intape @(ex_intro ?? 1) 
270 @(ex_intro … (mk_config ?? start_nop intape)) % % 
271 qed.
272
273 lemma nop_single_state: ∀sig.∀q1,q2:states ? (nop sig). q1 = q2.
274 normalize #sig * #n #ltn1 * #m #ltm1 
275 generalize in match ltn1; generalize in match ltm1;
276 <(le_n_O_to_eq … (le_S_S_to_le … ltn1)) <(le_n_O_to_eq … (le_S_S_to_le … ltm1)) 
277 // qed.
278
279 (************************** Sequential Composition ****************************)
280
281 definition seq_trans ≝ λsig. λM1,M2 : TM sig. 
282 λp. let 〈s,a〉 ≝ p in
283   match s with 
284   [ inl s1 ⇒ 
285       if halt sig M1 s1 then 〈inr … (start sig M2), None ?〉
286       else let 〈news1,m〉 ≝ trans sig M1 〈s1,a〉 in 〈inl … news1,m〉
287   | inr s2 ⇒ let 〈news2,m〉 ≝ trans sig M2 〈s2,a〉 in 〈inr … news2,m〉
288   ].
289  
290 definition seq ≝ λsig. λM1,M2 : TM sig. 
291   mk_TM sig 
292     (FinSum (states sig M1) (states sig M2))
293     (seq_trans sig M1 M2) 
294     (inl … (start sig M1))
295     (λs.match s with 
296       [ inl _ ⇒ false | inr s2 ⇒ halt sig M2 s2]). 
297
298 notation "a · b" non associative with precedence 65 for @{ 'middot $a $b}.
299 interpretation "sequential composition" 'middot a b = (seq ? a b).
300
301 definition Rcomp ≝ λA.λR1,R2:relation A.λa1,a2.
302   ∃am.R1 a1 am ∧ R2 am a2.
303   
304 interpretation "relation composition" 'compose R1 R2 = (Rcomp ? R1 R2).
305
306 definition lift_confL ≝ 
307   λsig,S1,S2,c.match c with 
308   [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inl … s) t ].
309   
310 definition lift_confR ≝ 
311   λsig,S1,S2,c.match c with
312   [ mk_config s t ⇒ mk_config sig (FinSum S1 S2) (inr … s) t ].
313   
314 definition halt_liftL ≝ 
315   λS1,S2,halt.λs:FinSum S1 S2.
316   match s with
317   [ inl s1 ⇒ halt s1
318   | inr _ ⇒ true ]. (* should be vacuous in all cases we use halt_liftL *)
319
320 definition halt_liftR ≝ 
321   λS1,S2,halt.λs:FinSum S1 S2.
322   match s with
323   [ inl _ ⇒ false 
324   | inr s2 ⇒ halt s2 ].
325       
326 lemma p_halt_liftL : ∀sig,S1,S2,halt,c.
327   halt (cstate sig S1 c) =
328      halt_liftL S1 S2 halt (cstate … (lift_confL … c)).
329 #sig #S1 #S2 #halt #c cases c #s #t %
330 qed.
331
332 lemma trans_seq_liftL : ∀sig,M1,M2,s,a,news,move.
333   halt ? M1 s = false → 
334   trans sig M1 〈s,a〉 = 〈news,move〉 → 
335   trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inl … news,move〉.
336 #sig (*#M1*) * #Q1 #T1 #init1 #halt1 #M2 #s #a #news #move
337 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
338 qed.
339
340 lemma trans_seq_liftR : ∀sig,M1,M2,s,a,news,move.
341   halt ? M2 s = false → 
342   trans sig M2 〈s,a〉 = 〈news,move〉 → 
343   trans sig (seq sig M1 M2) 〈inr … s,a〉 = 〈inr … news,move〉.
344 #sig #M1 * #Q2 #T2 #init2 #halt2 #s #a #news #move
345 #Hhalt #Htrans whd in ⊢ (??%?); >Hhalt >Htrans %
346 qed.
347
348 lemma step_seq_liftR : ∀sig,M1,M2,c0.
349  halt ? M2 (cstate ?? c0) = false → 
350  step sig (seq sig M1 M2) (lift_confR sig (states ? M1) (states ? M2) c0) =
351  lift_confR sig (states ? M1) (states ? M2) (step sig M2 c0).
352 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s #t
353   lapply (refl ? (trans ?? 〈s,current sig t〉))
354   cases (trans ?? 〈s,current sig t〉) in ⊢ (???% → %);
355   #s0 #m0 cases t
356   [ #Heq #Hhalt
357   | 2,3: #s1 #l1 #Heq #Hhalt 
358   |#ls #s1 #rs #Heq #Hhalt ]
359   whd in ⊢ (???(????%)); >Heq whd in ⊢ (???%);
360   whd in ⊢ (??(???%)?); whd in ⊢ (??%?); >(trans_seq_liftR … Heq) //
361 qed.
362
363 lemma step_seq_liftL : ∀sig,M1,M2,c0.
364  halt ? M1 (cstate ?? c0) = false → 
365  step sig (seq sig M1 M2) (lift_confL sig (states ? M1) (states ? M2) c0) =
366  lift_confL sig ?? (step sig M1 c0).
367 #sig #M1 (* * #Q1 #T1 #init1 #halt1 *) #M2 * #s #t
368   lapply (refl ? (trans ?? 〈s,current sig t〉))
369   cases (trans ?? 〈s,current sig t〉) in ⊢ (???% → %);
370   #s0 #m0 cases t
371   [ #Heq #Hhalt
372   | 2,3: #s1 #l1 #Heq #Hhalt 
373   |#ls #s1 #rs #Heq #Hhalt ]
374   whd in ⊢ (???(????%)); >Heq whd in ⊢ (???%);
375   whd in ⊢ (??(???%)?); whd in ⊢ (??%?); >(trans_seq_liftL … Heq) //
376 qed.
377
378 lemma trans_liftL_true : ∀sig,M1,M2,s,a.
379   halt ? M1 s = true → 
380   trans sig (seq sig M1 M2) 〈inl … s,a〉 = 〈inr … (start ? M2),None ?〉.
381 #sig #M1 #M2 #s #a #Hhalt whd in ⊢ (??%?); >Hhalt %
382 qed.
383
384 lemma eq_ctape_lift_conf_L : ∀sig,S1,S2,outc.
385   ctape sig (FinSum S1 S2) (lift_confL … outc) = ctape … outc.
386 #sig #S1 #S2 #outc cases outc #s #t %
387 qed.
388   
389 lemma eq_ctape_lift_conf_R : ∀sig,S1,S2,outc.
390   ctape sig (FinSum S1 S2) (lift_confR … outc) = ctape … outc.
391 #sig #S1 #S2 #outc cases outc #s #t %
392 qed.
393
394 theorem sem_seq: ∀sig.∀M1,M2:TM sig.∀R1,R2.
395   M1 ⊨ R1 → M2 ⊨ R2 → M1 · M2 ⊨ R1 ∘ R2.
396 #sig #M1 #M2 #R1 #R2 #HR1 #HR2 #t 
397 cases (HR1 t) #k1 * #outc1 * #Hloop1 #HM1
398 cases (HR2 (ctape sig (states ? M1) outc1)) #k2 * #outc2 * #Hloop2 #HM2
399 @(ex_intro … (k1+k2)) @(ex_intro … (lift_confR … outc2))
400 %
401 [@(loop_merge ??????????? 
402    (loop_lift ??? (lift_confL sig (states sig M1) (states sig M2))
403    (step sig M1) (step sig (seq sig M1 M2)) 
404    (λc.halt sig M1 (cstate … c)) 
405    (λc.halt_liftL ?? (halt sig M1) (cstate … c)) … Hloop1))
406   [ * *
407    [ #sl #tl whd in ⊢ (??%? → ?); #Hl %
408    | #sr #tr whd in ⊢ (??%? → ?); #Hr destruct (Hr) ]
409   || #c0 #Hhalt <step_seq_liftL //
410   | #x <p_halt_liftL %
411   |6:cases outc1 #s1 #t1 %
412   |7:@(loop_lift … (initc ?? (ctape … outc1)) … Hloop2) 
413     [ * #s2 #t2 %
414     | #c0 #Hhalt <step_seq_liftR // ]
415   |whd in ⊢ (??(???%)?);whd in ⊢ (??%?);
416    generalize in match Hloop1; cases outc1 #sc1 #tc1 #Hloop10 
417    >(trans_liftL_true sig M1 M2 ??) 
418     [ whd in ⊢ (??%?); whd in ⊢ (???%);
419       @config_eq whd in ⊢ (???%); //
420     | @(loop_Some ?????? Hloop10) ]
421  ]
422 | @(ex_intro … (ctape ? (FinSum (states ? M1) (states ? M2)) (lift_confL … outc1)))
423   % // >eq_ctape_lift_conf_L >eq_ctape_lift_conf_R //
424 ]
425 qed.
426
427 theorem sem_seq_app: ∀sig.∀M1,M2:TM sig.∀R1,R2,R3.
428   M1 ⊨ R1 → M2 ⊨ R2 → R1 ∘ R2 ⊆ R3 → M1 · M2 ⊨ R3.
429 #sig #M1 #M2 #R1 #R2 #R3 #HR1 #HR2 #Hsub
430 #t cases (sem_seq … HR1 HR2 t)
431 #k * #outc * #Hloop #Houtc @(ex_intro … k) @(ex_intro … outc)
432 % [@Hloop |@Hsub @Houtc]
433 qed.