# 7.1 Sets, Relations, Functions

Set theory provides a common language for modern mathematics. Many mathematical objects can be described as sets with additional structure, and many mathematical constructions can be described using membership, subsets, relations, and functions. In this section we introduce these basic notions carefully, since later parts of logic and foundations use them constantly.

### Sets and Membership

A set is a collection of objects, called its elements or members. If an object $a$ is an element of a set $A$, we write:
$$
a \in A
$$

If $a$ is not an element of $A$, we write:
$$
a \notin A
$$

The relation $\in$ is called membership. It is the most basic relation in set theory. The expression $a \in A$ is a statement, and it says that the object $a$ belongs to the set $A$.

For example, if:
$$
A = \{1,2,3\}
$$

then:
$$
1 \in A
$$

and:
$$
4 \notin A
$$

A set is determined by its elements. This means that two sets are equal exactly when they have the same elements.

### Definition 7.1 (Equality of Sets)

Two sets $A$ and $B$ are equal if and only if every object belongs to $A$ exactly when it belongs to $B$:
$$
A = B
\quad \text{if and only if} \quad
\forall x\,(x \in A \leftrightarrow x \in B)
$$

This principle is often called extensionality. It says that a set has no hidden structure beyond its elements. If two sets contain the same elements, then they are the same set.

### Example 7.2

The sets:
$$
\{1,2,3\}
$$

and:
$$
\{3,2,1\}
$$

are equal, because they have exactly the same elements.

Also:
$$
\{1,1,2,3\} = \{1,2,3\}
$$

because repetition does not create new elements.

Thus order and repetition do not matter for sets.

### Definition 7.3 (Subset)

Let $A$ and $B$ be sets. We write:
$$
A \subseteq B
$$

if:
$$
\forall x\,(x \in A \to x \in B)
$$

In words, $A \subseteq B$ means that every element of $A$ is also an element of $B$.

If:
$$
A \subseteq B
$$

and:
$$
A \ne B
$$

then $A$ is called a proper subset of $B$.

### Example 7.4

Let:
$$
A = \{1,2\}
$$

and:
$$
B = \{1,2,3\}
$$

Then:
$$
A \subseteq B
$$

because every element of $A$ is also an element of $B$.

But:
$$
B \nsubseteq A
$$

because:
$$
3 \in B
$$

and:
$$
3 \notin A
$$

### Lemma 7.5 (Set Equality by Double Inclusion)

For any sets $A$ and $B$:
$$
A = B
$$

if and only if:
$$
A \subseteq B
\quad \text{and} \quad
B \subseteq A
$$

Proof

Suppose first that $A = B$. Then every element of $A$ is an element of $B$, and every element of $B$ is an element of $A$, because the two sets are the same set. Hence:
$$
A \subseteq B
$$

and:
$$
B \subseteq A
$$

Conversely, suppose that:
$$
A \subseteq B
\quad \text{and} \quad
B \subseteq A
$$

Let $x$ be any object. If $x \in A$, then $x \in B$ because $A \subseteq B$. If $x \in B$, then $x \in A$ because $B \subseteq A$. Therefore:
$$
x \in A \leftrightarrow x \in B
$$

for every object $x$. By extensionality:
$$
A = B
$$

### Definition 7.6 (Empty Set)

The empty set is the set with no elements. It is denoted by:
$$
\varnothing
$$

A set $E$ is empty if:
$$
\forall x\,(x \notin E)
$$

The empty set is the unique set with this property.

### Lemma 7.7 (Uniqueness of the Empty Set)

If $E$ and $F$ are empty sets, then:
$$
E = F
$$

Proof

Assume that $E$ and $F$ are empty sets. To show $E = F$, it is enough to show double inclusion.

First, let $x \in E$. Since $E$ is empty, this is impossible. Therefore the implication:
$$
x \in E \to x \in F
$$

is true for every $x$, and hence:
$$
E \subseteq F
$$

The same argument shows:
$$
F \subseteq E
$$

By Lemma 7.5:
$$
E = F
$$

### Lemma 7.8 (Empty Set Is a Subset of Every Set)

