Skip to content

Commit 82487ba

Browse files
committed
chore(RingTheory/AdicCompletion): manual instances (#14534)
Instead of inferring `AddCommGroup`, `Module`, etc. instances for `AdicCompletion I M` from being a submodule, we explicitly spell out the definitions to improve performance. Also marks some definitions as `noncomputable` to save time spent on compilation. See https://leanprover.zulipchat.com/#narrow/stream/287929-mathlib4/topic/Adic.20completion.20is.20slow.
1 parent 3703193 commit 82487ba

File tree

4 files changed

+155
-21
lines changed

4 files changed

+155
-21
lines changed

Mathlib/LinearAlgebra/SModEq.lean

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,31 @@ theorem smul (hxy : x ≡ y [SMOD U]) (c : R) : c • x ≡ c • y [SMOD U] :=
9292
simp_rw [Quotient.mk_smul, hxy]
9393
#align smodeq.smul SModEq.smul
9494

95+
lemma nsmul (hxy : x ≡ y [SMOD U]) (n : ℕ) : n • x ≡ n • y [SMOD U] := by
96+
rw [SModEq.def] at hxy ⊢
97+
simp_rw [Quotient.mk_smul, hxy]
98+
99+
lemma zsmul (hxy : x ≡ y [SMOD U]) (n : ℤ) : n • x ≡ n • y [SMOD U] := by
100+
rw [SModEq.def] at hxy ⊢
101+
simp_rw [Quotient.mk_smul, hxy]
102+
95103
theorem mul {I : Ideal A} {x₁ x₂ y₁ y₂ : A} (hxy₁ : x₁ ≡ y₁ [SMOD I])
96104
(hxy₂ : x₂ ≡ y₂ [SMOD I]) : x₁ * x₂ ≡ y₁ * y₂ [SMOD I] := by
97105
simp only [SModEq.def, Ideal.Quotient.mk_eq_mk, map_mul] at hxy₁ hxy₂ ⊢
98106
rw [hxy₁, hxy₂]
99107

108+
lemma pow {I : Ideal A} {x y : A} (n : ℕ) (hxy : x ≡ y [SMOD I]) :
109+
x ^ n ≡ y ^ n [SMOD I] := by
110+
simp only [SModEq.def, Ideal.Quotient.mk_eq_mk, map_pow] at hxy ⊢
111+
rw [hxy]
112+
113+
lemma neg (hxy : x ≡ y [SMOD U]) : - x ≡ - y [SMOD U] := by
114+
simpa only [SModEq.def, Quotient.mk_neg, neg_inj]
115+
116+
lemma sub (hxy₁ : x₁ ≡ y₁ [SMOD U]) (hxy₂ : x₂ ≡ y₂ [SMOD U]) : x₁ - x₂ ≡ y₁ - y₂ [SMOD U] := by
117+
rw [SModEq.def] at hxy₁ hxy₂ ⊢
118+
simp_rw [Quotient.mk_sub, hxy₁, hxy₂]
119+
100120
theorem zero : x ≡ 0 [SMOD U] ↔ x ∈ U := by rw [SModEq.def, Submodule.Quotient.eq, sub_zero]
101121
#align smodeq.zero SModEq.zero
102122

Mathlib/RingTheory/AdicCompletion/Algebra.lean

Lines changed: 63 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ providing as much API as possible.
2121
2222
-/
2323

24+
suppress_compilation
25+
2426
open Submodule
2527

2628
variable {R : Type*} [CommRing R] (I : Ideal R)
@@ -45,6 +47,11 @@ theorem transitionMap_map_mul {m n : ℕ} (hmn : m ≤ n) (x y : R ⧸ (I ^ n
4547
transitionMap I R hmn (x * y) = transitionMap I R hmn x * transitionMap I R hmn y :=
4648
Quotient.inductionOn₂' x y (fun _ _ ↦ rfl)
4749

50+
@[local simp]
51+
theorem transitionMap_map_pow {m n a : ℕ} (hmn : m ≤ n) (x : R ⧸ (I ^ n • ⊤ : Ideal R)) :
52+
transitionMap I R hmn (x ^ a) = transitionMap I R hmn x ^ a :=
53+
Quotient.inductionOn' x (fun _ ↦ rfl)
54+
4855
/-- `AdicCompletion.transitionMap` as an algebra homomorphism. -/
4956
def transitionMapₐ {m n : ℕ} (hmn : m ≤ n) :
5057
R ⧸ (I ^ n • ⊤ : Ideal R) →ₐ[R] R ⧸ (I ^ m • ⊤ : Ideal R) :=
@@ -59,11 +66,35 @@ def subalgebra : Subalgebra R (∀ n, R ⧸ (I ^ n • ⊤ : Ideal R)) :=
5966
def subring : Subring (∀ n, R ⧸ (I ^ n • ⊤ : Ideal R)) :=
6067
Subalgebra.toSubring (subalgebra I)
6168

62-
instance : CommRing (AdicCompletion I R) :=
63-
inferInstanceAs <| CommRing (subring I)
69+
instance : Mul (AdicCompletion I R) where
70+
mul x y := ⟨x.val * y.val, by simp [x.property, y.property]⟩
6471

65-
instance : Algebra R (AdicCompletion I R) :=
66-
inferInstanceAs <| Algebra R (subalgebra I)
72+
instance : One (AdicCompletion I R) where
73+
one := ⟨1, by simp⟩
74+
75+
instance : NatCast (AdicCompletion I R) where
76+
natCast n := ⟨n, fun _ ↦ rfl⟩
77+
78+
instance : IntCast (AdicCompletion I R) where
79+
intCast n := ⟨n, fun _ ↦ rfl⟩
80+
81+
instance : Pow (AdicCompletion I R) ℕ where
82+
pow x n := ⟨x.val ^ n, fun _ ↦ by simp [x.property]⟩
83+
84+
instance : CommRing (AdicCompletion I R) :=
85+
let f : AdicCompletion I R → ∀ n, R ⧸ (I ^ n • ⊤ : Ideal R) := Subtype.val
86+
Subtype.val_injective.commRing f rfl rfl
87+
(fun _ _ ↦ rfl) (fun _ _ ↦ rfl) (fun _ ↦ rfl) (fun _ _ ↦ rfl) (fun _ _ ↦ rfl)
88+
(fun _ _ ↦ rfl) (fun _ _ ↦ rfl) (fun _ ↦ rfl) (fun _ ↦ rfl)
89+
90+
instance : Algebra R (AdicCompletion I R) where
91+
toFun r := ⟨algebraMap R (∀ n, R ⧸ (I ^ n • ⊤ : Ideal R)) r, by simp⟩
92+
map_one' := Subtype.ext <| map_one _
93+
map_mul' x y := Subtype.ext <| map_mul _ x y
94+
map_zero' := Subtype.ext <| map_zero _
95+
map_add' x y := Subtype.ext <| map_add _ x y
96+
commutes' r x := Subtype.ext <| Algebra.commutes' r x.val
97+
smul_def' r x := Subtype.ext <| Algebra.smul_def' r x.val
6798

6899
@[simp]
69100
theorem val_one (n : ℕ) : (1 : AdicCompletion I R).val n = 1 :=
@@ -100,11 +131,35 @@ def AdicCauchySequence.subalgebra : Subalgebra R (ℕ → R) :=
100131
def AdicCauchySequence.subring : Subring (ℕ → R) :=
101132
Subalgebra.toSubring (AdicCauchySequence.subalgebra I)
102133

103-
instance : CommRing (AdicCauchySequence I R) :=
104-
inferInstanceAs <| CommRing (AdicCauchySequence.subring I)
134+
instance : Mul (AdicCauchySequence I R) where
135+
mul x y := ⟨x.val * y.val, fun hmn ↦ SModEq.mul (x.property hmn) (y.property hmn)⟩
136+
137+
instance : One (AdicCauchySequence I R) where
138+
one := ⟨1, fun _ ↦ rfl⟩
139+
140+
instance : NatCast (AdicCauchySequence I R) where
141+
natCast n := ⟨n, fun _ ↦ rfl⟩
105142

106-
instance : Algebra R (AdicCauchySequence I R) :=
107-
inferInstanceAs <| Algebra R (AdicCauchySequence.subalgebra I)
143+
instance : IntCast (AdicCauchySequence I R) where
144+
intCast n := ⟨n, fun _ ↦ rfl⟩
145+
146+
instance : Pow (AdicCauchySequence I R) ℕ where
147+
pow x n := ⟨x.val ^ n, fun hmn ↦ SModEq.pow n (x.property hmn)⟩
148+
149+
instance : CommRing (AdicCauchySequence I R) :=
150+
let f : AdicCauchySequence I R → (ℕ → R) := Subtype.val
151+
Subtype.val_injective.commRing f rfl rfl
152+
(fun _ _ ↦ rfl) (fun _ _ ↦ rfl) (fun _ ↦ rfl) (fun _ _ ↦ rfl) (fun _ _ ↦ rfl)
153+
(fun _ _ ↦ rfl) (fun _ _ ↦ rfl) (fun _ ↦ rfl) (fun _ ↦ rfl)
154+
155+
instance : Algebra R (AdicCauchySequence I R) where
156+
toFun r := ⟨algebraMap R (∀ _, R) r, fun _ ↦ rfl⟩
157+
map_one' := Subtype.ext <| map_one _
158+
map_mul' x y := Subtype.ext <| map_mul _ x y
159+
map_zero' := Subtype.ext <| map_zero _
160+
map_add' x y := Subtype.ext <| map_add _ x y
161+
commutes' r x := Subtype.ext <| Algebra.commutes' r x.val
162+
smul_def' r x := Subtype.ext <| Algebra.smul_def' r x.val
108163

109164
@[simp]
110165
theorem one_apply (n : ℕ) : (1 : AdicCauchySequence I R) n = 1 :=

Mathlib/RingTheory/AdicCompletion/Basic.lean

Lines changed: 70 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ with respect to an ideal `I`:
2727
2828
-/
2929

30+
suppress_compilation
3031

3132
open Submodule
3233

@@ -213,11 +214,43 @@ def submodule : Submodule R (∀ n : ℕ, M ⧸ (I ^ n • ⊤ : Submodule R M))
213214
rw [Pi.add_apply, Pi.add_apply, LinearMap.map_add, hf hmn, hg hmn]
214215
smul_mem' c f hf m n hmn := by rw [Pi.smul_apply, Pi.smul_apply, LinearMap.map_smul, hf hmn]
215216

217+
instance : Zero (AdicCompletion I M) where
218+
zero := ⟨0, by simp⟩
219+
220+
instance : Add (AdicCompletion I M) where
221+
add x y := ⟨x.val + y.val, by simp [x.property, y.property]⟩
222+
223+
instance : Neg (AdicCompletion I M) where
224+
neg x := ⟨- x.val, by simp [x.property]⟩
225+
226+
instance : Sub (AdicCompletion I M) where
227+
sub x y := ⟨x.val - y.val, by simp [x.property, y.property]⟩
228+
229+
instance : SMul ℕ (AdicCompletion I M) where
230+
smul n x := ⟨n • x.val, by simp [x.property]⟩
231+
232+
instance : SMul ℤ (AdicCompletion I M) where
233+
smul n x := ⟨n • x.val, by simp [x.property]⟩
234+
216235
instance : AddCommGroup (AdicCompletion I M) :=
217-
inferInstanceAs <| AddCommGroup (submodule I M)
236+
let f : AdicCompletion I M → ∀ n, M ⧸ (I ^ n • ⊤ : Submodule R M) := Subtype.val
237+
Subtype.val_injective.addCommGroup f rfl (fun _ _ ↦ rfl) (fun _ ↦ rfl) (fun _ _ ↦ rfl)
238+
(fun _ _ ↦ rfl) (fun _ _ ↦ rfl)
239+
240+
instance : SMul R (AdicCompletion I M) where
241+
smul r x := ⟨r • x.val, by simp [x.property]⟩
218242

219243
instance : Module R (AdicCompletion I M) :=
220-
inferInstanceAs <| Module R (submodule I M)
244+
let f : AdicCompletion I M →+ ∀ n, M ⧸ (I ^ n • ⊤ : Submodule R M) :=
245+
{ toFun := Subtype.val, map_zero' := rfl, map_add' := fun _ _ ↦ rfl }
246+
Subtype.val_injective.module R f (fun _ _ ↦ rfl)
247+
248+
/-- The canonical inclusion from the completion to the product. -/
249+
@[simps]
250+
def incl : AdicCompletion I M →ₗ[R] (∀ n, M ⧸ (I ^ n • ⊤ : Submodule R M)) where
251+
toFun x := x.val
252+
map_add' _ _ := rfl
253+
map_smul' _ _ := rfl
221254

222255
/-- The canonical linear map to the completion. -/
223256
def of : M →ₗ[R] AdicCompletion I M where
@@ -279,6 +312,11 @@ theorem val_add (n : ℕ) (f g : AdicCompletion I M) : (f + g).val n = f.val n +
279312
theorem val_sub (n : ℕ) (f g : AdicCompletion I M) : (f - g).val n = f.val n - g.val n :=
280313
rfl
281314

315+
@[simp]
316+
theorem val_sum {α : Type*} (s : Finset α) (f : α → AdicCompletion I M) (n : ℕ) :
317+
(Finset.sum s f).val n = Finset.sum s (fun a ↦ (f a).val n) := by
318+
simp_rw [← incl_apply, map_sum, Finset.sum_apply]
319+
282320
/- No `simp` attribute, since it causes `simp` unification timeouts when considering
283321
the `AdicCompletion I R` module instance on `AdicCompletion I M` (see `AdicCompletion/Algebra`). -/
284322
theorem val_smul (n : ℕ) (r : R) (f : AdicCompletion I M) : (r • f).val n = r • f.val n :=
@@ -361,14 +399,39 @@ def submodule : Submodule R (ℕ → M) where
361399
intro r f hf m n hmn
362400
exact SModEq.smul (hf hmn) r
363401

364-
instance : CoeFun (AdicCauchySequence I M) (fun _ ↦ ℕ → M) where
365-
coe f := f.val
402+
instance : Zero (AdicCauchySequence I M) where
403+
zero := ⟨0, fun _ ↦ rfl⟩
404+
405+
instance : Add (AdicCauchySequence I M) where
406+
add x y := ⟨x.val + y.val, fun hmn ↦ SModEq.add (x.property hmn) (y.property hmn)⟩
407+
408+
instance : Neg (AdicCauchySequence I M) where
409+
neg x := ⟨- x.val, fun hmn ↦ SModEq.neg (x.property hmn)⟩
410+
411+
instance : Sub (AdicCauchySequence I M) where
412+
sub x y := ⟨x.val - y.val, fun hmn ↦ SModEq.sub (x.property hmn) (y.property hmn)⟩
366413

367-
instance : AddCommGroup (AdicCauchySequence I M) :=
368-
inferInstanceAs <| AddCommGroup (AdicCauchySequence.submodule I M)
414+
instance : SMul ℕ (AdicCauchySequence I M) where
415+
smul n x := ⟨n • x.val, fun hmn ↦ SModEq.nsmul (x.property hmn) n⟩
416+
417+
instance : SMul ℤ (AdicCauchySequence I M) where
418+
smul n x := ⟨n • x.val, fun hmn ↦ SModEq.zsmul (x.property hmn) n⟩
419+
420+
instance : AddCommGroup (AdicCauchySequence I M) := by
421+
let f : AdicCauchySequence I M → (ℕ → M) := Subtype.val
422+
apply Subtype.val_injective.addCommGroup f rfl (fun _ _ ↦ rfl) (fun _ ↦ rfl) (fun _ _ ↦ rfl)
423+
(fun _ _ ↦ rfl) (fun _ _ ↦ rfl)
424+
425+
instance : SMul R (AdicCauchySequence I M) where
426+
smul r x := ⟨r • x.val, fun hmn ↦ SModEq.smul (x.property hmn) r⟩
369427

370428
instance : Module R (AdicCauchySequence I M) :=
371-
inferInstanceAs <| Module R (AdicCauchySequence.submodule I M)
429+
let f : AdicCauchySequence I M →+ (ℕ → M) :=
430+
{ toFun := Subtype.val, map_zero' := rfl, map_add' := fun _ _ ↦ rfl }
431+
Subtype.val_injective.module R f (fun _ _ ↦ rfl)
432+
433+
instance : CoeFun (AdicCauchySequence I M) (fun _ ↦ ℕ → M) where
434+
coe f := f.val
372435

373436
@[simp]
374437
theorem zero_apply (n : ℕ) : (0 : AdicCauchySequence I M) n = 0 :=

Mathlib/RingTheory/AdicCompletion/Functoriality.lean

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ In this file we establish functorial properties of the adic completion.
2424
2525
-/
2626

27+
suppress_compilation
28+
2729
variable {R : Type*} [CommRing R] (I : Ideal R)
2830
variable {M : Type*} [AddCommGroup M] [Module R M]
2931
variable {N : Type*} [AddCommGroup N] [Module R N]
@@ -191,12 +193,6 @@ theorem map_zero : map I (0 : M →ₗ[R] N) = 0 := by
191193
ext
192194
simp
193195

194-
@[simp]
195-
theorem val_sum {α : Type*} (s : Finset α) (f : α → AdicCompletion I M) (n : ℕ) :
196-
(Finset.sum s f).val n = Finset.sum s (fun a ↦ (f a).val n) := by
197-
change (Submodule.subtype (AdicCompletion.submodule I M) _) n = _
198-
rw [map_sum, Finset.sum_apply, Submodule.coeSubtype]
199-
200196
/-- A linear equiv induces a linear equiv on adic completions. -/
201197
def congr (f : M ≃ₗ[R] N) :
202198
AdicCompletion I M ≃ₗ[AdicCompletion I R] AdicCompletion I N :=

0 commit comments

Comments
 (0)