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 "nat/minus.ma".
19 let rec mod_aux p m n: nat \def
25 |(S q) \Rightarrow mod_aux q (m-(S n)) n]].
27 definition mod : nat \to nat \to nat \def
31 | (S p) \Rightarrow mod_aux n n p].
33 interpretation "natural remainder" 'module x y =
34 (cic:/matita/nat/div_and_mod/mod.con 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 =
51 (cic:/matita/nat/div_and_mod/div.con x y).
53 theorem le_mod_aux_m_m:
54 \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)).
57 simplify.apply le_O_n.
59 apply (leb_elim n1 m).
60 simplify.intro.assumption.
61 simplify.intro.apply H.
62 cut (n1 \leq (S n) \to n1-(S m) \leq n).
63 apply Hcut.assumption.
65 simplify.apply le_O_n.
66 simplify.apply (trans_le ? n2 n).
67 apply le_minus_m.apply le_S_S_to_le.assumption.
70 theorem lt_mod_m_m: \forall n,m. O < m \to (n \mod m) < m.
71 intros 2.elim m.apply False_ind.
72 apply (not_le_Sn_O O H).
73 simplify.unfold lt.apply le_S_S.apply le_mod_aux_m_m.
77 theorem div_aux_mod_aux: \forall p,n,m:nat.
78 (n=(div_aux p n m)*(S m) + (mod_aux p n m)).
80 simplify.elim (leb n m).
81 simplify.apply refl_eq.
82 simplify.apply refl_eq.
84 apply (leb_elim n1 m).
85 simplify.intro.apply refl_eq.
88 elim (H (n1-(S m)) m).
89 change with (n1=(S m)+(n1-(S m))).
93 apply not_le_to_lt.exact H1.
96 theorem div_mod: \forall n,m:nat. O < m \to n=(n / m)*m+(n \mod m).
97 intros 2.elim m.elim (not_le_Sn_O O H).
99 apply div_aux_mod_aux.
102 inductive div_mod_spec (n,m,q,r:nat) : Prop \def
103 div_mod_spec_intro: r < m \to n=q*m+r \to (div_mod_spec n m q r).
106 definition div_mod_spec : nat \to nat \to nat \to nat \to Prop \def
107 \lambda n,m,q,r:nat.r < m \land n=q*m+r).
110 theorem div_mod_spec_to_not_eq_O: \forall n,m,q,r.(div_mod_spec n m q r) \to m \neq O.
111 intros 4.unfold Not.intros.elim H.absurd (le (S r) O).
112 rewrite < H1.assumption.
113 exact (not_le_Sn_O r).
116 theorem div_mod_spec_div_mod:
117 \forall n,m. O < m \to (div_mod_spec n m (n / m) (n \mod m)).
119 apply div_mod_spec_intro.
120 apply lt_mod_m_m.assumption.
121 apply div_mod.assumption.
124 theorem div_mod_spec_to_eq :\forall a,b,q,r,q1,r1.
125 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to
127 intros.elim H.elim H1.
128 apply (nat_compare_elim q q1).intro.
130 cut (eq nat ((q1-q)*b+r1) r).
131 cut (b \leq (q1-q)*b+r1).
133 apply (lt_to_not_le r b H2 Hcut2).
134 elim Hcut.assumption.
135 apply (trans_le ? ((q1-q)*b)).
137 apply le_SO_minus.exact H6.
141 rewrite > distr_times_minus.
142 rewrite > plus_minus.
153 (* the following case is symmetric *)
156 cut (eq nat ((q-q1)*b+r) r1).
157 cut (b \leq (q-q1)*b+r).
159 apply (lt_to_not_le r1 b H4 Hcut2).
160 elim Hcut.assumption.
161 apply (trans_le ? ((q-q1)*b)).
163 apply le_SO_minus.exact H6.
167 rewrite > distr_times_minus.
168 rewrite > plus_minus.
179 theorem div_mod_spec_to_eq2 :\forall a,b,q,r,q1,r1.
180 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to
182 intros.elim H.elim H1.
183 apply (inj_plus_r (q*b)).
185 rewrite > (div_mod_spec_to_eq a b q r q1 r1 H H1).
189 theorem div_mod_spec_times : \forall n,m:nat.div_mod_spec ((S n)*m) (S n) m O.
190 intros.constructor 1.
191 unfold lt.apply le_S_S.apply le_O_n.
192 rewrite < plus_n_O.rewrite < sym_times.reflexivity.
195 (* some properties of div and mod *)
196 theorem div_times: \forall n,m:nat. ((S n)*m) / (S n) = m.
198 apply (div_mod_spec_to_eq ((S n)*m) (S n) ? ? ? O).
199 goal 15. (* ?11 is closed with the following tactics *)
200 apply div_mod_spec_div_mod.
201 unfold lt.apply le_S_S.apply le_O_n.
202 apply div_mod_spec_times.
205 theorem div_n_n: \forall n:nat. O < n \to n / n = S O.
207 apply (div_mod_spec_to_eq n n (n / n) (n \mod n) (S O) O).
208 apply div_mod_spec_div_mod.assumption.
209 constructor 1.assumption.
210 rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity.
213 theorem eq_div_O: \forall n,m. n < m \to n / m = O.
215 apply (div_mod_spec_to_eq n m (n/m) (n \mod m) O n).
216 apply div_mod_spec_div_mod.
217 apply (le_to_lt_to_lt O n m).
218 apply le_O_n.assumption.
219 constructor 1.assumption.reflexivity.
222 theorem mod_n_n: \forall n:nat. O < n \to n \mod n = O.
224 apply (div_mod_spec_to_eq2 n n (n / n) (n \mod n) (S O) O).
225 apply div_mod_spec_div_mod.assumption.
226 constructor 1.assumption.
227 rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity.
230 theorem mod_S: \forall n,m:nat. O < m \to S (n \mod m) < m \to
231 ((S n) \mod m) = S (n \mod m).
233 apply (div_mod_spec_to_eq2 (S n) m ((S n) / m) ((S n) \mod m) (n / m) (S (n \mod m))).
234 apply div_mod_spec_div_mod.assumption.
235 constructor 1.assumption.rewrite < plus_n_Sm.
241 theorem mod_O_n: \forall n:nat.O \mod n = O.
242 intro.elim n.simplify.reflexivity.
243 simplify.reflexivity.
246 theorem lt_to_eq_mod:\forall n,m:nat. n < m \to n \mod m = n.
248 apply (div_mod_spec_to_eq2 n m (n/m) (n \mod m) O n).
249 apply div_mod_spec_div_mod.
250 apply (le_to_lt_to_lt O n m).apply le_O_n.assumption.
252 assumption.reflexivity.
256 theorem injective_times_r: \forall n:nat.injective nat nat (\lambda m:nat.(S n)*m).
257 change with (\forall n,p,q:nat.(S n)*p = (S n)*q \to p=q).
259 rewrite < (div_times n).
260 rewrite < (div_times n q).
261 apply eq_f2.assumption.
265 variant inj_times_r : \forall n,p,q:nat.(S n)*p = (S n)*q \to p=q \def
268 theorem lt_O_to_injective_times_r: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.n*m).
271 apply (lt_O_n_elim n H).intros.
272 apply (inj_times_r m).assumption.
275 variant inj_times_r1:\forall n. O < n \to \forall p,q:nat.n*p = n*q \to p=q
276 \def lt_O_to_injective_times_r.
278 theorem injective_times_l: \forall n:nat.injective nat nat (\lambda m:nat.m*(S n)).
281 apply (inj_times_r n x y).
283 rewrite < (sym_times y).
287 variant inj_times_l : \forall n,p,q:nat. p*(S n) = q*(S n) \to p=q \def
290 theorem lt_O_to_injective_times_l: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.m*n).
293 apply (lt_O_n_elim n H).intros.
294 apply (inj_times_l m).assumption.
297 variant inj_times_l1:\forall n. O < n \to \forall p,q:nat.p*n = q*n \to p=q
298 \def lt_O_to_injective_times_l.
300 (* n_divides computes the pair (div,mod) *)
302 (* p is just an upper bound, acc is an accumulator *)
303 let rec n_divides_aux p n m acc \def
307 [ O \Rightarrow pair nat nat acc n
308 | (S p) \Rightarrow n_divides_aux p (n / m) m (S acc)]
309 | (S a) \Rightarrow pair nat nat acc n].
311 (* n_divides n m = <q,r> if m divides n q times, with remainder r *)
312 definition n_divides \def \lambda n,m:nat.n_divides_aux n n m O.