Skip to content

feat: add normalize_instance% using core's normalizeInstance#37073

Open
kim-em wants to merge 2 commits intomasterfrom
replace_fast_instance
Open

feat: add normalize_instance% using core's normalizeInstance#37073
kim-em wants to merge 2 commits intomasterfrom
replace_fast_instance

Conversation

@kim-em
Copy link
Copy Markdown
Contributor

@kim-em kim-em commented Mar 24, 2026

This PR adds a new normalize_instance% term elaborator that delegates to Lean core's
normalizeInstance (from leanprover/lean4#12897), and replaces ~53
fast_instance% call sites where the replacement is safe.

normalize_instance% is a thin wrapper (~15 lines) around Lean.Meta.normalizeInstance. Like
fast_instance%, it reduces instances to constructor applications and reuses canonical
sub-instances. Unlike fast_instance%, it works at instances transparency and delegates all
normalization logic to core.

The replaced sites cover:

  • Equiv/type-alias transfers (WithAbs, WithConv, WithVal, WithLp, TransferInstance)
  • Surjective constructors (Con, RingCon quotients, module congruences)
  • DFunLike leaf instances (MultilinearMap, ContinuousMultilinearMap, AlternatingMap, Intertwining)
  • Quotient instances (LinearAlgebra/Quotient/Defs)
  • inferInstanceAs% sites (StandardPart, Presentation/Core)
  • Miscellaneous (InfiniteAdeleRing, ZMod)

This PR doesn't attempt to replace all fast_instance%; it gets more complicated!

🤖 Prepared with Claude Code

@github-actions github-actions bot added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Mar 24, 2026
@kim-em kim-em force-pushed the replace_fast_instance branch from b2d6174 to 9e22b9c Compare March 24, 2026 06:00
@kim-em
Copy link
Copy Markdown
Contributor Author

kim-em commented Mar 24, 2026

!radar

kim-em and others added 2 commits March 24, 2026 07:21
…zeInstance`

Adds a new `normalize_instance%` term elaborator that delegates instance
normalization to Lean core's `normalizeInstance` (from lean4#12897),
working at `instances` transparency. This is a step toward eventually
replacing `fast_instance%` with core functionality.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…ible

Replace ~53 `fast_instance%` call sites with `normalize_instance%`, which
delegates to Lean core's `normalizeInstance`. This covers equiv/type-alias
transfers, surjective constructors, DFunLike leaf instances, quotient
instances, and inferInstanceAs% sites.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@kim-em kim-em force-pushed the replace_fast_instance branch from 9e22b9c to 41b9556 Compare March 24, 2026 07:22
@github-actions github-actions bot removed the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Mar 24, 2026
@github-actions
Copy link
Copy Markdown

PR summary 1fb91738ac

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference

Declarations diff

+ instance (v : AbsoluteValue R S) : Ring (WithAbs v) := normalize_instance% (equiv v).ring
+ instance : AddCommGroup (ContinuousMultilinearMap R M₁ M₂) := normalize_instance%
+ instance : AddCommGroup (M [⋀^ι]→L[R] N) := normalize_instance%
+ instance : AddCommGroup (MultilinearMap R M₁ M₂) := normalize_instance%
+ instance : Algebra P.Core R := normalize_instance% (inferInstanceAs <| Algebra P.core R)
+ instance : Algebra P.Core S := normalize_instance% (inferInstanceAs <| Algebra P.core S)
+ instance : CommRing (InfiniteAdeleRing K) := normalize_instance% Pi.commRing
+ instance : CommRing (WithAbs v) := normalize_instance% (equiv v).commRing
+ instance : CommRing (WithVal v) := normalize_instance% (equiv v).commRing
+ instance : CommRing P.Core := normalize_instance% (inferInstanceAs <| CommRing P.core)
+ instance : CommSemiring (WithAbs v) := normalize_instance% (equiv v).commSemiring
+ instance : Field (WithVal v) := normalize_instance% (equiv v).field
+ instance : Module R (M [⋀^ι]→L[A] N) := normalize_instance%
+ instance : Module R' (ContinuousMultilinearMap A M₁ M₂) := normalize_instance%
+ instance : Module S (MultilinearMap R M₁ M₂) := normalize_instance%
+ instance : MulAction R' (ContinuousMultilinearMap A M₁ M₂) := normalize_instance%
+ instance : MulAction R' (M [⋀^ι]→L[A] N) := normalize_instance%
+ instance [ContinuousAdd N] : DistribMulAction R (M [⋀^ι]→L[A] N) := normalize_instance%
- instance (v : AbsoluteValue R S) : Ring (WithAbs v) := fast_instance% (equiv v).ring
- instance : AddCommGroup (ContinuousMultilinearMap R M₁ M₂) := fast_instance%
- instance : AddCommGroup (M [⋀^ι]→L[R] N) := fast_instance%
- instance : AddCommGroup (MultilinearMap R M₁ M₂) := fast_instance%
- instance : Algebra P.Core R := fast_instance% (inferInstanceAs <| Algebra P.core R)
- instance : Algebra P.Core S := fast_instance% (inferInstanceAs <| Algebra P.core S)
- instance : CommRing (InfiniteAdeleRing K) := fast_instance% Pi.commRing
- instance : CommRing (WithAbs v) := fast_instance% (equiv v).commRing
- instance : CommRing (WithVal v) := fast_instance% (equiv v).commRing
- instance : CommRing P.Core := fast_instance% (inferInstanceAs <| CommRing P.core)
- instance : CommSemiring (WithAbs v) := fast_instance% (equiv v).commSemiring
- instance : Field (WithVal v) := fast_instance% (equiv v).field
- instance : Module R (M [⋀^ι]→L[A] N) := fast_instance%
- instance : Module R' (ContinuousMultilinearMap A M₁ M₂) := fast_instance%
- instance : Module S (MultilinearMap R M₁ M₂) := fast_instance%
- instance : MulAction R' (ContinuousMultilinearMap A M₁ M₂) := fast_instance%
- instance : MulAction R' (M [⋀^ι]→L[A] N) := fast_instance%
- instance [ContinuousAdd N] : DistribMulAction R (M [⋀^ι]→L[A] N) := fast_instance%
-+-+-+ addCommMonoid

You can run this locally as follows
## summary with just the declaration names:
./scripts/pr_summary/declarations_diff.sh <optional_commit>

## more verbose report:
./scripts/pr_summary/declarations_diff.sh long <optional_commit>

The doc-module for scripts/pr_summary/declarations_diff.sh contains some details about this script.


No changes to technical debt.

You can run this locally as

./scripts/reporting/technical-debt-metrics.sh pr_summary
  • The relative value is the weighted sum of the differences with weight given by the inverse of the current value of the statistic.
  • The absolute value is the relative value divided by the total sum of the inverses of the current values (i.e. the weighted average of the differences).

@kim-em
Copy link
Copy Markdown
Contributor Author

kim-em commented Mar 24, 2026

!radar

@kim-em
Copy link
Copy Markdown
Contributor Author

kim-em commented Mar 24, 2026

!radar

@leanprover-radar
Copy link
Copy Markdown

leanprover-radar commented Mar 24, 2026

Benchmark results for 41b9556 against 1fb9173 are in. There are no significant changes. @kim-em

  • 🟥 build//instructions: +36.2G (+0.02%)

Small changes (1✅, 1🟥)

  • 🟥 build/module/Mathlib.Combinatorics.Young.SemistandardTableau//instructions: +274.6M (+4.69%)
  • build/module/Mathlib.Util.GetAllModules//instructions: -275.3M (-10.47%)

@JovanGerb
Copy link
Copy Markdown
Contributor

I agree it would be nice to have the same backend implementation for both inferInstanceAs and fast_insance% (or its equivalent). The normalize instances function in core does still have some rought edges: I've seen it throw some kernel errors for no apparent reason. It would be interesting to see how much of the current fast_instance% can be replaces by normalize_instance% and how this affects performance.

(When we do merge this, the doc-string of normalize_instance% should be a standalone explanation, instead of comparing itself with fast_instance%)

@mathlib-merge-conflicts mathlib-merge-conflicts bot added the merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot) label Mar 27, 2026
@mathlib-merge-conflicts
Copy link
Copy Markdown

This pull request has conflicts, please merge master and resolve them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-conflict The PR has a merge conflict with master, and needs manual merging. (this label is managed by a bot)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants