]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/nlibrary/re/re-setoids.ma
Setoid-Rewriting under Ex works for an arbitrary depth of Ex. Patches needed:
[helm.git] / helm / software / matita / nlibrary / re / re-setoids.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/pairs.ma".
16 include "datatypes/bool.ma".
17 include "sets/sets.ma".
18
19 (*
20 ninductive Admit : CProp[0] ≝ .
21 naxiom admit : Admit.
22 *)
23
24 (* single = is for the abstract equality of setoids, == is for concrete 
25    equalities (that may be lifted to the setoid level when needed *)
26 notation < "hvbox(a break mpadded width -50% (=)= b)" non associative with precedence 45 for @{ 'eq_low $a $b }.
27 notation > "a == b" non associative with precedence 45 for @{ 'eq_low $a $b }.
28
29
30 (* XXX move to lists.ma *)
31 ninductive list (A:Type[0]) : Type[0] ≝ 
32   | nil: list A
33   | cons: A -> list A -> list A.
34   
35 nlet rec eq_list (A : setoid) (l1, l2 : list A) on l1 : CProp[0] ≝ 
36 match l1 with
37 [ nil ⇒ match l2 return λ_.CProp[0] with [ nil ⇒ True | _ ⇒ False ]
38 | cons x xs ⇒ match l2 with [ nil ⇒ False | cons y ys ⇒ x = y ∧ eq_list ? xs ys]].
39    
40 interpretation "eq_list" 'eq_low a b = (eq_list ? a b).
41    
42 ndefinition LIST : setoid → setoid.
43 #S; @(list S); @(eq_list S);
44 ##[ #l; nelim l; //; #; @; //;
45 ##| #l1; nelim l1; ##[ #y; ncases y; //] #x xs H y; ncases y; ##[*] #y ys; *; #; @; /2/;
46 ##| #l1; nelim l1; ##[ #l2 l3; ncases l2; ncases l3; /3/; #z zs y ys; *] 
47     #x xs H l2 l3; ncases l2; ncases l3; /2/; #z zs y yz; *; #H1 H2; *; #H3 H4; @; /3/;##]
48 nqed.
49
50 alias symbol "hint_decl" (instance 1) = "hint_decl_Type1".
51 unification hint 0 ≔ S : setoid;
52   T ≟ carr S,
53   P1 ≟ refl ? (eq0 (LIST S)),
54   P2 ≟ sym ? (eq0 (LIST S)),
55   P3 ≟ trans ? (eq0 (LIST S)),
56   X ≟ mk_setoid (list S) (mk_equivalence_relation ? (eq_list T) P1 P2 P3)
57 (*-----------------------------------------------------------------------*) ⊢
58      carr X ≡ list T.
59
60 unification hint 0 ≔ S:setoid,a,b:list S;
61    R ≟ eq0 (LIST S),
62    L ≟ (list S)
63 (* -------------------------------------------- *) ⊢
64    eq_list S a b ≡ eq_rel L R a b.
65
66 notation "hvbox(hd break :: tl)"
67   right associative with precedence 47
68   for @{'cons $hd $tl}.
69
70 notation "[ list0 x sep ; ]"
71   non associative with precedence 90
72   for ${fold right @'nil rec acc @{'cons $x $acc}}.
73
74 notation "hvbox(l1 break @ l2)"
75   right associative with precedence 47
76   for @{'append $l1 $l2 }.
77
78 interpretation "nil" 'nil = (nil ?).
79 interpretation "cons" 'cons hd tl = (cons ? hd tl).
80
81 nlet rec append A (l1: list A) l2 on l1 ≝ 
82   match l1 with
83   [ nil ⇒ l2
84   | cons hd tl ⇒ hd :: append A tl l2 ].
85
86 interpretation "append" 'append l1 l2 = (append ? l1 l2).
87
88 ntheorem append_nil: ∀A:setoid.∀l:list A.l @ [] = l.
89 #A;#l;nelim l;//; #a;#l1;#IH;nnormalize;/2/;nqed.
90
91 ndefinition associative ≝ λA:setoid.λf:A → A → A.∀x,y,z.f x (f y z) = f (f x y) z. 
92
93 ntheorem associative_append: ∀A:setoid.associative (list A) (append A).
94 #A;#x;#y;#z;nelim x[ napply (refl ???) |#a;#x1;#H;nnormalize;/2/]nqed.
95
96 nlet rec flatten S (l : list (list S)) on l : list S ≝ 
97 match l with [ nil ⇒ [ ] | cons w tl ⇒ w @ flatten ? tl ].
98
99 (* end move to list *)
100
101 interpretation "iff" 'iff a b = (iff a b).  
102
103 ninductive eq (A:Type[0]) (x:A) : A → CProp[0] ≝ refl: eq A x x.
104
105 nlemma eq_rect_Type0_r':
106  ∀A.∀a,x.∀p:eq ? x a.∀P: ∀x:A. eq ? x a → Type[0]. P a (refl A a) → P x p.
107  #A; #a; #x; #p; ncases p; #P; #H; nassumption.
108 nqed.
109
110 nlemma eq_rect_Type0_r:
111  ∀A.∀a.∀P: ∀x:A. eq ? x a → Type[0]. P a (refl A a) → ∀x.∀p:eq ? x a.P x p.
112  #A; #a; #P; #p; #x0; #p0; napply (eq_rect_Type0_r' ??? p0); nassumption.
113 nqed.
114
115 nlemma eq_rect_CProp0_r':
116  ∀A.∀a,x.∀p:eq ? x a.∀P: ∀x:A. eq ? x a → CProp[0]. P a (refl A a) → P x p.
117  #A; #a; #x; #p; ncases p; #P; #H; nassumption.
118 nqed.
119
120 nlemma eq_rect_CProp0_r:
121  ∀A.∀a.∀P: ∀x:A. eq ? x a → CProp[0]. P a (refl A a) → ∀x.∀p:eq ? x a.P x p.
122  #A; #a; #P; #p; #x0; #p0; napply (eq_rect_CProp0_r' ??? p0); nassumption.
123 nqed.
124
125 (* XXX move to bool *)
126 interpretation "bool eq" 'eq_low a b = (eq bool a b). 
127
128 ndefinition BOOL : setoid.
129 @bool; @(eq bool); nnormalize; //; #x y; ##[ #E; ncases E; ##| #y H; ncases H; ##] //; nqed.
130
131 alias symbol "hint_decl" (instance 1) = "hint_decl_Type1".
132 alias id "refl" = "cic:/matita/ng/properties/relations/refl.fix(0,1,3)".
133 unification hint 0 ≔ ;
134   P1 ≟ refl ? (eq0 BOOL),
135   P2 ≟ sym ? (eq0 BOOL),
136   P3 ≟ trans ? (eq0 BOOL),
137   X ≟ mk_setoid bool (mk_equivalence_relation ? (eq bool) P1 P2 P3)
138 (*-----------------------------------------------------------------------*) ⊢
139      carr X ≡ bool.
140
141 unification hint 0 ≔ a,b;
142    R ≟ eq0 BOOL,
143    L ≟ bool
144 (* -------------------------------------------- *) ⊢
145    eq bool a b ≡ eq_rel L R a b.
146
147 nrecord Alpha : Type[1] ≝ { 
148    acarr :> setoid;
149    eqb: acarr → acarr → bool; 
150    eqb_true: ∀x,y. (eqb x y = true) = (x = y)
151 }.
152  
153 interpretation "eqb" 'eq_low a b = (eqb ? a b).
154
155 ninductive re (S: Type[0]) : Type[0] ≝
156    z: re S
157  | e: re S
158  | s: S → re S
159  | c: re S → re S → re S
160  | o: re S → re S → re S
161  | k: re S → re S.
162  
163 nlet rec eq_re (S:Alpha) (a,b : re S) on a : CProp[0] ≝ 
164   match a with
165   [ z ⇒ match b with [ z ⇒ True | _ ⇒ False]
166   | e ⇒ match b with [ e ⇒ True | _ ⇒ False]
167   | s x ⇒ match b with [ s y ⇒ x = y | _ ⇒ False]
168   | c r1 r2 ⇒ match b with [ c s1 s2 ⇒ eq_re ? r1 s1 ∧ eq_re ? r2 s2 | _ ⇒ False]
169   | o r1 r2 ⇒ match b with [ o s1 s2  ⇒ eq_re ? r1 s1 ∧ eq_re ? r2 s2 | _ ⇒ False] 
170   | k r1 ⇒ match b with [ k r2 ⇒ eq_re ? r1 r2 | _ ⇒ False]].
171   
172 interpretation "eq_re" 'eq_low a b = (eq_re ? a b).
173
174 ndefinition RE : Alpha → setoid.
175 #A; @(re A); @(eq_re A);
176 ##[ #p; nelim p; /2/;
177 ##| #p1; nelim p1; ##[##1,2: #p2; ncases p2; /2/;
178     ##|##2,3: #x p2; ncases p2; /2/;
179     ##|##4,5: #e1 e2 H1 H2 p2; ncases p2; /3/; #e3 e4; *; #; @; /2/;
180     ##|#r H p2; ncases p2; /2/;##]
181 ##| #p1; nelim p1;
182     ##[ ##1,2: #y z; ncases y; ncases z; //; nnormalize; #; ncases (?:False); //;
183     ##| ##3: #a; #y z; ncases y; ncases z; /2/; nnormalize; #; ncases (?:False); //;
184     ##| ##4,5: #r1 r2 H1 H2 y z; ncases y; ncases z; //; nnormalize;
185         ##[##1,3,4,5,6,8: #; ncases (?:False); //;##]
186         #r1 r2 r3 r4; nnormalize; *; #H1 H2; *; #H3 H4; /3/;
187     ##| #r H y z; ncases y; ncases z; //; nnormalize; ##[##1,2,3: #; ncases (?:False); //]
188         #r2 r3; /3/; ##]##]
189 nqed.
190
191 alias symbol "hint_decl" (instance 1) = "hint_decl_Type1".
192 alias id "carr" = "cic:/matita/ng/sets/setoids/carr.fix(0,0,1)".
193 unification hint 0 ≔ A : Alpha;
194   S ≟ acarr A,
195   T ≟ carr S,
196   P1 ≟ refl ? (eq0 (RE A)),
197   P2 ≟ sym ? (eq0 (RE A)),
198   P3 ≟ trans ? (eq0 (RE A)),
199   X ≟ mk_setoid (re T) (mk_equivalence_relation ? (eq_re A) P1 P2 P3)
200 (*-----------------------------------------------------------------------*) ⊢
201      carr X ≡ re T.
202
203 unification hint 0 ≔ A:Alpha,a,b:re A;
204    R ≟ eq0 (RE A),
205    L ≟ re A
206 (* -------------------------------------------- *) ⊢
207    eq_re A a b ≡ eq_rel L R a b.
208
209 notation < "a \sup ⋇" non associative with precedence 90 for @{ 'pk $a}.
210 notation > "a ^ *" non associative with precedence 75 for @{ 'pk $a}.
211 interpretation "star" 'pk a = (k ? a).
212 interpretation "or" 'plus a b = (o ? a b).
213            
214 notation "a · b" non associative with precedence 60 for @{ 'pc $a $b}.
215 interpretation "cat" 'pc a b = (c ? a b).
216
217 (* to get rid of \middot *)
218 ncoercion c  : ∀S.∀p:re S.  re S →  re S   ≝ c  on _p : re ?  to ∀_:?.?.
219
220 notation < "a" non associative with precedence 90 for @{ 'ps $a}.
221 notation > "` term 90 a" non associative with precedence 90 for @{ 'ps $a}.
222 interpretation "atom" 'ps a = (s ? a).
223
224 notation "ϵ" non associative with precedence 90 for @{ 'epsilon }.
225 interpretation "epsilon" 'epsilon = (e ?).
226
227 notation "0" non associative with precedence 90 for @{ 'empty_r }.
228 interpretation "empty" 'empty_r = (z ?).
229
230 notation > "'lang' S" non associative with precedence 90 for @{ Ω^(list $S) }.
231 notation > "'Elang' S" non associative with precedence 90 for @{ 𝛀^(list $S) }.
232
233 nlet rec conjunct S (l : list (list S)) (L : lang S) on l: CProp[0] ≝
234 match l with [ nil ⇒ True | cons w tl ⇒ w ∈ L ∧ conjunct ? tl L ].
235
236 (*
237 ndefinition sing_lang : ∀A:setoid.∀x:A.Ω^A ≝ λS.λx.{ w | x = w }.
238 interpretation "sing lang" 'singl x = (sing_lang ? x).
239 *)
240
241 interpretation "subset construction with type" 'comprehension t \eta.x = 
242   (mk_powerclass t x).
243
244 ndefinition cat : ∀A:setoid.∀l1,l2:lang A.lang A ≝ 
245   λS.λl1,l2.{ w ∈ list S | ∃w1,w2.w =_0 w1 @ w2 ∧ w1 ∈ l1 ∧ w2 ∈ l2}.
246 interpretation "cat lang" 'pc a b = (cat ? a b).
247
248 ndefinition star : ∀A:setoid.∀l:lang A.lang A ≝ 
249   λS.λl.{ w ∈ list S | ∃lw.flatten ? lw = w ∧ conjunct ? lw l}. 
250 interpretation "star lang" 'pk l = (star ? l).
251
252 notation > "𝐋 term 70 E" non associative with precedence 75 for @{L_re ? $E}.
253 nlet rec L_re (S : Alpha) (r : re S) on r : lang S ≝ 
254 match r with
255 [ z ⇒ ∅
256 | e ⇒ { [ ] }
257 | s x ⇒ { [x] }
258 | c r1 r2 ⇒ 𝐋 r1 · 𝐋 r2
259 | o r1 r2 ⇒  𝐋 r1 ∪ 𝐋 r2
260 | k r1 ⇒ (𝐋 r1) ^*].
261 notation "𝐋 term 70 E" non associative with precedence 75 for @{'L_re $E}.
262 interpretation "in_l" 'L_re E = (L_re ? E).
263
264 notation "a || b" left associative with precedence 30 for @{'orb $a $b}.
265 ndefinition orb ≝ λa,b:bool. match a with [ true ⇒ true | _ ⇒ b ].
266 interpretation "orb" 'orb a b = (orb a b).
267
268 ninductive pitem (S: Type[0]) : Type[0] ≝
269    pz: pitem S
270  | pe: pitem S
271  | ps: S → pitem S
272  | pp: S → pitem S
273  | pc: pitem S → pitem S → pitem S
274  | po: pitem S → pitem S → pitem S
275  | pk: pitem S → pitem S.
276  
277 nlet rec eq_pitem  (S : Alpha) (p1, p2 : pitem S) on p1 : CProp[0] ≝ 
278  match p1 with
279  [ pz ⇒ match p2 with [ pz ⇒ True | _ ⇒ False]
280  | pe ⇒ match p2 with [ pe ⇒ True | _ ⇒ False]
281  | ps x ⇒ match p2 with [ ps y ⇒ x = y | _ ⇒ False]
282  | pp x ⇒ match p2 with [ pp y ⇒ x = y | _ ⇒ False]
283  | pc a1 a2 ⇒ match p2 with [ pc b1 b2 ⇒ eq_pitem ? a1 b1 ∧ eq_pitem ? a2 b2| _ ⇒ False]
284  | po a1 a2 ⇒ match p2 with [ po b1 b2 ⇒ eq_pitem ? a1 b1 ∧ eq_pitem ? a2 b2| _ ⇒ False]
285  | pk a ⇒ match p2 with [ pk b ⇒ eq_pitem ? a b | _ ⇒ False]].
286  
287 interpretation "eq_pitem" 'eq_low a b = (eq_pitem ? a b). 
288  
289 nlemma PITEM : ∀S:Alpha.setoid.
290 #S; @(pitem S); @(eq_pitem …);
291 ##[ #p; nelim p; //; nnormalize; #; @; //;
292 ##| #p; nelim p; ##[##1,2: #y; ncases y; //; ##|##3,4: #x y; ncases y; //; #; napply (?^-1); nassumption;
293     ##|##5,6: #r1 r2 H1 H2 p2; ncases p2; //; #s1 s2; nnormalize; *; #; @; /2/;
294     ##| #r H y; ncases y; //; nnormalize; /2/;##]
295 ##| #x; nelim x; 
296     ##[ ##1,2: #y z; ncases y; ncases z; //; nnormalize; #; ncases (?:False); //;
297     ##| ##3,4: #a; #y z; ncases y; ncases z; /2/; nnormalize; #; ncases (?:False); //;
298     ##| ##5,6: #r1 r2 H1 H2 y z; ncases y; ncases z; //; nnormalize;
299         ##[##1,2,5,6,7,8,4,10: #; ncases (?:False); //;##]
300         #r1 r2 r3 r4; nnormalize; *; #H1 H2; *; #H3 H4; /3/;
301     ##| #r H y z; ncases y; ncases z; //; nnormalize; ##[##1,2,3,4: #; ncases (?:False); //]
302         #r2 r3; /3/; ##]##]
303 nqed.
304
305 alias symbol "hint_decl" (instance 1) = "hint_decl_Type1".
306 unification hint 0 ≔ SS:Alpha;
307     S ≟ acarr SS,
308     A ≟ carr S,
309     P1 ≟ refl ? (eq0 (PITEM SS)),
310     P2 ≟ sym ? (eq0 (PITEM SS)),
311     P3 ≟ trans ? (eq0 (PITEM SS)),
312     R ≟ mk_setoid (pitem S) (mk_equivalence_relation (pitem A) (eq_pitem SS) P1 P2 P3)
313 (*---------------------------*)⊢
314     carr R ≡ pitem A.
315     
316 unification hint 0 ≔ S:Alpha,a,b:pitem S;
317    R ≟ PITEM S,
318    L ≟ (pitem S)
319 (* -------------------------------------------- *) ⊢
320    eq_pitem S a b ≡ eq_rel L (eq0 R) a b.    
321     
322 (* XXX move to pair.ma *)   
323 nlet rec eq_pair (A, B : setoid) (a : A × B) (b : A × B) on a : CProp[0] ≝ 
324   match a with [ mk_pair a1 a2 ⇒ 
325   match b with [ mk_pair b1 b2 ⇒ a1 = b1 ∧ a2 = b2 ]].
326
327 interpretation "eq_pair" 'eq_low a b = (eq_pair ?? a b). 
328
329 nlemma PAIR : ∀A,B:setoid. setoid.
330 #A B; @(A × B); @(eq_pair …);
331 ##[ #ab; ncases ab; #a b; @; napply #;
332 ##| #ab cd; ncases ab; ncases cd; #a1 a2 b1 b2; *; #E1 E2;
333     @; napply (?^-1); //;
334 ##| #a b c; ncases a; ncases b; ncases c; #c1 c2 b1 b2 a1 a2;
335     *; #E1 E2; *; #E3 E4; @; ##[ napply (.= E1); //] napply (.= E2); //.##]
336 nqed.
337
338 alias symbol "hint_decl" (instance 1) = "hint_decl_Type1".
339 unification hint 0 ≔ AA, BB;
340     A ≟ carr AA, B ≟ carr BB,
341     P1 ≟ refl ? (eq0 (PAIR AA BB)),
342     P2 ≟ sym ? (eq0 (PAIR AA BB)),
343     P3 ≟ trans ? (eq0 (PAIR AA BB)),
344     R ≟ mk_setoid (A × B) (mk_equivalence_relation ? (eq_pair …) P1 P2 P3)
345 (*---------------------------------------------------------------------------*)⊢
346     carr R ≡ A × B.
347  
348 unification hint 0 ≔ S1,S2,a,b;
349    R ≟ PAIR S1 S2,
350    L ≟ (pair S1 S2)
351 (* -------------------------------------------- *) ⊢
352    eq_pair S1 S2 a b ≡ eq_rel L (eq0 R) a b.    
353
354 (* end move to pair *) 
355  
356 ndefinition pre ≝ λS.pitem S × bool.
357
358 notation "\fst term 90 x" non associative with precedence 90 for @{'fst $x}.
359 interpretation "fst" 'fst x = (fst ? ? x).
360 notation > "\snd term 90 x" non associative with precedence 90 for @{'snd $x}.
361 interpretation "snd" 'snd x = (snd ? ? x).
362
363 interpretation "pstar" 'pk a = (pk ? a).
364 interpretation "por" 'plus a b = (po ? a b).
365 interpretation "pcat" 'pc a b = (pc ? a b).
366 notation < ".a" non associative with precedence 90 for @{ 'pp $a}.
367 notation > "`. term 90 a" non associative with precedence 90 for @{ 'pp $a}.
368 interpretation "ppatom" 'pp a = (pp ? a).
369 (* to get rid of \middot *)
370 ncoercion pc : ∀S.∀p:pitem S. pitem S → pitem S  ≝ pc on _p : pitem ? to ∀_:?.?.
371 interpretation "patom" 'ps a = (ps ? a).
372 interpretation "pepsilon" 'epsilon = (pe ?).
373 interpretation "pempty" 'empty_r = (pz ?).
374
375 notation > "|term 19 e|" non associative with precedence 70 for @{forget ? $e}.
376 nlet rec forget (S: Alpha) (l : pitem S) on l: re S ≝
377  match l with
378   [ pz ⇒ 0
379   | pe ⇒ ϵ
380   | ps x ⇒ `x
381   | pp x ⇒ `x
382   | pc E1 E2 ⇒ (|E1| · |E2|)
383   | po E1 E2 ⇒ (|E1| + |E2|)
384   | pk E ⇒ |E|^* ].
385 notation < "|term 19 e|" non associative with precedence 70 for @{'forget $e}.
386 interpretation "forget" 'forget a = (forget ? a).
387
388 notation > "𝐋\p\ term 70 E" non associative with precedence 75 for @{L_pi ? $E}.
389 nlet rec L_pi (S : Alpha) (r : pitem S) on r : lang S ≝ 
390 match r with
391 [ pz ⇒ ∅
392 | pe ⇒ ∅
393 | ps _ ⇒ ∅
394 | pp x ⇒ { [x] }
395 | pc r1 r2 ⇒ 𝐋\p\ r1 · 𝐋 |r2| ∪ 𝐋\p\ r2
396 | po r1 r2 ⇒ 𝐋\p\ r1 ∪ 𝐋\p\ r2
397 | pk r1 ⇒ 𝐋\p\ r1 · 𝐋 (|r1|^* ) ].
398 notation > "𝐋\p term 70 E" non associative with precedence 75 for @{'L_pi $E}.
399 notation "𝐋\sub(\p) term 70 E" non associative with precedence 75 for @{'L_pi $E}.
400 interpretation "in_pl" 'L_pi E = (L_pi ? E).
401
402 (* The caml, as some patches for it *)
403 ncoercion setoid1_of_setoid : ∀s:setoid. setoid1 ≝ setoid1_of_setoid on _s: setoid to setoid1.   
404    
405 alias symbol "hint_decl" (instance 1) = "hint_decl_CProp2".
406 unification hint 0 ≔ S : setoid, x,y;
407   SS ≟ LIST S,
408   TT ≟ setoid1_of_setoid SS
409 (*-----------------------------------------*) ⊢ 
410   eq_list S x y ≡ eq_rel1 ? (eq1 TT) x y.
411   
412 unification hint 0 ≔ SS : setoid;
413   S ≟ carr SS,
414   TT ≟ setoid1_of_setoid (LIST SS)
415 (*-----------------------------------------------------------------*) ⊢ 
416   list S ≡ carr1 TT.
417
418 (* not as morphism *)
419 nlemma Not_morphism : CProp[0] ⇒_1 CProp[0].
420 @(λx:CProp[0].¬ x); #a b; *; #; @; /3/; nqed.
421
422 unification hint 0 ≔ P : CProp[0];
423    A ≟ CPROP, 
424    B ≟ CPROP,
425    M ≟ mk_unary_morphism1 ?? (λP.¬ P) (prop11 ?? Not_morphism)
426 (*------------------------*)⊢
427   fun11 A B M P ≡ ¬ P.
428
429 (* XXX Ex setoid support *)
430 nlemma Ex_morphism : ∀S:setoid.(S ⇒_1 CProp[0]) ⇒_1 CProp[0].
431 #S; @(λP: S ⇒_1 CProp[0].Ex S P); #P Q E; @; *; #x Px; @x; ncases (E x x #); /2/; nqed.
432
433 unification hint 0 ≔ S : setoid, P : S ⇒_1 CProp[0];
434    A ≟ unary_morphism1_setoid1 (setoid1_of_setoid S) CPROP, 
435    B ≟ CPROP,
436    M ≟ mk_unary_morphism1 ?? (λP: S ⇒_1 CProp[0].Ex S P) 
437         (prop11 ?? (Ex_morphism S))
438 (*------------------------*)⊢
439   fun11 A B M P ≡ Ex S (fun11 S CPROP P).
440
441 nlemma Ex_morphism_eta : ∀S:setoid.(S ⇒_1 CProp[0]) ⇒_1 CProp[0].
442 #S; @(λP: S ⇒_1 CProp[0].Ex S (λx.P x)); #P Q E; @; *; #x Px; @x; ncases (E x x #); /2/; nqed.
443
444 unification hint 0 ≔ S : setoid, P : S ⇒_1 CProp[0];
445    A ≟ unary_morphism1_setoid1 (setoid1_of_setoid S) CPROP, 
446    B ≟ CPROP,
447    M ≟ mk_unary_morphism1 ?? (λP: S ⇒_1 CProp[0].Ex S (λx.P x)) 
448         (prop11 ?? (Ex_morphism_eta S))
449 (*------------------------*)⊢
450   fun11 A B M P ≡ Ex S (λx.fun11 S CPROP P x).
451
452 nlemma Ex_setoid : ∀S:setoid.(S ⇒_1 CPROP) → setoid.
453 #T P; @ (Ex T (λx:T.P x)); @; ##[ #H1 H2; napply True |##*: //; ##] nqed.
454
455 unification hint 0 ≔ T,P ; 
456    S ≟ (Ex_setoid T P) 
457 (*---------------------------*) ⊢
458    Ex T (λx:T.P x) ≡ carr S.
459
460 (* couts how many Ex we are traversing *)
461 ninductive counter : Type[0] ≝ 
462  | End : counter 
463  | Next : (bool → bool) → (* dummy arg please the notation mechanism *)
464           counter → counter. 
465
466 (* to rewrite terms (live in setoid) *)
467 nlet rec mk_P (S, T : setoid) (n : counter) on n ≝ 
468   match n with [ End ⇒ T → CProp[0] | Next _ m ⇒ S → (mk_P S T m) ].
469
470 nlet rec mk_F (S, T : setoid) (n : counter) on n ≝ 
471   match n with [ End ⇒ T | Next _ m ⇒ S → (mk_F S T m) ].
472   
473 nlet rec mk_E (S, T : setoid) (n : counter) on n : ∀f,g : mk_F S T n. CProp[0] ≝ 
474   match n with 
475   [ End ⇒ λf,g:T. f = g 
476   | Next q m ⇒ λf,g: mk_F S T (Next q m). ∀x:S.mk_E S T m (f x) (g x) ].
477
478 nlet rec mk_H (S, T : setoid) (n : counter) on n : 
479 ∀P1,P2: mk_P S T n.∀f,g : mk_F S T n. CProp[1] ≝ 
480   match n with 
481   [ End ⇒ λP1,P2:mk_P S T End.λf,g:T. f = g → P1 f =_1 P2 g 
482   | Next q m ⇒ λP1,P2: mk_P S T (Next q m).λf,g: mk_F S T (Next q m). 
483               ∀x:S.mk_H S T m (P1 x) (P2 x) (f x) (g x) ].
484
485 nlet rec mk_Ex (S, T : setoid) (n : counter) on n : 
486 ∀P: mk_P S T n.∀f : mk_F S T n. CProp[0] ≝ 
487   match n with 
488   [ End ⇒ λP:mk_P S T End.λf:T. P f 
489   | Next q m ⇒ λP: mk_P S T (Next q m).λf: mk_F S T (Next q m). 
490               ∃x:S.mk_Ex S T m (P x) (f x) ].
491
492 nlemma Sig_generic : ∀S,T.∀n:counter.∀P,f,g.
493   mk_E S T n f g → mk_H S T n P P f g → mk_Ex S T n P f =_1 mk_Ex S T n P g.
494 #S T n; nelim n; nnormalize;
495 ##[ #P f g E H; /2/;
496 ##| #q m IH P f g E H; @; *; #x Px; @x; ncases (IH … (E x) (H x)); /3/; ##]
497 nqed.
498
499 (* to rewrite propositions (live in setoid1) *)
500 nlet rec mk_P1 (S : setoid) (T : setoid1) (n : counter) on n ≝ 
501   match n with [ End ⇒ T → CProp[0] | Next _ m ⇒ S → (mk_P1 S T m) ].
502
503 nlet rec mk_F1 (S : setoid) (T : setoid1) (n : counter) on n ≝ 
504   match n with [ End ⇒ T | Next _ m ⇒ S → (mk_F1 S T m) ].
505   
506 nlet rec mk_E1 (S : setoid) (T : setoid1) (n : counter) on n : ∀f,g : mk_F1 S T n. CProp[1] ≝ 
507   match n with 
508   [ End ⇒ λf,g:T. f =_1 g 
509   | Next q m ⇒ λf,g: mk_F1 S T (Next q m). ∀x:S.mk_E1 S T m (f x) (g x) ].
510
511 nlet rec mk_H1 (S : setoid) (T : setoid1) (n : counter) on n : 
512 ∀P1,P2: mk_P1 S T n.∀f,g : mk_F1 S T n. CProp[1] ≝ 
513   match n with 
514   [ End ⇒ λP1,P2:mk_P1 S T End.λf,g:T. f = g → P1 f =_1 P2 g 
515   | Next q m ⇒ λP1,P2: mk_P1 S T (Next q m).λf,g: mk_F1 S T (Next q m). 
516               ∀x:S.mk_H1 S T m (P1 x) (P2 x) (f x) (g x) ].
517
518 nlet rec mk_Ex1 (S : setoid) (T : setoid1) (n : counter) on n : 
519 ∀P: mk_P1 S T n.∀f : mk_F1 S T n. CProp[0] ≝ 
520   match n with 
521   [ End ⇒ λP:mk_P1 S T End.λf:T. P f 
522   | Next q m ⇒ λP: mk_P1 S T (Next q m).λf: mk_F1 S T (Next q m). 
523               ∃x:S.mk_Ex1 S T m (P x) (f x) ].
524
525 nlemma Sig_generic1 : ∀S,T.∀n:counter.∀P,f,g.
526   mk_E1 S T n f g → mk_H1 S T n P P f g → mk_Ex1 S T n P f =_1 mk_Ex1 S T n P g.
527 #S T n; nelim n; nnormalize;
528 ##[ #P f g E H; /2/;
529 ##| #q m IH P f g E H; @; *; #x Px; @x; ncases (IH … (E x) (H x)); /3/; ##]
530 nqed.
531
532 (* notation "∑x1,...,xn. E / H ; P" were:
533    - x1...xn are bound in E and P, H is bound in P
534    - H is an identifier that will have the type of E in P
535    - P is the proof that the two existentially quantified predicates are equal*)
536 notation > "∑ list1 ident x sep , . term 56 E / ident nE ; term 19 H" with precedence 20 
537 for @{ 'Sig_gen 
538   ${ fold right @{ 'End }  rec acc @{ ('Next (λ${ident x}.${ident x}) $acc) } }
539   ${ fold right @{ $E }              rec acc @{ λ${ident x}.$acc } } 
540   ${ fold right @{ λ${ident nE}.$H } rec acc @{ λ${ident x}.$acc } }
541 }.
542
543 interpretation "next" 'Next x y = (Next x y).
544 interpretation "end" 'End = End.
545 (*interpretation "sig_gen" 'Sig_gen n E H = (Sig_generic  ?? n ??? E H).*)
546 interpretation "sig_gen1" 'Sig_gen n E H = (Sig_generic1 ?? n ??? E H).
547
548 nlemma test0 : ∀S:setoid. ∀P: S ⇒_1 CPROP.∀f,g:S → S.
549  (∀x:S.f x = g x) → (Ex S (λw.P (f w))) =_1 (Ex S (λw.P (g w))).
550 #S P f g E; napply (∑w. E w / H ; ┼_1H); nqed.
551
552 nlemma test : ∀S:setoid. ∀P: S ⇒_1 CPROP.∀f,g:S → S.
553  (∀x:S.f x = g x) → (Ex S (λw.P (f w)∧ True)) =_1 (Ex S (λw.P (g w)∧ True)).
554 #S P f g E; napply (∑w. E w / H ; (┼_1H)╪_1#); nqed. 
555
556 nlemma test_bound : ∀S:setoid. ∀e,f: S ⇒_1 CPROP. e = f → 
557    (Ex S (λw.e w ∧ True)) =_1 (Ex S (λw.f w ∧ True)).
558 #S f g E; napply (.=_1 ∑x. E x x # / H ; (H ╪_1 #)); //; nqed.
559
560 nlemma test2 : ∀S:setoid. ∀ee: S ⇒_1 S ⇒_1 CPROP.
561  ∀x,y:setoid1_of_setoid S.x =_1 y → 
562    (True ∧ (Ex S (λw.ee x w ∧ True))) =_1 (True ∧ (Ex S (λw.ee y w ∧ True))).
563 #S m x y E; napply (.=_1 #╪_1(∑w. E / E ; ((E ╪_1 #) ╪_1 #))). //; nqed.
564
565 nlemma test3 : ∀S:setoid. ∀ee: S ⇒_1 S ⇒_1 CPROP.
566  ∀x,y:setoid1_of_setoid S.x =_1 y → 
567    ((Ex S (λw.ee x w ∧ True) ∨ True)) =_1 ((Ex S (λw.ee y w ∧ True) ∨ True)).
568 #S m x y E; napply (.=_1 (∑w. E / E ; ((E ╪_1 #) ╪_1 #)) ╪_1 #). //; nqed.
569
570 (* Ex setoid support end *)
571
572 ndefinition L_pi_ext : ∀S:Alpha.∀r:pitem S.Elang S.
573 #S r; @(𝐋\p r); #w1 w2 E; nelim r; 
574 ##[ ##1,2: /2/;
575 ##| #x; @; *;
576 ##| #x; @; #H; nchange in H with ([?] =_0 ?); ##[ napply ((.=_0 H) E); ##]
577     napply ((.=_0 H) E^-1);
578 ##| #e1 e2 H1 H2; (*
579     nchange in match (w1 ∈ 𝐋\p (?·?)) with ((∃_.?)∨?);
580     nchange in match (w2 ∈ 𝐋\p (?·?)) with ((∃_.?)∨?); good! *)
581     napply (.= (#‡H2));
582     ncut (∀x1,x2. (w1 = (x1@x2)) = (w2 = (x1@x2)));##[
583       #x1 x2; @; #X; ##[ napply ((.= E^-1) X) | napply ((.= E) X) ] ##] #X;
584     napply ((∑w1,w2. X w1 w2 / H ; (H╪_1#)╪_1#) ╪_1 #); 
585 ##| #e1 e2 H1 H2; napply (H1‡H2); (* good! *)
586 ##| #e H; 
587     ncut (∀x1,x2.(w1 = (x1@x2)) = (w2 = (x1@x2)));##[
588       #x1 x2; @; #X; ##[ napply ((.= E^-1) X) | napply ((.= E) X) ] ##] #X;
589     (* nnormalize in ⊢ (???%%); good! (but a bit too hard) *)
590     napply (∑w1,w2. X w1 w2 / H ; (H╪_1#)╪_1#); 
591 ##]
592 nqed.
593
594 unification hint 0 ≔ S : Alpha,e : pitem S; 
595   SS ≟ (list S),
596   X ≟ (mk_ext_powerclass SS (𝐋\p e) (ext_prop SS (L_pi_ext S e)))
597 (*-----------------------------------------------------------------*)⊢ 
598   ext_carr SS X ≡ 𝐋\p e.
599   
600 ndefinition epsilon ≝ 
601   λS:Alpha.λb.match b return λ_.lang S with [ true ⇒ { [ ] } | _ ⇒ ∅ ].
602
603 interpretation "epsilon" 'epsilon = (epsilon ?).
604 notation < "ϵ b" non associative with precedence 90 for @{'app_epsilon $b}.
605 interpretation "epsilon lang" 'app_epsilon b = (epsilon ? b).
606
607 ndefinition L_pr ≝ λS : Alpha.λp:pre S.  𝐋\p\ (\fst p) ∪ ϵ (\snd p).
608   
609 interpretation "L_pr" 'L_pi E = (L_pr ? E).
610
611 nlemma append_eq_nil : ∀S:setoid.∀w1,w2:list S. [ ] = w1 @ w2 → w1 = [ ].
612 #S w1; ncases w1; //. nqed.
613   
614 (* lemma 12 *)
615 nlemma epsilon_in_true : ∀S:Alpha.∀e:pre S. [ ] ∈ 𝐋\p e = (\snd e = true).
616 #S r; ncases r; #e b; @; ##[##2: #H; nrewrite > H; @2; /2/; ##] ncases b;//; 
617 *; ##[##2:*] nelim e;
618 ##[ ##1,2: *; ##| #c; *; ##| #c; *| ##7: #p H;
619 ##| #r1 r2 H G; *; ##[##2: nassumption; ##]
620 ##| #r1 r2 H1 H2; *; /2/ by {}]
621 *; #w1; *; #w2; *; *; 
622 ##[ #defw1 H1 foo; napply H;
623     napply (. (append_eq_nil ? ?? defw1)^-1╪_1#);
624     nassumption; 
625 ##| #defw1 H1 foo; napply H;
626     napply (. (append_eq_nil ? ?? defw1)^-1╪_1#);
627     nassumption; 
628 ##]
629 nqed.
630
631 nlemma not_epsilon_lp : ∀S:Alpha.∀e:pitem S. ¬ ([ ] ∈ (𝐋\p e)).
632 #S e; nelim e; ##[##1,2,3,4: nnormalize;/2/]
633 ##[ #p1 p2 np1 np2; *; ##[##2: napply np2] *; #w1; *; #w2; *; *; #abs;
634     nlapply (append_eq_nil ??? abs); # defw1; #; napply np1;
635     napply (. defw1^-1╪_1#);
636     nassumption;
637 ##| #p1 p2 np1 np2; *; nchange with (¬?); //;
638 ##| #r n; *; #w1; *; #w2; *; *; #abs; #; napply n;
639     nlapply (append_eq_nil ??? abs); # defw1; #;
640     napply (. defw1^-1╪_1#);
641     nassumption;##]
642 nqed.
643
644 ndefinition lo ≝ λS:Alpha.λa,b:pre S.〈\fst a + \fst b,\snd a || \snd b〉.
645 notation "a ⊕ b" left associative with precedence 60 for @{'oplus $a $b}.
646 interpretation "oplus" 'oplus a b = (lo ? a b).
647
648 ndefinition lc ≝ λS:Alpha.λbcast:∀S:Alpha.∀E:pitem S.pre S.λa,b:pre S.
649    match a with [ mk_pair e1 b1 ⇒
650    match b1 with 
651    [ false ⇒ 〈e1 · \fst b, \snd b〉 
652    | true ⇒ 〈e1 · \fst (bcast ? (\fst b)),\snd b || \snd (bcast ? (\fst b))〉]].
653    
654 notation < "a ⊙ b" left associative with precedence 60 for @{'lc $op $a $b}.
655 interpretation "lc" 'lc op a b = (lc ? op a b).
656 notation > "a ⊙ b" left associative with precedence 60 for @{'lc eclose $a $b}.
657
658 ndefinition lk ≝ λS:Alpha.λbcast:∀S:Alpha.∀E:pitem S.pre S.λa:pre S.
659    match a with [ mk_pair e1 b1 ⇒
660    match b1 with 
661    [ false ⇒ 〈e1^*, false〉 
662    | true ⇒ 〈(\fst (bcast ? e1))^*, true〉]].
663
664 notation < "a \sup ⊛" non associative with precedence 90 for @{'lk $op $a}.
665 interpretation "lk" 'lk op a = (lk ? op a).
666 notation > "a ^ ⊛" non associative with precedence 75 for @{'lk eclose $a}.
667
668 notation > "•" non associative with precedence 60 for @{eclose ?}.
669 nlet rec eclose (S: Alpha) (E: pitem S) on E : pre S ≝
670  match E with
671   [ pz ⇒ 〈 0, false 〉
672   | pe ⇒ 〈 ϵ,  true 〉
673   | ps x ⇒ 〈 `.x, false 〉
674   | pp x ⇒ 〈 `.x, false 〉
675   | po E1 E2 ⇒ •E1 ⊕ •E2
676   | pc E1 E2 ⇒ •E1 ⊙ 〈 E2, false 〉 
677   | pk E ⇒ 〈(\fst (•E))^*,true〉].
678 notation < "• x" non associative with precedence 60 for @{'eclose $x}.
679 interpretation "eclose" 'eclose x = (eclose ? x).
680 notation > "• x" non associative with precedence 60 for @{'eclose $x}.
681
682 ndefinition reclose ≝ λS:Alpha.λp:pre S.let p' ≝ •\fst p in 〈\fst p',\snd p || \snd p'〉.
683 interpretation "reclose" 'eclose x = (reclose ? x).
684
685 nlemma epsilon_or : ∀S:Alpha.∀b1,b2. ϵ(b1 || b2) = ϵ b1 ∪ ϵ b2. ##[##2: napply S]
686 #S b1 b2; ncases b1; ncases b2; 
687 nchange in match (true || true) with true;
688 nchange in match (true || false) with true;
689 nchange in match (ϵ true) with {[]};
690 nchange in match (ϵ false) with ∅;
691 ##[##1,4: napply ((cupID…)^-1);
692 ##| napply ((cup0 ? {[]})^-1);
693 ##| napply (.= (cup0 ? {[]})^-1); napply cupC; ##]
694 nqed.
695
696 (* XXX move somewere else *)
697 ndefinition if': ∀A,B:CPROP. A = B → A → B.
698 #A B; *; /2/. nqed.
699
700 ncoercion if : ∀A,B:CPROP. ∀p:A = B. A → B ≝ if' on _p : eq_rel1 ???? to ∀_:?.?.
701
702 (* theorem 16: 2 *)
703 nlemma oplus_cup : ∀S:Alpha.∀e1,e2:pre S.𝐋\p (e1 ⊕ e2) = 𝐋\p e1 ∪ 𝐋\p e2.
704 #S r1; ncases r1; #e1 b1 r2; ncases r2; #e2 b2; (* oh my!
705 nwhd in ⊢ (???(??%)?);
706 nchange in ⊢(???%?) with (𝐋\p (e1 + e2) ∪ ϵ (b1 || b2));
707 nchange in ⊢(???(??%?)?) with (𝐋\p (e1) ∪ 𝐋\p (e2)); *)
708 napply (.=_1 #╪_1 (epsilon_or ???));
709 napply (.=_1 (cupA…)^-1);
710 napply (.=_1 (cupA…)╪_1#);
711 napply (.=_1 (#╪_1(cupC…))╪_1#);
712 napply (.=_1 (cupA…)^-1╪_1#);
713 napply (.=_1 (cupA…));
714 //;
715 nqed.
716
717
718 (* XXX problem: auto does not find # (refl) when it has a concrete == *)
719 nlemma odotEt : ∀S:Alpha.∀e1,e2:pitem S.∀b2:bool.
720   〈e1,true〉 ⊙ 〈e2,b2〉 = 〈e1 · \fst (•e2),b2 || \snd (•e2)〉.
721 #S e1 e2 b2; ncases b2; nnormalize; @; //; @; napply refl; nqed.
722
723 nlemma LcatE : ∀S:Alpha.∀e1,e2:pitem S.
724   𝐋\p (e1 · e2) =  𝐋\p e1 · 𝐋  |e2| ∪ 𝐋\p e2. //; nqed.
725
726 nlemma cup_dotD : ∀S:Alpha.∀p,q,r:lang S.(p ∪ q) · r = (p · r) ∪ (q · r). 
727 #S p q r; napply ext_set; #w; nnormalize; @; 
728 ##[ *; #x; *; #y; *; *; #defw; *; /7/ by or_introl, or_intror, ex_intro, conj;
729 ##| *; *; #x; *; #y; *; *; /7/ by or_introl, or_intror, ex_intro, conj; ##]
730 nqed.
731
732
733 nlemma erase_dot : ∀S:Alpha.∀e1,e2:pitem S.𝐋 |e1 · e2| =  𝐋 |e1| · 𝐋 |e2|.
734 #S e1 e2; napply ext_set; nnormalize; #w; @; *; #w1; *; #w2; *; *; /7/ by ex_intro, conj;
735 nqed.
736
737 nlemma erase_plus : ∀S:Alpha.∀e1,e2:pitem S.𝐋 |e1 + e2| =  𝐋 |e1| ∪ 𝐋 |e2|.
738 #S e1 e2; napply ext_set; nnormalize; #w; @; *; /4/ by or_introl, or_intror; nqed.
739
740 nlemma erase_star : ∀S:Alpha.∀e1:pitem S.𝐋 |e1|^* = 𝐋 |e1^*|. //; nqed.
741
742 nlemma mem_single : ∀S:setoid.∀a,b:S. a ∈ {(b)} → a = b.
743 #S a b; nnormalize; /2/; nqed.
744
745 notation < "[\setoid\emsp\of\emsp term 19 x]" non associative with precedence 90 for @{'mk_setoid $x}.
746 interpretation "mk_setoid" 'mk_setoid x = (mk_setoid x ?).
747
748 nlemma cup_sub: ∀S.∀A,B:𝛀^S.∀x. ¬ (x ∈ A) → A ∪ (B - {(x)}) = (A ∪ B) - {(x)}.
749 #S A B x H; napply ext_set; #w; @; 
750 ##[ *; ##[ #wa; @; ##[@;//] #H2; napply H; napply (. (mem_single ??? H2)^-1╪_1#); //]
751     *; #wb nwn; @; ##[@2;//] //;
752 ##| *; *; ##[ #wa nwn; @; //] #wb nwn; @2; @; //;##]
753 nqed.
754
755 nlemma sub0 : ∀S.∀a:Ω^S. a - ∅ = a.
756 #S a; napply ext_set; #w; nnormalize; @; /3/; *; //; nqed.
757
758 nlemma subK : ∀S.∀a:Ω^S. a - a = ∅.
759 #S a; napply ext_set; #w; nnormalize; @; *; /2/; nqed.
760
761 nlemma subW : ∀S.∀a,b:Ω^S.∀w.w ∈ (a - b) → w ∈ a.
762 #S a b w; nnormalize; *; //; nqed.
763
764 nlemma erase_bull : ∀S:Alpha.∀a:pitem S. |\fst (•a)| = |a|.
765 #S a; nelim a; // by {};
766 ##[ #e1 e2 IH1 IH2; nchange in ⊢ (???%) with (|e1| · |e2|);
767
768 finqui: manca · morfismo, oppure un lemma che dice che == è come Leibnitz.
769
770     nrewrite < IH1; nrewrite < IH2;  
771     nchange in ⊢ (??(??%)?) with (\fst (•e1 ⊙ 〈e2,false〉));
772     ncases (•e1); #e3 b; ncases b; nnormalize;
773     ##[ ncases (•e2); //; ##| nrewrite > IH2; //]
774 ##| #e1 e2 IH1 IH2; nchange in ⊢ (???%) with (.|e1| + .|e2|);
775     nrewrite < IH2; nrewrite < IH1;
776     nchange in ⊢ (??(??%)?) with (\fst (•e1 ⊕ •e2));
777     ncases (•e1); ncases (•e2); //]
778 ##| #e IH; nchange in ⊢ (???%) with (.|e|^* ); nrewrite < IH;
779     nchange in ⊢ (??(??%)?) with (\fst (•e))^*; //; ##]
780 nqed.
781
782 nlemma eta_lp : ∀S.∀p:pre S.𝐋\p p = 𝐋\p 〈\fst p, \snd p〉.
783 #S p; ncases p; //; nqed.
784
785 nlemma epsilon_dot: ∀S.∀p:word S → Prop. {[]} · p = p. 
786 #S e; napply extP; #w; nnormalize; @; ##[##2: #Hw; @[]; @w; /3/; ##]
787 *; #w1; *; #w2; *; *; #defw defw1 Hw2; nrewrite < defw; nrewrite < defw1;
788 napply Hw2; nqed.
789
790 (* theorem 16: 1 → 3 *)
791 nlemma odot_dot_aux : ∀S.∀e1,e2: pre S.
792       𝐋\p (•(\fst e2)) =  𝐋\p (\fst e2) ∪ 𝐋 .|\fst e2| → 
793          𝐋\p (e1 ⊙ e2) =  𝐋\p e1 · 𝐋 .|\fst e2| ∪ 𝐋\p e2.
794 #S e1 e2 th1; ncases e1; #e1' b1'; ncases b1';
795 ##[ nwhd in ⊢ (??(??%)?); nletin e2' ≝ (\fst e2); nletin b2' ≝ (\snd e2); 
796     nletin e2'' ≝ (\fst (•(\fst e2))); nletin b2'' ≝ (\snd (•(\fst e2)));
797     nchange in ⊢ (??%?) with (?∪?); 
798     nchange in ⊢ (??(??%?)?) with (?∪?);
799     nchange in match (𝐋\p 〈?,?〉) with (?∪?);
800     nrewrite > (epsilon_or …); nrewrite > (cupC ? (ϵ ?)…);
801     nrewrite > (cupA …);nrewrite < (cupA ?? (ϵ?)…);
802     nrewrite > (?: 𝐋\p e2'' ∪ ϵ b2'' = 𝐋\p e2' ∪ 𝐋 .|e2'|); ##[##2:
803       nchange with (𝐋\p 〈e2'',b2''〉 =  𝐋\p e2' ∪ 𝐋 .|e2'|); 
804       ngeneralize in match th1;
805       nrewrite > (eta_lp…); #th1; nrewrite > th1; //;##]
806     nrewrite > (eta_lp ? e2); 
807     nchange in match (𝐋\p 〈\fst e2,?〉) with (𝐋\p e2'∪ ϵ b2');
808     nrewrite > (cup_dotD …); nrewrite > (epsilon_dot…);       
809     nrewrite > (cupC ? (𝐋\p e2')…); nrewrite > (cupA…);nrewrite > (cupA…);
810     nrewrite < (erase_bull S e2') in ⊢ (???(??%?)); //;
811 ##| ncases e2; #e2' b2'; nchange in match (〈e1',false〉⊙?) with 〈?,?〉;
812     nchange in match (𝐋\p ?) with (?∪?);
813     nchange in match (𝐋\p (e1'·?)) with (?∪?);
814     nchange in match (𝐋\p 〈e1',?〉) with (?∪?);
815     nrewrite > (cup0…); 
816     nrewrite > (cupA…); //;##]
817 nqed.
818
819 nlemma sub_dot_star : 
820   ∀S.∀X:word S → Prop.∀b. (X - ϵ b) · X^* ∪ {[]} = X^*.
821 #S X b; napply extP; #w; @;
822 ##[ *; ##[##2: nnormalize; #defw; nrewrite < defw; @[]; @; //]
823     *; #w1; *; #w2; *; *; #defw sube; *; #lw; *; #flx cj;
824     @ (w1 :: lw); nrewrite < defw; nrewrite < flx; @; //;
825     @; //; napply (subW … sube);
826 ##| *; #wl; *; #defw Pwl; nrewrite < defw; nelim wl in Pwl; ##[ #_; @2; //]
827     #w' wl' IH; *; #Pw' IHp; nlapply (IH IHp); *;
828     ##[ *; #w1; *; #w2; *; *; #defwl' H1 H2;
829         @; ncases b in H1; #H1; 
830         ##[##2: nrewrite > (sub0…); @w'; @(w1@w2);
831                 nrewrite > (associative_append ? w' w1 w2);
832                 nrewrite > defwl'; @; ##[@;//] @(wl'); @; //;
833            ##| ncases w' in Pw';
834                ##[ #ne; @w1; @w2; nrewrite > defwl'; @; //; @; //;
835                ##| #x xs Px; @(x::xs); @(w1@w2); 
836                    nrewrite > (defwl'); @; ##[@; //; @; //; @; nnormalize; #; ndestruct]
837                    @wl'; @; //; ##] ##]
838         ##| #wlnil; nchange in match (flatten ? (w'::wl')) with (w' @ flatten ? wl');
839             nrewrite < (wlnil); nrewrite > (append_nil…); ncases b;
840             ##[ ncases w' in Pw'; /2/; #x xs Pxs; @; @(x::xs); @([]);
841                 nrewrite > (append_nil…); @; ##[ @; //;@; //; nnormalize; @; #; ndestruct]
842                 @[]; @; //;
843             ##| @; @w'; @[]; nrewrite > (append_nil…); @; ##[##2: @[]; @; //] 
844                 @; //; @; //; @; *;##]##]##] 
845 nqed.
846
847 (* theorem 16: 1 *)
848 alias symbol "pc" (instance 13) = "cat lang".
849 alias symbol "in_pl" (instance 23) = "in_pl".
850 alias symbol "in_pl" (instance 5) = "in_pl".
851 alias symbol "eclose" (instance 21) = "eclose".
852 ntheorem bull_cup : ∀S:Alpha. ∀e:pitem S.  𝐋\p (•e) =  𝐋\p e ∪ 𝐋 .|e|.
853 #S e; nelim e; //;
854   ##[ #a; napply extP; #w; nnormalize; @; *; /3/ by or_introl, or_intror;
855   ##| #a; napply extP; #w; nnormalize; @; *; /3/ by or_introl; *;
856   ##| #e1 e2 IH1 IH2;  
857       nchange in ⊢ (??(??(%))?) with (•e1 ⊙ 〈e2,false〉);
858       nrewrite > (odot_dot_aux S (•e1) 〈e2,false〉 IH2);
859       nrewrite > (IH1 …); nrewrite > (cup_dotD …);
860       nrewrite > (cupA …); nrewrite > (cupC ?? (𝐋\p ?) …);
861       nchange in match (𝐋\p 〈?,?〉) with (𝐋\p e2 ∪ {}); nrewrite > (cup0 …);
862       nrewrite < (erase_dot …); nrewrite < (cupA …); //;
863   ##| #e1 e2 IH1 IH2;
864       nchange in match (•(?+?)) with (•e1 ⊕ •e2); nrewrite > (oplus_cup …);
865       nrewrite > (IH1 …); nrewrite > (IH2 …); nrewrite > (cupA …);
866       nrewrite > (cupC ? (𝐋\p e2)…);nrewrite < (cupA ??? (𝐋\p e2)…);
867       nrewrite > (cupC ?? (𝐋\p e2)…); nrewrite < (cupA …); 
868       nrewrite < (erase_plus …); //.
869   ##| #e; nletin e' ≝ (\fst (•e)); nletin b' ≝ (\snd (•e)); #IH;
870       nchange in match (𝐋\p ?) with  (𝐋\p 〈e'^*,true〉);
871       nchange in match (𝐋\p ?) with (𝐋\p (e'^* ) ∪ {[ ]});
872       nchange in ⊢ (??(??%?)?) with (𝐋\p e' · 𝐋 .|e'|^* );
873       nrewrite > (erase_bull…e);
874       nrewrite > (erase_star …);
875       nrewrite > (?: 𝐋\p e' =  𝐋\p e ∪ (𝐋 .|e| - ϵ b')); ##[##2:
876         nchange in IH : (??%?) with (𝐋\p e' ∪ ϵ b'); ncases b' in IH; 
877         ##[ #IH; nrewrite > (cup_sub…); //; nrewrite < IH; 
878             nrewrite < (cup_sub…); //; nrewrite > (subK…); nrewrite > (cup0…);//;
879         ##| nrewrite > (sub0 …); #IH; nrewrite < IH; nrewrite > (cup0 …);//; ##]##]
880       nrewrite > (cup_dotD…); nrewrite > (cupA…); 
881       nrewrite > (?: ((?·?)∪{[]} = 𝐋 .|e^*|)); //;
882       nchange in match (𝐋 .|e^*|) with ((𝐋. |e|)^* ); napply sub_dot_star;##]
883  nqed.
884
885 (* theorem 16: 3 *)      
886 nlemma odot_dot: 
887   ∀S.∀e1,e2: pre S.  𝐋\p (e1 ⊙ e2) =  𝐋\p e1 · 𝐋 .|\fst e2| ∪ 𝐋\p e2.
888 #S e1 e2; napply odot_dot_aux; napply (bull_cup S (\fst e2)); nqed.
889
890 nlemma dot_star_epsilon : ∀S.∀e:re S.𝐋 e · 𝐋 e^* ∪ {[]} =  𝐋 e^*.
891 #S e; napply extP; #w; nnormalize; @;
892 ##[ *; ##[##2: #H; nrewrite < H; @[]; /3/] *; #w1; *; #w2; 
893     *; *; #defw Hw1; *; #wl; *; #defw2 Hwl; @(w1 :: wl);
894     nrewrite < defw; nrewrite < defw2; @; //; @;//;
895 ##| *; #wl; *; #defw Hwl; ncases wl in defw Hwl; ##[#defw; #; @2; nrewrite < defw; //]
896     #x xs defw; *; #Hx Hxs; @; @x; @(flatten ? xs); nrewrite < defw;
897     @; /2/; @xs; /2/;##]
898  nqed.
899
900 nlemma nil_star : ∀S.∀e:re S. [ ] ∈ e^*.
901 #S e; @[]; /2/; nqed.
902
903 nlemma cupID : ∀S.∀l:word S → Prop.l ∪ l = l.
904 #S l; napply extP; #w; @; ##[*]//; #; @; //; nqed.
905
906 nlemma cup_star_nil : ∀S.∀l:word S → Prop. l^* ∪ {[]} = l^*.
907 #S a; napply extP; #w; @; ##[*; //; #H; nrewrite < H; @[]; @; //] #;@; //;nqed.
908
909 nlemma rcanc_sing : ∀S.∀A,C:word S → Prop.∀b:word S .
910   ¬ (A b) → A ∪ { (b) } = C → A = C - { (b) }.
911 #S A C b nbA defC; nrewrite < defC; napply extP; #w; @;
912 ##[ #Aw; /3/| *; *; //; #H nH; ncases nH; #abs; nlapply (abs H); *]
913 nqed.
914
915 (* theorem 16: 4 *)      
916 nlemma star_dot: ∀S.∀e:pre S. 𝐋\p (e^⊛) = 𝐋\p e · (𝐋 .|\fst e|)^*.
917 #S p; ncases p; #e b; ncases b;
918 ##[ nchange in match (〈e,true〉^⊛) with 〈?,?〉;
919     nletin e' ≝ (\fst (•e)); nletin b' ≝ (\snd (•e));
920     nchange in ⊢ (??%?) with (?∪?);
921     nchange in ⊢ (??(??%?)?) with (𝐋\p e' · 𝐋 .|e'|^* );
922     nrewrite > (?: 𝐋\p e' = 𝐋\p e ∪ (𝐋 .|e| - ϵ b' )); ##[##2:
923       nlapply (bull_cup ? e); #bc;
924       nchange in match (𝐋\p (•e)) in bc with (?∪?);
925       nchange in match b' in bc with b';
926       ncases b' in bc; ##[##2: nrewrite > (cup0…); nrewrite > (sub0…); //]
927       nrewrite > (cup_sub…); ##[napply rcanc_sing] //;##]
928     nrewrite > (cup_dotD…); nrewrite > (cupA…);nrewrite > (erase_bull…);
929     nrewrite > (sub_dot_star…);
930     nchange in match (𝐋\p 〈?,?〉) with (?∪?);
931     nrewrite > (cup_dotD…); nrewrite > (epsilon_dot…); //;    
932 ##| nwhd in match (〈e,false〉^⊛); nchange in match (𝐋\p 〈?,?〉) with (?∪?);
933     nrewrite > (cup0…);
934     nchange in ⊢ (??%?) with (𝐋\p e · 𝐋 .|e|^* );
935     nrewrite < (cup0 ? (𝐋\p e)); //;##]
936 nqed.
937
938 nlet rec pre_of_re (S : Alpha) (e : re S) on e : pitem S ≝ 
939   match e with 
940   [ z ⇒ pz ?
941   | e ⇒ pe ?
942   | s x ⇒ ps ? x
943   | c e1 e2 ⇒ pc ? (pre_of_re ? e1) (pre_of_re ? e2)
944   | o e1 e2 ⇒ po ? (pre_of_re ? e1) (pre_of_re ? e2)
945   | k e1 ⇒ pk ? (pre_of_re ? e1)].
946
947 nlemma notFalse : ¬False. @; //; nqed.
948
949 nlemma dot0 : ∀S.∀A:word S → Prop. {} · A = {}.
950 #S A; nnormalize; napply extP; #w; @; ##[##2: *]
951 *; #w1; *; #w2; *; *; //; nqed.
952
953 nlemma Lp_pre_of_re : ∀S.∀e:re S. 𝐋\p (pre_of_re ? e) = {}.
954 #S e; nelim e; ##[##1,2,3: //]
955 ##[ #e1 e2 H1 H2; nchange in match (𝐋\p (pre_of_re S (e1 e2))) with (?∪?);
956     nrewrite > H1; nrewrite > H2; nrewrite > (dot0…); nrewrite > (cupID…);//
957 ##| #e1 e2 H1 H2; nchange in match (𝐋\p (pre_of_re S (e1+e2))) with (?∪?);
958     nrewrite > H1; nrewrite > H2; nrewrite > (cupID…); //
959 ##| #e1 H1; nchange in match (𝐋\p (pre_of_re S (e1^* ))) with (𝐋\p (pre_of_re ??) · ?);
960     nrewrite > H1; napply dot0; ##]
961 nqed.
962
963 nlemma erase_pre_of_reK : ∀S.∀e. 𝐋 .|pre_of_re S e| = 𝐋 e.
964 #S A; nelim A; //; 
965 ##[ #e1 e2 H1 H2; nchange in match (𝐋 (e1 · e2)) with (𝐋 e1·?);
966     nrewrite < H1; nrewrite < H2; //
967 ##| #e1 e2 H1 H2; nchange in match (𝐋 (e1 + e2)) with (𝐋 e1 ∪ ?);
968     nrewrite < H1; nrewrite < H2; //
969 ##| #e1 H1; nchange in match (𝐋  (e1^* )) with ((𝐋 e1)^* );
970     nrewrite < H1; //]
971 nqed.     
972
973 (* corollary 17 *)
974 nlemma L_Lp_bull : ∀S.∀e:re S.𝐋 e = 𝐋\p (•pre_of_re ? e).
975 #S e; nrewrite > (bull_cup…); nrewrite > (Lp_pre_of_re…);
976 nrewrite > (cupC…); nrewrite > (cup0…); nrewrite > (erase_pre_of_reK…); //;
977 nqed.
978
979 nlemma Pext : ∀S.∀f,g:word S → Prop. f = g → ∀w.f w → g w.
980 #S f g H; nrewrite > H; //; nqed.
981  
982 (* corollary 18 *)
983 ntheorem bull_true_epsilon : ∀S.∀e:pitem S. \snd (•e) = true ↔ [ ] ∈ .|e|.
984 #S e; @;
985 ##[ #defsnde; nlapply (bull_cup ? e); nchange in match (𝐋\p (•e)) with (?∪?);
986     nrewrite > defsnde; #H; 
987     nlapply (Pext ??? H [ ] ?); ##[ @2; //] *; //;
988     E MO?
989
990 STOP
991
992 notation > "\move term 90 x term 90 E" 
993 non associative with precedence 60 for @{move ? $x $E}.
994 nlet rec move (S: Alpha) (x:S) (E: pitem S) on E : pre S ≝
995  match E with
996   [ pz ⇒ 〈 ∅, false 〉
997   | pe ⇒ 〈 ϵ, false 〉
998   | ps y ⇒ 〈 `y, false 〉
999   | pp y ⇒ 〈 `y, x == y 〉
1000   | po e1 e2 ⇒ \move x e1 ⊕ \move x e2 
1001   | pc e1 e2 ⇒ \move x e1 ⊙ \move x e2
1002   | pk e ⇒ (\move x e)^⊛ ].
1003 notation < "\move\shy x\shy E" non associative with precedence 60 for @{'move $x $E}.
1004 notation > "\move term 90 x term 90 E" non associative with precedence 60 for @{'move $x $E}.
1005 interpretation "move" 'move x E = (move ? x E).
1006
1007 ndefinition rmove ≝ λS:Alpha.λx:S.λe:pre S. \move x (\fst e).
1008 interpretation "rmove" 'move x E = (rmove ? x E).
1009
1010 nlemma XXz :  ∀S:Alpha.∀w:word S. w .∈ ∅ → False.
1011 #S w abs; ninversion abs; #; ndestruct;
1012 nqed.
1013
1014
1015 nlemma XXe :  ∀S:Alpha.∀w:word S. w .∈ ϵ → False.
1016 #S w abs; ninversion abs; #; ndestruct;
1017 nqed.
1018
1019 nlemma XXze :  ∀S:Alpha.∀w:word S. w .∈ (∅ · ϵ)  → False.
1020 #S w abs; ninversion abs; #; ndestruct; /2/ by XXz,XXe;
1021 nqed.
1022
1023
1024 naxiom in_move_cat:
1025  ∀S.∀w:word S.∀x.∀E1,E2:pitem S. w .∈ \move x (E1 · E2) → 
1026    (∃w1.∃w2. w = w1@w2 ∧ w1 .∈ \move x E1 ∧ w2 ∈ .|E2|) ∨ w .∈ \move x E2.
1027 #S w x e1 e2 H; nchange in H with (w .∈ \move x e1 ⊙ \move x e2);
1028 ncases e1 in H; ncases e2;
1029 ##[##1: *; ##[*; nnormalize; #; ndestruct] 
1030    #H; ninversion H; ##[##1,4,5,6: nnormalize; #; ndestruct]
1031    nnormalize; #; ndestruct; ncases (?:False); /2/ by XXz,XXze;
1032 ##|##2: *; ##[*; nnormalize; #; ndestruct] 
1033    #H; ninversion H; ##[##1,4,5,6: nnormalize; #; ndestruct]
1034    nnormalize; #; ndestruct; ncases (?:False); /2/ by XXz,XXze;
1035 ##| #r; *; ##[ *; nnormalize; #; ndestruct] 
1036    #H; ninversion H; ##[##1,4,5,6: nnormalize; #; ndestruct]
1037    ##[##2: nnormalize; #; ndestruct; @2; @2; //.##]
1038    nnormalize; #; ndestruct; ncases (?:False); /2/ by XXz;
1039 ##| #y; *; ##[ *; nnormalize; #defw defx; ndestruct; @2; @1; /2/ by conj;##]
1040    #H; ninversion H; nnormalize; #; ndestruct; 
1041    ##[ncases (?:False); /2/ by XXz] /3/ by or_intror;
1042 ##| #r1 r2; *; ##[ *; #defw]
1043     ...
1044 nqed.
1045
1046 ntheorem move_ok:
1047  ∀S:Alpha.∀E:pre S.∀a,w.w .∈ \move a E ↔ (a :: w) .∈ E. 
1048 #S E; ncases E; #r b; nelim r;
1049 ##[##1,2: #a w; @; 
1050    ##[##1,3: nnormalize; *; ##[##1,3: *; #; ndestruct; ##| #abs; ncases (XXz … abs); ##]
1051       #H; ninversion H; #; ndestruct;
1052    ##|##*:nnormalize; *; ##[##1,3: *; #; ndestruct; ##| #H1; ncases (XXz … H1); ##]
1053        #H; ninversion H; #; ndestruct;##]
1054 ##|#a c w; @; nnormalize; ##[*; ##[*; #; ndestruct; ##] #abs; ninversion abs; #; ndestruct;##]
1055    *; ##[##2: #abs; ninversion abs; #; ndestruct; ##] *; #; ndestruct;
1056 ##|#a c w; @; nnormalize; 
1057    ##[ *; ##[ *; #defw; nrewrite > defw; #ca; @2;  nrewrite > (eqb_t … ca); @; ##]
1058        #H; ninversion H; #; ndestruct;
1059    ##| *; ##[ *; #; ndestruct; ##] #H; ninversion H; ##[##2,3,4,5,6: #; ndestruct]
1060               #d defw defa; ndestruct; @1; @; //; nrewrite > (eqb_true S d d); //. ##]
1061 ##|#r1 r2 H1 H2 a w; @;
1062    ##[ #H; ncases (in_move_cat … H);
1063       ##[ *; #w1; *; #w2; *; *; #defw w1m w2m;
1064           ncases (H1 a w1); #H1w1; #_; nlapply (H1w1 w1m); #good; 
1065           nrewrite > defw; @2; @2 (a::w1); //; ncases good; ##[ *; #; ndestruct] //.
1066       ##|
1067       ...
1068 ##|
1069 ##|
1070 ##]
1071 nqed.
1072
1073
1074 notation > "x ↦* E" non associative with precedence 60 for @{move_star ? $x $E}.
1075 nlet rec move_star (S : decidable) w E on w : bool × (pre S) ≝
1076  match w with
1077   [ nil ⇒ E
1078   | cons x w' ⇒ w' ↦* (x ↦ \snd E)].
1079
1080 ndefinition in_moves ≝ λS:decidable.λw.λE:bool × (pre S). \fst(w ↦* E).
1081
1082 ncoinductive equiv (S:decidable) : bool × (pre S) → bool × (pre S) → Prop ≝
1083  mk_equiv:
1084   ∀E1,E2: bool × (pre S).
1085    \fst E1  = \fst E2 →
1086     (∀x. equiv S (x ↦ \snd E1) (x ↦ \snd E2)) →
1087      equiv S E1 E2.
1088
1089 ndefinition NAT: decidable.
1090  @ nat eqb; /2/.
1091 nqed.
1092
1093 include "hints_declaration.ma".
1094
1095 alias symbol "hint_decl" (instance 1) = "hint_decl_Type1".
1096 unification hint 0 ≔ ; X ≟ NAT ⊢ carr X ≡ nat.
1097
1098 ninductive unit: Type[0] ≝ I: unit.
1099
1100 nlet corec foo_nop (b: bool):
1101  equiv ?
1102   〈 b, pc ? (ps ? 0) (pk ? (pc ? (ps ? 1) (ps ? 0))) 〉
1103   〈 b, pc ? (pk ? (pc ? (ps ? 0) (ps ? 1))) (ps ? 0) 〉 ≝ ?.
1104  @; //; #x; ncases x
1105   [ nnormalize in ⊢ (??%%); napply (foo_nop false)
1106   | #y; ncases y
1107      [ nnormalize in ⊢ (??%%); napply (foo_nop false)
1108      | #w; nnormalize in ⊢ (??%%); napply (foo_nop false) ]##]
1109 nqed.
1110
1111 (*
1112 nlet corec foo (a: unit):
1113  equiv NAT
1114   (eclose NAT (pc ? (ps ? 0) (pk ? (pc ? (ps ? 1) (ps ? 0)))))
1115   (eclose NAT (pc ? (pk ? (pc ? (ps ? 0) (ps ? 1))) (ps ? 0)))
1116 ≝ ?.
1117  @;
1118   ##[ nnormalize; //
1119   ##| #x; ncases x
1120        [ nnormalize in ⊢ (??%%);
1121          nnormalize in foo: (? → ??%%);
1122          @; //; #y; ncases y
1123            [ nnormalize in ⊢ (??%%); napply foo_nop
1124            | #y; ncases y
1125               [ nnormalize in ⊢ (??%%);
1126                 
1127             ##| #z; nnormalize in ⊢ (??%%); napply foo_nop ]##]
1128      ##| #y; nnormalize in ⊢ (??%%); napply foo_nop
1129   ##]
1130 nqed.
1131 *)
1132
1133 ndefinition test1 : pre ? ≝ ❨ `0 | `1 ❩^* `0.
1134 ndefinition test2 : pre ? ≝ ❨ (`0`1)^* `0 | (`0`1)^* `1 ❩.
1135 ndefinition test3 : pre ? ≝ (`0 (`0`1)^* `1)^*.
1136
1137
1138 nlemma foo: in_moves ? [0;0;1;0;1;1] (ɛ test3) = true.
1139  nnormalize in match test3;
1140  nnormalize;
1141 //;
1142 nqed.
1143
1144 (**********************************************************)
1145
1146 ninductive der (S: Type[0]) (a: S) : re S → re S → CProp[0] ≝
1147    der_z: der S a (z S) (z S)
1148  | der_e: der S a (e S) (z S)
1149  | der_s1: der S a (s S a) (e ?)
1150  | der_s2: ∀b. a ≠ b → der S a (s S b) (z S)
1151  | der_c1: ∀e1,e2,e1',e2'. in_l S [] e1 → der S a e1 e1' → der S a e2 e2' →
1152             der S a (c ? e1 e2) (o ? (c ? e1' e2) e2')
1153  | der_c2: ∀e1,e2,e1'. Not (in_l S [] e1) → der S a e1 e1' →
1154             der S a (c ? e1 e2) (c ? e1' e2)
1155  | der_o: ∀e1,e2,e1',e2'. der S a e1 e1' → der S a e2 e2' →
1156     der S a (o ? e1 e2) (o ? e1' e2').
1157
1158 nlemma eq_rect_CProp0_r:
1159  ∀A.∀a,x.∀p:eq ? x a.∀P: ∀x:A. eq ? x a → CProp[0]. P a (refl A a) → P x p.
1160  #A; #a; #x; #p; ncases p; #P; #H; nassumption.
1161 nqed.
1162
1163 nlemma append1: ∀A.∀a:A.∀l. [a] @ l = a::l. //. nqed.
1164
1165 naxiom in_l1: ∀S,r1,r2,w. in_l S [ ] r1 → in_l S w r2 → in_l S w (c S r1 r2).
1166 (* #S; #r1; #r2; #w; nelim r1
1167   [ #K; ninversion K
1168   | #H1; #H2; napply (in_c ? []); //
1169   | (* tutti casi assurdi *) *)
1170
1171 ninductive in_l' (S: Type[0]) : word S → re S → CProp[0] ≝
1172    in_l_empty1: ∀E.in_l S [] E → in_l' S [] E 
1173  | in_l_cons: ∀a,w,e,e'. in_l' S w e' → der S a e e' → in_l' S (a::w) e.
1174
1175 ncoinductive eq_re (S: Type[0]) : re S → re S → CProp[0] ≝
1176    mk_eq_re: ∀E1,E2.
1177     (in_l S [] E1 → in_l S [] E2) →
1178     (in_l S [] E2 → in_l S [] E1) →
1179     (∀a,E1',E2'. der S a E1 E1' → der S a E2 E2' → eq_re S E1' E2') →
1180       eq_re S E1 E2.
1181
1182 (* serve il lemma dopo? *)
1183 ntheorem eq_re_is_eq: ∀S.∀E1,E2. eq_re S E1 E2 → ∀w. in_l ? w E1 → in_l ? w E2.
1184  #S; #E1; #E2; #H1; #w; #H2; nelim H2 in E2 H1 ⊢ %
1185   [ #r; #K (* ok *)
1186   | #a; #w; #R1; #R2; #K1; #K2; #K3; #R3; #K4; @2 R2; //; ncases K4;
1187
1188 (* IL VICEVERSA NON VALE *)
1189 naxiom in_l_to_in_l: ∀S,w,E. in_l' S w E → in_l S w E.
1190 (* #S; #w; #E; #H; nelim H
1191   [ //
1192   | #a; #w'; #r; #r'; #H1; (* e si cade qua sotto! *)
1193   ]
1194 nqed. *)
1195
1196 ntheorem der1: ∀S,a,e,e',w. der S a e e' → in_l S w e' → in_l S (a::w) e.
1197  #S; #a; #E; #E'; #w; #H; nelim H
1198   [##1,2: #H1; ninversion H1
1199      [##1,8: #_; #K; (* non va ndestruct K; *) ncases (?:False); (* perche' due goal?*) /2/
1200      |##2,9: #X; #Y; #K; ncases (?:False); /2/
1201      |##3,10: #x; #y; #z; #w; #a; #b; #c; #d; #e; #K; ncases (?:False); /2/
1202      |##4,11: #x; #y; #z; #w; #a; #b; #K; ncases (?:False); /2/
1203      |##5,12: #x; #y; #z; #w; #a; #b; #K; ncases (?:False); /2/
1204      |##6,13: #x; #y; #K; ncases (?:False); /2/
1205      |##7,14: #x; #y; #z; #w; #a; #b; #c; #d; #K; ncases (?:False); /2/]
1206 ##| #H1; ninversion H1
1207      [ //
1208      | #X; #Y; #K; ncases (?:False); /2/
1209      | #x; #y; #z; #w; #a; #b; #c; #d; #e; #K; ncases (?:False); /2/
1210      | #x; #y; #z; #w; #a; #b; #K; ncases (?:False); /2/
1211      | #x; #y; #z; #w; #a; #b; #K; ncases (?:False); /2/
1212      | #x; #y; #K; ncases (?:False); /2/
1213      | #x; #y; #z; #w; #a; #b; #c; #d; #K; ncases (?:False); /2/ ]
1214 ##| #H1; #H2; #H3; ninversion H3
1215      [ #_; #K; ncases (?:False); /2/
1216      | #X; #Y; #K; ncases (?:False); /2/
1217      | #x; #y; #z; #w; #a; #b; #c; #d; #e; #K; ncases (?:False); /2/
1218      | #x; #y; #z; #w; #a; #b; #K; ncases (?:False); /2/
1219      | #x; #y; #z; #w; #a; #b; #K; ncases (?:False); /2/
1220      | #x; #y; #K; ncases (?:False); /2/
1221      | #x; #y; #z; #w; #a; #b; #c; #d; #K; ncases (?:False); /2/ ]
1222 ##| #r1; #r2; #r1'; #r2'; #H1; #H2; #H3; #H4; #H5; #H6;
1223