Skip to content

Conversation

@georgerennie
Copy link
Contributor

This PR provides a Decidable instance for Nat.isPowerOfTwo based on the formula (n ≠ 0) ∧ (n &&& (n - 1)) = 0.

To do this it includes theorems about Nat.testBit to show that the n.log2th bit is set in n and n - 1 for non powers of two.

Bitwise lemmas are needed to reason about the &&& so the file Init.Data.Nat.Power2 is renamed to Init.Data.Nat.Power2.Basic and Init.Data.Nat.Power2.Lemmas introduced that depends on Init.Data.Nat.Bitwise.Lemmas to prevent circular includes.

This PR provides a `Decidable` instance for `Nat.isPowerOfTwo` based on
the identity `(n ≠ 0) ∧ (n &&& (n - 1)) = 0`.

To do this it includes theorems about `Nat.testBit` to show that the
`n.log2`th bit is set in `n` and `n - 1` for non powers of two.

Bitwise lemmas are needed to reason about the `&&&` so the file
`Init.Data.Nat.Power2` is renamed to `Init.Data.Nat.Power2.Basic`
and `Init.Data.Nat.Power2.Lemmas` introduced that depends on
`Init.Data.Nat.Bitwise.Lemmas` to prevent circular includes.
@georgerennie georgerennie requested a review from kim-em as a code owner January 5, 2026 16:04
@georgerennie
Copy link
Contributor Author

changelog-library

@github-actions github-actions bot added changelog-library Library toolchain-available A toolchain is available for this PR, at leanprover/lean4-pr-releases:pr-release-NNNN labels Jan 5, 2026
@leanprover-community-bot
Copy link
Collaborator

Mathlib CI status (docs):

  • ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase c358b0c7341de7cdd732b8f09ab552f7405facfb --onto 8207919728b9299f43b75e1efd52a6900580819b. You can force Mathlib CI using the force-mathlib-ci label. (2026-01-05 17:03:11)

@leanprover-bot
Copy link
Collaborator

Reference manual CI status:

  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase c358b0c7341de7cdd732b8f09ab552f7405facfb --onto 8207919728b9299f43b75e1efd52a6900580819b. You can force reference manual CI using the force-manual-ci label. (2026-01-05 17:03:13)

@kim-em kim-em added changelog-library Library and removed changelog-library Library labels Jan 9, 2026
@kim-em kim-em enabled auto-merge January 9, 2026 07:37
@kim-em kim-em added this pull request to the merge queue Jan 9, 2026
Merged via the queue into leanprover:master with commit b771d12 Jan 9, 2026
16 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog-library Library 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.

4 participants