TExcelTAD
Home › Discrete Mathematics › Predicates and Quantifiers

Predicates and Quantifiers

Propositional logic cannot say "every user has a password" or "some number is prime". For that we need predicates (statements with variables) and quantifiers, which say for how many values a predicate holds. This is predicate logic, the language in which almost all of mathematics, and database queries, are written.

The two quantifiers

Symbol Read Meaning Python over a finite domain
∀x∈D. P(x)\forall x \in D.\ P(x) "for all xx in DD" P(x)P(x) is true for every xx in DD all(P(x) for x in D)
∃x∈D. P(x)\exists x \in D.\ P(x) "there exists xx in DD" P(x)P(x) is true for at least one xx any(P(x) for x in D)

The domain of discourse DD matters. "∃x. x2=2\exists x.\ x^2 = 2" is true over the real numbers and false over the rationals.

python · try it yourself
D = range(1, 21)
is_prime = lambda n: n > 1 and all(n % d for d in range(2, n))
print("forall x in 1..20: x^2 >= x  ->", all(x * x >= x for x in D))
print("exists x in 1..20: x prime and x > 15 ->", any(is_prime(x) and x > 15 for x in D))
print("witnesses:", [x for x in D if is_prime(x) and x > 15])
print("all over an empty domain:", all(x > 100 for x in []), " any:", any(x > 100 for x in []))
forall x in 1..20: x^2 >= x  -> True
exists x in 1..20: x prime and x > 15 -> True
witnesses: [17, 19]
all over an empty domain: True  any: False

The last line is a useful convention: "for all" over an empty set is vacuously true, "there exists" over an empty set is false.

Order of quantifiers matters

Let L(p,q)L(p, q) mean "person pp likes song qq".

  • ∀p. ∃q. L(p,q)\forall p.\ \exists q.\ L(p, q): everyone likes some song (maybe different songs).
  • ∃q. ∀p. L(p,q)\exists q.\ \forall p.\ L(p, q): there is one song that everyone likes.

The second implies the first, but not the other way round. In calculus the same issue separates continuity from uniform continuity.

python · try it yourself
likes = {("ann", "jazz"), ("bob", "rock"), ("cid", "rock"), ("cid", "jazz")}
people, songs = ["ann", "bob", "cid"], ["jazz", "rock"]
L = lambda p, q: (p, q) in likes
print("forall p exists q:", all(any(L(p, q) for q in songs) for p in people))
print("exists q forall p:", any(all(L(p, q) for p in people) for q in songs))
forall p exists q: True
exists q forall p: False

Negating quantified statements

Negation swaps the quantifier and moves inward:

¬∀x. P(x)≡∃x. ¬P(x)¬∃x. P(x)≡∀x. ¬P(x)\neg \forall x.\ P(x) \equiv \exists x.\ \neg P(x) \qquad \neg \exists x.\ P(x) \equiv \forall x.\ \neg P(x)

"Not every test passes" means "some test fails". To disprove a "for all" claim you need one counterexample; to disprove an "exists" claim you must show the property fails for every element.

Negating a nested statement step by step:

¬ ∀ε>0. ∃δ>0. Q(ε,δ)≡∃ε>0. ¬∃δ>0. Q(ε,δ)≡∃ε>0. ∀δ>0. ¬Q(ε,δ)\begin{aligned} \neg\, \forall \varepsilon > 0.\ \exists \delta > 0.\ Q(\varepsilon, \delta) &\equiv \exists \varepsilon > 0.\ \neg \exists \delta > 0.\ Q(\varepsilon, \delta) \\ &\equiv \exists \varepsilon > 0.\ \forall \delta > 0.\ \neg Q(\varepsilon, \delta) \end{aligned}

Validity in predicate logic

Some quantified formulas are true however the predicates and domain are chosen. For example, ∃y. ∀x. P(x,y)→∀x. ∃y. P(x,y)\exists y.\ \forall x.\ P(x, y) \to \forall x.\ \exists y.\ P(x, y) is valid: if one yy works for every xx, that yy is a witness for each xx. But the converse is not valid, as the songs example shows. Unlike propositional logic, there is no truth table to check validity here, because the domain may be infinite.

In software

Quantifiers are everywhere in code and queries:

Logic Python SQL
∀x∈D. P(x)\forall x \in D.\ P(x) all(P(x) for x in D) NOT EXISTS (SELECT … WHERE NOT P)
∃x∈D. P(x)\exists x \in D.\ P(x) any(P(x) for x in D) EXISTS (SELECT … WHERE P)

SQL has no "for all", so "customers who ordered every product" is written with a double negation: there is no product they did not order. Property-based testing tools (Hypothesis, QuickCheck) test a ∀\forall statement by generating many random inputs and searching for a counterexample.

Exercises

Exercise 1: Translate

Over the integers, translate: (a) "every integer has a larger integer"; (b) "there is a largest integer". Which is true?

Answer

(a) ∀x. ∃y. y>x\forall x.\ \exists y.\ y > x (true); (b) ∃y. ∀x. y≥x\exists y.\ \forall x.\ y \ge x (false).

Solution

(a) For any xx, take y=x+1y = x + 1. (b) is false: its negation ∀y. ∃x. x>y\forall y.\ \exists x.\ x > y is true, again with x=y+1x = y + 1. Note that (a) and (b) differ only in the order of the quantifiers (and the inequality).

Exercise 2: Negate

Negate and simplify: "Every server has a backup that is online."

Answer

"Some server has no backup that is online", that is, all of that server's backups are offline (or it has none).

Solution

Write ∀s. ∃b. (B(b,s)∧O(b))\forall s.\ \exists b.\ (B(b, s) \land O(b)). The negation is

∃s. ∀b. ¬(B(b,s)∧O(b))≡∃s. ∀b. (B(b,s)→¬O(b))\exists s.\ \forall b.\ \neg (B(b, s) \land O(b)) \equiv \exists s.\ \forall b.\ (B(b, s) \to \neg O(b))

so there is a server all of whose backups are offline.

Exercise 3: Check over a finite domain (code)

Over D={1,…,30}D = \{1, \ldots, 30\}, decide: (a) ∀x. ∃y. x+y=31\forall x.\ \exists y.\ x + y = 31; (b) ∃x. ∀y. x∣y\exists x.\ \forall y.\ x \mid y (where x∣yx \mid y means xx divides yy).

Answer

Both are true: (a) y=31−xy = 31 - x is in DD; (b) x=1x = 1 divides everything.

Solution
python · try it yourself
D = range(1, 31)
print("(a)", all(any(x + y == 31 for y in D) for x in D))
print("(b)", any(all(y % x == 0 for y in D) for x in D))
print("witnesses for (b):", [x for x in D if all(y % x == 0 for y in D)])
(a) True
(b) True
witnesses for (b): [1]

Test yourself

The negation of ∀x. P(x)\forall x.\ P(x) is…

  1. ∀x. ¬P(x)\forall x.\ \neg P(x)
  2. ∃x. ¬P(x)\exists x.\ \neg P(x)
  3. ¬∃x. P(x)\neg \exists x.\ P(x)
  4. ∃x. P(x)\exists x.\ P(x)

What does all(x > 0 for x in []) return in Python?

  1. True
  2. False
  3. An error
  4. None

Which statement is stronger (implies the other)?

  1. ∀p. ∃q. L(p,q)\forall p.\ \exists q.\ L(p, q)
  2. ∃q. ∀p. L(p,q)\exists q.\ \forall p.\ L(p, q)
  3. They are equivalent
  4. Neither implies the other