4.1. Sets and Functions

In the previous parts we used Lean mainly to reason about programs: we defined functions, inductive types and semantics, and proved properties about them. In this final part we shift our focus and use Lean as a tool for formalizing mathematics. The Mathlib library, which we have already been using as a source of tactics, contains hundreds of thousands of definitions and theorems, ranging from elementary set theory to advanced topics in algebra, analysis and topology. The three chapters of this part closely follow chapters 4, 5 and 6 of the book Mathematics in Lean, by Jeremy Avigad and Patrick Massot, available online.

We begin with the most basic vocabulary of mathematics: sets and functions. We will see how Mathlib represents sets, how to prove inclusions and equalities between them, and how to work with images and preimages of functions. We finish with two classic theorems: Cantor's theorem, which states that no set is in bijection with its power set, and the Schröder–Bernstein theorem, which constructs a bijection out of two injections.

namespace Ch24 open Set Function

4.1.1. Sets

4.1.1.1. Sets as Predicates

In Zermelo–Fraenkel set theory, sets are the primitive objects and everything else — numbers, pairs, functions — is encoded in terms of them. In Lean's type theory the situation is reversed: the primitive objects are types, and a set of elements of a type α is simply a predicate on α. In Mathlib, the type Set α is defined as α → Prop, and membership x ∈ s means, by definition, s x.

This choice has an important consequence: all elements of a set s : Set α have the same type α. In Lean it makes no sense to ask whether the number 2 belongs to a set of functions. In mathematical practice this is rarely a limitation, since we almost always work with subsets of a fixed universe — the natural numbers, the reals, the points of a space — and it is this universe that we take as the type α.

Mathlib provides the usual operations: inclusion s ⊆ t, intersection s ∩ t, union s ∪ t, difference s \ t, complement sᶜ, the empty set ∅ and the universal set univ, which contains every element of α. We can also write sets using set-builder notation, as in { n | Even n }. Each of these operations is defined in terms of the logical connectives: x ∈ s ∩ t is, by definition, x ∈ s ∧ x ∈ t; x ∈ s ∪ t is x ∈ s ∨ x ∈ t; and s ⊆ t is ∀ x, x ∈ s → x ∈ t. As a result, proofs about sets turn out to be proofs in propositional and predicate logic — exactly the kind of proof we already know how to write.

variable {α : Type*} variable (s t u : Set α) example : s ⊆ t ↔ ∀ ⦃x⦄, x ∈ s → x ∈ t := Iff.rfl example (x : α) : x ∈ s ∩ t ↔ x ∈ s ∧ x ∈ t := Iff.rfl example (x : α) : x ∈ s ∪ t ↔ x ∈ s ∨ x ∈ t := Iff.rfl

The three examples above are proved by Iff.rfl: the two sides are definitionally equal. The notation ⦃x⦄ marks a strict implicit argument: it is only inferred once the following argument, the proof of x ∈ s, is supplied.

4.1.1.2. Proving Inclusions

To prove an inclusion s ⊆ t, we introduce a generic element x and a proof that x ∈ s, and show that x ∈ t. Since the operations on sets reduce to connectives, we can directly use the tools for conjunctions and disjunctions: the projections .1 and .2, anonymous constructors ⟨_, _⟩, and the Or patterns of rintro.

example (h : s ⊆ t) : s ∩ u ⊆ t ∩ u := α:Type u_1s:Set αt:Set αu:Set αh:s ⊆ t⊢ s ∩ u ⊆ t ∩ u intro x α:Type u_1s:Set αt:Set αu:Set αh:s ⊆ tx:αxsu:x ∈ s ∩ u⊢ x ∈ t ∩ u All goals completed! 🐙

In the proof above, xsu : x ∈ s ∩ u is treated as a conjunction, even though Lean displays it as membership in an intersection. If we prefer to make the rewriting explicit, Mathlib provides lemmas such as Set.mem_inter_iff and Set.mem_union, but they are rarely needed. The rintro tactic, which combines intro with pattern matching, is especially useful: we can take a hypothesis apart at the very moment we introduce it.

example : s ∩ (t ∪ u) ⊆ s ∩ t ∪ s ∩ u := α:Type u_1s:Set αt:Set αu:Set α⊢ s ∩ (t ∪ u) ⊆ s ∩ t ∪ s ∩ u α:Type u_1s:Set αt:Set αu:Set αx:αxs:x ∈ sxt:x ∈ t⊢ x ∈ s ∩ t ∪ s ∩ uα:Type u_1s:Set αt:Set αu:Set αx:αxs:x ∈ sxu:x ∈ u⊢ x ∈ s ∩ t ∪ s ∩ u α:Type u_1s:Set αt:Set αu:Set αx:αxs:x ∈ sxt:x ∈ t⊢ x ∈ s ∩ t ∪ s ∩ u α:Type u_1s:Set αt:Set αu:Set αx:αxs:x ∈ sxt:x ∈ t⊢ x ∈ s ∩ t All goals completed! 🐙 α:Type u_1s:Set αt:Set αu:Set αx:αxs:x ∈ sxu:x ∈ u⊢ x ∈ s ∩ t ∪ s ∩ u α:Type u_1s:Set αt:Set αu:Set αx:αxs:x ∈ sxu:x ∈ u⊢ x ∈ s ∩ u All goals completed! 🐙

The pattern ⟨xs, xt | xu⟩ destructs the conjunction and then performs a case analysis on the disjunction, producing two goals. The set difference x ∈ s \ t means x ∈ s ∧ x ∉ t, and the negation x ∉ t is an implication x ∈ t → False. The following example combines all of these ideas:

example : (s \ t) \ u ⊆ s \ (t ∪ u) := α:Type u_1s:Set αt:Set αu:Set α⊢ (s \ t) \ u ⊆ s \ (t ∪ u) α:Type u_1s:Set αt:Set αu:Set αx:αxnu:x ∉ uxs:x ∈ sxnt:x ∉ t⊢ x ∈ s \ (t ∪ u) α:Type u_1s:Set αt:Set αu:Set αx:αxnu:x ∉ uxs:x ∈ sxnt:x ∉ t⊢ x ∉ t ∪ u α:Type u_1s:Set αt:Set αu:Set αx:αxnu:x ∉ uxs:x ∈ sxnt:x ∉ txt:x ∈ t⊢ Falseα:Type u_1s:Set αt:Set αu:Set αx:αxnu:x ∉ uxs:x ∈ sxnt:x ∉ txu:x ∈ u⊢ False α:Type u_1s:Set αt:Set αu:Set αx:αxnu:x ∉ uxs:x ∈ sxnt:x ∉ txt:x ∈ t⊢ Falseα:Type u_1s:Set αt:Set αu:Set αx:αxnu:x ∉ uxs:x ∈ sxnt:x ∉ txu:x ∈ u⊢ False All goals completed! 🐙

The tactic use xs supplies the first component of the conjunction x ∈ s ∧ x ∉ t ∪ u, leaving the second one as the goal. To prove the negation, we introduce the hypothesis x ∈ t ∪ u, split into the two cases, and in each one contradiction finds a hypothesis and its negation in the context.

