The Mathlib Handbook

The volumes of the handbook are not written yet. This site shows the map of Mathlib that they will follow, at Mathlib 4472534b60.

The areas / V03. Sets, finite structures and big operators

Mathlib.Basic.SetLike.Basic

46 declarations and 13 deprecated aliases. Mathlib docs · Source

Typeclass for types with a set-like extensionality property

The Membership typeclass is used to let terms of a type have elements. Many instances of Membership have a set-like extensionality property: things are equal iff they have the same elements. The SetLike typeclass provides a unified interface to define a Membership that is extensional in this way.

The main use of SetLike is for algebraic subobjects (such as Submonoid and Submodule), whose non-proof data consists only of a carrier set. In such a situation, the projection to the carrier set is injective.

In general, a type A is SetLike with elements of type B if it has an injective map to Set B. This module provides standard boilerplate for every SetLike: a coe_sort, a coe to set, and various extensionality and simp lemmas. The order induced by set inclusion is called PartialOrder.ofSetlike: this is not an instance for flexibility in choosing orders. The class IsMemLE abstractly states the order is equal to that induced by set inclusion; an instance is automatically available when defining a PartialOrder as .ofSetLike (MySubobject X).

A typical subobject should be declared as:

structure MySubobject (X : Type*) [ObjectTypeclass X] where
  (carrier : Set X)
  (op_mem' : ∀ {x : X}, x ∈ carrier → sorry ∈ carrier)

namespace MySubobject

variable {X : Type*} [ObjectTypeclass X] {x : X}

instance : SetLike (MySubobject X) X :=
  ⟨MySubobject.carrier, fun p q h => by cases p; cases q; congr!⟩

instance : PartialOrder (MySubobject X) := .ofSetLike (MySubobject X)

@[simp] lemma mem_carrier {p : MySubobject X} : x ∈ p.carrier ↔ x ∈ (p : Set X) := Iff.rfl

@[ext] theorem ext {p q : MySubobject X} (h : ∀ x, x ∈ p ↔ x ∈ q) : p = q := SetLike.ext h

/-- Copy of a `MySubobject` with a new `carrier` equal to the old one. Useful to fix definitional
equalities. See Note [range copy pattern]. -/
protected def copy (p : MySubobject X) (s : Set X) (hs : s = ↑p) : MySubobject X :=
  { carrier := s
    op_mem' := hs.symm ▸ p.op_mem' }

@[simp] lemma coe_copy (p : MySubobject X) (s : Set X) (hs : s = ↑p) :
  (p.copy s hs : Set X) = s := rfl

lemma copy_eq (p : MySubobject X) (s : Set X) (hs : s = ↑p) : p.copy s hs = p :=
  SetLike.coe_injective hs

end MySubobject

An alternative to SetLike could have been an extensional Membership typeclass:

class ExtMembership (α : out_param <| Type u) (β : Type v) extends Membership α β where
  (ext_iff : ∀ {s t : β}, s = t ↔ ∀ (x : α), x ∈ s ↔ x ∈ t)

While this is equivalent, SetLike conveniently uses a carrier set projection directly.

Tags

subobjects

def SetLike.ctorIdx E1 #

{A : Type u_1} → {B : outParam (Type u_2)} → SetLike A B → ℕ

class SetLike E1 #

Type u_1 → outParam (Type u_2) → Type (max u_1 u_2)

A class to indicate that there is a canonical injection between A and Set B.

This has the effect of giving terms of A elements of type B (through a Membership instance) and a compatible coercion to Type* as a subtype.

Note: if SetLike.coe is a projection, implementers should create a simp lemma such as

@[simp] lemma mem_carrier {p : MySubobject X} : x ∈ p.carrier ↔ x ∈ (p : Set X) := Iff.rfl

to normalize terms.

If you declare an unbundled subclass of SetLike, for example:

class MulMemClass (S : Type*) (M : Type*) [Mul M] [SetLike S M] where
  ...

Then you should not repeat the outParam declaration so SetLike will supply the value instead. This ensures your subclass will not have issues with synthesis of the [Mul M] parameter starting before the value of M is known.

ctor SetLike.mk E0 #

{A : Type u_1} → {B : outParam (Type u_2)} → (coe : A → Set B) → Function.Injective coe → SetLike A B

abbrev SetLike.coe E1 #

{A : Type u_1} → {B : outParam (Type u_2)} → [self : SetLike A B] → A → Set B

The coercion from a term of a SetLike to its corresponding Set.

theorem SetLike.coe_injective E0 #

∀ {A : Type u_1} {B : outParam (Type u_2)} [self : SetLike A B], Function.Injective SetLike.coe

The coercion from a term of a SetLike to its corresponding Set is injective.

instance SetLike.instCoeTCSet E0 #

{A : Type u_1} → {B : Type u_2} → [i : SetLike A B] → CoeTC A (Set B)

instance SetLike.instMembership E0 #

{A : Type u_1} → {B : Type u_2} → [i : SetLike A B] → Membership B A

instance SetLike.instCoeSortType E0 #

{A : Type u_1} → {B : Type u_2} → [i : SetLike A B] → CoeSort A (Type u_2)

def SetLike.delabSubtypeSetLike E1 #

Lean.PrettyPrinter.Delaborator.Delab

For terms that match the CoeSort instance's body, pretty print as ↥S rather than as { x // x ∈ S }. The discriminating feature is that membership uses the SetLike.instMembership instance.

theorem SetLike.coe_sort_coe E0 #

∀ {A : Type u_1} {B : Type u_2} [i : SetLike A B] (p : A), ↑↑p = ↥p

theorem SetLike.exists E1 #

∀ {A : Type u_1} {B : Type u_2} [i : SetLike A B] {p : A} {q : ↥p → Prop}, (∃ x, q x) ↔ ∃ x, ∃ (h : x ∈ p), q ⟨x, h⟩

theorem SetLike.forall E1 #

∀ {A : Type u_1} {B : Type u_2} [i : SetLike A B] {p : A} {q : ↥p → Prop},
  (∀ (x : ↥p), q x) ↔ ∀ (x : B) (h : x ∈ p), q ⟨x, h⟩

theorem SetLike.coe_set_eq E0 #

∀ {A : Type u_1} {B : Type u_2} [i : SetLike A B] {p q : A}, ↑p = ↑q ↔ p = q

theorem SetLike.coe_ne_coe E0 #

∀ {A : Type u_1} {B : Type u_2} [i : SetLike A B] {p q : A}, ↑p ≠ ↑q ↔ p ≠ q

theorem SetLike.ext' E1 #

∀ {A : Type u_1} {B : Type u_2} [i : SetLike A B] {p q : A}, ↑p = ↑q → p = q

theorem SetLike.ext'_iff E0 #

∀ {A : Type u_1} {B : Type u_2} [i : SetLike A B] {p q : A}, p = q ↔ ↑p = ↑q

theorem SetLike.ext E1 #

∀ {A : Type u_1} {B : Type u_2} [i : SetLike A B] {p q : A}, (∀ (x : B), x ∈ p ↔ x ∈ q) → p = q

Note: implementers of SetLike must copy this lemma in order to tag it with @[ext].

theorem SetLike.ext_iff E0 #

∀ {A : Type u_1} {B : Type u_2} [i : SetLike A B] {p q : A}, p = q ↔ ∀ (x : B), x ∈ p ↔ x ∈ q

theorem SetLike.mem_coe E0 #

∀ {A : Type u_1} {B : Type u_2} [i : SetLike A B] {p : A} {x : B}, x ∈ ↑p ↔ x ∈ p

theorem SetLike.coe_eq_coe E0 #

∀ {A : Type u_1} {B : Type u_2} [i : SetLike A B] {p : A} {x y : ↥p}, ↑x = ↑y ↔ x = y

theorem SetLike.coe_mem E0 #

∀ {A : Type u_1} {B : Type u_2} [i : SetLike A B] {p : A} (x : ↥p), ↑x ∈ p

theorem SetLike.mem_of_subset E0 #

∀ {A : Type u_1} {B : Type u_2} [i : SetLike A B] {p : A} {s : Set B}, s ⊆ ↑p → ∀ {x : B}, x ∈ s → x ∈ p

theorem SetLike.eta E0 #

∀ {A : Type u_1} {B : Type u_2} [i : SetLike A B] {p : A} (x : ↥p) (hx : ↑x ∈ p), ⟨↑x, hx⟩ = x

theorem SetLike.setOfPred_mem_eq E0 #

∀ {A : Type u_1} {B : Type u_2} [i : SetLike A B] (a : A), {b | b ∈ a} = ↑a

theorem SetLike.mem_of_subsingleton E0 #

∀ {A : Type u_1} {B : Type u_2} [i : SetLike A B] [Subsingleton B] (S : A) [h : Nonempty ↥S] {b : B}, b ∈ S

theorem SetLike.exists_notMem_of_ne_top E1 #

∀ {A : Type u_1} {B : Type u_2} [i : SetLike A B] [inst : LE A] [inst_1 : OrderTop A] (s : A),
  s ≠ ⊤ → autoParam (↑⊤ = Set.univ) SetLike.exists_notMem_of_ne_top._auto_1 → ∃ b, b ∉ s

If s is a proper element of a SetLike structure (i.e., s ≠ ⊤) and the top element coerces to the universal set, then there exists an element not in s.

class IsMemLE E1 #

(A : Type u_1) → (B : outParam (Type u_2)) → [Membership B A] → [LE A] → Prop

A class to indicate that the order on a type corresponds to set inclusion.

An instance of this class is automatically available on any order defined via LE.ofMembership.

ctor IsMemLE.mk E0 #

∀ {A : Type u_1} {B : outParam (Type u_2)} [inst : Membership B A] [inst_1 : LE A],
  (∀ {S T : A}, S ≤ T ↔ ∀ ⦃x : B⦄, x ∈ S → x ∈ T) → IsMemLE A B

theorem IsMemLE.le_iff_mem_imp_mem E1 #

∀ {A : Type u_1} {B : outParam (Type u_2)} {inst : Membership B A} {inst_1 : LE A} [self : IsMemLE A B] {S T : A},
  S ≤ T ↔ ∀ ⦃x : B⦄, x ∈ S → x ∈ T

The order corresponds to set inclusion.

theorem mem_of_le_of_mem E0 #

∀ {A : Type u_1} {B : outParam (Type u_2)} {inst : Membership B A} {inst_1 : LE A} [self : IsMemLE A B] {S T : A},
  S ≤ T → ∀ ⦃x : B⦄, x ∈ S → x ∈ T

Alias of the forward direction of IsMemLE.le_iff_mem_imp_mem.


The order corresponds to set inclusion.

instance instIsMemLESet E0 #

∀ (α : Type u_1), IsMemLE (Set α) α

def LE.ofMembership E1 #

(A : Type u_1) → {B : Type u_2} → [Membership B A] → LE A

The order induced from a Membership instance by inclusion.

An order defined this way automatically makes available an instance of IsMemLE.

instance instIsMemLE E0 #

∀ (A : Type u_1) {B : Type u_2} [inst : Membership B A], IsMemLE A B

def Preorder.ofMembership E1 #

(A : Type u_1) → {B : Type u_2} → [Membership B A] → Preorder A

The preorder induced from a Membership instance by inclusion.

A preorder defined this way automatically makes available an instance of IsMemLE.

def PartialOrder.ofSetLike E1 #

(A : Type u_1) → {B : Type u_2} → [SetLike A B] → PartialOrder A

The partial order induced from a SetLike instance by inclusion.

A partial order defined this way automatically makes available an instance of IsMemLE.

theorem not_le_iff_exists_mem_notMem E1 #

∀ {A : Type u_1} {B : Type u_2} [inst : Membership B A] [inst_1 : LE A] [IsMemLE A B] {p q : A}, ¬p ≤ q ↔ ∃ x ∈ p, x ∉ q

theorem lt_iff_le_and_exists_mem_notMem E1 #

∀ {A : Type u_1} {B : Type u_2} [inst : Membership B A] [inst_1 : Preorder A] [IsMemLE A B] {p q : A},
  p < q ↔ p ≤ q ∧ ∃ x ∈ q, x ∉ p

theorem exists_mem_notMem_of_lt E1 #

∀ {A : Type u_1} {B : Type u_2} [inst : Membership B A] [inst_1 : Preorder A] [IsMemLE A B] {p q : A},
  p < q → ∃ x ∈ q, x ∉ p

theorem SetLike.coe_subset_coe E0 #

∀ {A : Type u_1} {B : Type u_2} [inst : SetLike A B] [inst_1 : LE A] [IsMemLE A B] {p q : A}, ↑p ⊆ ↑q ↔ p ≤ q

theorem SetLike.coe_mono E0 #

∀ {A : Type u_1} {B : Type u_2} [inst : SetLike A B] [inst_1 : Preorder A] [IsMemLE A B], Monotone SetLike.coe

theorem SetLike.coe_ssubset_coe E0 #

∀ {A : Type u_1} {B : Type u_2} [inst : SetLike A B] [inst_1 : PartialOrder A] [IsMemLE A B] {p q : A}, ↑p ⊂ ↑q ↔ p < q

theorem SetLike.coe_strictMono E0 #

∀ {A : Type u_1} {B : Type u_2} [inst : SetLike A B] [inst_1 : PartialOrder A] [IsMemLE A B], StrictMono SetLike.coe

abbrev SetLike.instSubtypeSet E1 #

{X : Type u_3} → {p : Set X → Prop} → SetLike { s // p s } X

membership is inherited from Set X

abbrev SetLike.instSubtype E1 #

{X : Type u_3} → {S : Type u_4} → [SetLike S X] → {p : S → Prop} → SetLike { s // p s } X

membership is inherited from S

theorem SetLike.mem_mk_set E0 #

∀ {X : Type u_3} {p : Set X → Prop} {U : Set X} {h : p U} {x : X}, x ∈ ⟨U, h⟩ ↔ x ∈ U

theorem SetLike.mem_mk E0 #

∀ {X : Type u_3} {S : Type u_4} [inst : SetLike S X] {p : S → Prop} {U : S} {h : p U} {x : X}, x ∈ ⟨U, h⟩ ↔ x ∈ U

Deprecated aliases

NameNew nameSince
IsConcreteLEIsMemLE2026-11-20
IsConcreteLE.exists_of_ltexists_mem_notMem_of_lt2026-11-20
IsConcreteLE.le_iffIsMemLE.le_iff_mem_imp_mem2026-11-20
IsConcreteLE.lt_iff_le_and_existslt_iff_le_and_exists_mem_notMem2026-11-20
IsConcreteLE.not_le_iff_existsnot_le_iff_exists_mem_notMem2026-11-20
LE.ofSetLikeLE.ofMembership2026-09-01
SetLike.coe_injective'SetLike.coe_injective2026-06-04
SetLike.exists_not_mem_of_ne_topSetLike.exists_notMem_of_ne_top2026-09-28
SetLike.exists_of_ltexists_mem_notMem_of_lt2026-09-01
SetLike.le_defIsMemLE.le_iff_mem_imp_mem2026-09-01
SetLike.lt_iff_le_and_existslt_iff_le_and_exists_mem_notMem2026-09-01
SetLike.not_le_iff_existsnot_le_iff_exists_mem_notMem2026-09-01
SetLike.setOf_mem_eqSetLike.setOfPred_mem_eq2026-07-09