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