]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/library/nat/congruence.ma
ported to new syntactic requirement about terms being surrounded by parens
[helm.git] / helm / matita / library / nat / congruence.ma
1 (**************************************************************************)
2 (*       ___                                                              *)
3 (*      ||M||                                                             *)
4 (*      ||A||       A project by Andrea Asperti                           *)
5 (*      ||T||                                                             *)
6 (*      ||I||       Developers:                                           *)
7 (*      ||T||       A.Asperti, C.Sacerdoti Coen,                          *)
8 (*      ||A||       E.Tassi, S.Zacchiroli                                 *)
9 (*      \   /                                                             *)
10 (*       \ /        Matita is distributed under the terms of the          *)
11 (*        v         GNU Lesser General Public License Version 2.1         *)
12 (*                                                                        *)
13 (**************************************************************************)
14
15 set "baseuri" "cic:/matita/nat/congruence".
16
17 include "nat/relevant_equations.ma".
18 include "nat/primes.ma".
19
20 definition S_mod: nat \to nat \to nat \def
21 \lambda n,m:nat. (S m) \mod n.
22
23 definition congruent: nat \to nat \to nat \to Prop \def
24 \lambda n,m,p:nat. mod n p = mod m p.
25
26 theorem congruent_n_n: \forall n,p:nat.congruent n n p.
27 intros.unfold congruent.reflexivity.
28 qed.
29
30 theorem transitive_congruent: \forall p:nat. transitive nat 
31 (\lambda n,m. congruent n m p).
32 intros.unfold transitive.unfold congruent.intros.
33 whd.apply (trans_eq ? ? (y \mod p)).
34 apply H.apply H1.
35 qed.
36
37 theorem le_to_mod: \forall n,m:nat. n \lt m \to n = n \mod m.
38 intros.
39 apply (div_mod_spec_to_eq2 n m O n (n/m) (n \mod m)).
40 constructor 1.assumption.simplify.reflexivity.
41 apply div_mod_spec_div_mod.
42 apply (le_to_lt_to_lt O n m).apply le_O_n.assumption.
43 qed.
44
45 theorem mod_mod : \forall n,p:nat. O<p \to n \mod p = (n \mod p) \mod p.
46 intros.
47 rewrite > (div_mod (n \mod p) p) in \vdash (? ? % ?).
48 rewrite > (eq_div_O ? p).reflexivity.
49 (* uffa: hint non lo trova lt vs. le*)
50 apply lt_mod_m_m.
51 assumption.
52 assumption.
53 qed.
54
55 theorem congruent_n_mod_n : 
56 \forall n,p:nat. O < p \to congruent n (n \mod p) p.
57 intros.unfold congruent.
58 apply mod_mod.assumption.
59 qed.
60
61 theorem eq_times_plus_to_congruent: \forall n,m,p,r:nat. O< p \to 
62 n = r*p+m \to congruent n m p.
63 intros.unfold congruent.
64 apply (div_mod_spec_to_eq2 n p (div n p) (mod n p) (r +(div m p)) (mod m p)).
65 apply div_mod_spec_div_mod.assumption.
66 constructor 1.
67 apply lt_mod_m_m.assumption.
68 rewrite > sym_times.
69 rewrite > distr_times_plus.
70 rewrite > sym_times.
71 rewrite > (sym_times p).
72 rewrite > assoc_plus.
73 rewrite < div_mod.
74 assumption.assumption.
75 qed.
76
77 theorem divides_to_congruent: \forall n,m,p:nat. O < p \to m \le n \to 
78 divides p (n - m) \to congruent n m p.
79 intros.elim H2.
80 apply (eq_times_plus_to_congruent n m p n2).
81 assumption.
82 rewrite < sym_plus.
83 apply minus_to_plus.assumption.
84 rewrite > sym_times. assumption.
85 qed.
86
87 theorem congruent_to_divides: \forall n,m,p:nat.
88 O < p \to congruent n m p \to divides p (n - m).
89 intros.unfold congruent in H1.
90 apply (witness ? ? ((n / p)-(m / p))).
91 rewrite > sym_times.
92 rewrite > (div_mod n p) in \vdash (? ? % ?).
93 rewrite > (div_mod m p) in \vdash (? ? % ?).
94 rewrite < (sym_plus (m \mod p)).
95 rewrite < H1.
96 rewrite < (eq_minus_minus_minus_plus ? (n \mod p)).
97 rewrite < minus_plus_m_m.
98 apply sym_eq.
99 apply times_minus_l.
100 assumption.assumption.
101 qed.
102
103 theorem mod_times: \forall n,m,p:nat. 
104 O < p \to mod (n*m) p = mod ((mod n p)*(mod m p)) p.
105 intros.
106 change with (congruent (n*m) ((mod n p)*(mod m p)) p).
107 apply (eq_times_plus_to_congruent ? ? p 
108 ((n / p)*p*(m / p) + (n / p)*(m \mod p) + (n \mod p)*(m / p))).
109 assumption.
110 apply (trans_eq ? ? (((n/p)*p+(n \mod p))*((m/p)*p+(m \mod p)))).
111 apply eq_f2.
112 apply div_mod.assumption.
113 apply div_mod.assumption.
114 apply (trans_eq ? ? (((n/p)*p)*((m/p)*p) + (n/p)*p*(m \mod p) +
115 (n \mod p)*((m / p)*p) + (n \mod p)*(m \mod p))).
116 apply times_plus_plus.
117 apply eq_f2.
118 rewrite < assoc_times.
119 rewrite > (assoc_times (n/p) p (m \mod p)).
120 rewrite > (sym_times p (m \mod p)).
121 rewrite < (assoc_times (n/p) (m \mod p) p).
122 rewrite < times_plus_l.
123 rewrite < (assoc_times (n \mod p)).
124 rewrite < times_plus_l.
125 apply eq_f2.
126 apply eq_f2.reflexivity.
127 reflexivity.reflexivity.
128 reflexivity.
129 qed.
130
131 theorem congruent_times: \forall n,m,n1,m1,p. O < p \to congruent n n1 p \to 
132 congruent m m1 p \to congruent (n*m) (n1*m1) p.
133 unfold congruent. 
134 intros. 
135 rewrite > (mod_times n m p H).
136 rewrite > H1.
137 rewrite > H2.
138 apply sym_eq.
139 apply mod_times.assumption.
140 qed.
141
142 theorem congruent_pi: \forall f:nat \to nat. \forall n,m,p:nat.O < p \to
143 congruent (pi n f m) (pi n (\lambda m. mod (f m) p) m) p.
144 intros.
145 elim n.change with (congruent (f m) (f m \mod p) p).
146 apply congruent_n_mod_n.assumption.
147 change with (congruent ((f (S n1+m))*(pi n1 f m)) 
148 (((f (S n1+m))\mod p)*(pi n1 (\lambda m.(f m) \mod p) m)) p).
149 apply congruent_times.
150 assumption.
151 apply congruent_n_mod_n.assumption.
152 assumption.
153 qed.