Fcore_Raux.v 32.1 KB
Newer Older
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1 2 3
Require Export Reals.
Require Export ZArith.

4 5
Section Rmissing.

6
Theorem Rle_0_minus :
BOLDO Sylvie's avatar
BOLDO Sylvie committed
7 8 9 10 11 12 13 14
  forall x y, (x <= y)%R -> (0 <= y - x)%R.
Proof.
intros.
apply Rge_le.
apply Rge_minus.
now apply Rle_ge.
Qed.

15
Theorem Rabs_eq_Rabs :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
  forall x y : R,
  Rabs x = Rabs y -> x = y \/ x = Ropp y.
Proof.
intros x y H.
unfold Rabs in H.
destruct (Rcase_abs x) as [_|_].
assert (H' := f_equal Ropp H).
rewrite Ropp_involutive in H'.
rewrite H'.
destruct (Rcase_abs y) as [_|_].
left.
apply Ropp_involutive.
now right.
rewrite H.
now destruct (Rcase_abs y) as [_|_] ; [right|left].
Qed.

33 34 35 36 37 38 39 40 41 42 43 44 45

Theorem Rabs_Rminus_pos:
  forall x y : R,
  (0 <= y)%R -> (y <= 2*x)%R ->
  (Rabs (x-y) <= x)%R.
intros x y Hx Hy.
unfold Rabs; case (Rcase_abs (x - y)); intros H.
apply Rplus_le_reg_l with x; ring_simplify; assumption.
apply Rplus_le_reg_l with (-x)%R; ring_simplify.
auto with real.
Qed.


46
Theorem Rplus_le_reg_r :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
47 48 49 50 51 52 53 54
  forall r r1 r2 : R,
  (r1 + r <= r2 + r)%R -> (r1 <= r2)%R.
Proof.
intros.
apply Rplus_le_reg_l with r.
now rewrite 2!(Rplus_comm r).
Qed.

55
Theorem Rmult_lt_reg_r :
56 57 58 59 60 61 62 63 64
  forall r r1 r2 : R, (0 < r)%R ->
  (r1 * r < r2 * r)%R -> (r1 < r2)%R.
Proof.
intros.
apply Rmult_lt_reg_l with r.
exact H.
now rewrite 2!(Rmult_comm r).
Qed.

65
Theorem Rmult_le_reg_r :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
66 67 68 69 70 71 72 73 74
  forall r r1 r2 : R, (0 < r)%R ->
  (r1 * r <= r2 * r)%R -> (r1 <= r2)%R.
Proof.
intros.
apply Rmult_le_reg_l with r.
exact H.
now rewrite 2!(Rmult_comm r).
Qed.

75
Theorem Rmult_eq_reg_r :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
76 77 78 79 80 81 82 83 84
  forall r r1 r2 : R, (r1 * r)%R = (r2 * r)%R ->
  r <> 0%R -> r1 = r2.
Proof.
intros r r1 r2 H1 H2.
apply Rmult_eq_reg_l with r.
now rewrite 2!(Rmult_comm r).
exact H2.
Qed.

85 86 87 88 89 90 91 92 93
Theorem Rmult_minus_distr_r :
  forall r r1 r2 : R,
  ((r1 - r2) * r = r1 * r - r2 * r)%R.
Proof.
intros r r1 r2.
rewrite <- 3!(Rmult_comm r).
apply Rmult_minus_distr_l.
Qed.

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
Theorem Rmult_min_distr_r :
  forall r r1 r2 : R,
  (0 <= r)%R ->
  (Rmin r1 r2 * r)%R = Rmin (r1 * r) (r2 * r).
Proof.
intros r r1 r2 [Hr|Hr].
unfold Rmin.
destruct (Rle_dec r1 r2) as [H1|H1] ;
  destruct (Rle_dec (r1 * r) (r2 * r)) as [H2|H2] ;
  try easy.
apply (f_equal (fun x => Rmult x r)).
apply Rle_antisym.
exact H1.
apply Rmult_le_reg_r with (1 := Hr).
apply Rlt_le.
now apply Rnot_le_lt.
apply Rle_antisym.
apply Rmult_le_compat_r.
now apply Rlt_le.
apply Rlt_le.
now apply Rnot_le_lt.
exact H2.
rewrite <- Hr.
rewrite 3!Rmult_0_r.
unfold Rmin.
destruct (Rle_dec 0 0) as [H0|H0].
easy.
elim H0.
apply Rle_refl.
Qed.

Theorem Rmult_min_distr_l :
  forall r r1 r2 : R,
  (0 <= r)%R ->
  (r * Rmin r1 r2)%R = Rmin (r * r1) (r * r2).
Proof.
intros r r1 r2 Hr.
rewrite 3!(Rmult_comm r).
now apply Rmult_min_distr_r.
Qed.

135
Theorem exp_increasing_weak :
136 137 138 139 140 141 142 143 144 145
  forall x y : R,
  (x <= y)%R -> (exp x <= exp y)%R.
Proof.
intros x y [H|H].
apply Rlt_le.
now apply exp_increasing.
rewrite H.
apply Rle_refl.
Qed.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
Theorem Rinv_lt :
  forall x y,
  (0 < x)%R -> (x < y)%R -> (/y < /x)%R.
Proof.
intros x y Hx Hxy.
apply Rinv_lt_contravar.
apply Rmult_lt_0_compat.
exact Hx.
now apply Rlt_trans with x.
exact Hxy.
Qed.

Theorem Rinv_le :
  forall x y,
  (0 < x)%R -> (x <= y)%R -> (/y <= /x)%R.
Proof.
intros x y Hx Hxy.
apply Rle_Rinv.
exact Hx.
now apply Rlt_le_trans with x.
exact Hxy.
Qed.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
169 170 171 172 173 174 175 176 177 178 179
Theorem sqrt_ge_0 :
  forall x : R,
  (0 <= sqrt x)%R.
Proof.
intros x.
unfold sqrt.
destruct (Rcase_abs x) as [_|H].
apply Rle_refl.
apply Rsqrt_positivity.
Qed.

180
Theorem Rabs_le :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
181 182 183 184 185 186 187 188 189 190 191
  forall x y,
  (-y <= x <= y)%R -> (Rabs x <= y)%R.
Proof.
intros x y (Hyx,Hxy).
unfold Rabs.
case Rcase_abs ; intros Hx.
apply Ropp_le_cancel.
now rewrite Ropp_involutive.
exact Hxy.
Qed.

192
Theorem Rabs_le_inv :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
193 194 195 196 197 198 199 200 201 202 203 204 205 206
  forall x y,
  (Rabs x <= y)%R -> (-y <= x <= y)%R.
Proof.
intros x y Hxy.
split.
apply Rle_trans with (- Rabs x)%R.
now apply Ropp_le_contravar.
apply Ropp_le_cancel.
rewrite Ropp_involutive, <- Rabs_Ropp.
apply RRle_abs.
apply Rle_trans with (2 := Hxy).
apply RRle_abs.
Qed.

207
Theorem Rabs_ge :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
208 209 210 211 212 213 214 215 216 217 218 219 220
  forall x y,
  (y <= -x \/ x <= y)%R -> (x <= Rabs y)%R.
Proof.
intros x y [Hyx|Hxy].
apply Rle_trans with (-y)%R.
apply Ropp_le_cancel.
now rewrite Ropp_involutive.
rewrite <- Rabs_Ropp.
apply RRle_abs.
apply Rle_trans with (1 := Hxy).
apply RRle_abs.
Qed.

221
Theorem Rabs_ge_inv :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
222 223 224 225
  forall x y,
  (x <= Rabs y)%R -> (y <= -x \/ x <= y)%R.
Proof.
intros x y.
226
unfold Rabs.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
227 228 229 230 231 232 233
case Rcase_abs ; intros Hy Hxy.
left.
apply Ropp_le_cancel.
now rewrite Ropp_involutive.
now right.
Qed.

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
Theorem Rabs_lt :
  forall x y,
  (-y < x < y)%R -> (Rabs x < y)%R.
Proof.
intros x y (Hyx,Hxy).
now apply Rabs_def1.
Qed.

Theorem Rabs_lt_inv :
  forall x y,
  (Rabs x < y)%R -> (-y < x < y)%R.
Proof.
intros x y H.
now split ; eapply Rabs_def2.
Qed.

Theorem Rabs_gt :
  forall x y,
  (y < -x \/ x < y)%R -> (x < Rabs y)%R.
Proof.
intros x y [Hyx|Hxy].
rewrite <- Rabs_Ropp.
apply Rlt_le_trans with (Ropp y).
apply Ropp_lt_cancel.
now rewrite Ropp_involutive.
apply RRle_abs.
apply Rlt_le_trans with (1 := Hxy).
apply RRle_abs.
Qed.

Theorem Rabs_gt_inv :
  forall x y,
  (x < Rabs y)%R -> (y < -x \/ x < y)%R.
Proof.
intros x y.
unfold Rabs.
case Rcase_abs ; intros Hy Hxy.
left.
apply Ropp_lt_cancel.
now rewrite Ropp_involutive.
now right.
Qed.

277 278
End Rmissing.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
279 280
Section Zmissing.

281
Theorem Zopp_le_cancel :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
282 283 284 285 286 287 288 289
  forall x y : Z,
  (-y <= -x)%Z -> Zle x y.
Proof.
intros x y Hxy.
apply Zplus_le_reg_r with (-x - y)%Z.
now ring_simplify.
Qed.

290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
Theorem Zmin_left :
  forall x y : Z,
  (x <= y)%Z -> Zmin x y = x.
Proof.
intros x y.
generalize (Zmin_spec x y).
omega.
Qed.

Theorem Zmin_right :
  forall x y : Z,
  (y <= x)%Z -> Zmin x y = y.
Proof.
intros x y.
generalize (Zmin_spec x y).
omega.
Qed.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
308 309
End Zmissing.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
Section Z2R.

Fixpoint P2R (p : positive) :=
  match p with
  | xH => 1%R
  | xO xH => 2%R
  | xO t => (2 * P2R t)%R
  | xI xH => 3%R
  | xI t => (1 + 2 * P2R t)%R
  end.

Definition Z2R n :=
  match n with
  | Zpos p => P2R p
  | Zneg p => Ropp (P2R p)
  | Z0 => R0
  end.

328
Theorem P2R_INR :
BOLDO Sylvie's avatar
BOLDO Sylvie committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349
  forall n, P2R n = INR (nat_of_P n).
Proof.
induction n ; simpl ; try (
  rewrite IHn ;
  rewrite <- (mult_INR 2) ;
  rewrite <- (nat_of_P_mult_morphism 2) ;
  change (2 * n)%positive with (xO n)).
(* xI *)
rewrite (Rplus_comm 1).
change (nat_of_P (xO n)) with (Pmult_nat n 2).
case n ; intros ; simpl ; try apply refl_equal.
case (Pmult_nat p 4) ; intros ; try apply refl_equal.
rewrite Rplus_0_l.
apply refl_equal.
apply Rplus_comm.
(* xO *)
case n ; intros ; apply refl_equal.
(* xH *)
apply refl_equal.
Qed.

350
Theorem Z2R_IZR :
BOLDO Sylvie's avatar
BOLDO Sylvie committed
351 352 353 354 355 356 357 358 359 360
  forall n, Z2R n = IZR n.
Proof.
intro.
case n ; intros ; simpl.
apply refl_equal.
apply P2R_INR.
apply Ropp_eq_compat.
apply P2R_INR.
Qed.

361
Theorem opp_Z2R :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
362 363 364 365 366 367 368
  forall n, Z2R (-n) = (- Z2R n)%R.
Proof.
intros.
repeat rewrite Z2R_IZR.
apply Ropp_Ropp_IZR.
Qed.

369
Theorem plus_Z2R :
BOLDO Sylvie's avatar
BOLDO Sylvie committed
370 371 372 373 374 375 376
  forall m n, (Z2R (m + n) = Z2R m + Z2R n)%R.
Proof.
intros.
repeat rewrite Z2R_IZR.
apply plus_IZR.
Qed.

377
Theorem minus_IZR :
BOLDO Sylvie's avatar
BOLDO Sylvie committed
378 379 380 381 382 383 384 385 386 387
  forall n m : Z,
  IZR (n - m) = (IZR n - IZR m)%R.
Proof.
intros.
unfold Zminus.
rewrite plus_IZR.
rewrite Ropp_Ropp_IZR.
apply refl_equal.
Qed.

388
Theorem minus_Z2R :
BOLDO Sylvie's avatar
BOLDO Sylvie committed
389 390 391 392 393 394 395
  forall m n, (Z2R (m - n) = Z2R m - Z2R n)%R.
Proof.
intros.
repeat rewrite Z2R_IZR.
apply minus_IZR.
Qed.

396
Theorem mult_Z2R :
BOLDO Sylvie's avatar
BOLDO Sylvie committed
397 398 399 400 401 402 403
  forall m n, (Z2R (m * n) = Z2R m * Z2R n)%R.
Proof.
intros.
repeat rewrite Z2R_IZR.
apply mult_IZR.
Qed.

404
Theorem le_Z2R :
BOLDO Sylvie's avatar
BOLDO Sylvie committed
405 406 407 408 409 410 411
  forall m n, (Z2R m <= Z2R n)%R -> (m <= n)%Z.
Proof.
intros m n.
repeat rewrite Z2R_IZR.
apply le_IZR.
Qed.

412
Theorem Z2R_le :
BOLDO Sylvie's avatar
BOLDO Sylvie committed
413 414 415 416 417 418 419
  forall m n, (m <= n)%Z -> (Z2R m <= Z2R n)%R.
Proof.
intros m n.
repeat rewrite Z2R_IZR.
apply IZR_le.
Qed.

420
Theorem lt_Z2R :
BOLDO Sylvie's avatar
BOLDO Sylvie committed
421 422 423 424 425 426 427
  forall m n, (Z2R m < Z2R n)%R -> (m < n)%Z.
Proof.
intros m n.
repeat rewrite Z2R_IZR.
apply lt_IZR.
Qed.

428
Theorem Z2R_lt :
BOLDO Sylvie's avatar
BOLDO Sylvie committed
429 430 431 432 433 434 435
  forall m n, (m < n)%Z -> (Z2R m < Z2R n)%R.
Proof.
intros m n.
repeat rewrite Z2R_IZR.
apply IZR_lt.
Qed.

436
Theorem Z2R_le_lt :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
437 438 439 440 441 442 443 444
  forall m n p, (m <= n < p)%Z -> (Z2R m <= Z2R n < Z2R p)%R.
Proof.
intros m n p (H1, H2).
split.
now apply Z2R_le.
now apply Z2R_lt.
Qed.

445
Theorem le_lt_Z2R :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
446 447 448 449 450
  forall m n p, (Z2R m <= Z2R n < Z2R p)%R -> (m <= n < p)%Z.
Proof.
intros m n p (H1, H2).
split.
now apply le_Z2R.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
451 452
now apply lt_Z2R.
Qed.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
453

454
Theorem eq_Z2R :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
455 456 457 458 459 460 461
  forall m n, (Z2R m = Z2R n)%R -> (m = n)%Z.
Proof.
intros m n H.
apply eq_IZR.
now rewrite <- 2!Z2R_IZR.
Qed.

462
Theorem neq_Z2R :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
463 464 465 466 467 468 469
  forall m n, (Z2R m <> Z2R n)%R -> (m <> n)%Z.
Proof.
intros m n H H'.
apply H.
now apply f_equal.
Qed.

470
Theorem Z2R_neq :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
471
  forall m n, (m <> n)%Z -> (Z2R m <> Z2R n)%R.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
472 473 474 475 476 477
Proof.
intros m n.
repeat rewrite Z2R_IZR.
apply IZR_neq.
Qed.

478 479
Theorem abs_Z2R :
  forall z, Z2R (Zabs z) = Rabs (Z2R z).
Guillaume Melquiond's avatar
Guillaume Melquiond committed
480
Proof.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
481 482
intros.
repeat rewrite Z2R_IZR.
483
now rewrite Rabs_Zabs.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
484 485
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
486 487
End Z2R.

488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
Section Zcompare.

Inductive Zeq_bool_prop (x y : Z) : bool -> Prop :=
  | Zeq_bool_true : x = y -> Zeq_bool_prop x y true
  | Zeq_bool_false : x <> y -> Zeq_bool_prop x y false.

Theorem Zeq_bool_spec :
  forall x y, Zeq_bool_prop x y (Zeq_bool x y).
Proof.
intros x y.
generalize (Zeq_is_eq_bool x y).
case (Zeq_bool x y) ; intros (H1, H2) ; constructor.
now apply H2.
intros H.
specialize (H1 H).
discriminate H1.
Qed.

Inductive Zcompare_prop (x y : Z) : comparison -> Prop :=
  | Zcompare_Lt_ : (x < y)%Z -> Zcompare_prop x y Lt
  | Zcompare_Eq_ : x = y -> Zcompare_prop x y Eq
  | Zcompare_Gt_ : (y < x)%Z -> Zcompare_prop x y Gt.

Theorem Zcompare_spec :
  forall x y, Zcompare_prop x y (Zcompare x y).
Proof.
intros x y.
destruct (Z_dec x y) as [[H|H]|H].
generalize (Zlt_compare _ _ H).
case (Zcompare x y) ; try easy.
now constructor.
generalize (Zgt_compare _ _ H).
case (Zcompare x y) ; try easy.
constructor.
now apply Zgt_lt.
generalize (proj2 (Zcompare_Eq_iff_eq _ _) H).
case (Zcompare x y) ; try easy.
now constructor.
Qed.

Theorem Zcompare_Lt :
  forall x y,
  (x < y)%Z -> Zcompare x y = Lt.
Proof.
easy.
Qed.

Theorem Zcompare_Eq :
  forall x y,
  (x = y)%Z -> Zcompare x y = Eq.
Proof.
intros x y.
apply <- Zcompare_Eq_iff_eq.
Qed.

Theorem Zcompare_Gt :
  forall x y,
  (y < x)%Z -> Zcompare x y = Gt.
Proof.
intros x y.
apply Zlt_gt.
Qed.

End Zcompare.

Section Rcompare.

Definition Rcompare x y :=
  match total_order_T x y with
  | inleft (left _) => Lt
  | inleft (right _) => Eq
  | inright _ => Gt
  end.

Inductive Rcompare_prop (x y : R) : comparison -> Prop :=
  | Rcompare_Lt_ : (x < y)%R -> Rcompare_prop x y Lt
  | Rcompare_Eq_ : x = y -> Rcompare_prop x y Eq
  | Rcompare_Gt_ : (y < x)%R -> Rcompare_prop x y Gt.

Theorem Rcompare_spec :
  forall x y, Rcompare_prop x y (Rcompare x y).
Proof.
intros x y.
unfold Rcompare.
now destruct (total_order_T x y) as [[H|H]|H] ; constructor.
Qed.

575
Global Opaque Rcompare.
576 577 578 579 580 581 582 583 584 585 586 587 588 589

Theorem Rcompare_Lt :
  forall x y,
  (x < y)%R -> Rcompare x y = Lt.
Proof.
intros x y H.
case Rcompare_spec ; intro H'.
easy.
rewrite H' in H.
elim (Rlt_irrefl _ H).
elim (Rlt_irrefl x).
now apply Rlt_trans with y.
Qed.

590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
Theorem Rcompare_Lt_inv :
  forall x y,
  Rcompare x y = Lt -> (x < y)%R.
Proof.
intros x y.
now case Rcompare_spec.
Qed.

Theorem Rcompare_not_Lt :
  forall x y,
  (y <= x)%R -> Rcompare x y <> Lt.
Proof.
intros x y H1 H2.
apply Rle_not_lt with (1 := H1).
now apply Rcompare_Lt_inv.
Qed.

Theorem Rcompare_not_Lt_inv :
  forall x y,
  Rcompare x y <> Lt -> (y <= x)%R.
Proof.
intros x y H.
apply Rnot_lt_le.
contradict H.
now apply Rcompare_Lt.
Qed.

617 618 619 620 621 622 623 624 625
Theorem Rcompare_Eq :
  forall x y,
  x = y -> Rcompare x y = Eq.
Proof.
intros x y H.
rewrite H.
now case Rcompare_spec ; intro H' ; try elim (Rlt_irrefl _ H').
Qed.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
626 627 628 629 630 631 632 633
Theorem Rcompare_Eq_inv :
  forall x y,
  Rcompare x y = Eq -> x = y.
