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