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