Proof.
intros x y.
now case Rcompare_spec.
Qed.

634 635 636 637 638 639 640 641 642 643 644 645 646
Theorem Rcompare_Gt :
  forall x y,
  (y < x)%R -> Rcompare x y = Gt.
Proof.
intros x y H.
case Rcompare_spec ; intro H'.
elim (Rlt_irrefl x).
now apply Rlt_trans with y.
rewrite H' in H.
elim (Rlt_irrefl _ H).
easy.
Qed.

647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
Theorem Rcompare_Gt_inv :
  forall x y,
  Rcompare x y = Gt -> (y < x)%R.
Proof.
intros x y.
now case Rcompare_spec.
Qed.

Theorem Rcompare_not_Gt :
  forall x y,
  (x <= y)%R -> Rcompare x y <> Gt.
Proof.
intros x y H1 H2.
apply Rle_not_lt with (1 := H1).
now apply Rcompare_Gt_inv.
Qed.

Theorem Rcompare_not_Gt_inv :
  forall x y,
  Rcompare x y <> Gt -> (x <= y)%R.
Proof.
intros x y H.
apply Rnot_lt_le.
contradict H.
now apply Rcompare_Gt.
Qed.

674 675 676 677 678 679 680 681 682 683 684 685 686
Theorem Rcompare_Z2R :
  forall x y, Rcompare (Z2R x) (Z2R y) = Zcompare x y.
