Skip to content

feat: adjust instances in inferInstanceAs and def ... deriving#12897

Merged
nomeata merged 3 commits intoleanprover:masterfrom
Kha:push-vqmkmnsnsrvl
Mar 22, 2026
Merged

feat: adjust instances in inferInstanceAs and def ... deriving#12897
nomeata merged 3 commits intoleanprover:masterfrom
Kha:push-vqmkmnsnsrvl

Conversation

@Kha
Copy link
Copy Markdown
Member

@Kha Kha commented Mar 12, 2026

This PR adjusts the results of inferInstanceAs and the def deriving handler to conform to recently strengthened restrictions on reducibility. This change ensures that when deriving or inferring an instance for a semireducible type definition, the definition's RHS is not leaked when the instance is reduced at lower than semireducible transparency.

More specifically, given the "source type" and "target type" (the given and expected type for inferInstanceAs, the right-hand side and applied left-hand side of the def for deriving), we synthesize an instance for the source type and then unfold and rewrap its components (fields, nested instances) as necessary to make them compatible with the target type. The individual steps are represented by the following options, which all default to enabled and can be disabled to help with porting:

  • backward.inferInstanceAs.wrap: master switch for instance adjustment in both inferInstanceAs and the default deriving handler
  • backward.inferInstanceAs.wrap.reuseSubInstances: reuse existing instances for the target type for sub-instance fields to avoid non-defeq instance diamonds
  • backward.inferInstanceAs.wrap.instances: wrap non-reducible instances in auxiliary definitions
  • backward.inferInstanceAs.wrap.data: wrap data fields in auxiliary definitions (proof fields are always wrapped)

This PR is an extension and rewrite of prior work in Mathlib: leanprover-community/mathlib4#36420

Last(?) part of fix for #9077

🤖 Prepared with Claude Code

Breaking changes

Proofs that relied on the prior "defeq abuse" of these instance or that depended on their specific structure may need adjustments. As inferInstanceAs A now needs to know the source and target types exactly before it can continue, it cannot be used anymore as a synonym for (inferInstance : A), use the latter instead when source and target type are identical.

@github-actions github-actions Bot added the toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN label Mar 12, 2026
@leanprover-bot
Copy link
Copy Markdown
Collaborator

leanprover-bot commented Mar 12, 2026

Reference manual CI status:

  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2026-03-09 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-manual, reference manual CI should run now. You can force reference manual CI using the force-manual-ci label. (2026-03-12 18:11:21)
  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2026-03-17 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-manual, reference manual CI should run now. You can force reference manual CI using the force-manual-ci label. (2026-03-18 05:08:40)
  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2026-03-18 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-manual, reference manual CI should run now. You can force reference manual CI using the force-manual-ci label. (2026-03-19 05:16:11)

mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Mar 12, 2026
@github-actions github-actions Bot added the mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN label Mar 12, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Mar 12, 2026
@mathlib-lean-pr-testing mathlib-lean-pr-testing Bot added the breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan label Mar 12, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

@Kha Kha force-pushed the push-vqmkmnsnsrvl branch from be5f146 to ee406f7 Compare March 17, 2026 08:24
mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Mar 17, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Mar 17, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

@kim-em kim-em force-pushed the push-vqmkmnsnsrvl branch 2 times, most recently from 468f21d to 4547327 Compare March 18, 2026 04:18
kim-em pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Mar 18, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Mar 18, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Mar 18, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Mar 18, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Mar 18, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Mar 18, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Mar 18, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Mar 18, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Mar 18, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Mar 18, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Mar 18, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

@kim-em kim-em changed the title experiment: normalize instances from def deriving feat: normalize instances in inferInstanceAs and deriving Mar 22, 2026
@kim-em kim-em marked this pull request as ready for review March 22, 2026 06:34
@kim-em kim-em added backport releases/v4.29.0 changelog-language Language features and metaprograms labels Mar 22, 2026
mathlib-nightly-testing Bot pushed a commit to leanprover-community/batteries that referenced this pull request Mar 22, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

@Kha
Copy link
Copy Markdown
Member Author

Kha commented Mar 22, 2026

!bench

@leanprover-radar
Copy link
Copy Markdown

leanprover-radar commented Mar 22, 2026

Benchmark results for 30e3a09 against 90b5e31 are in. Significant changes detected! @Kha

  • 🟥 build//instructions: +7.1G (+0.06%)

