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/nat/div_and_mod".
17 include "datatypes/constructors.ma".
18 include "nat/minus.ma".
21 let rec mod_aux p m n: nat \def
27 |(S q) \Rightarrow mod_aux q (m-(S n)) n]].
29 definition mod : nat \to nat \to nat \def
33 | (S p) \Rightarrow mod_aux n n p].
35 interpretation "natural remainder" 'module x y =
36 (cic:/matita/nat/div_and_mod/mod.con x y).
38 let rec div_aux p m n : nat \def
44 |(S q) \Rightarrow S (div_aux q (m-(S n)) n)]].
46 definition div : nat \to nat \to nat \def
50 | (S p) \Rightarrow div_aux n n p].
52 interpretation "natural divide" 'divide x y =
53 (cic:/matita/nat/div_and_mod/div.con x y).
55 theorem le_mod_aux_m_m:
56 \forall p,n,m. n \leq p \to (mod_aux p n m) \leq m.
58 apply (le_n_O_elim n H (\lambda n.(mod_aux O n m) \leq m)).
59 simplify.apply le_O_n.
61 apply (leb_elim n1 m).
62 simplify.intro.assumption.
63 simplify.intro.apply H.
64 cut (n1 \leq (S n) \to n1-(S m) \leq n).
65 apply Hcut.assumption.
67 simplify.apply le_O_n.
68 simplify.apply (trans_le ? n2 n).
69 apply le_minus_m.apply le_S_S_to_le.assumption.
72 theorem lt_mod_m_m: \forall n,m. O < m \to (n \mod m) < m.
73 intros 2.elim m.apply False_ind.
74 apply (not_le_Sn_O O H).
75 simplify.unfold lt.apply le_S_S.apply le_mod_aux_m_m.
79 theorem div_aux_mod_aux: \forall p,n,m:nat.
80 (n=(div_aux p n m)*(S m) + (mod_aux p n m)).
82 simplify.elim (leb n m).
83 simplify.apply refl_eq.
84 simplify.apply refl_eq.
86 apply (leb_elim n1 m).
87 simplify.intro.apply refl_eq.
90 elim (H (n1-(S m)) m).
91 change with (n1=(S m)+(n1-(S m))).
95 apply not_le_to_lt.exact H1.
98 theorem div_mod: \forall n,m:nat. O < m \to n=(n / m)*m+(n \mod m).
99 intros 2.elim m.elim (not_le_Sn_O O H).
101 apply div_aux_mod_aux.
104 theorem eq_times_div_minus_mod:
105 \forall a,b:nat. O \lt b \to
106 (a /b)*b = a - (a \mod b).
108 rewrite > (div_mod a b) in \vdash (? ? ? (? % ?))
109 [ apply (minus_plus_m_m (times (div a b) b) (mod a b))
114 inductive div_mod_spec (n,m,q,r:nat) : Prop \def
115 div_mod_spec_intro: r < m \to n=q*m+r \to (div_mod_spec n m q r).
118 definition div_mod_spec : nat \to nat \to nat \to nat \to Prop \def
119 \lambda n,m,q,r:nat.r < m \land n=q*m+r).
122 theorem div_mod_spec_to_not_eq_O: \forall n,m,q,r.(div_mod_spec n m q r) \to m \neq O.
123 intros 4.unfold Not.intros.elim H.absurd (le (S r) O).
124 rewrite < H1.assumption.
125 exact (not_le_Sn_O r).
128 theorem div_mod_spec_div_mod:
129 \forall n,m. O < m \to (div_mod_spec n m (n / m) (n \mod m)).
131 apply div_mod_spec_intro.
132 apply lt_mod_m_m.assumption.
133 apply div_mod.assumption.
136 theorem div_mod_spec_to_eq :\forall a,b,q,r,q1,r1.
137 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to
139 intros.elim H.elim H1.
140 apply (nat_compare_elim q q1).intro.
142 cut (eq nat ((q1-q)*b+r1) r).
143 cut (b \leq (q1-q)*b+r1).
145 apply (lt_to_not_le r b H2 Hcut2).
146 elim Hcut.assumption.
147 apply (trans_le ? ((q1-q)*b)).
149 apply le_SO_minus.exact H6.
153 rewrite > distr_times_minus.
154 rewrite > plus_minus.
165 (* the following case is symmetric *)
168 cut (eq nat ((q-q1)*b+r) r1).
169 cut (b \leq (q-q1)*b+r).
171 apply (lt_to_not_le r1 b H4 Hcut2).
172 elim Hcut.assumption.
173 apply (trans_le ? ((q-q1)*b)).
175 apply le_SO_minus.exact H6.
179 rewrite > distr_times_minus.
180 rewrite > plus_minus.
191 theorem div_mod_spec_to_eq2 :\forall a,b,q,r,q1,r1.
192 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to
194 intros.elim H.elim H1.
195 apply (inj_plus_r (q*b)).
197 rewrite > (div_mod_spec_to_eq a b q r q1 r1 H H1).
201 theorem div_mod_spec_times : \forall n,m:nat.div_mod_spec ((S n)*m) (S n) m O.
202 intros.constructor 1.
203 unfold lt.apply le_S_S.apply le_O_n.
204 rewrite < plus_n_O.rewrite < sym_times.reflexivity.
207 lemma div_plus_times: \forall m,q,r:nat. r < m \to (q*m+r)/ m = q.
209 apply (div_mod_spec_to_eq (q*m+r) m ? ((q*m+r) \mod m) ? r)
210 [apply div_mod_spec_div_mod.
211 apply (le_to_lt_to_lt ? r)
212 [apply le_O_n|assumption]
213 |apply div_mod_spec_intro[assumption|reflexivity]
217 lemma mod_plus_times: \forall m,q,r:nat. r < m \to (q*m+r) \mod m = r.
219 apply (div_mod_spec_to_eq2 (q*m+r) m ((q*m+r)/ m) ((q*m+r) \mod m) q r)
220 [apply div_mod_spec_div_mod.
221 apply (le_to_lt_to_lt ? r)
222 [apply le_O_n|assumption]
223 |apply div_mod_spec_intro[assumption|reflexivity]
226 (* some properties of div and mod *)
227 theorem div_times: \forall n,m:nat. ((S n)*m) / (S n) = m.
229 apply (div_mod_spec_to_eq ((S n)*m) (S n) ? ? ? O);
230 [2: apply div_mod_spec_div_mod.
231 unfold lt.apply le_S_S.apply le_O_n.
233 | apply div_mod_spec_times
237 (*a simple variant of div_times theorem *)
238 theorem lt_O_to_div_times: \forall a,b:nat. O \lt b \to
242 rewrite > (S_pred b H).
246 theorem div_n_n: \forall n:nat. O < n \to n / n = S O.
248 apply (div_mod_spec_to_eq n n (n / n) (n \mod n) (S O) O).
249 apply div_mod_spec_div_mod.assumption.
250 constructor 1.assumption.
251 rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity.
254 theorem eq_div_O: \forall n,m. n < m \to n / m = O.
256 apply (div_mod_spec_to_eq n m (n/m) (n \mod m) O n).
257 apply div_mod_spec_div_mod.
258 apply (le_to_lt_to_lt O n m).
259 apply le_O_n.assumption.
260 constructor 1.assumption.reflexivity.
263 theorem mod_n_n: \forall n:nat. O < n \to n \mod n = O.
265 apply (div_mod_spec_to_eq2 n n (n / n) (n \mod n) (S O) O).
266 apply div_mod_spec_div_mod.assumption.
267 constructor 1.assumption.
268 rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity.
271 theorem mod_S: \forall n,m:nat. O < m \to S (n \mod m) < m \to
272 ((S n) \mod m) = S (n \mod m).
274 apply (div_mod_spec_to_eq2 (S n) m ((S n) / m) ((S n) \mod m) (n / m) (S (n \mod m))).
275 apply div_mod_spec_div_mod.assumption.
276 constructor 1.assumption.rewrite < plus_n_Sm.
282 theorem mod_O_n: \forall n:nat.O \mod n = O.
283 intro.elim n.simplify.reflexivity.
284 simplify.reflexivity.
287 theorem lt_to_eq_mod:\forall n,m:nat. n < m \to n \mod m = n.
289 apply (div_mod_spec_to_eq2 n m (n/m) (n \mod m) O n).
290 apply div_mod_spec_div_mod.
291 apply (le_to_lt_to_lt O n m).apply le_O_n.assumption.
293 assumption.reflexivity.
296 theorem mod_SO: \forall n:nat. mod n (S O) = O.
305 theorem div_SO: \forall n:nat. div n (S O) = n.
307 rewrite > (div_mod ? (S O)) in \vdash (? ? ? %)
315 theorem le_div: \forall n,m. O < n \to m/n \le m.
317 rewrite > (div_mod m n) in \vdash (? ? %)
318 [apply (trans_le ? (m/n*n))
319 [rewrite > times_n_SO in \vdash (? % ?).
321 [apply le_n|assumption]
329 theorem injective_times_r: \forall n:nat.injective nat nat (\lambda m:nat.(S n)*m).
330 change with (\forall n,p,q:nat.(S n)*p = (S n)*q \to p=q).
332 rewrite < (div_times n).
333 rewrite < (div_times n q).
334 apply eq_f2.assumption.
338 variant inj_times_r : \forall n,p,q:nat.(S n)*p = (S n)*q \to p=q \def
341 theorem lt_O_to_injective_times_r: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.n*m).
344 apply (lt_O_n_elim n H).intros.
345 apply (inj_times_r m).assumption.
348 variant inj_times_r1:\forall n. O < n \to \forall p,q:nat.n*p = n*q \to p=q
349 \def lt_O_to_injective_times_r.
351 theorem injective_times_l: \forall n:nat.injective nat nat (\lambda m:nat.m*(S n)).
354 apply (inj_times_r n x y).
356 rewrite < (sym_times y).
360 variant inj_times_l : \forall n,p,q:nat. p*(S n) = q*(S n) \to p=q \def
363 theorem lt_O_to_injective_times_l: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.m*n).
366 apply (lt_O_n_elim n H).intros.
367 apply (inj_times_l m).assumption.
370 variant inj_times_l1:\forall n. O < n \to \forall p,q:nat.p*n = q*n \to p=q
371 \def lt_O_to_injective_times_l.
373 (* n_divides computes the pair (div,mod) *)
375 (* p is just an upper bound, acc is an accumulator *)
376 let rec n_divides_aux p n m acc \def
380 [ O \Rightarrow pair nat nat acc n
381 | (S p) \Rightarrow n_divides_aux p (n / m) m (S acc)]
382 | (S a) \Rightarrow pair nat nat acc n].
384 (* n_divides n m = <q,r> if m divides n q times, with remainder r *)
385 definition n_divides \def \lambda n,m:nat.n_divides_aux n n m O.