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 include "datatypes/constructors.ma".
16 include "nat/minus.ma".
18 let rec mod_aux p m n: nat \def
24 |(S q) \Rightarrow mod_aux q (m-(S n)) n]].
26 definition mod : nat \to nat \to nat \def
30 | (S p) \Rightarrow mod_aux n n p].
32 interpretation "natural remainder" 'module x y = (mod x y).
34 let rec div_aux p m n : nat \def
40 |(S q) \Rightarrow S (div_aux q (m-(S n)) n)]].
42 definition div : nat \to nat \to nat \def
46 | (S p) \Rightarrow div_aux n n p].
48 interpretation "natural divide" 'divide x y = (div x y).
50 theorem le_mod_aux_m_m:
51 \forall p,n,m. n \leq p \to (mod_aux p n m) \leq m.
53 apply (le_n_O_elim n H (\lambda n.(mod_aux O n m) \leq m)).
54 simplify.apply le_O_n.
56 apply (leb_elim n1 m).
57 simplify.intro.assumption.
58 simplify.intro.apply H.
59 cut (n1 \leq (S n) \to n1-(S m) \leq n).
60 apply Hcut.assumption.
62 simplify.apply le_O_n.
63 simplify.apply (trans_le ? n2 n).
64 apply le_minus_m.apply le_S_S_to_le.assumption.
67 theorem lt_mod_m_m: \forall n,m. O < m \to (n \mod m) < m.
68 intros 2.elim m.apply False_ind.
69 apply (not_le_Sn_O O H).
70 simplify.unfold lt.apply le_S_S.apply le_mod_aux_m_m.
74 theorem div_aux_mod_aux: \forall p,n,m:nat.
75 (n=(div_aux p n m)*(S m) + (mod_aux p n m)).
77 simplify.elim (leb n m).
78 simplify.apply refl_eq.
79 simplify.apply refl_eq.
81 apply (leb_elim n1 m).
82 simplify.intro.apply refl_eq.
85 elim (H (n1-(S m)) m).
86 change with (n1=(S m)+(n1-(S m))).
90 apply not_le_to_lt.exact H1.
93 theorem div_mod: \forall n,m:nat. O < m \to n=(n / m)*m+(n \mod m).
94 intros 2.elim m.elim (not_le_Sn_O O H).
96 apply div_aux_mod_aux.
99 theorem eq_times_div_minus_mod:
100 \forall a,b:nat. O \lt b \to
101 (a /b)*b = a - (a \mod b).
103 rewrite > (div_mod a b) in \vdash (? ? ? (? % ?))
104 [ apply (minus_plus_m_m (times (div a b) b) (mod a b))
109 inductive div_mod_spec (n,m,q,r:nat) : Prop \def
110 div_mod_spec_intro: r < m \to n=q*m+r \to (div_mod_spec n m q r).
113 definition div_mod_spec : nat \to nat \to nat \to nat \to Prop \def
114 \lambda n,m,q,r:nat.r < m \land n=q*m+r).
117 theorem div_mod_spec_to_not_eq_O: \forall n,m,q,r.(div_mod_spec n m q r) \to m \neq O.
118 intros 4.unfold Not.intros.elim H.absurd (le (S r) O).
119 rewrite < H1.assumption.
120 exact (not_le_Sn_O r).
123 theorem div_mod_spec_div_mod:
124 \forall n,m. O < m \to (div_mod_spec n m (n / m) (n \mod m)).
126 apply div_mod_spec_intro.
127 apply lt_mod_m_m.assumption.
128 apply div_mod.assumption.
131 theorem div_mod_spec_to_eq :\forall a,b,q,r,q1,r1.
132 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to
134 intros.elim H.elim H1.
135 apply (nat_compare_elim q q1).intro.
137 cut (eq nat ((q1-q)*b+r1) r).
138 cut (b \leq (q1-q)*b+r1).
140 apply (lt_to_not_le r b H2 Hcut2).
141 elim Hcut.assumption.
142 apply (trans_le ? ((q1-q)*b)).
144 apply le_SO_minus.exact H6.
148 rewrite > distr_times_minus.
149 rewrite > plus_minus.
160 (* the following case is symmetric *)
163 cut (eq nat ((q-q1)*b+r) r1).
164 cut (b \leq (q-q1)*b+r).
166 apply (lt_to_not_le r1 b H4 Hcut2).
167 elim Hcut.assumption.
168 apply (trans_le ? ((q-q1)*b)).
170 apply le_SO_minus.exact H6.
174 rewrite > distr_times_minus.
175 rewrite > plus_minus.
186 theorem div_mod_spec_to_eq2 :\forall a,b,q,r,q1,r1.
187 (div_mod_spec a b q r) \to (div_mod_spec a b q1 r1) \to
189 intros.elim H.elim H1.
190 apply (inj_plus_r (q*b)).
192 rewrite > (div_mod_spec_to_eq a b q r q1 r1 H H1).
196 theorem div_mod_spec_times : \forall n,m:nat.div_mod_spec ((S n)*m) (S n) m O.
197 intros.constructor 1.
198 unfold lt.apply le_S_S.apply le_O_n.
199 rewrite < plus_n_O.rewrite < sym_times.reflexivity.
202 lemma div_plus_times: \forall m,q,r:nat. r < m \to (q*m+r)/ m = q.
204 apply (div_mod_spec_to_eq (q*m+r) m ? ((q*m+r) \mod m) ? r)
205 [apply div_mod_spec_div_mod.
206 apply (le_to_lt_to_lt ? r)
207 [apply le_O_n|assumption]
208 |apply div_mod_spec_intro[assumption|reflexivity]
212 lemma mod_plus_times: \forall m,q,r:nat. r < m \to (q*m+r) \mod m = r.
214 apply (div_mod_spec_to_eq2 (q*m+r) m ((q*m+r)/ m) ((q*m+r) \mod m) q r)
215 [apply div_mod_spec_div_mod.
216 apply (le_to_lt_to_lt ? r)
217 [apply le_O_n|assumption]
218 |apply div_mod_spec_intro[assumption|reflexivity]
222 (* some properties of div and mod *)
223 theorem div_times: \forall n,m:nat. ((S n)*m) / (S n) = m.
225 apply (div_mod_spec_to_eq ((S n)*m) (S n) ? ? ? O);
226 [2: apply div_mod_spec_div_mod.
227 unfold lt.apply le_S_S.apply le_O_n.
229 | apply div_mod_spec_times
233 (*a simple variant of div_times theorem *)
234 theorem lt_O_to_div_times: \forall a,b:nat. O \lt b \to
238 rewrite > (S_pred b H).
242 theorem div_n_n: \forall n:nat. O < n \to n / n = S O.
244 apply (div_mod_spec_to_eq n n (n / n) (n \mod n) (S O) O).
245 apply div_mod_spec_div_mod.assumption.
246 constructor 1.assumption.
247 rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity.
250 theorem eq_div_O: \forall n,m. n < m \to n / m = O.
252 apply (div_mod_spec_to_eq n m (n/m) (n \mod m) O n).
253 apply div_mod_spec_div_mod.
254 apply (le_to_lt_to_lt O n m).
255 apply le_O_n.assumption.
256 constructor 1.assumption.reflexivity.
259 theorem mod_n_n: \forall n:nat. O < n \to n \mod n = O.
261 apply (div_mod_spec_to_eq2 n n (n / n) (n \mod n) (S O) O).
262 apply div_mod_spec_div_mod.assumption.
263 constructor 1.assumption.
264 rewrite < plus_n_O.simplify.rewrite < plus_n_O.reflexivity.
267 theorem mod_S: \forall n,m:nat. O < m \to S (n \mod m) < m \to
268 ((S n) \mod m) = S (n \mod m).
270 apply (div_mod_spec_to_eq2 (S n) m ((S n) / m) ((S n) \mod m) (n / m) (S (n \mod m))).
271 apply div_mod_spec_div_mod.assumption.
272 constructor 1.assumption.rewrite < plus_n_Sm.
278 theorem mod_O_n: \forall n:nat.O \mod n = O.
279 intro.elim n.simplify.reflexivity.
280 simplify.reflexivity.
283 theorem lt_to_eq_mod:\forall n,m:nat. n < m \to n \mod m = n.
285 apply (div_mod_spec_to_eq2 n m (n/m) (n \mod m) O n).
286 apply div_mod_spec_div_mod.
287 apply (le_to_lt_to_lt O n m).apply le_O_n.assumption.
289 assumption.reflexivity.
292 theorem mod_SO: \forall n:nat. mod n (S O) = O.
301 theorem div_SO: \forall n:nat. div n (S O) = n.
303 rewrite > (div_mod ? (S O)) in \vdash (? ? ? %)
311 theorem or_div_mod: \forall n,q. O < q \to
312 ((S (n \mod q)=q) \land S n = (S (div n q)) * q \lor
313 ((S (n \mod q)<q) \land S n= (div n q) * q + S (n\mod q))).
315 elim (le_to_or_lt_eq ? ? (lt_mod_m_m n q H))
318 |rewrite < plus_n_Sm.
327 rewrite < H1 in ⊢ (? ? ? (? ? %)).
337 theorem injective_times_r: \forall n:nat.injective nat nat (\lambda m:nat.(S n)*m).
338 change with (\forall n,p,q:nat.(S n)*p = (S n)*q \to p=q).
340 rewrite < (div_times n).
341 rewrite < (div_times n q).
342 apply eq_f2.assumption.
346 variant inj_times_r : \forall n,p,q:nat.(S n)*p = (S n)*q \to p=q \def
349 theorem lt_O_to_injective_times_r: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.n*m).
352 apply (lt_O_n_elim n H).intros.
353 apply (inj_times_r m).assumption.
356 variant inj_times_r1:\forall n. O < n \to \forall p,q:nat.n*p = n*q \to p=q
357 \def lt_O_to_injective_times_r.
359 theorem injective_times_l: \forall n:nat.injective nat nat (\lambda m:nat.m*(S n)).
362 apply (inj_times_r n x y).
364 rewrite < (sym_times y).
368 variant inj_times_l : \forall n,p,q:nat. p*(S n) = q*(S n) \to p=q \def
371 theorem lt_O_to_injective_times_l: \forall n:nat. O < n \to injective nat nat (\lambda m:nat.m*n).
374 apply (lt_O_n_elim n H).intros.
375 apply (inj_times_l m).assumption.
378 variant inj_times_l1:\forall n. O < n \to \forall p,q:nat.p*n = q*n \to p=q
379 \def lt_O_to_injective_times_l.
382 (* n_divides computes the pair (div,mod) *)
384 (* p is just an upper bound, acc is an accumulator *)
385 let rec n_divides_aux p n m acc \def
389 [ O \Rightarrow pair nat nat acc n
390 | (S p) \Rightarrow n_divides_aux p (n / m) m (S acc)]
391 | (S a) \Rightarrow pair nat nat acc n].
393 (* n_divides n m = <q,r> if m divides n q times, with remainder r *)
394 definition n_divides \def \lambda n,m:nat.n_divides_aux n n m O.