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