Skip to content

perf: make the support of onFinset opaque #24944

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions Mathlib/Data/Finsupp/Defs.lean
Original file line number Diff line number Diff line change
Expand Up @@ -215,36 +215,40 @@ section OnFinset

variable [Zero M]

private irreducible_def onFinset_support (s : Finset α) (f : α → M) : Finset α :=
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the kernel problematic here or just the elaborator?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know. It would indeed be a good test to make this just @[irreducible] and see if the performance is any different. Do you mind trying that?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can but I don't think I will have time until early next week.

haveI := Classical.decEq M
{a ∈ s | f a ≠ 0}

/-- `Finsupp.onFinset s f hf` is the finsupp function representing `f` restricted to the finset `s`.
The function must be `0` outside of `s`. Use this when the set needs to be filtered anyways,
otherwise a better set representation is often available. -/
def onFinset (s : Finset α) (f : α → M) (hf : ∀ a, f a ≠ 0 → a ∈ s) : α →₀ M where
support :=
haveI := Classical.decEq M
{a ∈ s | f a ≠ 0}
support := onFinset_support s f
toFun := f
mem_support_toFun := by classical simpa
mem_support_toFun := by classical simpa [onFinset_support_def]

@[simp, norm_cast] lemma coe_onFinset (s : Finset α) (f : α → M) (hf) : onFinset s f hf = f := rfl

@[simp]
theorem onFinset_apply {s : Finset α} {f : α → M} {hf a} : (onFinset s f hf : α →₀ M) a = f a :=
rfl

theorem support_onFinset [DecidableEq M] {s : Finset α} {f : α → M}
(hf : ∀ a : α, f a ≠ 0 → a ∈ s) :
(Finsupp.onFinset s f hf).support = {a ∈ s | f a ≠ 0} := by
dsimp [onFinset]; rw [onFinset_support]; congr

@[simp]
theorem support_onFinset_subset {s : Finset α} {f : α → M} {hf} :
(onFinset s f hf).support ⊆ s := by
classical convert filter_subset (f · ≠ 0) s
classical
rw [support_onFinset]
exact filter_subset (f · ≠ 0) s

theorem mem_support_onFinset {s : Finset α} {f : α → M} (hf : ∀ a : α, f a ≠ 0 → a ∈ s) {a : α} :
a ∈ (Finsupp.onFinset s f hf).support ↔ f a ≠ 0 := by
rw [Finsupp.mem_support_iff, Finsupp.onFinset_apply]

theorem support_onFinset [DecidableEq M] {s : Finset α} {f : α → M}
(hf : ∀ a : α, f a ≠ 0 → a ∈ s) :
(Finsupp.onFinset s f hf).support = {a ∈ s | f a ≠ 0} := by
dsimp [onFinset]; congr

end OnFinset

section OfSupportFinite
Expand Down
7 changes: 6 additions & 1 deletion Mathlib/RingTheory/AlgebraTower.lean
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,15 @@ variable (b : Basis ι R M) (h : Function.Bijective (algebraMap R A))

/-- If `R` and `A` have a bijective `algebraMap R A` and act identically on `M`,
then a basis for `M` as `R`-module is also a basis for `M` as `R'`-module. -/
@[simps! repr_apply_support_val repr_apply_toFun]
@[simps! repr_apply_toFun]
noncomputable def Basis.algebraMapCoeffs : Basis ι A M :=
b.mapCoeffs (RingEquiv.ofBijective _ h) fun c x => by simp

@[simp]
theorem Basis.algebraMapCoeffs_repr (m : M) :
(b.algebraMapCoeffs A h).repr m = (b.repr m).mapRange (algebraMap R A) (map_zero _) := by
rfl

theorem Basis.algebraMapCoeffs_apply (i : ι) : b.algebraMapCoeffs A h i = b i :=
b.mapCoeffs_apply _ _ _

Expand Down
2 changes: 1 addition & 1 deletion Mathlib/RingTheory/MvPowerSeries/Basic.lean
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ variable (σ) (R)
def C : R →+* MvPowerSeries σ R :=
{ monomial R (0 : σ →₀ ℕ) with
map_one' := rfl
map_mul' := fun a b => (monomial_mul_monomial 0 0 a b).symm
map_mul' := fun a b => Eq.trans (by simp) (monomial_mul_monomial _ _ a b).symm
map_zero' := (monomial R 0).map_zero }

variable {σ} {R}
Expand Down