
This is part 1 of 11 in the Formal Methods 101 series. Set theory is the base that the rest of the series builds on, so I start with the notation and the operators.
Introduction
Set theory is about sets, which are unordered collections of elements. A set is written with braces:
S = {a, b, c}
Here S is the set and a, b and c are its elements. Order does not matter, so {a, b, c} and {c, b, a} are the same set.
All members of a set must be of the same type. A set can itself contain complex types (including other sets), but the type of the members is then the complex type, and the restriction can be met through the properties of those types.
Set membership
The empty set is the set with no elements. It is written as either of:
∅ = {}
The symbols for membership and for comparing sets are below. In every example, S = {a, b, c}.
| Symbol | Meaning | Example |
|---|---|---|
| ∈ | is an element of | a ∈ S |
| ∉ | is not an element of | d ∉ S |
| ⊂ | proper (strict) subset of | T = {a, b}, so T ⊂ S |
| ⊄ | not a proper subset of | Q = {a, b, c}, so Q ⊄ S (they are equal); W = {b, c, d}, so W ⊄ S |
| ⊆ | subset of (equal sets allowed) | Q ⊆ S and T ⊆ S |
| ⊈ | not a subset of | W = {b, c, d}, so W ⊈ S |
| ⊃ | strict superset of | S ⊃ T |
| ⊅ | not a strict superset of | S ⊅ Q (they are equal) and W ⊅ S |
| ⊇ | superset of (equal sets allowed) | S ⊇ Q and S ⊇ T |
| ⊉ | not a superset of | W ⊉ S |
Set operators
The next four operators combine two sets. I use S = {a, b, c, d} and T = {a, b, e, f} throughout.
- Intersection (∩) keeps the elements that are in both sets. S ∩ T = {a, b}.
- Union (∪) keeps the elements that are in either set. S ∪ T = {a, b, c, d, e, f}.
- Difference (\), also called the relative complement, keeps what is in the first set but not the second. S \ T = {c, d} is the relative complement of T in S, and T \ S = {e, f} is the relative complement of S in T.
- Symmetric difference (Δ) keeps the elements that are in exactly one of the two sets: (S \ T) ∪ (T \ S) = {c, d, e, f}.
Absolute complement
The absolute complement is taken against a universal set. Let S = {a, b, c, d, e, f} be the universe, T = {a, b} and W = {c, d}. Both T and W are subsets of S (T ⊂ S and W ⊂ S). The absolute complement of T ∪ W is the rest of the universe:
S \ (T ∪ W) = {e, f}
Cartesian product
The Cartesian product (×) produces the set of all possible ordered pairs between two sets. With S = {a, b, c} and T = {d, e, f}:
S × T = {(a, d), (a, e), (a, f), (b, d), (b, e), (b, f), (c, d), (c, e), (c, f)}
T × S = {(d, a), (e, a), (f, a), (d, b), (e, b), (f, b), (d, c), (e, c), (f, c)}
The two products hold different pairs, because in an ordered pair the first element comes from the first set.
Power set
The power set P(S) is the set of all subsets of S, including the empty subset. For S = {a, b}:
P(S) = {{a, b}, {a}, {b}, ∅}
Cardinality
The cardinality of a set is the number of members in it, written |S|. For S = {a, b}, |S| = 2.
Insert, delete and lookup
Three operators work on one element at a time. Starting from S = {a, b, c}:
- Insert(Set, element) adds a member to a set. Insert(S, d) returns S = {a, b, c, d}.
- Delete(Set, element) removes a member from a set. Delete(S, a) returns S = {b, c}.
- Lookup(Set, element) determines whether a set contains a specific member. Lookup(S, c) returns TRUE and Lookup(S, d) returns FALSE.
Common sets
- ℕ: the natural numbers (0, 1, 2, 3, 4, 5, …).
- ℤ: the integers (…, -3, -2, -1, 0, 1, 2, 3, …).
- ℚ: the rational numbers.
- ℝ: the real numbers.