Learning objectives
By the end of this week you can:
- 1 Explain why propositional logic cannot express “every” and “some”, and what a predicate adds.
- 2 Evaluate a predicate P(x) for a given value, and say why P(x) alone is not a proposition.
- 3 Decide the truth value of ∀x P(x) and ∃x P(x) over a given domain, using a counterexample or a witness.
- 4 Explain how the domain changes the truth value of the same statement.
- 5 Translate English sentences into predicate logic and back, using the rule “∀ goes with →, ∃ goes with ∧”.
- 6 Negate quantified statements correctly with De Morgan's laws for quantifiers.
- 7 Write ∀ and ∃ over finite data in Python with all() and any(), and recognize them in SQL.
★ the wow moment
SQL is predicate logic with a nicer keyboard.
SELECT … WHERE EXISTS (…) is ∃, and SQL has no FOR ALL at all: databases write “every student has taken Java” as NOT EXISTS a student WHERE NOT java, that is, ¬∃x(S(x) ∧ ¬J(x)). That is De Morgan's law for quantifiers, the topic of this week, running millions of times per second in every database you will ever use.
Why we need more than propositions
Here is a famous argument, more than two thousand years old:
All humans are mortal. Socrates is a human. Therefore, Socrates is mortal.
Everybody agrees it is correct. Now try to write it in the propositional logic of weeks 2 and 3. The best we can do is three unrelated letters: = “all humans are mortal”, = “Socrates is a human”, = “Socrates is mortal”. The argument becomes , and nothing in and forces . Propositional logic cannot see inside a sentence. It does not know that “all humans” talks about Socrates.
The same problem shows up in software every day:
- “Every request is authenticated.”
- “Some server in the cluster is overloaded.”
- “No user has two active sessions.”
With 10,000 users we would need 10,000 separate propositions, one per user. What we really want is to write a statement once, with a variable, and say “for all of them” or “for some of them”. That is exactly what predicate logic adds, with two new ingredients: predicates and quantifiers.
Predicates
Take : “”.
| Statement | Proposition? | Truth value |
|---|---|---|
| : | no, is unknown | none yet |
| : | yes | true |
| : | yes | false |
In plain words: a predicate is a yes/no question with a blank. “Is ___ greater than 3?” has no answer until you fill in the blank.
If you program, you already know predicates. A predicate is simply a function that returns a boolean:
def P(x): # P(x): "x > 3"
return x > 3
def is_valid_ipv4(s): # V(s): "s is a valid IPv4 address"
parts = s.split(".")
return len(parts) == 4 and all(p.isdigit() and 0 <= int(p) <= 255 for p in parts)
P(4) # True
is_valid_ipv4("256.1.1.1") # False
Predicates with several variables
A predicate can have more than one blank. The number of variables is its arity.
- : “”. Then is true and is false.
- : “ loves ”. is, we hope, true.
- : “student has taken course ”, a predicate a university database answers thousands of times a day.
Building with connectives
Everything from weeks 2 and 3 still works: once the blanks are filled, the results are propositions and we can combine them with . Let be “”.
| Expression | Value |
|---|---|
| true ∨ false = true | |
| true ∧ false = false | |
| true → false = false | |
| true → true = true |
Answer
is true → true = true. is true → false = false: 6 is even but not divisible by 4.
The domain
Before we can say “for all ” we must answer: all from where? That set is the domain (also called the universe of discourse), written or .
The domain is not a detail. It decides the answer:
| Statement | Domain | True or false? |
|---|---|---|
| “every satisfies ” | integers | true |
| “every satisfies ” | real numbers | false: gives |
| “some satisfies ” | rational numbers | false ( is irrational, week 7) |
| “some satisfies ” | real numbers | true: |
The universal quantifier ∀
The symbol is an upside-down A, for All. It reads “for all”, “for every”, “for each” or “for any”.
That has a name.
The asymmetry is the key to this whole topic:
- To show is false: find one counterexample. Easy.
- To show is true: you must cover every element. With a finite domain, check them all; with an infinite domain you need an argument, a proof (week 7). A thousand examples that work prove nothing about the thousand-and-first.
∀ is a loop, and a big AND
How would a computer evaluate over a finite domain? It loops:
def for_all(domain, P):
for x in domain:
if not P(x):
return False # counterexample found: stop immediately
return True # survived every element
Python has this built in: all(P(x) for x in domain). It also stops at the first false value. When the domain is finite, say , the loop is just a long conjunction:
The existential quantifier ∃
The symbol is a backwards E, for Exists. It reads “there exists”, “there is”, “for some” or “at least one”.
The asymmetry is now reversed: proving true needs one example; proving false needs you to rule out every element.
def exists(domain, P):
for x in domain:
if P(x):
return True # witness found: stop immediately
return False # nobody worked
That is any(P(x) for x in domain), and over a finite domain it is a long disjunction:
The two quantifiers side by side
| Read as | for every | there is an |
| True when | is true for every | is true for at least one |
| False when | there is an with false | is false for every |
| Shown true by | an argument covering all | one witness |
| Shown false by | one counterexample | an argument covering all |
| Finite domain | ||
| Python | all(...) | any(...) |
Reading quantified formulas correctly
Precedence
and bind more tightly than every connective. So
When the quantifier should cover more, use parentheses. In this course we always write the parentheses, and you should too.
Bound and free variables
In the variable is bound by the quantifier, while is free: nothing tells us its value. A formula with a free variable is still a predicate (a question with a blank), not a proposition. It becomes a proposition when every variable is either bound or given a value: is true over the integers ().
The scope of a quantifier is the part of the formula it covers, usually the parenthesis right after it. A bound variable can be renamed without changing the meaning, exactly like a loop variable in code: and say the same thing.
Restricted domains, and the golden rule
Mathematics constantly quantifies over part of a domain: “every negative number”, “some prime number”, “every student in this class”. There is a short notation:
What do these mean in plain predicate logic? Read them slowly.
- “Every negative has ” means: for every , if is negative, then .
- “Some positive has ” means: there is a that is positive and has .
This gives the single most useful rule of the week.
The rule is not a convention. The other two combinations say something different, and the next section shows exactly why.
Seeing it with real data
Our domain is eight people. Some are students in this class (), some have taken Java (). Pick a statement with the buttons, then click the S and J values to change the data and watch what happens. Pay attention to the two statements marked ✗.
| x | S(x) | J(x) | body | loop |
|---|
Click any S or J value to flip it, and watch the verdict change.
Python
SQL
What the lab shows, in words:
- , “every student has taken Java”. For people who are not students, is false, so is automatically true (week 2: a promise with a false hypothesis is never broken). The implication filters: only the students are tested. This is the correct translation.
- says “everyone is a student in this class and has taken Java”. Kerem, Mert and Lina are not students, so it is false even when every student took Java. Wrong translation: it accidentally claims that the whole domain consists of students.
- , “some student has taken Java”, needs one person who is both. Correct translation.
- is true as soon as there is any non-student, since the implication is vacuously true for them. It says almost nothing. Wrong translation. Try it: select it and flip every student’s J to 0. Nobody in the class has taken Java, yet the formula stays true, held up by Kerem, Mert or Lina.
Translating English into logic
Translation is a skill, and a recipe helps.
Worked example: one sentence, two domains
“Every student in this class has taken a course in Java.”
- Domain = students in this class. Everybody in the domain is already a student, so no is needed: .
- Domain = all people. Now we must filter: .
Both are correct; they differ only because the domains differ. A narrower domain gives a shorter formula, a wider domain needs the filter.
A dictionary of sentence patterns
| English | Logic |
|---|---|
| All A are B. | |
| Some A are B. | |
| No A is B. | |
| Not all A are B. | |
| Some A are not B. | |
| Only A are B. | |
| Everything is A. | |
| Nothing is A. |
Practice with a strange island
Lewis Carroll style, from the lecture slides. The domain is all creatures; : is a fleegle, : is a snurd, : is a thingamabob. Cover the right column and translate first.
| English | Logic |
|---|---|
| Everything is a fleegle. | |
| Nothing is a snurd. | |
| All fleegles are snurds. | |
| Some fleegles are thingamabobs. | |
| No snurd is a thingamabob. | |
| If any fleegle is a snurd, then it is also a thingamabob. |
The last one hides a trap: “any” here means every. “If any fleegle is a snurd, then it is also a thingamabob” is a rule about each fleegle-snurd, so it is universal.
Specifications from systems
Engineers write requirements in exactly this form. Let : message is larger than megabytes, : message is compressed, : user is active, : network link is available.
| Requirement | Logic |
|---|---|
| Every mail message larger than one megabyte will be compressed. | |
| If a user is active, at least one network link will be available. |
The second one already contains two quantifiers, one inside the other. Nested quantifiers are next week’s topic.
The lions argument
Another classic from the slides. Domain: all creatures. : is a lion, : is fierce, : drinks coffee.
| Sentence | Logic |
|---|---|
| All lions are fierce. | |
| Some lions do not drink coffee. | |
| Therefore, some fierce creatures do not drink coffee. |
Is the argument valid? Take the lion that does not drink coffee (the witness of line 2). By line 1 it is fierce. So it is a fierce creature that does not drink coffee, a witness for line 3. In week 6 we will turn this kind of reasoning into formal rules of inference.
From logic back to English
Going the other way is just as important: you must be able to read a formula. Let the domain be all students in your school, : has a computer, : and are friends.
Word by word: “for every student , either has a computer, or there is a student who has a computer and is a friend of .” Now say it like a human: “Every student either has a computer or has a friend who has one.” A good translation sounds like a sentence a person would actually say.
Negating quantified statements
Your manager says: “Every test passed.” You find one failing test. What is the exact opposite of the manager’s claim?
- Not “no test passed”. That is far too strong.
- It is: “some test did not pass.” One failing test is enough.
That is the whole idea, written as two laws.
Why are they called De Morgan’s laws? Over a finite domain , is a big ∧ and is a big ∨, so the first law is literally week 3’s De Morgan:
| Negation | Equivalent to | True when |
|---|---|---|
| at least one makes false | ||
| every makes false |
Negating restricted statements, step by step
The negation must keep the filter, so it combines De Morgan for quantifiers with week 3’s .
Answers
- : “some number is at least as large as its own square.” Over the integers it is true, with witnesses and .
- : “no number squares to 2”. True over , false over .
- “Some request is not authenticated.” With : is a request and : is authenticated: .
Equivalences, and two traps
Two distributive laws hold for quantifiers:
“Everyone is tall and smart” says the same as “everyone is tall, and everyone is smart.” Fine. But the other two combinations fail:
Quantifiers in code and data
You will meet ∀ and ∃ every day as a software engineer, usually without the symbols.
Python. all() is and any() is , both with short-circuiting:
grades = {"Ada": 92, "Can": 78, "Deniz": 45}
all(g >= 50 for g in grades.values()) # ∀: did everyone pass? → False
any(g >= 90 for g in grades.values()) # ∃: did anyone get an A? → True
not any(g < 0 for g in grades.values()) # ¬∃ ≡ ∀¬: no negative grades → True
Note one subtle point: all([]) is True and any([]) is False. A ∀ statement over an empty domain is vacuously true (there is no counterexample), and an ∃ statement over an empty domain is false (there is no witness).
SQL. A WHERE clause is a predicate, and EXISTS is . SQL has no FOR ALL. To ask “has every student taken Java?”, a database uses this week’s law, :
SELECT NOT EXISTS (
SELECT * FROM people
WHERE is_student AND NOT took_java
) AS every_student_took_java;
Testing. A unit test checks one example: an ∃-style piece of evidence. Property-based testing states a ∀ property and lets the computer hunt for a counterexample:
from hypothesis import given, strategies as st
@given(st.lists(st.integers()))
def test_sorting_keeps_length(xs): # ∀ lists xs: len(sorted(xs)) == len(xs)
assert len(sorted(xs)) == len(xs)
If the property is false, Hypothesis prints a counterexample, and usually the smallest one it can find.
A look ahead: nested quantifiers
Many real statements need two quantifiers, one inside the other. “Every user has a password” is . And the order matters enormously: “for every number there is a bigger one” (, true) is very different from “there is a number bigger than every number” (, false). That is the whole of week 5.
Summary
- A predicate is a statement with a variable: a yes/no question with a blank. It becomes a proposition when the blank is filled.
- Always fix the domain; the same statement can be true in one domain and false in another.
- : true for every . Disproved by one counterexample. Over finite data it is a big ∧ and
all(). - : true for at least one . Proved by one witness. Over finite data it is a big ∨ and
any(). - Golden rule: ∀ goes with →, ∃ goes with ∧. and are the two classic mistakes.
- Negation: swap the quantifier and push ¬ inside. and .
- ∀ distributes over ∧ and ∃ over ∨, but not the other way round.
Practice
Answers
- .
- .
- . Being in this class requires knowing Python: “only Python-knowers are in the class” means “if in the class, then knows Python”.
- : “some student in this class has no GitHub account.”
- “No student in this class knows neither Python nor has a GitHub account”, or more naturally: “every student in this class knows Python or has a GitHub account.” Check: .
- (a) True for all integers: if then , and if then . (b) True, witness : (also ). (c) True: if then is even. This last one is our first real proof; week 7 does many more.
Before the lab
Lab 3 puts this week into code: you will evaluate ∀ and ∃ statements over a real student–course dataset with all() and any(), find witnesses and counterexamples automatically, and check De Morgan’s laws for quantifiers on data.
In industry
-
Databases
WHERE clauses are predicates; EXISTS and NOT EXISTS are quantifiers. Query optimizers rewrite queries with exactly the equivalences on this page.
-
Access control
Cloud permission systems answer questions like “does there exist a policy that allows this request?” and “for every request from this role, is access denied?”.
-
Testing
Unit tests check a few examples (∃). Property-based testing (Hypothesis in Python) tries thousands of random inputs to hunt for a counterexample to a ∀ statement.
-
Specifications
Requirements like “every message larger than 1 MB is compressed” are ∀ statements. Writing them in logic exposes ambiguity before any code is written.
Lab 3: Quantifiers over Real Data
Write ∀ and ∃ as loops, find counterexamples and witnesses, answer questions about a class of students with the golden rule, and put De Morgan's laws for quantifiers into code.
due Fri, Oct 16 · 23:59 · 100 points · open now
Go deeper
- Mathematics for Computer Science (Lehman, Leighton, Meyer) · Chapter 3.6, predicate formulas
- Stanford CS103 Mathematical Foundations of Computing · first-order logic lectures, with excellent translation practice
- Discrete Mathematics: An Open Introduction (Oscar Levin) · Section 0.2, quantifiers and predicates
- forall x: Calgary, an open logic textbook · Part V, first-order logic and symbolization
- Hypothesis: property-based testing for Python · ∀ statements as tests
Tags:predicatepropositional functionquantifieruniversalexistentialforallexistsdomaincounterexamplewitnessnegationDe MorganSQLallany