Documentation

CslibFrontier.Foundations.Control.Monad.FindM

Step equations for monadic list search #

Core List.findM? p xs runs the effectful test p on each element of xs from left to right and returns the first element for which p returns true, or none. Core defines List.findM? without exposing its body and has no nil or cons equations for a general monad, so this module imports Init.Data.List.Control with import all, as CslibFrontier.Foundations.Control.Monad.FilterM does, and provides those two equations, List.findM?_nil and List.findM?_cons, and nothing else. Both are general list lemmas destined for Lean core or Batteries.

@[simp]
theorem List.findM?_nil {m : Type → Type u} [Monad m] {α : Type} (p : α → m Bool) :

findM? finds nothing in the empty list.

theorem List.findM?_cons {m : Type → Type u} [Monad m] {α : Type} (p : α → m Bool) (a : α) (as : List α) :
findM? p (a :: as) = do let b ← p a if b = true then pure (some a) else findM? p as

One step of findM?: test the head, and search the tail only when the test fails.