| 1 |
From Coq Require Import Btauto. |
| 2 |
From Coq Require Import Equalities. |
| 3 |
From Coq Require Import Orders. |
| 4 |
Require Import Crypto.Util.Structures.Equalities. |
| 5 |
Require Import Crypto.Util.Tactics.UniquePose. |
| 6 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 7 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 8 |
Require Import Crypto.Util.Tactics.SpecializeAllWays. |
| 9 |
Require Import Crypto.Util.Tactics.SpecializeBy. |
| 10 |
|
| 11 |
Module Type UsualEqLt := UsualEq <+ HasLt. |
| 12 |
Module Type UsualEqLe := UsualEq <+ HasLe. |
| 13 |
Module Type UsualEqLtLe := UsualEq <+ HasLt <+ HasLe. |
| 14 |
|
| 15 |
Module Type UsualEqLt' := UsualEqLt <+ EqNotation <+ LtNotation. |
| 16 |
Module Type UsualEqLe' := UsualEqLe <+ EqNotation <+ LeNotation. |
| 17 |
Module Type UsualEqLtLe' := UsualEqLtLe <+ EqNotation <+ LtNotation <+ LeNotation. |
| 18 |
|
| 19 |
Module Type EqLeBool := Typ <+ HasEqb <+ HasLeb. |
| 20 |
Module Type EqLtBool := Typ <+ HasEqb <+ HasLtb. |
| 21 |
Module Type EqLtLeBool := Typ <+ HasEqb <+ HasLtb <+ HasLeb. |
| 22 |
|
| 23 |
Module Type EqLtBoolNotation (E:EqLtBool) := EqbNotation E E <+ LtbNotation E E. |
| 24 |
Module Type EqLeBoolNotation (E:EqLeBool) := EqbNotation E E <+ LebNotation E E. |
| 25 |
Module Type EqLtLeBoolNotation (E:EqLtLeBool) := EqbNotation E E <+ LtbNotation E E <+ LebNotation E E. |
| 26 |
|
| 27 |
Module Type EqLeBool' := EqLeBool <+ EqLeBoolNotation. |
| 28 |
Module Type EqLtBool' := EqLtBool <+ EqLtBoolNotation. |
| 29 |
Module Type EqLtLeBool' := EqLtLeBool <+ EqLtLeBoolNotation. |
| 30 |
|
| 31 |
Module Type TotalEqLeBool := EqLeBool <+ LebIsTotal. |
| 32 |
Module Type TotalEqLtLeBool := EqLtLeBool <+ LebIsTotal. |
| 33 |
Module Type TotalEqLeBool' := EqLeBool' <+ LebIsTotal. |
| 34 |
Module Type TotalEqLtLeBool' := EqLtLeBool' <+ LebIsTotal. |
| 35 |
|
| 36 |
Module Type TotalTransitiveEqLeBool := TotalEqLeBool <+ LebIsTransitive. |
| 37 |
Module Type TotalTransitiveEqLtLeBool := TotalEqLtLeBool <+ LebIsTransitive. |
| 38 |
Module Type TotalTransitiveEqLeBool' := TotalEqLeBool' <+ LebIsTransitive. |
| 39 |
Module Type TotalTransitiveEqLtLeBool' := TotalEqLtLeBool' <+ LebIsTransitive. |
| 40 |
|
| 41 |
Local Coercion is_true : bool >-> Sortclass. |
| 42 |
|
| 43 |
Module Type IsStrOrderBool (Import E:EqLtBool'). |
| 44 |
#[global] |
| 45 |
Declare Instance ltb_strorder : StrictOrder ltb. |
| 46 |
#[global] |
| 47 |
Declare Instance ltb_compat : Proper (eqb==>eqb==>eq) ltb. |
| 48 |
End IsStrOrderBool. |
| 49 |
|
| 50 |
Module Type StrOrderBool := EqbType <+ HasLtb <+ IsStrOrderBool. |
| 51 |
Module Type StrOrderBool' := StrOrderBool <+ EqLtBoolNotation. |
| 52 |
|
| 53 |
Module Type LebIsLtbEqb (Import E:EqLtLeBool'). |
| 54 |
Axiom leb_ltbeqb : forall x y, ((x <=? y) = ((x <? y) || (x =? y)))%bool. |
| 55 |
End LebIsLtbEqb. |
| 56 |
|
| 57 |
Module Type TotalOrderBool := StrOrderBool <+ HasLeb <+ LebIsLtbEqb <+ LebIsTotal. |
| 58 |
Module Type TotalOrderBool' := TotalOrderBool <+ EqLtLeBoolNotation. |
| 59 |
|
| 60 |
Module TransitiveLeBool_of_TotalOrderBool (Import T : TotalOrderBool') <: LebIsTransitive T. |
| 61 |
#[global] |
| 62 |
Instance leb_trans : Transitive T.leb | 10. |
| 63 |
Proof. |
| 64 |
intros x y z. |
| 65 |
pose proof (_ : Transitive ltb) as ltb_trans. |
| 66 |
pose proof (_ : Transitive eqb) as eqb_trans. |
| 67 |
pose proof (_ : Symmetric eqb) as eqb_sym. |
| 68 |
pose proof (_ : Reflexive eqb) as eqb_refl. |
| 69 |
pose proof (_ : Irreflexive ltb) as ltb_irr. |
| 70 |
pose proof (fun x y z w H => ltb_compat x y H z w). |
| 71 |
hnf in *. |
| 72 |
cbv [respectful complement is_true] in *. |
| 73 |
specialize_all_ways. |
| 74 |
repeat match goal with |
| 75 |
| [ H : forall x : t, _ |- _ ] => clear H |
| 76 |
end. |
| 77 |
generalize (leb_total x y). |
| 78 |
generalize (leb_total y z). |
| 79 |
generalize (leb_total x z). |
| 80 |
rewrite !leb_ltbeqb, !Bool.orb_true_iff. |
| 81 |
cbv [is_true]. |
| 82 |
intros. |
| 83 |
let t := |
| 84 |
repeat first [ match goal with |
| 85 |
| [ H : false = true |- _ ] => exfalso; clear -H; congruence |
| 86 |
| [ H : False |- _ ] => exfalso; exact H |
| 87 |
| [ H : true = true |- _ ] => clear H |
| 88 |
| [ H : false = false |- _ ] => clear H |
| 89 |
| [ H : false = true -> _ |- _ ] => clear H |
| 90 |
| [ H : _ -> false = true -> _ |- _ ] => clear H |
| 91 |
| [ H : _ -> true = true |- _ ] => clear H |
| 92 |
| [ H : _ -> _ -> true = true |- _ ] => clear H |
| 93 |
| [ H : true = true -> _ |- _ ] => specialize (H eq_refl) |
| 94 |
| [ H : _ -> true = true -> _ |- _ ] => specialize (fun H' => H H' eq_refl) |
| 95 |
| [ |- true = true \/ _ ] => left; reflexivity |
| 96 |
| [ |- false = true \/ _ ] => right |
| 97 |
| [ |- true = true ] => reflexivity |
| 98 |
| [ |- false = true ] => exfalso |
| 99 |
| [ |- ?x = true ] => destruct x |
| 100 |
| [ |- ?x = true \/ _ ] => destruct x |
| 101 |
| [ H : context[false = true] |- _ ] |
| 102 |
=> lazymatch type of H with _ \/ _ => idtac end; |
| 103 |
destruct H |
| 104 |
| [ H : ?x = true |- _ ] => generalize dependent x; intros; subst |
| 105 |
| [ H : ?x = true -> false = true |- _ ] => destruct x |
| 106 |
| [ H : ?x = true -> False |- _ ] => destruct x |
| 107 |
end ] in |
| 108 |
t; destruct_one_head'_or; t. |
| 109 |
Qed. |
| 110 |
End TransitiveLeBool_of_TotalOrderBool. |
| 111 |
|
| 112 |
From Coq Require Import OrderedType. |
| 113 |
|
| 114 |
Module Type MiniOrderedType := MiniOrderedType. |
| 115 |
Module Type OrderedTypeOrig := OrderedType.OrderedType. |
| 116 |
|
| 117 |
Module Type IsStrOrderOrig (Import T : EqLt). |
| 118 |
Axiom lt_trans : forall x y z : t, lt x y -> lt y z -> lt x z. |
| 119 |
Axiom lt_not_eq : forall x y : t, lt x y -> ~ eq x y. |
| 120 |
End IsStrOrderOrig. |
| 121 |
Module Type HasCompareOrig (Import T : EqLt). |
| 122 |
Parameter compare : forall x y : t, Compare lt eq x y. |
| 123 |
End HasCompareOrig. |
| 124 |
|
| 125 |
Module Type HasMiniOrderedType (T : EqLt) := Nop <+ IsStrOrderOrig T <+ HasCompareOrig T. |
| 126 |
|
| 127 |
Module Type UsualMiniOrderedType <: MiniOrderedType := Typ <+ HasUsualEq <+ UsualIsEqOrig <+ HasLt <+ HasMiniOrderedType. |
| 128 |
|
| 129 |
(* variant that can be included nicely *) |
| 130 |
Module OT_of_MOT (Import O : MiniOrderedType). |
| 131 |
Definition eq_dec : forall x y : t, {eq x y} + {~ eq x y}. |
| 132 |
Proof with auto with ordered_type. |
| 133 |
intros x y; elim (compare x y); intro H; [ right | left | right ]... |
| 134 |
assert (~ eq y x)... |
| 135 |
Defined. |
| 136 |
End OT_of_MOT. |
| 137 |
|
| 138 |
Module Type UsualOrderedTypeOrig <: OrderedTypeOrig := UsualMiniOrderedType <+ HasEqDec. |
| 139 |
|
| 140 |
Module BackportStrOrder (Import E : EqLt) (EE : IsEq E) (S : IsStrOrder E) <: IsStrOrderOrig E. |
| 141 |
Definition lt_trans : Transitive lt := _. |
| 142 |
Lemma lt_not_eq : forall x y : t, lt x y -> ~ eq x y. |
| 143 |
Proof. |
| 144 |
pose proof (_ : Irreflexive lt) as H. |
| 145 |
intros x y H1 H2. |
| 146 |
rewrite H2 in H1. |
| 147 |
apply H in H1. |
| 148 |
assumption. |
| 149 |
Qed. |
| 150 |
End BackportStrOrder. |
| 151 |
Module BackportCompare (Import E : EqLt) (C : HasCmp E) (CS : CmpSpec E C) <: HasCompareOrig E. |
| 152 |
Definition compare (x y : t) : Compare lt eq x y. |
| 153 |
Proof. |
| 154 |
refine (match C.compare x y as c return CompareSpec _ _ _ c -> Compare _ _ _ _ with |
| 155 |
| Eq => fun H => EQ _ |
| 156 |
| Lt => fun H => LT _ |
| 157 |
| Gt => fun H => GT _ |
| 158 |
end (CS.compare_spec x y)); |
| 159 |
inversion H; assumption. |
| 160 |
Defined. |
| 161 |
End BackportCompare. |
| 162 |
Module UpdateStrOrder_StrOrder (Import E : EqLt) (EE : IsEqOrig E) (S : IsStrOrderOrig E). |
| 163 |
#[global] |
| 164 |
Instance lt_strorder : StrictOrder lt | 100. |
| 165 |
Proof. |
| 166 |
pose proof S.lt_trans. |
| 167 |
pose proof S.lt_not_eq. |
| 168 |
pose proof EE.eq_refl. |
| 169 |
split; cbv in *; eauto. |
| 170 |
Qed. |
| 171 |
End UpdateStrOrder_StrOrder. |
| 172 |
Module UpdateStrOrder_Compat (Import E : EqLt) (EE : IsEqOrig E) (S : IsStrOrderOrig E) (C : HasCompareOrig E). |
| 173 |
#[global] |
| 174 |
Instance lt_compat : Proper (eq ==> eq ==> iff) lt | 100. |
| 175 |
Proof. |
| 176 |
cbv. |
| 177 |
intros x y H x' y' H'. |
| 178 |
let t := |
| 179 |
repeat match goal with |
| 180 |
| [ H : ?T, H' : ~?T |- _ ] => exfalso; apply H', H |
| 181 |
| [ H : ?T |- ?T ] => exact H |
| 182 |
| [ H : ?T, H' : ?T |- _ ] => clear H' |
| 183 |
| [ H : eq _ _, H' : eq _ _ |- _ ] |
| 184 |
=> unique pose proof (EE.eq_trans H H') |
| 185 |
| [ H : eq _ _ |- _ ] |
| 186 |
=> unique pose proof (EE.eq_sym H) |
| 187 |
| [ H : lt _ _ |- _ ] |
| 188 |
=> unique pose proof (@S.lt_not_eq _ _ H) |
| 189 |
| [ H : lt _ _, H' : lt _ _ |- _ ] |
| 190 |
=> unique pose proof (@S.lt_trans _ _ _ H H') |
| 191 |
| [ |- _ /\ _ ] => split; intros |
| 192 |
end in |
| 193 |
t; |
| 194 |
destruct (C.compare y y'); t; |
| 195 |
destruct (C.compare x x'); t; |
| 196 |
destruct (C.compare x y'); t. |
| 197 |
Qed. |
| 198 |
End UpdateStrOrder_Compat. |
| 199 |
Module UpdateStrOrder (E : EqLt) (EE : IsEqOrig E) (S : IsStrOrderOrig E) (C : HasCompareOrig E) <: IsStrOrder E := UpdateStrOrder_StrOrder E EE S <+ UpdateStrOrder_Compat E EE S C. |
| 200 |
Module UpdateCmp (Import E : EqLt) (C : HasCompareOrig E) <: HasCmp E. |
| 201 |
Definition compare (x y : t) : comparison |
| 202 |
:= match C.compare x y with |
| 203 |
| EQ _ => Eq |
| 204 |
| LT _ => Lt |
| 205 |
| GT _ => Gt |
| 206 |
end. |
| 207 |
End UpdateCmp. |
| 208 |
Module UpdateCompare (Import E : EqLt) (C : HasCompareOrig E) <: HasCompare E. |
| 209 |
Include UpdateCmp E C. |
| 210 |
Lemma compare_spec x y : CompareSpec (eq x y) (lt x y) (lt y x) (compare x y). |
| 211 |
Proof. |
| 212 |
cbv [compare]; destruct C.compare; constructor; assumption. |
| 213 |
Qed. |
| 214 |
End UpdateCompare. |
| 215 |
Module HasEqDec_DecStrOrder (Import E : DecStrOrder') <: HasEqDec E. |
| 216 |
Lemma eq_dec (x y : t) : { x==y } + { ~ x==y }. |
| 217 |
Proof. |
| 218 |
refine (match compare x y as c return CompareSpec _ _ _ c -> _ with |
| 219 |
| Eq |
| 220 |
=> fun pf |
| 221 |
=> left match pf in CompareSpec _ _ _ c return match c return Prop with Eq => _ | _ => True end with |
| 222 |
| CompEq pf => pf |
| 223 |
| _ => I |
| 224 |
end |
| 225 |
| Lt |
| 226 |
=> fun pf |
| 227 |
=> right match pf in CompareSpec _ _ _ c return match c return Prop with Lt => _ | _ => True end with |
| 228 |
| CompLt pf' => _ |
| 229 |
| _ => I |
| 230 |
end |
| 231 |
| Gt |
| 232 |
=> fun pf |
| 233 |
=> right match pf in CompareSpec _ _ _ c return match c return Prop with Gt => _ | _ => True end with |
| 234 |
| CompGt pf' => _ |
| 235 |
| _ => I |
| 236 |
end |
| 237 |
end (compare_spec x y)); |
| 238 |
clear pf; intro; eapply (_ : Irreflexive lt), lt_compat; (eassumption + reflexivity). |
| 239 |
Defined. |
| 240 |
End HasEqDec_DecStrOrder. |
| 241 |
|
| 242 |
Module OT_of_New (Import O : Orders.OrderedType) <: OrderedTypeOrig. |
| 243 |
Definition t := O.t. |
| 244 |
Definition eq := O.eq. |
| 245 |
Definition lt := O.lt. |
| 246 |
Include BackportEq O O. |
| 247 |
Include BackportStrOrder O O O. |
| 248 |
Include BackportCompare O O O. |
| 249 |
Include OT_of_MOT. |
| 250 |
End OT_of_New. |
| 251 |
|
| 252 |
Module OT_of_Orig (Import O : MiniOrderedType) <: Orders.OrderedType. |
| 253 |
Definition t := O.t. |
| 254 |
Definition eq := O.eq. |
| 255 |
Definition lt := O.lt. |
| 256 |
Include UpdateEq O O. |
| 257 |
Include UpdateStrOrder O O O O. |
| 258 |
Include UpdateCompare O O. |
| 259 |
Include HasEqDec_DecStrOrder. |
| 260 |
End OT_of_Orig. |
| 261 |
|
| 262 |
Module UsualOT_of_UsualOrig (Import O : UsualMiniOrderedType) <: UsualOrderedType. |
| 263 |
Definition t := O.t. |
| 264 |
Definition lt := O.lt. |
| 265 |
Include HasUsualEq. |
| 266 |
Include UsualIsEq. |
| 267 |
Include UpdateStrOrder O O O O. |
| 268 |
Include UpdateCompare O O. |
| 269 |
Include HasEqDec_DecStrOrder. |
| 270 |
End UsualOT_of_UsualOrig. |
| 271 |
|
| 272 |
Module OrderedTypeOrigFacts (O:OrderedTypeOrig) := OrderedType.OrderedTypeFacts O. |
| 273 |
Module Type OrderedTypeOrigFactsT (O:OrderedTypeOrig) := Nop <+ OrderedTypeOrigFacts O. |
| 274 |
Module OrderedTypeOrigFacts_RemoveHints (O:OrderedTypeOrig) (F:OrderedTypeOrigFactsT O). |
| 275 |
Global Remove Hints |
| 276 |
F.eq_equiv |
| 277 |
F.lt_compat |
| 278 |
F.lt_strorder |
| 279 |
: typeclass_instances. |
| 280 |
End OrderedTypeOrigFacts_RemoveHints. |
| 281 |
|
| 282 |
Module Type KeyOrderedTypeT (O:OrderedType) := Nop <+ KeyOrderedType O. |
| 283 |
Module KeyOrderedType_RemoveHints (O:OrderedTypeOrig) (F:KeyOrderedTypeT O). |
| 284 |
Include OrderedTypeOrigFacts_RemoveHints O F.MO. |
| 285 |
Global Remove Hints |
| 286 |
F.eqk_equiv |
| 287 |
F.eqke_equiv |
| 288 |
F.ltk_strorder |
| 289 |
F.ltk_compat |
| 290 |
F.ltk_compat' |
| 291 |
: typeclass_instances. |
| 292 |
End KeyOrderedType_RemoveHints. |