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