]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/nlibrary/re/re.ma
re 16.4 almost done
[helm.git] / helm / software / matita / nlibrary / 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 "datatypes/list.ma".
16 include "datatypes/pairs.ma".
17 include "arithmetics/nat.ma".
18
19 interpretation "iff" 'iff a b = (iff a b).  
20
21 nrecord Alpha : Type[1] ≝ { carr :> Type[0];
22    eqb: carr → carr → bool;
23    eqb_true: ∀x,y. (eqb x y = true) ↔ (x = y)
24 }.
25  
26 notation "a == b" non associative with precedence 45 for @{ 'eqb $a $b }.
27 interpretation "eqb" 'eqb a b = (eqb ? a b).
28
29 ndefinition word ≝ λS:Alpha.list S.
30
31 ninductive re (S: Alpha) : Type[0] ≝
32    z: re S
33  | e: re S
34  | s: S → re S
35  | c: re S → re S → re S
36  | o: re S → re S → re S
37  | k: re S → re S.
38  
39 notation < "a \sup ⋇" non associative with precedence 90 for @{ 'pk $a}.
40 notation > "a ^ *" non associative with precedence 90 for @{ 'pk $a}.
41 interpretation "star" 'pk a = (k ? a).
42 interpretation "or" 'plus a b = (o ? a b).
43            
44 notation "a · b" non associative with precedence 60 for @{ 'pc $a $b}.
45 interpretation "cat" 'pc a b = (c ? a b).
46
47 (* to get rid of \middot *)
48 ncoercion c  : ∀S:Alpha.∀p:re S.  re S →  re S   ≝ c  on _p : re ?  to ∀_:?.?.
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 nlet 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 nlet 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 ]. napply True. nqed.
65
66 ndefinition empty_lang ≝ λS.λw:word S.False.
67 notation "{}" non associative with precedence 90 for @{'empty_lang}.
68 interpretation "empty lang" 'empty_lang = (empty_lang ?).
69
70 ndefinition sing_lang ≝ λS.λx,w:word S.x=w.
71 notation "{x}" non associative with precedence 90 for @{'sing_lang $x}.
72 interpretation "sing lang" 'sing_lang x = (sing_lang ? x).
73
74 ndefinition union : ∀S,l1,l2,w.Prop ≝ λS.λl1,l2.λw:word S.l1 w ∨ l2 w.
75 interpretation "union lang" 'union a b = (union ? a b).
76
77 ndefinition cat : ∀S,l1,l2,w.Prop ≝ 
78   λS.λl1,l2.λw:word S.∃w1,w2.w1 @ w2 = w ∧ l1 w1 ∧ l2 w2.
79 interpretation "cat lang" 'pc a b = (cat ? a b).
80
81 ndefinition star ≝ λS.λl.λw:word S.∃lw.flatten ? lw = w ∧ conjunct ? lw l. 
82 interpretation "star lang" 'pk l = (star ? l).
83
84 notation > "𝐋 term 90 E" non associative with precedence 90 for @{in_l ? $E}.
85 nlet 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 ⇒ 𝐋 r1 · 𝐋 r2
91 | o r1 r2 ⇒  𝐋 r1 ∪ 𝐋 r2
92 | k r1 ⇒ (𝐋 r1) ^*].
93 notation "𝐋 term 90 E" non associative with precedence 90 for @{'in_l $E}.
94 interpretation "in_l" 'in_l E = (in_l ? E).
95 interpretation "in_l mem" 'mem w l = (in_l ? l w).
96
97 notation "a || b" left associative with precedence 30 for @{'orb $a $b}.
98 interpretation "orb" 'orb a b = (orb a b).
99
100 ndefinition if_then_else ≝ λT:Type[0].λe,t,f.match e return λ_.T with [ true ⇒ t | false ⇒ f].
101 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 }.
102 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 }.
103 interpretation "if_then_else" 'if_then_else e t f = (if_then_else ? e t f).
104
105 ninductive pitem (S: Alpha) : Type[0] ≝
106    pz: pitem S
107  | pe: pitem S
108  | ps: S → pitem S
109  | pp: S → pitem S
110  | pc: pitem S → pitem S → pitem S
111  | po: pitem S → pitem S → pitem S
112  | pk: pitem S → pitem S.
113  
114 ndefinition pre ≝ λS.pitem S × bool.
115
116 interpretation "pstar" 'pk a = (pk ? a).
117 interpretation "por" 'plus a b = (po ? a b).
118 interpretation "pcat" 'pc a b = (pc ? a b).
119 notation < ".a" non associative with precedence 90 for @{ 'pp $a}.
120 notation > "`. term 90 a" non associative with precedence 90 for @{ 'pp $a}.
121 interpretation "ppatom" 'pp a = (pp ? a).
122 (* to get rid of \middot *)
123 ncoercion pc : ∀S.∀p:pitem S. pitem S → pitem S  ≝ pc on _p : pitem ? to ∀_:?.?.
124 interpretation "patom" 'ps a = (ps ? a).
125 interpretation "pepsilon" 'epsilon = (pe ?).
126 interpretation "pempty" 'empty = (pz ?).
127
128 notation > ".|term 19 e|" non associative with precedence 90 for @{forget ? $e}.
129 nlet rec forget (S: Alpha) (l : pitem S) on l: re S ≝
130  match l with
131   [ pz ⇒ ∅
132   | pe ⇒ ϵ
133   | ps x ⇒ `x
134   | pp x ⇒ `x
135   | pc E1 E2 ⇒ (.|E1| · .|E2|)
136   | po E1 E2 ⇒ (.|E1| + .|E2|)
137   | pk E ⇒ .|E|^* ].
138 notation < ".|term 19 e|" non associative with precedence 90 for @{'forget $e}.
139 interpretation "forget" 'forget a = (forget ? a).
140
141 notation "\fst term 90 x" non associative with precedence 90 for @{'fst $x}.
142 interpretation "fst" 'fst x = (fst ? ? x).
143 notation > "\snd term 90 x" non associative with precedence 90 for @{'snd $x}.
144 interpretation "snd" 'snd x = (snd ? ? x).
145
146 notation > "𝐋\p\ term 90 E" non associative with precedence 90 for @{in_pl ? $E}.
147 nlet rec in_pl (S : Alpha) (r : pitem S) on r : word S → Prop ≝ 
148 match r with
149 [ pz ⇒ {}
150 | pe ⇒ {}
151 | ps _ ⇒ {}
152 | pp x ⇒ { [x] }
153 | pc r1 r2 ⇒ 𝐋\p\ r1 · 𝐋 .|r2| ∪ 𝐋\p\ r2
154 | po r1 r2 ⇒ 𝐋\p\ r1 ∪ 𝐋\p\ r2
155 | pk r1 ⇒ 𝐋\p\ r1 · 𝐋 (.|r1|^* ) ].
156 notation > "𝐋\p term 90 E" non associative with precedence 90 for @{'in_pl $E}.
157 notation "𝐋\sub(\p) term 90 E" non associative with precedence 90 for @{'in_pl $E}.
158 interpretation "in_pl" 'in_pl E = (in_pl ? E).
159 interpretation "in_pl mem" 'mem w l = (in_pl ? l w).
160
161 ndefinition 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 ndefinition in_prl ≝ λS : Alpha.λp:pre S.  𝐋\p\ (\fst p) ∪ ϵ (\snd p).
168   
169 interpretation "in_prl mem" 'mem w l = (in_prl ? l w).
170 interpretation "in_prl" 'in_pl E = (in_prl ? E).
171
172 nlemma append_eq_nil : ∀S.∀w1,w2:word S. w1 @ w2 = [ ] → w1 = [ ].
173 #S w1; nelim w1; //. #x tl IH w2; nnormalize; #abs; ndestruct; nqed.
174
175 (* lemma 12 *)
176 nlemma epsilon_in_true : ∀S.∀e:pre S. [ ] ∈ e ↔ \snd e = true.
177 #S r; ncases r; #e b; @; ##[##2: #H; nrewrite > H; @2; /2/; ##] ncases b;//; 
178 nnormalize; *; ##[##2:*] nelim e;
179 ##[ ##1,2: *; ##| #c; *; ##| #c; nnormalize; #; ndestruct; ##| ##7: #p H;
180 ##| #r1 r2 H G; *; ##[##2: /3/ by or_intror]
181 ##| #r1 r2 H1 H2; *; /3/ by or_intror, or_introl; ##]
182 *; #w1; *; #w2; *; *; #defw1; nrewrite > (append_eq_nil … w1 w2 …); /3/ by {};//;
183 nqed.
184
185 nlemma not_epsilon_lp : ∀S.∀e:pitem S. ¬ (𝐋\p e [ ]).
186 #S e; nelim e; nnormalize; /2/ by nmk;
187 ##[ #; @; #; ndestruct;
188 ##| #r1 r2 n1 n2; @; *; /2/; *; #w1; *; #w2; *; *; #H;
189     nrewrite > (append_eq_nil …H…); /2/;
190 ##| #r1 r2 n1 n2; @; *; /2/;
191 ##| #r n; @; *; #w1; *; #w2; *; *; #H;     
192     nrewrite > (append_eq_nil …H…); /2/;##]
193 nqed.
194
195 ndefinition lo ≝ λS:Alpha.λa,b:pre S.〈\fst a + \fst b,\snd a || \snd b〉.
196 notation "a ⊕ b" left associative with precedence 60 for @{'oplus $a $b}.
197 interpretation "oplus" 'oplus a b = (lo ? a b).
198
199 ndefinition lc ≝ λS:Alpha.λbcast:∀S:Alpha.∀E:pitem S.pre S.λa,b:pre S.
200    match a with [ mk_pair e1 b1 ⇒
201    match b1 with 
202    [ false ⇒ 〈e1 · \fst b, \snd b〉 
203    | true ⇒ 〈e1 · \fst (bcast ? (\fst b)),\snd b || \snd (bcast ? (\fst b))〉]].
204    
205 notation < "a ⊙ b" left associative with precedence 60 for @{'lc $op $a $b}.
206 interpretation "lc" 'lc op a b = (lc ? op a b).
207 notation > "a ⊙ b" left associative with precedence 60 for @{'lc eclose $a $b}.
208
209 ndefinition lk ≝ λS:Alpha.λbcast:∀S:Alpha.∀E:pitem S.pre S.λa:pre S.
210    match a with [ mk_pair e1 b1 ⇒
211    match b1 with 
212    [ false ⇒ 〈e1^*, false〉 
213    | true ⇒ 〈(\fst (bcast ? e1))^*, true〉]].
214
215 notation < "a \sup ⊛" non associative with precedence 90 for @{'lk $op $a}.
216 interpretation "lk" 'lk op a = (lk ? op a).
217 notation > "a^⊛" non associative with precedence 90 for @{'lk eclose $a}.
218
219 notation > "•" non associative with precedence 60 for @{eclose ?}.
220 nlet rec eclose (S: Alpha) (E: pitem S) on E : pre S ≝
221  match E with
222   [ pz ⇒ 〈 ∅, false 〉
223   | pe ⇒ 〈 ϵ,  true 〉
224   | ps x ⇒ 〈 `.x, false 〉
225   | pp x ⇒ 〈 `.x, false 〉
226   | po E1 E2 ⇒ •E1 ⊕ •E2
227   | pc E1 E2 ⇒ •E1 ⊙ 〈 E2, false 〉 
228   | pk E ⇒ 〈(\fst (•E))^*,true〉].
229 notation < "• x" non associative with precedence 60 for @{'eclose $x}.
230 interpretation "eclose" 'eclose x = (eclose ? x).
231 notation > "• x" non associative with precedence 60 for @{'eclose $x}.
232
233 ndefinition reclose ≝ λS:Alpha.λp:pre S.let p' ≝ •\fst p in 〈\fst p',\snd p || \snd p'〉.
234 interpretation "reclose" 'eclose x = (reclose ? x).
235
236 ndefinition eq_f1 ≝ λS.λa,b:word S → Prop.∀w.a w ↔ b w.
237 notation > "A =1 B" non associative with precedence 45 for @{'eq_f1 $A $B}.
238 notation "A =\sub 1 B" non associative with precedence 45 for @{'eq_f1 $A $B}.
239 interpretation "eq f1" 'eq_f1 a b = (eq_f1 ? a b).
240
241 naxiom extP : ∀S.∀p,q:word S → Prop.(p =1 q) → p = q.
242
243 nlemma epsilon_or : ∀S:Alpha.∀b1,b2. ϵ(b1 || b2) = ϵ b1 ∪ ϵ b2. ##[##2: napply S]
244 #S b1 b2; ncases b1; ncases b2; napply extP; #w; nnormalize; @; /2/; *; //; *;
245 nqed.
246
247 nlemma cupA : ∀S.∀a,b,c:word S → Prop.a ∪ b ∪ c = a ∪ (b ∪ c).
248 #S a b c; napply extP; #w; nnormalize; @; *; /3/; *; /3/; nqed.
249
250 nlemma cupC : ∀S. ∀a,b:word S → Prop.a ∪ b = b ∪ a.
251 #S a b; napply extP; #w; @; *; nnormalize; /2/; nqed.
252
253 (* theorem 16: 2 *)
254 nlemma oplus_cup : ∀S:Alpha.∀e1,e2:pre S.𝐋\p (e1 ⊕ e2) = 𝐋\p e1 ∪ 𝐋\p e2.
255 #S r1; ncases r1; #e1 b1 r2; ncases r2; #e2 b2;
256 nwhd in ⊢ (??(??%)?);
257 nchange in ⊢(??%?) with (𝐋\p (e1 + e2) ∪ ϵ (b1 || b2));
258 nchange in ⊢(??(??%?)?) with (𝐋\p (e1) ∪ 𝐋\p (e2));
259 nrewrite > (epsilon_or S …); nrewrite > (cupA S (𝐋\p e1) …);
260 nrewrite > (cupC ? (ϵ b1) …); nrewrite < (cupA S (𝐋\p e2) …);
261 nrewrite > (cupC ? ? (ϵ b1) …); nrewrite < (cupA …); //;
262 nqed.
263
264 nlemma odotEt : 
265   ∀S.∀e1,e2:pitem S.∀b2. 〈e1,true〉 ⊙ 〈e2,b2〉 = 〈e1 · \fst (•e2),b2 || \snd (•e2)〉.
266 #S e1 e2 b2; nnormalize; ncases (•e2); //; nqed.
267
268 nlemma LcatE : ∀S.∀e1,e2:pitem S.𝐋\p (e1 · e2) =  𝐋\p e1 · 𝐋 .|e2| ∪ 𝐋\p e2. //; nqed.
269
270 nlemma cup_dotD : ∀S.∀p,q,r:word S → Prop.(p ∪ q) · r = (p · r) ∪ (q · r). 
271 #S p q r; napply extP; #w; nnormalize; @; 
272 ##[ *; #x; *; #y; *; *; #defw; *; /7/ by or_introl, or_intror, ex_intro, conj;
273 ##| *; *; #x; *; #y; *; *; /7/ by or_introl, or_intror, ex_intro, conj; ##]
274 nqed.
275
276 nlemma cup0 :∀S.∀p:word S → Prop.p ∪ {} = p.
277 #S p; napply extP; #w; nnormalize; @; /2/; *; //; *; nqed.
278
279 nlemma erase_dot : ∀S.∀e1,e2:pitem S.𝐋 .|e1 · e2| =  𝐋 .|e1| · 𝐋 .|e2|.
280 #S e1 e2; napply extP; nnormalize; #w; @; *; #w1; *; #w2; *; *; /7/ by ex_intro, conj;
281 nqed.
282
283 nlemma erase_plus : ∀S.∀e1,e2:pitem S.𝐋 .|e1 + e2| =  𝐋 .|e1| ∪ 𝐋 .|e2|.
284 #S e1 e2; napply extP; nnormalize; #w; @; *; /4/ by or_introl, or_intror; nqed.
285
286 nlemma erase_star : ∀S.∀e1:pitem S.𝐋 .|e1|^* = 𝐋 .|e1^*|. //; nqed.
287
288 ndefinition substract := λS.λp,q:word S → Prop.λw.p w ∧ ¬ q w.
289 interpretation "substract" 'minus a b = (substract ? a b).
290
291 nlemma cup_sub: ∀S.∀a,b:word S → Prop. ¬ (a []) → a ∪ (b - {[]}) = (a ∪ b) - {[]}.
292 #S a b c; napply extP; #w; nnormalize; @; *; /4/; *; /4/; nqed.
293
294 nlemma sub0 : ∀S.∀a:word S → Prop. a - {} = a.
295 #S a; napply extP; #w; nnormalize; @; /3/; *; //; nqed.
296
297 nlemma subK : ∀S.∀a:word S → Prop. a - a = {}.
298 #S a; napply extP; #w; nnormalize; @; *; /2/; nqed.
299
300 nlemma subW : ∀S.∀a,b:word S → Prop.∀w.(a - b) w → a w.
301 #S a b w; nnormalize; *; //; nqed.
302
303 nlemma erase_bull : ∀S.∀a:pitem S. .|\fst (•a)| = .|a|.
304 #S a; nelim a; // by {};
305 ##[ #e1 e2 IH1 IH2; nchange in ⊢ (???%) with (.|e1| · .|e2|);
306     nrewrite < IH1; nrewrite < IH2;  
307     nchange in ⊢ (??(??%)?) with (\fst (•e1 ⊙ 〈e2,false〉));
308     ncases (•e1); #e3 b; ncases b; nnormalize;
309     ##[ ncases (•e2); //; ##| nrewrite > IH2; //]
310 ##| #e1 e2 IH1 IH2; nchange in ⊢ (???%) with (.|e1| + .|e2|);
311     nrewrite < IH2; nrewrite < IH1;
312     nchange in ⊢ (??(??%)?) with (\fst (•e1 ⊕ •e2));
313     ncases (•e1); ncases (•e2); //;
314 ##| #e IH; nchange in ⊢ (???%) with (.|e|^* ); nrewrite < IH;
315     nchange in ⊢ (??(??%)?) with (\fst (•e))^*; //; ##]
316 nqed.
317
318 nlemma eta_lp : ∀S.∀p:pre S.𝐋\p p = 𝐋\p 〈\fst p, \snd p〉.
319 #S p; ncases p; //; nqed.
320
321 nlemma epsilon_dot: ∀S.∀p:word S → Prop. {[]} · p = p. 
322 #S e; napply extP; #w; nnormalize; @; ##[##2: #Hw; @[]; @w; /3/; ##]
323 *; #w1; *; #w2; *; *; #defw defw1 Hw2; nrewrite < defw; nrewrite < defw1;
324 napply Hw2; nqed.
325
326 (* theorem 16: 1 → 3 *)
327 nlemma odot_dot_aux : ∀S.∀e1,e2: pre S.
328       𝐋\p (•(\fst e2)) =  𝐋\p (\fst e2) ∪ 𝐋 .|\fst e2| → 
329          𝐋\p (e1 ⊙ e2) =  𝐋\p e1 · 𝐋 .|\fst e2| ∪ 𝐋\p e2.
330 #S e1 e2 th1; ncases e1; #e1' b1'; ncases b1';
331 ##[ nwhd in ⊢ (??(??%)?); nletin e2' ≝ (\fst e2); nletin b2' ≝ (\snd e2); 
332     nletin e2'' ≝ (\fst (•(\fst e2))); nletin b2'' ≝ (\snd (•(\fst e2)));
333     nchange in ⊢ (??%?) with (?∪?); 
334     nchange in ⊢ (??(??%?)?) with (?∪?);
335     nchange in match (𝐋\p 〈?,?〉) with (?∪?);
336     nrewrite > (epsilon_or …); nrewrite > (cupC ? (ϵ ?)…);
337     nrewrite > (cupA …);nrewrite < (cupA ?? (ϵ?)…);
338     nrewrite > (?: 𝐋\p e2'' ∪ ϵ b2'' = 𝐋\p e2' ∪ 𝐋 .|e2'|); ##[##2:
339       nchange with (𝐋\p 〈e2'',b2''〉 =  𝐋\p e2' ∪ 𝐋 .|e2'|); 
340       ngeneralize in match th1;
341       nrewrite > (eta_lp…); #th1; nrewrite > th1; //;##]
342     nrewrite > (eta_lp ? e2); 
343     nchange in match (𝐋\p 〈\fst e2,?〉) with (𝐋\p e2'∪ ϵ b2');
344     nrewrite > (cup_dotD …); nrewrite > (epsilon_dot…);       
345     nrewrite > (cupC ? (𝐋\p e2')…); nrewrite > (cupA…);nrewrite > (cupA…);
346     nrewrite < (erase_bull S e2') in ⊢ (???(??%?)); //;
347 ##| ncases e2; #e2' b2'; nchange in match (〈e1',false〉⊙?) with 〈?,?〉;
348     nchange in match (𝐋\p ?) with (?∪?);
349     nchange in match (𝐋\p (e1'·?)) with (?∪?);
350     nchange in match (𝐋\p 〈e1',?〉) with (?∪?);
351     nrewrite > (cup0…); 
352     nrewrite > (cupA…); //;##]
353 nqed.
354
355 (* theorem 16: 1 *)
356 alias symbol "pc" (instance 13) = "cat lang".
357 alias symbol "in_pl" (instance 23) = "in_pl".
358 alias symbol "in_pl" (instance 5) = "in_pl".
359 alias symbol "eclose" (instance 21) = "eclose".
360 ntheorem bull_cup : ∀S:Alpha. ∀e:pitem S.  𝐋\p (•e) =  𝐋\p e ∪ 𝐋 .|e|.
361 #S e; nelim e; //;
362   ##[ #a; napply extP; #w; nnormalize; @; *; /3/ by or_introl, or_intror;
363   ##| #a; napply extP; #w; nnormalize; @; *; /3/ by or_introl; *;
364   ##| #e1 e2 IH1 IH2;  
365       nchange in ⊢ (??(??(%))?) with (•e1 ⊙ 〈e2,false〉);
366       nrewrite > (odot_dot_aux S (•e1) 〈e2,false〉 IH2);
367       nrewrite > (IH1 …); nrewrite > (cup_dotD …);
368       nrewrite > (cupA …); nrewrite > (cupC ?? (𝐋\p ?) …);
369       nchange in match (𝐋\p 〈?,?〉) with (𝐋\p e2 ∪ {}); nrewrite > (cup0 …);
370       nrewrite < (erase_dot …); nrewrite < (cupA …); //;
371   ##| #e1 e2 IH1 IH2;
372       nchange in match (•(?+?)) with (•e1 ⊕ •e2); nrewrite > (oplus_cup …);
373       nrewrite > (IH1 …); nrewrite > (IH2 …); nrewrite > (cupA …);
374       nrewrite > (cupC ? (𝐋\p e2)…);nrewrite < (cupA ??? (𝐋\p e2)…);
375       nrewrite > (cupC ?? (𝐋\p e2)…); nrewrite < (cupA …); 
376       nrewrite < (erase_plus …); //.
377   ##| #e; nletin e' ≝ (\fst (•e)); nletin b' ≝ (\snd (•e)); #IH;
378       nchange in match (𝐋\p ?) with  (𝐋\p 〈e'^*,true〉);
379       nchange in match (𝐋\p ?) with (𝐋\p (e'^* ) ∪ {[ ]});
380       nchange in ⊢ (??(??%?)?) with (𝐋\p e' · 𝐋 .|e'|^* );
381       nrewrite > (erase_bull…e);
382       nrewrite > (erase_star …);
383       nrewrite > (?: 𝐋\p e' =  𝐋\p e ∪ (𝐋 .|e| - ϵ b')); ##[##2:
384         nchange in IH : (??%?) with (𝐋\p e' ∪ ϵ b'); ncases b' in IH; 
385         ##[ #IH; nrewrite > (cup_sub…); //; nrewrite < IH; 
386             nrewrite < (cup_sub…); //; nrewrite > (subK…); nrewrite > (cup0…);//;
387         ##| nrewrite > (sub0 …); #IH; nrewrite < IH; nrewrite > (cup0 …);//; ##]##]
388       nrewrite > (cup_dotD…); nrewrite > (cupA…); 
389       nrewrite > (?: ((?·?)∪{[]} = 𝐋 .|e^*|)); //;
390       nchange in ⊢ (???%) with ((𝐋. |e|)^* ); napply extP; #w; @;
391       ##[ *; ##[##2: nnormalize; #defw; nrewrite < defw; @[]; @; //]
392           *; #w1; *; #w2; *; *; #defw sube; *; #lw; *; #flx cj;
393           @ (w1 :: lw); nrewrite < defw; nrewrite < flx; @; //;
394           @; //; napply (subW … sube);
395       ##| *; #wl; *; #defw Pwl; nrewrite < defw; nelim wl in Pwl;
396          ##[ #_; @2; //;
397          ##| #w' wl' IH; *; #Pw' IHp; nlapply (IH IHp); *;
398             ##[ *; #w1; *; #w2; *; *; #defwl' H1 H2;
399                 @; ncases b' in H1; #H1; 
400                 ##[##2: nrewrite > (sub0…); @w'; @(w1@w2);
401                         nrewrite > (associative_append ? w' w1 w2);
402                         nrewrite > defwl'; @; ##[@;//] @(wl'); @; //;
403                 ##| ncases w' in Pw';
404                     ##[ #ne; @w1; @w2; nrewrite > defwl'; @; //; @; //;
405                     ##| #x xs Px; @(x::xs); @(w1@w2); 
406                         nrewrite > (defwl'); @; ##[@; //; @; //; @; nnormalize; #; ndestruct]
407                         @wl'; @; //; ##] ##]
408             ##| #wlnil; nchange in match (flatten ? (w'::wl')) with (w' @ flatten ? wl');
409                 nrewrite < (wlnil); nrewrite > (append_nil…); ncases b';
410                 ##[ ncases w' in Pw'; /2/; #x xs Pxs; @; @(x::xs); @([]);
411                     nrewrite > (append_nil…); @; ##[ @; //;@; //; nnormalize; @; #; ndestruct]
412                     @[]; @; //;
413                 ##| @; @w'; @[]; nrewrite > (append_nil…); @; ##[##2: @[]; @; //] 
414                     @; //; @; //; @; *;##]##]##]##]##]
415 nqed.
416
417 (* theorem 16: 3 *)      
418 nlemma odot_dot: 
419   ∀S.∀e1,e2: pre S.  𝐋\p (e1 ⊙ e2) =  𝐋\p e1 · 𝐋 .|\fst e2| ∪ 𝐋\p e2.
420 #S e1 e2; napply odot_dot_aux; napply (bull_cup S (\fst e2)); nqed.
421
422 nlemma dot_star_epsilon : ∀S.∀e:re S.𝐋 e · 𝐋 e^* ∪ {[]} =  𝐋 e^*.
423 #S e; napply extP; #w; nnormalize; @;
424 ##[ *; ##[##2: #H; nrewrite < H; @[]; /3/] *; #w1; *; #w2; 
425     *; *; #defw Hw1; *; #wl; *; #defw2 Hwl; @(w1 :: wl);
426     nrewrite < defw; nrewrite < defw2; @; //; @;//;
427 ##| *; #wl; *; #defw Hwl; ncases wl in defw Hwl; ##[#defw; #; @2; nrewrite < defw; //]
428     #x xs defw; *; #Hx Hxs; @; @x; @(flatten ? xs); nrewrite < defw;
429     @; /2/; @xs; /2/;##]
430  nqed.
431
432 nlemma nil_star : ∀S.∀e:re S. [ ] ∈ e^*.
433 #S e; @[]; /2/; nqed.
434
435 nlemma cupID : ∀S.∀l:word S → Prop.l ∪ l = l.
436 #S l; napply extP; #w; @; ##[*]//; #; @; //; nqed.
437
438 nlemma cup_star_nil : ∀S.∀l:word S → Prop. l^* ∪ {[]} = l^*.
439 #S a; napply extP; #w; @; ##[*; //; #H; nrewrite < H; @[]; @; //] #;@; //;nqed.
440
441 naxiom Admit : False.
442
443 nlemma key_id : ∀S.∀e:pitem S. 𝐋\p e · 𝐋 .|e|^* ∪ {[]} = 𝐋\p e · 𝐋 .|e|^* ∪ 𝐋 .|e|^*.
444 #S e; napply extP; #w; @;##[##2:*]
445 ##[*; #w1; *; #w2; *; *; #defw Hw1 Hw2;@; @w1; @w2; /3/;
446 ##|*; #wl; *; #H; nrewrite < H;
447 (*
448    ngeneralize in match e;
449    nelim wl;##[#e;#_;@2;//] #x xs IH e;*; #Hx Hxs; ncases (IH Hxs); 
450    ##[##2: #H; nnormalize; nrewrite < H; nrewrite > (append_nil…);
451
452    ncases wl; ##[#_;@2; //] #x xs; *; #Hx Hxs; @; @x; @(flatten ? xs); @;
453    ##[@;//;##|@xs; @; //]
454    ngeneralize in match Hx; ngeneralize in match x; nelim e; nnormalize; //;
455    ##[#e1 e2 IH1 IH2 x; *; #w1; *; #w2; *; *; #defx Hw1 Hw2;
456       @; @w1; @w2; /4/ by conj;
457    ##|#e1 e2 IH1 IH2 y;*; #; ##[@|@2] /2/;
458    ##|#e IH y; *; #wl; *; #delwl Hw2; nrewrite < delwl;
459       nelim wl in Hw2; ##[#_;@[];@[];@;//; 
460 *)
461    ncases Admit;
462 ##|*;##[##2: #H; nrewrite < H; @2; //] *; #w1; *; #w2; *; *; #defw Hw1 Hw2;
463    @; @w1; @w2; /3/;##]
464 nqed.
465
466 (* theorem 16: 4 *)      
467 nlemma star_dot: ∀S.∀e:pre S. 𝐋\p (e^⊛) = 𝐋\p e · (𝐋 .|\fst e|)^*.
468 #S e; ncases e; #e' b'; ncases b';
469 ##[ nchange in match (〈e',true〉^⊛) with 〈?,?〉;
470     nletin e'' ≝ (\fst (•e'));
471     nchange in ⊢ (??%?) with (?∪?);
472     nchange in ⊢ (??(??%?)?) with (𝐋\p e'' · 𝐋 .|e''|^* );
473     nrewrite > (?:  𝐋\p e'' ·  𝐋.|e''|^* ∪ {[]} =  (𝐋\p e' ∪ 𝐋.|e'|) · 𝐋.|e''|^* ∪ {[]}); ##[##2:
474       nrewrite < (bull_cup…); nchange in ⊢ (???(??(??%?)?)) with (?∪?);
475       nchange in match e'' with e'';
476       ncases (\snd (•e')); ##[##2: nrewrite > (cup0…); //]
477       nrewrite > (cup_dotD…); nrewrite > (epsilon_dot…);
478       nrewrite > (cupA…); nrewrite > (cup_star_nil…);
479       napply key_id;##]
480     nrewrite > (cup_dotD…); nrewrite > (cupA…);
481     nrewrite > (?: ?·? ∪ {[]} = 𝐋.|e'|^* ); ##[##2:
482       nrewrite > (erase_bull…); nrewrite > (dot_star_epsilon…); //]
483     nrewrite > (erase_bull…);  
484     nchange in match (𝐋\p 〈?,?〉) with (?∪?);
485     nrewrite > (cup_dotD…);nrewrite > (epsilon_dot…); //;
486 ##| nwhd in match (〈e',false〉^⊛); nchange in match (𝐋\p 〈?,?〉) with (?∪?);
487     nrewrite > (cup0…);
488     nchange in ⊢ (??%?) with (𝐋\p e' · 𝐋 .|e'|^* );
489     nrewrite < (cup0 ? (𝐋\p e')); //;##]
490 nqed.
491
492 STOP
493
494 notation > "\move term 90 x term 90 E" 
495 non associative with precedence 60 for @{move ? $x $E}.
496 nlet rec move (S: Alpha) (x:S) (E: pitem S) on E : pre S ≝
497  match E with
498   [ pz ⇒ 〈 ∅, false 〉
499   | pe ⇒ 〈 ϵ, false 〉
500   | ps y ⇒ 〈 `y, false 〉
501   | pp y ⇒ 〈 `y, x == y 〉
502   | po e1 e2 ⇒ \move x e1 ⊕ \move x e2 
503   | pc e1 e2 ⇒ \move x e1 ⊙ \move x e2
504   | pk e ⇒ (\move x e)^⊛ ].
505 notation < "\move\shy x\shy E" non associative with precedence 60 for @{'move $x $E}.
506 notation > "\move term 90 x term 90 E" non associative with precedence 60 for @{'move $x $E}.
507 interpretation "move" 'move x E = (move ? x E).
508
509 ndefinition rmove ≝ λS:Alpha.λx:S.λe:pre S. \move x (\fst e).
510 interpretation "rmove" 'move x E = (rmove ? x E).
511
512 nlemma XXz :  ∀S:Alpha.∀w:word S. w .∈ ∅ → False.
513 #S w abs; ninversion abs; #; ndestruct;
514 nqed.
515
516
517 nlemma XXe :  ∀S:Alpha.∀w:word S. w .∈ ϵ → False.
518 #S w abs; ninversion abs; #; ndestruct;
519 nqed.
520
521 nlemma XXze :  ∀S:Alpha.∀w:word S. w .∈ (∅ · ϵ)  → False.
522 #S w abs; ninversion abs; #; ndestruct; /2/ by XXz,XXe;
523 nqed.
524
525
526 naxiom in_move_cat:
527  ∀S.∀w:word S.∀x.∀E1,E2:pitem S. w .∈ \move x (E1 · E2) → 
528    (∃w1.∃w2. w = w1@w2 ∧ w1 .∈ \move x E1 ∧ w2 ∈ .|E2|) ∨ w .∈ \move x E2.
529 #S w x e1 e2 H; nchange in H with (w .∈ \move x e1 ⊙ \move x e2);
530 ncases e1 in H; ncases e2;
531 ##[##1: *; ##[*; nnormalize; #; ndestruct] 
532    #H; ninversion H; ##[##1,4,5,6: nnormalize; #; ndestruct]
533    nnormalize; #; ndestruct; ncases (?:False); /2/ by XXz,XXze;
534 ##|##2: *; ##[*; nnormalize; #; ndestruct] 
535    #H; ninversion H; ##[##1,4,5,6: nnormalize; #; ndestruct]
536    nnormalize; #; ndestruct; ncases (?:False); /2/ by XXz,XXze;
537 ##| #r; *; ##[ *; nnormalize; #; ndestruct] 
538    #H; ninversion H; ##[##1,4,5,6: nnormalize; #; ndestruct]
539    ##[##2: nnormalize; #; ndestruct; @2; @2; //.##]
540    nnormalize; #; ndestruct; ncases (?:False); /2/ by XXz;
541 ##| #y; *; ##[ *; nnormalize; #defw defx; ndestruct; @2; @1; /2/ by conj;##]
542    #H; ninversion H; nnormalize; #; ndestruct; 
543    ##[ncases (?:False); /2/ by XXz] /3/ by or_intror;
544 ##| #r1 r2; *; ##[ *; #defw]
545     ...
546 nqed.
547
548 ntheorem move_ok:
549  ∀S:Alpha.∀E:pre S.∀a,w.w .∈ \move a E ↔ (a :: w) .∈ E. 
550 #S E; ncases E; #r b; nelim r;
551 ##[##1,2: #a w; @; 
552    ##[##1,3: nnormalize; *; ##[##1,3: *; #; ndestruct; ##| #abs; ncases (XXz … abs); ##]
553       #H; ninversion H; #; ndestruct;
554    ##|##*:nnormalize; *; ##[##1,3: *; #; ndestruct; ##| #H1; ncases (XXz … H1); ##]
555        #H; ninversion H; #; ndestruct;##]
556 ##|#a c w; @; nnormalize; ##[*; ##[*; #; ndestruct; ##] #abs; ninversion abs; #; ndestruct;##]
557    *; ##[##2: #abs; ninversion abs; #; ndestruct; ##] *; #; ndestruct;
558 ##|#a c w; @; nnormalize; 
559    ##[ *; ##[ *; #defw; nrewrite > defw; #ca; @2;  nrewrite > (eqb_t … ca); @; ##]
560        #H; ninversion H; #; ndestruct;
561    ##| *; ##[ *; #; ndestruct; ##] #H; ninversion H; ##[##2,3,4,5,6: #; ndestruct]
562               #d defw defa; ndestruct; @1; @; //; nrewrite > (eqb_true S d d); //. ##]
563 ##|#r1 r2 H1 H2 a w; @;
564    ##[ #H; ncases (in_move_cat … H);
565       ##[ *; #w1; *; #w2; *; *; #defw w1m w2m;
566           ncases (H1 a w1); #H1w1; #_; nlapply (H1w1 w1m); #good; 
567           nrewrite > defw; @2; @2 (a::w1); //; ncases good; ##[ *; #; ndestruct] //.
568       ##|
569       ...
570 ##|
571 ##|
572 ##]
573 nqed.
574
575
576 notation > "x ↦* E" non associative with precedence 60 for @{move_star ? $x $E}.
577 nlet rec move_star (S : decidable) w E on w : bool × (pre S) ≝
578  match w with
579   [ nil ⇒ E
580   | cons x w' ⇒ w' ↦* (x ↦ \snd E)].
581
582 ndefinition in_moves ≝ λS:decidable.λw.λE:bool × (pre S). \fst(w ↦* E).
583
584 ncoinductive equiv (S:decidable) : bool × (pre S) → bool × (pre S) → Prop ≝
585  mk_equiv:
586   ∀E1,E2: bool × (pre S).
587    \fst E1  = \fst E2 →
588     (∀x. equiv S (x ↦ \snd E1) (x ↦ \snd E2)) →
589      equiv S E1 E2.
590
591 ndefinition NAT: decidable.
592  @ nat eqb; /2/.
593 nqed.
594
595 include "hints_declaration.ma".
596
597 alias symbol "hint_decl" (instance 1) = "hint_decl_Type1".
598 unification hint 0 ≔ ; X ≟ NAT ⊢ carr X ≡ nat.
599
600 ninductive unit: Type[0] ≝ I: unit.
601
602 nlet corec foo_nop (b: bool):
603  equiv ?
604   〈 b, pc ? (ps ? 0) (pk ? (pc ? (ps ? 1) (ps ? 0))) 〉
605   〈 b, pc ? (pk ? (pc ? (ps ? 0) (ps ? 1))) (ps ? 0) 〉 ≝ ?.
606  @; //; #x; ncases x
607   [ nnormalize in ⊢ (??%%); napply (foo_nop false)
608   | #y; ncases y
609      [ nnormalize in ⊢ (??%%); napply (foo_nop false)
610      | #w; nnormalize in ⊢ (??%%); napply (foo_nop false) ]##]
611 nqed.
612
613 (*
614 nlet corec foo (a: unit):
615  equiv NAT
616   (eclose NAT (pc ? (ps ? 0) (pk ? (pc ? (ps ? 1) (ps ? 0)))))
617   (eclose NAT (pc ? (pk ? (pc ? (ps ? 0) (ps ? 1))) (ps ? 0)))
618 ≝ ?.
619  @;
620   ##[ nnormalize; //
621   ##| #x; ncases x
622        [ nnormalize in ⊢ (??%%);
623          nnormalize in foo: (? → ??%%);
624          @; //; #y; ncases y
625            [ nnormalize in ⊢ (??%%); napply foo_nop
626            | #y; ncases y
627               [ nnormalize in ⊢ (??%%);
628                 
629             ##| #z; nnormalize in ⊢ (??%%); napply foo_nop ]##]
630      ##| #y; nnormalize in ⊢ (??%%); napply foo_nop
631   ##]
632 nqed.
633 *)
634
635 ndefinition test1 : pre ? ≝ ❨ `0 | `1 ❩^* `0.
636 ndefinition test2 : pre ? ≝ ❨ (`0`1)^* `0 | (`0`1)^* `1 ❩.
637 ndefinition test3 : pre ? ≝ (`0 (`0`1)^* `1)^*.
638
639
640 nlemma foo: in_moves ? [0;0;1;0;1;1] (ɛ test3) = true.
641  nnormalize in match test3;
642  nnormalize;
643 //;
644 nqed.
645
646 (**********************************************************)
647
648 ninductive der (S: Type[0]) (a: S) : re S → re S → CProp[0] ≝
649    der_z: der S a (z S) (z S)
650  | der_e: der S a (e S) (z S)
651  | der_s1: der S a (s S a) (e ?)
652  | der_s2: ∀b. a ≠ b → der S a (s S b) (z S)
653  | der_c1: ∀e1,e2,e1',e2'. in_l S [] e1 → der S a e1 e1' → der S a e2 e2' →
654             der S a (c ? e1 e2) (o ? (c ? e1' e2) e2')
655  | der_c2: ∀e1,e2,e1'. Not (in_l S [] e1) → der S a e1 e1' →
656             der S a (c ? e1 e2) (c ? e1' e2)
657  | der_o: ∀e1,e2,e1',e2'. der S a e1 e1' → der S a e2 e2' →
658     der S a (o ? e1 e2) (o ? e1' e2').
659
660 nlemma eq_rect_CProp0_r:
661  ∀A.∀a,x.∀p:eq ? x a.∀P: ∀x:A. eq ? x a → CProp[0]. P a (refl A a) → P x p.
662  #A; #a; #x; #p; ncases p; #P; #H; nassumption.
663 nqed.
664
665 nlemma append1: ∀A.∀a:A.∀l. [a] @ l = a::l. //. nqed.
666
667 naxiom in_l1: ∀S,r1,r2,w. in_l S [ ] r1 → in_l S w r2 → in_l S w (c S r1 r2).
668 (* #S; #r1; #r2; #w; nelim r1
669   [ #K; ninversion K
670   | #H1; #H2; napply (in_c ? []); //
671   | (* tutti casi assurdi *) *)
672
673 ninductive in_l' (S: Type[0]) : word S → re S → CProp[0] ≝
674    in_l_empty1: ∀E.in_l S [] E → in_l' S [] E 
675  | in_l_cons: ∀a,w,e,e'. in_l' S w e' → der S a e e' → in_l' S (a::w) e.
676
677 ncoinductive eq_re (S: Type[0]) : re S → re S → CProp[0] ≝
678    mk_eq_re: ∀E1,E2.
679     (in_l S [] E1 → in_l S [] E2) →
680     (in_l S [] E2 → in_l S [] E1) →
681     (∀a,E1',E2'. der S a E1 E1' → der S a E2 E2' → eq_re S E1' E2') →
682       eq_re S E1 E2.
683
684 (* serve il lemma dopo? *)
685 ntheorem eq_re_is_eq: ∀S.∀E1,E2. eq_re S E1 E2 → ∀w. in_l ? w E1 → in_l ? w E2.
686  #S; #E1; #E2; #H1; #w; #H2; nelim H2 in E2 H1 ⊢ %
687   [ #r; #K (* ok *)
688   | #a; #w; #R1; #R2; #K1; #K2; #K3; #R3; #K4; @2 R2; //; ncases K4;
689
690 (* IL VICEVERSA NON VALE *)
691 naxiom in_l_to_in_l: ∀S,w,E. in_l' S w E → in_l S w E.
692 (* #S; #w; #E; #H; nelim H
693   [ //
694   | #a; #w'; #r; #r'; #H1; (* e si cade qua sotto! *)
695   ]
696 nqed. *)
697
698 ntheorem der1: ∀S,a,e,e',w. der S a e e' → in_l S w e' → in_l S (a::w) e.
699  #S; #a; #E; #E'; #w; #H; nelim H
700   [##1,2: #H1; ninversion H1
701      [##1,8: #_; #K; (* non va ndestruct K; *) ncases (?:False); (* perche' due goal?*) /2/
702      |##2,9: #X; #Y; #K; ncases (?:False); /2/
703      |##3,10: #x; #y; #z; #w; #a; #b; #c; #d; #e; #K; ncases (?:False); /2/
704      |##4,11: #x; #y; #z; #w; #a; #b; #K; ncases (?:False); /2/
705      |##5,12: #x; #y; #z; #w; #a; #b; #K; ncases (?:False); /2/
706      |##6,13: #x; #y; #K; ncases (?:False); /2/
707      |##7,14: #x; #y; #z; #w; #a; #b; #c; #d; #K; ncases (?:False); /2/]
708 ##| #H1; ninversion H1
709      [ //
710      | #X; #Y; #K; ncases (?:False); /2/
711      | #x; #y; #z; #w; #a; #b; #c; #d; #e; #K; ncases (?:False); /2/
712      | #x; #y; #z; #w; #a; #b; #K; ncases (?:False); /2/
713      | #x; #y; #z; #w; #a; #b; #K; ncases (?:False); /2/
714      | #x; #y; #K; ncases (?:False); /2/
715      | #x; #y; #z; #w; #a; #b; #c; #d; #K; ncases (?:False); /2/ ]
716 ##| #H1; #H2; #H3; ninversion H3
717      [ #_; #K; ncases (?:False); /2/
718      | #X; #Y; #K; ncases (?:False); /2/
719      | #x; #y; #z; #w; #a; #b; #c; #d; #e; #K; ncases (?:False); /2/
720      | #x; #y; #z; #w; #a; #b; #K; ncases (?:False); /2/
721      | #x; #y; #z; #w; #a; #b; #K; ncases (?:False); /2/
722      | #x; #y; #K; ncases (?:False); /2/
723      | #x; #y; #z; #w; #a; #b; #c; #d; #K; ncases (?:False); /2/ ]
724 ##| #r1; #r2; #r1'; #r2'; #H1; #H2; #H3; #H4; #H5; #H6;
725