Skip to content

Commit b013df0

Browse files
committed
checkpoint
1 parent d0e08c4 commit b013df0

File tree

1 file changed

+63
-17
lines changed

1 file changed

+63
-17
lines changed

src/Init/Data/BitVec/Lemmas.lean

Lines changed: 63 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,11 @@ theorem neg_eq_not_add (x : BitVec w) : -x = ~~~x + 1 := by
11171117
have hx : x.toNat < 2^w := x.isLt
11181118
rw [Nat.sub_sub, Nat.add_comm 1 x.toNat, ← Nat.sub_sub, Nat.sub_add_cancel (by omega)]
11191119

1120+
-- @[simp, bv_toNat] theorem toInt_sub (x y : BitVec w) :
1121+
-- (x - y).toInt = ((x.toNat + (((2 : Nat) ^ w : Nat) - y.toNat)) : Int).bmod (2 ^ w) := by
1122+
-- simp [toInt_eq_toNat_bmod]
1123+
-- norm_cast
1124+
-- simp
11201125
/-! ### mul -/
11211126

11221127
theorem mul_def {n} {x y : BitVec n} : x * y = (ofFin <| x.toFin * y.toFin) := by rfl
@@ -1211,23 +1216,40 @@ theorem toNat_intMax_eq : (intMax w).toNat = 2^w - 1 := by
12111216
(ofBoolListLE bs).getMsb i = (decide (i < bs.length) && bs.getD (bs.length - 1 - i) false) := by
12121217
simp [getMsb_eq_getLsb]
12131218

1214-
1215-
-- PROOF SKETCH --
1216-
1217-
@[simp] theorem ofInt_negSucc (w n : Nat) :
1218-
BitVec.ofInt w (Int.negSucc n) = ~~~ (BitVec.ofNat w n) := by
1219-
apply BitVec.eq_of_toNat_eq
1220-
simp
1221-
sorry
1222-
1223-
@[simp] theorem getLsb_ofInt (n : Nat) (x : Int) (i : Nat) :
1224-
getLsb (BitVec.ofInt n x) i = (i < n && x.testBit i) := by
1225-
cases x
1226-
case ofNat x =>
1227-
simp only [Int.ofNat_eq_coe, ofInt_natCast, getLsb_ofNat, Int.testBit_natCast]
1228-
case negSucc x =>
1229-
simp only [ofInt_negSucc, getLsb_not, getLsb_ofNat, Bool.not_and, Int.testBit]
1230-
cases decide (i < n) <;> simp
1219+
theorem Int.negSucc_div_ofNat (a b : Nat) (hb : b ≠ 0) :
1220+
(Int.negSucc a) / (Int.ofNat b) = Int.negSucc (((a / b) : Nat)) := by
1221+
rcases b with rfl | b
1222+
· contradiction
1223+
· norm_cast
1224+
1225+
#check Int.ediv
1226+
#reduce (Int.negSucc 4) % (Int.ofNat 1) -- 0
1227+
#reduce (Int.negSucc 4) % (Int.ofNat 1) -- 0
1228+
#reduce (Int.negSucc 4) % (Int.ofNat 1) -- 0
1229+
#reduce (Int.negSucc 4) % (Int.ofNat 1) -- 0
1230+
#reduce (Int.negSucc 4) % (Int.ofNat 1) -- 0
1231+
1232+
#reduce (Int.negSucc 0) % (Int.ofNat 8) -- 7
1233+
#reduce (Int.negSucc 1) % (Int.ofNat 8) -- 6
1234+
#reduce (Int.negSucc 2) % (Int.ofNat 8) -- 5
1235+
#reduce (Int.negSucc 3) % (Int.ofNat 8) -- 4
1236+
#reduce (Int.negSucc 4) % (Int.ofNat 8) -- 3
1237+
#reduce (Int.negSucc 5) % (Int.ofNat 8) -- 2
1238+
#reduce (Int.negSucc 6) % (Int.ofNat 8) -- 1
1239+
#reduce (Int.negSucc 7) % (Int.ofNat 8) -- 0
1240+
#reduce (Int.negSucc 8) % (Int.ofNat 8) -- 7
1241+
#reduce (Int.negSucc 9) % (Int.ofNat 8) -- 6
1242+
#reduce (Int.negSucc 10) % (Int.ofNat 8) -- 5
1243+
#reduce (Int.negSucc 11) % (Int.ofNat 8) -- 4
1244+
#reduce (Int.negSucc 12) % (Int.ofNat 8) -- 3
1245+
#reduce (Int.negSucc 13) % (Int.ofNat 8) -- 2
1246+
#reduce (Int.negSucc 14) % (Int.ofNat 8) -- 1
1247+
#reduce (Int.negSucc 15) % (Int.ofNat 8) -- 0
1248+
#reduce (Int.negSucc 16) % (Int.ofNat 8) -- 1
1249+
#reduce (Int.negSucc 17) % (Int.ofNat 8) -- 2
1250+
#reduce (Int.negSucc 18) % (Int.ofNat 8)
1251+
1252+
#check Nat.emod_pos_of_not_dvd
12311253

12321254
@[simp] theorem toInt_sshiftRight (x : BitVec n) (i : Nat) :
12331255
(x.sshiftRight i).toInt = (x.toInt >>> i).bmod (2^n) := by
@@ -1269,6 +1291,30 @@ case inr h =>
12691291
-- we know that x.msb = true by 'h'.
12701292
sorry
12711293

1294+
@[simp]
1295+
theorem toInt_zero : (0#w).toInt = 0 := by
1296+
simp [toInt_eq_toNat_bmod]
1297+
1298+
@[simp]
1299+
theorem toInt_neg (x : BitVec w) :
1300+
(-x).toInt = (((((2 : Nat) ^ w) - x.toNat) : Nat) : Int).bmod (2 ^ w) := by
1301+
simp [toInt_eq_toNat_bmod]
1302+
1303+
1304+
theorem ofInt_ofNat (n : Nat) : BitVec.ofInt w (Int.ofNat n) = n#w := by
1305+
apply eq_of_toNat_eq
1306+
simp
1307+
1308+
@[simp] theorem getLsb_ofInt (n : Nat) (x : Int) (i : Nat) :
1309+
getLsb (BitVec.ofInt n x) i = (i < n && x.testBit i) := by
1310+
cases x
1311+
case ofNat x =>
1312+
rw [ofInt_ofNat]
1313+
simp
1314+
rw [getLsb_ofNat]
1315+
case negSucc x =>
1316+
sorry
1317+
12721318
theorem getLsb_sshiftRight (x : BitVec n) (s i : Nat) :
12731319
getLsb (x.sshiftRight s) i =
12741320
if i ≥ n then false

0 commit comments

Comments
 (0)