]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/lib/re/re.ma
Decidability of equality (draft)
[helm.git] / matita / matita / lib / re / re.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 "arithmetics/nat.ma".
16 include "basics/list.ma".
17
18 interpretation "iff" 'iff a b = (iff a b).  
19
20 record Alpha : Type[1] ≝ { carr :> Type[0];
21    eqb: carr → carr → bool;
22    eqb_true: ∀x,y. (eqb x y = true) ↔ (x = y)
23 }.
24  
25 notation "a == b" non associative with precedence 45 for @{ 'eqb $a $b }.
26 interpretation "eqb" 'eqb a b = (eqb ? a b).
27
28 definition word ≝ λS:Alpha.list S.
29
30 inductive re (S: Alpha) : Type[0] ≝
31    z: re S
32  | e: re S
33  | s: S → re S
34  | c: re S → re S → re S
35  | o: re S → re S → re S
36  | k: re S → re S.
37  
38 (* notation < "a \sup ⋇" non associative with precedence 90 for @{ 'pk $a}.*)
39 notation "a ^ *" non associative with precedence 90 for @{ 'pk $a}.
40 interpretation "star" 'pk a = (k ? a).
41 interpretation "or" 'plus a b = (o ? a b).
42            
43 notation "a · b" non associative with precedence 60 for @{ 'pc $a $b}.
44 interpretation "cat" 'pc a b = (c ? a b).
45
46 (* to get rid of \middot 
47 ncoercion c  : ∀S:Alpha.∀p:re S.  re S →  re S   ≝ c  on _p : re ?  to ∀_:?.?.
48 *)
49
50 notation < "a" non associative with precedence 90 for @{ 'ps $a}.
51 notation > "` term 90 a" non associative with precedence 90 for @{ 'ps $a}.
52 interpretation "atom" 'ps a = (s ? a).
53
54 notation "ϵ" non associative with precedence 90 for @{ 'epsilon }.
55 interpretation "epsilon" 'epsilon = (e ?).
56
57 notation "∅" non associative with precedence 90 for @{ 'empty }.
58 interpretation "empty" 'empty = (z ?).
59
60 let rec flatten (S : Alpha) (l : list (word S)) on l : word S ≝ 
61 match l with [ nil ⇒ [ ] | cons w tl ⇒ w @ flatten ? tl ].
62
63 let rec conjunct (S : Alpha) (l : list (word S)) (r : word S → Prop) on l: Prop ≝
64 match l with [ nil ⇒ ? | cons w tl ⇒ r w ∧ conjunct ? tl r ]. 
65 // qed.
66
67 definition empty_lang ≝ λS.λw:word S.False.
68 notation "{}" non associative with precedence 90 for @{'empty_lang}.
69 interpretation "empty lang" 'empty_lang = (empty_lang ?).
70
71 definition sing_lang ≝ λS.λx,w:word S.x=w.
72 (* notation "{x}" non associative with precedence 90 for @{'sing_lang $x}.*)
73 interpretation "sing lang" 'singl x = (sing_lang ? x).
74
75 definition union : ∀S,l1,l2,w.Prop ≝ λS.λl1,l2.λw:word S.l1 w ∨ l2 w.
76 interpretation "union lang" 'union a b = (union ? a b).
77
78 definition cat : ∀S,l1,l2,w.Prop ≝ 
79   λS.λl1,l2.λw:word S.∃w1,w2.w1 @ w2 = w ∧ l1 w1 ∧ l2 w2.
80 interpretation "cat lang" 'pc a b = (cat ? a b).
81
82 definition star ≝ λS.λl.λw:word S.∃lw.flatten ? lw = w ∧ conjunct ? lw l. 
83 interpretation "star lang" 'pk l = (star ? l).
84
85 let rec in_l (S : Alpha) (r : re S) on r : word S → Prop ≝ 
86 match r with
87 [ z ⇒ {}
88 | e ⇒ { [ ] }
89 | s x ⇒ { [x] }
90 | c r1 r2 ⇒ (in_l ? r1) · (in_l ? r2)
91 | o r1 r2 ⇒ (in_l ? r1) ∪ (in_l ? r2)
92 | k r1 ⇒ (in_l ? r1) ^*].
93
94 notation "\sem{term 19 E}" non associative with precedence 75 for @{'in_l $E}.
95 interpretation "in_l" 'in_l E = (in_l ? E).
96 interpretation "in_l mem" 'mem w l = (in_l ? l w).
97
98 lemma rsem_star : ∀S.∀r: re S. \sem{r^*} = \sem{r}^*.
99 // qed.
100
101 notation "a || b" left associative with precedence 30 for @{'orb $a $b}.
102 interpretation "orb" 'orb a b = (orb a b).
103
104 definition if_then_else ≝ λT:Type[0].λe,t,f.match e return λ_.T with [ true ⇒ t | false ⇒ f].
105 notation > "'if' term 19 e 'then' term 19 t 'else' term 19 f" non associative with precedence 19 for @{ 'if_then_else $e $t $f }.
106 notation < "'if' \nbsp term 19 e \nbsp 'then' \nbsp term 19 t \nbsp 'else' \nbsp term 90 f \nbsp" non associative with precedence 19 for @{ 'if_then_else $e $t $f }.
107 interpretation "if_then_else" 'if_then_else e t f = (if_then_else ? e t f).
108
109 inductive pitem (S: Alpha) : Type[0] ≝
110    pz: pitem S
111  | pe: pitem S
112  | ps: S → pitem S
113  | pp: S → pitem S
114  | pc: pitem S → pitem S → pitem S
115  | po: pitem S → pitem S → pitem S
116  | pk: pitem S → pitem S.
117  
118 definition pre ≝ λS.pitem S × bool.
119
120 interpretation "pstar" 'pk a = (pk ? a).
121 interpretation "por" 'plus a b = (po ? a b).
122 interpretation "pcat" 'pc a b = (pc ? a b).
123 notation < ".a" non associative with precedence 90 for @{ 'pp $a}.
124 notation > "`. term 90 a" non associative with precedence 90 for @{ 'pp $a}.
125 interpretation "ppatom" 'pp a = (pp ? a).
126
127 (* to get rid of \middot 
128 ncoercion pc : ∀S.∀p:pitem S. pitem S → pitem S  ≝ pc on _p : pitem ? to ∀_:?.?.
129 *)
130
131 interpretation "patom" 'ps a = (ps ? a).
132 interpretation "pepsilon" 'epsilon = (pe ?).
133 interpretation "pempty" 'empty = (pz ?).
134
135 let rec forget (S: Alpha) (l : pitem S) on l: re S ≝
136  match l with
137   [ pz ⇒ ∅
138   | pe ⇒ ϵ
139   | ps x ⇒ `x
140   | pp x ⇒ `x
141   | pc E1 E2 ⇒ (forget ? E1) · (forget ? E2)
142   | po E1 E2 ⇒ (forget ? E1) + (forget ? E2)
143   | pk E ⇒ (forget ? E)^* ].
144   
145 (* notation < "|term 19 e|" non associative with precedence 70 for @{'forget $e}.*)
146 interpretation "forget" 'norm a = (forget ? a).
147
148 let rec in_pl (S : Alpha) (r : pitem S) on r : word S → Prop ≝ 
149 match r with
150 [ pz ⇒ {}
151 | pe ⇒ {}
152 | ps _ ⇒ {}
153 | pp x ⇒ { [x] }
154 | pc r1 r2 ⇒ (in_pl ? r1) · \sem{forget ? r2} ∪ (in_pl ? r2)
155 | po r1 r2 ⇒ (in_pl ? r1) ∪ (in_pl ? r2)
156 | pk r1 ⇒ (in_pl ? r1) · \sem{forget ? r1}^*  ].
157
158 interpretation "in_pl" 'in_l E = (in_pl ? E).
159 interpretation "in_pl mem" 'mem w l = (in_pl ? l w).
160
161 definition epsilon ≝ λS,b.if b then { ([ ] : word S) } else {}.
162
163 interpretation "epsilon" 'epsilon = (epsilon ?).
164 notation < "ϵ b" non associative with precedence 90 for @{'app_epsilon $b}.
165 interpretation "epsilon lang" 'app_epsilon b = (epsilon ? b).
166
167 definition in_prl ≝ λS : Alpha.λp:pre S. 
168   if (\snd p) then \sem{\fst p} ∪ { ([ ] : word S) } else \sem{\fst p}.
169   
170 interpretation "in_prl mem" 'mem w l = (in_prl ? l w).
171 interpretation "in_prl" 'in_l E = (in_prl ? E).
172
173 lemma sem_pre_true : ∀S.∀i:pitem S. 
174   \sem{〈i,true〉} = \sem{i} ∪ { ([ ] : word S) }. 
175 // qed.
176
177 lemma sem_pre_false : ∀S.∀i:pitem S. 
178   \sem{〈i,false〉} = \sem{i}. 
179 // qed.
180
181 lemma sem_cat: ∀S.∀i1,i2:pitem S. 
182   \sem{i1 · i2} = \sem{i1} · \sem{|i2|} ∪ \sem{i2}.
183 // qed.
184
185 lemma sem_cat_w: ∀S.∀i1,i2:pitem S.∀w.
186   \sem{i1 · i2} w = ((\sem{i1} · \sem{|i2|}) w ∨ \sem{i2} w).
187 // qed.
188
189 lemma sem_plus: ∀S.∀i1,i2:pitem S. 
190   \sem{i1 + i2} = \sem{i1} ∪ \sem{i2}.
191 // qed.
192
193 lemma sem_plus_w: ∀S.∀i1,i2:pitem S.∀w. 
194   \sem{i1 + i2} w = (\sem{i1} w ∨ \sem{i2} w).
195 // qed.
196
197 lemma sem_star : ∀S.∀i:pitem S.
198   \sem{i^*} = \sem{i} · \sem{|i|}^*.
199 // qed.
200
201 lemma sem_star_w : ∀S.∀i:pitem S.∀w.
202   \sem{i^*} w = (∃w1,w2.w1 @ w2 = w ∧ \sem{i} w1 ∧ \sem{|i|}^* w2).
203 // qed.
204
205 lemma append_eq_nil : ∀S.∀w1,w2:word S. w1 @ w2 = [ ] → w1 = [ ].
206 #S #w1 #w2 cases w1 // #a #tl normalize #H destruct qed.
207
208 lemma not_epsilon_lp : ∀S:Alpha.∀e:pitem S. ¬ ([ ] ∈ e).
209 #S #e elim e normalize /2/  
210   [#r1 #r2 * #n1 #n2 % * /2/ * #w1 * #w2 * * #H 
211    >(append_eq_nil …H…) /2/
212   |#r1 #r2 #n1 #n2 % * /2/
213   |#r #n % * #w1 * #w2 * * #H >(append_eq_nil …H…) /2/
214   ]
215 qed.
216
217 (* lemma 12 *)
218 lemma epsilon_to_true : ∀S.∀e:pre S. [ ] ∈ e → \snd e = true.
219 #S * #i #b cases b // normalize #H @False_ind /2/ 
220 qed.
221
222 lemma true_to_epsilon : ∀S.∀e:pre S. \snd e = true → [ ] ∈ e.
223 #S * #i #b #btrue normalize in btrue; >btrue %2 // 
224 qed.
225
226 definition lo ≝ λS:Alpha.λa,b:pre S.〈\fst a + \fst b,\snd a || \snd b〉.
227 notation "a ⊕ b" left associative with precedence 60 for @{'oplus $a $b}.
228 interpretation "oplus" 'oplus a b = (lo ? a b).
229
230 lemma lo_def: ∀S.∀i1,i2:pitem S.∀b1,b2. 〈i1,b1〉⊕〈i2,b2〉=〈i1+i2,b1||b2〉.
231 // qed.
232
233 definition pre_concat_r ≝ λS:Alpha.λi:pitem S.λe:pre S.
234   match e with [ pair i1 b ⇒ 〈i · i1, b〉].
235  
236 notation "i ◂ e" left associative with precedence 60 for @{'ltrif $i $e}.
237 interpretation "pre_concat_r" 'ltrif i e = (pre_concat_r ? i e).
238   
239 definition eq_f1 ≝ λS.λa,b:word S → Prop.∀w.a w ↔ b w.
240 notation "A =1 B" non associative with precedence 45 for @{'eq_f1 $A $B}.
241 interpretation "eq f1" 'eq_f1 a b = (eq_f1 ? a b).
242
243 lemma eq_to_ex_eq: ∀S.∀A,B:word S → Prop. 
244   A = B → A =1 B. 
245 #S #A #B #H >H /2/ qed.
246
247 lemma ext_eq_trans: ∀S.∀A,B,C:word S → Prop. 
248   A =1 B → B =1 C → A =1 C. 
249 #S #A #B #C #eqAB #eqBC #w cases (eqAB w) cases (eqBC w) /4/
250 qed.   
251
252 lemma union_assoc: ∀S.∀A,B,C:word S → Prop. 
253   A ∪ B ∪ C =1 A ∪ (B ∪ C).
254 #S #A #B #C #w % [* [* /3/ | /3/] | * [/3/ | * /3/]
255 qed.   
256
257 lemma sem_pre_concat_r : ∀S,i.∀e:pre S.
258   \sem{i ◂ e} =1 \sem{i} · \sem{|\fst e|} ∪ \sem{e}.
259 #S #i * #i1 #b1 cases b1 /2/
260 >sem_pre_true >sem_cat >sem_pre_true /2/ 
261 qed.
262  
263 definition lc ≝ λS:Alpha.λbcast:∀S:Alpha.pitem S → pre S.λe1:pre S.λi2:pitem S.
264   match e1 with 
265   [ pair i1 b1 ⇒ match b1 with 
266     [ true ⇒ (i1 ◂ (bcast ? i2)) 
267     | false ⇒ 〈i1 · i2,false〉
268     ]
269   ].
270         
271 definition lift ≝ λS.λf:pitem S →pre S.λe:pre S. 
272   match e with 
273   [ pair i b ⇒ 〈\fst (f i), \snd (f i) || b〉].
274
275 notation "a ▸ b" left associative with precedence 60 for @{'lc eclose $a $b}.
276 interpretation "lc" 'lc op a b = (lc ? op a b).
277
278 definition lk ≝ λS:Alpha.λbcast:∀S:Alpha.∀E:pitem S.pre S.λe:pre S.
279   match e with 
280   [ pair i1 b1 ⇒
281     match b1 with 
282     [true ⇒ 〈(\fst (bcast ? i1))^*, true〉
283     |false ⇒ 〈i1^*,false〉
284     ]
285   ]. 
286
287 (*
288 lemma oplus_tt : ∀S: Alpha.∀i1,i2:pitem S. 
289   〈i1,true〉 ⊕ 〈i2,true〉  = 〈i1 + i2,true〉.
290 // qed.
291
292 lemma oplus_tf : ∀S: Alpha.∀i1,i2:pitem S. 
293   〈i1,true〉 ⊕ 〈i2,false〉  = 〈i1 + i2,true〉.
294 // qed.
295
296 lemma oplus_ft : ∀S: Alpha.∀i1,i2:pitem S. 
297   〈i1,false〉 ⊕ 〈i2,true〉  = 〈i1 + i2,true〉.
298 // qed.
299
300 lemma oplus_ff : ∀S: Alpha.∀i1,i2:pitem S. 
301   〈i1,false〉 ⊕ 〈i2,false〉  = 〈i1 + i2,false〉.
302 // qed. *)
303
304 (* notation < "a \sup ⊛" non associative with precedence 90 for @{'lk $op $a}.*)
305 interpretation "lk" 'lk op a = (lk ? op a).
306 notation "a^⊛" non associative with precedence 90 for @{'lk eclose $a}.
307
308 notation "•" non associative with precedence 60 for @{eclose ?}.
309
310 let rec eclose (S: Alpha) (i: pitem S) on i : pre S ≝
311  match i with
312   [ pz ⇒ 〈 ∅, false 〉
313   | pe ⇒ 〈 ϵ,  true 〉
314   | ps x ⇒ 〈 `.x, false〉
315   | pp x ⇒ 〈 `.x, false 〉
316   | po i1 i2 ⇒ •i1 ⊕ •i2
317   | pc i1 i2 ⇒ •i1 ▸ i2
318   | pk i ⇒ 〈(\fst (•i))^*,true〉].
319   
320 notation "• x" non associative with precedence 60 for @{'eclose $x}.
321 interpretation "eclose" 'eclose x = (eclose ? x).
322
323 lemma eclose_plus: ∀S:Alpha.∀i1,i2:pitem S.
324   •(i1 + i2) = •i1 ⊕ •i2.
325 // qed.
326
327 lemma eclose_dot: ∀S:Alpha.∀i1,i2:pitem S.
328   •(i1 · i2) = •i1 ▸ i2.
329 // qed.
330
331 lemma eclose_star: ∀S:Alpha.∀i:pitem S.
332   •i^* = 〈(\fst(•i))^*,true〉.
333 // qed.
334
335 definition reclose ≝ λS. lift S (eclose S). 
336 interpretation "reclose" 'eclose x = (reclose ? x).
337
338 lemma epsilon_or : ∀S:Alpha.∀b1,b2. epsilon S (b1 || b2) =1 ϵ b1 ∪ ϵ b2. 
339 #S #b1 #b2 #w % cases b1 cases b2 normalize /2/ * /2/ * ;
340 qed.
341
342 (*
343 lemma cupA : ∀S.∀a,b,c:word S → Prop.a ∪ b ∪ c = a ∪ (b ∪ c).
344 #S a b c; napply extP; #w; nnormalize; @; *; /3/; *; /3/; nqed.
345
346 nlemma cupC : ∀S. ∀a,b:word S → Prop.a ∪ b = b ∪ a.
347 #S a b; napply extP; #w; @; *; nnormalize; /2/; nqed.*)
348
349 (* theorem 16: 2 *)
350 lemma sem_oplus: ∀S:Alpha.∀e1,e2:pre S.
351   \sem{e1 ⊕ e2} =1 \sem{e1} ∪ \sem{e2}. 
352 #S * #i1 #b1 * #i2 #b2 #w %
353   [cases b1 cases b2 normalize /2/ * /3/ * /3/
354   |cases b1 cases b2 normalize /2/ * /3/ * /3/
355   ]
356 qed.
357
358 lemma odot_true : 
359   ∀S.∀i1,i2:pitem S.
360   〈i1,true〉 ▸ i2 = i1 ◂ (•i2).
361 // qed.
362
363 lemma odot_true_bis : 
364   ∀S.∀i1,i2:pitem S.
365   〈i1,true〉 ▸ i2 = 〈i1 · \fst (•i2), \snd (•i2)〉.
366 #S #i1 #i2 normalize cases (•i2) // qed.
367
368 lemma odot_false: 
369   ∀S.∀i1,i2:pitem S.
370   〈i1,false〉 ▸ i2 = 〈i1 · i2, false〉.
371 // qed.
372
373 lemma LcatE : ∀S.∀e1,e2:pitem S.
374   \sem{e1 · e2} = \sem{e1} · \sem{|e2|} ∪ \sem{e2}. 
375 // qed.
376
377 (*
378 nlemma cup_dotD : ∀S.∀p,q,r:word S → Prop.(p ∪ q) · r = (p · r) ∪ (q · r). 
379 #S p q r; napply extP; #w; nnormalize; @; 
380 ##[ *; #x; *; #y; *; *; #defw; *; /7/ by or_introl, or_intror, ex_intro, conj;
381 ##| *; *; #x; *; #y; *; *; /7/ by or_introl, or_intror, ex_intro, conj; ##]
382 nqed.
383
384 nlemma cup0 :∀S.∀p:word S → Prop.p ∪ {} = p.
385 #S p; napply extP; #w; nnormalize; @; /2/; *; //; *; nqed.*)
386
387 lemma erase_dot : ∀S.∀e1,e2:pitem S. |e1 · e2| = c ? (|e1|) (|e2|).
388 // qed.
389
390 lemma erase_plus : ∀S.∀i1,i2:pitem S.
391   |i1 + i2| = |i1| + |i2|.
392 // qed.
393
394 lemma erase_star : ∀S.∀i:pitem S.|i^*| = |i|^*. 
395 // qed.
396
397 definition substract := λS.λp,q:word S → Prop.λw.p w ∧ ¬ q w.
398 interpretation "substract" 'minus a b = (substract ? a b).
399
400 (* nlemma cup_sub: ∀S.∀a,b:word S → Prop. ¬ (a []) → a ∪ (b - {[]}) = (a ∪ b) - {[]}.
401 #S a b c; napply extP; #w; nnormalize; @; *; /4/; *; /4/; nqed.
402
403 nlemma sub0 : ∀S.∀a:word S → Prop. a - {} = a.
404 #S a; napply extP; #w; nnormalize; @; /3/; *; //; nqed.
405
406 nlemma subK : ∀S.∀a:word S → Prop. a - a = {}.
407 #S a; napply extP; #w; nnormalize; @; *; /2/; nqed.
408
409 nlemma subW : ∀S.∀a,b:word S → Prop.∀w.(a - b) w → a w.
410 #S a b w; nnormalize; *; //; nqed. *)
411
412 lemma erase_bull : ∀S.∀i:pitem S. |\fst (•i)| = |i|.
413 #S #i elim i // 
414   [ #i1 #i2 #IH1 #IH2 >erase_dot <IH1 >eclose_dot
415     cases (•i1) #i11 #b1 cases b1 // <IH2 >odot_true_bis //
416   | #i1 #i2 #IH1 #IH2 >eclose_plus >(erase_plus … i1) <IH1 <IH2
417     cases (•i1) #i11 #b1 cases (•i2) #i21 #b2 //  
418   | #i #IH >eclose_star >(erase_star … i) <IH cases (•i) //
419   ]
420 qed.
421
422 axiom eq_ext_sym: ∀S.∀A,B:word S →Prop. 
423   A =1 B → B =1 A.
424
425 axiom union_ext_l: ∀S.∀A,B,C:word S →Prop. 
426   A =1 C  → A ∪ B =1 C ∪ B.
427   
428 axiom union_ext_r: ∀S.∀A,B,C:word S →Prop. 
429   B =1 C → A ∪ B =1 A ∪ C.
430   
431 axiom union_comm : ∀S.∀A,B:word S →Prop. 
432   A ∪ B =1 B ∪ A.
433
434 axiom union_idemp: ∀S.∀A:word S →Prop. 
435   A ∪ A =1 A.
436
437 axiom cat_ext_l: ∀S.∀A,B,C:word S →Prop. 
438   A =1 C  → A · B =1 C · B.
439   
440 axiom cat_ext_r: ∀S.∀A,B,C:word S →Prop. 
441   B =1 C → A · B =1 A · C.
442   
443 lemma distr_cat_r: ∀S.∀A,B,C:word S →Prop.
444   (A ∪ B) · C =1  A · C ∪ B · C. 
445 #S #A #B #C #w %
446   [* #w1 * #w2 * * #eqw * /6/ |* * #w1 * #w2 * * /6/] 
447 qed.
448
449 axiom fix_star: ∀S.∀A:word S → Prop. 
450   A^* =1 A · A^* ∪ { [ ] }.
451
452 axiom star_epsilon: ∀S:Alpha.∀A:word S → Prop.
453   A^* ∪ { [ ] } =1 A^*.
454
455 lemma sem_eclose_star: ∀S:Alpha.∀i:pitem S.
456   \sem{〈i^*,true〉} =1 \sem{〈i,false〉}·\sem{|i|}^* ∪ { [ ] }.
457 /2/ qed.
458
459 (*
460 lemma sem_eclose_star: ∀S:Alpha.∀i:pitem S.
461   \sem{〈i^*,true〉} =1 \sem{〈i,true〉}·\sem{|i|}^* ∪ { [ ] }.
462 /2/ qed.
463
464 #S #i #b cases b 
465   [>sem_pre_true >sem_star
466   |/2/
467   ] *)
468
469 (* this kind of results are pretty bad for automation;
470    better not index them *)
471 lemma epsilon_cat_r: ∀S.∀A:word S →Prop.
472    A · { [ ] } =1  A. 
473 #S #A #w %
474   [* #w1 * #w2 * * #eqw #inw1 normalize #eqw2 <eqw //
475   |#inA @(ex_intro … w) @(ex_intro … [ ]) /3/
476   ]
477 qed-.
478
479 lemma epsilon_cat_l: ∀S.∀A:word S →Prop.
480    { [ ] } · A =1  A. 
481 #S #A #w %
482   [* #w1 * #w2 * * #eqw normalize #eqw2 <eqw <eqw2 //
483   |#inA @(ex_intro … [ ]) @(ex_intro … w) /3/
484   ]
485 qed-.
486
487
488 lemma distr_cat_r_eps: ∀S.∀A,C:word S →Prop.
489   (A ∪ { [ ] }) · C =1  A · C ∪ C. 
490 #S #A #C @ext_eq_trans [|@distr_cat_r |@union_ext_r @epsilon_cat_l]
491 qed.
492
493 (* axiom eplison_cut_l: ∀S.∀A:word S →Prop. 
494    { [ ] } · A =1  A.
495    
496  axiom eplison_cut_r: ∀S.∀A:word S →Prop.
497    A · { [ ] } =1  A. *)
498
499 (*
500 lemma eta_lp : ∀S.∀p:pre S.𝐋\p p = 𝐋\p 〈\fst p, \snd p〉.
501 #S p; ncases p; //; nqed.
502
503 nlemma epsilon_dot: ∀S.∀p:word S → Prop. {[]} · p = p. 
504 #S e; napply extP; #w; nnormalize; @; ##[##2: #Hw; @[]; @w; /3/; ##]
505 *; #w1; *; #w2; *; *; #defw defw1 Hw2; nrewrite < defw; nrewrite < defw1;
506 napply Hw2; nqed.*)
507
508 (* theorem 16: 1 → 3 *)
509 lemma odot_dot_aux : ∀S.∀e1:pre S.∀i2:pitem S.
510    \sem{•i2} =1  \sem{i2} ∪ \sem{|i2|} →
511    \sem{e1 ▸ i2} =1  \sem{e1} · \sem{|i2|} ∪ \sem{i2}.
512 #S * #i1 #b1 #i2 cases b1
513   [2:#th >odot_false >sem_pre_false >sem_pre_false >sem_cat /2/
514   |#H >odot_true >sem_pre_true @(ext_eq_trans … (sem_pre_concat_r …))
515    >erase_bull @ext_eq_trans [|@(union_ext_r … H)]
516     @ext_eq_trans [|@union_ext_r [|@union_comm ]]
517     @ext_eq_trans [|@eq_ext_sym @union_assoc ] /3/ 
518   ]
519 qed.
520
521 axiom star_fix : 
522   ∀S.∀X:word S → Prop.(X - {[ ]}) · X^* ∪ {[ ]} =1 X^*.
523   
524 axiom sem_fst: ∀S.∀e:pre S. \sem{\fst e} =1 \sem{e}-{[ ]}.
525
526 axiom sem_fst_aux: ∀S.∀e:pre S.∀i:pitem S.∀A. 
527  \sem{e} =1 \sem{i} ∪ A → \sem{\fst e} =1 \sem{i} ∪ (A - {[ ]}).
528
529 (* theorem 16: 1 *)
530 theorem sem_bull: ∀S:Alpha. ∀e:pitem S.  \sem{•e} =1 \sem{e} ∪ \sem{|e|}.
531 #S #e elim e 
532   [#w normalize % [/2/ | * //]
533   |/2/ 
534   |#x normalize #w % [ /2/ | * [@False_ind | //]]
535   |#x normalize #w % [ /2/ | * // ] 
536   |#i1 #i2 #IH1 #IH2 >eclose_dot
537    @ext_eq_trans [|@odot_dot_aux //] >sem_cat 
538    @ext_eq_trans
539      [|@union_ext_l 
540        [|@ext_eq_trans [|@(cat_ext_l … IH1)] @distr_cat_r]]
541    @ext_eq_trans [|@union_assoc]
542    @ext_eq_trans [||@eq_ext_sym @union_assoc]
543    @union_ext_r //
544   |#i1 #i2 #IH1 #IH2 >eclose_plus
545    @ext_eq_trans [|@sem_oplus] >sem_plus >erase_plus 
546    @ext_eq_trans [|@(union_ext_r … IH2)]
547    @ext_eq_trans [|@eq_ext_sym @union_assoc]
548    @ext_eq_trans [||@union_assoc] @union_ext_l
549    @ext_eq_trans [||@eq_ext_sym @union_assoc]
550    @ext_eq_trans [||@union_ext_r [|@union_comm]]
551    @ext_eq_trans [||@union_assoc] /3/
552   |#i #H >sem_pre_true >sem_star >erase_bull >sem_star
553    @ext_eq_trans [|@union_ext_l [|@cat_ext_l [|@sem_fst_aux //]]]
554    @ext_eq_trans [|@union_ext_l [|@distr_cat_r]]
555    @ext_eq_trans [|@union_assoc] @union_ext_r >erase_star @star_fix 
556   ]
557 qed.
558
559 definition lifted_cat ≝ λS:Alpha.λe:pre S. 
560   lift S (lc S eclose e).
561
562 notation "e1 ⊙ e2" left associative with precedence 70 for @{'odot $e1 $e2}.
563
564 interpretation "lifted cat" 'odot e1 e2 = (lifted_cat ? e1 e2).
565
566 lemma sem_odot_true: ∀S:Alpha.∀e1:pre S.∀i. 
567   \sem{e1 ⊙ 〈i,true〉} =1 \sem{e1 ▸ i} ∪ { [ ] }.
568 #S #e1 #i 
569 cut (e1 ⊙ 〈i,true〉 = 〈\fst (e1 ▸ i), \snd(e1 ▸ i) || true〉) [//]
570 #H >H cases (e1 ▸ i) #i1 #b1 cases b1 
571   [>sem_pre_true @ext_eq_trans [||@eq_ext_sym @union_assoc]
572    @union_ext_r /2/ 
573   |/2/
574   ]
575 qed.
576
577 lemma eq_odot_false: ∀S:Alpha.∀e1:pre S.∀i. 
578   e1 ⊙ 〈i,false〉 = e1 ▸ i.
579 #S #e1 #i  
580 cut (e1 ⊙ 〈i,false〉 = 〈\fst (e1 ▸ i), \snd(e1 ▸ i) || false〉) [//]
581 cases (e1 ▸ i) #i1 #b1 cases b1 #H @H
582 qed.
583
584 lemma sem_odot: 
585   ∀S.∀e1,e2: pre S. \sem{e1 ⊙ e2} =1 \sem{e1}· \sem{|\fst e2|} ∪ \sem{e2}.
586 #S #e1 * #i2 * 
587   [>sem_pre_true 
588    @ext_eq_trans [|@sem_odot_true]
589    @ext_eq_trans [||@union_assoc] @union_ext_l @odot_dot_aux //
590   |>sem_pre_false >eq_odot_false @odot_dot_aux //
591   ]
592 qed.
593
594 (*
595 nlemma dot_star_epsilon : ∀S.∀e:re S.𝐋 e · 𝐋 e^* ∪ {[]} =  𝐋 e^*.
596 #S e; napply extP; #w; nnormalize; @;
597 ##[ *; ##[##2: #H; nrewrite < H; @[]; /3/] *; #w1; *; #w2; 
598     *; *; #defw Hw1; *; #wl; *; #defw2 Hwl; @(w1 :: wl);
599     nrewrite < defw; nrewrite < defw2; @; //; @;//;
600 ##| *; #wl; *; #defw Hwl; ncases wl in defw Hwl; ##[#defw; #; @2; nrewrite < defw; //]
601     #x xs defw; *; #Hx Hxs; @; @x; @(flatten ? xs); nrewrite < defw;
602     @; /2/; @xs; /2/;##]
603  nqed.
604
605 nlemma nil_star : ∀S.∀e:re S. [ ] ∈ e^*.
606 #S e; @[]; /2/; nqed.
607
608 nlemma cupID : ∀S.∀l:word S → Prop.l ∪ l = l.
609 #S l; napply extP; #w; @; ##[*]//; #; @; //; nqed.
610
611 nlemma cup_star_nil : ∀S.∀l:word S → Prop. l^* ∪ {[]} = l^*.
612 #S a; napply extP; #w; @; ##[*; //; #H; nrewrite < H; @[]; @; //] #;@; //;nqed.
613
614 nlemma rcanc_sing : ∀S.∀A,C:word S → Prop.∀b:word S .
615   ¬ (A b) → A ∪ { (b) } = C → A = C - { (b) }.
616 #S A C b nbA defC; nrewrite < defC; napply extP; #w; @;
617 ##[ #Aw; /3/| *; *; //; #H nH; ncases nH; #abs; nlapply (abs H); *]
618 nqed.
619 *)
620
621 (* theorem 16: 4 *)      
622 theorem sem_ostar: ∀S.∀e:pre S. 
623   \sem{e^⊛} =1  \sem{e} · \sem{|\fst e|}^*.
624 #S * #i #b cases b
625   [>sem_pre_true >sem_pre_true >sem_star >erase_bull
626    @ext_eq_trans [|@union_ext_l [|@cat_ext_l [|@sem_fst_aux //]]]
627    @ext_eq_trans [|@union_ext_l [|@distr_cat_r]]
628    @ext_eq_trans [||@eq_ext_sym @distr_cat_r]
629    @ext_eq_trans [|@union_assoc] @union_ext_r 
630    @ext_eq_trans [||@eq_ext_sym @epsilon_cat_l] @star_fix 
631   |>sem_pre_false >sem_pre_false >sem_star /2/
632   ]
633 qed.
634   
635 (*
636 nlet rec pre_of_re (S : Alpha) (e : re S) on e : pitem S ≝ 
637   match e with 
638   [ z ⇒ pz ?
639   | e ⇒ pe ?
640   | s x ⇒ ps ? x
641   | c e1 e2 ⇒ pc ? (pre_of_re ? e1) (pre_of_re ? e2)
642   | o e1 e2 ⇒ po ? (pre_of_re ? e1) (pre_of_re ? e2)
643   | k e1 ⇒ pk ? (pre_of_re ? e1)].
644
645 nlemma notFalse : ¬False. @; //; nqed.
646
647 nlemma dot0 : ∀S.∀A:word S → Prop. {} · A = {}.
648 #S A; nnormalize; napply extP; #w; @; ##[##2: *]
649 *; #w1; *; #w2; *; *; //; nqed.
650
651 nlemma Lp_pre_of_re : ∀S.∀e:re S. 𝐋\p (pre_of_re ? e) = {}.
652 #S e; nelim e; ##[##1,2,3: //]
653 ##[ #e1 e2 H1 H2; nchange in match (𝐋\p (pre_of_re S (e1 e2))) with (?∪?);
654     nrewrite > H1; nrewrite > H2; nrewrite > (dot0…); nrewrite > (cupID…);//
655 ##| #e1 e2 H1 H2; nchange in match (𝐋\p (pre_of_re S (e1+e2))) with (?∪?);
656     nrewrite > H1; nrewrite > H2; nrewrite > (cupID…); //
657 ##| #e1 H1; nchange in match (𝐋\p (pre_of_re S (e1^* ))) with (𝐋\p (pre_of_re ??) · ?);
658     nrewrite > H1; napply dot0; ##]
659 nqed.
660
661 nlemma erase_pre_of_reK : ∀S.∀e. 𝐋 |pre_of_re S e| = 𝐋 e.
662 #S A; nelim A; //; 
663 ##[ #e1 e2 H1 H2; nchange in match (𝐋 (e1 · e2)) with (𝐋 e1·?);
664     nrewrite < H1; nrewrite < H2; //
665 ##| #e1 e2 H1 H2; nchange in match (𝐋 (e1 + e2)) with (𝐋 e1 ∪ ?);
666     nrewrite < H1; nrewrite < H2; //
667 ##| #e1 H1; nchange in match (𝐋  (e1^* )) with ((𝐋 e1)^* );
668     nrewrite < H1; //]
669 nqed.     
670
671 (* corollary 17 *)
672 nlemma L_Lp_bull : ∀S.∀e:re S.𝐋 e = 𝐋\p (•pre_of_re ? e).
673 #S e; nrewrite > (bull_cup…); nrewrite > (Lp_pre_of_re…);
674 nrewrite > (cupC…); nrewrite > (cup0…); nrewrite > (erase_pre_of_reK…); //;
675 nqed.
676
677 nlemma Pext : ∀S.∀f,g:word S → Prop. f = g → ∀w.f w → g w.
678 #S f g H; nrewrite > H; //; nqed.
679  
680 (* corollary 18 *)
681 ntheorem bull_true_epsilon : ∀S.∀e:pitem S. \snd (•e) = true ↔ [ ] ∈ |e|.
682 #S e; @;
683 ##[ #defsnde; nlapply (bull_cup ? e); nchange in match (𝐋\p (•e)) with (?∪?);
684     nrewrite > defsnde; #H; 
685     nlapply (Pext ??? H [ ] ?); ##[ @2; //] *; //;
686
687 *)
688
689