1-- Set-theoretic relations and functions2-- Part of Cumulus, the Grace17 mathematics library.3-- Copyright (c) 2026 Grant Passmore.4-- Released under the Apache License 2.0. See LICENSE in the repository root.5-- Authors: Grant Passmore67module Grace.Cumulus.Set.Relation8import Finite910-- (x, y) is the Kuratowski ordered pair. The maplet alias x ↦ y is11-- reserved for entries of function-like graphs; both elaborate to orderedPair.12notationinfixr72"↦"=> orderedPair1314-- A generous set bound containing every Kuratowski pair drawn from A and B.15-- Separation then cuts out the Cartesian product.16def productBound (A B : Set) : Set:=𝒫(𝒫(A ∪ B))17def product (A B : Set) : Set:= { p ∈ productBound(A, B) |∃ x ∈ A, ∃ y ∈ B, p = (x, y) }18notationinfixl65"×"=> product1920-- Every coordinate occurring in a Kuratowski pair in r lies in union(union(r)).21def coordinateBound (r : Set) : Set:=⋃(⋃(r))22def domain (r : Set) : Set:= { x ∈ coordinateBound(r) |∃ y, (x, y) ∈ r }23def range (r : Set) : Set:= { y ∈ coordinateBound(r) |∃ x, (x, y) ∈ r }24def restrict (r A : Set) : Set:= { p ∈ r |∃ x ∈ A, ∃ y, p = (x, y) }2526predicate RelationBetween (r A B : Set) := (r ⊆ A × B)27predicate Functional (r : Set) :=∀ x, ∀ y, relates(r, x, y) →∀ z, relates(r, x, z) → y = z28predicate FunctionBetween (r A B : Set) := RelationBetween(r, A, B) ∧ Functional(r) ∧ (∀ x ∈ A, ∃ y ∈ B, relates(r, x, y))2930-- A function from A to B is written f : A → B.31notationmixfix"_ : _ → _"=> FunctionBetween3233theorem product.pair (x y A B : Set)34requires hx : x ∈ A35requires hy : y ∈ B36shows (x, y) ∈ A × B37proof38apply product.membership.backward39constructor40 . unfold productBound41applyset.powerset.backward42intros z hz43applyset.powerset.backward44intros w hw45simp [orderedPair, pair.membership] at hz46cases hz with hsingle | hpair47 . rw hsingle at hw48simp [pair.membership] at hw49cases hw with hwx | hwx250 . rw hwx51simp52 . rw hwx253simp54 . rw hpair at hw55simp [pair.membership] at hw56cases hw with hwx | hwy57 . rw hwx58simp59 . rw hwy60simp61 . refine⟨x, hx, y, hy, rfl⟩6263theorem product.monotone (A B C D : Set)64requires hAC : A ⊆ C65requires hBD : B ⊆ D66shows (A × B ⊆ C × D)67proof68intros p hp69obtain⟨bounded, x, hxA, y, hyB, pairShape⟩:= product.membership.forward with hp70specialize hAC with x, hxA71specialize hBD with y, hyB72have pairIn := product.pair with hAC, hBD73 rwa pairShape7475theorem RelationBetween.monotone (r A B C D : Set)76requires relation : RelationBetween(r, A, B)77requires hAC : A ⊆ C78requires hBD : B ⊆ D79shows RelationBetween(r, C, D)80proof81unfold RelationBetween at relation82unfold RelationBetween83have productSubset := product.monotone with hAC, hBD84intros p hp85specialize relation with p, hp86exact productSubset with p, relation8788theorem orderedPair.first (a b c d : Set)89requires h : (a, b) = (c, d)90shows a = c91proof92simp [orderedPair] at h93have firstA := pair.left at {a, a}, {a, b}94rw h at firstA95simp [pair.membership] at firstA96cases firstA with haa | hab97have cMember := pair.left at c, c98rw← haa at cMember99simp [pair.membership] at cMember100cases cMember with hca | hca2101simp102simp103have cMember := pair.left at c, d104rw← hab at cMember105simp [pair.membership] at cMember106cases cMember with hca | hca2107simp108simp109110theorem orderedPair.injective (a b c d : Set)111requires h : (a, b) = (c, d)112shows a = c ∧ b = d113proof114constructor115exact orderedPair.first with h116have first := orderedPair.first with h117rw first at h118simp [orderedPair] at h119have outer := pair.cancel_left with h120exact pair.cancel_left with outer121122theorem range.contains (x y r : Set)123requires hPair : (x, y) ∈ r124shows y ∈ range(r)125proof126apply range.membership.backward127constructor128unfold coordinateBound129applyset.union.backward130witness {x, y}131constructor132applyset.union.backward133witness (x, y)134constructor135exact hPair136simp [orderedPair, pair.membership]137exact pair.right138witness x139exact hPair140141theorem range.empty142shows range(∅) =∅143proof144ext y145simp [range.membership, set.empty]146147theorem range.singleton_pair (x y : Set)148shows range({(x, y)}) = {y}149proof150ext z151constructor152intro hz153simp [range.membership] at hz154cases hz with hBound ∧ hWitness155obtain⟨source, hPair⟩:= hWitness156have hOnlyPair := singleton.membership.forward with hPair157have hCoordinates := orderedPair.injective with hOnlyPair158cases hCoordinates with hSource ∧ hValue159apply singleton.membership.backward160exact hValue161intro hz162have hValue := singleton.membership.forward with hz163rw hValue164have hPair := singleton.self at (x, y)165exact range.contains with hPair166167theorem range.pair_of_pairs (a b c d : Set)168shows range({(a, b), (c, d)}) = {b, d}169proof170ext z171constructor172 . intro hz173simp [range.membership] at hz174obtain⟨hBound, source, hPair⟩:= hz175simp [pair.membership] at hPair176cases hPair with hFirst | hSecond177 . obtain⟨hSource, hValue⟩:= orderedPair.injective with hFirst178rw hValue179exact pair.left180 . obtain⟨hSource, hValue⟩:= orderedPair.injective with hSecond181rw hValue182exact pair.right183 . intro hz184simp [pair.membership] at hz185cases hz with hFirst | hSecond186 . rw hFirst187exact range.contains with (pair.left at (a, b), (c, d))188 . rw hSecond189exact range.contains with (pair.right at (a, b), (c, d))190