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