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

fix: replace unary Nat.succ simp rules with simprocs #3808

Merged
merged 5 commits into from
Apr 4, 2024

Conversation

joehendrix
Copy link
Contributor

@joehendrix joehendrix commented Mar 30, 2024

This removes simp attributes from Nat.succ.injEq and Nat.succ_sub_succ_eq_sub to replace them with simprocs. This is because any reductions involving Nat.succ has a high risk of leading proof performance problems when dealing with even moderately large numbers.

Here are a couple examples that will both report a maximum recursion depth error currently. These examples are fixed by this PR.

example : (123456: Nat) = 12345667 := by
  simp

example (x : Nat) (p : x = 0) : 1000 - (x + 1000) = 0 := by
  simp

@joehendrix joehendrix added full-ci awaiting-mathlib We should not merge this until we have a successful Mathlib build labels Mar 30, 2024
@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc March 30, 2024 01:15 Inactive
@digama0
Copy link
Collaborator

digama0 commented Mar 30, 2024

This seems like it would be better addressed by finding a way for that expression not to count as having the form succ x - succ y, either in this particular simp lemma or in the discrimination tree in general. Disabling it may be the best short term option, but this seems like a regression.

@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 30, 2024
@joehendrix
Copy link
Contributor Author

I'll take a stab at a simproc that covers this case, but I don't think this should be a reason to touch discriminator tree patterns (e.g., the logic in isNatOffset). I expect that would have big downstream effects.

@joehendrix joehendrix requested a review from leodemoura as a code owner April 2, 2024 18:42
@joehendrix joehendrix changed the title fix: remove simp on succ_sub_succ_eq_sub fix: replace unary Nat.succ simp rules with simprocs Apr 2, 2024
@joehendrix joehendrix added the WIP This is work in progress, and only a PR for the sake of CI or sharing. No review yet. label Apr 2, 2024
@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc April 2, 2024 20:32 Inactive
@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc April 2, 2024 21:04 Inactive
@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc April 2, 2024 22:12 Inactive
This introduces a simproc for equality and another
for subtraction so that we can remove simp attributes
on Nat.succ_sub_succ_eq_sub and Nat.succ.injEq.

Both of these have the potential to trigger stack overflows
or performance problem when applied to terms with large Nat
constants.  The simproc operations bypass this through use
of lemmas.
@joehendrix joehendrix removed the awaiting-mathlib We should not merge this until we have a successful Mathlib build label Apr 3, 2024
@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc April 3, 2024 21:02 Inactive
@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc April 3, 2024 21:51 Inactive
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Apr 3, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Apr 3, 2024
@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot added the breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan label Apr 3, 2024
@leanprover-community-mathlib4-bot
Copy link
Collaborator

leanprover-community-mathlib4-bot commented Apr 3, 2024

Mathlib CI status (docs):

@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc April 3, 2024 22:55 Inactive
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Apr 3, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Apr 3, 2024
@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc April 3, 2024 23:54 Inactive
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Apr 4, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Apr 4, 2024
@joehendrix joehendrix removed the WIP This is work in progress, and only a PR for the sake of CI or sharing. No review yet. label Apr 4, 2024
@joehendrix joehendrix added this pull request to the merge queue Apr 4, 2024
Merged via the queue into master with commit f31c395 Apr 4, 2024
21 checks passed
@eric-wieser
Copy link
Contributor

@semorrison, this is misleading; the CI job "built", but the "build" step of the CI job failed.

if e.isAppOfArity ``HAdd.hAdd 6 then
let inst := e.appFn!.appFn!.appArg!
unless inst.isAppOfArity ``instHAdd 2 do return none
unless inst.appArg!.isAppOfArity ``instAddNat 0 do return none
Copy link
Contributor

@eric-wieser eric-wieser Apr 5, 2024

Choose a reason for hiding this comment

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

This is going to fail if the instance is actually AddMonoid.toAdd from mathlib, which seems very likely to occur; which is to say, I think this needs to do (reducible) defeq matching, not syntactic matching.

Copy link
Contributor

Choose a reason for hiding this comment

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

A small repro:

import Mathlib.Data.Nat.Basic

example : 1 + 2 = 3 := by
  rewrite [add_comm]
  -- goal is no longer about `instAddNat`
  sorry

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you make an example showing this? Using import Mathlib is fine.

Copy link
Contributor

Choose a reason for hiding this comment

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

@joehendrix: I just went back in time and posted one :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Note that this was fixed in mathlib in leanprover-community/mathlib3#18129

Choose a reason for hiding this comment

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

For reference, the pattern of matching operators by name and typeclasses by defeq is called "keyed matching". (I learned this name from Reflexive tactics for algebra, revisited by Kazuhiko Sakaguchi, Section 3.2.)

@kim-em
Copy link
Collaborator

kim-em commented Apr 17, 2024

@semorrison, this is misleading; the CI job "built", but the "build" step of the CI job failed.

Indeed, now that the linter runs even when the build fails, we need to change the logic.

mathlib-bors bot pushed a commit to leanprover-community/mathlib4 that referenced this pull request Apr 18, 2024
mathlib-bors bot pushed a commit to leanprover-community/mathlib4 that referenced this pull request Apr 18, 2024
mathlib-bors bot pushed a commit to leanprover-community/mathlib4 that referenced this pull request Apr 18, 2024
uniwuni pushed a commit to leanprover-community/mathlib4 that referenced this pull request Apr 19, 2024
callesonne pushed a commit to leanprover-community/mathlib4 that referenced this pull request Apr 22, 2024
Jun2M pushed a commit to leanprover-community/mathlib4 that referenced this pull request Apr 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaks-mathlib This is not necessarily a blocker for merging: but there needs to be a plan 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.

6 participants