Skip to content
The Lumenary
Grades 6–8Ages 11–14 60 min

Set-builder notation, quantifiers and infinite sets

Describing sets by a rule, saying “for all” and “there exists” precisely, denying them correctly — and proving that no list, however long, contains every number.

Formally verified · 21 LeanComputationally tested · 2 PythonAI-drafted · awaiting human reviewMathematically reviewed: Awaiting human review

Your guides: Chloe, Margaret, Gideon

MotivationH

One late bus

A transport official in Kampala announces: “Every bus on Route 7 arrived on time today.” Nadia waited twenty minutes for hers. What exactly does she need to show to prove the announcement false? Not that no bus was on time — just that at least one bus was late.

Now compare two sentences about a village:

  • “Everyone in the village has someone they can ask for help.”
  • “There is someone in the village whom everyone can ask for help.”

They use the same words, in a different order, and say very different things. The first could be true because neighbours help each other; the second needs one remarkable person.

And here is a bigger question: how many natural numbers are there? “Infinitely many,” you might say. But what does that mean — and how could anyone be sure, without checking them all?

This lesson gives you the precise language for all three questions: sets described by rules, the words for all and there exists, and infinite sets.

IntuitionH

Rules, checks and searches

A set can be given by a list of its elements, like {2,4,6}\{2, 4, 6\}, or by a rule that says which elements belong. Think of a door with a doorkeeper: you name the crowd waiting outside (“the natural numbers”), and the doorkeeper lets in exactly those who pass a test (“is even”). Rules can describe sets far too large to list, such as all the even numbers.

Statements about all members and some members of a set behave like checks and searches:

  • “Every element has the property” is a check: it is true only if every single element passes. One failure makes it false.
  • “Some element has the property” is a search: it is true as soon as one element passes. It is false only if every element fails.

On a small set such as {1,2,3}\{1, 2, 3\}, “every element has property PP” just means “P(1)P(1) and P(2)P(2) and P(3)P(3)”, while “some element has PP” means “P(1)P(1) or P(2)P(2) or P(3)P(3)”. Denying a long “and” gives a long “or” of denials — which is exactly why one late bus is enough.

DefinitionsH

Definitions and notation

Throughout, N={0,1,2,3,… }\N = \{0, 1, 2, 3, \dots\} is the set of natural numbers and Z\Z the set of integers.

Definition— set-builder notation

If AA is a set and P(x)P(x) is a statement about xx, then

{ x∈A:P(x) }\{\, x \in A : P(x) \,\}

is the set of those elements xx of AA for which P(x)P(x) is true. Read it as “the set of xx in AA such that P(x)P(x)”. (Many books write a vertical bar instead of the colon: {x∈A∣P(x)}\{x \in A \mid P(x)\}.)

A second form describes a set by a formula for its elements: { 2k:k∈N }\{\, 2k : k \in \N \,\} is the set of all numbers 2k2k with kk a natural number, that is {0,2,4,6,… }\{0, 2, 4, 6, \dots\}.

The letter in set-builder notation is a placeholder: {x∈N:x<3}\{x \in \N : x < 3\} and {n∈N:n<3}\{n \in \N : n < 3\} are the same set, {0,1,2}\{0, 1, 2\}.

Definition— quantifiers

Let AA be a set and P(x)P(x) a statement about xx.

  • ∀x∈A, P(x)\forall x \in A,\ P(x) means: for every xx in AA, P(x)P(x) is true. The symbol ∀\forall (an upside-down A, for “all”) is the universal quantifier.
  • ∃x∈A, P(x)\exists x \in A,\ P(x) means: there exists at least one xx in AA for which P(x)P(x) is true. The symbol ∃\exists (a backwards E, for “exists”) is the existential quantifier.

“There exists” means at least one, not exactly one: ∃n∈N, n>5\exists n \in \N,\ n > 5 is true, with many witnesses. On its own, a statement such as “x>5x > 5” is neither true nor false — it becomes true or false once we say what xx is, or quantify it.

Definition— finite and infinite sets

A set is finite if its elements can be listed as a1,a2,…,ana_1, a_2, \dots, a_n for some natural number nn (with n=0n = 0 for the empty set). Otherwise it is infinite.

Worked examplesH

Worked examples

Example— Reading set-builder notation

  • {n∈N:n is even}={0,2,4,6,… }={2k:k∈N}\{n \in \N : n \text{ is even}\} = \{0, 2, 4, 6, \dots\} = \{2k : k \in \N\} — two descriptions of the same set.
  • {x∈Z:x2<10}={−3,−2,−1,0,1,2,3}\{x \in \Z : x^2 < 10\} = \{-3, -2, -1, 0, 1, 2, 3\}, because 32=9<103^2 = 9 < 10 but 42=164^2 = 16.
  • {p∈N:p is prime and p<20}={2,3,5,7,11,13,17,19}\{p \in \N : p \text{ is prime and } p < 20\} = \{2, 3, 5, 7, 11, 13, 17, 19\}.
  • {n∈N:n<0}=∅\{n \in \N : n < 0\} = \varnothing: no natural number passes the test.
  • The set in front of the colon matters: {x∈Z:2x=7}=∅\{x \in \Z : 2x = 7\} = \varnothing, but {x∈Q:2x=7}={72}\{x \in \Q : 2x = 7\} = \{\tfrac{7}{2}\}.

Example— Translating sentences

SentenceIn symbols
The square of every integer is at least 00.∀n∈Z, n2≥0\forall n \in \Z,\ n^2 \ge 0
Some prime number is even.∃p∈N, p is prime and p is even\exists p \in \N,\ p \text{ is prime and } p \text{ is even}
No odd number is a multiple of 22.¬ ∃n∈N, n is odd and n is a multiple of 2\neg\, \exists n \in \N,\ n \text{ is odd and } n \text{ is a multiple of } 2
Every integer has an opposite.∀x∈Z, ∃y∈Z, x+y=0\forall x \in \Z,\ \exists y \in \Z,\ x + y = 0
Some integer is the opposite of every integer.∃y∈Z, ∀x∈Z, x+y=0\exists y \in \Z,\ \forall x \in \Z,\ x + y = 0

The symbol ¬\neg means “not”. The last two rows use the same pieces in a different order; the first is true and the second is false.

Example— Negating, step by step

To deny a statement, swap ∀\forall and ∃\exists and deny what follows.

  • “Every bus was on time” becomes “some bus was not on time”.
  • “Some student in the class has a pet snake” becomes “every student in the class does not have a pet snake”, that is, “no student has a pet snake”.
  • “For every natural number nn, n2+n+41n^2 + n + 41 is prime” becomes “there is a natural number nn for which n2+n+41n^2 + n + 41 is not prime”. The denial is the true one: at n=40n = 40 we get 1681=41⋅411681 = 41 \cdot 41.
  • With two quantifiers, apply the rule twice: “∀x ∃y, x+y=0\forall x\ \exists y,\ x + y = 0” becomes “∃x ∀y, x+y≠0\exists x\ \forall y,\ x + y \neq 0” — “some integer has no opposite”.
ProofH

The rules, proved

In this section AA is any set and P(x)P(x) any statement about elements of AA.

Theorem— Theorem 1 (negating quantifiers)

(a) “Not every x∈Ax \in A has PP” is equivalent to “some x∈Ax \in A does not have PP”:

¬ (∀x∈A, P(x))  ⟺  ∃x∈A, ¬P(x).\neg\, \big(\forall x \in A,\ P(x)\big) \iff \exists x \in A,\ \neg P(x).

(b) “No x∈Ax \in A has PP” is equivalent to “every x∈Ax \in A fails PP”:

¬ (∃x∈A, P(x))  ⟺  ∀x∈A, ¬P(x).\neg\, \big(\exists x \in A,\ P(x)\big) \iff \forall x \in A,\ \neg P(x).

Proof

We prove (b) first.

(⇒\Rightarrow) Suppose no element of AA has PP, and take any x∈Ax \in A. If P(x)P(x) were true, xx would be an element with PP, which is impossible. So ¬P(x)\neg P(x).

(⇐\Leftarrow) Suppose every element fails PP. If some xx had PP, it would both have and fail PP. So no element has PP.

Now (a).

(⇐\Leftarrow) If some xx does not have PP, then xx is a counterexample to “every element has PP”, so that statement is false.

(⇒\Rightarrow) Suppose not every element has PP, and assume, for contradiction, that no element fails PP. By (b), applied to the statement “xx fails PP”, every element does not fail PP — that is, every element has PP. That contradicts our assumption. So some element fails PP.

∎

The last step used the principle that “not failing PP” means “having PP”. Every statement is either true or false — the law of excluded middle. Almost all of mathematics uses it freely; the logic viewpoint below explains who doesn't, and why.

Theorem— Theorem 2 (the order of quantifiers matters)

In N\N, the statement ∀x ∃y, y>x\forall x\ \exists y,\ y > x is true, but ∃y ∀x, y>x\exists y\ \forall x,\ y > x is false.

Proof

The first is true. Let xx be any natural number. Then y=x+1y = x + 1 is a natural number with y>xy > x.

The second is false. Suppose some yy satisfied y>xy > x for every natural number xx. Taking x=yx = y gives y>yy > y, which is impossible.

∎

In the first statement yy may depend on xx (we chose y=x+1y = x + 1). In the second, one single yy must work for every xx at once. In general, “∃y ∀x\exists y\ \forall x” implies “∀x ∃y\forall x\ \exists y” — the one yy that works for everybody works for each xx — but not the other way round.

Theorem— Theorem 3 (vacuous truth)

For every property PP, the statement ∀x∈∅, P(x)\forall x \in \varnothing,\ P(x) is true, and ∃x∈∅, P(x)\exists x \in \varnothing,\ P(x) is false.

Proof

By Theorem 1(a), the denial of “∀x∈∅, P(x)\forall x \in \varnothing,\ P(x)” is “there is an x∈∅x \in \varnothing with ¬P(x)\neg P(x)”. That would need an element of the empty set, and there is none. So the denial is false and the statement is true. Similarly, “∃x∈∅, P(x)\exists x \in \varnothing,\ P(x)” needs an element of ∅\varnothing, so it is false.

∎

So “every element of the empty set is even” and “every element of the empty set is odd” are both true. They do not contradict each other, because there is nothing that would have to be even and odd at once.

Theorem— Theorem 4 (ℕ is infinite)

For every finite list a1,…,ana_1, \dots, a_n of natural numbers there is a natural number that is not on the list. So N\N is infinite.

Proof

If the list is empty, 00 is not on it. Otherwise let MM be the largest number on the list. Then M+1M + 1 is a natural number, and for every ii we have ai≤M<M+1a_i \le M < M + 1, so ai≠M+1a_i \neq M + 1. So M+1M + 1 is not on the list.

Therefore no finite list contains all of N\N: by definition, N\N is infinite.

∎

