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