]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/tutorial/chapter9.ma
update in standard library
[helm.git] / matita / matita / lib / tutorial / chapter9.ma
1 (*
2 Broadcasting points
3
4 Intuitively, a regular expression e must be understood as a pointed expression with a single 
5 point in front of it. Since however we only allow points before symbols, we must broadcast 
6 this initial point inside e traversing all nullable subexpressions, that essentially corresponds 
7 to the ϵ-closure operation on automata. We use the notation •(_) to denote such an operation;
8 its definition is the expected one: let us start discussing an example.
9
10 Example
11 Let us broadcast a point inside (a + ϵ)(b*a + b)b. We start working in parallel on the 
12 first occurrence of a (where the point stops), and on ϵ that gets traversed. We have hence 
13 reached the end of a + ϵ and we must pursue broadcasting inside (b*a + b)b. Again, we work in 
14 parallel on the two additive subterms b^*a and b; the first point is allowed to both enter the 
15 star, and to traverse it, stopping in front of a; the second point just stops in front of b. 
16 No point reached that end of b^*a + b hence no further propagation is possible. In conclusion: 
17                •((a + ϵ)(b^*a + b)b) = 〈(•a + ϵ)((•b)^*•a + •b)b, false〉
18 *)
19
20 include "tutorial/chapter8.ma".
21
22 (* Broadcasting a point inside an item generates a pre, since the point could possibly reach 
23 the end of the expression. 
24 Broadcasting inside a i1+i2 amounts to broadcast in parallel inside i1 and i2.
25 If we define
26                  〈i1,b1〉 ⊕ 〈i2,b2〉 = 〈i1 + i2, b1∨ b2〉
27 then, we just have •(i1+i2) = •(i1)⊕ •(i2).
28 *)
29
30 definition lo ≝ λS:DeqSet.λa,b:pre S.〈fst … a + fst \dots b,snd \dots a ∨ snd \dots b〉.
31 notation "a ⊕ b" left associative with precedence 60 for @{'oplus $a $b}.
32 interpretation "oplus" 'oplus a b = (lo ? a b).
33
34 lemma lo_def: ∀S.∀i1,i2:pitem S.∀b1,b2. 〈i1,b1〉⊕〈i2,b2〉=〈i1+i2,b1∨b2〉.
35 // qed.
36
37 (*
38 Concatenation is a bit more complex. In order to broadcast a point inside i1 · i2 
39 we should start broadcasting it inside i1 and then proceed into i2 if and only if a 
40 point reached the end of i1. This suggests to define •(i1 · i2) as •(i1) ▹ i2, where 
41 e ▹ i is a general operation of concatenation between a pre and an item, defined by 
42 cases on the boolean in e: 
43
44        〈i1,true〉 ▹ i2  = i1 ◃ •(i_2)
45        〈i1,false〉 ▹ i2 = i1 · i2
46 In turn, ◃ says how to concatenate an item with a pre, that is however extremely simple:
47         i1 ◃ 〈i1,b〉  = 〈i_1 · i2, b〉
48 Let us come to the formalized definitions:
49 *)
50
51 definition pre_concat_r ≝ λS:DeqSet.λi:pitem S.λe:pre S.
52   match e with [ pair i1 b ⇒ 〈i · i1, b〉].
53  
54 notation "i ◃ e" left associative with precedence 60 for @{'lhd $i $e}.
55 interpretation "pre_concat_r" 'lhd i e = (pre_concat_r ? i e).
56
57 lemma eq_to_ex_eq: ∀S.∀A,B:word S → Prop. 
58   A = B → A ≐ B. 
59 #S #A #B #H >H #x % // qed.
60
61 (* The behaoviour of ◃ is summarized by the following, easy lemma: *)
62
63 lemma sem_pre_concat_r : ∀S,i.∀e:pre S.
64   \sem{i ◃ e} \doteq \sem{i} · \sem{|fst \dots e|} ∪ \sem{e}.
65 #S #i * #i1 #b1 cases b1 [2: @eq_to_ex_eq //] 
66 >sem_pre_true >sem_cat >sem_pre_true whd in match (fst ???); // 
67 qed.
68  
69 (* The definition of $•(-)$ (eclose) and ▹ (pre_concat_l) are mutually recursive.
70 In this situation, a viable alternative that is usually simpler to reason about, 
71 is to abstract one of the two functions with respect to the other. In particular
72 we abstract pre_concat_l with respect to an input bcast function from items to
73 pres. *)
74
75 definition pre_concat_l ≝ λS:DeqSet.λbcast:∀S:DeqSet.pitem S → pre S.λe1:pre S.λi2:pitem S.
76   match e1 with 
77   [ pair i1 b1 ⇒ match b1 with 
78     [ true ⇒ (i1 ◃ (bcast ? i2)) 
79     | false ⇒ 〈i1 · i2,false〉
80     ]
81   ].
82
83 notation "a ▹ b" left associative with precedence 60 for @{'tril eclose $a $b}.
84 interpretation "item-pre concat" 'tril op a b = (pre_concat_l ? op a b).
85
86 (* We are ready to give the formal definition of the broadcasting operation. *)
87
88 notation "•" non associative with precedence 60 for @{eclose ?}.
89
90 let rec eclose (S: DeqSet) (i: pitem S) on i : pre S ≝
91  match i with
92   [ pz ⇒ 〈 pz ?, false 〉
93   | pe ⇒ 〈 ϵ,  true 〉
94   | ps x ⇒ 〈 `.x, false 〉
95   | pp x ⇒ 〈 `.x, false 〉
96   | po i1 i2 ⇒ •i1 ⊕ •i2
97   | pc i1 i2 ⇒ •i1 ▹ i2
98   | pk i ⇒ 〈(fst \dots (•i))^*,true〉].
99   
100 notation "• x" non associative with precedence 60 for @{'eclose $x}.
101 interpretation "eclose" 'eclose x = (eclose ? x).
102
103 (* Here are a few simple properties of ▹ and •(-) *)
104
105 lemma pcl_true : ∀S.∀i1,i2:pitem S.
106   〈i1,true〉 ▹ i2 = i1 ◃ (•i2).
107 // qed.
108
109 lemma pcl_true_bis : ∀S.∀i1,i2:pitem S.
110   〈i1,true〉 ▹ i2 = 〈i1 · fst \dots (•i2), snd \dots (•i2)〉.
111 #S #i1 #i2 normalize cases (•i2) // qed.
112
113 lemma pcl_false: ∀S.∀i1,i2:pitem S.
114   〈i1,false〉 ▹  i2  = 〈i1 · i2, false〉.
115 // qed.
116
117 lemma eclose_plus: ∀S:DeqSet.∀i1,i2:pitem S.
118   •(i1 + i2) = •i1 ⊕ •i2.
119 // qed.
120
121 lemma eclose_dot: ∀S:DeqSet.∀i1,i2:pitem S.
122   •(i1 · i2) = •i1 ▹ i2.
123 // qed.
124
125 lemma eclose_star: ∀S:DeqSet.∀i:pitem S.
126   •i^* = 〈(fst \dots(•i))^*,true〉.
127 // qed.
128
129 (* The definition of •(-) (eclose) can then be lifted from items to pres
130 in the obvious way. *)
131
132 definition lift ≝ λS.λf:pitem S →pre S.λe:pre S. 
133   match e with 
134   [ pair i b ⇒ 〈fst \dots (f i), snd \dots (f i) ∨ b〉].
135   
136 definition preclose ≝ λS. lift S (eclose S). 
137 interpretation "preclose" 'eclose x = (preclose ? x).
138
139 (* Obviously, broadcasting does not change the carrier of the item,
140 as it is easily proved by structural induction. *)
141
142 lemma erase_bull : ∀S.∀i:pitem S. |fst \dots (•i)| = |i|.
143 #S #i elim i // 
144   [ #i1 #i2 #IH1 #IH2 >erase_dot <IH1 >eclose_dot
145     cases (•i1) #i11 #b1 cases b1 // <IH2 >pcl_true_bis //
146   | #i1 #i2 #IH1 #IH2 >eclose_plus >(erase_plus … i1) <IH1 <IH2
147     cases (•i1) #i11 #b1 cases (•i2) #i21 #b2 //  
148   | #i #IH >eclose_star >(erase_star … i) <IH cases (•i) //
149   ]
150 qed.
151
152 (* We are now ready to state the main semantic properties of ⊕, ◃ and •(-):
153
154 sem_oplus:     \sem{e1 ⊕ e2} =1 \sem{e1} ∪ \sem{e2} 
155 sem_pcl:       \sem{e1 ▹ i2} =1  \sem{e1} · \sem{|i2|} ∪ \sem{i2}
156 sem_bullet     \sem{•i} =1 \sem{i} ∪ \sem{|i|}
157
158 The proof of sem_oplus is straightforward. *)
159
160 lemma sem_oplus: ∀S:DeqSet.∀e1,e2:pre S.
161   \sem{e1 ⊕ e2} ≐ \sem{e1} ∪ \sem{e2}. 
162 #S * #i1 #b1 * #i2 #b2 #w %
163   [cases b1 cases b2 normalize /2/ * /3/ * /3/
164   |cases b1 cases b2 normalize /2/ * /3/ * /3/
165   ]
166 qed.
167
168 (* For the others, we proceed as follow: we first prove the following 
169 auxiliary lemma, that assumes sem_bullet:
170
171 sem_pcl_aux: 
172    \sem{•i2} =1  \sem{i2} ∪ \sem{|i2|} →
173    \sem{e1 ▹ i2} =1  \sem{e1} · \sem{|i2|} ∪ \sem{i2}.
174
175 Then, using the previous result, we prove sem_bullet by induction 
176 on i. Finally, sem_pcl_aux and sem_bullet give sem_pcl. *)
177
178 lemma LcatE : ∀S.∀e1,e2:pitem S.
179   \sem{e1 · e2} = \sem{e1} · \sem{|e2|} ∪ \sem{e2}. 
180 // qed.
181
182 lemma sem_pcl_aux : ∀S.∀e1:pre S.∀i2:pitem S.
183    \sem{•i2} ≐ \sem{i2} ∪ \sem{|i2|} →
184    \sem{e1 ▹ i2} ≐ \sem{e1} · \sem{|i2|} ∪ \sem{i2}.
185 #S * #i1 #b1 #i2 cases b1
186   [2:#th >pcl_false >sem_pre_false >sem_pre_false >sem_cat /2/
187   |#H >pcl_true >sem_pre_true @(eqP_trans … (sem_pre_concat_r …))
188    >erase_bull @eqP_trans [|@(eqP_union_l … H)]
189     @eqP_trans [|@eqP_union_l[|@union_comm ]]
190     @eqP_trans [|@eqP_sym @union_assoc ] /3/ 
191   ]
192 qed.
193   
194 lemma minus_eps_pre_aux: ∀S.∀e:pre S.∀i:pitem S.∀A. 
195  \sem{e} ≐ \sem{i} ∪ A → \sem{fst \dots e} ≐ \sem{i} ∪ (A - {[ ]}).
196 #S #e #i #A #seme
197 @eqP_trans [|@minus_eps_pre]
198 @eqP_trans [||@eqP_union_r [|@eqP_sym @minus_eps_item]]
199 @eqP_trans [||@distribute_substract] 
200 @eqP_substract_r //
201 qed.
202
203 theorem sem_bull: ∀S:DeqSet. ∀i:pitem S.  \sem{•i} ≐ \sem{i} ∪ \sem{|i|}.
204 #S #e elim e 
205   [#w normalize % [/2/ | * //]
206   |/2/ 
207   |#x normalize #w % [ /2/ | * [@False_ind | //]]
208   |#x normalize #w % [ /2/ | * // ] 
209   |#i1 #i2 #IH1 #IH2 >eclose_dot
210    @eqP_trans [|@sem_pcl_aux //] >sem_cat 
211    @eqP_trans
212      [|@eqP_union_r
213        [|@eqP_trans [|@(cat_ext_l … IH1)] @distr_cat_r]]
214    @eqP_trans [|@union_assoc]
215    @eqP_trans [||@eqP_sym @union_assoc]
216    @eqP_union_l //
217   |#i1 #i2 #IH1 #IH2 >eclose_plus
218    @eqP_trans [|@sem_oplus] >sem_plus >erase_plus 
219    @eqP_trans [|@(eqP_union_l … IH2)]
220    @eqP_trans [|@eqP_sym @union_assoc]
221    @eqP_trans [||@union_assoc] @eqP_union_r
222    @eqP_trans [||@eqP_sym @union_assoc]
223    @eqP_trans [||@eqP_union_l [|@union_comm]]
224    @eqP_trans [||@union_assoc] /2/
225   |#i #H >sem_pre_true >sem_star >erase_bull >sem_star
226    @eqP_trans [|@eqP_union_r [|@cat_ext_l [|@minus_eps_pre_aux //]]]
227    @eqP_trans [|@eqP_union_r [|@distr_cat_r]]
228    @eqP_trans [|@union_assoc] @eqP_union_l >erase_star 
229    @eqP_sym @star_fix_eps 
230   ]
231 qed.
232
233 (*
234 Blank item
235  
236
237 As a corollary of theorem sem_bullet, given a regular expression e, we can easily 
238 find an item with the same semantics of $e$: it is enough to get an item (blank e) 
239 having e as carrier and no point, and then broadcast a point in it. The semantics of
240 (blank e) is obviously the empty language: from the point of view of the automaton,
241 it corresponds with the pit state. *)
242
243 let rec blank (S: DeqSet) (i: re S) on i :pitem S ≝
244  match i with
245   [ z ⇒ pz ?
246   | e ⇒ ϵ
247   | s y ⇒ `y
248   | o e1 e2 ⇒ (blank S e1) + (blank S e2) 
249   | c e1 e2 ⇒ (blank S e1) · (blank S e2)
250   | k e ⇒ (blank S e)^* ].
251   
252 lemma forget_blank: ∀S.∀e:re S.|blank S e| = e.
253 #S #e elim e normalize //
254 qed.
255
256 lemma sem_blank: ∀S.∀e:re S.\sem{blank S e} ≐ ∅.
257 #S #e elim e 
258   [1,2:@eq_to_ex_eq // 
259   |#s @eq_to_ex_eq //
260   |#e1 #e2 #Hind1 #Hind2 >sem_cat 
261    @eqP_trans [||@(union_empty_r … ∅)] 
262    @eqP_trans [|@eqP_union_l[|@Hind2]] @eqP_union_r
263    @eqP_trans [||@(cat_empty_l … ?)] @cat_ext_l @Hind1
264   |#e1 #e2 #Hind1 #Hind2 >sem_plus 
265    @eqP_trans [||@(union_empty_r … ∅)] 
266    @eqP_trans [|@eqP_union_l[|@Hind2]] @eqP_union_r @Hind1
267   |#e #Hind >sem_star
268    @eqP_trans [||@(cat_empty_l … ?)] @cat_ext_l @Hind
269   ]
270 qed.
271    
272 theorem re_embedding: ∀S.∀e:re S. 
273   \sem{•(blank S e)} ≐ \sem{e}.
274 #S #e @eqP_trans [|@sem_bull] >forget_blank 
275 @eqP_trans [|@eqP_union_r [|@sem_blank]]
276 @eqP_trans [|@union_comm] @union_empty_r.
277 qed.
278
279 (*
280 Lifted Operators
281  
282
283 Plus and bullet have been already lifted from items to pres. We can now 
284 do a similar job for concatenation ⊙ and Kleene's star ⊛.*)
285
286 definition lifted_cat ≝ λS:DeqSet.λe:pre S. 
287   lift S (pre_concat_l S eclose e).
288
289 notation "e1 ⊙ e2" left associative with precedence 70 for @{'odot $e1 $e2}.
290
291 interpretation "lifted cat" 'odot e1 e2 = (lifted_cat ? e1 e2).
292
293 lemma odot_true_b : ∀S.∀i1,i2:pitem S.∀b. 
294   〈i1,true〉 ⊙ 〈i2,b〉 = 〈i1 · (fst \dots (•i2)),snd \dots (•i2) ∨ b〉.
295 #S #i1 #i2 #b normalize in ⊢ (??%?); cases (•i2) // 
296 qed.
297
298 lemma odot_false_b : ∀S.∀i1,i2:pitem S.∀b.
299   〈i1,false〉 ⊙ 〈i2,b〉 = 〈i1 · i2 ,b〉.
300 // 
301 qed.
302   
303 lemma erase_odot:∀S.∀e1,e2:pre S.
304   |fst \dots (e1 ⊙ e2)| = |fst \dots e1| · (|fst \dots e2|).
305 #S * #i1 * * #i2 #b2 // >odot_true_b >erase_dot 
306 whd in match (fst ???) in ⊢ (???%); whd in match (fst ???) in ⊢ (???%);//  
307 qed.
308
309 (* Let us come to the star operation: *)
310
311 definition lk ≝ λS:DeqSet.λe:pre S.
312   match e with 
313   [ pair i1 b1 ⇒
314     match b1 with 
315     [true ⇒ 〈(fst \dots (eclose ? i1))^*, true〉
316     |false ⇒ 〈i1^*,false〉
317     ]
318   ]. 
319
320 (* notation < "a \sup ⊛" non associative with precedence 90 for @{'lk $a}.*)
321 interpretation "lk" 'lk a = (lk ? a).
322 notation "a^⊛" non associative with precedence 90 for @{'lk $a}.
323
324 lemma ostar_true: ∀S.∀i:pitem S.
325   〈i,true〉^⊛ = 〈(fst \dots (•i))^*, true〉.
326 // qed.
327
328 lemma ostar_false: ∀S.∀i:pitem S.
329   〈i,false〉^⊛ = 〈i^*, false〉.
330 // qed.
331   
332 lemma erase_ostar: ∀S.∀e:pre S.
333   |fst \dots (e^⊛)| = |fst \dots e|^*.
334 #S * #i * whd in match (fst ???) in ⊢ (???%); // qed.
335
336 lemma sem_odot_true: ∀S:DeqSet.∀e1:pre S.∀i. 
337   \sem{e1 ⊙ 〈i,true〉} ≐ \sem{e1 ▹ i} ∪ { [ ] }.
338 #S #e1 #i 
339 cut (e1 ⊙ 〈i,true〉 = 〈fst \dots (e1 ▹ i), snd \dots(e1 ▹ i) ∨ true〉) [//]
340 #H >H cases (e1 ▹ i) #i1 #b1 cases b1 
341   [>sem_pre_true @eqP_trans [||@eqP_sym @union_assoc]
342    @eqP_union_l #w normalize % [/2/|* //] 
343   |/2/
344   ]
345 qed.
346
347 lemma eq_odot_false: ∀S:DeqSet.∀e1:pre S.∀i. 
348   e1 ⊙ 〈i,false〉 = e1 ▹ i.
349 #S #e1 #i  
350 cut (e1 ⊙ 〈i,false〉 = 〈fst \dots (e1 ▹ i), snd \dots(e1 ▹ i) ∨ false〉) [//]
351 cases (e1 ▹ i) #i1 #b1 cases b1 #H @H
352 qed.
353
354 (* We conclude this section with the proof of the main semantic properties
355 of ⊙ and ⊛. *)
356
357 lemma sem_odot: 
358   ∀S.∀e1,e2: pre S. \sem{e1 ⊙ e2} ≐ \sem{e1}· \sem{|fst \dots e2|} ∪ \sem{e2}.
359 #S #e1 * #i2 * 
360   [>sem_pre_true 
361    @eqP_trans [|@sem_odot_true]
362    @eqP_trans [||@union_assoc] @eqP_union_r @sem_pcl_aux //
363   |>sem_pre_false >eq_odot_false @sem_pcl_aux //
364   ]
365 qed.
366
367 theorem sem_ostar: ∀S.∀e:pre S. 
368   \sem{e^⊛} ≐  \sem{e} · \sem{|fst \dots e|}^*.
369 #S * #i #b cases b
370   [>sem_pre_true >sem_pre_true >sem_star >erase_bull
371    @eqP_trans [|@eqP_union_r[|@cat_ext_l [|@minus_eps_pre_aux //]]]
372    @eqP_trans [|@eqP_union_r [|@distr_cat_r]]
373    @eqP_trans [||@eqP_sym @distr_cat_r]
374    @eqP_trans [|@union_assoc] @eqP_union_l
375    @eqP_trans [||@eqP_sym @epsilon_cat_l] @eqP_sym @star_fix_eps 
376   |>sem_pre_false >sem_pre_false >sem_star /2/
377   ]
378 qed.