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