Proof.
intros x y.
case Rcompare_spec ; intros H ; apply sym_eq.
apply Zcompare_Lt.
now apply lt_Z2R.
apply Zcompare_Eq.
now apply eq_Z2R.
apply Zcompare_Gt.
now apply lt_Z2R.
Qed.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
687 688 689 690 691 692 693 694 695 696 697
Theorem Rcompare_sym :
  forall x y,
  Rcompare x y = CompOpp (Rcompare y x).
Proof.
intros x y.
destruct (Rcompare_spec y x) as [H|H|H].
now apply Rcompare_Gt.
now apply Rcompare_Eq.
now apply Rcompare_Lt.
Qed.

698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782
Theorem Rcompare_plus_r :
  forall z x y,
  Rcompare (x + z) (y + z) = Rcompare x y.
Proof.
intros z x y.
destruct (Rcompare_spec x y) as [H|H|H].
apply Rcompare_Lt.
now apply Rplus_lt_compat_r.
apply Rcompare_Eq.
now rewrite H.
apply Rcompare_Gt.
now apply Rplus_lt_compat_r.
Qed.

Theorem Rcompare_plus_l :
  forall z x y,
  Rcompare (z + x) (z + y) = Rcompare x y.
Proof.
intros z x y.
rewrite 2!(Rplus_comm z).
apply Rcompare_plus_r.
Qed.

