]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/universal/tuples.ma
Definition of the structure of the transition table of a
[helm.git] / matita / matita / lib / turing / universal / tuples.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
13 (* COMPARE BIT
14
15 *)
16
17 include "turing/universal/marks.ma".
18
19 definition STape ≝ FinProd … FSUnialpha FinBool.
20
21 definition only_bits ≝ λl.
22   ∀c.memb STape c l = true → is_bit (\fst c) = true.
23   
24 definition no_grids ≝ λl.
25   ∀c.memb STape c l = true → is_grid (\fst c) = false.
26
27 definition no_bars ≝ λl.
28   ∀c.memb STape c l = true → is_bar (\fst c) = false.
29
30 definition no_marks ≝ λl.
31   ∀c.memb STape c l = true → is_marked ? c = false.
32   
33 definition tuple_TM : nat → list STape → Prop ≝ 
34  λn,t.∃qin,qout,mv,b1,b2.
35  only_bits qin ∧ only_bits qout ∧ only_bits mv ∧
36  |qin| = n ∧ |qout| = n (* ∧ |mv| = ? *) ∧ 
37  t = qin@〈comma,b1〉::qout@〈comma,b2〉::mv.
38  
39 inductive table_TM : nat → list STape → Prop ≝ 
40 | ttm_nil  : ∀n.table_TM n [] 
41 | ttm_cons : ∀n,t1,T,b.tuple_TM n t1 → table_TM n T → table_TM n (t1@〈bar,b〉::T).
42
43 (*
44 l0 x* a l1 x0* a0 l2 ------> l0 x a* l1 x0 a0* l2
45    ^                               ^
46
47 if current (* x *) = #
48    then 
49    else if x = 0
50       then move_right; ----
51            adv_to_mark_r;
52            if current (* x0 *) = 0
53               then advance_mark ----
54                    adv_to_mark_l;
55                    advance_mark
56               else STOP
57       else x = 1 (* analogo *)
58
59 *)
60
61
62 (*
63    MARK NEXT TUPLE machine
64    (partially axiomatized)
65    
66    marks the first character after the first bar (rightwards)
67  *)
68  
69 definition bar_or_grid ≝ λc:STape.is_bar (\fst c) ∨ is_grid (\fst c).
70
71 definition mark_next_tuple ≝ 
72   seq ? (adv_to_mark_r ? bar_or_grid)
73      (ifTM ? (test_char ? (λc:STape.is_bar (\fst c)))
74        (move_right_and_mark ?) (nop ?) 1).
75
76 definition R_mark_next_tuple ≝ 
77   λt1,t2.
78     ∀ls,c,rs1,rs2.
79     (* c non può essere un separatore ... speriamo *)
80     t1 = midtape STape ls c (rs1@〈grid,false〉::rs2) → 
81     no_marks rs1 → no_grids rs1 → bar_or_grid c = false → 
82     (∃rs3,rs4,d,b.rs1 = rs3 @ 〈bar,false〉 :: rs4 ∧
83       no_bars rs3 ∧
84       Some ? 〈d,b〉 = option_hd ? (rs4@〈grid,false〉::rs2) ∧
85       t2 = midtape STape (〈bar,false〉::reverse ? rs3@c::ls) 〈d,true〉 (tail ? (rs4@〈grid,false〉::rs2)))
86     ∨
87     (no_bars rs1 ∧ t2 = midtape ? (reverse ? rs1@c::ls) 〈grid,false〉 rs2).
88      
89 axiom tech_split :
90   ∀A:DeqSet.∀f,l.
91    (∀x.memb A x l = true → f x = false) ∨
92    (∃l1,c,l2.f c = true ∧ l = l1@c::l2 ∧ ∀x.memb ? x l1 = true → f x = false).
93 (*#A #f #l elim l
94 [ % #x normalize #Hfalse *)
95      
96 theorem sem_mark_next_tuple :
97   Realize ? mark_next_tuple R_mark_next_tuple.
98 #intape 
99 lapply (sem_seq ? (adv_to_mark_r ? bar_or_grid)
100          (ifTM ? (test_char ? (λc:STape.is_bar (\fst c))) (move_right_and_mark ?) (nop ?) 1) ????)
101 [@sem_if [5: // |6: @sem_move_right_and_mark |7: // |*:skip]
102 | //
103 |||#Hif cases (Hif intape) -Hif
104    #j * #outc * #Hloop * #ta * #Hleft #Hright
105    @(ex_intro ?? j) @ex_intro [|% [@Hloop] ]
106    -Hloop
107    #ls #c #rs1 #rs2 #Hrs #Hrs1 #Hrs1' #Hc
108    cases (Hleft … Hrs)
109    [ * #Hfalse >Hfalse in Hc; #Htf destruct (Htf)
110    | * #_ #Hta cases (tech_split STape (λc.is_bar (\fst c)) rs1)
111      [ #H1 lapply (Hta rs1 〈grid,false〉 rs2 (refl ??) ? ?)
112        [ * #x #b #Hx whd in ⊢ (??%?); >(Hrs1' … Hx) >(H1 … Hx) %
113        | %
114        | -Hta #Hta cases Hright
115          [ * #tb * whd in ⊢ (%→?); #Hcurrent
116            @False_ind cases (Hcurrent 〈grid,false〉 ?)
117            [ normalize #Hfalse destruct (Hfalse)
118            | >Hta % ]
119          | * #tb * whd in ⊢ (%→?); #Hcurrent
120            cases (Hcurrent 〈grid,false〉 ?)
121            [  #_ #Htb whd in ⊢ (%→?); #Houtc
122              %2 %
123              [ @H1
124              | >Houtc >Htb >Hta % ]
125            | >Hta % ]
126          ]
127        ]
128     | * #rs3 * #c0 * #rs4 * * #Hc0 #Hsplit #Hrs3
129       % @(ex_intro ?? rs3) @(ex_intro ?? rs4)
130      lapply (Hta rs3 c0 (rs4@〈grid,false〉::rs2) ???)
131      [ #x #Hrs3' whd in ⊢ (??%?); >Hsplit in Hrs1;>Hsplit in Hrs3;
132        #Hrs3 #Hrs1 >(Hrs1 …) [| @memb_append_l1 @Hrs3'|]
133        >(Hrs3 … Hrs3') @Hrs1' >Hsplit @memb_append_l1 //
134      | whd in ⊢ (??%?); >Hc0 %
135      | >Hsplit >associative_append % ] -Hta #Hta
136        cases Hright
137        [ * #tb * whd in ⊢ (%→?); #Hta'
138          whd in ⊢ (%→?); #Htb
139          cases (Hta' c0 ?)
140          [ #_ #Htb' >Htb' in Htb; #Htb
141            generalize in match Hsplit; -Hsplit
142            cases rs4 in Hta;
143            [ #Hta #Hsplit >(Htb … Hta)
144              >(?:c0 = 〈bar,false〉)
145              [ @(ex_intro ?? grid) @(ex_intro ?? false)
146                % [ % [ % 
147                [(* Hsplit *) @daemon |(*Hrs3*) @daemon ] | % ] | % ] 
148                | (* Hc0 *) @daemon ]
149            | #r5 #rs5 >(eq_pair_fst_snd … r5)
150              #Hta #Hsplit >(Htb … Hta)
151              >(?:c0 = 〈bar,false〉)
152              [ @(ex_intro ?? (\fst r5)) @(ex_intro ?? (\snd r5))
153                % [ % [ % [ (* Hc0, Hsplit *) @daemon | (*Hrs3*) @daemon ] | % ]
154                      | % ] | (* Hc0 *) @daemon ] ] | >Hta % ]
155              | * #tb * whd in ⊢ (%→?); #Hta'
156                whd in ⊢ (%→?); #Htb
157                cases (Hta' c0 ?)
158                [ #Hfalse @False_ind >Hfalse in Hc0;
159                  #Hc0 destruct (Hc0)
160                | >Hta % ]
161 ]]]]
162 qed.
163
164 definition init_current ≝ 
165   seq ? (adv_to_mark_l ? (is_marked ?))
166     (seq ? (clear_mark ?)
167        (seq ? (adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
168           (seq ? (move_r ?) (mark ?)))).
169           
170 definition R_init_current ≝ λt1,t2.
171   ∀l1,c,l2,b,l3,c1,rs,c0,b0. no_marks l1 → no_grids l2 → is_grid c = false → 
172   Some ? 〈c0,b0〉 = option_hd ? (reverse ? (〈c,true〉::l2)) → 
173   t1 = midtape STape (l1@〈c,true〉::l2@〈grid,b〉::l3) 〈c1,false〉 rs → 
174   t2 = midtape STape (〈grid,b〉::l3) 〈c0,true〉
175         ((tail ? (reverse ? (l1@〈c,false〉::l2))@〈c1,false〉::rs)).
176
177 lemma sem_init_current : Realize ? init_current R_init_current.
178 #intape 
179 cases (sem_seq ????? (sem_adv_to_mark_l ? (is_marked ?))
180         (sem_seq ????? (sem_clear_mark ?)
181            (sem_seq ????? (sem_adv_to_mark_l ? (λc:STape.is_grid (\fst c)))
182              (sem_seq ????? (sem_move_r ?) (sem_mark ?)))) intape)
183 #k * #outc * #Hloop #HR 
184 @(ex_intro ?? k) @(ex_intro ?? outc) % [@Hloop]
185 cases HR -HR #ta * whd in ⊢ (%→?); #Hta 
186 * #tb * whd in ⊢ (%→?); #Htb 
187 * #tc * whd in ⊢ (%→?); #Htc 
188 * #td * whd in ⊢ (%→%→?); #Htd #Houtc
189 #l1 #c #l2 #b #l3 #c1 #rs #c0 #b0 #Hl1 #Hl2 #Hc #Hc0 #Hintape
190 cases (Hta … Hintape) [ * #Hfalse normalize in Hfalse; destruct (Hfalse) ]
191 -Hta * #_ #Hta lapply (Hta l1 〈c,true〉 ? (refl ??) ??) [@Hl1|%]
192 -Hta #Hta lapply (Htb … Hta) -Htb #Htb cases (Htc … Htb) [ >Hc -Hc * #Hc destruct (Hc) ] 
193 -Htc * #_ #Htc lapply (Htc … (refl ??) (refl ??) ?) [@Hl2]
194 -Htc #Htc lapply (Htd … Htc) -Htd
195 >reverse_append >reverse_cons 
196 >reverse_cons in Hc0; cases (reverse … l2)
197 [ normalize in ⊢ (%→?); #Hc0 destruct (Hc0)
198   #Htd >(Houtc … Htd) %
199 | * #c2 #b2 #tl2 normalize in ⊢ (%→?);
200   #Hc0 #Htd >(Houtc … Htd)
201   whd in ⊢ (???%); destruct (Hc0)
202   >associative_append >associative_append %
203 ]
204 qed.
205
206 definition match_tuple_step ≝ 
207   ifTM ? (test_char ? (λc:STape.¬ is_grid (\fst c))) 
208    (single_finalTM ? 
209      (seq ? compare
210       (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
211         (nop ?)
212         (seq ? mark_next_tuple 
213            (ifTM ? (test_char ? (λc:STape.is_grid (\fst c)))
214              (mark ?) (seq ? (move_l ?) init_current) tc_true)) tc_true)))
215     (nop ?) tc_true.
216
217 definition R_match_tuple_step_true ≝ λt1,t2.
218   ∀ls,c,l1,l2,c1,l3,l4,rs,n.
219   is_bit c = true → only_bits l1 → no_grids l2 → is_bit c1 = true →
220   only_bits l3 → n = |l2| → |l2| = |l3| →
221   table_TM (S n) (〈c1,true〉::l3@〈comma,false〉::l4) → 
222   t1 = midtape STape (〈grid,false〉::ls) 〈c,true〉 
223          (l1@〈grid,false〉::l2@〈bar,false〉::〈c1,true〉::l3@〈comma,false〉::l4@〈grid,false〉::rs) → 
224   (* facciamo match *)
225   (〈c,true〉::l2 = 〈c1,true〉::l3 ∧
226   t2 = midtape ? (reverse ? l2@〈c,false〉::〈grid,false〉::ls) 〈grid,false〉
227         (l2@〈bar,false〉::〈c1,false〉::l3@〈comma,true〉::l4@〈grid,false〉::rs))
228   ∨
229   (* non facciamo match e marchiamo la prossima tupla *)
230   (〈c,true〉::l2 ≠ 〈c1,true〉::l3 ∧
231    ∃c2,l5,l6,l7.l4 = l5@〈bar,false〉::〈c2,false〉::l6@〈comma,false〉::l7 ∧
232    (* condizioni su l5 l6 l7 *)
233    t2 = midtape STape (〈grid,false〉::ls) 〈c,true〉 
234          (l1@〈grid,false〉::l2@〈bar,false〉::〈c1,true〉::l3@〈comma,false〉::
235           l5@〈bar,false〉::〈c2,true〉::l6@〈comma,false〉::l7))
236   ∨  
237   (* non facciamo match e non c'è una prossima tupla:
238      non specifichiamo condizioni sul nastro di output, perché
239      non eseguiremo altre operazioni, quindi il suo formato non ci interessa *)
240   (〈c,true〉::l2 ≠ 〈c1,true〉::l3 ∧ no_bars l4 ∧ current ? t2 = Some ? 〈grid,true〉).  
241   
242 definition R_match_tuple_step_false ≝ λt1,t2.
243   ∀ls,c,rs.t1 = midtape STape ls c rs → is_grid (\fst c) = true ∧ t2 = t1.