fix: tail-recursive BitVec.ofBoolListLE/ofBoolListBE to avoid stack overflow#13576
Draft
kim-em wants to merge 3 commits intoleanprover:masterfrom
Draft
fix: tail-recursive BitVec.ofBoolListLE/ofBoolListBE to avoid stack overflow#13576kim-em wants to merge 3 commits intoleanprover:masterfrom
BitVec.ofBoolListLE/ofBoolListBE to avoid stack overflow#13576kim-em wants to merge 3 commits intoleanprover:masterfrom
Conversation
…ck overflow This PR adds tail-recursive replacements for `BitVec.ofBoolListLE` and `BitVec.ofBoolListBE`, registered via `@[csimp]`, to avoid stack overflow on lists with ~1M elements. The reference definitions in `Init.Data.BitVec.Basic` recurse via `concat`, which is clean for proofs but allocates O(n) stack frames. The new implementations in `Init.Data.BitVec.Impl` pack bits in 64-bit chunks (`packChunk`, `collectChunks`) and combine them via a balanced tree merge (`mergePass`, `treeMerge`), giving O(n log n) work and O(1) stack usage. Correctness is established via a list-level spec function `flattenList` giving the intended Nat semantics of `(value, width)` pairs, with `flattenList_append`, `flattenList_mergePassList` (key bit-packing identity), and a chunk-local `testBit_flattenList_collectChunks_aux`. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
Mathlib CI status (docs):
|
Collaborator
|
Reference manual CI status:
|
….Internal` Codex review feedback applied: - Mark `packChunk`, `collectChunks`, `mergePass`, `treeMerge` as `private` — these have unreachable-fuel branches that make them bad public contracts. - Mark proof-only scaffolding (`flattenList`, `totalWidth`, `WellFormedList`, `mergePassList`) as `private` to avoid exposing them as API. - Extract `half_le_pow_of_le_double` (the `arr.size ≤ 2^(k+1) → (arr.size+1)/2 ≤ 2^k` bound used in the `treeMerge` halving step) into a standalone lemma. This isolates the omega/`Int.pow_succ` workaround to one place and turns `treeMerge_go_eq_flattenList`'s arithmetic step into a one-liner. Public surface is now just `ofBoolListLEImpl`, `ofBoolListBEImpl`, and the two `@[csimp]` theorems. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Replace the file-wide `public section` with `public` modifiers on just the two `ofBoolListLEImpl`/`ofBoolListBEImpl` defs and the two `@[csimp]` theorems. Everything else (the chunked-encoding helpers `packChunk`, `collectChunks`, `mergePass`, `treeMerge`, the proof-only `flattenList`/`totalWidth`/ `WellFormedList`/`mergePassList`, and all auxiliary lemmas) is now file-local. Also drop redundant imports `Init.Data.Nat.Lemmas` (transitive via `Init.Data.Array.Lemmas`) and `Init.Data.List.Lemmas` (transitive via `Init.Data.List.Nat.TakeDrop`). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This PR adds tail-recursive replacements for
BitVec.ofBoolListLEandBitVec.ofBoolListBE, registered via@[csimp], to avoid stack overflow on lists with ~1M elements.The reference definitions in
Init.Data.BitVec.Basicrecurse viaconcat, which is clean for proofs but allocates O(n) stack frames. The new implementations inInit.Data.BitVec.Implpack bits in 64-bit chunks (packChunk,collectChunks) and combine them via a balanced tree merge (mergePass,treeMerge), giving O(n log n) work and O(1) stack usage.Correctness is established via a list-level spec function
flattenListgiving the intended Nat semantics of(value, width)pairs, withflattenList_append,flattenList_mergePassList(key bit-packing identity), and a chunk-localtestBit_flattenList_collectChunks_aux. The two@[csimp]theorems chain throughBitVec.eq_of_getLsbD_eqand the existinggetLsbD_ofBoolListLE/getLsbD_ofBoolListBEcharacterizations.🤖 Prepared with Claude Code