Large changes (5🟥)

  • 🟥 compiled/parser//instructions: +192.6M (+0.51%)
  • 🟥 elab/charactersIn//instructions: +12.1G (+24.75%)
  • 🟥 elab/charactersIn//task-clock: +365ms (+16.89%)
  • 🟥 elab/charactersIn//wall-clock: +365ms (+16.81%)
  • 🟥 elab/workspaceSymbols//instructions: +674.5M (+2.05%)

Medium changes (3✅, 4🟥)

  • build/module/Lake.CLI.Main//instructions: -2.0G (-5.72%) (reduced significance based on absolute threshold)
  • build/module/Lean.Compiler.IR.EmitLLVM//instructions: -1.2G (-2.95%)
  • build/module/Lean.Meta.Tactic.Grind.EMatch//instructions: -1.5G (-2.04%) (reduced significance based on absolute threshold)
  • 🟥 misc/import Lean//instructions: +24.8M (+1.64%)
  • 🟥 size/Init/.olean//bytes: +376kiB (+0.41%)
  • 🟥 size/all/.olean//bytes: +1MiB (+0.43%)
  • 🟥 size/libleanshared.so//bytes: +620kiB (+0.42%)

Small changes (142✅, 127🟥)

Too many entries to display here. View the full report on radar instead.

@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

Kha added 3 commits March 22, 2026 12:12
This PR adjusts the results of `inferInstanceAs` and the `def` `deriving` handler to conform to recently strengthened restrictions on reducibility. This change ensures that when deriving or inferring an instance for a semireducible type definition, the definition's RHS is not leaked when the instance is reduced at lower than semireducible transparency.

More specifically, given the "source type" and "target type" (the given and expected type for `inferInstanceAs`, the right-hand side and applied left-hand side of the `def` for `deriving`), we synthesize an instance for the source type and then unfold and rewrap its components (fields, nested instances) as necessary to make them compatible with the target type. The individual steps are represented by the following options, which all default to enabled and can be disabled to help with porting:
- `backward.inferInstanceAs.wrap`: master switch for instance adjustment in both `inferInstanceAs` and the default `deriving` handler
- `backward.inferInstanceAs.wrap.reuseSubInstances`: reuse existing instances for the target type for sub-instance fields to avoid non-defeq instance diamonds
- `backward.inferInstanceAs.wrap.instances`: wrap non-reducible instances in auxiliary definitions
- `backward.inferInstanceAs.wrap.data`: wrap data fields in auxiliary definitions (proof fields are always wrapped)

This PR is an extension and rewrite of prior work in Mathlib: leanprover-community/mathlib4#36420

Last(?) part of fix for leanprover#9077

🤖 Prepared with Claude Code

# Breaking changes

Proofs that relied on the prior "defeq abuse" of these instance or that depended on their specific structure may need adjustments. As `inferInstanceAs A` now needs to know the source and target types exactly before it can continue, it cannot be used anymore as a synonym for `(inferInstance : A)`, use the latter instead when source and target type are identical.
@github-actions
Copy link
Copy Markdown
Contributor

The backport to releases/v4.29.0 failed:

The process '/usr/bin/git' failed with exit code 1

To backport manually, run these commands in your terminal:

# Fetch latest updates from GitHub
git fetch
# Create a new working tree
git worktree add .worktrees/backport-releases/v4.29.0 releases/v4.29.0
# Navigate to the new working tree
cd .worktrees/backport-releases/v4.29.0
# Create a new branch
git switch --create backport-12897-to-releases/v4.29.0
# Cherry-pick the merged commit of this pull request and resolve the conflicts
git cherry-pick -x --mainline 1 4bf7fa7447eea00cecba8327bb9c9e5f4485f0a7
# Push it to GitHub
git push --set-upstream origin backport-12897-to-releases/v4.29.0
# Go back to the original working tree
cd ../..
# Delete the working tree
git worktree remove .worktrees/backport-releases/v4.29.0

Then, create a pull request where the base branch is releases/v4.29.0 and the compare/head branch is backport-12897-to-releases/v4.29.0.

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

Labels

backport releases/v4.29.0 breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan changelog-language Language features and metaprograms mathlib4-nightly-available A branch for this PR exists at leanprover-community/mathlib4-nightly-testing:lean-pr-testing-NNNN merge-ci Enable merge queue CI checks for PR. In particular, produce artifacts for all major platforms. toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants