1
(***********************************************************************)
2
(* v * The Coq Proof Assistant / The Coq Development Team *)
3
(* <O___,, * INRIA-Rocquencourt & LRI-CNRS-Orsay *)
4
(* \VV/ *************************************************************)
5
(* // * This file is distributed under the terms of the *)
6
(* * GNU Lesser General Public License Version 2.1 *)
7
(***********************************************************************)
9
(* Finite sets library.
10
* Authors: Pierre Letouzey and Jean-Christophe Filliâtre
11
* Institution: LRI, CNRS UMR 8623 - Université Paris Sud
12
* 91405 Orsay, France *)
14
(* $Id: OrderedTypeEx.v 11699 2008-12-18 11:49:08Z letouzey $ *)
16
Require Import OrderedType.
17
Require Import ZArith.
19
Require Import NArith Ndec.
20
Require Import Compare_dec.
22
(** * Examples of Ordered Type structures. *)
24
(** First, a particular case of [OrderedType] where
25
the equality is the usual one of Coq. *)
27
Module Type UsualOrderedType.
28
Parameter Inline t : Type.
29
Definition eq := @eq t.
30
Parameter Inline lt : t -> t -> Prop.
31
Definition eq_refl := @refl_equal t.
32
Definition eq_sym := @sym_eq t.
33
Definition eq_trans := @trans_eq t.
34
Axiom lt_trans : forall x y z : t, lt x y -> lt y z -> lt x z.
35
Axiom lt_not_eq : forall x y : t, lt x y -> ~ eq x y.
36
Parameter compare : forall x y : t, Compare lt eq x y.
37
Parameter eq_dec : forall x y : t, { eq x y } + { ~ eq x y }.
40
(** a [UsualOrderedType] is in particular an [OrderedType]. *)
42
Module UOT_to_OT (U:UsualOrderedType) <: OrderedType := U.
44
(** [nat] is an ordered type with respect to the usual order on natural numbers. *)
46
Module Nat_as_OT <: UsualOrderedType.
50
Definition eq := @eq nat.
51
Definition eq_refl := @refl_equal t.
52
Definition eq_sym := @sym_eq t.
53
Definition eq_trans := @trans_eq t.
57
Lemma lt_trans : forall x y z : t, lt x y -> lt y z -> lt x z.
58
Proof. unfold lt in |- *; intros; apply lt_trans with y; auto. Qed.
60
Lemma lt_not_eq : forall x y : t, lt x y -> ~ eq x y.
61
Proof. unfold lt, eq in |- *; intros; omega. Qed.
63
Definition compare : forall x y : t, Compare lt eq x y.
65
intros; case (lt_eq_lt_dec x y).
66
simple destruct 1; intro.
69
intro; constructor 3; auto.
72
Definition eq_dec := eq_nat_dec.
77
(** [Z] is an ordered type with respect to the usual order on integers. *)
79
Open Local Scope Z_scope.
81
Module Z_as_OT <: UsualOrderedType.
84
Definition eq := @eq Z.
85
Definition eq_refl := @refl_equal t.
86
Definition eq_sym := @sym_eq t.
87
Definition eq_trans := @trans_eq t.
89
Definition lt (x y:Z) := (x<y).
91
Lemma lt_trans : forall x y z, x<y -> y<z -> x<z.
92
Proof. intros; omega. Qed.
94
Lemma lt_not_eq : forall x y, x<y -> ~ x=y.
95
Proof. intros; omega. Qed.
97
Definition compare : forall x y, Compare lt eq x y.
99
intros x y; case_eq (x ?= y); intros.
100
apply EQ; unfold eq; apply Zcompare_Eq_eq; auto.
101
apply LT; unfold lt, Zlt; auto.
102
apply GT; unfold lt, Zlt; rewrite <- Zcompare_Gt_Lt_antisym; auto.
105
Definition eq_dec := Z_eq_dec.
109
(** [positive] is an ordered type with respect to the usual order on natural numbers. *)
111
Open Local Scope positive_scope.
113
Module Positive_as_OT <: UsualOrderedType.
114
Definition t:=positive.
115
Definition eq:=@eq positive.
116
Definition eq_refl := @refl_equal t.
117
Definition eq_sym := @sym_eq t.
118
Definition eq_trans := @trans_eq t.
120
Definition lt p q:= (p ?= q) Eq = Lt.
122
Lemma lt_trans : forall x y z : t, lt x y -> lt y z -> lt x z.
124
unfold lt; intros x y z.
125
change ((Zpos x < Zpos y)%Z -> (Zpos y < Zpos z)%Z -> (Zpos x < Zpos z)%Z).
129
Lemma lt_not_eq : forall x y : t, lt x y -> ~ eq x y.
134
rewrite Pcompare_refl in H; discriminate.
137
Definition compare : forall x y : t, Compare lt eq x y.
140
case_eq ((x ?= y) Eq); intros.
141
apply EQ; apply Pcompare_Eq_eq; auto.
142
apply LT; unfold lt; auto.
144
replace Eq with (CompOpp Eq); auto.
145
rewrite <- Pcompare_antisym; rewrite H; auto.
148
Definition eq_dec : forall x y, { eq x y } + { ~ eq x y }.
150
intros; unfold eq; decide equality.
156
(** [N] is an ordered type with respect to the usual order on natural numbers. *)
158
Open Local Scope positive_scope.
160
Module N_as_OT <: UsualOrderedType.
162
Definition eq:=@eq N.
163
Definition eq_refl := @refl_equal t.
164
Definition eq_sym := @sym_eq t.
165
Definition eq_trans := @trans_eq t.
167
Definition lt p q:= Nleb q p = false.
169
Definition lt_trans := Nltb_trans.
171
Lemma lt_not_eq : forall x y : t, lt x y -> ~ eq x y.
176
rewrite Nleb_refl in H; discriminate.
179
Definition compare : forall x y : t, Compare lt eq x y.
182
case_eq ((x ?= y)%N); intros.
183
apply EQ; apply Ncompare_Eq_eq; auto.
184
apply LT; unfold lt; auto.
185
generalize (Nleb_Nle y x).
186
unfold Nle; rewrite <- Ncompare_antisym.
187
destruct (x ?= y)%N; simpl; try discriminate.
189
destruct (Nleb y x); intuition.
191
generalize (Nleb_Nle x y).
192
unfold Nle; destruct (x ?= y)%N; simpl; try discriminate.
193
destruct (Nleb x y); intuition.
196
Definition eq_dec : forall x y, { eq x y } + { ~ eq x y }.
198
intros. unfold eq. decide equality. apply Positive_as_OT.eq_dec.
204
(** From two ordered types, we can build a new OrderedType
205
over their cartesian product, using the lexicographic order. *)
207
Module PairOrderedType(O1 O2:OrderedType) <: OrderedType.
208
Module MO1:=OrderedTypeFacts(O1).
209
Module MO2:=OrderedTypeFacts(O2).
211
Definition t := prod O1.t O2.t.
213
Definition eq x y := O1.eq (fst x) (fst y) /\ O2.eq (snd x) (snd y).
216
O1.lt (fst x) (fst y) \/
217
(O1.eq (fst x) (fst y) /\ O2.lt (snd x) (snd y)).
219
Lemma eq_refl : forall x : t, eq x x.
221
intros (x1,x2); red; simpl; auto.
224
Lemma eq_sym : forall x y : t, eq x y -> eq y x.
226
intros (x1,x2) (y1,y2); unfold eq; simpl; intuition.
229
Lemma eq_trans : forall x y z : t, eq x y -> eq y z -> eq x z.
231
intros (x1,x2) (y1,y2) (z1,z2); unfold eq; simpl; intuition eauto.
234
Lemma lt_trans : forall x y z : t, lt x y -> lt y z -> lt x z.
236
intros (x1,x2) (y1,y2) (z1,z2); unfold eq, lt; simpl; intuition.
238
left; eapply MO1.lt_eq; eauto.
239
left; eapply MO1.eq_lt; eauto.
243
Lemma lt_not_eq : forall x y : t, lt x y -> ~ eq x y.
245
intros (x1,x2) (y1,y2); unfold eq, lt; simpl; intuition.
246
apply (O1.lt_not_eq H0 H1).
247
apply (O2.lt_not_eq H3 H2).
250
Definition compare : forall x y : t, Compare lt eq x y.
251
intros (x1,x2) (y1,y2).
252
destruct (O1.compare x1 y1).
253
apply LT; unfold lt; auto.
254
destruct (O2.compare x2 y2).
255
apply LT; unfold lt; auto.
256
apply EQ; unfold eq; auto.
257
apply GT; unfold lt; auto.
258
apply GT; unfold lt; auto.
261
Definition eq_dec : forall x y : t, {eq x y} + {~ eq x y}.
263
intros; elim (compare x y); intro H; [ right | left | right ]; auto.
264
auto using lt_not_eq.
265
assert (~ eq y x); auto using lt_not_eq, eq_sym.