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 |
|---|---|---|---|
| "for all in " | is true for every in | all(P(x) for x in D) |
|
| "there exists in " | is true for at least one | any(P(x) for x in D) |
The domain of discourse matters. "" is true over the real numbers and false over the rationals.
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 mean "person likes song ".
- : everyone likes some song (maybe different songs).
- : 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.
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:
"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:
Validity in predicate logic
Some quantified formulas are true however the predicates and domain are chosen. For example, is valid: if one works for every , that is a witness for each . 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 |
|---|---|---|
all(P(x) for x in D) |
NOT EXISTS (SELECT … WHERE NOT P) |
|
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 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) (true); (b) (false).
Solution
(a) For any , take . (b) is false: its negation is true, again with . 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 . The negation is
so there is a server all of whose backups are offline.
Exercise 3: Check over a finite domain (code)
Over , decide: (a) ; (b) (where means divides ).
Answer
Both are true: (a) is in ; (b) divides everything.
Solution
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 is…
What does all(x > 0 for x in []) return in Python?
TrueFalse- An error
None
Which statement is stronger (implies the other)?
- They are equivalent
- Neither implies the other