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/div_and_mod".
17 include "datatypes/constructors.ma".
18 include "auto/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 = (mod x y).
36 let rec div_aux p m n : nat \def
42 |(S q) \Rightarrow S (div_aux q (m-(S n)) n)]].
44 definition div : nat \to nat \to nat \def
48 | (S p) \Rightarrow div_aux n n p].
50 interpretation "natural divide" 'divide x y = (div x y).
52 theorem le_mod_aux_m_m:
53 \forall p,n,m. n \leq p \to (mod_aux p n m) \leq m.
56 [ apply (le_n_O_elim n H (\lambda n.(mod_aux O n m) \leq m)).
61 apply (leb_elim n1 m);simplify;intro
64 cut (n1 \leq (S n) \to n1-(S m) \leq n)
68 | elim n1;simplify;autobatch
70 | apply (trans_le ? n2 n)
81 theorem lt_mod_m_m: \forall n,m. O < m \to (n \mod m) < m.
85 apply (not_le_Sn_O O H)
95 theorem div_aux_mod_aux: \forall p,n,m:nat.
96 (n=(div_aux p n m)*(S m) + (mod_aux p n m)).
99 [ elim (leb n m);autobatch
100 (*simplify;apply refl_eq.*)
101 | apply (leb_elim n1 m);simplify;intro
103 | rewrite > assoc_plus.
104 elim (H (n1-(S m)) m).
105 change with (n1=(S m)+(n1-(S m))).
108 (*apply plus_minus_m_m.
109 change with (m < n1).
116 theorem div_mod: \forall n,m:nat. O < m \to n=(n / m)*m+(n \mod m).
119 [ elim (not_le_Sn_O O H)
121 apply div_aux_mod_aux
125 inductive div_mod_spec (n,m,q,r:nat) : Prop \def
126 div_mod_spec_intro: r < m \to n=q*m+r \to (div_mod_spec n m q r).
129 definition div_mod_spec : nat \to nat \to nat \to nat \to Prop \def
130 \lambda n,m,q,r:nat.r < m \land n=q*m+r).
133 theorem div_mod_spec_to_not_eq_O: \forall n,m,q,r.(div_mod_spec n m q r) \to m \neq O.
138 absurd (le (S r) O);autobatch.
141 | exact (not_le_Sn_O r).
145 theorem div_mod_spec_div_mod:
146 \forall n,m. O < m \to (div_mod_spec n m (n / m) (n \mod m)).
149 (*apply div_mod_spec_intro
157 theorem div_mod_spec_to_eq :\forall a,b,q,r,q1,r1.
158 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to
163 apply (nat_compare_elim q q1)
166 cut (eq nat ((q1-q)*b+r1) r)
167 [ cut (b \leq (q1-q)*b+r1)
169 [ apply (lt_to_not_le r b H2 Hcut2)
173 | apply (trans_le ? ((q1-q)*b));autobatch
174 (*[ apply le_times_n.
177 | rewrite < sym_plus.
181 | rewrite < sym_times.
182 rewrite > distr_times_minus.
183 rewrite > plus_minus;autobatch
184 (*[ rewrite > sym_times.
198 | (* the following case is symmetric *)
201 cut (eq nat ((q-q1)*b+r) r1)
202 [ cut (b \leq (q-q1)*b+r)
204 [ apply (lt_to_not_le r1 b H4 Hcut2)
208 | apply (trans_le ? ((q-q1)*b));autobatch
209 (*[ apply le_times_n.
212 | rewrite < sym_plus.
216 | rewrite < sym_times.
217 rewrite > distr_times_minus.
218 rewrite > plus_minus;autobatch
219 (*[ rewrite > sym_times.
232 theorem div_mod_spec_to_eq2 :\forall a,b,q,r,q1,r1.
233 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to
238 apply (inj_plus_r (q*b)).
240 rewrite > (div_mod_spec_to_eq a b q r q1 r1 H H1).
244 theorem div_mod_spec_times : \forall n,m:nat.div_mod_spec ((S n)*m) (S n) m O.
251 | rewrite < plus_n_O.
258 (*il corpo del seguente teorema non e' stato strutturato *)
259 (* some properties of div and mod *)
260 theorem div_times: \forall n,m:nat. ((S n)*m) / (S n) = m.
262 apply (div_mod_spec_to_eq ((S n)*m) (S n) ? ? ? O);
263 [2: apply div_mod_spec_div_mod.autobatch.
267 (*unfold lt.apply le_S_S.apply le_O_n.
268 apply div_mod_spec_times.*)
271 theorem div_n_n: \forall n:nat. O < n \to n / n = S O.
273 apply (div_mod_spec_to_eq n n (n / n) (n \mod n) (S O) O);autobatch.
274 (*[ apply div_mod_spec_div_mod.
278 | rewrite < plus_n_O.
286 theorem eq_div_O: \forall n,m. n < m \to n / m = O.
288 apply (div_mod_spec_to_eq n m (n/m) (n \mod m) O n);autobatch.
289 (*[ apply div_mod_spec_div_mod.
290 apply (le_to_lt_to_lt O n m)
301 theorem mod_n_n: \forall n:nat. O < n \to n \mod n = O.
303 apply (div_mod_spec_to_eq2 n n (n / n) (n \mod n) (S O) O);autobatch.
304 (*[ apply div_mod_spec_div_mod.
308 | rewrite < plus_n_O.
316 theorem mod_S: \forall n,m:nat. O < m \to S (n \mod m) < m \to
317 ((S n) \mod m) = S (n \mod m).
319 apply (div_mod_spec_to_eq2 (S n) m ((S n) / m) ((S n) \mod m) (n / m) (S (n \mod m)))
321 (*apply div_mod_spec_div_mod.
325 | rewrite < plus_n_Sm.
334 theorem mod_O_n: \forall n:nat.O \mod n = O.
337 (*simplify;reflexivity*)
341 theorem lt_to_eq_mod:\forall n,m:nat. n < m \to n \mod m = n.
343 apply (div_mod_spec_to_eq2 n m (n/m) (n \mod m) O n);autobatch.
344 (*[ apply div_mod_spec_div_mod.
345 apply (le_to_lt_to_lt O n m)
357 theorem injective_times_r: \forall n:nat.injective nat nat (\lambda m:nat.(S n)*m).
358 change with (\forall n,p,q:nat.(S n)*p = (S n)*q \to p=q).
360 rewrite < (div_times n).
362 (*rewrite < (div_times n q).
369 variant inj_times_r : \forall n,p,q:nat.(S n)*p = (S n)*q \to p=q \def
372 theorem lt_O_to_injective_times_r: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.n*m).
375 apply (lt_O_n_elim n H).
378 (*apply (inj_times_r m).
382 variant inj_times_r1:\forall n. O < n \to \forall p,q:nat.n*p = n*q \to p=q
383 \def lt_O_to_injective_times_r.
385 theorem injective_times_l: \forall n:nat.injective nat nat (\lambda m:nat.m*(S n)).
389 (*apply (inj_times_r n x y).
391 rewrite < (sym_times y).
395 variant inj_times_l : \forall n,p,q:nat. p*(S n) = q*(S n) \to p=q \def
398 theorem lt_O_to_injective_times_l: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.m*n).
401 apply (lt_O_n_elim n H).
404 (*apply (inj_times_l m).
408 variant inj_times_l1:\forall n. O < n \to \forall p,q:nat.p*n = q*n \to p=q
409 \def lt_O_to_injective_times_l.
411 (* n_divides computes the pair (div,mod) *)
413 (* p is just an upper bound, acc is an accumulator *)
414 let rec n_divides_aux p n m acc \def
418 [ O \Rightarrow pair nat nat acc n
419 | (S p) \Rightarrow n_divides_aux p (n / m) m (S acc)]
420 | (S a) \Rightarrow pair nat nat acc n].
422 (* n_divides n m = <q,r> if m divides n q times, with remainder r *)
423 definition n_divides \def \lambda n,m:nat.n_divides_aux n n m O.