doc-next-gen

Mathlib.Data.Set.Defs

Module docstring

{"# Sets

This file sets up the theory of sets whose elements have a given type.

Main definitions

Given a type X and a predicate p : X → Prop:

  • Set X : the type of sets whose elements have type X
  • {a : X | p a} : Set X : the set of all elements of X satisfying p
  • {a | p a} : Set X : a more concise notation for {a : X | p a}
  • {f x y | (x : X) (y : Y)} : Set Z : a more concise notation for {z : Z | ∃ x y, f x y = z}
  • {a ∈ S | p a} : Set X : given S : Set X, the subset of S consisting of its elements satisfying p.

Implementation issues

As in Lean 3, Set X := X → Prop This file is a port of the core Lean 3 file lib/lean/library/init/data/set.lean.

"}

Set definition
(α : Type u)
Full source
/-- A set is a collection of elements of some type `α`.

Although `Set` is defined as `α → Prop`, this is an implementation detail which should not be
relied on. Instead, `setOf` and membership of a set (`∈`) should be used to convert between sets
and predicates.
-/
def Set (α : Type u) := α → Prop
Set over a type
Informal description
A set over a type $\alpha$ is a collection of elements of type $\alpha$, represented as a predicate on $\alpha$.
setOf definition
{α : Type u} (p : α → Prop) : Set α
Full source
/-- Turn a predicate `p : α → Prop` into a set, also written as `{x | p x}` -/
def setOf {α : Type u} (p : α → Prop) : Set α :=
  p
Set comprehension from predicate
Informal description
Given a predicate $p : \alpha \to \text{Prop}$, the function `setOf` constructs the set $\{x \mid p x\}$ of all elements of type $\alpha$ that satisfy $p$.
Set.Mem definition
(s : Set α) (a : α) : Prop
Full source
/-- Membership in a set -/
protected def Mem (s : Set α) (a : α) : Prop :=
  s a
Set membership
Informal description
The membership relation for sets, where $a \in s$ is defined to mean that the element $a$ of type $\alpha$ satisfies the predicate $s : \alpha \to \mathrm{Prop}$.
Set.instMembership instance
: Membership α (Set α)
Full source
instance : Membership α (Set α) :=
  ⟨Set.Mem⟩
Membership Relation for Sets
Informal description
For any type $\alpha$, the set of elements of $\alpha$ has a membership relation $\in$ where $a \in s$ means the element $a$ satisfies the predicate $s : \alpha \to \mathrm{Prop}$.
Set.ext theorem
{a b : Set α} (h : ∀ (x : α), x ∈ a ↔ x ∈ b) : a = b
Full source
theorem ext {a b : Set α} (h : ∀ (x : α), x ∈ ax ∈ a ↔ x ∈ b) : a = b :=
  funext (fun x ↦ propext (h x))
Extensionality of Sets: $a = b \iff \forall x, x \in a \leftrightarrow x \in b$
Informal description
Two sets $a$ and $b$ over a type $\alpha$ are equal if and only if they contain exactly the same elements, i.e., for every $x \in \alpha$, $x \in a$ if and only if $x \in b$.
Set.Subset definition
(s₁ s₂ : Set α)
Full source
/-- The subset relation on sets. `s ⊆ t` means that all elements of `s` are elements of `t`.

Note that you should **not** use this definition directly, but instead write `s ⊆ t`. -/
protected def Subset (s₁ s₂ : Set α) :=
  ∀ ⦃a⦄, a ∈ s₁a ∈ s₂
Subset relation on sets
Informal description
For two sets $s_1$ and $s_2$ over a type $\alpha$, $s_1 \subseteq s_2$ means that every element $a$ in $s_1$ is also an element of $s_2$.
Set.instLE instance
: LE (Set α)
Full source
/-- We introduce `≤` before `⊆` to help the unifier when applying lattice theorems
to subset hypotheses. -/
instance : LE (Set α) :=
  ⟨Set.Subset⟩
Partial Order on Sets via Subset Inclusion
Informal description
For any type $\alpha$, the collection of sets over $\alpha$ is equipped with a partial order relation $\leq$ defined by subset inclusion.
Set.instHasSubset instance
: HasSubset (Set α)
Full source
instance : HasSubset (Set α) :=
  ⟨(· ≤ ·)⟩
Subset Relation on Sets
Informal description
For any type $\alpha$, the collection of sets over $\alpha$ is equipped with a subset relation $\subseteq$.
Set.instEmptyCollection instance
: EmptyCollection (Set α)
Full source
instance : EmptyCollection (Set α) :=
  ⟨fun _ ↦ False⟩
The Empty Set as a Set Over Any Type
Informal description
The empty set is a set over any type $\alpha$.
Mathlib.Meta.setBuilder definition
: Lean.ParserDescr✝
Full source
/-- Set builder syntax. This can be elaborated to either a `Set` or a `Finset` depending on context.

The elaborators for this syntax are located in:
* `Data.Set.Defs` for the `Set` builder notation elaborator for syntax of the form `{x | p x}`,
  `{x : α | p x}`, `{binder x | p x}`.
* `Data.Finset.Basic` for the `Finset` builder notation elaborator for syntax of the form
  `{x ∈ s | p x}`.
* `Data.Fintype.Basic` for the `Finset` builder notation elaborator for syntax of the form
  `{x | p x}`, `{x : α | p x}`, `{x ∉ s | p x}`, `{x ≠ a | p x}`.
* `Order.LocallyFinite.Basic` for the `Finset` builder notation elaborator for syntax of the form
  `{x ≤ a | p x}`, `{x ≥ a | p x}`, `{x < a | p x}`, `{x > a | p x}`.
-/
syntax (name := setBuilder) "{" extBinder " | " term "}" : term
Set builder notation
Informal description
The syntax `{x | p x}` represents set builder notation, which constructs a set (or finset) consisting of all elements `x` satisfying the predicate `p x`. The exact interpretation (as a `Set` or `Finset`) depends on the context and available elaborators.
Mathlib.Meta.elabSetBuilder definition
: TermElab
Full source
/-- Elaborate set builder notation for `Set`.

* `{x | p x}` is elaborated as `Set.setOf fun x ↦ p x`
* `{x : α | p x}` is elaborated as `Set.setOf fun x : α ↦ p x`
* `{binder x | p x}`, where `x` is bound by the `binder` binder, is elaborated as
  `{x | binder x ∧ p x}`. The typical example is `{x ∈ s | p x}`, which is elaborated as
  `{x | x ∈ s ∧ p x}`. The possible binders are
  * `· ∈ s`, `· ∉ s`
  * `· ⊆ s`, `· ⊂ s`, `· ⊇ s`, `· ⊃ s`
  * `· ≤ a`, `· ≥ a`, `· < a`, `· > a`, `· ≠ a`

  More binders can be declared using the `binder_predicate` command, see `Init.BinderPredicates` for
  more info.

See also
* `Data.Finset.Basic` for the `Finset` builder notation elaborator partly overriding this one for
  syntax of the form `{x ∈ s | p x}`.
* `Data.Fintype.Basic` for the `Finset` builder notation elaborator partly overriding this one for
  syntax of the form `{x | p x}`, `{x : α | p x}`, `{x ∉ s | p x}`, `{x ≠ a | p x}`.
* `Order.LocallyFinite.Basic` for the `Finset` builder notation elaborator partly overriding this
  one for syntax of the form `{x ≤ a | p x}`, `{x ≥ a | p x}`, `{x < a | p x}`, `{x > a | p x}`.
-/
@[term_elab setBuilder]
def elabSetBuilder : TermElab
  | `({ $x:ident | $p }), expectedType? => do
    elabTerm (← `(setOf fun $x:ident ↦ $p)) expectedType?
  | `({ $x:ident : $t | $p }), expectedType? => do
    elabTerm (← `(setOf fun $x:ident : $t ↦ $p)) expectedType?
  | `({ $x:ident $b:binderPred | $p }), expectedType? => do
    elabTerm (← `(setOf fun $x:ident ↦ satisfies_binder_pred% $x $b ∧ $p)) expectedType?
  | _, _ => throwUnsupportedSyntax
