Skip to content

[Merged by Bors] - feat: custom elaborators for differential geometry#27021

Closed
grunweg wants to merge 108 commits intoleanprover-community:masterfrom
grunweg:diffgeo-custom-elaborators
Closed

[Merged by Bors] - feat: custom elaborators for differential geometry#27021
grunweg wants to merge 108 commits intoleanprover-community:masterfrom
grunweg:diffgeo-custom-elaborators

Conversation

@grunweg
Copy link
Copy Markdown
Contributor

@grunweg grunweg commented Jul 12, 2025

Add two sets of custom elaborators for differential geometry, to

  • infer the model with corners in MDifferentiable{WithinAt,At,On} or ContMDiff{WithinAt,At,On} expressions from the local context, and provide shorter notation in that setting
  • do the same for mfderiv: mfderiv% f is shorthand for mfderiv I J f; mfderiv[s] f x is short for mfderivWithin I J f s
  • convert a fibre bundle section from a dependent section to a function into the total space, making working with differentiability of sections of fibre bundles less cumbersome

This was discussed on zulip, to overall broad consensus.

This code has substantial tests (which have caught bugs in the past). It has also been tested in the branch in #26221 (uncovering minor issues, but not substantial ones --- the worst of which have been fixed).
Many thanks go to fpvandoorn (for lots of in-person comments on improving the meta code), and to thorimur for rewriting the entire implementation from ground up to be much more robust and maintainable.

A number of tweaks and extensions can be made in future PRs: this set of elaborators is useful already.
Once it is deemed sufficiently robust, differential geometry files can be converted to use it.

From the path towards geodesics and the Levi-Civita connection.

Co-authored-by: Patrick Massot patrickmassot@free.fr


Open in Gitpod

@github-actions github-actions bot added the t-differential-geometry Manifolds etc label Jul 12, 2025
@github-actions
Copy link
Copy Markdown

github-actions bot commented Jul 12, 2025

PR summary b4264fbd75

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference
Mathlib.Geometry.Manifold.Notation (new file) 2076

Declarations diff

+ findModel
+ findModels

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

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

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


No changes to technical debt.

You can run this locally as

./scripts/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).

@grunweg grunweg added the t-meta Tactics, attributes or user commands label Jul 12, 2025
@grunweg grunweg force-pushed the diffgeo-custom-elaborators branch from 6f64439 to 7c4c0b0 Compare July 12, 2025 08:28
@sgouezel
Copy link
Copy Markdown
Contributor

Thanks for this! I agree that this is extremely useful, and should be merged quickly if that's possible. However, I think that the scoping to the Manifold namespace is really important, and should be included in the current PR.

@grunweg
Copy link
Copy Markdown
Contributor Author

grunweg commented Jul 12, 2025

Sure, I'm happy to do this - can you point me at how?

@grunweg
Copy link
Copy Markdown
Contributor Author

grunweg commented Jul 12, 2025

Trying the obvious things elab[Manifold]:max "CMDiff" nt:term:arg t:term:arg : term => do ... and elab:max[Manifold] "CMDiff" nt:term:arg t:term:arg : term => do did not work.

(edit: But Yael suggested a fix on zulip, thanks!)

@sgouezel sgouezel added the awaiting-author A reviewer has asked the author a question or requested changes. label Sep 29, 2025
Copy link
Copy Markdown
Contributor Author

@grunweg grunweg left a comment

Choose a reason for hiding this comment

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

Thanks for the comments; let me look into them now!

@grunweg grunweg force-pushed the diffgeo-custom-elaborators branch 2 times, most recently from 1ee60c5 to 2dc7a2c Compare September 30, 2025 09:40
@grunweg grunweg removed the awaiting-author A reviewer has asked the author a question or requested changes. label Sep 30, 2025
@grunweg grunweg force-pushed the diffgeo-custom-elaborators branch from 2dc7a2c to 6a84a48 Compare September 30, 2025 09:46
grunweg added a commit to grunweg/mathlib4 that referenced this pull request Sep 30, 2025
This depends on the custom elaborators added in leanprover-community#27021;
for some reason, using these fails. To be investigated!
Add two sets of custom elaborators for differential geometry, to
- infer the model with corners in `MDifferentiable{WithinAt,At,On}` or `ContMDiff{WithinAt,At,On}`
  expressions from the local context, and provide shorter notation in that setting
- convert a fibre bundle section from a dependent section to a function into the total space,
  making working with differentiability of sections of fibre bundles less cumbersome

This was [discussed on zulip](...), to overall broad consensus.

This code has substantial tests and has been tried in the branch in leanprover-community#26221 (uncovering minor issues,
but not substantial ones). The implementation itself is of prototype quality and will be cleaned up
in the future. Once it is more robust, more differential geometry files can be converted to use it.

-----------

