Sets, Functions & Relations

A typed working language for discrete structures: set operations, Cartesian products, relations, total and partial functions, images, composition, inverses, and finite-path closures.

Sets say which objects are present. Relations say which tuples satisfy a condition. Functions are relations with an exact output obligation: for every input in their domain, there is one and only one output. Keeping those types visible turns many apparently different problems—database joins, graph edges, state transitions, lookup tables, and API mappings—into the same small collection of operations.

Sets: membership determines identity #

Extensionality is a proof method, not just a definition. To prove A=BA=B, prove both inclusions:

A⊆BandB⊆A,A\subseteq B \quad\text{and}\quad B\subseteq A,

where A⊆BA\subseteq B means ∀x (x∈A⇒x∈B)\forall x\,(x\in A\Rightarrow x\in B). By contrast, x∈Ax\in A compares an object with a set; A⊆BA\subseteq B compares two sets. Confusing those two levels is one of the most common type errors in discrete mathematics.

A set-builder expression should be read relative to an already available universe or set:

{x∈U:P(x)}.\{x\in U:P(x)\}.

Writing an unrestricted “set of every object satisfying PP” is not always legitimate. In particular, the attempted collection R={x:x∉x}R=\{x:x\notin x\} would imply R∈R↔R∉RR\in R\leftrightarrow R\notin R; this is Russell's paradox, not a set.

Operations are membership programs #

For A,B⊆UA,B\subseteq U, every ordinary operation can be specified by the Boolean test used for membership.

Construct Membership condition What it keeps
A∪BA\cup B x∈A∨x∈Bx\in A\lor x\in B either side
A∩BA\cap B x∈A∧x∈Bx\in A\land x\in B both sides
A∖BA\setminus B x∈A∧x∉Bx\in A\land x\notin B AA except BB
Ac=U∖AA^c=U\setminus A x∉Ax\notin A the complement relative to UU
A△BA\mathbin{\triangle}B exactly one of x∈A,x∈Bx\in A,x\in B membership that differs
P(A)\mathcal P(A) X⊆AX\subseteq A every subset of AA

This view makes the algebra checkable one element at a time. For example,

x∈(A∩B)c  ⟺  ¬(x∈A∧x∈B)  ⟺  x∉A∨x∉B  ⟺  x∈Ac∪Bc.\begin{aligned} x\in (A\cap B)^c &\iff \neg(x\in A\land x\in B)\\ &\iff x\notin A\lor x\notin B\\ &\iff x\in A^c\cup B^c. \end{aligned}

Extensionality then gives (A∩B)c=Ac∪Bc(A\cap B)^c=A^c\cup B^c. The same method proves associativity, distributivity, absorption, and the second De Morgan law. It also prevents two frequent transcription errors:

A∖B=A∩Bc,(A∩B)c=Ac∪Bc.A\setminus B=A\cap B^c, \qquad (A\cap B)^c=A^c\cup B^c.

Families and the empty-family edge case #

For an indexed family (Ai)i∈I(A_i)_{i\in I} inside a fixed universe UU,

x∈⋃i∈IAi  ⟺  ∃i∈I  (x∈Ai),x∈⋂i∈IAi  ⟺  ∀i∈I  (x∈Ai).x\in\bigcup_{i\in I}A_i \iff \exists i\in I\;(x\in A_i), \qquad x\in\bigcap_{i\in I}A_i \iff \forall i\in I\;(x\in A_i).

If I=∅I=\varnothing, the existential condition is false, so the union is ∅\varnothing. The universal condition is vacuously true, so the intersection is UU relative to the declared universe. Without an ambient UU, “the intersection of no sets is the set of everything” does not define a set in ordinary axiomatic set theory.

Why a finite power set has size 2n2^n #

Let AA have nn elements. A subset S⊆AS\subseteq A makes one independent binary decision for every a∈Aa\in A: include it or exclude it. Equivalently, SS has a characteristic function

χS:A→{0,1},χS(a)=1  ⟺  a∈S.\chi_S:A\to\{0,1\},\qquad \chi_S(a)=1\iff a\in S.

This is a bijection between P(A)\mathcal P(A) and all nn-bit functions, so

∣P(A)∣=2n.|\mathcal P(A)|=2^n.

The edge case is informative: P(∅)={∅}\mathcal P(\varnothing)=\{\varnothing\}, hence 20=12^0=1.

Cartesian products make positions explicit #

An ordered pair ⟨a,b⟩\langle a,b\rangle remembers which value is first. A particular set encoding, such as ⟨a,b⟩={{a},{a,b}}\langle a,b\rangle=\{\{a\},\{a,b\}\}, is useful in foundational reductions, but applications normally rely only on the invariant

⟨a,b⟩=⟨c,d⟩  ⟺  a=c∧b=d.\langle a,b\rangle=\langle c,d\rangle\iff a=c\land b=d.

The Cartesian product

A×B={⟨a,b⟩:a∈A∧b∈B}A\times B=\{\langle a,b\rangle:a\in A\land b\in B\}

is typed: the first coordinate comes from AA, the second from BB. If AA and BB are finite, then

