Skip to content
This repository was archived by the owner on Jul 24, 2024. It is now read-only.

Commit c9ba7a5

Browse files
rwbartonmergify[bot]
authored andcommitted
refactor(category_theory,algebra/category): make algebraic categories not [reducible] (#1491)
* refactor(category_theory,algebra/category): make algebraic categories not [reducible] Adapted from part of #1438. * Update src/algebra/category/CommRing/basic.lean Co-Authored-By: Scott Morrison <scott@tqft.net> * adding missing forget2 instances * Converting Reid's comment to a [Note] * adding examples testing coercions
1 parent 340178d commit c9ba7a5

File tree

11 files changed

+230
-137
lines changed

11 files changed

+230
-137
lines changed

src/algebra/category/CommRing/basic.lean

Lines changed: 56 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Released under Apache 2.0 license as described in the file LICENSE.
44
Authors: Scott Morrison, Johannes Hölzl, Yury Kudryashov
55
-/
66

7-
import algebra.category.Mon.basic
7+
import algebra.category.Group
88
import category_theory.fully_faithful
99
import algebra.ring
1010
import data.int.basic
@@ -18,103 +18,117 @@ We introduce the bundled categories:
1818
* `CommSemiRing`
1919
* `CommRing`
2020
along with the relevant forgetful functors between them.
21+
22+
## Implementation notes
23+
24+
See the note [locally reducible category instances].
25+
2126
-/
2227

2328
universes u v
2429

2530
open category_theory
2631

2732
/-- The category of semirings. -/
28-
@[reducible] def SemiRing : Type (u+1) := bundled semiring
33+
def SemiRing : Type (u+1) := bundled semiring
2934

3035
namespace SemiRing
3136

3237
/-- Construct a bundled SemiRing from the underlying type and typeclass. -/
3338
def of (R : Type u) [semiring R] : SemiRing := bundled.of R
3439

40+
local attribute [reducible] SemiRing
41+
42+
instance : has_coe_to_sort SemiRing := infer_instance
43+
3544
instance (R : SemiRing) : semiring R := R.str
3645

3746
instance bundled_hom : bundled_hom @ring_hom :=
3847
⟨@ring_hom.to_fun, @ring_hom.id, @ring_hom.comp, @ring_hom.coe_inj⟩
3948

40-
instance has_forget_to_Mon : has_forget₂ SemiRing.{u} Mon.{u} :=
41-
bundled_hom.mk_has_forget₂ @semiring.to_monoid (λ R₁ R₂ f, f.to_monoid_hom) (λ _ _ _, rfl)
49+
instance : concrete_category SemiRing := infer_instance
50+
51+
instance has_forget_to_Mon : has_forget₂ SemiRing Mon :=
52+
bundled_hom.mk_has_forget₂ @semiring.to_monoid (λ R₁ R₂, ring_hom.to_monoid_hom) (λ _ _ _, rfl)
53+
instance has_forget_to_AddCommMon : has_forget₂ SemiRing AddCommMon :=
54+
-- can't use bundled_hom.mk_has_forget₂, since AddCommMon is an induced category
55+
{ forget₂ :=
56+
{ obj := λ R, AddCommMon.of R,
57+
map := λ R₁ R₂ f, ring_hom.to_add_monoid_hom f } }
4258

4359
end SemiRing
4460

4561
/-- The category of rings. -/
46-
@[reducible] def Ring : Type (u+1) := bundled ring
62+
def Ring : Type (u+1) := induced_category SemiRing (bundled.map @ring.to_semiring)
4763

4864
namespace Ring
4965

50-
instance (R : Ring) : ring R := R.str
51-
5266
/-- Construct a bundled Ring from the underlying type and typeclass. -/
5367
def of (R : Type u) [ring R] : Ring := bundled.of R
5468

55-
instance bundled_hom : bundled_hom _ :=
56-
SemiRing.bundled_hom.full_subcategory @ring.to_semiring
69+
local attribute [reducible] Ring
70+
71+
instance : has_coe_to_sort Ring := infer_instance
72+
73+
instance (R : Ring) : ring R := R.str
74+
75+
instance : concrete_category Ring := infer_instance
5776

58-
instance has_forget_to_SemiRing : has_forget₂ Ring.{u} SemiRing.{u} :=
59-
SemiRing.bundled_hom.full_subcategory_has_forget₂ _
77+
instance has_forget_to_SemiRing : has_forget₂ Ring SemiRing := infer_instance
78+
instance has_forget_to_AddCommGroup : has_forget₂ Ring AddCommGroup :=
79+
-- can't use bundled_hom.mk_has_forget₂, since AddCommGroup is an induced category
80+
{ forget₂ :=
81+
{ obj := λ R, AddCommGroup.of R,
82+
map := λ R₁ R₂ f, ring_hom.to_add_monoid_hom f } }
6083

6184
end Ring
6285

6386
/-- The category of commutative semirings. -/
64-
@[reducible] def CommSemiRing : Type (u+1) := bundled comm_semiring
87+
def CommSemiRing : Type (u+1) := induced_category SemiRing (bundled.map comm_semiring.to_semiring)
6588

6689
namespace CommSemiRing
6790

68-
instance (R : CommSemiRing) : comm_semiring R := R.str
69-
7091
/-- Construct a bundled CommSemiRing from the underlying type and typeclass. -/
7192
def of (R : Type u) [comm_semiring R] : CommSemiRing := bundled.of R
7293

73-
instance bundled_hom : bundled_hom _ :=
74-
SemiRing.bundled_hom.full_subcategory @comm_semiring.to_semiring
94+
local attribute [reducible] CommSemiRing
95+
96+
instance : has_coe_to_sort CommSemiRing := infer_instance
97+
98+
instance (R : CommSemiRing) : comm_semiring R := R.str
7599

76-
instance has_forget_to_SemiRing : has_forget₂ CommSemiRing.{u} SemiRing.{u} :=
77-
bundled_hom.full_subcategory_has_forget₂ _ _
100+
instance : concrete_category CommSemiRing := infer_instance
101+
102+
instance has_forget_to_SemiRing : has_forget₂ CommSemiRing SemiRing := infer_instance
78103

79104
/-- The forgetful functor from commutative rings to (multiplicative) commutative monoids. -/
80-
instance has_forget_to_CommMon : has_forget₂ CommSemiRing.{u} CommMon.{u} :=
81-
bundled_hom.mk_has_forget₂
82-
@comm_semiring.to_comm_monoid
83-
(λ R₁ R₂ f, f.to_monoid_hom)
84-
(by intros; refl)
105+
instance has_forget_to_CommMon : has_forget₂ CommSemiRing CommMon :=
106+
has_forget₂.mk'
107+
(λ R : CommSemiRing, CommMon.of R) (λ R, rfl)
108+
(λ R₁ R₂ f, f.to_monoid_hom) (by tidy)
85109

86110
end CommSemiRing
87111

88112
/-- The category of commutative rings. -/
89-
@[reducible] def CommRing : Type (u+1) := bundled comm_ring
113+
def CommRing : Type (u+1) := induced_category Ring (bundled.map comm_ring.to_ring)
90114

91115
namespace CommRing
92116

93-
instance (R : CommRing) : comm_ring R := R.str
94-
95117
/-- Construct a bundled CommRing from the underlying type and typeclass. -/
96118
def of (R : Type u) [comm_ring R] : CommRing := bundled.of R
97119

98-
instance bundled_hom : bundled_hom _ :=
99-
Ring.bundled_hom.full_subcategory @comm_ring.to_ring
120+
local attribute [reducible] CommRing
100121

101-
@[simp] lemma id_eq (R : CommRing) : 𝟙 R = ring_hom.id R := rfl
102-
@[simp] lemma comp_eq {R₁ R₂ R₃ : CommRing} (f : R₁ ⟶ R₂) (g : R₂ ⟶ R₃) :
103-
f ≫ g = g.comp f := rfl
122+
instance : has_coe_to_sort CommRing := infer_instance
123+
124+
instance (R : CommRing) : comm_ring R := R.str
104125

105-
@[simp] lemma forget_obj_eq_coe {R : CommRing} : (forget CommRing).obj R = R := rfl
106-
@[simp] lemma forget_map_eq_coe {R₁ R₂ : CommRing} (f : R₁ ⟶ R₂) :
107-
(forget CommRing).map f = f :=
108-
rfl
126+
instance : concrete_category CommRing := infer_instance
109127

110-
instance has_forget_to_Ring : has_forget₂ CommRing.{u} Ring.{u} :=
111-
by apply bundled_hom.full_subcategory_has_forget₂
128+
instance has_forget_to_Ring : has_forget₂ CommRing Ring := infer_instance
112129

113130
/-- The forgetful functor from commutative rings to (multiplicative) commutative monoids. -/
114-
instance has_forget_to_CommSemiRing : has_forget₂ CommRing.{u} CommSemiRing.{u} :=
115-
bundled_hom.mk_has_forget₂
116-
@comm_ring.to_comm_semiring
117-
(λ _ _, id)
118-
(by intros; refl)
131+
instance has_forget_to_CommSemiRing : has_forget₂ CommRing CommSemiRing :=
132+
has_forget₂.mk' (λ R : CommRing, CommSemiRing.of R) (λ R, rfl) (λ R₁ R₂ f, f) (by tidy)
119133

120134
end CommRing

src/algebra/category/CommRing/colimits.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ begin
320320
-- trans
321321
{ exact eq.trans r_ih_h r_ih_k },
322322
-- map
323-
{ rw cocone.naturality_bundled, },
323+
{ rw cocone.naturality_concrete, },
324324
-- zero
325325
{ erw is_ring_hom.map_zero ⇑((s.ι).app r), refl },
326326
-- one

src/algebra/category/CommRing/limits.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ variables {J : Type u} [small_category J]
3131

3232
instance comm_ring_obj (F : J ⥤ CommRing.{u}) (j) :
3333
comm_ring ((F ⋙ forget CommRing).obj j) :=
34-
by { dsimp, apply_instance }
34+
by { change comm_ring (F.obj j), apply_instance }
3535

3636
instance sections_submonoid (F : J ⥤ CommRing.{u}) :
3737
is_submonoid (F ⋙ forget CommRing).sections :=

src/algebra/category/Group.lean

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -16,61 +16,71 @@ We introduce the bundled categories:
1616
* `CommGroup`
1717
* `AddCommGroup`
1818
along with the relevant forgetful functors between them, and to the bundled monoid categories.
19+
20+
## Implementation notes
21+
22+
See the note [locally reducible category instances].
1923
-/
2024

2125
universes u v
2226

2327
open category_theory
2428

2529
/-- The category of groups and group morphisms. -/
26-
@[reducible, to_additive AddGroup]
27-
def Group : Type (u+1) := bundled group
30+
@[to_additive AddGroup]
31+
def Group : Type (u+1) := induced_category Mon (bundled.map group.to_monoid)
2832

2933
namespace Group
3034

31-
@[to_additive add_group]
32-
instance (G : Group) : group G := G.str
33-
3435
/-- Construct a bundled Group from the underlying type and typeclass. -/
3536
@[to_additive] def of (X : Type u) [group X] : Group := bundled.of X
3637

38+
local attribute [reducible] Group
39+
3740
@[to_additive]
38-
instance bundled_hom : bundled_hom _ :=
39-
Mon.bundled_hom.full_subcategory @group.to_monoid
41+
instance : has_coe_to_sort Group := infer_instance
42+
43+
@[to_additive add_group]
44+
instance (G : Group) : group G := G.str
4045

4146
@[to_additive]
4247
instance : has_one Group := ⟨Group.of punit⟩
4348

49+
@[to_additive]
50+
instance : concrete_category Group := infer_instance
51+
4452
@[to_additive has_forget_to_AddMon]
45-
instance has_forget_to_Mon : has_forget₂ Group.{u} Mon.{u} :=
46-
Mon.bundled_hom.full_subcategory_has_forget₂ _
53+
instance has_forget_to_Mon : has_forget₂ Group Mon := infer_instance
4754

4855
end Group
4956

5057

5158
/-- The category of commutative groups and group morphisms. -/
52-
@[reducible, to_additive AddCommGroup]
53-
def CommGroup : Type (u+1) := bundled comm_group
59+
@[to_additive AddCommGroup]
60+
def CommGroup : Type (u+1) := induced_category Group (bundled.map comm_group.to_group)
5461

5562
namespace CommGroup
5663

64+
/-- Construct a bundled CommGroup from the underlying type and typeclass. -/
65+
@[to_additive] def of (G : Type u) [comm_group G] : CommGroup := bundled.of G
66+
67+
local attribute [reducible] CommGroup
68+
69+
@[to_additive]
70+
instance : has_coe_to_sort CommGroup := infer_instance
71+
5772
@[to_additive add_comm_group]
5873
instance (G : CommGroup) : comm_group G := G.str
5974

60-
/-- Construct a bundled CommGroup from the underlying type and typeclass. -/
61-
@[to_additive] def of (G : Type u) [comm_group G] : CommGroup := bundled.of G
75+
@[to_additive] instance : has_one CommGroup := ⟨CommGroup.of punit⟩
6276

63-
@[to_additive] instance : bundled_hom _ :=
64-
Group.bundled_hom.full_subcategory @comm_group.to_group
77+
@[to_additive] instance : concrete_category CommGroup := infer_instance
6578

6679
@[to_additive has_forget_to_AddGroup]
67-
instance has_forget_to_Group : has_forget₂ CommGroup.{u} Group.{u} :=
68-
Group.bundled_hom.full_subcategory_has_forget₂ _
80+
instance has_forget_to_Group : has_forget₂ CommGroup Group := infer_instance
6981

7082
@[to_additive has_forget_to_AddCommMon]
71-
instance has_forget_to_CommMon : has_forget₂ CommGroup.{u} CommMon.{u} :=
72-
CommMon.bundled_hom.full_subcategory_has_forget₂ comm_group.to_comm_monoid
73-
74-
@[to_additive] instance : has_one CommGroup := ⟨CommGroup.of punit⟩
83+
instance has_forget_to_CommMon : has_forget₂ CommGroup CommMon :=
84+
induced_category.has_forget₂ (λ G : CommGroup, CommMon.of G)
7585

7686
end CommGroup

src/algebra/category/Mon/basic.lean

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,50 +16,87 @@ We introduce the bundled categories:
1616
* `CommMon`
1717
* `AddCommMon`
1818
along with the relevant forgetful functors between them.
19+
20+
## Implementation notes
21+
22+
### Note [locally reducible category instances]
23+
24+
We make SemiRing (and the other categories) locally reducible in order
25+
to define its instances. This is because writing, for example,
26+
27+
instance : concrete_category SemiRing := by { delta SemiRing, apply_instance }
28+
29+
results in an instance of the form `id (bundled_hom.concrete_category _)`
30+
and this `id`, not being [reducible], prevents a later instance search
31+
(once SemiRing is no longer reducible) from seeing that the morphisms of
32+
SemiRing are really semiring morphisms (`→+*`), and therefore have a coercion
33+
to functions, for example. It's especially important that the `has_coe_to_sort`
34+
instance not contain an extra `id` as we want the `semiring ↥R` instance to
35+
also apply to `semiring R.α` (it seems to be impractical to guarantee that
36+
we always access `R.α` through the coercion rather than directly).
37+
38+
TODO: Probably @[derive] should be able to create instances of the
39+
required form (without `id`), and then we could use that instead of
40+
this obscure `local attribute [reducible]` method.
1941
-/
2042

2143
universes u v
2244

2345
open category_theory
2446

2547
/-- The category of monoids and monoid morphisms. -/
26-
@[reducible, to_additive AddMon]
48+
@[to_additive AddMon]
2749
def Mon : Type (u+1) := bundled monoid
2850

2951
namespace Mon
3052

31-
@[to_additive add_monoid]
32-
instance (x : Mon) : monoid x := x.str
33-
3453
/-- Construct a bundled Mon from the underlying type and typeclass. -/
3554
@[to_additive]
3655
def of (M : Type u) [monoid M] : Mon := bundled.of M
3756

57+
local attribute [reducible] Mon
58+
59+
@[to_additive]
60+
instance : has_coe_to_sort Mon := infer_instance
61+
62+
@[to_additive add_monoid]
63+
instance (M : Mon) : monoid M := M.str
64+
3865
@[to_additive]
3966
instance bundled_hom : bundled_hom @monoid_hom :=
4067
⟨@monoid_hom.to_fun, @monoid_hom.id, @monoid_hom.comp, @monoid_hom.coe_inj⟩
4168

69+
@[to_additive]
70+
instance : concrete_category Mon := infer_instance
71+
4272
end Mon
4373

4474
/-- The category of commutative monoids and monoid morphisms. -/
45-
@[reducible, to_additive AddCommMon]
46-
def CommMon : Type (u+1) := bundled comm_monoid
75+
@[to_additive AddCommMon]
76+
def CommMon : Type (u+1) := induced_category Mon (bundled.map @comm_monoid.to_monoid)
4777

4878
namespace CommMon
4979

50-
@[to_additive add_comm_monoid]
51-
instance (x : CommMon) : comm_monoid x := x.str
52-
5380
/-- Construct a bundled CommMon from the underlying type and typeclass. -/
5481
@[to_additive]
55-
def of (X : Type u) [comm_monoid X] : CommMon := bundled.of X
82+
def of (M : Type u) [comm_monoid M] : CommMon := bundled.of M
83+
84+
local attribute [reducible] CommMon
5685

5786
@[to_additive]
58-
instance bundled_hom : bundled_hom _ :=
59-
Mon.bundled_hom.full_subcategory @comm_monoid.to_monoid
87+
instance : has_coe_to_sort CommMon := infer_instance
88+
89+
@[to_additive add_comm_monoid]
90+
instance (M : CommMon) : comm_monoid M := M.str
91+
92+
@[to_additive]
93+
instance : concrete_category CommMon := infer_instance
6094

6195
@[to_additive has_forget_to_AddMon]
62-
instance has_forget_to_Mon : has_forget₂ CommMon.{u} Mon.{u} :=
63-
Mon.bundled_hom.full_subcategory_has_forget₂ _
96+
instance has_forget_to_Mon : has_forget₂ CommMon Mon := infer_instance
6497

6598
end CommMon
99+
100+
-- We verify that the coercions of morphisms to functions work correctly:
101+
example {R S : Mon} (f : R ⟶ S) : (R : Type) → (S : Type) := f
102+
example {R S : CommMon} (f : R ⟶ S) : (R : Type) → (S : Type) := f

src/algebra/category/Mon/colimits.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ begin
172172
-- trans
173173
{ exact eq.trans r_ih_h r_ih_k },
174174
-- map
175-
{ rw cocone.naturality_bundled, },
175+
{ rw cocone.naturality_concrete, },
176176
-- mul
177177
{ rw is_monoid_hom.map_mul ⇑((s.ι).app r_j) },
178178
-- one

0 commit comments

Comments
 (0)