4.3. Discrete Mathematics

Discrete mathematics studies finite or countable structures: finite sets, counting, graphs, trees, formulas. It is the part of mathematics closest to computing, and not by accident: many of its objects are defined inductively, exactly like the data types we program in Lean. In this chapter, based on chapter 6 of Mathematics in Lean, we study Mathlib's finite sets (Finset) and finite types (Fintype), use these notions to formalize counting arguments, and finish by returning to a familiar topic — inductive types — now seen through the eyes of someone who wants to prove mathematical theorems about them.

namespace Ch26 open Finset

4.3.1. Finite Sets and Finite Types

4.3.1.1. The Finset Type

In the chapter on sets we saw that Set α is the type of predicates on α. Such a set may be infinite, and in general there is no way to compute with it: we cannot, for instance, list its elements or count them. The type Finset α, by contrast, represents finite sets of elements of α. Internally, a Finset α is a list without duplicates, considered up to the order of its elements; for this reason, many operations on Finset are computable.

To compute intersections, unions and differences, however, we need to decide whether two elements are equal, so these operations require an instance [DecidableEq α]. Without it, the expression s ∩ t is not even accepted.

section variable {α : Type*} [DecidableEq α] variable (a : α) (s t : Finset α) a ∈ s : Prop#check a ∈ s s ∩ t : Finset α#check s ∩ t end

As with sets, identities between finite sets can be proved by extensionality, reducing them to propositional tautologies — and the tauto tactic solves propositional tautologies automatically. Naturally, Mathlib also contains the corresponding lemmas.

section variable (a b c : Finset ℕ) example : a ∩ (b ∪ c) = (a ∩ b) ∪ (a ∩ c) := a:Finset ℕb:Finset ℕc:Finset ℕ⊢ a ∩ (b ∪ c) = a ∩ b ∪ a ∩ c a:Finset ℕb:Finset ℕc:Finset ℕx:ℕ⊢ x ∈ a ∩ (b ∪ c) ↔ x ∈ a ∩ b ∪ a ∩ c a:Finset ℕb:Finset ℕc:Finset ℕx:ℕ⊢ x ∈ a ∧ (x ∈ b ∨ x ∈ c) ↔ x ∈ a ∧ x ∈ b ∨ x ∈ a ∧ x ∈ c All goals completed! 🐙 example : a ∩ (b ∪ c) = (a ∩ b) ∪ (a ∩ c) := a:Finset ℕb:Finset ℕc:Finset ℕ⊢ a ∩ (b ∪ c) = a ∩ b ∪ a ∩ c All goals completed! 🐙 end

4.3.1.2. Building Finite Sets

The notation {0, 1, 2} builds a finite set from its elements. It is an abbreviation for insert 0 (insert 1 {2}). Since Finset ignores order and repetitions, sets written in different ways may be equal, and for concrete elements the decide tactic checks the equality by computation.

example : ({0, 1, 2} : Finset ℕ) = {1, 2, 0} := ⊢ {0, 1, 2} = {1, 2, 0} All goals completed! 🐙 example (x : ℕ) : ({x, x} : Finset ℕ) = {x} := x:ℕ⊢ {x, x} = {x} All goals completed! 🐙 example (s : Finset ℕ) (a : ℕ) (h : a ∉ s) : (insert a s).erase a = s := Finset.erase_insert h

Other ways of building finite sets are filtering, {m ∈ s | P m}, which is notation for s.filter P, the image s.image f, the Cartesian product s ×ˢ t, and the power set s.powerset.

example (n : ℕ) : {m ∈ range n | Even m} = (range n).filter Even := rfl example : {m ∈ range 10 | Even m} = {0, 2, 4, 6, 8} := ⊢ {m ∈ range 10 | Even m} = {0, 2, 4, 6, 8} All goals completed! 🐙 example : (range 5).image (fun x => x * 2) = {x ∈ range 10 | Even x} := ⊢ image (fun x => x * 2) (range 5) = {x ∈ range 10 | Even x} All goals completed! 🐙

Sums and products, which we used in the previous chapter, are special cases of a more general operation, the fold (Finset.fold), which combines the values of a function over the elements of a finite set using an associative and commutative operation. Commutativity and associativity are essential: since the set has no order, the result cannot depend on the order in which the elements are combined.

30#eval (range 5).fold (fun x y : ℤ => x + y) 0 (fun n : ℕ => (n : ℤ) ^ 2)
30

4.3.1.3. Induction on Finite Sets

The induction principle Finset.induction_on, which we have already used, says that every finite set is obtained from ∅ by successively inserting new elements. As an example, we prove that a product of nonzero factors is nonzero:

