chore: generalize universe in Array.find? (#6318)

This PR generalizes the universe level for `Array.find?`, by giving it a
separate implementation from `Array.findM?`.
This commit is contained in:
Kim Morrison 2024-12-05 17:11:40 +11:00 committed by GitHub
parent 1400b95ffb
commit c366a291ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 20 additions and 5 deletions

View file

@ -21,3 +21,4 @@ import Init.Data.Array.Set
import Init.Data.Array.Monadic
import Init.Data.Array.FinRange
import Init.Data.Array.Perm
import Init.Data.Array.Find

View file

@ -474,6 +474,10 @@ def findSomeM? {α : Type u} {β : Type v} {m : Type v → Type w} [Monad m] (f
| _ => pure ⟨⟩
return none
/--
Note that the universe level is contrained to `Type` here,
to avoid having to have the predicate live in `p : α → m (ULift Bool)`.
-/
@[inline]
def findM? {α : Type} {m : Type → Type} [Monad m] (p : α → m Bool) (as : Array α) : m (Option α) := do
for a in as do
@ -585,8 +589,12 @@ def zipWithIndex (arr : Array α) : Array (α × Nat) :=
arr.mapIdx fun i a => (a, i)
@[inline]
def find? {α : Type} (p : α → Bool) (as : Array α) : Option α :=
Id.run <| as.findM? p
def find? {α : Type u} (p : α → Bool) (as : Array α) : Option α :=
Id.run do
for a in as do
if p a then
return a
return none
@[inline]
def findSome? {α : Type u} {β : Type v} (f : α → Option β) (as : Array α) : Option β :=

View file

@ -230,7 +230,13 @@ theorem findRevM?_toArray [Monad m] [LawfulMonad m] (f : α → m Bool) (l : Lis
@[simp] theorem find?_toArray (f : α → Bool) (l : List α) :
l.toArray.find? f = l.find? f := by
rw [Array.find?, ← findM?_id, findM?_toArray, Id.run]
rw [Array.find?]
simp only [Id.run, Id, Id.pure_eq, Id.bind_eq, forIn_toArray]
induction l with
| nil => simp
| cons a l ih =>
simp only [forIn_cons, Id.pure_eq, Id.bind_eq, find?]
by_cases f a <;> simp_all
theorem isPrefixOfAux_toArray_succ [BEq α] (l₁ l₂ : List α) (hle : l₁.length ≤ l₂.length) (i : Nat) :
Array.isPrefixOfAux l₁.toArray l₂.toArray hle (i + 1) =

View file

@ -354,7 +354,7 @@ theorem containsThenInsert_snd {k : α} : (m.containsThenInsert k).2 = m.insert
ext HashMap.containsThenInsertIfNew_snd
@[simp]
theorem length_toList [EquivBEq α] [LawfulHashable α] :
theorem length_toList [EquivBEq α] [LawfulHashable α] :
m.toList.length = m.size :=
HashMap.length_keys
@ -369,7 +369,7 @@ theorem contains_toList [EquivBEq α] [LawfulHashable α] {k : α}:
HashMap.contains_keys
@[simp]
theorem mem_toList [LawfulBEq α] [LawfulHashable α] {k : α}:
theorem mem_toList [LawfulBEq α] [LawfulHashable α] {k : α} :
k ∈ m.toList ↔ k ∈ m :=
HashMap.mem_keys