Theorem Rcompare_mult_r :
  forall z x y,
  (0 < z)%R ->
  Rcompare (x * z) (y * z) = Rcompare x y.
Proof.
intros z x y Hz.
destruct (Rcompare_spec x y) as [H|H|H].
apply Rcompare_Lt.
now apply Rmult_lt_compat_r.
apply Rcompare_Eq.
now rewrite H.
apply Rcompare_Gt.
now apply Rmult_lt_compat_r.
Qed.

Theorem Rcompare_mult_l :
  forall z x y,
  (0 < z)%R ->
  Rcompare (z * x) (z * y) = Rcompare x y.
Proof.
intros z x y.
rewrite 2!(Rmult_comm z).
apply Rcompare_mult_r.
Qed.

Theorem Rcompare_middle :
  forall x d u,
  Rcompare (x - d) (u - x) = Rcompare x ((d + u) / 2).
Proof.
intros x d u.
rewrite <- (Rcompare_plus_r (- x / 2 - d / 2) x).
rewrite <- (Rcompare_mult_r (/2) (x - d)).
field_simplify (x + (- x / 2 - d / 2))%R.
now field_simplify ((d + u) / 2 + (- x / 2 - d / 2))%R.
apply Rinv_0_lt_compat.
now apply (Z2R_lt 0 2).
Qed.

Theorem Rcompare_half_l :
  forall x y, Rcompare (x / 2) y = Rcompare x (2 * y).
Proof.
intros x y.
rewrite <- (Rcompare_mult_r 2%R).
unfold Rdiv.
rewrite Rmult_assoc, Rinv_l, Rmult_1_r.
now rewrite Rmult_comm.
now apply (Z2R_neq 2 0).
now apply (Z2R_lt 0 2).
Qed.

Theorem Rcompare_half_r :
  forall x y, Rcompare x (y / 2) = Rcompare (2 * x) y.
Proof.
intros x y.
rewrite <- (Rcompare_mult_r 2%R).
unfold Rdiv.
rewrite Rmult_assoc, Rinv_l, Rmult_1_r.
now rewrite Rmult_comm.
now apply (Z2R_neq 2 0).
now apply (Z2R_lt 0 2).
Qed.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
Theorem Rcompare_sqr :
  forall x y,
  (0 <= x)%R -> (0 <= y)%R ->
  Rcompare (x * x) (y * y) = Rcompare x y.
Proof.
intros x y Hx Hy.
destruct (Rcompare_spec x y) as [H|H|H].
apply Rcompare_Lt.
now apply Rsqr_incrst_1.
rewrite H.
now apply Rcompare_Eq.
apply Rcompare_Gt.
now apply Rsqr_incrst_1.
Qed.

798 799
End Rcompare.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
Section Rle_bool.

Definition Rle_bool x y :=
  match Rcompare x y with
  | Gt => false
  | _ => true
  end.

Inductive Rle_bool_prop (x y : R) : bool -> Prop :=
  | Rle_bool_true_ : (x <= y)%R -> Rle_bool_prop x y true
  | Rle_bool_false_ : (y < x)%R -> Rle_bool_prop x y false.

Theorem Rle_bool_spec :
  forall x y, Rle_bool_prop x y (Rle_bool x y).
Proof.
intros x y.
unfold Rle_bool.
case Rcompare_spec ; constructor.
now apply Rlt_le.
rewrite H.
apply Rle_refl.
exact H.
Qed.

Theorem Rle_bool_true :
  forall x y,
  (x <= y)%R -> Rle_bool x y = true.
Proof.
intros x y Hxy.
case Rle_bool_spec ; intros H.
apply refl_equal.
elim (Rlt_irrefl x).
now apply Rle_lt_trans with y.
Qed.

Theorem Rle_bool_false :
  forall x y,
  (y < x)%R -> Rle_bool x y = false.
Proof.
intros x y Hxy.
case Rle_bool_spec ; intros H.
elim (Rlt_irrefl x).
now apply Rle_lt_trans with y.
apply refl_equal.
Qed.

End Rle_bool.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
848 849 850 851
Section Floor_Ceil.

Definition Zfloor (x : R) := (up x - 1)%Z.

852
Theorem Zfloor_lb :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
853 854 855 856 857 858 859 860 861 862 863 864 865
  forall x : R,
  (Z2R (Zfloor x) <= x)%R.
Proof.
intros x.
unfold Zfloor.
rewrite minus_Z2R.
simpl.
rewrite Z2R_IZR.
apply Rplus_le_reg_r with (1 - x)%R.
ring_simplify.
exact (proj2 (archimed x)).
Qed.

866
Theorem Zfloor_ub :
867 868 869 870 871 872 873 874 875 876 877 878 879
  forall x : R,
  (x < Z2R (Zfloor x) + 1)%R.
Proof.
intros x.
unfold Zfloor.
rewrite minus_Z2R.
unfold Rminus.
rewrite Rplus_assoc.
rewrite Rplus_opp_l, Rplus_0_r.
rewrite Z2R_IZR.
exact (proj1 (archimed x)).
Qed.

880
Theorem Zfloor_lub :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
881 882 883 884 885 886 887 888
  forall n x,
  (Z2R n <= x)%R ->
  (n <= Zfloor x)%Z.
Proof.
intros n x Hnx.
apply Zlt_succ_le.
apply lt_Z2R.
apply Rle_lt_trans with (1 := Hnx).
889 890 891
unfold Zsucc.
rewrite plus_Z2R.
apply Zfloor_ub.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
892 893
Qed.

894
Theorem Zfloor_imp :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
895 896 897 898 899 900 901 902 903 904 905 906 907
  forall n x,
  (Z2R n <= x < Z2R (n + 1))%R ->
  Zfloor x = n.
