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 inductive div_mod_spec (n,m,q,r:nat) : Prop \def
105 div_mod_spec_intro: r < m \to n=q*m+r \to (div_mod_spec n m q r).
108 definition div_mod_spec : nat \to nat \to nat \to nat \to Prop \def
109 \lambda n,m,q,r:nat.r < m \land n=q*m+r).
112 theorem div_mod_spec_to_not_eq_O: \forall n,m,q,r.(div_mod_spec n m q r) \to m \neq O.
113 intros 4.unfold Not.intros.elim H.absurd (le (S r) O).
114 rewrite < H1.assumption.
115 exact (not_le_Sn_O r).
118 theorem div_mod_spec_div_mod:
119 \forall n,m. O < m \to (div_mod_spec n m (n / m) (n \mod m)).
121 apply div_mod_spec_intro.
122 apply lt_mod_m_m.assumption.
123 apply div_mod.assumption.
126 theorem div_mod_spec_to_eq :\forall a,b,q,r,q1,r1.
127 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to
129 intros.elim H.elim H1.
130 apply (nat_compare_elim q q1).intro.
132 cut (eq nat ((q1-q)*b+r1) r).
133 cut (b \leq (q1-q)*b+r1).
135 apply (lt_to_not_le r b H2 Hcut2).
136 elim Hcut.assumption.
137 apply (trans_le ? ((q1-q)*b)).
139 apply le_SO_minus.exact H6.
143 rewrite > distr_times_minus.
144 rewrite > plus_minus.
155 (* the following case is symmetric *)
158 cut (eq nat ((q-q1)*b+r) r1).
159 cut (b \leq (q-q1)*b+r).
161 apply (lt_to_not_le r1 b H4 Hcut2).
162 elim Hcut.assumption.
163 apply (trans_le ? ((q-q1)*b)).
165 apply le_SO_minus.exact H6.
169 rewrite > distr_times_minus.
170 rewrite > plus_minus.
181 theorem div_mod_spec_to_eq2 :\forall a,b,q,r,q1,r1.
182 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to
184 intros.elim H.elim H1.
185 apply (inj_plus_r (q*b)).
187 rewrite > (div_mod_spec_to_eq a b q r q1 r1 H H1).
191 theorem div_mod_spec_times : \forall n,m:nat.div_mod_spec ((S n)*m) (S n) m O.
192 intros.constructor 1.
193 unfold lt.apply le_S_S.apply le_O_n.
194 rewrite < plus_n_O.rewrite < sym_times.reflexivity.
197 lemma div_plus_times: \forall m,q,r:nat. r < m \to (q*m+r)/ m = q.
199 apply (div_mod_spec_to_eq (q*m+r) m ? ((q*m+r) \mod m) ? r)
200 [apply div_mod_spec_div_mod.
201 apply (le_to_lt_to_lt ? r)
202 [apply le_O_n|assumption]
203 |apply div_mod_spec_intro[assumption|reflexivity]
207 lemma mod_plus_times: \forall m,q,r:nat. r < m \to (q*m+r) \mod m = r.
209 apply (div_mod_spec_to_eq2 (q*m+r) m ((q*m+r)/ m) ((q*m+r) \mod m) q 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]
216 (* some properties of div and mod *)
217 theorem div_times: \forall n,m:nat. ((S n)*m) / (S n) = m.
219 apply (div_mod_spec_to_eq ((S n)*m) (S n) ? ? ? O);
220 [2: apply div_mod_spec_div_mod.
221 unfold lt.apply le_S_S.apply le_O_n.
223 | apply div_mod_spec_times
227 theorem div_n_n: \forall n:nat. O < n \to n / n = S O.
229 apply (div_mod_spec_to_eq n n (n / n) (n \mod n) (S O) O).
230 apply div_mod_spec_div_mod.assumption.
231 constructor 1.assumption.
232 rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity.
235 theorem eq_div_O: \forall n,m. n < m \to n / m = O.
237 apply (div_mod_spec_to_eq n m (n/m) (n \mod m) O n).
238 apply div_mod_spec_div_mod.
239 apply (le_to_lt_to_lt O n m).
240 apply le_O_n.assumption.
241 constructor 1.assumption.reflexivity.
244 theorem mod_n_n: \forall n:nat. O < n \to n \mod n = O.
246 apply (div_mod_spec_to_eq2 n n (n / n) (n \mod n) (S O) O).
247 apply div_mod_spec_div_mod.assumption.
248 constructor 1.assumption.
249 rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity.
252 theorem mod_S: \forall n,m:nat. O < m \to S (n \mod m) < m \to
253 ((S n) \mod m) = S (n \mod m).
255 apply (div_mod_spec_to_eq2 (S n) m ((S n) / m) ((S n) \mod m) (n / m) (S (n \mod m))).
256 apply div_mod_spec_div_mod.assumption.
257 constructor 1.assumption.rewrite < plus_n_Sm.
263 theorem mod_O_n: \forall n:nat.O \mod n = O.
264 intro.elim n.simplify.reflexivity.
265 simplify.reflexivity.
268 theorem lt_to_eq_mod:\forall n,m:nat. n < m \to n \mod m = n.
270 apply (div_mod_spec_to_eq2 n m (n/m) (n \mod m) O n).
271 apply div_mod_spec_div_mod.
272 apply (le_to_lt_to_lt O n m).apply le_O_n.assumption.
274 assumption.reflexivity.
278 theorem injective_times_r: \forall n:nat.injective nat nat (\lambda m:nat.(S n)*m).
279 change with (\forall n,p,q:nat.(S n)*p = (S n)*q \to p=q).
281 rewrite < (div_times n).
282 rewrite < (div_times n q).
283 apply eq_f2.assumption.
287 variant inj_times_r : \forall n,p,q:nat.(S n)*p = (S n)*q \to p=q \def
290 theorem lt_O_to_injective_times_r: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.n*m).
293 apply (lt_O_n_elim n H).intros.
294 apply (inj_times_r m).assumption.
297 variant inj_times_r1:\forall n. O < n \to \forall p,q:nat.n*p = n*q \to p=q
298 \def lt_O_to_injective_times_r.
300 theorem injective_times_l: \forall n:nat.injective nat nat (\lambda m:nat.m*(S n)).
303 apply (inj_times_r n x y).
305 rewrite < (sym_times y).
309 variant inj_times_l : \forall n,p,q:nat. p*(S n) = q*(S n) \to p=q \def
312 theorem lt_O_to_injective_times_l: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.m*n).
315 apply (lt_O_n_elim n H).intros.
316 apply (inj_times_l m).assumption.
319 variant inj_times_l1:\forall n. O < n \to \forall p,q:nat.p*n = q*n \to p=q
320 \def lt_O_to_injective_times_l.
322 (* n_divides computes the pair (div,mod) *)
324 (* p is just an upper bound, acc is an accumulator *)
325 let rec n_divides_aux p n m acc \def
329 [ O \Rightarrow pair nat nat acc n
330 | (S p) \Rightarrow n_divides_aux p (n / m) m (S acc)]
331 | (S a) \Rightarrow pair nat nat acc n].
333 (* n_divides n m = <q,r> if m divides n q times, with remainder r *)
334 definition n_divides \def \lambda n,m:nat.n_divides_aux n n m O.
337 (*a simple variant of div_times theorem *)
338 theorem div_times_ltO: \forall a,b:nat. O \lt b \to
342 rewrite > (S_pred b H).