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 (**************************************************************************)
16 include "nat/primes.ma".
18 include "nat/generic_iter_p.ma".
20 (* sigma_p in Z is a specialization of iter_p_gen *)
21 definition sigma_p: nat \to (nat \to bool) \to (nat \to Z) \to Z \def
22 \lambda n, p, g. (iter_p_gen n p Z g OZ Zplus).
24 theorem symmetricZPlus: symmetric Z Zplus.
25 change with (\forall a,b:Z. (Zplus a b) = (Zplus b a)).
31 theorem true_to_sigma_p_Sn:
32 \forall n:nat. \forall p:nat \to bool. \forall g:nat \to Z.
33 p n = true \to sigma_p (S n) p g =
34 (g n)+(sigma_p n p g).
37 apply true_to_iter_p_gen_Sn.
41 theorem false_to_sigma_p_Sn:
42 \forall n:nat. \forall p:nat \to bool. \forall g:nat \to Z.
43 p n = false \to sigma_p (S n) p g = sigma_p n p g.
46 apply false_to_iter_p_gen_Sn.
50 theorem eq_sigma_p: \forall p1,p2:nat \to bool.
51 \forall g1,g2: nat \to Z.\forall n.
52 (\forall x. x < n \to p1 x = p2 x) \to
53 (\forall x. x < n \to g1 x = g2 x) \to
54 sigma_p n p1 g1 = sigma_p n p2 g2.
61 theorem eq_sigma_p1: \forall p1,p2:nat \to bool.
62 \forall g1,g2: nat \to Z.\forall n.
63 (\forall x. x < n \to p1 x = p2 x) \to
64 (\forall x. x < n \to p1 x = true \to g1 x = g2 x) \to
65 sigma_p n p1 g1 = sigma_p n p2 g2.
72 theorem sigma_p_false:
73 \forall g: nat \to Z.\forall n.sigma_p n (\lambda x.false) g = O.
76 apply iter_p_gen_false.
79 theorem sigma_p_plus: \forall n,k:nat.\forall p:nat \to bool.
82 = sigma_p k (\lambda x.p (x+n)) (\lambda x.g (x+n)) + sigma_p n p g.
85 apply (iter_p_gen_plusA Z n k p g OZ Zplus)
86 [ apply symmetricZPlus.
89 | apply associative_Zplus
93 theorem false_to_eq_sigma_p: \forall n,m:nat.n \le m \to
94 \forall p:nat \to bool.
95 \forall g: nat \to Z. (\forall i:nat. n \le i \to i < m \to
96 p i = false) \to sigma_p m p g = sigma_p n p g.
99 apply (false_to_eq_iter_p_gen);
105 \forall p1,p2:nat \to bool.
106 \forall g: nat \to nat \to Z.
108 (\lambda x.andb (p1 (div x m)) (p2 (mod x m)))
109 (\lambda x.g (div x m) (mod x m)) =
111 (\lambda x.sigma_p m p2 (g x)).
114 apply (iter_p_gen2 n m p1 p2 Z g OZ Zplus)
115 [ apply symmetricZPlus
116 | apply associative_Zplus
122 (* a stronger, dependent version, required e.g. for dirichlet product *)
126 \forall p1:nat \to bool.
127 \forall p2:nat \to nat \to bool.
128 \forall g: nat \to nat \to Z.
130 (\lambda x.andb (p1 (div x m)) (p2 (div x m) (mod x m)))
131 (\lambda x.g (div x m) (mod x m)) =
133 (\lambda x.sigma_p m (p2 x) (g x)).
136 apply (iter_p_gen2' n m p1 p2 Z g OZ Zplus)
137 [ apply symmetricZPlus
138 | apply associative_Zplus
144 lemma sigma_p_gi: \forall g: nat \to Z.
145 \forall n,i.\forall p:nat \to bool.i < n \to p i = true \to
146 sigma_p n p g = g i + sigma_p n (\lambda x. andb (p x) (notb (eqb x i))) g.
149 apply (iter_p_gen_gi)
150 [ apply symmetricZPlus
151 | apply associative_Zplus
159 theorem eq_sigma_p_gh:
160 \forall g: nat \to Z.
161 \forall h,h1: nat \to nat.\forall n,n1.
162 \forall p1,p2:nat \to bool.
163 (\forall i. i < n \to p1 i = true \to p2 (h i) = true) \to
164 (\forall i. i < n \to p1 i = true \to h1 (h i) = i) \to
165 (\forall i. i < n \to p1 i = true \to h i < n1) \to
166 (\forall j. j < n1 \to p2 j = true \to p1 (h1 j) = true) \to
167 (\forall j. j < n1 \to p2 j = true \to h (h1 j) = j) \to
168 (\forall j. j < n1 \to p2 j = true \to h1 j < n) \to
169 sigma_p n p1 (\lambda x.g(h x)) = sigma_p n1 p2 g.
172 apply (eq_iter_p_gen_gh Z OZ Zplus ? ? ? g h h1 n n1 p1 p2)
173 [ apply symmetricZPlus
174 | apply associative_Zplus
187 theorem divides_exp_to_lt_ord:\forall n,m,j,p. O < n \to prime p \to
188 p \ndivides n \to j \divides n*(exp p m) \to ord j p < S m.
190 cut (m = ord (n*(exp p m)) p)
193 apply divides_to_le_ord
194 [elim (le_to_or_lt_eq ? ? (le_O_n j))
197 apply (lt_to_not_eq ? ? H).
199 rewrite < H4 in H5.simplify in H5.
200 elim (times_O_to_O ? ? H5)
201 [apply sym_eq.assumption
203 apply (not_le_Sn_n O).
204 rewrite < H6 in \vdash (? ? %).
206 elim H1.apply lt_to_le.assumption
209 |rewrite > (times_n_O O).
211 [assumption|apply lt_O_exp.apply (prime_to_lt_O ? H1)]
216 rewrite > (p_ord_exp1 p ? m n)
218 |apply (prime_to_lt_O ? H1)
225 theorem divides_exp_to_divides_ord_rem:\forall n,m,j,p. O < n \to prime p \to
226 p \ndivides n \to j \divides n*(exp p m) \to ord_rem j p \divides n.
229 [cut (n = ord_rem (n*(exp p m)) p)
231 apply divides_to_divides_ord_rem
233 |rewrite > (times_n_O O).
235 [assumption|apply lt_O_exp.apply (prime_to_lt_O ? H1)]
240 rewrite > (p_ord_exp1 p ? m n)
242 |apply (prime_to_lt_O ? H1)
247 |elim (le_to_or_lt_eq ? ? (le_O_n j))
250 apply (lt_to_not_eq ? ? H).
252 rewrite < H4 in H5.simplify in H5.
253 elim (times_O_to_O ? ? H5)
254 [apply sym_eq.assumption
256 apply (not_le_Sn_n O).
257 rewrite < H6 in \vdash (? ? %).
259 elim H1.apply lt_to_le.assumption
266 theorem sigma_p_divides_b:
267 \forall n,m,p:nat.O < n \to prime p \to Not (divides p n) \to
268 \forall g: nat \to Z.
269 sigma_p (S (n*(exp p m))) (\lambda x.divides_b x (n*(exp p m))) g =
270 sigma_p (S n) (\lambda x.divides_b x n)
271 (\lambda x.sigma_p (S m) (\lambda y.true) (\lambda y.g (x*(exp p y)))).
274 apply (iter_p_gen_divides Z OZ Zplus n m p ? ? ? g)
278 | apply symmetricZPlus
279 | apply associative_Zplus
286 (* sigma_p and Ztimes *)
287 lemma Ztimes_sigma_pl: \forall z:Z.\forall n:nat.\forall p. \forall f.
288 z * (sigma_p n p f) = sigma_p n p (\lambda i.z*(f i)).
290 apply (distributive_times_plus_iter_p_gen Z Zplus OZ Ztimes n z p f)
291 [ apply symmetricZPlus
292 | apply associative_Zplus
295 | apply symmetric_Ztimes
296 | apply distributive_Ztimes_Zplus
298 rewrite > (Ztimes_z_OZ a).
303 lemma Ztimes_sigma_pr: \forall z:Z.\forall n:nat.\forall p. \forall f.
304 (sigma_p n p f) * z = sigma_p n p (\lambda i.(f i)*z).
306 rewrite < sym_Ztimes.
307 rewrite > Ztimes_sigma_pl.
310 |intros.apply sym_Ztimes
316 \forall g: nat \to Z.
317 \forall h2:nat \to nat \to nat.
318 \forall h11,h12:nat \to nat.
320 \forall p1,p21:nat \to bool.
321 \forall p22:nat \to nat \to bool.
322 (\forall x. x < k \to p1 x = true \to
323 p21 (h11 x) = true \land p22 (h11 x) (h12 x) = true
324 \land h2 (h11 x) (h12 x) = x
325 \land (h11 x) < n \land (h12 x) < m) \to
326 (\forall i,j. i < n \to j < m \to p21 i = true \to p22 i j = true \to
327 p1 (h2 i j) = true \land
328 h11 (h2 i j) = i \land h12 (h2 i j) = j
329 \land h2 i j < k) \to
331 sigma_p n p21 (\lambda x:nat.sigma_p m (p22 x) (\lambda y. g (h2 x y))).
334 unfold sigma_p in \vdash (? ? ? (? ? ? ? (\lambda x:?.%) ? ?)).
336 [ apply symmetricZPlus
337 |apply associative_Zplus
349 \forall g: nat \to nat \to Z.
350 \forall h11,h12,h21,h22: nat \to nat \to nat.
352 \forall p11,p21:nat \to bool.
353 \forall p12,p22:nat \to nat \to bool.
354 (\forall i,j. i < n2 \to j < m2 \to p21 i = true \to p22 i j = true \to
355 p11 (h11 i j) = true \land p12 (h11 i j) (h12 i j) = true
356 \land h21 (h11 i j) (h12 i j) = i \land h22 (h11 i j) (h12 i j) = j
357 \land h11 i j < n1 \land h12 i j < m1) \to
358 (\forall i,j. i < n1 \to j < m1 \to p11 i = true \to p12 i j = true \to
359 p21 (h21 i j) = true \land p22 (h21 i j) (h22 i j) = true
360 \land h11 (h21 i j) (h22 i j) = i \land h12 (h21 i j) (h22 i j) = j
361 \land (h21 i j) < n2 \land (h22 i j) < m2) \to
362 sigma_p n1 p11 (\lambda x:nat .sigma_p m1 (p12 x) (\lambda y. g x y)) =
363 sigma_p n2 p21 (\lambda x:nat .sigma_p m2 (p22 x) (\lambda y. g (h11 x y) (h12 x y))).
366 unfold sigma_p in \vdash (? ? (? ? ? ? (\lambda x:?.%) ? ?) ?).
367 unfold sigma_p in \vdash (? ? ? (? ? ? ? (\lambda x:?.%) ? ?)).
369 apply(iter_p_gen_2_eq Z OZ Zplus ? ? ? g h11 h12 h21 h22 n1 m1 n2 m2 p11 p21 p12 p22)
370 [ apply symmetricZPlus
371 | apply associative_Zplus
389 letin ha:= (\lambda x,y.(((h11 x y)*m1) + (h12 x y))).
390 letin ha12:= (\lambda x.(h21 (x/m1) (x \mod m1))).
391 letin ha22:= (\lambda x.(h22 (x/m1) (x \mod m1))).
394 (sigma_p n2 p21 (\lambda x:nat. sigma_p m2 (p22 x)
395 (\lambda y:nat.(g (((h11 x y)*m1+(h12 x y))/m1) (((h11 x y)*m1+(h12 x y))\mod m1)) ) ) ))
397 apply (sigma_p_knm (\lambda e. (g (e/m1) (e \mod m1))) ha ha12 ha22);intros
398 [ elim (and_true ? ? H3).
401 [ cut((x \mod m1) < m1)
402 [ elim (H1 ? ? Hcut1 Hcut2 H4 H5).
427 | apply (lt_times_n_to_lt m1)
429 | apply (le_to_lt_to_lt ? x)
430 [ apply (eq_plus_to_le ? ? (x \mod m1)).
437 | apply not_le_to_lt.unfold.intro.
438 generalize in match H2.
439 apply (le_n_O_elim ? H6).
444 | elim (H ? ? H2 H3 H4 H5).
449 cut(((h11 i j)*m1 + (h12 i j))/m1 = (h11 i j))
450 [ cut(((h11 i j)*m1 + (h12 i j)) \mod m1 = (h12 i j))
454 [ apply true_to_true_to_andb_true
471 [ apply (lt_to_le_to_lt ? ((h11 i j)*m1 + m1) )
474 | rewrite > sym_plus.
475 rewrite > (sym_times (h11 i j) m1).
476 rewrite > times_n_Sm.
481 | apply not_le_to_lt.unfold.intro.
482 generalize in match H9.
483 apply (le_n_O_elim ? H8).
487 | apply not_le_to_lt.unfold.intro.
488 generalize in match H7.
489 apply (le_n_O_elim ? H8).
494 | rewrite > (mod_plus_times m1 (h11 i j) (h12 i j)).
498 | rewrite > (div_plus_times m1 (h11 i j) (h12 i j)).
503 | apply (eq_sigma_p1)
504 [ intros. reflexivity
507 [ intros. reflexivity
509 rewrite > (div_plus_times)
510 [ rewrite > (mod_plus_times)
512 | elim (H x x1 H2 H4 H3 H5).
515 | elim (H x x1 H2 H4 H3 H5).
523 rewrite < sigma_p2' in \vdash (? ? ? %).
525 letin h := (\lambda x.(h11 (x/m2) (x\mod m2))*m1 + (h12 (x/m2) (x\mod m2))).
526 letin h1 := (\lambda x.(h21 (x/m1) (x\mod m1))*m2 + (h22 (x/m1) (x\mod m1))).
528 (sigma_p (n2*m2) (\lambda x:nat.p21 (x/m2)\land p22 (x/m2) (x\mod m2))
529 (\lambda x:nat.g ((h x)/m1) ((h x)\mod m1))))
536 [cut (x \mod m2 < m2)
537 [elim (and_true ? ? H3).
538 elim (H ? ? Hcut1 Hcut2 H4 H5).
545 apply div_plus_times.
549 apply mod_plus_times.
555 |apply (lt_times_n_to_lt m2)
557 |apply (le_to_lt_to_lt ? x)
558 [apply (eq_plus_to_le ? ? (x \mod m2)).
565 |apply not_le_to_lt.unfold.intro.
566 generalize in match H2.
567 apply (le_n_O_elim ? H4).
573 |apply (eq_sigma_p_gh ? h h1);intros
576 [cut (i \mod m2 < m2)
577 [elim (and_true ? ? H3).
578 elim (H ? ? Hcut1 Hcut2 H4 H5).
583 cut ((h11 (i/m2) (i\mod m2)*m1+h12 (i/m2) (i\mod m2))/m1 =
584 h11 (i/m2) (i\mod m2))
585 [cut ((h11 (i/m2) (i\mod m2)*m1+h12 (i/m2) (i\mod m2))\mod m1 =
586 h12 (i/m2) (i\mod m2))
592 |apply mod_plus_times.
595 |apply div_plus_times.
601 |apply (lt_times_n_to_lt m2)
603 |apply (le_to_lt_to_lt ? i)
604 [apply (eq_plus_to_le ? ? (i \mod m2)).
611 |apply not_le_to_lt.unfold.intro.
612 generalize in match H2.
613 apply (le_n_O_elim ? H4).
620 [cut (i \mod m2 < m2)
621 [elim (and_true ? ? H3).
622 elim (H ? ? Hcut1 Hcut2 H4 H5).
627 cut ((h11 (i/m2) (i\mod m2)*m1+h12 (i/m2) (i\mod m2))/m1 =
628 h11 (i/m2) (i\mod m2))
629 [cut ((h11 (i/m2) (i\mod m2)*m1+h12 (i/m2) (i\mod m2))\mod m1 =
630 h12 (i/m2) (i\mod m2))
638 |apply mod_plus_times.
641 |apply div_plus_times.
647 |apply (lt_times_n_to_lt m2)
649 |apply (le_to_lt_to_lt ? i)
650 [apply (eq_plus_to_le ? ? (i \mod m2)).
657 |apply not_le_to_lt.unfold.intro.
658 generalize in match H2.
659 apply (le_n_O_elim ? H4).
666 [cut (i \mod m2 < m2)
667 [elim (and_true ? ? H3).
668 elim (H ? ? Hcut1 Hcut2 H4 H5).
673 apply lt_times_plus_times
674 [assumption|assumption]
678 |apply (lt_times_n_to_lt m2)
680 |apply (le_to_lt_to_lt ? i)
681 [apply (eq_plus_to_le ? ? (i \mod m2)).
688 |apply not_le_to_lt.unfold.intro.
689 generalize in match H2.
690 apply (le_n_O_elim ? H4).
697 [cut (j \mod m1 < m1)
698 [elim (and_true ? ? H3).
699 elim (H1 ? ? Hcut1 Hcut2 H4 H5).
704 cut ((h21 (j/m1) (j\mod m1)*m2+h22 (j/m1) (j\mod m1))/m2 =
705 h21 (j/m1) (j\mod m1))
706 [cut ((h21 (j/m1) (j\mod m1)*m2+h22 (j/m1) (j\mod m1))\mod m2 =
707 h22 (j/m1) (j\mod m1))
713 |apply mod_plus_times.
716 |apply div_plus_times.
722 |apply (lt_times_n_to_lt m1)
724 |apply (le_to_lt_to_lt ? j)
725 [apply (eq_plus_to_le ? ? (j \mod m1)).
732 |apply not_le_to_lt.unfold.intro.
733 generalize in match H2.
734 apply (le_n_O_elim ? H4).
741 [cut (j \mod m1 < m1)
742 [elim (and_true ? ? H3).
743 elim (H1 ? ? Hcut1 Hcut2 H4 H5).
748 cut ((h21 (j/m1) (j\mod m1)*m2+h22 (j/m1) (j\mod m1))/m2 =
749 h21 (j/m1) (j\mod m1))
750 [cut ((h21 (j/m1) (j\mod m1)*m2+h22 (j/m1) (j\mod m1))\mod m2 =
751 h22 (j/m1) (j\mod m1))
759 |apply mod_plus_times.
762 |apply div_plus_times.
768 |apply (lt_times_n_to_lt m1)
770 |apply (le_to_lt_to_lt ? j)
771 [apply (eq_plus_to_le ? ? (j \mod m1)).
778 |apply not_le_to_lt.unfold.intro.
779 generalize in match H2.
780 apply (le_n_O_elim ? H4).
787 [cut (j \mod m1 < m1)
788 [elim (and_true ? ? H3).
789 elim (H1 ? ? Hcut1 Hcut2 H4 H5).
794 apply (lt_times_plus_times ? ? ? m2)
795 [assumption|assumption]
799 |apply (lt_times_n_to_lt m1)
801 |apply (le_to_lt_to_lt ? j)
802 [apply (eq_plus_to_le ? ? (j \mod m1)).
809 |apply not_le_to_lt.unfold.intro.
810 generalize in match H2.
811 apply (le_n_O_elim ? H4).