Proof.
intros n x Hnx.
apply Zle_antisym.
apply Zlt_succ_le.
apply lt_Z2R.
apply Rle_lt_trans with (2 := proj2 Hnx).
apply Zfloor_lb.
now apply Zfloor_lub.
Qed.

908
Theorem Zfloor_Z2R :
909 910 911 912 913 914 915 916 917 918 919
  forall n,
  Zfloor (Z2R n) = n.
Proof.
intros n.
apply Zfloor_imp.
split.
apply Rle_refl.
apply Z2R_lt.
apply Zlt_succ.
Qed.

920 921 922 923 924 925 926 927 928 929
Theorem Zfloor_le :
  forall x y, (x <= y)%R ->
  (Zfloor x <= Zfloor y)%Z.
Proof.
intros x y Hxy.
apply Zfloor_lub.
apply Rle_trans with (2 := Hxy).
apply Zfloor_lb.
Qed.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
930 931
Definition Zceil (x : R) := (- Zfloor (- x))%Z.

932
Theorem Zceil_ub :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
933 934 935 936 937 938 939 940 941 942 943
  forall x : R,
  (x <= Z2R (Zceil x))%R.
Proof.
intros x.
unfold Zceil.
rewrite opp_Z2R.
apply Ropp_le_cancel.
rewrite Ropp_involutive.
apply Zfloor_lb.
Qed.

944
Theorem Zceil_glb :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
945 946 947 948 949 950 951 952 953 954 955 956 957
  forall n x,
  (x <= Z2R n)%R ->
  (Zceil x <= n)%Z.
Proof.
intros n x Hnx.
unfold Zceil.
apply Zopp_le_cancel.
rewrite Zopp_involutive.
apply Zfloor_lub.
rewrite opp_Z2R.
now apply Ropp_le_contravar.
Qed.

958
Theorem Zceil_imp :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976
  forall n x,
  (Z2R (n - 1) < x <= Z2R n)%R ->
  Zceil x = n.
Proof.
intros n x Hnx.
unfold Zceil.
rewrite <- (Zopp_involutive n).
apply f_equal.
apply Zfloor_imp.
split.
rewrite opp_Z2R.
now apply Ropp_le_contravar.
rewrite <- (Zopp_involutive 1).
rewrite <- Zopp_plus_distr.
rewrite opp_Z2R.
now apply Ropp_lt_contravar.
Qed.

977
Theorem Zceil_Z2R :
978 979 980 981 982
  forall n,
  Zceil (Z2R n) = n.
Proof.
intros n.
unfold Zceil.
983
rewrite <- opp_Z2R, Zfloor_Z2R.
984 985 986
apply Zopp_involutive.
Qed.

987 988 989 990 991 992 993 994 995 996
Theorem Zceil_le :
  forall x y, (x <= y)%R ->
  (Zceil x <= Zceil y)%Z.
Proof.
intros x y Hxy.
apply Zceil_glb.
apply Rle_trans with (1 := Hxy).
apply Zceil_ub.
Qed.

997
Theorem Zceil_floor_neq :
998
  forall x : R,
Guillaume Melquiond's avatar
Guillaume Melquiond committed
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
  (Z2R (Zfloor x) <> x)%R ->
  (Zceil x = Zfloor x + 1)%Z.
Proof.
intros x Hx.
apply Zceil_imp.
split.
ring_simplify (Zfloor x + 1 - 1)%Z.
apply Rnot_le_lt.
intros H.
apply Hx.
apply Rle_antisym.
apply Zfloor_lb.
exact H.
1012 1013 1014
apply Rlt_le.
rewrite plus_Z2R.
apply Zfloor_ub.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1015 1016
Qed.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
1017 1018 1019 1020 1021 1022 1023 1024 1025
Definition Ztrunc x := if Rlt_le_dec x 0 then Zceil x else Zfloor x.

Theorem Ztrunc_Z2R :
  forall n,
  Ztrunc (Z2R n) = n.
Proof.
intros n.
unfold Ztrunc.
destruct Rlt_le_dec as [H|H].
1026 1027
apply Zceil_Z2R.
apply Zfloor_Z2R.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
Qed.

Theorem Ztrunc_floor :
  forall x,
  (0 <= x)%R ->
  Ztrunc x = Zfloor x.
Proof.
intros x Hx.
unfold Ztrunc.
destruct Rlt_le_dec as [H|_].
elim Rlt_irrefl with (1 := Rle_lt_trans _ _ _ Hx H).
easy.
Qed.

Theorem Ztrunc_ceil :
  forall x,
  (x <= 0)%R ->
  Ztrunc x = Zceil x.
Proof.
intros x Hx.
unfold Ztrunc.
destruct Rlt_le_dec as [_|H].
easy.
rewrite (Rle_antisym _ _ Hx H).
fold (Z2R 0).
1053 1054
rewrite Zceil_Z2R.
apply Zfloor_Z2R.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1055 1056
Qed.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
Theorem Ztrunc_le : 
 forall x y, (x <= y)%R ->
  (Ztrunc x <= Ztrunc y)%Z.
Proof.
intros x y Hxy; unfold Ztrunc at 1.
destruct Rlt_le_dec as [Hx|Hx].
unfold Ztrunc; destruct Rlt_le_dec as [Hy|Hy].
now apply Zceil_le.
apply Zle_trans with 0%Z.
apply Zceil_glb.
simpl; auto with real.
apply Zfloor_lub.
now simpl.
rewrite Ztrunc_floor.
now apply Zfloor_le.
now apply Rle_trans with (1:=Hx). 
Qed.


Guillaume Melquiond's avatar
Guillaume Melquiond committed
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
Theorem Ztrunc_opp :
  forall x,
  Ztrunc (- x) = Zopp (Ztrunc x).
