A Coq library for domain theory (http://rwd.rdockins.name/domains/)

root / realcuts.v

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
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
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
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
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
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
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
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
Require Import QArith.
Require Import Setoid.

Require Import basics.
Require Import preord.
Require Import categories.
Require Import sets.
Require Import finsets.
Require Import esets.
Require Import effective.
Require Import directed.
Require Import plotkin.
Require Import joinable.
Require Import approx_rels.
Require Import profinite.
Require Import cusl.

Require Import rational_intervals.
Require Import realdom.

Require Import Qabs.
Require Import Qminmax.



Section cuts.
  Record cut  := Cut
    { cut_upper : eset Qpreord
    ; cut_lower : eset Qpreord
    ; cut_proper : forall u l, u ∈ cut_upper -> l ∈ cut_lower -> l <= u
    ; cut_is_lower : forall (q1 q2:Qpreord), q1 <= q2 -> q2 ∈ cut_lower -> q1 ∈ cut_lower
    ; cut_is_upper : forall (q1 q2:Qpreord), q1 <= q2 -> q1 ∈ cut_upper -> q2 ∈ cut_upper
     (** probably should reformulate the prereals as directed sets of
         intervals where the endpoints may be -∞ or ∞.
       *)
    ; cut_nonextended : (exists x, x ∈ cut_upper) <-> (exists x, x ∈ cut_lower)
    }.

  Definition cut_ord (x y:cut) :=
    cut_upper x ⊆ cut_upper y /\
    cut_lower x ⊆ cut_lower y.

  Program Definition cut_ord_mixin : Preord.mixin_of cut :=
    Preord.Mixin cut cut_ord _ _ .
  Next Obligation.
    intros. red; split; red; auto.
  Qed.
  Next Obligation.
    unfold cut_ord. intuition; repeat intro; eauto.
  Qed.

  Definition cut_preord := Preord.Pack cut cut_ord_mixin.

  Program Definition rints_to_cut
          (X:eset PreRealDom)
          (Hdir:directed true X)
          (Hdown:forall (r r':PreRealDom), r ≤ r' -> r' ∈ X -> r ∈ X )
          : cut :=
    Cut (eimage' _ _ rint_end   X)
        (eimage' _ _ rint_start X)
        _ _ _ _.
  Next Obligation.
    unfold eimage'.
    intros.
    destruct H as [n1 ?].
    destruct H0 as [n2 ?].
    revert H. case_eq (X n1); intros.
    revert H0. case_eq (X n2); intros.
    destruct H1 as [? _]. destruct H2 as [? _].
    red in H1. simpl in H1.
    red in H2. simpl in H2.
    rewrite H2. rewrite H1.
    destruct (Hdir (c::c0::nil)%list).
    exists c. apply cons_elem; auto.
    red; simpl; intros.
    apply cons_elem in H3. destruct H3. rewrite H3.
    exists n1. rewrite H. auto.
    apply cons_elem in H3. destruct H3. rewrite H3.
    exists n2. rewrite H0. auto.
    apply nil_elem in H3. elim H3.
    destruct H3.
    assert (c ≤ x).
    apply H3. apply cons_elem; auto.
    assert (c0 ≤ x).
    apply H3. apply cons_elem; right. apply cons_elem; auto.
    apply Qle_trans with (rint_start x).
    apply rint_ord_test in H6. intuition.
    apply Qle_trans with (rint_end x).
    apply rint_proper.
    apply rint_ord_test in H5. intuition.
    elim H2.
    elim H1.
  Qed.
  Next Obligation.
    repeat intro.
    unfold eimage' in H0.
    destruct H0 as [n ?].
    revert H0. case_eq (X n); intros.
    destruct H1 as [? _]. red in H1. simpl in H1.
    assert (q1 <= rint_end c).
    apply Qle_trans with q2; auto.
    rewrite H1. apply rint_proper.
    set (r := RatInt q1 (rint_end c) H2).
    assert (r ∈ X).
    apply Hdown with c.
    apply rint_ord_test.
    split; simpl; auto.
    apply Qle_trans with q2; auto.
    rewrite H1; auto.
    apply Qle_refl.
    apply Qle_refl.
    exists n. rewrite H0. auto.
    destruct H3 as [n' ?].
    exists n'.
    unfold eimage'.
    destruct (X n').
    assert (q1 == rint_start c0).
    destruct H3.
    assert (rint_start r == rint_start c0).
    apply rint_ord_test in H3.
    apply rint_ord_test in H4.
    apply Qle_antisym; intuition.
    simpl in H5. auto.
    split; red; simpl; auto.
    symmetry; auto.
    auto.
    elim H1.
  Qed.
  Next Obligation.
    repeat intro.
    unfold eimage' in H0.
    destruct H0 as [n ?].
    revert H0. case_eq (X n); intros.
    destruct H1 as [? _]. red in H1. simpl in H1.
    assert (rint_start c <= q2).
    apply Qle_trans with q1; auto.
    rewrite H1. apply rint_proper.
    set (r := RatInt (rint_start c) q2 H2).
    assert (r ∈ X).
    apply Hdown with c.
    apply rint_ord_test.
    split; simpl; auto.
    apply Qle_refl.
    apply Qle_trans with q1; auto.
    rewrite H1; auto.
    apply Qle_refl.
    exists n. rewrite H0. auto.
    destruct H3 as [n' ?].
    exists n'.
    unfold eimage'.
    destruct (X n').
    assert (q2 == rint_end c0).
    destruct H3.
    assert (rint_end r == rint_end c0).
    apply rint_ord_test in H3.
    apply rint_ord_test in H4.
    apply Qle_antisym; intuition.
    simpl in H5. auto.
    split; red; simpl; auto.
    symmetry; auto.
    auto.
    elim H1.
  Qed.
  Next Obligation.
    intros.
    unfold eimage'.
    split; intros [x ?].
    destruct H as [n ?].
    revert H. case_eq (X n); intros.
    exists (rint_start c).
    exists n. rewrite H. auto.
    elim H0.
    destruct H as [n ?].
    revert H. case_eq (X n); intros.
    exists (rint_end c).
    exists n. rewrite H. auto.
    elim H0.
  Qed.

  Lemma rints_to_cut_upper q X Hdir Hdown :
    q ∈ cut_upper (rints_to_cut X Hdir Hdown) <->
    exists r, r ∈ X /\ q == rint_end r.
  Proof.
    simpl; intros. unfold eimage'.
    split; intros.
    destruct H as [n ?].
    revert H. case_eq (X n); intros.
    exists c. split.
    exists n. rewrite H; auto.
    destruct H0; auto.
    elim H0.
    destruct H as [r [??]].
    destruct H as [n ?]. exists n.
    destruct (X n); auto.
    assert (rint_end r == rint_end c).
    destruct H.
    apply rint_ord_test in H.
    apply rint_ord_test in H1.
    apply Qle_antisym; intuition.
    rewrite H1 in H0.
    split; red; simpl; auto.
    symmetry; auto.
  Qed.

  Lemma rints_to_cut_lower q X Hdir Hdown :
    q ∈ cut_lower (rints_to_cut X Hdir Hdown) <->
    exists r, r ∈ X /\ q == rint_start r.
  Proof.
    simpl; intros. unfold eimage'.
    split; intros.
    destruct H as [n ?].
    revert H. case_eq (X n); intros.
    exists c. split.
    exists n. rewrite H; auto.
    destruct H0; auto.
    elim H0.
    destruct H as [r [??]].
    destruct H as [n ?]. exists n.
    destruct (X n); auto.
    assert (rint_start r == rint_start c).
    destruct H.
    apply rint_ord_test in H.
    apply rint_ord_test in H1.
    apply Qle_antisym; intuition.
    rewrite H1 in H0.
    split; red; simpl; auto.
    symmetry; auto.
  Qed.

  Opaque rints_to_cut.


  Parameter A:∂PLT.

  Program Definition hom_to_cut (f:A → PreRealDom) : PLT.ord A → cut_preord :=
    Preord.Hom (PLT.ord A) cut_preord 
               (fun a => rints_to_cut (erel_image _ _ (PLT.dec A) (PLT.hom_rel f) a) 
                                    (PLT.hom_directed _ _ _ f a)
                                     _)
               _.
  Next Obligation.
    intros.
    apply erel_image_elem in H0.
    apply erel_image_elem.
    revert H0. apply PLT.hom_order; auto.
  Qed.
  Next Obligation.
    simpl; intros.
    assert (erel_image _ _ (PLT.dec A) (PLT.hom_rel f) a ⊆
            erel_image _ _ (PLT.dec A) (PLT.hom_rel f) b).
    red; simpl; intros.
    apply erel_image_elem in H0.
    apply erel_image_elem.
    revert H0.
    apply PLT.hom_order; auto.

    split; red; intros.
    apply rints_to_cut_upper in H1.
    apply rints_to_cut_upper.
    destruct H1 as [r [??]]. exists r; split; auto.
    apply rints_to_cut_lower in H1.
    apply rints_to_cut_lower.
    destruct H1 as [r [??]]. exists r; split; auto.
  Qed.

  Program Definition cut_to_hom_rel (f:PLT.ord A → cut_preord) : erel A PreRealDom :=
    @esubset (prod_preord A PreRealDom)
            (fun ar => rint_end (snd ar) ∈ cut_upper (f (fst ar)) /\
                       rint_start (snd ar) ∈ cut_lower (f (fst ar)))
            _
            (eff_enum _ (effective_prod (PLT.effective A) (PLT.effective PreRealDom))).
  Next Obligation.
    intros.
    apply semidec_conj.
    apply semidec_in.
    constructor. apply Qeq_dec.
    apply semidec_in.
    constructor. apply Qeq_dec.
  Qed.

  Lemma cut_to_hom_rel_elem (f:PLT.ord A → cut_preord) a r :
    (a,r) ∈ cut_to_hom_rel f <-> 
       (rint_end r ∈ cut_upper (f a) /\ rint_start r ∈ cut_lower (f a)).
  Proof.
    unfold cut_to_hom_rel.
    rewrite esubset_elem. intuition.
    apply eprod_elem; split; apply eff_complete.
    simpl; intros.
    destruct H as [[??][??]].
    intuition.
    cut (rint_end (snd b) ∈ cut_upper (f (fst a0))).
    destruct (Preord.axiom _ _ f (fst a0) (fst b)); auto.
    revert H4. apply cut_is_upper.
    apply rint_ord_test in H3. intuition.
    cut (rint_start (snd b) ∈ cut_lower (f (fst a0))).
    destruct (Preord.axiom _ _ f (fst a0) (fst b)); auto.
    revert H5. apply cut_is_lower.
    apply rint_ord_test in H3. intuition.
  Qed.

  Program Definition cut_to_hom (f:PLT.ord A → cut_preord) : A → PreRealDom :=
    PLT.Hom true A PreRealDom (cut_to_hom_rel f) _ _. 
  Next Obligation.
    simpl; intros.
    apply cut_to_hom_rel_elem in H1.
    apply cut_to_hom_rel_elem.
    apply rint_ord_test in H0.
    intuition.
    cut (rint_end y' ∈ cut_upper (f x)).
    destruct (Preord.axiom _ _ f x x'); auto.
    revert H0.
    apply cut_is_upper; auto.
    cut (rint_start y' ∈ cut_lower(f x)).
    destruct (Preord.axiom _ _ f x x'); auto.
    revert H4.
    apply cut_is_lower; auto.
  Qed.
  Next Obligation.
    intros f a. apply prove_directed; auto.
    intros.
    apply erel_image_elem in H.
    apply erel_image_elem in H0.
    apply cut_to_hom_rel_elem in H.
    apply cut_to_hom_rel_elem in H0.
    destruct H. destruct H0.
    assert (Qmax (rint_start x) (rint_start y) <= Qmin (rint_end x) (rint_end y)).
    apply Q.max_case.
    intros. rewrite <- H3; auto.
    apply Q.min_case.
    intros. rewrite <- H3; auto.
    eapply cut_proper; eauto.
    eapply cut_proper; eauto.
    apply Q.min_case.
    intros. rewrite <- H3; auto.
    eapply cut_proper; eauto.
    eapply cut_proper; eauto.
    exists (RatInt _ _ H3).
    split.
    apply rint_ord_test. simpl.
    split. apply Q.le_max_l. apply Q.le_min_l.
    split.
    apply rint_ord_test. simpl.
    split. apply Q.le_max_r. apply Q.le_min_r.
    apply erel_image_elem.
    apply cut_to_hom_rel_elem.
    simpl. split.
    apply Q.min_case; simpl; auto.
    intros p q ?. apply member_eq.
    split; red; simpl; auto. symmetry; auto.
    apply Q.max_case; simpl; auto.
    intros p q ?. apply member_eq.
    split; red; simpl; auto. symmetry; auto.
  Qed.


  Lemma cut_roundtrip1 f :
    cut_to_hom (hom_to_cut f) ≈ f.
  Proof.
    split; hnf; simpl; intros [a r] H.
    apply cut_to_hom_rel_elem in H. destruct H.
    simpl in *.
    rewrite rints_to_cut_upper in H.
    rewrite rints_to_cut_lower in H0.
    destruct H as [r1 [??]].
    destruct H0 as [r2 [??]].
    apply erel_image_elem in H.
    apply erel_image_elem in H0.
    destruct (plt_hom_directed2 _ _ _ f a r1 r2) as [r' [?[??]]]; auto.
    apply PLT.hom_order with a r'; auto.
    apply rint_ord_test.
    apply rint_ord_test in H4.
    apply rint_ord_test in H5.
    split.
    rewrite H2; intuition.
    rewrite H1; intuition.

    apply cut_to_hom_rel_elem.
    split; simpl.
    rewrite rints_to_cut_upper.
    exists r. split; auto.
    apply erel_image_elem. auto. reflexivity.
    rewrite rints_to_cut_lower.
    exists r. split; auto.
    apply erel_image_elem. auto. reflexivity.
  Qed.

  Lemma cut_roundtrip2 f :
    hom_to_cut (cut_to_hom f) ≈ f.
  Proof.
    intro a. split; split; simpl; hnf; intros.
    apply rints_to_cut_upper in H.
    destruct H as [r [??]].
    apply erel_image_elem in H. 
    apply cut_to_hom_rel_elem in H. destruct H.
    revert H. apply member_eq.
    split; red; simpl; auto. symmetry; auto.
    apply rints_to_cut_lower in H.
    destruct H as [r [??]].
    apply erel_image_elem in H. 
    apply cut_to_hom_rel_elem in H. destruct H.
    revert H1. apply member_eq.
    split; red; simpl; auto. symmetry; auto.
    
    apply rints_to_cut_upper.
    destruct (cut_nonextended (f a)) as [? _].
    destruct H0 as [z ?]; eauto.
    assert (z <= a0).
    eapply cut_proper; eauto.
    exists (RatInt z a0 H1).
    split; simpl; auto.
    apply erel_image_elem.
    apply cut_to_hom_rel_elem.
    simpl. split; auto. reflexivity.
    
    apply rints_to_cut_lower.
    destruct (cut_nonextended (f a)) as [_ ?].
    destruct H0 as [z ?]; eauto.
    assert (a0 <= z).
    eapply cut_proper; eauto.
    exists (RatInt a0 z H1).
    split; simpl; auto.
    apply erel_image_elem.
    apply cut_to_hom_rel_elem.
    simpl. split; auto. reflexivity.
  Qed.

  Definition cut_canonical (x:cut) :=
    (forall u, u ∈ cut_upper x -> exists u', u' ∈ cut_upper x /\ u' < u) /\ 
    (forall l, l ∈ cut_lower x -> exists l', l' ∈ cut_lower x /\ l < l').

  Definition located (x:cut) :=
    forall (ε:Q), ε > 0 ->
      exists u l,
        u ∈ cut_upper x /\
        l ∈ cut_lower x /\
        u - l <= ε.

  Lemma canonical_to_hom (f:PLT.ord A → cut_preord) :
    (forall a, cut_canonical (f a)) ->
    canonical A (cut_to_hom f).
  Proof.
    repeat intro.
    destruct (H a).
    simpl in H0.
    apply cut_to_hom_rel_elem in H0. destruct H0.
    destruct (H1 (rint_end x)) as [u' [??]]; auto.
    destruct (H2 (rint_start x)) as [l' [??]]; auto.
    assert (l' <= u').
    apply cut_proper with (f a); auto.
    exists (RatInt l' u' H8).
    split; simpl.
    apply cut_to_hom_rel_elem; split; simpl; auto.
    red; split; simpl; auto.
  Qed.

  Lemma canonical_to_cut (f:A → PreRealDom) :
    canonical A f ->
    forall a, cut_canonical (hom_to_cut f a).
  Proof.
    repeat intro.
    split; simpl; intros.
    apply rints_to_cut_upper in H0.
    destruct H0 as [r [??]].
    apply erel_image_elem in H0.
    destruct (H a r) as [r' [??]]; auto.
    exists (rint_end r').
    split.
    apply rints_to_cut_upper.
    exists r'. split.
    apply erel_image_elem. auto.
    reflexivity.
    rewrite H1.
    destruct H3; auto.
    apply rints_to_cut_lower in H0.
    destruct H0 as [r [??]].
    apply erel_image_elem in H0.
    destruct (H a r) as [r' [??]]; auto.
    exists (rint_start r').
    split.
    apply rints_to_cut_lower.
    exists r'. split.
    apply erel_image_elem. auto.
    reflexivity.
    rewrite H1.
    destruct H3; auto.
  Qed.

  Lemma located_converges (f:PLT.ord A → cut_preord) :
    (forall a, located (f a)) ->
   realdom_converges A (cut_to_hom f).
  Proof.
    repeat intro.
    destruct (H a ε) as [u [l [?[??]]]]; auto.
    assert (l <= u).
    apply cut_proper with (f a); auto.
    exists (RatInt l u H4). split; simpl; auto.
    apply cut_to_hom_rel_elem. split; simpl; auto.
  Qed.

  Lemma converges_located (f:A → PreRealDom) :
    realdom_converges A f ->
    forall a, located (hom_to_cut f a).
  Proof.
    repeat intro.
    destruct (H a ε) as [r [??]]; auto.
    exists (rint_end r), (rint_start r); split; simpl; auto.
    apply rints_to_cut_upper.
    exists r; split; auto.
    apply erel_image_elem. auto. reflexivity.
    split.
    apply rints_to_cut_lower.
    exists r; split; auto.
    apply erel_image_elem. auto. reflexivity.
    auto.
  Qed.

  Lemma hom_to_cut_mono (f g:A → PreRealDom) :
    f ≤ g -> hom_to_cut f ≤ hom_to_cut g.
  Proof.
    repeat intro.
    split; red; simpl; intros.
    apply rints_to_cut_upper in H0.
    apply rints_to_cut_upper.
    destruct H0 as [r [??]].
    exists r. split; auto.
    apply erel_image_elem in H0.
    apply erel_image_elem.
    apply H; auto.
    apply rints_to_cut_lower in H0.
    apply rints_to_cut_lower.
    destruct H0 as [r [??]].
    exists r. split; auto.
    apply erel_image_elem in H0.
    apply erel_image_elem.
    apply H; auto.
  Qed.

  Lemma cut_to_hom_mono (f g:PLT.ord A → cut_preord) :
    f ≤ g -> cut_to_hom f ≤ cut_to_hom g.
  Proof.
    repeat intro; simpl in *.
    destruct a as [a r].
    apply cut_to_hom_rel_elem in H0.
    apply cut_to_hom_rel_elem.
    destruct H0. split.
    apply (H a); auto.
    apply (H a); auto.
  Qed.

End cuts.

Canonical Structure cut_preord.

Definition cut_lt (x y:cut) :=
  exists u l,
    u ∈ cut_upper x /\
    l ∈ cut_lower y /\
    u < l.

Module interval_limit_cuts.

Section limit.
  Variable seq_upper : N -> cut.
  Variable seq_lower : N -> cut.

  Hypothesis seq_proper : forall n, cut_lt (seq_lower n) (seq_upper n).
  Hypothesis seq_upper_inside : forall n m, (n < m)%N -> cut_lt (seq_upper m) (seq_upper n).
  Hypothesis seq_lower_inside : forall n m, (n < m)%N -> cut_lt (seq_lower n) (seq_lower m).

  Definition limit_upper : eset Qpreord :=
    union ( (fun n => Some (cut_upper (seq_upper n))) : eset (eset Qpreord)).

  Definition limit_lower : eset Qpreord :=
    union ( (fun n => Some (cut_lower (seq_lower n)))  : eset (eset Qpreord)).

  Program Definition interval_limit : cut :=
    Cut limit_upper limit_lower _ _ _ _.
  Next Obligation.
    unfold limit_upper, limit_lower. intros. 
    apply union_axiom in H.
    apply union_axiom in H0.
    destruct H as [XU [??]].
    destruct H0 as [XL [??]].
    destruct H as [n1 ?].
    destruct H0 as [n2 ?].
    set (o := (1 + N.max n1 n2)%N).
    destruct (seq_lower_inside n2 (1 + N.max n1 n2)) as [a [b [?[??]]]].
    zify. omega.
    apply Qle_trans with a.
    apply cut_proper with (seq_lower n2); auto.
    rewrite <- H0; auto.
    apply Qle_trans with b.
    intuition.
    destruct (seq_proper (1 + N.max n1 n2)) as [c [d [?[??]]]].
    apply Qle_trans with c.
    apply cut_proper with (seq_lower (1 + N.max n1 n2)); auto.
    apply Qle_trans with d.
    intuition.
    destruct (seq_upper_inside n1 (1 + N.max n1 n2)) as [e [f [?[??]]]].
    zify. omega.
    apply Qle_trans with e.
    apply cut_proper with (seq_upper (1 + N.max n1 n2)); auto.
    apply Qle_trans with f.
    intuition.
    apply cut_proper with (seq_upper n1); auto.
    rewrite <- H. auto.
  Qed.
  Next Obligation.
    unfold limit_lower; intros.
    apply union_axiom in H0.
    destruct H0 as [X [??]].
    destruct H0 as [n ?].
    apply union_axiom.
    exists X. split; auto.
    exists n. auto.
    rewrite H0. rewrite H0 in H1.
    revert H1. apply cut_is_lower. auto.
  Qed.
  Next Obligation.
    unfold limit_upper; intros.
    apply union_axiom in H0.
    destruct H0 as [X [??]].
    destruct H0 as [n ?].
    apply union_axiom.
    exists X. split; auto.
    exists n. auto.
    rewrite H0. rewrite H0 in H1.
    revert H1. apply cut_is_upper. auto.
  Qed.
  Next Obligation.
    unfold limit_upper, limit_lower.
    destruct (seq_proper 0) as [u [l [?[??]]]].
    split; intros.
    destruct (cut_nonextended (seq_lower 0)).
    destruct H3 as [l' ?].
    exists u. auto.
    exists l'.
    apply union_axiom.
    exists (cut_lower (seq_lower 0)).
    split; auto.
    exists 0%N. auto.
    
    destruct (cut_nonextended (seq_upper 0)).
    destruct H4 as [u' ?].
    exists l. auto.
    exists u'.
    apply union_axiom.
    exists (cut_upper (seq_upper 0)).
    split; auto.
    exists 0%N. auto.
  Qed.

  Lemma interval_limit_inside_upper : forall n,
    cut_lt interval_limit (seq_upper n).                                  
  Proof.
    repeat intro. hnf.
    destruct (seq_upper_inside n (n+1)) as [u' [l' [?[??]]]].
    zify. omega.
    exists u'. exists l'. intuition.
    unfold interval_limit. simpl.
    unfold limit_upper.
    apply union_axiom.
    exists (cut_upper (seq_upper (n+1))).
    split. exists (n+1)%N. auto.
    auto.
  Qed.

  Lemma interval_limit_inside_lower : forall n,
    cut_lt (seq_lower n) interval_limit.
  Proof.
    repeat intro. hnf.
    destruct (seq_lower_inside n (n+1)) as [u' [l' [?[??]]]].
    zify. omega.
    exists u'. exists l'. intuition.
    unfold interval_limit. simpl.
    unfold limit_upper.
    apply union_axiom.
    exists (cut_lower (seq_lower (n+1))).
    split. exists (n+1)%N. auto.
    auto.
  Qed.

  Lemma interval_limit_least_defined lim :
    (forall n, cut_lt lim (seq_upper n)) ->
    (forall n, cut_lt (seq_lower n) lim) ->
    interval_limit ≤ lim.
  Proof.
    repeat intro. split; hnf; simpl; intros.

    unfold limit_upper in H1.
    apply union_axiom in H1.
    destruct H1 as [X [??]].
    destruct H1 as [n ?].
    destruct (H n) as [u [l [?[??]]]].
    rewrite H1 in H2.
    apply cut_is_upper with u; auto.
    apply Qle_trans with l; intuition.
    apply cut_proper with (seq_upper n); auto.
    
    unfold limit_lower in H1.
    apply union_axiom in H1.
    destruct H1 as [X [??]].
    destruct H1 as [n ?].
    destruct (H0 n) as [u [l [?[??]]]].
    rewrite H1 in H2.
    apply cut_is_lower with l; auto.
    apply Qle_trans with u; intuition.
    apply cut_proper with (seq_lower n); auto.
  Qed.

  Hypothesis seq_converges : forall ε, 0 < ε ->
     exists n a b,
       a ∈ cut_upper (seq_upper n) /\
       b ∈ cut_lower (seq_lower n) /\
       a - b <= ε.
       
  Lemma interval_limit_located : located interval_limit.
  Proof.
    red; intros.
    destruct (seq_converges ε H) as [n [a [b [?[??]]]]].
    exists a. exists b. intuition.
    simpl. unfold limit_upper.
    apply union_axiom. exists (cut_upper (seq_upper n)).
    split; auto. exists n. auto.
    apply union_axiom. exists (cut_lower (seq_lower n)).
    split; auto. exists n. auto.
  Qed.
End limit.
End interval_limit_cuts.



Definition pow2 (n:N) : positive :=
 match n with
 | N0     => 1%positive
 | Npos p => shift_pos p 1
 end.

Lemma pow2_commute n1 n2 :
  (pow2 n1 * pow2 n2 = pow2 (n1+n2))%positive.
Proof.
  unfold pow2.
  destruct n1; simpl; auto.
  destruct n2; simpl; auto.
  apply Pos.mul_1_r.
  unfold shift_pos.
  rewrite Pos.iter_add.
  induction p using Pos.peano_ind; simpl; auto.
  rewrite Pos.iter_succ. simpl.
  rewrite IHp.
  rewrite Pos.iter_succ. auto.
Qed.

Lemma pow2_div_eq (a b:N) :
  (a <= b)%N -> (pow2 (b - a) * pow2 a = pow2 b)%positive.
Proof.
  intros.
  rewrite pow2_commute.
  replace (b - a + a)%N with b; auto.
  assert (0 <= a)%N.
  hnf. simpl. destruct a; discriminate.
  symmetry. apply N.sub_add; auto.
Qed. 

Definition oneOver2n (n:N) : Q := (1 # pow2 n)%Q.

Lemma oneOver2n_pos n : 0 < oneOver2n n.
Proof.
  unfold oneOver2n. intuition.
Qed.

Lemma oneOver2n_commute n1 n2 :
  (oneOver2n n1 * oneOver2n n2 == oneOver2n (n1+n2))%Q.
Proof.
  unfold oneOver2n.
  red. simpl. symmetry.
  f_equal. apply pow2_commute.
Qed.

Lemma oneOver2n_div_eq a b  :
  (a <= b)%N -> (oneOver2n (b - a) * oneOver2n a == oneOver2n b)%Q.
Proof.
  intro.
  unfold oneOver2n.
  red. simpl. symmetry.
  f_equal. apply pow2_div_eq. auto.
Qed.

Lemma pow2_ge1 (a:N) :
  (1 <= pow2 a)%positive.
Proof.
  induction a using N.peano_ind.
  simpl. reflexivity.
  replace (N.succ a) with (1+a)%N.
  2: zify; omega.
  rewrite <- pow2_commute.
  simpl pow2 at 1. unfold shift_pos. simpl Pos.iter.
  zify. omega.
Qed.

Lemma pow2_mono (a b:N) :
  (a <= b)%N -> (pow2 a <= pow2 b)%positive.
Proof.
  revert b. induction a using N.peano_ind.
  simpl; intros.
  apply pow2_ge1.
  intro b. induction b using N.peano_ind.
  intros.
  elimtype False. zify. omega.
  intros.
  replace (N.succ a) with (1+a)%N.
  replace (N.succ b) with (1+b)%N.
  2: zify; omega.
  2: zify; omega.
  rewrite <- pow2_commute.
  rewrite <- pow2_commute.
  cut (pow2 a <= pow2 b)%positive.
  simpl. zify. omega.
  apply IHa. zify. omega.
Qed.

Lemma oneOver2n_subsumes (a b c:N) :
    (a < b)%N -> (a < c)%N -> 
       oneOver2n b + oneOver2n c <= oneOver2n a.
Proof.
  intros. unfold oneOver2n.
  red. simpl.
  cut ((pow2 c + pow2 b) * pow2 a <= (pow2 b * pow2 c))%positive. auto.
  rewrite pow2_commute.
  rewrite Pos.mul_add_distr_r.
  rewrite pow2_commute.
  rewrite pow2_commute.
  replace (b + c)%N with (1 + (b+c-1))%N by (zify; omega).
  rewrite <- (pow2_commute 1).
  simpl pow2 at 3. unfold shift_pos. simpl Pos.iter.
  replace 2%positive with (1 + 1)%positive.
  rewrite Pos.mul_add_distr_r.
  simpl.
  apply Pos.add_le_mono.
  apply pow2_mono.
  zify. omega.
  apply pow2_mono.
  zify. omega.
  zify. omega.
Qed.  

Fixpoint pos_log2 (p:positive) : N :=
  match p with
    | xH => 1%N
    | xO p' => N.succ (pos_log2 p')
    | xI p' => N.succ (pos_log2 p')
  end.

Lemma pow2_succ (n:N) : (pow2 (N.succ n) = (pow2 n)~0)%positive.
Proof.
  destruct n.
  compute. auto.
  simpl.
  unfold shift_pos. simpl.
  rewrite Pos.iter_succ.
  auto.
Qed.    

Lemma pow2_pos_log2 (p:positive) : (p < pow2 (pos_log2 p))%positive.
Proof.
  red. unfold Pos.compare.
  generalize Eq.
  induction p.
  simpl; intros.
  rewrite pow2_succ.
  simpl. apply IHp.
  simpl; intros.
  rewrite pow2_succ.
  simpl. apply IHp.
  simpl. auto.
Qed.    

Lemma oneOver2n_small : forall ε:Q, 0 < ε -> exists n, oneOver2n n <= ε.
Proof.
  intros.
  exists (pos_log2 (Qden ε)).
  destruct ε.
  red in H. simpl in H.
  ring_simplify in H.
  simpl.
  red; simpl.
  apply Z.le_trans with (1 * 'Qden)%Z.
  simpl. reflexivity.
  apply Zmult_le_compat.
  omega.
  cut ( Qden <  pow2 (pos_log2 Qden))%positive.
  zify. omega.
  apply pow2_pos_log2.
  omega.
  compute. discriminate.
Qed.


Module cauchy_limit_cuts.

Program Definition widen_cut (n:N) (x:cut) : cut :=
  Cut (image (Preord.Hom _ _ (Qplus (oneOver2n n)) _) (cut_upper x))
      (image (Preord.Hom _ _ (Qplus (- oneOver2n n)) _) (cut_lower x))
      _ _ _ _.
Next Obligation.
  intros. red in H. simpl in H.
  red. simpl. rewrite H. reflexivity.
Qed.
Next Obligation.
  intros. red in H. simpl in H.
  red. simpl. rewrite H. reflexivity.
Qed.
Next Obligation.
  simpl; intros.
  apply image_axiom2 in H.
  apply image_axiom2 in H0.
  destruct H as [q1 [??]].
  destruct H0 as [q2 [??]].
  simpl in *.
  destruct H1. red in H1. simpl in H1.
  destruct H2. red in H2. simpl in H2.
  rewrite H2. rewrite H1.
  apply Qle_trans with (0 + q2)%Q.
  apply Qplus_le_compat.
  rewrite <- (Qplus_le_l _ _ (oneOver2n n)).
  ring_simplify.
  apply Qlt_le_weak.
  apply oneOver2n_pos.
  apply Qle_refl.
  apply Qle_trans with (0 + q1)%Q.
  apply Qplus_le_compat; auto.
  apply Qle_refl.
  apply cut_proper with x; auto.
  apply Qplus_le_compat.
  apply Qlt_le_weak.
  apply oneOver2n_pos.
  apply Qle_refl.
Qed.
Next Obligation.
  intros.
  apply image_axiom2 in H0.
  destruct H0 as [y [??]]. simpl in *.
  apply image_axiom1'.
  exists (q1 + oneOver2n n)%Q.
  split. simpl.
  split; red; simpl; ring.
  eapply cut_is_lower; eauto.
  destruct H1. red in H1. simpl in H1.
  rewrite <- (Qplus_le_l _ _ (-oneOver2n n)).
  ring_simplify.
  apply Qle_trans with q2; auto.
  rewrite H1.
  ring_simplify. apply Qle_refl.
Qed.
Next Obligation.
  intros.
  apply image_axiom2 in H0.
  destruct H0 as [y [??]]. simpl in *.
  apply image_axiom1'.
  exists (q2 - oneOver2n n)%Q.
  split. simpl.
  split; red; simpl; ring.
  eapply cut_is_upper; eauto.
  destruct H1. red in H1. simpl in H1.
  rewrite <- (Qplus_le_l _ _ (oneOver2n n)).
  ring_simplify.
  apply Qle_trans with q1; auto.
  rewrite H1.
  ring_simplify. apply Qle_refl.
Qed.
Next Obligation.
  intros. split; intros [q ?].
  apply image_axiom2 in H.
  destruct H as [y [??]]; simpl in *.
  destruct (cut_nonextended x).
  destruct H1 as [z ?]; eauto.
  exists (- oneOver2n n + z)%Q.
  apply image_axiom1'. exists z. split; auto.
  apply image_axiom2 in H.
  destruct H as [y [??]]; simpl in *.
  destruct (cut_nonextended x).
  destruct H2 as [z ?]; eauto.
  exists (oneOver2n n + z)%Q.
  apply image_axiom1'. exists z. split; auto.
Qed.

Definition near ε (x y:cut) :=
  (exists u l, u ∈ cut_upper x /\ l ∈ cut_lower y /\ u - l <= ε) /\
  (exists u l, u ∈ cut_upper y /\ l ∈ cut_lower x /\ u - l <= ε).

Definition is_limit (seq:N -> cut) (c:cut) :=
  forall ε:Q, ε > 0 ->
    exists m:N, forall (n:N), (m <= n)%N -> near ε (seq n) c.

Lemma is_limit_located seq lim : is_limit seq lim -> located lim.
Proof.
  red; intros.
  destruct (H (ε/(2#1))); auto.
  apply Qlt_shift_div_l.
  compute. auto.
  ring_simplify. auto.
  destruct (H1 x). reflexivity.
  destruct H2 as [u1 [l1 [?[??]]]].
  destruct H3 as [u2 [l2 [?[??]]]].
  exists u2. exists l1.
  split; auto. split; auto.
  rewrite <- (Qplus_le_l _ _ l1).
  rewrite <- (Qplus_le_l _ _ (-l2)).
  ring_simplify.
  apply Qle_trans with (ε/(2#1)).
  ring_simplify in H7; auto.
  rewrite <- (Qplus_le_l _ _ u1).
  rewrite <- (Qplus_le_l _ _ (-l1)).
  ring_simplify.
  apply Qle_trans with (ε/(2#1) + ε/(2#1) )%Q.
  rewrite <- Qplus_assoc.
  apply Qplus_le_compat. apply Qle_refl.
  ring_simplify in H5. auto.
  rewrite <- (Qplus_le_l _ _ l2).
  ring_simplify.
  field_simplify.
  cut (ε + l2 <= ε + u1).
  intros. field_simplify in H8. auto.
  apply Qplus_le_compat.
  apply Qle_refl.
  eapply cut_proper; eauto.
Qed.

Definition cauchy_sequence (seq:N -> cut) :=
  forall n m o,
     (o <= m)%N -> (o <= n)%N ->
        exists u, u ∈ cut_upper (seq m) /\
        exists l, l ∈ cut_lower (seq n) /\
        u - l < oneOver2n o.

Section cut_cauchy_limit.
  Variable seq : N -> cut.
  Hypothesis seq_cauchy : cauchy_sequence seq.

  Program Definition limit_uppers (n:N) : eset Qpreord :=
    image (Preord.Hom _ _ (Qplus (oneOver2n n)) _) (cut_upper (seq n)).
  Next Obligation.
    intros. red; simpl.
    red in H. simpl in H. rewrite H. reflexivity.
  Qed.

  Program Definition limit_lowers (n:N) : eset Qpreord :=
    image (Preord.Hom _ _ (Qplus (-oneOver2n n)) _) (cut_lower (seq n)).
  Next Obligation.
    intros. red; simpl.
    red in H. simpl in H. rewrite H. reflexivity.
  Qed.
 
  Definition limit_upper : eset Qpreord :=
    union ( (fun n => Some (limit_uppers n)) : eset (eset Qpreord)).

  Definition limit_lower : eset Qpreord :=
    union ( (fun n => Some (limit_lowers n)) : eset (eset Qpreord)).

  Program Definition limit : cut :=
    Cut limit_upper limit_lower _ _ _ _.
  Next Obligation.
    simpl; intros.
    apply union_axiom in H.
    apply union_axiom in H0.
    destruct H as [R [??]].
    destruct H0 as [S [??]].
    destruct H as [n ?].
    destruct H0 as [m ?].
    rewrite H in H1.
    rewrite H0 in H2. clear H H0.
    unfold limit_uppers in H1.
    apply image_axiom2 in H1.
    destruct H1 as [y [??]].
    simpl in *.
    destruct H0 as [? _]. red in H0; simpl in H0.
    rewrite H0.
    unfold limit_lowers in H2.
    apply image_axiom2 in H2.
    destruct H2 as [y' [??]].
    simpl in *.
    destruct H2 as [? _]. red in H2. simpl in H2. rewrite H2.
    destruct (seq_cauchy n m (N.min n m)) as [u' [? [l' [??]]]].
    apply N.le_min_r.
    apply N.le_min_l.
    assert (y' <= u'). eapply cut_proper; eauto.
    assert (l' <= y). eapply cut_proper; eauto.
    rewrite <- (Qplus_le_l _ _ (oneOver2n m)).
    ring_simplify. 
    apply Qle_trans with u'; auto.
    rewrite <- (Qplus_le_l _ _ (-l')).
    apply Qle_trans with (oneOver2n (N.min n m)).
    ring_simplify in H5. ring_simplify. intuition.
    rewrite <- (Qplus_le_l _ _ (l')). ring_simplify.
    apply Qplus_le_compat; auto.
    apply N.min_case.
    rewrite <- (Qplus_le_l _ _ (-oneOver2n n)). ring_simplify.
    apply Qlt_le_weak. apply oneOver2n_pos.
    rewrite <- (Qplus_le_l _ _ (-oneOver2n m)). ring_simplify.
    apply Qlt_le_weak. apply oneOver2n_pos.
  Qed.    
  Next Obligation.
    unfold limit_lower.
    simpl; intros.
    apply union_axiom in H0.
    apply union_axiom.
    destruct H0 as [X [??]].
    exists X. split; auto.
    destruct H0 as [n ?].
    rewrite H0. rewrite H0 in H1.
    unfold limit_lowers in H1.
    unfold limit_lowers.
    apply image_axiom2 in H1.
    destruct H1 as [q [??]]. simpl in *.
    apply image_axiom1'. simpl.
    exists (q1 + oneOver2n n)%Q.
    split.
    split; red; simpl; ring.
    apply cut_is_lower with q; auto.
    apply Qle_trans with (q2 + oneOver2n n)%Q.
    apply Qplus_le_compat; auto. apply Qle_refl.
    destruct H2. red in H2; simpl in H2. rewrite H2.
    ring_simplify. apply Qle_refl.
  Qed.
  Next Obligation.
    unfold limit_upper.
    simpl; intros.
    apply union_axiom in H0.
    apply union_axiom.
    destruct H0 as [X [??]].
    exists X. split; auto.
    destruct H0 as [n ?].
    rewrite H0. rewrite H0 in H1.
    unfold limit_uppers in H1.
    unfold limit_uppers.
    apply image_axiom2 in H1.
    destruct H1 as [q [??]]. simpl in *.
    apply image_axiom1'. simpl.
    exists (q2 - oneOver2n n)%Q.
    split.
    split; red; simpl; ring.
    apply cut_is_upper with q; auto.
    apply Qle_trans with (q1 - oneOver2n n)%Q.
    destruct H2. red in H2; simpl in H2. rewrite H2.
    ring_simplify. apply Qle_refl.
    apply Qplus_le_compat; auto. apply Qle_refl.
  Qed.
  Next Obligation.
    unfold limit_upper, limit_lower.
    destruct (seq_cauchy 0 0 0)%N as [u [? [l [??]]]]. 
    reflexivity.
    reflexivity.
    split; intros.
    exists (-oneOver2n 0 + l)%Q.
    apply union_axiom.
    exists (limit_lowers 0).
    split. exists 0%N. auto.
    unfold limit_lowers.
    apply image_axiom1'.
    simpl.
    exists l. split; auto.
    exists (oneOver2n 0 + u)%Q.
    apply union_axiom.
    exists (limit_uppers 0).
    split. exists 0%N. auto.
    unfold limit_lowers.
    apply image_axiom1'.
    simpl.
    exists u. split; auto.
  Qed.    

  Lemma limit_correct : is_limit seq limit.
  Proof.
    red; intros.
    destruct (oneOver2n_small ε) as [m ?]; auto.
    exists (m+1)%N. intros.
    split.
    destruct (seq_cauchy (m+1) n (m+1))%N as [u [? [l [??]]]]; auto.
    reflexivity.
    exists u. exists (-oneOver2n (m+1) + l)%Q. split; auto. split.
    simpl.
    unfold limit_lower.
    apply union_axiom.
    exists (limit_lowers (m+1)).
    split. exists (m+1)%N. auto.
    unfold limit_lowers.
    apply image_axiom1'; simpl.
    exists l. split; auto.
    rewrite <- (Qplus_le_l _ _ (-oneOver2n (m+1))).
    ring_simplify.
    apply Qle_trans with (oneOver2n (m+1)).
    ring_simplify in H4. intuition.
    rewrite <- (Qplus_le_l _ _ (oneOver2n (m+1))).
    ring_simplify.
    rewrite <- oneOver2n_commute.
    unfold oneOver2n at 2. simpl.
    unfold shift_pos. simpl.
    field_simplify.
    field_simplify in H0. auto.
            
    destruct (seq_cauchy n (m+1) (m+1))%N as [u [? [l [??]]]]; auto.
    reflexivity.
    exists (oneOver2n (m+1) + u)%Q. exists l. split; auto. 
    simpl.
    unfold limit_upper.
    apply union_axiom.
    exists (limit_uppers (m+1)).
    split. exists (m+1)%N. auto.
    unfold limit_uppers.
    apply image_axiom1'; simpl.
    exists u. split; auto.
    split; auto.
    rewrite <- (Qplus_le_l _ _ (-oneOver2n (m+1))).
    ring_simplify.
    apply Qle_trans with (oneOver2n (m+1)).
    ring_simplify in H4. intuition.
    rewrite <- (Qplus_le_l _ _ (oneOver2n (m+1))).
    ring_simplify.
    rewrite <- oneOver2n_commute.
    unfold oneOver2n at 2. simpl.
    unfold shift_pos. simpl.
    field_simplify.
    field_simplify in H0. auto.
  Qed.

  Lemma located_limit : located limit.
  Proof.
    eapply is_limit_located. apply limit_correct.
  Qed.
End cut_cauchy_limit.

End cauchy_limit_cuts.



Program Definition shift_cut (q:Q) (c:cut) :=
  Cut (image (Preord.Hom _ _ (Qplus q) _) (cut_upper c))
      (image (Preord.Hom _ _ (Qplus q) _) (cut_lower c))
      _ _ _ _.
Next Obligation.
  red; intros.
  simpl. red in H. simpl in H. rewrite H. reflexivity.
Qed.
Next Obligation.
  red; intros.
  simpl. red in H. simpl in H. rewrite H. reflexivity.
Qed.
Next Obligation.
  simpl; intros.
  apply image_axiom2 in H.
  destruct H as [u' [??]].
  apply image_axiom2 in H0.
  destruct H0 as [l' [??]].
  simpl in *.
  destruct H1. destruct H2.
  red in H1. simpl in H1.
  red in H2. simpl in H2.
  rewrite H2. rewrite H1.
  apply Qplus_le_compat.
  apply Qle_refl.
  eapply cut_proper; eauto.
Qed.
Next Obligation.
  intros.
  apply image_axiom2 in H0.
  destruct H0 as [y [??]]. simpl in *.
  apply image_axiom1'. simpl.
  exists (q1 - q)%Q.
  split.
  split; red; simpl; ring.
  apply cut_is_lower with y; auto.
  rewrite <- (Qplus_le_l _ _ q).
  ring_simplify.
  apply Qle_trans with q2; auto.
  destruct H1. red in H1. simpl in H1.
  rewrite H1.
  ring_simplify. apply Qle_refl.
Qed.
Next Obligation.
  intros.
  apply image_axiom2 in H0.
  destruct H0 as [y [??]]. simpl in *.
  apply image_axiom1'. simpl.
  exists (q2 - q)%Q.
  split.
  split; red; simpl; ring.
  apply cut_is_upper with y; auto.
  rewrite <- (Qplus_le_l _ _ (q)).
  ring_simplify.
  apply Qle_trans with q1; auto.
  destruct H1. red in H1. simpl in H1.
  rewrite H1.
  ring_simplify. apply Qle_refl.
Qed.
Next Obligation.
  intros.
  split; intros.
  destruct H as [x ?].
  apply image_axiom2 in H.
  destruct H as [y [??]]. simpl in*.
  destruct (cut_nonextended c).
  destruct H1 as [l ?]; eauto.
  exists (l + q)%Q.
  apply image_axiom1'. simpl.
  exists l. split.
  split; red; simpl; ring.
  auto.
  destruct H as [x ?].
  apply image_axiom2 in H.
  destruct H as [y [??]]. simpl in*.
  destruct (cut_nonextended c).
  destruct H2 as [u ?]; eauto.
  exists (u + q)%Q.
  apply image_axiom1'. simpl.
  exists u. split.
  split; red; simpl; ring.
  auto.
Qed.


Record cut_telescope := CutTelescope
  { seq_upper : N -> cut
  ; seq_lower : N -> cut
  ; seq_proper : forall n, cut_lt (seq_lower n) (seq_upper n)
  ; seq_upper_inside : forall n m, (n < m)%N -> cut_lt (seq_upper m) (seq_upper n)
  ; seq_lower_inside : forall n m, (n < m)%N -> cut_lt (seq_lower n) (seq_lower m)
  ; seq_converges : forall ε, 0 < ε ->
     exists n a b,
       a ∈ cut_upper (seq_upper n) /\
       b ∈ cut_lower (seq_lower n) /\
       a - b <= ε
  }.

Section cauchy_to_telescope.
  Variable seq : N -> cut.
  Hypothesis Hcauchy : cauchy_limit_cuts.cauchy_sequence seq.

  Program Definition cauchy_telescope :=
    CutTelescope
      (fun n => shift_cut (oneOver2n n) (seq (n+1)))
      (fun n => shift_cut (-oneOver2n n) (seq (n+1)))
      _ _ _ _.
  Next Obligation.
    repeat intro.
    red. simpl.
    destruct (Hcauchy (n+1) (n+1) n)%N as [u [? [l [??]]]]; auto.
    zify; omega.
    zify; omega.

    exists (u - oneOver2n n)%Q.
    exists (l + oneOver2n n)%Q.
    split.
    apply image_axiom1'. simpl.
    exists u. split.
    split; red; simpl; ring. auto.
    split.
    apply image_axiom1'. simpl.
    exists l. split.
    split; red; simpl; ring. auto.
    rewrite <- (Qplus_lt_l _ _ (oneOver2n n)).
    ring_simplify.
    rewrite <- (Qplus_lt_l _ _ (-l)). ring_simplify.
    apply Qle_lt_trans with (u-l).
    ring_simplify. apply Qle_refl.
    eapply Qlt_le_trans. apply H1.
    rewrite <- (Qplus_le_l _ _ (-oneOver2n n)).
    ring_simplify.
    apply Qlt_le_weak.
    apply oneOver2n_pos.
  Qed.    
  Next Obligation.
    repeat intro. red.
    destruct (Hcauchy (n+1) (m+1) (n+1))%N as [u [? [l [??]]]]; intuition.
    zify. omega.

    exists (u + oneOver2n m)%Q.
    exists (l + oneOver2n n)%Q.
    split.
    simpl.
    apply image_axiom1'.
    simpl. exists u. split.
    split; red; simpl; ring.
    auto.
    split.
    apply image_axiom1'.
    simpl. exists l. split.
    split; red; simpl; ring.
    auto.
    rewrite <- (Qplus_lt_l _ _ (-oneOver2n m)).
    ring_simplify.
    rewrite <- (Qplus_lt_l _ _ (-l)).
    apply Qle_lt_trans with (u-l); auto.
    ring_simplify. apply Qle_refl.
    eapply Qlt_le_trans. apply H2.
    ring_simplify.
    rewrite <- (Qplus_le_l _ _ (oneOver2n m)).
    ring_simplify.
    apply oneOver2n_subsumes; auto.
    zify. omega.
  Qed.
  Next Obligation.
    repeat intro. red.
    destruct (Hcauchy (m+1) (n+1) (n+1))%N as [u [? [l [??]]]]; intuition.
    zify. omega.

    exists (u - oneOver2n n)%Q.
    exists (l - oneOver2n m)%Q.
    split.
    simpl.
    apply image_axiom1'.
    simpl. exists u. split.
    split; red; simpl; ring.
    auto.
    split.
    apply image_axiom1'.
    simpl. exists l. split.
    split; red; simpl; ring.
    auto.
    rewrite <- (Qplus_lt_l _ _ (oneOver2n n)).
    ring_simplify.
    rewrite <- (Qplus_lt_l _ _ (-l)).
    ring_simplify.
    apply Qle_lt_trans with (u-l); auto.
    ring_simplify. apply Qle_refl.
    eapply Qlt_le_trans. apply H2.
    rewrite <- (Qplus_le_l _ _ (oneOver2n m)).
    ring_simplify.
    apply oneOver2n_subsumes; auto.
    zify. omega.
  Qed.
  Next Obligation.
    intros.
    destruct (oneOver2n_small (ε/(3#1))).
    apply Qlt_shift_div_l. compute. auto.
    ring_simplify. auto.

    exists x.
    destruct (Hcauchy (x+1) (x+1) (x+1))%N as [u [? [l [??]]]]; intuition.
    exists (u + oneOver2n x)%Q.
    exists (l - oneOver2n x)%Q.
    split.
    simpl.
    apply image_axiom1'.
    simpl.
    exists u. split; auto.
    split; red; simpl; ring.
    split.
    simpl.
    apply image_axiom1'.
    simpl.
    exists l. split; auto.
    split; red; simpl; ring.
    ring_simplify.
    rewrite <- (Qplus_le_l _ _ (-(2#1) * oneOver2n x)).
    ring_simplify.
    apply Qlt_le_weak.
    eapply Qlt_le_trans. apply H3.
    rewrite <- (Qplus_le_l _ _ ((2#1) * oneOver2n x)).
    ring_simplify.
    rewrite <- oneOver2n_commute.
    unfold oneOver2n at 2. simpl.
    unfold shift_pos. simpl.
    ring_simplify.
    apply Qle_trans with ((5#2)*(ε/(3#1))).
    apply Qmult_le_compat; intuition.
    unfold Qdiv. unfold Qinv. simpl.
    ring_simplify.
    apply Qle_trans with (1*ε).
    apply Qmult_le_compat; intuition.
    ring_simplify. apply Qle_refl.
  Qed.

  Let interval_limit (t:cut_telescope) : cut :=
    interval_limit_cuts.interval_limit 
      (seq_upper t) (seq_lower t)
      (seq_proper t) (seq_upper_inside t) (seq_lower_inside t).

  Lemma telescope_limit_is_limit :
    cauchy_limit_cuts.is_limit seq (interval_limit cauchy_telescope).
  Proof.
    red. intros.
    destruct (oneOver2n_small (ε/(2#1))).
    apply Qlt_shift_div_l. compute. auto.
    ring_simplify. auto.
    exists x. intros. split.
    destruct (Hcauchy (x+1) n x)%N as [u [? [l [??]]]]; auto.
    zify; omega. 
    exists u. exists (l - oneOver2n x). split; auto.
    split; simpl.
    unfold interval_limit_cuts.limit_lower.
    apply union_axiom.
    exists (cut_lower (shift_cut (- oneOver2n x) (seq (x+1)))).
    split. exists x. auto.
    simpl. apply image_axiom1'.
    simpl. exists l. split; auto.
    split; red; simpl; ring.
    ring_simplify.
    rewrite <- (Qplus_le_l _ _ (- oneOver2n x)).
    ring_simplify.
    apply Qlt_le_weak. eapply Qlt_le_trans. apply H4.
    rewrite <- (Qplus_le_l _ _ (oneOver2n x)).
    ring_simplify.
    apply Qle_trans with ((2#1) * (ε / (2#1))).
    apply Qmult_le_compat; intuition.
    field_simplify.
    field_simplify.
    intuition.
        
    destruct (Hcauchy n (x+1) x)%N as [u [? [l [??]]]]; auto.
    zify; omega. 
    exists (u + oneOver2n x)%Q. exists l. 
    split.
    simpl.
    unfold interval_limit_cuts.limit_upper.
    apply union_axiom.
    exists (cut_upper (shift_cut (oneOver2n x) (seq (x+1)))).
    split. exists x. auto.
    simpl. apply image_axiom1'.
    simpl. exists u. split; auto.
    split; red; simpl; ring.
    split; auto.
    rewrite <- (Qplus_le_l _ _ (- oneOver2n x)).
    ring_simplify.
    apply Qlt_le_weak. eapply Qlt_le_trans. apply H4.
    rewrite <- (Qplus_le_l _ _ (oneOver2n x)).
    ring_simplify.
    apply Qle_trans with ((2#1) * (ε / (2#1))).
    apply Qmult_le_compat; intuition.
    field_simplify.
    field_simplify.
    intuition.
  Qed.

End cauchy_to_telescope.