Skip to content

Commit 0aa016e

Browse files
committed
chore(Algebra.Basic): override toFun and smul in Algebra.id (#9949)
The current definition of `Algebra.id` is `(RingHom.id _).toAlgebra`. The problem with this is that `RingHom.id` is a `def` and is not reducible. Thus Lean will often refuse to unfold it causing unification to fail unecessarily in typeclass searches. This overrides the data fields from `RingHom.id`.
1 parent 725f123 commit 0aa016e

5 files changed

Lines changed: 32 additions & 10 deletions

File tree

Mathlib/Algebra/Algebra/Basic.lean

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,8 +436,14 @@ theorem coe_linearMap : ⇑(Algebra.linearMap R A) = algebraMap R A :=
436436
rfl
437437
#align algebra.coe_linear_map Algebra.coe_linearMap
438438

439-
instance id : Algebra R R :=
440-
(RingHom.id R).toAlgebra
439+
/- The identity map inducing an `Algebra` structure. -/
440+
instance id : Algebra R R where
441+
-- We override `toFun` and `toSMul` because `RingHom.id` is not reducible and cannot
442+
-- be made so without a significant performance hit.
443+
-- see library note [reducible non-instances].
444+
toFun x := x
445+
toSMul := Mul.toSMul _
446+
__ := (RingHom.id R).toAlgebra
441447
#align algebra.id Algebra.id
442448

443449
variable {R A}

Mathlib/Analysis/NormedSpace/Star/GelfandDuality.lean

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,8 @@ theorem WeakDual.CharacterSpace.mem_spectrum_iff_exists {a : A} {z : ℂ} :
115115
z ∈ spectrum ℂ a ↔ ∃ f : characterSpace ℂ A, f a = z := by
116116
refine' ⟨fun hz => _, _⟩
117117
· obtain ⟨f, hf⟩ := WeakDual.CharacterSpace.exists_apply_eq_zero hz
118-
simp only [map_sub, sub_eq_zero, AlgHomClass.commutes, Algebra.id.map_eq_id,
119-
RingHom.id_apply] at hf
120-
refine ⟨f, ?_⟩
121-
rw [AlgHomClass.commutes, Algebra.id.map_eq_id, RingHom.id_apply] at hf
122-
exact hf.symm
118+
simp only [map_sub, sub_eq_zero, AlgHomClass.commutes] at hf
119+
exact ⟨_, hf.symm⟩
123120
· rintro ⟨f, rfl⟩
124121
exact AlgHom.apply_mem_spectrum f a
125122
#align weak_dual.character_space.mem_spectrum_iff_exists WeakDual.CharacterSpace.mem_spectrum_iff_exists

Mathlib/LinearAlgebra/CliffordAlgebra/Equivs.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ def toQuaternion : CliffordAlgebra (Q c₁ c₂) →ₐ[R] ℍ[R,c₁,c₂] :=
295295
CliffordAlgebra.lift (Q c₁ c₂)
296296
⟨{ toFun := fun v => (⟨0, v.1, v.2, 0⟩ : ℍ[R,c₁,c₂])
297297
map_add' := fun v₁ v₂ => by simp
298-
map_smul' := fun r v => by dsimp; rw [mul_zero]; rfl }, fun v => by
298+
map_smul' := fun r v => by dsimp; rw [mul_zero] }, fun v => by
299299
dsimp
300300
ext
301301
all_goals dsimp; ring⟩

Mathlib/RingTheory/Kaehler.lean

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,7 @@ def Derivation.liftKaehlerDifferential (D : Derivation R S M) : Ω[S⁄R] →ₗ
268268
refine Submodule.smul_induction_on hx ?_ ?_
269269
· rintro x (hx : _ = _) y -
270270
dsimp
271-
rw [show ↑(x • y) = x * ↑y by rfl, Derivation.tensorProductTo_mul, hx, y.prop, zero_smul,
272-
zero_smul, zero_add]
271+
rw [Derivation.tensorProductTo_mul, hx, y.prop, zero_smul, zero_smul, zero_add]
273272
· intro x y ex ey; rw [map_add, ex, ey, zero_add]
274273
#align derivation.lift_kaehler_differential Derivation.liftKaehlerDifferential
275274

test/Simp.lean

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import Mathlib.Algebra.Algebra.Pi
2+
import Mathlib.Algebra.Algebra.Basic
3+
4+
/-!
5+
Tests for the behavior of `simp`.
6+
--/
7+
8+
/- Taken from [Zulip](https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/
9+
topic/simp.20.5BX.5D.20fails.2C.20rw.20.5BX.5D.20works) -/
10+
-- Example 1: succeeds
11+
example {α R : Type*} [CommRing R] (f : α → R) (r : R) (a : α) :
12+
(r • f) a = r • (f a) := by
13+
simp only [Pi.smul_apply] -- succeeds
14+
15+
-- Example 2: used to fail, now succeeds!
16+
example {α R : Type*} [CommRing R] (f : α → R) (r : R) (a : α) :
17+
(r • f) a = r • (f a) := by
18+
let _ : SMul R R := SMulZeroClass.toSMul
19+
simp only [Pi.smul_apply]
20+

0 commit comments

Comments
 (0)