4.1.1.3. Set Equality and Extensionality

Two sets are equal when they have the same elements. This is the principle of extensionality, and in Lean it is applied by the ext tactic: to prove s = t, ext x reduces the goal to x ∈ s ↔ x ∈ t.

example : s ∩ t = t ∩ s := α:Type u_1s:Set αt:Set αu:Set α⊢ s ∩ t = t ∩ s α:Type u_1s:Set αt:Set αu:Set αx:α⊢ x ∈ s ∩ t ↔ x ∈ t ∩ s α:Type u_1s:Set αt:Set αu:Set αx:α⊢ x ∈ s ∧ x ∈ t ↔ x ∈ t ∧ x ∈ s α:Type u_1s:Set αt:Set αu:Set αx:α⊢ x ∈ s ∧ x ∈ t → x ∈ t ∧ x ∈ sα:Type u_1s:Set αt:Set αu:Set αx:α⊢ x ∈ t ∧ x ∈ s → x ∈ s ∧ x ∈ t α:Type u_1s:Set αt:Set αu:Set αx:α⊢ x ∈ s ∧ x ∈ t → x ∈ t ∧ x ∈ s α:Type u_1s:Set αt:Set αu:Set αx:αxs:x ∈ sxt:x ∈ t⊢ x ∈ t ∧ x ∈ s; All goals completed! 🐙 α:Type u_1s:Set αt:Set αu:Set αx:α⊢ x ∈ t ∧ x ∈ s → x ∈ s ∧ x ∈ t α:Type u_1s:Set αt:Set αu:Set αx:αxt:x ∈ txs:x ∈ s⊢ x ∈ s ∧ x ∈ t; All goals completed! 🐙

After ext, the goal is an equivalence between two conjunctions, which we prove by handling the two directions separately. Of course, the simplifier knows that conjunction is commutative, and the whole proof fits on one line:

example : s ∩ t = t ∩ s := α:Type u_1s:Set αt:Set αu:Set α⊢ s ∩ t = t ∩ s α:Type u_1s:Set αt:Set αu:Set αx:α⊢ x ∈ s ∩ t ↔ x ∈ t ∩ s; All goals completed! 🐙

An alternative to ext is to use the antisymmetry of inclusion, Set.Subset.antisymm: to prove s = t, it suffices to prove s ⊆ t and t ⊆ s. This is the usual way of proving set equalities in mathematical texts — "we show both inclusions".

example : s ∩ t = t ∩ s := α:Type u_1s:Set αt:Set αu:Set α⊢ s ∩ t = t ∩ s α:Type u_1s:Set αt:Set αu:Set α⊢ s ∩ t ⊆ t ∩ sα:Type u_1s:Set αt:Set αu:Set α⊢ t ∩ s ⊆ s ∩ t α:Type u_1s:Set αt:Set αu:Set α⊢ s ∩ t ⊆ t ∩ s α:Type u_1s:Set αt:Set αu:Set αx:αxs:x ∈ sxt:x ∈ t⊢ x ∈ t ∩ s; All goals completed! 🐙 α:Type u_1s:Set αt:Set αu:Set α⊢ t ∩ s ⊆ s ∩ t α:Type u_1s:Set αt:Set αu:Set αx:αxt:x ∈ txs:x ∈ s⊢ x ∈ s ∩ t; All goals completed! 🐙

4.1.1.4. Set-Builder Notation

The notation { x | P x } defines the set of elements satisfying P. By definition, y ∈ { x | P x } is simply P y. As an example, we define the sets of even and odd natural numbers and show that their union is the universe. The key point of the proof is the law of excluded middle: every natural number is either even or not.

def evens : Set ℕ := { n | Even n } def odds : Set ℕ := { n | ¬ Even n } example : evens ∪ odds = univ := α:Type u_1s:Set αt:Set αu:Set α⊢ evens ∪ odds = univ α:Type u_1s:Set αt:Set αu:Set α⊢ {n | Even n} ∪ {n | ¬Even n} = univ α:Type u_1s:Set αt:Set αu:Set αn:ℕ⊢ n ∈ {n | Even n} ∪ {n | ¬Even n} ↔ n ∈ univ α:Type u_1s:Set αt:Set αu:Set αn:ℕ⊢ Even n ∨ ¬Even n All goals completed! 🐙

The annotation -Nat.not_even_iff_odd removes that lemma from the simplifier's rule set; without it, simp would rewrite ¬ Even n as Odd n, and the goal would no longer be a literal instance of the excluded middle. The empty set and the universe are also defined by set-builder notation — as { x | False } and { x | True } — which explains the following two examples:

example (x : ℕ) (h : x ∈ (∅ : Set ℕ)) : False := h example (x : ℕ) : x ∈ (univ : Set ℕ) := trivial

4.1.1.5. Bounded Quantifiers

It is very common to quantify over the elements of a set: "for every x ∈ s, …" or "there exists x ∈ s such that …". Lean offers the notation ∀ x ∈ s, P x, which abbreviates ∀ x, x ∈ s → P x, and ∃ x ∈ s, P x, which abbreviates ∃ x, x ∈ s ∧ P x.

section variable (s t : Set ℕ) example (h₀ : ∀ x ∈ s, ¬ Even x) (h₁ : ∀ x ∈ s, Prime x) : ∀ x ∈ s, ¬ Even x ∧ Prime x := α:Type u_1s✝:Set αt✝:Set αu:Set αs:Set ℕt:Set ℕh₀:∀ x ∈ s, ¬Even xh₁:∀ x ∈ s, Prime x⊢ ∀ x ∈ s, ¬Even x ∧ Prime x intro x α:Type u_1s✝:Set αt✝:Set αu:Set αs:Set ℕt:Set ℕh₀:∀ x ∈ s, ¬Even xh₁:∀ x ∈ s, Prime xx:ℕxs:x ∈ s⊢ ¬Even x ∧ Prime x α:Type u_1s✝:Set αt✝:Set αu:Set αs:Set ℕt:Set ℕh₀:∀ x ∈ s, ¬Even xh₁:∀ x ∈ s, Prime xx:ℕxs:x ∈ s⊢ ¬Even xα:Type u_1s✝:Set αt✝:Set αu:Set αs:Set ℕt:Set ℕh₀:∀ x ∈ s, ¬Even xh₁:∀ x ∈ s, Prime xx:ℕxs:x ∈ s⊢ Prime x α:Type u_1s✝:Set αt✝:Set αu:Set αs:Set ℕt:Set ℕh₀:∀ x ∈ s, ¬Even xh₁:∀ x ∈ s, Prime xx:ℕxs:x ∈ s⊢ ¬Even x All goals completed! 🐙 α:Type u_1s✝:Set αt✝:Set αu:Set αs:Set ℕt:Set ℕh₀:∀ x ∈ s, ¬Even xh₁:∀ x ∈ s, Prime xx:ℕxs:x ∈ s⊢ Prime x All goals completed! 🐙 example (h : ∃ x ∈ s, ¬ Even x ∧ Prime x) : ∃ x ∈ s, Prime x := α:Type u_1s✝:Set αt✝:Set αu:Set αs:Set ℕt:Set ℕh:∃ x ∈ s, ¬Even x ∧ Prime x⊢ ∃ x ∈ s, Prime x α:Type u_1s✝:Set αt✝:Set αu:Set αs:Set ℕt:Set ℕx:ℕxs:x ∈ sleft✝:¬Even xprime_x:Prime x⊢ ∃ x ∈ s, Prime x All goals completed! 🐙 end

