| 1 |
(** * Definition and classification of the [×] Type, with primitive projections *) |
| 2 |
(** In this file, we classify the basic structure of [×] types ([prod] |
| 3 |
in Coq). In particular, we classify equalities of non-dependent |
| 4 |
pairs (inhabitants of [×] types), so that when we have an equality |
| 5 |
between two such pairs, or when we want such an equality, we have |
| 6 |
a systematic way of reducing such equalities to equalities at |
| 7 |
simpler types. *) |
| 8 |
From Coq Require Import Morphisms. |
| 9 |
Require Import Crypto.Util.IffT. |
| 10 |
Require Import Crypto.Util.Equality. |
| 11 |
Require Import Crypto.Util.GlobalSettings. |
| 12 |
|
| 13 |
Local Set Primitive Projections. |
| 14 |
|
| 15 |
Declare Scope primproj_type_scope. |
| 16 |
Declare Scope primproj_scope. |
| 17 |
Delimit Scope primproj_type_scope with primproj_type. |
| 18 |
Delimit Scope primproj_scope with primproj. |
| 19 |
|
| 20 |
Module Import Primitive. |
| 21 |
Record prod A B := pair { fst : A ; snd : B }. |
| 22 |
Global Arguments pair {_ _}. |
| 23 |
Global Arguments fst {_ _}. |
| 24 |
Global Arguments snd {_ _}. |
| 25 |
|
| 26 |
Module Export Notations. |
| 27 |
Notation "x * y" := (@prod x y) : type_scope. |
| 28 |
Notation "( x , y , .. , z )" := (pair .. (pair x y) .. z) : core_scope. |
| 29 |
End Notations. |
| 30 |
|
| 31 |
Local Set Implicit Arguments. |
| 32 |
Definition prod_beq A B (eq_A : A -> A -> bool) (eq_B : B -> B -> bool) (X Y : A * B) : bool |
| 33 |
:= let '(a, b) := X in let '(a', b') := Y in (eq_A a a' && eq_B b b')%bool. |
| 34 |
Global Arguments prod_beq / . |
| 35 |
Lemma prod_dec_bl A B eq_A eq_B |
| 36 |
(A_bl : forall x y : A, eq_A x y = true -> x = y) |
| 37 |
(B_bl : forall x y : B, eq_B x y = true -> x = y) |
| 38 |
(x y : A * B) |
| 39 |
: prod_beq eq_A eq_B x y = true -> x = y. |
| 40 |
Proof. |
| 41 |
destruct x as [a b], y as [a' b']. |
| 42 |
specialize (A_bl a a'); specialize (B_bl b b'). |
| 43 |
cbn; intro H; destruct A_bl; [ | destruct B_bl; [ | reflexivity ] ]; |
| 44 |
edestruct eq_A; edestruct eq_B; cbn in *; try reflexivity; try assumption. |
| 45 |
Defined. |
| 46 |
Lemma prod_dec_lb A B eq_A eq_B |
| 47 |
(A_lb : forall x y : A, x = y -> eq_A x y = true) |
| 48 |
(B_lb : forall x y : B, x = y -> eq_B x y = true) |
| 49 |
(x y : A * B) |
| 50 |
: x = y -> prod_beq eq_A eq_B x y = true. |
| 51 |
Proof. |
| 52 |
intro H; specialize (A_lb _ _ (f_equal fst H)); specialize (B_lb _ _ (f_equal snd H)). |
| 53 |
cbn in *; cbv [fst snd] in *. |
| 54 |
rewrite A_lb, B_lb; reflexivity. |
| 55 |
Defined. |
| 56 |
Definition prod_eq_dec A B eq_A eq_B |
| 57 |
(A_bl : forall x y : A, eq_A x y = true -> x = y) |
| 58 |
(B_bl : forall x y : B, eq_B x y = true -> x = y) |
| 59 |
(A_lb : forall x y : A, x = y -> eq_A x y = true) |
| 60 |
(B_lb : forall x y : B, x = y -> eq_B x y = true) |
| 61 |
(x y : A * B) |
| 62 |
: {x = y} + {x <> y}. |
| 63 |
Proof. |
| 64 |
refine (let H := let b := @prod_beq A B eq_A eq_B x y in if b as b0 return ({b0 = true} + {b0 = false}) then left eq_refl else right eq_refl in |
| 65 |
match H with |
| 66 |
| left e => left (@prod_dec_bl A B eq_A eq_B A_bl B_bl x y e) |
| 67 |
| right H' => right (fun e => _ (@prod_dec_lb A B eq_A eq_B A_lb B_lb x y e)) |
| 68 |
end). |
| 69 |
congruence. |
| 70 |
Defined. |
| 71 |
End Primitive. |
| 72 |
Notation "x * y" := (@prod x%primproj_type y%primproj_type) : primproj_type_scope. |
| 73 |
Notation "( x , y , .. , z )" := (pair .. (pair x%primproj y%primproj) .. z%primproj) : primproj_scope. |
| 74 |
|
| 75 |
Local Arguments f_equal {_ _} _ {_ _} _. |
| 76 |
|
| 77 |
Definition fst_pair {A B} (a:A) (b:B) : fst (a,b) = a := eq_refl. |
| 78 |
Definition snd_pair {A B} (a:A) (b:B) : snd (a,b) = b := eq_refl. |
| 79 |
Create HintDb cancel_primpair discriminated. #[global] Hint Rewrite @fst_pair @snd_pair : cancel_primpair. |
| 80 |
|
| 81 |
(** ** Equality for [prod] *) |
| 82 |
Section prod. |
| 83 |
(** *** Projecting an equality of a pair to equality of the first components *) |
| 84 |
Definition fst_path {A B} {u v : prod A B} (p : u = v) |
| 85 |
: fst u = fst v |
| 86 |
:= f_equal fst p. |
| 87 |
|
| 88 |
(** *** Projecting an equality of a pair to equality of the second components *) |
| 89 |
Definition snd_path {A B} {u v : prod A B} (p : u = v) |
| 90 |
: snd u = snd v |
| 91 |
:= f_equal snd p. |
| 92 |
|
| 93 |
(** *** Equality of [prod] is itself a [prod] *) |
| 94 |
Definition path_prod_uncurried {A B : Type} (u v : prod A B) |
| 95 |
(pq : prod (fst u = fst v) (snd u = snd v)) |
| 96 |
: u = v. |
| 97 |
Proof. |
| 98 |
destruct u as [u1 u2], v as [v1 v2]; simpl in *. |
| 99 |
destruct pq as [p q]. |
| 100 |
destruct p, q; simpl in *. |
| 101 |
reflexivity. |
| 102 |
Defined. |
| 103 |
|
| 104 |
(** *** Curried version of proving equality of sigma types *) |
| 105 |
Definition path_prod {A B : Type} (u v : prod A B) |
| 106 |
(p : fst u = fst v) (q : snd u = snd v) |
| 107 |
: u = v |
| 108 |
:= path_prod_uncurried u v (pair p q). |
| 109 |
|
| 110 |
(** *** Equivalence of equality of [prod] with a [prod] of equality *) |
| 111 |
(** We could actually use [IsIso] here, but for simplicity, we |
| 112 |
don't. If we wanted to deal with proofs of equality of × types |
| 113 |
in dependent positions, we'd need it. *) |
| 114 |
Definition path_prod_uncurried_iff {A B} |
| 115 |
(u v : @prod A B) |
| 116 |
: u = v <-> (prod (fst u = fst v) (snd u = snd v)). |
| 117 |
Proof. |
| 118 |
split; [ intro; subst; split; reflexivity | apply path_prod_uncurried ]. |
| 119 |
Defined. |
| 120 |
|
| 121 |
(** *** Eta-expansion of [@eq (prod _ _)] *) |
| 122 |
Definition path_prod_eta {A B} {u v : @prod A B} (p : u = v) |
| 123 |
: p = path_prod_uncurried u v (fst_path p, snd_path p). |
| 124 |
Proof. destruct p; reflexivity. Defined. |
| 125 |
|
| 126 |
(** *** Induction principle for [@eq (prod _ _)] *) |
| 127 |
Definition path_prod_rect {A B} {u v : @prod A B} (P : u = v -> Type) |
| 128 |
(f : forall p q, P (path_prod_uncurried u v (p, q))) |
| 129 |
: forall p, P p. |
| 130 |
Proof. intro p; specialize (f (fst_path p) (snd_path p)); destruct p; exact f. Defined. |
| 131 |
Definition path_prod_rec {A B u v} (P : u = v :> @prod A B -> Set) := path_prod_rect P. |
| 132 |
Definition path_prod_ind {A B u v} (P : u = v :> @prod A B -> Prop) := path_prod_rec P. |
| 133 |
End prod. |
| 134 |
|
| 135 |
Lemma prod_iff_and (A B : Prop) : (A /\ B) <-> (A * B). |
| 136 |
Proof. repeat (intros [? ?] || intro || split); assumption. Defined. |
| 137 |
|
| 138 |
Global Instance iff_prod_Proper |
| 139 |
: Proper (iff ==> iff ==> iff) (fun A B => prod A B). |
| 140 |
Proof. repeat intro; tauto. Defined. |
| 141 |
Global Instance iff_iffTp_prod_Proper |
| 142 |
: Proper (iff ==> iffTp ==> iffTp) (fun A B => prod A B) | 1. |
| 143 |
Proof. |
| 144 |
intros ?? [?] ?? [?]; constructor; tauto. |
| 145 |
Defined. |
| 146 |
Global Instance iffTp_iff_prod_Proper |
| 147 |
: Proper (iffTp ==> iff ==> iffTp) (fun A B => prod A B) | 1. |
| 148 |
Proof. |
| 149 |
intros ?? [?] ?? [?]; constructor; tauto. |
| 150 |
Defined. |
| 151 |
Global Instance iffTp_iffTp_prod_Proper |
| 152 |
: Proper (iffTp ==> iffTp ==> iffTp) (fun A B => prod A B) | 1. |
| 153 |
Proof. |
| 154 |
intros ?? [?] ?? [?]; constructor; tauto. |
| 155 |
Defined. |
| 156 |
Global Hint Extern 2 (Proper _ prod) => apply iffTp_iffTp_prod_Proper : typeclass_instances. |
| 157 |
Global Hint Extern 2 (Proper _ (fun A => prod A)) => refine iff_iffTp_prod_Proper : typeclass_instances. |
| 158 |
Global Hint Extern 2 (Proper _ (fun A B => prod A B)) => refine iff_prod_Proper : typeclass_instances. |
| 159 |
|
| 160 |
(** ** Useful Tactics *) |
| 161 |
(** *** [inversion_prod] *) |
| 162 |
Ltac simpl_proj_pair_in H := |
| 163 |
repeat match type of H with |
| 164 |
| context G[fst (pair ?x ?p)] |
| 165 |
=> let G' := context G[x] in change G' in H |
| 166 |
| context G[snd (pair ?x ?p)] |
| 167 |
=> let G' := context G[p] in change G' in H |
| 168 |
end. |
| 169 |
Ltac induction_path_prod H := |
| 170 |
let H0 := fresh H in |
| 171 |
let H1 := fresh H in |
| 172 |
induction H as [H0 H1] using path_prod_rect; |
| 173 |
simpl_proj_pair_in H0; |
| 174 |
simpl_proj_pair_in H1. |
| 175 |
Ltac inversion_prod_step := |
| 176 |
match goal with |
| 177 |
| [ H : _ = pair _ _ |- _ ] |
| 178 |
=> induction_path_prod H |
| 179 |
| [ H : pair _ _ = _ |- _ ] |
| 180 |
=> induction_path_prod H |
| 181 |
end. |
| 182 |
Ltac inversion_prod := repeat inversion_prod_step. |
| 183 |
|
| 184 |
(** *** [subst_prod] *) |
| 185 |
(** The tactic [subst_prod] is like [subst], but it works on equations |
| 186 |
of the form [_ = (x, y)] and [(x, y) = _] for [x] and [y] |
| 187 |
identifiers. *) |
| 188 |
Ltac do_subst_prod A B x y := |
| 189 |
is_var x; is_var y; |
| 190 |
let H := fresh in |
| 191 |
let xy := fresh x in |
| 192 |
remember (@pair A B x y) as xy eqn:H; |
| 193 |
assert (fst xy = x) by (subst xy; reflexivity); |
| 194 |
assert (snd xy = y) by (subst xy; reflexivity); |
| 195 |
subst x y; |
| 196 |
clear H; try subst xy. |
| 197 |
Ltac subst_prod_step := |
| 198 |
match goal with |
| 199 |
| [ H : _ = @pair ?A ?B ?x ?y |- _ ] => do_subst_prod A B x y |
| 200 |
| [ H : @pair ?A ?B ?x ?y = _ |- _ ] => do_subst_prod A B x y |
| 201 |
end. |
| 202 |
Ltac subst_prod := repeat subst_prod_step. |