-
Notifications
You must be signed in to change notification settings - Fork 354
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 by Bors] - chore: bump Std, changes for leanprover/std4#366 #8700
Changes from all commits
766c33e
9db8667
6240309
db5e363
bfc4612
82a3392
068ccf3
c43641c
18e1a0c
8b31141
2fd0cb4
5cad514
1a433ef
da0024a
d0245fb
85c799e
f4100cf
7b6f1a3
fcbe2e5
a936ad1
c3b7df4
3e71922
e90cfb7
8c7c2d9
e8952a9
3765c7e
c731f15
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,20 +31,10 @@ theorem toNat_inj {x y : BitVec w} : x.toNat = y.toNat ↔ x = y := | |
theorem toNat_lt_toNat {x y : BitVec w} : x.toNat < y.toNat ↔ x < y := | ||
Iff.rfl | ||
|
||
@[simp] | ||
lemma ofNat_eq_mod_two_pow (n : Nat) : (BitVec.ofNat w n).toNat = n % 2^w := rfl | ||
|
||
lemma toNat_ofNat {m} (h : m < 2^w) : (BitVec.ofNat w m).toNat = m := Fin.val_cast_of_lt h | ||
|
||
@[simp] | ||
lemma toNat_ofFin (x : Fin (2^w)) : (ofFin x).toNat = x.val := rfl | ||
attribute [simp] toNat_ofNat toNat_ofFin | ||
|
||
theorem toNat_append (msbs : BitVec w) (lsbs : BitVec v) : | ||
(msbs ++ lsbs).toNat = msbs.toNat <<< v ||| lsbs.toNat := by | ||
rcases msbs with ⟨msbs, hm⟩ | ||
rcases lsbs with ⟨lsbs, hl⟩ | ||
simp only [HAppend.hAppend, append, toNat_ofFin] | ||
rw [toNat_ofNat (Nat.add_comm w v ▸ append_lt hl hm)] | ||
lemma toNat_ofNat_of_lt {m} (h : m < 2^w) : (BitVec.ofNat w m).toNat = m := by | ||
simp only [toNat_ofNat, mod_eq_of_lt h] | ||
|
||
#noalign bitvec.bits_to_nat_to_bool | ||
|
||
|
@@ -60,14 +50,9 @@ lemma extractLsb_eq {w : ℕ} (hi lo : ℕ) (a : BitVec w) : | |
|
||
theorem toNat_extractLsb' {i j} {x : BitVec w} : | ||
(extractLsb' i j x).toNat = x.toNat / 2 ^ i % (2 ^ j) := by | ||
simp only [extractLsb', ofNat_eq_mod_two_pow, shiftRight_eq_div_pow] | ||
|
||
theorem getLsb_eq_testBit {i} {x : BitVec w} : getLsb x i = x.toNat.testBit i := 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. Where did this go? 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. The RHS is now the definition of |
||
simp only [getLsb, Nat.shiftLeft_eq, one_mul, Nat.and_two_pow] | ||
cases' testBit (BitVec.toNat x) i | ||
<;> simp [pos_iff_ne_zero.mp (two_pow_pos i)] | ||
simp only [extractLsb', toNat_ofNat, shiftRight_eq_div_pow] | ||
|
||
theorem ofFin_val {n : ℕ} (i : Fin <| 2 ^ n) : (ofFin i).toNat = i.val := by | ||
theorem ofFin_val {n : ℕ} (i : Fin <| 2 ^ n) : (ofFin i).toNat = i.val := | ||
rfl | ||
#align bitvec.of_fin_val Std.BitVec.ofFin_val | ||
|
||
|
@@ -91,16 +76,12 @@ theorem decide_addLsb_mod_two {x b} : decide (addLsb x b % 2 = 1) = b := by | |
simp [addLsb] | ||
#align bitvec.to_bool_add_lsb_mod_two Std.BitVec.decide_addLsb_mod_two | ||
|
||
@[simp] | ||
lemma ofNat_toNat (x : BitVec w) : BitVec.ofNat w x.toNat = x := by | ||
rcases x with ⟨x⟩ | ||
simp [BitVec.ofNat] | ||
apply Fin.cast_val_eq_self x | ||
#align bitvec.of_nat_to_nat Std.BitVec.ofNat_toNat | ||
lemma ofNat_toNat' (x : BitVec w) : (x.toNat)#w = x := by | ||
rw [ofNat_toNat, truncate_eq] | ||
|
||
lemma ofNat_toNat' (x : BitVec w) (h : w = v): | ||
lemma ofNat_toNat_of_eq (x : BitVec w) (h : w = v): | ||
BitVec.ofNat v x.toNat = x.cast h := by | ||
cases h; rw [ofNat_toNat, cast_eq] | ||
cases h; rw [ofNat_toNat', cast_eq] | ||
|
||
theorem toFin_val {n : ℕ} (v : BitVec n) : (toFin v : ℕ) = v.toNat := by | ||
rfl | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,15 +126,7 @@ theorem xor_bit : ∀ a m b n, bit a m ^^^ bit b n = bit (bne a b) (m ^^^ n) := | |
bitwise_bit | ||
#align nat.lxor_bit Nat.xor_bit | ||
|
||
@[simp] | ||
theorem testBit_bitwise {f : Bool → Bool → Bool} (h : f false false = false) (m n k) : | ||
testBit (bitwise f m n) k = f (testBit m k) (testBit n k) := by | ||
induction' k with k ih generalizing m n | ||
<;> cases' m using bitCasesOn with a m | ||
<;> cases' n using bitCasesOn with b n | ||
<;> rw [bitwise_bit h] | ||
· simp [testBit_zero] | ||
· simp [testBit_succ, ih] | ||
attribute [simp] Nat.testBit_bitwise | ||
#align nat.test_bit_bitwise Nat.testBit_bitwise | ||
|
||
@[simp] | ||
|
@@ -152,9 +144,7 @@ theorem testBit_ldiff : ∀ m n k, testBit (ldiff m n) k = (testBit m k && not ( | |
testBit_bitwise rfl | ||
#align nat.test_bit_ldiff Nat.testBit_ldiff | ||
|
||
@[simp] | ||
theorem testBit_xor : ∀ m n k, testBit (m ^^^ n) k = bne (testBit m k) (testBit n k) := | ||
testBit_bitwise rfl | ||
attribute [simp] testBit_xor | ||
#align nat.test_bit_lxor Nat.testBit_xor | ||
|
||
end | ||
|
@@ -217,31 +207,19 @@ theorem zero_of_testBit_eq_false {n : ℕ} (h : ∀ i, testBit n i = false) : n | |
theorem testBit_eq_false_of_lt {n i} (h : n < 2 ^ i) : n.testBit i = false := by | ||
simp [testBit, shiftRight_eq_div_pow, Nat.div_eq_of_lt h] | ||
|
||
@[simp] | ||
theorem zero_testBit (i : ℕ) : testBit 0 i = false := by | ||
simp only [testBit, zero_shiftRight, bodd_zero] | ||
#align nat.zero_test_bit Nat.zero_testBit | ||
|
||
/-- The ith bit is the ith element of `n.bits`. -/ | ||
theorem testBit_eq_inth (n i : ℕ) : n.testBit i = n.bits.getI i := by | ||
induction' i with i ih generalizing n | ||
· simp [testBit, bodd_eq_bits_head, List.getI_zero_eq_headI] | ||
· simp only [testBit, zero_eq, shiftRight_zero, and_one_is_mod, mod_two_of_bodd, | ||
bodd_eq_bits_head, List.getI_zero_eq_headI] | ||
cases List.headI (bits n) <;> rfl | ||
conv_lhs => rw [← bit_decomp n] | ||
rw [testBit_succ, ih n.div2, div2_bits_eq_tail] | ||
cases n.bits <;> simp | ||
#align nat.test_bit_eq_inth Nat.testBit_eq_inth | ||
|
||
/-- Bitwise extensionality: Two numbers agree if they agree at every bit position. -/ | ||
theorem eq_of_testBit_eq {n m : ℕ} (h : ∀ i, testBit n i = testBit m i) : n = m := by | ||
induction' n using Nat.binaryRec with b n hn generalizing m | ||
· simp only [zero_testBit] at h | ||
exact (zero_of_testBit_eq_false fun i => (h i).symm).symm | ||
induction' m using Nat.binaryRec with b' m | ||
· simp only [zero_testBit] at h | ||
exact zero_of_testBit_eq_false h | ||
suffices h' : n = m by | ||
rw [h', show b = b' by simpa using h 0] | ||
exact hn fun i => by convert h (i + 1) using 1 <;> rw [testBit_succ] | ||
Comment on lines
-234
to
-244
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. This proof looks much nicer to me than the one (for the converse, 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. I guess that is inevitable, since we don't have |
||
#align nat.eq_of_test_bit_eq Nat.eq_of_testBit_eq | ||
|
||
theorem exists_most_significant_bit {n : ℕ} (h : n ≠ 0) : | ||
|
@@ -295,20 +273,20 @@ theorem lt_of_testBit {n m : ℕ} (i : ℕ) (hn : testBit n i = false) (hm : tes | |
|
||
@[simp] | ||
theorem testBit_two_pow_self (n : ℕ) : testBit (2 ^ n) n = true := by | ||
rw [testBit, shiftRight_eq_div_pow, Nat.div_self (pow_pos (α := ℕ) zero_lt_two n), bodd_one] | ||
rw [testBit, shiftRight_eq_div_pow, Nat.div_self (pow_pos (α := ℕ) zero_lt_two n)] | ||
simp | ||
#align nat.test_bit_two_pow_self Nat.testBit_two_pow_self | ||
|
||
theorem testBit_two_pow_of_ne {n m : ℕ} (hm : n ≠ m) : testBit (2 ^ n) m = false := by | ||
rw [testBit, shiftRight_eq_div_pow] | ||
cases' hm.lt_or_lt with hm hm | ||
· rw [Nat.div_eq_of_lt, bodd_zero] | ||
exact Nat.pow_lt_pow_of_lt_right one_lt_two hm | ||
· rw [Nat.div_eq_of_lt] | ||
· simp | ||
· exact Nat.pow_lt_pow_of_lt_right one_lt_two hm | ||
· rw [pow_div hm.le zero_lt_two, ← tsub_add_cancel_of_le (succ_le_of_lt <| tsub_pos_of_lt hm)] | ||
-- Porting note: XXX why does this make it work? | ||
rw [(rfl : succ 0 = 1)] | ||
simp only [ge_iff_le, tsub_le_iff_right, pow_succ, bodd_mul, | ||
Bool.and_eq_false_eq_eq_false_or_eq_false, or_true] | ||
exact Or.inr rfl | ||
simp [pow_succ, and_one_is_mod, mul_mod_left] | ||
#align nat.test_bit_two_pow_of_ne Nat.testBit_two_pow_of_ne | ||
|
||
theorem testBit_two_pow (n m : ℕ) : testBit (2 ^ n) m = (n = m) := by | ||
|
@@ -372,25 +350,10 @@ theorem zero_xor (n : ℕ) : 0 ^^^ n = n := by simp [HXor.hXor, Xor.xor, xor] | |
theorem xor_zero (n : ℕ) : n ^^^ 0 = n := by simp [HXor.hXor, Xor.xor, xor] | ||
#align nat.lxor_zero Nat.xor_zero | ||
|
||
@[simp] | ||
theorem zero_land (n : ℕ) : 0 &&& n = 0 := by | ||
simp only [HAnd.hAnd, AndOp.and, land, bitwise_zero_left, ite_false, Bool.and_true]; | ||
#align nat.zero_land Nat.zero_land | ||
|
||
@[simp] | ||
theorem land_zero (n : ℕ) : n &&& 0 = 0 := by | ||
simp only [HAnd.hAnd, AndOp.and, land, bitwise_zero_right, ite_false, Bool.and_false] | ||
#align nat.land_zero Nat.land_zero | ||
|
||
@[simp] | ||
theorem zero_lor (n : ℕ) : 0 ||| n = n := by | ||
simp only [HOr.hOr, OrOp.or, lor, bitwise_zero_left, ite_true, Bool.or_true] | ||
#align nat.zero_lor Nat.zero_lor | ||
|
||
@[simp] | ||
theorem lor_zero (n : ℕ) : n ||| 0 = n := by | ||
simp only [HOr.hOr, OrOp.or, lor, bitwise_zero_right, ite_true, Bool.or_false] | ||
#align nat.lor_zero Nat.lor_zero | ||
#align nat.zero_land Nat.zero_and | ||
#align nat.land_zero Nat.and_zero | ||
#align nat.zero_lor Nat.zero_or | ||
#align nat.lor_zero Nat.or_zero | ||
|
||
|
||
/-- Proving associativity of bitwise operations in general essentially boils down to a huge case | ||
|
@@ -488,11 +451,11 @@ theorem xor_trichotomy {a b c : ℕ} (h : a ≠ b ^^^ c) : | |
all_goals | ||
exact lt_of_testBit i (by | ||
-- Porting note: this was originally `simp [h, hi]` | ||
rw [Nat.testBit_xor, h, Bool.bne_eq_xor, Bool.true_xor,hi] | ||
rw [Nat.testBit_xor, h, Bool.xor, Bool.true_xor, hi] | ||
rfl | ||
) h fun j hj => by | ||
-- Porting note: this was originally `simp [hi' _ hj]` | ||
rw [Nat.testBit_xor, hi' _ hj, Bool.bne_eq_xor, Bool.xor_false, eq_self_iff_true] | ||
rw [Nat.testBit_xor, hi' _ hj, Bool.xor, Bool.xor_false, eq_self_iff_true] | ||
trivial | ||
#align nat.lxor_trichotomy Nat.xor_trichotomy | ||
|
||
|
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.
https://github.com/leanprover/std4/pull/366/files#r1410924581