@@ -729,4 +729,105 @@ theorem Iter.all_eq_not_any_not {α β : Type w} [Iterator α Id β]
729729 · simp [ihs ‹_›]
730730 · simp
731731
732+ theorem Iter.findSomeM ?_eq_match_step {α β : Type w} {γ : Type x} {m : Type x → Type w'} [Monad m]
733+ [Iterator α Id β] [IteratorLoop α Id m] [LawfulMonad m] [Finite α Id] [LawfulIteratorLoop α Id m]
734+ {it : Iter (α := α) β} {f : β → m (Option γ)} :
735+ it.findSomeM? f = (do
736+ match it.step.val with
737+ | .yield it' out =>
738+ match ← f out with
739+ | none => it'.findSomeM? f
740+ | some fx => return (some fx)
741+ | .skip it' => it'.findSomeM? f
742+ | .done => return none) := by
743+ rw [findSomeM?, forIn_eq_match_step]
744+ cases it.step using PlausibleIterStep.casesOn
745+ · simp only [bind_assoc]
746+ apply bind_congr; intro fx
747+ split <;> simp [findSomeM?]
748+ · simp [findSomeM?]
749+ · simp
750+
751+ theorem Iter.findSome ?_eq_findSomeM ? {α β : Type w} {γ : Type x}
752+ [Iterator α Id β] [IteratorLoop α Id Id] [Finite α Id]
753+ {it : Iter (α := α) β} {f : β → Option γ} :
754+ it.findSome? f = Id.run (it.findSomeM? (pure <| f ·)) :=
755+ (rfl)
756+
757+ theorem Iter.findSome ?_eq_findSome ?_toIterM {α β γ : Type w}
758+ [Iterator α Id β] [IteratorLoop α Id Id.{w}] [Finite α Id]
759+ {it : Iter (α := α) β} {f : β → Option γ} :
760+ it.findSome? f = (it.toIterM.findSome? f).run :=
761+ (rfl)
762+
763+ theorem Iter.findSome ?_eq_match_step {α β : Type w} {γ : Type x}
764+ [Iterator α Id β] [IteratorLoop α Id Id] [LawfulMonad Id] [Finite α Id]
765+ [LawfulIteratorLoop α Id Id] {it : Iter (α := α) β} {f : β → Option γ} :
766+ it.findSome? f = (match it.step.val with
767+ | .yield it' out =>
768+ match f out with
769+ | none => it'.findSome? f
770+ | some fx => some fx
771+ | .skip it' => it'.findSome? f
772+ | .done => none) := by
773+ rw [findSome?_eq_findSomeM?, findSomeM?_eq_match_step]
774+ split
775+ · simp only [pure_bind, findSome?_eq_findSomeM?]
776+ split <;> simp
777+ · simp [findSome?_eq_findSomeM?]
778+ · simp
779+
780+ theorem Iter.findM ?_eq_findSomeM ? {α β : Type w} {m : Type w → Type w'} [Monad m]
781+ [Iterator α Id β] [IteratorLoop α Id m] [Finite α Id]
782+ {it : Iter (α := α) β} {f : β → m (ULift Bool)} :
783+ it.findM? f = it.findSomeM? (fun x => return if (← f x).down then some x else none) :=
784+ (rfl)
785+
786+ theorem Iter.findM ?_eq_match_step {α β : Type w} {m : Type w → Type w'} [Monad m]
787+ [Iterator α Id β] [IteratorLoop α Id m] [LawfulMonad m] [Finite α Id] [LawfulIteratorLoop α Id m]
788+ {it : Iter (α := α) β} {f : β → m (ULift Bool)} :
789+ it.findM? f = (do
790+ match it.step.val with
791+ | .yield it' out =>
792+ if (← f out).down then return (some out) else it'.findM? f
793+ | .skip it' => it'.findM? f
794+ | .done => return none) := by
795+ rw [findM?_eq_findSomeM?, findSomeM?_eq_match_step]
796+ split
797+ · simp only [bind_assoc]
798+ apply bind_congr; intro fx
799+ split <;> simp [findM?_eq_findSomeM?]
800+ · simp [findM?_eq_findSomeM?]
801+ · simp
802+
803+ theorem Iter.find ?_eq_findM ? {α β : Type w} [Iterator α Id β]
804+ [IteratorLoop α Id Id] [Finite α Id] {it : Iter (α := α) β} {f : β → Bool} :
805+ it.find? f = Id.run (it.findM? (pure <| .up <| f ·)) :=
806+ (rfl)
807+
808+ theorem Iter.find ?_eq_find ?_toIterM {α β : Type w} [Iterator α Id β]
809+ [IteratorLoop α Id Id] [Finite α Id] {it : Iter (α := α) β} {f : β → Bool} :
810+ it.find? f = (it.toIterM.find? f).run :=
811+ (rfl)
812+
813+ theorem Iter.find ?_eq_findSome ? {α β : Type w} [Iterator α Id β]
814+ [IteratorLoop α Id Id] [Finite α Id] {it : Iter (α := α) β} {f : β → Bool} :
815+ it.find? f = it.findSome? (fun x => if f x then some x else none) := by
816+ simp [find?_eq_findM?, findSome?_eq_findSomeM?, findM?_eq_findSomeM?]
817+
818+ theorem Iter.find ?_eq_match_step {α β : Type w}
819+ [Iterator α Id β] [IteratorLoop α Id Id] [Finite α Id] [LawfulIteratorLoop α Id Id]
820+ {it : Iter (α := α) β} {f : β → Bool} :
821+ it.find? f = (match it.step.val with
822+ | .yield it' out =>
823+ if f out then some out else it'.find? f
824+ | .skip it' => it'.find? f
825+ | .done => none) := by
826+ rw [find?_eq_findM?, findM?_eq_match_step]
827+ split
828+ · simp only [pure_bind]
829+ split <;> simp [find?_eq_findM?]
830+ · simp [find?_eq_findM?]
831+ · simp
832+
732833end Std.Iterators
0 commit comments