Skip to content

Commit c04865a

Browse files
committed
SetoidRewrite: various cleanups and simplifications
1 parent 85cf65e commit c04865a

File tree

1 file changed

+42
-55
lines changed

1 file changed

+42
-55
lines changed

contrib/SetoidRewrite.v

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
(* Typeclass instances to allow rewriting in categories. Examples are given later in the file. *)
1+
(** * Typeclass instances to allow rewriting in categories *)
22

3-
(* Init.Tactics contains the definition of the Coq stdlib typeclass_inferences database. It must be imported before Basics.Overture. *)
3+
(** The file using the setoid rewriting machinery from the Coq standard library to allow rewriting in wild-categories. Examples are given later in the file. *)
44

5-
(** Warning: This imports Coq.Setoids.Setoid from the standard library. Currently the setoid rewriting machinery requires this to work, it depends on this file explicitly. This imports the whole standard library into the namespace.
6-
7-
All files that import WildCat/SetoidRewrite.v will also recursively import the entire Coq.Init standard library. *)
8-
9-
(** Because of this, this file needs to be the *first* file Require'd in any file that uses it. Otherwise, the typeclasses hintdb is cleared, breaking typeclass inference. Moreover, if Foo Requires this file, then Foo must also be the first file Require'd in any file that Requires Foo, and so on. In the long term it would be good if this could be avoided.*)
5+
(** Init.Tactics contains the definition of the Coq stdlib typeclass_inferences database. It must be imported before Basics.Overture. Otherwise, the typeclasses hintdb is cleared, breaking typeclass inference. Because of this, this file also needs to be the first file Require'd in any file that uses it. Moreover, if Foo Requires this file, then Foo must also be the first file Require'd in any file that Requires Foo, and so on. In the long term it would be good if this could be avoided. *)
106

117
#[warnings="-deprecated-from-Coq"]
128
From Coq Require Init.Tactics.
@@ -18,11 +14,13 @@ Import CMorphisms.ProperNotations.
1814
From HoTT Require Import WildCat.Core
1915
WildCat.NatTrans WildCat.Equiv.
2016

17+
(** ** Setoid rewriting *)
18+
2119
#[export] Instance reflexive_proper_proxy {A : Type}
2220
{R : Relation A} `(Reflexive A R) (x : A)
2321
: CMorphisms.ProperProxy R x := reflexivity x.
2422

25-
(* forall (x y : A), x $== y -> forall (a b : A), a $== b -> y $== b -> x $==a *)
23+
(** forall (x y : A), x $== y -> forall (a b : A), a $== b -> y $== b -> x $==a *)
2624
#[export] Instance IsProper_GpdHom_from {A : Type} `{Is0Gpd A}
2725
: CMorphisms.Proper
2826
(GpdHom ==>
@@ -32,22 +30,25 @@ Proof.
3230
intros x y eq_xy a b eq_ab eq_yb.
3331
exact (transitivity eq_xy (transitivity eq_yb
3432
(symmetry _ _ eq_ab))).
33+
(* We could also just write:
34+
exact (eq_xy $@ eq_yb $@ eq_ab^$).
35+
The advantage of the above proof is that it works for other transitive and symmetric relations. *)
3536
Defined.
3637

37-
(* forall (x y : A), x $== y -> forall (a b : A), a $== b -> x $== a -> y $== b *)
38+
(** forall (x y : A), x $== y -> forall (a b : A), a $== b -> x $== a -> y $== b *)
3839
#[export] Instance IsProper_GpdHom_to {A : Type} `{Is0Gpd A}
3940
: CMorphisms.Proper
4041
(GpdHom ==>
4142
GpdHom ==>
4243
CRelationClasses.arrow) GpdHom.
4344
Proof.
44-
intros x y eq_xy a b eq_ab eq_yb.
45-
unshelve refine (transitivity _ eq_ab).
46-
unshelve refine (transitivity _ eq_yb).
45+
intros x y eq_xy a b eq_ab eq_xa.
46+
refine (transitivity _ eq_ab).
47+
refine (transitivity _ eq_xa).
4748
exact (symmetry _ _ eq_xy).
4849
Defined.
4950

50-
(* forall a : A, x $== y -> a $== x -> a $== y *)
51+
(** forall a : A, x $== y -> a $== x -> a $== y *)
5152
#[export] Instance IsProper_GpdHom_to_a {A : Type} `{Is0Gpd A}
5253
{a : A}
5354
: CMorphisms.Proper
@@ -58,7 +59,7 @@ Proof.
5859
by transitivity x.
5960
Defined.
6061

61-
(* forall a : A, x $== y -> a $== y -> a $== x *)
62+
(** forall a : A, x $== y -> a $== y -> a $== x *)
6263
#[export] Instance IsProper_GpdHom_from_a {A : Type} `{Is0Gpd A}
6364
{a : A}
6465
: CMorphisms.Proper
@@ -76,8 +77,8 @@ Open Scope signatureT_scope.
7677
(H0 : CMorphisms.Proper (R ++> R') f)
7778
: CMorphisms.Proper (R --> R') f.
7879
Proof.
79-
intros a b Rab.
80-
apply H0. unfold CRelationClasses.flip. symmetry. exact Rab.
80+
intros a b Rba; unfold CRelationClasses.flip in Rba.
81+
apply H0. symmetry. exact Rba.
8182
Defined.
8283

8384
#[export] Instance symmetric_flip_snd {A B C : Type} {R : Relation A}
@@ -90,38 +91,34 @@ Defined.
9091

9192
#[export] Instance IsProper_fmap {A B : Type} `{Is1Cat A}
9293
`{Is1Cat A} (F : A -> B) `{Is1Functor _ _ F} (a b : A)
93-
: CMorphisms.Proper (GpdHom ==> GpdHom) (@fmap _ _ _ _ F _ a b) := fun _ _ eq => fmap2 F eq.
94+
: CMorphisms.Proper (GpdHom ==> GpdHom) (@fmap _ _ _ _ F _ a b)
95+
:= fun _ _ => fmap2 F.
9496

9597
#[export] Instance IsProper_catcomp_g {A : Type} `{Is1Cat A}
9698
{a b c : A} (g : b $-> c)
97-
: CMorphisms.Proper (GpdHom ==> GpdHom) (@cat_comp _ _ _ a b c g).
98-
Proof.
99-
intros f1 f2.
100-
apply (is0functor_postcomp a b c g ).
101-
Defined.
99+
: CMorphisms.Proper (GpdHom ==> GpdHom) (@cat_comp _ _ _ a b c g)
100+
:= fun _ _ => cat_postwhisker g.
102101

103102
#[export] Instance IsProper_catcomp {A : Type} `{Is1Cat A}
104103
{a b c : A}
105104
: CMorphisms.Proper (GpdHom ==> GpdHom ==> GpdHom)
106105
(@cat_comp _ _ _ a b c).
107106
Proof.
108107
intros g1 g2 eq_g f1 f2 eq_f.
109-
rewrite eq_f.
110-
apply (is0functor_precomp a b c f2).
111-
exact eq_g.
108+
exact (cat_comp2 eq_f eq_g).
112109
Defined.
113110

114111
#[export] Instance gpd_hom_to_hom_proper {A B : Type} `{Is0Gpd A}
115112
{R : Relation B} (F : A -> B)
116-
`{CMorphisms.Proper _ (GpdHom ==> R) F}
113+
{H2 : CMorphisms.Proper (GpdHom ==> R) F}
117114
: CMorphisms.Proper (Hom ==> R) F.
118115
Proof.
119116
intros a b eq_ab; apply H2; exact eq_ab.
120117
Defined.
121118

122119
#[export] Instance gpd_hom_is_proper1 {A : Type} `{Is0Gpd A}
123-
: CMorphisms.Proper
124-
(Hom ==> Hom ==> CRelationClasses.arrow) Hom.
120+
: CMorphisms.Proper
121+
(Hom ==> Hom ==> CRelationClasses.arrow) Hom.
125122
Proof.
126123
intros x y eq_xy a b eq_ab f.
127124
refine (transitivity _ eq_ab).
@@ -130,35 +127,34 @@ Proof.
130127
Defined.
131128

132129
#[export] Instance transitive_hom {A : Type} `{Is01Cat A} {x : A}
133-
: CMorphisms.Proper
134-
(Hom ==> CRelationClasses.arrow) (Hom x).
135-
Proof.
136-
intros y z g f.
137-
exact (g $o f).
138-
Defined.
130+
: CMorphisms.Proper
131+
(Hom ==> CRelationClasses.arrow) (Hom x)
132+
:= fun y z => cat_comp.
133+
134+
(** ** Examples of setoid rewriting *)
139135

136+
(** Rewriting works in hypotheses. *)
140137
Proposition IsEpic_HasSection {A} `{Is1Cat A}
141138
{a b : A} (f : a $-> b) :
142139
SectionOf f -> Epic f.
143140
Proof.
144-
intros section c g h eq_gf_hf.
145-
destruct section as [right_inverse is_section].
146-
apply (is0functor_precomp _ _ _ right_inverse) in eq_gf_hf;
141+
intros [right_inverse is_section] c g h eq_gf_hf.
142+
apply (fmap (cat_precomp _ right_inverse)) in eq_gf_hf;
147143
unfold cat_precomp in eq_gf_hf.
148144
rewrite 2 cat_assoc, is_section, 2 cat_idr in eq_gf_hf.
149145
exact eq_gf_hf.
150146
Defined.
151147

148+
(** A different approach, working in the goal. *)
152149
Proposition IsMonic_HasRetraction {A} `{Is1Cat A}
153150
{b c : A} (f : b $-> c) :
154151
RetractionOf f -> Monic f.
155152
Proof.
156-
intros retraction a g h eq_fg_fh.
157-
destruct retraction as [left_inverse is_retraction].
158-
apply (is0functor_postcomp _ _ _ left_inverse) in eq_fg_fh;
159-
unfold cat_postcomp in eq_fg_fh.
160-
rewrite <- 2 cat_assoc, is_retraction, 2 cat_idl in eq_fg_fh.
161-
assumption.
153+
intros [left_inverse is_retraction] a g h eq_fg_fh.
154+
rewrite <- (cat_idl g), <- (cat_idl h).
155+
rewrite <- is_retraction.
156+
rewrite 2 cat_assoc.
157+
exact (_ $@L eq_fg_fh).
162158
Defined.
163159

164160
Proposition nat_equiv_faithful {A B : Type}
@@ -168,19 +164,10 @@ Proposition nat_equiv_faithful {A B : Type}
168164
: Faithful F -> Faithful G.
169165
Proof.
170166
intros faithful_F x y f g eq_Gf_Gg.
171-
apply (@fmap _ _ _ _ _ (is0functor_precomp _
172-
_ _ (cat_equiv_natequiv tau x))) in eq_Gf_Gg.
173-
cbn in eq_Gf_Gg.
174-
unfold cat_precomp in eq_Gf_Gg.
175-
rewrite <- 2 (isnat tau) in eq_Gf_Gg.
176167
apply faithful_F.
177-
assert (X : RetractionOf (tau y)). {
178-
unshelve eapply Build_RetractionOf.
179-
- exact ((tau y)^-1$).
180-
- exact (cate_issect _ ).
181-
}
182-
apply IsMonic_HasRetraction in X.
183-
apply X in eq_Gf_Gg. assumption.
168+
apply (cate_monic_equiv (tau y)).
169+
rewrite 2 (isnat tau).
170+
by apply cat_prewhisker.
184171
Defined.
185172

186173
Section SetoidRewriteTests.

0 commit comments

Comments
 (0)