2 ||M|| This file is part of HELM, an Hypertextual, Electronic
3 ||A|| Library of Mathematics, developed at the Computer Science
4 ||T|| Department of the University of Bologna, Italy.
7 ||A|| This file is distributed under the terms of the
8 \ / GNU General Public License Version 2
10 V_______________________________________________________________ *)
12 include "arithmetics/nat.ma".
14 let rec mod_aux p m n: nat ≝
17 | S q ⇒ match (leb m n) with
19 | false ⇒ mod_aux q (m-(S n)) n]].
21 definition mod : nat → nat → nat ≝
24 | S p ⇒ mod_aux n n p].
26 interpretation "natural remainder" 'module x y = (mod x y).
28 let rec div_aux p m n : nat ≝
31 | S q ⇒ match (leb m n) with
33 | false ⇒ S (div_aux q (m-(S n)) n)]].
35 definition div : nat → nat → nat ≝
38 | S p ⇒ div_aux n n p].
40 interpretation "natural divide" 'divide x y = (div x y).
42 theorem le_mod_aux_m_m:
43 ∀p,n,m. n ≤ p → mod_aux p n m ≤ m.
45 [ normalize #n #m #lenO @(le_n_O_elim …lenO) //
46 | #q #Hind #n #m #len normalize
47 @(leb_elim n m) normalize //
48 #notlenm @Hind @le_plus_to_minus
49 @(transitive_le … len) /2/
52 theorem lt_mod_m_m: ∀n,m. O < m → n \mod m < m.
55 |#p #_ normalize @le_S_S /2/
59 theorem div_aux_mod_aux: ∀p,n,m:nat.
60 n=(div_aux p n m)*(S m) + (mod_aux p n m).
63 |#q #Hind #n #m normalize
64 @(leb_elim n m) #lenm normalize //
65 >(associative_plus ???) <(Hind (n-(S m)) m)
66 applyS plus_minus_m_m (* bello *) /2/
69 theorem div_mod: ∀n,m:nat. n=(n / m)*m+(n \mod m).
70 #n #m (cases m) normalize //
73 theorem eq_times_div_minus_mod:
74 ∀a,b:nat. (a / b) * b = a - (a \mod b).
75 #a #b (applyS minus_plus_m_m) qed.
77 inductive div_mod_spec (n,m,q,r:nat) : Prop ≝
78 div_mod_spec_intro: r < m → n=q*m+r → div_mod_spec n m q r.
80 theorem div_mod_spec_to_not_eq_O:
81 ∀n,m,q,r.div_mod_spec n m q r → m ≠ O.
85 theorem div_mod_spec_div_mod:
86 ∀n,m. O < m → div_mod_spec n m (n / m) (n \mod m).
87 #n #m #posm % /2/ qed.
89 theorem div_mod_spec_to_eq :∀ a,b,q,r,q1,r1.
90 div_mod_spec a b q r → div_mod_spec a b q1 r1 → q = q1.
91 #a #b #q #r #q1 #r1 * #ltrb #spec * #ltr1b #spec1
92 @(leb_elim q q1) #leqq1
93 [(elim (le_to_or_lt_eq … leqq1)) //
94 #ltqq1 @False_ind @(absurd ?? (not_le_Sn_n a))
95 @(lt_to_le_to_lt ? ((S q)*b) ?)
96 [>spec (applyS (monotonic_lt_plus_r … ltrb))
97 |@(transitive_le ? (q1*b)) /2/
99 (* this case is symmetric *)
100 |@False_ind @(absurd ?? (not_le_Sn_n a))
101 @(lt_to_le_to_lt ? ((S q1)*b) ?)
102 [>spec1 (applyS (monotonic_lt_plus_r … ltr1b))
103 |cut (q1 < q) [/2/] #ltq1q @(transitive_le ? (q*b)) /2/
108 theorem div_mod_spec_to_eq2: ∀a,b,q,r,q1,r1.
109 div_mod_spec a b q r → div_mod_spec a b q1 r1 → r = r1.
110 #a #b #q #r #q1 #r1 #spec #spec1
111 cut (q=q1) [@(div_mod_spec_to_eq … spec spec1)]
112 #eqq (elim spec) #_ #eqa (elim spec1) #_ #eqa1
113 @(injective_plus_r (q*b)) //
117 theorem div_mod_spec_times : ∀ n,m:nat.div_mod_spec ((S n)*m) (S n) m O.
118 intros.constructor 1.
119 unfold lt.apply le_S_S.apply le_O_n. demodulate. reflexivity.
120 (*rewrite < plus_n_O.rewrite < sym_times.reflexivity.*)
123 lemma div_plus_times: ∀m,q,r:nat. r < m → (q*m+r)/ m = q.
125 @(div_mod_spec_to_eq … (div_mod_spec_div_mod ???)) /2/
128 lemma mod_plus_times: ∀m,q,r:nat. r < m → (q*m+r) \mod m = r.
130 @(div_mod_spec_to_eq2 … (div_mod_spec_div_mod ???)) /2/
133 (* some properties of div and mod *)
134 theorem div_times: ∀a,b:nat. O < b → a*b/b = a.
136 @(div_mod_spec_to_eq (a*b) b … O (div_mod_spec_div_mod …))
137 // @div_mod_spec_intro // qed.
140 theorem div_n_n: ∀n:nat. O < n → n / n = 1.
143 theorem eq_div_O: ∀n,m. n < m → n / m = O.
145 @(div_mod_spec_to_eq n m (n/m) … n (div_mod_spec_div_mod …))
148 theorem mod_n_n: ∀n:nat. O < n → n \mod n = O.
150 @(div_mod_spec_to_eq2 n n … 1 0 (div_mod_spec_div_mod …))
153 theorem mod_S: ∀n,m:nat. O < m → S (n \mod m) < m →
154 ((S n) \mod m) = S (n \mod m).
156 @(div_mod_spec_to_eq2 (S n) m … (n / m) ? (div_mod_spec_div_mod …))
157 // @div_mod_spec_intro// (applyS eq_f) //
160 theorem mod_O_n: ∀n:nat.O \mod n = O.
163 theorem lt_to_eq_mod: ∀n,m:nat. n < m → n \mod m = n.
165 @(div_mod_spec_to_eq2 n m (n/m) … O n (div_mod_spec_div_mod …))
169 theorem mod_1: ∀n:nat. mod n 1 = O.
170 #n @sym_eq @le_n_O_to_eq
171 @le_S_S_to_le /2/ qed.
173 theorem div_1: ∀n:nat. div n 1 = n.
174 #n @sym_eq napplyS (div_mod n 1) qed. *)
176 theorem or_div_mod: ∀n,q. O < q →
177 ((S (n \mod q)=q) ∧ S n = (S (div n q)) * q ∨
178 ((S (n \mod q)<q) ∧ S n = (div n q) * q + S (n\mod q))).
180 (elim (le_to_or_lt_eq ?? (lt_mod_m_m n q posq))) #H
181 [%2 % // (applyS eq_f) //
182 |%1 % // /demod/ <H in ⊢(? ? ? (? % ?)) @eq_f//
187 theorem injective_times_r:
188 ∀n:nat. O < n → injective nat nat (λm:nat.n*m).
190 <(div_times a n posn) <(div_times b n posn) //
193 theorem injective_times_l:
194 ∀n:nat. O < n → injective nat nat (λm:nat.m*n).
197 (* n_divides computes the pair (div,mod)
198 (* p is just an upper bound, acc is an accumulator *)
199 let rec n_divides_aux p n m acc \def
203 [ O \Rightarrow pair nat nat acc n
204 | (S p) \Rightarrow n_divides_aux p (n / m) m (S acc)]
205 | (S a) \Rightarrow pair nat nat acc n].
207 (* n_divides n m = <q,r> if m divides n q times, with remainder r *)
208 definition n_divides \def \lambda n,m:nat.n_divides_aux n n m O. *)
212 theorem lt_div_S: ∀n,m. O < m → n < S(n / m)*m.
213 #n #m #posm (change with (n < m +(n/m)*m))
214 >(div_mod n m) in ⊢ (? % ?) >(commutative_plus ? ?)
215 @monotonic_lt_plus_l @lt_mod_m_m //
218 theorem le_div: ∀n,m. O < n → m/n ≤ m.
220 >(div_mod m n) in \vdash (? ? %) @(transitive_le ? (m/n*n)) /2/
223 theorem le_plus_mod: ∀m,n,q. O < q →
224 (m+n) \mod q ≤ m \mod q + n \mod q .
226 (elim (decidable_le q (m \mod q + n \mod q))) #Hle
227 [@(transitive_le … Hle) @le_S_S_to_le @le_S /2/
228 |cut ((m+n)\mod q = m\mod q+n\mod q) //
229 @(div_mod_spec_to_eq2 … (m/q + n/q) ? (div_mod_spec_div_mod … posq)).
232 |>(div_mod n q) in ⊢ (? ? (? ? %) ?)
233 (applyS (eq_f … (λx.plus x (n \mod q))))
234 >(div_mod m q) in ⊢ (? ? (? % ?) ?)
235 (applyS (eq_f … (λx.plus x (m \mod q)))) //
240 theorem le_plus_div: ∀m,n,q. O < q →
241 m/q + n/q \le (m+n)/q.
242 #m #n #q #posq @(le_times_to_le … posq)
243 @(le_plus_to_le_r ((m+n) \mod q))
245 >(commutative_times …) in ⊢ (? ? %) <(div_mod …)
246 >(div_mod m q) in ⊢ (? ? (? % ?)) >(div_mod n q) in ⊢ (? ? (? ? %))
247 >(commutative_plus …) in ⊢ (? ? (? % ?)) >(associative_plus …) in ⊢ (? ? %)
248 <(associative_plus …) in ⊢ (? ? (? ? %)) (applyS monotonic_le_plus_l) /2/
251 theorem le_times_to_le_div: ∀a,b,c:nat.
252 O < b → b*c ≤ a → c ≤ a/b.
254 @le_S_S_to_le @(lt_times_n_to_lt_l b) @(le_to_lt_to_lt ? a)/2/
257 theorem le_times_to_le_div2: ∀m,n,q. O < q →
260 @(le_times_to_le q ? ? posq) @(le_plus_to_le (n \mod q)) /2/
264 theorem lt_m_nm: ∀n,m. O < m → 1 < n → m < n*m.
267 theorem lt_times_to_lt_div: ∀m,n,q. n < m*q → n/q < m.
269 @(lt_times_n_to_lt_l q …) @(lt_plus_to_lt_l (n \mod q)) /2/
273 theorem lt_div: ∀n,m. O < m → 1 < n → m/n < m.
275 @lt_times_to_lt_div (applyS lt_m_nm) //.
278 theorem le_div_plus_S: ∀m,n,q. O < q →
279 (m+n)/q \le S(m/q + n/q).
281 @le_S_S_to_le @lt_times_to_lt_div
282 @(lt_to_le_to_lt … (lt_plus … (lt_div_S m … posq) (lt_div_S n … posq)))
286 theorem le_div_S_S_div: ∀n,m. O < m → (S n)/m ≤ S (n /m).
287 #n #m #posm @le_times_to_le_div2 /2/
290 theorem le_times_div_div_times: ∀a,n,m.O < m →
292 #a #n #m #posm @le_times_to_le_div /2/
295 theorem monotonic_div: ∀n.O < n →
296 monotonic nat le (λm.div m n).
297 #n #posn #a #b #leab @le_times_to_le_div/2/
300 theorem pos_div: ∀n,m:nat. O < m → O < n → n \mod m = O →
302 #n #m #posm #posn #mod0
303 @(lt_times_n_to_lt_l m)// (* MITICO *)
307 theorem lt_div_n_m_n: ∀n,m:nat. 1 < m → O < n → n / m < n.
309 @(leb_elim 1 (n / m))/2/ (* MITICO *)
312 theorem eq_div_div_div_times: ∀n,m,q. O < n → O < m →
315 @(div_mod_spec_to_eq … (q\mod n+n*(q/n\mod m)) … (div_mod_spec_div_mod …)) /2/
316 @div_mod_spec_intro // @(lt_to_le_to_lt ? (n*(S (q/n\mod m))))
317 [(applyS monotonic_lt_plus_l) /2/
318 |@monotonic_le_times_r/2/
322 theorem eq_div_div_div_div: ∀n,m,q. O < n → O < m →
325 @(trans_eq ? ? (q/(n*m)))
327 |@sym_eq (applyS eq_div_div_div_times) //
332 theorem SSO_mod: \forall n,m. O < m \to (S(S O))*n/m = (n/m)*(S(S O)) + mod ((S(S O))*n/m) (S(S O)).
334 rewrite < (lt_O_to_div_times n (S(S O))) in ⊢ (? ? ? (? (? (? % ?) ?) ?))
335 [rewrite > eq_div_div_div_div
336 [rewrite > sym_times in ⊢ (? ? ? (? (? (? (? % ?) ?) ?) ?)).
345 (* Forall a,b : N. 0 < b \to b * (a/b) <= a < b * (a/b +1) *)
346 (* The theorem is shown in two different parts: *)
348 theorem lt_to_div_to_and_le_times_lt_S: \forall a,b,c:nat.
349 O \lt b \to a/b = c \to (b*c \le a \land a \lt b*(S c)).
354 rewrite > eq_times_div_minus_mod
355 [ apply (le_minus_m a (a \mod b))
358 | rewrite < (times_n_Sm b c).
361 rewrite > (div_mod a b) in \vdash (? % ?)
362 [ rewrite > (sym_plus b ((a/b)*b)).
371 theorem le_plus_to_minus_r: ∀a,b,c. a + b \le c → a \le c -b.
372 #a #b #c #H @(le_plus_to_le_r … b) /2/
375 theorem le_minus_to_plus_r: ∀a,b,c. c ≤ b → a ≤ b - c → a + c ≤ b.
376 #a #b #c #Hlecb #H >(plus_minus_m_m … Hlecb) /2/
379 theorem lt_minus_to_plus: ∀a,b,c. a - b < c → a < c + b.
380 #a #b #c #H @not_le_to_lt
381 @(not_to_not … (lt_to_not_le …H)) /2/
384 theorem lt_minus_to_plus_r: ∀a,b,c. c ≤ a →
385 a < b + c → a - c < b.
386 #a #b #c #lea #H @not_le_to_lt
387 @(not_to_not … (lt_to_not_le …H)) /2/
390 theorem lt_to_le_times_to_lt_S_to_div: ∀a,c,b:nat.
391 O < b → (b*c) ≤ a → a < (b*(S c)) → a/b = c.
392 #a #c #b #posb#lea #lta
393 @(div_mod_spec_to_eq … (a-b*c) (div_mod_spec_div_mod … posb …))
394 @div_mod_spec_intro [@lt_minus_to_plus_r // |/2/]
397 theorem div_times_times: ∀a,b,c:nat. O < c → O < b →
400 >(commutative_times b c) <(eq_div_div_div_times … )//
401 >(div_times … posc)//
404 theorem times_mod: ∀a,b,c:nat.
405 O < c → O < b → (a*c) \mod (b*c) = c*(a\mod b).
407 @(div_mod_spec_to_eq2 (a*c) (b*c) (a/b) ((a*c) \mod (b*c)) (a/b) (c*(a \mod b)))
408 [>(div_times_times … posc posb …) @div_mod_spec_div_mod /2/
410 [(applyS monotonic_lt_times_l) /2/
411 |(applyS (eq_f …(λx.x*c))) //
416 theorem le_div_times_m: ∀a,i,m. O < i → O < m →
417 (a * (m / i)) / m ≤ a / i.
419 @(transitive_le ? ((a*m/i)/m))
421 |>(eq_div_div_div_div … posi posm) >(div_times …) //
426 theorem le_div_times_Sm: ∀a,i,m. O < i → O < m →
427 a / i ≤ (a * S (m / i))/m.
429 apply (trans_le ? ((a * S (m/i))/((S (m/i))*i)))
430 [rewrite < (eq_div_div_div_times ? i)
431 [rewrite > lt_O_to_div_times
438 |apply le_times_to_le_div
440 |apply (trans_le ? (m*(a*S (m/i))/(S (m/i)*i)))
441 [apply le_times_div_div_times.
442 rewrite > (times_n_O O).
447 |rewrite > sym_times.
448 apply le_times_to_le_div2
449 [rewrite > (times_n_O O).