]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/turing/universal/copy.ma
Progress
[helm.git] / matita / matita / lib / turing / universal / copy.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/tuples.ma".
18
19 definition write_states ≝ initN 2.
20
21 definition write ≝ λalpha,c.
22   mk_TM alpha write_states
23   (λp.let 〈q,a〉 ≝ p in
24     match q with 
25     [ O ⇒ 〈1,Some ? 〈c,N〉〉
26     | S _ ⇒ 〈1,None ?〉 ])
27   O (λx.x == 1).
28   
29 definition R_write ≝ λalpha,c,t1,t2.
30   ∀ls,x,rs.t1 = midtape alpha ls x rs → t2 = midtape alpha ls c rs.
31   
32 axiom sem_write : ∀alpha,c.Realize ? (write alpha c) (R_write alpha c).
33
34 definition copy_step_subcase ≝
35   λalpha,c,elseM.ifTM ? (test_char ? (λx.x == 〈c,true〉))
36     (seq (FinProd alpha FinBool) (adv_mark_r …)
37       (seq ? (move_l …)
38         (seq ? (adv_to_mark_l … (is_marked alpha))
39           (seq ? (write ? 〈c,false〉)
40             (seq ? (move_r …)
41               (seq ? (mark …)
42                 (seq ? (move_r …) (adv_to_mark_r … (is_marked alpha)))))))))
43     elseM tc_true.
44
45 definition R_copy_step_subcase ≝ 
46   λalpha,c,RelseM,t1,t2.
47     ∀a,l1,x0,a0,l2,x,l3.
48     t1 = midtape (FinProd … alpha FinBool) (l1@〈a0,false〉::〈x0,true〉::l2) 
49          〈x,true〉 (〈a,false〉::l3) → 
50     (∀c.memb ? c l1 = true → is_marked ? c = false) →          
51     (x = c ∧ t2 = midtape ? (〈x,false〉::l1@〈a0,true〉::〈x,false〉::l2) 〈a,true〉 l3) ∨
52     (x ≠ c ∧ RelseM t1 t2).
53     
54 axiom sem_copy_step_subcase : 
55   ∀alpha,c,elseM,RelseM.
56   Realize ? (copy_step_subcase alpha c elseM) (R_copy_step_subcase alpha c RelseM).
57     
58 (*
59 if current = 0,tt
60    then advance_mark_r;
61         move_l;
62         advance_to_mark_l;
63         write(0,ff)
64         move_r;
65         mark;
66         move_r;
67         advance_to_mark_r;
68 else if current = 1,tt
69    then advance_mark_r;
70         move_l;
71         advance_to_mark_l;
72         write(1,ff)
73         move_r;
74         mark;
75         move_r;
76         advance_to_mark_r;
77 else if current = null 
78    then advance_mark_r;
79         move_l;
80         advance_to_mark_l
81         adv_mark_r;
82         move_r;
83         advance_to_mark_r
84 *)
85
86 definition nocopy_subcase ≝
87   ifTM STape (test_char ? (λx:STape.x == 〈null,true〉))
88     (seq ? (adv_mark_r …)
89       (seq ? (move_l …)
90         (seq ? (adv_to_mark_l … (is_marked ?))
91           (seq ? (adv_mark_r …)
92             (seq ? (move_r …) (adv_to_mark_r … (is_marked ?)))))))
93     (nop ?) tc_true.
94
95 definition R_nocopy_subcase ≝ 
96   λt1,t2.
97     ∀a,l1,x0,a0,l2,x,l3.
98     t1 = midtape STape (l1@〈a0,false〉::〈x0,true〉::l2) 
99          〈x,true〉 (〈a,false〉::l3) → 
100     (∀c.memb ? c l1 = true → is_marked ? c = false) →          
101     (x = null ∧
102      t2 = midtape ? (〈x,false〉::l1@〈a0,true〉::〈x0,false〉::l2) 〈a,true〉 l3) ∨
103     (x ≠ null ∧ t2 = t1).
104     
105 axiom sem_nocopy_subcase : Realize ? nocopy_subcase R_nocopy_subcase.
106 (* #intape
107 cases (sem_if ? (test_char ? (λx:STape.x == 〈null,true〉)) ?????? tc_true
108  (sem_test_char ? (λx:STape.x == 〈null,true〉))
109         (sem_seq … (sem_adv_mark_r …)
110            (sem_seq … (sem_move_l …)
111              (sem_seq … (sem_adv_to_mark_l … (is_marked ?))
112                (sem_seq … (sem_adv_mark_r …)
113                  (sem_seq … (sem_move_r …) (sem_adv_to_mark_r … (is_marked ?))
114                  ))))) (sem_nop ?) intape)
115 #k * #outc * #Hloop #HR @(ex_intro ?? k) @(ex_intro ?? outc)  % [@Hloop] -Hloop
116 cases HR -HR
117 [| * #ta * whd in ⊢ (%→%→?); #Hta #Houtc
118    #ls #x #rs #Hintape %2  >Hintape in Hta; #Hta cases (Hta ? (refl ??)) -Hta #Hx #Hta %
119    [ lapply (\Pf Hx) @not_to_not #Hx' >Hx' %
120    | <Hta @Houtc ] ]
121 @daemon
122 qed. *)
123
124 definition copy_step ≝
125   ifTM ? (test_char STape (λc.is_bit (\fst c)))
126   (single_finalTM ? (copy_step_subcase FSUnialpha (bit false)
127     (copy_step_subcase FSUnialpha (bit true) nocopy_subcase)))
128   (nop ?)
129   tc_true.
130   
131 definition R_copy_step_true ≝ 
132   λt1,t2.
133     ∀a,l1,x0,a0,l2,c,l3.
134     t1 = midtape STape (l1@〈a0,false〉::〈x0,true〉::l2) 
135          〈c,true〉 (〈a,false〉::l3) → 
136     (∀c.memb ? c l1 = true → is_marked ? c = false) →          
137     (∃x. c = bit x ∧
138     t2 = midtape STape (〈bit x,false〉::l1@〈a0,true〉::〈bit x,false〉::l2) 〈a,true〉 l3) ∨
139     (c = null ∧
140      t2 = midtape ? (〈null,false〉::l1@〈a0,true〉::〈x0,false〉::l2) 〈a,true〉 l3).
141      
142 definition R_copy_step_false ≝ 
143   λt1,t2.
144    ∀ls,c,rs.t1 = midtape (FinProd … FSUnialpha FinBool) ls c rs → 
145    bit_or_null (\fst c) = false ∧ t2 = t1.
146
147 axiom sem_comp_step : 
148   accRealize ? copy_step (inr … (inl … (inr … 0))) R_copy_step_true R_copy_step_false.
149
150 (*
151 1) il primo carattere è marcato
152 2) l'ultimo carattere è l'unico che può essere null, gli altri sono bit
153 3) il terminatore non è né bit, né null
154 *)
155    
156 definition copy0 ≝ whileTM ? copy_step (inr … (inl … (inr … 0))).
157
158 let rec merge_config (l1,l2:list STape) ≝ 
159   match l1 with
160   [ nil ⇒ nil ?
161   | cons p1 l1' ⇒ match l2 with
162     [ nil ⇒ nil ? 
163     | cons p2 l2' ⇒ 
164            let 〈c1,b1〉 ≝ p1 in let 〈c2,b2〉 ≝ p2 in
165            match c2 with
166            [ null ⇒ p1 :: merge_config l1' l2'
167            | _ ⇒ p2 :: merge_config l1' l2' ] ] ].
168
169 definition R_copy0 ≝ λt1,t2.
170   ∀ls,c,c0,rs,l1,l3,l4.
171   t1 = midtape STape (l3@l4@〈c0,true〉::ls) 〈c,true〉 (l1@rs) → 
172   no_marks l1 → no_marks (l3@l4) → |l1| = |l4| → 
173   ∀l1',bv.〈c,false〉::l1 = l1'@[〈comma,bv〉] → only_bits_or_nulls l1' → 
174   ∀l4',bg.l4@[〈c0,true〉] = 〈grid,bg〉::l4' → only_bits_or_nulls l4' → 
175   (c = comma ∧ t2 = t1) ∨
176   (c ≠ comma ∧ 
177     t2 = midtape ? (reverse ? l1'@l3@〈grid,true〉::
178                   merge_config l4' (reverse ? l1')@ls) 
179      〈comma,true〉 rs).
180
181 axiom sem_copy0 : Realize ? copy0 R_copy0.
182
183 definition copy ≝ 
184   seq STape (move_l …) (seq ? (adv_to_mark_l … (is_marked ?))
185    (seq ? (clear_mark …) (seq ? (adv_to_mark_r … (is_marked ?)) (clear_mark …)))).
186
187 definition R_copy ≝ λt1,t2.
188   ∀ls,c,c0,rs,l1,l3,l4.
189   t1 = midtape STape (l3@〈grid,false〉::l4@〈c0,true〉::ls) 〈c,true〉 (l1@〈comma,false〉::rs) → 
190   no_marks l1 → no_marks l3 → no_marks l4 → |l1| = |l4| → 
191   only_bits_or_nulls (〈c0,true〉::l4) → only_bits_or_nulls (〈c,true〉::l1) → 
192   t2 = midtape STape (reverse ? l1@l3@〈grid,false〉::
193           merge_config (l4@[〈c0,false〉]) (reverse ? (〈c,false〉::l1))@ls) 
194      〈comma,false〉 rs.
195      
196 axiom sem_copy : Realize ? copy R_copy.