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 : β → γ → Propis the unwieldyRelation.Comp R S := fun a c ↦ ∃ b, R a b ∧ S b c - map of a relation
R : α → β → Prop, underf : α → γ,g : β → δis the monstruousRelation.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 typeRel α β, 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 ofRel α βwith the function typeα → β → Prop. - Instead, we get to provide an explicit notation
a ~[R] bthat very closely follows the paper conventiona ~R bof using relations as infixes. This notation is scoped to theRelnamespace. - The use of
Setis an extra layer of indirection to avoid automation confusingRel α βwithα × β → Prop. - It can directly be used in the theory of uniform spaces because of the syntactic equality between
Rel α αandSet (α × α). - A relation can still be defined from an explicit function
α → β → Prop, with nice notation: What was previouslyfun a b ↦ R a bbecomes{(a, b) | R a b}. - In general, fallout is manageably small and easily fixed by inserting the
{(a, b) | R a b}anda ~[R] bnotations. The benefits can be seen in places likeCategoryTheory.Category.RelCatwhere automation is significantly improved, orCombinatorics.Hall.Basicwhere defeq abuse is avoided and dot notation becomes available. Zulip