Learning Outcomes
SLO | After this lecture you will be able to... | CLO | Bloom |
|---|---|---|---|
SLO 7.1 | Explain why propositional logic is not enough and name the parts of first-order logic | CLO2 | C2 Understand |
SLO 7.2 | Translate English sentences with ALL and SOME into first-order logic using ∀ and ∃ | CLO2 | C3 Apply |
SLO 7.3 | Decide whether a quantified sentence is true in a small world and apply universal instantiation | CLO2 | C3 Apply |
CLO2: Demonstrate first-order predicate calculus, logical reasoning and problem-solving using Prolog language.
1. Why Predicate Logic?
§1.1 The limits of propositional logic
Slide L07-S06
Propositional logic (Lecture 6) treats a whole sentence as one block: 'Ali is a student' becomes a single symbol P1. It cannot look inside the sentence to see the object (Ali) and the property (is a student). This causes two problems:
- Too many symbols: every fact about every student needs its own symbol. The rule 'every student needs 75% attendance' must be written again for each of our 60 students: P1 → Q1, P2 → Q2, ...
- No ALL or SOME: 'some student topped CS-511' cannot be written without naming the student.
What we want to say | Propositional logic | First-order logic |
|---|---|---|
Ali is a student. | P1 | Student(Ali) |
Every student needs 75% attendance. | P1 → Q1, P2 → Q2, ... (60 rules) | ∀x Student(x) → NeedsAttendance(x) |
Some student topped CS-511. | cannot say without naming who | ∃x Student(x) ∧ Topped(x, CS511) |
Names: first-order logic (FOL) = predicate logic = first-order predicate calculus. 'First-order' means the quantifiers range over objects only. FOL is the base of Prolog (Lecture 9), database queries and expert systems. |
2. The Building Blocks
§2.1 Symbols of first-order logic
Slide L07-S07
Part | Meaning | Examples |
|---|---|---|
Constant | Names one particular object (capital letter) | Ali, Sana, Burewala, CS511, Waqas |
Variable | Stands for any object (small letter) | x, y, z |
Predicate | A property or relation; gives TRUE or FALSE | Student(Ali), Enrolled(Ali, CS511) |
Function | Maps objects to ONE object (not true/false) | fatherOf(Ali), marks(Ali, CS511) |
Connectives | The five from Lecture 6 | ¬ ∧ ∨ → ↔ |
Quantifiers | For all, there exists | ∀x, ∃x |
Equality | Two terms name the same object | capitalOf(Punjab) = Lahore |
Arity (the number of arguments) of a predicate or function is fixed: Student has arity 1, Enrolled has arity 2 (student, course). The order of the arguments matters: Advises(Waqas, Ali) - Waqas advises Ali - is a different sentence from Advises(Ali, Waqas).
§2.2 Terms and sentences
Slide L07-S08
Kind | What it is | Examples | Ask yourself |
|---|---|---|---|
Term | Names an object | Ali, x, fatherOf(Ali), marks(Sana, CS511) | Who or what? |
Atomic sentence | Predicate applied to terms | Student(Ali), Older(fatherOf(Ali), Ali) | True or false? |
Complex sentence | Atomic sentences + connectives | Student(Ali) ∧ ¬Hostel(Ali) | True or false? |
Check: fatherOf(Ali) is NOT true or false - it is a person (a term). Only sentences have a truth value. |
3. Quantifiers: ALL and SOME
§3.1 The universal quantifier ∀ (for all)
Slide L07-S10
∀x P(x) is true when P is true for EVERY object in the world. Real sentences usually talk about all objects of one kind ('all students'), so we use an implication: 'All students need 75% attendance' is ∀x Student(x) → NeedsAttendance(x) - for every x, IF x is a student THEN x needs 75% attendance. The rule then says nothing about teachers or buildings. You can think of ∀ as a big AND over all objects.
Local example: 'Every wheat field in Burewala needs irrigation in December' = ∀x (WheatField(x) ∧ In(x, Burewala)) → NeedsIrrigation(x, December).
§3.2 The existential quantifier ∃ (there exists)
Slide L07-S11
∃x P(x) is true when P is true for AT LEAST ONE object (one or more, not 'exactly one'). 'Some student lives in the hostel' is ∃x Student(x) ∧ Hostel(x) - there is an x that is a student AND lives in the hostel. You can think of ∃ as a big OR. Local example: 'Some patient in Burewala tested positive for dengue' = ∃x Patient(x) ∧ In(x, Burewala) ∧ Positive(x, Dengue).
Rule of thumb: ∀ goes with → ('all A are B' = ∀x A(x) → B(x)); ∃ goes with ∧ ('some A are B' = ∃x A(x) ∧ B(x)). |
§3.3 Negation of quantifiers
Slide L07-S12
Moving ¬ across a quantifier flips it (De Morgan's law for quantifiers):
Rule | Equivalence | Example |
|---|---|---|
¬ over ∀ | ¬∀x P(x) ≡ ∃x ¬P(x) | 'Not all crops need canal water' = 'some crop does not need canal water' |
¬ over ∃ | ¬∃x P(x) ≡ ∀x ¬P(x) | 'No student lives in the hostel' = 'every student does not live in the hostel' |
With a kind: ¬∀x (Crop(x) → W(x)) ≡ ∃x (Crop(x) ∧ ¬W(x)) and ¬∃x (Student(x) ∧ Hostel(x)) ≡ ∀x (Student(x) → ¬Hostel(x)). These were checked by a program in every possible world over three objects.
§3.4 Nested quantifiers: order matters
Slide L07-S12
Sentence | Meaning |
|---|---|
∀x ∃y Advises(y, x) | Every student has SOME advisor (it may be a different teacher for each student). |
∃y ∀x Advises(y, x) | There is ONE teacher who advises EVERY student - a much stronger claim. |
Quantifiers of the same type can be swapped (∀x ∀y = ∀y ∀x), but when ∀ and ∃ are mixed the order changes the meaning.
§3.5 Translating English into FOL
Slide L07-S13
Method: (1) write the key - constants and predicates with their arity; (2) find the quantifier words: all / every / each → ∀ with →; some / a / there is → ∃ with ∧; no / none → ¬∃ ... ∧ (or ∀ ... → ¬); 'only A are B' → ∀x B(x) → A(x); (3) write the formula with brackets.
Key: Student(x), Teacher(x), Course(x), Enrolled(x, c), Teaches(t, c), Passed(x, c), Hostel(x), TakesLab(x); constants Ali, Waqas, CS511.
English | First-order logic |
|---|---|
Ali is enrolled in CS-511. | Enrolled(Ali, CS511) |
Every student is enrolled in some course. | ∀x Student(x) → ∃c Course(c) ∧ Enrolled(x, c) |
Some student failed CS-511. | ∃x Student(x) ∧ ¬Passed(x, CS511) |
No student lives in the hostel. | ¬∃x Student(x) ∧ Hostel(x) ≡ ∀x Student(x) → ¬Hostel(x) |
Waqas teaches every course. | ∀c Course(c) → Teaches(Waqas, c) |
Only enrolled students take the AI lab. | ∀x TakesLab(x) → Enrolled(x, CS511) |
4. Truth in a World
§4.1 Models: checking a sentence in a small world
Slide L07-S15
A model (world) for FOL lists the objects (the domain) and which predicates are true for which objects. Our world: six people - students Ali (82% attendance), Sana (91%), Bilal (68%) and Hira (77%); teachers Waqas (advises Ali and Sana) and Ayesha (advises Bilal and Hira). All four students take CS-511 and nobody lives in the hostel. Eligible(x) means attendance ≥ 75%. A ∀ sentence is false as soon as ONE counterexample exists; an ∃ sentence is true as soon as ONE witness exists. The program below checks this (plain Python, no libraries):
"""L07 demo: checking first-order sentences in one small 'world' (a model). Plain Python, no libraries. A world = a domain of objects + which predicates are true for which objects.""" people = ["Ali", "Sana", "Bilal", "Hira", "Waqas", "Ayesha"] student = {"Ali", "Sana", "Bilal", "Hira"} teacher = {"Waqas", "Ayesha"} attendance = {"Ali": 82, "Sana": 91, "Bilal": 68, "Hira": 77} advises = {("Waqas", "Ali"), ("Waqas", "Sana"), ("Ayesha", "Bilal"), ("Ayesha", "Hira")} enrolled = {"Ali", "Sana", "Bilal", "Hira"} # all four take CS-511 hostel = set() # nobody in this world lives in the hostel Student = lambda x: x in student Teacher = lambda x: x in teacher Eligible = lambda x: attendance.get(x, 0) >= 75 # 75% attendance rule Advises = lambda y, x: (y, x) in advises Enrolled = lambda x: x in enrolled Hostel = lambda x: x in hostel implies = lambda a, b: (not a) or b def forall(f): bad = [x for x in people if not f(x)] return (True, None) if not bad else (False, bad[0]) # False + a counterexample def exists(f): good = [x for x in people if f(x)] return (True, good[0]) if good else (False, None) # True + a witness def show(text, result): value, obj = result note = f" (witness: {obj})" if value and obj else f" (counterexample: {obj})" if obj else "" print(f"{text:<52} {str(value):<5}{note}") show("∀x Student(x) → Eligible(x)", forall(lambda x: implies(Student(x), Eligible(x)))) show("∃x Student(x) ∧ ¬Eligible(x)", exists(lambda x: Student(x) and not Eligible(x))) show("∀x Student(x) → ∃y Teacher(y) ∧ Advises(y,x)", forall(lambda x: implies(Student(x), exists(lambda y: Teacher(y) and Advises(y, x))[0]))) show("∃y Teacher(y) ∧ ∀x (Student(x) → Advises(y,x))", exists(lambda y: Teacher(y) and forall(lambda x: implies(Student(x), Advises(y, x)))[0])) print() print("Translation traps (right form first, wrong form second):") show("∀x Student(x) → Enrolled(x) right", forall(lambda x: implies(Student(x), Enrolled(x)))) show("∀x Student(x) ∧ Enrolled(x) wrong", forall(lambda x: Student(x) and Enrolled(x))) show("∃x Student(x) ∧ Hostel(x) right", exists(lambda x: Student(x) and Hostel(x))) show("∃x Student(x) → Hostel(x) wrong", exists(lambda x: implies(Student(x), Hostel(x)))) |
Real output:
∀x Student(x) → Eligible(x) False (counterexample: Bilal) ∃x Student(x) ∧ ¬Eligible(x) True (witness: Bilal) ∀x Student(x) → ∃y Teacher(y) ∧ Advises(y,x) True ∃y Teacher(y) ∧ ∀x (Student(x) → Advises(y,x)) False Translation traps (right form first, wrong form second): ∀x Student(x) → Enrolled(x) right True ∀x Student(x) ∧ Enrolled(x) wrong False (counterexample: Waqas) ∃x Student(x) ∧ Hostel(x) right False ∃x Student(x) → Hostel(x) wrong True (witness: Waqas) |
Read the traps carefully: ∀x Student(x) ∧ Enrolled(x) claims that EVERYONE is a student, so Waqas is a counterexample. ∃x Student(x) → Hostel(x) is 'true' only because Waqas is not a student (an implication with a false condition is true) - although no student lives in the hostel. Wrong connectives give wrong answers.
§4.2 Universal instantiation: a first proof
Slide L07-S16
Universal instantiation (UI): from ∀x P(x) we may conclude P(c) for any constant c, by the substitution {x/c}. Existential instantiation (EI): from ∃x P(x) we may give the unknown object a NEW name (e.g. T1); never reuse a known name, because we do not know who it is.
Step | Sentence | Reason |
|---|---|---|
1 | ∀x Student(x) ∧ Attendance75(x) → Eligible(x) | Rule in the KB |
2 | Student(Ali) | Fact |
3 | Attendance75(Ali) | Fact (82%) |
4 | Student(Ali) ∧ Attendance75(Ali) → Eligible(Ali) | UI of 1 with {x/Ali} |
5 | Student(Ali) ∧ Attendance75(Ali) | AND-introduction of 2, 3 |
6 | Eligible(Ali) | Modus ponens on 4, 5 |
Lecture 8 shows how a computer does such proofs automatically (unification and resolution), and Lecture 9 shows the same rule as a Prolog program.
5. Worked Examples
Key: Crop(x), Kharif(x), NeedsWater(x), GrownIn(x, p), Farmer(x), Field(x), Owns(f, x); constants Cotton, Vehari.
English | Step-by-step | First-order logic |
|---|---|---|
Cotton is a Kharif crop. | A fact about one object | Crop(Cotton) ∧ Kharif(Cotton) |
All Kharif crops need water. | all → ∀ with → | ∀x (Crop(x) ∧ Kharif(x)) → NeedsWater(x) |
Some crop is grown in Vehari. | some → ∃ with ∧ | ∃x Crop(x) ∧ GrownIn(x, Vehari) |
Not every crop is Kharif. | ¬∀ = ∃ ... ¬ | ∃x Crop(x) ∧ ¬Kharif(x) |
6. Common Mistakes
Slide L07-S18
Wrong idea | Correct idea |
|---|---|
All students are enrolled: ∀x Student(x) ∧ Enrolled(x) | ∀x Student(x) → Enrolled(x) (∀ goes with →) |
Some student is in the hostel: ∃x Student(x) → Hostel(x) | ∃x Student(x) ∧ Hostel(x) (∃ goes with ∧) |
¬∀x P(x) means ∀x ¬P(x) | ¬∀x P(x) ≡ ∃x ¬P(x) - not all = some are not |
∀x ∃y and ∃y ∀x mean the same | With mixed quantifiers the order changes the meaning |
fatherOf(Ali) is true or false | It is a term (a person); only sentences are true or false |
Word Bank
Word | Simple meaning | Example |
|---|---|---|
Predicate | A property or relation that is true or false of objects | Student(Ali) |
Constant | A name for one particular object | Burewala |
Variable | A placeholder for any object | x |
Function (in logic) | Gives one object from other objects | fatherOf(Ali) |
Arity | Number of arguments | Enrolled has arity 2 |
Quantifier | Says how many objects: all (∀) or some (∃) | ∀x, ∃x |
Domain | The set of objects in a world | the six people in S15 |
Witness | An object that makes an ∃ sentence true | Bilal for ∃x ¬Eligible(x) |
Counterexample | An object that makes a ∀ sentence false | Bilal for ∀x Eligible(x) |
Instantiation | Replacing a variable with a constant | {x/Ali} |
Summary
- FOL adds objects, predicates, functions, variables and quantifiers to propositional logic.
- Predicates give true/false; functions give objects. Arity and argument order matter.
- ∀x is true when every object satisfies the sentence; ∃x when at least one does. ∀ goes with →, ∃ goes with ∧.
- ¬ flips a quantifier; with mixed ∀ and ∃ the order matters.
- Universal instantiation substitutes a constant for a variable; with modus ponens it gives the first FOL proofs.
Self-Check Questions
- Give two reasons why propositional logic is not enough for a university's rules. (see §1.1)
- What is the difference between a predicate and a function? Give one example of each. (see §2.1-2.2)
- Translate 'All Kharif crops need water' and explain why → is used, not ∧. (see §3.1, §5)
- Translate 'Some student failed CS-511' and explain why ∧ is used, not →. (see §3.2)
- Rewrite ¬∃x (Student(x) ∧ Hostel(x)) using ∀. (see §3.3)
- Explain the difference between ∀x ∃y Advises(y, x) and ∃y ∀x Advises(y, x). (see §3.4)
- In the S15 world, why is ∀x Student(x) → Eligible(x) false? (see §4.1)
- Write the six-step proof that Ali is eligible and name each rule. (see §4.2)
Reading: Russell & Norvig (4e) Sections 8.2-8.3; Luger (6e) Section 2.2.