Skip to content

feat: adjust deriving Inhabited to use structure field defaults#9815

Merged
kmill merged 8 commits intoleanprover:masterfrom
kmill:kmill_9463
Apr 9, 2026
Merged

feat: adjust deriving Inhabited to use structure field defaults#9815
kmill merged 8 commits intoleanprover:masterfrom
kmill:kmill_9463

Conversation

@kmill
Copy link
Copy Markdown
Collaborator

@kmill kmill commented Aug 10, 2025

This PR changes the Inhabited deriving handler for structure types to use default field values when present; this ensures that {} and default are interchangeable when all fields have default values. The handler effectively uses by refine' {..} <;> exact default to construct the inhabitant. (Note: when default field values cannot be resolved, they are ignored, as usual for ellipsis mode.)

Implementation note: the handler now constructs the Expr directly and adds it to the environment, though the instance is still added using elabCommand.

Closes #9463

@kmill kmill requested a review from kim-em as a code owner August 10, 2025 00:44
@kmill kmill added the changelog-language Language features and metaprograms label Aug 10, 2025
@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 Aug 10, 2025
@leanprover-bot
Copy link
Copy Markdown
Collaborator

leanprover-bot commented Aug 10, 2025

Reference manual CI status:

  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2025-08-06 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. (2025-08-10 01:47:53)
  • 💥 Reference manual branch lean-pr-testing-9815 build failed against this PR. (2026-04-01 19:45:01) View Log
  • 🟡 Reference manual branch lean-pr-testing-9815 build against this PR didn't complete normally. (2026-04-01 19:45:13) View Log
  • 💥 Reference manual branch lean-pr-testing-9815 build failed against this PR. (2026-04-07 14:25:02) View Log
  • 🟡 Reference manual branch lean-pr-testing-9815 build against this PR didn't complete normally. (2026-04-07 14:25:08) View Log
  • 💥 Reference manual branch lean-pr-testing-9815 build failed against this PR. (2026-04-07 16:46:02) View Log
  • 🟡 Reference manual branch lean-pr-testing-9815 build against this PR didn't complete normally. (2026-04-07 16:46:42) View Log
  • ✅ Reference manual branch lean-pr-testing-9815 has successfully built against this PR. (2026-04-08 18:16:16) View Log
  • 🟡 Reference manual branch lean-pr-testing-9815 build against this PR didn't complete normally. (2026-04-08 18:17:27) View Log
  • ✅ Reference manual branch lean-pr-testing-9815 has successfully built against this PR. (2026-04-09 04:00:52) View Log
  • 🟡 Reference manual branch lean-pr-testing-9815 build against this PR didn't complete normally. (2026-04-09 04:01:57) View Log
  • ✅ Reference manual branch lean-pr-testing-9815 has successfully built against this PR. (2026-04-09 17:35:50) View Log
  • ✅ Reference manual branch lean-pr-testing-9815 has successfully built against this PR. (2026-04-09 17:36:19) View Log
  • 🟡 Reference manual branch lean-pr-testing-9815 build against this PR didn't complete normally. (2026-04-09 17:36:30) View Log
  • 🟡 Reference manual branch lean-pr-testing-9815 build against this PR didn't complete normally. (2026-04-09 17:37:41) View Log

ghost pushed a commit to leanprover-community/batteries that referenced this pull request Aug 10, 2025
ghost pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Aug 10, 2025
@ghost ghost added the builds-mathlib CI has verified that Mathlib builds against this PR label Aug 10, 2025
@ghost
Copy link
Copy Markdown

ghost commented Aug 10, 2025

Mathlib CI status (docs):

@kmill kmill added this pull request to the merge queue Aug 10, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 10, 2025
@kmill kmill marked this pull request as draft August 11, 2025 12:30
@kmill
Copy link
Copy Markdown
Collaborator Author

kmill commented Aug 11, 2025

The strategy doesn't work in the prelude, since refine doesn't exist yet.

Best would be a handler that doesn't go through elabCommand, since we're able to make a precise Expr easily enough.

This is also currently just a problem with prelude, and we could write those Inhabited instance by hand.

