@@ -748,6 +748,20 @@ theorem Iter.findSomeM?_eq_match_step {α β : Type w} {γ : Type x} {m : Type x
748748 · simp [findSomeM?]
749749 · simp
750750
751+ theorem Iter.findSomeM ?_toList {α β : Type w} {γ : Type x} {m : Type x → Type w'} [Monad m]
752+ [Iterator α Id β] [IteratorLoop α Id m] [IteratorCollect α Id Id]
753+ [LawfulMonad m] [Finite α Id] [LawfulIteratorLoop α Id m] [LawfulIteratorCollect α Id Id]
754+ {it : Iter (α := α) β} {f : β → m (Option γ)} :
755+ it.toList.findSomeM? f = it.findSomeM? f := by
756+ induction it using Iter.inductSteps with | step it ihy ihs
757+ rw [it.findSomeM?_eq_match_step, it.toList_eq_match_step]
758+ cases it.step using PlausibleIterStep.casesOn
759+ · simp only [List.findSomeM?_cons]
760+ apply bind_congr; intro fx
761+ split <;> simp [ihy ‹_›]
762+ · simp [ihs ‹_›]
763+ · simp
764+
751765theorem Iter.findSome ?_eq_findSomeM ? {α β : Type w} {γ : Type x}
752766 [Iterator α Id β] [IteratorLoop α Id Id] [Finite α Id]
753767 {it : Iter (α := α) β} {f : β → Option γ} :
@@ -761,7 +775,7 @@ theorem Iter.findSome?_eq_findSome?_toIterM {α β γ : Type w}
761775 (rfl)
762776
763777theorem Iter.findSome ?_eq_match_step {α β : Type w} {γ : Type x}
764- [Iterator α Id β] [IteratorLoop α Id Id] [LawfulMonad Id] [ Finite α Id]
778+ [Iterator α Id β] [IteratorLoop α Id Id] [Finite α Id]
765779 [LawfulIteratorLoop α Id Id] {it : Iter (α := α) β} {f : β → Option γ} :
766780 it.findSome? f = (match it.step.val with
767781 | .yield it' out =>
@@ -777,6 +791,21 @@ theorem Iter.findSome?_eq_match_step {α β : Type w} {γ : Type x}
777791 · simp [findSome?_eq_findSomeM?]
778792 · simp
779793
794+ theorem Iter.findSome ?_toList {α β : Type w} {γ : Type x}
795+ [Iterator α Id β] [IteratorLoop α Id Id] [IteratorCollect α Id Id]
796+ [Finite α Id] [LawfulIteratorLoop α Id Id] [LawfulIteratorCollect α Id Id]
797+ {it : Iter (α := α) β} {f : β → Option γ} :
798+ it.toList.findSome? f = it.findSome? f := by
799+ simp [findSome?_eq_findSomeM?, List.findSome?_eq_findSomeM?, findSomeM?_toList]
800+
801+ theorem Iter.findSomeM ?_pure {α β : Type w} {γ : Type x} {m : Type x → Type w'} [Monad m]
802+ [Iterator α Id β] [IteratorLoop α Id m] [IteratorLoop α Id Id]
803+ [LawfulMonad m] [Finite α Id] [LawfulIteratorLoop α Id m] [LawfulIteratorLoop α Id Id]
804+ {it : Iter (α := α) β} {f : β → Option γ} :
805+ it.findSomeM? (pure <| f ·) = pure (f := m) (it.findSome? f) := by
806+ letI : IteratorCollect α Id Id := .defaultImplementation
807+ simp [← findSomeM?_toList, ← findSome?_toList, List.findSomeM?_pure]
808+
780809theorem Iter.findM ?_eq_findSomeM ? {α β : Type w} {m : Type w → Type w'} [Monad m]
781810 [Iterator α Id β] [IteratorLoop α Id m] [Finite α Id]
782811 {it : Iter (α := α) β} {f : β → m (ULift Bool)} :
@@ -800,6 +829,20 @@ theorem Iter.findM?_eq_match_step {α β : Type w} {m : Type w → Type w'} [Mon
800829 · simp [findM?_eq_findSomeM?]
801830 · simp
802831
832+ theorem Iter.findM ?_toList {α β : Type } {m : Type → Type w'} [Monad m]
833+ [Iterator α Id β] [IteratorLoop α Id m] [IteratorCollect α Id Id]
834+ [LawfulMonad m] [Finite α Id] [LawfulIteratorLoop α Id m] [LawfulIteratorCollect α Id Id]
835+ {it : Iter (α := α) β} {f : β → m Bool} :
836+ it.toList.findM? f = it.findM? (.up <$> f ·) := by
837+ simp [findM?_eq_findSomeM?, List.findM?_eq_findSomeM?, findSomeM?_toList]
838+
839+ theorem Iter.findM ?_eq_findM ?_toList {α β : Type } {m : Type → Type w'} [Monad m]
840+ [Iterator α Id β] [IteratorLoop α Id m] [IteratorCollect α Id Id]
841+ [LawfulMonad m] [Finite α Id] [LawfulIteratorLoop α Id m] [LawfulIteratorCollect α Id Id]
842+ {it : Iter (α := α) β} {f : β → m (ULift Bool)} :
843+ it.findM? f = it.toList.findM? (ULift.down <$> f ·) := by
844+ simp [findM?_toList]
845+
803846theorem Iter.find ?_eq_findM ? {α β : Type w} [Iterator α Id β]
804847 [IteratorLoop α Id Id] [Finite α Id] {it : Iter (α := α) β} {f : β → Bool} :
805848 it.find? f = Id.run (it.findM? (pure <| .up <| f ·)) :=
@@ -830,4 +873,26 @@ theorem Iter.find?_eq_match_step {α β : Type w}
830873 · simp [find?_eq_findM?]
831874 · simp
832875
876+ theorem Iter.find ?_toList {α β : Type w}
877+ [Iterator α Id β] [IteratorLoop α Id Id] [IteratorCollect α Id Id]
878+ [Finite α Id] [LawfulIteratorLoop α Id Id] [LawfulIteratorCollect α Id Id]
879+ {it : Iter (α := α) β} {f : β → Bool} :
880+ it.toList.find? f = it.find? f := by
881+ simp [find?_eq_findSome?, List.find?_eq_findSome?_guard, findSome?_toList, Option.guard_def]
882+
883+ theorem Iter.findM ?_pure {α β : Type w} {m : Type w → Type w'} [Monad m]
884+ [Iterator α Id β] [IteratorLoop α Id m] [IteratorLoop α Id Id]
885+ [LawfulMonad m] [Finite α Id] [LawfulIteratorLoop α Id m] [LawfulIteratorLoop α Id Id]
886+ {it : Iter (α := α) β} {f : β → ULift Bool} :
887+ it.findM? (pure (f := m) <| f ·) = pure (f := m) (it.find? (ULift.down <| f ·)) := by
888+ induction it using Iter.inductSteps with | step it ihy ihs
889+ rw [findM?_eq_match_step, find?_eq_match_step]
890+ cases it.step using PlausibleIterStep.casesOn
891+ · simp only [pure_bind]
892+ split
893+ · simp
894+ · simp [ihy ‹_›]
895+ · simp [ihs ‹_›]
896+ · simp
897+
833898end Std.Iterators
0 commit comments