| 1 |
From Coq Require Import Morphisms. |
| 2 |
From Coq Require Import Relation_Definitions. |
| 3 |
Require Import Crypto.Util.Decidable. |
| 4 |
Require Import Crypto.Util.Tactics.DestructHead. |
| 5 |
Require Import Crypto.Util.Tactics.SetoidSubst. |
| 6 |
Require Import Crypto.Util.GlobalSettings. |
| 7 |
|
| 8 |
Scheme Equality for sum. |
| 9 |
|
| 10 |
Definition sumwise {A B} (RA:relation A) (RB : relation B) : relation (A + B) |
| 11 |
:= fun x y => match x, y with |
| 12 |
| inl x', inl y' => RA x' y' |
| 13 |
| inr x', inr y' => RB x' y' |
| 14 |
| _, _ => False |
| 15 |
end. |
| 16 |
|
| 17 |
Arguments sumwise {A B} _ _ _ _. |
| 18 |
|
| 19 |
Global Instance inl_Proper_sumwise {A B RA RB} |
| 20 |
: Proper (RA ==> sumwise RA RB) (@inl A B) | 10. |
| 21 |
Proof. repeat intro; cbv; assumption. Qed. |
| 22 |
Global Instance inr_Proper_sumwise {A B RA RB} |
| 23 |
: Proper (RB ==> sumwise RA RB) (@inr A B) | 10. |
| 24 |
Proof. repeat intro; cbv; assumption. Qed. |
| 25 |
|
| 26 |
Global Instance Equivalence_sumwise |
| 27 |
: forall {A B} {RA:relation A} {RB:relation B} |
| 28 |
{RA_equiv:Equivalence RA} {RB_equiv:Equivalence RB}, |
| 29 |
Equivalence (sumwise RA RB) | 10. |
| 30 |
Proof. |
| 31 |
intros A B RA RB RA_equiv RB_equiv. |
| 32 |
split; repeat intros [?|?]; simpl; trivial; destruct RA_equiv, RB_equiv; try tauto; eauto. |
| 33 |
Qed. |
| 34 |
|
| 35 |
Lemma sumwise_beq_bp {A B} {A_beq B_beq} {A_le : relation A} {B_le : relation B} |
| 36 |
(A_bp : forall x y, A_beq x y = true -> A_le x y) |
| 37 |
(B_bp : forall x y, B_beq x y = true -> B_le x y) |
| 38 |
: forall x y, sum_beq _ _ A_beq B_beq x y = true -> sumwise A_le B_le x y. |
| 39 |
Proof. |
| 40 |
intros [x|x] [y|y]; cbn; auto using Bool.diff_false_true. |
| 41 |
Defined. |
| 42 |
|
| 43 |
Lemma sumwise_beq_pb {A B} {A_beq B_beq} {A_le : relation A} {B_le : relation B} |
| 44 |
(A_pb : forall x y, A_le x y -> A_beq x y = true) |
| 45 |
(B_pb : forall x y, B_le x y -> B_beq x y = true) |
| 46 |
: forall x y, sumwise A_le B_le x y -> sum_beq _ _ A_beq B_beq x y = true. |
| 47 |
Proof. |
| 48 |
intros [x|x] [y|y]; cbn; auto using Bool.diff_false_true. |
| 49 |
Defined. |
| 50 |
|
| 51 |
Definition sum_le {A B} (RA:relation A) (RB : relation B) : relation (A + B) |
| 52 |
:= fun x y => match x, y with |
| 53 |
| inl x', inl y' => RA x' y' |
| 54 |
| inr x', inr y' => RB x' y' |
| 55 |
| inl _, inr _ => True |
| 56 |
| inr _, inl _ => False |
| 57 |
end. |
| 58 |
|
| 59 |
Arguments sum_le {A B} _ _ _ _. |
| 60 |
|
| 61 |
Global Instance inl_Proper_sum_le {A B RA RB} |
| 62 |
: Proper (RA ==> sum_le RA RB) (@inl A B) | 10. |
| 63 |
Proof. repeat intro; cbv; assumption. Qed. |
| 64 |
Global Instance inr_Proper_sum_le {A B RA RB} |
| 65 |
: Proper (RB ==> sum_le RA RB) (@inr A B) | 10. |
| 66 |
Proof. repeat intro; cbv; assumption. Qed. |
| 67 |
|
| 68 |
Global Instance sum_le_Reflexive {A B RA RB} |
| 69 |
`{@Reflexive A RA,@Reflexive B RB} |
| 70 |
: Reflexive (sum_le RA RB) | 10. |
| 71 |
Proof. repeat intro; destruct_head'_sum; cbn; reflexivity. Defined. |
| 72 |
|
| 73 |
Global Instance sum_le_Transitive {A B RA RB} |
| 74 |
`{@Transitive A RA,@Transitive B RB} |
| 75 |
: Transitive (sum_le RA RB) | 10. |
| 76 |
Proof. |
| 77 |
repeat intro; destruct_head'_sum; cbn in *. |
| 78 |
all: try ((idtac + exfalso); assumption). |
| 79 |
all: etransitivity; eassumption. |
| 80 |
Defined. |
| 81 |
|
| 82 |
Global Instance sum_le_Irreflexive {A B RA RB} |
| 83 |
`{@Irreflexive A RA,@Irreflexive B RB} |
| 84 |
: Irreflexive (sum_le RA RB) | 10. |
| 85 |
Proof. |
| 86 |
intros [?|?] H'; cbn in H'; apply irreflexivity in H'; assumption. |
| 87 |
Qed. |
| 88 |
|
| 89 |
Definition sum_leb {A B} (A_leb : A -> A -> bool) (B_leb : B -> B -> bool) |
| 90 |
: A + B -> A + B -> bool |
| 91 |
:= fun x y => match x, y with |
| 92 |
| inl x', inl y' => A_leb x' y' |
| 93 |
| inr x', inr y' => B_leb x' y' |
| 94 |
| inl _, inr _ => true |
| 95 |
| inr _, inl _ => false |
| 96 |
end. |
| 97 |
|
| 98 |
Arguments sum_leb {A B} _ _ _ _. |
| 99 |
|
| 100 |
Lemma sum_leb_bp {A B} {A_leb B_leb} {A_le : relation A} {B_le : relation B} |
| 101 |
(A_bp : forall x y, A_leb x y = true -> A_le x y) |
| 102 |
(B_bp : forall x y, B_leb x y = true -> B_le x y) |
| 103 |
: forall x y, sum_leb A_leb B_leb x y = true -> sum_le A_le B_le x y. |
| 104 |
Proof. |
| 105 |
intros [x|x] [y|y]; cbn; auto using Bool.diff_false_true. |
| 106 |
Defined. |
| 107 |
|
| 108 |
Lemma sum_leb_pb {A B} {A_leb B_leb} {A_le : relation A} {B_le : relation B} |
| 109 |
(A_pb : forall x y, A_le x y -> A_leb x y = true) |
| 110 |
(B_pb : forall x y, B_le x y -> B_leb x y = true) |
| 111 |
: forall x y, sum_le A_le B_le x y -> sum_leb A_leb B_leb x y = true. |
| 112 |
Proof. |
| 113 |
intros [x|x] [y|y]; cbn; auto using Bool.diff_false_true. |
| 114 |
Defined. |
| 115 |
|
| 116 |
Ltac congruence_sum_step := |
| 117 |
match goal with |
| 118 |
| [ H : inl _ = inr _ |- _ ] => solve [ inversion H ] |
| 119 |
| [ H : inr _ = inl _ |- _ ] => solve [ inversion H ] |
| 120 |
| [ H : inl _ = inl _ |- _ ] => inversion H; clear H |
| 121 |
| [ H : inr _ = inr _ |- _ ] => inversion H; clear H |
| 122 |
end. |
| 123 |
Ltac congruence_sum := repeat congruence_sum_step. |
| 124 |
|
| 125 |
Local Hint Extern 0 => solve [ solve_decidable_transparent ] : typeclass_instances. |
| 126 |
Global Instance dec_sumwise {A B RA RB} {HA : DecidableRel RA} {HB : DecidableRel RB} : DecidableRel (@sumwise A B RA RB) | 10. |
| 127 |
Proof. |
| 128 |
intros [x|x] [y|y]; exact _. |
| 129 |
Defined. |
| 130 |
|
| 131 |
Global Instance dec_sum_le {A B RA RB} {HA : DecidableRel RA} {HB : DecidableRel RB} : DecidableRel (@sum_le A B RA RB) | 10. |
| 132 |
Proof. |
| 133 |
intros [x|x] [y|y]; exact _. |
| 134 |
Defined. |
| 135 |
|
| 136 |
Global Instance sum_le_StrictOrder {A B RA RB} `{@StrictOrder A RA,@StrictOrder B RB} : StrictOrder (sum_le RA RB) | 10. |
| 137 |
Proof. split; exact _. Defined. |
| 138 |
|
| 139 |
Global Instance sumwise_Proper_iff {A B eqA eqB leA leB} |
| 140 |
{A_Proper : Proper (eqA ==> eqA ==> iff) leA} |
| 141 |
{B_Proper : Proper (eqB ==> eqB ==> iff) leB} |
| 142 |
: Proper (@sumwise A B eqA eqB ==> @sumwise A B eqA eqB ==> iff) |
| 143 |
(@sumwise A B leA leB) | 5. |
| 144 |
Proof. |
| 145 |
cbv [Proper respectful] in *; repeat intro; destruct_head'_sum; cbn in *. |
| 146 |
all: try (exfalso; assumption). |
| 147 |
all: try reflexivity. |
| 148 |
all: eauto with nocore. |
| 149 |
Qed. |
| 150 |
|
| 151 |
Global Instance sum_le_Proper_iff {A B eqA eqB leA leB} |
| 152 |
{A_Proper : Proper (eqA ==> eqA ==> iff) leA} |
| 153 |
{B_Proper : Proper (eqB ==> eqB ==> iff) leB} |
| 154 |
: Proper (@sumwise A B eqA eqB ==> @sumwise A B eqA eqB ==> iff) |
| 155 |
(@sum_le A B leA leB) | 5. |
| 156 |
Proof. |
| 157 |
cbv [Proper respectful] in *; repeat intro; destruct_head'_sum; cbn in *. |
| 158 |
all: try (exfalso; assumption). |
| 159 |
all: try reflexivity. |
| 160 |
all: eauto with nocore. |
| 161 |
Qed. |
| 162 |
|
| 163 |
Global Instance sum_le_Proper_le {A B leA leB} |
| 164 |
{A_Proper : Proper (Basics.flip leA ==> leA ==> Basics.impl) leA} |
| 165 |
{B_Proper : Proper (Basics.flip leB ==> leB ==> Basics.impl) leB} |
| 166 |
: Proper (Basics.flip (@sum_le A B leA leB) ==> @sum_le A B leA leB ==> Basics.impl) |
| 167 |
(@sum_le A B leA leB) | 10. |
| 168 |
Proof. |
| 169 |
cbv [Basics.flip Basics.impl Proper respectful] in *; repeat intro; destruct_head'_sum; cbn in *. |
| 170 |
all: try (exfalso; assumption). |
| 171 |
all: try exact I. |
| 172 |
all: eauto with nocore. |
| 173 |
Qed. |
| 174 |
|
| 175 |
Global Instance sum_le_Proper_le_flip {A B leA leB} |
| 176 |
{A_Proper : Proper (leA ==> Basics.flip leA ==> Basics.flip Basics.impl) leA} |
| 177 |
{B_Proper : Proper (leB ==> Basics.flip leB ==> Basics.flip Basics.impl) leB} |
| 178 |
: Proper (@sum_le A B leA leB ==> Basics.flip (@sum_le A B leA leB) ==> Basics.flip Basics.impl) |
| 179 |
(@sum_le A B leA leB) | 10. |
| 180 |
Proof. |
| 181 |
cbv [Basics.flip Basics.impl Proper respectful] in *; repeat intro; destruct_head'_sum; cbn in *. |
| 182 |
all: try (exfalso; assumption). |
| 183 |
all: try exact I. |
| 184 |
all: eauto with nocore. |
| 185 |
Qed. |
| 186 |
|
| 187 |
Module Sum. |
| 188 |
Notation eqb := sum_beq. |
| 189 |
Notation eqb_bl := internal_sum_dec_bl. |
| 190 |
Notation eqb_lb := internal_sum_dec_lb. |
| 191 |
|
| 192 |
Definition compare {A B} |
| 193 |
(A_compare : A -> A -> comparison) |
| 194 |
(B_compare : B -> B -> comparison) |
| 195 |
(x y : A + B) |
| 196 |
: comparison |
| 197 |
:= match x, y with |
| 198 |
| inl x, inl y => A_compare x y |
| 199 |
| inr x, inr y => B_compare x y |
| 200 |
| inl _, inr _ => Lt |
| 201 |
| inr _, inl _ => Gt |
| 202 |
end. |
| 203 |
End Sum. |
| 204 |
|
| 205 |
(** ** Equality for [sum] *) |
| 206 |
Section sum. |
| 207 |
Local Notation sum_code u v |
| 208 |
:= (match u, v with |
| 209 |
| inl u', inl v' |
| 210 |
| inr u', inr v' |
| 211 |
=> u' = v' |
| 212 |
| inl _, _ |
| 213 |
| inr _, _ |
| 214 |
=> False |
| 215 |
end). |
| 216 |
|
| 217 |
(** *** Equality of [sum] is a [match] *) |
| 218 |
Definition path_sum {A B} (u v : sum A B) (p : sum_code u v) |
| 219 |
: u = v. |
| 220 |
Proof. destruct u, v; first [ apply f_equal | exfalso ]; exact p. Defined. |
| 221 |
|
| 222 |
(** *** Equivalence of equality of [sum] with [sum_code] *) |
| 223 |
Definition unpath_sum {A B} {u v : sum A B} (p : u = v) |
| 224 |
: sum_code u v. |
| 225 |
Proof. subst v; destruct u; reflexivity. Defined. |
| 226 |
|
| 227 |
Definition path_sum_iff {A B} |
| 228 |
(u v : @sum A B) |
| 229 |
: u = v <-> sum_code u v. |
| 230 |
Proof. |
| 231 |
split; [ apply unpath_sum | apply path_sum ]. |
| 232 |
Defined. |
| 233 |
|
| 234 |
(** *** Eta-expansion of [@eq (sum _ _)] *) |
| 235 |
Definition path_sum_eta {A B} {u v : @sum A B} (p : u = v) |
| 236 |
: p = path_sum u v (unpath_sum p). |
| 237 |
Proof. destruct u, p; reflexivity. Defined. |
| 238 |
|
| 239 |
(** *** Induction principle for [@eq (sum _ _)] *) |
| 240 |
Definition path_sum_rect {A B} {u v : @sum A B} (P : u = v -> Type) |
| 241 |
(f : forall p, P (path_sum u v p)) |
| 242 |
: forall p, P p. |
| 243 |
Proof. intro p; specialize (f (unpath_sum p)); destruct u, p; exact f. Defined. |
| 244 |
Definition path_sum_rec {A B u v} (P : u = v :> @sum A B -> Set) := path_sum_rect P. |
| 245 |
Definition path_sum_ind {A B u v} (P : u = v :> @sum A B -> Prop) := path_sum_rec P. |
| 246 |
End sum. |
| 247 |
|
| 248 |
(** ** Useful Tactics *) |
| 249 |
(** *** [inversion_sum] *) |
| 250 |
Ltac induction_path_sum H := |
| 251 |
induction H as [H] using path_sum_rect; |
| 252 |
try match type of H with |
| 253 |
| False => exfalso; exact H |
| 254 |
end. |
| 255 |
Ltac inversion_sum_step := |
| 256 |
match goal with |
| 257 |
| [ H : inl _ = inl _ |- _ ] |
| 258 |
=> induction_path_sum H |
| 259 |
| [ H : inl _ = inr _ |- _ ] |
| 260 |
=> induction_path_sum H |
| 261 |
| [ H : inr _ = inl _ |- _ ] |
| 262 |
=> induction_path_sum H |
| 263 |
| [ H : inr _ = inr _ |- _ ] |
| 264 |
=> induction_path_sum H |
| 265 |
end. |
| 266 |
Ltac inversion_sum := repeat inversion_sum_step. |