Skip to content

Commit 90389a8

Browse files
authored
feat: improvements to grind annotations for Fin (#11299)
This PR add many `@[grind]` annotations for `Fin`, and updates the tests.
1 parent 26b435f commit 90389a8

File tree

8 files changed

+686
-421
lines changed

8 files changed

+686
-421
lines changed

src/Init/Data/BitVec/Lemmas.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,7 @@ theorem toInt_setWidth' {m n : Nat} (p : m ≤ n) {x : BitVec m} :
10561056
@[simp, grind =] theorem toFin_setWidth' {m n : Nat} (p : m ≤ n) (x : BitVec m) :
10571057
(setWidth' p x).toFin = x.toFin.castLE (Nat.pow_le_pow_right (by omega) (by omega)) := by
10581058
ext
1059-
rw [setWidth'_eq, toFin_setWidth, Fin.val_ofNat, Fin.coe_castLE, val_toFin,
1059+
rw [setWidth'_eq, toFin_setWidth, Fin.val_ofNat, Fin.val_castLE, val_toFin,
10601060
Nat.mod_eq_of_lt (by apply BitVec.toNat_lt_twoPow_of_le p)]
10611061

10621062
theorem toNat_setWidth_of_le {w w' : Nat} {b : BitVec w} (h : w ≤ w') : (b.setWidth w').toNat = b.toNat := by

src/Init/Data/Fin/Basic.lean

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,11 @@ instance neg (n : Nat) : Neg (Fin n) :=
246246

247247
theorem neg_def (a : Fin n) : -a = ⟨(n - a) % n, Nat.mod_lt _ a.pos⟩ := rfl
248248

249+
-- Later we give another version called `Fin.val_neg` that splits on `a = 0`.
250+
protected theorem val_neg' (a : Fin n) : ((-a : Fin n) : Nat) = (n - a) % n :=
251+
rfl
252+
253+
@[deprecated Fin.val_neg' (since := "2025-11-21")]
249254
protected theorem coe_neg (a : Fin n) : ((-a : Fin n) : Nat) = (n - a) % n :=
250255
rfl
251256

src/Init/Data/Fin/Lemmas.lean

Lines changed: 81 additions & 38 deletions
Large diffs are not rendered by default.

src/Init/GrindInstances/ToInt.lean

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ instance : ToInt (Fin n) (.co 0 n) where
108108
toInt_inj x y w := Fin.eq_of_val_eq (Int.ofNat_inj.mp w)
109109
toInt_mem := by simp
110110

111-
@[simp] theorem toInt_fin (x : Fin n) : ToInt.toInt x = (x.val : Int) := rfl
111+
@[simp, grind =] theorem toInt_fin (x : Fin n) : ToInt.toInt x = (x.val : Int) := rfl
112112

113113
instance [NeZero n] : ToInt.Zero (Fin n) (.co 0 n) where
114114
toInt_zero := rfl

src/Init/Prelude.lean

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2264,6 +2264,7 @@ structure Fin (n : Nat) where
22642264
isLt : LT.lt val n
22652265

22662266
attribute [coe] Fin.val
2267+
grind_pattern Fin.isLt => self.val
22672268

22682269
theorem Fin.eq_of_val_eq {n} : ∀ {i j : Fin n}, Eq i.val j.val → Eq i j
22692270
| ⟨_, _⟩, ⟨_, _⟩, rfl => rfl

0 commit comments

Comments
 (0)