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