-
Notifications
You must be signed in to change notification settings - Fork 450
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
Timeout with large arithmetic expression involving numerals #4861
Comments
Yes, a simple way to avoid timeout is to split the expression into multiple functions or definitions. |
This modification improves the performance of the example in issue #4861. It no longer times out but is still expensive. Here is the analysis of the performance issue: Given `(x : Int)`, to elaborate `x ^ 1`, a few default instances have to be tried. First, the homogeneous instance is tried and fails since `Int` does not implement `Pow Int`. Then, the `NatPow` instance is tried, and it also fails. The same process is performed for each term of the form `p ^ 1`. There are seveal of them at #4861. After all of these fail, the lower priority default instance for numerals is tried, and `x ^ 1` becomes `x ^ (1 : Nat)`. Then, `HPow Int Nat Int` can be applied, and the elaboration succeeds. However, this process has to be repeated for every single term of the form `p ^ 1`. The elaborator tries all homogeneous `HPow` and `NatPow` instances for all `p ^ 1` terms before trying the lower priority default instance `OfNat`. This commit ensures `Int` has a `NatPow` instance instead of `HPow Int Nat Int`. This change shortcuts the process, but it still first tries the homogeneous `HPow` instance, fails, and then tries `NatPow`. The elaboration can be made much more efficient by writing `p ^ (1 : Nat)`.
This modification improves the performance of the example in issue #4861. It no longer times out but is still expensive. Here is the analysis of the performance issue: Given `(x : Int)`, to elaborate `x ^ 1`, a few default instances have to be tried. First, the homogeneous instance is tried and fails since `Int` does not implement `Pow Int`. Then, the `NatPow` instance is tried, and it also fails. The same process is performed for each term of the form `p ^ 1`. There are seveal of them at #4861. After all of these fail, the lower priority default instance for numerals is tried, and `x ^ 1` becomes `x ^ (1 : Nat)`. Then, `HPow Int Nat Int` can be applied, and the elaboration succeeds. However, this process has to be repeated for every single term of the form `p ^ 1`. The elaborator tries all homogeneous `HPow` and `NatPow` instances for all `p ^ 1` terms before trying the lower priority default instance `OfNat`. This commit ensures `Int` has a `NatPow` instance instead of `HPow Int Nat Int`. This change shortcuts the process, but it still first tries the homogeneous `HPow` instance, fails, and then tries `NatPow`. The elaboration can be made much more efficient by writing `p ^ (1 : Nat)`.
For a performance measurement, the example spends about 3% of the elaboration time inside the binop elaborator, and then I am guessing the rest of the time it is in the synthetic metavariable synthesis loop. I thought about using variable (xs : Array Float)
#check Array.map (fun x => x ^ 2) xs One thing we could do to dramatically reduce the number of instance problems in this example is to re-use the elaborated functions when the types of both operands are known in |
Thanks for investigating this! Just a note that in the terms produced by (Not sure which of the fixes being investigated still work in this setting.) |
In Heather's We can't do this in general because we want to support HomogenousPow for Float applications. I think we could measure the performance impact of having binop fully elaborate exponents for mathlib, to see if it's worth thinking about redesigning how NatPow and HomogeneousPow work. |
Prerequisites
Please put an X between the brackets as you perform the following steps:
https://github.com/leanprover/lean4/issues
Avoid dependencies to Mathlib or Batteries.
https://live.lean-lang.org/#project=lean-nightly
(You can also use the settings there to switch to “Lean nightly”)
Description
The following code timeouts with the default
maxHeartbeats
setting. It sometimes fails atwhnf
and sometimes atisDefEq
, depending on the specific heartbeat setting used.The problem goes away when introducing a new parameter
w : Nat
and replacing all1
byw
.set_option diagnostics true
shows some interesting type class numbers:Context
The
linear_combination
tactic in mathlib is suffering from performance issues, and handling terms that look like the example above seems to be part of the reason why it is so slow.Steps to Reproduce
Expected behavior: Elaboration should be very fast
Actual behavior: Timeouts
Versions
4.11.0-nightly-2024-07-27 on live.lean-lang.org
Impact
Add 👍 to issues you consider important. If others are impacted by this issue, please ask them to add 👍 to it.
The text was updated successfully, but these errors were encountered: