Skip to content

Commit 2fb3dbc

Browse files
committed
fix bitvec proof
1 parent 62ef4bb commit 2fb3dbc

File tree

1 file changed

+14
-28
lines changed

1 file changed

+14
-28
lines changed

Auto/Embedding/LamBitVec.lean

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -46,27 +46,13 @@ namespace BVLems
4646
theorem sub_def (a b : BitVec n) : a - b = a.sub b := rfl
4747

4848
theorem toNat_shiftLeft {a : BitVec n} (i : Nat) : (a <<< i).toNat = (a.toNat * (2 ^ i)) % (2 ^ n) := by
49-
rw [shiftLeft_def]; rcases a with ⟨⟨a, isLt⟩⟩
50-
unfold BitVec.shiftLeft BitVec.toNat BitVec.ofNat
51-
dsimp; rw [Nat.shiftLeft_eq]
49+
rw [BitVec.toNat_shiftLeft, Nat.shiftLeft_eq]
5250

5351
theorem toNat_ushiftRight {a : BitVec n} (i : Nat) : (a >>> i).toNat = (a.toNat) / (2 ^ i) := by
54-
rw [ushiftRight_def]; rcases a with ⟨⟨a, isLt⟩⟩
55-
unfold BitVec.ushiftRight BitVec.toNat
56-
dsimp; rw [Nat.shiftRight_eq_div_pow]
52+
rw [BitVec.toNat_ushiftRight, Nat.shiftRight_eq_div_pow]
5753