Set Builder Notation Elaborator
Informal description
The elaborator for set builder notation in Lean, which translates expressions like `{x | p x}`, `{x : α | p x}`, or `{x ∈ s | p x}` into corresponding `Set.setOf` terms. It handles various binder forms including membership (`∈`, `∉`), subset relations (`⊆`, `⊂`, `⊇`, `⊃`), and order relations (`≤`, `≥`, `<`, `>`, `≠`), and can be extended with additional binders via the `binder_predicate` command.
Mathlib.Meta.setOf.unexpander definition
: Lean.PrettyPrinter.Unexpander
Full source
/-- Unexpander for set builder notation. -/
@[app_unexpander setOf]
def setOf.unexpander : Lean.PrettyPrinter.Unexpander
  | `($_ fun $x:ident ↦ $p) => `({ $x:ident | $p })
  | `($_ fun ($x:ident : $ty:term) ↦ $p) => `({ $x:ident : $ty:term | $p })
  | _ => throw ()
Set builder notation unexpander
Informal description
The unexpander for set builder notation transforms expressions of the form `setOf (fun x ↦ p)` into the more readable `{x | p}` and `setOf (fun (x : ty) ↦ p)` into `{x : ty | p}`.
Mathlib.Meta.term{_|_} definition
: Lean.ParserDescr✝
Full source
macromacro (priority := low) "{" t:term " | " bs:extBinders "}" : term =>
  `({x | ∃ᵉ $bs:extBinders, $t = x})
Set builder notation with binders
Informal description
The notation `{ f x y | (x : X) (y : Y) }` represents the set of all elements of the form `f x y` where `x` ranges over type `X` and `y` ranges over type `Y`. This is equivalent to the set `{z : Z | ∃ x y, f x y = z}`. If `f x y` is a single identifier, it must be parenthesized to avoid ambiguity with the simpler set notation `{x | p x}`.
Mathlib.Meta.macroPattSetBuilder definition
: Lean.ParserDescr✝
Full source
macro (name := macroPattSetBuilder) (priority := low-1)
  "{" pat:term " : " t:term " | " p:term "}" : term =>
  `({ x : $t | match x with | $pat => $p })
Set-builder notation with pattern matching
Informal description
The notation `{ pat : X | p }` represents the set of all elements of type `X` that match the pattern `pat` and satisfy the proposition `p`. Here, `pat` is a pattern that elements of type `X` can match against, and `p` is a condition that may depend on variables introduced by the pattern. For example, `{ (m, n) : ℕ × ℕ | m * n = 12 }` denotes the set of all pairs of natural numbers whose product is 12. When the type `X` can be inferred, the shorthand `{ pat | p }` can be used instead.
Mathlib.Meta.term{_|_}_1 definition
: Lean.ParserDescr✝
Full source
macromacro (priority := low-1) "{" pat:term " | " p:term "}" : term =>
  `({ x | match x with | $pat => $p })
Pattern-matching set builder notation
Informal description
The notation `{ pat | p }` constructs a set where each element `x` matches the pattern `pat` and satisfies the condition `p`. This is syntactic sugar for `{ x | match x with | pat => p }`, which creates a set comprehension where elements are filtered based on pattern matching.
Mathlib.Meta.setOfPatternMatchUnexpander definition
: Lean.PrettyPrinter.Unexpander
Full source
/-- Pretty printing for set-builder notation with pattern matching. -/
@[app_unexpander setOf]
def setOfPatternMatchUnexpander : Lean.PrettyPrinter.Unexpander
  | `($_ fun $x:ident ↦ match $y:ident with | $pat => $p) =>
      if x == y then
        `({ $pat:term | $p:term })
      else
        throw ()
  | `($_ fun ($x:ident : $ty:term) ↦ match $y:ident with | $pat => $p) =>
      if x == y then
        `({ $pat:term : $ty:term | $p:term })
      else
        throw ()
  | _ => throw ()
Set-builder notation unexpander for pattern matching
Informal description
The unexpander `Mathlib.Meta.setOfPatternMatchUnexpander` converts Lean's pattern matching syntax in set-builder notation to a more readable form. Specifically, it transforms expressions of the form `{x | match y with | pat => p}` into `{pat | p}` when `x` and `y` are the same variable, and similarly for typed patterns `{x : ty | match y with | pat => p}` into `{pat : ty | p}`.
Set.univ definition
: Set α
Full source
/-- The universal set on a type `α` is the set containing all elements of `α`.

This is conceptually the "same as" `α` (in set theory, it is actually the same), but type theory
makes the distinction that `α` is a type while `Set.univ` is a term of type `Set α`. `Set.univ` can
itself be coerced to a type `↥Set.univ` which is in bijection with (but distinct from) `α`. -/
def univ : Set α := {_a | True}
Universal set
Informal description
The universal set on a type $\alpha$ is the set containing all elements of $\alpha$, represented as the predicate $\{x \mid \text{True}\}$.
Set.insert definition
(a : α) (s : Set α) : Set α
Full source
/-- `Set.insert a s` is the set `{a} ∪ s`.

Note that you should **not** use this definition directly, but instead write `insert a s` (which is
mediated by the `Insert` typeclass). -/
protected def insert (a : α) (s : Set α) : Set α := {b | b = a ∨ b ∈ s}
Insertion of an element into a set
Informal description
The function `Set.insert a s` constructs the set $\{a\} \cup s$, which consists of all elements that are equal to $a$ or belong to the set $s$.
Set.instInsert instance
: Insert α (Set α)
Full source
instance : Insert α (Set α) := ⟨Set.insert⟩
Insertion of Elements into Sets
Informal description
For any type $\alpha$, there is a canonical way to insert an element into a set of $\alpha$.
Set.singleton definition
(a : α) : Set α
Full source
/-- The singleton of an element `a` is the set with `a` as a single element.

Note that you should **not** use this definition directly, but instead write `{a}`. -/
protected def singleton (a : α) : Set α := {b | b = a}
Singleton set
Informal description
The singleton set $\{a\}$ is the set containing exactly one element $a$ of type $\alpha$, defined as $\{b \mid b = a\}$.
Set.instSingletonSet instance
: Singleton α (Set α)
Full source
instance instSingletonSet : Singleton α (Set α) := ⟨Set.singleton⟩
Singleton Sets in the Type of Sets
Informal description
For any type $\alpha$, the type of sets over $\alpha$ has a singleton structure, where for any element $a \in \alpha$, the singleton set $\{a\}$ can be formed.
Set.union definition
(s₁ s₂ : Set α) : Set α
Full source
/-- The union of two sets `s` and `t` is the set of elements contained in either `s` or `t`.

Note that you should **not** use this definition directly, but instead write `s ∪ t`. -/
protected def union (s₁ s₂ : Set α) : Set α := {a | a ∈ s₁ ∨ a ∈ s₂}
Union of two sets
Informal description
Given two sets $s_1$ and $s_2$ over a type $\alpha$, their union $s_1 \cup s_2$ is the set of all elements $a \in \alpha$ that belong to either $s_1$ or $s_2$.
Set.instUnion instance
: Union (Set α)
Full source
instance : Union (Set α) := ⟨Set.union⟩
Union Operation on Sets
Informal description
For any type $\alpha$, the type of sets over $\alpha$ has a union operation $\cup$ that combines two sets into their union.
Set.inter definition
(s₁ s₂ : Set α) : Set α
Full source
/-- The intersection of two sets `s` and `t` is the set of elements contained in both `s` and `t`.

Note that you should **not** use this definition directly, but instead write `s ∩ t`. -/
protected def inter (s₁ s₂ : Set α) : Set α := {a | a ∈ s₁ ∧ a ∈ s₂}
Intersection of two sets
Informal description
The intersection of two sets $s_1$ and $s_2$ over a type $\alpha$ is the set $\{a \in \alpha \mid a \in s_1 \land a \in s_2\}$ consisting of all elements that belong to both $s_1$ and $s_2$.
Set.instInter instance
: Inter (Set α)
Full source
instance : Inter (Set α) := ⟨Set.inter⟩
Intersection Operation on Sets
Informal description
For any type $\alpha$, the type of sets over $\alpha$ has an intersection operation $\cap$ that combines two sets into their intersection.
Set.compl definition
(s : Set α) : Set α
Full source
/-- The complement of a set `s` is the set of elements not contained in `s`.

Note that you should **not** use this definition directly, but instead write `sᶜ`. -/
protected def compl (s : Set α) : Set α := {a | a ∉ s}
Set complement
Informal description
The complement of a set $s$ in a type $\alpha$ is the set $\{a \in \alpha \mid a \notin s\}$ consisting of all elements of $\alpha$ that are not in $s$.
Set.diff definition
(s t : Set α) : Set α
Full source
/-- The difference of two sets `s` and `t` is the set of elements contained in `s` but not in `t`.

Note that you should **not** use this definition directly, but instead write `s \ t`. -/
protected def diff (s t : Set α) : Set α := {a ∈ s | a ∉ t}
Set difference
Informal description
The difference of two sets $s$ and $t$ is the set $\{x \in s \mid x \notin t\}$ consisting of all elements that belong to $s$ but not to $t$.
Set.instSDiff instance
: SDiff (Set α)
Full source
instance : SDiff (Set α) := ⟨Set.diff⟩
Set Difference Operation on Sets
Informal description
For any type $\alpha$, the set difference operation $\setminus$ is defined on the type of sets over $\alpha$.
Set.powerset definition
(s : Set α) : Set (Set α)
Full source
/-- `𝒫 s` is the set of all subsets of `s`. -/
def powerset (s : Set α) : Set (Set α) := {t | t ⊆ s}
Powerset of a set
Informal description
The powerset of a set $s$ is the set of all subsets of $s$, denoted $\mathcal{P}(s) = \{t \mid t \subseteq s\}$.
Set.term𝒫_ definition
: Lean.ParserDescr✝
Full source
@[inherit_doc] prefix:100 "𝒫" => powerset
Powerset notation
Informal description
The notation `𝒫 s` represents the powerset of a set `s`, which is the set of all subsets of `s`.
Set.image definition
{β : Type v} (f : α → β) (s : Set α) : Set β
Full source
/-- The image of `s : Set α` by `f : α → β`, written `f '' s`, is the set of `b : β` such that
`f a = b` for some `a ∈ s`. -/
def image {β : Type v} (f : α → β) (s : Set α) : Set β := {f a | a ∈ s}
Image of a set under a function
Informal description
The image of a set $s \subseteq \alpha$ under a function $f : \alpha \to \beta$ is the set $\{f(a) \mid a \in s\}$ of all elements in $\beta$ that are mapped to by some element in $s$.
Set.instFunctor instance
: Functor Set
Full source
instance : Functor Set where map := @Set.image
The Functor Structure on Sets
Informal description
The type `Set` forms a functor, where for any function $f : \alpha \to \beta$, the functor action maps a set $s \subseteq \alpha$ to its image under $f$, $\{f(a) \mid a \in s\}$.
Set.instLawfulFunctor instance
: LawfulFunctor Set
Full source
instance : LawfulFunctor Set where
  id_map _ := funext fun _ ↦ propext ⟨fun ⟨_, sb, rfl⟩ ↦ sb, fun sb ↦ ⟨_, sb, rfl⟩⟩
  comp_map g h _ := funext <| fun c ↦ propext
    ⟨fun ⟨a, ⟨h₁, h₂⟩⟩ ↦ ⟨g a, ⟨⟨a, ⟨h₁, rfl⟩⟩, h₂⟩⟩,
     fun ⟨_, ⟨⟨a, ⟨h₁, h₂⟩⟩, h₃⟩⟩ ↦ ⟨a, ⟨h₁, show h (g a) = c from h₂ ▸ h₃⟩⟩⟩
  map_const := rfl
Lawful Functor Structure on Sets
Informal description
The type `Set` forms a lawful functor, where the functorial action respects the identity function and composition of functions. Specifically, for any function $f : \alpha \to \beta$, the functor action maps a set $s \subseteq \alpha$ to its image under $f$, $\{f(a) \mid a \in s\}$, and this action satisfies the functor laws: 1. Identity: `id <$> s = s` for any set $s$. 2. Composition: `(g ∘ f) <$> s = g <$> (f <$> s)` for any functions $f : \alpha \to \beta$ and $g : \beta \to \gamma$.
Set.Nonempty definition
(s : Set α) : Prop
Full source
/-- The property `s.Nonempty` expresses the fact that the set `s` is not empty. It should be used
in theorem assumptions instead of `∃ x, x ∈ s` or `s ≠ ∅` as it gives access to a nice API thanks
to the dot notation. -/
protected def Nonempty (s : Set α) : Prop :=
  ∃ x, x ∈ s
Nonempty set
Informal description
A set $s$ over a type $\alpha$ is called nonempty if there exists an element $x \in \alpha$ such that $x$ belongs to $s$.