Documentation

Init.Data.Vector.Monadic

Lemmas about Vector.forIn' and Vector.forIn. #

Monadic operations #

@[simp]
theorem Vector.map_toArray_inj {m : Type u_1 → Type u_2} {α : Type u_1} {n : Nat} [Monad m] [LawfulMonad m] {xs ys : m (Vector α n)} :
toArray <$> xs = toArray <$> ys xs = ys

mapM #

theorem Vector.mapM_congr {m : Type u_1 → Type u_2} {α : Type u_3} {n : Nat} {β : Type u_1} [Monad m] {xs ys : Vector α n} (w : xs = ys) {f : αm β} :
mapM f xs = mapM f ys
@[simp]
theorem Vector.mapM_mk_empty {m : Type u_1 → Type u_2} {α : Type u_3} {β : Type u_1} [Monad m] (f : αm β) :
mapM f { toArray := #[], size_toArray := } = pure { toArray := #[], size_toArray := }
@[simp]
theorem Vector.mapM_append {m : Type u_1 → Type u_2} {α : Type u_3} {β : Type u_1} {n n' : Nat} [Monad m] [LawfulMonad m] (f : αm β) {xs : Vector α n} {ys : Vector α n'} :
mapM f (xs ++ ys) = do let __do_liftmapM f xs let __do_lift_1mapM f ys pure (__do_lift ++ __do_lift_1)

foldlM and foldrM #

theorem Vector.foldlM_map {m : Type u_1 → Type u_2} {β₁ : Type u_3} {β₂ : Type u_4} {α : Type u_1} {n : Nat} [Monad m] (f : β₁β₂) (g : αβ₂m α) (xs : Vector β₁ n) (init : α) :
foldlM g init (map f xs) = foldlM (fun (x : α) (y : β₁) => g x (f y)) init xs
theorem Vector.foldrM_map {m : Type u_1 → Type u_2} {β₁ : Type u_3} {β₂ : Type u_4} {α : Type u_1} {n : Nat} [Monad m] [LawfulMonad m] (f : β₁β₂) (g : β₂αm α) (xs : Vector β₁ n) (init : α) :
foldrM g init (map f xs) = foldrM (fun (x : β₁) (y : α) => g (f x) y) init xs
theorem Vector.foldlM_filterMap {m : Type u_1 → Type u_2} {α : Type u_3} {β : Type u_4} {γ : Type u_1} {n : Nat} [Monad m] [LawfulMonad m] (f : αOption β) (g : γβm γ) (xs : Vector α n) (init : γ) :
Array.foldlM g init (Array.filterMap f xs.toArray) = foldlM (fun (x : γ) (y : α) => match f y with | some b => g x b | none => pure x) init xs
theorem Vector.foldrM_filterMap {m : Type u_1 → Type u_2} {α : Type u_3} {β : Type u_4} {γ : Type u_1} {n : Nat} [Monad m] [LawfulMonad m] (f : αOption β) (g : βγm γ) (xs : Vector α n) (init : γ) :
Array.foldrM g init (Array.filterMap f xs.toArray) = foldrM (fun (x : α) (y : γ) => match f x with | some b => g b y | none => pure y) init xs
theorem Vector.foldlM_filter {m : Type u_1 → Type u_2} {α : Type u_3} {β : Type u_1} {n : Nat} [Monad m] [LawfulMonad m] (p : αBool) (g : βαm β) (xs : Vector α n) (init : β) :
Array.foldlM g init (Array.filter p xs.toArray) = foldlM (fun (x : β) (y : α) => if p y = true then g x y else pure x) init xs
theorem Vector.foldrM_filter {m : Type u_1 → Type u_2} {α : Type u_3} {β : Type u_1} {n : Nat} [Monad m] [LawfulMonad m] (p : αBool) (g : αβm β) (xs : Vector α n) (init : β) :
Array.foldrM g init (Array.filter p xs.toArray) = foldrM (fun (x : α) (y : β) => if p x = true then g x y else pure y) init xs
@[simp]
theorem Vector.foldlM_attachWith {m : Type u_1 → Type u_2} {α : Type u_3} {n : Nat} {β : Type u_1} [Monad m] (xs : Vector α n) {q : αProp} (H : ∀ (a : α), a xsq a) {f : β{ x : α // q x }m β} {b : β} :
foldlM f b (xs.attachWith q H) = foldlM (fun (b : β) (x : { x : α // x xs }) => match x with | a, h => f b a, ) b xs.attach
@[simp]
theorem Vector.foldrM_attachWith {m : Type u_1 → Type u_2} {α : Type u_3} {n : Nat} {β : Type u_1} [Monad m] [LawfulMonad m] (xs : Vector α n) {q : αProp} (H : ∀ (a : α), a xsq a) {f : { x : α // q x }βm β} {b : β} :
foldrM f b (xs.attachWith q H) = foldrM (fun (a : { x : α // x xs }) (acc : β) => f a.val, acc) b xs.attach

forM #

theorem Vector.forM_congr {m : Type u_1 → Type u_2} {α : Type u_3} {n : Nat} [Monad m] {as bs : Vector α n} (w : as = bs) {f : αm PUnit} :
forM as f = forM bs f
@[simp]
theorem Vector.forM_append {m : Type u_1 → Type u_2} {α : Type u_3} {n n' : Nat} [Monad m] [LawfulMonad m] (xs : Vector α n) (ys : Vector α n') (f : αm PUnit) :
forM (xs ++ ys) f = do forM xs f forM ys f
@[simp]
theorem Vector.forM_map {m : Type u_1 → Type u_2} {α : Type u_3} {n : Nat} {β : Type u_4} [Monad m] [LawfulMonad m] (xs : Vector α n) (g : αβ) (f : βm PUnit) :
forM (map g xs) f = forM xs fun (a : α) => f (g a)

forIn' #

theorem Vector.forIn'_congr {m : Type u_1 → Type u_2} {α : Type u_3} {n : Nat} {β : Type u_1} [Monad m] {xs ys : Vector α n} (w : xs = ys) {b b' : β} (hb : b = b') {f : (a' : α) → a' xsβm (ForInStep β)} {g : (a' : α) → a' ysβm (ForInStep β)} (h : ∀ (a : α) (m_1 : a ys) (b : β), f a b = g a m_1 b) :
forIn' xs b f = forIn' ys b' g
theorem Vector.forIn'_eq_foldlM {m : Type u_1 → Type u_2} {α : Type u_3} {n : Nat} {β : Type u_1} [Monad m] [LawfulMonad m] (xs : Vector α n) (f : (a : α) → a xsβm (ForInStep β)) (init : β) :
forIn' xs init f = ForInStep.value <$> foldlM (fun (b : ForInStep β) (x : { x : α // x xs }) => match x with | a, m_1 => match b with | ForInStep.yield b => f a m_1 b | ForInStep.done b => pure (ForInStep.done b)) (ForInStep.yield init) xs.attach

We can express a for loop over a vector as a fold, in which whenever we reach .done b we keep that value through the rest of the fold.

@[simp]
theorem Vector.forIn'_yield_eq_foldlM {m : Type u_1 → Type u_2} {α : Type u_3} {n : Nat} {β γ : Type u_1} [Monad m] [LawfulMonad m] (xs : Vector α n) (f : (a : α) → a xsβm γ) (g : (a : α) → a xsβγβ) (init : β) :
(forIn' xs init fun (a : α) (m_1 : a xs) (b : β) => (fun (c : γ) => ForInStep.yield (g a m_1 b c)) <$> f a m_1 b) = foldlM (fun (b : β) (x : { x : α // x xs }) => match x with | a, m_1 => g a m_1 b <$> f a m_1 b) init xs.attach

We can express a for loop over a vector which always yields as a fold.

theorem Vector.forIn'_pure_yield_eq_foldl {m : Type u_1 → Type u_2} {α : Type u_3} {n : Nat} {β : Type u_1} [Monad m] [LawfulMonad m] (xs : Vector α n) (f : (a : α) → a xsββ) (init : β) :
(forIn' xs init fun (a : α) (m_1 : a xs) (b : β) => pure (ForInStep.yield (f a m_1 b))) = pure (foldl (fun (b : β) (x : { x : α // x xs }) => match x with | a, h => f a h b) init xs.attach)
@[simp]
theorem Vector.forIn'_yield_eq_foldl {α : Type u_1} {n : Nat} {β : Type u_2} (xs : Vector α n) (f : (a : α) → a xsββ) (init : β) :
(forIn' xs init fun (a : α) (m : a xs) (b : β) => ForInStep.yield (f a m b)) = foldl (fun (b : β) (x : { x : α // x xs }) => match x with | a, h => f a h b) init xs.attach
@[simp]
theorem Vector.forIn'_map {m : Type u_1 → Type u_2} {α : Type u_3} {n : Nat} {β : Type u_4} {γ : Type u_1} {init : γ} [Monad m] [LawfulMonad m] (xs : Vector α n) (g : αβ) (f : (b : β) → b map g xsγm (ForInStep γ)) :
forIn' (map g xs) init f = forIn' xs init fun (a : α) (h : a xs) (y : γ) => f (g a) y
theorem Vector.forIn_eq_foldlM {m : Type u_1 → Type u_2} {α : Type u_3} {β : Type u_1} {n : Nat} [Monad m] [LawfulMonad m] (f : αβm (ForInStep β)) (init : β) (xs : Vector α n) :
forIn xs init f = ForInStep.value <$> foldlM (fun (b : ForInStep β) (a : α) => match b with | ForInStep.yield b => f a b | ForInStep.done b => pure (ForInStep.done b)) (ForInStep.yield init) xs

We can express a for loop over a vector as a fold, in which whenever we reach .done b we keep that value through the rest of the fold.

@[simp]
theorem Vector.forIn_yield_eq_foldlM {m : Type u_1 → Type u_2} {α : Type u_3} {n : Nat} {β γ : Type u_1} [Monad m] [LawfulMonad m] (xs : Vector α n) (f : αβm γ) (g : αβγβ) (init : β) :
(forIn xs init fun (a : α) (b : β) => (fun (c : γ) => ForInStep.yield (g a b c)) <$> f a b) = foldlM (fun (b : β) (a : α) => g a b <$> f a b) init xs

We can express a for loop over a vector which always yields as a fold.

theorem Vector.forIn_pure_yield_eq_foldl {m : Type u_1 → Type u_2} {α : Type u_3} {n : Nat} {β : Type u_1} [Monad m] [LawfulMonad m] (xs : Vector α n) (f : αββ) (init : β) :
(forIn xs init fun (a : α) (b : β) => pure (ForInStep.yield (f a b))) = pure (foldl (fun (b : β) (a : α) => f a b) init xs)
@[simp]
theorem Vector.forIn_yield_eq_foldl {α : Type u_1} {n : Nat} {β : Type u_2} (xs : Vector α n) (f : αββ) (init : β) :
(forIn xs init fun (a : α) (b : β) => ForInStep.yield (f a b)) = foldl (fun (b : β) (a : α) => f a b) init xs
@[simp]
theorem Vector.forIn_map {m : Type u_1 → Type u_2} {α : Type u_3} {n : Nat} {β : Type u_4} {γ : Type u_1} {init : γ} [Monad m] [LawfulMonad m] (xs : Vector α n) (g : αβ) (f : βγm (ForInStep γ)) :
forIn (map g xs) init f = forIn xs init fun (a : α) (y : γ) => f (g a) y