]> matita.cs.unibo.it Git - helm.git/blob - weblib/arithmetics/div_and_mod.ma
4044695d221c775f833f90f212f09e7397d86942
[helm.git] / weblib / arithmetics / div_and_mod.ma
1 (*
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.                     
5     ||I||                                                                 
6     ||T||  
7     ||A||  This file is distributed under the terms of the 
8     \   /  GNU General Public License Version 2        
9      \ /      
10       V_______________________________________________________________ *)
11
12 include "arithmetics/nat.ma".
13
14 let rec mod_aux p m n: nat ≝
15 match p with
16   [ O ⇒ m
17   | S q ⇒ match (leb m n) with
18     [ true ⇒ m
19     | false ⇒ mod_aux q (m-(S n)) n]].
20
21 definition mod : nat → nat → nat ≝
22 λn,m. match m with 
23   [ O ⇒ n
24   | S p ⇒ mod_aux n n p]. 
25
26 interpretation "natural remainder" 'module x y = (mod x y).
27
28 let rec div_aux p m n : nat ≝
29 match p with
30   [ O ⇒ O
31   | S q ⇒ match (leb m n) with
32     [ true ⇒ O
33     | false ⇒ S (div_aux q (m-(S n)) n)]].
34
35 definition div : nat → nat → nat ≝
36 λn,m.match m with 
37   [ O ⇒ S n
38   | S p ⇒ div_aux n n p]. 
39
40 interpretation "natural divide" 'divide x y = (div x y).
41
42 theorem le_mod_aux_m_m: 
43 ∀p,n,m. n ≤ p → mod_aux p n m ≤ m.
44 #p (elim p)
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/
50 qed.
51
52 theorem lt_mod_m_m: ∀n,m. O < m → n \mod m  < m.
53 #n #m (cases m) 
54   [#abs @False_ind /2/
55   |#p #_ normalize @le_S_S /2/ 
56   ]
57 qed.
58
59 theorem div_aux_mod_aux: ∀p,n,m:nat. 
60 n=(div_aux p n m)*(S m) + (mod_aux p n m).
61 #p (elim p)
62   [#n #m normalize //
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/
67 qed.
68
69 theorem div_mod: ∀n,m:nat. n=(n / m)*m+(n \mod m).
70 #n #m (cases m) normalize //
71 qed.
72
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.
76
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.
79
80 theorem div_mod_spec_to_not_eq_O: 
81   ∀n,m,q,r.div_mod_spec n m q r → m ≠ O.
82 #n #m #q #r * /2/ 
83 qed.
84
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.
88
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/
98       ]
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/
104       ]
105   ]
106 qed.
107
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)) //
114 qed.
115
116 (* boh
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.*)
121 qed. *)
122
123 lemma div_plus_times: ∀m,q,r:nat. r < m → (q*m+r)/ m = q.
124 #m #q #r #ltrm
125 @(div_mod_spec_to_eq … (div_mod_spec_div_mod ???)) /2/
126 qed.
127
128 lemma mod_plus_times: ∀m,q,r:nat. r < m → (q*m+r) \mod m = r. 
129 #m #q #r #ltrm
130 @(div_mod_spec_to_eq2 … (div_mod_spec_div_mod ???)) /2/
131 qed.
132
133 (* some properties of div and mod *)
134 theorem div_times: ∀a,b:nat. O < b → a*b/b = a.
135 #a #b #posb 
136 @(div_mod_spec_to_eq (a*b) b … O (div_mod_spec_div_mod …))
137 // @div_mod_spec_intro // qed.
138
139 theorem div_n_n: ∀n:nat. O < n → n / n = 1.
140 /2/ qed.
141
142 theorem eq_div_O: ∀n,m. n < m → n / m = O.
143 #n #m #ltnm 
144 @(div_mod_spec_to_eq n m (n/m) … n (div_mod_spec_div_mod …))
145 /2/ qed. 
146
147 theorem mod_n_n: ∀n:nat. O < n → n \mod n = O.
148 #n #posn 
149 @(div_mod_spec_to_eq2 n n … 1 0 (div_mod_spec_div_mod …))
150 /2/ qed. 
151
152 theorem mod_S: ∀n,m:nat. O < m → S (n \mod m) < m → 
153 ((S n) \mod m) = S (n \mod m).
154 #n #m #posm #H 
155 @(div_mod_spec_to_eq2 (S n) m … (n / m) ? (div_mod_spec_div_mod …))
156 // @div_mod_spec_intro// (applyS eq_f) //
157 qed.
158
159 theorem mod_O_n: ∀n:nat.O \mod n = O.
160 /2/ qed.
161
162 theorem lt_to_eq_mod: ∀n,m:nat. n < m → n \mod m = n.
163 #n #m #ltnm 
164 @(div_mod_spec_to_eq2 n m (n/m) … O n (div_mod_spec_div_mod …))
165 /2/ qed. 
166
167 (*
168 theorem mod_1: ∀n:nat. mod n 1 = O.
169 #n @sym_eq @le_n_O_to_eq
170 @le_S_S_to_le /2/ qed.
171
172 theorem div_1: ∀n:nat. div n 1 = n.
173 #n @sym_eq napplyS (div_mod n 1) qed. *)
174
175 theorem or_div_mod: ∀n,q. O < q →
176   ((S (n \mod q)=q) ∧ S n = (S (div n q)) * q ∨
177   ((S (n \mod q)<q) ∧ S n = (div n q) * q + S (n\mod q))).
178 #n #q #posq 
179 (elim (le_to_or_lt_eq ?? (lt_mod_m_m n q posq))) #H
180   [%2 % // (applyS eq_f) //
181   |%1 % // /demod/ <H in ⊢(? ? ? (? % ?)) @eq_f//
182   ]
183 qed.
184
185 (* injectivity *)
186 theorem injective_times_r: 
187   ∀n:nat. O < n → injective nat nat (λm:nat.n*m).
188 #n #posn #a #b #eqn 
189 <(div_times a n posn) <(div_times b n posn) // 
190 qed.
191
192 theorem injective_times_l: 
193     ∀n:nat. O < n → injective nat nat (λm:nat.m*n).
194 /2/ qed.
195
196 (* n_divides computes the pair (div,mod) 
197 (* p is just an upper bound, acc is an accumulator *)
198 let rec n_divides_aux p n m acc \def
199   match n \mod m with
200   [ O \Rightarrow 
201     match p with
202       [ O \Rightarrow pair nat nat acc n
203       | (S p) \Rightarrow n_divides_aux p (n / m) m (S acc)]
204   | (S a) \Rightarrow pair nat nat acc n].
205
206 (* n_divides n m = <q,r> if m divides n q times, with remainder r *)
207 definition n_divides \def \lambda n,m:nat.n_divides_aux n n m O. *)
208
209 (* inequalities *)
210
211 theorem lt_div_S: ∀n,m. O < m → n < S(n / m)*m.
212 #n #m #posm (change with (n < m +(n/m)*m))
213 >(div_mod n m) in ⊢ (? % ?) >commutative_plus 
214 @monotonic_lt_plus_l @lt_mod_m_m // 
215 qed.
216
217 theorem le_div: ∀n,m. O < n → m/n ≤ m.
218 #n #m #posn
219 >(div_mod m n) in ⊢ (? ? %) @(transitive_le ? (m/n*n)) /2/
220 qed.
221
222 theorem le_plus_mod: ∀m,n,q. O < q →
223 (m+n) \mod q ≤ m \mod q + n \mod q .
224 #m #n #q #posq
225 (elim (decidable_le q (m \mod q + n \mod q))) #Hle
226   [@(transitive_le … Hle) @le_S_S_to_le @le_S /2/
227   |cut ((m+n)\mod q = m\mod q+n\mod q) //
228      @(div_mod_spec_to_eq2 … (m/q + n/q) ? (div_mod_spec_div_mod … posq)).
229      @div_mod_spec_intro
230       [@not_le_to_lt //
231       |>(div_mod n q) in ⊢ (? ? (? ? %) ?)
232        (applyS (eq_f … (λx.plus x (n \mod q))))
233        >(div_mod m q) in ⊢ (? ? (? % ?) ?)
234        (applyS (eq_f … (λx.plus x (m \mod q)))) //
235       ]
236   ]
237 qed.
238
239 theorem le_plus_div: ∀m,n,q. O < q →
240   m/q + n/q \le (m+n)/q.
241 #m #n #q #posq @(le_times_to_le … posq)
242 @(le_plus_to_le_r ((m+n) \mod q))
243 (* bruttino *)
244 >commutative_times in ⊢ (? ? %) <div_mod
245 >(div_mod m q) in ⊢ (? ? (? % ?)) >(div_mod n q) in ⊢ (? ? (? ? %))
246 >commutative_plus in ⊢ (? ? (? % ?)) >associative_plus in ⊢ (? ? %)
247 <associative_plus in ⊢ (? ? (? ? %)) (applyS monotonic_le_plus_l) /2/
248 qed.
249
250 theorem le_times_to_le_div: ∀a,b,c:nat. 
251   O < b → b*c ≤ a → c ≤ a/b.
252 #a #b #c #posb #Hle
253 @le_S_S_to_le @(lt_times_n_to_lt_l b) @(le_to_lt_to_lt ? a)/2/
254 qed.
255
256 theorem le_times_to_le_div2: ∀m,n,q. O < q → 
257   n ≤ m*q → n/q ≤ m.
258 #m #n #q #posq #Hle
259 @(le_times_to_le q ? ? posq) @(le_plus_to_le (n \mod q)) /2/
260 qed.
261
262 (* da spostare 
263 theorem lt_m_nm: ∀n,m. O < m → 1 < n → m < n*m.
264 /2/ qed. *)
265    
266 theorem lt_times_to_lt_div: ∀m,n,q. n < m*q → n/q < m.
267 #m #n #q #Hlt
268 @(lt_times_n_to_lt_l q …) @(lt_plus_to_lt_l (n \mod q)) /2/
269 qed.
270
271 (*
272 theorem lt_div: ∀n,m. O < m → 1 < n → m/n < m.
273 #n #m #posm #lt1n
274 @lt_times_to_lt_div (applyS lt_m_nm) //.
275 qed. 
276   
277 theorem le_div_plus_S: ∀m,n,q. O < q →
278 (m+n)/q \le S(m/q + n/q).
279 #m #n #q #posq
280 @le_S_S_to_le @lt_times_to_lt_div
281 @(lt_to_le_to_lt … (lt_plus … (lt_div_S m … posq) (lt_div_S n … posq)))
282 //
283 qed. *)
284
285 theorem le_div_S_S_div: ∀n,m. O < m → (S n)/m ≤ S (n /m).
286 #n #m #posm @le_times_to_le_div2 /2/
287 qed.
288
289 theorem le_times_div_div_times: ∀a,n,m.O < m → 
290 a*(n/m) ≤ a*n/m. 
291 #a #n #m #posm @le_times_to_le_div /2/
292 qed.
293
294 theorem monotonic_div: ∀n.O < n →
295   monotonic nat le (λm.div m n).
296 #n #posn #a #b #leab @le_times_to_le_div/2/
297 qed.
298
299 theorem pos_div: ∀n,m:nat. O < m → O < n → n \mod m = O → 
300   O < n / m.
301 #n #m #posm #posn #mod0
302 @(lt_times_n_to_lt_l m)// (* MITICO *)
303 qed.
304
305 (*
306 theorem lt_div_n_m_n: ∀n,m:nat. 1 < m → O < n → n / m < n.
307 #n #m #ltm #posn
308 @(leb_elim 1 (n / m))/2/ (* MITICO *)
309 qed. *)
310
311 theorem eq_div_div_div_times: ∀n,m,q. O < n → O < m →
312  q/n/m = q/(n*m).
313 #n #m #q #posn #posm
314 @(div_mod_spec_to_eq … (q\mod n+n*(q/n\mod m)) … (div_mod_spec_div_mod …)) /2/
315 @div_mod_spec_intro // @(lt_to_le_to_lt ? (n*(S (q/n\mod m))))
316   [(applyS monotonic_lt_plus_l) /2/
317   |@monotonic_le_times_r/2/
318   ]
319 qed.
320
321 theorem eq_div_div_div_div: ∀n,m,q. O < n → O < m →
322 q/n/m = q/m/n.
323 #n #m #q #posn #posm
324 @(trans_eq ? ? (q/(n*m)))
325   [/2/
326   |@sym_eq (applyS eq_div_div_div_times) //
327   ]
328 qed.
329
330 (*
331 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)).
332 intros.
333 rewrite < (lt_O_to_div_times n (S(S O))) in ⊢ (? ? ? (? (? (? % ?) ?) ?))
334   [rewrite > eq_div_div_div_div
335     [rewrite > sym_times in ⊢ (? ? ? (? (? (? (? % ?) ?) ?) ?)).
336      apply div_mod.
337      apply lt_O_S
338     |apply lt_O_S
339     |assumption
340     ]
341   |apply lt_O_S
342   ]
343 qed. *)
344 (* Forall a,b : N. 0 < b \to b * (a/b) <= a < b * (a/b +1) *)
345 (* The theorem is shown in two different parts: *)
346 (*
347 theorem lt_to_div_to_and_le_times_lt_S: \forall a,b,c:nat.
348 O \lt b \to a/b = c \to (b*c \le a \land a \lt b*(S c)).
349 intros.
350 split
351 [ rewrite < H1.
352   rewrite > sym_times.
353   rewrite > eq_times_div_minus_mod
354   [ apply (le_minus_m a (a \mod b))
355   | assumption
356   ]
357 | rewrite < (times_n_Sm b c).
358   rewrite < H1.
359   rewrite > sym_times.
360   rewrite > (div_mod a b) in \vdash (? % ?)
361   [ rewrite > (sym_plus b ((a/b)*b)).
362     apply lt_plus_r.
363     apply lt_mod_m_m.
364     assumption
365   | assumption
366   ]
367 ]
368 qed. *)
369
370 theorem lt_to_le_times_to_lt_S_to_div: ∀a,c,b:nat.
371 O < b → (b*c) ≤ a → a < (b*(S c)) → a/b = c.
372 #a #c #b #posb#lea #lta
373 @(div_mod_spec_to_eq … (a-b*c) (div_mod_spec_div_mod … posb …))
374 @div_mod_spec_intro [@lt_plus_to_minus // |/2/]
375 qed.
376
377 theorem div_times_times: ∀a,b,c:nat. O < c → O < b → 
378   (a/b) = (a*c)/(b*c).
379 #a #b #c #posc #posb
380 >(commutative_times b) <eq_div_div_div_times //
381 >div_times //
382 qed.
383
384 theorem times_mod: ∀a,b,c:nat.
385 O < c → O < b → (a*c) \mod (b*c) = c*(a\mod b).
386 #a #b #c #posc #posb
387 @(div_mod_spec_to_eq2 (a*c) (b*c) (a/b) ((a*c) \mod (b*c)) (a/b) (c*(a \mod b)))
388   [>(div_times_times … posc) // @div_mod_spec_div_mod /2/
389   |@div_mod_spec_intro
390     [applyS (monotonic_lt_times_r … c posc) /2/
391     |(applyS (eq_f …(λx.x*c))) //
392     ]
393   ]
394 qed.
395
396 theorem le_div_times_m: ∀a,i,m. O < i → O < m →
397  (a * (m / i)) / m ≤ a / i.
398 #a #i #m #posi #posm
399 @(transitive_le ? ((a*m/i)/m))
400   [@monotonic_div /2/
401   |>eq_div_div_div_div // >div_times //
402   ]
403 qed.
404
405 (* serve ?
406 theorem le_div_times_Sm: ∀a,i,m. O < i → O < m →
407 a / i ≤ (a * S (m / i))/m.
408 intros.
409 apply (trans_le ? ((a * S (m/i))/((S (m/i))*i)))
410   [rewrite < (eq_div_div_div_times ? i)
411     [rewrite > lt_O_to_div_times
412       [apply le_n
413       |apply lt_O_S
414       ]
415     |apply lt_O_S
416     |assumption
417     ]
418   |apply le_times_to_le_div
419     [assumption
420     |apply (trans_le ? (m*(a*S (m/i))/(S (m/i)*i)))
421       [apply le_times_div_div_times.
422        rewrite > (times_n_O O).
423        apply lt_times
424         [apply lt_O_S
425         |assumption
426         ]
427       |rewrite > sym_times.
428        apply le_times_to_le_div2
429         [rewrite > (times_n_O O).
430          apply lt_times
431           [apply lt_O_S
432           |assumption
433           ]
434         |apply le_times_r.
435          apply lt_to_le.
436          apply lt_div_S.
437          assumption
438         ]
439       ]
440     ]
441   ]
442 qed. *)