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".
20 let rec mod_aux p m n: nat \def
26 |(S q) \Rightarrow mod_aux q (m-(S n)) n]].
28 definition mod : nat \to nat \to nat \def
32 | (S p) \Rightarrow mod_aux n n p].
34 interpretation "natural remainder" 'module x y =
35 (cic:/matita/nat/div_and_mod/mod.con x y).
37 let rec div_aux p m n : nat \def
43 |(S q) \Rightarrow S (div_aux q (m-(S n)) n)]].
45 definition div : nat \to nat \to nat \def
49 | (S p) \Rightarrow div_aux n n p].
51 interpretation "natural divide" 'divide x y =
52 (cic:/matita/nat/div_and_mod/div.con x y).
54 theorem le_mod_aux_m_m:
55 \forall p,n,m. n \leq p \to (mod_aux p n m) \leq m.
57 apply (le_n_O_elim n H (\lambda n.(mod_aux O n m) \leq m)).
58 simplify.apply le_O_n.
60 apply (leb_elim n1 m).
61 simplify.intro.assumption.
62 simplify.intro.apply H.
63 cut (n1 \leq (S n) \to n1-(S m) \leq n).
64 apply Hcut.assumption.
66 simplify.apply le_O_n.
67 simplify.apply (trans_le ? n2 n).
68 apply le_minus_m.apply le_S_S_to_le.assumption.
71 theorem lt_mod_m_m: \forall n,m. O < m \to (n \mod m) < m.
72 intros 2.elim m.apply False_ind.
73 apply (not_le_Sn_O O H).
74 simplify.unfold lt.apply le_S_S.apply le_mod_aux_m_m.
78 theorem div_aux_mod_aux: \forall p,n,m:nat.
79 (n=(div_aux p n m)*(S m) + (mod_aux p n m)).
81 simplify.elim (leb n m).
82 simplify.apply refl_eq.
83 simplify.apply refl_eq.
85 apply (leb_elim n1 m).
86 simplify.intro.apply refl_eq.
89 elim (H (n1-(S m)) m).
90 change with (n1=(S m)+(n1-(S m))).
94 apply not_le_to_lt.exact H1.
97 theorem div_mod: \forall n,m:nat. O < m \to n=(n / m)*m+(n \mod m).
98 intros 2.elim m.elim (not_le_Sn_O O H).
100 apply div_aux_mod_aux.
103 inductive div_mod_spec (n,m,q,r:nat) : Prop \def
104 div_mod_spec_intro: r < m \to n=q*m+r \to (div_mod_spec n m q r).
107 definition div_mod_spec : nat \to nat \to nat \to nat \to Prop \def
108 \lambda n,m,q,r:nat.r < m \land n=q*m+r).
111 theorem div_mod_spec_to_not_eq_O: \forall n,m,q,r.(div_mod_spec n m q r) \to m \neq O.
112 intros 4.unfold Not.intros.elim H.absurd (le (S r) O).
113 rewrite < H1.assumption.
114 exact (not_le_Sn_O r).
117 theorem div_mod_spec_div_mod:
118 \forall n,m. O < m \to (div_mod_spec n m (n / m) (n \mod m)).
120 apply div_mod_spec_intro.
121 apply lt_mod_m_m.assumption.
122 apply div_mod.assumption.
125 theorem div_mod_spec_to_eq :\forall a,b,q,r,q1,r1.
126 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to
128 intros.elim H.elim H1.
129 apply (nat_compare_elim q q1).intro.
131 cut (eq nat ((q1-q)*b+r1) r).
132 cut (b \leq (q1-q)*b+r1).
134 apply (lt_to_not_le r b H2 Hcut2).
135 elim Hcut.assumption.
136 apply (trans_le ? ((q1-q)*b)).
138 apply le_SO_minus.exact H6.
142 rewrite > distr_times_minus.
143 rewrite > plus_minus.
154 (* the following case is symmetric *)
157 cut (eq nat ((q-q1)*b+r) r1).
158 cut (b \leq (q-q1)*b+r).
160 apply (lt_to_not_le r1 b H4 Hcut2).
161 elim Hcut.assumption.
162 apply (trans_le ? ((q-q1)*b)).
164 apply le_SO_minus.exact H6.
168 rewrite > distr_times_minus.
169 rewrite > plus_minus.
180 theorem div_mod_spec_to_eq2 :\forall a,b,q,r,q1,r1.
181 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to
183 intros.elim H.elim H1.
184 apply (inj_plus_r (q*b)).
186 rewrite > (div_mod_spec_to_eq a b q r q1 r1 H H1).
190 theorem div_mod_spec_times : \forall n,m:nat.div_mod_spec ((S n)*m) (S n) m O.
191 intros.constructor 1.
192 unfold lt.apply le_S_S.apply le_O_n.
193 rewrite < plus_n_O.rewrite < sym_times.reflexivity.
196 lemma div_plus_times: \forall m,q,r:nat. r < m \to (q*m+r)/ m = q.
198 apply (div_mod_spec_to_eq (q*m+r) m ? ((q*m+r) \mod m) ? r)
199 [apply div_mod_spec_div_mod.
200 apply (le_to_lt_to_lt ? r)
201 [apply le_O_n|assumption]
202 |apply div_mod_spec_intro[assumption|reflexivity]
206 lemma mod_plus_times: \forall m,q,r:nat. r < m \to (q*m+r) \mod m = r.
208 apply (div_mod_spec_to_eq2 (q*m+r) m ((q*m+r)/ m) ((q*m+r) \mod m) q r)
209 [apply div_mod_spec_div_mod.
210 apply (le_to_lt_to_lt ? r)
211 [apply le_O_n|assumption]
212 |apply div_mod_spec_intro[assumption|reflexivity]
215 (* some properties of div and mod *)
216 theorem div_times: \forall n,m:nat. ((S n)*m) / (S n) = m.
218 apply (div_mod_spec_to_eq ((S n)*m) (S n) ? ? ? O);
219 [2: apply div_mod_spec_div_mod.
220 unfold lt.apply le_S_S.apply le_O_n.
222 | apply div_mod_spec_times
226 theorem div_n_n: \forall n:nat. O < n \to n / n = S O.
228 apply (div_mod_spec_to_eq n n (n / n) (n \mod n) (S O) O).
229 apply div_mod_spec_div_mod.assumption.
230 constructor 1.assumption.
231 rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity.
234 theorem eq_div_O: \forall n,m. n < m \to n / m = O.
236 apply (div_mod_spec_to_eq n m (n/m) (n \mod m) O n).
237 apply div_mod_spec_div_mod.
238 apply (le_to_lt_to_lt O n m).
239 apply le_O_n.assumption.
240 constructor 1.assumption.reflexivity.
243 theorem mod_n_n: \forall n:nat. O < n \to n \mod n = O.
245 apply (div_mod_spec_to_eq2 n n (n / n) (n \mod n) (S O) O).
246 apply div_mod_spec_div_mod.assumption.
247 constructor 1.assumption.
248 rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity.
251 theorem mod_S: \forall n,m:nat. O < m \to S (n \mod m) < m \to
252 ((S n) \mod m) = S (n \mod m).
254 apply (div_mod_spec_to_eq2 (S n) m ((S n) / m) ((S n) \mod m) (n / m) (S (n \mod m))).
255 apply div_mod_spec_div_mod.assumption.
256 constructor 1.assumption.rewrite < plus_n_Sm.
262 theorem mod_O_n: \forall n:nat.O \mod n = O.
263 intro.elim n.simplify.reflexivity.
264 simplify.reflexivity.
267 theorem lt_to_eq_mod:\forall n,m:nat. n < m \to n \mod m = n.
269 apply (div_mod_spec_to_eq2 n m (n/m) (n \mod m) O n).
270 apply div_mod_spec_div_mod.
271 apply (le_to_lt_to_lt O n m).apply le_O_n.assumption.
273 assumption.reflexivity.
277 theorem injective_times_r: \forall n:nat.injective nat nat (\lambda m:nat.(S n)*m).
278 change with (\forall n,p,q:nat.(S n)*p = (S n)*q \to p=q).
280 rewrite < (div_times n).
281 rewrite < (div_times n q).
282 apply eq_f2.assumption.
286 variant inj_times_r : \forall n,p,q:nat.(S n)*p = (S n)*q \to p=q \def
289 theorem lt_O_to_injective_times_r: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.n*m).
292 apply (lt_O_n_elim n H).intros.
293 apply (inj_times_r m).assumption.
296 variant inj_times_r1:\forall n. O < n \to \forall p,q:nat.n*p = n*q \to p=q
297 \def lt_O_to_injective_times_r.
299 theorem injective_times_l: \forall n:nat.injective nat nat (\lambda m:nat.m*(S n)).
302 apply (inj_times_r n x y).
304 rewrite < (sym_times y).
308 variant inj_times_l : \forall n,p,q:nat. p*(S n) = q*(S n) \to p=q \def
311 theorem lt_O_to_injective_times_l: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.m*n).
314 apply (lt_O_n_elim n H).intros.
315 apply (inj_times_l m).assumption.
318 variant inj_times_l1:\forall n. O < n \to \forall p,q:nat.p*n = q*n \to p=q
319 \def lt_O_to_injective_times_l.
321 (* n_divides computes the pair (div,mod) *)
323 (* p is just an upper bound, acc is an accumulator *)
324 let rec n_divides_aux p n m acc \def
328 [ O \Rightarrow pair nat nat acc n
329 | (S p) \Rightarrow n_divides_aux p (n / m) m (S acc)]
330 | (S a) \Rightarrow pair nat nat acc n].
332 (* n_divides n m = <q,r> if m divides n q times, with remainder r *)
333 definition n_divides \def \lambda n,m:nat.n_divides_aux n n m O.