The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V33. Data structures and computation

Mathlib.Control.Fold

49 declarations and 8 deprecated aliases. Mathlib docs · Source

List folds generalized to Traversable

Informally, we can think of foldl as a special case of traverse where we do not care about the reconstructed data structure and, in a state monad, we care about the final state.

The obvious way to define foldl would be to use the state monad but it is nicer to reason about a more abstract interface with foldMap as a primitive and foldMap_hom as a defining property.

def foldMap {α ω} [One ω] [Mul ω] (f : α → ω) : t α → ω := ...

lemma foldMap_hom (α β) [Monoid α] [Monoid β] (f : α →* β) (g : γ → α) (x : t γ) :
    f (foldMap g x) = foldMap (f ∘ g) x :=
...

foldMap uses a monoid ω to accumulate a value for every element of a data structure and foldMap_hom uses a monoid homomorphism to substitute the monoid used by foldMap. The two are sufficient to define foldl, foldr and toList. toList permits the formulation of specifications in terms of operations on lists.

Each fold function can be defined using a specialized monoid. toList uses a free monoid represented as a list with concatenation while foldl uses endofunctions together with function composition.

The definition through monoids uses traverse together with the applicative functor const m (where m is the monoid). As an implementation, const guarantees that no resource is spent on reconstructing the structure during traversal.

A special class could be defined for foldable, similarly to Haskell, but the author cannot think of instances of foldable that are not also Traversable.

abbrev Monoid.Foldl E1 #

Type u → Type u

For a list, foldl f x [y₀,y₁] reduces as follows:

calc  foldl f x [y₀,y₁]
    = foldl f (f x y₀) [y₁]      : rfl
... = foldl f (f (f x y₀) y₁) [] : rfl
... = f (f x y₀) y₁              : rfl

with

f : α → β → α
x : α
[y₀,y₁] : List β

We can view the above as a composition of functions:

... = f (f x y₀) y₁              : rfl
... = flip f y₁ (flip f y₀ x)    : rfl
... = (flip f y₁ ∘ flip f y₀) x  : rfl

We can use traverse and const to construct this composition:

