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