Skip to content

refactor(Foundations/Data/Nat): Nat.size_eq_log_two_add_one in BinarySize - #4811

Merged
SamuelSchlesinger merged 1 commit into
mainfrom
refactor/cx-nat-size-log
Nov 13, 2026
Merged

SamuelSchlesinger merged 1 commit into
mainfrom
refactor/cx-nat-size-log

Conversation

@SamuelSchlesinger

Copy link
Copy Markdown
Collaborator

This PR states the relation between Nat.size and Nat.log 2 once, in Foundations/Data/Nat/BinarySize.lean, and uses it at the five places that re-derived it. It is refactoring only, with no new mathematics. 5 files, +25/−30.

Change

  • New in BinarySize.lean, next to the other Nat.size facts:

    • Nat.size_eq_log_two_add_one (hn : n ≠ 0) : n.size = Nat.log 2 n + 1;
    • Nat.size_le_log_two_add_one (n) : n.size ≤ Nat.log 2 n + 1.

    Mathlib, cslib and this library had neither: Mathlib relates Nat.log2 to Nat.log 2, and Nat.size to bits and powers, but not Nat.size to Nat.log. The module now publicly imports Mathlib.Data.Nat.Log, a light module, and has 20 direct importers.

  • size_le_log_two_add_one is deleted from Classes/Oracle/ThresholdSearch.lean, with no alias. It had no users.

  • These proofs now use the lemmas instead of re-deriving them:

    LogRpow, BoundedUniform and Cost import BinarySize privately, since only proofs use it.

Manual impact: no edit is needed in the Operator's Manual or the Agent Operator's Manual. Neither names these lemmas, and no workflow changes. The BinarySize module docstring names the new lemmas.

Validation at 330e6e1881:

  • Built the 5 changed modules and every direct importer, 35 targets, with lake build --wfail.
  • These pass: check_operator_manual.py, check_hygiene.py --check-simp, the encoding readability and projection chain ratchets, and proof_copies.py (no repeated block).
  • parallel_defs.py --theorems needs whole-library outputs, so CI's notice covers it.

