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: safe exponentiation #4637

Merged
merged 1 commit into from
Jul 3, 2024
Merged

feat: safe exponentiation #4637

merged 1 commit into from
Jul 3, 2024

Conversation

leodemoura
Copy link
Member

Summary:

  • 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 and/or crashing without memory.

TODO: improve support in the kernel. It is using a hard-coded limit for now.

- 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.
@github-actions github-actions bot temporarily deployed to lean-lang.org/lean4/doc July 3, 2024 03:53 Inactive
@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 Jul 3, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/batteries that referenced this pull request Jul 3, 2024
leanprover-community-mathlib4-bot added a commit to leanprover-community/mathlib4 that referenced this pull request Jul 3, 2024
@leanprover-community-mathlib4-bot leanprover-community-mathlib4-bot added the builds-mathlib CI has verified that Mathlib builds against this PR label Jul 3, 2024
@leanprover-community-mathlib4-bot
Copy link
Collaborator

Mathlib CI status (docs):

@leodemoura leodemoura added this pull request to the merge queue Jul 3, 2024
Merged via the queue into master with commit f5fd962 Jul 3, 2024
16 checks passed
@eric-wieser
Copy link
Contributor

  • if n > exponentiation.threshold

What do you think about using (max (log2 a) 1) * n > exponentiation.threshold instead? That would also prevent memory being consumed when a is itself large.

@@ -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);
Copy link
Contributor

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 to a * 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

Copy link
Contributor

@eric-wieser eric-wieser Aug 12, 2024

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

Copy link
Contributor

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!

Parcly-Taxel added a commit to Parcly-Taxel/lean4 that referenced this pull request Aug 9, 2024
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.
Parcly-Taxel added a commit to Parcly-Taxel/lean4 that referenced this pull request Aug 9, 2024
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.
Parcly-Taxel added a commit to Parcly-Taxel/lean4 that referenced this pull request Aug 12, 2024
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
builds-mathlib CI has verified that Mathlib builds against this PR 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.

3 participants