1-- The algebra of relations: domains and ranges, restrictions, converses,2-- and composition3-- Part of Cumulus, the Grace17 mathematics library.4-- Copyright (c) 2026 Grant Passmore.5-- Released under the Apache License 2.0. See LICENSE in the repository root.6-- Authors: Grant Passmore78module Grace.Cumulus.Relation.Algebra9import Set.Function.Image1011-- Domains and ranges1213theorem domain.contains (x y r : Set)14requires h : relates(r, x, y)15shows x ∈ domain(r)16proof17have member : (x, y) ∈ r := h18apply domain.membership.backward19constructor20 . unfold coordinateBound21applyset.union.backward22witness {x, y}23constructor24 . applyset.union.backward25witness (x, y)26constructor27 . exact member28 . simp [orderedPair, pair.membership]29 . simp [pair.membership]30 . witness y31exact member3233theorem domain.membership_iff (x r : Set)34shows x ∈ domain(r) ↔∃ y, relates(r, x, y)35proof36constructor37 . intro h38simp [domain.membership] at h39obtain⟨y, hy⟩:= h.right40witness y41exact hy42 . intro h43obtain⟨y, hy⟩:= h44exact domain.contains with hy4546theorem range.membership_iff (y r : Set)47shows y ∈ range(r) ↔∃ x, relates(r, x, y)48proof49constructor50 . intro h51simp [range.membership] at h52obtain⟨x, hx⟩:= h.right53witness x54exact hx55 . intro h56obtain⟨x, hx⟩:= h57exact range.contains at x, y, r with hx5859theorem domain.subset (r A B : Set)60requires relation : RelationBetween(r, A, B)61shows domain(r) ⊆ A62proof63intros x hx64obtain⟨y, hy⟩:= domain.membership_iff.forward with hx65have bounds := RelationBetween.bounds with relation, hy66exact bounds.left6768theorem range.subset (r A B : Set)69requires relation : RelationBetween(r, A, B)70shows range(r) ⊆ B71proof72intros y hy73obtain⟨x, hx⟩:= range.membership_iff.forward with hy74have bounds := RelationBetween.bounds with relation, hx75exact bounds.right7677theorem domain.monotone (r s : Set)78requires h : r ⊆ s79shows domain(r) ⊆ domain(s)80proof81intros x hx82obtain⟨y, hy⟩:= domain.membership_iff.forward with hx83have inS : (x, y) ∈ s := h with (x, y), hy84exact domain.contains at x, y, s with inS8586theorem range.monotone (r s : Set)87requires h : r ⊆ s88shows range(r) ⊆ range(s)89proof90intros y hy91obtain⟨x, hx⟩:= range.membership_iff.forward with hy92have inS : (x, y) ∈ s := h with (x, y), hx93exact range.contains with inS9495theorem domain.empty96shows domain(∅) =∅97proof98apply Subset.empty_iff.forward99intros x hx100obtain⟨y, hy⟩:= domain.membership_iff.forward with hx101have member : (x, y) ∈∅:= hy102exfalso103exactset.empty with member104105theorem domain.union2 (r s : Set)106shows domain(r ∪ s) = domain(r) ∪ domain(s)107proof108ext x109constructor110 . intro hx111obtain⟨y, hy⟩:= domain.membership_iff.forward with hx112have member : (x, y) ∈ r ∪ s := hy113simp [union2.membership] at member114simp [union2.membership]115cases member with hr | hs116 . left117exact domain.contains at x, y, r with hr118 . right119exact domain.contains at x, y, s with hs120 . intro hx121simp [union2.membership] at hx122have leftSubset := union2.subset_left at r, s123have rightSubset := union2.subset_right at r, s124cases hx with hr | hs125 . exact domain.monotone with leftSubset, x, hr126 . exact domain.monotone with rightSubset, x, hs127128theorem range.union2 (r s : Set)129shows range(r ∪ s) = range(r) ∪ range(s)130proof131ext y132constructor133 . intro hy134obtain⟨x, hx⟩:= range.membership_iff.forward with hy135have member : (x, y) ∈ r ∪ s := hx136simp [union2.membership] at member137simp [union2.membership]138cases member with hr | hs139 . left140exact range.contains with hr141 . right142exact range.contains with hs143 . intro hy144simp [union2.membership] at hy145have leftSubset := union2.subset_left at r, s146have rightSubset := union2.subset_right at r, s147cases hy with hr | hs148 . exact range.monotone with leftSubset, y, hr149 . exact range.monotone with rightSubset, y, hs150151theorem RelationBetween.domain_range (r A B : Set)152requires relation : RelationBetween(r, A, B)153shows r ⊆ domain(r) × range(r)154proof155have included : r ⊆ A × B := relation156intros p hp157have shape : p ∈ A × B := included with p, hp158simp [product.membership] at shape159obtain⟨bound, x, hx, y, hy, equation⟩:= shape160rw equation at hp161rw equation162have inDomain := domain.contains at x, y, r with hp163have inRange := range.contains with hp164exact product.pair with inDomain, inRange165166theorem FunctionBetween.domain (f A B : Set)167requires function : f : A → B168shows domain(f) = A169proof170apply Subset.antisymmetric171 . exact domain.subset with function.left172 . intros x hx173have related := FunctionBetween.relates_apply with function, hx174exact domain.contains with related175176theorem FunctionBetween.range (f A B : Set)177requires function : f : A → B178shows range(f) = functionImage(f, A, B)179proof180ext y181constructor182 . intro hy183obtain⟨x, hx⟩:= range.membership_iff.forward with hy184have bounds := RelationBetween.bounds with function.left, hx185have value := FunctionBetween.apply_value with function, bounds.left, hx186simp [functionImage.membership]187constructor188 . exact bounds.right189 . witness x190constructor191 . exact bounds.left192 . symm193exact value194 . intro hy195simp [functionImage.membership] at hy196obtain⟨inB, x, hx, shape⟩:= hy197have related := FunctionBetween.relates_apply with function, hx198rw shape199exact range.contains at x, f(x), f with related200201-- Restrictions202203theorem restrict.subset (r A : Set)204shows restrict(r, A) ⊆ r205proof206intros p hp207simp [restrict.membership] at hp208exact hp.left209210theorem restrict.relation (r A B S : Set)211requires relation : RelationBetween(r, A, B)212shows RelationBetween(restrict(r, S), S ∩ A, B)213proof214intros p hp215simp [restrict.membership] at hp216obtain⟨hr, x, hx, y, equation⟩:= hp217rw equation at hr218rw equation219have bounds := RelationBetween.bounds at r, A, B, x, y with relation, hr220apply product.pair221 . simp [intersection.membership]222 . exact bounds.right223224theorem restrict.whole (r A B : Set)225requires relation : RelationBetween(r, A, B)226shows restrict(r, A) = r227proof228have included : r ⊆ A × B := relation229apply Subset.antisymmetric230 . exact restrict.subset at r, A231 . intros p hp232have shape : p ∈ A × B := included with p, hp233simp [product.membership] at shape234obtain⟨bound, x, hx, y, hy, equation⟩:= shape235simp [restrict.membership]236constructor237 . exact hp238 . witness x239constructor240 . exact hx241 . witness y242exact equation243244theorem restrict.empty (r : Set)245shows restrict(r, ∅) =∅246proof247apply Subset.empty_iff.forward248intros p hp249simp [restrict.membership] at hp250251theorem restrict.intersection (r S T : Set)252shows restrict(restrict(r, S), T) = restrict(r, S ∩ T)253proof254ext p255constructor256 . intro hp257simp [restrict.membership] at hp258obtain⟨inner, x, hxT, y, equation⟩:= hp259simp [restrict.membership] at inner260obtain⟨hr, z, hzS, w, equationZ⟩:= inner261rw equation at equationZ262have coordinates := orderedPair.injective with equationZ263simp [restrict.membership]264constructor265 . exact hr266 . witness x267constructor268 . simp [intersection.membership]269 . witness y270exact equation271 . intro hp272simp [restrict.membership] at hp273obtain⟨hr, x, hx, y, equation⟩:= hp274simp [intersection.membership] at hx275simp [restrict.membership]276constructor277 . constructor278 . exact hr279 . exact⟨x, hx.left, y, equation⟩280 . exact⟨x, hx.right, y, equation⟩281282theorem restrict.monotone (r S T : Set)283requires h : S ⊆ T284shows restrict(r, S) ⊆ restrict(r, T)285proof286intros p hp287simp [restrict.membership] at hp288obtain⟨hr, x, hx, y, equation⟩:= hp289simp [restrict.membership]290constructor291 . exact hr292 . exact⟨x, (h with x, hx), y, equation⟩293294theorem restrict.union2 (r S T : Set)295shows restrict(r, S ∪ T) = restrict(r, S) ∪ restrict(r, T)296proof297ext p298constructor299 . intro hp300simp [restrict.membership] at hp301obtain⟨hr, x, hx, y, equation⟩:= hp302simp [union2.membership] at hx303simp [union2.membership]304cases hx with hS | hT305 . left306simp [restrict.membership]307exact⟨hr, x, hS, y, equation⟩308 . right309simp [restrict.membership]310exact⟨hr, x, hT, y, equation⟩311 . intro hp312simp [union2.membership] at hp313have leftSubset := union2.subset_left at S, T314have rightSubset := union2.subset_right at S, T315cases hp with hS | hT316 . exact restrict.monotone at r, S, S ∪ T with leftSubset, p, hS317 . exact restrict.monotone at r, T, S ∪ T with rightSubset, p, hT318319theorem restrict.domain (r S : Set)320shows domain(restrict(r, S)) = domain(r) ∩ S321proof322ext x323constructor324 . intro hx325obtain⟨y, hy⟩:= domain.membership_iff.forward with hx326have related := restrict.relates.forward with hy327simp [intersection.membership]328constructor329 . exact domain.contains with related.right330 . exact related.left331 . intro hx332simp [intersection.membership] at hx333obtain⟨y, hy⟩:= domain.membership_iff.forward with hx.left334have related := restrict.relates.backward at r, S, x, y with⟨hx.right, hy⟩335exact domain.contains with related336337-- Converses338339theorem converse.converse (r A B : Set)340requires relation : RelationBetween(r, A, B)341shows converse(converse(r, A, B), B, A) = r342proof343ext p344constructor345 . intro hp346simp [converse.membership] at hp347obtain⟨bound, y, hy, x, hx, equation, related⟩:= hp348have member : (y, x) ∈ converse(r, A, B) := related349simp [converse.membership] at member350obtain⟨boundInner, x2, hx2, y2, hy2, equationInner, relatedInner⟩:= member351have coordinates := orderedPair.injective with equationInner352rw equation353 rwa [← coordinates.left, ← coordinates.right] at relatedInner354 . intro hp355have included : r ⊆ A × B := relation356have shape : p ∈ A × B := included with p, hp357simp [product.membership] at shape358obtain⟨bound, x, hx, y, hy, equation⟩:= shape359rw equation at hp360rw equation361have once := converse.relates at r, A, B, x, y with hx, hy, hp362exact converse.relates with hy, hx, once363364theorem converse.monotone (r s A B : Set)365requires h : r ⊆ s366shows converse(r, A, B) ⊆ converse(s, A, B)367proof368intros p hp369simp [converse.membership] at hp370obtain⟨bound, x, hx, y, hy, equation, related⟩:= hp371rw equation372have inS : (x, y) ∈ s := h with (x, y), related373exact converse.relates at s, A, B, x, y with hx, hy, inS374375theorem converse.domain (r A B : Set)376requires relation : RelationBetween(r, A, B)377shows domain(converse(r, A, B)) = range(r)378proof379ext y380constructor381 . intro hy382obtain⟨x, hx⟩:= domain.membership_iff.forward with hy383have member : (y, x) ∈ converse(r, A, B) := hx384simp [converse.membership] at member385obtain⟨bound, x2, hx2, y2, hy2, equation, related⟩:= member386have coordinates := orderedPair.injective with equation387rw [coordinates.left]388exact range.contains at x2, y2, r with related389 . intro hy390obtain⟨x, hx⟩:= range.membership_iff.forward with hy391have bounds := RelationBetween.bounds with relation, hx392have flipped := converse.relates with bounds.left, bounds.right, hx393exact domain.contains with flipped394395theorem converse.range (r A B : Set)396requires relation : RelationBetween(r, A, B)397shows range(converse(r, A, B)) = domain(r)398proof399ext x400constructor401 . intro hx402obtain⟨y, hy⟩:= range.membership_iff.forward with hx403have member : (y, x) ∈ converse(r, A, B) := hy404simp [converse.membership] at member405obtain⟨bound, x2, hx2, y2, hy2, equation, related⟩:= member406have coordinates := orderedPair.injective with equation407rw [coordinates.right]408exact domain.contains with related409 . intro hx410obtain⟨y, hy⟩:= domain.membership_iff.forward with hx411have bounds := RelationBetween.bounds with relation, hy412have flipped := converse.relates with bounds.left, bounds.right, hy413exact range.contains at y, x, converse(r, A, B) with flipped414415-- Composition416417theorem compose.associative (h g f A B C D : Set)418requires fFunction : f : A → B419requires gFunction : g : B → C420requires hFunction : h : C → D421shows compose(h, compose(g, f, A, B, C), A, C, D) = compose(compose(h, g, B, C, D), f, A, B, D)422proof423have inner := compose.function with fFunction, gFunction424have outer := compose.function with gFunction, hFunction425apply FunctionBetween.ext with (compose.function with inner, hFunction), (compose.function with fFunction, outer)426intros x hx427have valueB := FunctionBetween.apply_closed with fFunction, hx428have leftStep := compose.apply with inner, hFunction, hx429have innerStep := compose.apply with fFunction, gFunction, hx430have rightStep := compose.apply with fFunction, outer, hx431have outerStep := compose.apply with gFunction, hFunction, valueB432rw [leftStep, innerStep, rightStep, outerStep]433rfl434435theorem compose.restrict (g f A B C S : Set)436requires fFunction : f : A → B437requires gFunction : g : B → C438requires hS : S ⊆ A439shows restrict(compose(g, f, A, B, C), S) = compose(g, restrict(f, S), S, B, C)440proof441have composed := compose.function with fFunction, gFunction442have closed : ∀ x ∈ S, f(x) ∈ B443intros x hx444exact FunctionBetween.apply_closed with fFunction, (hS with x, hx)445have restricted := restrict.function with fFunction, hS, closed446have closedComposed : ∀ x ∈ S, compose(g, f, A, B, C)(x) ∈ C447intros x hx448exact FunctionBetween.apply_closed with composed, (hS with x, hx)449have restrictedComposed := restrict.function with composed, hS, closedComposed450have composedRestricted := compose.function with restricted, gFunction451apply FunctionBetween.ext with restrictedComposed, composedRestricted452intros x hx453have hxA : x ∈ A := hS with x, hx454have leftStep := restrict.apply with composed, hS, closedComposed, hx455have innerStep := compose.apply with fFunction, gFunction, hxA456have rightStep := compose.apply with restricted, gFunction, hx457have restrictedStep := restrict.apply with fFunction, hS, closed, hx458rw [leftStep, innerStep, rightStep, restrictedStep]459rfl460