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

Commit f86abc7

Browse files
fpvandoornmergify[bot]
authored andcommitted
fix(*): lower instance priority (#1724)
* fix(*): lower instance priority use lower instance priority for instances that always apply also do this for automatically generated instances using the `extend` keyword also add a comment to most places where we short-circuit type-class inference. This can lead to greatly increased search times (see issue #1561), so we might want to delete some/all of them. * put default_priority option inside section Default priority also applies to all instances, not just automatically-generates ones the scope of set_option is limited to a section * two more low priorities * fix some broken proofs * fix proof * more fixes * more fixes * increase max_depth a bit * update notes * fix linter issues
1 parent 2b3eaa8 commit f86abc7

File tree

92 files changed

+499
-91
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+499
-91
lines changed

src/algebra/category/CommRing/basic.lean

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ def of (R : Type u) [semiring R] : SemiRing := bundled.of R
3939

4040
local attribute [reducible] SemiRing
4141

42-
instance : has_coe_to_sort SemiRing := infer_instance
42+
instance : has_coe_to_sort SemiRing := infer_instance -- short-circuit type class inference
4343

4444
instance (R : SemiRing) : semiring R := R.str
4545

4646
instance bundled_hom : bundled_hom @ring_hom :=
4747
⟨@ring_hom.to_fun, @ring_hom.id, @ring_hom.comp, @ring_hom.coe_inj⟩
4848

49-
instance : concrete_category SemiRing := infer_instance
49+
instance : concrete_category SemiRing := infer_instance -- short-circuit type class inference
5050

5151
instance has_forget_to_Mon : has_forget₂ SemiRing Mon :=
5252
bundled_hom.mk_has_forget₂ @semiring.to_monoid (λ R₁ R₂, ring_hom.to_monoid_hom) (λ _ _ _, rfl)
@@ -68,13 +68,13 @@ def of (R : Type u) [ring R] : Ring := bundled.of R
6868

6969
local attribute [reducible] Ring
7070

71-
instance : has_coe_to_sort Ring := infer_instance
71+
instance : has_coe_to_sort Ring := infer_instance -- short-circuit type class inference
7272

7373
instance (R : Ring) : ring R := R.str
7474

75-
instance : concrete_category Ring := infer_instance
75+
instance : concrete_category Ring := infer_instance -- short-circuit type class inference
7676

77-
instance has_forget_to_SemiRing : has_forget₂ Ring SemiRing := infer_instance
77+
instance has_forget_to_SemiRing : has_forget₂ Ring SemiRing := infer_instance -- short-circuit type class inference
7878
instance has_forget_to_AddCommGroup : has_forget₂ Ring AddCommGroup :=
7979
-- can't use bundled_hom.mk_has_forget₂, since AddCommGroup is an induced category
8080
{ forget₂ :=
@@ -93,13 +93,13 @@ def of (R : Type u) [comm_semiring R] : CommSemiRing := bundled.of R
9393

9494
local attribute [reducible] CommSemiRing
9595

96-
instance : has_coe_to_sort CommSemiRing := infer_instance
96+
instance : has_coe_to_sort CommSemiRing := infer_instance -- short-circuit type class inference
9797

9898
instance (R : CommSemiRing) : comm_semiring R := R.str
9999

100-
instance : concrete_category CommSemiRing := infer_instance
100+
instance : concrete_category CommSemiRing := infer_instance -- short-circuit type class inference
101101

102-
instance has_forget_to_SemiRing : has_forget₂ CommSemiRing SemiRing := infer_instance
102+
instance has_forget_to_SemiRing : has_forget₂ CommSemiRing SemiRing := infer_instance -- short-circuit type class inference
103103

104104
/-- The forgetful functor from commutative rings to (multiplicative) commutative monoids. -/
105105
instance has_forget_to_CommMon : has_forget₂ CommSemiRing CommMon :=
@@ -119,13 +119,13 @@ def of (R : Type u) [comm_ring R] : CommRing := bundled.of R
119119

120120
local attribute [reducible] CommRing
121121

122-
instance : has_coe_to_sort CommRing := infer_instance
122+
instance : has_coe_to_sort CommRing := infer_instance -- short-circuit type class inference
123123

124124
instance (R : CommRing) : comm_ring R := R.str
125125

126-
instance : concrete_category CommRing := infer_instance
126+
instance : concrete_category CommRing := infer_instance -- short-circuit type class inference
127127

128-
instance has_forget_to_Ring : has_forget₂ CommRing Ring := infer_instance
128+
instance has_forget_to_Ring : has_forget₂ CommRing Ring := infer_instance -- short-circuit type class inference
129129

130130
/-- The forgetful functor from commutative rings to (multiplicative) commutative monoids. -/
131131
instance has_forget_to_CommSemiRing : has_forget₂ CommRing CommSemiRing :=

src/algebra/category/Group.lean

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ namespace Group
3939
local attribute [reducible] Group
4040

4141
@[to_additive]
42-
instance : has_coe_to_sort Group := infer_instance
42+
instance : has_coe_to_sort Group := infer_instance -- short-circuit type class inference
4343

4444
@[to_additive add_group]
4545
instance (G : Group) : group G := G.str
@@ -48,10 +48,10 @@ instance (G : Group) : group G := G.str
4848
instance : has_one Group := ⟨Group.of punit⟩
4949

5050
@[to_additive]
51-
instance : concrete_category Group := infer_instance
51+
instance : concrete_category Group := infer_instance -- short-circuit type class inference
5252

5353
@[to_additive has_forget_to_AddMon]
54-
instance has_forget_to_Mon : has_forget₂ Group Mon := infer_instance
54+
instance has_forget_to_Mon : has_forget₂ Group Mon := infer_instance -- short-circuit type class inference
5555

5656
end Group
5757

@@ -68,17 +68,17 @@ namespace CommGroup
6868
local attribute [reducible] CommGroup
6969

7070
@[to_additive]
71-
instance : has_coe_to_sort CommGroup := infer_instance
71+
instance : has_coe_to_sort CommGroup := infer_instance -- short-circuit type class inference
7272

7373
@[to_additive add_comm_group]
7474
instance (G : CommGroup) : comm_group G := G.str
7575

7676
@[to_additive] instance : has_one CommGroup := ⟨CommGroup.of punit⟩
7777

78-
@[to_additive] instance : concrete_category CommGroup := infer_instance
78+
@[to_additive] instance : concrete_category CommGroup := infer_instance -- short-circuit type class inference
7979

8080
@[to_additive has_forget_to_AddGroup]
81-
instance has_forget_to_Group : has_forget₂ CommGroup Group := infer_instance
81+
instance has_forget_to_Group : has_forget₂ CommGroup Group := infer_instance -- short-circuit type class inference
8282

8383
@[to_additive has_forget_to_AddCommMon]
8484
instance has_forget_to_CommMon : has_forget₂ CommGroup CommMon :=

src/algebra/category/Mon/basic.lean

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def of (M : Type u) [monoid M] : Mon := bundled.of M
5858
local attribute [reducible] Mon
5959

6060
@[to_additive]
61-
instance : has_coe_to_sort Mon := infer_instance
61+
instance : has_coe_to_sort Mon := infer_instance -- short-circuit type class inference
6262

6363
@[to_additive add_monoid]
6464
instance (M : Mon) : monoid M := M.str
@@ -68,7 +68,7 @@ instance bundled_hom : bundled_hom @monoid_hom :=
6868
⟨@monoid_hom.to_fun, @monoid_hom.id, @monoid_hom.comp, @monoid_hom.coe_inj⟩
6969

7070
@[to_additive]
71-
instance : concrete_category Mon := infer_instance
71+
instance : concrete_category Mon := infer_instance -- short-circuit type class inference
7272

7373
end Mon
7474

@@ -85,16 +85,16 @@ def of (M : Type u) [comm_monoid M] : CommMon := bundled.of M
8585
local attribute [reducible] CommMon
8686

8787
@[to_additive]
88-
instance : has_coe_to_sort CommMon := infer_instance
88+
instance : has_coe_to_sort CommMon := infer_instance -- short-circuit type class inference
8989

9090
@[to_additive add_comm_monoid]
9191
instance (M : CommMon) : comm_monoid M := M.str
9292

9393
@[to_additive]
94-
instance : concrete_category CommMon := infer_instance
94+
instance : concrete_category CommMon := infer_instance -- short-circuit type class inference
9595

9696
@[to_additive has_forget_to_AddMon]
97-
instance has_forget_to_Mon : has_forget₂ CommMon Mon := infer_instance
97+
instance has_forget_to_Mon : has_forget₂ CommMon Mon := infer_instance -- short-circuit type class inference
9898

9999
end CommMon
100100

src/algebra/char_zero.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ theorem ordered_cancel_comm_monoid.char_zero_of_inj_zero {α : Type*}
3434
(H : ∀ n:ℕ, (n:α) = 0 → n = 0) : char_zero α :=
3535
char_zero_of_inj_zero (@add_left_cancel _ _) H
3636

37+
@[priority 100] -- see Note [lower instance priority]
3738
instance linear_ordered_semiring.to_char_zero {α : Type*}
3839
[linear_ordered_semiring α] : char_zero α :=
3940
ordered_cancel_comm_monoid.char_zero_of_inj_zero $

src/algebra/euclidean_domain.lean

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import data.int.basic
1010

1111
universe u
1212

13+
section prio
14+
set_option default_priority 100 -- see Note [default priority]
1315
class euclidean_domain (α : Type u) extends nonzero_comm_ring α :=
1416
(quotient : α → α → α)
1517
(quotient_zero : ∀ a, quotient a 0 = 0)
@@ -28,15 +30,18 @@ class euclidean_domain (α : Type u) extends nonzero_comm_ring α :=
2830
function from weak to strong. I've currently divided the lemmas into
2931
strong and weak depending on whether they require `val_le_mul_left` or not. -/
3032
(mul_left_not_lt : ∀ a {b}, b ≠ 0 → ¬r (a * b) a)
33+
end prio
3134

3235
namespace euclidean_domain
3336
variable {α : Type u}
3437
variables [euclidean_domain α]
3538

3639
local infix ` ≺ `:50 := euclidean_domain.r
3740

41+
@[priority 100] -- see Note [lower instance priority]
3842
instance : has_div α := ⟨quotient⟩
3943

44+
@[priority 100] -- see Note [lower instance priority]
4045
instance : has_mod α := ⟨remainder⟩
4146

4247
theorem div_add_mod (a b : α) : b * (a / b) + a % b = a :=
@@ -235,6 +240,7 @@ by have := @xgcd_aux_P _ _ _ a b a b 1 0 0 1
235240
(by rw [P, mul_one, mul_zero, add_zero]) (by rw [P, mul_one, mul_zero, zero_add]);
236241
rwa [xgcd_aux_val, xgcd_val] at this
237242

243+
@[priority 100] -- see Note [lower instance priority]
238244
instance (α : Type*) [e : euclidean_domain α] : integral_domain α :=
239245
by haveI := classical.dec_eq α; exact
240246
{ eq_zero_or_eq_zero_of_mul_eq_zero :=
@@ -331,6 +337,7 @@ instance int.euclidean_domain : euclidean_domain ℤ :=
331337
by rw [← mul_one a.nat_abs, int.nat_abs_mul];
332338
exact mul_le_mul_of_nonneg_left (int.nat_abs_pos_of_ne_zero b0) (nat.zero_le _) }
333339

340+
@[priority 100] -- see Note [lower instance priority]
334341
instance discrete_field.to_euclidean_domain {K : Type u} [discrete_field K] : euclidean_domain K :=
335342
{ quotient := (/),
336343
remainder := λ a b, if b = 0 then a else 0,

src/algebra/field.lean

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ open set
99
universe u
1010
variables {α : Type u}
1111

12-
-- Default priority sufficient as core version has custom-set lower priority (100)
1312
/-- Core version `division_ring_has_div` erratically requires two instances of `division_ring` -/
13+
-- priority 900 sufficient as core version has custom-set lower priority (100)
14+
@[priority 900] -- see Note [lower instance priority]
1415
instance division_ring_has_div' [division_ring α] : has_div α := ⟨algebra.div⟩
1516

17+
@[priority 100] -- see Note [lower instance priority]
1618
instance division_ring.to_domain [s : division_ring α] : domain α :=
1719
{ eq_zero_or_eq_zero_of_mul_eq_zero := λ a b h,
1820
classical.by_contradiction $ λ hn,
@@ -122,6 +124,7 @@ lemma div_eq_iff_mul_eq (hb : b ≠ 0) : a / b = c ↔ c * b = a :=
122124

123125
end division_ring
124126

127+
@[priority 100] -- see Note [lower instance priority]
125128
instance field.to_integral_domain [F : field α] : integral_domain α :=
126129
{ ..F, ..division_ring.to_domain }
127130

src/algebra/gcd_domain.lean

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,17 @@ variables {α : Type*}
1313

1414
set_option old_structure_cmd true
1515

16+
17+
18+
section prio
19+
set_option default_priority 100 -- see Note [default priority]
1620
/-- Normalization domain: multiplying with `norm_unit` gives a normal form for associated elements. -/
1721
class normalization_domain (α : Type*) extends integral_domain α :=
1822
(norm_unit : α → units α)
1923
(norm_unit_zero : norm_unit 0 = 1)
2024
(norm_unit_mul : ∀{a b}, a ≠ 0 → b ≠ 0 → norm_unit (a * b) = norm_unit a * norm_unit b)
2125
(norm_unit_coe_units : ∀(u : units α), norm_unit u = u⁻¹)
26+
end prio
2227

2328
export normalization_domain (norm_unit norm_unit_zero norm_unit_mul norm_unit_coe_units)
2429

@@ -127,6 +132,8 @@ quotient.induction_on a normalize_idem
127132

128133
end associates
129134

135+
section prio
136+
set_option default_priority 100 -- see Note [default priority]
130137
/-- GCD domain: an integral domain with normalization and `gcd` (greatest common divisor) and
131138
`lcm` (least common multiple) operations. In this setting `gcd` and `lcm` form a bounded lattice on
132139
the associated elements where `gcd` is the infimum, `lcm` is the supremum, `1` is bottom, and
@@ -142,6 +149,7 @@ class gcd_domain (α : Type*) extends normalization_domain α :=
142149
(gcd_mul_lcm : ∀a b, gcd a b * lcm a b = normalize (a * b))
143150
(lcm_zero_left : ∀a, lcm 0 a = 0)
144151
(lcm_zero_right : ∀a, lcm a 0 = 0)
152+
end prio
145153

146154
export gcd_domain (gcd lcm gcd_dvd_left gcd_dvd_right dvd_gcd lcm_zero_left lcm_zero_right)
147155

src/algebra/group/hom.lean

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,15 @@ When they can be inferred from the type it is faster to use this method than to
4747
is_group_hom, is_monoid_hom, monoid_hom
4848
4949
-/
50+
51+
/- Note [low priority instance on morphisms]:
52+
We have instances stating that the composition or the product of two morphisms is again a morphism.
53+
Type class inference will "succeed" in applying these instances when they shouldn't apply (for
54+
example when the goal is just `⊢ is_mul_hom f` the instances `is_mul_hom.comp` or `is_mul_hom.mul`
55+
might still succeed). This can cause type class inference to loop.
56+
To avoid this, we make the priority of these instances very low. We should think about not making
57+
these declarations instances in the first place.
58+
-/
5059
universes u v
5160
variables {α : Type u} {β : Type v}
5261

@@ -67,13 +76,15 @@ variables [has_mul α] [has_mul β] {γ : Type*} [has_mul γ]
6776
instance id : is_mul_hom (id : α → α) := {map_mul := λ _ _, rfl}
6877

6978
/-- The composition of maps which preserve multiplication, also preserves multiplication. -/
70-
@[to_additive "The composition of addition preserving maps also preserves addition"]
79+
-- see Note [low priority instance on morphisms]
80+
@[priority 10, to_additive "The composition of addition preserving maps also preserves addition"]
7181
instance comp (f : α → β) (g : β → γ) [is_mul_hom f] [hg : is_mul_hom g] : is_mul_hom (g ∘ f) :=
7282
{ map_mul := λ x y, by simp only [function.comp, map_mul f, map_mul g] }
7383

7484
/-- A product of maps which preserve multiplication,
7585
preserves multiplication when the target is commutative. -/
76-
@[instance, to_additive]
86+
-- see Note [low priority instance on morphisms]
87+
@[instance, priority 10, to_additive]
7788
lemma mul {α β} [semigroup α] [comm_semigroup β]
7889
(f g : α → β) [is_mul_hom f] [is_mul_hom g] :
7990
is_mul_hom (λa, f a * g a) :=
@@ -88,6 +99,8 @@ lemma inv {α β} [has_mul α] [comm_group β] (f : α → β) [is_mul_hom f] :
8899

89100
end is_mul_hom
90101

102+
section prio
103+
set_option default_priority 100 -- see Note [default priority]
91104
/-- Predicate for add_monoid homomorphisms (deprecated -- use the bundled `monoid_hom` version). -/
92105
class is_add_monoid_hom [add_monoid α] [add_monoid β] (f : α → β) extends is_add_hom f : Prop :=
93106
(map_zero : f 0 = 0)
@@ -96,6 +109,7 @@ class is_add_monoid_hom [add_monoid α] [add_monoid β] (f : α → β) extends
96109
@[to_additive is_add_monoid_hom]
97110
class is_monoid_hom [monoid α] [monoid β] (f : α → β) extends is_mul_hom f : Prop :=
98111
(map_one : f 1 = 1)
112+
end prio
99113

100114
namespace is_monoid_hom
101115
variables [monoid α] [monoid β] (f : α → β) [is_monoid_hom f]
@@ -121,7 +135,7 @@ variables [monoid α] [monoid β] (f : α → β) [is_monoid_hom f]
121135
instance id : is_monoid_hom (@id α) := { map_one := rfl }
122136

123137
/-- The composite of two monoid homomorphisms is a monoid homomorphism. -/
124-
@[to_additive]
138+
@[priority 10, to_additive] -- see Note [low priority instance on morphisms]
125139
instance comp {γ} [monoid γ] (g : β → γ) [is_monoid_hom g] :
126140
is_monoid_hom (g ∘ f) :=
127141
{ map_one := show g _ = 1, by rw [map_one f, map_one g] }
@@ -142,12 +156,15 @@ instance is_add_monoid_hom_mul_right {γ : Type*} [semiring γ] (x : γ) :
142156

143157
end is_add_monoid_hom
144158

159+
section prio
160+
set_option default_priority 100 -- see Note [default priority]
145161
/-- Predicate for additive group homomorphism (deprecated -- use bundled `monoid_hom`). -/
146162
class is_add_group_hom [add_group α] [add_group β] (f : α → β) extends is_add_hom f : Prop
147163

148164
/-- Predicate for group homomorphisms (deprecated -- use bundled `monoid_hom`). -/
149165
@[to_additive is_add_group_hom]
150166
class is_group_hom [group α] [group β] (f : α → β) extends is_mul_hom f : Prop
167+
end prio
151168

152169
/-- Construct `is_group_hom` from its only hypothesis. The default constructor tries to get
153170
`is_mul_hom` from class instances, and this makes some proofs fail. -/
@@ -161,7 +178,7 @@ variables [group α] [group β] (f : α → β) [is_group_hom f]
161178
open is_mul_hom (map_mul)
162179

163180
/-- A group homomorphism is a monoid homomorphism. -/
164-
@[to_additive to_is_add_monoid_hom]
181+
@[priority 100, to_additive to_is_add_monoid_hom] -- see Note [lower instance priority]
165182
instance to_is_monoid_hom : is_monoid_hom f :=
166183
is_monoid_hom.of_mul f
167184

@@ -179,7 +196,7 @@ eq_inv_of_mul_eq_one $ by rw [← map_mul f, inv_mul_self, map_one f]
179196
instance id : is_group_hom (@id α) := { }
180197

181198
/-- The composition of two group homomomorphisms is a group homomorphism. -/
182-
@[to_additive]
199+
@[priority 10, to_additive] -- see Note [low priority instance on morphisms]
183200
instance comp {γ} [group γ] (g : β → γ) [is_group_hom g] : is_group_hom (g ∘ f) := { }
184201

185202
/-- A group homomorphism is injective iff its kernel is trivial. -/
@@ -192,7 +209,7 @@ lemma injective_iff (f : α → β) [is_group_hom f] :
192209
simpa using inv_eq_of_mul_eq_one (h _ hxy)⟩
193210

194211
/-- The product of group homomorphisms is a group homomorphism if the target is commutative. -/
195-
@[instance, to_additive]
212+
@[instance, priority 10, to_additive] -- see Note [low priority instance on morphisms]
196213
lemma mul {α β} [group α] [comm_group β]
197214
(f g : α → β) [is_group_hom f] [is_group_hom g] :
198215
is_group_hom (λa, f a * g a) :=

src/algebra/lie_algebra.lean

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ end
7979

8080
end ring_commutator
8181

82+
section prio
83+
set_option default_priority 100 -- see Note [default priority]
8284
/--
8385
A Lie ring is an additive group with compatible product, known as the bracket, satisfying the
8486
Jacobi identity. The bracket is not associative unless it is identically zero.
@@ -88,6 +90,7 @@ class lie_ring (L : Type v) [add_comm_group L] extends has_bracket L :=
8890
(lie_add : ∀ (x y z : L), ⁅z, x + y⁆ = ⁅z, x⁆ + ⁅z, y⁆)
8991
(lie_self : ∀ (x : L), ⁅x, x⁆ = 0)
9092
(jacobi : ∀ (x y z : L), ⁅x, ⁅y, z⁆⁆ + ⁅y, ⁅z, x⁆⁆ + ⁅z, ⁅x, y⁆⁆ = 0)
93+
end prio
9194

9295
section lie_ring
9396

@@ -150,13 +153,16 @@ def lie_ring.of_associative_ring (A : Type v) [ring A] : lie_ring A :=
150153

151154
end lie_ring
152155

156+
section prio
157+
set_option default_priority 100 -- see Note [default priority]
153158
/--
154159
A Lie algebra is a module with compatible product, known as the bracket, satisfying the Jacobi
155160
identity. Forgetting the scalar multiplication, every Lie algebra is a Lie ring.
156161
-/
157162
class lie_algebra (R : Type u) (L : Type v)
158163
[comm_ring R] [add_comm_group L] extends module R L, lie_ring L :=
159164
(lie_smul : ∀ (t : R) (x y : L), ⁅x, t • y⁆ = t • ⁅x, y⁆)
165+
end prio
160166

161167
@[simp] lemma lie_smul (R : Type u) (L : Type v) [comm_ring R] [add_comm_group L] [lie_algebra R L]
162168
(t : R) (x y : L) : ⁅x, t • y⁆ = t • ⁅x, y⁆ :=

0 commit comments

Comments
 (0)