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