-
Notifications
You must be signed in to change notification settings - Fork 463
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: safe exponentiation #4637
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/- | ||
Copyright (c) 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
Released under Apache 2.0 license as described in the file LICENSE. | ||
Authors: Leonardo de Moura | ||
-/ | ||
prelude | ||
import Lean.CoreM | ||
|
||
namespace Lean | ||
|
||
register_builtin_option exponentiation.threshold : Nat := { | ||
defValue := 256 | ||
descr := "maximum value for \ | ||
which exponentiation operations are safe to evaluate. When an exponent \ | ||
is a value greater than this threshold, the exponentiation will not be evaluated, \ | ||
and a warning will be logged. This helps to prevent the system from becoming \ | ||
unresponsive due to excessively large computations." | ||
} | ||
|
||
/-- | ||
Returns `true` if `n` is `≤ exponentiation.threshold`. Otherwise, | ||
reports a warning and returns `false`. | ||
This method ensures there is at most one warning message of this kind in the message log. | ||
-/ | ||
def checkExponent (n : Nat) : CoreM Bool := do | ||
let threshold := exponentiation.threshold.get (← getOptions) | ||
if n > threshold then | ||
if (← reportMessageKind `unsafe.exponentiation) then | ||
logWarning s!"exponent {n} exceeds the threshold {threshold}, exponentiation operation was not evaluated, use `set_option {exponentiation.threshold.name} <num>` to set a new threshold" | ||
return false | ||
else | ||
return true | ||
|
||
end Lean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/-- | ||
warning: exponent 10000000 exceeds the threshold 256, exponentiation operation was not evaluated, use `set_option exponentiation.threshold <num>` to set a new threshold | ||
--- | ||
error: maximum recursion depth has been reached | ||
use `set_option maxRecDepth <num>` to increase limit | ||
use `set_option diagnostics true` to get diagnostic information | ||
-/ | ||
#guard_msgs in | ||
example : 2^2^8000000 = 3^3^10000000 := | ||
rfl | ||
|
||
/-- | ||
-/ | ||
#guard_msgs in | ||
set_option exponentiation.threshold 258 in | ||
example : 2^257 = 2*2^256 := | ||
rfl | ||
|
||
/-- | ||
warning: exponent 2008 exceeds the threshold 256, exponentiation operation was not evaluated, use `set_option exponentiation.threshold <num>` to set a new threshold | ||
--- | ||
warning: declaration uses 'sorry' | ||
--- | ||
error: (kernel) deep recursion detected | ||
--- | ||
info: k : Nat | ||
h : k = 2008 ^ 2 + 2 ^ 2008 | ||
⊢ ((4032064 + 2 ^ 2008) ^ 2 + 2 ^ (4032064 + 2 ^ 2008)) % 10 = 6 | ||
-/ | ||
#guard_msgs in | ||
example (k : Nat) (h : k = 2008^2 + 2^2008) : (k^2 + 2^k)%10 = 6 := by | ||
simp [h] | ||
trace_state | ||
sorry | ||
|
||
/-- | ||
warning: declaration uses 'sorry' | ||
--- | ||
info: k : Nat | ||
h : k = 2008 ^ 2 + 2 ^ 2008 | ||
⊢ ((2008 ^ 2 + 2 ^ 2008) ^ 2 + 2 ^ (2008 ^ 2 + 2 ^ 2008)) % 10 = 6 | ||
-/ | ||
#guard_msgs in | ||
example (k : Nat) (h : k = 2008^2 + 2^2008) : (k^2 + 2^k)%10 = 6 := by | ||
rw [h] | ||
trace_state | ||
sorry |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a little worried that this doesn't actually help; I think when I tried this patch, instead of:
The result is now:
a ^ n
via nat reductiona ^ n
is expanded toa * a ^ (n - 1)
, allocating small objects and filling up the stacka ^ (n - 1)
via nat reductionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's the smallest test I could construct:
Apologies for the Qq use
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this was fixed by #4934!