| 1 |
From Coq Require Import PArith ZArith NArith. |
| 2 |
From Coq Require OrderedTypeEx. |
| 3 |
From Coq Require Import Equalities. |
| 4 |
From Coq Require Import Orders. |
| 5 |
From Coq Require Import OrdersEx. |
| 6 |
Require Import Crypto.Util.Structures.Equalities. |
| 7 |
Require Import Crypto.Util.Structures.Equalities.Iso. |
| 8 |
Require Import Crypto.Util.Structures.Orders. |
| 9 |
Require Import Crypto.Util.Structures.Orders.Iso. |
| 10 |
Require Import Crypto.Util.Structures.Orders.Option. |
| 11 |
Require Import Crypto.Util.Structures.Orders.Sum. |
| 12 |
Require Import Crypto.Util.Structures.Orders.Flip. |
| 13 |
Require Import Crypto.Util.Tactics.BreakMatch. |
| 14 |
|
| 15 |
Module OptionPositiveOrderedTypeBits <: UsualOrderedType := OptionUsualOrderedType PositiveOrderedTypeBits. |
| 16 |
Module OptionPositiveOrderedTypeBitsOrig <: UsualOrderedTypeOrig := OptionUsualOrderedTypeOrig OrderedTypeEx.PositiveOrderedTypeBits. |
| 17 |
|
| 18 |
Module NIsoOptionPositiveCommon. |
| 19 |
Definition t := N. |
| 20 |
Include HasUsualEq. |
| 21 |
Include UsualIsEq. |
| 22 |
Definition eq_dec : forall x y : t, {eq x y} + {~ eq x y} := N.eq_dec. |
| 23 |
Definition to_ (x : t) : option positive |
| 24 |
:= match x with |
| 25 |
| N0 => None |
| 26 |
| Npos p => Some p |
| 27 |
end. |
| 28 |
Definition of_ (x : option positive) : t |
| 29 |
:= match x with |
| 30 |
| None => N0 |
| 31 |
| Some p => Npos p |
| 32 |
end. |
| 33 |
Lemma Proper_to_ : Proper (eq ==> OptionPositiveOrderedTypeBits.eq) to_. |
| 34 |
Proof. cbv [eq]; repeat intro; subst; reflexivity. Qed. |
| 35 |
Lemma Proper_of_ : Proper (OptionPositiveOrderedTypeBits.eq ==> eq) of_. |
| 36 |
Proof. cbv [OptionPositiveOrderedTypeBits.eq]; repeat intro; subst; reflexivity. Qed. |
| 37 |
Lemma of_to : forall x : t, eq (of_ (to_ x)) x. |
| 38 |
Proof. cbv [eq of_ to_]; intros; break_innermost_match; reflexivity. Qed. |
| 39 |
Lemma to_of : forall x : option positive, OptionPositiveOrderedTypeBits.eq (to_ (of_ x)) x. |
| 40 |
Proof. |
| 41 |
intros [|]; reflexivity. |
| 42 |
Qed. |
| 43 |
Definition lt (x y : t) : Prop := OptionPositiveOrderedTypeBits.lt (to_ x) (to_ y). |
| 44 |
Global Instance lt_strorder : StrictOrder lt | 100. |
| 45 |
Proof. |
| 46 |
split; hnf; cbv [lt complement]; intros *; |
| 47 |
apply OptionPositiveOrderedTypeBits.lt_strorder. |
| 48 |
Qed. |
| 49 |
Definition lt_compat : Proper (eq ==> eq ==> iff) lt := _. |
| 50 |
Global Instance Proper_to_lt : Proper (lt ==> OptionPositiveOrderedTypeBits.lt) to_ | 100. |
| 51 |
Proof. cbv [lt Proper respectful]; trivial. Qed. |
| 52 |
Global Instance Proper_of_lt : Proper (OptionPositiveOrderedTypeBits.lt ==> lt) of_ | 100. |
| 53 |
Proof. cbv [lt Proper respectful]; intros *; rewrite !to_of; trivial. Qed. |
| 54 |
Include BackportEq. |
| 55 |
Include BackportStrOrder. |
| 56 |
End NIsoOptionPositiveCommon. |
| 57 |
|
| 58 |
Module NIsoOptionPositive <: IsoOrderedType OptionPositiveOrderedTypeBits. |
| 59 |
Include NIsoOptionPositiveCommon. |
| 60 |
Definition compare (x y : t) : comparison := OptionPositiveOrderedTypeBits.compare (to_ x) (to_ y). |
| 61 |
Lemma compare_spec (x y : t) : CompareSpec (eq x y) (lt x y) (lt y x) (compare x y). |
| 62 |
Proof. |
| 63 |
cbv [eq]. |
| 64 |
destruct (OptionPositiveOrderedTypeBits.compare_spec (to_ x) (to_ y) : CompareSpec _ (lt _ _) (lt _ _) (compare _ _)); constructor; trivial. |
| 65 |
rewrite <- (of_to x), <- (of_to y); congruence. |
| 66 |
Qed. |
| 67 |
End NIsoOptionPositive. |
| 68 |
|
| 69 |
Module NIsoOptionPositiveOrig <: IsoOrderedTypeOrig OptionPositiveOrderedTypeBitsOrig. |
| 70 |
Import OrderedType. |
| 71 |
Include NIsoOptionPositiveCommon. |
| 72 |
Definition compare (x y : t) : Compare lt eq x y. |
| 73 |
Proof. |
| 74 |
refine match OptionPositiveOrderedTypeBitsOrig.compare (to_ x) (to_ y) with |
| 75 |
| LT pf => LT _ |
| 76 |
| EQ pf => EQ (Logic.eq_trans (Logic.eq_sym (of_to _)) (Logic.eq_trans (f_equal _ (_ pf)) (of_to _))) |
| 77 |
| GT pf => GT _ |
| 78 |
end; |
| 79 |
try assumption. |
| 80 |
{ cbv [OptionPositiveOrderedTypeBitsOrig.eq Option.option_eq]. |
| 81 |
clear. |
| 82 |
abstract now do 2 destruct to_; intuition congruence. } |
| 83 |
Defined. |
| 84 |
End NIsoOptionPositiveOrig. |
| 85 |
|
| 86 |
Module NOrderedTypeBits <: UsualOrderedType := NIsoOptionPositive. |
| 87 |
Module NOrderedTypeBitsOrig <: UsualOrderedTypeOrig := NIsoOptionPositiveOrig. |
| 88 |
|
| 89 |
Module NegativeOrderedTypeBits <: UsualOrderedType := FlipUsualOrderedType PositiveOrderedTypeBits. |
| 90 |
Module NegativeOrderedTypeBitsOrig <: UsualOrderedTypeOrig := FlipUsualOrderedTypeOrig OrderedTypeEx.PositiveOrderedTypeBits. |
| 91 |
|
| 92 |
Module SumNegNOrderedTypeBits <: UsualOrderedType := SumUsualOrderedType NegativeOrderedTypeBits NOrderedTypeBits. |
| 93 |
Module SumNegNOrderedTypeBitsOrig <: UsualOrderedTypeOrig := SumUsualOrderedTypeOrig NegativeOrderedTypeBitsOrig NOrderedTypeBitsOrig. |
| 94 |
|
| 95 |
Module ZIsoSumNegNCommon. |
| 96 |
Definition t := Z. |
| 97 |
Include HasUsualEq. |
| 98 |
Include UsualIsEq. |
| 99 |
Definition eq_dec : forall x y : t, {eq x y} + {~ eq x y} := Z.eq_dec. |
| 100 |
Definition to_ (x : t) : positive + N |
| 101 |
:= match x with |
| 102 |
| Zneg p => inl p |
| 103 |
| Z0 => inr N0 |
| 104 |
| Zpos p => inr (Npos p) |
| 105 |
end. |
| 106 |
Definition of_ (x : positive + N) : t |
| 107 |
:= match x with |
| 108 |
| inl p => Zneg p |
| 109 |
| inr N0 => Z0 |
| 110 |
| inr (Npos p) => Zpos p |
| 111 |
end. |
| 112 |
Lemma Proper_to_ : Proper (eq ==> SumNegNOrderedTypeBits.eq) to_. |
| 113 |
Proof. cbv [eq]; repeat intro; subst; reflexivity. Qed. |
| 114 |
Lemma Proper_of_ : Proper (SumNegNOrderedTypeBits.eq ==> eq) of_. |
| 115 |
Proof. cbv [SumNegNOrderedTypeBits.eq]; repeat intro; subst; reflexivity. Qed. |
| 116 |
Lemma of_to : forall x : t, eq (of_ (to_ x)) x. |
| 117 |
Proof. cbv [eq of_ to_]; intros; break_innermost_match; reflexivity. Qed. |
| 118 |
Lemma to_of : forall x : positive + N, SumNegNOrderedTypeBits.eq (to_ (of_ x)) x. |
| 119 |
Proof. |
| 120 |
intros [|[|]]; cbn; reflexivity. |
| 121 |
Qed. |
| 122 |
Definition lt (x y : t) : Prop := SumNegNOrderedTypeBits.lt (to_ x) (to_ y). |
| 123 |
Global Instance lt_strorder : StrictOrder lt | 100. |
| 124 |
Proof. |
| 125 |
split; hnf; cbv [lt complement]; intros *; |
| 126 |
apply SumNegNOrderedTypeBits.lt_strorder. |
| 127 |
Qed. |
| 128 |
Definition lt_compat : Proper (eq ==> eq ==> iff) lt := _. |
| 129 |
Global Instance Proper_to_lt : Proper (lt ==> SumNegNOrderedTypeBits.lt) to_ | 100. |
| 130 |
Proof. cbv [lt Proper respectful]; trivial. Qed. |
| 131 |
Global Instance Proper_of_lt : Proper (SumNegNOrderedTypeBits.lt ==> lt) of_ | 100. |
| 132 |
Proof. cbv [lt Proper respectful]; intros *; rewrite !to_of; trivial. Qed. |
| 133 |
Include BackportEq. |
| 134 |
Include BackportStrOrder. |
| 135 |
End ZIsoSumNegNCommon. |
| 136 |
|
| 137 |
Module ZIsoSumNegN <: IsoOrderedType SumNegNOrderedTypeBits. |
| 138 |
Include ZIsoSumNegNCommon. |
| 139 |
Definition compare (x y : t) : comparison := SumNegNOrderedTypeBits.compare (to_ x) (to_ y). |
| 140 |
Lemma compare_spec (x y : t) : CompareSpec (eq x y) (lt x y) (lt y x) (compare x y). |
| 141 |
Proof. |
| 142 |
cbv [eq]. |
| 143 |
destruct (SumNegNOrderedTypeBits.compare_spec (to_ x) (to_ y) : CompareSpec _ (lt _ _) (lt _ _) (compare _ _)); constructor; trivial. |
| 144 |
rewrite <- (of_to x), <- (of_to y); congruence. |
| 145 |
Qed. |
| 146 |
End ZIsoSumNegN. |
| 147 |
|
| 148 |
Module ZIsoSumNegNOrig <: IsoOrderedTypeOrig SumNegNOrderedTypeBitsOrig. |
| 149 |
Import OrderedType. |
| 150 |
Include ZIsoSumNegNCommon. |
| 151 |
Definition compare (x y : t) : Compare lt eq x y. |
| 152 |
Proof. |
| 153 |
refine match SumNegNOrderedTypeBitsOrig.compare (to_ x) (to_ y) with |
| 154 |
| LT pf => LT _ |
| 155 |
| EQ pf => EQ (Logic.eq_trans (Logic.eq_sym (of_to _)) (Logic.eq_trans (f_equal _ (_ pf)) (of_to _))) |
| 156 |
| GT pf => GT _ |
| 157 |
end; |
| 158 |
try assumption. |
| 159 |
{ cbv [SumNegNOrderedTypeBitsOrig.eq Option.option_eq]. |
| 160 |
clear. |
| 161 |
abstract now do 2 destruct to_; intuition congruence. } |
| 162 |
Defined. |
| 163 |
End ZIsoSumNegNOrig. |
| 164 |
|
| 165 |
Module ZOrderedTypeBits <: UsualOrderedType := ZIsoSumNegN. |
| 166 |
Module ZOrderedTypeBitsOrig <: UsualOrderedTypeOrig := ZIsoSumNegNOrig. |