lean4 icon indicating copy to clipboard operation
lean4 copied to clipboard

fix: tail-recursive `BitVec.ofBoolListLE`/`ofBoolListBE` to avoid stack overflow

Open kim-em opened this issue 7 months ago • 2 comments

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. The two @[csimp] theorems chain through BitVec.eq_of_getLsbD_eq and the existing getLsbD_ofBoolListLE/getLsbD_ofBoolListBE characterizations.

🤖 Prepared with Claude Code

kim-em avatar Apr 29 '26 13:04 kim-em

Mathlib CI status (docs):

  • ❗ Mathlib CI can not be attempted yet, as the nightly-testing-2026-04-29 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-mathlib, Mathlib CI should run now. You can force Mathlib CI using the force-mathlib-ci label. (2026-04-29 14:47:34)
  • 💥 Mathlib branch lean-pr-testing-13576 build failed against this PR. (2026-05-16 07:55:26) View Log Forcing Mathlib CI because the force-mathlib-ci label is present, despite problem: - ❗ Batteries/Mathlib CI will not be attempted unless your PR branches off the nightly-with-mathlib branch. Try git rebase 1fb7efad0255f2f227a68e7023c4c5a6a8e42265 --onto 4792cd22887c8b529a351f6563b693426ff2a8f8. (2026-06-18 04:46:49)
  • ✅ Mathlib branch lean-pr-testing-13576 has successfully built against this PR. (2026-06-18 05:42:22) View Log

Reference manual CI status:

  • ❗ Reference manual CI can not be attempted yet, as the nightly-testing-2026-04-29 tag does not exist there yet. We will retry when you push more commits. If you rebase your branch onto nightly-with-manual, reference manual CI should run now. You can force reference manual CI using the force-manual-ci label. (2026-04-29 14:47:35)
  • ❗ Reference manual CI will not be attempted unless your PR branches off the nightly-with-manual branch. Try git rebase 1fb7efad0255f2f227a68e7023c4c5a6a8e42265 --onto 803553a556fd82fa1060efb0c43eda542130cb16. You can force reference manual CI using the force-manual-ci label. (2026-06-18 04:46:50)

leanprover-bot avatar Apr 29 '26 14:04 leanprover-bot