Proof.
intros x.
destruct (Rlt_le_dec x 0) as [H|H].
rewrite Ztrunc_floor.
rewrite Ztrunc_ceil.
apply sym_eq.
apply Zopp_involutive.
now apply Rlt_le.
rewrite <- Ropp_0.
apply Ropp_le_contravar.
now apply Rlt_le.
rewrite Ztrunc_ceil.
unfold Zceil.
rewrite Ropp_involutive.
now rewrite Ztrunc_floor.
rewrite <- Ropp_0.
now apply Ropp_le_contravar.
Qed.

Theorem Ztrunc_abs :
  forall x,
  Ztrunc (Rabs x) = Zabs (Ztrunc x).
Proof.
intros x.
rewrite Ztrunc_floor. 2: apply Rabs_pos.
unfold Ztrunc.
destruct Rlt_le_dec as [H|H].
rewrite Rabs_left with (1 := H).
rewrite Zabs_non_eq.
apply sym_eq.
apply Zopp_involutive.
1110
apply Zceil_glb.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
now apply Rlt_le.
rewrite Rabs_pos_eq with (1 := H).
apply sym_eq.
apply Zabs_eq.
now apply Zfloor_lub.
Qed.

Theorem Ztrunc_lub :
  forall n x,
  (Z2R n <= Rabs x)%R ->
  (n <= Zabs (Ztrunc x))%Z.
Proof.
intros n x H.
rewrite <- Ztrunc_abs.
rewrite Ztrunc_floor. 2: apply Rabs_pos.
now apply Zfloor_lub.
Qed.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
1129
End Floor_Ceil.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1130

1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200
Section Even_Odd.

Definition Zeven (n : Z) :=
  match n with
  | Zpos (xO _) => true
  | Zneg (xO _) => true
  | Z0 => true
  | _ => false
  end.

Theorem Zeven_mult :
  forall x y, Zeven (x * y) = orb (Zeven x) (Zeven y).
Proof.
now intros [|[xp|xp|]|[xp|xp|]] [|[yp|yp|]|[yp|yp|]].
Qed.

Theorem Zeven_opp :
  forall x, Zeven (- x) = Zeven x.
Proof.
now intros [|[n|n|]|[n|n|]].
Qed.

Theorem Zeven_ex :
  forall x, exists p, x = (2 * p + if Zeven x then 0 else 1)%Z.
Proof.
intros [|[n|n|]|[n|n|]].
now exists Z0.
now exists (Zpos n).
now exists (Zpos n).
now exists Z0.
exists (Zneg n - 1)%Z.
change (2 * Zneg n - 1 = 2 * (Zneg n - 1) + 1)%Z.
ring.
now exists (Zneg n).
now exists (-1)%Z.
Qed.

Theorem Zeven_2xp1 :
  forall n, Zeven (2 * n + 1) = false.
Proof.
intros n.
destruct (Zeven_ex (2 * n + 1)) as (p, Hp).
revert Hp.
case (Zeven (2 * n + 1)) ; try easy.
intros H.
apply False_ind.
omega.
Qed.

Theorem Zeven_plus :
  forall x y, Zeven (x + y) = Bool.eqb (Zeven x) (Zeven y).
Proof.
intros x y.
destruct (Zeven_ex x) as (px, Hx).
rewrite Hx at 1.
destruct (Zeven_ex y) as (py, Hy).
rewrite Hy at 1.
replace (2 * px + (if Zeven x then 0 else 1) + (2 * py + (if Zeven y then 0 else 1)))%Z
  with (2 * (px + py) + ((if Zeven x then 0 else 1) + (if Zeven y then 0 else 1)))%Z by ring.
case (Zeven x) ; case (Zeven y).
rewrite Zplus_0_r.
now rewrite Zeven_mult.
apply Zeven_2xp1.
apply Zeven_2xp1.
replace (2 * (px + py) + (1 + 1))%Z with (2 * (px + py + 1))%Z by ring.
now rewrite Zeven_mult.
Qed.

End Even_Odd.

Guillaume Melquiond's avatar
Guillaume Melquiond committed
1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224
Section Proof_Irrelevance.

Scheme eq_dep_elim := Induction for eq Sort Type.

Definition eqbool_dep P (h1 : P true) b :=
  match b return P b -> Prop with
  | true => fun (h2 : P true) => h1 = h2
  | false => fun (h2 : P false) => False
  end.

Lemma eqbool_irrelevance : forall (b : bool) (h1 h2 : b = true), h1 = h2.
Proof.
assert (forall (h : true = true), refl_equal true = h).
apply (eq_dep_elim bool true (eqbool_dep _ _) (refl_equal _)).
intros b.
case b.
intros h1 h2.
now rewrite <- (H h1).
intros h.
discriminate h.
Qed.

End Proof_Irrelevance.

BOLDO Sylvie's avatar
BOLDO Sylvie committed
1225 1226
Section pow.

1227
Record radix := { radix_val : Z ; radix_prop : Zle_bool 2 radix_val = true }.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1228

Guillaume Melquiond's avatar
Guillaume Melquiond committed
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
Theorem radix_val_inj :
  forall r1 r2, radix_val r1 = radix_val r2 -> r1 = r2.
Proof.
intros (r1, H1) (r2, H2) H.
simpl in H.
revert H1.
rewrite H.
intros H1.
apply f_equal.
apply eqbool_irrelevance.
Qed.

1241
Variable r : radix.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1242

1243
Theorem radix_gt_1 : (1 < radix_val r)%Z.
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1244
Proof.
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257
destruct r as (v, Hr). simpl.
apply Zlt_le_trans with 2%Z.
easy.
now apply Zle_bool_imp_le.
Qed.

Theorem radix_pos : (0 < Z2R (radix_val r))%R.
Proof.
destruct r as (v, Hr). simpl.
apply (Z2R_lt 0).
apply Zlt_le_trans with 2%Z.
easy.
now apply Zle_bool_imp_le.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1258 1259
Qed.

