-
Notifications
You must be signed in to change notification settings - Fork 715
feat: add BitVec container operations #11329
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
base: master
Are you sure you want to change the base?
Conversation
|
Mathlib CI status (docs):
|
|
Reference manual CI status:
|
|
I think you should have reverse induction (inducting from the least significant bit) as well. This would use BitVec.concat. |
|
@linesthatinterlace That's a good idea! I'm working on adding it at the moment. But do you mean *from the most significant bit? |
|
No, I originally said that, but I think actually cons is defined from the most significant bit because BitVec generally takes a big-endian approach. |
If for |
|
I was able to get a native I'm also by no means a C++ expert, so any feedback is welcome! |
This commit introduces a native C++ implementation for `BitVec.popcount` to significantly improve its performance, especially on large bitvectors. - The `mpz` class is extended with a `popcount` method. - A new extern function `lean_bitvec_popcount` is implemented in the runtime. It uses compiler intrinsics for hardware popcount instructions (e.g., `__builtin_popcountll`, `__popcnt64`) when available, and gracefully falls back to a generic implementation on other platforms. - `BitVec.zerocount` is refactored to be a cheap calculation based on the now-fast `popcount`, rather than a separate fold.
d9cb31f to
7faf83b
Compare
aaef830 to
df2d2b0
Compare
This PR adds fold operations and container-style operations for
BitVec, following patterns fromListandArray. This includes fold operations (foldr,foldl,foldrIdx,foldlIdxwith cons theorems), conversion operations (ofFn,toList,toArray,toVector), counting operations (popcount,zerocount,countP,countIdxP), transformation operations (map,mapIdx,zipWith), and distance/similarity operations (dot,hammingDist,parity).Related Zulip discussion: https://leanprover.zulipchat.com/#narrow/channel/217875-Is-there-code-for-X.3F/topic/Why.20isn't.20there.20code.20for.20.60BitVec.2Efoldr.60.3F
New modules
Init.Data.BitVec.Folds(additions): AddedfoldrIdx_nil,foldrIdx_cons,foldlIdx_nil,foldlIdx_constheorems for indexed foldsInit.Data.BitVec.OfFn: Conversion operations (ofFn,toList,toArray,toVector,ofArray,ofVector)Init.Data.BitVec.Count: Counting operations (popcount,zerocount,countP,countIdxP)Init.Data.BitVec.Map: Transformation operations (map,mapIdx,zipWith)Init.Data.BitVec.Hamming: Distance and similarity operations (dot,hammingDist,parity)Implementation details
foldrIdx,foldlIdx)tests/lean/run/bitvec_container.lean(60+ test cases)Notes
Hamming.leanshould go to mathlib4 instead?