aboutsummaryrefslogtreecommitdiff
path: root/theories/Reals/ConstructiveReals.v
blob: 25242f5ea93f4ac740d220f7929377d8a38534b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
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
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
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
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
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
(************************************************************************)
(*         *   The Coq Proof Assistant / The Coq Development Team       *)
(*  v      *   INRIA, CNRS and contributors - Copyright 1999-2019       *)
(* <O___,, *       (see CREDITS file for the list of authors)           *)
(*   \VV/  **************************************************************)
(*    //   *    This file is distributed under the terms of the         *)
(*         *     GNU Lesser General Public License Version 2.1          *)
(*         *     (see LICENSE file for the text of the license)         *)
(************************************************************************)
(************************************************************************)

(** An interface for constructive and computable real numbers.
    All of its instances are isomorphic (see file ConstructiveRealsMorphisms).
    For example it contains the Cauchy reals implemented in file
    ConstructivecauchyReals and the sumbool-based Dedekind reals defined by

Structure R := {
  (* The cuts are represented as propositional functions, rather than subsets,
     as there are no subsets in type theory. *)
  lower : Q -> Prop;
  upper : Q -> Prop;
  (* The cuts respect equality on Q. *)
  lower_proper : Proper (Qeq ==> iff) lower;
  upper_proper : Proper (Qeq ==> iff) upper;
  (* The cuts are inhabited. *)
  lower_bound : { q : Q | lower q };
  upper_bound : { r : Q | upper r };
  (* The lower cut is a lower set. *)
  lower_lower : forall q r, q < r -> lower r -> lower q;
  (* The lower cut is open. *)
  lower_open : forall q, lower q -> exists r, q < r /\ lower r;
  (* The upper cut is an upper set. *)
  upper_upper : forall q r, q < r -> upper q -> upper r;
  (* The upper cut is open. *)
  upper_open : forall r, upper r -> exists q,  q < r /\ upper q;
  (* The cuts are disjoint. *)
  disjoint : forall q, ~ (lower q /\ upper q);
  (* There is no gap between the cuts. *)
  located : forall q r, q < r -> { lower q } + { upper r }
}.

  see github.com/andrejbauer/dedekind-reals for the Prop-based
  version of those Dedekind reals (although Prop fails to make
  them an instance of ConstructiveReals).

  Any computation about constructive reals, can be worked
  in the fastest instance for it; we then transport the results
  to all other instances by the isomorphisms. This way of working
  is different from the usual interfaces, where we would rather
  prove things abstractly, by quantifying universally on the instance.

  The functions of ConstructiveReals do not have a direct impact
  on performance, because algorithms will be extracted from instances,
  and because fast ConstructiveReals morphisms should be coded
  manually. However, since instances are forced to implement
  those functions, it is probable that they will also use them
  in their algorithms. So those functions hint at what we think
  will yield fast and small extracted programs. *)


Require Import QArith.

Definition isLinearOrder (X : Set) (Xlt : X -> X -> Set) : Set
  := (forall x y:X, Xlt x y -> Xlt y x -> False)
     * (forall x y z : X, Xlt x y -> Xlt y z -> Xlt x z)
     * (forall x y z : X, Xlt x z -> Xlt x y + Xlt y z).

Definition orderEq (X : Set) (Xlt : X -> X -> Set) (x y : X) : Prop
  := (Xlt x y -> False) /\ (Xlt y x -> False).

Definition orderAppart (X : Set) (Xlt : X -> X -> Set) (x y : X) : Set
  := Xlt x y  +  Xlt y x.

Definition orderLe (X : Set) (Xlt : X -> X -> Set) (x y : X) : Prop
  := Xlt y x -> False.

Definition sig_forall_dec_T : Type
  := forall (P : nat -> Prop), (forall n, {P n} + {~P n})
                   -> {n | ~P n} + {forall n, P n}.

Definition sig_not_dec_T : Type := forall P : Prop, { ~~P } + { ~P }.

Record ConstructiveReals : Type :=
  {
    CRcarrier : Set;

    (* Put this order relation in sort Set rather than Prop,
       to allow the definition of fast ConstructiveReals morphisms.
       For example, the Cauchy reals do store information in
       the proofs of CRlt, which is used in algorithms in sort Set. *)
    CRlt : CRcarrier -> CRcarrier -> Set;
    CRltLinear : isLinearOrder CRcarrier CRlt;

    (* The propositional truncation of CRlt. It facilitates proofs
       when computations are not considered important, for example in
       classical reals with extra logical axioms. *)
    CRltProp : CRcarrier -> CRcarrier -> Prop;
    (* This choice algorithm can be slow, keep it for the classical
       quotient of the reals, where computations are blocked by
       axioms like LPO. *)
    CRltEpsilon : forall x y : CRcarrier, CRltProp x y -> CRlt x y;
    CRltForget : forall x y : CRcarrier, CRlt x y -> CRltProp x y;
    CRltDisjunctEpsilon : forall a b c d : CRcarrier,
        (CRltProp a b \/ CRltProp c d) -> CRlt a b  +  CRlt c d;

    (* Constants *)
    CRzero : CRcarrier;
    CRone : CRcarrier;

    (* Addition and multiplication *)
    CRplus : CRcarrier -> CRcarrier -> CRcarrier;
    CRopp : CRcarrier -> CRcarrier; (* Computable opposite,
                         stronger than Prop-existence of opposite *)
    CRmult : CRcarrier -> CRcarrier -> CRcarrier;

    CRisRing : ring_theory CRzero CRone CRplus CRmult
                          (fun x y => CRplus x (CRopp y)) CRopp (orderEq CRcarrier CRlt);
    CRisRingExt : ring_eq_ext CRplus CRmult CRopp (orderEq CRcarrier CRlt);

    (* Compatibility with order *)
    CRzero_lt_one : CRlt CRzero CRone; (* 0 # 1 would only allow 0 < 1 because
                                    of Fmult_lt_0_compat so request 0 < 1 directly. *)
    CRplus_lt_compat_l : forall r r1 r2 : CRcarrier,
        CRlt r1 r2 -> CRlt (CRplus r r1) (CRplus r r2);
    CRplus_lt_reg_l : forall r r1 r2 : CRcarrier,
        CRlt (CRplus r r1) (CRplus r r2) -> CRlt r1 r2;
    CRmult_lt_0_compat : forall x y : CRcarrier,
        CRlt CRzero x -> CRlt CRzero y -> CRlt CRzero (CRmult x y);

    (* A constructive total inverse function on F would need to be continuous,
       which is impossible because we cannot connect plus and minus infinities.
       Therefore it has to be a partial function, defined on non zero elements.
       For this reason we cannot use Coq's field_theory and field tactic.

       To implement Finv by Cauchy sequences we need orderAppart,
       ~orderEq is not enough. *)
    CRinv : forall x : CRcarrier, orderAppart _ CRlt x CRzero -> CRcarrier;
    CRinv_l : forall (r:CRcarrier) (rnz : orderAppart _ CRlt r CRzero),
        orderEq _ CRlt (CRmult (CRinv r rnz) r) CRone;
    CRinv_0_lt_compat : forall (r : CRcarrier) (rnz : orderAppart _ CRlt r CRzero),
        CRlt CRzero r -> CRlt CRzero (CRinv r rnz);

    (* The initial field morphism (in characteristic zero).
       The abstract definition by iteration of addition is
       probably the slowest. Let each instance implement
       a faster (and often simpler) version. *)
    CR_of_Q : Q -> CRcarrier;
    CR_of_Q_plus : forall q r : Q, orderEq _ CRlt (CR_of_Q (q+r))
                                           (CRplus (CR_of_Q q) (CR_of_Q r));
    CR_of_Q_mult : forall q r : Q, orderEq _ CRlt (CR_of_Q (q*r))
                                           (CRmult (CR_of_Q q) (CR_of_Q r));
    CR_of_Q_one : orderEq _ CRlt (CR_of_Q 1) CRone;
    CR_of_Q_lt : forall q r : Q,
        Qlt q r -> CRlt (CR_of_Q q) (CR_of_Q r);
    lt_CR_of_Q : forall q r : Q,
        CRlt (CR_of_Q q) (CR_of_Q r) -> Qlt q r;

    (* This function is very fast in both the Cauchy and Dedekind
       instances, because this rational number q is almost what
       the proof of CRlt x y contains.
       This function is also the heart of the computation of
       constructive real numbers : it approximates x to any
       requested precision y. *)
    CR_Q_dense : forall x y : CRcarrier, CRlt x y ->
       { q : Q  &  prod (CRlt x (CR_of_Q q))
                        (CRlt (CR_of_Q q) y) };
    CR_archimedean : forall x : CRcarrier,
        { n : positive  &  CRlt x (CR_of_Q (Z.pos n # 1)) };

    CRminus (x y : CRcarrier) : CRcarrier
    := CRplus x (CRopp y);

    (* Definitions of convergence and Cauchy-ness. The formulas
       with orderLe or CRlt are logically equivalent, the choice of
       orderLe in sort Prop is a question of performance.
       It is very rare to turn back to the strict order to
       define functions in sort Set, so we prefer to discard
       those proofs during extraction. And even in those rare cases,
       it is easy to divide epsilon by 2 for example. *)
    CR_cv (un : nat -> CRcarrier) (l : CRcarrier) : Set
    := forall p:positive,
        { n : nat  |  forall i:nat, le n i
                           -> orderLe _ CRlt (CR_of_Q (-1#p)) (CRminus (un i) l)
                             /\ orderLe _ CRlt (CRminus (un i) l) (CR_of_Q (1#p)) };
    CR_cauchy (un : nat -> CRcarrier) : Set
    := forall p : positive,
        { n : nat  |  forall i j:nat, le n i -> le n j
                             -> orderLe _ CRlt (CR_of_Q (-1#p)) (CRminus (un i) (un j))
                               /\ orderLe _ CRlt (CRminus (un i) (un j)) (CR_of_Q (1#p)) };

    (* For the Cauchy reals, this algorithm consists in building
       a Cauchy sequence of rationals un : nat -> Q that has
       the same limit as xn. For each n:nat, un n is a 1/n
       rational approximation of a point of xn that has converged
       within 1/n. *)
    CR_complete :
      forall xn : (nat -> CRcarrier),
        CR_cauchy xn -> { l : CRcarrier  &  CR_cv xn l };
  }.

Lemma CRlt_asym : forall (R : ConstructiveReals) (x y : CRcarrier R),
    CRlt R x y -> CRlt R y x -> False.
Proof.
  intros. destruct (CRltLinear R), p.
  apply (f x y); assumption.
Qed.

Lemma CRlt_proper
  : forall R : ConstructiveReals,
    CMorphisms.Proper
      (CMorphisms.respectful (orderEq _ (CRlt R))
                             (CMorphisms.respectful (orderEq _ (CRlt R)) CRelationClasses.iffT)) (CRlt R).
Proof.
  intros R x y H x0 y0 H0. destruct H, H0.
  destruct (CRltLinear R). split.
  - intro. destruct (s x y x0). assumption.
    contradiction. destruct (s y y0 x0).
    assumption. assumption. contradiction.
  - intro. destruct (s y x y0). assumption.
    contradiction. destruct (s x x0 y0).
    assumption. assumption. contradiction.
Qed.

Lemma CRle_refl : forall (R : ConstructiveReals) (x : CRcarrier R),
    CRlt R x x -> False.
Proof.
  intros. destruct (CRltLinear R), p.
  exact (f x x H H).
Qed.

Lemma CRle_lt_trans : forall (R : ConstructiveReals) (r1 r2 r3 : CRcarrier R),
    (CRlt R r2 r1 -> False) -> CRlt R r2 r3 -> CRlt R r1 r3.
Proof.
  intros. destruct (CRltLinear R).
  destruct (s r2 r1 r3 H0). contradiction. apply c.
Qed.

Lemma CRlt_le_trans : forall (R : ConstructiveReals) (r1 r2 r3 : CRcarrier R),
    CRlt R r1 r2 -> (CRlt R r3 r2 -> False) -> CRlt R r1 r3.
Proof.
  intros. destruct (CRltLinear R).
  destruct (s r1 r3 r2 H). apply c. contradiction.
Qed.

Lemma CRle_trans : forall (R : ConstructiveReals) (x y z : CRcarrier R),
    orderLe _ (CRlt R) x y -> orderLe _ (CRlt R) y z -> orderLe _ (CRlt R) x z.
Proof.
  intros. intro abs. apply H0.
  apply (CRlt_le_trans _ _ x); assumption.
Qed.

Lemma CRlt_trans : forall (R : ConstructiveReals) (x y z : CRcarrier R),
    CRlt R x y -> CRlt R y z -> CRlt R x z.
Proof.
  intros. apply (CRlt_le_trans R _ y _ H).
  apply CRlt_asym. exact H0.
Defined.

Lemma CRlt_trans_flip : forall (R : ConstructiveReals) (x y z : CRcarrier R),
    CRlt R y z -> CRlt R x y -> CRlt R x z.
Proof.
  intros. apply (CRlt_le_trans R _ y). exact H0.
  apply CRlt_asym. exact H.
Defined.

Lemma CReq_refl : forall (R : ConstructiveReals) (x : CRcarrier R),
    orderEq _ (CRlt R) x x.
Proof.
  split; apply CRle_refl.
Qed.

Lemma CReq_sym : forall (R : ConstructiveReals) (x y : CRcarrier R),
    orderEq _ (CRlt R) x y
    -> orderEq _ (CRlt R) y x.
Proof.
  intros. destruct H. split; intro abs; contradiction.
Qed.

Lemma CReq_trans : forall (R : ConstructiveReals) (x y z : CRcarrier R),
    orderEq _ (CRlt R) x y
    -> orderEq _ (CRlt R) y z
    -> orderEq _ (CRlt R) x z.
Proof.
  intros. destruct H,H0. destruct (CRltLinear R), p. split.
  - intro abs. destruct (s _ y _ abs); contradiction.
  - intro abs. destruct (s _ y _ abs); contradiction.
Qed.

Lemma CR_setoid : forall R : ConstructiveReals,
    Setoid_Theory (CRcarrier R) (orderEq _ (CRlt R)).
Proof.
  split. intro x. apply CReq_refl.
  intros x y. apply CReq_sym.
  intros x y z. apply CReq_trans.
Qed.

Lemma CRplus_0_r : forall (R : ConstructiveReals) (x : CRcarrier R),
    orderEq _ (CRlt R) (CRplus R x (CRzero R)) x.
Proof.
  intros. destruct (CRisRing R).
  apply (CReq_trans R _ (CRplus R (CRzero R) x)).
  apply Radd_comm. apply Radd_0_l.
Qed.

Lemma CRmult_1_r : forall (R : ConstructiveReals) (x : CRcarrier R),
    orderEq _ (CRlt R) (CRmult R x (CRone R)) x.
Proof.
  intros. destruct (CRisRing R).
  apply (CReq_trans R _ (CRmult R (CRone R) x)).
  apply Rmul_comm. apply Rmul_1_l.
Qed.

Lemma CRplus_opp_l : forall (R : ConstructiveReals) (x : CRcarrier R),
    orderEq _ (CRlt R) (CRplus R (CRopp R x) x) (CRzero R).
Proof.
  intros. destruct (CRisRing R).
  apply (CReq_trans R _ (CRplus R x (CRopp R x))).
  apply Radd_comm. apply Ropp_def.
Qed.

Lemma CRplus_lt_compat_r : forall (R : ConstructiveReals) (r r1 r2 : CRcarrier R),
    CRlt R r1 r2 -> CRlt R (CRplus R r1 r) (CRplus R r2 r).
Proof.
  intros. destruct (CRisRing R).
  apply (CRlt_proper R (CRplus R r r1) (CRplus R r1 r) (Radd_comm _ _)
                     (CRplus R r2 r) (CRplus R r2 r)).
  apply CReq_refl.
  apply (CRlt_proper R _ _ (CReq_refl _ _) _ (CRplus R r r2)).
  apply Radd_comm. apply CRplus_lt_compat_l. exact H.
Qed.

Lemma CRplus_lt_reg_r : forall (R : ConstructiveReals) (r r1 r2 : CRcarrier R),
    CRlt R (CRplus R r1 r) (CRplus R r2 r) -> CRlt R r1 r2.
Proof.
  intros. destruct (CRisRing R).
  apply (CRlt_proper R (CRplus R r r1) (CRplus R r1 r) (Radd_comm _ _)
                     (CRplus R r2 r) (CRplus R r2 r)) in H.
  2: apply CReq_refl.
  apply (CRlt_proper R _ _ (CReq_refl _ _) _ (CRplus R r r2)) in H.
  apply CRplus_lt_reg_l in H. exact H.
  apply Radd_comm.
Qed.

Lemma CRplus_le_compat_l : forall (R : ConstructiveReals) (r r1 r2 : CRcarrier R),
    orderLe _ (CRlt R) r1 r2
    -> orderLe _ (CRlt R) (CRplus R r r1) (CRplus R r r2).
Proof.
  intros. intros abs. apply CRplus_lt_reg_l in abs. apply H. exact abs.
Qed.

Lemma CRplus_le_compat_r : forall (R : ConstructiveReals) (r r1 r2 : CRcarrier R),
    orderLe _ (CRlt R) r1 r2
    -> orderLe _ (CRlt R) (CRplus R r1 r) (CRplus R r2 r).
Proof.
  intros. intros abs. apply CRplus_lt_reg_r in abs. apply H. exact abs.
Qed.

Lemma CRplus_le_reg_l : forall (R : ConstructiveReals) (r r1 r2 : CRcarrier R),
    orderLe _ (CRlt R) (CRplus R r r1) (CRplus R r r2)
    -> orderLe _ (CRlt R) r1 r2.
Proof.
  intros. intro abs. apply H. clear H.
  apply CRplus_lt_compat_l. exact abs.
Qed.

Lemma CRplus_le_reg_r : forall (R : ConstructiveReals) (r r1 r2 : CRcarrier R),
    orderLe _ (CRlt R) (CRplus R r1 r) (CRplus R r2 r)
    -> orderLe _ (CRlt R) r1 r2.
Proof.
  intros. intro abs. apply H. clear H.
  apply CRplus_lt_compat_r. exact abs.
Qed.

Lemma CRplus_lt_le_compat :
  forall (R : ConstructiveReals) (r1 r2 r3 r4 : CRcarrier R),
    CRlt R r1 r2
    -> (CRlt R r4 r3 -> False)
    -> CRlt R (CRplus R r1 r3) (CRplus R r2 r4).
Proof.
  intros. apply (CRlt_le_trans R _ (CRplus R r2 r3)).
  apply CRplus_lt_compat_r. exact H. intro abs.
  apply CRplus_lt_reg_l in abs. contradiction.
Qed.

Lemma CRplus_eq_reg_l : forall (R : ConstructiveReals) (r r1 r2 : CRcarrier R),
    orderEq _ (CRlt R) (CRplus R r r1) (CRplus R r r2)
    -> orderEq _ (CRlt R) r1 r2.
Proof.
  intros.
  destruct (CRisRingExt R). clear Rmul_ext Ropp_ext.
  pose proof (Radd_ext
                (CRopp R r) (CRopp R r) (CReq_refl _ _)
                _ _ H).
  destruct (CRisRing R).
  apply (CReq_trans _ r1) in H0.
  apply (CReq_trans R _ _ _ H0).
  apply (CReq_trans R _ (CRplus R (CRplus R (CRopp R r) r) r2)).
  apply Radd_assoc.
  apply (CReq_trans R _ (CRplus R (CRzero R) r2)).
  apply Radd_ext. apply CRplus_opp_l. apply CReq_refl.
  apply Radd_0_l. apply CReq_sym.
  apply (CReq_trans R _ (CRplus R (CRplus R (CRopp R r) r) r1)).
  apply Radd_assoc.
  apply (CReq_trans R _ (CRplus R (CRzero R) r1)).
  apply Radd_ext. apply CRplus_opp_l. apply CReq_refl.
  apply Radd_0_l.
Qed.

Lemma CRplus_eq_reg_r : forall (R : ConstructiveReals) (r r1 r2 : CRcarrier R),
    orderEq _ (CRlt R) (CRplus R r1 r) (CRplus R r2 r)
    -> orderEq _ (CRlt R) r1 r2.
Proof.
  intros. apply (CRplus_eq_reg_l R r).
  apply (CReq_trans R _ (CRplus R r1 r)). apply (Radd_comm (CRisRing R)).
  apply (CReq_trans R _ (CRplus R r2 r)).
  exact H. apply (Radd_comm (CRisRing R)).
Qed.

Lemma CRopp_involutive : forall (R : ConstructiveReals) (r : CRcarrier R),
    orderEq _ (CRlt R) (CRopp R (CRopp R r)) r.
Proof.
  intros. apply (CRplus_eq_reg_l R (CRopp R r)).
  apply (CReq_trans R _ (CRzero R)). apply CRisRing.
  apply CReq_sym, (CReq_trans R _ (CRplus R r (CRopp R r))).
  apply CRisRing. apply CRisRing.
Qed.

Lemma CRopp_gt_lt_contravar
  : forall (R : ConstructiveReals) (r1 r2 : CRcarrier R),
    CRlt R r2 r1 -> CRlt R (CRopp R r1) (CRopp R r2).
Proof.
  intros. apply (CRplus_lt_reg_l R r1).
  destruct (CRisRing R).
  apply (CRle_lt_trans R _ (CRzero R)). apply Ropp_def.
  apply (CRplus_lt_compat_l R (CRopp R r2)) in H.
  apply (CRle_lt_trans R _ (CRplus R (CRopp R r2) r2)).
  apply (CRle_trans R _ (CRplus R r2 (CRopp R r2))).
  destruct (Ropp_def r2). exact H0.
  destruct (Radd_comm r2 (CRopp R r2)). exact H1.
  apply (CRlt_le_trans R _ _ _ H).
  destruct (Radd_comm r1 (CRopp R r2)). exact H0.
Qed.

Lemma CRopp_lt_cancel : forall (R : ConstructiveReals) (r1 r2 : CRcarrier R),
    CRlt R (CRopp R r2) (CRopp R r1) -> CRlt R r1 r2.
Proof.
  intros. apply (CRplus_lt_compat_r R r1) in H.
  destruct (CRplus_opp_l R r1) as [_ H1].
  apply (CRlt_le_trans R _ _ _ H) in H1. clear H.
  apply (CRplus_lt_compat_l R r2) in H1.
  destruct (CRplus_0_r R r2) as [_ H0].
  apply (CRlt_le_trans R _ _ _ H1) in H0. clear H1.
  destruct (Radd_assoc (CRisRing R) r2 (CRopp R r2) r1) as [H _].
  apply (CRle_lt_trans R _ _ _ H) in H0. clear H.
  apply (CRle_lt_trans R _ (CRplus R (CRzero R) r1)).
  apply (Radd_0_l (CRisRing R)).
  apply (CRle_lt_trans R _ (CRplus R (CRplus R r2 (CRopp R r2)) r1)).
  2: exact H0. apply CRplus_le_compat_r.
  destruct (Ropp_def (CRisRing R) r2). exact H.
Qed.

Lemma CRopp_plus_distr : forall (R : ConstructiveReals) (r1 r2 : CRcarrier R),
    orderEq _ (CRlt R) (CRopp R (CRplus R r1 r2)) (CRplus R (CRopp R r1) (CRopp R r2)).
Proof.
  intros. destruct (CRisRing R), (CRisRingExt R).
  apply (CRplus_eq_reg_l R (CRplus R r1 r2)).
  apply (CReq_trans R _ (CRzero R)). apply Ropp_def.
  apply (CReq_trans R _ (CRplus R (CRplus R r2 r1) (CRplus R (CRopp R r1) (CRopp R r2)))).
  apply (CReq_trans R _ (CRplus R r2 (CRplus R r1 (CRplus R (CRopp R r1) (CRopp R r2))))).
  apply (CReq_trans R _ (CRplus R r2 (CRopp R r2))).
  apply CReq_sym. apply Ropp_def. apply Radd_ext.
  apply CReq_refl.
  apply (CReq_trans R _ (CRplus R (CRzero R) (CRopp R r2))).
  apply CReq_sym, Radd_0_l.
  apply (CReq_trans R _ (CRplus R (CRplus R r1 (CRopp R r1)) (CRopp R r2))).
  apply Radd_ext. 2: apply CReq_refl. apply CReq_sym, Ropp_def.
  apply CReq_sym, Radd_assoc. apply Radd_assoc.
  apply Radd_ext. 2: apply CReq_refl. apply Radd_comm.
Qed.

Lemma CRmult_plus_distr_l : forall (R : ConstructiveReals) (r1 r2 r3 : CRcarrier R),
    orderEq _ (CRlt R) (CRmult R r1 (CRplus R r2 r3))
            (CRplus R (CRmult R r1 r2) (CRmult R r1 r3)).
Proof.
  intros. destruct (CRisRing R).
  apply (CReq_trans R _ (CRmult R (CRplus R r2 r3) r1)).
  apply Rmul_comm.
  apply (CReq_trans R _ (CRplus R (CRmult R r2 r1) (CRmult R r3 r1))).
  apply Rdistr_l.
  apply (CReq_trans R _ (CRplus R (CRmult R r1 r2) (CRmult R r3 r1))).
  destruct (CRisRingExt R). apply Radd_ext.
  apply Rmul_comm. apply CReq_refl.
  destruct (CRisRingExt R). apply Radd_ext.
  apply CReq_refl. apply Rmul_comm.
Qed.

(* x == x+x -> x == 0 *)
Lemma CRzero_double : forall (R : ConstructiveReals) (x : CRcarrier R),
    orderEq _ (CRlt R) x (CRplus R x x)
    -> orderEq _ (CRlt R) x (CRzero R).
Proof.
  intros.
  apply (CRplus_eq_reg_l R x), CReq_sym, (CReq_trans R _ x).
  apply CRplus_0_r. exact H.
Qed.

Lemma CRmult_0_r : forall (R : ConstructiveReals) (x : CRcarrier R),
    orderEq _ (CRlt R) (CRmult R x (CRzero R)) (CRzero R).
Proof.
  intros. apply CRzero_double.
  apply (CReq_trans R _ (CRmult R x (CRplus R (CRzero R) (CRzero R)))).
  destruct (CRisRingExt R). apply Rmul_ext. apply CReq_refl.
  apply CReq_sym, CRplus_0_r.
  destruct (CRisRing R). apply CRmult_plus_distr_l.
Qed.

Lemma CRopp_mult_distr_r : forall (R : ConstructiveReals) (r1 r2 : CRcarrier R),
    orderEq _ (CRlt R) (CRopp R (CRmult R r1 r2))
            (CRmult R r1 (CRopp R r2)).
Proof.
  intros. apply (CRplus_eq_reg_l R (CRmult R r1 r2)).
  destruct (CRisRing R).
  apply (CReq_trans R _ (CRzero R)). apply Ropp_def.
  apply (CReq_trans R _ (CRmult R r1 (CRplus R r2 (CRopp R r2)))).
  2: apply CRmult_plus_distr_l.
  apply (CReq_trans R _ (CRmult R r1 (CRzero R))).
  apply CReq_sym, CRmult_0_r.
  destruct (CRisRingExt R). apply Rmul_ext. apply CReq_refl.
  apply CReq_sym, Ropp_def.
Qed.

Lemma CRopp_mult_distr_l : forall (R : ConstructiveReals) (r1 r2 : CRcarrier R),
    orderEq _ (CRlt R) (CRopp R (CRmult R r1 r2))
            (CRmult R (CRopp R r1) r2).
Proof.
  intros. apply (CReq_trans R _ (CRmult R r2 (CRopp R r1))).
  apply (CReq_trans R _ (CRopp R (CRmult R r2 r1))).
  apply (Ropp_ext (CRisRingExt R)).
  apply CReq_sym, (Rmul_comm (CRisRing R)).
  apply CRopp_mult_distr_r.
  apply CReq_sym, (Rmul_comm (CRisRing R)).
Qed.

Lemma CRmult_lt_compat_r : forall (R : ConstructiveReals) (r r1 r2 : CRcarrier R),
    CRlt R (CRzero R) r
    -> CRlt R r1 r2
    -> CRlt R (CRmult R r1 r) (CRmult R r2 r).
Proof.
  intros. apply (CRplus_lt_reg_r R (CRopp R (CRmult R r1 r))).
  apply (CRle_lt_trans R _ (CRzero R)).
  apply (Ropp_def (CRisRing R)).
  apply (CRlt_le_trans R _ (CRplus R (CRmult R r2 r) (CRmult R (CRopp R r1) r))).
  apply (CRlt_le_trans R _ (CRmult R (CRplus R r2 (CRopp R r1)) r)).
  apply CRmult_lt_0_compat. 2: exact H.
  apply (CRplus_lt_reg_r R r1).
  apply (CRle_lt_trans R _ r1). apply (Radd_0_l (CRisRing R)).
  apply (CRlt_le_trans R _ r2 _ H0).
  apply (CRle_trans R _ (CRplus R r2 (CRplus R (CRopp R r1) r1))).
  apply (CRle_trans R _ (CRplus R r2 (CRzero R))).
  destruct (CRplus_0_r R r2). exact H1.
  apply CRplus_le_compat_l. destruct (CRplus_opp_l R r1). exact H1.
  destruct (Radd_assoc (CRisRing R) r2 (CRopp R r1) r1). exact H2.
  destruct (CRisRing R).
  destruct (Rdistr_l r2 (CRopp R r1) r). exact H2.
  apply CRplus_le_compat_l. destruct (CRopp_mult_distr_l R r1 r).
  exact H1.
Qed.

Lemma CRinv_r : forall (R : ConstructiveReals) (r:CRcarrier R)
                  (rnz : orderAppart _ (CRlt R) r (CRzero R)),
    orderEq _ (CRlt R) (CRmult R r (CRinv R r rnz)) (CRone R).
Proof.
  intros. apply (CReq_trans R _ (CRmult R (CRinv R r rnz) r)).
  apply (CRisRing R). apply CRinv_l.
Qed.

Lemma CRmult_lt_reg_r : forall (R : ConstructiveReals) (r r1 r2 : CRcarrier R),
    CRlt R (CRzero R) r
    -> CRlt R (CRmult R r1 r) (CRmult R r2 r)
    -> CRlt R r1 r2.
Proof.
  intros. apply (CRmult_lt_compat_r R (CRinv R r (inr H))) in H0.
  2: apply CRinv_0_lt_compat, H.
  apply (CRle_lt_trans R _ (CRmult R (CRmult R r1 r) (CRinv R r (inr H)))).
  - clear H0. apply (CRle_trans R _ (CRmult R r1 (CRone R))).
    destruct (CRmult_1_r R r1). exact H0.
    apply (CRle_trans R _ (CRmult R r1 (CRmult R r (CRinv R r (inr H))))).
    destruct (Rmul_ext (CRisRingExt R) r1 r1 (CReq_refl R r1)
                       (CRmult R r (CRinv R r (inr H))) (CRone R)).
    apply CRinv_r. exact H0.
    destruct (Rmul_assoc (CRisRing R) r1 r (CRinv R r (inr H))). exact H1.
  - apply (CRlt_le_trans R _ (CRmult R (CRmult R r2 r) (CRinv R r (inr H)))).
    exact H0. clear H0.
    apply (CRle_trans R _ (CRmult R r2 (CRone R))).
    2: destruct (CRmult_1_r R r2); exact H1.
    apply (CRle_trans R _ (CRmult R r2 (CRmult R r (CRinv R r (inr H))))).
    destruct (Rmul_assoc (CRisRing R) r2 r (CRinv R r (inr H))). exact H0.
    destruct (Rmul_ext (CRisRingExt R) r2 r2 (CReq_refl R r2)
                       (CRmult R r (CRinv R r (inr H))) (CRone R)).
    apply CRinv_r. exact H1.
Qed.

Lemma CRmult_lt_reg_l : forall (R : ConstructiveReals) (r r1 r2 : CRcarrier R),
    CRlt R (CRzero R) r
    -> CRlt R (CRmult R r r1) (CRmult R r r2)
    -> CRlt R r1 r2.
Proof.
  intros.
  destruct (Rmul_comm (CRisRing R) r r1) as [H1 _].
  apply (CRle_lt_trans R _ _ _ H1) in H0. clear H1.
  destruct (Rmul_comm (CRisRing R) r r2) as [_ H1].
  apply (CRlt_le_trans R _ _ _ H0) in H1. clear H0.
  apply CRmult_lt_reg_r in H1.
  exact H1. exact H.
Qed.

Lemma CRmult_le_compat_l : forall (R : ConstructiveReals) (r r1 r2 : CRcarrier R),
    CRlt R (CRzero R) r
    -> orderLe _ (CRlt R) r1 r2
    -> orderLe _ (CRlt R) (CRmult R r r1) (CRmult R r r2).
Proof.
  intros. intro abs. apply CRmult_lt_reg_l in abs.
  contradiction. exact H.
Qed.

Lemma CRmult_le_compat_r : forall (R : ConstructiveReals) (r r1 r2 : CRcarrier R),
    CRlt R (CRzero R) r
    -> orderLe _ (CRlt R) r1 r2
    -> orderLe _ (CRlt R) (CRmult R r1 r) (CRmult R r2 r).
Proof.
  intros. intro abs. apply CRmult_lt_reg_r in abs.
  contradiction. exact H.
Qed.

Lemma CRmult_eq_reg_r : forall (R : ConstructiveReals) (r r1 r2 : CRcarrier R),
    orderAppart _ (CRlt R) (CRzero R) r
    -> orderEq _ (CRlt R) (CRmult R r1 r) (CRmult R r2 r)
    -> orderEq _ (CRlt R) r1 r2.
Proof.
  intros. destruct H0,H.
  - split.
    + intro abs. apply H0. apply CRmult_lt_compat_r.
      exact c. exact abs.
    + intro abs. apply H1. apply CRmult_lt_compat_r.
      exact c. exact abs.
  - split.
    + intro abs. apply H1. apply CRopp_lt_cancel.
      apply (CRle_lt_trans R _ (CRmult R r1 (CRopp R r))).
      apply CRopp_mult_distr_r.
      apply (CRlt_le_trans R _ (CRmult R r2 (CRopp R r))).
      2: apply CRopp_mult_distr_r.
      apply CRmult_lt_compat_r. 2: exact abs.
      apply (CRplus_lt_reg_r R r). apply (CRle_lt_trans R _ r).
      apply (Radd_0_l (CRisRing R)).
      apply (CRlt_le_trans R _ (CRzero R) _ c).
      apply CRplus_opp_l.
    + intro abs. apply H0. apply CRopp_lt_cancel.
      apply (CRle_lt_trans R _ (CRmult R r2 (CRopp R r))).
      apply CRopp_mult_distr_r.
      apply (CRlt_le_trans R _ (CRmult R r1 (CRopp R r))).
      2: apply CRopp_mult_distr_r.
      apply CRmult_lt_compat_r. 2: exact abs.
      apply (CRplus_lt_reg_r R r). apply (CRle_lt_trans R _ r).
      apply (Radd_0_l (CRisRing R)).
      apply (CRlt_le_trans R _ (CRzero R) _ c).
      apply CRplus_opp_l.
Qed.

Lemma CR_of_Q_proper : forall (R : ConstructiveReals) (q r : Q),
    q == r -> orderEq _ (CRlt R) (CR_of_Q R q) (CR_of_Q R r).
Proof.
  split.
  - intro abs. apply lt_CR_of_Q in abs. rewrite H in abs.
    exact (Qlt_not_le r r abs (Qle_refl r)).
  - intro abs. apply lt_CR_of_Q in abs. rewrite H in abs.
    exact (Qlt_not_le r r abs (Qle_refl r)).
Qed.

Lemma CR_of_Q_zero : forall (R : ConstructiveReals),
    orderEq _ (CRlt R) (CR_of_Q R 0) (CRzero R).
Proof.
  intros. apply CRzero_double.
  apply (CReq_trans R _ (CR_of_Q R (0+0))). apply CR_of_Q_proper.
  reflexivity. apply CR_of_Q_plus.
Qed.

Lemma CR_of_Q_opp : forall (R : ConstructiveReals) (q : Q),
    orderEq _ (CRlt R) (CR_of_Q R (-q)) (CRopp R (CR_of_Q R q)).
Proof.
  intros. apply (CRplus_eq_reg_l R (CR_of_Q R q)).
  apply (CReq_trans R _ (CRzero R)).
  apply (CReq_trans R _ (CR_of_Q R (q-q))).
  apply CReq_sym, CR_of_Q_plus.
  apply (CReq_trans R _ (CR_of_Q R 0)).
  apply CR_of_Q_proper. ring. apply CR_of_Q_zero.
  apply CReq_sym. apply (CRisRing R).
Qed.

Lemma CR_of_Q_le : forall (R : ConstructiveReals) (r q : Q),
    Qle r q
    -> orderLe _ (CRlt R) (CR_of_Q R r) (CR_of_Q R q).
Proof.
  intros. intro abs. apply lt_CR_of_Q in abs.
  exact (Qlt_not_le _ _ abs H).
Qed.

Lemma CR_of_Q_pos : forall (R : ConstructiveReals) (q:Q),
    Qlt 0 q -> CRlt R (CRzero R) (CR_of_Q R q).
Proof.
  intros. apply (CRle_lt_trans R _ (CR_of_Q R 0)).
  apply CR_of_Q_zero. apply CR_of_Q_lt. exact H.
Qed.

Lemma CR_cv_above_rat
  : forall (R : ConstructiveReals) (xn : nat -> Q) (x : CRcarrier R) (q : Q),
    CR_cv R (fun n : nat => CR_of_Q R (xn n)) x
    -> CRlt R (CR_of_Q R q) x
    -> { n : nat | forall p:nat, le n p -> Qlt q (xn p) }.
Proof.
  intros.
  destruct (CR_Q_dense R _ _ H0) as [r [H1 H2]].
  apply lt_CR_of_Q in H1. clear H0.
  destruct (Qarchimedean (/(r-q))) as [p pmaj].
  destruct (H p) as [n nmaj].
  exists n. intros k lenk. specialize (nmaj k lenk) as [H3 _].
  apply (lt_CR_of_Q R), (CRlt_le_trans R _ (CRplus R x (CR_of_Q R (-1#p)))).
  apply (CRlt_trans R _ (CRplus R (CR_of_Q R r) (CR_of_Q R (-1#p)))).
  2: apply CRplus_lt_compat_r, H2.
  apply (CRlt_le_trans R _ (CR_of_Q R (r+(-1#p)))).
  - apply CR_of_Q_lt.
    apply (Qplus_lt_l _ _ (-(-1#p)-q)). field_simplify.
    setoid_replace (-1*(-1#p)) with (1#p). 2: reflexivity.
    apply (Qmult_lt_l _ _ (r-q)) in pmaj.
    rewrite Qmult_inv_r in pmaj. apply Qlt_shift_div_r in pmaj.
    2: reflexivity. setoid_replace (-1*q + r) with (r-q). exact pmaj.
    ring. intro abs. apply Qlt_minus_iff in H1.
    rewrite abs in H1. inversion H1.
    apply Qlt_minus_iff in H1. exact H1.
  - apply CR_of_Q_plus.
  - apply (CRplus_le_reg_r R (CRopp R x)).
    apply (CRle_trans R _ (CR_of_Q R (-1#p))). 2: exact H3. clear H3.
    apply (CRle_trans R _ (CRplus R (CRopp R x) (CRplus R x (CR_of_Q R (-1 # p))))).
    exact (proj1 (Radd_comm (CRisRing R) _ _)).
    apply (CRle_trans R _ (CRplus R (CRplus R (CRopp R x) x) (CR_of_Q R (-1 # p)))).
    exact (proj2 (Radd_assoc (CRisRing R) _ _ _)).
    apply (CRle_trans R _ (CRplus R (CRzero R) (CR_of_Q R (-1 # p)))).
    apply CRplus_le_compat_r. exact (proj2 (CRplus_opp_l R _)).
    exact (proj2 (Radd_0_l (CRisRing R) _)).
Qed.

Lemma CR_cv_below_rat
  : forall (R : ConstructiveReals) (xn : nat -> Q) (x : CRcarrier R) (q : Q),
    CR_cv R (fun n : nat => CR_of_Q R (xn n)) x
    -> CRlt R x (CR_of_Q R q)
    -> { n : nat | forall p:nat, le n p -> Qlt (xn p) q }.
Proof.
  intros.
  destruct (CR_Q_dense R _ _ H0) as [r [H1 H2]].
  apply lt_CR_of_Q in H2. clear H0.
  destruct (Qarchimedean (/(q-r))) as [p pmaj].
  destruct (H p) as [n nmaj].
  exists n. intros k lenk. specialize (nmaj k lenk) as [_ H4].
  apply (lt_CR_of_Q R), (CRle_lt_trans R _ (CRplus R x (CR_of_Q R (1#p)))).
  - apply (CRplus_le_reg_r R (CRopp R x)).
    apply (CRle_trans R _ (CR_of_Q R (1#p))). exact H4. clear H4.
    apply (CRle_trans R _ (CRplus R (CRopp R x) (CRplus R x (CR_of_Q R (1 # p))))).
    2: exact (proj1 (Radd_comm (CRisRing R) _ _)).
    apply (CRle_trans R _ (CRplus R (CRplus R (CRopp R x) x) (CR_of_Q R (1 # p)))).
    2: exact (proj1 (Radd_assoc (CRisRing R) _ _ _)).
    apply (CRle_trans R _ (CRplus R (CRzero R) (CR_of_Q R (1 # p)))).
    exact (proj1 (Radd_0_l (CRisRing R) _)).
    apply CRplus_le_compat_r. exact (proj1 (CRplus_opp_l R _)).
  - apply (CRlt_trans R _ (CRplus R (CR_of_Q R r) (CR_of_Q R (1 # p)))).
    apply CRplus_lt_compat_r. exact H1.
    apply (CRle_lt_trans R _ (CR_of_Q R (r + (1#p)))).
    apply CR_of_Q_plus. apply CR_of_Q_lt.
    apply (Qmult_lt_l _ _ (q-r)) in pmaj.
    rewrite Qmult_inv_r in pmaj. apply Qlt_shift_div_r in pmaj.
    apply (Qplus_lt_l _ _ (-r)). field_simplify.
    setoid_replace (-1*r + q) with (q-r). exact pmaj.
    ring. reflexivity. intro abs. apply Qlt_minus_iff in H2.
    rewrite abs in H2. inversion H2.
    apply Qlt_minus_iff in H2. exact H2.
Qed.

Lemma CR_cv_const : forall (R : ConstructiveReals) (x y : CRcarrier R),
    CR_cv R (fun _ => x) y -> orderEq _ (CRlt R) x y.
Proof.
  intros. destruct (CRisRing R). split.
  - intro abs.
    destruct (CR_Q_dense R x y abs) as [q [H0 H1]].
    destruct (CR_Q_dense R _ _ H1) as [r [H2 H3]].
    apply lt_CR_of_Q in H2.
    destruct (Qarchimedean (/(r-q))) as [p pmaj].
    destruct (H p) as [n nmaj]. specialize (nmaj n (le_refl n)) as [nmaj _].
    apply nmaj. clear nmaj.
    apply (CRlt_trans R _ (CR_of_Q R (q-r))).
    apply (CRlt_le_trans R _ (CRplus R (CR_of_Q R q) (CRopp R (CR_of_Q R r)))).
    + apply CRplus_lt_le_compat. exact H0.
      intro H4. apply CRopp_lt_cancel in H4. exact (CRlt_asym R _ _ H4 H3).
    + apply (CRle_trans R _ (CRplus R (CR_of_Q R q) (CR_of_Q R (-r)))).
      apply CRplus_le_compat_l. exact (proj1 (CR_of_Q_opp R r)).
      exact (proj1 (CR_of_Q_plus R _ _)).
    + apply CR_of_Q_lt.
      apply (Qplus_lt_l _ _ (-(-1#p)+r-q)). field_simplify.
      setoid_replace (-1*(-1#p)) with (1#p). 2: reflexivity.
      apply (Qmult_lt_l _ _ (r-q)) in pmaj.
      rewrite Qmult_inv_r in pmaj. apply Qlt_shift_div_r in pmaj.
      2: reflexivity. setoid_replace (-1*q + r) with (r-q). exact pmaj.
      ring. intro H4. apply Qlt_minus_iff in H2.
      rewrite H4 in H2. inversion H2.
      apply Qlt_minus_iff in H2. exact H2.
  - intro abs.
    destruct (CR_Q_dense R _ _ abs) as [q [H0 H1]].
    destruct (CR_Q_dense R _ _ H0) as [r [H2 H3]].
    apply lt_CR_of_Q in H3.
    destruct (Qarchimedean (/(q-r))) as [p pmaj].
    destruct (H p) as [n nmaj]. specialize (nmaj n (le_refl n)) as [_ nmaj].
    apply nmaj. clear nmaj.
    apply (CRlt_trans R _ (CR_of_Q R (q-r))).
    + apply CR_of_Q_lt.
      apply (Qmult_lt_l _ _ (q-r)) in pmaj.
      rewrite Qmult_inv_r in pmaj. apply Qlt_shift_div_r in pmaj.
      exact pmaj. reflexivity.
      intro H4. apply Qlt_minus_iff in H3.
      rewrite H4 in H3. inversion H3.
      apply Qlt_minus_iff in H3. exact H3.
    + apply (CRle_lt_trans R _ (CRplus R (CR_of_Q R q) (CR_of_Q R (-r)))).
      apply CR_of_Q_plus.
      apply (CRle_lt_trans R _ (CRplus R (CR_of_Q R q) (CRopp R (CR_of_Q R r)))).
      apply CRplus_le_compat_l. exact (proj2 (CR_of_Q_opp R r)).
      apply CRplus_lt_le_compat. exact H1.
      intro H4. apply CRopp_lt_cancel in H4.
      exact (CRlt_asym R _ _ H4 H2).
Qed.