I agree that merging not fully polished code is not ideal, but propose doing so for two reasons:
- the work in leanprover-community#26221 used these elaborators extensively (to test them).
  Reverting those changes, merging the code, only to re-apply them is not a good use of time.
  Merging this PR now enables merging the work in leanprover-community#26221 soon.
- the feature is clearly useful and not going away: in the files that use it, it works well already.
  Holding off on further adopting, but merging it now seems like a good compromise.
@grunweg
Copy link
Copy Markdown
Contributor Author

grunweg commented Oct 7, 2025

My current opinion is that

  • the current test should definitely stay, as a regression test (I've updated its comment to clarify this)
  • I don't expect the TopologicalSpace instance to be found;
  • there should be a test for the correct spelling of this.

I've asked on zulip about this last point.

@grunweg
Copy link
Copy Markdown
Contributor Author

grunweg commented Oct 7, 2025

Added a regression test about the wrong arg precendence for parsing s.

@grunweg grunweg force-pushed the diffgeo-custom-elaborators branch from 3b2f1f2 to faf1b24 Compare October 7, 2025 14:48
@grunweg
Copy link
Copy Markdown
Contributor Author

grunweg commented Oct 7, 2025

With a little reminder on zulip, I found the right spelling for smoothness and added a test covering it.

@grunweg
Copy link
Copy Markdown
Contributor Author

grunweg commented Oct 7, 2025

Hmm, relatedly, I notice that TangentBundle is an abbrev for Bundle.TotalSpace _ (TangentSpace _). Should findModel see TangentBundle as a Bundle.TotalSpace, or handle it as a separate case (or not handle it)? If the former, then we should use match_expr ← whnfR e with in fromTotalSpace to unwrap TangentBundle. But if it has separate instances, it might make sense to handle it as a separate case if at all (presumably in a subsequent PR!).

This is a question of the algorithm's spec, so I'll defer to those with greater library knowledge in this area. :)

I think that

  • the elaborators should definitely support TangentBundle (either now, or in a follow-up)
  • TangentBundle has separate instances, but not about manifolds... so I wondered if just changing the match_expr suffices. Trying it out, the answer seems to be no. So, it might be best to add a new case to findModel, and perhaps do so in a follow-up.

@grunweg
Copy link
Copy Markdown
Contributor Author

grunweg commented Oct 7, 2025

In other words: from my side, this PR is ready to get merged. There are a number of folliow-up changes I'd like to make, but these can be subsequent PRs.

@thorimur
Copy link
Copy Markdown
Contributor

thorimur commented Oct 7, 2025

LGTM (after updating Mathlib.Lean.Elab.Term + noshake)! 🎉

Thanks so much for your work on this, and to the others who contributed—and especially for your responsiveness during the review process! I'm very excited to see these continue to be iterated on and—hopefully at some not-too-distant date!—start to be used in Mathlib. :)

@grunweg
Copy link
Copy Markdown
Contributor Author

grunweg commented Oct 8, 2025

CI passes on this PR. I've gone over the diff again, and also resolved all stale review comments. I'll keep Thomas' more substantial meta comments open (for future me to re-read them); they are all addressed. All in all, this seems ready for merging (but I'm of course biased).

@thorimur Let me just repeat again that I'm really grateful for your extremely detailed review. I learned a lot (and will probably learn more as I return to your comments to re-process them). It was a pleasure, until next time/ one of the follow-up changes :-)
Would you like to add yourself to the authors of this file? I have no idea what mathlib's etiquette says, but you have certainly rewritten the meta code significantly.

@thorimur
Copy link
Copy Markdown
Contributor

thorimur commented Oct 8, 2025

I'm so glad my comments were useful to you! :) I'm always happy to talk about metaprogramming, so if you return to these comments later, feel free to ask me further questions about them (e.g. on Zulip)! :)

Would you like to add yourself to the authors of this file? I have no idea what mathlib's etiquette says, but you have certainly rewritten the meta code significantly.

Hmm, I'm actually not sure if I should be on the author's list either!

I once heard someone say that the authors list is basically the answer to "who do I ping if I need to address something in this file", and from that perspective, I'd be happy to take on the responsibility of being among those pinged.

On the other hand, I'm not sure if it's a great idea to set a precedent of receiving authorship credit as a reviewer.

I'm perfectly okay with either outcome, but I'd rather defer to an opinion from a maintainer on what should happen here than ask to be there myself.

