-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathordered.v
105 lines (80 loc) · 2.8 KB
/
ordered.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
Require Export Coq.Classes.RelationClasses.
Set Default Goal Selector "all".
Class Ordered(A : Set) :=
{ lt : A -> A -> Prop;
lt_strict :> StrictOrder lt
}.
Class OrderedKeyed (A K : Set) :=
{ keyof : A -> K;
OA :> Ordered A
}.
Class KeyOrdered (A K : Set) :=
{ getkey : A -> K;
OK :> Ordered K
}.
Definition KOlt{A K : Set}`{KeyOrdered A K}(a b : A) : Prop := lt (getkey a) (getkey b).
Instance KOisO A K `{KeyOrdered A K} : Ordered A.
Proof.
destruct (_:KeyOrdered A K) as [gk [ltk [SOI SOT]]]. unshelve eexists.
- intros a b. exact (ltk (gk a) (gk b)).
- split.
+ unfold Irreflexive, Reflexive, complement in *. intro. apply SOI.
+ unfold Transitive in *. intros x y z. apply SOT.
Defined.
Instance KOisOK A K `{KeyOrdered A K} : OrderedKeyed A K :=
{ keyof := getkey }.
Class ComparableKeyed (A K : Set) :=
{ OKOK :> OrderedKeyed A K;
compare : K -> K -> comparison;
compare_spec x y: CompareSpecT (eq x y)
(forall (a b : A), x = keyof a -> y = keyof b -> lt a b)
(forall (a b : A), x = keyof a -> y = keyof b -> lt b a)
(compare x y);
lt_same_keys w x y z: lt w y -> keyof x = keyof w -> keyof z = keyof y -> lt x z
}.
Require Coq.Init.Nat.
Require Coq.Arith.PeanoNat.
Module Test.
Import Nat.
Import PeanoNat.
Open Scope nat_scope.
Context {A : Set}.
Context {Ord : Ordered A}.
Record OK : Set := { val : A; key : nat }.
Definition OKlt (a b : OK) : Prop := a.(key) < b.(key).
Lemma le_Sn_n : forall n, S n <= n -> False.
Proof.
induction n as [|? IHn]. intro H.
- inversion H.
- apply IHn. apply le_S_n. assumption.
Qed.
Lemma OKlt_strict : StrictOrder OKlt.
Proof.
eexists. red.
- red. red. intros [v k]. cbv. apply le_Sn_n.
- intros [? ?] [? ykey] [? ?]. unfold OKlt, key. transitivity ykey. assumption.
Qed.
Instance OKOrd : Ordered OK := { lt := OKlt; lt_strict := OKlt_strict }.
Lemma OKOrd_compare_spec (x y:nat) :
CompareSpecT (eq x y)
(forall (a b : OK), x = a.(key) -> y = b.(key) -> OKlt a b)
(forall (a b : OK), x = a.(key) -> y = b.(key) -> OKlt b a)
(Nat.compare x y).
Proof.
destruct (CompareSpec2Type (Nat.compare_spec x y)). constructor.
- assumption.
- intros ? ? -> -> . assumption.
- intros ? ? -> -> . assumption.
Qed.
Lemma OKOrd_lt_same_keys w x y z :
OKlt w y -> x.(key) = w.(key) -> z.(key) = y.(key) -> OKlt x z.
Proof.
unfold OKlt. intros H -> -> . assumption.
Qed.
Instance OKnat : OrderedKeyed OK nat := { keyof := key }.
Instance CKnat : ComparableKeyed OK nat :=
{ compare := Nat.compare;
compare_spec := OKOrd_compare_spec;
lt_same_keys := OKOrd_lt_same_keys
}.
End Test.