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.Data.List.Fold

8 declarations. Mathlib docs · Source

Bird–Wadler duality of list folds

In their 1988 book Introduction to Functional Programming [birdwadler], Richard Bird and Philip Wadler stated three duality theorems between foldl and foldr. Denoting the combining function as f, the theorems are:

  1. If α = β and f is commutative and associative, l.foldl = l.foldr
  2. If f is left-commutative, l.foldl = l.foldr
  3. l.reverse.foldl = l.foldr and l.reverse.foldr = l.foldl

Note that f's type differs between Lean's foldl (β → α → β) and foldr (α → β → β), so flips need to be inserted judiciously. For the history behind this type difference see the appendix to [danvy], which uses a version of foldl where f : α → β → β to derive among other things a slight generalisation of the first theorem:

  1. If α = β, f is associative and a commutes with all x : α, l.foldl f a = l.foldr f a

Main declarations

  • List.foldl_eq_foldr_of_commute, List.foldl_eq_foldr: first duality theorem.
  • List.foldl_flip_eq_foldr, List.foldr_flip_eq_foldl: second duality theorem.

The third duality theorem is in the standard library under the names List.foldl_reverse, List.foldr_eq_foldl_reverse, List.foldr_reverse and List.foldl_eq_foldr_reverse.

theorem List.foldl_cons_nil E1 #

∀ {α : Type u_1} {l : List α}, List.foldl (flip List.cons) [] l = l.reverse

theorem List.foldl_cons_eq_apply_foldl E1 #

∀ {α : Type u_1} {β : Type u_2} {l : List α} {v : β → α → β} {a : α} {b : β} [hv : RightCommutative v],
  List.foldl v b (a :: l) = v (List.foldl v b l) a

theorem List.foldr_cons_eq_foldr_apply E0 #

∀ {α : Type u_1} {β : Type u_2} {l : List α} {f : α → β → β} {a : α} {b : β} [hf : LeftCommutative f],
  List.foldr f b (a :: l) = List.foldr f (f a b) l

theorem List.foldl1_eq_foldr1 E1 #

∀ {α : Type u_1} {l : List α} {f : α → α → α} [ha : Std.Associative f] {a b : α},
  f (List.foldl f a l) b = f a (List.foldr f b l)

theorem List.foldl_eq_foldr_of_commute E3 #

∀ {α : Type u_1} {l : List α} {a : α} {f : α → α → α} [Std.Associative f],
  (∀ (x : α), f a x = f x a) → List.foldl f a l = List.foldr f a l

First Bird–Wadler duality theorem.

theorem List.foldl_eq_foldr E3 #

∀ {α : Type u_1} {l : List α} {a : α} {f : α → α → α} [hf : Std.Commutative f] [Std.Associative f],
  List.foldl f a l = List.foldr f a l

First Bird–Wadler duality theorem for commutative functions.

theorem List.foldl_flip_eq_foldr E3 #

∀ {α : Type u_1} {β : Type u_2} {l : List α} {f : α → β → β} {b : β} [LeftCommutative f],
  List.foldl (flip f) b l = List.foldr f b l

Second Bird–Wadler duality theorem.

theorem List.foldr_flip_eq_foldl E3 #

∀ {α : Type u_1} {β : Type u_2} {l : List α} {v : β → α → β} {b : β} [RightCommutative v],
  List.foldr (flip v) b l = List.foldl v b l

Second Bird–Wadler duality theorem.