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