Skip to content

[Merged by Bors] - feat: beta reduce in the T% elaborator#30879

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

[Merged by Bors] - feat: beta reduce in the T% elaborator#30879
grunweg wants to merge 14 commits intoleanprover-community:masterfrom
grunweg:diffgeo-custom-elaborators-betareduce

Conversation

@grunweg
Copy link
Copy Markdown
Contributor

@grunweg grunweg commented Oct 25, 2025

Whenever the T% elaborator succeeds, head beta reduce the resulting application.
This enables applying it with anonymous functions or inside big operators without issue.
(For instance, all changes to SmoothSection in #30357 are now possible without additional dsimp steps.)

In the process, also extract the workhorse component of the elaborator into an independent function
and improve its doc-string to mention the corresponding tracing option.


Open in Gitpod

@grunweg grunweg added t-meta Tactics, attributes or user commands t-differential-geometry Manifolds etc labels Oct 25, 2025
@github-actions
Copy link
Copy Markdown

github-actions bot commented Oct 25, 2025

PR summary 13a1b47278

Import changes for modified files

No significant changes to the import graph

Import changes for all files
Files Import difference

Declarations diff

+ totalSpaceMk

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 force-pushed the diffgeo-custom-elaborators-betareduce branch from 4154850 to 220e6a6 Compare October 25, 2025 09:53
@mathlib4-dependent-issues-bot mathlib4-dependent-issues-bot added the blocked-by-other-PR This PR depends on another PR (this label is automatically managed by a bot) label Oct 25, 2025
@grunweg
Copy link
Copy Markdown
Contributor Author

grunweg commented Oct 27, 2025

Thanks for the comments! I'm aware it's a bit hard to judge if these changes are useful.

This question ("should T% beta-reduce") came up in direct conversation with Floris (fpvandoorn). An instance where I might have hit this is in #30357 in SmoothSection.lean: if I want to use the T% elaborator in ContMDiffWithinAt.sum_section_of_locallyFinite, for example (and I do, because it's much easier to read), the lemma statement changes slightly.

Running dsimp at the start of the proof makes it work again: otherwise, the subsequent rw fails.

I tested this after this PR; I remember seeing these kind of error a lot of times when trying this without #30879. I can re-test (tomorrow perhaps) in SmoothSection on top of just #30413, to see if this PR actually makes a difference.

@grunweg
Copy link
Copy Markdown
Contributor Author

grunweg commented Oct 27, 2025

The general theme is: I have a function fun x \map TotalSpace.mk' F x (some slightly more complicated expression using x), and want to write this using T%. If I wrote T% (fun x \maps <complicated expression>), this sometimes yields a defeq, but not syntactically equal expression.
was e.g. sum finsum, sum or infinite sum; SmoothSection has examples.

@thorimur
Copy link
Copy Markdown
Contributor

thorimur commented Oct 27, 2025

Ah, okay, I think I see...does this mean we want to head-beta-reduce e.app x instead of just e?

@mathlib4-dependent-issues-bot mathlib4-dependent-issues-bot removed the blocked-by-other-PR This PR depends on another PR (this label is automatically managed by a bot) label Oct 28, 2025
@mathlib4-dependent-issues-bot
Copy link
Copy Markdown
Collaborator

@grunweg grunweg force-pushed the diffgeo-custom-elaborators-betareduce branch from 220e6a6 to e5035cc Compare October 28, 2025 10:23
@thorimur thorimur added the awaiting-author A reviewer has asked the author a question or requested changes. label Nov 7, 2025
@thorimur thorimur self-assigned this Nov 7, 2025
@grunweg
Copy link
Copy Markdown
Contributor Author

grunweg commented Nov 11, 2025

I just tested with the current state of this PR: all the need for dsimp is gone; it seems head-beta reducing e.app x was exactly the right call 🎉

This reverts commit e8de60f.
@grunweg grunweg changed the title feat: beta and eta reduce in the T% elaborator feat: beta reduce in the T% elaborator Nov 11, 2025
@grunweg
Copy link
Copy Markdown
Contributor Author

grunweg commented Nov 11, 2025

Thanks for the careful review, yet again. I've revamped the PR title and PR description - this is ready for another look.

@grunweg grunweg removed the awaiting-author A reviewer has asked the author a question or requested changes. label Nov 11, 2025
@thorimur
Copy link
Copy Markdown
Contributor

Awesome! Looks ready to me! :)

maintainer merge

@github-actions
Copy link
Copy Markdown

🚀 Pull request has been placed on the maintainer queue by thorimur.

