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