~ubuntu-branches/ubuntu/wily/coq-doc/wily

« back to all changes in this revision

Viewing changes to theories/ZArith/ZOdiv.v

  • Committer: Bazaar Package Importer
  • Author(s): Stéphane Glondu, Stéphane Glondu, Samuel Mimram
  • Date: 2010-01-07 22:50:39 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100107225039-n3cq82589u0qt0s2
Tags: 8.2pl1-1
[ Stéphane Glondu ]
* New upstream release (Closes: #563669)
  - remove patches
* Packaging overhaul:
  - use git, advertise it in Vcs-* fields of debian/control
  - use debhelper 7 and dh with override
  - use source format 3.0 (quilt)
* debian/control:
  - set Maintainer to d-o-m, set Uploaders to Sam and myself
  - add Homepage field
  - bump Standards-Version to 3.8.3
* Register PDF documentation into doc-base
* Add debian/watch
* Update debian/copyright

[ Samuel Mimram ]
* Change coq-doc's description to mention that it provides documentation in
  pdf format, not postscript, closes: #543545.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(************************************************************************)
 
2
(*  v      *   The Coq Proof Assistant  /  The Coq Development Team     *)
 
3
(* <O___,, * CNRS-Ecole Polytechnique-INRIA Futurs-Universite Paris Sud *)
 
4
(*   \VV/  **************************************************************)
 
5
(*    //   *      This file is distributed under the terms of the       *)
 
6
(*         *       GNU Lesser General Public License Version 2.1        *)
 
7
(************************************************************************)
 
8
 
 
9
 
 
10
Require Import BinPos BinNat Nnat ZArith_base ROmega ZArithRing.
 
11
Require Export ZOdiv_def.
 
12
Require Zdiv.
 
13
 
 
14
Open Scope Z_scope.
 
15
 
 
16
(** This file provides results about the Round-Toward-Zero Euclidean 
 
17
  division [ZOdiv_eucl], whose projections are [ZOdiv] and [ZOmod].
 
18
  Definition of this division can be found in file [ZOdiv_def]. 
 
19
 
 
20
  This division and the one defined in Zdiv agree only on positive 
 
21
  numbers. Otherwise, Zdiv performs Round-Toward-Bottom. 
 
22
 
 
23
  The current approach is compatible with the division of usual 
 
24
  programming languages such as Ocaml. In addition, it has nicer 
 
25
  properties with respect to opposite and other usual operations.
 
26
*)
 
27
 
 
28
(** Since ZOdiv and Zdiv are not meant to be used concurrently, 
 
29
   we reuse the same notation. *)
 
30
 
 
31
Infix "/" := ZOdiv : Z_scope.
 
32
Infix "mod" := ZOmod (at level 40, no associativity) : Z_scope.
 
33
 
 
34
Infix "/" := Ndiv : N_scope.
 
35
Infix "mod" := Nmod (at level 40, no associativity) : N_scope.
 
36
 
 
37
(** Auxiliary results on the ad-hoc comparison [NPgeb]. *)
 
38
 
 
39
Lemma NPgeb_Zge : forall (n:N)(p:positive), 
 
40
  NPgeb n p = true -> Z_of_N n >= Zpos p.
 
41
Proof.
 
42
 destruct n as [|n]; simpl; intros.
 
43
 discriminate.
 
44
 red; simpl; destruct Pcompare; now auto.
 
45
Qed.
 
46
 
 
47
Lemma NPgeb_Zlt : forall (n:N)(p:positive), 
 
48
  NPgeb n p = false -> Z_of_N n < Zpos p.
 
49
Proof.
 
50
 destruct n as [|n]; simpl; intros.
 
51
 red; auto.
 
52
 red; simpl; destruct Pcompare; now auto.
 
53
Qed.
 
54
 
 
55
(** * Relation between division on N and on Z. *)
 
56
 
 
57
Lemma Ndiv_Z0div : forall a b:N, 
 
58
  Z_of_N (a/b) = (Z_of_N a / Z_of_N b).
 
59
Proof.
 
60
  intros.
 
61
  destruct a; destruct b; simpl; auto.
 
62
  unfold Ndiv, ZOdiv; simpl; destruct Pdiv_eucl; auto.
 
63
Qed.
 
64
 
 
65
Lemma Nmod_Z0mod : forall a b:N, 
 
66
  Z_of_N (a mod b) = (Z_of_N a) mod (Z_of_N b).
 
67
Proof.
 
68
  intros.
 
69
  destruct a; destruct b; simpl; auto.
 
70
  unfold Nmod, ZOmod; simpl; destruct Pdiv_eucl; auto.
 
71
Qed.
 
72
 
 
73
(** * Characterization of this euclidean division. *)
 
74
 
 
75
(** First, the usual equation [a=q*b+r]. Notice that [a mod 0] 
 
76
   has been chosen to be [a], so this equation holds even for [b=0].
 
77
*)
 
78
 
 
79
Theorem N_div_mod_eq : forall a b, 
 
80
  a = (b * (Ndiv a b) + (Nmod a b))%N.
 
81
Proof.
 
82
  intros; generalize (Ndiv_eucl_correct a b).
 
83
  unfold Ndiv, Nmod; destruct Ndiv_eucl; simpl.
 
84
  intro H; rewrite H; rewrite Nmult_comm; auto.
 
85
Qed.
 
86
 
 
87
Theorem ZO_div_mod_eq : forall a b, 
 
88
  a = b * (ZOdiv a b) + (ZOmod a b).
 
89
Proof.
 
90
  intros; generalize (ZOdiv_eucl_correct a b).
 
91
  unfold ZOdiv, ZOmod; destruct ZOdiv_eucl; simpl.
 
92
  intro H; rewrite H; rewrite Zmult_comm; auto.
 
93
Qed.
 
94
 
 
95
(** Then, the inequalities constraining the remainder. *)
 
96
 
 
97
Theorem Pdiv_eucl_remainder : forall a b:positive, 
 
98
  Z_of_N (snd (Pdiv_eucl a b)) < Zpos b. 
 
99
Proof.
 
100
  induction a; cbv beta iota delta [Pdiv_eucl]; fold Pdiv_eucl; cbv zeta.
 
101
  intros b; generalize (IHa b); case Pdiv_eucl.
 
102
    intros q1 r1 Hr1; simpl in Hr1.
 
103
    case_eq (NPgeb (2*r1+1) b); intros; unfold snd.
 
104
    romega with *.
 
105
    apply NPgeb_Zlt; auto.
 
106
  intros b; generalize (IHa b); case Pdiv_eucl.
 
107
    intros q1 r1 Hr1; simpl in Hr1.
 
108
    case_eq (NPgeb (2*r1) b); intros; unfold snd.
 
109
    romega with *.
 
110
    apply NPgeb_Zlt; auto.
 
111
  destruct b; simpl; romega with *.
 
112
Qed.
 
113
 
 
114
Theorem Nmod_lt : forall (a b:N), b<>0%N -> 
 
115
  (a mod b < b)%N.
 
116
Proof.
 
117
  destruct b as [ |b]; intro H; try solve [elim H;auto].
 
118
  destruct a as [ |a]; try solve [compute;auto]; unfold Nmod, Ndiv_eucl.
 
119
  generalize (Pdiv_eucl_remainder a b); destruct Pdiv_eucl; simpl.
 
120
  romega with *.
 
121
Qed.
 
122
 
 
123
(** The remainder is bounded by the divisor, in term of absolute values *)
 
124
 
 
125
Theorem ZOmod_lt : forall a b:Z, b<>0 -> 
 
126
  Zabs (a mod b) < Zabs b.
 
127
Proof.
 
128
  destruct b as [ |b|b]; intro H; try solve [elim H;auto]; 
 
129
  destruct a as [ |a|a]; try solve [compute;auto]; unfold ZOmod, ZOdiv_eucl; 
 
130
  generalize (Pdiv_eucl_remainder a b); destruct Pdiv_eucl; simpl; 
 
131
  try rewrite Zabs_Zopp; rewrite Zabs_eq; auto; apply Z_of_N_le_0.
 
132
Qed.
 
133
 
 
134
(** The sign of the remainder is the one of [a]. Due to the possible 
 
135
   nullity of [a], a general result is to be stated in the following form:
 
136
*)   
 
137
 
 
138
Theorem ZOmod_sgn : forall a b:Z, 
 
139
  0 <= Zsgn (a mod b) * Zsgn a.
 
140
Proof.
 
141
  destruct b as [ |b|b]; destruct a as [ |a|a]; simpl; auto with zarith;
 
142
  unfold ZOmod, ZOdiv_eucl; destruct Pdiv_eucl;
 
143
  simpl; destruct n0; simpl; auto with zarith.
 
144
Qed.
 
145
 
 
146
(** This can also be said in a simplier way: *)
 
147
 
 
148
Theorem Zsgn_pos_iff : forall z, 0 <= Zsgn z <-> 0 <= z.
 
149
Proof.
 
150
 destruct z; simpl; intuition auto with zarith.
 
151
Qed.
 
152
 
 
153
Theorem ZOmod_sgn2 : forall a b:Z, 
 
154
  0 <= (a mod b) * a.
 
155
Proof.
 
156
  intros; rewrite <-Zsgn_pos_iff, Zsgn_Zmult; apply ZOmod_sgn.
 
157
Qed.  
 
158
 
 
159
(** Reformulation of [ZOdiv_lt] and [ZOmod_sgn] in 2 
 
160
  then 4 particular cases. *)
 
161
 
 
162
Theorem ZOmod_lt_pos : forall a b:Z, 0<=a -> b<>0 ->   
 
163
  0 <= a mod b < Zabs b.
 
164
Proof.
 
165
  intros.
 
166
  assert (0 <= a mod b).
 
167
   generalize (ZOmod_sgn a b).
 
168
   destruct (Zle_lt_or_eq 0 a H).
 
169
   rewrite <- Zsgn_pos in H1; rewrite H1; romega with *.
 
170
   subst a; simpl; auto.
 
171
  generalize (ZOmod_lt a b H0); romega with *.
 
172
Qed.
 
173
 
 
174
Theorem ZOmod_lt_neg : forall a b:Z, a<=0 -> b<>0 ->   
 
175
  -Zabs b < a mod b <= 0.
 
176
Proof.
 
177
  intros.
 
178
  assert (a mod b <= 0).
 
179
   generalize (ZOmod_sgn a b).
 
180
   destruct (Zle_lt_or_eq a 0 H).
 
181
   rewrite <- Zsgn_neg in H1; rewrite H1; romega with *.
 
182
   subst a; simpl; auto.
 
183
  generalize (ZOmod_lt a b H0); romega with *.
 
184
Qed.
 
185
 
 
186
Theorem ZOmod_lt_pos_pos : forall a b:Z, 0<=a -> 0<b -> 0 <= a mod b < b.
 
187
Proof.
 
188
  intros; generalize (ZOmod_lt_pos a b); romega with *.
 
189
Qed.
 
190
 
 
191
Theorem ZOmod_lt_pos_neg : forall a b:Z, 0<=a -> b<0 -> 0 <= a mod b < -b.
 
192
Proof.
 
193
  intros; generalize (ZOmod_lt_pos a b); romega with *.
 
194
Qed.
 
195
 
 
196
Theorem ZOmod_lt_neg_pos : forall a b:Z, a<=0 -> 0<b -> -b < a mod b <= 0.
 
197
Proof.
 
198
  intros; generalize (ZOmod_lt_neg a b); romega with *.
 
199
Qed.
 
200
 
 
201
Theorem ZOmod_lt_neg_neg : forall a b:Z, a<=0 -> b<0 -> b < a mod b <= 0.
 
202
Proof.
 
203
  intros; generalize (ZOmod_lt_neg a b); romega with *.
 
204
Qed.
 
205
 
 
206
(** * Division and Opposite *)
 
207
 
 
208
(* The precise equalities that are invalid with "historic" Zdiv. *)
 
209
 
 
210
Theorem ZOdiv_opp_l : forall a b:Z, (-a)/b = -(a/b).
 
211
Proof.
 
212
 destruct a; destruct b; simpl; auto; 
 
213
  unfold ZOdiv, ZOdiv_eucl; destruct Pdiv_eucl; simpl; auto with zarith.
 
214
Qed.
 
215
 
 
216
Theorem ZOdiv_opp_r : forall a b:Z, a/(-b) = -(a/b).
 
217
Proof.
 
218
 destruct a; destruct b; simpl; auto; 
 
219
  unfold ZOdiv, ZOdiv_eucl; destruct Pdiv_eucl; simpl; auto with zarith.
 
220
Qed.
 
221
 
 
222
Theorem ZOmod_opp_l : forall a b:Z, (-a) mod b = -(a mod b).
 
223
Proof.
 
224
 destruct a; destruct b; simpl; auto; 
 
225
  unfold ZOmod, ZOdiv_eucl; destruct Pdiv_eucl; simpl; auto with zarith.
 
226
Qed.
 
227
 
 
228
Theorem ZOmod_opp_r : forall a b:Z, a mod (-b) = a mod b.
 
229
Proof.
 
230
 destruct a; destruct b; simpl; auto; 
 
231
  unfold ZOmod, ZOdiv_eucl; destruct Pdiv_eucl; simpl; auto with zarith.
 
232
Qed.
 
233
 
 
234
Theorem ZOdiv_opp_opp : forall a b:Z, (-a)/(-b) = a/b.
 
235
Proof.
 
236
 destruct a; destruct b; simpl; auto; 
 
237
  unfold ZOdiv, ZOdiv_eucl; destruct Pdiv_eucl; simpl; auto with zarith.
 
238
Qed.
 
239
 
 
240
Theorem ZOmod_opp_opp : forall a b:Z, (-a) mod (-b) = -(a mod b).
 
241
Proof.
 
242
 destruct a; destruct b; simpl; auto; 
 
243
  unfold ZOmod, ZOdiv_eucl; destruct Pdiv_eucl; simpl; auto with zarith.
 
244
Qed.
 
245
 
 
246
(** * Unicity results *)
 
247
 
 
248
Definition Remainder a b r := 
 
249
  (0 <= a /\ 0 <= r < Zabs b) \/ (a <= 0 /\ -Zabs b < r <= 0).
 
250
 
 
251
Definition Remainder_alt a b r := 
 
252
  Zabs r < Zabs b /\ 0 <= r * a.
 
253
 
 
254
Lemma Remainder_equiv : forall a b r, 
 
255
 Remainder a b r <-> Remainder_alt a b r.
 
256
Proof.
 
257
  unfold Remainder, Remainder_alt; intuition.
 
258
  romega with *.
 
259
  romega with *.
 
260
  rewrite <-(Zmult_opp_opp).
 
261
  apply Zmult_le_0_compat; romega.
 
262
  assert (0 <= Zsgn r * Zsgn a) by (rewrite <-Zsgn_Zmult, Zsgn_pos_iff; auto). 
 
263
  destruct r; simpl Zsgn in *; romega with *.
 
264
Qed.
 
265
 
 
266
Theorem ZOdiv_mod_unique_full:
 
267
 forall a b q r, Remainder a b r -> 
 
268
   a = b*q + r -> q = a/b /\ r = a mod b.
 
269
Proof.
 
270
  destruct 1 as [(H,H0)|(H,H0)]; intros.
 
271
  apply Zdiv.Zdiv_mod_unique with b; auto.
 
272
  apply ZOmod_lt_pos; auto.
 
273
  romega with *.
 
274
  rewrite <- H1; apply ZO_div_mod_eq.
 
275
 
 
276
  rewrite <- (Zopp_involutive a).
 
277
  rewrite ZOdiv_opp_l, ZOmod_opp_l.
 
278
  generalize (Zdiv.Zdiv_mod_unique b (-q) (-a/b) (-r) (-a mod b)).
 
279
  generalize (ZOmod_lt_pos (-a) b).
 
280
  rewrite <-ZO_div_mod_eq, <-Zopp_mult_distr_r, <-Zopp_plus_distr, <-H1.
 
281
  romega with *.
 
282
Qed.
 
283
 
 
284
Theorem ZOdiv_unique_full: 
 
285
 forall a b q r, Remainder a b r -> 
 
286
  a = b*q + r -> q = a/b.
 
287
Proof.
 
288
 intros; destruct (ZOdiv_mod_unique_full a b q r); auto.
 
289
Qed.
 
290
 
 
291
Theorem ZOdiv_unique:
 
292
 forall a b q r, 0 <= a -> 0 <= r < b -> 
 
293
   a = b*q + r -> q = a/b.
 
294
Proof.
 
295
  intros; eapply ZOdiv_unique_full; eauto.
 
296
  red; romega with *.
 
297
Qed.
 
298
 
 
299
Theorem ZOmod_unique_full: 
 
300
 forall a b q r, Remainder a b r -> 
 
301
  a = b*q + r -> r = a mod b.
 
302
Proof.
 
303
 intros; destruct (ZOdiv_mod_unique_full a b q r); auto.
 
304
Qed.
 
305
 
 
306
Theorem ZOmod_unique:
 
307
 forall a b q r, 0 <= a -> 0 <= r < b -> 
 
308
   a = b*q + r -> r = a mod b.
 
309
Proof.
 
310
  intros; eapply ZOmod_unique_full; eauto.
 
311
  red; romega with *.
 
312
Qed.
 
313
 
 
314
(** * Basic values of divisions and modulo. *)
 
315
 
 
316
Lemma ZOmod_0_l: forall a, 0 mod a = 0.
 
317
Proof.
 
318
  destruct a; simpl; auto.
 
319
Qed.
 
320
 
 
321
Lemma ZOmod_0_r: forall a, a mod 0 = a.
 
322
Proof.
 
323
  destruct a; simpl; auto.
 
324
Qed.
 
325
 
 
326
Lemma ZOdiv_0_l: forall a, 0/a = 0.
 
327
Proof.
 
328
  destruct a; simpl; auto.
 
329
Qed.
 
330
 
 
331
Lemma ZOdiv_0_r: forall a, a/0 = 0.
 
332
Proof.
 
333
  destruct a; simpl; auto.
 
334
Qed.
 
335
 
 
336
Lemma ZOmod_1_r: forall a, a mod 1 = 0.
 
337
Proof.
 
338
  intros; symmetry; apply ZOmod_unique_full with a; auto with zarith.
 
339
  rewrite Remainder_equiv; red; simpl; auto with zarith.
 
340
Qed.
 
341
 
 
342
Lemma ZOdiv_1_r: forall a, a/1 = a.
 
343
Proof.
 
344
  intros; symmetry; apply ZOdiv_unique_full with 0; auto with zarith.
 
345
  rewrite Remainder_equiv; red; simpl; auto with zarith.
 
346
Qed.
 
347
 
 
348
Hint Resolve ZOmod_0_l ZOmod_0_r ZOdiv_0_l ZOdiv_0_r ZOdiv_1_r ZOmod_1_r 
 
349
 : zarith.
 
350
 
 
351
Lemma ZOdiv_1_l: forall a, 1 < a -> 1/a = 0.
 
352
Proof.
 
353
  intros; symmetry; apply ZOdiv_unique with 1; auto with zarith.
 
354
Qed.
 
355
 
 
356
Lemma ZOmod_1_l: forall a, 1 < a ->  1 mod a = 1.
 
357
Proof.
 
358
  intros; symmetry; apply ZOmod_unique with 0; auto with zarith.
 
359
Qed.
 
360
 
 
361
Lemma ZO_div_same : forall a:Z, a<>0 -> a/a = 1.
 
362
Proof.
 
363
  intros; symmetry; apply ZOdiv_unique_full with 0; auto with *.
 
364
  rewrite Remainder_equiv; red; simpl; romega with *.
 
365
Qed.
 
366
 
 
367
Lemma ZO_mod_same : forall a, a mod a = 0.
 
368
Proof.
 
369
  destruct a; intros; symmetry.
 
370
  compute; auto.
 
371
  apply ZOmod_unique with 1; auto with *; romega with *.
 
372
  apply ZOmod_unique_full with 1; auto with *; red; romega with *.
 
373
Qed.
 
374
 
 
375
Lemma ZO_mod_mult : forall a b, (a*b) mod b = 0.
 
376
Proof.
 
377
  intros a b; destruct (Z_eq_dec b 0) as [Hb|Hb].
 
378
  subst; simpl; rewrite ZOmod_0_r; auto with zarith.
 
379
  symmetry; apply ZOmod_unique_full with a; [ red; romega with * | ring ].
 
380
Qed.
 
381
 
 
382
Lemma ZO_div_mult : forall a b:Z, b <> 0 -> (a*b)/b = a.
 
383
Proof.
 
384
  intros; symmetry; apply ZOdiv_unique_full with 0; auto with zarith; 
 
385
   [ red; romega with * | ring].
 
386
Qed.
 
387
 
 
388
(** * Order results about ZOmod and ZOdiv *)
 
389
 
 
390
(* Division of positive numbers is positive. *)
 
391
 
 
392
Lemma ZO_div_pos: forall a b, 0 <= a -> 0 <= b -> 0 <= a/b.
 
393
Proof.
 
394
  intros.
 
395
  destruct (Zle_lt_or_eq 0 b H0).
 
396
  assert (H2:=ZOmod_lt_pos_pos a b H H1).
 
397
  rewrite (ZO_div_mod_eq a b) in H.
 
398
  destruct (Z_lt_le_dec (a/b) 0); auto.
 
399
  assert (b*(a/b) <= -b).
 
400
   replace (-b) with (b*-1); [ | ring].
 
401
   apply Zmult_le_compat_l; auto with zarith.
 
402
  romega.
 
403
  subst b; rewrite ZOdiv_0_r; auto.
 
404
Qed.
 
405
 
 
406
(** As soon as the divisor is greater or equal than 2, 
 
407
    the division is strictly decreasing. *)
 
408
 
 
409
Lemma ZO_div_lt : forall a b:Z, 0 < a -> 2 <= b -> a/b < a.
 
410
Proof.
 
411
  intros. 
 
412
  assert (Hb : 0 < b) by romega.
 
413
  assert (H1 : 0 <= a/b) by (apply ZO_div_pos; auto with zarith).
 
414
  assert (H2 : 0 <= a mod b < b) by (apply ZOmod_lt_pos_pos; auto with zarith).
 
415
  destruct (Zle_lt_or_eq 0 (a/b) H1) as [H3|H3]; [ | rewrite <- H3; auto].
 
416
  pattern a at 2; rewrite (ZO_div_mod_eq a b).
 
417
  apply Zlt_le_trans with (2*(a/b)).
 
418
  romega.
 
419
  apply Zle_trans with (b*(a/b)).
 
420
  apply Zmult_le_compat_r; auto.
 
421
  romega.
 
422
Qed.
 
423
 
 
424
(** A division of a small number by a bigger one yields zero. *)
 
425
 
 
426
Theorem ZOdiv_small: forall a b, 0 <= a < b -> a/b = 0.
 
427
Proof.
 
428
  intros a b H; apply sym_equal; apply ZOdiv_unique with a; auto with zarith.
 
429
Qed.
 
430
 
 
431
(** Same situation, in term of modulo: *)
 
432
 
 
433
Theorem ZOmod_small: forall a n, 0 <= a < n -> a mod n = a.
 
434
Proof.
 
435
  intros a b H; apply sym_equal; apply ZOmod_unique with 0; auto with zarith.
 
436
Qed.
 
437
 
 
438
(** [Zge] is compatible with a positive division. *)
 
439
 
 
440
Lemma ZO_div_monotone_pos : forall a b c:Z, 0<=c -> 0<=a<=b -> a/c <= b/c.
 
441
Proof.
 
442
  intros.
 
443
  destruct H0.
 
444
  destruct (Zle_lt_or_eq 0 c H); 
 
445
   [ clear H | subst c; do 2 rewrite ZOdiv_0_r; auto].
 
446
  generalize (ZO_div_mod_eq a c).
 
447
  generalize (ZOmod_lt_pos_pos a c H0 H2).
 
448
  generalize (ZO_div_mod_eq b c).
 
449
  generalize (ZOmod_lt_pos_pos b c (Zle_trans _ _ _ H0 H1) H2).
 
450
  intros.
 
451
  elim (Z_le_gt_dec (a / c) (b / c)); auto with zarith.
 
452
  intro.
 
453
  absurd (a - b >= 1).
 
454
  omega.
 
455
  replace (a-b) with (c * (a/c-b/c) + a mod c - b mod c) by 
 
456
    (symmetry; pattern a at 1; rewrite H5; pattern b at 1; rewrite H3; ring).
 
457
  assert (c * (a / c - b / c) >= c * 1).
 
458
  apply Zmult_ge_compat_l.
 
459
  omega.
 
460
  omega.
 
461
  assert (c * 1 = c).
 
462
  ring.
 
463
  omega.
 
464
Qed.
 
465
 
 
466
Lemma ZO_div_monotone : forall a b c, 0<=c -> a<=b -> a/c <= b/c.
 
467
Proof.
 
468
  intros.
 
469
  destruct (Z_le_gt_dec 0 a).
 
470
  apply ZO_div_monotone_pos; auto with zarith.
 
471
  destruct (Z_le_gt_dec 0 b).
 
472
  apply Zle_trans with 0.
 
473
  apply Zle_left_rev.
 
474
  simpl.
 
475
  rewrite <- ZOdiv_opp_l.
 
476
  apply ZO_div_pos; auto with zarith.
 
477
  apply ZO_div_pos; auto with zarith.
 
478
  rewrite <-(Zopp_involutive a), (ZOdiv_opp_l (-a)).
 
479
  rewrite <-(Zopp_involutive b), (ZOdiv_opp_l (-b)).
 
480
  generalize (ZO_div_monotone_pos (-b) (-a) c H).
 
481
  romega.
 
482
Qed.
 
483
 
 
484
(** With our choice of division, rounding of (a/b) is always done toward zero: *)
 
485
 
 
486
Lemma ZO_mult_div_le : forall a b:Z, 0 <= a -> 0 <= b*(a/b) <= a.
 
487
Proof.
 
488
  intros a b Ha.
 
489
  destruct b as [ |b|b].
 
490
  simpl; auto with zarith.
 
491
  split.
 
492
  apply Zmult_le_0_compat; auto with zarith.
 
493
  apply ZO_div_pos; auto with zarith.
 
494
  generalize (ZO_div_mod_eq a (Zpos b)) (ZOmod_lt_pos_pos a (Zpos b) Ha); romega with *.
 
495
  change (Zneg b) with (-Zpos b); rewrite ZOdiv_opp_r, Zmult_opp_opp.
 
496
  split.
 
497
  apply Zmult_le_0_compat; auto with zarith.
 
498
  apply ZO_div_pos; auto with zarith.
 
499
  generalize (ZO_div_mod_eq a (Zpos b)) (ZOmod_lt_pos_pos a (Zpos b) Ha); romega with *.
 
500
Qed.
 
501
 
 
502
Lemma ZO_mult_div_ge : forall a b:Z, a <= 0 -> a <= b*(a/b) <= 0.
 
503
Proof.
 
504
  intros a b Ha.
 
505
  destruct b as [ |b|b].
 
506
  simpl; auto with zarith.
 
507
  split.
 
508
  generalize (ZO_div_mod_eq a (Zpos b)) (ZOmod_lt_neg_pos a (Zpos b) Ha); romega with *.
 
509
  apply Zle_left_rev; unfold Zplus.
 
510
  rewrite Zopp_mult_distr_r, <-ZOdiv_opp_l.
 
511
  apply Zmult_le_0_compat; auto with zarith.
 
512
  apply ZO_div_pos; auto with zarith.
 
513
  change (Zneg b) with (-Zpos b); rewrite ZOdiv_opp_r, Zmult_opp_opp.
 
514
  split.
 
515
  generalize (ZO_div_mod_eq a (Zpos b)) (ZOmod_lt_neg_pos a (Zpos b) Ha); romega with *.
 
516
  apply Zle_left_rev; unfold Zplus.
 
517
  rewrite Zopp_mult_distr_r, <-ZOdiv_opp_l.
 
518
  apply Zmult_le_0_compat; auto with zarith.
 
519
  apply ZO_div_pos; auto with zarith.
 
520
Qed.
 
521
 
 
522
(** The previous inequalities between [b*(a/b)] and [a] are exact 
 
523
    iff the modulo is zero. *)
 
524
 
 
525
Lemma ZO_div_exact_full_1 : forall a b:Z, a = b*(a/b) -> a mod b = 0.
 
526
Proof.
 
527
  intros; generalize (ZO_div_mod_eq a b); romega.
 
528
Qed.
 
529
 
 
530
Lemma ZO_div_exact_full_2 : forall a b:Z, a mod b = 0 -> a = b*(a/b).
 
531
Proof.
 
532
  intros; generalize (ZO_div_mod_eq a b); romega.
 
533
Qed.
 
534
 
 
535
(** A modulo cannot grow beyond its starting point. *)
 
536
 
 
537
Theorem ZOmod_le: forall a b, 0 <= a -> 0 <= b -> a mod b <= a.
 
538
Proof. 
 
539
  intros a b H1 H2.
 
540
  destruct (Zle_lt_or_eq _ _ H2).
 
541
  case (Zle_or_lt b a); intros H3.
 
542
  case (ZOmod_lt_pos_pos a b); auto with zarith.
 
543
  rewrite ZOmod_small; auto with zarith.
 
544
  subst; rewrite ZOmod_0_r; auto with zarith.
 
545
Qed.
 
546
 
 
547
(** Some additionnal inequalities about Zdiv. *)
 
548
 
 
549
Theorem ZOdiv_le_upper_bound: 
 
550
  forall a b q, 0 <= a -> 0 < b -> a <= q*b -> a/b <= q.
 
551
Proof.
 
552
  intros a b q H1 H2 H3.
 
553
  apply Zmult_le_reg_r with b; auto with zarith.
 
554
  apply Zle_trans with (2 := H3).
 
555
  pattern a at 2; rewrite (ZO_div_mod_eq a b); auto with zarith.
 
556
  rewrite (Zmult_comm b); case (ZOmod_lt_pos_pos a b); auto with zarith.
 
557
Qed.
 
558
 
 
559
Theorem ZOdiv_lt_upper_bound: 
 
560
  forall a b q, 0 <= a -> 0 < b -> a < q*b -> a/b < q.
 
561
Proof.
 
562
  intros a b q H1 H2 H3.
 
563
  apply Zmult_lt_reg_r with b; auto with zarith.
 
564
  apply Zle_lt_trans with (2 := H3).
 
565
  pattern a at 2; rewrite (ZO_div_mod_eq a b); auto with zarith.
 
566
  rewrite (Zmult_comm b); case (ZOmod_lt_pos_pos a b); auto with zarith.
 
567
Qed.
 
568
 
 
569
Theorem ZOdiv_le_lower_bound: 
 
570
  forall a b q, 0 <= a -> 0 < b -> q*b <= a -> q <= a/b.
 
571
Proof.
 
572
  intros a b q H1 H2 H3.
 
573
  assert (q < a / b + 1); auto with zarith.
 
574
  apply Zmult_lt_reg_r with b; auto with zarith.
 
575
  apply Zle_lt_trans with (1 := H3).
 
576
  pattern a at 1; rewrite (ZO_div_mod_eq a b); auto with zarith.
 
577
  rewrite Zmult_plus_distr_l; rewrite (Zmult_comm b); case (ZOmod_lt_pos_pos a b);
 
578
   auto with zarith.
 
579
Qed.
 
580
 
 
581
Theorem ZOdiv_sgn: forall a b, 
 
582
  0 <= Zsgn (a/b) * Zsgn a * Zsgn b.
 
583
Proof.
 
584
  destruct a as [ |a|a]; destruct b as [ |b|b]; simpl; auto with zarith; 
 
585
  unfold ZOdiv; simpl; destruct Pdiv_eucl; simpl; destruct n; simpl; auto with zarith.
 
586
Qed.
 
587
 
 
588
(** * Relations between usual operations and Zmod and Zdiv *)
 
589
 
 
590
(** First, a result that used to be always valid with Zdiv, 
 
591
    but must be restricted here. 
 
592
    For instance, now (9+(-5)*2) mod 2 = -1 <> 1 = 9 mod 2 *)
 
593
 
 
594
Lemma ZO_mod_plus : forall a b c:Z, 
 
595
 0 <= (a+b*c) * a -> 
 
596
 (a + b * c) mod c = a mod c.
 
597
Proof.
 
598
  intros; destruct (Z_eq_dec a 0) as [Ha|Ha].
 
599
  subst; simpl; rewrite ZOmod_0_l; apply ZO_mod_mult.
 
600
  intros; destruct (Z_eq_dec c 0) as [Hc|Hc].
 
601
  subst; do 2 rewrite ZOmod_0_r; romega.
 
602
  symmetry; apply ZOmod_unique_full with (a/c+b); auto with zarith.
 
603
  rewrite Remainder_equiv; split.
 
604
  apply ZOmod_lt; auto.
 
605
  apply Zmult_le_0_reg_r with (a*a); eauto.
 
606
  destruct a; simpl; auto with zarith.
 
607
  replace ((a mod c)*(a+b*c)*(a*a)) with (((a mod c)*a)*((a+b*c)*a)) by ring.
 
608
  apply Zmult_le_0_compat; auto.
 
609
  apply ZOmod_sgn2.
 
610
  rewrite Zmult_plus_distr_r, Zmult_comm.
 
611
  generalize (ZO_div_mod_eq a c); romega.
 
612
Qed.
 
613
 
 
614
Lemma ZO_div_plus : forall a b c:Z, 
 
615
 0 <= (a+b*c) * a -> c<>0 -> 
 
616
 (a + b * c) / c = a / c + b.
 
617
Proof.
 
618
  intros; destruct (Z_eq_dec a 0) as [Ha|Ha].
 
619
  subst; simpl; apply ZO_div_mult; auto.
 
620
  symmetry.
 
621
  apply ZOdiv_unique_full with (a mod c); auto with zarith.
 
622
  rewrite Remainder_equiv; split.
 
623
  apply ZOmod_lt; auto.
 
624
  apply Zmult_le_0_reg_r with (a*a); eauto.
 
625
  destruct a; simpl; auto with zarith.
 
626
  replace ((a mod c)*(a+b*c)*(a*a)) with (((a mod c)*a)*((a+b*c)*a)) by ring.
 
627
  apply Zmult_le_0_compat; auto.
 
628
  apply ZOmod_sgn2.
 
629
  rewrite Zmult_plus_distr_r, Zmult_comm.
 
630
  generalize (ZO_div_mod_eq a c); romega.
 
631
Qed.
 
632
 
 
633
Theorem ZO_div_plus_l: forall a b c : Z, 
 
634
 0 <= (a*b+c)*c -> b<>0 -> 
 
635
 b<>0 -> (a * b + c) / b = a + c / b.
 
636
Proof.
 
637
  intros a b c; rewrite Zplus_comm; intros; rewrite ZO_div_plus;
 
638
  try apply Zplus_comm; auto with zarith. 
 
639
Qed.
 
640
 
 
641
(** Cancellations. *)
 
642
 
 
643
Lemma ZOdiv_mult_cancel_r : forall a b c:Z, 
 
644
 c<>0 -> (a*c)/(b*c) = a/b.
 
645
Proof.
 
646
 intros a b c Hc.
 
647
 destruct (Z_eq_dec b 0).
 
648
 subst; simpl; do 2 rewrite ZOdiv_0_r; auto.
 
649
 symmetry.
 
650
 apply ZOdiv_unique_full with ((a mod b)*c); auto with zarith.
 
651
 rewrite Remainder_equiv.
 
652
 split.
 
653
 do 2 rewrite Zabs_Zmult.
 
654
 apply Zmult_lt_compat_r.
 
655
 romega with *.
 
656
 apply ZOmod_lt; auto.
 
657
 replace ((a mod b)*c*(a*c)) with (((a mod b)*a)*(c*c)) by ring.
 
658
 apply Zmult_le_0_compat.
 
659
 apply ZOmod_sgn2.
 
660
 destruct c; simpl; auto with zarith.
 
661
 pattern a at 1; rewrite (ZO_div_mod_eq a b); ring.
 
662
Qed.
 
663
 
 
664
Lemma ZOdiv_mult_cancel_l : forall a b c:Z, 
 
665
 c<>0 -> (c*a)/(c*b) = a/b.
 
666
Proof.
 
667
  intros.
 
668
  rewrite (Zmult_comm c a); rewrite (Zmult_comm c b).
 
669
  apply ZOdiv_mult_cancel_r; auto.
 
670
Qed.
 
671
 
 
672
Lemma ZOmult_mod_distr_l: forall a b c, 
 
673
  (c*a) mod (c*b) = c * (a mod b).
 
674
Proof.
 
675
  intros; destruct (Z_eq_dec c 0) as [Hc|Hc].
 
676
  subst; simpl; rewrite ZOmod_0_r; auto.
 
677
  destruct (Z_eq_dec b 0) as [Hb|Hb].
 
678
  subst; repeat rewrite Zmult_0_r || rewrite ZOmod_0_r; auto.
 
679
  assert (c*b <> 0).
 
680
   contradict Hc; eapply Zmult_integral_l; eauto.
 
681
  rewrite (Zplus_minus_eq _ _ _ (ZO_div_mod_eq (c*a) (c*b))).
 
682
  rewrite (Zplus_minus_eq _ _ _ (ZO_div_mod_eq a b)).
 
683
  rewrite ZOdiv_mult_cancel_l; auto with zarith.
 
684
  ring.
 
685
Qed.
 
686
 
 
687
Lemma ZOmult_mod_distr_r: forall a b c, 
 
688
  (a*c) mod (b*c) = (a mod b) * c.
 
689
Proof.
 
690
  intros; repeat rewrite (fun x => (Zmult_comm x c)).
 
691
  apply ZOmult_mod_distr_l; auto.
 
692
Qed.
 
693
 
 
694
(** Operations modulo. *)
 
695
 
 
696
Theorem ZOmod_mod: forall a n, (a mod n) mod n = a mod n.
 
697
Proof.
 
698
  intros.
 
699
  generalize (ZOmod_sgn2 a n).
 
700
  pattern a at 2 4; rewrite (ZO_div_mod_eq a n); auto with zarith.
 
701
  rewrite Zplus_comm; rewrite (Zmult_comm n).
 
702
  intros.
 
703
  apply sym_equal; apply ZO_mod_plus; auto with zarith.
 
704
  rewrite Zmult_comm; auto.
 
705
Qed.
 
706
 
 
707
Theorem ZOmult_mod: forall a b n,
 
708
 (a * b) mod n = ((a mod n) * (b mod n)) mod n.
 
709
Proof.
 
710
  intros.
 
711
  generalize (Zmult_le_0_compat _ _ (ZOmod_sgn2 a n) (ZOmod_sgn2 b n)).
 
712
  pattern a at 2 3; rewrite (ZO_div_mod_eq a n); auto with zarith.
 
713
  pattern b at 2 3; rewrite (ZO_div_mod_eq b n); auto with zarith.
 
714
  set (A:=a mod n); set (B:=b mod n); set (A':=a/n); set (B':=b/n).
 
715
  replace (A*(n*A'+A)*(B*(n*B'+B))) with (((n*A' + A) * (n*B' + B))*(A*B)) 
 
716
   by ring.
 
717
  replace ((n*A' + A) * (n*B' + B))
 
718
   with (A*B + (A'*B+B'*A+n*A'*B')*n) by ring.
 
719
  intros.
 
720
  apply ZO_mod_plus; auto with zarith.
 
721
Qed.
 
722
 
 
723
(** addition and modulo
 
724
  
 
725
  Generally speaking, unlike with Zdiv, we don't have 
 
726
       (a+b) mod n = (a mod n + b mod n) mod n 
 
727
  for any a and b. 
 
728
  For instance, take (8 + (-10)) mod 3 = -2 whereas 
 
729
  (8 mod 3 + (-10 mod 3)) mod 3 = 1. *)
 
730
 
 
731
Theorem ZOplus_mod: forall a b n,
 
732
 0 <= a * b -> 
 
733
 (a + b) mod n = (a mod n + b mod n) mod n.
 
734
Proof.
 
735
  assert (forall a b n, 0<a -> 0<b ->
 
736
           (a + b) mod n = (a mod n + b mod n) mod n).
 
737
  intros a b n Ha Hb.
 
738
  assert (H : 0<=a+b) by (romega with * ); revert H.
 
739
  pattern a at 1 2; rewrite (ZO_div_mod_eq a n); auto with zarith.
 
740
  pattern b at 1 2; rewrite (ZO_div_mod_eq b n); auto with zarith.
 
741
  replace ((n * (a / n) + a mod n) + (n * (b / n) + b mod n))
 
742
     with ((a mod n + b mod n) + (a / n + b / n) * n) by ring.
 
743
  intros.
 
744
  apply ZO_mod_plus; auto with zarith.
 
745
  apply Zmult_le_0_compat; auto with zarith.
 
746
  apply Zplus_le_0_compat.
 
747
  apply Zmult_le_reg_r with a; auto with zarith.
 
748
  simpl; apply ZOmod_sgn2; auto.
 
749
  apply Zmult_le_reg_r with b; auto with zarith.
 
750
  simpl; apply ZOmod_sgn2; auto.
 
751
  (* general situation *)
 
752
  intros a b n Hab.
 
753
  destruct (Z_eq_dec a 0).
 
754
  subst; simpl; symmetry; apply ZOmod_mod.
 
755
  destruct (Z_eq_dec b 0).
 
756
  subst; simpl; do 2 rewrite Zplus_0_r; symmetry; apply ZOmod_mod.
 
757
  assert (0<a /\ 0<b \/ a<0 /\ b<0).
 
758
   destruct a; destruct b; simpl in *; intuition; romega with *.
 
759
  destruct H0.
 
760
  apply H; intuition.
 
761
  rewrite <-(Zopp_involutive a), <-(Zopp_involutive b).
 
762
  rewrite <- Zopp_plus_distr; rewrite ZOmod_opp_l.
 
763
  rewrite (ZOmod_opp_l (-a)),(ZOmod_opp_l (-b)).
 
764
  match goal with |- _ = (-?x+-?y) mod n => 
 
765
   rewrite <-(Zopp_plus_distr x y), ZOmod_opp_l end.
 
766
  f_equal; apply H; auto with zarith.
 
767
Qed.
 
768
 
 
769
Lemma ZOplus_mod_idemp_l: forall a b n, 
 
770
 0 <= a * b -> 
 
771
 (a mod n + b) mod n = (a + b) mod n.
 
772
Proof.
 
773
  intros. 
 
774
  rewrite ZOplus_mod.
 
775
  rewrite ZOmod_mod.
 
776
  symmetry.
 
777
  apply ZOplus_mod; auto.
 
778
  destruct (Z_eq_dec a 0).
 
779
  subst; rewrite ZOmod_0_l; auto.
 
780
  destruct (Z_eq_dec b 0).
 
781
  subst; rewrite Zmult_0_r; auto with zarith.
 
782
  apply Zmult_le_reg_r with (a*b).
 
783
  assert (a*b <> 0).
 
784
   intro Hab.
 
785
   rewrite (Zmult_integral_l _ _ n1 Hab) in n0; auto with zarith.
 
786
  auto with zarith.
 
787
  simpl.
 
788
  replace (a mod n * b * (a*b)) with ((a mod n * a)*(b*b)) by ring.
 
789
  apply Zmult_le_0_compat.
 
790
  apply ZOmod_sgn2.
 
791
  destruct b; simpl; auto with zarith.
 
792
Qed.
 
793
 
 
794
Lemma ZOplus_mod_idemp_r: forall a b n, 
 
795
 0 <= a*b -> 
 
796
 (b + a mod n) mod n = (b + a) mod n.
 
797
Proof.
 
798
  intros.
 
799
  rewrite Zplus_comm, (Zplus_comm b a).
 
800
  apply ZOplus_mod_idemp_l; auto.
 
801
Qed.
 
802
 
 
803
Lemma ZOmult_mod_idemp_l: forall a b n, (a mod n * b) mod n = (a * b) mod n.
 
804
Proof.
 
805
  intros; rewrite ZOmult_mod, ZOmod_mod, <- ZOmult_mod; auto.
 
806
Qed.
 
807
 
 
808
Lemma ZOmult_mod_idemp_r: forall a b n, (b * (a mod n)) mod n = (b * a) mod n.
 
809
Proof.
 
810
  intros; rewrite ZOmult_mod, ZOmod_mod, <- ZOmult_mod; auto.
 
811
Qed.
 
812
 
 
813
(** Unlike with Zdiv, the following result is true without restrictions. *)
 
814
 
 
815
Lemma ZOdiv_ZOdiv : forall a b c, (a/b)/c = a/(b*c).
 
816
Proof.
 
817
  (* particular case: a, b, c positive *)
 
818
  assert (forall a b c, a>0 -> b>0 -> c>0 -> (a/b)/c = a/(b*c)).
 
819
   intros a b c H H0 H1.
 
820
   pattern a at 2;rewrite (ZO_div_mod_eq a b).
 
821
   pattern (a/b) at 2;rewrite (ZO_div_mod_eq (a/b) c).
 
822
   replace (b * (c * (a / b / c) + (a / b) mod c) + a mod b) with
 
823
    ((a / b / c)*(b * c)  + (b * ((a / b) mod c) + a mod b)) by ring.
 
824
   assert (b*c<>0).
 
825
    intro H2; 
 
826
    assert (H3: c <> 0) by auto with zarith; 
 
827
    rewrite (Zmult_integral_l _ _ H3 H2) in H0; auto with zarith.
 
828
   assert (0<=a/b) by (apply (ZO_div_pos a b); auto with zarith).
 
829
   assert (0<=a mod b < b) by (apply ZOmod_lt_pos_pos; auto with zarith).
 
830
   assert (0<=(a/b) mod c < c) by 
 
831
    (apply ZOmod_lt_pos_pos; auto with zarith).
 
832
   rewrite ZO_div_plus_l; auto with zarith.
 
833
   rewrite (ZOdiv_small (b * ((a / b) mod c) + a mod b)).
 
834
   ring.
 
835
   split.
 
836
   apply Zplus_le_0_compat;auto with zarith.
 
837
   apply Zle_lt_trans with (b * ((a / b) mod c) + (b-1)).
 
838
   apply Zplus_le_compat;auto with zarith.
 
839
   apply Zle_lt_trans with (b * (c-1) + (b - 1)).
 
840
   apply Zplus_le_compat;auto with zarith.
 
841
   replace (b * (c - 1) + (b - 1)) with (b*c-1);try ring;auto with zarith.
 
842
   repeat (apply Zmult_le_0_compat || apply Zplus_le_0_compat); auto with zarith.
 
843
   apply (ZO_div_pos (a/b) c); auto with zarith.
 
844
  (* b c positive, a general *)
 
845
  assert (forall a b c, b>0 -> c>0 -> (a/b)/c = a/(b*c)).
 
846
   intros; destruct a as [ |a|a]; try reflexivity.
 
847
   apply H; auto with zarith.
 
848
   change (Zneg a) with (-Zpos a); repeat rewrite ZOdiv_opp_l.
 
849
   f_equal; apply H; auto with zarith.
 
850
  (* c positive, a b general *)
 
851
  assert (forall a b c, c>0 -> (a/b)/c = a/(b*c)).
 
852
   intros; destruct b as [ |b|b].
 
853
   repeat rewrite ZOdiv_0_r; reflexivity.
 
854
   apply H0; auto with zarith.
 
855
   change (Zneg b) with (-Zpos b); 
 
856
   repeat (rewrite ZOdiv_opp_r || rewrite ZOdiv_opp_l || rewrite <- Zopp_mult_distr_l).
 
857
   f_equal; apply H0; auto with zarith.
 
858
  (* a b c general *)
 
859
  intros; destruct c as [ |c|c].
 
860
  rewrite Zmult_0_r; repeat rewrite ZOdiv_0_r; reflexivity.
 
861
  apply H1; auto with zarith.
 
862
  change (Zneg c) with (-Zpos c); 
 
863
  rewrite <- Zopp_mult_distr_r; do 2 rewrite ZOdiv_opp_r.
 
864
  f_equal; apply H1; auto with zarith.
 
865
Qed.
 
866
 
 
867
(** A last inequality: *)
 
868
 
 
869
Theorem ZOdiv_mult_le:
 
870
 forall a b c, 0<=a -> 0<=b -> 0<=c -> c*(a/b) <= (c*a)/b.
 
871
Proof.
 
872
  intros a b c Ha Hb Hc.
 
873
  destruct (Zle_lt_or_eq _ _ Ha); 
 
874
   [ | subst; rewrite ZOdiv_0_l, Zmult_0_r, ZOdiv_0_l; auto].
 
875
  destruct (Zle_lt_or_eq _ _ Hb); 
 
876
   [ | subst; rewrite ZOdiv_0_r, ZOdiv_0_r, Zmult_0_r; auto].
 
877
  destruct (Zle_lt_or_eq _ _ Hc); 
 
878
   [ | subst; rewrite ZOdiv_0_l; auto].
 
879
  case (ZOmod_lt_pos_pos a b); auto with zarith; intros Hu1 Hu2.
 
880
  case (ZOmod_lt_pos_pos c b); auto with zarith; intros Hv1 Hv2.
 
881
  apply Zmult_le_reg_r with b; auto with zarith.
 
882
  rewrite <- Zmult_assoc.
 
883
  replace (a / b * b) with (a - a mod b).
 
884
  replace (c * a / b * b) with (c * a - (c * a) mod b).
 
885
  rewrite Zmult_minus_distr_l.
 
886
  unfold Zminus; apply Zplus_le_compat_l.
 
887
  match goal with |- - ?X <= -?Y => assert (Y <= X); auto with zarith end.
 
888
  apply Zle_trans with ((c mod b) * (a mod b)); auto with zarith.
 
889
  rewrite ZOmult_mod; auto with zarith.
 
890
  apply (ZOmod_le ((c mod b) * (a mod b)) b); auto with zarith.
 
891
  apply Zmult_le_compat_r; auto with zarith.
 
892
  apply (ZOmod_le c b); auto.
 
893
  pattern (c * a) at 1; rewrite (ZO_div_mod_eq (c * a) b); try ring; 
 
894
    auto with zarith.
 
895
  pattern a at 1; rewrite (ZO_div_mod_eq a b); try ring; auto with zarith.
 
896
Qed.
 
897
 
 
898
(** ZOmod is related to divisibility (see more in Znumtheory) *)
 
899
 
 
900
Lemma ZOmod_divides : forall a b, 
 
901
 a mod b = 0 <-> exists c, a = b*c.
 
902
Proof.
 
903
 split; intros.
 
904
 exists (a/b).
 
905
 pattern a at 1; rewrite (ZO_div_mod_eq a b).
 
906
 rewrite H; auto with zarith.
 
907
 destruct H as [c Hc].
 
908
 destruct (Z_eq_dec b 0).
 
909
 subst b; simpl in *; subst a; auto.
 
910
 symmetry.
 
911
 apply ZOmod_unique_full with c; auto with zarith.
 
912
 red; romega with *.
 
913
Qed.
 
914
 
 
915
(** * Interaction with "historic" Zdiv *)
 
916
 
 
917
(** They agree at least on positive numbers: *)
 
918
 
 
919
Theorem ZOdiv_eucl_Zdiv_eucl_pos : forall a b:Z, 0 <= a -> 0 < b -> 
 
920
  a/b = Zdiv.Zdiv a b /\ a mod b = Zdiv.Zmod a b.
 
921
Proof.
 
922
  intros.
 
923
  apply Zdiv.Zdiv_mod_unique with b.
 
924
  apply ZOmod_lt_pos; auto with zarith.
 
925
  rewrite Zabs_eq; auto with *; apply Zdiv.Z_mod_lt; auto with *.
 
926
  rewrite <- Zdiv.Z_div_mod_eq; auto with *.
 
927
  symmetry; apply ZO_div_mod_eq; auto with *.
 
928
Qed.
 
929
 
 
930
Theorem ZOdiv_Zdiv_pos : forall a b, 0 <= a -> 0 <= b -> 
 
931
  a/b = Zdiv.Zdiv a b.
 
932
Proof.
 
933
 intros a b Ha Hb.
 
934
 destruct (Zle_lt_or_eq _ _ Hb).
 
935
 generalize (ZOdiv_eucl_Zdiv_eucl_pos a b Ha H); intuition.
 
936
 subst; rewrite ZOdiv_0_r, Zdiv.Zdiv_0_r; reflexivity.
 
937
Qed.
 
938
 
 
939
Theorem ZOmod_Zmod_pos : forall a b, 0 <= a -> 0 < b -> 
 
940
  a mod b = Zdiv.Zmod a b.
 
941
Proof.
 
942
 intros a b Ha Hb; generalize (ZOdiv_eucl_Zdiv_eucl_pos a b Ha Hb);
 
943
 intuition.
 
944
Qed.
 
945
 
 
946
(** Modulos are null at the same places *)
 
947
 
 
948
Theorem ZOmod_Zmod_zero : forall a b, b<>0 -> 
 
949
 (a mod b = 0 <-> Zdiv.Zmod a b = 0).
 
950
Proof.
 
951
 intros.
 
952
 rewrite ZOmod_divides, Zdiv.Zmod_divides; intuition.
 
953
Qed.