For every set $A$:
$$
\varnothing \subseteq A
$$

Proof

We must show:
$$
\forall x\,(x \in \varnothing \to x \in A)
$$

Let $x$ be arbitrary. The statement $x \in \varnothing$ is false by definition of the empty set. Therefore:
$$
x \in \varnothing \to x \in A
$$

is true. Since $x$ was arbitrary:
$$
\varnothing \subseteq A
$$

### Definition 7.9 (Union)

The union of $A$ and $B$ is the set:
$$
A \cup B = \{x : x \in A \text{ or } x \in B\}
$$

Thus:
$$
x \in A \cup B
\quad \text{if and only if} \quad
x \in A \lor x \in B
$$

The union contains everything that belongs to at least one of the two sets.

### Definition 7.10 (Intersection)

The intersection of $A$ and $B$ is the set:
$$
A \cap B = \{x : x \in A \text{ and } x \in B\}
$$

Thus:
$$
x \in A \cap B
\quad \text{if and only if} \quad
x \in A \land x \in B
$$

The intersection contains exactly the elements common to both sets.

### Definition 7.11 (Difference)

The difference of $A$ and $B$ is the set:
$$
A \setminus B = \{x : x \in A \text{ and } x \notin B\}
$$

Thus:
$$
x \in A \setminus B
\quad \text{if and only if} \quad
x \in A \land x \notin B
$$

The difference consists of the elements of $A$ that are not in $B$.

### Example 7.12

Let:
$$
A = \{1,2,3\}
$$

and:
$$
B = \{3,4,5\}
$$

Then:
$$
A \cup B = \{1,2,3,4,5\}
$$

$$
A \cap B = \{3\}
$$

$$
A \setminus B = \{1,2\}
$$

$$
B \setminus A = \{4,5\}
$$

### Lemma 7.13 (Commutativity of Union and Intersection)

For all sets $A$ and $B$:
$$
A \cup B = B \cup A
$$

and:
$$
A \cap B = B \cap A
$$

Proof

We prove the statement for union first. Let $x$ be arbitrary. Then:
$$
x \in A \cup B
$$

if and only if:
$$
x \in A \lor x \in B
$$

which is equivalent to:
$$
x \in B \lor x \in A
$$

which is equivalent to:
$$
x \in B \cup A
$$

Therefore:
$$
A \cup B = B \cup A
$$

The proof for intersection is similar. We have:
$$
x \in A \cap B
$$

if and only if:
$$
x \in A \land x \in B
$$

which is equivalent to:
$$
x \in B \land x \in A
$$

which is equivalent to:
$$
x \in B \cap A
$$

Therefore:
$$
A \cap B = B \cap A
$$

### Lemma 7.14 (Associativity of Union and Intersection)

For all sets $A$, $B$, and $C$:
$$
(A \cup B) \cup C = A \cup (B \cup C)
$$

and:
$$
(A \cap B) \cap C = A \cap (B \cap C)
$$

Proof

We prove the statement for union. Let $x$ be arbitrary. Then:
$$
x \in (A \cup B) \cup C
$$

if and only if:
$$
x \in A \cup B \lor x \in C
$$

if and only if:
$$
(x \in A \lor x \in B) \lor x \in C
$$

if and only if:
$$
x \in A \lor (x \in B \lor x \in C)
$$

if and only if:
$$
x \in A \lor x \in B \cup C
$$

if and only if:
$$
x \in A \cup (B \cup C)
$$

By extensionality:
$$
(A \cup B) \cup C = A \cup (B \cup C)
$$

The proof for intersection is the same with "or" replaced by "and". For every $x$:
$$
x \in (A \cap B) \cap C
$$

if and only if:
$$
(x \in A \land x \in B) \land x \in C
$$

if and only if:
$$
x \in A \land (x \in B \land x \in C)
$$

if and only if:
$$
x \in A \cap (B \cap C)
$$

Thus:
$$
(A \cap B) \cap C = A \cap (B \cap C)
$$

### Lemma 7.15 (Distributive Laws)