The example above mentions Prime, the general notion of a prime element in a commutative monoid: p is prime if it is neither zero nor a unit and, whenever p ∣ a * b, we have p ∣ a or p ∣ b. For the natural numbers, Mathlib also defines Nat.Prime, with the usual characterization (p ≥ 2 and the only divisors of p are 1 and p). The two notions coincide on the naturals, and the lemma Nat.prime_iff lets us move from one to the other:

example (n : ℕ) : Prime n ↔ Nat.Prime n := Nat.prime_iff.symm

We will use Nat.Prime extensively in the next chapter.

4.1.1.6. Indexed Unions and Intersections

Given a family of sets A : I → Set α, indexed by a type I, Mathlib defines the union ⋃ i, A i and the intersection ⋂ i, A i. Once again, membership reduces to logic: x ∈ ⋃ i, A i is equivalent to ∃ i, x ∈ A i (lemma Set.mem_iUnion) and x ∈ ⋂ i, A i is equivalent to ∀ i, x ∈ A i (lemma Set.mem_iInter). Indexed unions generalize binary union in the same way that the existential quantifier generalizes disjunction.

section variable {I : Type*} variable (A B : I → Set α) example : (s ∩ ⋃ i, A i) = ⋃ i, A i ∩ s := α:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set α⊢ s ∩ ⋃ i, A i = ⋃ i, A i ∩ s α:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:α⊢ x ∈ s ∩ ⋃ i, A i ↔ x ∈ ⋃ i, A i ∩ s α:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:α⊢ (x ∈ s ∧ ∃ i, x ∈ A i) ↔ ∃ i, x ∈ A i ∧ x ∈ s α:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:α⊢ (x ∈ s ∧ ∃ i, x ∈ A i) → ∃ i, x ∈ A i ∧ x ∈ sα:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:α⊢ (∃ i, x ∈ A i ∧ x ∈ s) → x ∈ s ∧ ∃ i, x ∈ A i α:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:α⊢ (x ∈ s ∧ ∃ i, x ∈ A i) → ∃ i, x ∈ A i ∧ x ∈ s α:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:αxs:x ∈ si:IxAi:x ∈ A i⊢ ∃ i, x ∈ A i ∧ x ∈ s All goals completed! 🐙 α:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:α⊢ (∃ i, x ∈ A i ∧ x ∈ s) → x ∈ s ∧ ∃ i, x ∈ A i α:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:αi:IxAi:x ∈ A ixs:x ∈ s⊢ x ∈ s ∧ ∃ i, x ∈ A i All goals completed! 🐙 example : (⋂ i, A i ∩ B i) = (⋂ i, A i) ∩ ⋂ i, B i := α:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set α⊢ ⋂ i, A i ∩ B i = (⋂ i, A i) ∩ ⋂ i, B i α:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:α⊢ x ∈ ⋂ i, A i ∩ B i ↔ x ∈ (⋂ i, A i) ∩ ⋂ i, B i α:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:α⊢ (∀ (i : I), x ∈ A i ∧ x ∈ B i) ↔ (∀ (i : I), x ∈ A i) ∧ ∀ (i : I), x ∈ B i α:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:α⊢ (∀ (i : I), x ∈ A i ∧ x ∈ B i) → (∀ (i : I), x ∈ A i) ∧ ∀ (i : I), x ∈ B iα:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:α⊢ ((∀ (i : I), x ∈ A i) ∧ ∀ (i : I), x ∈ B i) → ∀ (i : I), x ∈ A i ∧ x ∈ B i α:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:α⊢ (∀ (i : I), x ∈ A i ∧ x ∈ B i) → (∀ (i : I), x ∈ A i) ∧ ∀ (i : I), x ∈ B i α:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:αh:∀ (i : I), x ∈ A i ∧ x ∈ B i⊢ (∀ (i : I), x ∈ A i) ∧ ∀ (i : I), x ∈ B i α:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:αh:∀ (i : I), x ∈ A i ∧ x ∈ B i⊢ ∀ (i : I), x ∈ A iα:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:αh:∀ (i : I), x ∈ A i ∧ x ∈ B i⊢ ∀ (i : I), x ∈ B i α:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:αh:∀ (i : I), x ∈ A i ∧ x ∈ B i⊢ ∀ (i : I), x ∈ A i α:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:αh:∀ (i : I), x ∈ A i ∧ x ∈ B ii:I⊢ x ∈ A i All goals completed! 🐙 α:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:αh:∀ (i : I), x ∈ A i ∧ x ∈ B i⊢ ∀ (i : I), x ∈ B i α:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:αh:∀ (i : I), x ∈ A i ∧ x ∈ B ii:I⊢ x ∈ B i All goals completed! 🐙 α:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:α⊢ ((∀ (i : I), x ∈ A i) ∧ ∀ (i : I), x ∈ B i) → ∀ (i : I), x ∈ A i ∧ x ∈ B i α:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:αh1:∀ (i : I), x ∈ A ih2:∀ (i : I), x ∈ B ii:I⊢ x ∈ A i ∧ x ∈ B i α:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:αh1:∀ (i : I), x ∈ A ih2:∀ (i : I), x ∈ B ii:I⊢ x ∈ A iα:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:αh1:∀ (i : I), x ∈ A ih2:∀ (i : I), x ∈ B ii:I⊢ x ∈ B i α:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:αh1:∀ (i : I), x ∈ A ih2:∀ (i : I), x ∈ B ii:I⊢ x ∈ A i All goals completed! 🐙 α:Type u_1s:Set αt:Set αu:Set αI:Type u_2A:I → Set αB:I → Set αx:αh1:∀ (i : I), x ∈ A ih2:∀ (i : I), x ∈ B ii:I⊢ x ∈ B i All goals completed! 🐙 end

The first identity expresses the distributivity of intersection over arbitrary unions; the second says that the intersection of a family of intersections is the intersection of the intersections. We can also index unions by the elements of a set, writing ⋃ p ∈ primes, A p. For instance, the set of numbers divisible by the square of some prime can be written in two ways:

def primes : Set ℕ := { x | Nat.Prime x } example : (⋃ p ∈ primes, { x | p ^ 2 ∣ x }) = { x | ∃ p ∈ primes, p ^ 2 ∣ x } := α:Type u_1s:Set αt:Set αu:Set α⊢ ⋃ p ∈ primes, {x | p ^ 2 ∣ x} = {x | ∃ p ∈ primes, p ^ 2 ∣ x} α:Type u_1s:Set αt:Set αu:Set αx✝:ℕ⊢ x✝ ∈ ⋃ p ∈ primes, {x | p ^ 2 ∣ x} ↔ x✝ ∈ {x | ∃ p ∈ primes, p ^ 2 ∣ x} All goals completed! 🐙

