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