]> matita.cs.unibo.it Git - helm.git/blob - helm/software/matita/nlibrary/re/re-setoids.ma
28332fbffa74bccccd122ae632992e758c271350
[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 ⇒ ? | _ ⇒ ? ]
29 | cons x xs ⇒ match l2 with [ nil ⇒ ? | cons y ys ⇒ x = y ∧ eq_list ? xs ys]].
30 ##[ napply True|napply False|napply False]nqed.
31    
32 ndefinition LIST : setoid → setoid.
33 #S; @(list S); @(eq_list S); ncases admit; nqed.  
34
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 90 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 (* -------------------------------------------- *) ⊢
299    eq_list S a b ≡ eq_rel (list S) (eq0 R) a b.
300
301 notation > "B ⇒_0 C" right associative with precedence 72 for @{'umorph0 $B $C}.
302 notation > "B ⇒_1 C" right associative with precedence 72 for @{'umorph1 $B $C}.
303 notation "B ⇒\sub 0 C" right associative with precedence 72 for @{'umorph0 $B $C}.
304 notation "B ⇒\sub 1 C" right associative with precedence 72 for @{'umorph1 $B $C}.
305
306 interpretation "unary morphism 0" 'umorph0 A B = (unary_morphism A B).
307 interpretation "unary morphism 1" 'umorph1 A B = (unary_morphism1 A B).
308
309 ncoercion setoid1_of_setoid : ∀s:setoid. setoid1 ≝ setoid1_of_setoid on _s: setoid to setoid1.
310
311 nlemma exists_is_morph: (* BUG *) ∀S,T:setoid.∀P: S ⇒_1 (T ⇒_1 (CProp[0]:?)).
312   ∀y,z:S.y =_0 z → (Ex T (P y)) = (Ex T (P z)).
313 #S T P y z E; @;
314 ##[ *; #x Px; @x; alias symbol "refl" (instance 4) = "refl".
315     alias symbol "prop2" (instance 2) = "prop21".
316     napply (. E^-1‡#); napply Px;
317 ##| *; #x Px; @x; napply (. E‡#); napply Px;##]
318 nqed.
319
320 ndefinition ex_morph : ∀S:setoid. (S ⇒_1 CPROP) ⇒_1 CPROP.
321 #S; @; ##[ #P; napply (Ex ? P); ##| #P1 P2 E; @; 
322 *; #x; #H; @ x; nlapply (E x x ?); //; *; /2/;
323 nqed.
324  
325 nlemma Sig: ∀S,T:setoid.∀P: S → (T → CProp[0]).
326   ∀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)).
327 #S T P y z Q E; @; *; #x Px; @x; nlapply (E x Q); *; /2/; nqed.
328
329
330 nlemma test : ∀S:setoid. ∀ee: S ⇒_1 S ⇒_1 CPROP.
331  ∀x,y:setoid1_of_setoid S.x =_1 y → (Ex S (λw.ee x w ∧ True)) =_1 (Ex S (λw.ee y w ∧ True)).
332 #S m x y E;
333 alias symbol "trans" (instance 1) = "trans1".
334 alias symbol "refl" (instance 5) = "refl".
335 alias symbol "prop2" (instance 3) = "prop21".
336 alias symbol "trans" (instance 1) = "trans1".
337 alias symbol "prop2" (instance 3) = "prop21".
338 (* super bug 1+1: 
339      eseguire senza modificare per ottenrere degli alias, fare back di 1 passo
340      e ri-eseguire. Se riesegue senza aggiungere altri alias allora hai gli 
341      alias giusti (ma se fate back di più passi, gli alias non vanno più bene...).
342      ora (m x w) e True possono essere sostituiti da ?, se invece 
343      si toglie anche l'∧, allora un super bug si scatena (meta contesto locale
344      scazzato, con y al posto di x, unificata con se stessa ma col contesto
345      locale corretto...). lo stesso (o simile) bug salta fuori se esegui
346      senza gli alias giusti con ? al posto di True o (m x w). 
347      
348      bug a parte, pare inferisca tutto lui...
349      la E astratta nella prova è solo per fargli inferire x e y, se Sig
350      lo si riformula in modo più naturale (senza y=z) allora bisogna passare
351      x e y esplicitamente. *)  
352 napply (.= (Sig ? S (λw,x.(m x w) ∧ True) ?? E (λw,E.(E‡#)‡#))); 
353 //; nqed.
354 STOP
355 napply (λw:S.(.= ((E‡#)‡#)) #); ##[##2: napply w| napply m. #H; napply H;
356
357  
358  
359  let form ≝ comp1_unary_morphisms ??? (ex_morph (list S)) ee in
360   form x = form y.
361  #S ee x y E;
362  nletin F ≝ (comp1_unary_morphisms ??? (ex_morph (list S)) ee);
363  
364  nnormalize;
365   nchange in E with (eq_rel1 ? (eq1 (setoid1_of_setoid (LIST S))) x y);
366
367  ncheck (exists_is_morph (LIST S) (LIST S) ? ?? (E‡#)).
368  nletin xxx ≝ (exists_is_morph); (LIST S)); (LIST S) ee x y E);
369  
370  nchange with (F x = F y);
371  nchange in E with (eq_rel1 ? (eq1 (setoid1_of_setoid (LIST S))) x y);
372  napply (.= † E);
373  napply #.
374 nqed.
375
376
377 E : w = w2
378   
379   
380   Σ(λx.(#‡E)‡#) : ∃x.x = w ∧ m → ∃x.x = w2 ∧ m 
381   λx.(#‡E)‡#    : ∀x.x = w ∧ m → x = w2 ∧ m 
382
383
384 w;
385 F ≟ ex_moprh ∘ G
386 g ≟ fun11 G
387 ------------------------------
388 ex (λx.g w x) ==?== fun11 F w
389
390 x ⊢ fun11 ?h ≟ λw. ?g w x
391 ?G ≟ morphism_leibniz (T → S) ∘ ?h 
392 ------------------------------
393 (λw. (λx:T.?g w x)) ==?== fun11 ?G
394
395 ...
396 x ⊢ fun11 ?h ==?== λw. eq x w ∧ m [w] 
397 (λw,x.eq x w ∧ m[w]) ==?== fun11 ?G
398 ex (λx.?g w x) ==?== ex (λx.x = w ∧ m[w])
399
400 ndefinition ex_morph : ∀S:setoid. (S ⇒_1 CPROP) ⇒_1 CPROP.
401 #S; @; ##[ #P; napply (Ex ? P); ##| ncases admit. ##] nqed.
402
403 ndefinition ex_morph1 : ∀S:setoid. (S ⇒_1 CPROP) ⇒_1 CPROP.
404 #S; @; ##[ #P; napply (Ex ? (λx.P); ##| ncases admit. ##] nqed.
405
406
407 nlemma d : ∀S:Alpha.
408  ∀ee: (setoid1_of_setoid (list S)) ⇒_1 (setoid1_of_setoid (list S)) ⇒_1 CPROP.
409  ∀x,y:list S.x = y →
410  let form ≝ comp1_unary_morphisms ??? (ex_morph (list S)) ee in
411   form x = form y.
412  #S ee x y E;
413  nletin F ≝ (comp1_unary_morphisms ??? (ex_morph (list S)) ee);
414  
415  nnormalize;
416  
417  nchange with (F x = F y);
418  nchange in E with (eq_rel1 ? (eq1 (setoid1_of_setoid (LIST S))) x y);
419  napply (.= † E);
420  napply #.
421 nqed.
422
423
424 nlemma d : ∀S:Alpha.∀ee: (setoid1_of_setoid (list S)) ⇒_1 (setoid1_of_setoid (list S)) ⇒_1 CPROP.∀x,y:list S.x = y →
425  let form ≝ comp1_unary_morphisms ??? (ex_morph (list S)) ee in
426   form x = form y.
427  #S ee x y E;
428  nletin F ≝ (comp1_unary_morphisms ??? (ex_morph (list S)) ee);
429  
430  nnormalize;
431  
432  nchange with (F x = F y);
433  nchange in E with (eq_rel1 ? (eq1 (setoid1_of_setoid (LIST S))) x y);
434  napply (.= † E);
435  napply #.
436 nqed.
437  
438   
439 nlemma d : ∀S:Alpha.(setoid1_of_setoid (list S)) ⇒_1 CPROP.
440  #S; napply (comp1_unary_morphisms ??? (ex_morph (list S)) ?);
441  napply (eq1).
442   
443   
444   
445 (*
446 ndefinition comp_ex : ∀X,S,T,K.∀P:X ⇒_1 (S ⇒_1 T).∀Pc : (S ⇒_1 T) ⇒_1 K. X ⇒_1 K.
447  *)
448
449 ndefinition L_pi_ext : ∀S:Alpha.∀r:pitem S.Elang S.
450 #S r; @(𝐋\p r); #w1 w2 E; nelim r; /2/;
451 ##[ #x; @; #H; ##[ nchange in H ⊢ % with ([?]=?); napply ((.= H) E)]
452     nchange in H ⊢ % with ([?]=?); napply ((.= H) E^-1);
453 ##| #e1 e2 H1 H2; 
454     nchange in match (w1 ∈ 𝐋\p (?·?)) with ((∃_.?)∨?);
455     napply (.= (#‡H2));
456     napply (.= (Eexists ?? ? w1 w2 E)‡#);
457     
458     
459     nchange in match (w2 ∈ 𝐋\p (?·?)) with (?∨?);
460     napply (.= 
461     
462
463  //; napply (trans ?? ??? H E);
464    napply (trans (list S) (eq0 (LIST S)) ? [?] ?(*w1 [x] w2*) H E); 
465   nlapply (trans (list S) (eq0 (LIST S))).
466
467 napply (.= H); nnormalize; nlapply (trans ? [x] w1 w2 E H); napply (.= ?) [napply (w1 = [x])] ##[##2: napply #; napply sym1; napply refl1 ]
468
469 ndefinition epsilon ≝ 
470   λS:Alpha.λb.match b return λ_.lang S with [ true ⇒ { [ ] } | _ ⇒ ∅ ].
471
472 interpretation "epsilon" 'epsilon = (epsilon ?).
473 notation < "ϵ b" non associative with precedence 90 for @{'app_epsilon $b}.
474 interpretation "epsilon lang" 'app_epsilon b = (epsilon ? b).
475
476 ndefinition L_pr ≝ λS : Alpha.λp:pre S.  𝐋\p\ (\fst p) ∪ ϵ (\snd p).
477   
478 interpretation "L_pr" 'L_pi E = (L_pr ? E).
479
480 nlemma append_eq_nil : ∀S:setoid.∀w1,w2:list S. w1 @ w2 = [ ] → w1 = [ ].
481 #S w1; ncases w1; //. nqed.
482
483 (* lemma 12 *)
484 nlemma epsilon_in_true : ∀S:Alpha.∀e:pre S. [ ] ∈ 𝐋\p e = (\snd e = true).
485 #S r; ncases r; #e b; @; ##[##2: #H; nrewrite > H; @2; /2/; ##] ncases b;//; 
486 *; ##[##2:*] nelim e;
487 ##[ ##1,2: *; ##| #c; *; ##| #c; *| ##7: #p H;
488 ##| #r1 r2 H G; *; ##[##2: nassumption; ##]
489 ##| #r1 r2 H1 H2; *; /2/ by {}]
490 *; #w1; *; #w2; *; *; 
491 ##[ #defw1 H1 foo; napply H; napply (. #‡#); (append_eq_nil … defw1)^-1‡#);
492
493  nrewrite > (append_eq_nil ? … w1 w2 …); /3/ by {};//;
494 nqed.
495
496 nlemma not_epsilon_lp : ∀S.∀e:pitem S. ¬ (𝐋\p e [ ]).
497 #S e; nelim e; nnormalize; /2/ by nmk;
498 ##[ #; @; #; ndestruct;
499 ##| #r1 r2 n1 n2; @; *; /2/; *; #w1; *; #w2; *; *; #H;
500     nrewrite > (append_eq_nil …H…); /2/;
501 ##| #r1 r2 n1 n2; @; *; /2/;
502 ##| #r n; @; *; #w1; *; #w2; *; *; #H;     
503     nrewrite > (append_eq_nil …H…); /2/;##]
504 nqed.
505
506 ndefinition lo ≝ λS:Alpha.λa,b:pre S.〈\fst a + \fst b,\snd a || \snd b〉.
507 notation "a ⊕ b" left associative with precedence 60 for @{'oplus $a $b}.
508 interpretation "oplus" 'oplus a b = (lo ? a b).
509
510 ndefinition lc ≝ λS:Alpha.λbcast:∀S:Alpha.∀E:pitem S.pre S.λa,b:pre S.
511    match a with [ mk_pair e1 b1 ⇒
512    match b1 with 
513    [ false ⇒ 〈e1 · \fst b, \snd b〉 
514    | true ⇒ 〈e1 · \fst (bcast ? (\fst b)),\snd b || \snd (bcast ? (\fst b))〉]].
515    
516 notation < "a ⊙ b" left associative with precedence 60 for @{'lc $op $a $b}.
517 interpretation "lc" 'lc op a b = (lc ? op a b).
518 notation > "a ⊙ b" left associative with precedence 60 for @{'lc eclose $a $b}.
519
520 ndefinition lk ≝ λS:Alpha.λbcast:∀S:Alpha.∀E:pitem S.pre S.λa:pre S.
521    match a with [ mk_pair e1 b1 ⇒
522    match b1 with 
523    [ false ⇒ 〈e1^*, false〉 
524    | true ⇒ 〈(\fst (bcast ? e1))^*, true〉]].
525
526 notation < "a \sup ⊛" non associative with precedence 90 for @{'lk $op $a}.
527 interpretation "lk" 'lk op a = (lk ? op a).
528 notation > "a^⊛" non associative with precedence 90 for @{'lk eclose $a}.
529
530 notation > "•" non associative with precedence 60 for @{eclose ?}.
531 nlet rec eclose (S: Alpha) (E: pitem S) on E : pre S ≝
532  match E with
533   [ pz ⇒ 〈 ∅, false 〉
534   | pe ⇒ 〈 ϵ,  true 〉
535   | ps x ⇒ 〈 `.x, false 〉
536   | pp x ⇒ 〈 `.x, false 〉
537   | po E1 E2 ⇒ •E1 ⊕ •E2
538   | pc E1 E2 ⇒ •E1 ⊙ 〈 E2, false 〉 
539   | pk E ⇒ 〈(\fst (•E))^*,true〉].
540 notation < "• x" non associative with precedence 60 for @{'eclose $x}.
541 interpretation "eclose" 'eclose x = (eclose ? x).
542 notation > "• x" non associative with precedence 60 for @{'eclose $x}.
543
544 ndefinition reclose ≝ λS:Alpha.λp:pre S.let p' ≝ •\fst p in 〈\fst p',\snd p || \snd p'〉.
545 interpretation "reclose" 'eclose x = (reclose ? x).
546
547 ndefinition eq_f1 ≝ λS.λa,b:word S → Prop.∀w.a w ↔ b w.
548 notation > "A =1 B" non associative with precedence 45 for @{'eq_f1 $A $B}.
549 notation "A =\sub 1 B" non associative with precedence 45 for @{'eq_f1 $A $B}.
550 interpretation "eq f1" 'eq_f1 a b = (eq_f1 ? a b).
551
552 naxiom extP : ∀S.∀p,q:word S → Prop.(p =1 q) → p = q.
553
554 nlemma epsilon_or : ∀S:Alpha.∀b1,b2. ϵ(b1 || b2) = ϵ b1 ∪ ϵ b2. ##[##2: napply S]
555 #S b1 b2; ncases b1; ncases b2; napply extP; #w; nnormalize; @; /2/; *; //; *;
556 nqed.
557
558 nlemma cupA : ∀S.∀a,b,c:word S → Prop.a ∪ b ∪ c = a ∪ (b ∪ c).
559 #S a b c; napply extP; #w; nnormalize; @; *; /3/; *; /3/; nqed.
560
561 nlemma cupC : ∀S. ∀a,b:word S → Prop.a ∪ b = b ∪ a.
562 #S a b; napply extP; #w; @; *; nnormalize; /2/; nqed.
563
564 (* theorem 16: 2 *)
565 nlemma oplus_cup : ∀S:Alpha.∀e1,e2:pre S.𝐋\p (e1 ⊕ e2) = 𝐋\p e1 ∪ 𝐋\p e2.
566 #S r1; ncases r1; #e1 b1 r2; ncases r2; #e2 b2;
567 nwhd in ⊢ (??(??%)?);
568 nchange in ⊢(??%?) with (𝐋\p (e1 + e2) ∪ ϵ (b1 || b2));
569 nchange in ⊢(??(??%?)?) with (𝐋\p (e1) ∪ 𝐋\p (e2));
570 nrewrite > (epsilon_or S …); nrewrite > (cupA S (𝐋\p e1) …);
571 nrewrite > (cupC ? (ϵ b1) …); nrewrite < (cupA S (𝐋\p e2) …);
572 nrewrite > (cupC ? ? (ϵ b1) …); nrewrite < (cupA …); //;
573 nqed.
574
575 nlemma odotEt : 
576   ∀S.∀e1,e2:pitem S.∀b2. 〈e1,true〉 ⊙ 〈e2,b2〉 = 〈e1 · \fst (•e2),b2 || \snd (•e2)〉.
577 #S e1 e2 b2; nnormalize; ncases (•e2); //; nqed.
578
579 nlemma LcatE : ∀S.∀e1,e2:pitem S.𝐋\p (e1 · e2) =  𝐋\p e1 · 𝐋 .|e2| ∪ 𝐋\p e2. //; nqed.
580
581 nlemma cup_dotD : ∀S.∀p,q,r:word S → Prop.(p ∪ q) · r = (p · r) ∪ (q · r). 
582 #S p q r; napply extP; #w; nnormalize; @; 
583 ##[ *; #x; *; #y; *; *; #defw; *; /7/ by or_introl, or_intror, ex_intro, conj;
584 ##| *; *; #x; *; #y; *; *; /7/ by or_introl, or_intror, ex_intro, conj; ##]
585 nqed.
586
587 nlemma cup0 :∀S.∀p:word S → Prop.p ∪ {} = p.
588 #S p; napply extP; #w; nnormalize; @; /2/; *; //; *; nqed.
589
590 nlemma erase_dot : ∀S.∀e1,e2:pitem S.𝐋 .|e1 · e2| =  𝐋 .|e1| · 𝐋 .|e2|.
591 #S e1 e2; napply extP; nnormalize; #w; @; *; #w1; *; #w2; *; *; /7/ by ex_intro, conj;
592 nqed.
593
594 nlemma erase_plus : ∀S.∀e1,e2:pitem S.𝐋 .|e1 + e2| =  𝐋 .|e1| ∪ 𝐋 .|e2|.
595 #S e1 e2; napply extP; nnormalize; #w; @; *; /4/ by or_introl, or_intror; nqed.
596
597 nlemma erase_star : ∀S.∀e1:pitem S.𝐋 .|e1|^* = 𝐋 .|e1^*|. //; nqed.
598
599 ndefinition substract := λS.λp,q:word S → Prop.λw.p w ∧ ¬ q w.
600 interpretation "substract" 'minus a b = (substract ? a b).
601
602 nlemma cup_sub: ∀S.∀a,b:word S → Prop. ¬ (a []) → a ∪ (b - {[]}) = (a ∪ b) - {[]}.
603 #S a b c; napply extP; #w; nnormalize; @; *; /4/; *; /4/; nqed.
604
605 nlemma sub0 : ∀S.∀a:word S → Prop. a - {} = a.
606 #S a; napply extP; #w; nnormalize; @; /3/; *; //; nqed.
607
608 nlemma subK : ∀S.∀a:word S → Prop. a - a = {}.
609 #S a; napply extP; #w; nnormalize; @; *; /2/; nqed.
610
611 nlemma subW : ∀S.∀a,b:word S → Prop.∀w.(a - b) w → a w.
612 #S a b w; nnormalize; *; //; nqed.
613
614 nlemma erase_bull : ∀S.∀a:pitem S. .|\fst (•a)| = .|a|.
615 #S a; nelim a; // by {};
616 ##[ #e1 e2 IH1 IH2; nchange in ⊢ (???%) with (.|e1| · .|e2|);
617     nrewrite < IH1; nrewrite < IH2;  
618     nchange in ⊢ (??(??%)?) with (\fst (•e1 ⊙ 〈e2,false〉));
619     ncases (•e1); #e3 b; ncases b; nnormalize;
620     ##[ ncases (•e2); //; ##| nrewrite > IH2; //]
621 ##| #e1 e2 IH1 IH2; nchange in ⊢ (???%) with (.|e1| + .|e2|);
622     nrewrite < IH2; nrewrite < IH1;
623     nchange in ⊢ (??(??%)?) with (\fst (•e1 ⊕ •e2));
624     ncases (•e1); ncases (•e2); //;
625 ##| #e IH; nchange in ⊢ (???%) with (.|e|^* ); nrewrite < IH;
626     nchange in ⊢ (??(??%)?) with (\fst (•e))^*; //; ##]
627 nqed.
628
629 nlemma eta_lp : ∀S.∀p:pre S.𝐋\p p = 𝐋\p 〈\fst p, \snd p〉.
630 #S p; ncases p; //; nqed.
631
632 nlemma epsilon_dot: ∀S.∀p:word S → Prop. {[]} · p = p. 
633 #S e; napply extP; #w; nnormalize; @; ##[##2: #Hw; @[]; @w; /3/; ##]
634 *; #w1; *; #w2; *; *; #defw defw1 Hw2; nrewrite < defw; nrewrite < defw1;
635 napply Hw2; nqed.
636
637 (* theorem 16: 1 → 3 *)
638 nlemma odot_dot_aux : ∀S.∀e1,e2: pre S.
639       𝐋\p (•(\fst e2)) =  𝐋\p (\fst e2) ∪ 𝐋 .|\fst e2| → 
640          𝐋\p (e1 ⊙ e2) =  𝐋\p e1 · 𝐋 .|\fst e2| ∪ 𝐋\p e2.
641 #S e1 e2 th1; ncases e1; #e1' b1'; ncases b1';
642 ##[ nwhd in ⊢ (??(??%)?); nletin e2' ≝ (\fst e2); nletin b2' ≝ (\snd e2); 
643     nletin e2'' ≝ (\fst (•(\fst e2))); nletin b2'' ≝ (\snd (•(\fst e2)));
644     nchange in ⊢ (??%?) with (?∪?); 
645     nchange in ⊢ (??(??%?)?) with (?∪?);
646     nchange in match (𝐋\p 〈?,?〉) with (?∪?);
647     nrewrite > (epsilon_or …); nrewrite > (cupC ? (ϵ ?)…);
648     nrewrite > (cupA …);nrewrite < (cupA ?? (ϵ?)…);
649     nrewrite > (?: 𝐋\p e2'' ∪ ϵ b2'' = 𝐋\p e2' ∪ 𝐋 .|e2'|); ##[##2:
650       nchange with (𝐋\p 〈e2'',b2''〉 =  𝐋\p e2' ∪ 𝐋 .|e2'|); 
651       ngeneralize in match th1;
652       nrewrite > (eta_lp…); #th1; nrewrite > th1; //;##]
653     nrewrite > (eta_lp ? e2); 
654     nchange in match (𝐋\p 〈\fst e2,?〉) with (𝐋\p e2'∪ ϵ b2');
655     nrewrite > (cup_dotD …); nrewrite > (epsilon_dot…);       
656     nrewrite > (cupC ? (𝐋\p e2')…); nrewrite > (cupA…);nrewrite > (cupA…);
657     nrewrite < (erase_bull S e2') in ⊢ (???(??%?)); //;
658 ##| ncases e2; #e2' b2'; nchange in match (〈e1',false〉⊙?) with 〈?,?〉;
659     nchange in match (𝐋\p ?) with (?∪?);
660     nchange in match (𝐋\p (e1'·?)) with (?∪?);
661     nchange in match (𝐋\p 〈e1',?〉) with (?∪?);
662     nrewrite > (cup0…); 
663     nrewrite > (cupA…); //;##]
664 nqed.
665
666 nlemma sub_dot_star : 
667   ∀S.∀X:word S → Prop.∀b. (X - ϵ b) · X^* ∪ {[]} = X^*.
668 #S X b; napply extP; #w; @;
669 ##[ *; ##[##2: nnormalize; #defw; nrewrite < defw; @[]; @; //]
670     *; #w1; *; #w2; *; *; #defw sube; *; #lw; *; #flx cj;
671     @ (w1 :: lw); nrewrite < defw; nrewrite < flx; @; //;
672     @; //; napply (subW … sube);
673 ##| *; #wl; *; #defw Pwl; nrewrite < defw; nelim wl in Pwl; ##[ #_; @2; //]
674     #w' wl' IH; *; #Pw' IHp; nlapply (IH IHp); *;
675     ##[ *; #w1; *; #w2; *; *; #defwl' H1 H2;
676         @; ncases b in H1; #H1; 
677         ##[##2: nrewrite > (sub0…); @w'; @(w1@w2);
678                 nrewrite > (associative_append ? w' w1 w2);
679                 nrewrite > defwl'; @; ##[@;//] @(wl'); @; //;
680            ##| ncases w' in Pw';
681                ##[ #ne; @w1; @w2; nrewrite > defwl'; @; //; @; //;
682                ##| #x xs Px; @(x::xs); @(w1@w2); 
683                    nrewrite > (defwl'); @; ##[@; //; @; //; @; nnormalize; #; ndestruct]
684                    @wl'; @; //; ##] ##]
685         ##| #wlnil; nchange in match (flatten ? (w'::wl')) with (w' @ flatten ? wl');
686             nrewrite < (wlnil); nrewrite > (append_nil…); ncases b;
687             ##[ ncases w' in Pw'; /2/; #x xs Pxs; @; @(x::xs); @([]);
688                 nrewrite > (append_nil…); @; ##[ @; //;@; //; nnormalize; @; #; ndestruct]
689                 @[]; @; //;
690             ##| @; @w'; @[]; nrewrite > (append_nil…); @; ##[##2: @[]; @; //] 
691                 @; //; @; //; @; *;##]##]##] 
692 nqed.
693
694 (* theorem 16: 1 *)
695 alias symbol "pc" (instance 13) = "cat lang".
696 alias symbol "in_pl" (instance 23) = "in_pl".
697 alias symbol "in_pl" (instance 5) = "in_pl".
698 alias symbol "eclose" (instance 21) = "eclose".
699 ntheorem bull_cup : ∀S:Alpha. ∀e:pitem S.  𝐋\p (•e) =  𝐋\p e ∪ 𝐋 .|e|.
700 #S e; nelim e; //;
701   ##[ #a; napply extP; #w; nnormalize; @; *; /3/ by or_introl, or_intror;
702   ##| #a; napply extP; #w; nnormalize; @; *; /3/ by or_introl; *;
703   ##| #e1 e2 IH1 IH2;  
704       nchange in ⊢ (??(??(%))?) with (•e1 ⊙ 〈e2,false〉);
705       nrewrite > (odot_dot_aux S (•e1) 〈e2,false〉 IH2);
706       nrewrite > (IH1 …); nrewrite > (cup_dotD …);
707       nrewrite > (cupA …); nrewrite > (cupC ?? (𝐋\p ?) …);
708       nchange in match (𝐋\p 〈?,?〉) with (𝐋\p e2 ∪ {}); nrewrite > (cup0 …);
709       nrewrite < (erase_dot …); nrewrite < (cupA …); //;
710   ##| #e1 e2 IH1 IH2;
711       nchange in match (•(?+?)) with (•e1 ⊕ •e2); nrewrite > (oplus_cup …);
712       nrewrite > (IH1 …); nrewrite > (IH2 …); nrewrite > (cupA …);
713       nrewrite > (cupC ? (𝐋\p e2)…);nrewrite < (cupA ??? (𝐋\p e2)…);
714       nrewrite > (cupC ?? (𝐋\p e2)…); nrewrite < (cupA …); 
715       nrewrite < (erase_plus …); //.
716   ##| #e; nletin e' ≝ (\fst (•e)); nletin b' ≝ (\snd (•e)); #IH;
717       nchange in match (𝐋\p ?) with  (𝐋\p 〈e'^*,true〉);
718       nchange in match (𝐋\p ?) with (𝐋\p (e'^* ) ∪ {[ ]});
719       nchange in ⊢ (??(??%?)?) with (𝐋\p e' · 𝐋 .|e'|^* );
720       nrewrite > (erase_bull…e);
721       nrewrite > (erase_star …);
722       nrewrite > (?: 𝐋\p e' =  𝐋\p e ∪ (𝐋 .|e| - ϵ b')); ##[##2:
723         nchange in IH : (??%?) with (𝐋\p e' ∪ ϵ b'); ncases b' in IH; 
724         ##[ #IH; nrewrite > (cup_sub…); //; nrewrite < IH; 
725             nrewrite < (cup_sub…); //; nrewrite > (subK…); nrewrite > (cup0…);//;
726         ##| nrewrite > (sub0 …); #IH; nrewrite < IH; nrewrite > (cup0 …);//; ##]##]
727       nrewrite > (cup_dotD…); nrewrite > (cupA…); 
728       nrewrite > (?: ((?·?)∪{[]} = 𝐋 .|e^*|)); //;
729       nchange in match (𝐋 .|e^*|) with ((𝐋. |e|)^* ); napply sub_dot_star;##]
730  nqed.
731
732 (* theorem 16: 3 *)      
733 nlemma odot_dot: 
734   ∀S.∀e1,e2: pre S.  𝐋\p (e1 ⊙ e2) =  𝐋\p e1 · 𝐋 .|\fst e2| ∪ 𝐋\p e2.
735 #S e1 e2; napply odot_dot_aux; napply (bull_cup S (\fst e2)); nqed.
736
737 nlemma dot_star_epsilon : ∀S.∀e:re S.𝐋 e · 𝐋 e^* ∪ {[]} =  𝐋 e^*.
738 #S e; napply extP; #w; nnormalize; @;
739 ##[ *; ##[##2: #H; nrewrite < H; @[]; /3/] *; #w1; *; #w2; 
740     *; *; #defw Hw1; *; #wl; *; #defw2 Hwl; @(w1 :: wl);
741     nrewrite < defw; nrewrite < defw2; @; //; @;//;
742 ##| *; #wl; *; #defw Hwl; ncases wl in defw Hwl; ##[#defw; #; @2; nrewrite < defw; //]
743     #x xs defw; *; #Hx Hxs; @; @x; @(flatten ? xs); nrewrite < defw;
744     @; /2/; @xs; /2/;##]
745  nqed.
746
747 nlemma nil_star : ∀S.∀e:re S. [ ] ∈ e^*.
748 #S e; @[]; /2/; nqed.
749
750 nlemma cupID : ∀S.∀l:word S → Prop.l ∪ l = l.
751 #S l; napply extP; #w; @; ##[*]//; #; @; //; nqed.
752
753 nlemma cup_star_nil : ∀S.∀l:word S → Prop. l^* ∪ {[]} = l^*.
754 #S a; napply extP; #w; @; ##[*; //; #H; nrewrite < H; @[]; @; //] #;@; //;nqed.
755
756 nlemma rcanc_sing : ∀S.∀A,C:word S → Prop.∀b:word S .
757   ¬ (A b) → A ∪ { (b) } = C → A = C - { (b) }.
758 #S A C b nbA defC; nrewrite < defC; napply extP; #w; @;
759 ##[ #Aw; /3/| *; *; //; #H nH; ncases nH; #abs; nlapply (abs H); *]
760 nqed.
761
762 (* theorem 16: 4 *)      
763 nlemma star_dot: ∀S.∀e:pre S. 𝐋\p (e^⊛) = 𝐋\p e · (𝐋 .|\fst e|)^*.
764 #S p; ncases p; #e b; ncases b;
765 ##[ nchange in match (〈e,true〉^⊛) with 〈?,?〉;
766     nletin e' ≝ (\fst (•e)); nletin b' ≝ (\snd (•e));
767     nchange in ⊢ (??%?) with (?∪?);
768     nchange in ⊢ (??(??%?)?) with (𝐋\p e' · 𝐋 .|e'|^* );
769     nrewrite > (?: 𝐋\p e' = 𝐋\p e ∪ (𝐋 .|e| - ϵ b' )); ##[##2:
770       nlapply (bull_cup ? e); #bc;
771       nchange in match (𝐋\p (•e)) in bc with (?∪?);
772       nchange in match b' in bc with b';
773       ncases b' in bc; ##[##2: nrewrite > (cup0…); nrewrite > (sub0…); //]
774       nrewrite > (cup_sub…); ##[napply rcanc_sing] //;##]
775     nrewrite > (cup_dotD…); nrewrite > (cupA…);nrewrite > (erase_bull…);
776     nrewrite > (sub_dot_star…);
777     nchange in match (𝐋\p 〈?,?〉) with (?∪?);
778     nrewrite > (cup_dotD…); nrewrite > (epsilon_dot…); //;    
779 ##| nwhd in match (〈e,false〉^⊛); nchange in match (𝐋\p 〈?,?〉) with (?∪?);
780     nrewrite > (cup0…);
781     nchange in ⊢ (??%?) with (𝐋\p e · 𝐋 .|e|^* );
782     nrewrite < (cup0 ? (𝐋\p e)); //;##]
783 nqed.
784
785 nlet rec pre_of_re (S : Alpha) (e : re S) on e : pitem S ≝ 
786   match e with 
787   [ z ⇒ pz ?
788   | e ⇒ pe ?
789   | s x ⇒ ps ? x
790   | c e1 e2 ⇒ pc ? (pre_of_re ? e1) (pre_of_re ? e2)
791   | o e1 e2 ⇒ po ? (pre_of_re ? e1) (pre_of_re ? e2)
792   | k e1 ⇒ pk ? (pre_of_re ? e1)].
793
794 nlemma notFalse : ¬False. @; //; nqed.
795
796 nlemma dot0 : ∀S.∀A:word S → Prop. {} · A = {}.
797 #S A; nnormalize; napply extP; #w; @; ##[##2: *]
798 *; #w1; *; #w2; *; *; //; nqed.
799
800 nlemma Lp_pre_of_re : ∀S.∀e:re S. 𝐋\p (pre_of_re ? e) = {}.
801 #S e; nelim e; ##[##1,2,3: //]
802 ##[ #e1 e2 H1 H2; nchange in match (𝐋\p (pre_of_re S (e1 e2))) with (?∪?);
803     nrewrite > H1; nrewrite > H2; nrewrite > (dot0…); nrewrite > (cupID…);//
804 ##| #e1 e2 H1 H2; nchange in match (𝐋\p (pre_of_re S (e1+e2))) with (?∪?);
805     nrewrite > H1; nrewrite > H2; nrewrite > (cupID…); //
806 ##| #e1 H1; nchange in match (𝐋\p (pre_of_re S (e1^* ))) with (𝐋\p (pre_of_re ??) · ?);
807     nrewrite > H1; napply dot0; ##]
808 nqed.
809
810 nlemma erase_pre_of_reK : ∀S.∀e. 𝐋 .|pre_of_re S e| = 𝐋 e.
811 #S A; nelim A; //; 
812 ##[ #e1 e2 H1 H2; nchange in match (𝐋 (e1 · e2)) with (𝐋 e1·?);
813     nrewrite < H1; nrewrite < H2; //
814 ##| #e1 e2 H1 H2; nchange in match (𝐋 (e1 + e2)) with (𝐋 e1 ∪ ?);
815     nrewrite < H1; nrewrite < H2; //
816 ##| #e1 H1; nchange in match (𝐋  (e1^* )) with ((𝐋 e1)^* );
817     nrewrite < H1; //]
818 nqed.     
819
820 (* corollary 17 *)
821 nlemma L_Lp_bull : ∀S.∀e:re S.𝐋 e = 𝐋\p (•pre_of_re ? e).
822 #S e; nrewrite > (bull_cup…); nrewrite > (Lp_pre_of_re…);
823 nrewrite > (cupC…); nrewrite > (cup0…); nrewrite > (erase_pre_of_reK…); //;
824 nqed.
825
826 nlemma Pext : ∀S.∀f,g:word S → Prop. f = g → ∀w.f w → g w.
827 #S f g H; nrewrite > H; //; nqed.
828  
829 (* corollary 18 *)
830 ntheorem bull_true_epsilon : ∀S.∀e:pitem S. \snd (•e) = true ↔ [ ] ∈ .|e|.
831 #S e; @;
832 ##[ #defsnde; nlapply (bull_cup ? e); nchange in match (𝐋\p (•e)) with (?∪?);
833     nrewrite > defsnde; #H; 
834     nlapply (Pext ??? H [ ] ?); ##[ @2; //] *; //;
835     E MO?
836
837 STOP
838
839 notation > "\move term 90 x term 90 E" 
840 non associative with precedence 60 for @{move ? $x $E}.
841 nlet rec move (S: Alpha) (x:S) (E: pitem S) on E : pre S ≝
842  match E with
843   [ pz ⇒ 〈 ∅, false 〉
844   | pe ⇒ 〈 ϵ, false 〉
845   | ps y ⇒ 〈 `y, false 〉
846   | pp y ⇒ 〈 `y, x == y 〉
847   | po e1 e2 ⇒ \move x e1 ⊕ \move x e2 
848   | pc e1 e2 ⇒ \move x e1 ⊙ \move x e2
849   | pk e ⇒ (\move x e)^⊛ ].
850 notation < "\move\shy x\shy E" non associative with precedence 60 for @{'move $x $E}.
851 notation > "\move term 90 x term 90 E" non associative with precedence 60 for @{'move $x $E}.
852 interpretation "move" 'move x E = (move ? x E).
853
854 ndefinition rmove ≝ λS:Alpha.λx:S.λe:pre S. \move x (\fst e).
855 interpretation "rmove" 'move x E = (rmove ? x E).
856
857 nlemma XXz :  ∀S:Alpha.∀w:word S. w .∈ ∅ → False.
858 #S w abs; ninversion abs; #; ndestruct;
859 nqed.
860
861
862 nlemma XXe :  ∀S:Alpha.∀w:word S. w .∈ ϵ → False.
863 #S w abs; ninversion abs; #; ndestruct;
864 nqed.
865
866 nlemma XXze :  ∀S:Alpha.∀w:word S. w .∈ (∅ · ϵ)  → False.
867 #S w abs; ninversion abs; #; ndestruct; /2/ by XXz,XXe;
868 nqed.
869
870
871 naxiom in_move_cat:
872  ∀S.∀w:word S.∀x.∀E1,E2:pitem S. w .∈ \move x (E1 · E2) → 
873    (∃w1.∃w2. w = w1@w2 ∧ w1 .∈ \move x E1 ∧ w2 ∈ .|E2|) ∨ w .∈ \move x E2.
874 #S w x e1 e2 H; nchange in H with (w .∈ \move x e1 ⊙ \move x e2);
875 ncases e1 in H; ncases e2;
876 ##[##1: *; ##[*; nnormalize; #; ndestruct] 
877    #H; ninversion H; ##[##1,4,5,6: nnormalize; #; ndestruct]
878    nnormalize; #; ndestruct; ncases (?:False); /2/ by XXz,XXze;
879 ##|##2: *; ##[*; nnormalize; #; ndestruct] 
880    #H; ninversion H; ##[##1,4,5,6: nnormalize; #; ndestruct]
881    nnormalize; #; ndestruct; ncases (?:False); /2/ by XXz,XXze;
882 ##| #r; *; ##[ *; nnormalize; #; ndestruct] 
883    #H; ninversion H; ##[##1,4,5,6: nnormalize; #; ndestruct]
884    ##[##2: nnormalize; #; ndestruct; @2; @2; //.##]
885    nnormalize; #; ndestruct; ncases (?:False); /2/ by XXz;
886 ##| #y; *; ##[ *; nnormalize; #defw defx; ndestruct; @2; @1; /2/ by conj;##]
887    #H; ninversion H; nnormalize; #; ndestruct; 
888    ##[ncases (?:False); /2/ by XXz] /3/ by or_intror;
889 ##| #r1 r2; *; ##[ *; #defw]
890     ...
891 nqed.
892
893 ntheorem move_ok:
894  ∀S:Alpha.∀E:pre S.∀a,w.w .∈ \move a E ↔ (a :: w) .∈ E. 
895 #S E; ncases E; #r b; nelim r;
896 ##[##1,2: #a w; @; 
897    ##[##1,3: nnormalize; *; ##[##1,3: *; #; ndestruct; ##| #abs; ncases (XXz … abs); ##]
898       #H; ninversion H; #; ndestruct;
899    ##|##*:nnormalize; *; ##[##1,3: *; #; ndestruct; ##| #H1; ncases (XXz … H1); ##]
900        #H; ninversion H; #; ndestruct;##]
901 ##|#a c w; @; nnormalize; ##[*; ##[*; #; ndestruct; ##] #abs; ninversion abs; #; ndestruct;##]
902    *; ##[##2: #abs; ninversion abs; #; ndestruct; ##] *; #; ndestruct;
903 ##|#a c w; @; nnormalize; 
904    ##[ *; ##[ *; #defw; nrewrite > defw; #ca; @2;  nrewrite > (eqb_t … ca); @; ##]
905        #H; ninversion H; #; ndestruct;
906    ##| *; ##[ *; #; ndestruct; ##] #H; ninversion H; ##[##2,3,4,5,6: #; ndestruct]
907               #d defw defa; ndestruct; @1; @; //; nrewrite > (eqb_true S d d); //. ##]
908 ##|#r1 r2 H1 H2 a w; @;
909    ##[ #H; ncases (in_move_cat … H);
910       ##[ *; #w1; *; #w2; *; *; #defw w1m w2m;
911           ncases (H1 a w1); #H1w1; #_; nlapply (H1w1 w1m); #good; 
912           nrewrite > defw; @2; @2 (a::w1); //; ncases good; ##[ *; #; ndestruct] //.
913       ##|
914       ...
915 ##|
916 ##|
917 ##]
918 nqed.
919
920
921 notation > "x ↦* E" non associative with precedence 60 for @{move_star ? $x $E}.
922 nlet rec move_star (S : decidable) w E on w : bool × (pre S) ≝
923  match w with
924   [ nil ⇒ E
925   | cons x w' ⇒ w' ↦* (x ↦ \snd E)].
926
927 ndefinition in_moves ≝ λS:decidable.λw.λE:bool × (pre S). \fst(w ↦* E).
928
929 ncoinductive equiv (S:decidable) : bool × (pre S) → bool × (pre S) → Prop ≝
930  mk_equiv:
931   ∀E1,E2: bool × (pre S).
932    \fst E1  = \fst E2 →
933     (∀x. equiv S (x ↦ \snd E1) (x ↦ \snd E2)) →
934      equiv S E1 E2.
935
936 ndefinition NAT: decidable.
937  @ nat eqb; /2/.
938 nqed.
939
940 include "hints_declaration.ma".
941
942 alias symbol "hint_decl" (instance 1) = "hint_decl_Type1".
943 unification hint 0 ≔ ; X ≟ NAT ⊢ carr X ≡ nat.
944
945 ninductive unit: Type[0] ≝ I: unit.
946
947 nlet corec foo_nop (b: bool):
948  equiv ?
949   〈 b, pc ? (ps ? 0) (pk ? (pc ? (ps ? 1) (ps ? 0))) 〉
950   〈 b, pc ? (pk ? (pc ? (ps ? 0) (ps ? 1))) (ps ? 0) 〉 ≝ ?.
951  @; //; #x; ncases x
952   [ nnormalize in ⊢ (??%%); napply (foo_nop false)
953   | #y; ncases y
954      [ nnormalize in ⊢ (??%%); napply (foo_nop false)
955      | #w; nnormalize in ⊢ (??%%); napply (foo_nop false) ]##]
956 nqed.
957
958 (*
959 nlet corec foo (a: unit):
960  equiv NAT
961   (eclose NAT (pc ? (ps ? 0) (pk ? (pc ? (ps ? 1) (ps ? 0)))))
962   (eclose NAT (pc ? (pk ? (pc ? (ps ? 0) (ps ? 1))) (ps ? 0)))
963 ≝ ?.
964  @;
965   ##[ nnormalize; //
966   ##| #x; ncases x
967        [ nnormalize in ⊢ (??%%);
968          nnormalize in foo: (? → ??%%);
969          @; //; #y; ncases y
970            [ nnormalize in ⊢ (??%%); napply foo_nop
971            | #y; ncases y
972               [ nnormalize in ⊢ (??%%);
973                 
974             ##| #z; nnormalize in ⊢ (??%%); napply foo_nop ]##]
975      ##| #y; nnormalize in ⊢ (??%%); napply foo_nop
976   ##]
977 nqed.
978 *)
979
980 ndefinition test1 : pre ? ≝ ❨ `0 | `1 ❩^* `0.
981 ndefinition test2 : pre ? ≝ ❨ (`0`1)^* `0 | (`0`1)^* `1 ❩.
982 ndefinition test3 : pre ? ≝ (`0 (`0`1)^* `1)^*.
983
984
985 nlemma foo: in_moves ? [0;0;1;0;1;1] (ɛ test3) = true.
986  nnormalize in match test3;
987  nnormalize;
988 //;
989 nqed.
990
991 (**********************************************************)
992
993 ninductive der (S: Type[0]) (a: S) : re S → re S → CProp[0] ≝
994    der_z: der S a (z S) (z S)
995  | der_e: der S a (e S) (z S)
996  | der_s1: der S a (s S a) (e ?)
997  | der_s2: ∀b. a ≠ b → der S a (s S b) (z S)
998  | der_c1: ∀e1,e2,e1',e2'. in_l S [] e1 → der S a e1 e1' → der S a e2 e2' →
999             der S a (c ? e1 e2) (o ? (c ? e1' e2) e2')
1000  | der_c2: ∀e1,e2,e1'. Not (in_l S [] e1) → der S a e1 e1' →
1001             der S a (c ? e1 e2) (c ? e1' e2)
1002  | der_o: ∀e1,e2,e1',e2'. der S a e1 e1' → der S a e2 e2' →
1003     der S a (o ? e1 e2) (o ? e1' e2').
1004
1005 nlemma eq_rect_CProp0_r:
1006  ∀A.∀a,x.∀p:eq ? x a.∀P: ∀x:A. eq ? x a → CProp[0]. P a (refl A a) → P x p.
1007  #A; #a; #x; #p; ncases p; #P; #H; nassumption.
1008 nqed.
1009
1010 nlemma append1: ∀A.∀a:A.∀l. [a] @ l = a::l. //. nqed.
1011
1012 naxiom in_l1: ∀S,r1,r2,w. in_l S [ ] r1 → in_l S w r2 → in_l S w (c S r1 r2).
1013 (* #S; #r1; #r2; #w; nelim r1
1014   [ #K; ninversion K
1015   | #H1; #H2; napply (in_c ? []); //
1016   | (* tutti casi assurdi *) *)
1017
1018 ninductive in_l' (S: Type[0]) : word S → re S → CProp[0] ≝
1019    in_l_empty1: ∀E.in_l S [] E → in_l' S [] E 
1020  | in_l_cons: ∀a,w,e,e'. in_l' S w e' → der S a e e' → in_l' S (a::w) e.
1021
1022 ncoinductive eq_re (S: Type[0]) : re S → re S → CProp[0] ≝
1023    mk_eq_re: ∀E1,E2.
1024     (in_l S [] E1 → in_l S [] E2) →
1025     (in_l S [] E2 → in_l S [] E1) →
1026     (∀a,E1',E2'. der S a E1 E1' → der S a E2 E2' → eq_re S E1' E2') →
1027       eq_re S E1 E2.
1028
1029 (* serve il lemma dopo? *)
1030 ntheorem eq_re_is_eq: ∀S.∀E1,E2. eq_re S E1 E2 → ∀w. in_l ? w E1 → in_l ? w E2.
1031  #S; #E1; #E2; #H1; #w; #H2; nelim H2 in E2 H1 ⊢ %
1032   [ #r; #K (* ok *)
1033   | #a; #w; #R1; #R2; #K1; #K2; #K3; #R3; #K4; @2 R2; //; ncases K4;
1034
1035 (* IL VICEVERSA NON VALE *)
1036 naxiom in_l_to_in_l: ∀S,w,E. in_l' S w E → in_l S w E.
1037 (* #S; #w; #E; #H; nelim H
1038   [ //
1039   | #a; #w'; #r; #r'; #H1; (* e si cade qua sotto! *)
1040   ]
1041 nqed. *)
1042
1043 ntheorem der1: ∀S,a,e,e',w. der S a e e' → in_l S w e' → in_l S (a::w) e.
1044  #S; #a; #E; #E'; #w; #H; nelim H
1045   [##1,2: #H1; ninversion H1
1046      [##1,8: #_; #K; (* non va ndestruct K; *) ncases (?:False); (* perche' due goal?*) /2/
1047      |##2,9: #X; #Y; #K; ncases (?:False); /2/
1048      |##3,10: #x; #y; #z; #w; #a; #b; #c; #d; #e; #K; ncases (?:False); /2/
1049      |##4,11: #x; #y; #z; #w; #a; #b; #K; ncases (?:False); /2/
1050      |##5,12: #x; #y; #z; #w; #a; #b; #K; ncases (?:False); /2/
1051      |##6,13: #x; #y; #K; ncases (?:False); /2/
1052      |##7,14: #x; #y; #z; #w; #a; #b; #c; #d; #K; ncases (?:False); /2/]
1053 ##| #H1; ninversion H1
1054      [ //
1055      | #X; #Y; #K; ncases (?:False); /2/
1056      | #x; #y; #z; #w; #a; #b; #c; #d; #e; #K; ncases (?:False); /2/
1057      | #x; #y; #z; #w; #a; #b; #K; ncases (?:False); /2/
1058      | #x; #y; #z; #w; #a; #b; #K; ncases (?:False); /2/
1059      | #x; #y; #K; ncases (?:False); /2/
1060      | #x; #y; #z; #w; #a; #b; #c; #d; #K; ncases (?:False); /2/ ]
1061 ##| #H1; #H2; #H3; ninversion H3
1062      [ #_; #K; ncases (?:False); /2/
1063      | #X; #Y; #K; ncases (?:False); /2/
1064      | #x; #y; #z; #w; #a; #b; #c; #d; #e; #K; ncases (?:False); /2/
1065      | #x; #y; #z; #w; #a; #b; #K; ncases (?:False); /2/
1066      | #x; #y; #z; #w; #a; #b; #K; ncases (?:False); /2/
1067      | #x; #y; #K; ncases (?:False); /2/
1068      | #x; #y; #z; #w; #a; #b; #c; #d; #K; ncases (?:False); /2/ ]
1069 ##| #r1; #r2; #r1'; #r2'; #H1; #H2; #H3; #H4; #H5; #H6;
1070