Skip to content

feat: structure instance notation elaboration improvements#7717

Merged
kmill merged 20 commits intoleanprover:masterfrom
kmill:structinst_flat
Mar 30, 2025
Merged

feat: structure instance notation elaboration improvements#7717
kmill merged 20 commits intoleanprover:masterfrom
kmill:structinst_flat

Conversation

@kmill
Copy link
Copy Markdown
Collaborator

@kmill kmill commented Mar 29, 2025

This PR changes how {...}/where notation ("structure instance notation") elaborates. The notation now tries to simulate a flat representation as much as possible, without exposing the details of subobjects. Features:

  • When fields are elaborated, their expected types now have a couple reductions applied. For all projections and constructors associated to the structure and its parents, projections of constructors are reduced and constructors of projections are eta reduced, and also implementation detail local variables are zeta reduced in propositions (so tactic proofs should never see them anymore). Furthermore, field values are beta reduced automatically in successive field types. The example in mathlib4#12129 now shows a goal of 0 = 0 rather than { toFun := fun x => x }.toFun 0 = 0.
  • All parents can now be used as field names, not just the subobject parents. These are like additional sources but with three constraints: every field of the value must be used, the fields must not overlap with other provided fields, and every field of the specified parent must be provided for. Similar to sources, the values are hoisted to lets if they are not already variables, to avoid multiple evaluation. They are implementation detail local variables, so they get unfolded for successive fields.
  • All class parents are now used to fill in missing fields, not just the subobject parents. Closes Field missing on diamond-shaped instances #6046. Rules: (1) only those parents whose fields are a subset of the remaining fields are considered, (2) parents are considered only before any fields are elaborated, and (3) only those parents whose type can be computed are considered (this can happen if a parent depends on another parent, which is possible since feat: change structure command to elaborate fields as if structures are flat #7302).
  • Default values and autoparams now respect the resolution order completely: each field has at most one default value definition that can provide for it. The algorithm that tries to unstick default values by walking up the subobject hierarchy has been removed. If there are applications of default value priorities, we might consider it in a future release.
  • The resulting constructors are now fully packed. This is implemented by doing structure eta reduction of the elaborated expressions.
  • "Magic field definitions" (as reported on Zulip) have been eliminated. This was where fields were being solved for by unification, tricking the default value system into thinking they had actually been provided. Now the default value system keeps track of which fields it has actually solved for, and which fields the user did not provide. Explicit structure fields (the default kind) without any explicit value definition will result in an error. If it was solved for by unification, the error message will include the inferred value, like "field 'f' must be explicitly provided, its synthesized value is v"
  • When the notation is used in patterns, it now no longer inserts fields using class parents, and it no longer applies autoparams or default values. The motivation is that one expects patterns to match only the given fields. This is still imperfect, since fields might be solved for indirectly.
  • Elaboration now attempts error recovery. Extraneous fields log errors and are ignored, missing fields are filled with sorry.

This is a breaking change, but generally the mitigation is to remove dsimp only from the beginnings of proofs. Sometimes "magic fields" need to be provided — four possible mitigations are (1) to provide the field, (2) to provide _ for the value of the field, (3) to add .. to the structure instance notation, (4) or decide to modify the structure command to make the field implicit. Lastly, sometimes parent instances don't apply when they should. This could be because some of the provided fields overlap with the class, or it could be that the parent depends on some of the fields for synthesis — and as parents are only considered before any fields are elaborated, such parents might not be possible to use — we will look into refining this further.

There is also a change to elaboration: now the afterTypeChecking attributes are run with all structure data set up (e.g. the list of parents, along with all parent projections in the environment). This is necessary since attributes like @[ext] use structure instance notation, and the notation needs all this data to be set up now.

@kmill kmill added the changelog-language Language features and metaprograms label Mar 29, 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 Mar 29, 2025
ghost pushed a commit to leanprover-community/batteries that referenced this pull request Mar 29, 2025
ghost pushed a commit to leanprover-community/mathlib4 that referenced this pull request Mar 29, 2025
@ghost
Copy link
Copy Markdown

ghost commented Mar 29, 2025

Mathlib CI status (docs):