calc   const.run (traverse (fun y ↦ const.mk' (flip f y)) [y₀,y₁]) x
     = const.run ((::) <$> const.mk' (flip f y₀) <*>
         traverse (fun y ↦ const.mk' (flip f y)) [y₁]) x
...  = const.run ((::) <$> const.mk' (flip f y₀) <*>
         ( (::) <$> const.mk' (flip f y₁) <*> traverse (fun y ↦ const.mk' (flip f y)) [] )) x
...  = const.run ((::) <$> const.mk' (flip f y₀) <*>
         ( (::) <$> const.mk' (flip f y₁) <*> pure [] )) x
...  = const.run ( ((::) <$> const.mk' (flip f y₁) <*> pure []) ∘
         ((::) <$> const.mk' (flip f y₀)) ) x
...  = const.run ( const.mk' (flip f y₁) ∘ const.mk' (flip f y₀) ) x
...  = const.run ( flip f y₁ ∘ flip f y₀ ) x
...  = f (f x y₀) y₁

And this is how const turns a monoid into an applicative functor and how the monoid of endofunctions define Foldl.

def Monoid.Foldl.mk E1 #

{α : Type u} → (α → α) → Monoid.Foldl α

def Monoid.Foldl.get E1 #

{α : Type u} → Monoid.Foldl α → α → α

theorem Monoid.Foldl.ofFreeMonoid_apply E0 #

∀ {α β : Type u} (f : β → α → β) (xs : FreeMonoid α),
  (Monoid.Foldl.ofFreeMonoid f) xs = MulOpposite.op { asHom := TypeCat.ofHom (flip (List.foldl f) xs.toList) }

def Monoid.Foldl.ofFreeMonoid E1 #

{α β : Type u} → (β → α → β) → FreeMonoid α →* Monoid.Foldl β

abbrev Monoid.Foldr E1 #

Type u → Type u

def Monoid.Foldr.mk E1 #

{α : Type u} → (α → α) → Monoid.Foldr α

def Monoid.Foldr.get E1 #

{α : Type u} → Monoid.Foldr α → α → α

theorem Monoid.Foldr.ofFreeMonoid_apply_asHom E0 #

∀ {α β : Type u} (f : α → β → β) (xs : FreeMonoid α),
  ((Monoid.Foldr.ofFreeMonoid f) xs).asHom = TypeCat.ofHom (flip (List.foldr f) xs.toList)

def Monoid.Foldr.ofFreeMonoid E1 #

{α β : Type u} → (α → β → β) → FreeMonoid α →* Monoid.Foldr β

abbrev Monoid.foldlM E1 #

(m : Type u → Type u) → [Monad m] → Type u → Type u

def Monoid.foldlM.mk E1 #

{m : Type u → Type u} → [inst : Monad m] → {α : Type u} → (α → m α) → Monoid.foldlM m α

def Monoid.foldlM.get E1 #

{m : Type u → Type u} → [inst : Monad m] → {α : Type u} → Monoid.foldlM m α → α → m α

theorem Monoid.foldlM.ofFreeMonoid_apply E0 #

∀ {m : Type u → Type u} [inst : Monad m] {α β : Type u} [inst_1 : LawfulMonad m] (f : β → α → m β) (xs : FreeMonoid α),
  (Monoid.foldlM.ofFreeMonoid f) xs = MulOpposite.op { asHom := flip (List.foldlM f) xs.toList }

def Monoid.foldlM.ofFreeMonoid E1 #

{m : Type u → Type u} →
  [inst : Monad m] → {α β : Type u} → [inst_1 : LawfulMonad m] → (β → α → m β) → FreeMonoid α →* Monoid.foldlM m β

abbrev Monoid.foldrM E1 #

(m : Type u → Type u) → [Monad m] → Type u → Type u

def Monoid.foldrM.mk E1 #

{m : Type u → Type u} → [inst : Monad m] → {α : Type u} → (α → m α) → Monoid.foldrM m α

def Monoid.foldrM.get E1 #

{m : Type u → Type u} → [inst : Monad m] → {α : Type u} → Monoid.foldrM m α → α → m α

theorem Monoid.foldrM.ofFreeMonoid_apply_asHom E0 #

∀ {m : Type u → Type u} [inst : Monad m] {α β : Type u} [inst_1 : LawfulMonad m] (f : α → β → m β) (xs : FreeMonoid α)
  (a : CategoryTheory.KleisliCat.mk m β), ((Monoid.foldrM.ofFreeMonoid f) xs).asHom a = flip (List.foldrM f) xs.toList a

def Monoid.foldrM.ofFreeMonoid E1 #

{m : Type u → Type u} →
  [inst : Monad m] → {α β : Type u} → [inst_1 : LawfulMonad m] → (α → β → m β) → FreeMonoid α →* Monoid.foldrM m β

def Traversable.foldMap E1 #

{t : Type u → Type u} → [Traversable t] → {α ω : Type u} → [One ω] → [Mul ω] → (α → ω) → t α → ω

def Traversable.foldl E1 #

{α β : Type u} → {t : Type u → Type u} → [Traversable t] → (α → β → α) → α → t β → α

def Traversable.foldr E1 #

{α β : Type u} → {t : Type u → Type u} → [Traversable t] → (α → β → β) → β → t α → β

def Traversable.toList E1 #

{α : Type u} → {t : Type u → Type u} → [Traversable t] → t α → List α

Conceptually, toList collects all the elements of a collection in a list. This idea is formalized by

lemma toList_spec (x : t α) : toList x = foldMap FreeMonoid.mk x.

The definition of toList is based on foldl and List.cons for speed. It is faster than using foldMap FreeMonoid.mk because, by using foldl and List.cons, each insertion is done in constant time. As a consequence, toList performs in linear.

On the other hand, foldMap FreeMonoid.mk creates a singleton list around each element and concatenates all the resulting lists. In xs ++ ys, concatenation takes a time proportional to length xs. Since the order in which concatenation is evaluated is unspecified, nothing prevents each element of the traversable to be appended at the end xs ++ [x] which would yield a O(n²) run time.

def Traversable.length E1 #

{α : Type u} → {t : Type u → Type u} → [Traversable t] → t α → ℕ

def Traversable.foldlM E1 #

{α β : Type u} →
  {t : Type u → Type u} → [Traversable t] → {m : Type u → Type u} → [Monad m] → (α → β → m α) → α → t β → m α

Monadic left fold over a traversable structure, via foldMap.

def Traversable.foldrM E1 #

{α β : Type u} →
  {t : Type u → Type u} → [Traversable t] → {m : Type u → Type u} → [Monad m] → (α → β → m β) → β → t α → m β

Monadic right fold over a traversable structure, via foldMap.

def Traversable.mapFold E1 #

{α β : Type u} →
  [inst : Monoid α] → [inst_1 : Monoid β] → (α →* β) → ApplicativeTransformation (Functor.Const α) (Functor.Const β)

theorem Traversable.Free.map_eq_map E0 #

∀ {α β : Type u} (f : α → β) (xs : List α), f <$> xs = ((FreeMonoid.map f) { toList := xs }).toList

theorem Traversable.foldl.unop_ofFreeMonoid E1 #

∀ {α β : Type u} (f : β → α → β) (xs : FreeMonoid α) (a : β),
  (CategoryTheory.ConcreteCategory.hom (MulOpposite.unop ((Monoid.Foldl.ofFreeMonoid f) xs)).asHom) a =
    List.foldl f a xs.toList

theorem Traversable.foldMap_hom E1 #

∀ {α β γ : Type u} {t : Type u → Type u} [inst : Traversable t] [LawfulTraversable t] [inst_2 : Monoid α]
  [inst_3 : Monoid β] (f : α →* β) (g : γ → α) (x : t γ), f (Traversable.foldMap g x) = Traversable.foldMap (⇑f ∘ g) x

theorem Traversable.foldMap_hom_free E1 #

∀ {α β : Type u} {t : Type u → Type u} [inst : Traversable t] [LawfulTraversable t] [inst_2 : Monoid β]
  (f : FreeMonoid α →* β) (x : t α),
  f (Traversable.foldMap FreeMonoid.of x) = Traversable.foldMap (⇑f ∘ FreeMonoid.of) x

theorem Traversable.foldl.ofFreeMonoid_comp_of E0 #

∀ {α β : Type u} (f : α → β → α), ⇑(Monoid.Foldl.ofFreeMonoid f) ∘ FreeMonoid.of = Monoid.Foldl.mk ∘ flip f

theorem Traversable.foldr.ofFreeMonoid_comp_of E0 #

∀ {α β : Type u} (f : β → α → α), ⇑(Monoid.Foldr.ofFreeMonoid f) ∘ FreeMonoid.of = Monoid.Foldr.mk ∘ f

theorem Traversable.foldlM.ofFreeMonoid_comp_of E0 #

∀ {α β : Type u} {m : Type u → Type u} [inst : Monad m] [inst_1 : LawfulMonad m] (f : α → β → m α),
  ⇑(Monoid.foldlM.ofFreeMonoid f) ∘ FreeMonoid.of = Monoid.foldlM.mk ∘ flip f

theorem Traversable.foldrM.ofFreeMonoid_comp_of E0 #

∀ {α β : Type u} {m : Type u → Type u} [inst : Monad m] [inst_1 : LawfulMonad m] (f : β → α → m α),
  ⇑(Monoid.foldrM.ofFreeMonoid f) ∘ FreeMonoid.of = Monoid.foldrM.mk ∘ f

theorem Traversable.toList_spec E1 #

∀ {α : Type u} {t : Type u → Type u} [inst : Traversable t] [LawfulTraversable t] (xs : t α),
  Traversable.toList xs = (Traversable.foldMap FreeMonoid.of xs).toList

theorem Traversable.foldMap_map E1 #

∀ {α β γ : Type u} {t : Type u → Type u} [inst : Traversable t] [LawfulTraversable t] [inst_2 : Monoid γ] (f : α → β)
  (g : β → γ) (xs : t α), Traversable.foldMap g (f <$> xs) = Traversable.foldMap (g ∘ f) xs

theorem Traversable.foldl_toList E1 #

∀ {α β : Type u} {t : Type u → Type u} [inst : Traversable t] [LawfulTraversable t] (f : α → β → α) (xs : t β) (x : α),
  Traversable.foldl f x xs = List.foldl f x (Traversable.toList xs)

theorem Traversable.foldr_toList E1 #

∀ {α β : Type u} {t : Type u → Type u} [inst : Traversable t] [LawfulTraversable t] (f : α → β → β) (xs : t α) (x : β),
  Traversable.foldr f x xs = List.foldr f x (Traversable.toList xs)

theorem Traversable.toList_map E1 #

∀ {α β : Type u} {t : Type u → Type u} [inst : Traversable t] [LawfulTraversable t] (f : α → β) (xs : t α),
  Traversable.toList (f <$> xs) = f <$> Traversable.toList xs

theorem Traversable.foldl_map E0 #

∀ {α β γ : Type u} {t : Type u → Type u} [inst : Traversable t] [LawfulTraversable t] (g : β → γ) (f : α → γ → α)
  (a : α) (l : t β), Traversable.foldl f a (g <$> l) = Traversable.foldl (fun x y ↦ f x (g y)) a l

theorem Traversable.foldr_map E0 #

∀ {α β γ : Type u} {t : Type u → Type u} [inst : Traversable t] [LawfulTraversable t] (g : β → γ) (f : γ → α → α)
  (a : α) (l : t β), Traversable.foldr f a (g <$> l) = Traversable.foldr (f ∘ g) a l

theorem Traversable.toList_eq_self E0 #

∀ {α : Type u} {xs : List α}, Traversable.toList xs = xs

theorem Traversable.length_toList E1 #

∀ {α : Type u} {t : Type u → Type u} [inst : Traversable t] [LawfulTraversable t] {xs : t α},
  Traversable.length xs = (Traversable.toList xs).length

theorem Traversable.foldlM_toList E1 #

∀ {α β : Type u} {t : Type u → Type u} [inst : Traversable t] [LawfulTraversable t] {m : Type u → Type u}
  [inst_2 : Monad m] [LawfulMonad m] {f : α → β → m α} {x : α} {xs : t β},
  Traversable.foldlM f x xs = List.foldlM f x (Traversable.toList xs)

theorem Traversable.foldrM_toList E1 #

∀ {α β : Type u} {t : Type u → Type u} [inst : Traversable t] [LawfulTraversable t] {m : Type u → Type u}
  [inst_2 : Monad m] [LawfulMonad m] (f : α → β → m β) (x : β) (xs : t α),
  Traversable.foldrM f x xs = List.foldrM f x (Traversable.toList xs)

theorem Traversable.foldlM_map E0 #

∀ {α β γ : Type u} {t : Type u → Type u} [inst : Traversable t] [LawfulTraversable t] {m : Type u → Type u}
  [inst_2 : Monad m] [LawfulMonad m] (g : β → γ) (f : α → γ → m α) (a : α) (l : t β),
  Traversable.foldlM f a (g <$> l) = Traversable.foldlM (fun x y ↦ f x (g y)) a l

theorem Traversable.foldrM_map E0 #

∀ {α β γ : Type u} {t : Type u → Type u} [inst : Traversable t] [LawfulTraversable t] {m : Type u → Type u}
  [inst_2 : Monad m] [LawfulMonad m] (g : β → γ) (f : γ → α → m α) (a : α) (l : t β),
  Traversable.foldrM f a (g <$> l) = Traversable.foldrM (f ∘ g) a l

Deprecated aliases

NameNew nameSince
Traversable.foldlmTraversable.foldlM2026-10-08
Traversable.foldlm.ofFreeMonoid_comp_ofTraversable.foldlM.ofFreeMonoid_comp_of2026-10-08
Traversable.foldlm_mapTraversable.foldlM_map2026-10-08
Traversable.foldlm_toListTraversable.foldlM_toList2026-10-08
Traversable.foldrmTraversable.foldrM2026-10-08
Traversable.foldrm.ofFreeMonoid_comp_ofTraversable.foldrM.ofFreeMonoid_comp_of2026-10-08
Traversable.foldrm_mapTraversable.foldrM_map2026-10-08
Traversable.foldrm_toListTraversable.foldrM_toList2026-10-08