Debian Codegraph @

src/Util/Level.v

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import Lia.
3 From Coq Require Import Relations.
4 From Coq Require Import Morphisms.
5 Require Import Crypto.Util.Bool.Reflect.
6 Require Import Crypto.Util.Decidable.
7
8 Module Level.
9 Local Coercion is_true : bool >-> Sortclass.
10 Local Set Boolean Equality Schemes.
11 Local Set Decidable Equality Schemes.
12 Inductive Level :=
13 | neg_inf
14 | level (lvl : Z)
15 | pos_inf
16 .
17 Notation t := Level (only parsing).
18 Module Export Exports1.
19 Declare Scope level_scope.
20 Delimit Scope level_scope with level.
21 Bind Scope level_scope with Level.
22 Global Coercion level : Z >-> Level.
23 End Exports1.
24 Local Open Scope level_scope.
25
26 Definition of_nat (n : nat) : Level := Z.of_nat n.
27 Definition of_N (n : N) : Level := Z.of_N n.
28 Definition of_pos (n : positive) : Level := Z.pos n.
29
30 Module Export Exports2.
31 Global Coercion of_nat : nat >-> Level.
32 Global Coercion of_N : N >-> Level.
33 Global Coercion of_pos : positive >-> Level.
34 End Exports2.
35
36 (** next tighter level *)
37 Definition next (l : Level) : Level
38 := match l with
39 | neg_inf => neg_inf
40 | level lvl => level (Z.pred lvl)
41 | pos_inf => pos_inf
42 end.
43 (** previous looser level *)
44 Definition prev (l : Level) : Level
45 := match l with
46 | neg_inf => neg_inf
47 | level lvl => level (Z.succ lvl)
48 | pos_inf => pos_inf
49 end.
50
51 Notation eqb := Level_beq.
52 Lemma eqb_eq x y : eqb x y = true <-> x = y.
53 Proof. split; [ apply internal_Level_dec_bl | apply internal_Level_dec_lb ]. Qed.
54 Lemma eqb_neq x y : eqb x y = false <-> x <> y.
55 Proof. rewrite <- eqb_eq; destruct (eqb x y); split; congruence. Qed.
56 Lemma eqb_refl x : eqb x x = true.
57 Proof. rewrite eqb_eq; reflexivity. Qed.
58 Lemma eqb_sym x y : eqb x y = eqb y x.
59 Proof. destruct (eqb x y) eqn:?, (eqb y x) eqn:?; rewrite ?eqb_eq, ?eqb_neq in *; subst; congruence. Qed.
60 Lemma eqb_spec : reflect_rel eq eqb.
61 Proof. intros x y; destruct (eqb x y) eqn:?; constructor; rewrite ?eqb_eq, ?eqb_neq in *; assumption. Qed.
62 Definition dec_eq_Level : DecidableRel (@eq Level) := dec_rel_of_bool_dec_rel eqb internal_Level_dec_bl internal_Level_dec_lb.
63 Lemma eqb_compat : Proper (eq ==> eq ==> eq) eqb.
64 Proof. repeat intro; subst; reflexivity. Qed.
65
66 Definition compare (l1 l2 : Level) : comparison
67 := match l1, l2 with
68 | neg_inf, neg_inf => Eq
69 | neg_inf, _ => Lt
70 | _, neg_inf => Gt
71 | pos_inf, pos_inf => Eq
72 | pos_inf, _ => Gt
73 | _, pos_inf => Lt
74 | level l1, level l2 => Z.compare l1 l2
75 end.
76 Definition ltb (l1 l2 : Level) : bool
77 := match compare l1 l2 with
78 | Lt => true
79 | _ => false
80 end.
81 Definition leb (l1 l2 : Level) : bool
82 := orb (eqb l1 l2) (ltb l1 l2).
83 Definition le : relation Level := leb.
84 Definition lt : relation Level := ltb.
85 Module Export Exports3.
86 Global Arguments le _ _ / .
87 Global Arguments lt _ _ / .
88 Global Arguments ltb !_ !_ / .
89 Global Arguments leb !_ !_ / .
90 Global Arguments compare !_ !_ / .
91 End Exports3.
92
93 Module Import Notations1.
94 Notation "-∞" := neg_inf.
95 Notation "∞" := pos_inf.
96 Infix "?=" := compare : level_scope.
97 Infix "=?" := eqb : level_scope.
98 Infix "<=?" := leb : level_scope.
99 Infix "<?" := ltb : level_scope.
100 Infix "<=" := le : level_scope.
101 Infix "<" := lt : level_scope.
102 Notation "l1 >=? l2" := (leb l2 l1) (only parsing) : level_scope.
103 Notation "l1 >? l2" := (ltb l2 l1) (only parsing) : level_scope.
104 Notation "l1 >= l2" := (le l2 l1) (only parsing) : level_scope.
105 Notation "l1 > l2" := (lt l2 l1) (only parsing) : level_scope.
106 End Notations1.
107 Lemma compare_spec x y : CompareSpec (x = y) (x < y) (y < x) (x ?= y).
108 Proof.
109 destruct x as [|x|], y as [|y|]; cbv [lt ltb compare]; try destruct (Z.compare_spec x y), (Z.compare_spec y x); constructor;
110 subst; try reflexivity; try lia.
111 Qed.
112 Lemma compare_refl x : (x ?= x) = Eq.
113 Proof. destruct x; cbn; try reflexivity; now rewrite Z.compare_refl. Qed.
114 Lemma compare_antisym x y : (y ?= x) = CompOpp (x ?= y).
115 Proof. destruct x, y; cbn; try reflexivity; now rewrite Z.compare_antisym. Qed.
116 Lemma compare_eq x y : (x ?= y) = Eq -> x = y.
117 Proof. destruct x, y; cbn; try congruence; intro; now apply f_equal, Z.compare_eq. Qed.
118 Lemma compare_eq_iff x y : (x ?= y) = Eq <-> x = y.
119 Proof. destruct x, y; cbn; rewrite ?Z.compare_eq_iff; split; congruence. Qed.
120
121 Module Export Notations.
122 Export Notations1.
123 End Notations.
124
125 Module Export Exports.
126 Export Exports1.
127 Export Exports2.
128 Export Exports3.
129 Global Existing Instances eqb_spec dec_eq_Level eqb_compat | 10.
130 End Exports.
131 End Level.
132 Export Level.Notations.
133 Export Level.Exports.
134 Notation Level := Level.Level.
135
136 Inductive Associativity := LeftAssoc | RightAssoc | NoAssoc | FullyAssoc | ExplicitAssoc (l r : Level).