]> matita.cs.unibo.it Git - helm.git/blob - weblib/tutorial/chapter8.ma
commit by user andrea
[helm.git] / weblib / tutorial / chapter8.ma
1 (*
2 \ 5h1\ 6Broadcasting points\ 5/h1\ 6
3 Intuitively, a regular expression e must be understood as a pointed expression with a single 
4 point in front of it. Since however we only allow points before symbols, we must broadcast 
5 this initial point inside e traversing all nullable subexpressions, that essentially corresponds 
6 to the ϵ-closure operation on automata. We use the notation •(_) to denote such an operation;
7 its definition is the expected one: let us start discussing an example.
8
9 \ 5b\ 6Example\ 5/b\ 6
10 Let us broadcast a point inside (a + ϵ)(b*a + b)b. We start working in parallel on the 
11 first occurrence of a (where the point stops), and on ϵ that gets traversed. We have hence 
12 reached the end of a + ϵ and we must pursue broadcasting inside (b*a + b)b. Again, we work in 
13 parallel on the two additive subterms b^*a and b; the first point is allowed to both enter the 
14 star, and to traverse it, stopping in front of a; the second point just stops in front of b. 
15 No point reached that end of b^*a + b hence no further propagation is possible. In conclusion: 
16                •((a + ϵ)(b^*a + b)b) = 〈(•a + ϵ)((•b)^*•a + •b)b, false〉
17 *)
18
19
20 include "tutorial/chapter7.ma".
21
22 definition lo ≝ λS:DeqSet.λa,b:pre S.〈\fst a + \fst b,\snd a ∨ \snd b〉.
23 notation "a ⊕ b" left associative with precedence 60 for @{'oplus $a $b}.
24 interpretation "oplus" 'oplus a b = (lo ? a b).
25
26 lemma lo_def: ∀S.∀i1,i2:pitem S.∀b1,b2. 〈i1,b1〉⊕〈i2,b2〉=〈i1+i2,b1∨b2〉.
27 // qed.
28
29 definition pre_concat_r ≝ λS:DeqSet.λi:pitem S.λe:pre S.
30   match e with [ mk_Prod i1 b ⇒ 〈i · i1, b〉].
31  
32 notation "i ◃ e" left associative with precedence 60 for @{'lhd $i $e}.
33 interpretation "pre_concat_r" 'lhd i e = (pre_concat_r ? i e).
34
35 lemma eq_to_ex_eq: ∀S.∀A,B:word S → Prop. 
36   A = B → A =1 B. 
37 #S #A #B #H >H /2/ qed.
38
39 lemma sem_pre_concat_r : ∀S,i.∀e:pre S.
40   \sem{i ◃ e} =1 \sem{i} · \sem{|\fst e|} ∪ \sem{e}.
41 #S #i * #i1 #b1 cases b1 [2: @eq_to_ex_eq //] 
42 >sem_pre_true >sem_cat >sem_pre_true /2/ 
43 qed.
44  
45 definition pre_concat_l ≝ λS:DeqSet.λbcast:∀S:DeqSet.pitem S → pre S.λe1:pre S.λi2:pitem S.
46   match e1 with 
47   [ mk_Prod i1 b1 ⇒ match b1 with 
48     [ true ⇒ (i1 ◃ (bcast ? i2)) 
49     | false ⇒ 〈i1 · i2,false〉
50     ]
51   ].
52
53 notation "a ▹ b" left associative with precedence 60 for @{'tril eclose $a $b}.
54 interpretation "item-pre concat" 'tril op a b = (pre_concat_l ? op a b).
55
56 notation "•" non associative with precedence 60 for @{eclose ?}.
57
58 let rec eclose (S: DeqSet) (i: pitem S) on i : pre S ≝
59  match i with
60   [ pz ⇒ 〈 `∅, false 〉
61   | pe ⇒ 〈 ϵ,  true 〉
62   | ps x ⇒ 〈 `.x, false〉
63   | pp x ⇒ 〈 `.x, false 〉
64   | po i1 i2 ⇒ •i1 ⊕ •i2
65   | pc i1 i2 ⇒ •i1 ▹ i2
66   | pk i ⇒ 〈(\fst (•i))^*,true〉].
67   
68 notation "• x" non associative with precedence 60 for @{'eclose $x}.
69 interpretation "eclose" 'eclose x = (eclose ? x).
70
71 lemma eclose_plus: ∀S:DeqSet.∀i1,i2:pitem S.
72   •(i1 + i2) = •i1 ⊕ •i2.
73 // qed.
74
75 lemma eclose_dot: ∀S:DeqSet.∀i1,i2:pitem S.
76   •(i1 · i2) = •i1 ▹ i2.
77 // qed.
78
79 lemma eclose_star: ∀S:DeqSet.∀i:pitem S.
80   •i^* = 〈(\fst(•i))^*,true〉.
81 // qed.
82
83 definition lift ≝ λS.λf:pitem S →pre S.λe:pre S. 
84   match e with 
85   [ mk_Prod i b ⇒ 〈\fst (f i), \snd (f i) ∨ b〉].
86   
87 definition preclose ≝ λS. lift S (eclose S). 
88 interpretation "preclose" 'eclose x = (preclose ? x).
89
90 (* theorem 16: 2 *)
91 lemma sem_oplus: ∀S:DeqSet.∀e1,e2:pre S.
92   \sem{e1 ⊕ e2} =1 \sem{e1} ∪ \sem{e2}. 
93 #S * #i1 #b1 * #i2 #b2 #w %
94   [cases b1 cases b2 normalize /2/ * /3/ * /3/
95   |cases b1 cases b2 normalize /2/ * /3/ * /3/
96   ]
97 qed.
98
99 lemma odot_true : 
100   ∀S.∀i1,i2:pitem S.
101   〈i1,true〉 ▹ i2 = i1 ◃ (•i2).
102 // qed.
103
104 lemma odot_true_bis : 
105   ∀S.∀i1,i2:pitem S.
106   〈i1,true〉 ▹ i2 = 〈i1 · \fst (•i2), \snd (•i2)〉.
107 #S #i1 #i2 normalize cases (•i2) // qed.
108
109 lemma odot_false: 
110   ∀S.∀i1,i2:pitem S.
111   〈i1,false〉 ▹ i2 = 〈i1 · i2, false〉.
112 // qed.
113
114 lemma LcatE : ∀S.∀e1,e2:pitem S.
115   \sem{e1 · e2} = \sem{e1} · \sem{|e2|} ∪ \sem{e2}. 
116 // qed.
117
118 lemma erase_bull : ∀S.∀i:pitem S. |\fst (•i)| = |i|.
119 #S #i elim i // 
120   [ #i1 #i2 #IH1 #IH2 >erase_dot <IH1 >eclose_dot
121     cases (•i1) #i11 #b1 cases b1 // <IH2 >odot_true_bis //
122   | #i1 #i2 #IH1 #IH2 >eclose_plus >(erase_plus … i1) <IH1 <IH2
123     cases (•i1) #i11 #b1 cases (•i2) #i21 #b2 //  
124   | #i #IH >eclose_star >(erase_star … i) <IH cases (•i) //
125   ]
126 qed.
127
128 (*
129 lemma sem_eclose_star: ∀S:DeqSet.∀i:pitem S.
130   \sem{〈i^*,true〉} =1 \sem{〈i,false〉}·\sem{|i|}^* ∪ {ϵ}.
131 /2/ qed.
132 *)
133
134 (* theorem 16: 1 → 3 *)
135 lemma odot_dot_aux : ∀S.∀e1:pre S.∀i2:pitem S.
136    \sem{•i2} =1  \sem{i2} ∪ \sem{|i2|} →
137    \sem{e1 ▹ i2} =1  \sem{e1} · \sem{|i2|} ∪ \sem{i2}.
138 #S * #i1 #b1 #i2 cases b1
139   [2:#th >odot_false >sem_pre_false >sem_pre_false >sem_cat /2/
140   |#H >odot_true >sem_pre_true @(eqP_trans … (sem_pre_concat_r …))
141    >erase_bull @eqP_trans [|@(eqP_union_l … H)]
142     @eqP_trans [|@eqP_union_l[|@union_comm ]]
143     @eqP_trans [|@eqP_sym @union_assoc ] /3/ 
144   ]
145 qed.
146   
147 lemma minus_eps_pre_aux: ∀S.∀e:pre S.∀i:pitem S.∀A. 
148  \sem{e} =1 \sem{i} ∪ A → \sem{\fst e} =1 \sem{i} ∪ (A - {[ ]}).
149 #S #e #i #A #seme
150 @eqP_trans [|@minus_eps_pre]
151 @eqP_trans [||@eqP_union_r [|@eqP_sym @minus_eps_item]]
152 @eqP_trans [||@distribute_substract] 
153 @eqP_substract_r //
154 qed.
155
156 (* theorem 16: 1 *)
157 theorem sem_bull: ∀S:DeqSet. ∀i:pitem S.  \sem{•i} =1 \sem{i} ∪ \sem{|i|}.
158 #S #e elim e 
159   [#w normalize % [/2/ | * //]
160   |/2/ 
161   |#x normalize #w % [ /2/ | * [@False_ind | //]]
162   |#x normalize #w % [ /2/ | * // ] 
163   |#i1 #i2 #IH1 #IH2 >eclose_dot
164    @eqP_trans [|@odot_dot_aux //] >sem_cat 
165    @eqP_trans
166      [|@eqP_union_r
167        [|@eqP_trans [|@(cat_ext_l … IH1)] @distr_cat_r]]
168    @eqP_trans [|@union_assoc]
169    @eqP_trans [||@eqP_sym @union_assoc]
170    @eqP_union_l //
171   |#i1 #i2 #IH1 #IH2 >eclose_plus
172    @eqP_trans [|@sem_oplus] >sem_plus >erase_plus 
173    @eqP_trans [|@(eqP_union_l … IH2)]
174    @eqP_trans [|@eqP_sym @union_assoc]
175    @eqP_trans [||@union_assoc] @eqP_union_r
176    @eqP_trans [||@eqP_sym @union_assoc]
177    @eqP_trans [||@eqP_union_l [|@union_comm]]
178    @eqP_trans [||@union_assoc] /2/
179   |#i #H >sem_pre_true >sem_star >erase_bull >sem_star
180    @eqP_trans [|@eqP_union_r [|@cat_ext_l [|@minus_eps_pre_aux //]]]
181    @eqP_trans [|@eqP_union_r [|@distr_cat_r]]
182    @eqP_trans [|@union_assoc] @eqP_union_l >erase_star 
183    @eqP_sym @star_fix_eps 
184   ]
185 qed.
186
187 (* blank item *)
188 let rec blank (S: DeqSet) (i: re S) on i :pitem S ≝
189  match i with
190   [ z ⇒ `∅
191   | e ⇒ ϵ
192   | s y ⇒ `y
193   | o e1 e2 ⇒ (blank S e1) + (blank S e2) 
194   | c e1 e2 ⇒ (blank S e1) · (blank S e2)
195   | k e ⇒ (blank S e)^* ].
196   
197 lemma forget_blank: ∀S.∀e:re S.|blank S e| = e.
198 #S #e elim e normalize //
199 qed.
200
201 lemma sem_blank: ∀S.∀e:re S.\sem{blank S e} =1 ∅.
202 #S #e elim e 
203   [1,2:@eq_to_ex_eq // 
204   |#s @eq_to_ex_eq //
205   |#e1 #e2 #Hind1 #Hind2 >sem_cat 
206    @eqP_trans [||@(union_empty_r … ∅)] 
207    @eqP_trans [|@eqP_union_l[|@Hind2]] @eqP_union_r
208    @eqP_trans [||@(cat_empty_l … ?)] @cat_ext_l @Hind1
209   |#e1 #e2 #Hind1 #Hind2 >sem_plus 
210    @eqP_trans [||@(union_empty_r … ∅)] 
211    @eqP_trans [|@eqP_union_l[|@Hind2]] @eqP_union_r @Hind1
212   |#e #Hind >sem_star
213    @eqP_trans [||@(cat_empty_l … ?)] @cat_ext_l @Hind
214   ]
215 qed.
216    
217 theorem re_embedding: ∀S.∀e:re S. 
218   \sem{•(blank S e)} =1 \sem{e}.
219 #S #e @eqP_trans [|@sem_bull] >forget_blank 
220 @eqP_trans [|@eqP_union_r [|@sem_blank]]
221 @eqP_trans [|@union_comm] @union_empty_r.
222 qed.
223
224 (* lefted operations *)
225 definition lifted_cat ≝ λS:DeqSet.λe:pre S. 
226   lift S (pre_concat_l S eclose e).
227
228 notation "e1 ⊙ e2" left associative with precedence 70 for @{'odot $e1 $e2}.
229
230 interpretation "lifted cat" 'odot e1 e2 = (lifted_cat ? e1 e2).
231
232 lemma odot_true_b : ∀S.∀i1,i2:pitem S.∀b. 
233   〈i1,true〉 ⊙ 〈i2,b〉 = 〈i1 · (\fst (•i2)),\snd (•i2) ∨ b〉.
234 #S #i1 #i2 #b normalize in ⊢ (??%?); cases (•i2) // 
235 qed.
236
237 lemma odot_false_b : ∀S.∀i1,i2:pitem S.∀b.
238   〈i1,false〉 ⊙ 〈i2,b〉 = 〈i1 · i2 ,b〉.
239 // 
240 qed.
241   
242 lemma erase_odot:∀S.∀e1,e2:pre S.
243   |\fst (e1 ⊙ e2)| = |\fst e1| · (|\fst e2|).
244 #S * #i1 * * #i2 #b2 // >odot_true_b >erase_dot //  
245 qed.
246
247 definition lk ≝ λS:DeqSet.λe:pre S.
248   match e with 
249   [ mk_Prod i1 b1 ⇒
250     match b1 with 
251     [true ⇒ 〈(\fst (eclose ? i1))^*, true〉
252     |false ⇒ 〈i1^*,false〉
253     ]
254   ]. 
255
256 (* notation < "a \sup ⊛" non associative with precedence 90 for @{'lk $a}.*)
257 interpretation "lk" 'lk a = (lk ? a).
258 notation "a^⊛" non associative with precedence 90 for @{'lk $a}.
259
260
261 lemma ostar_true: ∀S.∀i:pitem S.
262   〈i,true〉^⊛ = 〈(\fst (•i))^*, true〉.
263 // qed.
264
265 lemma ostar_false: ∀S.∀i:pitem S.
266   〈i,false〉^⊛ = 〈i^*, false〉.
267 // qed.
268   
269 lemma erase_ostar: ∀S.∀e:pre S.
270   |\fst (e^⊛)| = |\fst e|^*.
271 #S * #i * // qed.
272
273 lemma sem_odot_true: ∀S:DeqSet.∀e1:pre S.∀i. 
274   \sem{e1 ⊙ 〈i,true〉} =1 \sem{e1 ▹ i} ∪ { [ ] }.
275 #S #e1 #i 
276 cut (e1 ⊙ 〈i,true〉 = 〈\fst (e1 ▹ i), \snd(e1 ▹ i) ∨ true〉) [//]
277 #H >H cases (e1 ▹ i) #i1 #b1 cases b1 
278   [>sem_pre_true @eqP_trans [||@eqP_sym @union_assoc]
279    @eqP_union_l /2/ 
280   |/2/
281   ]
282 qed.
283
284 lemma eq_odot_false: ∀S:DeqSet.∀e1:pre S.∀i. 
285   e1 ⊙ 〈i,false〉 = e1 ▹ i.
286 #S #e1 #i  
287 cut (e1 ⊙ 〈i,false〉 = 〈\fst (e1 ▹ i), \snd(e1 ▹ i) ∨ false〉) [//]
288 cases (e1 ▹ i) #i1 #b1 cases b1 #H @H
289 qed.
290
291 lemma sem_odot: 
292   ∀S.∀e1,e2: pre S. \sem{e1 ⊙ e2} =1 \sem{e1}· \sem{|\fst e2|} ∪ \sem{e2}.
293 #S #e1 * #i2 * 
294   [>sem_pre_true 
295    @eqP_trans [|@sem_odot_true]
296    @eqP_trans [||@union_assoc] @eqP_union_r @odot_dot_aux //
297   |>sem_pre_false >eq_odot_false @odot_dot_aux //
298   ]
299 qed.
300
301 (* theorem 16: 4 *)      
302 theorem sem_ostar: ∀S.∀e:pre S. 
303   \sem{e^⊛} =1  \sem{e} · \sem{|\fst e|}^*.
304 #S * #i #b cases b
305   [>sem_pre_true >sem_pre_true >sem_star >erase_bull
306    @eqP_trans [|@eqP_union_r[|@cat_ext_l [|@minus_eps_pre_aux //]]]
307    @eqP_trans [|@eqP_union_r [|@distr_cat_r]]
308    @eqP_trans [||@eqP_sym @distr_cat_r]
309    @eqP_trans [|@union_assoc] @eqP_union_l
310    @eqP_trans [||@eqP_sym @epsilon_cat_l] @eqP_sym @star_fix_eps 
311   |>sem_pre_false >sem_pre_false >sem_star /2/
312   ]
313 qed.