Bird–Wadler duality of list folds #
In their 1988 book Introduction to Functional Programming [BW88],
Richard Bird and Philip Wadler stated three duality theorems between foldl and foldr.
Denoting the combining function as f, the theorems are:
- If
α = βandfis commutative and associative,l.foldl = l.foldr - If
fis left-commutative,l.foldl = l.foldr l.reverse.foldl = l.foldrandl.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 [Dan23], which uses a version of foldl where f : α → β → β to derive
among other things a slight generalisation of the first theorem:
- If
α = β,fis associative andacommutes with allx : α,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.
First Bird–Wadler duality theorem.
First Bird–Wadler duality theorem for commutative functions.
Second Bird–Wadler duality theorem.
Second Bird–Wadler duality theorem.
Alias of List.foldl_cons_eq_apply_foldl.
Alias of List.foldr_cons_eq_foldr_apply.
Alias of List.foldr_flip_eq_foldl.
Second Bird–Wadler duality theorem.
Alias of List.foldl_cons_eq_apply_foldl.
Alias of List.foldl1_eq_foldr1.
Alias of List.foldl_cons_eq_apply_foldl.