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

Commit 36a25d9

Browse files
kim-emgebner
andcommitted
feat(algebra/category/*): get rid of the local reducible hack (#3354)
I thought I did this back in April, but apparently never made the PR. We currently use a strange hack when setting up concrete categories, making them locally reducible. There's a library note about this, which ends: ``` TODO: Probably @[derive] should be able to create instances of the required form (without `id`), and then we could use that instead of this obscure `local attribute [reducible]` method. ``` This PR makes the small change required to `delta_instance` to make this happen, and then stops using the hack in setting up concrete categories (and deletes the library note explaining this hack). Co-authored-by: Scott Morrison <scott.morrison@gmail.com> Co-authored-by: Gabriel Ebner <gebner@gebner.org>
1 parent 75b9cfa commit 36a25d9

File tree

13 files changed

+168
-175
lines changed

13 files changed

+168
-175
lines changed

src/algebra/category/CommRing/basic.lean

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@ We introduce the bundled categories:
1515
* `CommSemiRing`
1616
* `CommRing`
1717
along with the relevant forgetful functors between them.
18-
19-
## Implementation notes
20-
21-
See the note [locally reducible category instances].
22-
2318
-/
2419

2520
universes u v
@@ -31,23 +26,18 @@ def SemiRing : Type (u+1) := bundled semiring
3126

3227
namespace SemiRing
3328

29+
instance bundled_hom : bundled_hom @ring_hom :=
30+
⟨@ring_hom.to_fun, @ring_hom.id, @ring_hom.comp, @ring_hom.coe_inj⟩
31+
32+
attribute [derive [has_coe_to_sort, large_category, concrete_category]] SemiRing
33+
3434
/-- Construct a bundled SemiRing from the underlying type and typeclass. -/
3535
def of (R : Type u) [semiring R] : SemiRing := bundled.of R
3636

3737
instance : inhabited SemiRing := ⟨of punit⟩
3838

39-
local attribute [reducible] SemiRing
40-
41-
instance : has_coe_to_sort SemiRing := infer_instance -- short-circuit type class inference
42-
4339
instance (R : SemiRing) : semiring R := R.str
4440

45-
instance bundled_hom : bundled_hom @ring_hom :=
46-
⟨@ring_hom.to_fun, @ring_hom.id, @ring_hom.comp, @ring_hom.coe_inj⟩
47-
48-
instance : category SemiRing := infer_instance -- short-circuit type class inference
49-
instance : concrete_category SemiRing := infer_instance -- short-circuit type class inference
50-
5141
instance has_forget_to_Mon : has_forget₂ SemiRing Mon :=
5242
bundled_hom.mk_has_forget₂
5343
(λ R hR, @monoid_with_zero.to_monoid R (@semiring.to_monoid_with_zero R hR))
@@ -68,20 +58,15 @@ namespace Ring
6858

6959
instance : bundled_hom.parent_projection @ring.to_semiring := ⟨⟩
7060

61+
attribute [derive [has_coe_to_sort, large_category, concrete_category]] Ring
62+
7163
/-- Construct a bundled Ring from the underlying type and typeclass. -/
7264
def of (R : Type u) [ring R] : Ring := bundled.of R
7365

7466
instance : inhabited Ring := ⟨of punit⟩
7567

76-
local attribute [reducible] Ring
77-
78-
instance : has_coe_to_sort Ring := by apply_instance -- short-circuit type class inference
79-
8068
instance (R : Ring) : ring R := R.str
8169

82-
instance : category Ring := infer_instance -- short-circuit type class inference
83-
instance : concrete_category Ring := infer_instance -- short-circuit type class inference
84-
8570
instance has_forget_to_SemiRing : has_forget₂ Ring SemiRing := bundled_hom.forget₂ _ _
8671
instance has_forget_to_AddCommGroup : has_forget₂ Ring AddCommGroup :=
8772
-- can't use bundled_hom.mk_has_forget₂, since AddCommGroup is an induced category
@@ -98,20 +83,15 @@ namespace CommSemiRing
9883

9984
instance : bundled_hom.parent_projection @comm_semiring.to_semiring := ⟨⟩
10085

86+
attribute [derive [has_coe_to_sort, large_category, concrete_category]] CommSemiRing
87+
10188
/-- Construct a bundled CommSemiRing from the underlying type and typeclass. -/
10289
def of (R : Type u) [comm_semiring R] : CommSemiRing := bundled.of R
10390

10491
instance : inhabited CommSemiRing := ⟨of punit⟩
10592

106-
local attribute [reducible] CommSemiRing
107-
108-
instance : has_coe_to_sort CommSemiRing := infer_instance -- short-circuit type class inference
109-
11093
instance (R : CommSemiRing) : comm_semiring R := R.str
11194

112-
instance : category CommSemiRing := infer_instance -- short-circuit type class inference
113-
instance : concrete_category CommSemiRing := infer_instance -- short-circuit type class inference
114-
11595
instance has_forget_to_SemiRing : has_forget₂ CommSemiRing SemiRing := bundled_hom.forget₂ _ _
11696

11797
/-- The forgetful functor from commutative rings to (multiplicative) commutative monoids. -/
@@ -129,20 +109,15 @@ namespace CommRing
129109

130110
instance : bundled_hom.parent_projection @comm_ring.to_ring := ⟨⟩
131111

112+
attribute [derive [has_coe_to_sort, large_category, concrete_category]] CommRing
113+
132114
/-- Construct a bundled CommRing from the underlying type and typeclass. -/
133115
def of (R : Type u) [comm_ring R] : CommRing := bundled.of R
134116

135117
instance : inhabited CommRing := ⟨of punit⟩
136118

137-
local attribute [reducible] CommRing
138-
139-
instance : has_coe_to_sort CommRing := infer_instance -- short-circuit type class inference
140-
141119
instance (R : CommRing) : comm_ring R := R.str
142120

143-
instance : category CommRing := infer_instance -- short-circuit type class inference
144-
instance : concrete_category CommRing := infer_instance -- short-circuit type class inference
145-
146121
instance has_forget_to_Ring : has_forget₂ CommRing Ring := bundled_hom.forget₂ _ _
147122

148123
/-- The forgetful functor from commutative rings to (multiplicative) commutative monoids. -/

src/algebra/category/Group/basic.lean

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@ We introduce the bundled categories:
1515
* `CommGroup`
1616
* `AddCommGroup`
1717
along with the relevant forgetful functors between them, and to the bundled monoid categories.
18-
19-
## Implementation notes
20-
21-
See the note [locally reducible category instances].
2218
-/
2319

2420
universes u v
@@ -37,17 +33,14 @@ namespace Group
3733
@[to_additive]
3834
instance : bundled_hom.parent_projection group.to_monoid := ⟨⟩
3935

36+
attribute [derive [has_coe_to_sort, large_category, concrete_category]] Group AddGroup
37+
4038
/-- Construct a bundled `Group` from the underlying type and typeclass. -/
4139
@[to_additive] def of (X : Type u) [group X] : Group := bundled.of X
4240

4341
/-- Construct a bundled `AddGroup` from the underlying type and typeclass. -/
4442
add_decl_doc AddGroup.of
4543

46-
local attribute [reducible] Group
47-
48-
@[to_additive]
49-
instance : has_coe_to_sort Group := infer_instance -- short-circuit type class inference
50-
5144
@[to_additive add_group]
5245
instance (G : Group) : group G := G.str
5346

@@ -65,13 +58,7 @@ instance : unique (1 : Group) :=
6558
@[simp, to_additive]
6659
lemma one_apply (G H : Group) (g : G) : (1 : G ⟶ H) g = 1 := rfl
6760

68-
@[to_additive]
69-
instance : category Group := infer_instance -- short-circuit type class inference
70-
71-
@[to_additive]
72-
instance : concrete_category Group := infer_instance -- short-circuit type class inference
73-
74-
@[to_additive,ext]
61+
@[to_additive, ext]
7562
lemma ext (G H : Group) (f₁ f₂ : G ⟶ H) (w : ∀ x, f₁ x = f₂ x) : f₁ = f₂ :=
7663
by { ext1, apply w }
7764

@@ -97,17 +84,14 @@ namespace CommGroup
9784
@[to_additive]
9885
instance : bundled_hom.parent_projection comm_group.to_group := ⟨⟩
9986

87+
attribute [derive [has_coe_to_sort, large_category, concrete_category]] CommGroup AddCommGroup
88+
10089
/-- Construct a bundled `CommGroup` from the underlying type and typeclass. -/
10190
@[to_additive] def of (G : Type u) [comm_group G] : CommGroup := bundled.of G
10291

10392
/-- Construct a bundled `AddCommGroup` from the underlying type and typeclass. -/
10493
add_decl_doc AddCommGroup.of
10594

106-
local attribute [reducible] CommGroup
107-
108-
@[to_additive]
109-
instance : has_coe_to_sort CommGroup := infer_instance -- short-circuit type class inference
110-
11195
@[to_additive add_comm_group_instance]
11296
instance comm_group_instance (G : CommGroup) : comm_group G := G.str
11397

@@ -123,12 +107,6 @@ instance : unique (1 : CommGroup) :=
123107
@[simp, to_additive]
124108
lemma one_apply (G H : CommGroup) (g : G) : (1 : G ⟶ H) g = 1 := rfl
125109

126-
@[to_additive]
127-
instance : category CommGroup := infer_instance -- short-circuit type class inference
128-
129-
@[to_additive]
130-
instance : concrete_category CommGroup := infer_instance -- short-circuit type class inference
131-
132110
@[to_additive,ext]
133111
lemma ext (G H : CommGroup) (f₁ f₂ : G ⟶ H) (w : ∀ x, f₁ x = f₂ x) : f₁ = f₂ :=
134112
by { ext1, apply w }

src/algebra/category/Mon/basic.lean

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

4720
universes u v
4821

@@ -57,11 +30,17 @@ add_decl_doc AddMon
5730

5831
namespace Mon
5932

33+
@[to_additive]
34+
instance bundled_hom : bundled_hom @monoid_hom :=
35+
⟨@monoid_hom.to_fun, @monoid_hom.id, @monoid_hom.comp, @monoid_hom.coe_inj⟩
36+
37+
attribute [derive [has_coe_to_sort, large_category, concrete_category]] Mon AddMon
38+
6039
/-- Construct a bundled `Mon` from the underlying type and typeclass. -/
6140
@[to_additive]
6241
def of (M : Type u) [monoid M] : Mon := bundled.of M
6342

64-
/-- Construct a bundled Mon from the underlying type and typeclass. -/
43+
/-- Construct a bundled `Mon` from the underlying type and typeclass. -/
6544
add_decl_doc AddMon.of
6645

6746
@[to_additive]
@@ -70,24 +49,9 @@ instance : inhabited Mon :=
7049
-- which breaks to_additive.
7150
⟨@of punit $ @group.to_monoid _ $ @comm_group.to_group _ punit.comm_group⟩
7251

73-
local attribute [reducible] Mon
74-
75-
@[to_additive]
76-
instance : has_coe_to_sort Mon := infer_instance -- short-circuit type class inference
77-
7852
@[to_additive add_monoid]
7953
instance (M : Mon) : monoid M := M.str
8054

81-
@[to_additive]
82-
instance bundled_hom : bundled_hom @monoid_hom :=
83-
⟨@monoid_hom.to_fun, @monoid_hom.id, @monoid_hom.comp, @monoid_hom.coe_inj⟩
84-
85-
@[to_additive]
86-
instance : category Mon := infer_instance -- short-circuit type class inference
87-
88-
@[to_additive]
89-
instance : concrete_category Mon := infer_instance -- short-circuit type class inference
90-
9155
end Mon
9256

9357
/-- The category of commutative monoids and monoid morphisms. -/
@@ -102,6 +66,8 @@ namespace CommMon
10266
@[to_additive]
10367
instance : bundled_hom.parent_projection comm_monoid.to_monoid := ⟨⟩
10468

69+
attribute [derive [has_coe_to_sort, large_category, concrete_category]] CommMon AddCommMon
70+
10571
/-- Construct a bundled `CommMon` from the underlying type and typeclass. -/
10672
@[to_additive]
10773
def of (M : Type u) [comm_monoid M] : CommMon := bundled.of M
@@ -115,20 +81,9 @@ instance : inhabited CommMon :=
11581
-- which breaks to_additive.
11682
⟨@of punit $ @comm_group.to_comm_monoid _ punit.comm_group⟩
11783

118-
local attribute [reducible] CommMon
119-
120-
@[to_additive]
121-
instance : has_coe_to_sort CommMon := infer_instance -- short-circuit type class inference
122-
12384
@[to_additive add_comm_monoid]
12485
instance (M : CommMon) : comm_monoid M := M.str
12586

126-
@[to_additive]
127-
instance : category CommMon := infer_instance -- short-circuit type class inference
128-
129-
@[to_additive]
130-
instance : concrete_category CommMon := infer_instance -- short-circuit type class inference
131-
13287
@[to_additive has_forget_to_AddMon]
13388
instance has_forget_to_Mon : has_forget₂ CommMon Mon := bundled_hom.forget₂ _ _
13489

src/computability/halting.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ begin
181181
simp at e,
182182
by_cases eval c ∈ C,
183183
{ simp [h] at e, rwa ← e },
184-
{ simp at h, simp [h] at e,
184+
{ simp [h] at e,
185185
rw e at h, contradiction }
186186
end
187187

src/linear_algebra/finsupp.lean

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,9 @@ begin
181181
rintro l ⟨⟩,
182182
apply finsupp.induction l, {exact zero_mem _},
183183
refine λ x a l hl a0, add_mem _ _,
184-
haveI := classical.dec_pred (λ x, ∃ i, x ∈ s i),
185184
by_cases (∃ i, x ∈ s i); simp [h],
186185
{ cases h with i hi,
187-
exact le_supr (λ i, supported M R (s i)) i (single_mem_supported R _ hi) },
188-
{ rw filter_single_of_neg,
189-
{ simp },
190-
{ exact h } }
186+
exact le_supr (λ i, supported M R (s i)) i (single_mem_supported R _ hi) }
191187
end
192188

193189
theorem supported_union (s t : set α) :

src/measure_theory/category/Meas.lean

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ noncomputable theory
2828
open category_theory measure_theory
2929
universes u v
3030

31-
@[reducible] def Meas : Type (u+1) := bundled measurable_space
31+
@[derive has_coe_to_sort]
32+
def Meas : Type (u+1) := bundled measurable_space
3233

3334
namespace Meas
3435

@@ -39,6 +40,10 @@ def of (α : Type u) [measurable_space α] : Meas := ⟨α⟩
3940

4041
instance unbundled_hom : unbundled_hom @measurable := ⟨@measurable_id, @measurable.comp⟩
4142

43+
attribute [derive [large_category, concrete_category]] Meas
44+
45+
instance : inhabited Meas := ⟨Meas.of empty⟩
46+
4247
/-- `Measure X` is the measurable space of measures over the measurable space `X`. It is the
4348
weakest measurable space, s.t. λμ, μ s is measurable for all measurable sets `s` in `X`. An
4449
important purpose is to assign a monadic structure on it, the Giry monad. In the Giry monad,

src/tactic/basic.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import tactic.alias
22
import tactic.clear
33
import tactic.converter.apply_congr
4+
import tactic.delta_instance
45
import tactic.elide
56
import tactic.explode
67
import tactic.find

0 commit comments

Comments
 (0)