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