∣A×B∣=∣A∣ ∣B∣,|A\times B|=|A|\,|B|,

because every one of the ∣A∣|A| first-coordinate choices can be paired with every one of the ∣B∣|B| second-coordinate choices. An nn-tuple is an element of A1×⋯×AnA_1\times\cdots\times A_n; do not silently identify it with an arbitrarily nested binary pair unless an encoding has been chosen.

Relations are sets of typed witnesses #

This representation turns a two-argument predicate into stored witnesses. It also exposes the types: “is a dependency of” may relate packages to packages, while “owns” may relate users to services. Composition is legal only when the middle types agree.

For R⊆A×BR\subseteq A\times B:

dom(R)={a∈A:∃b∈B  aRb},ran(R)={b∈B:∃a∈A  aRb},R−1={⟨b,a⟩:aRb}.\begin{aligned} \mathop{\mathrm{dom}}(R)&=\{a\in A:\exists b\in B\;aRb\},\\ \mathop{\mathrm{ran}}(R)&=\{b\in B:\exists a\in A\;aRb\},\\ R^{-1}&=\{\langle b,a\rangle:aRb\}. \end{aligned}

If R⊆A×BR\subseteq A\times B and S⊆B×CS\subseteq B\times C, use the same order as function composition:

a (S∘R) c  ⟺  ∃b∈B  (aRb∧bSc).a\,(S\circ R)\,c \iff \exists b\in B\;(aRb\land bSc).

Read S∘RS\circ R right to left: first follow RR, then SS.

Functions are total, single-valued relations #

As a relation, Γf\Gamma_f satisfies existence and uniqueness:

∀a∈A  ∃!b∈B  (a,b)∈Γf.\forall a\in A\;\exists!b\in B\;(a,b)\in\Gamma_f.

A partial function p:A⇀Bp:A\rightharpoonup B keeps uniqueness but drops existence for some inputs. Its actual domain is dom(p)⊆A\mathop{\mathrm{dom}}(p)\subseteq A. A map that returns either a value or an explicit error can instead be modelled as a total function A→B+EA\to B+E; that makes failure data part of the codomain rather than silently leaving the function undefined.

The diagram is a constraint ladder. Moving downward adds obligations; it does not change the function's declared domain or codomain. In particular, surjectivity is not “the range is whatever happened to occur.” It is the assertion that the range equals the already declared codomain.

Term for f:A→Bf:A\to B Quantified condition Diagnostic question
range ran(f)={f(a):a∈A}\mathop{\mathrm{ran}}(f)=\{f(a):a\in A\} Which outputs actually occur?
injective f(a1)=f(a2)⇒a1=a2f(a_1)=f(a_2)\Rightarrow a_1=a_2 Can two inputs collide?
surjective ∀b∈B  ∃a∈A  f(a)=b\forall b\in B\;\exists a\in A\;f(a)=b Is any declared output unreachable?
bijective injective and surjective Does every output have exactly one input?

Images and preimages have different algebra #

For S⊆AS\subseteq A and T⊆BT\subseteq B,

f[S]={f(a):a∈S},f−1[T]={a∈A:f(a)∈T}.f[S]=\{f(a):a\in S\},\qquad f^{-1}[T]=\{a\in A:f(a)\in T\}.

The notation f−1[T]f^{-1}[T] means preimage even when ff has no inverse function.

Preimages preserve all Boolean set operations because membership is tested pointwise:

f−1[T1∪T2]=f−1[T1]∪f−1[T2],f−1[T1∩T2]=f−1[T1]∩f−1[T2],f−1[B∖T]=A∖f−1[T].f^{-1}[T_1\cup T_2]=f^{-1}[T_1]\cup f^{-1}[T_2], \quad f^{-1}[T_1\cap T_2]=f^{-1}[T_1]\cap f^{-1}[T_2], \quad f^{-1}[B\setminus T]=A\setminus f^{-1}[T].

Images always preserve unions, but only satisfy

f[S1∩S2]⊆f[S1]∩f[S2].f[S_1\cap S_2]\subseteq f[S_1]\cap f[S_2].

Equality can fail when ff is not injective. Let f(x)=x2f(x)=x^2, S1={−1}S_1=\{-1\}, and S2={1}S_2=\{1\}. Then S1∩S2=∅S_1\cap S_2=\varnothing, but both images are {1}\{1\}, so their image intersection is nonempty. Injectivity removes exactly this collision and restores equality.

Two identities are useful diagnostics:

S⊆f−1[f[S]],f[f−1[T]]⊆T.S\subseteq f^{-1}[f[S]],\qquad f[f^{-1}[T]]\subseteq T.

The first is equality when ff is injective; the second is equality when every element of TT lies in the range of ff.

Composition, identities, and inverses #

Given f:A→Bf:A\to B and g:B→Cg:B\to C,

(g∘f)(a)=g(f(a)).(g\circ f)(a)=g(f(a)).

Composition is associative because both parenthesizations evaluate to the same value:

((h∘g)∘f)(a)=h(g(f(a)))=(h∘(g∘f))(a).((h\circ g)\circ f)(a)=h(g(f(a)))=(h\circ(g\circ f))(a).

