-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOrds.v
1841 lines (1544 loc) · 49.6 KB
/
Ords.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
Require Import syntax.
Require Import alist.
Require Import infrastructure.
Require vellvm. (* In order to avoid name clash on "comparison", I didn't Import it *)
Require Import sflib.
Require Export Coqlib.
Export LLVMsyntax.
Export LLVMinfra.
Require Import Decs.
Require Import TODO.
(* For "all_once" tactic. TODO: move this to TODOProof *)
Require Import TODOProof.
(* for des_ifs_safe tac *)
(* I checked dependency, but conceptually this may not good.. *)
(* file in def/ imports file in proof/ *)
Print Orders.OrderedType.
Print OrderedType.OrderedType.
(* Which one to use? *)
Print MSetAVL.Make.
(* MSet uses Orders.OrderedType. We go for that. *)
(* FYI: OrderedType.OrderedType is only for backward compatibility: *)
(* https://coq.inria.fr/library/ *)
Require Import Orders.
(* Because "Orders" name is already used, I choose "Ords" as this file's name *)
Require Import OrdersAlt.
Print OrdersAlt.
Print OrdersAlt.OrderedTypeAlt.
(* It seems convenient, but I will stick to standard. *)
(* I expect libraries are well engineered with standard, but I am unsure for Alt. *)
Print OrdersFacts.
Print OrdersTac.
(* Print OrdersEx. *) (* TODO: It exists in stlib web reference. Why not here? *)
Print OrdersLists.
Set Implicit Arguments.
Require Import Recdef.
(* https://coq.inria.fr/refman/Reference-Manual004.html#sec78 *)
(* Defines a recursive function by well founded recursion. The module Recdef of the standard library must be loaded for this feature. The {} annotation is mandatory and must be one of the following: *)
Local Open Scope nat.
Module OrdIdx.
Definition t := nat.
Definition zero: t := 0.
Definition one: t := 1.
Definition two: t := 2.
Definition three: t := 3.
Definition four: t := 4.
Definition five: t := 5.
Definition six: t := 6.
Definition seven: t := 7.
Definition eight: t := 8.
Definition nine: t := 9.
Definition onezero: t := 10.
Definition oneone: t := 11.
Definition onetwo: t := 12.
Definition onethree: t := 13.
Definition onefour: t := 14.
Definition onefive: t := 15.
Definition onesix: t := 16.
Definition oneseven: t := 17.
Definition oneeight: t := 18.
Definition onenine: t := 19.
Definition compare: t -> t -> comparison := Nat.compare.
End OrdIdx.
Notation "0" := OrdIdx.zero : OrdIdx_scope.
Notation "1" := OrdIdx.one : OrdIdx_scope.
Notation "2" := OrdIdx.two : OrdIdx_scope.
Notation "3" := OrdIdx.three : OrdIdx_scope.
Notation "4" := OrdIdx.four : OrdIdx_scope.
Notation "5" := OrdIdx.five : OrdIdx_scope.
Notation "6" := OrdIdx.six : OrdIdx_scope.
Notation "7" := OrdIdx.seven : OrdIdx_scope.
Notation "8" := OrdIdx.eight : OrdIdx_scope.
Notation "9" := OrdIdx.nine : OrdIdx_scope.
Notation "10" := OrdIdx.onezero : OrdIdx_scope.
Notation "11" := OrdIdx.oneone : OrdIdx_scope.
Notation "12" := OrdIdx.onetwo : OrdIdx_scope.
Notation "13" := OrdIdx.onethree : OrdIdx_scope.
Notation "14" := OrdIdx.onefour : OrdIdx_scope.
Notation "15" := OrdIdx.onefive : OrdIdx_scope.
Notation "16" := OrdIdx.onesix : OrdIdx_scope.
Notation "17" := OrdIdx.oneseven : OrdIdx_scope.
Notation "18" := OrdIdx.oneeight : OrdIdx_scope.
Notation "19" := OrdIdx.onenine : OrdIdx_scope.
Local Open Scope OrdIdx_scope.
Local Close Scope Z_scope.
Local Close Scope nat_scope.
(* Axiom addr_compare: forall A, A -> A -> bool. *)
(* Axiom addr_compare_spec: forall A (a0 a1: A), addr_compare a0 a1 = true -> a0 = a1. *)
(* Axiom wrap_compare: forall A (f: A -> A -> comparison), A -> A -> comparison. *)
Definition wrap_compare A (f: A -> A -> comparison): A -> A -> comparison := f.
Hint Unfold wrap_compare.
Definition lazy_comparison := unit -> comparison.
(* tactics *)
Ltac solve_leibniz_ cmp CL :=
repeat match goal with
| [H:cmp ?a ?b = Eq |- _] => apply CL in H
end; subst.
Ltac finish_refl_ cmp REFL:=
try match goal with
| [H: cmp ?a ?a = Lt |- _] =>
rewrite REFL in H; congruence
| [H: cmp ?a ?a = Gt |- _] =>
rewrite REFL in H; congruence
end;
try by apply REFL.
Ltac apply_trans_ cmp TRANS :=
try match goal with
| [H1: cmp ?x ?y = ?c, H2: cmp ?y ?x = ?c |- _] =>
exploit (TRANS c x y x); eauto; (idtac; []; i);
exploit (TRANS c y x y); eauto; (idtac; []; i)
end;
try match goal with
| [H1: cmp ?x ?y = ?c, H2: cmp ?y ?z = ?c |- _] =>
exploit (TRANS c x y z); eauto; (idtac; []; i)
end.
Ltac comp_sym comp sym :=
match goal with
| [ |- context[match comp ?a ?b with _ => _ end]] =>
rewrite (sym a b); destruct (comp a b)
end; ss.
Section LISTS.
Fixpoint lexico_order (x0: list lazy_comparison): comparison :=
match x0 with
| [] => Eq
| hd :: tl =>
match (hd tt) with
| Eq => lexico_order tl
| Lt => Lt
| Gt => Gt
end
end
.
Definition comparison_trans (a b: comparison): option comparison :=
match a, b with
| Lt, Lt => Some Lt
| Eq, Eq => Some Eq
| Gt, Gt => Some Gt
| _, Eq => Some a
| Eq, _ => Some b
| _, _ => None
end.
Lemma comparison_trans_spec
x y c
:
comparison_trans x y = Some c <->
((x = Eq /\ y = c)
\/ (x = c /\ y = Eq)
\/ (x = c /\ y = c))
.
Proof.
red.
split; ii.
- destruct x, y; ss; clarify;
try (by left; ss); right; try (by left; ss); right; ss.
- des; clarify; ss; destruct c; ss.
Qed.
Lemma comparison_trans_same
c
:
<<SAME: comparison_trans c c = Some c>>
.
Proof. destruct c; ss. Qed.
Lemma comparison_trans_any_lt_result
c0 c1
(LT: comparison_trans c0 Lt = Some c1)
:
c1 = Lt
.
Proof. destruct c0; ss; clarify. Qed.
Lemma comparison_trans_lt_any_result
c0 c1
(LT: comparison_trans Lt c0 = Some c1)
:
c1 = Lt
.
Proof. destruct c0; ss; clarify. Qed.
Lemma comparison_trans_any_gt_result
c0 c1
(GT: comparison_trans c0 Gt = Some c1)
:
c1 = Gt
.
Proof. destruct c0; ss; clarify. Qed.
Lemma comparison_trans_gt_any_result
c0 c1
(GT: comparison_trans Gt c0 = Some c1)
:
c1 = Gt
.
Proof. destruct c0; ss; clarify. Qed.
Definition compare_list X (compare: X -> X -> comparison) :=
fix compare_list_ (a0 b0: list X): comparison :=
match a0, b0 with
| a :: a1, b :: b1 => lexico_order [fun _ => (compare a b) ; fun _ => (compare_list_ a1 b1)]
| [], [] => Eq
| [], _ => Lt
| _, [] => Gt
end
.
Lemma compare_list_sym
X l0
(compare: X -> X -> comparison)
(IHLIST: Forall (fun x => forall y, compare y x = CompOpp (compare x y)) l0)
l1
:
<<SYM: compare_list compare l1 l0 = CompOpp (compare_list compare l0 l1)>>
.
Proof.
red.
generalize dependent l1.
revert IHLIST.
induction l0; ii; ss; des_ifs_safe.
- des_ifs.
- ss. inv IHLIST.
erewrite H1. clear H1.
abstr (compare a x) QQ0. clear_tac.
erewrite IHl0; ss. clear IHl0.
abstr (compare_list compare l0 l2) QQ1. clear_tac.
des_ifs.
Qed.
Lemma compare_list_sym'
X l0 l1
(compare: X -> X -> comparison)
(IHLIST: forall x y, compare y x = CompOpp (compare x y))
:
<<SYM: compare_list compare l1 l0 = CompOpp (compare_list compare l0 l1)>>
.
Proof.
apply compare_list_sym.
induction l0; econs; eauto.
Qed.
Lemma compare_list_trans
X l0
(compare: X -> X -> comparison)
(IHLIST:
Forall
(fun x => forall z y c,
comparison_trans (compare x y) (compare y z) = Some c -> compare x z = c) l0)
:
<<TRANS: forall l2 l1 c,
comparison_trans (compare_list compare l0 l1) (compare_list compare l1 l2) = Some c ->
compare_list compare l0 l2 = c>>
.
Proof.
{
ginduction l0; ii; ss.
{ des_ifs; ss; clarify; [].
des_ifs. }
{ inv IHLIST.
destruct l1, l2; ss; clarify.
{ des_ifs; ss; clarify. }
rename H2 into IHONE.
rename H3 into IHLIST.
rename x into y.
rename a into x.
rename x0 into z.
specialize (IHONE z y).
apply comparison_trans_spec in H; des; ss.
- destruct (compare x y) eqn:CMP0;
destruct (compare y z) eqn:CMP1;
try (expl IHONE (ss; eauto)); try rewrite IHONE0; ss; [].
destruct (compare_list compare l0 l1) eqn:CMPL0;
destruct (compare_list compare l1 l2) eqn:CMPL1;
try (by (erewrite IHl0; ss; eauto));
try (erewrite IHl0; ss; [|rewrite CMPL0; rewrite CMPL1]; ss; eauto).
- destruct (compare x y) eqn:CMP0;
destruct (compare y z) eqn:CMP1;
try (expl IHONE (ss; eauto)); try rewrite IHONE0; ss; [].
destruct (compare_list compare l0 l1) eqn:CMPL0;
destruct (compare_list compare l1 l2) eqn:CMPL1;
try (by (erewrite IHl0; ss; eauto));
try (erewrite IHl0; ss; [|rewrite CMPL0; rewrite CMPL1]; ss; eauto).
- destruct (compare x y) eqn:CMP0;
destruct (compare y z) eqn:CMP1; clarify;
try (expl IHONE (ss; eauto)); try rewrite IHONE0; ss; [].
destruct (compare_list compare l0 l1) eqn:CMPL0;
destruct (compare_list compare l1 l2) eqn:CMPL1;
try (by (erewrite IHl0; ss; eauto));
try (erewrite IHl0; ss; [|rewrite CMPL0; rewrite CMPL1]; ss; eauto).
}
}
Unshelve.
all: ss.
Qed.
Lemma compare_list_leibniz
X
(compare: X -> X -> comparison)
(CMP_LEIBNIZ: forall a b, compare a b = Eq -> a = b)
l1 l2
(CMPL_EQ: compare_list compare l1 l2 = Eq)
: l1 = l2.
Proof.
generalize dependent l2.
induction l1.
- intros. simpl in *. des_ifs.
- intros. simpl in *. des_ifs.
exploit CMP_LEIBNIZ; eauto. i. subst.
exploit IHl1; eauto. i. subst.
eauto.
Qed.
Lemma compare_list_leibniz'
X l1 l2
(compare: X -> X -> comparison)
(CMP_LEIBNIZ: Forall (fun a => forall b, compare a b = Eq -> a = b) l1)
(CMPL_EQ: compare_list compare l1 l2 = Eq)
: l1 = l2.
Proof.
generalize dependent l2.
induction l1.
- intros. simpl in *. des_ifs.
- intros. simpl in *. des_ifs.
inversion CMP_LEIBNIZ. subst.
match goal with
| [H: forall b, compare a b = Eq -> a = b |- _] =>
exploit H; eauto; i; subst
end.
exploit IHl1; eauto. i. subst.
eauto.
Qed.
Lemma compare_list_trans'
X compare
(COMP_LEIBNIZ: forall x y, compare x y = Eq -> x = y)
(COMP_REFL: forall x, compare x x = Eq)
(COMP_TRANS: forall c (x y z:X), compare x y = c -> compare y z = c -> compare x z = c)
: forall c l1 l2 l3, compare_list compare l1 l2 = c ->
compare_list compare l2 l3 = c ->
compare_list compare l1 l3 = c.
Proof.
intros c' l1. revert c'.
induction l1.
{ simpl. intros. des_ifs. }
simpl. intros.
des_ifs; simpl in *; des_ifs;
try by (solve_leibniz_ compare COMP_LEIBNIZ;
apply_trans_ (compare_list compare) (fun c (x y z:list X) => @IHl1 c y z);
apply_trans_ compare COMP_TRANS;
finish_refl_ compare COMP_REFL; congruence).
Qed.
Lemma compare_list_trans''
X
(compare: X -> X -> comparison)
(COMP_LEIBNIZ: forall x y, compare x y = Eq -> x = y)
(COMP_REFL: forall x, compare x x = Eq)
c l0 l1 l2
(IHLIST:
Forall
(fun x => forall c y z,
compare x y = c -> compare y z = c -> compare x z = c) l0)
:
<<TRANS:
compare_list compare l0 l1 = c -> compare_list compare l1 l2 = c ->
compare_list compare l0 l2 = c>>
.
Proof.
revert c l1 l2.
induction l0; ii.
{ simpl in *. des_ifs. }
destruct l1, l2; simpl in *; try congruence.
inv IHLIST.
des_ifs;
try by (solve_leibniz_ compare COMP_LEIBNIZ;
apply_trans_ (compare_list compare) (fun c (x y z:list X) => @IHl0 H4 c y z);
apply_trans_ compare (fun c (x y z:X) => H3 c y z);
finish_refl_ compare COMP_REFL; try congruence).
Qed.
Lemma compare_list_refl
X compare
(COMP_REFL: forall (x:X), compare x x = Eq)
: forall l, compare_list compare l l = Eq.
Proof.
intros l. induction l.
- eauto.
- simpl. rewrite COMP_REFL.
rewrite IHl. eauto.
Qed.
End LISTS.
(* TODO: move to TODO.v? *)
(* Fixpoint map2 A B C (f: A -> B -> C) (a0: list A) (b0: list B) := *)
(* match a0, b0 with *)
(* | a :: a1, b :: b1 => (f a b) :: (map2 f a1 b1) *)
(* | _, _ => [] *)
(* end *)
(* . *)
(* Anyhow, besides mutual-fixpoint problem, we cannot use map2 for lexico_order *)
(* lexico_order (map2 [] [1]) will give Eq *)
(* This should hold: map2 [] [1] <> map2 [1] [] <> map2 [] []. *)
(* So simple option type will not rescue here *)
Module Type AltUsual.
Parameter t: Type.
Parameter compare: t -> t -> comparison.
Parameter compare_sym: forall
x y
,
<<SYM: compare y x = CompOpp (compare x y)>>
.
Parameter compare_trans: forall
(* x y z c (* c should come at the end, to make "specialize" easy *) *)
c x y z (* for compatibility with Alt *)
(XY: compare x y = c)
(YZ: compare y z = c)
,
<<XZ: compare x z = c>>
.
Parameter compare_leibniz: forall
x y
(EQ: compare x y = Eq)
,
x = y
.
(* Parameter eq_dec : forall *)
(* (x y:t) *)
(* , *)
(* {x = y} + {x <> y}. *)
End AltUsual.
Module AltFacts (E: OrdersAlt.OrderedTypeAlt).
(* Include E. *)
(* TODO: This prohibits including "EOrigFacts" later. Is there smarter way to do this? *)
Module EOrig := OrdersAlt.OT_from_Alt E.
(* Module EOrigFull <: OrderedTypeFull := (OT_to_Full EOrig). *)
Module EOrigFacts := OrdersFacts.OrderedTypeFacts EOrig.
(* Import EOrigFacts.OrderTac. *)
Include EOrigFacts.
Lemma compare_eq_any_trans
c x y z
(XY: E.compare x y = Eq)
(YZ: E.compare y z = c)
:
<<XZ: E.compare x z = c>>
.
Proof.
red.
destruct c; try EOrigFacts.order.
- erewrite EOrigFacts.eq_trans; eauto.
- erewrite EOrigFacts.OrderTac.eq_lt; eauto.
- repeat rewrite EOrigFacts.compare_gt_iff in *.
erewrite EOrigFacts.OrderTac.lt_eq; eauto.
apply EOrigFacts.eq_sym; ss.
Qed.
Lemma compare_any_eq_trans
c x y z
(XY: E.compare x y = c)
(YZ: E.compare y z = Eq)
:
<<XZ: E.compare x z = c>>
.
Proof.
red.
destruct c; try EOrigFacts.order.
- erewrite EOrigFacts.eq_trans; eauto.
- erewrite EOrigFacts.OrderTac.lt_eq; eauto.
- repeat rewrite EOrigFacts.compare_gt_iff in *.
erewrite EOrigFacts.OrderTac.eq_lt; eauto.
apply EOrigFacts.eq_sym; ss.
Qed.
End AltFacts.
Module Alt_from_AltUsual (E: AltUsual) <: OrdersAlt.OrderedTypeAlt := E.
Module OT_from_AltUsual (E:AltUsual) <: Orders.OrderedType.
Module EAlt := Alt_from_AltUsual E.
Include OrdersAlt.OT_from_Alt EAlt.
Lemma eq_leibniz : forall x y, eq x y <-> x = y.
Proof.
i. split.
- apply E.compare_leibniz.
- i. hexploit (E.compare_sym x y). red. i.
subst. destruct (E.compare y y); ss.
Qed.
Lemma eq_dec_l : forall x y:t, {x = y} + {x <> y}.
Proof.
i. destruct (eq_dec x y).
- left. apply eq_leibniz. ss.
- right. ii. apply n. apply eq_leibniz. ss.
Qed.
End OT_from_AltUsual.
(* Module Alt_from_AltUsual (E: AltUsual) <: OrdersAlt.OrderedTypeAlt := E. *)
Module AltUsualFacts (E: AltUsual). (* <: (AltFacts E). *)
(* TODO: How to check subtype of these? forall E, AltUsualFacts E >= AltFacts E*)
(* Module EAlt <: OrdersAlt.OrderedTypeAlt := Alt_from_AltUsual E. *)
(* Module EAltFacts := (AltFacts E). *)
(* Include EAlt. *)
(* Include EAltFacts. *)
Include E.
Include AltFacts E.
Lemma eq_repl_l
x y
(EQ: compare x y = Eq)
z
:
compare x z = compare y z
.
Proof. destruct (compare y z) eqn:T; eapply compare_eq_any_trans; eauto. Qed.
Lemma eq_repl_r
x y
(EQ: compare y x = Eq)
z
:
compare z x = compare z y
.
Proof.
(* apply EAltFacts.eq_sym in EQ. *)
destruct (compare z y) eqn:T; eapply compare_any_eq_trans; eauto.
Qed.
Lemma weird_lemma
x y z
(EQ: compare y x = compare z y)
:
compare z x = compare z y
.
Proof.
destruct (compare z y) eqn:T; eapply compare_trans; eauto.
Qed.
Lemma eq_dec_l
(x y : t)
: {x = y} + {x <> y}.
Proof.
destruct (eq_dec x y).
- eauto using compare_leibniz.
- right. ii. subst.
eauto using (compare_refl y).
Qed.
End AltUsualFacts.
(* I write "Orders.OrderedType" in order to not confuse with "OrderedType.OrderedType" *)
(* This is weird... I think it is impossible to define OrderedType X => OrderedType (option X) *)
(* because OrderedType's lt's irrefl is defined with leibniz eq, not eq of that module *)
Module option_orig (E: Orders.OrderedType) <: Orders.OrderedType.
Module EAlt <: OrdersAlt.OrderedTypeAlt := OrdersAlt.OT_to_Alt E.
Import EAlt. (* To get compare_trans *)
Module EFull <: OrderedTypeFull := (OT_to_Full E).
(* Module EFullTotal <: TotalOrder := OTF_to_TotalOrder EFull. *)
(* Module ETac := OrdersTac.MakeOrderTac EFull EFullTotal. *)
Module EFacts := OrdersFacts.OrderedTypeFacts EFull.
(* OrdersFacts.OrderedTypeFullFacts <--------- What is this for? *)
Import EFacts.
(* ETac is included in EFacts *)
Definition t := option E.t.
Definition eq (x y: t) :=
match x, y with
| Some x_, Some y_ => E.eq x_ y_
| None, None => True
| _, _ => False
end.
Definition eq_dec (x y:t): {eq x y} + {~ eq x y}.
destruct x, y; ss.
- apply E.eq_dec.
- right; ii; ss.
- right; ii; ss.
- left; ss.
Defined.
Global Program Instance eq_equiv : Equivalence eq.
Next Obligation.
ii. destruct x; ss. apply eq_refl; ss.
Qed.
Next Obligation.
ii. destruct x; ss.
des_ifs. ss. apply eq_sym; ss.
Qed.
Next Obligation.
ii. destruct x; ss; des_ifs; ss.
eapply eq_trans; eauto.
Qed.
Definition compare (x y: t): comparison :=
match x, y with
| Some x_, Some y_ => E.compare x_ y_
| None, Some _ => Lt
| Some _, None => Gt
| None, None => Eq
end.
Definition lt (x y: t): Prop := compare x y = Lt.
Lemma compare_spec (x y: t):
CompareSpec (eq x y) (lt x y) (lt y x) (compare x y).
Proof.
ii. destruct x, y; ss; try (by econs).
hexploit E.compare_spec; eauto; []; intro SPEC; des.
destruct (E.compare t0 t1) eqn:T0;
rewrite T0 in *; inv SPEC; econs; ss.
-
compute. eapply compare_lt_iff; ss.
Qed.
Global Program Instance lt_strorder : StrictOrder lt.
Next Obligation.
ii. unfold lt in *. destruct x; ss.
apply compare_lt_iff in H. order.
Qed.
Global Program Instance lt_compat : Proper (eq ==> eq ==> iff) lt.
Next Obligation.
ii.
unfold eq in *. des_ifs.
unfold lt in *. ss. repeat rewrite compare_lt_iff in *.
split; ii; clarify; order.
Qed.
Next Obligation.
intros x y z LTXY LTYZ. unfold lt in *.
destruct x, y, z; ss.
SearchAbout (EAlt.compare).
SearchAbout (E.compare). (* Can't we search at once?? *)
eapply EAlt.compare_trans; eauto.
Qed.
End option_orig.
Module option (E: AltUsual) <: AltUsual.
Definition t := option E.t.
Definition compare (x y: t): comparison :=
match x, y with
| Some x_, Some y_ => E.compare x_ y_
| None, Some _ => Lt
| Some _, None => Gt
| None, None => Eq
end.
Lemma compare_sym
x y
:
<<SYM: compare y x = CompOpp (compare x y)>>
.
Proof. destruct x, y; ss. apply E.compare_sym. Qed.
Lemma compare_trans: forall
c x y z (* for compatibility with Alt *)
(XY: compare x y = c)
(YZ: compare y z = c)
,
<<XZ: compare x z = c>>
.
Proof.
destruct x, y, z, c; ss; apply E.compare_trans; ss.
Qed.
Lemma compare_leibniz: forall
x y
(EQ: compare x y = Eq)
,
x = y
.
Proof.
destruct x, y; ss. intro. f_equal. apply E.compare_leibniz; ss.
Qed.
End option.
Module optionFacts (E: AltUsual) := AltUsualFacts E.
(* Module prod_ord (E1 E2: Orders.OrderedType). *)
(* Definition t : Type := E1.t * E2.t. *)
(* Definition eq v1 v2 := *)
(* E1.eq (fst v1) (fst v2) /\ E2.eq (snd v1) (snd v2). *)
(* Lemma eq_equiv : Equivalence eq. *)
(* Proof. *)
(* unfold eq. *)
(* split; ii. *)
(* - split; try apply Equivalence_Reflexive. *)
(* - des. split; apply Equivalence_Symmetric; eauto. *)
(* - des. split; eapply Equivalence_Transitive; eauto. *)
(* Qed. *)
(* Definition lt (x y:t) : Prop := *)
(* E1.lt (fst x) (fst y) \/ *)
(* (E1.eq (fst x) (fst y) /\ E2.lt (snd x) (snd y)). *)
(* Lemma lt_strorder : StrictOrder lt. *)
(* Proof. *)
(* unfold lt. constructor. *)
(* - ii. des. *)
(* + eapply (@StrictOrder_Irreflexive _ _ E1.lt_strorder); eauto. *)
(* + eapply (@StrictOrder_Irreflexive _ _ E2.lt_strorder); eauto. *)
(* - ii. des. *)
(* + left. eapply (@StrictOrder_Transitive _ _ E1.lt_strorder); eauto. *)
(* + left. *)
(* apply StrictOrder_Irreflexive. *)
(* Orders.OrderedType *)
Module floating_point <: AltUsual.
Definition t := floating_point.
Definition case_ord(x: t): OrdIdx.t :=
match x with
| fp_float => 0
| fp_double => 1
| fp_fp128 => 2
| fp_x86_fp80 => 3
| fp_ppc_fp128 => 4
end
.
Definition compare (x y: t): comparison := OrdIdx.compare (case_ord x) (case_ord y).
Lemma compare_sym
x y
:
<<SYM: compare y x = CompOpp (compare x y)>>
.
Proof. destruct x, y; ss. Qed.
Lemma compare_trans: forall
c x y z (* for compatibility with Alt *)
(XY: compare x y = c)
(YZ: compare y z = c)
,
<<XZ: compare x z = c>>
.
Proof.
destruct x, y, z, c; ss.
Qed.
Lemma compare_leibniz: forall
x y
(EQ: compare x y = Eq)
,
x = y
.
Proof.
destruct x, y; ss.
Qed.
End floating_point.
Module floating_pointFacts := AltUsualFacts floating_point.
(* Module floating_pointFacts := OrdersFacts.OrderedTypeFacts floating_point. *)
(* TODO: Can I define it inside original module? *)
(* just make a wrapper for UsualOrderedType? *)
Module UOT_to_AltU (E: Orders.UsualOrderedType) <: AltUsual.
Import E.
Module EAlt := OrdersAlt.OT_to_Alt E.
Include EAlt.
Lemma compare_leibniz: forall
x y
(EQ: compare x y = Eq)
,
x = y
.
Proof.
ii.
apply EAlt.compare_Eq. ss.
Qed.
End UOT_to_AltU.
Module NatAltUsual := UOT_to_AltU Nat.
Module NatAltUsualFacts := AltUsualFacts NatAltUsual.
Ltac to_nat_alt_compare :=
repeat match goal with
| [ H: context[OrdIdx.compare ?a ?b] |- _ ] =>
replace (OrdIdx.compare a b) with (NatAltUsual.compare a b) in H by ss
| [ |- context[OrdIdx.compare ?a ?b] ] =>
replace (OrdIdx.compare a b) with (NatAltUsual.compare a b) by ss
end.
(* I Really want to remove this tactic ... *)
Module sz <: AltUsual.
Definition t := sz.
Definition compare (x y:t) : comparison := OrdIdx.compare x y.
Lemma compare_sym
x y
:
<<SYM: compare y x = CompOpp (compare x y)>>
.
Proof. apply Nat.compare_antisym. Qed.
Lemma compare_leibniz: forall
x y
(EQ: compare x y = Eq)
,
x = y
.
Proof. apply Nat.compare_eq. Qed.
Lemma compare_trans: forall
c x y z (* for compatibility with Alt *)
(XY: compare x y = c)
(YZ: compare y z = c)
,
<<XZ: compare x z = c>>
.
Proof.
intros.
destruct c.
- apply nat_compare_eq in XY.
apply nat_compare_eq in YZ.
subst. apply Nat.compare_refl.
- rewrite <- nat_compare_lt in *.
red. rewrite <- nat_compare_lt. omega.
- rewrite <- nat_compare_gt in *.
red. rewrite <- nat_compare_gt. omega.
Qed.
End sz.
Module szFacts := AltUsualFacts sz.
Module varg <: AltUsual := option sz.
Module vargFacts := optionFacts varg. (* or AltUsualFacts varg directly? *)
Module Float <: AltUsual.
(* Floats.float *)
Definition t := Float.
(* Integers.Int.int *)
(* Floats.Float.to_bits *)
(* Floats.Float *)
(* Floats.Float.cmp *)
Definition compare (x y: t): comparison := wrap_compare (FLOAT.compare) x y.
Lemma compare_leibniz: forall
x y
(EQ: compare x y = Eq)
,
x = y
.
Proof. apply FLOAT.compare_leibniz; eauto. Qed.
Lemma compare_sym
x y
:
<<SYM: compare y x = CompOpp (compare x y)>>
.
Proof. apply FLOAT.compare_sym; eauto. Qed.
Lemma compare_trans: forall
c x y z
(XY: compare x y = c)
(YZ: compare y z = c)
,
<<XZ: compare x z = c>>
.
Proof. apply FLOAT.compare_trans; eauto. Qed.
End Float.
Module FloatFacts := AltUsualFacts Float.
Module Int <: AltUsual.
(* Floats.float *)
Definition t := Int.
(* Integers.Int.int *)
(* Floats.Float.to_bits *)
(* Floats.Float *)
(* Floats.Float.cmp *)
Definition compare (x y: t): comparison := Z.compare x y.
Lemma compare_leibniz: forall
x y
(EQ: compare x y = Eq)
,
x = y
.
Proof. exact Z.compare_eq. Qed.
Lemma compare_sym
x y
:
<<SYM: compare y x = CompOpp (compare x y)>>
.
Proof. rewrite Zcompare_antisym. reflexivity. Qed.
Lemma compare_trans: forall
c x y z
(XY: compare x y = c)
(YZ: compare y z = c)
,
<<XZ: compare x z = c>>