(If this turns out to be complicated, though, perhaps let's leave me off and not hold up the PR any further—I'd like to stay involved with these elaborators, so maybe I'll contribute to this file directly in the future and render the question moot. :) )

@sgouezel
Copy link
Copy Markdown
Contributor

sgouezel commented Oct 8, 2025

bors d+
I think it definitely makes sense to add you as a second author to the file (not the least because then you will feel responsible for it, and then we can ping you again :-)

@mathlib-bors
Copy link
Copy Markdown
Contributor

mathlib-bors bot commented Oct 8, 2025

✌️ grunweg can now approve this pull request. To approve and merge a pull request, simply reply with bors r+. More detailed instructions are available here.

@ghost ghost added the delegated This pull request has been delegated to the PR author (or occasionally another non-maintainer). label Oct 8, 2025
@grunweg
Copy link
Copy Markdown
Contributor Author

grunweg commented Oct 8, 2025

Thanks for the review! I've added Thomas as an author; let's merge this!
bors merge

mathlib-bors bot pushed a commit that referenced this pull request Oct 8, 2025
Add two sets of custom elaborators for differential geometry, to
- infer the model with corners in `MDifferentiable{WithinAt,At,On}` or `ContMDiff{WithinAt,At,On}` expressions from the local context, and provide shorter notation in that setting
- do the same for mfderiv: `mfderiv% f` is shorthand for `mfderiv I J f`; `mfderiv[s] f x` is short for `mfderivWithin I J f s`
- convert a fibre bundle section from a dependent section to a function into the total space, making working with differentiability of sections of fibre bundles less cumbersome

This was [discussed on zulip](https://leanprover.zulipchat.com/#narrow/channel/287929-mathlib4/topic/Differential.20geometry.20elaborators.20experiment/with/528035295), to overall broad consensus.

This code has substantial tests (which have caught bugs in the past). It has also been tested in the branch in #26221 (uncovering minor issues, but not substantial ones

Co-authored-by: thorimur <68410468+thorimur@users.noreply.github.com>
Co-authored-by: thorimur <thomasmurrills@gmail.com>
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
@mathlib-bors
Copy link
Copy Markdown
Contributor

mathlib-bors bot commented Oct 8, 2025

Pull request successfully merged into master.

Build succeeded:

@mathlib-bors mathlib-bors bot changed the title feat: custom elaborators for differential geometry [Merged by Bors] - feat: custom elaborators for differential geometry Oct 8, 2025
@mathlib-bors mathlib-bors bot closed this Oct 8, 2025
@grunweg grunweg deleted the diffgeo-custom-elaborators branch October 8, 2025 16:27
grunweg added a commit to grunweg/mathlib4 that referenced this pull request Oct 8, 2025
This depends on the custom elaborators added in leanprover-community#27021;
for some reason, using these fails. To be investigated!
Jlh18 pushed a commit to Jlh18/mathlib4 that referenced this pull request Oct 14, 2025
…ity#27021)

Add two sets of custom elaborators for differential geometry, to
- infer the model with corners in `MDifferentiable{WithinAt,At,On}` or `ContMDiff{WithinAt,At,On}` expressions from the local context, and provide shorter notation in that setting
- do the same for mfderiv: `mfderiv% f` is shorthand for `mfderiv I J f`; `mfderiv[s] f x` is short for `mfderivWithin I J f s`
- convert a fibre bundle section from a dependent section to a function into the total space, making working with differentiability of sections of fibre bundles less cumbersome

This was [discussed on zulip](https://leanprover.zulipchat.com/#narrow/channel/287929-mathlib4/topic/Differential.20geometry.20elaborators.20experiment/with/528035295), to overall broad consensus.

This code has substantial tests (which have caught bugs in the past). It has also been tested in the branch in leanprover-community#26221 (uncovering minor issues, but not substantial ones

Co-authored-by: thorimur <68410468+thorimur@users.noreply.github.com>
Co-authored-by: thorimur <thomasmurrills@gmail.com>
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
BeibeiX0 pushed a commit to BeibeiX0/mathlib4 that referenced this pull request Nov 7, 2025
…ity#27021)

Add two sets of custom elaborators for differential geometry, to
- infer the model with corners in `MDifferentiable{WithinAt,At,On}` or `ContMDiff{WithinAt,At,On}` expressions from the local context, and provide shorter notation in that setting
- do the same for mfderiv: `mfderiv% f` is shorthand for `mfderiv I J f`; `mfderiv[s] f x` is short for `mfderivWithin I J f s`
- convert a fibre bundle section from a dependent section to a function into the total space, making working with differentiability of sections of fibre bundles less cumbersome

This was [discussed on zulip](https://leanprover.zulipchat.com/#narrow/channel/287929-mathlib4/topic/Differential.20geometry.20elaborators.20experiment/with/528035295), to overall broad consensus.

This code has substantial tests (which have caught bugs in the past). It has also been tested in the branch in leanprover-community#26221 (uncovering minor issues, but not substantial ones

Co-authored-by: thorimur <68410468+thorimur@users.noreply.github.com>
Co-authored-by: thorimur <thomasmurrills@gmail.com>
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

delegated This pull request has been delegated to the PR author (or occasionally another non-maintainer). t-differential-geometry Manifolds etc t-meta Tactics, attributes or user commands

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants