Skip to content

Commit 8f9e0d4

Browse files
committed
more lemmas
1 parent 3dbc68e commit 8f9e0d4

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed

src/Init/Data/Iterators/Lemmas/Consumers/Loop.lean

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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+
751765
theorem 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

763777
theorem 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+
780809
theorem 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 : TypeType 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 : TypeType 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+
803846
theorem 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+
833898
end Std.Iterators

src/Init/Data/Iterators/Lemmas/Consumers/Monadic/Loop.lean

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,20 @@ theorem IterM.findSome?_eq_match_step {α β γ : Type w} {m : Type w → Type w
554554
apply bind_congr; intro step
555555
split <;> simp [findSome?_eq_findSomeM?]
556556

557+
theorem IterM.findSomeM?_pure {α β γ : Type w} {m : Type w → Type w'} [Monad m]
558+
[Iterator α m β] [IteratorLoop α m m]
559+
[LawfulMonad m] [Finite α m] [LawfulIteratorLoop α m m]
560+
{it : IterM (α := α) m β} {f : β → Option γ} :
561+
it.findSomeM? (pure <| f ·) = it.findSome? f := by
562+
induction it using IterM.inductSteps with | step it ihy ihs
563+
rw [findSomeM?_eq_match_step, findSome?_eq_match_step]
564+
apply bind_congr; intro step
565+
cases step using PlausibleIterStep.casesOn
566+
· simp only [pure_bind]
567+
split <;> simp [ihy ‹_›]
568+
· simp [ihs ‹_›]
569+
· simp
570+
557571
theorem IterM.findM?_eq_findSomeM? {α β : Type w} {m : Type w → Type w'} [Monad m]
558572
[Iterator α m β] [IteratorLoop α m m] [Finite α m]
559573
{it : IterM (α := α) m β} {f : β → m (ULift Bool)} :
@@ -605,4 +619,20 @@ theorem IterM.find?_eq_match_step {α β : Type w} {m : Type w → Type w'} [Mon
605619
· simp [find?_eq_findM?]
606620
· simp
607621

622+
theorem IterM.findM?_pure {α β : Type w} {m : Type w → Type w'} [Monad m]
623+
[Iterator α m β] [IteratorLoop α m m]
624+
[LawfulMonad m] [Finite α m] [LawfulIteratorLoop α m m]
625+
{it : IterM (α := α) m β} {f : β → ULift Bool} :
626+
it.findM? (pure (f := m) <| f ·) = it.find? (ULift.down <| f ·) := by
627+
induction it using IterM.inductSteps with | step it ihy ihs
628+
rw [findM?_eq_match_step, find?_eq_match_step]
629+
apply bind_congr; intro step
630+
cases step using PlausibleIterStep.casesOn
631+
· simp only [pure_bind]
632+
split
633+
· simp
634+
· simp [ihy ‹_›]
635+
· simp [ihs ‹_›]
636+
· simp
637+
608638
end Std.Iterators

src/Init/Data/List/Control.lean

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,21 @@ def findSomeM? {m : Type u → Type v} [Monad m] {α : Type w} {β : Type u} (f
405405
| some b => pure (some b)
406406
| none => findSomeM? f as
407407

408+
@[simp, grind =]
409+
theorem findSomeM?_nil [Monad m] {α : Type w} {β : Type u}
410+
{f : α → m (Option β)} :
411+
([] : List α).findSomeM? f = pure none :=
412+
(rfl)
413+
414+
@[grind =]
415+
theorem findSomeM?_cons [Monad m] {α : Type w} {β : Type u}
416+
{f : α → m (Option β)} {a : α} {as : List α} :
417+
(a::as).findSomeM? f = (do
418+
match ← f a with
419+
| some b => return some b
420+
| none => as.findSomeM? f) :=
421+
(rfl)
422+
408423
@[simp]
409424
theorem findSomeM?_pure [Monad m] [LawfulMonad m] {f : α → Option β} {as : List α} :
410425
findSomeM? (m := m) (pure <| f ·) as = pure (as.findSome? f) := by
@@ -426,6 +441,10 @@ theorem findSomeM?_id (f : α → Id (Option β)) (as : List α) :
426441
findSomeM? (m := Id) f as = as.findSome? f :=
427442
findSomeM?_pure
428443

444+
theorem findSome?_eq_findSomeM? {f : α → Option β} {as : List α} :
445+
as.findSome? f = (as.findSomeM? (pure (f := Id) <| f ·)).run := by
446+
simp
447+
429448
theorem findM?_eq_findSomeM? [Monad m] [LawfulMonad m] {p : α → m Bool} {as : List α} :
430449
as.findM? p = as.findSomeM? fun a => return if (← p a) then some a else none := by
431450
induction as with

0 commit comments

Comments
 (0)