For all sets $A$, $B$, and $C$:
$$
A \cap (B \cup C) = (A \cap B) \cup (A \cap C)
$$

and:
$$
A \cup (B \cap C) = (A \cup B) \cap (A \cup C)
$$

Proof

We prove the first identity. Let $x$ be arbitrary. Then:
$$
x \in A \cap (B \cup C)
$$

if and only if:
$$
x \in A \land x \in B \cup C
$$

if and only if:
$$
x \in A \land (x \in B \lor x \in C)
$$

if and only if:
$$
(x \in A \land x \in B) \lor (x \in A \land x \in C)
$$

if and only if:
$$
x \in A \cap B \lor x \in A \cap C
$$

if and only if:
$$
x \in (A \cap B) \cup (A \cap C)
$$

Therefore:
$$
A \cap (B \cup C) = (A \cap B) \cup (A \cap C)
$$

The second identity is proved similarly. For every $x$:
$$
x \in A \cup (B \cap C)
$$

if and only if:
$$
x \in A \lor (x \in B \land x \in C)
$$

if and only if:
$$
(x \in A \lor x \in B) \land (x \in A \lor x \in C)
$$

if and only if:
$$
x \in (A \cup B) \cap (A \cup C)
$$

Thus:
$$
A \cup (B \cap C) = (A \cup B) \cap (A \cup C)
$$

### Definition 7.16 (Power Set)

The power set of $A$ is:
$$
\mathcal{P}(A) = \{X : X \subseteq A\}
$$

Thus:
$$
X \in \mathcal{P}(A)
\quad \text{if and only if} \quad
X \subseteq A
$$

### Example 7.17

If:
$$
A = \{1,2\}
$$

then:
$$
\mathcal{P}(A)=\{\varnothing,\{1\},\{2\},\{1,2\}\}
$$

The power set contains sets as elements. The elements of $\mathcal{P}(A)$ are subsets of $A$, not usually elements of $A$.

### Definition 7.18 (Ordered Pair)

An ordered pair is written:
$$
(a,b)
$$

and is characterized by the property:
$$
(a,b)=(c,d)
\quad \text{if and only if} \quad
a=c \text{ and } b=d
$$

This property is the essential feature of ordered pairs.

### Definition 7.19 (Cartesian Product)

The Cartesian product of sets $A$ and $B$ is:
$$
A \times B = \{(a,b) : a \in A \text{ and } b \in B\}
$$

Thus an element of $A \times B$ is an ordered pair whose first coordinate belongs to $A$ and whose second coordinate belongs to $B$.

### Example 7.20

If:
$$
A = \{1,2\}
$$

and:
$$
B = \{x,y\}
$$

then:
$$
A \times B = \{(1,x),(1,y),(2,x),(2,y)\}
$$

In general, $A \times B$ and $B \times A$ are different, because ordered pairs remember which coordinate comes first.

### Definition 7.21 (Relation)

A relation from $A$ to $B$ is a subset:
$$
R \subseteq A \times B
$$

If $A=B$, then a relation from $A$ to $A$ is called a relation on $A$.

If:
$$
(a,b) \in R
$$

we often write:
$$
aRb
$$

A relation records which elements are related to which other elements.

### Example 7.22

Let:
$$
A = \{1,2,3\}
$$

Define a relation $R$ on $A$ by:
$$
aRb
\quad \text{if and only if} \quad
a < b
$$

Then:
$$
R = \{(1,2),(1,3),(2,3)\}
$$

The pair $(2,1)$ is not in $R$, because $2 < 1$ is false.

### Properties of Relations

Let $R$ be a relation on a set $A$.

The relation $R$ is reflexive if:
$$
\forall a \in A,\ aRa
$$

It is symmetric if:
$$
\forall a,b \in A,\ aRb \to bRa
$$

It is transitive if:
$$
\forall a,b,c \in A,\ (aRb \land bRc) \to aRc
$$

It is antisymmetric if:
$$
\forall a,b \in A,\ (aRb \land bRa) \to a=b
$$

These properties are used to define important classes of relations, especially equivalence relations and order relations.