A less trivial example: if x is not divisible by any prime, then x = 1. The proof uses contrapose!, which replaces the goal P → Q by ¬ Q → ¬ P and pushes the negations inward, together with the lemma Nat.exists_prime_and_dvd, according to which every n ≠ 1 has a prime divisor.

example : (⋂ p ∈ primes, { x | ¬ p ∣ x }) ⊆ { x | x = 1 } := α:Type u_1s:Set αt:Set αu:Set α⊢ ⋂ p ∈ primes, {x | ¬p ∣ x} ⊆ {x | x = 1} α:Type u_1s:Set αt:Set αu:Set αx:ℕ⊢ x ∈ ⋂ p ∈ primes, {x | ¬p ∣ x} → x ∈ {x | x = 1} α:Type u_1s:Set αt:Set αu:Set αx:ℕ⊢ x ∉ {x | x = 1} → x ∉ ⋂ p ∈ primes, {x | ¬p ∣ x} α:Type u_1s:Set αt:Set αu:Set αx:ℕ⊢ ¬x = 1 → ∃ x_1 ∈ primes, x_1 ∣ x All goals completed! 🐙

4.1.2. Functions

4.1.2.1. Image and Preimage

Let f : α → β. For s : Set α, the image of s under f, written f '' s, is the set { y | ∃ x, x ∈ s ∧ f x = y }. For v : Set β, the preimage of v, written f ⁻¹' v, is the set { x | f x ∈ v }. The preimage behaves very well with respect to set operations — it preserves intersections, unions and complements — and the proofs are often trivial:

section variable {β : Type*} variable (f : α → β) variable (u v : Set β) example : f ⁻¹' (u ∩ v) = f ⁻¹' u ∩ f ⁻¹' v := α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set β⊢ f ⁻¹' (u ∩ v) = f ⁻¹' u ∩ f ⁻¹' v α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βx✝:α⊢ x✝ ∈ f ⁻¹' (u ∩ v) ↔ x✝ ∈ f ⁻¹' u ∩ f ⁻¹' v All goals completed! 🐙

With the image there is an existential quantifier involved, and a bit more work is required. A useful trick is the rfl pattern in rintro: when destructing a hypothesis y ∈ f '' s, we obtain x, xs : x ∈ s and an equation f x = y; the rfl pattern replaces y by f x throughout the context.

example : f '' (s ∪ t) = f '' s ∪ f '' t := α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set β⊢ f '' (s ∪ t) = f '' s ∪ f '' t α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βy:β⊢ y ∈ f '' (s ∪ t) ↔ y ∈ f '' s ∪ f '' t; α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βy:β⊢ y ∈ f '' (s ∪ t) → y ∈ f '' s ∪ f '' tα:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βy:β⊢ y ∈ f '' s ∪ f '' t → y ∈ f '' (s ∪ t) α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βy:β⊢ y ∈ f '' (s ∪ t) → y ∈ f '' s ∪ f '' t α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βx:αxs:x ∈ s⊢ f x ∈ f '' s ∪ f '' tα:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βx:αxt:x ∈ t⊢ f x ∈ f '' s ∪ f '' t α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βx:αxs:x ∈ s⊢ f x ∈ f '' s ∪ f '' t α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βx:αxs:x ∈ s⊢ f x ∈ f '' s All goals completed! 🐙 α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βx:αxt:x ∈ t⊢ f x ∈ f '' s ∪ f '' t α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βx:αxt:x ∈ t⊢ f x ∈ f '' t All goals completed! 🐙 α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βy:β⊢ y ∈ f '' s ∪ f '' t → y ∈ f '' (s ∪ t) α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βx:αxs:x ∈ s⊢ f x ∈ f '' (s ∪ t)α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βx:αxt:x ∈ t⊢ f x ∈ f '' (s ∪ t) α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βx:αxs:x ∈ s⊢ f x ∈ f '' (s ∪ t) All goals completed! 🐙 α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βx:αxt:x ∈ t⊢ f x ∈ f '' (s ∪ t) All goals completed! 🐙

Note that use x, xs leaves the goal f x = f x, which the tactic closes automatically with rfl. Image and preimage are related by inclusions in both directions, which hold without any assumption on f:

example : s ⊆ f ⁻¹' (f '' s) := α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set β⊢ s ⊆ f ⁻¹' (f '' s) intro x α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βx:αxs:x ∈ s⊢ x ∈ f ⁻¹' (f '' s) α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βx:αxs:x ∈ s⊢ f x ∈ f '' s All goals completed! 🐙 example : f '' (f ⁻¹' u) ⊆ u := α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set β⊢ f '' (f ⁻¹' u) ⊆ u α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βx:αxmem:x ∈ f ⁻¹' u⊢ f x ∈ u All goals completed! 🐙

The fundamental relationship between the two operations is the following equivalence, called Set.image_subset_iff in Mathlib. In the language of order theory, it says that image and preimage form a Galois connection.

example : f '' s ⊆ v ↔ s ⊆ f ⁻¹' v := α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set β⊢ f '' s ⊆ v ↔ s ⊆ f ⁻¹' v α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set β⊢ f '' s ⊆ v → s ⊆ f ⁻¹' vα:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set β⊢ s ⊆ f ⁻¹' v → f '' s ⊆ v α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set β⊢ f '' s ⊆ v → s ⊆ f ⁻¹' v intro h α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βh:f '' s ⊆ vx:α⊢ x ∈ s → x ∈ f ⁻¹' v α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βh:f '' s ⊆ vx:αxs:x ∈ s⊢ x ∈ f ⁻¹' v α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βh:f '' s ⊆ vx:αxs:x ∈ sthis:f x ∈ f '' s⊢ x ∈ f ⁻¹' v All goals completed! 🐙 α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set β⊢ s ⊆ f ⁻¹' v → f '' s ⊆ v α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βh:s ⊆ f ⁻¹' vx:αxs:x ∈ s⊢ f x ∈ v All goals completed! 🐙

4.1.2.2. Injectivity and Surjectivity

A function f is injective (Function.Injective) when f x₁ = f x₂ implies x₁ = x₂, and surjective (Function.Surjective) when every y is of the form f x. Under these hypotheses, the inclusions of the previous section become equalities:

example (h : Injective f) : f ⁻¹' (f '' s) ⊆ s := α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βh:Injective f⊢ f ⁻¹' (f '' s) ⊆ s α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βh:Injective fx:αy:αys:y ∈ sfxeq:f y = f x⊢ x ∈ s α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βh:Injective fx:αy:αys:y ∈ sfxeq:f y = f x⊢ y ∈ s All goals completed! 🐙 example (h : Surjective f) : u ⊆ f '' (f ⁻¹' u) := α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βh:Surjective f⊢ u ⊆ f '' (f ⁻¹' u) intro y α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βh:Surjective fy:βyu:y ∈ u⊢ y ∈ f '' (f ⁻¹' u) α:Type u_1s:Set αt:Set αu✝:Set αβ:Type u_2f:α → βu:Set βv:Set βh:Surjective fx:αyu:f x ∈ u⊢ f x ∈ f '' (f ⁻¹' u) All goals completed! 🐙 end

Often a function is not injective on its whole domain, but it is injective when restricted to a subset. Mathlib expresses this with Set.InjOn: InjOn f s means that f is injective on s. The real logarithm, for example, is defined for all reals (with log 0 = 0 and log x = log |x|), but it is injective only on the positive reals. The proof uses the fact that exp (log x) = x for x > 0. The image of a whole function also has its own name, range f; the range of the exponential function is the set of positive reals.

section open Real example : InjOn log { x | x > 0 } := α:Type u_1s:Set αt:Set αu:Set α⊢ InjOn log {x | x > 0} intro x α:Type u_1s:Set αt:Set αu:Set αx:ℝxpos:x ∈ {x | x > 0}⊢ ∀ ⦃x₂ : ℝ⦄, x₂ ∈ {x | x > 0} → log x = log x₂ → x = x₂ α:Type u_1s:Set αt:Set αu:Set αx:ℝxpos:x ∈ {x | x > 0}y:ℝ⊢ y ∈ {x | x > 0} → log x = log y → x = y α:Type u_1s:Set αt:Set αu:Set αx:ℝxpos:x ∈ {x | x > 0}y:ℝypos:y ∈ {x | x > 0}⊢ log x = log y → x = y α:Type u_1s:Set αt:Set αu:Set αx:ℝxpos:x ∈ {x | x > 0}y:ℝypos:y ∈ {x | x > 0}e:log x = log y⊢ x = y calc x = exp (log x) := α:Type u_1s:Set αt:Set αu:Set αx:ℝxpos:x ∈ {x | x > 0}y:ℝypos:y ∈ {x | x > 0}e:log x = log y⊢ x = rexp (log x) All goals completed! 🐙 _ = exp (log y) := α:Type u_1s:Set αt:Set αu:Set αx:ℝxpos:x ∈ {x | x > 0}y:ℝypos:y ∈ {x | x > 0}e:log x = log y⊢ rexp (log x) = rexp (log y) All goals completed! 🐙 _ = y := α:Type u_1s:Set αt:Set αu:Set αx:ℝxpos:x ∈ {x | x > 0}y:ℝypos:y ∈ {x | x > 0}e:log x = log y⊢ rexp (log y) = y All goals completed! 🐙 example : range exp = { y | y > 0 } := α:Type u_1s:Set αt:Set αu:Set α⊢ range rexp = {y | y > 0} α:Type u_1s:Set αt:Set αu:Set αy:ℝ⊢ y ∈ range rexp ↔ y ∈ {y | y > 0}; α:Type u_1s:Set αt:Set αu:Set αy:ℝ⊢ y ∈ range rexp → y ∈ {y | y > 0}α:Type u_1s:Set αt:Set αu:Set αy:ℝ⊢ y ∈ {y | y > 0} → y ∈ range rexp α:Type u_1s:Set αt:Set αu:Set αy:ℝ⊢ y ∈ range rexp → y ∈ {y | y > 0} α:Type u_1s:Set αt:Set αu:Set αx:ℝ⊢ rexp x ∈ {y | y > 0} All goals completed! 🐙 α:Type u_1s:Set αt:Set αu:Set αy:ℝ⊢ y ∈ {y | y > 0} → y ∈ range rexp α:Type u_1s:Set αt:Set αu:Set αy:ℝypos:y ∈ {y | y > 0}⊢ y ∈ range rexp α:Type u_1s:Set αt:Set αu:Set αy:ℝypos:y ∈ {y | y > 0}⊢ rexp (log y) = y All goals completed! 🐙 end

4.1.2.3. Inverses and the Axiom of Choice

Every injective function f : α → β has a left inverse, provided α is nonempty: a function g : β → α with g (f x) = x for all x. The definition of g is natural — given y, if y is in the image of f, we return some x with f x = y; otherwise, we return an arbitrary default value. But the phrase "some x" hides a use of the axiom of choice: we only know that such an x exists, without having any way to compute it.

In Lean, the axiom of choice appears as the function Classical.choose. Given a proof h : ∃ x, P x, Classical.choose h is an element of α, and Classical.choose_spec h is a proof that this element satisfies P. Since Classical.choose cannot be executed, definitions that use it must be marked noncomputable. The class Inhabited provides the default value default.

section variable {β : Type*} [Inhabited α] noncomputable def inverse (f : α → β) : β → α := fun y : β => open Classical in if h : ∃ x, f x = y then Classical.choose h else default theorem inverse_spec {f : α → β} (y : β) (h : ∃ x, f x = y) : f (inverse f y) = y := α:Type u_1β:Type u_2inst✝:Inhabited αf:α → βy:βh:∃ x, f x = y⊢ f (inverse f y) = y α:Type u_1β:Type u_2inst✝:Inhabited αf:α → βy:βh:∃ x, f x = y⊢ f (Classical.choose h) = y All goals completed! 🐙

The condition ∃ x, f x = y is not decidable in general; the line open Classical in makes available the instance that decides any proposition using the excluded middle, which allows the if. The theorem inverse_spec says that, when y is in the image, inverse f y is indeed a preimage of y. With it, we can characterize injectivity and surjectivity in terms of inverses:

variable (f : α → β) example : Injective f ↔ LeftInverse (inverse f) f := α:Type u_1s:Set αt:Set αu:Set αβ:Type u_2inst✝:Inhabited αf:α → β⊢ Injective f ↔ LeftInverse (inverse f) f α:Type u_1s:Set αt:Set αu:Set αβ:Type u_2inst✝:Inhabited αf:α → β⊢ Injective f → LeftInverse (inverse f) fα:Type u_1s:Set αt:Set αu:Set αβ:Type u_2inst✝:Inhabited αf:α → β⊢ LeftInverse (inverse f) f → Injective f α:Type u_1s:Set αt:Set αu:Set αβ:Type u_2inst✝:Inhabited αf:α → β⊢ Injective f → LeftInverse (inverse f) f intro h α:Type u_1s:Set αt:Set αu:Set αβ:Type u_2inst✝:Inhabited αf:α → βh:Injective fy:α⊢ inverse f (f y) = y α:Type u_1s:Set αt:Set αu:Set αβ:Type u_2inst✝:Inhabited αf:α → βh:Injective fy:α⊢ f (inverse f (f y)) = f y α:Type u_1s:Set αt:Set αu:Set αβ:Type u_2inst✝:Inhabited αf:α → βh:Injective fy:α⊢ ∃ x, f x = f y All goals completed! 🐙 α:Type u_1s:Set αt:Set αu:Set αβ:Type u_2inst✝:Inhabited αf:α → β⊢ LeftInverse (inverse f) f → Injective f intro h α:Type u_1s:Set αt:Set αu:Set αβ:Type u_2inst✝:Inhabited αf:α → βh:LeftInverse (inverse f) fx₁:α⊢ ∀ ⦃a₂ : α⦄, f x₁ = f a₂ → x₁ = a₂ α:Type u_1s:Set αt:Set αu:Set αβ:Type u_2inst✝:Inhabited αf:α → βh:LeftInverse (inverse f) fx₁:αx₂:α⊢ f x₁ = f x₂ → x₁ = x₂ α:Type u_1s:Set αt:Set αu:Set αβ:Type u_2inst✝:Inhabited αf:α → βh:LeftInverse (inverse f) fx₁:αx₂:αe:f x₁ = f x₂⊢ x₁ = x₂ All goals completed! 🐙 example : Surjective f ↔ RightInverse (inverse f) f := α:Type u_1s:Set αt:Set αu:Set αβ:Type u_2inst✝:Inhabited αf:α → β⊢ Surjective f ↔ RightInverse (inverse f) f α:Type u_1s:Set αt:Set αu:Set αβ:Type u_2inst✝:Inhabited αf:α → β⊢ Surjective f → RightInverse (inverse f) fα:Type u_1s:Set αt:Set αu:Set αβ:Type u_2inst✝:Inhabited αf:α → β⊢ RightInverse (inverse f) f → Surjective f α:Type u_1s:Set αt:Set αu:Set αβ:Type u_2inst✝:Inhabited αf:α → β⊢ Surjective f → RightInverse (inverse f) f intro h α:Type u_1s:Set αt:Set αu:Set αβ:Type u_2inst✝:Inhabited αf:α → βh:Surjective fy:β⊢ f (inverse f y) = y α:Type u_1s:Set αt:Set αu:Set αβ:Type u_2inst✝:Inhabited αf:α → βh:Surjective fy:β⊢ ∃ x, f x = y All goals completed! 🐙 α:Type u_1s:Set αt:Set αu:Set αβ:Type u_2inst✝:Inhabited αf:α → β⊢ RightInverse (inverse f) f → Surjective f intro h α:Type u_1s:Set αt:Set αu:Set αβ:Type u_2inst✝:Inhabited αf:α → βh:RightInverse (inverse f) fy:β⊢ ∃ a, f a = y α:Type u_1s:Set αt:Set αu:Set αβ:Type u_2inst✝:Inhabited αf:α → βh:RightInverse (inverse f) fy:β⊢ f (inverse f y) = y All goals completed! 🐙 end

Recall that LeftInverse g f means ∀ x, g (f x) = x, and RightInverse g f means LeftInverse f g, that is, ∀ y, f (g y) = y.

4.1.2.4. Cantor's Theorem

We close the section on functions with one of the most celebrated results in mathematics: there is no surjective function from a set onto its power set. In particular, the power set of α is "strictly larger" than α. The proof is the famous diagonal argument. Suppose that f : α → Set α is surjective and consider the set S = \{ i \mid i \notin f(i) \}. By surjectivity, S = f(j) for some j. If j ∈ f j, then j ∈ S, and by the definition of S, j ∉ f j; if j ∉ f j, then j ∈ S = f j. Either way we have a contradiction.

theorem cantor : ∀ f : α → Set α, ¬ Surjective f := α:Type u_1⊢ ∀ (f : α → Set α), ¬Surjective f intro f α:Type u_1f:α → Set αsurjf:Surjective f⊢ False α:Type u_1f:α → Set αsurjf:Surjective fS:Set α := {i | i ∉ f i}⊢ False α:Type u_1f:α → Set αsurjf:Surjective fS:Set α := {i | i ∉ f i}j:αh:f j = S⊢ False have h₁ : j ∉ f j := α:Type u_1⊢ ∀ (f : α → Set α), ¬Surjective f α:Type u_1f:α → Set αsurjf:Surjective fS:Set α := {i | i ∉ f i}j:αh:f j = Sh':j ∈ f j⊢ False have : j ∉ f j := α:Type u_1⊢ ∀ (f : α → Set α), ¬Surjective f rwa [hα:Type u_1f:α → Set αsurjf:Surjective fS:Set α := {i | i ∉ f i}j:αh:f j = Sh':j ∈ S⊢ j ∉ f j at h' All goals completed! 🐙 α:Type u_1f:α → Set αsurjf:Surjective fS:Set α := {i | i ∉ f i}j:αh:f j = Sh₁:j ∉ f jh₂:j ∈ S⊢ False have h₃ : j ∉ S := α:Type u_1⊢ ∀ (f : α → Set α), ¬Surjective f rwa [hα:Type u_1f:α → Set αsurjf:Surjective fS:Set α := {i | i ∉ f i}j:αh:f j = Sh₁:j ∉ Sh₂:j ∈ S⊢ j ∉ S at h₁ All goals completed! 🐙

The formal proof follows the informal argument step by step. First we show that j ∉ f j (since assuming j ∈ f j leads to a contradiction); it follows that j ∈ S, by definition; but, rewriting with h : f j = S, we also get j ∉ S. The same diagonal argument appears, in other guises, in Russell's paradox, in the undecidability of the halting problem, and in Gödel's incompleteness theorems.

4.1.3. The Schröder–Bernstein Theorem

If there is an injective function from α to β and another from β to α, then there is a bijection between α and β. This is the Schröder–Bernstein theorem, which is fundamental for comparing cardinalities: it guarantees that the relation "|α| ≤ |β|", defined by the existence of an injection, is antisymmetric. The statement is simple, but the proof requires a clever construction, and formalizing it is an excellent exercise in manipulating sets and functions.

4.1.3.1. The Construction

Let f : α → β and g : β → α be injective. The idea is to use f on a part A of α and the inverse of g on the rest. For this to work, every point outside A must be in the image of g, so that the inverse makes sense. We therefore start with the set A_0 = \alpha \setminus g(\beta) of points that are not in the image of g — these points certainly have to be handled by f. But then the points of g(f(A_0)) cannot be handled by the inverse of g, since that would conflict with injectivity: their values would coincide with values already reached by f. So they must also go into A, and we repeat the reasoning:

A_0 = \alpha \setminus g(\beta), \qquad A_{n+1} = g(f(A_n)), \qquad A = \bigcup_{n} A_n.

The desired function is h x = f x if x ∈ A, and h x = g⁻¹ x otherwise. For the inverse of g we use Function.invFun, Mathlib's version of our inverse function (which requires only Nonempty β instead of Inhabited β). The relevant lemmas are Function.leftInverse_invFun, which says that invFun g is a left inverse of g when g is injective, and Function.invFun_eq, which says that g (invFun g x) = x when x is in the image of g.

section noncomputable section open Classical variable {β : Type*} [Nonempty β] variable (f : α → β) (g : β → α) def sbAux : ℕ → Set α | 0 => univ \ g '' univ | n + 1 => g '' (f '' sbAux n) def sbSet := ⋃ n, sbAux f g n def sbFun (x : α) : β := if x ∈ sbSet f g then f x else invFun g x

The first lemma confirms the intuition behind the construction: a point outside A is outside A_0, hence it is in the image of g, and invFun g acts on it as a right inverse.

theorem sb_right_inv {x : α} (hx : x ∉ sbSet f g) : g (invFun g x) = x := α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αx:αhx:x ∉ sbSet f g⊢ g (invFun g x) = x have : x ∈ g '' univ := α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αx:αhx:x ∉ sbSet f g⊢ g (invFun g x) = x α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αx:αhx:x ∉ g '' univ⊢ x ∈ sbSet f g α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αx:αhx:x ∉ g '' univ⊢ ∃ i, x ∈ sbAux f g i α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αx:αhx:x ∉ g '' univ⊢ x ∈ sbAux f g 0 α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αx:αhx:x ∉ g '' univ⊢ x ∈ univ ∧ x ∉ g '' univ All goals completed! 🐙 have : ∃ y, g y = x := α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αx:αhx:x ∉ sbSet f g⊢ g (invFun g x) = x All goals completed! 🐙 All goals completed! 🐙

4.1.3.2. Injectivity

To show that h is injective, suppose h x₁ = h x₂. There are three cases. If both are in A, then f x₁ = f x₂, and the injectivity of f settles it. If neither is in A, then invFun g x₁ = invFun g x₂, and applying g to both sides, by the previous lemma, we get x₁ = x₂. The interesting case is when only one of them, say x₁, is in A: then f x₁ = invFun g x₂, whence x₂ = g (f x₁). Since x_1 \in A_n for some n, we have x_2 \in A_{n+1} \subseteq A — which shows that this case actually never occurs.

The formal proof uses the wlog tactic ("without loss of generality") to reduce the case in which one of the points is in A to the case in which it is x₁: the tactic asks us to prove the goal assuming the negation of the additional hypothesis, using the result itself (with the roles of x₁ and x₂ swapped) as a resource. The set tactic introduces local abbreviations for A and h, making the goals readable.

theorem sb_injective (hf : Injective f) : Injective (sbFun f g) := α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective f⊢ Injective (sbFun f g) α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective fA:Set α := sbSet f gA_def:A = sbSet f g⊢ Injective (sbFun f g) α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective fA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f g⊢ Injective h intro x₁ α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective fA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gx₁:αx₂:α⊢ h x₁ = h x₂ → x₁ = x₂ α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective fA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gx₁:αx₂:αhxeq:h x₁ = h x₂⊢ x₁ = x₂ α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective fA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gx₁:αx₂:αhxeq:(if x₁ ∈ A then f x₁ else invFun g x₁) = if x₂ ∈ A then f x₂ else invFun g x₂⊢ x₁ = x₂ α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective fA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gx₁:αx₂:αhxeq:(if x₁ ∈ A then f x₁ else invFun g x₁) = if x₂ ∈ A then f x₂ else invFun g x₂xA:x₁ ∈ A ∨ x₂ ∈ A⊢ x₁ = x₂α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective fA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gx₁:αx₂:αhxeq:(if x₁ ∈ A then f x₁ else invFun g x₁) = if x₂ ∈ A then f x₂ else invFun g x₂xA:¬(x₁ ∈ A ∨ x₂ ∈ A)⊢ x₁ = x₂ α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective fA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gx₁:αx₂:αhxeq:(if x₁ ∈ A then f x₁ else invFun g x₁) = if x₂ ∈ A then f x₂ else invFun g x₂xA:x₁ ∈ A ∨ x₂ ∈ A⊢ x₁ = x₂ α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective fA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gx₁:αx₂:αhxeq:(if x₁ ∈ A then f x₁ else invFun g x₁) = if x₂ ∈ A then f x₂ else invFun g x₂xA:x₁ ∈ A ∨ x₂ ∈ Athis:∀ ⦃x₁ x₂ : α⦄, ((if x₁ ∈ A then f x₁ else invFun g x₁) = if x₂ ∈ A then f x₂ else invFun g x₂) → x₁ ∈ A ∨ x₂ ∈ A → x₁ ∈ A → x₁ = x₂x₁A:x₁ ∉ A⊢ x₁ = x₂α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective fA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gx₁:αx₂:αhxeq:(if x₁ ∈ A then f x₁ else invFun g x₁) = if x₂ ∈ A then f x₂ else invFun g x₂xA:x₁ ∈ A ∨ x₂ ∈ Ax₁A:x₁ ∈ A⊢ x₁ = x₂ α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective fA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gx₁:αx₂:αhxeq:(if x₁ ∈ A then f x₁ else invFun g x₁) = if x₂ ∈ A then f x₂ else invFun g x₂xA:x₁ ∈ A ∨ x₂ ∈ Athis:∀ ⦃x₁ x₂ : α⦄, ((if x₁ ∈ A then f x₁ else invFun g x₁) = if x₂ ∈ A then f x₂ else invFun g x₂) → x₁ ∈ A ∨ x₂ ∈ A → x₁ ∈ A → x₁ = x₂x₁A:x₁ ∉ A⊢ x₁ = x₂ α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective fA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gx₁:αx₂:αhxeq:(if x₁ ∈ A then f x₁ else invFun g x₁) = if x₂ ∈ A then f x₂ else invFun g x₂xA:x₁ ∈ A ∨ x₂ ∈ Athis:∀ ⦃x₁ x₂ : α⦄, ((if x₁ ∈ A then f x₁ else invFun g x₁) = if x₂ ∈ A then f x₂ else invFun g x₂) → x₁ ∈ A ∨ x₂ ∈ A → x₁ ∈ A → x₁ = x₂x₁A:x₁ ∉ A⊢ x₂ = x₁ All goals completed! 🐙 have x₂A : x₂ ∈ A := α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective f⊢ Injective (sbFun f g) α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective fA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gx₁:αx₂:αhxeq:(if x₁ ∈ A then f x₁ else invFun g x₁) = if x₂ ∈ A then f x₂ else invFun g x₂xA:x₁ ∈ A ∨ x₂ ∈ Ax₁A:x₁ ∈ Ax₂nA:x₂ ∉ A⊢ False α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective fA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gx₁:αx₂:αhxeq:f x₁ = invFun g x₂xA:x₁ ∈ A ∨ x₂ ∈ Ax₁A:x₁ ∈ Ax₂nA:x₂ ∉ A⊢ False α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective fA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gx₁:αx₂:αhxeq:f x₁ = invFun g x₂xA:x₁ ∈ A ∨ x₂ ∈ Ax₁A:∃ i, x₁ ∈ sbAux f g ix₂nA:x₂ ∉ A⊢ False have x₂eq : x₂ = g (f x₁) := α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective f⊢ Injective (sbFun f g) All goals completed! 🐙 α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective fA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gx₁:αx₂:αhxeq:f x₁ = invFun g x₂xA:x₁ ∈ A ∨ x₂ ∈ Ax₂nA:x₂ ∉ Ax₂eq:x₂ = g (f x₁)n:ℕhn:x₁ ∈ sbAux f g n⊢ False α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective fA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gx₁:αx₂:αhxeq:f x₁ = invFun g x₂xA:x₁ ∈ A ∨ x₂ ∈ Ax₂nA:x₂ ∉ Ax₂eq:x₂ = g (f x₁)n:ℕhn:x₁ ∈ sbAux f g n⊢ x₂ ∈ A α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective fA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gx₁:αx₂:αhxeq:f x₁ = invFun g x₂xA:x₁ ∈ A ∨ x₂ ∈ Ax₂nA:x₂ ∉ Ax₂eq:x₂ = g (f x₁)n:ℕhn:x₁ ∈ sbAux f g n⊢ ∃ i, x₂ ∈ sbAux f g i α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective fA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gx₁:αx₂:αhxeq:f x₁ = invFun g x₂xA:x₁ ∈ A ∨ x₂ ∈ Ax₂nA:x₂ ∉ Ax₂eq:x₂ = g (f x₁)n:ℕhn:x₁ ∈ sbAux f g n⊢ x₂ ∈ sbAux f g (n + 1) α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective fA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gx₁:αx₂:αhxeq:f x₁ = invFun g x₂xA:x₁ ∈ A ∨ x₂ ∈ Ax₂nA:x₂ ∉ Ax₂eq:x₂ = g (f x₁)n:ℕhn:x₁ ∈ sbAux f g n⊢ g (f x₁) ∈ g '' (f '' sbAux f g n) All goals completed! 🐙 α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective fA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gx₁:αx₂:αhxeq:f x₁ = f x₂xA:x₁ ∈ A ∨ x₂ ∈ Ax₁A:x₁ ∈ Ax₂A:x₂ ∈ A⊢ x₁ = x₂ All goals completed! 🐙 α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective fA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gx₁:αx₂:αhxeq:(if x₁ ∈ A then f x₁ else invFun g x₁) = if x₂ ∈ A then f x₂ else invFun g x₂xA:x₁ ∉ A ∧ x₂ ∉ A⊢ x₁ = x₂ α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhf:Injective fA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gx₁:αx₂:αhxeq:invFun g x₁ = invFun g x₂xA:x₁ ∉ A ∧ x₂ ∉ A⊢ x₁ = x₂ All goals completed! 🐙

4.1.3.3. Surjectivity

Given y : β, we consider the point g y. If g y ∉ A, then h (g y) = invFun g (g y) = y, by the injectivity of g. If g y ∈ A, then g(y) \in A_n for some n. It cannot be n = 0, since A_0 excludes the image of g. Hence g(y) \in A_{n+1} = g(f(A_n)), that is, g y = g (f x) for some x \in A_n; by the injectivity of g, y = f x = h x.

theorem sb_surjective (hg : Injective g) : Surjective (sbFun f g) := α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhg:Injective g⊢ Surjective (sbFun f g) α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhg:Injective gA:Set α := sbSet f gA_def:A = sbSet f g⊢ Surjective (sbFun f g) α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhg:Injective gA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f g⊢ Surjective h α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhg:Injective gA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gy:β⊢ ∃ a, h a = y α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhg:Injective gA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gy:βgyA:g y ∈ A⊢ ∃ a, h a = yα:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhg:Injective gA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gy:βgyA:g y ∉ A⊢ ∃ a, h a = y α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhg:Injective gA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gy:βgyA:g y ∈ A⊢ ∃ a, h a = y α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhg:Injective gA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gy:βgyA:∃ i, g y ∈ sbAux f g i⊢ ∃ a, h a = y α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhg:Injective gA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gy:βn:ℕhn:g y ∈ sbAux f g n⊢ ∃ a, h a = y α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhg:Injective gA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gy:βhn:g y ∈ sbAux f g 0⊢ ∃ a, h a = yα:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhg:Injective gA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gy:βn:ℕhn:g y ∈ sbAux f g (n + 1)⊢ ∃ a, h a = y α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhg:Injective gA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gy:βhn:g y ∈ sbAux f g 0⊢ ∃ a, h a = y All goals completed! 🐙 α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhg:Injective gA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gy:βn:ℕhn:∃ x, (∃ x_1 ∈ sbAux f g n, f x_1 = x) ∧ g x = g y⊢ ∃ a, h a = y α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhg:Injective gA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gy:βn:ℕx:αxmem:x ∈ sbAux f g nhx:g (f x) = g y⊢ ∃ a, h a = y α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhg:Injective gA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gy:βn:ℕx:αxmem:x ∈ sbAux f g nhx:g (f x) = g y⊢ h x = y have : x ∈ A := α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhg:Injective g⊢ Surjective (sbFun f g) α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhg:Injective gA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gy:βn:ℕx:αxmem:x ∈ sbAux f g nhx:g (f x) = g y⊢ ∃ i, x ∈ sbAux f g i All goals completed! 🐙 α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhg:Injective gA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gy:βn:ℕx:αxmem:x ∈ sbAux f g nhx:g (f x) = g ythis:x ∈ A⊢ f x = y All goals completed! 🐙 α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhg:Injective gA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gy:βgyA:g y ∉ A⊢ h (g y) = y α:Type u_1β:Type u_2inst✝:Nonempty βf:α → βg:β → αhg:Injective gA:Set α := sbSet f gA_def:A = sbSet f gh:α → β := sbFun f gh_def:h = sbFun f gy:βgyA:g y ∉ A⊢ invFun g (g y) = y All goals completed! 🐙

Putting the two parts together, we obtain the theorem. Recall that Function.Bijective is defined as the conjunction of injectivity and surjectivity.

theorem schroeder_bernstein {f : α → β} {g : β → α} (hf : Injective f) (hg : Injective g) : ∃ h : α → β, Bijective h := ⟨sbFun f g, sb_injective f g hf, sb_surjective f g hg⟩ end end end Ch24

4.1.4. Exercises

  1. Prove that s ∩ (s ∪ t) = s and that s ∪ s ∩ t = s, in two ways: with ext and with Set.Subset.antisymm.

  2. Prove that (s \ t) ∪ t = s ∪ t and that (s \ t) ∪ (t \ s) = (s ∪ t) \ (s ∩ t).

  3. Prove that { n | Nat.Prime n } ∩ { n | n > 2 } ⊆ { n | ¬ Even n }. Hint: use Nat.Prime.eq_two_or_odd and Nat.odd_iff.

  4. Prove that (s ∪ ⋂ i, A i) = ⋂ i, A i ∪ s. Hint: in one of the directions, use by_cases xs : x ∈ s.

  5. Prove that (⋃ p ∈ primes, { x | x ≤ p }) = univ. Hint: use Set.eq_univ_of_forall and Nat.exists_infinite_primes.

  6. Let f : α → β. Prove the following properties of images and preimages:

    • if s ⊆ t, then f '' s ⊆ f '' t;

    • f '' (s ∩ t) ⊆ f '' s ∩ f '' t, with equality if f is injective;

    • f ⁻¹' (u ∪ v) = f ⁻¹' u ∪ f ⁻¹' v;

    • f '' s ∩ v = f '' (s ∩ f ⁻¹' v).

  7. Show that the square root is injective on { x : ℝ | x ≥ 0 } and that (range fun x : ℝ => x ^ 2) = { y : ℝ | y ≥ 0 }. Hint: Real.sq_sqrt and Real.sqrt_sq.

  8. Adapt the proof of Cantor's theorem to show that there is no surjective function f : α → (α → Bool).

  9. Prove that the composition of injective functions is injective and that, if g ∘ f is surjective, then g is surjective.