58-
theorem toNat_zeroExtend' {a : BitVec n} (le : n ≤ m) : (a.setWidth' le).toNat = a.toNat := rfl
59-
60-
theorem toNat_zeroExtend {a : BitVec n} (i : Nat) : (a.zeroExtend i).toNat = a.toNat % (2 ^ i) := by
61-
unfold BitVec.zeroExtend BitVec.setWidth; cases hdec : decide (n ≤ i)
62-
case false =>
63-
have hnle := of_decide_eq_false hdec
64-
rw [Bool.dite_eq_false (proof:=hnle)]; rfl
65-
case true =>
66-
have hle := of_decide_eq_true hdec
67-
rw [Bool.dite_eq_true (proof:=hle), toNat_zeroExtend']
68-
rw [Nat.mod_eq_of_lt]; rcases a with ⟨⟨a, isLt⟩⟩;
69-
apply Nat.le_trans isLt; apply Nat.pow_le_pow_right (Nat.lt_succ_of_lt .refl) hle
54+
theorem toNat_zeroExtend {n} {a : BitVec n} (i : Nat) : (a.zeroExtend i).toNat = a.toNat % (2 ^ i) :=
55+
BitVec.toNat_setWidth i a
7056

7157
theorem toNat_sub (a b : BitVec n) : (a - b).toNat = (2 ^ n - b.toNat + a.toNat) % (2 ^ n) := rfl
7258

@@ -172,7 +158,7 @@ namespace BVLems
172158
rw [Bool.ite_eq_true _ _ _ hle, smtshiftLeft_def, toNat_ofNat, Nat.mod_eq_of_lt]
173159
apply Nat.le_trans hle (Nat.le_of_lt (Nat.le_pow .refl))
174160

175-
theorem shl_equiv'.{u} (a : BitVec n) (b : Nat) : a <<< b =
161+
theorem shl_equiv_GLift.{u} (a : BitVec n) (b : Nat) : a <<< b =
176162
(Bool.ite' (b < n) (GLift.up.{1, u} (a <<< BitVec.ofNat n b)) (GLift.up.{1, u} 0)).down := by
177163
have h := shl_equiv a b; rw [Bool.ite_simp] at h; rw [Bool.ite'_comm (f := GLift.down)]; exact h
178164

@@ -215,7 +201,7 @@ namespace BVLems
215201
rw [Bool.ite_eq_true _ _ _ hle, smtushiftRight_def, toNat_ofNat, Nat.mod_eq_of_lt]
216202
apply Nat.le_trans hle (Nat.le_of_lt (Nat.le_pow .refl))
217203

218-
theorem lshr_equiv'.{u} (a : BitVec n) (b : Nat) : a >>> b =
204+
theorem lshr_equiv_GLift.{u} (a : BitVec n) (b : Nat) : a >>> b =
219205
(Bool.ite' (b < n) (GLift.up.{1, u} (a >>> BitVec.ofNat n b)) (GLift.up.{1, u} 0)).down := by
220206
have h := lshr_equiv a b; rw [Bool.ite_simp] at h; rw [Bool.ite'_comm (f := GLift.down)]; exact h
221207

@@ -242,7 +228,7 @@ namespace BVLems
242228
rw [smtushiftRight_def, eqof, shiftRight_eq_zero_iff] at heq
243229
rw [toNat_zeroExtend, Nat.mod_eq_of_lt heq]
244230

245-
theorem lshr_toNat_equiv_long'.{u} (a : BitVec n) (b : BitVec m) (h : m > n) : a >>> b.toNat =
231+
theorem lshr_toNat_equiv_long_GLift.{u} (a : BitVec n) (b : BitVec m) (h : m > n) : a >>> b.toNat =
246232
(Bool.ite' (GLift.up.{1, u} (b >>> (BitVec.ofNat m n)) = GLift.up.{1, u} 0#m)
247233
(GLift.up.{1, u} (a >>> (zeroExtend n b))) (GLift.up.{1, u} 0)).down := by
248234
have h := lshr_toNat_equiv_long a b h; rw [Bool.ite_simp] at h
@@ -258,7 +244,7 @@ namespace BVLems
258244
rw [Bool.ite_eq_true _ _ _ hle, toNat_ofNat, Nat.mod_eq_of_lt]
259245
apply Nat.le_trans hle (Nat.le_of_lt (Nat.le_pow .refl))
260246

261-
theorem ashr_equiv'.{u} (a : BitVec n) (b : Nat) : a.sshiftRight b =
247+
theorem ashr_equiv_GLift.{u} (a : BitVec n) (b : Nat) : a.sshiftRight b =
262248
(Bool.ite' (b < n) (GLift.up.{1, u} (a.sshiftRight (BitVec.ofNat n b).toNat))
263249
(Bool.ite' (GLift.up.{1, u} a.msb = GLift.up.{1, u} true) (GLift.up.{1, u} (1#n).neg) (GLift.up.{1, u} 0#n))).down := by
264250
have h := ashr_equiv a b; rw [Bool.ite_simp] at h; rw [eqGLift_equiv]
@@ -287,7 +273,7 @@ namespace BVLems
287273
rw [smtushiftRight_def, eqof, shiftRight_eq_zero_iff] at heq
288274
rw [toNat_zeroExtend, Nat.mod_eq_of_lt heq]
289275

290-
theorem ashr_toNat_equiv_long'.{u} (a : BitVec n) (b : BitVec m) (h : m > n) : a.sshiftRight b.toNat =
276+
theorem ashr_toNat_equiv_long_GLift.{u} (a : BitVec n) (b : BitVec m) (h : m > n) : a.sshiftRight b.toNat =
291277
(Bool.ite' (GLift.up.{1, u} (b >>> (BitVec.ofNat m n)) = GLift.up.{1, u} 0#m)
292278
(GLift.up.{1, u} (a.sshiftRight (zeroExtend n b).toNat))
293279
(Bool.ite'
@@ -377,7 +363,7 @@ theorem LamEquiv.shl_equiv
377363
⟨.mkBvNatBinOp (.ofBvshl _) wfa wfb,
378364
.mkIte (.mkNatBinOp (.ofNlt) wfb (.ofBase (.ofNatVal n)))
379365
(.mkBvBinOp (.ofBvsmtshl _) wfa (.mkBvofNat wfb)) (.ofBase (.ofBvVal _ _)), fun lctxTerm => by
380-
apply GLift.down.inj; apply BVLems.shl_equiv'
366+
apply GLift.down.inj; apply BVLems.shl_equiv_GLift
381367

382368
abbrev LamTerm.shl_toNat_equiv_short (n : Nat) (a : LamTerm) (m : Nat) (b : LamTerm) :=
383369
LamTerm.mkBvBinOp n (.bvsmtshl n) a (.mkBvUOp m (.bvzeroExtend m n) b)
@@ -469,7 +455,7 @@ theorem LamEquiv.lshr_equiv
469455
⟨.mkBvNatBinOp (.ofBvlshr _) wfa wfb,
470456
.mkIte (.mkNatBinOp (.ofNlt) wfb (.ofBase (.ofNatVal n)))
471457
(.mkBvBinOp (.ofBvsmtlshr _) wfa (.mkBvofNat wfb)) (.ofBase (.ofBvVal _ _)), fun lctxTerm => by
472-
apply GLift.down.inj; apply BVLems.lshr_equiv'
458+
apply GLift.down.inj; apply BVLems.lshr_equiv_GLift
473459

474460
abbrev LamTerm.lshr_toNat_equiv_short (n : Nat) (a : LamTerm) (m : Nat) (b : LamTerm) :=
475461
LamTerm.mkBvBinOp n (.bvsmtlshr n) a (.mkBvUOp m (.bvzeroExtend m n) b)
@@ -530,7 +516,7 @@ theorem LamEquiv.lshr_toNat_equiv_long
530516
.mkIte
531517
(.mkEq (.mkBvBinOp (.ofBvsmtlshr _) wfb (.ofBase (.ofBvVal _ _))) (.ofBase (.ofBvVal _ _)))
532518
(.mkBvBinOp (.ofBvsmtlshr _) wfa (.mkBvUOp (.ofBvzeroExtend _ _) wfb)) (.ofBase (.ofBvVal _ _)), fun lctxTerm => by
533-
apply GLift.down.inj; apply BVLems.lshr_toNat_equiv_long' _ _ h⟩
519+
apply GLift.down.inj; apply BVLems.lshr_toNat_equiv_long_GLift _ _ h⟩
534520

535521
def LamTerm.ashr_equiv (n : Nat) (a b bbv : LamTerm) :=
536522
LamTerm.mkIte (.base (.bv n)) (.mkNatBinOp .nlt b (.base (.natVal n)))
@@ -569,7 +555,7 @@ theorem LamEquiv.ashr_equiv
569555
(.mkIte (.mkEq (.mkBvUOp (.ofBvmsb _) wfa) (.ofBase .ofTrueB))
570556
(.mkBvUOp (.ofBvneg _) ((.ofBase (.ofBvVal _ _))))
571557
(.ofBase (.ofBvVal _ _))), fun lctxTerm => by
572-
apply GLift.down.inj; apply BVLems.ashr_equiv'
558+
apply GLift.down.inj; apply BVLems.ashr_equiv_GLift
573559

574560
abbrev LamTerm.ashr_toNat_equiv_short (n : Nat) (a : LamTerm) (m : Nat) (b : LamTerm) :=
575561
LamTerm.mkBvBinOp n (.bvsmtashr n) a (.mkBvUOp m (.bvzeroExtend m n) b)
@@ -636,7 +622,7 @@ theorem LamEquiv.ashr_toNat_equiv_long
636622
(.mkIte (.mkEq (.mkBvUOp (.ofBvmsb _) wfa) (.ofBase .ofTrueB))
637623
(.mkBvUOp (.ofBvneg _) (.ofBase (.ofBvVal _ _)))
638624
(.ofBase (.ofBvVal _ _))), fun lctxTerm => by
639-
apply GLift.down.inj; apply BVLems.ashr_toNat_equiv_long' _ _ h⟩
625+
apply GLift.down.inj; apply BVLems.ashr_toNat_equiv_long_GLift _ _ h⟩
640626

641627
inductive BVCastType where
642628
| ofNat : Nat → BVCastType

0 commit comments

Comments
 (0)