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

Commit 5cba21d

Browse files
committed
chore(*): swap order of [fintype A] [decidable_eq A] (#3705)
@fpvandoorn suggested in #3603 swapping the order of some `[fintype A] [decidable_eq A]` arguments to solve a linter problem with slow typeclass lookup. The reasoning is that Lean solves typeclass search problems from right to left, and * it's "less likely" that a type is a `fintype` than it has `decidable_eq`, so we can fail earlier if `fintype` comes second * typeclass search for `[decidable_eq]` can already be slow, so it's better to avoid it. This PR applies this suggestion across the library. Co-authored-by: Scott Morrison <scott.morrison@gmail.com>
1 parent f8fd0c3 commit 5cba21d

File tree

17 files changed

+51
-51
lines changed

17 files changed

+51
-51
lines changed

src/algebra/lie_algebra.lean

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ section matrices
788788
open_locale matrix
789789

790790
variables {R : Type u} [comm_ring R]
791-
variables {n : Type w} [fintype n] [decidable_eq n]
791+
variables {n : Type w} [decidable_eq n] [fintype n]
792792

793793
/-- An important class of Lie rings are those arising from the associative algebra structure on
794794
square matrices over a commutative ring. -/
@@ -837,18 +837,18 @@ by simp [linear_equiv.symm_conj_apply, matrix.lie_conj, matrix.comp_to_matrix_mu
837837

838838
/-- For square matrices, the natural map that reindexes a matrix's rows and columns with equivalent
839839
types is an equivalence of Lie algebras. -/
840-
def matrix.reindex_lie_equiv {m : Type w₁} [fintype m] [decidable_eq m]
840+
def matrix.reindex_lie_equiv {m : Type w₁} [decidable_eq m] [fintype m]
841841
(e : n ≃ m) : matrix n n R ≃ₗ⁅R⁆ matrix m m R :=
842842
{ map_lie := λ M N, by simp only [lie_ring.of_associative_ring_bracket, matrix.reindex_mul,
843843
matrix.mul_eq_mul, linear_equiv.map_sub, linear_equiv.to_fun_apply],
844844
..(matrix.reindex_linear_equiv e e) }
845845

846-
@[simp] lemma matrix.reindex_lie_equiv_apply {m : Type w₁} [fintype m] [decidable_eq m]
846+
@[simp] lemma matrix.reindex_lie_equiv_apply {m : Type w₁} [decidable_eq m] [fintype m]
847847
(e : n ≃ m) (M : matrix n n R) :
848848
matrix.reindex_lie_equiv e M = λ i j, M (e.symm i) (e.symm j) :=
849849
rfl
850850

851-
@[simp] lemma matrix.reindex_lie_equiv_symm_apply {m : Type w₁} [fintype m] [decidable_eq m]
851+
@[simp] lemma matrix.reindex_lie_equiv_symm_apply {m : Type w₁} [decidable_eq m] [fintype m]
852852
(e : n ≃ m) (M : matrix m m R) :
853853
(matrix.reindex_lie_equiv e).symm M = λ i j, M (e i) (e j) :=
854854
rfl
@@ -905,7 +905,7 @@ end skew_adjoint_endomorphisms
905905
section skew_adjoint_matrices
906906
open_locale matrix
907907

908-
variables {R : Type u} {n : Type w} [comm_ring R] [fintype n] [decidable_eq n]
908+
variables {R : Type u} {n : Type w} [comm_ring R] [decidable_eq n] [fintype n]
909909
variables (J : matrix n n R)
910910

911911
local attribute [instance] matrix.lie_ring
@@ -955,7 +955,7 @@ by simp [skew_adjoint_matrices_lie_subalgebra_equiv]
955955

956956
/-- An equivalence of matrix algebras commuting with the transpose endomorphisms restricts to an
957957
equivalence of Lie algebras of skew-adjoint matrices. -/
958-
def skew_adjoint_matrices_lie_subalgebra_equiv_transpose {m : Type w} [fintype m] [decidable_eq m]
958+
def skew_adjoint_matrices_lie_subalgebra_equiv_transpose {m : Type w} [decidable_eq m] [fintype m]
959959
(e : matrix n n R ≃ₐ[R] matrix m m R) (h : ∀ A, (e A)ᵀ = e (Aᵀ)) :
960960
skew_adjoint_matrices_lie_subalgebra J ≃ₗ⁅R⁆ skew_adjoint_matrices_lie_subalgebra (e J) :=
961961
lie_algebra.equiv.of_subalgebras _ _ e.to_lie_equiv
@@ -967,7 +967,7 @@ begin
967967
end
968968

969969
@[simp] lemma skew_adjoint_matrices_lie_subalgebra_equiv_transpose_apply
970-
{m : Type w} [fintype m] [decidable_eq m]
970+
{m : Type w} [decidable_eq m] [fintype m]
971971
(e : matrix n n R ≃ₐ[R] matrix m m R) (h : ∀ A, (e A)ᵀ = e (Aᵀ))
972972
(A : skew_adjoint_matrices_lie_subalgebra J) :
973973
(skew_adjoint_matrices_lie_subalgebra_equiv_transpose J e h A : matrix m m R) = e A :=

src/data/equiv/list.lean

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ by haveI := decidable_eq_of_encodable α; exact
105105
of_equiv {s : multiset α // s.nodup}
106106
⟨λ ⟨a, b⟩, ⟨a, b⟩, λ⟨a, b⟩, ⟨a, b⟩, λ ⟨a, b⟩, rfl, λ⟨a, b⟩, rfl⟩
107107

108-
def fintype_arrow (α : Type*) (β : Type*) [fintype α] [decidable_eq α] [encodable β] :
108+
def fintype_arrow (α : Type*) (β : Type*) [decidable_eq α] [fintype α] [encodable β] :
109109
trunc (encodable (α → β)) :=
110110
(fintype.equiv_fin α).map $
111111
λf, encodable.of_equiv (fin (fintype.card α) → β) $
112112
equiv.arrow_congr f (equiv.refl _)
113113

114-
def fintype_pi (α : Type*) (π : α → Type*) [fintype α] [decidable_eq α] [∀a, encodable (π a)] :
114+
def fintype_pi (α : Type*) (π : α → Type*) [decidable_eq α] [fintype α] [∀a, encodable (π a)] :
115115
trunc (encodable (Πa, π a)) :=
116116
(encodable.trunc_encodable_of_fintype α).bind $ λa,
117117
(@fintype_arrow α (Σa, π a) _ _ (@encodable.sigma _ _ a _)).bind $ λf,

src/data/fintype/basic.lean

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def equiv_fin_of_forall_mem_list {α} [decidable_eq α]
144144
`n = card α`. Since it is not unique, and depends on which permutation
145145
of the universe list is used, the bijection is wrapped in `trunc` to
146146
preserve computability. -/
147-
def equiv_fin (α) [fintype α] [decidable_eq α] : trunc (α ≃ fin (card α)) :=
147+
def equiv_fin (α) [decidable_eq α] [fintype α] : trunc (α ≃ fin (card α)) :=
148148
by unfold card finset.card; exact
149149
quot.rec_on_subsingleton (@univ α _).1
150150
(λ l (h : ∀ x:α, x ∈ l) (nd : l.nodup), trunc.mk (equiv_fin_of_forall_mem_list h nd))
@@ -190,7 +190,7 @@ def of_bijective [fintype α] (f : α → β) (H : function.bijective f) : finty
190190
λ b, let ⟨a, e⟩ := H.2 b in e ▸ mem_map_of_mem _ (mem_univ _)⟩
191191

192192
/-- If `f : α → β` is a surjection and `α` is a fintype, then `β` is also a fintype. -/
193-
def of_surjective [fintype α] [decidable_eq β] (f : α → β) (H : function.surjective f) : fintype β :=
193+
def of_surjective [decidable_eq β] [fintype α] (f : α → β) (H : function.surjective f) : fintype β :=
194194
⟨univ.image f, λ b, let ⟨a, e⟩ := H b in e ▸ mem_image_of_mem _ (mem_univ _)⟩
195195

196196
noncomputable def of_injective [fintype β] (f : α → β) (H : function.injective f) : fintype α :=
@@ -258,7 +258,7 @@ end set
258258
lemma finset.card_univ [fintype α] : (finset.univ : finset α).card = fintype.card α :=
259259
rfl
260260

261-
lemma finset.card_univ_diff [fintype α] [decidable_eq α] (s : finset α) :
261+
lemma finset.card_univ_diff [decidable_eq α] [fintype α] (s : finset α) :
262262
(finset.univ \ s).card = fintype.card α - s.card :=
263263
finset.card_sdiff (subset_univ s)
264264

@@ -572,7 +572,7 @@ by rw [← e.equiv_of_fintype_self_embedding_to_embedding, univ_map_equiv_to_emb
572572

573573
namespace fintype
574574

575-
variables [fintype α] [decidable_eq α] {δ : α → Type*}
575+
variables [decidable_eq α] [fintype α] {δ : α → Type*}
576576

577577
/-- Given for all `a : α` a finset `t a` of `δ a`, then one can define the
578578
finset `fintype.pi_finset t` of all functions taking values in `t a` for all `a`. This is the
@@ -732,7 +732,7 @@ theorem quotient.fin_choice_aux_eq {ι : Type*} [decidable_eq ι]
732732
subst j, refl
733733
end
734734

735-
def quotient.fin_choice {ι : Type*} [fintype ι] [decidable_eq ι]
735+
def quotient.fin_choice {ι : Type*} [decidable_eq ι] [fintype ι]
736736
{α : ι → Type*} [S : ∀ i, setoid (α i)]
737737
(f : ∀ i, quotient (S i)) : @quotient (Π i, α i) (by apply_instance) :=
738738
quotient.lift_on (@quotient.rec_on _ _ (λ l : multiset ι,
@@ -750,7 +750,7 @@ quotient.lift_on (@quotient.rec_on _ _ (λ l : multiset ι,
750750
(λ f, ⟦λ i, f i (finset.mem_univ _)⟧)
751751
(λ a b h, quotient.sound $ λ i, h _ _)
752752

753-
theorem quotient.fin_choice_eq {ι : Type*} [fintype ι] [decidable_eq ι]
753+
theorem quotient.fin_choice_eq {ι : Type*} [decidable_eq ι] [fintype ι]
754754
{α : ι → Type*} [∀ i, setoid (α i)]
755755
(f : ∀ i, α i) : quotient.fin_choice (λ i, ⟦f i⟧) = ⟦f⟧ :=
756756
begin
@@ -901,7 +901,7 @@ section choose
901901
open fintype
902902
open equiv
903903

904-
variables [fintype α] [decidable_eq α] (p : α → Prop) [decidable_pred p]
904+
variables [decidable_eq α] [fintype α] (p : α → Prop) [decidable_pred p]
905905

906906
def choose_x (hp : ∃! a : α, p a) : {a // p a} :=
907907
⟨finset.choose p univ (by simp; exact hp), finset.choose_property _ _ _⟩
@@ -916,8 +916,8 @@ end choose
916916
section bijection_inverse
917917
open function
918918

919-
variables [fintype α] [decidable_eq α]
920-
variables [fintype β] [decidable_eq β]
919+
variables [decidable_eq α] [fintype α]
920+
variables [decidable_eq β] [fintype β]
921921
variables {f : α → β}
922922

923923
/-- `

src/data/fintype/card.lean

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ finset.card_eq_sum_ones _
3030
section
3131
open finset
3232

33-
variables {ι : Type*} [fintype ι] [decidable_eq ι]
33+
variables {ι : Type*} [decidable_eq ι] [fintype ι]
3434

3535
@[to_additive]
3636
lemma prod_extend_by_one [comm_monoid α] (s : finset ι) (f : ι → α) :
@@ -120,12 +120,12 @@ multiset.card_pi _ _
120120
(fintype.pi_finset t).card = ∏ a, card (t a) :=
121121
by simp [fintype.pi_finset, card_map]
122122

123-
@[simp] lemma fintype.card_pi {β : α → Type*} [fintype α] [decidable_eq α]
123+
@[simp] lemma fintype.card_pi {β : α → Type*} [decidable_eq α] [fintype α]
124124
[f : Π a, fintype (β a)] : fintype.card (Π a, β a) = ∏ a, fintype.card (β a) :=
125125
fintype.card_pi_finset _
126126

127127
-- FIXME ouch, this should be in the main file.
128-
@[simp] lemma fintype.card_fun [fintype α] [decidable_eq α] [fintype β] :
128+
@[simp] lemma fintype.card_fun [decidable_eq α] [fintype α] [fintype β] :
129129
fintype.card (α → β) = fintype.card β ^ fintype.card α :=
130130
by rw [fintype.card_pi, finset.prod_const, nat.pow_eq_pow]; refl
131131

@@ -229,7 +229,7 @@ begin
229229
simp [finset.ext_iff]
230230
end
231231

232-
@[to_additive] lemma finset.prod_fiberwise [fintype β] [decidable_eq β] [comm_monoid γ]
232+
@[to_additive] lemma finset.prod_fiberwise [decidable_eq β] [fintype β] [comm_monoid γ]
233233
(s : finset α) (f : α → β) (g : α → γ) :
234234
∏ b : β, ∏ a in s.filter (λ a, f a = b), g a = ∏ a in s, g a :=
235235
begin
@@ -247,7 +247,7 @@ begin
247247
end
248248

249249
@[to_additive]
250-
lemma fintype.prod_fiberwise [fintype α] [fintype β] [decidable_eq β] [comm_monoid γ]
250+
lemma fintype.prod_fiberwise [fintype α] [decidable_eq β] [fintype β] [comm_monoid γ]
251251
(f : α → β) (g : α → γ) :
252252
(∏ b : β, ∏ a : {a // f a = b}, g (a : α)) = ∏ a, g a :=
253253
begin

src/data/matrix/basic.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ end
425425
The ring homomorphism `α →+* matrix n n α`
426426
sending `a` to the diagonal matrix with `a` on the diagonal.
427427
-/
428-
def scalar (n : Type u) [fintype n] [decidable_eq n] : α →+* matrix n n α :=
428+
def scalar (n : Type u) [decidable_eq n] [fintype n] : α →+* matrix n n α :=
429429
{ to_fun := λ a, a • 1,
430430
map_zero' := by simp,
431431
map_add' := by { intros, ext, simp [add_mul], },

src/field_theory/finite.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ open polynomial
4949

5050
/-- The cardinality of a field is at most `n` times the cardinality of the image of a degree `n`
5151
polynomial -/
52-
lemma card_image_polynomial_eval [fintype R] [decidable_eq R] {p : polynomial R} (hp : 0 < p.degree) :
52+
lemma card_image_polynomial_eval [decidable_eq R] [fintype R] {p : polynomial R} (hp : 0 < p.degree) :
5353
fintype.card R ≤ nat_degree p * (univ.image (λ x, eval x p)).card :=
5454
finset.card_le_mul_card_image _ _
5555
(λ a _, calc _ = (p - C a).roots.card : congr_arg card

src/group_theory/order_of_element.lean

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,13 @@ def is_cyclic.comm_group [hg : group α] [is_cyclic α] : comm_group α :=
304304
hm ▸ hn ▸ gpow_mul_comm _ _ _,
305305
..hg }
306306

307-
lemma is_cyclic_of_order_of_eq_card [group α] [fintype α] [decidable_eq α]
307+
lemma is_cyclic_of_order_of_eq_card [group α] [decidable_eq α] [fintype α]
308308
(x : α) (hx : order_of x = fintype.card α) : is_cyclic α :=
309309
⟨⟨x, set.eq_univ_iff_forall.1 $ set.eq_of_subset_of_card_le
310310
(set.subset_univ _)
311311
(by {rw [fintype.card_congr (equiv.set.univ α), ← hx, order_eq_card_gpowers], refl})⟩⟩
312312

313-
lemma order_of_eq_card_of_forall_mem_gpowers [group α] [fintype α] [decidable_eq α]
313+
lemma order_of_eq_card_of_forall_mem_gpowers [group α] [decidable_eq α] [fintype α]
314314
{g : α} (hx : ∀ x, x ∈ gpowers g) : order_of g = fintype.card α :=
315315
by {rw [← fintype.card_congr (equiv.set.univ α), order_eq_card_gpowers],
316316
simp [hx], apply fintype.card_of_finset', simp, intro x, exact hx x}
@@ -362,7 +362,7 @@ else
362362
by clear _let_match; substI this; apply_instance
363363

364364
open finset nat
365-
lemma is_cyclic.card_pow_eq_one_le [group α] [fintype α] [decidable_eq α] [is_cyclic α] {n : ℕ}
365+
lemma is_cyclic.card_pow_eq_one_le [group α] [decidable_eq α] [fintype α] [is_cyclic α] {n : ℕ}
366366
(hn0 : 0 < n) : (univ.filter (λ a : α, a ^ n = 1)).card ≤ n :=
367367
let ⟨g, hg⟩ := is_cyclic.exists_generator α in
368368
calc (univ.filter (λ a : α, a ^ n = 1)).card
@@ -397,7 +397,7 @@ by { simp only [mem_powers_iff_mem_gpowers], exact is_cyclic.exists_generator α
397397

398398
section
399399

400-
variables [group α] [fintype α] [decidable_eq α]
400+
variables [group α] [decidable_eq α] [fintype α]
401401

402402
lemma is_cyclic.image_range_order_of (ha : ∀ x : α, x ∈ gpowers a) :
403403
finset.image (λ i, a ^ i) (range (order_of a)) = univ :=
@@ -415,7 +415,7 @@ end
415415

416416
section totient
417417

418-
variables [group α] [fintype α] [decidable_eq α] (hn : ∀ n : ℕ, 0 < n → (univ.filter (λ a : α, a ^ n = 1)).card ≤ n)
418+
variables [group α] [decidable_eq α] [fintype α] (hn : ∀ n : ℕ, 0 < n → (univ.filter (λ a : α, a ^ n = 1)).card ≤ n)
419419
include hn
420420

421421
lemma card_pow_eq_one_eq_order_of_aux (a : α) :
@@ -515,7 +515,7 @@ is_cyclic_of_order_of_eq_card x (finset.mem_filter.1 hx).2
515515

516516
end totient
517517

518-
lemma is_cyclic.card_order_of_eq_totient [group α] [is_cyclic α] [fintype α] [decidable_eq α]
518+
lemma is_cyclic.card_order_of_eq_totient [group α] [is_cyclic α] [decidable_eq α] [fintype α]
519519
{d : ℕ} (hd : d ∣ fintype.card α) : (univ.filter (λ a : α, order_of a = d)).card = totient d :=
520520
card_order_of_eq_totient_aux₂ (λ n, is_cyclic.card_pow_eq_one_le) hd
521521

src/linear_algebra/char_poly.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ open polynomial matrix
3232
open_locale big_operators
3333

3434
variables {R : Type u} [comm_ring R]
35-
variables {n : Type w} [fintype n] [decidable_eq n]
35+
variables {n : Type w} [decidable_eq n] [fintype n]
3636

3737
open finset
3838

src/linear_algebra/char_poly/coeff.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ open polynomial matrix
3535
open_locale big_operators
3636

3737
variables {R : Type u} [comm_ring R]
38-
variables {n G : Type v} [fintype n] [decidable_eq n]
38+
variables {n G : Type v} [decidable_eq n] [fintype n]
3939
variables {α β : Type v} [decidable_eq α]
4040

4141

src/linear_algebra/determinant.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ open equiv equiv.perm finset function
1414
namespace matrix
1515
open_locale matrix big_operators
1616

17-
variables {n : Type u} [fintype n] [decidable_eq n] {R : Type v} [comm_ring R]
17+
variables {n : Type u} [decidable_eq n] [fintype n] {R : Type v} [comm_ring R]
1818

1919
local notation ` σ:max := ((sign σ : ℤ ) : R)
2020

0 commit comments

Comments
 (0)