kmill added 3 commits April 1, 2026 08:06
This PR changes the deriving handler for `Inhabited` instances so that for structures it uses each fields' default values, if possible. For structure type `S`, the handler uses `by refine' {.. : S} <;> exact default` to construct the default value. A limitation to this is that if there are any stuck defaults due to cycles, they use `default`.

Closes leanprover#9463
mathlib-nightly-testing bot pushed a commit to leanprover-community/batteries that referenced this pull request Apr 1, 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 Apr 1, 2026
leanprover-bot added a commit to leanprover/reference-manual that referenced this pull request Apr 1, 2026
mathlib-nightly-testing bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Apr 1, 2026
@leanprover-bot leanprover-bot added the breaks-manual This is not necessarily a blocker for merging, but there needs to be a plan. label Apr 1, 2026
@mathlib-lean-pr-testing mathlib-lean-pr-testing bot added breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan and removed builds-mathlib CI has verified that Mathlib builds against this PR labels Apr 1, 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 Apr 7, 2026
mathlib-nightly-testing bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Apr 7, 2026
leanprover-bot added a commit to leanprover/reference-manual that referenced this pull request Apr 7, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

@mathlib-lean-pr-testing mathlib-lean-pr-testing bot added builds-mathlib CI has verified that Mathlib builds against this PR and removed breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan labels Apr 8, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

@kmill kmill marked this pull request as ready for review April 8, 2026 20:07
@kmill kmill enabled auto-merge April 8, 2026 20:08
@kmill kmill added this pull request to the merge queue Apr 8, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 8, 2026
@kmill kmill requested a review from Kha as a code owner April 9, 2026 02:54
mathlib-nightly-testing bot pushed a commit to leanprover-community/batteries that referenced this pull request Apr 9, 2026
mathlib-nightly-testing bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Apr 9, 2026
leanprover-bot added a commit to leanprover/reference-manual that referenced this pull request Apr 9, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

deriving Inhabited

instance : Inhabited TacticParsedSnapshot where
default := { toSnapshot := default, stx := default, finished := default }
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@Kha Language.Snapshot uses decl_name% to fill desc, so without the override here (and in Snapshot), you get "Lean.Elab.Tactic.instInhabitedTacticParsedSnapshot" as the desc. This appears in the output of the snapshotTree.lean test, because the default value is actually used somewhere.

A few questions for you:

  1. Is going through this effort to have a blank desc worth it, or would it be ok to let desc be "Lean.Elab.Tactic.instInhabitedTacticParsedSnapshot"?
  2. Notice I have to do toSnapshot := default to inherit the Inhabited instance. I'm concerned about this, since it might invite mistakes. It suggests that I should add an "elaborate an Inhabited instance mode" to the structure instance elaborator, where it would try inheriting parent default values, similar to what it does for parent classes.
    i. This would be more engineering effort... Maybe this PR leaves things better than they were and no need to chase perfection?
    ii. Or would it be better to have a mode to turn off autoParams, which would fix the immediate issue?
    iii. Or is it already perfect as it is?

The overarching question is "are you concerned about this instance needing to be written, or does it look fine?"

@kmill kmill added this pull request to the merge queue Apr 9, 2026
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 9, 2026
mathlib-nightly-testing bot pushed a commit to leanprover-community/batteries that referenced this pull request Apr 9, 2026
mathlib-nightly-testing bot pushed a commit to leanprover-community/mathlib4-nightly-testing that referenced this pull request Apr 9, 2026
leanprover-bot added a commit to leanprover/reference-manual that referenced this pull request Apr 9, 2026
@mathlib-lean-pr-testing
Copy link
Copy Markdown

Mathlib CI status (docs):

@kmill kmill added this pull request to the merge queue Apr 9, 2026
Merged via the queue into leanprover:master with commit e0a29f4 Apr 9, 2026
20 checks passed
kmill added a commit that referenced this pull request Apr 13, 2026
…nstances

This PR makes the `deriving Inhabited` handler for `structure`s be able to inherit `Inhabited` instances from structure parents, using the same mechanism as for class parents. This fixes a regression introduced by #9815, which lost the ability to apply `Inhabited` instances for parents represented as subobject fields. With this PR, now it works for all parents.

Implementation detail: adds `struct_inst_default%` for synthesizing a structure default value using `Inhabited` instances for parents and fields.

Closes #13372
github-merge-queue bot pushed a commit that referenced this pull request Apr 14, 2026
…nstances (#13395)

This PR makes the `deriving Inhabited` handler for `structure`s be able
to inherit `Inhabited` instances from structure parents, using the same
mechanism as for class parents. This fixes a regression introduced by
#9815, which lost the ability to apply `Inhabited` instances for parents
represented as subobject fields. With this PR, now it works for all
parents in the hierarchy.

Implementation detail: adds `struct_inst_default%` for synthesizing a
structure default value using `Inhabited` instances for parents and
fields.

Closes #13372
volodeyka pushed a commit that referenced this pull request Apr 16, 2026
)

This PR changes the `Inhabited` deriving handler for `structure` types
to use default field values when present; this ensures that `{}` and
`default` are interchangeable when all fields have default values. The
handler effectively uses `by refine' {..} <;> exact default` to
construct the inhabitant. (Note: when default field values cannot be
resolved, they are ignored, as usual for ellipsis mode.)

Implementation note: the handler now constructs the `Expr` directly and
adds it to the environment, though the `instance` is still added using
`elabCommand`.

Closes #9463
volodeyka pushed a commit that referenced this pull request Apr 16, 2026
…nstances (#13395)

This PR makes the `deriving Inhabited` handler for `structure`s be able
to inherit `Inhabited` instances from structure parents, using the same
mechanism as for class parents. This fixes a regression introduced by
#9815, which lost the ability to apply `Inhabited` instances for parents
represented as subobject fields. With this PR, now it works for all
parents in the hierarchy.

Implementation detail: adds `struct_inst_default%` for synthesizing a
structure default value using `Inhabited` instances for parents and
fields.

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

Labels

builds-manual CI has verified that the Lean Language Reference builds against this PR builds-mathlib CI has verified that Mathlib builds against this PR 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 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.

deriving Inhabited ignores field defaults

2 participants