Look at the quantifiers: for every list there exists a missing number, and the missing number depends on the list. The swapped statement — “there is a number missing from every finite list” — is false, because whatever number mm you pick, the one-item list mm contains it.

Theorem— Theorem 5 (the even numbers are infinite)

For every finite list of natural numbers there is an even number that is not on it.

Proof

If the list is empty, 00 is missing. Otherwise let MM be its largest number. Then 2(M+1)2(M + 1) is even, and 2(M+1)≥M+1>M2(M + 1) \ge M + 1 > M, so it is bigger than every number on the list and cannot be on it.

∎

Note

Infinity is not a natural number. If there were a largest natural number — call it ∞\infty — then ∞+1\infty + 1 would be a natural number larger still. Theorem 2 says exactly that there is no largest natural number. “N\N is infinite” is a statement about the set N\N (it cannot be listed), not about some enormous number inside it. Euclid proved the same kind of fact about primes around 300 BCE: given any finite list of primes, multiplying them and adding 11 gives a number whose prime factors are all missing from the list (Elements, Book IX, Proposition 20; see factors and primes).

Formal mathematicsF

Quantifiers in Lean

Lean's logic is made of exactly these quantifiers: ∀ x, P x and ∃ x, P x. A set described by a rule is represented by the rule itself — a property of natural numbers:

EvensLean 4Formally verified
/-- The set `{n ∈ ℕ | n is even}`, written as a predicate: `Evens n` is the statement that `n`
leaves remainder `0` on division by `2`. -/
def Evens (n : Nat) : Prop := n % 2 = 0
Axioms used: No axiomsformal/Lumenary/Quantifiers.leanTry it in the Lean web editor
evens_examplesLean 4Proved here from basic factsFormally verified
/-- Membership is checked by evaluating the property: `10` is in the set, `7` is not. -/
theorem evens_examples : Evens 10 ∧ ¬ Evens 7 := by
  unfold Evens
  decide
Axioms used: No axiomsformal/Lumenary/Quantifiers.leanTry it in the Lean web editor

Two set-builder descriptions give the same set when membership in one is equivalent to membership in the other (↔, “if and only if”):

evens_iff_doubleLean 4Proved here from basic factsFormally verified
/-- Two set-builder descriptions of the same set: `{n ∈ ℕ | n is even} = {2k | k ∈ ℕ}`.
A number is even exactly when it is `2k` for some natural number `k`. -/
theorem evens_iff_double (n : Nat) : Evens n ↔ ∃ k, n = 2 * k := by
  unfold Evens
  constructor
  · intro h
    exact ⟨n / 2, by omega⟩
  · intro ⟨k, hk⟩
    omega
Axioms used: propext, Quot.soundformal/Lumenary/Quantifiers.leanTry it in the Lean web editor
small_squaresLean 4Proved here from basic factsFormally verified
/-- `{n ∈ ℕ | n² < 20} = {0, 1, 2, 3, 4}`: a natural number has square below `20` exactly when it
is at most `4`. If `n ≥ 5` then `n · n ≥ 5 · 5 = 25`; if `n ≤ 4` then `n · n ≤ 4 · 4 = 16`. -/
theorem small_squares (n : Nat) : n * n < 20 ↔ n ≤ 4 := by
  constructor
  · intro h
    apply Nat.le_of_not_lt
    intro h5
    have : 5 * 5 ≤ n * n := Nat.mul_le_mul h5 h5
    omega
  · intro h
    have : n * n ≤ 4 * 4 := Nat.mul_le_mul h h
    omega
Axioms used: propext, Quot.soundformal/Lumenary/Quantifiers.leanTry it in the Lean web editor

The negation rules of Theorem 1, proved step by step. Notice Classical.byContradiction in the first proof: that is the law of excluded middle at work, exactly where the human proof used it.

not_forall_iffLean 4Proved here from basic factsFormally verified
/-- **"Not every `x` has `P`" means "some `x` does not have `P`".**
(←) If `x` fails `P`, then not every element has `P`.
(→) By contradiction: if *no* `x` failed `P`, then every `x` would have `P` — again by
contradiction — which we were told is false. This direction uses classical logic
(`Classical.byContradiction`, a form of the law of excluded middle). -/
theorem not_forall_iff {α : Type} (P : α → Prop) : (¬ ∀ x, P x) ↔ ∃ x, ¬ P x := by
  constructor
  · intro h
    apply Classical.byContradiction
    intro hnone
    apply h
    intro x
    apply Classical.byContradiction
    intro hx
    exact hnone ⟨x, hx⟩
  · intro ⟨x, hx⟩ hall
    exact hx (hall x)
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/Quantifiers.leanTry it in the Lean web editor
not_exists_iffLean 4Proved here from basic factsFormally verified
/-- **"No `x` has `P`" means "every `x` fails `P`".** Both directions are direct: an `x` with
`P x` would contradict "every `x` fails `P`", and vice versa. (Core Lean: `not_exists`.) -/
theorem not_exists_iff {α : Type} (P : α → Prop) : (¬ ∃ x, P x) ↔ ∀ x, ¬ P x := by
  constructor
  · intro h x hx
    exact h ⟨x, hx⟩
  · intro h ⟨x, hx⟩
    exact h x hx
Axioms used: No axiomsformal/Lumenary/Quantifiers.leanTry it in the Lean web editor

Core Lean also has the first rule in its library; this version just applies it:

not_forall_libraryLean 4Applies a library theoremFormally verified
/-- The same law, applying core Lean's `Classical.not_forall`. -/
theorem not_forall_library {α : Type} (P : α → Prop) : (¬ ∀ x, P x) ↔ ∃ x, ¬ P x :=
  Classical.not_forall
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/Quantifiers.leanTry it in the Lean web editor

Applying the two rules one after another negates a statement with two quantifiers:

not_forall_existsLean 4Proved here from basic factsFormally verified
/-- Negating a statement with two quantifiers: apply the two rules one after the other.
`¬ (∀ x, ∃ y, R x y)` becomes `∃ x, ¬ ∃ y, R x y`, which becomes `∃ x, ∀ y, ¬ R x y`. -/
theorem not_forall_exists {α β : Type} (R : α → β → Prop) :
    (¬ ∀ x, ∃ y, R x y) ↔ ∃ x, ∀ y, ¬ R x y := by
  rw [not_forall_iff]
  constructor
  · intro ⟨x, hx⟩
    exact ⟨x, (not_exists_iff (R x)).mp hx⟩
  · intro ⟨x, hx⟩
    exact ⟨x, (not_exists_iff (R x)).mpr hx⟩
Axioms used: propext, Classical.choice, Quot.soundformal/Lumenary/Quantifiers.leanTry it in the Lean web editor
forall_exists_gtLean 4Proved here from basic factsFormally verified
/-- In ℕ, **for every `x` there is a `y` with `y > x`**: given `x`, take `y = x + 1`.
Notice that `y` is allowed to depend on `x`. -/
theorem forall_exists_gt : ∀ x : Nat, ∃ y, y > x :=
  fun x => ⟨x + 1, Nat.lt_succ_self x⟩
Axioms used: No axiomsformal/Lumenary/Quantifiers.leanTry it in the Lean web editor
not_exists_forall_gtLean 4Proved here from basic factsFormally verified
/-- In ℕ, **there is no `y` with `y > x` for every `x`**: such a `y` would have to be bigger
than itself. Same symbols as `forall_exists_gt`, different order — and the opposite answer. -/
theorem not_exists_forall_gt : ¬ ∃ y : Nat, ∀ x, y > x := by
  intro ⟨y, hy⟩
  exact Nat.lt_irrefl y (hy y)
Axioms used: No axiomsformal/Lumenary/Quantifiers.leanTry it in the Lean web editor
int_forall_existsLean 4Proved here from basic factsFormally verified
/-- In ℤ, every number has a partner that adds to zero: `∀ x, ∃ y, x + y = 0` (take `y = -x`). -/
theorem int_forall_exists : ∀ x : Int, ∃ y, x + y = 0 :=
  fun x => ⟨-x, by omega⟩
Axioms used: propext, Quot.soundformal/Lumenary/Quantifiers.leanTry it in the Lean web editor
int_not_exists_forallLean 4Proved here from basic factsFormally verified
/-- But no single number is a partner for *every* `x`: `¬ ∃ y, ∀ x, x + y = 0`. Taking `x = 0`
forces `y = 0`, and then `x = 1` gives `1 = 0`. -/
theorem int_not_exists_forall : ¬ ∃ y : Int, ∀ x : Int, x + y = 0 := by
  intro ⟨y, hy⟩
  have h0 := hy 0
  have h1 := hy 1
  omega
Axioms used: propext, Quot.soundformal/Lumenary/Quantifiers.leanTry it in the Lean web editor

Vacuous truth, as Lean sees it: to prove something about every element of the empty list, you are handed an element of the empty list — and that is already impossible.

vacuous_empty_listLean 4Proved here from basic factsFormally verified
/-- **Vacuous truth.** "Every element of the empty list is even" is true — and so is "every
element of the empty list is odd". There is no element to check, so there is no counterexample. -/
theorem vacuous_empty_list :
    (∀ n, n ∈ ([] : List Nat) → Evens n) ∧ (∀ n, n ∈ ([] : List Nat) → ¬ Evens n) := by
  constructor
  · intro n h
    exact absurd h (List.not_mem_nil)
  · intro n h
    exact absurd h (List.not_mem_nil)
Axioms used: No axiomsformal/Lumenary/Quantifiers.leanTry it in the Lean web editor
vacuous_below_zeroLean 4Proved here from basic factsFormally verified
/-- "Every natural number less than `0` equals `42`" is (vacuously) true: no natural number is
less than `0`. -/
theorem vacuous_below_zero : ∀ n : Nat, n < 0 → n = 42 := by
  intro n h
  omega
Axioms used: propext, Quot.soundformal/Lumenary/Quantifiers.leanTry it in the Lean web editor

Checking cases is not the same as proving “for all”. On a finite range, Lean's decide simply runs through the cases. For all of N\N we need an argument that reduces infinitely many cases to finitely many — here, the four possible remainders on division by 44:

squares_mod_four_checkLean 4Proved here from basic factsFormally verified
/-- For each of the `50` numbers `n < 50`, `n²` leaves remainder `0` or `1` on division by `4`.
`decide` proves this by checking all fifty cases — a proof about these fifty numbers only. -/
theorem squares_mod_four_check : ∀ n, n < 50 → n * n % 4 = 0 ∨ n * n % 4 = 1 := by
  decide