example {α : Type*} [DecidableEq α] (f : α → ℕ) (s : Finset α) (h : ∀ x ∈ s, f x ≠ 0) : ∏ x ∈ s, f x ≠ 0 := α:Type u_1inst✝:DecidableEq αf:α → ℕs:Finset αh:∀ x ∈ s, f x ≠ 0⊢ ∏ x ∈ s, f x ≠ 0 induction s using Finset.induction_on with α:Type u_1inst✝:DecidableEq αf:α → ℕh:∀ x ∈ ∅, f x ≠ 0⊢ ∏ x ∈ ∅, f x ≠ 0 All goals completed! 🐙 α:Type u_1inst✝:DecidableEq αf:α → ℕa:αs:Finset αanins:a ∉ sih:(∀ x ∈ s, f x ≠ 0) → ∏ x ∈ s, f x ≠ 0h:∀ x ∈ insert a s, f x ≠ 0⊢ ∏ x ∈ insert a s, f x ≠ 0 α:Type u_1inst✝:DecidableEq αf:α → ℕa:αs:Finset αanins:a ∉ sih:(∀ x ∈ s, f x ≠ 0) → ∏ x ∈ s, f x ≠ 0h:∀ x ∈ insert a s, f x ≠ 0⊢ f a * ∏ x ∈ s, f x ≠ 0 α:Type u_1inst✝:DecidableEq αf:α → ℕa:αs:Finset αanins:a ∉ sih:(∀ x ∈ s, f x ≠ 0) → ∏ x ∈ s, f x ≠ 0h:∀ x ∈ insert a s, f x ≠ 0⊢ f a ≠ 0α:Type u_1inst✝:DecidableEq αf:α → ℕa:αs:Finset αanins:a ∉ sih:(∀ x ∈ s, f x ≠ 0) → ∏ x ∈ s, f x ≠ 0h:∀ x ∈ insert a s, f x ≠ 0⊢ ∏ x ∈ s, f x ≠ 0 α:Type u_1inst✝:DecidableEq αf:α → ℕa:αs:Finset αanins:a ∉ sih:(∀ x ∈ s, f x ≠ 0) → ∏ x ∈ s, f x ≠ 0h:∀ x ∈ insert a s, f x ≠ 0⊢ f a ≠ 0 α:Type u_1inst✝:DecidableEq αf:α → ℕa:αs:Finset αanins:a ∉ sih:(∀ x ∈ s, f x ≠ 0) → ∏ x ∈ s, f x ≠ 0h:∀ x ∈ insert a s, f x ≠ 0⊢ a ∈ insert a s All goals completed! 🐙 α:Type u_1inst✝:DecidableEq αf:α → ℕa:αs:Finset αanins:a ∉ sih:(∀ x ∈ s, f x ≠ 0) → ∏ x ∈ s, f x ≠ 0h:∀ x ∈ insert a s, f x ≠ 0⊢ ∏ x ∈ s, f x ≠ 0 α:Type u_1inst✝:DecidableEq αf:α → ℕa:αs:Finset αanins:a ∉ sih:(∀ x ∈ s, f x ≠ 0) → ∏ x ∈ s, f x ≠ 0h:∀ x ∈ insert a s, f x ≠ 0⊢ ∀ x ∈ s, f x ≠ 0 intro x α:Type u_1inst✝:DecidableEq αf:α → ℕa:αs:Finset αanins:a ∉ sih:(∀ x ∈ s, f x ≠ 0) → ∏ x ∈ s, f x ≠ 0h:∀ x ∈ insert a s, f x ≠ 0x:αxs:x ∈ s⊢ f x ≠ 0 All goals completed! 🐙

The insert case receives the element a, the set s, the hypothesis anins : a ∉ s and the induction hypothesis. The hypothesis a ∉ s is what allows us to use Finset.prod_insert, which splits the factor f a off the product.

4.3.1.4. Minimum, Maximum and Cardinality

Every nonempty finite set of natural numbers has a least and a greatest element. The functions Finset.min' and Finset.max' take, in addition to the set, a proof that it is nonempty. (The variants Finset.min and Finset.max, without the proof, return a value in a type extended with an extra element, used for the empty set.)

example : Finset.Nonempty ({2, 6, 7} : Finset ℕ) := ⟨6, ⊢ 6 ∈ {2, 6, 7} All goals completed! 🐙⟩ example : Finset.min' {2, 6, 7} ⟨6, ⊢ 6 ∈ {2, 6, 7} All goals completed! 🐙⟩ = 2 := ⊢ {2, 6, 7}.min' ⋯ = 2 All goals completed! 🐙

The cardinality of s is written #s (or s.card). It can be seen as the sum of the constant function 1 over s:

5#eval #(range 5)
5
example (s : Finset ℕ) : #s = ∑ _i ∈ s, 1 := s:Finset ℕ⊢ #s = ∑ _i ∈ s, 1 All goals completed! 🐙

4.3.1.5. Finite Types

A type α is finite when it has an instance of Fintype, which provides a finite set Finset.univ containing every element of α. The cardinality of a finite type is Fintype.card α, defined as #(univ : Finset α). The types Fin n, Bool, products and sums of finite types, and function types between finite types are all finite, and the simplifier knows how to compute their cardinalities.

section variable {α : Type*} [Fintype α] example : ∀ x : α, x ∈ Finset.univ := α:Type u_1inst✝:Fintype α⊢ ∀ (x : α), x ∈ univ α:Type u_1inst✝:Fintype αx:α⊢ x ∈ univ All goals completed! 🐙 example : Fintype.card α = #(Finset.univ : Finset α) := rfl end example : Fintype.card (Fin 5) = 5 := ⊢ Fintype.card (Fin 5) = 5 All goals completed! 🐙 example : Fintype.card (Fin 5 × Fin 3) = 15 := ⊢ Fintype.card (Fin 5 × Fin 3) = 15 All goals completed! 🐙