@ghost ghost added the breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan label Mar 29, 2025
ghost pushed a commit to leanprover-community/batteries that referenced this pull request Mar 29, 2025
ghost pushed a commit to leanprover-community/mathlib4 that referenced this pull request Mar 29, 2025
ghost pushed a commit to leanprover-community/batteries that referenced this pull request Mar 29, 2025
ghost pushed a commit to leanprover-community/mathlib4 that referenced this pull request Mar 29, 2025
ghost pushed a commit to leanprover-community/batteries that referenced this pull request Mar 30, 2025
ghost pushed a commit to leanprover-community/mathlib4 that referenced this pull request Mar 30, 2025
@kmill kmill added this pull request to the merge queue Mar 30, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 30, 2025
@kmill kmill enabled auto-merge March 30, 2025 07:07
@kmill kmill added this pull request to the merge queue Mar 30, 2025
ghost pushed a commit to leanprover-community/batteries that referenced this pull request Mar 30, 2025
ghost pushed a commit to leanprover-community/mathlib4 that referenced this pull request Mar 30, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Mar 30, 2025
@kmill kmill requested review from kim-em and tydeu as code owners March 30, 2025 17:25
@kmill kmill enabled auto-merge March 30, 2025 17:26
@kmill kmill added this pull request to the merge queue Mar 30, 2025
ghost pushed a commit to leanprover-community/batteries that referenced this pull request Mar 30, 2025
ghost pushed a commit to leanprover-community/mathlib4 that referenced this pull request Mar 30, 2025
Merged via the queue into leanprover:master with commit 3f98f6b Mar 30, 2025
17 checks passed
@kmill
Copy link
Copy Markdown
Collaborator Author

kmill commented Mar 31, 2025

benchmark PR: leanprover-community/mathlib4#23445

github-merge-queue bot pushed a commit that referenced this pull request Mar 31, 2025
This PR fixes an oversight in #7717, and now fields get a FieldInfo node
with the correct projection function.

Note that for copied fields "go to definition" still does not go
anywhere, since copied projection function has no declaration range. We
probably should make such fields instead go to the origin projection
function.
kim-em added a commit to leanprover-community/mathlib4 that referenced this pull request Apr 3, 2025
* chore: bump to nightly-2025-03-22

* Merge master into nightly-testing

* chore: adaptations for nightly-2025-03-22

* fix

* Merge master into nightly-testing

* protected

* chore: bump to nightly-2025-03-24

* update batteries and aesop

* fixes for count_cons_of_ne

* fix fix to Sym.inhabitedSym' (need `default`, not the `a` that happens to be in context)

* bump heartbeats in MathlibTest/observe

* fix, deprecated

* fix merge

* Update lean-toolchain for testing leanprover/lean4#7672

* Trigger CI for leanprover/lean4#7672

* update batteries

* fixes for leanprover/lean4#7672

* Trigger CI for leanprover/lean4#7672

* Trigger CI for leanprover/lean4#7672

* Trigger CI for leanprover/lean4#7672

* Trigger CI for leanprover/lean4#7672

* Trigger CI for leanprover/lean4#7672

* update batteries

* fixes

* chore: bump to nightly-2025-03-25

* Trigger CI for leanprover/lean4#7672

* Trigger CI for leanprover/lean4#7672

* update batteries

* Trigger CI for leanprover/lean4#7672

* one fix

* fixes

* maxheartbeats

* fixes for leanprover/lean4#7672

* fixes for leanprover/lean4#7672

* fixes for leanprover/lean4#7672

* fixes for leanprover/lean4#7672

* cleanups

* .

* chore: bump to nightly-2025-03-26

* update aesop

* Update lean-toolchain for testing leanprover/lean4#7690

* Trigger CI for leanprover/lean4#7690

* Trigger CI for leanprover/lean4#7690

* maxHeartbeats

* max heartbeats

* invalidate cache

* another heartbeats

* bump batteries

* deprecation

* Update lean-toolchain for testing leanprover/lean4#7692

* Delete

* chore: bump to nightly-2025-03-27

* update batteries

* bump batteries

* many more maxHeartbeats

* chore: bump leantar v0.1.15