Axioms used: No axiomsformal/Lumenary/Quantifiers.leanTry it in the Lean web editor
squares_mod_fourLean 4Proved here from basic factsFormally verified
/-- The same fact for **every** natural number. No computer can check infinitely many cases, but
an argument can: the remainder of `n · n` on division by `4` depends only on the remainder `r` of
`n` (`Nat.mul_mod`), and there are only four possible remainders `r = 0, 1, 2, 3`. -/
theorem squares_mod_four (n : Nat) : n * n % 4 = 0 ∨ n * n % 4 = 1 := by
  have four_cases : ∀ r, r < 4 → r * r % 4 = 0 ∨ r * r % 4 = 1 := by decide
  rw [Nat.mul_mod]
  exact four_cases (n % 4) (Nat.mod_lt n (by decide))
Axioms used: propextformal/Lumenary/Quantifiers.leanTry it in the Lean web editor

Finally, Theorems 4 and 5. A finite list's largest number is computed by a small program, listMax; a proof by induction on the list shows that every entry is at most listMax; and then listMax + 1 cannot be on the list.

listMaxLean 4Formally verified
/-- The largest number in a finite list of natural numbers (`0` for the empty list). -/
def listMax : List Nat → Nat
  | [] => 0
  | x :: xs => max x (listMax xs)
Axioms used: No axiomsformal/Lumenary/Quantifiers.leanTry it in the Lean web editor
le_listMaxLean 4Proved here from basic factsFormally verified
/-- Every number in a list is at most `listMax` of the list. Proof by induction on the list:
the head is at most `max head rest`, and every element of the tail is at most `listMax tail`,
which is at most `max head (listMax tail)`. -/
theorem le_listMax : ∀ (l : List Nat) (x : Nat), x ∈ l → x ≤ listMax l
  | [], x, h => absurd h List.not_mem_nil
  | y :: ys, x, h => by
    cases h with
    | head => exact Nat.le_max_left _ _
    | tail _ h' => exact Nat.le_trans (le_listMax ys x h') (Nat.le_max_right y (listMax ys))
Axioms used: propextformal/Lumenary/Quantifiers.leanTry it in the Lean web editor
nat_infiniteLean 4Proved here from basic factsFormally verified
/-- **ℕ is infinite**: for every finite list of natural numbers, one more than its largest
number is missing from it. (If `listMax l + 1` were in the list, it would be at most
`listMax l`.) -/
theorem nat_infinite (l : List Nat) : listMax l + 1 ∉ l := by
  intro h
  have := le_listMax l (listMax l + 1) h
  omega
Axioms used: propext, Quot.soundformal/Lumenary/Quantifiers.leanTry it in the Lean web editor
no_list_of_all_natsLean 4Proved here from basic factsFormally verified
/-- So no finite list contains every natural number. -/
theorem no_list_of_all_nats : ¬ ∃ l : List Nat, ∀ n, n ∈ l := by
  intro ⟨l, hl⟩
  exact nat_infinite l (hl (listMax l + 1))
Axioms used: propext, Quot.soundformal/Lumenary/Quantifiers.leanTry it in the Lean web editor
evens_infiniteLean 4Proved here from basic factsFormally verified
/-- **The even numbers are infinite too**: for every finite list, `2 · (largest + 1)` is even,
bigger than every number in the list, and therefore missing from it. -/
theorem evens_infinite (l : List Nat) : ∃ n, Evens n ∧ n ∉ l := by
  refine ⟨2 * (listMax l + 1), ?_, ?_⟩
  · unfold Evens
    omega
  · intro h
    have := le_listMax l (2 * (listMax l + 1)) h
    omega
Axioms used: propext, Quot.soundformal/Lumenary/Quantifiers.leanTry it in the Lean web editor

What is formalised: every theorem of the proof section, using core Lean only (for an empty list, Lean's listMax is 00, so the missing number it produces is 11 rather than 00 — either works). Mathlib, Lean's large mathematical library, has the general notions: Set.Infinite, Set.Finite, and the theorem Set.infinite_of_not_bddAbove, which says that a set of natural numbers with no upper bound is infinite.

ComputationC

all(), any() and lists that miss numbers

For all and there exists, as all() and any()PythonExact computationExhaustive finite checkComputationally tested
"""For all and there exists, on finite sets: Python's all() and any().

On a finite set, "for every x, P(x)" is a long AND and "there is an x with
P(x)" is a long OR - exactly Python's all() and any(). The experiment checks
the negation rules for every predicate on every universe with at most five
elements, shows vacuous truth (all([]) is True), and shows that the order of
mixed quantifiers changes the answer. It also shows why a finite check cannot
settle a statement about all of N: cutting N off at 9 makes "for every x
there is a bigger y" false. Exhaustive-finite evidence only.
"""

import itertools

def forall(universe, pred):
    """'For every x in universe, pred(x)' on a finite universe."""
    return all(pred(x) for x in universe)

def exists(universe, pred):
    """'There is an x in universe with pred(x)' on a finite universe."""
    return any(pred(x) for x in universe)

def forall_exists(xs, ys, rel):
    """'For every x there is a y with rel(x, y)' (y may depend on x)."""
    return all(any(rel(x, y) for y in ys) for x in xs)

def exists_forall(xs, ys, rel):
    """'There is a y with rel(x, y) for every x' (one y for all x)."""
    return any(all(rel(x, y) for x in xs) for y in ys)

def predicates(universe):
    """Every predicate on a finite universe, as a set of the true elements."""
    items = list(universe)
    for r in range(len(items) + 1):
        for chosen in itertools.combinations(items, r):
            yield frozenset(chosen)

def negation_rules_hold(universe):
    """Count predicates for which both negation rules agree, and the total."""
    agree = total = 0
    for truth in predicates(universe):
        p = truth.__contains__
        rule1 = (not forall(universe, p)) == exists(universe, lambda x: not p(x))
        rule2 = (not exists(universe, p)) == forall(universe, lambda x: not p(x))
        agree += rule1 and rule2
        total += 1
    return agree, total

def main():
    nums = range(100)
    print("1. Quantifiers over the finite set {0, ..., 99}")
    print("   every n has n*n % 4 in {0, 1}:   ",
          forall(nums, lambda n: n * n % 4 in (0, 1)))
    print("   some n has n*n % 4 == 2:         ",
          exists(nums, lambda n: n * n % 4 == 2))
    print()
    print("2. Negation rules, every predicate on universes of size 0..5")
    agree = total = 0
    for size in range(6):
        a, t = negation_rules_hold(range(size))
        agree, total = agree + a, total + t
    print(f"   not-all == some-not and not-some == all-not: {agree}/{total}")
    print()
    print("3. Vacuous truth: statements about the elements of the empty set")
    print("   all([]) =", all([]), "  any([]) =", any([]))
    print("   every element of {} is even:", forall([], lambda n: n % 2 == 0))
    print("   every element of {} is odd: ", forall([], lambda n: n % 2 == 1))
    print()
    print("4. The order of quantifiers")
    zs = range(-5, 6)
    add0 = lambda x, y: x + y == 0  # noqa: E731
    print("   on {-5..5}, for all x some y has x + y = 0:",
          forall_exists(zs, zs, add0))
    print("   on {-5..5}, some y has x + y = 0 for all x:",
          exists_forall(zs, zs, add0))
    cut = range(10)
    bigger = lambda x, y: y > x  # noqa: E731
    print("   on {0..9}, for all x some y > x:", forall_exists(cut, cut, bigger))
    print("   on {0..9}, some y >= every x:   ",
          exists_forall(cut, cut, lambda x, y: y >= x))
    print("   In N the answers flip: every x has a bigger y, and no y is >=")
    print("   every x. A finite piece of N has a largest element; N has not.")
    print()
    print("Epistemic status: exact, exhaustive checks on finite sets. Part 2")
    print("checks the negation rules for the 63 predicates on universes of up")
    print("to 5 elements; the lesson proves them for every set. Part 4 shows a")
    print("finite check of a statement about all of N can give the wrong answer.")

if __name__ == "__main__":
    main()
Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.

Part 4 holds a warning. On the finite set {0,…,9}\{0, \dots, 9\}, “for every xx there is a bigger yy” is false (nothing is bigger than 99), and “some yy is at least every xx” is true (y=9y = 9). In N\N both answers flip. So no finite check can settle a statement about all natural numbers — for that we need a proof like Theorem 2.

Set-builder notation in Python, and lists that miss numbersPythonExact computationComputationally tested
"""Set-builder notation as Python set comprehensions, and why N is infinite.

{n in A : P(n)} becomes {n for n in A if P(n)}, and {2k : k in A} becomes
{2 * k for k in A}. The experiment checks that two descriptions of the even
numbers agree on a finite range, lists a few small sets, then plays the
"infinite set" game: whatever finite list of natural numbers you give, one
more than its largest element is missing, and twice that is a missing even
number. Exact computation on finite ranges and sample lists; the lesson
proves the general statements.
"""

def evens_by_property(limit):
    """{n in {0..limit-1} : n is even}."""
    return {n for n in range(limit) if n % 2 == 0}

def evens_by_formula(limit):
    """{2k : k in N, 2k < limit}."""
    return {2 * k for k in range((limit + 1) // 2)}

def small_squares():
    """{x in Z : x * x < 10}. Searching -10..10 is enough, because
    |x| >= 4 gives x * x >= 16."""
    return sorted(x for x in range(-10, 11) if x * x < 10)

def primes_below(n):
    """{p in N : p is prime and p < n}."""
    return [p for p in range(2, n) if all(p % d for d in range(2, p))]

def missing_natural(numbers):
    """A natural number not in the finite list: one more than the largest."""
    return max(numbers, default=-1) + 1

def missing_even(numbers):
    """An even natural number not in the finite list."""
    return 2 * (max(numbers, default=-1) + 1)

def main():
    print("1. Set-builder notation as set comprehensions")
    same = evens_by_property(1000) == evens_by_formula(1000)
    print("   {n < 1000 : n even} == {2k : 2k < 1000}:", same)
    print("   {x in Z : x*x < 10} =", small_squares())
    print("   {p prime : p < 30} =", primes_below(30))
    print("   {n in 0..99 : n < 0} =", {n for n in range(100) if n < 0},
          "(the empty set)")
    print()
    print("2. No finite list contains every natural number")
    lists = [[], [0, 1, 2, 3], [7, 3, 12], [2, 4, 6, 8, 10, 12],
             [5, 10 ** 6, 41]]
    for nums in lists:
        n, e = missing_natural(nums), missing_even(nums)
        assert n not in nums and e not in nums and e % 2 == 0
        print(f"   {str(nums):<22} misses {n:>7} and the even number {e:>7}")
    print("   Whatever finite list you write, the recipe finds a number it")
    print("   misses - so N and the even numbers are infinite.")
    print()
    print("Epistemic status: exact computations. Part 1 compares two")
    print("descriptions only on 0..999, and part 2 tries five lists; neither")
    print("is a proof about all numbers. The lesson proves that largest + 1")
    print("is missing from every finite list (and Lean checks the proof).")

if __name__ == "__main__":
    main()
Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.
Explore

Explore: negation as a truth table

Take the three-element set {1,2,3}\{1, 2, 3\} and a property PP. Let pp, qq and rr stand for the statements P(1)P(1), P(2)P(2) and P(3)P(3). Then “every element has PP” is p∧q∧rp \wedge q \wedge r (“and”), and “some element fails PP” is ¬p∨¬q∨¬r\neg p \vee \neg q \vee \neg r (“or”). Theorem 1(a) predicts that denying the first gives the second. The truth table checks all eight possible properties at once:

Not every one has P = some one fails P

p, q, r stand for P(1), P(2), P(3).

Read as: ¬((p ∧ q) ∧ r)

Read as: (¬p ∨ ¬q) ∨ ¬r

Use the symbols ¬ ∧ ∨ → ↔, the words not, and, or, implies, iff, or ASCII ~ & | -> <->. Variables are single lowercase letters. ¬ binds tightest, then ∧, ∨, →, ↔; p → q → r means p → (q → r).

Try:

Contingent — true in 7 of 8 rows, false in the others.

Equivalent — the two formulas agree in all 8 rows.

pqrp ∧ q(p ∧ q) ∧ r¬((p ∧ q) ∧ r)(¬p ∨ ¬q) ∨ ¬rRows where they differ
TTTTTFF
TTFTFTT
TFTFFTT
TFFFFTT
FTTFFTT
FTFFFTT
FFTFFTT
FFFFFTT

8 rows: one for every way of making the 3 variables true or false.

And Theorem 1(b): “no element has PP” against “every element fails PP”.

No one has P = every one fails P

p, q, r stand for P(1), P(2), P(3).

Read as: ¬((p ∨ q) ∨ r)

Read as: (¬p ∧ ¬q) ∧ ¬r

Use the symbols ¬ ∧ ∨ → ↔, the words not, and, or, implies, iff, or ASCII ~ & | -> <->. Variables are single lowercase letters. ¬ binds tightest, then ∧, ∨, →, ↔; p → q → r means p → (q → r).

Try:

Contingent — true in 1 of 8 rows, false in the others.

Equivalent — the two formulas agree in all 8 rows.

pqrp ∨ q(p ∨ q) ∨ r¬((p ∨ q) ∨ r)(¬p ∧ ¬q) ∧ ¬rRows where they differ
TTTTTFF
TTFTTFF
TFTTTFF
TFFTTFF
FTTTTFF
FTFTTFF
FFTFTFF
FFFFFTT

8 rows: one for every way of making the 3 variables true or false.

Things to try: the two columns agree in every row, so each pair is a logical equivalence (these are De Morgan's laws). Now replace not p or not q or not r by the tempting wrong denial not p and not q and not r (“every element fails”) in the first table, and find a row where it disagrees with not (p and q and r).

Exercises

Exercises

1. Set-builder notation, quantifiers and infinite sets

Warm-upHuman

List the elements of {n∈N:n2<30}\{n \in \N : n^2 < 30\}. (Remember that N={0,1,2,… }\N = \{0, 1, 2, \dots\}.)

Write elements separated by commas. Order and repeats don’t matter — just like in a set!

2. Set-builder notation, quantifiers and infinite sets

Warm-upHuman

Which sentence is the denial (negation) of “Every train from Mombasa arrived on time today”?

Choose one

3. Set-builder notation, quantifiers and infinite sets

CoreHuman

Is the statement “Every element of the empty set is greater than 100100” true or false?

Choose one

4. Set-builder notation, quantifiers and infinite sets

CoreHuman

Which of these statements are true? Choose all that apply.

Choose all that apply

5. Set-builder notation, quantifiers and infinite sets

CoreHuman

Let SS be the set of students in a class, and let M(t,s)M(t, s) mean “tt has met ss”. Which statement says “There is a student whom every student in the class has met”?

Choose one

6. Set-builder notation, quantifiers and infinite sets

CoreHuman

Which sentence is the denial of “Every country has a city with more than a million people”?

Choose one

7. Set-builder notation, quantifiers and infinite sets

CoreHuman

How many elements does the set {x∈Z:−3≤2x+1≤7}\{x \in \Z : -3 \le 2x + 1 \le 7\} have?

8. Set-builder notation, quantifiers and infinite sets

StretchHuman

Lena wants to prove that the even numbers are infinite. She is given a non-empty finite list of even natural numbers whose largest entry is MM. Which number is certainly an even number missing from the list, whatever the list is?

Choose one

9. Set-builder notation, quantifiers and infinite sets

CoreFormal

Which Lean statement says that there is no largest natural number?

Choose one

10. Set-builder notation, quantifiers and infinite sets

StretchComputational

Pierre de Fermat believed (around 1640) that every number 22k+12^{2^k} + 1 is prime. Write first_counterexample(prop, limit), which returns the first k in range(limit) for which prop(k) is False, or None if there is none. Then use it with the is_prime function provided to test Fermat's claim for k=0,1,…,5k = 0, 1, \dots, 5.

Runs entirely in your browser, in an isolated worker. Nothing is sent to a server.
Common misconceptionsH

Common misconceptions

Misconception

“Swapping ∀\forall and ∃\exists doesn't change the meaning.” “Everyone has someone they can ask for help” (∀x ∃y\forall x\ \exists y) is different from “someone can be asked for help by everyone” (∃y ∀x\exists y\ \forall x). In N\N, ∀x ∃y, y>x\forall x\ \exists y,\ y > x is true and ∃y ∀x, y>x\exists y\ \forall x,\ y > x is false. Read quantifiers from left to right, and remember that each one may depend on those before it.

Misconception

“The denial of ‘every xx has PP’ is ‘no xx has PP’.” To deny “every bus was on time” you need only one late bus. The correct denial is “some xx does not have PP”. “No bus was on time” is a much stronger claim.

Misconception

“A statement about every element of the empty set must be false — or meaningless.” It is true, because its denial would need an element of the empty set. This is not a trick: it is what makes rules like “a set with no elements is a subset of every set” work, and Python agrees — all([]) is True.

Misconception

“Infinity is a very large natural number.” Every natural number nn is followed by a larger one, n+1n + 1, so no natural number is “the last one”. “Infinite” describes the whole set N\N: it cannot be written as a finite list.

Misconception

“Checking a million cases proves a ‘for all’ statement.” It can make the statement very believable, and one failure would refute it — but n2+n+41n^2 + n + 41 is prime for n=0,1,…,39n = 0, 1, \dots, 39 and fails at n=40n = 40. A statement about infinitely many numbers needs a proof.

ViewpointsH

Other ways of seeing

Logic— Games, and the logicians who disagree

Logicians make the sceptic game from the intuition section precise: a quantified statement is true exactly when its defender has a winning strategy in a game where ∀\forall is the sceptic's move and ∃\exists is the defender's. A strategy for “∀x ∃y\forall x\ \exists y” is a rule for choosing yy from xx, such as y=x+1y = x + 1. This game semantics was developed by Jaakko Hintikka and others in the 20th century.

Not every mathematician accepts every rule in this lesson. Constructive mathematicians, following L. E. J. Brouwer, accept “∃x, P(x)\exists x,\ P(x)” only when one can actually produce such an xx. They accept three of the four directions in Theorem 1, but not “not every xx has PP, therefore some xx fails PP” for infinite sets — knowing that a universal statement is false does not, by itself, hand you a counterexample. Lean can work both ways, and marks the classical step explicitly (see the formal section). Quantifiers become part of a fully formal language in first-order logic.

Set theory— Why the “∈ A” matters

Set-builder notation always says where to look: {x∈A:P(x)}\{x \in A : P(x)\}, not just {x:P(x)}\{x : P(x)\}. This is not fussiness. In 1901 Bertrand Russell showed that the unrestricted rule “the set of all xx with P(x)P(x)” leads to a contradiction when P(x)P(x) is “xx is not a member of itself” (see Russell's paradox). Modern set theory allows only the restricted form — picking out elements of a set you already have — and that rule, called the axiom of separation, is safe.

Analysis— Every ε has an N

Calculus is written in quantifiers. “The numbers 1,12,13,…1, \tfrac{1}{2}, \tfrac{1}{3}, \dots get as close to 00 as you like” means: for every distance ε>0\varepsilon > 0 there is a position NN such that every term after the NN-th is within ε\varepsilon of 00,

∀ε>0  ∃N∈N  ∀n>N,  1n<ε.\forall \varepsilon > 0\ \ \exists N \in \N\ \ \forall n > N,\ \ \tfrac{1}{n} < \varepsilon.

Here NN depends on ε\varepsilon: for ε=0.01\varepsilon = 0.01 we can take N=100N = 100. Swapping the first two quantifiers would claim one NN that works for every ε\varepsilon — false, since no term 1n\tfrac{1}{n} is below every positive ε\varepsilon. The difference between “continuous” and “uniformly continuous”, later on, is exactly such a swap (see sequences and series).

Computation— Searching can refute, but never confirm

A computer can search for a counterexample to a “for all” statement, and if it finds one, the statement is refuted for good. If the search finds nothing, the statement might still fail further on. Goldbach's conjecture (1742) — every even number greater than 22 is the sum of two primes — has been checked by computer for every even number up to 4×10184 \times 10^{18} (Oliveira e Silva, Herzog and Pardi, 2014), and it is still unproved. On a finite set, though, checking every case is a proof, which is how Lean's decide works (see programming for mathematics).

History & global context

History and global context

  • Aristotle (Athens, 4th century BCE) studied the four forms “every A is B”, “no A is B”, “some A is B” and “some A is not B”, and how they deny one another — later drawn as the square of opposition.
  • Euclid's Elements (around 300 BCE) states that “prime numbers are more than any assigned multitude of prime numbers” (Book IX, Proposition 20) — a statement about every finite list, proved exactly in the style of Theorem 4.
  • Jaina mathematicians in ancient India classified numbers as enumerable, innumerable and infinite, and distinguished several kinds of infinity (described in G. G. Joseph's The Crest of the Peacock).
  • Galileo Galilei, in Two New Sciences (1638), noticed that the perfect squares can be paired off with all the natural numbers, although most numbers are not squares — a puzzle about infinite sets that Georg Cantor resolved in the 1870s–1890s by taking such pairings as the definition of “same size” (see countability).
  • Quantifiers as symbols came with modern logic: Gottlob Frege's Begriffsschrift (1879) and, independently, Charles Sanders Peirce (1885). The symbol ∃\exists was introduced by Giuseppe Peano (1897), and ∀\forall by Gerhard Gentzen (1935).
Further reading

Further reading

  • Daniel J. Velleman, How to Prove It: A Structured Approach (3rd edition, 2019), chapter 2 on quantificational logic and set-builder notation.
  • Richard Hammack, Book of Proof (3rd edition, 2018), chapters 1 and 2 on sets and logic — free to read online.
  • Eli Maor, To Infinity and Beyond: A Cultural History of the Infinite (1987).
  • Hans Magnus Enzensberger, The Number Devil (1997) — a story for young readers, with infinitely many numbers in it.