The identity idA(a)=a\mathop{\mathrm{id}}_A(a)=a is neutral: f∘idA=ff\circ\mathop{\mathrm{id}}_A=f and idB∘f=f\mathop{\mathrm{id}}_B\circ f=f. These are type-sensitive equations; idA\mathop{\mathrm{id}}_A and idB\mathop{\mathrm{id}}_B are usually different functions.

A map ℓ:B→A\ell:B\to A is a left inverse when ℓ∘f=idA\ell\circ f=\mathop{\mathrm{id}}_A. A map r:B→Ar:B\to A is a right inverse when f∘r=idBf\circ r=\mathop{\mathrm{id}}_B.

  • A left inverse forces injectivity: if f(a1)=f(a2)f(a_1)=f(a_2), apply ℓ\ell to both sides.
  • Conversely, an injective f:A→Bf:A\to B has a left inverse when AA is nonempty: use the unique preimage on f[A]f[A] and a fixed default a0∈Aa_0\in A elsewhere. The exceptional injection ∅→B\varnothing\to B has a left inverse only if B=∅B=\varnothing.
  • A right inverse forces surjectivity because b=f(r(b))b=f(r(b)) supplies a preimage.
  • Constructing a right inverse for an arbitrary surjection requires choosing one preimage from every nonempty fiber. It is automatic for finite explicitly enumerable fibers; for arbitrary families, “every surjection splits” is a form of the Axiom of Choice.
  • A two-sided inverse exists exactly for a bijection and is unique. Its value at bb is the unique aa with f(a)=bf(a)=b.

Relation powers and finite-path closure #

Let R⊆A×AR\subseteq A\times A be an endorelation and IA={(a,a):a∈A}I_A=\{(a,a):a\in A\}. Define

R0=IA,Rn+1=R∘Rn.R^0=I_A,\qquad R^{n+1}=R\circ R^n.

Then (a,b)∈Rn(a,b)\in R^n means there is an RR-path of exactly nn steps from aa to bb. The two standard closures are

R+=⋃n≥1Rn,R∗=⋃n≥0Rn=IA∪R+.R^+=\bigcup_{n\ge 1}R^n, \qquad R^*=\bigcup_{n\ge 0}R^n=I_A\cup R^+.

  • R+R^+ is the transitive closure: positive-length reachability.
  • R∗R^* is the reflexive-transitive closure: reachability in zero or more steps.
  • The reflexive closure alone is merely IA∪RI_A\cup R; it is generally smaller than R∗R^*.

The word “closure” includes a minimality claim. If TT is transitive and R⊆TR\subseteq T, induction gives Rn⊆TR^n\subseteq T for every n≥1n\ge1, hence R+⊆TR^+\subseteq T. Therefore R+R^+ is not just transitive; it is the least transitive relation containing RR. The same argument, adding reflexivity, proves the corresponding property of R∗R^*. This path-based definition matches the formal TransGen and ReflTransGen constructions in Mathlib's relation library.

A worked model: users, services, and owners #

Let UU be users, SS services, C⊆U×SC\subseteq U\times S the “may call” relation, and o:S→Uo:S\to U the owner function.

  • C[u]={s∈S:uCs}C[u]=\{s\in S:uCs\} is the set of services user uu may call.
  • C−1[s]={u∈U:uCs}C^{-1}[s]=\{u\in U:uCs\} is the caller set for service ss.
  • o−1[{u}]o^{-1}[\{u\}] is the fiber of services owned by uu.
  • oo is surjective only if every declared user owns at least one service; it is injective only if no user owns two services.
  • The relation O={(s,o(s)):s∈S}O=\{(s,o(s)):s\in S\} is functional from services to users. Its inverse relation need not be a function, because one user may own many services.

That last distinction is operationally important: reversing arrows in a data model does not automatically produce an inverse function. First inspect fiber cardinalities.

Diagnostic checklist #

When a discrete model feels ambiguous, ask these questions in order:

  1. What are the carrier sets, and which universe makes complements meaningful?
  2. Is each symbol membership, inclusion, a relation, or a function? Are its input/output types written down?
  3. If it is called a function, is it total? If failures are possible, are they represented as partiality or explicit result values?
  4. Is “onto” measured against a declared codomain or a retroactively chosen range?
  5. Does a claimed inverse satisfy the correct left and right identity equations?
  6. Does a closure include zero steps, positive steps, or only added identity pairs?
  7. For any equality of sets or relations, what does an arbitrary element/pair have to satisfy on each side?

Most errors become short counterexamples once the carrier sets and quantifiers are explicit.

Sources

  1. bookSets, Functions, and Relations: Part I — Naïve Set Theorypdfnotes
  2. bookSpecial Sections of Mathematics 1: Discrete Mathematics, Section 1, Topic 1pdfnotes
  3. docsMathlib — Set Operations
  4. docsMathlib — Basic Function Theory
  5. docsMathlib — Relations and Relation Closures

Searches titles, aliases, headings, summaries, and full text.↑↓ move ↵ open esc close