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 , prove both inclusions:
where means . By contrast, compares an object with a set; 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:
Writing an unrestricted “set of every object satisfying ” is not always legitimate. In particular, the attempted collection would imply ; this is Russell's paradox, not a set.
Operations are membership programs #
For , every ordinary operation can be specified by the Boolean test used for membership.
| Construct | Membership condition | What it keeps |
|---|---|---|
| either side | ||
| both sides | ||
| except | ||
| the complement relative to | ||
| exactly one of | membership that differs | |
| every subset of |
This view makes the algebra checkable one element at a time. For example,
Extensionality then gives . The same method proves associativity, distributivity, absorption, and the second De Morgan law. It also prevents two frequent transcription errors:
Families and the empty-family edge case #
For an indexed family inside a fixed universe ,
If , the existential condition is false, so the union is . The universal condition is vacuously true, so the intersection is relative to the declared universe. Without an ambient , “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 #
Let have elements. A subset makes one independent binary decision for every : include it or exclude it. Equivalently, has a characteristic function
This is a bijection between and all -bit functions, so
The edge case is informative: , hence .
Cartesian products make positions explicit #
An ordered pair remembers which value is first. A particular set encoding, such as , is useful in foundational reductions, but applications normally rely only on the invariant
The Cartesian product
is typed: the first coordinate comes from , the second from . If and are finite, then
because every one of the first-coordinate choices can be paired with every one of the second-coordinate choices. An -tuple is an element of ; 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 :
If and , use the same order as function composition:
Read right to left: first follow , then .
Functions are total, single-valued relations #
As a relation, satisfies existence and uniqueness:
A partial function keeps uniqueness but drops existence for some inputs. Its actual domain is . A map that returns either a value or an explicit error can instead be modelled as a total function ; that makes failure data part of the codomain rather than silently leaving the function undefined.
flowchart TD R["relation R ⊆ A × B"] P["partial function: at most one b per a"] F["total function f: A → B: exactly one b per a"] I["injective: each b has at most one preimage"] S["surjective: each b has at least one preimage"] J["bijective: each b has exactly one preimage"] R -->|"single-valued"| P P -->|"defined for every a"| F F --> I F --> S I --> J S --> J
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 | Quantified condition | Diagnostic question |
|---|---|---|
| range | Which outputs actually occur? | |
| injective | Can two inputs collide? | |
| surjective | Is any declared output unreachable? | |
| bijective | injective and surjective | Does every output have exactly one input? |
Images and preimages have different algebra #
For and ,
The notation means preimage even when has no inverse function.
Preimages preserve all Boolean set operations because membership is tested pointwise:
Images always preserve unions, but only satisfy
Equality can fail when is not injective. Let , , and . Then , but both images are , so their image intersection is nonempty. Injectivity removes exactly this collision and restores equality.
Two identities are useful diagnostics:
The first is equality when is injective; the second is equality when every element of lies in the range of .
Composition, identities, and inverses #
Given and ,
Composition is associative because both parenthesizations evaluate to the same value:
The identity is neutral: and . These are type-sensitive equations; and are usually different functions.
A map is a left inverse when . A map is a right inverse when .
- A left inverse forces injectivity: if , apply to both sides.
- Conversely, an injective has a left inverse when is nonempty: use the unique preimage on and a fixed default elsewhere. The exceptional injection has a left inverse only if .
- A right inverse forces surjectivity because 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 is the unique with .
Relation powers and finite-path closure #
Let be an endorelation and . Define
Then means there is an -path of exactly steps from to . The two standard closures are
- is the transitive closure: positive-length reachability.
- is the reflexive-transitive closure: reachability in zero or more steps.
- The reflexive closure alone is merely ; it is generally smaller than .
The word “closure” includes a minimality claim. If is transitive and , induction gives for every , hence . Therefore is not just transitive; it is the least transitive relation containing . The same argument, adding reflexivity, proves the corresponding property of . This path-based definition matches the formal TransGen and ReflTransGen constructions in Mathlib's relation library.
A worked model: users, services, and owners #
Let be users, services, the “may call” relation, and the owner function.
- is the set of services user may call.
- is the caller set for service .
- is the fiber of services owned by .
- is surjective only if every declared user owns at least one service; it is injective only if no user owns two services.
- The relation 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:
- What are the carrier sets, and which universe makes complements meaningful?
- Is each symbol membership, inclusion, a relation, or a function? Are its input/output types written down?
- If it is called a function, is it total? If failures are possible, are they represented as partiality or explicit result values?
- Is “onto” measured against a declared codomain or a retroactively chosen range?
- Does a claimed inverse satisfy the correct left and right identity equations?
- Does a closure include zero steps, positive steps, or only added identity pairs?
- 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
- bookSets, Functions, and Relations: Part I — Naïve Set Theorypdfnotes
- bookSpecial Sections of Mathematics 1: Discrete Mathematics, Section 1, Topic 1pdfnotes
- docsMathlib — Set Operations
- docsMathlib — Basic Function Theory
- docsMathlib — Relations and Relation Closures