-
Notifications
You must be signed in to change notification settings - Fork 124
feat: add BitVec.ofFn and lemmas #1078
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
Merged
Merged
Changes from 17 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
b630163
feat: add BitVec.ofFn and lemmas
fgdorais 9b358f6
Merge branch 'leanprover-community:main' into bitvec-offn
fgdorais f7ae994
feat: do both LE and BE versions
fgdorais 1f26464
feat: more getter lemmas
fgdorais d8bd13d
feat: add Nat.ofBits and Fin.ofBits
fgdorais 7a8b3e2
fix: typo
fgdorais 4613b2e
fix: long line
fgdorais 66a7724
fix: typos
fgdorais 4b995c9
feat: add `Int.testBit` and `Int.ofBits`
fgdorais 9ac228f
fix: use sign extension
fgdorais bbe6ae9
feat: add `Nat.ofBits`
fgdorais 287e090
feat: add `Fin.ofBits`
fgdorais 2ce1194
fix: revert last two commits
fgdorais c155232
chore: merge main
fgdorais 03b1be8
chore: merge #1089
fgdorais 9b39f2b
chore: merge main
fgdorais 3673ee9
chore: split file
fgdorais 7ef953f
feat: more getter lemmas
fgdorais c3efcc9
feat: add `Int.ofBits`
fgdorais 9e36b24
chore: merge #1096 (feat: add `Int.ofBits`)
fgdorais 03bff5d
feat: add toInt lemmas
fgdorais d5be6d0
Merge branch 'main' into bitvec-offn
fgdorais 1b1f76e
Merge branch 'main' into bitvec-offn
fgdorais ffef271
chore: remove upstreamed lemmas
fgdorais File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import Batteries.Data.BitVec.Basic | ||
import Batteries.Data.BitVec.Lemmas |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/- | ||
Copyright (c) 2024 François G. Dorais. All rights reserved. | ||
Released under Apache 2.0 license as described in the file LICENSE. | ||
Authors: François G. Dorais | ||
-/ | ||
|
||
import Batteries.Data.Fin.OfBits | ||
|
||
namespace BitVec | ||
|
||
/-- `ofFnLEAux f` returns the `BitVec m` whose `i`th bit is `f i` when `i < m`, little endian. -/ | ||
@[inline] def ofFnLEAux (m : Nat) (f : Fin n → Bool) : BitVec m := | ||
Fin.foldr n (fun i v => v.shiftConcat (f i)) 0 | ||
|
||
/-- `ofFnLE f` returns the `BitVec n` whose `i`th bit is `f i` with little endian ordering. -/ | ||
@[inline] def ofFnLE (f : Fin n → Bool) : BitVec n := ofFnLEAux n f | ||
|
||
/-- `ofFnBE f` returns the `BitVec n` whose `i`th bit is `f i` with big endian ordering. -/ | ||
@[inline] def ofFnBE (f : Fin n → Bool) : BitVec n := ofFnLE fun i => f i.rev |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/- | ||
Copyright (c) 2024 François G. Dorais. All rights reserved. | ||
Released under Apache 2.0 license as described in the file LICENSE. | ||
Authors: François G. Dorais | ||
-/ | ||
|
||
import Batteries.Data.BitVec.Basic | ||
|
||
namespace BitVec | ||
|
||
theorem getElem_shiftConcat (v : BitVec n) (b : Bool) (i) (h : i < n) : | ||
fgdorais marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(v.shiftConcat b)[i] = if i = 0 then b else v[i-1] := by | ||
rw [← getLsbD_eq_getElem, getLsbD_shiftConcat, getLsbD_eq_getElem, decide_eq_true h, | ||
Bool.true_and] | ||
|
||
@[simp] theorem getElem_shiftConcat_zero (v : BitVec n) (b : Bool) (h : 0 < n) : | ||
(v.shiftConcat b)[0] = b := by simp [getElem_shiftConcat] | ||
|
||
@[simp] theorem getElem_shiftConcat_succ (v : BitVec n) (b : Bool) (h : i + 1 < n) : | ||
(v.shiftConcat b)[i+1] = v[i] := by simp [getElem_shiftConcat] | ||
|
||
@[simp] theorem toNat_ofFnLEAux (m : Nat) (f : Fin n → Bool) : | ||
(ofFnLEAux m f).toNat = Nat.ofBits f % 2 ^ m := by | ||
simp only [ofFnLEAux] | ||
induction n with | ||
| zero => rfl | ||
| succ n ih => | ||
rw [Fin.foldr_succ, toNat_shiftConcat, Nat.shiftLeft_eq, Nat.pow_one, Nat.ofBits_succ, ih, | ||
← Nat.mod_add_div (Nat.ofBits (f ∘ Fin.succ)) (2 ^ m), Nat.mul_add 2, Nat.add_right_comm, | ||
Nat.mul_left_comm, Nat.add_mul_mod_self_left, Nat.mul_comm 2] | ||
rfl | ||
|
||
@[simp] theorem toFin_ofFnLEAux (m : Nat) (f : Fin n → Bool) : | ||
(ofFnLEAux m f).toFin = Fin.ofNat' (2 ^ m) (Nat.ofBits f) := by | ||
ext; simp | ||
|
||
@[simp] theorem toNat_ofFnLE (f : Fin n → Bool) : (ofFnLE f).toNat = Nat.ofBits f := by | ||
fgdorais marked this conversation as resolved.
Show resolved
Hide resolved
|
||
rw [ofFnLE, toNat_ofFnLEAux, Nat.mod_eq_of_lt (Nat.ofBits_lt_two_pow f)] | ||
|
||
@[simp] theorem toFin_ofFnLE (f : Fin n → Bool) : (ofFnLE f).toFin = Fin.ofBits f := by | ||
ext; simp | ||
|
||
theorem getElem_ofFnLEAux (f : Fin n → Bool) (i) (h : i < n) (h' : i < m) : | ||
(ofFnLEAux m f)[i] = f ⟨i, h⟩ := by | ||
simp only [ofFnLEAux] | ||
induction n generalizing i m with | ||
| zero => contradiction | ||
| succ n ih => | ||
simp only [Fin.foldr_succ, getElem_shiftConcat] | ||
cases i with | ||
| zero => | ||
simp | ||
| succ i => | ||
rw [ih (fun i => f i.succ)] <;> try omega | ||
simp | ||
|
||
@[simp] theorem getElem_ofFnLE (f : Fin n → Bool) (i) (h : i < n) : (ofFnLE f)[i] = f ⟨i, h⟩ := | ||
getElem_ofFnLEAux .. | ||
|
||
theorem getLsb'_ofFnLE (f : Fin n → Bool) (i) : (ofFnLE f).getLsb' i = f i := by simp | ||
|
||
@[simp] theorem getMsb'_ofFnLE (f : Fin n → Bool) (i) : (ofFnLE f).getMsb' i = f i.rev := by | ||
simp [getMsb'_eq_getLsb', Fin.rev]; congr 2; omega | ||
|
||
@[simp] theorem toNat_ofFnBE (f : Fin n → Bool) : (ofFnBE f).toNat = Nat.ofBits (f ∘ Fin.rev) := by | ||
simp [ofFnBE]; rfl | ||
|
||
@[simp] theorem toFin_ofFnBE (f : Fin n → Bool) : (ofFnBE f).toFin = Fin.ofBits (f ∘ Fin.rev) := by | ||
ext; simp | ||
|
||
@[simp] theorem getElem_ofFnBE (f : Fin n → Bool) (i) (h : i < n) : | ||
(ofFnBE f)[i] = f (Fin.rev ⟨i, h⟩) := by simp [ofFnBE] | ||
|
||
theorem getLsb'_ofFnBE (f : Fin n → Bool) (i) : (ofFnBE f).getLsb' i = f i.rev := by | ||
simp | ||
|
||
@[simp] theorem getMsb'_ofFnBE (f : Fin n → Bool) (i) : (ofFnBE f).getMsb' i = f i := by | ||
simp [ofFnBE] |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.