2.6. Sets
Chapter 3 of HTPIwL develops proofs about sets. A set of elements of a type α is determined by which elements belong to it, so the membership predicate determines the set. In Lean, we take this as the definition.
def Set (α : Type) : Type := α → Prop
Every element of a set comes from the fixed type α. In this typed setting, the collection of all sets that do not contain themselves cannot be written down, so Russell's paradox does not arise.B. Russell, letter to Frege, 16 June 1902. In J. van Heijenoort, From Frege to Gödel: A Source Book in Mathematical Logic, 1879–1931, Harvard University Press, 1967, pp. 124–125.
The instance below registers the notation x ∈ s, which unfolds by definition to the application s x. In this instance and the following ones, Lean binds the free type variable α automatically.
instance : Membership α (Set α) :=
⟨fun s a => s a⟩
A set given by a property is the predicate itself, and a membership proof is a proof of the property. Mathematical notation writes such a set in set-builder notation, as the set of all n such that ∃ k, n = 2 * k. Lean core has no set-builder notation, so we write the predicate directly.
def Evens : Set Nat := fun n => ∃ k, n = 2 * k
example : (6 : Nat) ∈ Evens := ⟨3, rfl⟩
The inclusion s ⊆ t states that every element of s belongs to t.
instance : HasSubset (Set α) :=
⟨fun s t => ∀ x, x ∈ s → x ∈ t⟩
An inclusion is a universally quantified implication, so its proofs begin by considering an arbitrary element together with the assumption that it belongs to the left side. Union and intersection apply the connectives of Lecture 1 pointwise.
instance : Union (Set α) :=
⟨fun s t => fun x => x ∈ s ∨ x ∈ t⟩
instance : Inter (Set α) :=
⟨fun s t => fun x => x ∈ s ∧ x ∈ t⟩
Membership in an intersection is by definition a conjunction, so the projections of Lecture 1 apply to it.
theorem inter_subset_left (α : Type) (s t : Set α) :
s ∩ t ⊆ s := α:Types:Set αt:Set α⊢ s ∩ t ⊆ s
α:Types:Set αt:Set αx:αhx:x ∈ s ∩ t⊢ x ∈ s
All goals completed! 🐙
Membership in a union is a disjunction, so the tactic cases splits it.
theorem union_subset_swap (α : Type) (s t : Set α) :
s ∪ t ⊆ t ∪ s := α:Types:Set αt:Set α⊢ s ∪ t ⊆ t ∪ s
α:Types:Set αt:Set αx:αhx:x ∈ s ∪ t⊢ x ∈ t ∪ s
cases hx with
α:Types:Set αt:Set αx:αh:x ∈ s⊢ x ∈ t ∪ s All goals completed! 🐙
α:Types:Set αt:Set αx:αh:x ∈ t⊢ x ∈ t ∪ s All goals completed! 🐙
Two sets with the same elements are equal. Proving such an equality requires extensionality principles beyond the logic presented so far, so we state set identities as inclusions.