Debian Codegraph @

src/Util/MSets/MSetN.v

raw
1 From Coq Require Import Lia.
2 From Coq Require Import Bool.
3 From Coq Require Import List.
4 From Coq Require Import Orders.
5 From Coq Require Import OrdersEx.
6 From Coq Require Import MSetInterface.
7 From Coq Require Import MSetPositive.
8 From Coq Require Import NArith.
9 Require Import Crypto.Util.MSets.MSetIso.
10 Require Import Crypto.Util.Structures.Equalities.Iso.
11 Require Import Crypto.Util.Structures.Orders.Iso.
12
13 Local Set Implicit Arguments.
14
15 (* TODO: Swap out for the version in Util/Structures/OrdersEx.v, and profile to see if there are any perf implications *)
16 Module NIsoPositive <: IsoOrderedType PositiveSet.E.
17 Definition t := N.
18 Definition eq : t -> t -> Prop := eq.
19 Definition eq_equiv : Equivalence eq := _.
20 Definition eq_dec : forall x y : t, {eq x y} + {~ eq x y} := N.eq_dec.
21 Definition to_ : t -> PositiveSet.E.t := N.succ_pos.
22 Definition of_ : PositiveSet.E.t -> t := Pos.pred_N.
23 Lemma Proper_to_ : Proper (eq ==> PositiveSet.E.eq) to_.
24 Proof. cbv [eq]; repeat intro; subst; reflexivity. Qed.
25 Lemma Proper_of_ : Proper (PositiveSet.E.eq ==> eq) of_.
26 Proof. cbv [PositiveSet.E.eq]; repeat intro; subst; reflexivity. Qed.
27 Lemma of_to : forall x : t, eq (of_ (to_ x)) x.
28 Proof. cbv [eq of_ to_]; intros; now rewrite N.pos_pred_succ. Qed.
29 Lemma to_of : forall x : PositiveSet.E.t, PositiveSet.E.eq (to_ (of_ x)) x.
30 Proof.
31 intro x.
32 cut (N.pos (to_ (of_ x)) = N.pos x); [ congruence | ].
33 cbv [PositiveSet.E.eq of_ to_].
34 rewrite N.pos_pred_spec, N.succ_pos_spec.
35 lia.
36 Qed.
37 Definition lt (x y : t) : Prop := PositiveSet.E.lt (to_ x) (to_ y).
38 Global Instance lt_strorder : StrictOrder lt | 100.
39 Proof.
40 split; hnf; cbv [lt complement]; intros *;
41 apply PositiveSet.E.lt_strorder.
42 Qed.
43 Definition lt_compat : Proper (eq ==> eq ==> iff) lt := _.
44 Definition compare (x y : t) : comparison := PositiveSet.E.compare (to_ x) (to_ y).
45 Lemma compare_spec (x y : t) : CompareSpec (eq x y) (lt x y) (lt y x) (compare x y).
46 Proof.
47 cbv [eq lt compare].
48 destruct (PositiveSet.E.compare_spec (to_ x) (to_ y)); constructor; try assumption.
49 cbv [PositiveSet.E.eq] in *.
50 rewrite <- (of_to x), <- (of_to y).
51 congruence.
52 Qed.
53 Global Instance Proper_to_lt : Proper (lt ==> PositiveSet.E.lt) to_ | 100.
54 Proof. cbv [lt Proper respectful]; trivial. Qed.
55 Global Instance Proper_of_lt : Proper (PositiveSet.E.lt ==> lt) of_ | 100.
56 Proof. cbv [lt Proper respectful]; intros *; rewrite !to_of; trivial. Qed.
57 End NIsoPositive.
58
59 Module NSet <: S := IsoSets PositiveSet NIsoPositive.