1260
Definition bpow e :=
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1261 1262 1263 1264 1265 1266
  match e with
  | Zpos p => Z2R (Zpower_pos (radix_val r) p)
  | Zneg p => Rinv (Z2R (Zpower_pos (radix_val r) p))
  | Z0 => R1
  end.

1267
Theorem Zpower_pos_powerRZ :
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
  forall n m,
  Z2R (Zpower_pos n m) = powerRZ (Z2R n) (Zpos m).
Proof.
intros.
rewrite Zpower_pos_nat.
simpl.
induction (nat_of_P m).
apply refl_equal.
unfold Zpower_nat.
simpl.
rewrite mult_Z2R.
apply Rmult_eq_compat_l.
exact IHn0.
Qed.

1283
Theorem bpow_powerRZ :
Guillaume Melquiond's avatar
Guillaume Melquiond committed
1284
  forall e,
1285
  bpow e = powerRZ (Z2R (radix_val r)) e.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1286
Proof.
1287
destruct e ; unfold bpow.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1288 1289 1290 1291 1292
reflexivity.
now rewrite Zpower_pos_powerRZ.
now rewrite Zpower_pos_powerRZ.
Qed.

1293 1294
Theorem  bpow_ge_0 :
  forall e : Z, (0 <= bpow e)%R.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1295 1296
Proof.
intros.
1297
rewrite bpow_powerRZ.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1298
apply powerRZ_le.
1299
apply radix_pos.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1300 1301
Qed.

1302 1303
Theorem bpow_gt_0 :
  forall e : Z, (0 < bpow e)%R.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1304 1305
Proof.
intros.
1306
rewrite bpow_powerRZ.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1307
apply powerRZ_lt.
1308
apply radix_pos.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1309 1310
Qed.

1311 1312
Theorem bpow_add :
  forall e1 e2 : Z, (bpow (e1 + e2) = bpow e1 * bpow e2)%R.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1313 1314
Proof.
intros.
1315
repeat rewrite bpow_powerRZ.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1316
apply powerRZ_add.
1317 1318
apply Rgt_not_eq.
apply radix_pos.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1319 1320
Qed.

1321 1322
Theorem bpow_1 :
  bpow 1 = Z2R (radix_val r).
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1323
Proof.
1324
unfold bpow, Zpower_pos, iter_pos.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1325 1326 1327
now rewrite Zmult_1_r.
Qed.

1328 1329
Theorem bpow_add1 :
  forall e : Z, (bpow (e+1) = Z2R (radix_val r) * bpow e)%R.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1330 1331
Proof.
intros.
1332 1333
rewrite <- bpow_1.
rewrite <- bpow_add.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1334 1335 1336
now rewrite Zplus_comm.
Qed.

1337 1338
Theorem bpow_opp :
  forall e : Z, (bpow (-e) = /bpow e)%R.
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1339 1340 1341 1342 1343 1344
Proof.
intros e; destruct e.
simpl; now rewrite Rinv_1.
now replace (-Zpos p)%Z with (Zneg p) by reflexivity.
replace (-Zneg p)%Z with (Zpos p) by reflexivity.
simpl; rewrite Rinv_involutive; trivial.
1345
generalize (bpow_gt_0 (Zpos p)).
BOLDO Sylvie's avatar
BOLDO Sylvie committed
1346 1347 1348
simpl; auto with real.
Qed.

1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
Theorem Z2R_Zpower_nat :
  forall e : nat,
  Z2R (Zpower_nat (radix_val r) e) = bpow (Z_of_nat e).
Proof.
intros [|e].
split.
rewrite <- nat_of_P_o_P_of_succ_nat_eq_succ.
rewrite <- Zpower_pos_nat.
now rewrite <- Zpos_eq_Z_of_nat_o_nat_of_P.
Qed.

1360
Theorem Z2R_Zpower :
1361 1362
  forall e : Z,
  (0 <= e)%Z ->
1363
  Z2R (Zpower (radix_val r) e) = bpow e.
1364 1365 1366 1367 1368 1369 1370
Proof.
intros [|e|e] H.
split.
split.
now elim H.
Qed.

1371
Theorem bpow_lt_aux :
1372
  forall e1 e2 : Z,
1373
  (e1 < e2)%Z -> (bpow e1 < bpow e2)%R.
1374 1375 1376
Proof.
intros e1 e2 H.
replace e2 with (e1 + (e2 - e1))%Z by ring.
1377 1378
rewrite <- (Rmult_1_r (bpow e1)).
rewrite bpow_add.
1379
apply Rmult_lt_compat_l.
1380
apply bpow_gt_0.
1381 1382 1383
assert (0 < e2 - e1)%Z by omega.
destruct (e2 - e1)%Z ; try discriminate H0.
clear.
1384
unfold bpow.
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395
apply (Z2R_lt 1).
rewrite Zpower_pos_nat.
case_eq (nat_of_P p).
intros H.
elim (lt_irrefl 0).
pattern O at 2 ; rewrite <- H.
apply lt_O_nat_of_P.
intros n _.
assert (1 < Zpower_nat (radix_val r) 1)%Z.
unfold Zpower_nat, iter_nat.
rewrite Zmult_1_r.
1396
apply radix_gt_1.
1397 1398 1399 1400 1401 1402 1403 1404 1405 1406
induction n.
exact H.
change (S (S n)) with (1 + (S n))%nat.
rewrite Zpower_nat_is_exp.
change 1%Z with (1 * 1)%Z.
apply Zmult_lt_compat.
now split.
now split.
Qed.

1407
Theorem bpow_lt :
1408
  forall e1 e2 : Z,
1409
  (e1 < e2)%Z <-> (bpow e1 < bpow e2)%R.
1410 1411 1412
Proof.
intros e1 e2.
split.
1413
apply bpow_lt_aux.
1414 1415 1416 <