A finite set s : Finset α can also be viewed as a type: the type of elements of α that belong to s, the subtype {x : α // x ∈ s}. This type is finite, and its cardinality is #s.

example (s : Finset ℕ) : Fintype.card s = #s := s:Finset ℕ⊢ Fintype.card ↥s = #s All goals completed! 🐙

4.3.2. Counting Arguments

4.3.2.1. Basic Rules

Counting is one of the central activities of discrete mathematics, and Mathlib contains the basic counting rules: the product rule, the inclusion–exclusion principle for two sets, the sum rule for disjoint sets, and the fact that an injective function preserves cardinalities.

section variable {α β : Type*} [DecidableEq α] [DecidableEq β] variable (s t : Finset α) (f : α → β) example : #(s ×ˢ t) = #s * #t := α:Type u_1β:Type u_2inst✝¹:DecidableEq αinst✝:DecidableEq βs:Finset αt:Finset αf:α → β⊢ #(s ×ˢ t) = #s * #t All goals completed! 🐙 example : #(s ∪ t) = #s + #t - #(s ∩ t) := α:Type u_1β:Type u_2inst✝¹:DecidableEq αinst✝:DecidableEq βs:Finset αt:Finset αf:α → β⊢ #(s ∪ t) = #s + #t - #(s ∩ t) All goals completed! 🐙 example (h : Disjoint s t) : #(s ∪ t) = #s + #t := α:Type u_1β:Type u_2inst✝¹:DecidableEq αinst✝:DecidableEq βs:Finset αt:Finset αf:α → βh:Disjoint s t⊢ #(s ∪ t) = #s + #t All goals completed! 🐙 example (h : Function.Injective f) : #(s.image f) = #s := α:Type u_1β:Type u_2inst✝¹:DecidableEq αinst✝:DecidableEq βs:Finset αt:Finset αf:α → βh:Function.Injective f⊢ #(image f s) = #s All goals completed! 🐙 end

For finite types there are analogous rules, which the simplifier applies automatically: the number of pairs is the product, the number of elements of a disjoint sum is the sum, and the number of functions from Fin n to α is |\alpha|^n.

section open Fintype variable {α β : Type*} [Fintype α] [Fintype β] example : card (α × β) = card α * card β := α:Type u_1β:Type u_2inst✝¹:Fintype αinst✝:Fintype β⊢ Fintype.card (α × β) = Fintype.card α * Fintype.card β All goals completed! 🐙 example : card (α ⊕ β) = card α + card β := α:Type u_1β:Type u_2inst✝¹:Fintype αinst✝:Fintype β⊢ Fintype.card (α ⊕ β) = Fintype.card α + Fintype.card β All goals completed! 🐙 example (n : ℕ) : card (Fin n → α) = card α ^ n := α:Type u_1β:Type u_2inst✝¹:Fintype αinst✝:Fintype βn:ℕ⊢ Fintype.card (Fin n → α) = Fintype.card α ^ n All goals completed! 🐙 end

Combining these rules, we can count more elaborate sets. For example, if m ≥ n, the sets {0, …, n - 1} and {m, …, m + n - 1} are disjoint, and their union has 2n elements. The proof uses the sum rule, the injectivity of the translation i ↦ m + i, and omega for the arithmetic; disjointness is reduced, via the lemma Finset.disjoint_iff_ne, to the fact that no element of the first set is equal to an element of the second.

example (m n : ℕ) (h : m ≥ n) : #(range n ∪ (range n).image (fun i => m + i)) = 2 * n := m:ℕn:ℕh:m ≥ n⊢ #(range n ∪ image (fun i => m + i) (range n)) = 2 * n m:ℕn:ℕh:m ≥ n⊢ n + n = 2 * nm:ℕn:ℕh:m ≥ n⊢ Function.Injective fun i => m + im:ℕn:ℕh:m ≥ n⊢ Disjoint (range n) (image (fun i => m + i) (range n)) m:ℕn:ℕh:m ≥ n⊢ n + n = 2 * n All goals completed! 🐙 m:ℕn:ℕh:m ≥ n⊢ Function.Injective fun i => m + i All goals completed! 🐙 m:ℕn:ℕh:m ≥ n⊢ Disjoint (range n) (image (fun i => m + i) (range n)) m:ℕn:ℕh:m ≥ n⊢ ∀ a < n, ∀ a_2 < n, ¬a = m + a_2 All goals completed! 🐙

4.3.2.2. Counting a Triangle

Consider the set of pairs (i, j) with i < j ≤ n. It has n(n+1)/2 elements, and the classic proof decomposes the set into "columns": for each j, there are exactly j pairs of the form (i, j), so the total is 0 + 1 + \cdots + n.

def triangle (n : ℕ) : Finset (ℕ × ℕ) := {p ∈ range (n + 1) ×ˢ range (n + 1) | p.1 < p.2}

In the formalization, we write the triangle as an indexed union (Finset.biUnion) of the columns (range j).image (·, j), show that the columns are pairwise disjoint, and apply Finset.card_biUnion, which says that the cardinality of a disjoint union is the sum of the cardinalities. The resulting sum is computed by the lemma Finset.sum_range_id, Mathlib's version of Gauss's formula.

example (n : ℕ) : #(triangle n) = (n + 1) * n / 2 := n:ℕ⊢ #(triangle n) = (n + 1) * n / 2 have : triangle n = (range (n + 1)).biUnion (fun j => (range j).image (·, j)) := n:ℕ⊢ #(triangle n) = (n + 1) * n / 2 n:ℕp:ℕ × ℕ⊢ p ∈ triangle n ↔ p ∈ (range (n + 1)).biUnion fun j => image (fun x => (x, j)) (range j) n:ℕp:ℕ × ℕ⊢ (p.1 < n + 1 ∧ p.2 < n + 1) ∧ p.1 < p.2 ↔ ∃ a < n + 1, ∃ a_1 < a, (a_1, a) = p n:ℕp:ℕ × ℕ⊢ (p.1 < n + 1 ∧ p.2 < n + 1) ∧ p.1 < p.2 → ∃ a < n + 1, ∃ a_1 < a, (a_1, a) = pn:ℕp:ℕ × ℕ⊢ (∃ a < n + 1, ∃ a_1 < a, (a_1, a) = p) → (p.1 < n + 1 ∧ p.2 < n + 1) ∧ p.1 < p.2 n:ℕp:ℕ × ℕ⊢ (p.1 < n + 1 ∧ p.2 < n + 1) ∧ p.1 < p.2 → ∃ a < n + 1, ∃ a_1 < a, (a_1, a) = p n:ℕp:ℕ × ℕhp3:p.1 < p.2hp1:p.1 < n + 1hp2:p.2 < n + 1⊢ ∃ a < n + 1, ∃ a_1 < a, (a_1, a) = p All goals completed! 🐙 n:ℕp:ℕ × ℕ⊢ (∃ a < n + 1, ∃ a_1 < a, (a_1, a) = p) → (p.1 < n + 1 ∧ p.2 < n + 1) ∧ p.1 < p.2 n:ℕp1:ℕhp1:p1 < n + 1p2:ℕhp2:p2 < p1⊢ ((p2, p1).1 < n + 1 ∧ (p2, p1).2 < n + 1) ∧ (p2, p1).1 < (p2, p1).2 n:ℕp1:ℕhp1:p1 < n + 1p2:ℕhp2:p2 < p1⊢ (p2 < n + 1 ∧ p1 < n + 1) ∧ p2 < p1 All goals completed! 🐙 n:ℕthis:triangle n = (range (n + 1)).biUnion fun j => image (fun x => (x, j)) (range j)⊢ ∑ u ∈ range (n + 1), #(image (fun x => (x, u)) (range u)) = (n + 1) * n / 2n:ℕthis:triangle n = (range (n + 1)).biUnion fun j => image (fun x => (x, j)) (range j)⊢ (↑(range (n + 1))).PairwiseDisjoint fun j => image (fun x => (x, j)) (range j) n:ℕthis:triangle n = (range (n + 1)).biUnion fun j => image (fun x => (x, j)) (range j)⊢ (↑(range (n + 1))).PairwiseDisjoint fun j => image (fun x => (x, j)) (range j)n:ℕthis:triangle n = (range (n + 1)).biUnion fun j => image (fun x => (x, j)) (range j)⊢ ∑ u ∈ range (n + 1), #(image (fun x => (x, u)) (range u)) = (n + 1) * n / 2 n:ℕthis:triangle n = (range (n + 1)).biUnion fun j => image (fun x => (x, j)) (range j)⊢ (↑(range (n + 1))).PairwiseDisjoint fun j => image (fun x => (x, j)) (range j) intro x n:ℕthis:triangle n = (range (n + 1)).biUnion fun j => image (fun x => (x, j)) (range j)x:ℕa✝:x ∈ ↑(range (n + 1))⊢ ∀ ⦃y : ℕ⦄, y ∈ ↑(range (n + 1)) → x ≠ y → Function.onFun Disjoint (fun j => image (fun x => (x, j)) (range j)) x y n:ℕthis:triangle n = (range (n + 1)).biUnion fun j => image (fun x => (x, j)) (range j)x:ℕa✝:x ∈ ↑(range (n + 1))y:ℕ⊢ y ∈ ↑(range (n + 1)) → x ≠ y → Function.onFun Disjoint (fun j => image (fun x => (x, j)) (range j)) x y n:ℕthis:triangle n = (range (n + 1)).biUnion fun j => image (fun x => (x, j)) (range j)x:ℕa✝¹:x ∈ ↑(range (n + 1))y:ℕa✝:y ∈ ↑(range (n + 1))⊢ x ≠ y → Function.onFun Disjoint (fun j => image (fun x => (x, j)) (range j)) x y n:ℕthis:triangle n = (range (n + 1)).biUnion fun j => image (fun x => (x, j)) (range j)x:ℕa✝¹:x ∈ ↑(range (n + 1))y:ℕa✝:y ∈ ↑(range (n + 1))xney:x ≠ y⊢ Function.onFun Disjoint (fun j => image (fun x => (x, j)) (range j)) x y All goals completed! 🐙 n:ℕthis:triangle n = (range (n + 1)).biUnion fun j => image (fun x => (x, j)) (range j)⊢ ∑ u ∈ range (n + 1), #(image (fun x => (x, u)) (range u)) = ∑ i ∈ range (n + 1), in:ℕthis:triangle n = (range (n + 1)).biUnion fun j => image (fun x => (x, j)) (range j)⊢ ∑ i ∈ range (n + 1), i = (n + 1) * n / 2 n:ℕthis:triangle n = (range (n + 1)).biUnion fun j => image (fun x => (x, j)) (range j)⊢ ∑ u ∈ range (n + 1), #(image (fun x => (x, u)) (range u)) = ∑ i ∈ range (n + 1), i n:ℕthis:triangle n = (range (n + 1)).biUnion fun j => image (fun x => (x, j)) (range j)⊢ (fun u => #(image (fun x => (x, u)) (range u))) = fun i => i n:ℕthis:triangle n = (range (n + 1)).biUnion fun j => image (fun x => (x, j)) (range j)i:ℕ⊢ #(image (fun x => (x, i)) (range i)) = i n:ℕthis:triangle n = (range (n + 1)).biUnion fun j => image (fun x => (x, j)) (range j)i:ℕ⊢ Function.Injective fun x => (x, i) intro i1 n:ℕthis:triangle n = (range (n + 1)).biUnion fun j => image (fun x => (x, j)) (range j)i:ℕi1:ℕi2:ℕ⊢ (fun x => (x, i)) i1 = (fun x => (x, i)) i2 → i1 = i2 All goals completed! 🐙 n:ℕthis:triangle n = (range (n + 1)).biUnion fun j => image (fun x => (x, j)) (range j)⊢ (n + 1) * (n + 1 - 1) / 2 = (n + 1) * n / 2 All goals completed! 🐙

The swap tactic swaps the order of the goals, letting us deal first with the disjointness condition generated by card_biUnion; trans introduces an intermediate term in a chain of equalities.

4.3.2.3. Double Counting

One of the most powerful methods in combinatorics is double counting: counting the same collection in two different ways. Let r be a relation between the elements of s and those of t. Suppose that every a ∈ s is related to at least three elements of t, and that every b ∈ t is related to at most one element of s. Then t has at least three times as many elements as s. The proof counts the related pairs (a, b): grouping by a, there are at least 3 #s pairs; grouping by b, there are at most #t.

open Classical in theorem doubleCounting {α β : Type*} (s : Finset α) (t : Finset β) (r : α → β → Prop) (h_left : ∀ a ∈ s, 3 ≤ #{b ∈ t | r a b}) (h_right : ∀ b ∈ t, #{a ∈ s | r a b} ≤ 1) : 3 * #s ≤ #t := α:Type u_1β:Type u_2s:Finset αt:Finset βr:α → β → Proph_left:∀ a ∈ s, 3 ≤ #({b ∈ t | r a b})h_right:∀ b ∈ t, #({a ∈ s | r a b}) ≤ 1⊢ 3 * #s ≤ #t calc 3 * #s = ∑ a ∈ s, 3 := α:Type u_1β:Type u_2s:Finset αt:Finset βr:α → β → Proph_left:∀ a ∈ s, 3 ≤ #({b ∈ t | r a b})h_right:∀ b ∈ t, #({a ∈ s | r a b}) ≤ 1⊢ 3 * #s = ∑ a ∈ s, 3 All goals completed! 🐙 _ ≤ ∑ a ∈ s, #{b ∈ t | r a b} := sum_le_sum h_left _ = ∑ a ∈ s, ∑ b ∈ t, if r a b then 1 else 0 := α:Type u_1β:Type u_2s:Finset αt:Finset βr:α → β → Proph_left:∀ a ∈ s, 3 ≤ #({b ∈ t | r a b})h_right:∀ b ∈ t, #({a ∈ s | r a b}) ≤ 1⊢ ∑ a ∈ s, #({b ∈ t | r a b}) = ∑ a ∈ s, ∑ b ∈ t, if r a b then 1 else 0 All goals completed! 🐙 _ = ∑ b ∈ t, ∑ a ∈ s, if r a b then 1 else 0 := sum_comm _ = ∑ b ∈ t, #{a ∈ s | r a b} := α:Type u_1β:Type u_2s:Finset αt:Finset βr:α → β → Proph_left:∀ a ∈ s, 3 ≤ #({b ∈ t | r a b})h_right:∀ b ∈ t, #({a ∈ s | r a b}) ≤ 1⊢ (∑ b ∈ t, ∑ a ∈ s, if r a b then 1 else 0) = ∑ b ∈ t, #({a ∈ s | r a b}) All goals completed! 🐙 _ ≤ ∑ b ∈ t, 1 := sum_le_sum h_right _ ≤ #t := α:Type u_1β:Type u_2s:Finset αt:Finset βr:α → β → Proph_left:∀ a ∈ s, 3 ≤ #({b ∈ t | r a b})h_right:∀ b ∈ t, #({a ∈ s | r a b}) ≤ 1⊢ ∑ b ∈ t, 1 ≤ #t All goals completed! 🐙

The central step is Finset.sum_comm, which swaps the order of a double sum. The line open Classical in makes the arbitrary relation r decidable, which is needed to filter the sets and to write the if.

4.3.2.4. The Pigeonhole Principle

The pigeonhole principle states that if we distribute more than k · n objects into n boxes, some box receives more than k objects. Mathlib expresses it as Finset.exists_lt_card_fiber_of_mul_lt_card_of_maps_to: if f maps s into t and #t * k < #s, then some fiber of f has more than k elements.

As an application, we show that among any n + 1 numbers smaller than 2n there are two that are coprime. We distribute the numbers into the n boxes u / 2 (that is, we group 2t and 2t + 1 together). Some box receives two distinct numbers, which must then be consecutive — and consecutive numbers are coprime.

example {n : ℕ} (A : Finset ℕ) (hA : #A = n + 1) (hA' : A ⊆ range (2 * n)) : ∃ m ∈ A, ∃ k ∈ A, m ≠ k ∧ Nat.Coprime m k := n:ℕA:Finset ℕhA:#A = n + 1hA':A ⊆ range (2 * n)⊢ ∃ m ∈ A, ∃ k ∈ A, m ≠ k ∧ m.Coprime k have : ∃ t ∈ range n, 1 < #{u ∈ A | u / 2 = t} := n:ℕA:Finset ℕhA:#A = n + 1hA':A ⊆ range (2 * n)⊢ ∃ m ∈ A, ∃ k ∈ A, m ≠ k ∧ m.Coprime k n:ℕA:Finset ℕhA:#A = n + 1hA':A ⊆ range (2 * n)⊢ ∀ a ∈ A, a / 2 ∈ range nn:ℕA:Finset ℕhA:#A = n + 1hA':A ⊆ range (2 * n)⊢ #(range n) * 1 < #A n:ℕA:Finset ℕhA:#A = n + 1hA':A ⊆ range (2 * n)⊢ ∀ a ∈ A, a / 2 ∈ range n intro u n:ℕA:Finset ℕhA:#A = n + 1hA':A ⊆ range (2 * n)u:ℕhu:u ∈ A⊢ u / 2 ∈ range n n:ℕA:Finset ℕhA:#A = n + 1hA':A ⊆ range (2 * n)u:ℕhu:u ∈ Athis:u ∈ range (2 * n)⊢ u / 2 ∈ range n n:ℕA:Finset ℕhA:#A = n + 1hA':A ⊆ range (2 * n)u:ℕhu:u ∈ Athis:u < 2 * n⊢ u / 2 < n All goals completed! 🐙 n:ℕA:Finset ℕhA:#A = n + 1hA':A ⊆ range (2 * n)⊢ #(range n) * 1 < #A All goals completed! 🐙 n:ℕA:Finset ℕhA:#A = n + 1hA':A ⊆ range (2 * n)t:ℕleft✝:t ∈ range nht':1 < #({u ∈ A | u / 2 = t})⊢ ∃ m ∈ A, ∃ k ∈ A, m ≠ k ∧ m.Coprime k n:ℕA:Finset ℕhA:#A = n + 1hA':A ⊆ range (2 * n)t:ℕleft✝:t ∈ range nht':∃ a, (a ∈ A ∧ a / 2 = t) ∧ ∃ b, (b ∈ A ∧ b / 2 = t) ∧ a ≠ b⊢ ∃ m ∈ A, ∃ k ∈ A, m ≠ k ∧ m.Coprime k n:ℕA:Finset ℕhA:#A = n + 1hA':A ⊆ range (2 * n)t:ℕleft✝:t ∈ range nm:ℕhm:m ∈ Ahm':m / 2 = tk:ℕhmk:m ≠ khk:k ∈ Ahk':k / 2 = t⊢ ∃ m ∈ A, ∃ k ∈ A, m ≠ k ∧ m.Coprime k n:ℕA:Finset ℕhA:#A = n + 1hA':A ⊆ range (2 * n)t:ℕleft✝:t ∈ range nm:ℕhm:m ∈ Ahm':m / 2 = tk:ℕhmk:m ≠ khk:k ∈ Ahk':k / 2 = t⊢ m.Coprime k have : m = k + 1 ∨ k = m + 1 := n:ℕA:Finset ℕhA:#A = n + 1hA':A ⊆ range (2 * n)⊢ ∃ m ∈ A, ∃ k ∈ A, m ≠ k ∧ m.Coprime k All goals completed! 🐙 n:ℕA:Finset ℕhA:#A = n + 1hA':A ⊆ range (2 * n)t:ℕleft✝:t ∈ range nk:ℕhk:k ∈ Ahk':k / 2 = thm:k + 1 ∈ Ahm':(k + 1) / 2 = thmk:k + 1 ≠ k⊢ (k + 1).Coprime kn:ℕA:Finset ℕhA:#A = n + 1hA':A ⊆ range (2 * n)t:ℕleft✝:t ∈ range nm:ℕhm:m ∈ Ahm':m / 2 = thmk:m ≠ m + 1hk:m + 1 ∈ Ahk':(m + 1) / 2 = t⊢ m.Coprime (m + 1) n:ℕA:Finset ℕhA:#A = n + 1hA':A ⊆ range (2 * n)t:ℕleft✝:t ∈ range nk:ℕhk:k ∈ Ahk':k / 2 = thm:k + 1 ∈ Ahm':(k + 1) / 2 = thmk:k + 1 ≠ k⊢ (k + 1).Coprime k All goals completed! 🐙 n:ℕA:Finset ℕhA:#A = n + 1hA':A ⊆ range (2 * n)t:ℕleft✝:t ∈ range nm:ℕhm:m ∈ Ahm':m / 2 = thmk:m ≠ m + 1hk:m + 1 ∈ Ahk':(m + 1) / 2 = t⊢ m.Coprime (m + 1) All goals completed! 🐙

4.3.3. Inductively Defined Types

4.3.3.1. Binary Trees

In Part II we defined many inductive types and proved properties about them. From the point of view of discrete mathematics, an inductive type is a family of finite structures, and the recursive functions on it define measures and transformations of these structures. As an example, consider unlabeled binary trees:

inductive BinTree where | empty : BinTree | node : BinTree → BinTree → BinTree namespace BinTree def size : BinTree → ℕ | empty => 0 | node l r => size l + size r + 1 def depth : BinTree → ℕ | empty => 0 | node l r => max (depth l) (depth r) + 1

A tree of depth d has at most 2ᵈ - 1 nodes. The proof by structural induction, written as a recursive definition, uses the gcongr tactic, which reduces an inequality between expressions of the same shape to inequalities between the parts that differ.

theorem size_le : ∀ t : BinTree, size t ≤ 2 ^ depth t - 1 | empty => Nat.zero_le _ l:BinTreer:BinTree⊢ (l.node r).size ≤ 2 ^ (l.node r).depth - 1 l:BinTreer:BinTree⊢ (l.node r).size ≤ 2 ^ (l.node r).depth - 1 l:BinTreer:BinTree⊢ l.size + r.size + 1 ≤ 2 ^ (max l.depth r.depth + 1) - 1 calc l.size + r.size + 1 ≤ (2 ^ l.depth - 1) + (2 ^ r.depth - 1) + 1 := l:BinTreer:BinTree⊢ l.size + r.size + 1 ≤ 2 ^ l.depth - 1 + (2 ^ r.depth - 1) + 1 l:BinTreer:BinTree⊢ l.size ≤ 2 ^ l.depth - 1l:BinTreer:BinTree⊢ r.size ≤ 2 ^ r.depth - 1 l:BinTreer:BinTree⊢ l.size ≤ 2 ^ l.depth - 1l:BinTreer:BinTree⊢ r.size ≤ 2 ^ r.depth - 1 All goals completed! 🐙 _ ≤ (2 ^ max l.depth r.depth - 1) + (2 ^ max l.depth r.depth - 1) + 1 := l:BinTreer:BinTree⊢ 2 ^ l.depth - 1 + (2 ^ r.depth - 1) + 1 ≤ 2 ^ max l.depth r.depth - 1 + (2 ^ max l.depth r.depth - 1) + 1 l:BinTreer:BinTree⊢ 1 ≤ 2l:BinTreer:BinTree⊢ l.depth ≤ max l.depth r.depthl:BinTreer:BinTree⊢ 1 ≤ 2l:BinTreer:BinTree⊢ r.depth ≤ max l.depth r.depth l:BinTreer:BinTree⊢ 1 ≤ 2l:BinTreer:BinTree⊢ l.depth ≤ max l.depth r.depthl:BinTreer:BinTree⊢ 1 ≤ 2l:BinTreer:BinTree⊢ r.depth ≤ max l.depth r.depth All goals completed! 🐙 _ ≤ 2 ^ (max l.depth r.depth + 1) - 1 := l:BinTreer:BinTree⊢ 2 ^ max l.depth r.depth - 1 + (2 ^ max l.depth r.depth - 1) + 1 ≤ 2 ^ (max l.depth r.depth + 1) - 1 have : 0 < 2 ^ max l.depth r.depth := l:BinTreer:BinTree⊢ 2 ^ max l.depth r.depth - 1 + (2 ^ max l.depth r.depth - 1) + 1 ≤ 2 ^ (max l.depth r.depth + 1) - 1 All goals completed! 🐙 All goals completed! 🐙

In the last step, omega treats 2 ^ max l.depth r.depth as a variable x and checks that (x - 1) + (x - 1) + 1 ≤ 2x - 1 when x > 0 (the fact 2 ^ (k + 1) = 2 · 2ᵏ is recognized by the tactic). A natural transformation on trees is mirroring, which swaps the left and right subtrees at every node. It preserves size:

def flip : BinTree → BinTree | empty => empty | node l r => node (flip r) (flip l) example : flip (node (node empty (node empty empty)) (node empty empty)) = node (node empty empty) (node (node empty empty) empty) := rfl theorem size_flip : ∀ t, size (flip t) = size t | empty => rfl l:BinTreer:BinTree⊢ (l.node r).flip.size = (l.node r).size l:BinTreer:BinTree⊢ (l.node r).flip.size = (l.node r).size l:BinTreer:BinTree⊢ r.size + l.size + 1 = l.size + r.size + 1 All goals completed! 🐙 end BinTree

4.3.3.2. Propositional Formulas

In Part I we studied propositional logic informally. Now we can formalize it: formulas are an inductive type, and the semantics is a recursive function that evaluates a formula under a valuation of the variables.

inductive PropForm : Type where | var (n : ℕ) : PropForm | fls : PropForm | conj (A B : PropForm) : PropForm | disj (A B : PropForm) : PropForm | impl (A B : PropForm) : PropForm namespace PropForm def eval : PropForm → (ℕ → Bool) → Bool | var n, v => v n | fls, _ => false | conj A B, v => A.eval v && B.eval v | disj A B, v => A.eval v || B.eval v | impl A B, v => !A.eval v || B.eval v def vars : PropForm → Finset ℕ | var n => {n} | fls => ∅ | conj A B => A.vars ∪ B.vars | disj A B => A.vars ∪ B.vars | impl A B => A.vars ∪ B.vars

Note that vars returns a finite set — yet another use of Finset. A fundamental fact, intuitively obvious, is that the value of a formula depends only on the variables that occur in it: two valuations that agree on those variables assign the same value to the formula. The proof is a structural induction in which each case is solved by simp_all, fed with the induction hypotheses.

theorem eval_eq_eval : ∀ (A : PropForm) (v1 v2 : ℕ → Bool), (∀ n ∈ A.vars, v1 n = v2 n) → A.eval v1 = A.eval v2 n:ℕv1:ℕ → Boolv2:ℕ → Boolh:∀ n_1 ∈ (var n).vars, v1 n_1 = v2 n_1⊢ (var n).eval v1 = (var n).eval v2 n:ℕv1:ℕ → Boolv2:ℕ → Boolh:∀ n_1 ∈ (var n).vars, v1 n_1 = v2 n_1⊢ (var n).eval v1 = (var n).eval v2 All goals completed! 🐙 v1:ℕ → Boolv2:ℕ → Boolh:∀ n ∈ fls.vars, v1 n = v2 n⊢ fls.eval v1 = fls.eval v2 v1:ℕ → Boolv2:ℕ → Boolh:∀ n ∈ fls.vars, v1 n = v2 n⊢ fls.eval v1 = fls.eval v2 All goals completed! 🐙 A:PropFormB:PropFormv1:ℕ → Boolv2:ℕ → Boolh:∀ n ∈ (A.conj B).vars, v1 n = v2 n⊢ (A.conj B).eval v1 = (A.conj B).eval v2 A:PropFormB:PropFormv1:ℕ → Boolv2:ℕ → Boolh:∀ n ∈ (A.conj B).vars, v1 n = v2 n⊢ (A.conj B).eval v1 = (A.conj B).eval v2 All goals completed! 🐙 A:PropFormB:PropFormv1:ℕ → Boolv2:ℕ → Boolh:∀ n ∈ (A.disj B).vars, v1 n = v2 n⊢ (A.disj B).eval v1 = (A.disj B).eval v2 A:PropFormB:PropFormv1:ℕ → Boolv2:ℕ → Boolh:∀ n ∈ (A.disj B).vars, v1 n = v2 n⊢ (A.disj B).eval v1 = (A.disj B).eval v2 All goals completed! 🐙 A:PropFormB:PropFormv1:ℕ → Boolv2:ℕ → Boolh:∀ n ∈ (A.impl B).vars, v1 n = v2 n⊢ (A.impl B).eval v1 = (A.impl B).eval v2 A:PropFormB:PropFormv1:ℕ → Boolv2:ℕ → Boolh:∀ n ∈ (A.impl B).vars, v1 n = v2 n⊢ (A.impl B).eval v1 = (A.impl B).eval v2 All goals completed! 🐙

Finally, substitution A.subst m C replaces every occurrence of the variable m in A by the formula C. The substitution lemma relates substitution and evaluation: evaluating A.subst m C under v is the same as evaluating A under the valuation that assigns to m the value of C and agrees with v on all other variables.

def subst : PropForm → ℕ → PropForm → PropForm | var n, m, C => if n = m then C else var n | fls, _, _ => fls | conj A B, m, C => conj (A.subst m C) (B.subst m C) | disj A B, m, C => disj (A.subst m C) (B.subst m C) | impl A B, m, C => impl (A.subst m C) (B.subst m C) theorem subst_eval_eq : ∀ (A : PropForm) (n : ℕ) (C : PropForm) (v : ℕ → Bool), (A.subst n C).eval v = A.eval (fun m => if m = n then C.eval v else v m) m:ℕn:ℕC:PropFormv:ℕ → Bool⊢ ((var m).subst n C).eval v = (var m).eval fun m => if m = n then C.eval v else v m m:ℕn:ℕC:PropFormv:ℕ → Bool⊢ ((var m).subst n C).eval v = (var m).eval fun m => if m = n then C.eval v else v m m:ℕn:ℕC:PropFormv:ℕ → Boolh:m = n⊢ ((var m).subst n C).eval v = (var m).eval fun m => if m = n then C.eval v else v mm:ℕn:ℕC:PropFormv:ℕ → Boolh:¬m = n⊢ ((var m).subst n C).eval v = (var m).eval fun m => if m = n then C.eval v else v m m:ℕn:ℕC:PropFormv:ℕ → Boolh:m = n⊢ ((var m).subst n C).eval v = (var m).eval fun m => if m = n then C.eval v else v mm:ℕn:ℕC:PropFormv:ℕ → Boolh:¬m = n⊢ ((var m).subst n C).eval v = (var m).eval fun m => if m = n then C.eval v else v m All goals completed! 🐙 | fls, _, _, _ => rfl A:PropFormB:PropFormn:ℕC:PropFormv:ℕ → Bool⊢ ((A.conj B).subst n C).eval v = (A.conj B).eval fun m => if m = n then C.eval v else v m A:PropFormB:PropFormn:ℕC:PropFormv:ℕ → Bool⊢ ((A.conj B).subst n C).eval v = (A.conj B).eval fun m => if m = n then C.eval v else v m All goals completed! 🐙 A:PropFormB:PropFormn:ℕC:PropFormv:ℕ → Bool⊢ ((A.disj B).subst n C).eval v = (A.disj B).eval fun m => if m = n then C.eval v else v m A:PropFormB:PropFormn:ℕC:PropFormv:ℕ → Bool⊢ ((A.disj B).subst n C).eval v = (A.disj B).eval fun m => if m = n then C.eval v else v m All goals completed! 🐙 A:PropFormB:PropFormn:ℕC:PropFormv:ℕ → Bool⊢ ((A.impl B).subst n C).eval v = (A.impl B).eval fun m => if m = n then C.eval v else v m A:PropFormB:PropFormn:ℕC:PropFormv:ℕ → Bool⊢ ((A.impl B).subst n C).eval v = (A.impl B).eval fun m => if m = n then C.eval v else v m All goals completed! 🐙 end PropForm

Results like these are the starting point for formalizing the theory of propositional logic: the soundness and completeness of deductive systems, the equivalence between formulas and their normal forms, or the correctness of a SAT solver.

end Ch26

4.3.4. Exercises

  1. Prove, for r s t : Finset ℕ, that (r ∪ s) ∩ (r ∪ t) = r ∪ s ∩ t and that (r \ s) \ t = r \ (s ∪ t).

  2. Let triangle' n = {p ∈ range n ×ˢ range n | p.1 ≤ p.2}. Prove that #(triangle' n) = #(triangle n). Hint: find an explicit bijection and use Finset.card_bij or Finset.card_image_of_injOn.

  3. Prove that Fintype.card (Bool → Fin 3) = 9 and that #(range n).powerset = 2 ^ n.

  4. Prove that depth t ≤ size t for every binary tree t.

  5. Prove that depth (flip t) = depth t and that flip (flip t) = t.

  6. Define a function PropForm.neg that maps A to impl A fls and prove that (neg A).eval v = !A.eval v.

  7. Prove that if n ∉ A.vars, then A.subst n C = A.

  8. Adapt the proof of Ch26.doubleCounting: if every a ∈ s is related to at least k elements of t and every b ∈ t to at most l elements of s, then k * #s ≤ l * #t.