feat: LawfulBEq instances for Array and Vector (#6922)

This PR adds `LawfulBEq` instances for `Array` and `Vector`.

(Note this replaces a contribution of @mehbark to Batteries for the
LawfulBEq instance for Vector, which was dropped during the release
process due to conflicts. Thanks for that contribution!)
This commit is contained in:
Kim Morrison 2025-02-04 00:44:25 +11:00 committed by GitHub
parent 8f5418dbda
commit 030daffba6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 22 additions and 5 deletions

View file

@ -108,3 +108,13 @@ namespace List
simp [beq_eq_decide, Array.beq_eq_decide]
end List
namespace Array
instance [BEq α] [LawfulBEq α] : LawfulBEq (Array α) where
rfl := by simp [BEq.beq, isEqv_self_beq]
eq_of_beq := by
rintro ⟨a⟩ ⟨b⟩ h
simpa using h
end Array

View file

@ -49,10 +49,20 @@ theorem beq_eq_decide [BEq α] (a b : Vector α n) :
(a == b) = decide (∀ (i : Nat) (h' : i < n), a[i] == b[i]) := by
simp [BEq.beq, isEqv_eq_decide]
@[simp] theorem beq_mk [BEq α] (a b : Array α) (ha : a.size = n) (hb : b.size = n) :
(mk a ha == mk b hb) = (a == b) := by
simp [BEq.beq]
@[simp] theorem beq_toArray [BEq α] (a b : Vector α n) : (a.toArray == b.toArray) = (a == b) := by
simp [beq_eq_decide, Array.beq_eq_decide]
@[simp] theorem beq_toList [BEq α] (a b : Vector α n) : (a.toList == b.toList) = (a == b) := by
simp [beq_eq_decide, List.beq_eq_decide]
instance [BEq α] [LawfulBEq α] : LawfulBEq (Vector α n) where
rfl := by simp [BEq.beq, isEqv_self_beq]
eq_of_beq := by
rintro ⟨a, rfl⟩ ⟨b, h⟩ h'
simpa using h'
end Vector

View file

@ -1289,12 +1289,9 @@ theorem mem_setIfInBounds (v : Vector α n) (i : Nat) (hi : i < n) (a : α) :
· intro h
constructor
· rintro ⟨a, ha⟩ ⟨b, hb⟩ h
simp at h
obtain ⟨hs, hi⟩ := Array.isEqv_iff_rel.mp h
ext i h
· simpa using hi _ (by omega)
simp_all
· rintro ⟨a, ha⟩
simpa using Array.isEqv_self_beq ..
simp
/-! ### isEqv -/