* invalidate cache

* cache flush, take 2

* feat(Cache): root hash generation counter

* 1-line fix

* chore: bump to nightly-2025-03-28

* update deps

* remove upstreamed

* remove all adaptation notes, hooray

* merge lean-pr-testing-7692

* chore: bump to nightly-2025-03-29

* Update lean-toolchain for testing leanprover/lean4#7717

* fix

* Trigger CI for leanprover/lean4#7717

* Trigger CI for leanprover/lean4#7717

* fixes

* fixes

* Trigger CI for leanprover/lean4#7717

* fixes

* fixes

* fixes

* fixes

* got through all of mathlib

* missing space

* fix tests

* Trigger CI for leanprover/lean4#7717

* Trigger CI for leanprover/lean4#7717

* Merge master into nightly-testing

* chore: bump to nightly-2025-03-30

* Update lean-toolchain for testing leanprover/lean4#7710

* Trigger CI for leanprover/lean4#7710

* Trigger CI for leanprover/lean4#7710

* Merge master into nightly-testing

* Trigger CI for leanprover/lean4#7717

* chore: remove fragile tests

* chore: remove fragile tests

* Trigger CI for leanprover/lean4#7710

* Update lean-toolchain for testing leanprover/lean4#7736

* how did this get dropped?

* Trigger CI for leanprover/lean4#7710

* Trigger CI for leanprover/lean4#7736

* deprecations in Cache

* fix

* chore: bump to nightly-2025-03-31

* update batteries

* merge fixes

* fix

* fixes

* Update lean-toolchain for testing leanprover/lean4#7756

* fix

* Update lean-toolchain for testing leanprover/lean4#7764

* eliminate some 7717 adaptation notes

* Start fixing, upstream changes needed

* Trigger CI for leanprover/lean4#7756

* Fix

* Fix

* Fix

* Fix

* Trigger CI for leanprover/lean4#7756

* Trigger CI for leanprover/lean4#7756

* chore: bump to nightly-2025-04-01

* lake update

* Trigger CI for leanprover/lean4#7756

* Fix

* Fix

* Update batteries

* Fix

* Fix

* Trigger CI for leanprover/lean4#7756

* lake update

* Fix

* Fix

* fix for auxlemma detection

* remove mention of aux proof in Mathlib.Control.Fix

* fix whatsnew

* better fix

* argh

* unfold aux lemmas before transforming aux decls

* fixes

* optimization: abstract nested proofs in toadditive

* docstring

* revert change

* fix merge

* deprecations in shake

* fixes

* fixes

* chore: bump to nightly-2025-04-02

* lake update

* fixes

* fix test

* fix

* fix merge

---------

Co-authored-by: leanprover-community-mathlib4-bot <leanprover-community-mathlib4-bot@users.noreply.github.com>
Co-authored-by: Kyle Miller <kmill31415@gmail.com>
Co-authored-by: Johan Commelin <johan@commelin.net>
Co-authored-by: Markus Himmel <markus@lean-fro.org>
Co-authored-by: Mario Carneiro <di.gama@gmail.com>
Co-authored-by: Sebastian Ullrich <sebasti@nullri.ch>
kmill added a commit to kmill/lean4 that referenced this pull request Apr 4, 2025
github-merge-queue bot pushed a commit that referenced this pull request Apr 4, 2025
…lts (#7815)

This PR adds tests and closes #6769. This was likely fixed as of #7717.
mathlib-bors bot pushed a commit to leanprover-community/mathlib4 that referenced this pull request May 6, 2025
Thanks to leanprover/lean4#7717, some `show .. by` in structures have become unnecessary.
riccardobrasca pushed a commit to leanprover-community/mathlib4 that referenced this pull request May 7, 2025
Thanks to leanprover/lean4#7717, some `show .. by` in structures have become unnecessary.
tannerduve pushed a commit to leanprover-community/mathlib4 that referenced this pull request May 13, 2025
Thanks to leanprover/lean4#7717, some `show .. by` in structures have become unnecessary.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

builds-mathlib CI has verified that Mathlib builds against this PR changelog-language Language features and metaprograms 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.

Field missing on diamond-shaped instances

1 participant