Documentation

Init.Data.Iterators.Lemmas.Combinators.FilterMap

theorem Std.Iterators.Iter.filterMapM_eq_toIter_filterMapM_toIterM {α β γ : Type w} [Iterator α Id β] {it : Iter β} {m : Type w → Type w'} [Monad m] {f : βm (Option γ)} :
theorem Std.Iterators.Iter.filterM_eq_toIter_filterM_toIterM {α β : Type w} [Iterator α Id β] {it : Iter β} {m : Type w → Type w'} [Monad m] {f : βm (ULift Bool)} :
theorem Std.Iterators.Iter.mapM_eq_toIter_mapM_toIterM {α β γ : Type w} [Iterator α Id β] {it : Iter β} {m : Type w → Type w'} [Monad m] {f : βm γ} :
theorem Std.Iterators.Iter.map_eq_toIter_map_toIterM {α β γ : Type w} [Iterator α Id β] {it : Iter β} {f : βγ} :
theorem Std.Iterators.Iter.filter_eq_toIter_filter_toIterM {α β : Type w} [Iterator α Id β] {it : Iter β} {m : Type w → Type w'} [Monad m] {f : βBool} :
theorem Std.Iterators.Iter.step_filterMapWithPostcondition {α β γ : Type w} [Iterator α Id β] {it : Iter β} {m : Type w → Type w'} {n : Type w → Type w''} {f : βPostconditionT n (Option γ)} [Monad n] [LawfulMonad n] [MonadLiftT m n] :
theorem Std.Iterators.Iter.step_filterWithPostcondition {α β : Type w} [Iterator α Id β] {it : Iter β} {m : Type w → Type w'} {n : Type w → Type w''} {f : βPostconditionT n (ULift Bool)} [Monad n] [LawfulMonad n] [MonadLiftT m n] :
theorem Std.Iterators.Iter.step_mapWithPostcondition {α β γ : Type w} [Iterator α Id β] {it : Iter β} {m : Type w → Type w'} {n : Type w → Type w''} {f : βPostconditionT n γ} [Monad n] [LawfulMonad n] [MonadLiftT m n] :
theorem Std.Iterators.Iter.step_filterMapM {α β : Type w} [Iterator α Id β] {it : Iter β} {m : Type w → Type w'} {n : Type w → Type w''} {β' : Type w} {f : βn (Option β')} [Monad n] [LawfulMonad n] [MonadLiftT m n] :
(filterMapM f it).step = match it.step with | IterStep.yield it' out, h => do let __do_liftf out match __do_lift with | none => pure (Shrink.deflate (PlausibleIterStep.skip (filterMapM f it') )) | some out' => pure (Shrink.deflate (PlausibleIterStep.yield (filterMapM f it') out' )) | IterStep.skip it', h => pure (Shrink.deflate (PlausibleIterStep.skip (filterMapM f it') )) | IterStep.done, h => pure (Shrink.deflate (PlausibleIterStep.done ))
theorem Std.Iterators.Iter.step_filterM {α β : Type w} [Iterator α Id β] {it : Iter β} {m : Type w → Type w'} {n : Type w → Type w''} {f : βn (ULift Bool)} [Monad n] [LawfulMonad n] [MonadLiftT m n] :
(filterM f it).step = match it.step with | IterStep.yield it' out, h => do let __do_liftf out match __do_lift with | { down := false } => pure (Shrink.deflate (PlausibleIterStep.skip (filterM f it') )) | { down := true } => pure (Shrink.deflate (PlausibleIterStep.yield (filterM f it') out )) | IterStep.skip it', h => pure (Shrink.deflate (PlausibleIterStep.skip (filterM f it') )) | IterStep.done, h => pure (Shrink.deflate (PlausibleIterStep.done ))
theorem Std.Iterators.Iter.step_mapM {α β γ : Type w} [Iterator α Id β] {it : Iter β} {n : Type w → Type w''} {f : βn γ} [Monad n] [LawfulMonad n] :
(mapM f it).step = match it.step with | IterStep.yield it' out, h => do let out'f out pure (Shrink.deflate (PlausibleIterStep.yield (mapM f it') out' )) | IterStep.skip it', h => pure (Shrink.deflate (PlausibleIterStep.skip (mapM f it') )) | IterStep.done, h => pure (Shrink.deflate (PlausibleIterStep.done ))
theorem Std.Iterators.Iter.step_filterMap {α β γ : Type w} [Iterator α Id β] {it : Iter β} {f : βOption γ} :
(filterMap f it).step = match it.step with | IterStep.yield it' out, h => match h' : f out with | none => PlausibleIterStep.skip (filterMap f it') | some out' => PlausibleIterStep.yield (filterMap f it') out' | IterStep.skip it', h => PlausibleIterStep.skip (filterMap f it') | IterStep.done, h => PlausibleIterStep.done
theorem Std.Iterators.Iter.val_step_filterMap {α β γ : Type w} [Iterator α Id β] {it : Iter β} {f : βOption γ} :
(filterMap f it).step.val = match it.step.val with | IterStep.yield it' out => match f out with | none => IterStep.skip (filterMap f it') | some out' => IterStep.yield (filterMap f it') out' | IterStep.skip it' => IterStep.skip (filterMap f it') | IterStep.done => IterStep.done

a weaker version of step_filterMap that does not use dependent match

theorem Std.Iterators.Iter.step_map {α β γ : Type w} [Iterator α Id β] {it : Iter β} {f : βγ} :
(map f it).step = match it.step with | IterStep.yield it' out, h => PlausibleIterStep.yield (map f it') (f out) | IterStep.skip it', h => PlausibleIterStep.skip (map f it') | IterStep.done, h => PlausibleIterStep.done
def Std.Iterators.Iter.step_filter {α β : Type w} [Iterator α Id β] {it : Iter β} {f : βBool} :
(filter f it).step = match it.step with | IterStep.yield it' out, h => if h' : f out = true then PlausibleIterStep.yield (filter f it') out else PlausibleIterStep.skip (filter f it') | IterStep.skip it', h => PlausibleIterStep.skip (filter f it') | IterStep.done, h => PlausibleIterStep.done
Equations
  • =
Instances For
    def Std.Iterators.Iter.val_step_filter {α β : Type w} [Iterator α Id β] {it : Iter β} {f : βBool} :
    (filter f it).step.val = match it.step.val with | IterStep.yield it' out => if f out = true then IterStep.yield (filter f it') out else IterStep.skip (filter f it') | IterStep.skip it' => IterStep.skip (filter f it') | IterStep.done => IterStep.done
    Equations
    • =
    Instances For
      @[simp]
      theorem Std.Iterators.Iter.toList_filterMap {α β γ : Type w} [Iterator α Id β] {it : Iter β} [IteratorCollect α Id Id] [LawfulIteratorCollect α Id Id] [Finite α Id] {f : βOption γ} :
      @[simp]
      theorem Std.Iterators.Iter.toList_map {α β γ : Type w} [Iterator α Id β] {it : Iter β} [IteratorCollect α Id Id] [LawfulIteratorCollect α Id Id] [Finite α Id] {f : βγ} :
      (map f it).toList = List.map f it.toList
      @[simp]
      theorem Std.Iterators.Iter.toList_filter {α β : Type w} [Iterator α Id β] {it : Iter β} [IteratorCollect α Id Id] [LawfulIteratorCollect α Id Id] [Finite α Id] {f : βBool} :
      @[simp]
      @[simp]
      theorem Std.Iterators.Iter.toListRev_map {α β γ : Type w} [Iterator α Id β] {it : Iter β} [IteratorCollect α Id Id] [LawfulIteratorCollect α Id Id] [Finite α Id] {f : βγ} :
      @[simp]
      theorem Std.Iterators.Iter.toListRev_filter {α β : Type w} [Iterator α Id β] {it : Iter β} [IteratorCollect α Id Id] [LawfulIteratorCollect α Id Id] [Finite α Id] {f : βBool} :
      @[simp]
      theorem Std.Iterators.Iter.toArray_filterMap {α β γ : Type w} [Iterator α Id β] {it : Iter β} [IteratorCollect α Id Id] [LawfulIteratorCollect α Id Id] [Finite α Id] {f : βOption γ} :
      @[simp]
      theorem Std.Iterators.Iter.toArray_map {α β γ : Type w} [Iterator α Id β] {it : Iter β} [IteratorCollect α Id Id] [LawfulIteratorCollect α Id Id] [Finite α Id] {f : βγ} :
      @[simp]
      theorem Std.Iterators.Iter.toArray_filter {α β : Type w} [Iterator α Id β] {it : Iter β} [IteratorCollect α Id Id] [LawfulIteratorCollect α Id Id] [Finite α Id] {f : βBool} :
      theorem Std.Iterators.Iter.foldM_filterMapM {α β γ δ : Type w} {m : Type w → Type w'} {n : Type w → Type w''} [Iterator α Id β] [Finite α Id] [Monad m] [Monad n] [LawfulMonad m] [LawfulMonad n] [IteratorLoop α Id Id] [IteratorLoop α Id m] [IteratorLoop α Id n] [MonadLiftT m n] [LawfulMonadLiftT m n] [LawfulIteratorLoop α Id Id] [LawfulIteratorLoop α Id m] [LawfulIteratorLoop α Id n] {f : βm (Option γ)} {g : δγn δ} {init : δ} {it : Iter β} :
      IterM.foldM g init (filterMapM f it) = foldM (fun (d : δ) (b : β) => do let __discrliftM (f b) match __discr with | some c => g d c | x => pure d) init it
      theorem Std.Iterators.Iter.foldM_mapM {α β γ δ : Type w} {m : Type w → Type w'} {n : Type w → Type w''} [Iterator α Id β] [Finite α Id] [Monad m] [Monad n] [LawfulMonad m] [LawfulMonad n] [IteratorLoop α Id m] [IteratorLoop α Id n] [LawfulIteratorLoop α Id m] [LawfulIteratorLoop α Id n] [MonadLiftT m n] [LawfulMonadLiftT m n] {f : βm γ} {g : δγn δ} {init : δ} {it : Iter β} :
      IterM.foldM g init (mapM f it) = foldM (fun (d : δ) (b : β) => do let cliftM (f b) g d c) init it
      theorem Std.Iterators.Iter.foldM_filterMap {α β γ : Type w} {δ : Type x} {m : Type x → Type w'} [Iterator α Id β] [Finite α Id] [Monad m] [LawfulMonad m] [IteratorLoop α Id m] [LawfulIteratorLoop α Id m] {f : βOption γ} {g : δγm δ} {init : δ} {it : Iter β} :
      foldM g init (filterMap f it) = foldM (fun (d : δ) (b : β) => match f b with | some c => g d c | x => pure d) init it
      theorem Std.Iterators.Iter.foldM_map {α β γ : Type w} {δ : Type x} {m : Type x → Type w'} [Iterator α Id β] [Finite α Id] [Monad m] [LawfulMonad m] [IteratorLoop α Id m] [LawfulIteratorLoop α Id m] {f : βγ} {g : δγm δ} {init : δ} {it : Iter β} :
      foldM g init (map f it) = foldM (fun (d : δ) (b : β) => g d (f b)) init it
      theorem Std.Iterators.Iter.fold_filterMapM {α β γ δ : Type w} {m : Type w → Type w'} [Iterator α Id β] [Finite α Id] [Monad m] [LawfulMonad m] [IteratorLoop α Id Id] [IteratorLoop α Id m] [LawfulIteratorLoop α Id Id] [LawfulIteratorLoop α Id m] {f : βm (Option γ)} {g : δγδ} {init : δ} {it : Iter β} :
      IterM.fold g init (filterMapM f it) = foldM (fun (d : δ) (b : β) => do let __discrf b match __discr with | some c => pure (g d c) | x => pure d) init it
      theorem Std.Iterators.Iter.fold_mapM {α β γ δ : Type w} {m : Type w → Type w'} [Iterator α Id β] [Finite α Id] [Monad m] [LawfulMonad m] [IteratorLoop α Id Id] [IteratorLoop α Id m] [LawfulIteratorLoop α Id Id] [LawfulIteratorLoop α Id m] {f : βm γ} {g : δγδ} {init : δ} {it : Iter β} :
      IterM.fold g init (mapM f it) = foldM (fun (d : δ) (b : β) => do let __do_liftf b pure (g d __do_lift)) init it
      theorem Std.Iterators.Iter.fold_filterMap {α β γ : Type w} {δ : Type x} [Iterator α Id β] [Finite α Id] [IteratorLoop α Id Id] [LawfulIteratorLoop α Id Id] {f : βOption γ} {g : δγδ} {init : δ} {it : Iter β} :
      fold g init (filterMap f it) = fold (fun (d : δ) (b : β) => match f b with | some c => g d c | x => d) init it
      theorem Std.Iterators.Iter.fold_map {α β γ : Type w} {δ : Type x} [Iterator α Id β] [Finite α Id] [IteratorLoop α Id Id] [LawfulIteratorLoop α Id Id] {f : βγ} {g : δγδ} {init : δ} {it : Iter β} :
      fold g init (map f it) = fold (fun (d : δ) (b : β) => g d (f b)) init it
      theorem Std.Iterators.Iter.anyM_filterMapM {α β β' : Type w} {m : Type w → Type w'} [Iterator α Id β] [Finite α Id] [Monad m] [LawfulMonad m] {it : Iter β} {f : βm (Option β')} {p : β'm (ULift Bool)} :
      IterM.anyM p (filterMapM f it) = IterM.anyM (fun (x : β) => do let __do_liftf x match __do_lift with | some fx => p fx | none => pure { down := false }) (mapM pure it)
      theorem Std.Iterators.Iter.anyM_eq_anyM_mapM_pure {α β : Type} {m : TypeType w'} [Iterator α Id β] [Finite α Id] [Monad m] [LawfulMonad m] [IteratorLoop α Id m] [LawfulIteratorLoop α Id m] {it : Iter β} {p : βm Bool} :
      anyM p it = ULift.down <$> IterM.anyM (fun (x : β) => ULift.up <$> p x) (mapM pure it)

      This lemma expresses Iter.anyM in terms of IterM.anyM. It requires all involved types to live in Type 0.

      theorem Std.Iterators.Iter.anyM_mapM {α β β' : Type w} {m : Type w → Type w'} [Iterator α Id β] [Finite α Id] [Monad m] [LawfulMonad m] {it : Iter β} {f : βm β'} {p : β'm (ULift Bool)} :
      IterM.anyM p (mapM f it) = IterM.anyM (fun (x : β) => do let __do_liftf x p __do_lift) (mapM pure it)
      theorem Std.Iterators.Iter.anyM_filterM {α β : Type w} {m : Type w → Type w'} [Iterator α Id β] [Finite α Id] [Monad m] [LawfulMonad m] {it : Iter β} {f p : βm (ULift Bool)} :
      IterM.anyM p (filterM f it) = IterM.anyM (fun (x : β) => do let __do_liftf x if __do_lift.down = true then p x else pure { down := false }) (mapM pure it)
      theorem Std.Iterators.Iter.anyM_filterMap {α β β' : Type w} {m : TypeType w'} [Iterator α Id β] [Finite α Id] [Monad m] [IteratorLoop α Id m] [LawfulMonad m] [LawfulIteratorLoop α Id m] {it : Iter β} {f : βOption β'} {p : β'm Bool} :
      anyM p (filterMap f it) = anyM (fun (x : β) => match f x with | some fx => p fx | none => pure false) it
      theorem Std.Iterators.Iter.anyM_map {α β β' : Type w} {m : TypeType w'} [Iterator α Id β] [Finite α Id] [Monad m] [IteratorLoop α Id m] [LawfulMonad m] [LawfulIteratorLoop α Id m] {it : Iter β} {f : ββ'} {p : β'm Bool} :
      anyM p (map f it) = anyM (fun (x : β) => p (f x)) it
      theorem Std.Iterators.Iter.anyM_filter {α β : Type w} {m : TypeType w'} [Iterator α Id β] [Finite α Id] [Monad m] [IteratorLoop α Id m] [LawfulMonad m] [LawfulIteratorLoop α Id m] {it : Iter β} {f : βBool} {p : βm Bool} :
      anyM p (filter f it) = anyM (fun (x : β) => if f x = true then p x else pure false) it
      theorem Std.Iterators.Iter.any_filterMapM {α β β' : Type w} {m : Type w → Type w'} [Iterator α Id β] [Finite α Id] [Monad m] [IteratorLoop α Id m] [LawfulMonad m] [LawfulIteratorLoop α Id m] {it : Iter β} {f : βm (Option β')} {p : β'Bool} :
      IterM.any p (filterMapM f it) = IterM.anyM (fun (x : β) => do let __do_liftf x match __do_lift with | some fx => pure { down := p fx } | none => pure { down := false }) (mapM pure it)
      theorem Std.Iterators.Iter.any_mapM {α β β' : Type w} {m : Type w → Type w'} [Iterator α Id β] [Finite α Id] [Monad m] [IteratorLoop α Id m] [LawfulMonad m] [LawfulIteratorLoop α Id m] {it : Iter β} {f : βm β'} {p : β'Bool} :
      IterM.any p (mapM f it) = IterM.anyM (fun (x : β) => (fun (x : β') => { down := p x }) <$> f x) (mapM pure it)
      theorem Std.Iterators.Iter.any_filterM {α β : Type w} {m : Type w → Type w'} [Iterator α Id β] [Finite α Id] [Monad m] [IteratorLoop α Id m] [LawfulMonad m] [LawfulIteratorLoop α Id m] {it : Iter β} {f : βm (ULift Bool)} {p : βBool} :
      IterM.any p (filterM f it) = IterM.anyM (fun (x : β) => do let __do_liftf x if __do_lift.down = true then pure { down := p x } else pure { down := false }) (mapM pure it)
      theorem Std.Iterators.Iter.any_filterMap {α β β' : Type w} [Iterator α Id β] [Finite α Id] [IteratorLoop α Id Id] [LawfulIteratorLoop α Id Id] {it : Iter β} {f : βOption β'} {p : β'Bool} :
      any p (filterMap f it) = any (fun (x : β) => match f x with | some fx => p fx | none => false) it
      theorem Std.Iterators.Iter.any_map {α β β' : Type w} [Iterator α Id β] [Finite α Id] [IteratorLoop α Id Id] [LawfulIteratorLoop α Id Id] {it : Iter β} {f : ββ'} {p : β'Bool} :
      any p (map f it) = any (fun (x : β) => p (f x)) it
      theorem Std.Iterators.Iter.allM_filterMapM {α β β' : Type w} {m : Type w → Type w'} [Iterator α Id β] [Finite α Id] [Monad m] [LawfulMonad m] {it : Iter β} {f : βm (Option β')} {p : β'm (ULift Bool)} :
      IterM.allM p (filterMapM f it) = IterM.allM (fun (x : β) => do let __do_liftf x match __do_lift with | some fx => p fx | none => pure { down := true }) (mapM pure it)
      theorem Std.Iterators.Iter.allM_eq_allM_mapM_pure {α β : Type} {m : TypeType w'} [Iterator α Id β] [Finite α Id] [Monad m] [LawfulMonad m] [IteratorLoop α Id m] [LawfulIteratorLoop α Id m] {it : Iter β} {p : βm Bool} :
      allM p it = ULift.down <$> IterM.allM (fun (x : β) => ULift.up <$> p x) (mapM pure it)

      This lemma expresses Iter.allM in terms of IterM.allM. It requires all involved types to live in Type 0.

      theorem Std.Iterators.Iter.allM_mapM {α β β' : Type w} {m : Type w → Type w'} [Iterator α Id β] [Finite α Id] [Monad m] [LawfulMonad m] {it : Iter β} {f : βm β'} {p : β'm (ULift Bool)} :
      IterM.allM p (mapM f it) = IterM.allM (fun (x : β) => do let __do_liftf x p __do_lift) (mapM pure it)
      theorem Std.Iterators.Iter.allM_filterM {α β : Type w} {m : Type w → Type w'} [Iterator α Id β] [Finite α Id] [Monad m] [LawfulMonad m] {it : Iter β} {f p : βm (ULift Bool)} :
      IterM.allM p (filterM f it) = IterM.allM (fun (x : β) => do let __do_liftf x if __do_lift.down = true then p x else pure { down := true }) (mapM pure it)
      theorem Std.Iterators.Iter.allM_filterMap {α β β' : Type w} {m : TypeType w'} [Iterator α Id β] [Finite α Id] [Monad m] [IteratorLoop α Id m] [LawfulMonad m] [LawfulIteratorLoop α Id m] {it : Iter β} {f : βOption β'} {p : β'm Bool} :
      allM p (filterMap f it) = allM (fun (x : β) => match f x with | some fx => p fx | none => pure true) it
      theorem Std.Iterators.Iter.allM_map {α β β' : Type w} {m : TypeType w'} [Iterator α Id β] [Finite α Id] [Monad m] [IteratorLoop α Id m] [LawfulMonad m] [LawfulIteratorLoop α Id m] {it : Iter β} {f : ββ'} {p : β'm Bool} :
      allM p (map f it) = allM (fun (x : β) => p (f x)) it
      theorem Std.Iterators.Iter.allM_filter {α β : Type w} {m : TypeType w'} [Iterator α Id β] [Finite α Id] [Monad m] [IteratorLoop α Id m] [LawfulMonad m] [LawfulIteratorLoop α Id m] {it : Iter β} {f : βBool} {p : βm Bool} :
      allM p (filter f it) = allM (fun (x : β) => if f x = true then p x else pure true) it
      theorem Std.Iterators.Iter.all_filterMapM {α β β' : Type w} {m : Type w → Type w'} [Iterator α Id β] [Finite α Id] [Monad m] [IteratorLoop α Id m] [LawfulMonad m] [LawfulIteratorLoop α Id m] {it : Iter β} {f : βm (Option β')} {p : β'Bool} :
      IterM.all p (filterMapM f it) = IterM.allM (fun (x : β) => do let __do_liftf x match __do_lift with | some fx => pure { down := p fx } | none => pure { down := true }) (mapM pure it)
      theorem Std.Iterators.Iter.all_mapM {α β β' : Type w} {m : Type w → Type w'} [Iterator α Id β] [Finite α Id] [Monad m] [IteratorLoop α Id m] [LawfulMonad m] [LawfulIteratorLoop α Id m] {it : Iter β} {f : βm β'} {p : β'Bool} :
      IterM.all p (mapM f it) = IterM.allM (fun (x : β) => (fun (x : β') => { down := p x }) <$> f x) (mapM pure it)
      theorem Std.Iterators.Iter.all_filterM {α β : Type w} {m : Type w → Type w'} [Iterator α Id β] [Finite α Id] [Monad m] [IteratorLoop α Id m] [LawfulMonad m] [LawfulIteratorLoop α Id m] {it : Iter β} {f : βm (ULift Bool)} {p : βBool} :
      IterM.all p (filterM f it) = IterM.allM (fun (x : β) => do let __do_liftf x if __do_lift.down = true then pure { down := p x } else pure { down := true }) (mapM pure it)
      theorem Std.Iterators.Iter.all_filterMap {α β β' : Type w} [Iterator α Id β] [Finite α Id] [IteratorLoop α Id Id] [LawfulIteratorLoop α Id Id] {it : Iter β} {f : βOption β'} {p : β'Bool} :
      all p (filterMap f it) = all (fun (x : β) => match f x with | some fx => p fx | none => true) it
      theorem Std.Iterators.Iter.all_map {α β β' : Type w} [Iterator α Id β] [Finite α Id] [IteratorLoop α Id Id] [LawfulIteratorLoop α Id Id] {it : Iter β} {f : ββ'} {p : β'Bool} :
      all p (map f it) = all (fun (x : β) => p (f x)) it