-
Notifications
You must be signed in to change notification settings - Fork 460
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
Conversation
- Adds configuration option `exponentiation.threshold` - An expression `b^n` where `b` and `n` are literals is not reduced by `whnf`, `simp`, and `isDefEq` if `n > exponentiation.threshold`. Motivation: prevents system from becoming irresponsive. TODO: improve support in the kernel. It is using a hard-coded limit for now.
Mathlib CI status (docs):
|
What do you think about using |
@@ -622,7 +634,7 @@ optional<expr> type_checker::reduce_nat(expr const & e) { | |||
if (f == *g_nat_add) return reduce_bin_nat_op(nat_add, e); | |||
if (f == *g_nat_sub) return reduce_bin_nat_op(nat_sub, e); | |||
if (f == *g_nat_mul) return reduce_bin_nat_op(nat_mul, e); | |||
if (f == *g_nat_pow) return reduce_bin_nat_op(nat_pow, e); | |||
if (f == *g_nat_pow) return reduce_pow(e); |
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:
- Lean asks GMP to compute a very large natural, using lots of CPU or perhaps running out of heap memory
The result is now:
- Lean refuses to expand
a ^ n
via nat reduction a ^ n
is expanded toa * a ^ (n - 1)
, allocating small objects and filling up the stack- Lean refuses to expand
a ^ (n - 1)
via nat reduction - ...
- Recursion uses lots of CPU, and hits a stack overflow
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.
Here's the smallest test I could construct:
import Lean
import Qq
open Qq
theorem foo : 2 ^ 100000000 = (2 ^ 10000) ^ 10000 := by
run_tac do
(← Lean.Elab.Tactic.getMainGoal).assign
q(Eq.refl ((2 ^ 10000) ^ 10000))
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!
In the Carleson project a non-fatal PANIC occurring in the application of certain applications of `Nat.reducePow` was discovered (https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/PANIC.20at.20Lean.2EExpr.2EappArg!.20application.20expected/near/457037665). We fix this regression from leanprover#4637. Fixes leanprover#4947.
In the Carleson project a non-fatal PANIC occurring in the application of certain applications of `Nat.reducePow` was discovered (https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/PANIC.20at.20Lean.2EExpr.2EappArg!.20application.20expected/near/457037665). We fix this regression from leanprover#4637. Fixes leanprover#4947.
In the Carleson project a non-fatal PANIC occurring in the application of certain applications of `Nat.reducePow` was discovered (https://leanprover.zulipchat.com/#narrow/stream/270676-lean4/topic/PANIC.20at.20Lean.2EExpr.2EappArg!.20application.20expected/near/457037665). We fix this regression from leanprover#4637. Fixes leanprover#4947.
Summary:
exponentiation.threshold
b^n
whereb
andn
are literals is not reduced bywhnf
,simp
, andisDefEq
ifn > exponentiation.threshold
.Motivation: prevents system from becoming irresponsive and/or crashing without memory.
TODO: improve support in the kernel. It is using a hard-coded limit for now.