### Definition 7.23 (Equivalence Relation)

A relation $R$ on a set $A$ is an equivalence relation if it is reflexive, symmetric, and transitive.

Equivalence relations formalize the idea of treating objects as the same according to some chosen criterion.

### Example 7.24

On the set of integers $\mathbb{Z}$, define:
$$
aRb
\quad \text{if and only if} \quad
a-b \text{ is even}
$$

This relation means that $a$ and $b$ have the same parity.

It is reflexive because:
$$
a-a=0
$$

is even.

It is symmetric because if $a-b$ is even, then:
$$
b-a=-(a-b)
$$

is also even.

It is transitive because if $a-b$ is even and $b-c$ is even, then:
$$
a-c=(a-b)+(b-c)
$$

is even.

Therefore $R$ is an equivalence relation.

### Definition 7.25 (Equivalence Class)

Let $R$ be an equivalence relation on $A$, and let $a \in A$. The equivalence class of $a$ is:
$$
[a] = \{x \in A : xRa\}
$$

The equivalence class of $a$ consists of all elements equivalent to $a`.

### Lemma 7.26 (Equivalence Classes Are Equal or Disjoint)

Let $R$ be an equivalence relation on $A$. For any $a,b \in A$, either:
$$
[a]=[b]
$$

or:
$$
[a] \cap [b]=\varnothing
$$

Proof

Suppose:
$$
[a] \cap [b] \ne \varnothing
$$

Then there exists an element $x$ such that:
$$
x \in [a] \cap [b]
$$

Thus:
$$
xRa
$$

and:
$$
xRb
$$

Since $R$ is symmetric, from $xRa$ we get:
$$
aRx
$$

Since $aRx$ and $xRb$, transitivity gives:
$$
aRb
$$

We show that $[a]\subseteq [b]$. Let $y \in [a]$. Then:
$$
yRa
$$

Since $aRb$, transitivity gives:
$$
yRb
$$

Therefore:
$$
y \in [b]
$$

So:
$$
[a]\subseteq [b]
$$

Similarly, since $aRb$, symmetry gives:
$$
bRa
$$

The same argument shows:
$$
[b]\subseteq [a]
$$

Hence:
$$
[a]=[b]
$$

Therefore, if two equivalence classes meet, they are equal. If they do not meet, they are disjoint.

### Definition 7.27 (Partial Order)

A relation $\leq$ on a set $A$ is a partial order if it is reflexive, antisymmetric, and transitive.

That is, for all $a,b,c \in A$:
$$
a \leq a
$$

$$
(a \leq b \land b \leq a) \to a=b
$$

$$
(a \leq b \land b \leq c) \to a \leq c
$$

A set together with a partial order is called a partially ordered set, or poset.

### Example 7.28

Let $A$ be any set. The subset relation:
$$
\subseteq
$$

is a partial order on $\mathcal{P}(A)$.

It is reflexive because:
$$
X \subseteq X
$$

for every $X \subseteq A$.

It is antisymmetric because:
$$
X \subseteq Y
\quad \text{and} \quad
Y \subseteq X
$$

imply:
$$
X=Y
$$

It is transitive because:
$$
X \subseteq Y
\quad \text{and} \quad
Y \subseteq Z
$$

imply:
$$
X \subseteq Z
$$

### Definition 7.29 (Linear Order)

A partial order $\leq$ on $A$ is a linear order if every two elements are comparable:
$$
\forall a,b \in A,\ a \leq b \lor b \leq a
$$

Linear orders are also called total orders.

### Example 7.30

The usual order on the natural numbers:
$$
\leq
$$

is a linear order, because any two natural numbers can be compared.

The subset order on a power set is usually not a linear order. For example, if:
$$
A=\{1,2\}
$$

then:
$$
\{1\} \nsubseteq \{2\}
$$

and:
$$
\{2\} \nsubseteq \{1\}
$$

Thus $\{1\}$ and $\{2\}$ are not comparable under $\subseteq$.

### Definition 7.31 (Function)

A function from $A$ to $B$ is a relation:
$$
f \subseteq A \times B
$$

such that for every $a \in A$, there exists a unique $b \in B$ with:
$$
(a,b) \in f
$$

We write:
$$
f : A \to B
$$

and we write:
$$
f(a)=b
$$

when:
$$
(a,b) \in f
$$

The set $A$ is called the domain of $f$, and the set $B$ is called the codomain of $f$.

### Example 7.32

Let:
$$
A=\{1,2,3\}
$$

and:
$$
B=\{a,b\}
$$

The relation:
$$
f=\{(1,a),(2,a),(3,b)\}
$$

is a function from $A$ to $B$, because every element of $A$ appears exactly once as a first coordinate.

The relation:
$$
g=\{(1,a),(1,b),(2,a)\}
$$

is not a function from $A$ to $B$, because $1$ has two outputs and $3$ has no output.

### Definition 7.33 (Image and Preimage)

Let:
$$
f : A \to B
$$

If $X \subseteq A$, the image of $X$ under $f$ is:
$$
f[X]=\{f(x):x \in X\}
$$

If $Y \subseteq B$, the preimage of $Y$ under $f$ is:
$$
f^{-1}[Y]=\{x \in A:f(x)\in Y\}
$$

The image moves subsets of the domain forward into the codomain. The preimage moves subsets of the codomain backward into the domain.

### Lemma 7.34 (Basic Image Properties)

Let $f:A\to B$, and let $X,Y\subseteq A$. Then:
$$
f[X\cup Y]=f[X]\cup f[Y]
$$

Also:
$$
f[X\cap Y]\subseteq f[X]\cap f[Y]
$$

Proof

First we prove:
$$
f[X\cup Y]=f[X]\cup f[Y]
$$

Let $b \in f[X\cup Y]$. Then there exists $a \in X\cup Y$ such that:
$$
f(a)=b
$$

Since $a \in X\cup Y$, either $a \in X$ or $a \in Y$. If $a \in X$, then $b \in f[X]$. If $a \in Y$, then $b \in f[Y]$. Hence:
$$
b \in f[X]\cup f[Y]
$$

Thus:
$$
f[X\cup Y]\subseteq f[X]\cup f[Y]
$$

Conversely, let $b \in f[X]\cup f[Y]$. Then either $b \in f[X]$ or $b \in f[Y]$.

If $b \in f[X]$, then there exists $a \in X$ such that $f(a)=b$. Since $X\subseteq X\cup Y$, we have $a \in X\cup Y$, so $b \in f[X\cup Y]$.

If $b \in f[Y]$, the same argument shows $b \in f[X\cup Y]$.

Therefore:
$$
f[X]\cup f[Y]\subseteq f[X\cup Y]
$$

By double inclusion:
$$
f[X\cup Y]=f[X]\cup f[Y]
$$

Now we prove:
$$
f[X\cap Y]\subseteq f[X]\cap f[Y]
$$

Let $b \in f[X\cap Y]$. Then there exists $a \in X\cap Y$ such that:
$$
f(a)=b
$$

Since $a \in X\cap Y$, we have $a \in X$ and $a \in Y$. Therefore $b \in f[X]$ and $b \in f[Y]$. Hence:
$$
b \in f[X]\cap f[Y]
$$

So:
$$
f[X\cap Y]\subseteq f[X]\cap f[Y]
$$

The reverse inclusion does not hold for arbitrary functions, because two different elements may have the same image.

### Definition 7.35 (Injective, Surjective, Bijective)

Let:
$$
f:A\to B
$$

The function $f$ is injective if distinct inputs have distinct outputs:
$$
\forall x,y \in A,\ f(x)=f(y)\to x=y
$$

The function $f$ is surjective if every element of the codomain is hit by the function:
$$
\forall b \in B,\ \exists a \in A,\ f(a)=b
$$

The function $f$ is bijective if it is both injective and surjective.

Injective functions are also called one to one functions. Surjective functions are also called onto functions.

### Example 7.36

The function:
$$
f:\mathbb{Z}\to\mathbb{Z}
$$

defined by:
$$
f(n)=n+1
$$

is bijective.

It is injective because if:
$$
n+1=m+1
$$

then:
$$
n=m
$$

It is surjective because for every $k\in\mathbb{Z}$, the integer:
$$
k-1
$$

satisfies:
$$
f(k-1)=k
$$

The function:
$$
g:\mathbb{N}\to\mathbb{N}
$$

defined by:
$$
g(n)=n+1
$$

is injective but not surjective if $0\in\mathbb{N}$, because no natural number maps to $0$.

### Lemma 7.37 (Inverse of a Bijection)

Let:
$$
f:A\to B
$$

be a bijection. Then there exists a unique function:
$$
f^{-1}:B\to A
$$

such that:
$$
f^{-1}(f(a))=a
$$

for all $a\in A$, and:
$$
f(f^{-1}(b))=b
$$

for all $b\in B$.

Proof

Since $f$ is surjective, for every $b\in B$ there exists at least one $a\in A$ such that:
$$
f(a)=b
$$

Since $f$ is injective, this element $a$ is unique. Indeed, if:
$$
f(a)=b
$$

and:
$$
f(a')=b
$$

then:
$$
f(a)=f(a')
$$

and injectivity gives:
$$
a=a'
$$

Therefore, for each $b\in B$, there is a unique $a\in A$ satisfying $f(a)=b$. Define:
$$
f^{-1}(b)=a
$$

where $a$ is this unique element.

This defines a function:
$$
f^{-1}:B\to A
$$

By construction, if $b=f(a)$, then the unique element mapping to $b$ is $a$, so:
$$
f^{-1}(f(a))=a
$$

Also, for any $b\in B$, the definition of $f^{-1}(b)$ gives:
$$
f(f^{-1}(b))=b
$$

Uniqueness follows because any function satisfying these two identities must send each $b\in B$ to the unique element of $A$ that maps to $b$ under $f$.

### Definition 7.38 (Composition)

Let:
$$
f:A\to B
$$

and:
$$
g:B\to C
$$

The composition of $g$ with $f$ is the function:
$$
g\circ f:A\to C
$$

defined by:
$$
(g\circ f)(a)=g(f(a))
$$

for all $a\in A$.

The function $f$ is applied first, and then $g$ is applied.

### Lemma 7.39 (Associativity of Composition)

Let:
$$
f:A\to B
$$

$$
g:B\to C
$$

$$
h:C\to D
$$

Then:
$$
h\circ(g\circ f)=(h\circ g)\circ f
$$

Proof

To prove equality of functions, it is enough to show that they have the same value at every input.

Let $a\in A$. Then:
$$
(h\circ(g\circ f))(a) =
h((g\circ f)(a))
$$

By definition of composition:
$$
h((g\circ f)(a)) =
h(g(f(a)))
$$

On the other hand:
$$
((h\circ g)\circ f)(a) =
(h\circ g)(f(a))
$$

Again by definition of composition:
$$
(h\circ g)(f(a)) =
h(g(f(a)))
$$

Thus:
$$
(h\circ(g\circ f))(a)=((h\circ g)\circ f)(a)
$$

for every $a\in A$. Therefore:
$$
h\circ(g\circ f)=(h\circ g)\circ f
$$

### Definition 7.40 (Identity Function)

For a set $A$, the identity function on $A$ is:
$$
\mathrm{id}_A:A\to A
$$

defined by:
$$
\mathrm{id}_A(a)=a
$$

for all $a\in A$.

### Lemma 7.41 (Identity Laws)

Let:
$$
f:A\to B
$$

Then:
$$
f\circ \mathrm{id}_A=f
$$

and:
$$
\mathrm{id}_B\circ f=f
$$

Proof

Let $a\in A$. Then:
$$
(f\circ \mathrm{id}_A)(a) =
f(\mathrm{id}_A(a)) =
f(a)
$$

Thus:
$$
f\circ \mathrm{id}_A=f
$$

Also:
$$
(\mathrm{id}_B\circ f)(a) =
\mathrm{id}_B(f(a)) =
f(a)
$$

Thus:
$$
\mathrm{id}_B\circ f=f
$$