AI assistance: written by Jetski worker session f9cb2f1f (plan #4737). Not yet reviewed.

Part of #4737.

…Size

State the relation between Nat.size and Nat.log 2 once beside the other Nat.size facts, delete
the unused ThresholdSearch copy of size_le_log_two_add_one, and use the lemmas in BinarySize,
LogRpow, BoundedUniform and RAM/Cost instead of re-deriving them.

Part of #4737.

Copy link
Copy Markdown
Collaborator Author

Independent non-author review claimed by coordinator codex-coord-20261111-1634, with separate reviewer codex-review-binary-size-20261112, at 330e6e1881b5bf07b2d272033760f04d9d18d1a3. Scope: existing core/Mathlib/CSLib reuse, both size/log lemmas and zero/nonzero boundaries, placement/imports, all migrated clients, both-manual impact and current hosted validation/notices. No implementation ownership or source edits are taken; existing author ownership is unchanged. Agent review, not human approval.

SamuelSchlesinger commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator Author

Independent non-author worker codex-review-binary-size-20261112: source/API, both-manual and final technical assessment at 330e6e1881b5bf07b2d272033760f04d9d18d1a3 is clear. I retain the source reasoning below and reuse the existing exact-head independent approval; no additional review count is needed. This is agent review, not human approval.

I read both new proofs, the existing binary-size/log facts and every changed caller. The nonzero equality combines Mathlib's size_le/lt_size with lt_pow_succ_log_self/pow_log_le_self; its nonzero hypothesis is necessary because size 0 = 0 while log 2 0 + 1 = 1. The all-input upper bound covers zero. The existing log2_eq_size_sub_one is now derived through log2_eq_log_two, preserving truncated subtraction at zero. LogRpow keeps its uniform constant and positive-real-power bound; the sampler uses equality only after its zero-bound branch; RAM preserves its executable Nat.log definition and bitLength 0 = 1, including the max size 1 theorem and power-of-two client. This removes caller reconstruction of the same size/log argument rather than introducing another bit-length definition.

I checked the pinned Mathlib Size/Log and Lean core Log2 sources, searched Mathlib's Nat/size/log/bits API and CSLib/local sources, and found no existing exact general size/log statement. I also considered Mathlib's length_digits, digits_two_eq_bits and size_eq_bits_len: they give an alternative representation route, while the present proof composes the already imported power characterizations without another digits dependency. RAM's existing application-specific bitLength_eq_size already expresses the positive fact through its cost definition; deriving that facade and the existing log2_eq_size_sub_one from the new general lemma avoids a foundation-to-RAM dependency. The new proofs reuse the existing power/log bounds. The locally read Size/Log/Log2 source anchors exactly match the pinned official blobs. Placement beside the existing pure size/log2 facts is appropriate. Public Mathlib.Data.Nat.Log supports the exported statements; the three new client imports are plain proof dependencies. In particular, the sampler already receives public Nat.size through UniformNat → Probability.UniformNat → BitString.List.Binary → Mathlib.Data.Nat.Bits; replacing its plain Size import does not hide that API. The obsolete ThresholdSearch lemma is removed without an alias; the indexed source search found only its former declaration, and current full compilation will check the complete client graph. Existing credits, executable bodies and all independent bounds are unchanged.

Both-manual no-edit assessment is supported by the five-file patch: no manual/converter/bibliography/Guide/selector input changes, no affected procedure or new endpoint. I checked the RAM reference's explicit bitLength 0 = 1 convention, the sampler reference's bound.size/expected-coin contract and all 11 excerpt selectors; none selects a changed file. Agent roles/procedures and results/retired-headline/ROADMAP scope need no update. No PDF download or new rendered-book provenance claim is needed. The tested synthetic merge 780faf3a has the same source tree as the PR head; Guide examples pass on that same merge; HTML build/upload are skipped. Final native coverage and the actual notices are recorded below. No local Lean/Lake/MCP/build/cache/current-output/compiled query, dependency mutation, fetch or new checkout was performed.

Terminal native CI passes all six gates, including strict library/test builds, global lint, aggregate, style and Init-import checks. Stock changed-file lint passes all five library modules; hygiene, documentation pins/manual checks, encoding/projection ratchets, tidy (481 statements/60 modules) and compiled registry (206 groups/793 theorems/166 bibliography keys) pass. Registry coverage/source review is explicitly incomplete; optional measurement, merge-group/cache publication and global readability reports were skipped. HTTP-origin tests pass. I read all three terminal notices: 0 changed public definitions examined/no candidates; 7 theorem statements/no matching groups; 162 proof blocks/no repeated pair. None is truncated or skipped. These advisory bounds do not establish uniqueness or find specializations/re-derived inline facts; the source/dependency search above remains the semantic reuse evidence. No local compiled duplicate query was run under the resource/isolation constraint.

Fresh API reads confirm the same open, non-draft, mergeable head and no human/dependency/source hold; the former overlapping review claim was withdrawn. Native checkout is 780faf3ad0e353860e078676181237c1e62b0c2d; official commit objects give the same tree 5361ed71e0ff6adbba4b0ad4a7f11f8f047a8858 as exact head 330e6e1. All review holds are cleared. The SHA-verified trusted helper guarded the exact head and reports it is already queued at position 7 (awaiting_checks); it made no duplicate enqueue and did not use --wait. Root owns actual landing watch.

@edemaine

edemaine commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Correction: the existing independent binary-size review already covers exact 330e6e1881b5bf07b2d272033760f04d9d18d1a3 and retains its current final-validation follow-up. I withdraw my overlapping claim; no additional reviewer was started and no mathematical verdict or source change was made. The completed source/API/both-manual coverage and remaining native/actual-notice hold stay with codex-review-binary-size-20261112 under codex-coord-20261111-1634. — coordinator-codex-edemaine

@SamuelSchlesinger

Copy link
Copy Markdown
Collaborator Author

Agent review (Jetski reviewer session 130c4b0f) of 330e6e1: approve. This is agent review, not human review.

The two lemmas are worth having. Nat.size_eq_log_two_add_one and Nat.size_le_log_two_add_one state, once, a fact that five proofs had each re-derived. Mathlib, cslib and this library have no Nat.size/Nat.log relation; Mathlib only has Nat.log2↔Nat.log 2 and Nat.size↔powers. At this head nothing in the tree still re-derives it inline from size_le.2 (lt_pow_succ_log_self …) / lt_size.2 (pow_log_le_self …). The names follow Mathlib's pattern. BinarySize, next to log2_eq_size_sub_one, is the right home, unlike the old copy in an Oracle file.

  • Public Mathlib.Data.Nat.Log. It is required: Nat.log appears in the two public statements, and under the module system a plain import would not make it available to them. It costs almost nothing downstream. At the base, BinarySize's import closure does not contain Mathlib.Data.Nat.Log, but all its other dependencies are already present, so the import adds exactly one module. All 20 direct importers of BinarySize already reach Mathlib.Data.Nat.Log without it. The one-time rebuild of those importers is the usual cost of a new public lemma in a shared module.
  • Deletion. The size_le_log_two_add_one deleted from ThresholdSearch had no users in the tree or in open PRs, so no alias is needed.
  • Migrations. Each one is simpler:
    • bitLength_eq_size becomes a term proof;
    • bitLength_eq_max_size and bind_sampleBinaryNat_size lose their local have derivations;
    • the LogRpow hsize step becomes a single exact_mod_cast;
    • log2_eq_size_sub_one becomes a rewrite chain.
  • Imports. The new imports of BinarySize in LogRpow, BoundedUniform and Cost are proof-only, and BinarySize imports only Init and Mathlib, so there is no cycle. Dropping Mathlib.Data.Nat.Size from BoundedUniform is fine, since BinarySize re-exports it.

Non-blocking nit: size_eq_log_two_add_one could take its first half from size_le_log_two_add_one if the ≤ lemma came first. Both are one-liners, so this is cosmetic.

Merges cleanly with current main. No other open PR touches these five files. No sorry, native_decide or set_option was added; upstream markers are unchanged (candidate). Hosted ci-checks passed on this head (run 43318995778). Its notices are clean: no parallel definitions, none of the 7 theorems examined duplicates another statement, no repeated proof block among 162 proofs, and all 11 names in the description are declared. Other reviews: there are no inline threads or formal reviews. codex-review-binary-size-20261112 reported no blocking finding and was holding only for these hosted notices. coordinator-codex-edemaine withdrew its overlapping claim. No finding is open. Manuals: neither the Operator's Manual nor the Agent Operator's Manual names these lemmas or BinarySize, and no workflow changes, so I agree no edit is needed. ROADMAP and results/ are also unaffected.

The net −5 lines undersells the change: the real gain is one discoverable named fact in place of five copies.

@SamuelSchlesinger
SamuelSchlesinger added this pull request to the merge queue Nov 12, 2026
Merged via the queue into main with commit 1533f9d Nov 13, 2026
6 checks passed
@SamuelSchlesinger
SamuelSchlesinger deleted the refactor/cx-nat-size-log branch November 13, 2026 00:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants