Skip to content
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: BitVec.shiftLeft_shiftLeft, BitVec.shiftRight_shiftRight #4148

Merged
merged 3 commits into from
May 13, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/Init/Data/BitVec/Lemmas.lean
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,32 @@ theorem shiftLeftZeroExtend_eq {x : BitVec w} :
(shiftLeftZeroExtend x i).msb = x.msb := by
simp [shiftLeftZeroExtend_eq, BitVec.msb]

theorem BitVec.shiftLeft_shiftLeft (w : Nat) (x : BitVec w) (n m : Nat) :
bollu marked this conversation as resolved.
Show resolved Hide resolved
(x <<< n) <<< m = x <<< (n + m) := by
ext i
simp only [getLsb_shiftLeft, Fin.is_lt, decide_True, Bool.true_and]
-- omega claims that the system cannot be proven, so we case bash.
-- The entire proof below should be redundant once omega is complete.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

decide statements are out of scope for omega!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to case bash and then hand off to omega, use:

theorem BitVec.shiftLeft_shiftLeft (w : Nat) (x : BitVec w) (n m : Nat) :
    (x <<< n) <<< m = x <<< (n + m) := by
  ext i
  simp only [getLsb_shiftLeft, Fin.is_lt, decide_True, Bool.true_and]
  rw [show i - (n + m) = (i - m - n) by omega]
  cases h₂ : decide (i < m) <;> cases h₃ : decide (i - m < w) <;> cases h₄ : decide (i - m < n) <;> cases h₅ : decide (i < n + m) <;>
    simp at * <;> omega

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet, thanks for the tutorial! rw [show _ by omega] is a neat one, I'm going to remember this golf trick :)

rw [Bool.eq_iff_iff]
have hgetLsbIndex : i - (n + m) = (i - m - n) := by omega
rw [hgetLsbIndex]
apply Iff.intro
. intro h
simp_all only [Bool.and_eq_true, Bool.not_eq_true', decide_eq_false_iff_not, Nat.not_lt,
decide_eq_true_eq]
rcases h with ⟨h₁, h₂, _⟩
constructor
. omega
. trivial
. intro h
simp_all only [Bool.and_eq_true, Bool.not_eq_true', decide_eq_false_iff_not, Nat.not_lt,
decide_eq_true_eq]
rcases h with ⟨h₁, h₂⟩
constructor
. omega
. simp only [h₂, and_true]
omega

/-! ### ushiftRight -/

@[simp, bv_toNat] theorem toNat_ushiftRight (x : BitVec n) (i : Nat) :
Expand Down Expand Up @@ -693,6 +719,11 @@ theorem msb_append {x : BitVec w} {y : BitVec v} :
simp only [getLsb_append, cond_eq_if]
split <;> simp [*]

theorem BitVec.shiftRight_shiftRight (w : Nat) (x : BitVec w) (n m : Nat) :
(x >>> n) >>> m = x >>> (n + m) := by
ext i
simp [Nat.add_assoc n m i]

/-! ### rev -/

theorem getLsb_rev (x : BitVec w) (i : Fin w) :
Expand Down
Loading