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 (* \ / Matita is distributed under the terms of the *)
11 (* v GNU Lesser General Public License Version 2.1 *)
13 (**************************************************************************)
15 set "baseuri" "cic:/matita/library_autobatch/nat/congruence".
17 include "auto/nat/relevant_equations.ma".
18 include "auto/nat/primes.ma".
20 definition S_mod: nat \to nat \to nat \def
21 \lambda n,m:nat. (S m) \mod n.
23 definition congruent: nat \to nat \to nat \to Prop \def
24 \lambda n,m,p:nat. mod n p = mod m p.
26 interpretation "congruent" 'congruent n m p = (congruent n m p).
28 notation < "hvbox(n break \cong\sub p m)"
29 (*non associative*) with precedence 45
30 for @{ 'congruent $n $m $p }.
32 theorem congruent_n_n: \forall n,p:nat.congruent n n p.
38 theorem transitive_congruent: \forall p:nat. transitive nat
39 (\lambda n,m. congruent n m p).
40 intros.unfold transitive.
44 apply (trans_eq ? ? (y \mod p))
45 [ (*qui autobatch non chiude il goal*)
47 | (*qui autobatch non chiude il goal*)
52 theorem le_to_mod: \forall n,m:nat. n \lt m \to n = n \mod m.
55 (*apply (div_mod_spec_to_eq2 n m O n (n/m) (n \mod m))
61 | apply div_mod_spec_div_mod.
62 apply (le_to_lt_to_lt O n m)
69 theorem mod_mod : \forall n,p:nat. O<p \to n \mod p = (n \mod p) \mod p.
72 (*rewrite > (div_mod (n \mod p) p) in \vdash (? ? % ?)
73 [ rewrite > (eq_div_O ? p)
82 theorem mod_times_mod : \forall n,m,p:nat. O<p \to O<m \to n \mod p = (n \mod (m*p)) \mod p.
84 apply (div_mod_spec_to_eq2 n p (n/p) (n \mod p)
85 (n/(m*p)*m + (n \mod (m*p)/p)))
87 (*apply div_mod_spec_div_mod.
93 | rewrite > times_plus_l.
96 [ rewrite > assoc_times.
97 rewrite < div_mod;autobatch
99 | rewrite > (times_n_O O).
100 apply lt_times;assumption
108 theorem congruent_n_mod_n :
109 \forall n,p:nat. O < p \to congruent n (n \mod p) p.
117 theorem congruent_n_mod_times :
118 \forall n,m,p:nat. O < p \to O < m \to congruent n (n \mod (m*p)) p.
119 intros.unfold congruent.
120 apply mod_times_mod;assumption.
123 theorem eq_times_plus_to_congruent: \forall n,m,p,r:nat. O< p \to
124 n = r*p+m \to congruent n m p.
127 apply (div_mod_spec_to_eq2 n p (div n p) (mod n p) (r +(div m p)) (mod m p))
129 (*apply div_mod_spec_div_mod.
136 (*cut (n = r * p + (m / p * p + m \mod p)).*)
137 (*lapply (div_mod m p H).
139 rewrite > distr_times_plus.
140 (*rewrite > (sym_times p (m/p)).*)
141 (*rewrite > sym_times.*)
142 rewrite > assoc_plus.
143 autobatch paramodulation.
149 rewrite > distr_times_plus.
151 rewrite > (sym_times p).
152 rewrite > assoc_plus.
153 rewrite < div_mod;assumption.
158 theorem divides_to_congruent: \forall n,m,p:nat. O < p \to m \le n \to
159 divides p (n - m) \to congruent n m p.
162 apply (eq_times_plus_to_congruent n m p n2)
164 | rewrite < sym_plus.
165 apply minus_to_plus;autobatch
167 | rewrite > sym_times. assumption
172 theorem congruent_to_divides: \forall n,m,p:nat.
173 O < p \to congruent n m p \to divides p (n - m).
175 unfold congruent in H1.
176 apply (witness ? ? ((n / p)-(m / p))).
178 rewrite > (div_mod n p) in \vdash (? ? % ?)
179 [ rewrite > (div_mod m p) in \vdash (? ? % ?)
180 [ rewrite < (sym_plus (m \mod p)).
183 rewrite < (eq_minus_minus_minus_plus ? (n \mod p)).
184 rewrite < minus_plus_m_m.
186 apply times_minus_l*)
193 theorem mod_times: \forall n,m,p:nat.
194 O < p \to mod (n*m) p = mod ((mod n p)*(mod m p)) p.
196 change with (congruent (n*m) ((mod n p)*(mod m p)) p).
197 apply (eq_times_plus_to_congruent ? ? p
198 ((n / p)*p*(m / p) + (n / p)*(m \mod p) + (n \mod p)*(m / p)))
200 | apply (trans_eq ? ? (((n/p)*p+(n \mod p))*((m/p)*p+(m \mod p))))
201 [ apply eq_f2;autobatch(*;apply div_mod.assumption.*)
202 | apply (trans_eq ? ? (((n/p)*p)*((m/p)*p) + (n/p)*p*(m \mod p) +
203 (n \mod p)*((m / p)*p) + (n \mod p)*(m \mod p)))
204 [ apply times_plus_plus
206 [ rewrite < assoc_times.
208 (*rewrite > (assoc_times (n/p) p (m \mod p)).
209 rewrite > (sym_times p (m \mod p)).
210 rewrite < (assoc_times (n/p) (m \mod p) p).
211 rewrite < times_plus_l.
212 rewrite < (assoc_times (n \mod p)).
213 rewrite < times_plus_l.
228 theorem congruent_times: \forall n,m,n1,m1,p. O < p \to congruent n n1 p \to
229 congruent m m1 p \to congruent (n*m) (n1*m1) p.
232 rewrite > (mod_times n m p H).
242 theorem congruent_pi: \forall f:nat \to nat. \forall n,m,p:nat.O < p \to
243 congruent (pi n f m) (pi n (\lambda m. mod (f m) p) m) p.
247 (*apply congruent_n_mod_n.
249 | apply congruent_times
252 (*apply congruent_n_mod_n.
254 | (*NB: QUI AUTO NON RIESCE A CHIUDERE IL GOAL*)