forked from leanprover/lean4
-
Notifications
You must be signed in to change notification settings - Fork 0
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
feat: theorems for ushiftRight
#33
Draft
mhk119
wants to merge
8
commits into
master
Choose a base branch
from
ushiftRight
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ca7a608
ushiftRight theorems
mhk119 76b3e74
toFin thm
mhk119 6878a2d
small change
mhk119 7d2bc58
feat: break theorems up into smaller lemmas
bollu d9bf86f
Update src/Init/Data/Nat/Bitwise/Basic.lean
mhk119 d0ae41a
Update src/Init/Data/BitVec/Lemmas.lean
mhk119 3a1591d
Update src/Init/Data/BitVec/Lemmas.lean
mhk119 9ecf998
Update src/Init/Data/BitVec/Lemmas.lean
mhk119 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 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 |
---|---|---|
|
@@ -1240,6 +1240,48 @@ theorem toNat_ushiftRight_lt (x : BitVec w) (n : Nat) (hn : n ≤ w) : | |
· apply hn | ||
· apply Nat.pow_pos (by decide) | ||
|
||
|
||
/-- Shifting right by `n`, which is larger than the bitwidth `w` produces `0. -/ | ||
theorem ushiftRight_eq_zero {x : BitVec w} {n : Nat} (hn : w ≤ n) : | ||
x >>> n = 0#w := by | ||
simp [bv_toNat] | ||
have : 2^w ≤ 2^n := Nat.pow_le_pow_of_le Nat.one_lt_two hn | ||
rw [Nat.shiftRight_eq_div_pow, Nat.div_eq_of_lt (by omega)] | ||
|
||
|
||
/-- | ||
Unsigned shift right by at least one bit makes the value of the bitvector less than or equal to `2^(w-1)`, | ||
makes the interpretation of the bitvector `Int` and `Nat` agree. | ||
-/ | ||
theorem toInt_ushiftRight_of_lt {x : BitVec w} {n : Nat} (hn : 0 < n) : | ||
(x >>> n).toInt = x.toNat >>> n := by | ||
rw [toInt_eq_toNat_cond] | ||
simp only [toNat_ushiftRight, ite_eq_left_iff, Nat.not_lt] | ||
intros h | ||
by_cases hn: n ≤ w | ||
· have h1 := Nat.mul_lt_mul_of_pos_left (toNat_ushiftRight_lt x n hn) Nat.two_pos | ||
simp only [toNat_ushiftRight, Nat.zero_lt_succ, Nat.mul_lt_mul_left] at h1 | ||
have : 2 ^ (w - n).succ ≤ 2^ w := Nat.pow_le_pow_of_le (by decide) (by omega) | ||
have := show 2 * x.toNat >>> n < 2 ^ w by | ||
omega | ||
omega | ||
· have : x.toNat >>> n = 0 := by | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be simplified with |
||
apply Nat.shiftRight_eq_zero | ||
have : 2^w ≤ 2^n := Nat.pow_le_pow_of_le (by decide) (by omega) | ||
omega | ||
simp [this] at h | ||
omega | ||
|
||
@[simp] | ||
theorem toFin_uShiftRight {x : BitVec w} {n : Nat} : | ||
(x >>> n).toFin = x.toFin / (Fin.ofNat' (2^w) (2^n)) := by | ||
apply Fin.eq_of_val_eq | ||
by_cases hn : n < w | ||
· simp [Nat.shiftRight_eq_div_pow, Nat.mod_eq_of_lt (Nat.pow_lt_pow_of_lt Nat.one_lt_two hn)] | ||
· simp only [Nat.not_lt] at hn | ||
rw [ushiftRight_eq_zero _ _ (by omega)] | ||
simp [Nat.dvd_iff_mod_eq_zero.mp (Nat.pow_dvd_pow 2 hn)] | ||
|
||
@[simp] | ||
theorem getMsbD_ushiftRight {x : BitVec w} {i n : Nat} : | ||
(x >>> n).getMsbD i = (decide (i < w) && (!decide (i < n) && x.getMsbD (i - n))) := by | ||
|
This file contains 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 |
---|---|---|
|
@@ -34,4 +34,8 @@ theorem shiftRight_eq_div_pow (m : Int) (n : Nat) : | |
theorem zero_shiftRight (n : Nat) : (0 : Int) >>> n = 0 := by | ||
simp [Int.shiftRight_eq_div_pow] | ||
|
||
@[simp] | ||
theorem shiftRight_zero (n : Int) : n >>> 0 = n := by | ||
simp [Int.shiftRight_eq_div_pow] | ||
Comment on lines
+38
to
+39
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should |
||
|
||
end Int |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
makes the
two times in a row is grammatically suspicious.