-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLibOrder.v
More file actions
1159 lines (890 loc) · 32.6 KB
/
LibOrder.v
File metadata and controls
1159 lines (890 loc) · 32.6 KB
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
(* This file is extracted from the TLC library.
http://github.com/charguer/tlc
DO NOT EDIT. *)
(**************************************************************************
* TLC: A library for Coq *
* Order relations *
**************************************************************************)
Set Implicit Arguments.
From SLF Require Import LibTactics LibLogic LibReflect LibOperation LibRelation.
Generalizable Variables A.
(**************************************************************************)
(* ################################################################# *)
(* * Preorder *)
(** Definition *)
Record preorder A (R:binary A) : Prop := {
preorder_refl : refl R;
preorder_trans : trans R }.
Arguments preorder_trans [A] [R] [p] y [x] [z].
(** Transformations *)
Lemma preorder_inverse : forall A (R:binary A),
preorder R ->
preorder (inverse R).
Proof using. hint trans_inverse. introv [Re Tr]. constructor~. Qed.
Lemma preorder_rclosure : forall A (R:binary A),
preorder R ->
preorder (rclosure R).
Proof using. hint refl_rclosure, trans_rclosure. introv [Re Tr]. constructor~. Qed.
(**************************************************************************)
(* ################################################################# *)
(* * Total preorder *)
(** Definition of total preorder relations *)
Record total_preorder A (R:binary A) : Prop := {
total_preorder_trans : trans R;
total_preorder_total : total R }.
Arguments total_preorder_trans [A] [R] t y [x] [z].
(** Conversion to preorder *)
Lemma total_preorder_refl : forall A (le:binary A),
total_preorder le ->
refl le.
Proof using. introv [Tr To]. intros x. destruct~ (To x x). Qed.
Hint Resolve total_preorder_refl.
Coercion total_preorder_to_preorder A (R:binary A)
(O:total_preorder R) : preorder R.
Proof using. lets [M _]: O. constructor~. Qed.
Hint Resolve total_preorder_to_preorder.
(** Transformations *)
Lemma total_preorder_inverse : forall A (R:binary A),
total_preorder R ->
total_preorder (inverse R).
Proof using. hint trans_inverse, total_inverse. introv [Tr To]. constructor~. Qed.
Lemma total_preorder_rclosure : forall A (R:binary A),
total_preorder R ->
total_preorder (rclosure R).
Proof using. hint trans_rclosure, total_rclosure. introv [Re Tr]. constructor~. Qed.
(** Properties *)
Lemma inverse_of_not : forall A (R:binary A) x y,
total R ->
~ R x y ->
inverse R x y.
Proof using. introv T H. destruct (T x y); auto_false~. Qed.
Lemma inverse_strict_of_not : forall A (R:binary A) x y,
total R ->
~ R x y ->
inverse (strict R) x y.
Proof using.
introv T H. destruct (T x y). auto_false~.
hnf. split~. intro_subst~.
Qed.
(**************************************************************************)
(* ################################################################# *)
(* * Order *)
(** Definition *)
Record order A (R:binary A) : Prop := {
order_refl : refl R;
order_trans : trans R;
order_antisym : antisym R }.
Arguments order_trans [A] [R] [o] y [x] [z].
Arguments order_antisym [A] [R] [o] [x] [y].
(** Conversion to preorder *)
Coercion order_to_preorder A (R:binary A)
(O:order R) : preorder R.
Proof using. destruct* O. constructors*. Qed.
Hint Resolve order_to_preorder.
(** Transformations *)
Lemma order_inverse : forall A (R:binary A),
order R ->
order (inverse R).
Proof using.
hint trans_inverse, antisym_inverse.
introv [Re Tr An]. constructor~.
Qed.
Lemma order_rclosure : forall A (R:binary A),
order R ->
order (rclosure R).
Proof using.
hint refl_rclosure, trans_rclosure, antisym_rclosure.
introv [Re Tr An]. constructor~.
Qed.
(** Properties *)
(* ********************************************************************** *)
(* ################################################################# *)
(** * Order relation upto an equivalence relation *)
(** Note: this is used in LibFix *)
Record order_wrt (A:Type) (E:binary A) (R:binary A) : Prop := {
order_wrt_refl : refl R;
order_wrt_trans : trans R;
order_wrt_antisym : antisym_wrt E R }.
Arguments order_wrt_trans [A] [E] [R] [o] y [x] [z].
Arguments order_wrt_antisym [A] [E] [R] [o] [x] [y].
(** Conversion to preorder *)
Coercion order_wrt_to_preorder A (E:binary A) (R:binary A)
(O:order_wrt E R) : preorder R.
Proof using. destruct* O. constructors*. Qed.
Hint Resolve order_wrt_to_preorder.
(** Transformations *)
Lemma order_wrt_inverse : forall A (E:binary A) (R:binary A),
order_wrt E R ->
order_wrt E (inverse R).
Proof using.
hint trans_inverse, antisym_wrt_inverse.
introv [Re Tr An]. constructor~.
Qed.
Lemma order_wrt_rclosure : forall A (E:binary A) (R:binary A),
order_wrt E R ->
order_wrt (rclosure E) (rclosure R).
Proof using.
hint refl_rclosure, trans_rclosure, antisym_wrt_rclosure.
introv [Re Tr An]. constructor~.
Qed.
(** Properties *)
(**************************************************************************)
(* ################################################################# *)
(* * Total Order *)
(** Definition *)
Record total_order A (R:binary A) : Prop := {
total_order_order : order R;
total_order_total : total R }.
(** Projections *)
Definition total_order_refl := order_refl.
Definition total_order_trans := order_trans.
Definition total_order_antisym := order_antisym.
Arguments total_order_trans [A] [R] [o] y [x] [z].
Arguments total_order_antisym [A] [R] [o] [x] [y].
(** Construction *)
Lemma total_order_intro : forall A (R:binary A),
trans R ->
antisym R ->
total R ->
total_order R.
Proof using.
introv Tra Ant Tot. constructor~. constructor~.
intros_all. destruct~ (Tot x x).
Qed.
(** Conversion to order *)
Coercion total_order_to_total_preorder A (R:binary A)
(O:total_order R) : total_preorder R.
Proof using. destruct* O. constructors*. applys* order_trans. Qed.
Definition total_order_to_order := total_order_order.
Hint Resolve total_order_to_order total_order_to_total_preorder.
(** Transformations *)
Lemma total_order_inverse : forall A (R:binary A),
total_order R ->
total_order (inverse R).
Proof using.
hint total_inverse, order_inverse.
introv [Or To]. constructor~.
Qed.
Lemma total_order_rclosure : forall A (R:binary A),
total_order R ->
total_order (rclosure R).
Proof using.
hint total_rclosure, order_rclosure.
introv [Or To]. constructor~.
Qed.
(** Properties *)
Section TotalOrderProp.
Variables (A:Type) (R : binary A).
(** WARNING: notations here are not typeclass operators
-- TODO: is this really what we want?
perhaps it would be clearer to inline these notations. *)
Notation "'le'" := (R).
Notation "'ge'" := (inverse R).
Notation "'lt'" := (strict R).
Notation "'gt'" := (inverse lt).
Ltac total_order_normalize :=
repeat rewrite rclosure_eq_fun;
repeat rewrite inverse_eq_fun;
repeat rewrite strict_eq_fun.
Lemma total_order_le_is_rclosure_lt : forall (To:total_order R),
le = rclosure lt.
Proof using.
extens. intros. total_order_normalize. iff M.
tests~: (x = y).
destruct M. autos*. subst*. dintuition eauto.
Qed.
Lemma total_order_lt_is_strict_le : forall (To:total_order R),
lt = strict le.
Proof using.
auto.
Qed.
Lemma total_order_ge_is_rclosure_gt : forall (To:total_order R),
ge = rclosure gt.
Proof using.
extens. intros. total_order_normalize. iff M.
tests~: (x = y).
destruct M. autos*. subst*. dintuition eauto.
Qed.
Lemma total_order_gt_is_strict_ge : forall (To:total_order R),
gt = strict ge.
Proof using.
extens. intros. total_order_normalize. iff M.
tests~: (x = y).
destruct M. autos*.
destruct M. autos*.
Qed.
Lemma total_order_lt_or_eq_or_gt : forall (To:total_order R) x y,
lt x y \/ x = y \/ gt x y.
Proof using.
introv H. intros. total_order_normalize. tests: (x = y).
branch~ 2.
destruct (total_order_total H x y).
branch~ 1.
branch~ 3.
Qed.
Lemma total_order_lt_or_ge : forall (To:total_order R) x y,
lt x y \/ ge x y.
Proof using.
intros. branches (total_order_lt_or_eq_or_gt To x y).
left~.
right~. subst. hnf. apply~ total_order_refl.
right~. rewrite~ total_order_ge_is_rclosure_gt.
total_order_normalize. hnfs~.
Qed.
Lemma total_order_le_or_gt : forall (To:total_order R) x y,
le x y \/ gt x y.
Proof using.
intros. branches~ (total_order_lt_or_eq_or_gt To x y).
left~. rewrite~ total_order_le_is_rclosure_lt. total_order_normalize. hnfs~.
left~. subst. apply~ total_order_refl.
Qed.
End TotalOrderProp.
(**************************************************************************)
(* ################################################################# *)
(* * Strict order *)
(** Definition *)
Record strict_order A (R:binary A) : Prop := {
strict_order_irrefl : irrefl R;
strict_order_asym : asym R;
strict_order_trans : trans R }.
Arguments strict_order_trans [A] [R] [s] y [x] [z].
(** Transformations *)
Lemma strict_order_inverse : forall A (R:binary A),
strict_order R ->
strict_order (inverse R).
Proof using.
hint antisym_inverse, trans_inverse, asym_inverse.
introv [Ir As Tr]. constructor~.
Qed.
Lemma strict_order_strict : forall A (R:binary A),
order R ->
strict_order (strict R).
Proof using.
introv [Re As Tr]. unfold strict. constructor; intros_all; simpls.
destruct* H.
applys* antisym_inv x y.
split. applys* As. intros E. subst. applys* antisym_inv y z.
Qed.
Lemma order_rclosure_of_strict_order : forall A (R:binary A),
strict_order R ->
order (rclosure R).
Proof using.
introv [Re As Tr]. rewrite rclosure_eq_fun. constructor; simpl.
intros_all~.
introv [H1|E1] [H2|E2]; subst; auto.
left. apply* trans_inv.
introv [H1|E1] [H2|E2]; try subst; auto.
false. apply* As.
Qed.
(**************************************************************************)
(* ################################################################# *)
(* * Total strict order *)
(** Definition *)
Record strict_total_order A (R:binary A) : Prop := {
strict_total_order_trans : trans R;
strict_total_order_trichotomous : trichotomous R }.
Arguments strict_total_order_trans [A] [R] [s] y [x] [z].
(** Conversion to strict order and back *)
Lemma strict_total_order_irrefl : forall A (R:binary A),
strict_total_order R ->
irrefl R.
Proof using. introv [Tr Tk]. intros x. lets: (Tk x x). inverts~ H. Qed.
Lemma strict_total_order_asym : forall A (R:binary A),
strict_total_order R ->
asym R.
Proof using. introv [Tr Tk]. intros x y. lets: (Tk x y). inverts~ H. Qed.
Coercion strict_total_order_to_strict_order A (R:binary A)
(O:strict_total_order R) : strict_order R.
Proof using.
lets [M _]: O. constructor;
autos~ strict_total_order_irrefl strict_total_order_asym.
Qed.
Hint Resolve strict_total_order_to_strict_order.
(** Transformation *)
Lemma strict_total_order_inverse : forall A (R:binary A),
strict_total_order R ->
strict_total_order (inverse R).
Proof using.
introv [Tr Tk]. constructor. apply~ trans_inverse.
apply~ trichotomous_inverse.
Qed.
(** From total order *)
Lemma strict_total_order_of_total_order : forall A (R:binary A),
total_order R ->
strict_total_order (strict R).
Proof using.
introv [[Re Tr As] To]. constructor.
apply~ trans_strict.
intros x y. tests: (x = y).
subst. apply trichotomy_eq. unfolds* strict.
unfold strict. destruct (To x y).
apply* trichotomy_left.
apply* trichotomy_right.
Qed.
(* ********************************************************************** *)
(* ################################################################# *)
(** * Definition of order operators *)
(* ---------------------------------------------------------------------- *)
(* ================================================================= *)
(** ** Classes and notation for comparison operators *)
(** Operators *)
Class Le (A : Type) := { le : binary A }.
Class Ge (A : Type) := { ge : binary A }.
Class Lt (A : Type) := { lt : binary A }.
Class Gt (A : Type) := { gt : binary A }.
Global Opaque le lt ge gt.
(** Structures *)
Class Le_preorder `{Le A} : Prop :=
{ le_preorder : preorder le }.
Class Le_total_preorder `{Le A} : Prop :=
{ le_total_preorder : total_preorder le }.
Class Le_order `{Le A} : Prop :=
{ le_order : order le }.
Class Le_total_order `{Le A} : Prop :=
{ le_total_order : total_order le }.
Class Lt_strict_order `{Lt A} : Prop :=
{ lt_strict_order : strict_order lt }.
Class Lt_strict_total_order `{Lt A} : Prop :=
{ lt_strict_total_order : strict_total_order lt }.
(** Notation *)
Declare Scope comp_scope.
Notation "x <= y" := (le x y)
(at level 70, no associativity) : comp_scope.
Notation "x >= y" := (ge x y)
(at level 70, no associativity) : comp_scope.
Notation "x < y" := (lt x y)
(at level 70, no associativity) : comp_scope.
Notation "x > y" := (gt x y)
(at level 70, no associativity) : comp_scope.
Open Scope comp_scope.
Notation "x <= y <= z" := (x <= y /\ y <= z)
(at level 70, y at next level) : comp_scope.
Notation "x <= y < z" := (x <= y /\ y < z)
(at level 70, y at next level) : comp_scope.
Notation "x < y <= z" := (x < y /\ y <= z)
(at level 70, y at next level) : comp_scope.
Notation "x < y < z" := (x < y /\ y < z)
(at level 70, y at next level) : comp_scope.
(* ---------------------------------------------------------------------- *)
(* ================================================================= *)
(** ** The operators [ge], [lt] and [gt] are deduced from [le] *)
Instance ge_of_le : forall `{Le A}, Ge A.
constructor. apply (inverse le). Defined.
Instance lt_of_le : forall `{Le A}, Lt A.
constructor. apply (strict le). Defined.
Instance gt_of_le : forall `{Le A}, Gt A.
constructor. apply (inverse lt). Defined.
Lemma ge_is_inverse_le : forall `{Le A}, ge = inverse le.
Proof using. extens*. Qed.
Lemma lt_is_strict_le : forall `{Le A}, lt = strict le.
Proof using. extens*. Qed.
Lemma gt_is_inverse_lt : forall `{Le A}, gt = inverse lt.
Proof using. extens*. Qed.
Lemma gt_is_inverse_strict_le : forall `{Le A}, gt = inverse (strict le).
Proof using. extens. intros. rewrite gt_is_inverse_lt. rewrite* lt_is_strict_le. Qed.
Global Opaque ge_of_le lt_of_le gt_of_le.
(** Local tactic [rew_to_le] *)
Hint Rewrite @gt_is_inverse_strict_le @ge_is_inverse_le @lt_is_strict_le : rew_to_le.
Tactic Notation "rew_to_le" :=
autorewrite with rew_to_le in *.
Hint Rewrite @ge_is_inverse_le @gt_is_inverse_lt : rew_to_le_lt.
Tactic Notation "rew_to_le_lt" :=
autorewrite with rew_to_le_lt in *.
Lemma gt_is_strict_inverse_le : forall `{Le A},
gt = strict (inverse le).
Proof using. intros. rew_to_le. apply inverse_strict. Qed.
Lemma le_is_rclosure_lt : forall `{Le A},
refl le ->
le = rclosure lt.
Proof using. intros. rew_to_le. rewrite~ rclosure_strict. Qed.
Lemma le_is_inverse_ge : forall `{Le A},
le = inverse ge.
Proof using. intros. rew_to_le. rewrite~ inverse_inverse. Qed.
Lemma lt_is_inverse_gt : forall `{Le A},
lt = inverse gt.
Proof using. intros. rew_to_le. rewrite~ inverse_inverse. Qed.
Lemma gt_is_strict_ge : forall `{Le A},
gt = strict ge.
Proof using. intros. rew_to_le. apply inverse_strict. Qed.
Lemma ge_is_rclosure_gt : forall `{Le A},
refl le ->
ge = rclosure gt.
Proof using. intros. rewrite gt_is_strict_ge. rewrite~ rclosure_strict. Qed.
(* ********************************************************************** *)
(* ################################################################# *)
(** * Classes for comparison properties *)
(* ---------------------------------------------------------------------- *)
(* ================================================================= *)
(** ** Definition of classes *)
(** symmetric structure *)
Class Ge_preorder `{Le A} : Prop :=
{ ge_preorder : preorder ge }.
Class Ge_total_preorder `{Le A} : Prop :=
{ ge_total_preorder : total_preorder ge }.
Class Ge_order `{Le A} : Prop :=
{ ge_order : order ge }.
Class Ge_total_order `{Le A} : Prop :=
{ ge_total_order : total_order le }.
Class Gt_strict_order `{Le A} : Prop :=
{ gt_strict_order : strict_order gt }.
Class Gt_strict_total_order `{Le A} : Prop :=
{ gt_strict_total_order : strict_total_order gt }.
(** properties of le *)
Class Le_refl `{Le A} :=
{ le_refl : refl le }.
Class Le_trans `{Le A} :=
{ le_trans : trans le }.
Class Le_antisym `{Le A} :=
{ le_antisym : antisym le }.
Class Le_total `{Le A} :=
{ le_total : total le }.
(** properties of ge *)
Class Ge_refl `{Ge A} :=
{ ge_refl : refl ge }.
Class Ge_trans `{Ge A} :=
{ ge_trans : trans ge }.
Class Ge_antisym `{Ge A} :=
{ ge_antisym : antisym ge }.
Class Ge_total `{Ge A} :=
{ ge_total : total ge }.
(** properties of lt *)
Class Lt_irrefl `{Lt A} :=
{ lt_irrefl : irrefl lt }.
Class Lt_trans `{Lt A} :=
{ lt_trans : trans lt }.
(** properties of gt *)
Class Gt_irrefl `{Gt A} :=
{ gt_irrefl : irrefl gt }.
Class Gt_trans `{Gt A} :=
{ gt_trans : trans gt }.
(** mixed transitivity results *)
Class Lt_le_trans `{Le A} :=
{ lt_le_trans : forall y x z, x < y -> y <= z -> x < z }.
Class Le_lt_trans `{Le A} :=
{ le_lt_trans : forall y x z, x <= y -> y < z -> x < z }.
Class Gt_ge_trans `{Le A} :=
{ gt_ge_trans : forall y x z, x > y -> y >= z -> x > z }.
Class Ge_gt_trans `{Le A} :=
{ ge_gt_trans : forall y x z, x >= y -> y > z -> x > z }.
Arguments lt_irrefl [A] [H] [Lt_irrefl].
Arguments le_trans {A} {H} {Le_trans} y [x] [z].
Arguments ge_trans {A} {H} {Ge_trans} y [x] [z].
Arguments lt_trans {A} {H} {Lt_trans} y [x] [z].
Arguments gt_trans {A} {H} {Gt_trans} y [x] [z].
(** conversion between operators *)
Class Ge_as_sle `{Le A} : Prop :=
{ ge_as_sle : forall x y : A, (x >= y) = (y <= x) }.
Class Gt_as_slt `{Le A} : Prop :=
{ gt_as_slt : forall x y : A, (x > y) = (y < x) }.
Class Ngt_as_sle `{Le A} : Prop :=
{ ngt_as_sle : forall x y : A, (~ x < y) = (y <= x) }.
Class Nlt_as_ge `{Le A} : Prop :=
{ nlt_as_ge : forall x y : A, (~ x < y) = (x >= y) }.
Class Ngt_as_le `{Le A} : Prop :=
{ ngt_as_le : forall x y : A, (~ x > y) = (x <= y) }.
Class Nle_as_gt `{Le A} : Prop :=
{ nle_as_gt : forall x y : A, (~ x <= y) = (x > y) }.
Class Nge_as_lt `{Le A} : Prop :=
{ nge_as_lt : forall x y : A, (~ x >= y) = (x < y) }.
(** inclusion between operators *)
Class Lt_to_le `{Le A} : Prop :=
{ lt_to_le : forall x y : A, (x < y) -> (x <= y) }.
Class Gt_to_ge `{Le A} : Prop :=
{ gt_to_ge : forall x y : A, (x > y) -> (x >= y) }.
Class Nle_to_sle `{Le A} : Prop :=
{ nle_to_sle : forall x y : A, (~ x <= y) -> (y <= x) }.
Class Nle_to_slt `{Le A} : Prop :=
{ nle_to_slt : forall x y : A, (~ x <= y) -> (y < x) }.
(** case analysis *)
Class Case_eq_lt_gt `{Le A} : Prop :=
{ case_eq_lt_gt : forall x y : A, x = y \/ x < y \/ x > y }.
Class Case_eq_lt_slt `{Le A} : Prop :=
{ case_eq_lt_slt : forall x y : A, x = y \/ x < y \/ y < x }.
Class Case_le_gt `{Le A} : Prop :=
{ case_le_gt : forall x y : A, x <= y \/ x > y }.
Class Case_le_slt `{Le A} : Prop :=
{ case_le_slt : forall x y : A, x <= y \/ y < x }.
Class Case_lt_ge `{Le A} : Prop :=
{ case_lt_ge : forall x y : A, x < y \/ x >= y }.
Class Case_lt_sle `{Le A} : Prop :=
{ case_lt_sle : forall x y : A, x < y \/ y <= x }.
(** case analysis under one assumption *)
Class Neq_case_lt_gt `{Le A} : Prop :=
{ neq_case_lt_gt : forall x y : A, x <> y -> x < y \/ x > y }.
Class Neq_case_lt_slt `{Le A} : Prop :=
{ neq_case_lt_slt : forall x y : A, x <> y -> x < y \/ y < x }.
Class Le_case_eq_lt `{Le A} : Prop :=
{ le_case_eq_lt : forall x y : A, x <= y -> x = y \/ x < y }.
Class Ge_case_eq_gt `{Le A} : Prop :=
{ ge_case_eq_gt : forall x y : A, x >= y -> x = y \/ x > y }.
(** case analysis under two assumptions *)
Class Le_neq_to_lt `{Le A} : Prop :=
{ le_neq_to_lt : forall x y : A, x <= y -> x <> y -> x < y }.
Class Ge_neq_to_gt `{Le A} : Prop :=
{ ge_neq_to_gt : forall x y : A, x >= y -> x <> y -> x > y }.
Class Nlt_nslt_to_eq `{Le A} : Prop :=
{ nlt_nslt_to_eq : forall x y : A, ~ (lt x y) -> ~ (lt y x) -> x = y }.
(** contradiction from case analysis *)
Class Lt_ge_false `{Le A} : Prop :=
{ lt_ge_false : forall x y : A, x < y -> x >= y -> False }.
Class Lt_gt_false `{Le A} : Prop :=
{ lt_gt_false : forall x y : A, x < y -> x > y -> False }.
Class Lt_slt_false `{Le A} : Prop :=
{ lt_slt_false : forall x y : A, x < y -> y < x -> False }.
(* ********************************************************************** *)
(* ################################################################# *)
(* * Instances for comparison structures *)
Section Instances.
Context `{Le A}.
Ltac auto_star ::= try solve [ dauto ].
(** derived structures *)
Global Instance Le_preorder_of_Le_order :
Le_order ->
Le_preorder.
Proof using. constructor. intros. apply* order_to_preorder. Qed.
Global Instance Le_total_preorder_of_Le_total_order :
Le_total_order ->
Le_total_preorder.
Proof using. constructor. intros. apply* total_order_to_total_preorder. Qed.
Global Instance Le_preorder_of_Total_preorder :
Le_total_preorder ->
Le_preorder.
Proof using. constructor. intros. apply* total_preorder_to_preorder. Qed.
Global Instance Le_order_of_Le_total_order :
Le_total_order ->
Le_order.
Proof using. constructor. intros. apply* total_order_to_order. Qed.
Global Instance lt_strict_order_of_lt_strict_total_order :
Lt_strict_total_order ->
Lt_strict_order.
Proof using. constructor. intros. apply* strict_total_order_to_strict_order. Qed.
Global Instance Lt_strict_order_of_Le_order :
Le_order ->
Lt_strict_order.
Proof using. constructor. intros. rew_to_le. apply* strict_order_strict. Qed.
Global Instance Lt_strict_total_order_of_Le_total_order :
Le_total_order ->
Lt_strict_total_order.
Proof using. constructor. intros. rew_to_le. apply* strict_total_order_of_total_order. Qed.
(** symmetric structures *)
Global Instance Ge_preorder_of_Le_order :
Le_order ->
Ge_preorder.
Proof using. constructor. rew_to_le. apply preorder_inverse. apply le_preorder. Qed.
Global Instance Ge_total_preorder_of_Le_total_order :
Le_total_order ->
Ge_total_preorder.
Proof using. constructor. rew_to_le. apply total_preorder_inverse. apply le_total_preorder. Qed.
Global Instance Ge_preorder_of_Total_preorder :
Le_total_preorder ->
Ge_preorder.
Proof using. constructor. rew_to_le. apply preorder_inverse. apply le_preorder. Qed.
Global Instance Ge_order_of_Le_total_order :
Le_total_order ->
Ge_order.
Proof using. constructor. rew_to_le. apply order_inverse. apply le_order. Qed.
Global Instance Gt_strict_order_of_lt_strict_total_order :
Lt_strict_total_order ->
Gt_strict_order.
Proof using. constructor. rewrite gt_is_inverse_lt. apply strict_order_inverse. apply lt_strict_order. Qed.
Global Instance Gt_strict_order_of_Le_order :
Le_order ->
Gt_strict_order.
Proof using. constructor. rewrite gt_is_inverse_lt. apply strict_order_inverse. apply lt_strict_order. Qed.
Global Instance Gt_strict_total_order_of_Le_total_order :
Le_total_order ->
Gt_strict_total_order.
Proof using. constructor. rewrite gt_is_inverse_lt. apply strict_total_order_inverse. apply lt_strict_total_order. Qed.
(** properties of le *)
Global Instance Le_refl_of_Le_preorder :
Le_preorder ->
Le_refl.
Proof using. intros [[Re Tr]]. constructor~. Qed.
Global Instance Le_trans_of_Le_preorder :
Le_preorder ->
Le_trans.
Proof using. intros [[Re Tr]]. constructor~. Qed.
Global Instance Le_antisym_of_Le_order :
Le_order ->
Le_antisym.
Proof using. constructor. intros. apply* order_antisym. Qed.
Global Instance Le_total_of_Le_total_order :
Le_total_order ->
Le_total.
Proof using. constructor. intros. apply* total_order_total. Qed.
(** properties of ge *)
Global Instance Ge_refl_of_Le_preorder :
Le_preorder ->
Ge_refl.
Proof using. constructor. rew_to_le. apply refl_inverse. apply le_refl. Qed.
Global Instance Ge_trans_of_Le_preorder :
Le_preorder ->
Ge_trans.
Proof using. constructor. rew_to_le. apply trans_inverse. apply le_trans. Qed.
Global Instance Ge_antisym_of_Le_order :
Le_order ->
Ge_antisym.
Proof using. constructor. rew_to_le. apply antisym_inverse. apply le_antisym. Qed.
Global Instance Ge_total_of_Le_total_order :
Le_total_order ->
Ge_total.
Proof using. constructor. rew_to_le. apply total_inverse. apply le_total. Qed.
(** properties of lt *)
Global Instance Lt_irrefl_of_Le_order :
Le_order ->
Lt_irrefl.
Proof using. constructor. apply strict_order_irrefl. apply lt_strict_order. Qed.
Global Instance Lt_trans_of_Le_order :
Le_order ->
Lt_trans.
Proof using. constructor. apply strict_order_trans. apply lt_strict_order. Qed.
(** properties of gt *)
Global Instance Gt_irrefl_of_Le_order :
Le_order ->
Gt_irrefl.
Proof using. constructor. apply strict_order_irrefl. apply gt_strict_order. Qed.
Global Instance Gt_trans_of_Le_order :
Le_order ->
Gt_trans.
Proof using. constructor. apply strict_order_trans. apply gt_strict_order. Qed.
(** mixed transitivity results *)
Global Instance Lt_le_trans_of_Le_order :
Le_order ->
Lt_le_trans.
Proof using.
constructor. introv K L. rew_to_le. destruct K as [U V].
split~. apply* le_trans. intro_subst. apply V. apply* le_antisym.
Qed.
Global Instance Le_lt_trans_of_Le_order :
Le_order ->
Le_lt_trans.
Proof using.
constructor. introv K L. rew_to_le. destruct L as [U V].
split~. apply* le_trans. intro_subst. apply V. apply* le_antisym.
Qed.
Global Instance gt_ge_trans_of_Le_order :
Le_order ->
Gt_ge_trans.
Proof using.
constructor. introv K L. rew_to_le_lt. hnf in *. apply* le_lt_trans.
Qed.
Global Instance ge_gt_trans_of_Le_order :
Le_order ->
Ge_gt_trans.
Proof using.
constructor. introv K L. rew_to_le_lt. hnf in *. apply* lt_le_trans.
Qed.
(** conversion between operators *)
Global Instance Ge_as_sle_of :
Ge_as_sle.
Proof using. constructor. intros. rew_to_le. auto. Qed.
Global Instance Gt_as_slt_of :
Gt_as_slt.
Proof using. constructor. intros. rew_to_le. auto. Qed.
Global Instance Ngt_as_sle_of_Le_total_order :
Le_total_order ->
Ngt_as_sle.
Proof using.
constructor. intros. rew_to_le. unfold strict. rew_logic. iff M.
destruct M.
forwards K:(inverse_strict_of_not (R:=le)); eauto.
apply le_total. apply (proj1 K).
subst. apply le_refl.
apply or_classic_l. intros P Q. apply P. apply* le_antisym.
Qed.
Global Instance Nlt_as_ge_of_Le_total_order :
Le_total_order ->
Nlt_as_ge.
Proof using. constructor. intros. rew_to_le_lt. unfold inverse. apply ngt_as_sle. Qed.
Global Instance Ngt_as_le_of_Le_total_order :
Le_total_order ->
Ngt_as_le.
Proof using. constructor. intros. rew_to_le_lt. unfold inverse. apply ngt_as_sle. Qed.
Global Instance Nle_as_gt_of_Le_total_order :
Le_total_order ->
Nle_as_gt.
Proof using.
constructor. intros. rew_to_le_lt. unfold inverse.
rewrite <- ngt_as_sle. rewrite~ not_not_eq.
Qed.
Global Instance Nge_as_lt_of_Le_total_order :
Le_total_order ->
Nge_as_lt.
Proof using.
constructor. intros. rew_to_le_lt. unfold inverse.
rewrite nle_as_gt. rewrite~ gt_is_inverse_lt.
Qed.
(** inclusion between operators *)
Global Instance Lt_to_le_of :
Lt_to_le.
Proof using. constructor. intros. rew_to_le. unfolds* strict. Qed.
Global Instance Gt_to_ge_of :
Gt_to_ge.
Proof using. constructor. intros. rew_to_le. unfolds* inverse, strict. Qed.
Global Instance Nle_to_sle_of_Le_total_order :
Le_total_order ->
Nle_to_sle.
Proof using.
constructor. introv K. rewrite nle_as_gt in K.
rew_to_le. unfolds* inverse, strict.
Qed.
Global Instance Nle_to_slt_of_Le_total_order :
Le_total_order ->
Nle_to_slt.
Proof using.
constructor. introv K. rewrite nle_as_gt in K.
rew_to_le. unfolds* inverse, strict.
Qed.
(** case analysis under no assumption *)
Global Instance Case_eq_lt_gt_of_Le_total_order :
Le_total_order ->
Case_eq_lt_gt.
Proof using.
introv K. constructor. intros.
lets [(M1&M2)|[M|(M1&M2)]]: (total_order_lt_or_eq_or_gt le_total_order x y).
rewrite le_is_rclosure_lt in M1 by applys* total_order_refl. destruct* M1.
autos*.
rewrite le_is_rclosure_lt in M1 by applys* total_order_refl. destruct* M1.
Qed.
Global Instance Case_eq_lt_slt_of_Le_total_order :
Le_total_order ->
Case_eq_lt_slt.
Proof using.
constructor. intros. pattern lt at 2. rewrite lt_is_inverse_gt.
apply case_eq_lt_gt.
Qed.
Global Instance Case_le_gt_of_Le_total_order :
Le_total_order ->
Case_le_gt.
Proof using.
constructor. intros.
rewrite le_is_rclosure_lt by applys* total_order_refl. rewrite rclosure_eq.
branches (total_order_lt_or_eq_or_gt le_total_order x y); eauto.
Qed.
Global Instance Case_eq_lt_ge_of_Le_total_order :
Le_total_order ->
Case_lt_ge.