Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: upstream @[simps] attribute #347

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
Open

feat: upstream @[simps] attribute #347

wants to merge 16 commits into from

Conversation

semorrison
Copy link
Collaborator

No description provided.

@semorrison semorrison added the awaiting-review This PR is ready for review; the author thinks it is ready to be merged. label Nov 4, 2023
Copy link
Contributor

@joehendrix joehendrix left a comment

Choose a reason for hiding this comment

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

I don't have bandwidth for a full review this week, but I added a couple notes.

Std/Lean/KVMap.lean Outdated Show resolved Hide resolved
Std/Lean/Linter.lean Outdated Show resolved Hide resolved
Std/Lean/Name.lean Outdated Show resolved Hide resolved
Std/Lean/Expr.lean Outdated Show resolved Hide resolved
@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot added the merge-conflict This PR has merge conflicts with the `main` branch which must be resolved by the author. label Nov 22, 2023
@semorrison semorrison removed the merge-conflict This PR has merge conflicts with the `main` branch which must be resolved by the author. label Nov 26, 2023
def updateName (nm : Name) (s : String) (isPrefix : Bool) : Name :=
nm.updateLast fun s' => if isPrefix then s ++ "_" ++ s' else s' ++ "_" ++ s

-- move
Copy link
Contributor

Choose a reason for hiding this comment

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

What should be moved?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've made updateName private, as it is idiosyncratic to simps, and moved the mkSimpContext material to Std/Lean/Meta/Simp.lean, and made a note there about the duplication occurring.

/-- Find the indices of the projections that need to be applied to elaborate `$e.$projName`.
Example: If `e : α ≃+ β` and ``projName = `invFun`` then this returns `[0, 1]`, because the first
projection of `MulEquiv` is `toEquiv` and the second projection of `Equiv` is `invFun`. -/
def findProjectionIndices (strName projName : Name) : MetaM (List Nat) := do
Copy link
Contributor

@joehendrix joehendrix Nov 27, 2023

Choose a reason for hiding this comment

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

Comment example is from Mathlib and uses e instead of strName.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I've clarified the comment to explain the use of e. It's still in terms of Mathlib. (This is a bigger overhaul: there are lots of Mathlib specific examples in this file --- Equiv isn't even defined in Std.)

let allProjs := pathToField ++ [fullProjName]
return allProjs.map (env.getProjectionFnInfo? · |>.get!.i)

/-- Auxiliary function of `getCompositeOfProjections`. -/
Copy link
Contributor

Choose a reason for hiding this comment

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

Could more detail be added either here to body of code?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@fpvandoorn, could I perhaps interest you in this? :-)

Copy link
Member

Choose a reason for hiding this comment

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

I added a few comments in the code.

Copy link
Member

Choose a reason for hiding this comment

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

Oh, I don't have push access to this repo. Can I get access, or shall I make a PR into this PR?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@fpvandoorn, for now, would you mind a PR to the PR? Really I should have made this PR from a fork so I could have given you access myself.

Copy link
Member

Choose a reason for hiding this comment

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


This is very similar to `expr.substs`, but this also reduces head let-expressions. -/
partial def _root_.Lean.Expr.instantiateLambdasOrApps (es : Array Expr) (e : Expr) : Expr :=
e.betaRev es.reverse true -- check if this is what I want
Copy link
Contributor

Choose a reason for hiding this comment

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

Comment seems fishy and this should either be moved to Std/Lean/Expr.lean or renamed.

Copy link
Member

Choose a reason for hiding this comment

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

I never tested whether this function satisfies the docstring exactly, and it doesn't do the second half of Also reduces head let-expressions in `e`, including those after instantiating all lambdas.

In leanprover-community/mathlib4@master...simps_upstream_tests I tried to see if doing that breaks anything in Mathlib. I expect not, given the low number of let expressions in definitions.

We can also keep the current functionality. It would be nice if Lean.Expr.beta supports the optional flags of Lean.Expr.betaRev, then this function doesn't need to exist.

""}even if the definition is a constructor application. For example, if you give a {
""}`MulEquiv` by giving the corresponding `Equiv` and the proof that it respects {
""}multiplication, then you need to mark it as `@[simps!]`, since the attribute needs to {
""}unfold the corresponding `Equiv` to get to the `toFun` field."
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd love to see this function and the one above broken up into smaller entities with comments about what they do.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, this code is not pretty, since I wrote it in Lean 3, and then ported it to Lean 4 keeping the Lean 3 structure. I think the proper way to make this code better is to make use of monad extensions with a state and context, instead of these functions that take ~10 arguments. That makes it also much easier to split off small sub-functions.
I don't have time for that refactor at the moment.

It is a bit tricky to break up this function in the current state, since there are recursive calls in multiple places.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Thanks.

While it would be really nice to get @[simps] into Std, it is not essential: users can always write the boilerplate by hand!

I propose that we shop this project around a bit, and see if anyone on zulip might be interested in taking on a refactor?

Copy link
Member

Choose a reason for hiding this comment

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

Sounds good to me

Std/Lean/Meta/Simp.lean Outdated Show resolved Hide resolved
@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot added the merge-conflict This PR has merge conflicts with the `main` branch which must be resolved by the author. label Jan 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-review This PR is ready for review; the author thinks it is ready to be merged. merge-conflict This PR has merge conflicts with the `main` branch which must be resolved by the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants