Commit 2025-07-06 13:16 59bd63c7

View on Github →

refactor: make Rel less see-through (#25587) There is tension throughout the library between considering relations between α and β simply as α → β → Prop, or as a bundled object Rel α β with dedicated operations and API. The former approach is used almost everywhere as it is very lightweight and has arguably native support from core Lean features, but it cracks at the seams whenever one starts talking about operations on relations. For example:

  • composition of relations R : α → β → Prop, S : β → γ → Prop is the unwieldy Relation.Comp R S := fun a c ↦ ∃ b, R a b ∧ S b c
  • map of a relation R : α → β → Prop, under f : α → γ, g : β → δ is the monstruous Relation.map R f g := fun c d ↦ ∃ a b, r a b ∧ f a = c ∧ g b = d. The latter approach is embodied by the existing type Rel α β, with dedicated notation like for composition. It makes it much easier to reason about operations relations, but currently its API suffers from the leakage of its definition as
def Rel (α β : Type*) := α → β → Prop

The fact that Rel isn't an abbrev confuses automation. But simply making it an abbrev would kill the point of having a separate less see-through type to perform relation operations on. A final point, and the original motivation for this refactor, is that uniform spaces need a theory of relations on a type α as elements of Set (α × α). This cannot be worked around, since Set (α × α) is the type of elements of a filter on α × α. This theory is already developed in Topology.UniformSpace.Defs, and duplicates the existing Rel material. This PR is a proposal to refactor Rel to be less see-through by redefining it as

abbrev Rel (α β : Type*) := Set (α × β)

This has several advantages:

  • The use of α × β means that one can't abuse the defeq of Rel α β with the function type α → β → Prop.
  • Instead, we get to provide an explicit notation a ~[R] b that very closely follows the paper convention a ~R b of using relations as infixes. This notation is scoped to the Rel namespace.
  • The use of Set is an extra layer of indirection to avoid automation confusing Rel α β with α × β → Prop.
  • It can directly be used in the theory of uniform spaces because of the syntactic equality between Rel α α and Set (α × α).
  • A relation can still be defined from an explicit function α → β → Prop, with nice notation: What was previously fun a b ↦ R a b becomes {(a, b) | R a b}.
  • In general, fallout is manageably small and easily fixed by inserting the {(a, b) | R a b} and a ~[R] b notations. The benefits can be seen in places like CategoryTheory.Category.RelCat where automation is significantly improved, or Combinatorics.Hall.Basic where defeq abuse is avoided and dot notation becomes available. Zulip

Estimated changes

modified def Function.graph
modified theorem Function.graph_comp
deleted theorem Function.graph_def
modified theorem Function.graph_id
added theorem Function.mem_graph
added def Rel.cod
added theorem Rel.cod_empty
added theorem Rel.cod_inv
added theorem Rel.cod_mono
added theorem Rel.cod_univ
deleted def Rel.codom
deleted theorem Rel.codom_inv
modified def Rel.comp
modified theorem Rel.comp_assoc
added theorem Rel.comp_empty
added theorem Rel.comp_id
deleted theorem Rel.comp_left_bot
deleted theorem Rel.comp_left_id
deleted theorem Rel.comp_left_top
deleted theorem Rel.comp_right_bot
deleted theorem Rel.comp_right_id
deleted theorem Rel.comp_right_top
added theorem Rel.comp_univ
modified def Rel.core
modified theorem Rel.core_comp
modified theorem Rel.core_id
modified theorem Rel.core_inter
modified theorem Rel.core_mono
deleted theorem Rel.core_subset
added theorem Rel.core_subset_core
deleted theorem Rel.core_union
added theorem Rel.core_union_subset
modified theorem Rel.core_univ
modified def Rel.dom
added theorem Rel.dom_empty
modified theorem Rel.dom_inv
modified theorem Rel.dom_mono
added theorem Rel.dom_univ
added theorem Rel.empty_comp
deleted theorem Rel.ext
added theorem Rel.id_comp
modified def Rel.image
deleted theorem Rel.image_bot
modified theorem Rel.image_comp
modified theorem Rel.image_core_gc
deleted theorem Rel.image_empty
added theorem Rel.image_empty_left
added theorem Rel.image_empty_right
modified theorem Rel.image_id
deleted theorem Rel.image_inter
added theorem Rel.image_inter_dom
deleted theorem Rel.image_inter_dom_eq
added theorem Rel.image_inter_subset
added theorem Rel.image_inv
modified theorem Rel.image_mono
deleted theorem Rel.image_subset
modified theorem Rel.image_subset_iff
added theorem Rel.image_subset_image
deleted theorem Rel.image_top
modified theorem Rel.image_union
deleted theorem Rel.image_univ
added theorem Rel.image_univ_left
added theorem Rel.image_univ_right
modified def Rel.inv
deleted theorem Rel.inv_bot
modified theorem Rel.inv_comp
deleted theorem Rel.inv_def
added theorem Rel.inv_empty
modified theorem Rel.inv_id
modified theorem Rel.inv_inv
added theorem Rel.inv_mono
deleted theorem Rel.inv_top
added theorem Rel.inv_univ
added theorem Rel.mem_cod
added theorem Rel.mem_comp
modified theorem Rel.mem_core
added theorem Rel.mem_dom
added theorem Rel.mem_id
modified theorem Rel.mem_image
added theorem Rel.mem_inv
modified theorem Rel.mem_preimage
modified def Rel.preimage
deleted theorem Rel.preimage_bot
modified theorem Rel.preimage_comp
deleted theorem Rel.preimage_def
deleted theorem Rel.preimage_empty
modified theorem Rel.preimage_id
deleted theorem Rel.preimage_inter
added theorem Rel.preimage_inter_cod
modified theorem Rel.preimage_inv
modified theorem Rel.preimage_mono
deleted theorem Rel.preimage_top
modified theorem Rel.preimage_union
deleted theorem Rel.preimage_univ
added theorem Rel.preimage_univ_left
modified def Rel.restrictDomain
added theorem Rel.univ_comp
deleted def Rel
deleted theorem Relation.is_graph_iff
modified def RelSeries.append
modified theorem RelSeries.append_apply_left
modified theorem RelSeries.append_assoc
modified theorem RelSeries.append_cons
modified def RelSeries.cons
modified theorem RelSeries.cons_cast_succ
modified theorem RelSeries.head_append
modified theorem RelSeries.head_cons
modified theorem RelSeries.head_map
modified theorem RelSeries.head_snoc
modified theorem RelSeries.last_append
modified theorem RelSeries.last_cons
modified theorem RelSeries.last_map
modified theorem RelSeries.last_snoc'
modified theorem RelSeries.last_snoc
modified theorem RelSeries.length_eq_zero
modified theorem RelSeries.length_ne_zero
modified theorem RelSeries.length_pos
modified def RelSeries.map
modified theorem RelSeries.map_apply
modified theorem RelSeries.mem_snoc
modified theorem RelSeries.rel_of_lt
modified theorem RelSeries.rel_or_eq_of_le
modified def RelSeries.reverse
modified def RelSeries.snoc
modified theorem RelSeries.snoc_castSucc
modified theorem RelSeries.tail_cons
modified theorem RelSeries.toList_append
modified theorem RelSeries.toList_chain'
modified theorem RelSeries.toList_cons
modified theorem RelSeries.toList_snoc