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_new".
17 include "datatypes/constructors.ma".
18 include "nat/minus.ma".
20 let rec mod_aux t m n: nat \def
21 match (leb (S m) n) with
25 [O \Rightarrow m (* if t is large enough this case never happens *)
26 |(S t1) \Rightarrow mod_aux t1 (m-n) n
30 definition mod: nat \to nat \to nat \def
31 \lambda m,n.mod_aux m m n.
33 interpretation "natural remainder" 'module x y =
34 (cic:/matita/nat/div_and_mod_new/mod.con x y).
36 lemma O_to_mod_aux: \forall m,n. mod_aux O m n = m.
38 simplify.elim (leb (S m) n);reflexivity.
41 lemma lt_to_mod_aux: \forall t,m,n. m < n \to mod_aux (S t) m n = m.
44 ( match (leb (S m) n) with
45 [ true \Rightarrow m | false \Rightarrow mod_aux t (m-n) n] = m).
46 rewrite > (le_to_leb_true ? ? H).
50 lemma le_to_mod_aux: \forall t,m,n. n \le m \to
51 mod_aux (S t) m n = mod_aux t (m-n) n.
54 (match (leb (S m) n) with
55 [ true \Rightarrow m | false \Rightarrow mod_aux t (m-n) n] = mod_aux t (m-n) n).
56 apply (leb_elim (S m) n);intro
57 [apply False_ind.apply (le_to_not_lt ? ? H).apply H1
62 let rec div_aux p m n : nat \def
63 match (leb (S m) n) with
68 |(S q) \Rightarrow S (div_aux q (m-n) n)]].
70 definition div : nat \to nat \to nat \def
71 \lambda n,m.div_aux n n m.
73 interpretation "natural divide" 'divide x y =
74 (cic:/matita/nat/div_and_mod_new/div.con x y).
76 theorem lt_mod_aux_m_m:
77 \forall n. O < n \to \forall t,m. m \leq t \to (mod_aux t m n) < n.
80 [rewrite > O_to_mod_aux.
81 apply (le_n_O_elim ? H1).
83 |apply (leb_elim (S m) n);intros
84 [rewrite > lt_to_mod_aux[assumption|assumption]
85 |rewrite > le_to_mod_aux
87 apply le_plus_to_minus.
88 apply (trans_le ? ? ? H2).
89 apply (lt_O_n_elim ? H).intro.
100 theorem lt_mod_m_m: \forall n,m. O < m \to (n \mod m) < m.
102 apply lt_mod_aux_m_m[assumption|apply le_n]
105 lemma mod_aux_O: \forall p,n:nat. mod_aux p n O = n.
109 |simplify.rewrite < minus_n_O.assumption
113 theorem div_aux_mod_aux: \forall m,p,n:nat.
114 (n=(div_aux p n m)*m + (mod_aux p n m)).
117 [intros.rewrite < times_n_O.simplify.apply sym_eq.apply mod_aux_O
119 [simplify.elim (leb n m1);reflexivity
120 |simplify.apply (leb_elim n1 m1);intro
123 rewrite > assoc_plus.
124 rewrite < (H (n1-(S m1))).
125 change with (n1=(S m1)+(n1-(S m1))).
127 apply plus_minus_m_m.
128 change with (m1 < n1).
129 apply not_le_to_lt.exact H1.
135 theorem div_mod: \forall n,m:nat. O < m \to n=(n / m)*m+(n \mod m).
136 intros.apply (div_aux_mod_aux m n n).
139 inductive div_mod_spec (n,m,q,r:nat) : Prop \def
140 div_mod_spec_intro: r < m \to n=q*m+r \to (div_mod_spec n m q r).
143 definition div_mod_spec : nat \to nat \to nat \to nat \to Prop \def
144 \lambda n,m,q,r:nat.r < m \land n=q*m+r).
147 theorem div_mod_spec_to_not_eq_O: \forall n,m,q,r.(div_mod_spec n m q r) \to m \neq O.
148 intros 4.unfold Not.intros.elim H.absurd (le (S r) O)
149 [rewrite < H1.assumption|exact (not_le_Sn_O r)]
152 theorem div_mod_spec_div_mod:
153 \forall n,m. O < m \to (div_mod_spec n m (n / m) (n \mod m)).
156 apply div_mod_spec_intro.
157 apply lt_mod_m_m.assumption.
158 apply div_mod.assumption.
162 theorem div_mod_spec_to_eq :\forall a,b,q,r,q1,r1.
163 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to q = q1.
164 intros.elim H.elim H1.
165 apply (nat_compare_elim q q1);intro
167 cut ((q1-q)*b+r1 = r)
168 [cut (b \leq (q1-q)*b+r1)
170 [apply (lt_to_not_le r b H2 Hcut2)
171 |elim Hcut.assumption
173 |autobatch depth=4. apply (trans_le ? ((q1-q)*b))
175 apply le_SO_minus.exact H6
180 |rewrite < sym_times.
181 rewrite > distr_times_minus.
201 (* the following case is symmetric *)
204 cut (eq nat ((q-q1)*b+r) r1).
205 cut (b \leq (q-q1)*b+r).
207 apply (lt_to_not_le r1 b H4 Hcut2).
208 elim Hcut.assumption.
209 apply (trans_le ? ((q-q1)*b)).
211 apply le_SO_minus.exact H6.
215 rewrite > distr_times_minus.
216 rewrite > plus_minus.
227 theorem div_mod_spec_to_eq2 :\forall a,b,q,r,q1,r1.
228 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to
230 intros.elim H.elim H1.
231 apply (inj_plus_r (q*b)).
233 rewrite > (div_mod_spec_to_eq a b q r q1 r1 H H1).
237 theorem div_mod_spec_times : \forall n,m:nat.div_mod_spec ((S n)*m) (S n) m O.
238 intros.constructor 1.
239 unfold lt.apply le_S_S.apply le_O_n.
240 rewrite < plus_n_O.rewrite < sym_times.reflexivity.
243 (* some properties of div and mod *)
244 theorem div_times: \forall n,m:nat. ((S n)*m) / (S n) = m.
246 apply (div_mod_spec_to_eq ((S n)*m) (S n) ? ? ? O).
247 goal 15. (* ?11 is closed with the following tactics *)
248 apply div_mod_spec_div_mod.
249 unfold lt.apply le_S_S.apply le_O_n.
250 apply div_mod_spec_times.
253 theorem div_n_n: \forall n:nat. O < n \to n / n = S O.
255 apply (div_mod_spec_to_eq n n (n / n) (n \mod n) (S O) O).
256 apply div_mod_spec_div_mod.assumption.
257 constructor 1.assumption.
258 rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity.
261 theorem eq_div_O: \forall n,m. n < m \to n / m = O.
263 apply (div_mod_spec_to_eq n m (n/m) (n \mod m) O n).
264 apply div_mod_spec_div_mod.
265 apply (le_to_lt_to_lt O n m).
266 apply le_O_n.assumption.
267 constructor 1.assumption.reflexivity.
270 theorem mod_n_n: \forall n:nat. O < n \to n \mod n = O.
272 apply (div_mod_spec_to_eq2 n n (n / n) (n \mod n) (S O) O).
273 apply div_mod_spec_div_mod.assumption.
274 constructor 1.assumption.
275 rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity.
278 theorem mod_S: \forall n,m:nat. O < m \to S (n \mod m) < m \to
279 ((S n) \mod m) = S (n \mod m).
281 apply (div_mod_spec_to_eq2 (S n) m ((S n) / m) ((S n) \mod m) (n / m) (S (n \mod m))).
282 apply div_mod_spec_div_mod.assumption.
283 constructor 1.assumption.rewrite < plus_n_Sm.
289 theorem mod_O_n: \forall n:nat.O \mod n = O.
290 intro.elim n.simplify.reflexivity.
291 simplify.reflexivity.
294 theorem lt_to_eq_mod:\forall n,m:nat. n < m \to n \mod m = n.
296 apply (div_mod_spec_to_eq2 n m (n/m) (n \mod m) O n).
297 apply div_mod_spec_div_mod.
298 apply (le_to_lt_to_lt O n m).apply le_O_n.assumption.
300 assumption.reflexivity.
304 theorem injective_times_r: \forall n:nat.injective nat nat (\lambda m:nat.(S n)*m).
305 change with (\forall n,p,q:nat.(S n)*p = (S n)*q \to p=q).
307 rewrite < (div_times n).
308 rewrite < (div_times n q).
309 apply eq_f2.assumption.
313 variant inj_times_r : \forall n,p,q:nat.(S n)*p = (S n)*q \to p=q \def
316 theorem lt_O_to_injective_times_r: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.n*m).
319 apply (lt_O_n_elim n H).intros.
320 apply (inj_times_r m).assumption.
323 variant inj_times_r1:\forall n. O < n \to \forall p,q:nat.n*p = n*q \to p=q
324 \def lt_O_to_injective_times_r.
326 theorem injective_times_l: \forall n:nat.injective nat nat (\lambda m:nat.m*(S n)).
329 apply (inj_times_r n x y).
331 rewrite < (sym_times y).
335 variant inj_times_l : \forall n,p,q:nat. p*(S n) = q*(S n) \to p=q \def
338 theorem lt_O_to_injective_times_l: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.m*n).
341 apply (lt_O_n_elim n H).intros.
342 apply (inj_times_l m).assumption.
345 variant inj_times_l1:\forall n. O < n \to \forall p,q:nat.p*n = q*n \to p=q
346 \def lt_O_to_injective_times_l.
348 (* n_divides computes the pair (div,mod) *)
350 (* p is just an upper bound, acc is an accumulator *)
351 let rec n_divides_aux p n m acc \def
355 [ O \Rightarrow pair nat nat acc n
356 | (S p) \Rightarrow n_divides_aux p (n / m) m (S acc)]
357 | (S a) \Rightarrow pair nat nat acc n].
359 (* n_divides n m = <q,r> if m divides n q times, with remainder r *)
360 definition n_divides \def \lambda n,m:nat.n_divides_aux n n m O.