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
Closed
Closed
Changes from all commits
Commits
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 |
---|---|---|
|
@@ -1316,6 +1316,51 @@ 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 | ||
|
||
/-- | ||
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. | ||
In the case when `n = 0`, then the shift right value equals the integer interpretation. | ||
-/ | ||
@[simp] | ||
theorem toInt_ushiftRight_eq_ite {x : BitVec w} {n : Nat} : | ||
(x >>> n).toInt = if n = 0 then x.toInt else x.toNat >>> n := by | ||
by_cases hn : n = 0 | ||
· simp [hn] | ||
· rw [toInt_ushiftRight_of_lt (by omega), toInt_eq_toNat_cond] | ||
simp [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.