1 (**************************************************************************)
4 (* ||A|| A project by Andrea Asperti *)
6 (* ||I|| Developers: *)
7 (* ||T|| A.Asperti, C.Sacerdoti Coen, *)
8 (* ||A|| E.Tassi, S.Zacchiroli *)
10 (* \ / This file is distributed under the terms of the *)
11 (* v GNU Lesser General Public License Version 2.1 *)
13 (**************************************************************************)
15 set "baseuri" "cic:/matita/nat/count".
17 include "nat/relevant_equations.ma".
18 include "nat/sigma_and_pi.ma".
19 include "nat/permutation.ma".
21 theorem sigma_f_g : \forall n,m:nat.\forall f,g:nat \to nat.
22 sigma n (\lambda p.f p + g p) m = sigma n f m + sigma n g m.
27 rewrite < (assoc_plus (g (S (n1+m)))).
28 rewrite > (sym_plus (g (S (n1+m)))).
29 rewrite > (assoc_plus (sigma n1 f m)).
34 theorem sigma_plus: \forall n,p,m:nat.\forall f:nat \to nat.
35 sigma (S (p+n)) f m = sigma p (\lambda x.(f ((S n) + x))) m + sigma n f m.
38 rewrite < (sym_plus n m).reflexivity.
40 rewrite > assoc_plus in \vdash (? ? ? %).
44 rewrite > (sym_plus n).
46 rewrite < (sym_plus m).
47 rewrite < (assoc_plus n1).
51 theorem sigma_plus1: \forall n,p,m:nat.\forall f:nat \to nat.
52 sigma (p+(S n)) f m = sigma p (\lambda x.(f ((S n) + x))) m + sigma n f m.
56 rewrite > assoc_plus in \vdash (? ? ? %).
59 rewrite < plus_n_Sm.simplify.
60 rewrite < (sym_plus n).
62 rewrite < (sym_plus m).
63 rewrite < (assoc_plus n).
67 theorem eq_sigma_sigma : \forall n,m:nat.\forall f:nat \to nat.
68 sigma (pred ((S n)*(S m))) f O =
69 sigma m (\lambda a.(sigma n (\lambda b.f (b*(S m) + a)) O)) O.
70 intro.elim n.simplify.
72 apply eq_sigma.intros.reflexivity.
74 (sigma (m+(S n1)*(S m)) f O =
75 sigma m (\lambda a.(f ((S(n1+O))*(S m)+a)) + (sigma n1 (\lambda b.f (b*(S m)+a)) O)) O).
79 rewrite > (S_pred ((S n1)*(S m))).
81 simplify.unfold lt.apply le_S_S.apply le_O_n.
84 theorem eq_sigma_sigma1 : \forall n,m:nat.\forall f:nat \to nat.
85 sigma (pred ((S n)*(S m))) f O =
86 sigma n (\lambda a.(sigma m (\lambda b.f (b*(S n) + a)) O)) O.
92 theorem sigma_times: \forall n,m,p:nat.\forall f:nat \to nat.
93 (sigma n f m)*p = sigma n (\lambda i.(f i) * p) m.
94 intro. elim n.simplify.reflexivity.
99 definition bool_to_nat: bool \to nat \def
100 \lambda b. match b with
101 [ true \Rightarrow (S O)
102 | false \Rightarrow O ].
104 theorem bool_to_nat_andb: \forall a,b:bool.
105 bool_to_nat (andb a b) = (bool_to_nat a)*(bool_to_nat b).
106 intros. elim a.elim b.
107 simplify.reflexivity.
112 definition count : nat \to (nat \to bool) \to nat \def
113 \lambda n.\lambda f. sigma (pred n) (\lambda n.(bool_to_nat (f n))) O.
115 theorem count_times:\forall n,m:nat.
116 \forall f,f1,f2:nat \to bool.
117 \forall g:nat \to nat \to nat.
118 \forall g1,g2: nat \to nat.
119 (\forall a,b:nat. a < (S n) \to b < (S m) \to (g b a) < (S n)*(S m)) \to
120 (\forall a,b:nat. a < (S n) \to b < (S m) \to (g1 (g b a)) = a) \to
121 (\forall a,b:nat. a < (S n) \to b < (S m) \to (g2 (g b a)) = b) \to
122 (\forall a,b:nat. a < (S n) \to b < (S m) \to f (g b a) = andb (f2 b) (f1 a)) \to
123 (count ((S n)*(S m)) f) = (count (S n) f1)*(count (S m) f2).
125 rewrite < eq_map_iter_i_sigma.
126 rewrite > (permut_to_eq_map_iter_i plus assoc_plus sym_plus ? ? ?
127 (\lambda i.g (div i (S n)) (mod i (S n)))).
128 rewrite > eq_map_iter_i_sigma.
129 rewrite > eq_sigma_sigma1.
132 sigma m (\lambda b.(bool_to_nat (f2 b))*(bool_to_nat (f1 a))) O) O)).
133 apply eq_sigma.intros.
134 apply eq_sigma.intros.
135 rewrite > (div_mod_spec_to_eq (i1*(S n) + i) (S n) ((i1*(S n) + i)/(S n))
136 ((i1*(S n) + i) \mod (S n)) i1 i).
137 rewrite > (div_mod_spec_to_eq2 (i1*(S n) + i) (S n) ((i1*(S n) + i)/(S n))
138 ((i1*(S n) + i) \mod (S n)) i1 i).
140 apply bool_to_nat_andb.
141 unfold lt.apply le_S_S.assumption.
142 unfold lt.apply le_S_S.assumption.
143 apply div_mod_spec_div_mod.
144 unfold lt.apply le_S_S.apply le_O_n.
145 constructor 1.unfold lt.apply le_S_S.assumption.
147 apply div_mod_spec_div_mod.
148 unfold lt.apply le_S_S.apply le_O_n.
149 constructor 1.unfold lt.apply le_S_S.assumption.
152 (sigma n (\lambda n.((bool_to_nat (f1 n)) *
153 (sigma m (\lambda n.bool_to_nat (f2 n)) O))) O)).
158 (sigma m (\lambda n.(bool_to_nat (f2 n))*(bool_to_nat (f1 i))) O)).
160 apply sym_eq. apply sigma_times.
161 change in match (pred (S n)) with n.
162 change in match (pred (S m)) with m.
163 apply sym_eq. apply sigma_times.
169 rewrite < S_pred in \vdash (? ? %).
170 change with ((g (i/(S n)) (i \mod (S n))) \lt (S n)*(S m)).
173 unfold lt. apply le_S_S.apply le_O_n.
174 apply (lt_times_to_lt_l n).
175 apply (le_to_lt_to_lt ? i).
176 rewrite > (div_mod i (S n)) in \vdash (? ? %).
179 unfold lt. apply le_S_S.apply le_O_n.
181 rewrite > S_pred in \vdash (? ? %).
183 rewrite > plus_n_O in \vdash (? ? %).
184 rewrite > sym_times. assumption.
185 rewrite > (times_n_O O).
187 unfold lt. apply le_S_S.apply le_O_n.
188 unfold lt. apply le_S_S.apply le_O_n.
189 rewrite > (times_n_O O).
191 unfold lt. apply le_S_S.apply le_O_n.
192 unfold lt. apply le_S_S.apply le_O_n.
196 cut (i < (S n)*(S m)).
197 cut (j < (S n)*(S m)).
198 cut ((i \mod (S n)) < (S n)).
199 cut ((i/(S n)) < (S m)).
200 cut ((j \mod (S n)) < (S n)).
201 cut ((j/(S n)) < (S m)).
202 rewrite > (div_mod i (S n)).
203 rewrite > (div_mod j (S n)).
204 rewrite < (H1 (i \mod (S n)) (i/(S n)) Hcut2 Hcut3).
205 rewrite < (H2 (i \mod (S n)) (i/(S n)) Hcut2 Hcut3) in \vdash (? ? (? % ?) ?).
206 rewrite < (H1 (j \mod (S n)) (j/(S n)) Hcut4 Hcut5).
207 rewrite < (H2 (j \mod (S n)) (j/(S n)) Hcut4 Hcut5) in \vdash (? ? ? (? % ?)).
208 rewrite > H6.reflexivity.
209 unfold lt. apply le_S_S.apply le_O_n.
210 unfold lt. apply le_S_S.apply le_O_n.
211 apply (lt_times_to_lt_l n).
212 apply (le_to_lt_to_lt ? j).
213 rewrite > (div_mod j (S n)) in \vdash (? ? %).
216 unfold lt. apply le_S_S.apply le_O_n.
217 rewrite < sym_times. assumption.
219 unfold lt. apply le_S_S.apply le_O_n.
220 apply (lt_times_to_lt_l n).
221 apply (le_to_lt_to_lt ? i).
222 rewrite > (div_mod i (S n)) in \vdash (? ? %).
225 unfold lt. apply le_S_S.apply le_O_n.
226 rewrite < sym_times. assumption.
228 unfold lt. apply le_S_S.apply le_O_n.
230 rewrite > S_pred in \vdash (? ? %).
231 apply le_S_S.assumption.
232 rewrite > (times_n_O O).
234 unfold lt. apply le_S_S.apply le_O_n.
235 unfold lt. apply le_S_S.apply le_O_n.
237 rewrite > S_pred in \vdash (? ? %).
238 apply le_S_S.assumption.
239 rewrite > (times_n_O O).
241 unfold lt. apply le_S_S.apply le_O_n.
242 unfold lt. apply le_S_S.apply le_O_n.
245 apply (not_le_Sn_O m1 H4).