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