]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/multi_universal/binaryTM.ma
progress in the semantics of binary machines
[helm.git] / matita / matita / lib / turing / multi_universal / binaryTM.ma
1 (**************************************************************************)
2 (*       ___                                                              *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||         The HELM team.                                      *)
8 (*      ||A||         http://helm.cs.unibo.it                             *)
9 (*      \   /                                                             *)
10 (*       \ /        This file is distributed under the terms of the       *)
11 (*        v         GNU General Public License Version 2                  *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 include "turing/mono.ma".
16
17 (* given a FinSet F:
18    - get its cardinality
19    - return its nth element
20    - return the index of a given element
21  *)
22 axiom FS_crd : FinSet → nat.
23 axiom FS_nth : ∀F:FinSet.nat → option F.
24 axiom index_of_FS : ∀F:FinSet.F → nat.
25
26 (* unary bit representation (with a given length) of a certain number *)
27 axiom unary_of_nat : nat → nat → (list bool).
28
29 axiom FinVector : Type[0] → nat → FinSet.
30
31 definition binary_base_states ≝ initN 6.
32
33 definition bin0 : binary_base_states ≝ mk_Sig ?? 0 (leb_true_to_le 1 6 (refl …)).
34 definition bin1 : binary_base_states ≝ mk_Sig ?? 1 (leb_true_to_le 2 6 (refl …)).
35 definition bin2 : binary_base_states ≝ mk_Sig ?? 2 (leb_true_to_le 3 6 (refl …)).
36 definition bin3 : binary_base_states ≝ mk_Sig ?? 3 (leb_true_to_le 4 6 (refl …)).
37 definition bin4 : binary_base_states ≝ mk_Sig ?? 4 (leb_true_to_le 5 6 (refl …)).
38 definition bin5 : binary_base_states ≝ mk_Sig ?? 5 (leb_true_to_le 6 6 (refl …)).
39
40 definition states_binaryTM : FinSet → FinSet → FinSet ≝ λsig,states.
41  FinProd (FinProd states binary_base_states) 
42          (FinProd (FinOption sig) (initN (S (2 * (FS_crd sig))))).
43
44 axiom daemon : ∀T:Type[0].T.
45
46 definition to_initN : ∀n,m.n < m → initN m ≝ λn,m,Hn.mk_Sig … n ….// qed.
47
48 definition initN_pred : ∀n.∀m:initN n.initN n ≝ λn,m.mk_Sig … (pred (pi1 … m)) …. 
49 cases m #m0 /2 by le_to_lt_to_lt/ qed.
50
51 (* controllare i contatori, molti andranno incrementati di uno *)
52 definition trans_binaryTM : ∀sig,states:FinSet.
53   (states × (option sig) → states × (option sig) × move) → 
54   ((states_binaryTM sig states) × (option bool) → 
55    (states_binaryTM sig states) × (option bool) × move) 
56 ≝ λsig,states,trans,p.
57   let 〈s,a〉 ≝ p in
58   let 〈s0,phase,ch,count〉 ≝ s in
59   let (H1 : O < S (2*FS_crd sig)) ≝ ? in
60   let (H2 : FS_crd sig < S (2*FS_crd sig)) ≝ ? in
61   match pi1 … phase with
62   [ O ⇒ (*** PHASE 0: read ***)
63       match pi1 … count with
64       [ O ⇒ 〈〈s0,bin1,ch,to_initN (FS_crd sig) ? H2〉,None ?,N〉
65       | S k ⇒ match a with
66         [ Some a0 ⇒ if (a0 == true) 
67                     then 〈〈s0,bin0,FS_nth sig k,initN_pred … count〉, None ?,R〉
68                     else 〈〈s0,bin0,ch,initN_pred … count〉,None ?,R〉 
69         | None ⇒ (* Overflow position! *)
70           〈〈s0,bin4,None ?,to_initN 0 ? H1〉,None ?,R〉 ] ]
71   | S phase ⇒ match phase with
72   [ O ⇒ (*** PHASE 1: restart ***)
73       match pi1 … count with
74       [ O ⇒ 〈〈s0,bin2,ch,to_initN (FS_crd sig) ? H2〉,None ?,N〉
75       | S k ⇒ 〈〈s0,bin1,ch,initN_pred … count〉,None ?,L〉 ]
76   | S phase ⇒ match phase with
77   [ O ⇒ (*** PHASE 2: write ***)
78       let 〈s',a',mv〉 ≝ trans 〈s0,ch〉 in
79       match pi1 … count with
80       [ O ⇒ let mv' ≝ match mv with [ R ⇒ N | _ ⇒ L ] in
81             let count' ≝ match mv with [ R ⇒ 0 | N ⇒ FS_crd sig | L ⇒ 2*(FS_crd sig) ] in
82              〈〈s',bin3,ch,to_initN count' ??〉,None ?,mv'〉
83       | S k ⇒ match a' with
84          [ None ⇒ 〈〈s0,bin2,ch,initN_pred … count〉,None ?,R〉
85          | Some a0' ⇒ let out ≝ (FS_nth ? k == a') in
86                       〈〈s0,bin2,ch,initN_pred … count〉,Some ? out,R〉 ]
87       ]
88   | S phase ⇒ match phase with
89   [ O ⇒ (*** PHASE 3: move head left ***)
90       match pi1 … count with
91       [ O ⇒ 〈〈s0,bin0,None ?,to_initN (FS_crd sig) ? H2〉, None ?,N〉 (* the end: restart *)
92       | S k ⇒ 〈〈s0,bin3,ch,initN_pred … count〉, None ?,L〉 ]
93   | S phase ⇒ match phase with
94   [ O ⇒ (*** PHASE 4: check position ***)
95       match a with
96       [ None ⇒ (* niltape/rightof: we can write *) 〈〈s0,bin2,ch,to_initN (FS_crd sig) ? H2〉,None ?,N〉
97       | Some _ ⇒ (* leftof *)
98         let 〈s',a',mv〉 ≝ trans 〈s0,ch〉 in
99         match a' with
100         [ None ⇒ (* we don't write anything: go to end of 2 *) 〈〈s0,bin2,ch,to_initN 0 ? H1〉,None ?,N〉
101         | Some _ ⇒ (* extend tape *) 〈〈s0,bin5,ch,to_initN (FS_crd sig) ? H2〉,None ?,L〉 ]
102       ]
103   | S _ ⇒ (*** PHASE 5: left extension ***)
104       match pi1 … count with
105       [ O ⇒ 〈〈s0,bin2,ch,to_initN (FS_crd sig) ? H2〉,None ?,N〉
106       | S k ⇒ 〈〈s0,bin5,ch,initN_pred … count〉,Some ? false,L〉 ]]]]]].
107 [2,3: //]
108 whd in match count'; cases mv whd in ⊢ (?%?); //
109 qed.
110
111 definition halt_binaryTM : ∀sig,M.states_binaryTM sig (states sig M) → bool ≝ 
112   λsig,M,s.let 〈s0,phase,ch,count〉 ≝ s in
113   pi1 … phase == O ∧ halt sig M s0.
114
115 (*
116  * Una mk_binaryTM prende in input una macchina M e produce una macchina che:
117  * - ha per alfabeto FinBool
118  * - ha stati di tipo ((states … M) × (initN 7)) × 
119        ((option sig) × (initN (2*dimensione dell'alfabeto di M + 1))
120  *   dove il primo elemento corrisponde allo stato della macchina input,
121  *   il secondo identifica la fase (lettura, scrittura, spostamento)
122  *   il terzo identifica il carattere oggetto letto
123  *   il quarto è un contatore
124  * - la funzione di transizione viene prodotta da trans_binaryTM
125  * - la funzione di arresto viene prodotta da halt_binaryTM
126  *)
127 definition mk_binaryTM ≝ 
128   λsig.λM:TM sig.
129   mk_TM FinBool (states_binaryTM sig (states sig M)) 
130     (trans_binaryTM sig (states sig M) (trans sig M)) 
131     (〈start sig M,bin0,None ?,FS_crd sig〉) (halt_binaryTM sig M).// qed.
132
133 definition bin_char ≝ λsig,ch.unary_of_nat (FS_crd sig) (index_of_FS sig ch).
134
135 definition bin_current ≝ λsig,t.match current ? t with
136 [ None ⇒ [ ] | Some c ⇒ bin_char sig c ].
137
138 definition tape_bin_lift ≝ λsig,t.
139 let ls' ≝ flatten ? (map ?? (bin_char sig) (left ? t)) in
140 let c' ≝ option_hd ? (bin_current sig t) in
141 let rs' ≝ tail ? (bin_current sig t)@flatten ? (map ?? (bin_char sig) (right ? t)) in
142  mk_tape ? ls' c' rs'.
143
144 definition R_bin_lift ≝ λsig,R,t1,t2.
145   ∃u1.t1 = tape_bin_lift sig u1 → 
146   ∃u2.t2 = tape_bin_lift sig u2 ∧ R u1 u2.
147   
148 definition state_bin_lift :
149   ∀sig.∀M:TM sig.states sig M → states ? (mk_binaryTM ? M)
150  ≝ λsig,M,q.〈q,bin0,None ?,FS_crd sig〉.// qed.
151
152 lemma lift_halt_binaryTM : 
153   ∀sig,M,q.halt sig M q = halt ? (mk_binaryTM sig M) (state_bin_lift ? M q).
154 // qed.
155
156 lemma binaryTM_bin0_bin1 :
157   ∀sig,M,t,q,ch.
158   step ? (mk_binaryTM sig M) (mk_config ?? (〈q,bin0,ch,O〉) t) 
159   = mk_config ?? (〈q,bin1,ch,to_initN (FS_crd sig) ??〉) t. //
160 qed.
161
162 lemma binaryTM_bin0_bin4 :
163   ∀sig,M,t,q,ch,k.
164   current ? t = None ? → S k <S (2*FS_crd sig) → 
165   step ? (mk_binaryTM sig M) (mk_config ?? (〈q,bin0,ch,S k〉) t) 
166   = mk_config ?? (〈q,bin4,None ?,to_initN 0 ??〉) (tape_move ? t R). [2,3://]
167 #sig #M #t #q #ch #k #Hcur #Hk
168 whd in match (step ???); whd in match (trans ???);
169 >Hcur %
170 qed.
171
172 lemma binaryTM_bin0_true :
173   ∀sig,M,t,q,ch,k.
174   current ? t = Some ? true → S k <S (2*FS_crd sig) → 
175   step ? (mk_binaryTM sig M) (mk_config ?? (〈q,bin0,ch,S k〉) t) 
176   = mk_config ?? (〈q,bin0,FS_nth sig k,to_initN k ??〉) (tape_move ? t R).[2,3:/2 by lt_S_to_lt/]
177 #sig #M #t #q #ch #k #Hcur #Hk
178 whd in match (step ???); whd in match (trans ???);
179 >Hcur %
180 qed.
181
182 lemma binaryTM_bin0_false :
183   ∀sig,M,t,q,ch,k.
184   current ? t = Some ? false → S k <S (2*FS_crd sig) → 
185   step ? (mk_binaryTM sig M) (mk_config ?? (〈q,bin0,ch,S k〉) t) 
186   = mk_config ?? (〈q,bin0,ch,to_initN k ??〉) (tape_move ? t R).[2,3:/2 by lt_S_to_lt/]
187 #sig #M #t #q #ch #k #Hcur #Hk
188 whd in match (step ???); whd in match (trans ???);
189 >Hcur %
190 qed.
191
192 (* to be checked *)
193 axiom binary_to_bin_char :∀sig,csl,csr,a.
194   csl@true::csr=bin_char sig a → FS_nth ? (length ? csr) = Some ? a.
195
196 lemma binaryTM_phase1_midtape_aux :
197   ∀sig,M,q,ls,a,rs,k.
198   halt sig M q=false → 
199   ∀csr,csl,t,ch.length ? csr < S (2*FS_crd sig) → 
200   t = mk_tape ? (reverse ? csl@ls) (option_hd ? (csr@rs)) (tail ? (csr@rs)) → 
201   csl@csr = bin_char sig a → 
202   loopM ? (mk_binaryTM sig M) (S (length ? csr) + k)
203     (mk_config ?? (〈q,bin0,ch,length ? csr〉) t) 
204   = loopM ? (mk_binaryTM sig M) k 
205       (mk_config ?? (〈q,bin1,Some ? a,FS_crd sig〉) 
206         (mk_tape ? (reverse ? (bin_char ? a)@ls) (option_hd ? rs) (tail ? rs))). [2,3://]
207 #sig #M #q #ls #a #rs #k #Hhalt #csr elim csr
208 [ #csl #t #ch #Hlen #Ht >append_nil #Hcsl >loopM_unfold >loop_S_false [|normalize //]
209   <loopM_unfold @eq_f >binaryTM_bin0_bin1 @eq_f >Ht 
210   whd in match (step ???); whd in match (trans ???); <Hcsl %
211 | #c cases c
212   [ #csr0 #IH #csl #t #ch #Hlen #Ht #Heq >loopM_unfold >loop_S_false [|normalize //]
213     <loopM_unfold lapply (binary_to_bin_char … Heq) #Ha >binaryTM_bin0_true 
214     [| >Ht % ]
215     lapply (IH (csl@[true]) (tape_move FinBool t R) ????)
216     [ //
217     | >Ht whd in match (option_hd ??) in ⊢ (??%?); whd in match (tail ??) in ⊢ (??%?);
218       cases csr0
219       [ cases rs
220         [ normalize >rev_append_def >rev_append_def >reverse_append %
221         | #r1 #rs1 normalize >rev_append_def >rev_append_def >reverse_append % ]
222       | #c1 #csr1 normalize >rev_append_def >rev_append_def >reverse_append % ]
223     | /2/
224     | @(Some ? a) ]
225     #H whd in match (plus ??); >Ha >H @eq_f @eq_f2 //
226     
227 qed.
228
229
230 lemma binaryTM_phase1_midtape :
231   ∀sig,M,t,q,ls,a,rs,ch.
232   t = mk_tape ? ls (option_hd ? (bin_char ? a)) (tail ? (bin_char sig a@rs)) →
233   step ? (mk_binaryTM sig M) (mk_config ?? (〈q,bin0,ch,FS_crd sig〉) t) 
234   = mk_config ?? (〈q,bin1,ch,to_initN (FS_crd sig) ??〉) 
235       (mk_tape ? (reverse ? (bin_char ? a)@ls) (option_hd ? rs) (tail ? rs)). [2,3://]
236 #sig #M #t #q #ls #a #rs #ch #Ht
237 whd in match (step ???); whd in match (trans ???);
238 >Hcur %
239 qed.
240
241
242 lemma binaryTM_loop :
243  ∀sig,M,i,t,q,tf,qf.
244  loopM sig M i (mk_config ?? q t) = Some ? (mk_config ?? qf tf) →
245  ∃k.loopM ? (mk_binaryTM sig M) k 
246   (mk_config ?? (state_bin_lift ? M q) (tape_bin_lift ? t)) = 
247   Some ? (mk_config ?? (state_bin_lift ? M qf) (tape_bin_lift ? tf)).
248 #sig #M #i elim i
249 [ #t #q #qf #tf change with (None ?) in ⊢ (??%?→?); #H destruct (H)
250 | -i #i #IH #t #q #tf #qf
251   >loopM_unfold 
252   lapply (refl ? (halt sig M (cstate ?? (mk_config ?? q t))))
253   cases (halt ?? q) in ⊢ (???%→?); #Hhalt
254   [ >(loop_S_true ??? (λc.halt ?? (cstate ?? c)) (mk_config ?? q t) Hhalt)
255     #H destruct (H) %{1} >loopM_unfold >loop_S_true // ]
256   (* interesting case: more than one step *)
257   >(loop_S_false ??? (λc.halt ?? (cstate ?? c)) (mk_config ?? q t) Hhalt)
258   <loopM_unfold >(config_expand ?? (step ???)) #Hloop 
259   lapply (IH … Hloop) -IH * #k0 #IH <config_expand in Hloop; #Hloop
260   %{(S k0)}
261   
262
263
264 (*
265 theorem sem_binaryTM : ∀sig,M.
266   mk_binaryTM sig M ⊫ R_bin_lift ? (R_TM ? M (start ? M)).
267 #sig #M #t #i generalize in match t; -t
268 @(nat_elim1 … i) #m #IH #intape #outc #Hloop
269
270 *)