@ghost ghost added the maintainer-merge A reviewer has approved the changed; awaiting maintainer approval. label Nov 14, 2025
@fpvandoorn
Copy link
Copy Markdown
Member

fpvandoorn commented Nov 21, 2025

This PR currently makes the change that T% σ x is not elaborated as an element of the total space anymore. This is undesirable. L66-68 of the test file should be:

/-- info: TotalSpace.mk' F x (σ x) : TotalSpace F V -/
#guard_msgs in
#check T% σ x

Changes needed:

  • T% should try to read 2 arguments ahead, if they exist (probably at precedence arg)
  • Then, after elaborating T% σ, we should apply it to x, and head-beta-reduce once more.

@fpvandoorn fpvandoorn added awaiting-author A reviewer has asked the author a question or requested changes. and removed maintainer-merge A reviewer has approved the changed; awaiting maintainer approval. labels Nov 21, 2025
@thorimur
Copy link
Copy Markdown
Contributor

thorimur commented Nov 21, 2025

This PR currently makes the change that T% σ x is not elaborated as an element of the total space anymore. This is undesirable. L66-68 of the test file should be:

/-- info: TotalSpace.mk' F x (σ x) : TotalSpace F V -/
#guard_msgs in
#check T% σ x

Changes needed:

* `T%` should try to read 2 arguments ahead, if they exist (probably at precedence `arg`)

* Then, after elaborating `T% σ`, we should apply it to `x`, and head-beta-reduce once more.

The only change this PR actually makes to this example is a parsing change: T% σ x is now parsed as T% (σ x) instead of (T% σ) x. On master, T% (σ x) becomes σ x : V x and (T% σ) x becomes (fun x ↦ TotalSpace.mk' F x (σ x)) x : TotalSpace F V. This PR doesn't actually affect beta reduction in either of these cases, only parsing for the unparenthesized T% σ x.

I don't think it's been the case (yet!) that T% elaborates points to points in the corresponding total space, but I think this would be neat! It would be a separate feature and thus separate PR, though. I believe we're better set up to handle that case if we handle things at the term level instead of parsing multiple args: then we can destructure into σ and x at the expression level.


There could be a separate point here about enforcing parentheses—maybe we expect T% σ x to parse as (T% σ) x always, and parsing a whole term is unexpected? We could still make points into elements of the total space in the way you'd like when parsing a single arg with T% (σ x), and maybe that would be clearer? Haven't thought about this. (This is a (somewhat) separate question, though.)


If you think this explanation is reasonable, I'd still recommend merging as-is, but I'll refrain from over-eagerly modifying labels here while still in conversation. 😁

@grunweg
Copy link
Copy Markdown
Contributor Author

grunweg commented Nov 25, 2025

I have reverted the precendence change from this PR: indeed, this should arguably be a separate PR. This PR is only about applying head-beta reduction. I have also updated the tests accordingly. I believe this addresses all your comments, Floris.

@grunweg grunweg removed the awaiting-author A reviewer has asked the author a question or requested changes. label Nov 25, 2025
@fpvandoorn
Copy link
Copy Markdown
Member

Ok, this change seems fine by itself, so let's merge it. The reason I asked for head-beta reducing the whole application, is because that is how I interpret this sentence of the PR description:

Whenever the T% elaborator succeeds, head beta reduce the resulting application.

But sure, let's do that in a later PR if desired.

bors merge

@ghost ghost added the ready-to-merge This PR has been sent to bors. label Nov 26, 2025
mathlib-bors bot pushed a commit that referenced this pull request Nov 26, 2025
Whenever the `T%` elaborator succeeds, head beta reduce the resulting application.
This enables applying it with anonymous functions or inside big operators without issue.
(For instance, all changes to `SmoothSection` in #30357 are now possible without additional `dsimp` steps.)

In the process, also extract the workhorse component of the elaborator into an independent function
and improve its doc-string to mention the corresponding tracing option.

Co-authored-by: thorimur <thomasmurrills@gmail.com>
Co-authored-by: thorimur <68410468+thorimur@users.noreply.github.com>
@mathlib-bors
Copy link
Copy Markdown
Contributor

mathlib-bors bot commented Nov 26, 2025

Pull request successfully merged into master.

Build succeeded:

@mathlib-bors mathlib-bors bot changed the title feat: beta reduce in the T% elaborator [Merged by Bors] - feat: beta reduce in the T% elaborator Nov 26, 2025
@mathlib-bors mathlib-bors bot closed this Nov 26, 2025
@grunweg grunweg deleted the diffgeo-custom-elaborators-betareduce branch November 26, 2025 13:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-merge This PR has been sent to bors. 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