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_auto/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 =
35 (cic:/matita/library_auto/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/library_auto/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.
58 [ apply (le_n_O_elim n H (\lambda n.(mod_aux O n m) \leq m)).
63 apply (leb_elim n1 m);simplify;intro
66 cut (n1 \leq (S n) \to n1-(S m) \leq n)
70 | elim n1;simplify;auto
72 | apply (trans_le ? n2 n)
83 theorem lt_mod_m_m: \forall n,m. O < m \to (n \mod m) < m.
87 apply (not_le_Sn_O O H)
97 theorem div_aux_mod_aux: \forall p,n,m:nat.
98 (n=(div_aux p n m)*(S m) + (mod_aux p n m)).
101 [ elim (leb n m);auto
102 (*simplify;apply refl_eq.*)
103 | apply (leb_elim n1 m);simplify;intro
105 | rewrite > assoc_plus.
106 elim (H (n1-(S m)) m).
107 change with (n1=(S m)+(n1-(S m))).
110 (*apply plus_minus_m_m.
111 change with (m < n1).
118 theorem div_mod: \forall n,m:nat. O < m \to n=(n / m)*m+(n \mod m).
121 [ elim (not_le_Sn_O O H)
123 apply div_aux_mod_aux
127 inductive div_mod_spec (n,m,q,r:nat) : Prop \def
128 div_mod_spec_intro: r < m \to n=q*m+r \to (div_mod_spec n m q r).
131 definition div_mod_spec : nat \to nat \to nat \to nat \to Prop \def
132 \lambda n,m,q,r:nat.r < m \land n=q*m+r).
135 theorem div_mod_spec_to_not_eq_O: \forall n,m,q,r.(div_mod_spec n m q r) \to m \neq O.
140 absurd (le (S r) O);auto.
143 | exact (not_le_Sn_O r).
147 theorem div_mod_spec_div_mod:
148 \forall n,m. O < m \to (div_mod_spec n m (n / m) (n \mod m)).
151 (*apply div_mod_spec_intro
159 theorem div_mod_spec_to_eq :\forall a,b,q,r,q1,r1.
160 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to
165 apply (nat_compare_elim q q1)
168 cut (eq nat ((q1-q)*b+r1) r)
169 [ cut (b \leq (q1-q)*b+r1)
171 [ apply (lt_to_not_le r b H2 Hcut2)
175 | apply (trans_le ? ((q1-q)*b));auto
176 (*[ apply le_times_n.
179 | rewrite < sym_plus.
183 | rewrite < sym_times.
184 rewrite > distr_times_minus.
185 rewrite > plus_minus;auto
186 (*[ rewrite > sym_times.
200 | (* the following case is symmetric *)
203 cut (eq nat ((q-q1)*b+r) r1)
204 [ cut (b \leq (q-q1)*b+r)
206 [ apply (lt_to_not_le r1 b H4 Hcut2)
210 | apply (trans_le ? ((q-q1)*b));auto
211 (*[ apply le_times_n.
214 | rewrite < sym_plus.
218 | rewrite < sym_times.
219 rewrite > distr_times_minus.
220 rewrite > plus_minus;auto
221 (*[ rewrite > sym_times.
234 theorem div_mod_spec_to_eq2 :\forall a,b,q,r,q1,r1.
235 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to
240 apply (inj_plus_r (q*b)).
242 rewrite > (div_mod_spec_to_eq a b q r q1 r1 H H1).
246 theorem div_mod_spec_times : \forall n,m:nat.div_mod_spec ((S n)*m) (S n) m O.
253 | rewrite < plus_n_O.
260 (*il corpo del seguente teorema non e' stato strutturato *)
261 (* some properties of div and mod *)
262 theorem div_times: \forall n,m:nat. ((S n)*m) / (S n) = m.
264 apply (div_mod_spec_to_eq ((S n)*m) (S n) ? ? ? O);
265 [2: apply div_mod_spec_div_mod.auto.
269 (*unfold lt.apply le_S_S.apply le_O_n.
270 apply div_mod_spec_times.*)
273 theorem div_n_n: \forall n:nat. O < n \to n / n = S O.
275 apply (div_mod_spec_to_eq n n (n / n) (n \mod n) (S O) O);auto.
276 (*[ apply div_mod_spec_div_mod.
280 | rewrite < plus_n_O.
288 theorem eq_div_O: \forall n,m. n < m \to n / m = O.
290 apply (div_mod_spec_to_eq n m (n/m) (n \mod m) O n);auto.
291 (*[ apply div_mod_spec_div_mod.
292 apply (le_to_lt_to_lt O n m)
303 theorem mod_n_n: \forall n:nat. O < n \to n \mod n = O.
305 apply (div_mod_spec_to_eq2 n n (n / n) (n \mod n) (S O) O);auto.
306 (*[ apply div_mod_spec_div_mod.
310 | rewrite < plus_n_O.
318 theorem mod_S: \forall n,m:nat. O < m \to S (n \mod m) < m \to
319 ((S n) \mod m) = S (n \mod m).
321 apply (div_mod_spec_to_eq2 (S n) m ((S n) / m) ((S n) \mod m) (n / m) (S (n \mod m)))
323 (*apply div_mod_spec_div_mod.
327 | rewrite < plus_n_Sm.
336 theorem mod_O_n: \forall n:nat.O \mod n = O.
339 (*simplify;reflexivity*)
343 theorem lt_to_eq_mod:\forall n,m:nat. n < m \to n \mod m = n.
345 apply (div_mod_spec_to_eq2 n m (n/m) (n \mod m) O n);auto.
346 (*[ apply div_mod_spec_div_mod.
347 apply (le_to_lt_to_lt O n m)
359 theorem injective_times_r: \forall n:nat.injective nat nat (\lambda m:nat.(S n)*m).
360 change with (\forall n,p,q:nat.(S n)*p = (S n)*q \to p=q).
362 rewrite < (div_times n).
364 (*rewrite < (div_times n q).
371 variant inj_times_r : \forall n,p,q:nat.(S n)*p = (S n)*q \to p=q \def
374 theorem lt_O_to_injective_times_r: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.n*m).
377 apply (lt_O_n_elim n H).
380 (*apply (inj_times_r m).
384 variant inj_times_r1:\forall n. O < n \to \forall p,q:nat.n*p = n*q \to p=q
385 \def lt_O_to_injective_times_r.
387 theorem injective_times_l: \forall n:nat.injective nat nat (\lambda m:nat.m*(S n)).
391 (*apply (inj_times_r n x y).
393 rewrite < (sym_times y).
397 variant inj_times_l : \forall n,p,q:nat. p*(S n) = q*(S n) \to p=q \def
400 theorem lt_O_to_injective_times_l: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.m*n).
403 apply (lt_O_n_elim n H).
406 (*apply (inj_times_l m).
410 variant inj_times_l1:\forall n. O < n \to \forall p,q:nat.p*n = q*n \to p=q
411 \def lt_O_to_injective_times_l.
413 (* n_divides computes the pair (div,mod) *)
415 (* p is just an upper bound, acc is an accumulator *)
416 let rec n_divides_aux p n m acc \def
420 [ O \Rightarrow pair nat nat acc n
421 | (S p) \Rightarrow n_divides_aux p (n / m) m (S acc)]
422 | (S a) \Rightarrow pair nat nat acc n].
424 (* n_divides n m = <q,r> if m divides n q times, with remainder r *)
425 definition n_divides \def \lambda n,m:nat.n_divides_aux n n m O.