Documentation

Init.Data.Fin.Fold

@[inline]
def Fin.foldl {α : Sort u_1} (n : Nat) (f : αFin nα) (init : α) :
α

Combine all the values that can be represented by Fin n with an initial value, starting at 0 and nesting to the left.

Example:

  • Fin.foldl 3 (· + ·.val) (0 : Nat) = ((0 + (0 : Fin 3).val) + (1 : Fin 3).val) + (2 : Fin 3).val
Equations
    Instances For
      @[specialize #[]]
      def Fin.foldl.loop {α : Sort u_1} (n : Nat) (f : αFin nα) (x : α) (i : Nat) :
      α

      Inner loop for Fin.foldl. Fin.foldl.loop n f x i = f (f (f x i) ...) (n-1)

      Equations
        Instances For
          @[inline]
          def Fin.foldr {α : Sort u_1} (n : Nat) (f : Fin nαα) (init : α) :
          α

          Combine all the values that can be represented by Fin n with an initial value, starting at n - 1 and nesting to the right.

          Example:

          • Fin.foldr 3 (·.val + ·) (0 : Nat) = (0 : Fin 3).val + ((1 : Fin 3).val + ((2 : Fin 3).val + 0))
          Equations
            Instances For
              @[specialize #[]]
              def Fin.foldr.loop {α : Sort u_1} (n : Nat) (f : Fin nαα) (i : Nat) :
              i nαα

              Inner loop for Fin.foldr. Fin.foldr.loop n f i x = f 0 (f ... (f (i-1) x))

              Equations
                Instances For
                  @[inline]
                  def Fin.foldlM {m : Type u_1 → Type u_2} {α : Type u_1} [Monad m] (n : Nat) (f : αFin nm α) (init : α) :
                  m α

                  Folds a monadic function over all the values in Fin n from left to right, starting with 0.

                  It is the sequence of steps:

                  Fin.foldlM n f x₀ = do
                    let x₁ ← f x₀ 0
                    let x₂ ← f x₁ 1
                    ...
                    let xₙ ← f xₙ₋₁ (n-1)
                    pure xₙ
                  
                  Equations
                    Instances For
                      @[specialize #[]]
                      def Fin.foldlM.loop {m : Type u_1 → Type u_2} {α : Type u_1} [Monad m] (n : Nat) (f : αFin nm α) (x : α) (i : Nat) :
                      m α

                      Inner loop for Fin.foldlM.

                      Fin.foldlM.loop n f xᵢ i = do
                        let xᵢ₊₁ ← f xᵢ i
                        ...
                        let xₙ ← f xₙ₋₁ (n-1)
                        pure xₙ
                      
                      Equations
                        Instances For
                          @[inline]
                          def Fin.foldrM {m : Type u_1 → Type u_2} {α : Type u_1} [Monad m] (n : Nat) (f : Fin nαm α) (init : α) :
                          m α

                          Folds a monadic function over Fin n from right to left, starting with n-1.

                          It is the sequence of steps:

                          Fin.foldrM n f xₙ = do
                            let xₙ₋₁ ← f (n-1) xₙ
                            let xₙ₋₂ ← f (n-2) xₙ₋₁
                            ...
                            let x₀ ← f 0 x₁
                            pure x₀
                          
                          Equations
                            Instances For
                              @[specialize #[]]
                              def Fin.foldrM.loop {m : Type u_1 → Type u_2} {α : Type u_1} [Monad m] (n : Nat) (f : Fin nαm α) :
                              { i : Nat // i n }αm α

                              Inner loop for Fin.foldrM.

                              Fin.foldrM.loop n f i xᵢ = do
                                let xᵢ₋₁ ← f (i-1) xᵢ
                                ...
                                let x₁ ← f 1 x₂
                                let x₀ ← f 0 x₁
                                pure x₀
                              
                              Equations
                                Instances For

                                  foldlM #

                                  theorem Fin.foldlM_congr {m : Type u_1 → Type u_2} {α : Type u_1} [Monad m] {n k : Nat} (w : n = k) (f : αFin nm α) :
                                  foldlM n f = foldlM k fun (x : α) (i : Fin k) => f x (Fin.cast i)
                                  theorem Fin.foldlM_loop_lt {m : Type u_1 → Type u_2} {α : Type u_1} {n i : Nat} [Monad m] (f : αFin nm α) (x : α) (h : i < n) :
                                  foldlM.loop n f x i = do let xf x i, h foldlM.loop n f x (i + 1)
                                  theorem Fin.foldlM_loop_eq {m : Type u_1 → Type u_2} {α : Type u_1} {n : Nat} [Monad m] (f : αFin nm α) (x : α) :
                                  foldlM.loop n f x n = pure x
                                  @[irreducible]
                                  theorem Fin.foldlM_loop {m : Type u_1 → Type u_2} {α : Type u_1} {n i : Nat} [Monad m] (f : αFin (n + 1)m α) (x : α) (h : i < n + 1) :
                                  foldlM.loop (n + 1) f x i = do let xf x i, h foldlM.loop n (fun (x : α) (j : Fin n) => f x j.succ) x i
                                  @[simp]
                                  theorem Fin.foldlM_zero {m : Type u_1 → Type u_2} {α : Type u_1} [Monad m] (f : αFin 0m α) :
                                  theorem Fin.foldlM_succ {m : Type u_1 → Type u_2} {α : Type u_1} {n : Nat} [Monad m] (f : αFin (n + 1)m α) :
                                  foldlM (n + 1) f = fun (x : α) => f x 0 >>= foldlM n fun (x : α) (j : Fin n) => f x j.succ
                                  theorem Fin.foldlM_succ_last {m : Type u_1 → Type u_2} {α : Type u_1} {n : Nat} [Monad m] [LawfulMonad m] (f : αFin (n + 1)m α) :
                                  foldlM (n + 1) f = fun (x : α) => do let xfoldlM n (fun (x : α) (j : Fin n) => f x j.castSucc) x f x (last n)

                                  Variant of foldlM_succ that splits off Fin.last n rather than 0.

                                  theorem Fin.foldlM_add {m : Type u_1 → Type u_2} {α : Type u_1} {n k : Nat} [Monad m] [LawfulMonad m] (f : αFin (n + k)m α) :
                                  foldlM (n + k) f = fun (x : α) => foldlM n (fun (x : α) (i : Fin n) => f x (castLE i)) x >>= foldlM k fun (x : α) (i : Fin k) => f x (natAdd n i)

                                  foldrM #

                                  theorem Fin.foldrM_congr {m : Type u_1 → Type u_2} {α : Type u_1} [Monad m] {n k : Nat} (w : n = k) (f : Fin nαm α) :
                                  foldrM n f = foldrM k fun (i : Fin k) => f (Fin.cast i)
                                  theorem Fin.foldrM_loop_zero {m : Type u_1 → Type u_2} {n : Nat} {α : Type u_1} [Monad m] (f : Fin nαm α) (x : α) :
                                  foldrM.loop n f 0, x = pure x
                                  theorem Fin.foldrM_loop_succ {m : Type u_1 → Type u_2} {n : Nat} {α : Type u_1} {i : Nat} [Monad m] (f : Fin nαm α) (x : α) (h : i < n) :
                                  foldrM.loop n f i + 1, h x = f i, h x >>= foldrM.loop n f i,
                                  theorem Fin.foldrM_loop {m : Type u_1 → Type u_2} {n : Nat} {α : Type u_1} {i : Nat} [Monad m] [LawfulMonad m] (f : Fin (n + 1)αm α) (x : α) (h : i + 1 n + 1) :
                                  foldrM.loop (n + 1) f i + 1, h x = foldrM.loop n (fun (j : Fin n) => f j.succ) i, x >>= f 0
                                  @[simp]
                                  theorem Fin.foldrM_zero {m : Type u_1 → Type u_2} {α : Type u_1} [Monad m] (f : Fin 0αm α) :
                                  theorem Fin.foldrM_succ {m : Type u_1 → Type u_2} {n : Nat} {α : Type u_1} [Monad m] [LawfulMonad m] (f : Fin (n + 1)αm α) :
                                  foldrM (n + 1) f = fun (x : α) => foldrM n (fun (i : Fin n) => f i.succ) x >>= f 0
                                  theorem Fin.foldrM_succ_last {m : Type u_1 → Type u_2} {n : Nat} {α : Type u_1} [Monad m] [LawfulMonad m] (f : Fin (n + 1)αm α) :
                                  foldrM (n + 1) f = fun (x : α) => f (last n) x >>= foldrM n fun (i : Fin n) => f i.castSucc
                                  theorem Fin.foldrM_add {m : Type u_1 → Type u_2} {n k : Nat} {α : Type u_1} [Monad m] [LawfulMonad m] (f : Fin (n + k)αm α) :
                                  foldrM (n + k) f = fun (x : α) => foldrM k (fun (i : Fin k) => f (natAdd n i)) x >>= foldrM n fun (i : Fin n) => f (castLE i)

                                  foldl #

                                  theorem Fin.foldl_congr {α : Sort u_1} {n k : Nat} (w : n = k) (f : αFin nα) :
                                  foldl n f = foldl k fun (x : α) (i : Fin k) => f x (Fin.cast i)
                                  theorem Fin.foldl_loop_lt {α : Sort u_1} {n i : Nat} (f : αFin nα) (x : α) (h : i < n) :
                                  foldl.loop n f x i = foldl.loop n f (f x i, h) (i + 1)
                                  theorem Fin.foldl_loop_eq {α : Sort u_1} {n : Nat} (f : αFin nα) (x : α) :
                                  foldl.loop n f x n = x
                                  @[irreducible]
                                  theorem Fin.foldl_loop {α : Sort u_1} {n i : Nat} (f : αFin (n + 1)α) (x : α) (h : i < n + 1) :
                                  foldl.loop (n + 1) f x i = foldl.loop n (fun (x : α) (j : Fin n) => f x j.succ) (f x i, h) i
                                  @[simp]
                                  theorem Fin.foldl_zero {α : Sort u_1} (f : αFin 0α) (x : α) :
                                  foldl 0 f x = x
                                  theorem Fin.foldl_succ {α : Sort u_1} {n : Nat} (f : αFin (n + 1)α) (x : α) :
                                  foldl (n + 1) f x = foldl n (fun (x : α) (i : Fin n) => f x i.succ) (f x 0)
                                  theorem Fin.foldl_succ_last {α : Sort u_1} {n : Nat} (f : αFin (n + 1)α) (x : α) :
                                  foldl (n + 1) f x = f (foldl n (fun (x1 : α) (x2 : Fin n) => f x1 x2.castSucc) x) (last n)
                                  theorem Fin.foldl_add {α : Sort u_1} {n m : Nat} (f : αFin (n + m)α) (x : α) :
                                  foldl (n + m) f x = foldl m (fun (x : α) (i : Fin m) => f x (natAdd n i)) (foldl n (fun (x : α) (i : Fin n) => f x (castLE i)) x)
                                  theorem Fin.foldl_eq_foldlM {α : Type u_1} {n : Nat} (f : αFin nα) (x : α) :
                                  foldl n f x = (foldlM n (fun (x1 : α) (x2 : Fin n) => pure (f x1 x2)) x).run
                                  theorem Fin.foldlM_pure {m : Type u_1 → Type u_2} {α : Type u_1} {x : α} [Monad m] [LawfulMonad m] {n : Nat} {f : αFin nα} :
                                  foldlM n (fun (x : α) (i : Fin n) => pure (f x i)) x = pure (foldl n f x)

                                  foldr #

                                  theorem Fin.foldr_congr {α : Sort u_1} {n k : Nat} (w : n = k) (f : Fin nαα) :
                                  foldr n f = foldr k fun (i : Fin k) => f (Fin.cast i)
                                  theorem Fin.foldr_loop_zero {n : Nat} {α : Sort u_1} (f : Fin nαα) (x : α) :
                                  foldr.loop n f 0 x = x
                                  theorem Fin.foldr_loop_succ {n : Nat} {α : Sort u_1} {i : Nat} (f : Fin nαα) (x : α) (h : i < n) :
                                  foldr.loop n f (i + 1) h x = foldr.loop n f i (f i, h x)
                                  theorem Fin.foldr_loop {n : Nat} {α : Sort u_1} {i : Nat} (f : Fin (n + 1)αα) (x : α) (h : i + 1 n + 1) :
                                  foldr.loop (n + 1) f (i + 1) h x = f 0 (foldr.loop n (fun (j : Fin n) => f j.succ) i x)
                                  @[simp]
                                  theorem Fin.foldr_zero {α : Sort u_1} (f : Fin 0αα) (x : α) :
                                  foldr 0 f x = x
                                  theorem Fin.foldr_succ {n : Nat} {α : Sort u_1} (f : Fin (n + 1)αα) (x : α) :
                                  foldr (n + 1) f x = f 0 (foldr n (fun (i : Fin n) => f i.succ) x)
                                  theorem Fin.foldr_succ_last {n : Nat} {α : Sort u_1} (f : Fin (n + 1)αα) (x : α) :
                                  foldr (n + 1) f x = foldr n (fun (x : Fin n) => f x.castSucc) (f (last n) x)
                                  theorem Fin.foldr_add {n m : Nat} {α : Sort u_1} (f : Fin (n + m)αα) (x : α) :
                                  foldr (n + m) f x = foldr n (fun (i : Fin n) => f (castLE i)) (foldr m (fun (i : Fin m) => f (natAdd n i)) x)
                                  theorem Fin.foldr_eq_foldrM {n : Nat} {α : Type u_1} (f : Fin nαα) (x : α) :
                                  foldr n f x = (foldrM n (fun (x1 : Fin n) (x2 : α) => pure (f x1 x2)) x).run
                                  theorem Fin.foldl_rev {n : Nat} {α : Sort u_1} (f : Fin nαα) (x : α) :
                                  foldl n (fun (x : α) (i : Fin n) => f i.rev x) x = foldr n f x
                                  theorem Fin.foldr_rev {α : Sort u_1} {n : Nat} (f : αFin nα) (x : α) :
                                  foldr n (fun (i : Fin n) (x : α) => f x i.rev) x = foldl n f x
                                  theorem Fin.foldrM_pure {m : Type u_1 → Type u_2} {α : Type u_1} {x : α} [Monad m] [LawfulMonad m] {n : Nat} {f : Fin nαα} :
                                  foldrM n (fun (i : Fin n) (x : α) => pure (f i x)) x = pure (foldr n f x)