People who lean on logic and philosophy and rational exposition end by starving the best part of the mind.
William Butler Yeats
5.1 Introduction¶
First-order logic (FOL), also called predicate logic, is a more powerful extension of propositional logic that enables complex reasoning about objects and their relationships—essential for real-world AI applications like expert systems.
Why First-Order Logic?¶
Limitations of Propositional Logic:
Propositional logic cannot express general statements about classes of objects. Consider:
“All mammals give birth to live babies”
“A cat is a mammal”
Therefore: “A cat gives birth to live babies”
In propositional logic, you would need separate rules for every mammal (cat, dog, elephant, etc.), making knowledge bases unwieldy or impossible when dealing with large or infinite domains.
What First-Order Logic Provides:
Objects: Represent entities in the domain (people, movies, awards)
Relations: Express relationships between objects
Functions: Map objects to other objects
Quantifiers: Make statements about all or some objects
Variables: Bind to any object in a domain
Real-World Applications:
Expert systems (medical diagnosis, legal reasoning)
Natural language understanding
Semantic web and knowledge graphs
Database query languages (SQL is based on FOL)
Automated theorem proving
Planning and scheduling systems
5.2 Syntax of First-Order Logic¶
5.2.1 Basic Components¶
Constants - Represent specific objects:
Examples:
Variables - Represent arbitrary objects from a domain:
Denoted:
Must be bound by quantifiers or substitution
Functions - Map objects to objects:
: the father of
: favorite food of person
Arity: number of arguments (e.g., has arity 1)
Predicates - Functions that return truth values:
: person eats food
: director won award for movie
Arity: number of arguments
Example:
This predicate is true when Steven Spielberg won Best Director for Saving Private Ryan.
5.2.2 Terms¶
A term represents an object and can be:
A constant:
A variable:
A function applied to terms: ,
Examples of terms:
✓
✓
✓ (grandfather of )
5.2.3 Atomic Formulas¶
An atomic formula (atom) is formed by applying a predicate to terms:
where is a predicate and are terms.
Examples:
- John eats beef
- is non-vegetarian
- is an ancestor of
5.2.4 Logical Operators¶
FOL uses the same operators as propositional logic:
| Operator | Symbol | Example |
|---|---|---|
| Negation | ||
| Conjunction | ||
| Disjunction | ||
| Implication | ||
| Biconditional |
5.2.5 Complex Formulas¶
Complex formulas are built recursively:
Every atomic formula is a formula
If is a formula, then is a formula
If and are formulas, then:
, , , are formulas
Quantified formulas (see next section)
5.3 Quantifiers¶
Quantifiers allow statements about entire domains or existence of objects.
5.3.1 Universal Quantifier ()¶
The universal quantifier means “for all ” or “for every ”.
Syntax:
Meaning: Formula is true for every object in the domain.
Examples:
Everyone who eats beef is non-vegetarian:
All mammals give birth to live babies:
Award winners are invited to the Oscars:
Connection to conjunction: Universal quantification over a finite domain is equivalent to conjunction:
5.3.2 Existential Quantifier ()¶
The existential quantifier means “there exists an ” or “for some ”.
Syntax:
Meaning: Formula is true for at least one object in the domain.
Examples:
Someone eats beef:
There is a non-vegetarian person who eats beef:
Note: is wrong - it’s true whenever there’s a vegetarian!
Everyone eats something:
Connection to disjunction: Existential quantification over a finite domain is equivalent to disjunction:
5.3.3 Quantifier Order Matters¶
The order of quantifiers changes meaning dramatically:
Example 1: Everyone eats something (possibly different for each person)
Example 2: There’s one special food everyone eats
The second is much stronger and likely false!
General Rule:
(order doesn’t matter for same quantifier)
But (order matters for mixed quantifiers!)
5.3.4 Scope and Bound Variables¶
The scope of a quantifier is the formula it applies to (usually in brackets).
A variable is bound if it appears within the scope of a quantifier for that variable.
A variable is free if it’s not bound by any quantifier.
Example:
Here, is bound, but is free.
Closed formulas (sentences) have no free variables - these are what knowledge bases contain.
5.3.5 Standardization¶
Standardization uses different variable names for different quantifiers to avoid confusion.
Bad (confusing):
The second is free!
Good (standardized):
Or better yet:
5.4 Semantics of First-Order Logic¶
5.4.1 Interpretations¶
An interpretation (or model) specifies:
Domain: A non-empty set of objects
Constant assignments: Each constant denotes a specific object
Function assignments: Each function maps objects to objects
Predicate assignments: Each predicate has a truth value for each combination of objects
Example: For a knowledge base about people and food:
Domain:
5.4.2 Truth in a Model¶
A formula is true in a model (satisfied) if:
For atomic formulas: Check the predicate assignment
For negation: is true iff is false
For connectives: Use truth tables as in propositional logic
For : True iff is true for every object in the domain
For : True iff is true for at least one object in the domain
5.4.3 Validity and Satisfiability¶
A formula is valid (tautology) if it’s true in all possible models.
Example: is valid.
A formula is satisfiable if it’s true in at least one model.
A formula is unsatisfiable (contradiction) if it’s false in all models.
Example: is unsatisfiable.
5.4.4 Entailment¶
means entails : in every model where is true, is also true.
Entailment via unsatisfiability:
This is the basis for proof by contradiction in FOL.
5.5 Quantifier Manipulation Laws¶
5.5.1 Negation and Quantifiers¶
De Morgan’s Laws for Quantifiers:
Key insight: Pushing negation through a quantifier flips its type.
Example:
“Not everyone is non-vegetarian” = “Someone is vegetarian”
Justification: For finite domain :
5.5.2 Operator Precedence¶
From highest to lowest precedence:
Quantifiers:
Negation:
Conjunction:
Disjunction:
Implication:
Biconditional:
Example:
means (quantifier has highest precedence)
To quantify the full expression, use brackets:
5.6 Unification and Substitution¶
5.6.1 Substitution¶
Substitution replaces variables with terms.
Notation: means replace with term
Example:
5.6.2 Types of Substitution¶
Ground Substitution: Replace variable with a constant
From , we can infer:
Flat Substitution: Replace one variable with another
From , we can infer (just renaming)
5.6.3 Unification¶
Unification finds a substitution that makes two expressions identical.
Definition: A substitution unifies expressions and if
Examples:
Unify and :
Substitution:
Result:
Unify and :
Substitution:
Result:
Unify and :
Cannot unify - would require and simultaneously
Unify and :
Substitution:
Result:
5.6.4 Most General Unifier (MGU)¶
Among all possible unifiers, the most general unifier (MGU) is preferred.
Example: Unify and
Possible: giving
Possible: giving
MGU: or giving or
The MGU is most general because it can be specialized further.
5.6.5 Unification Algorithm¶
Algorithm: UNIFY(E1, E2)
begin
θ ← {} // Empty substitution
if E1 and E2 are identical then
return θ
if E1 is a variable then
if E1 occurs in E2 then
return FAIL // Occur check
else
return {E1/E2}
if E2 is a variable then
return UNIFY(E2, E1)
if E1 = f(E1_1, ..., E1_n) and E2 = g(E2_1, ..., E2_m) then
if f ≠ g or n ≠ m then
return FAIL
θ ← {}
for i = 1 to n do
θ_i ← UNIFY(E1_iθ, E2_iθ)
if θ_i = FAIL then
return FAIL
θ ← θ ∪ θ_i
return θ
endComplexity: where is the size of expressions
Occur check: Prevents infinite structures like
5.7 Inference in First-Order Logic¶
5.7.1 Generalized Modus Ponens¶
Classical Modus Ponens from propositional logic:
In FOL, we need Generalized Modus Ponens with unification:
Given:
1. Rule: ∀x [P(x) → Q(x)]
2. Fact: P(a)
Steps:
1. Instantiate rule with x/a: P(a) → Q(a)
2. Apply Modus Ponens: Q(a)More generally:
where unifies each with
Example:
Given:
Rule: (eating meat makes you non-vegetarian)
Facts: and
Steps:
Unify with :
Check holds: ✓
Apply substitution to conclusion:
5.7.2 Universal Instantiation¶
From a universally quantified statement, infer any instance:
where is any ground term
Example:
5.7.3 Existential Instantiation¶
From an existentially quantified statement, introduce a new constant (Skolem constant):
where is a new constant (Skolem constant) not appearing elsewhere
Example:
Here, “John” is a Skolem constant representing “some king”.
5.7.4 Skolemization¶
Purpose: Eliminate existential quantifiers to simplify inference.
Skolem Constants (when is not in scope of ):
where is a new Skolem constant
Skolem Functions (when is in scope of ):
Here is a Skolem function: the food that person eats depends on .
Complex Example:
Original:
After Skolemization:
depends only on (outer )
depends on both and (both in its scope)
5.8 Forward and Backward Chaining¶
5.8.1 First-Order Definite Clauses¶
A first-order definite clause has the form:
Antecedent: conjunction of positive literals
Consequent: single positive literal
Variables are universally quantified (implicit)
Examples:
Facts are definite clauses with empty antecedent:
5.8.2 Forward Chaining Algorithm¶
Forward chaining is data-driven: start with known facts, derive new facts until query is reached.
Algorithm: FOL-FC-ASK(KB, query)
begin
facts ← all ground atomic sentences in KB
repeat
new ← {}
for each rule r in KB do
for each substitution θ that unifies premises of r with facts do
conclusion ← (head of r)θ
if conclusion = query then
return True
if conclusion not in facts then
new ← new ∪ {conclusion}
if new = {} then
return False // No new facts derived
facts ← facts ∪ new
until False
endProperties:
Sound: Only derives true consequences
Complete: For definite clauses, will find all entailed facts
Datalog complexity: Polynomial in KB size (for function-free Horn clauses)
Example: Prove
Given:
Facts: ,
Rules:
Iteration 1:
Apply rule 1 with : infer
Apply rule 1 with : infer
Iteration 2:
Apply rule 2 with : infer ✓
5.8.3 Backward Chaining Algorithm¶
Backward chaining is goal-driven: start with query, work backward to find supporting facts.
Algorithm: FOL-BC-ASK(KB, goal)
begin
if goal is a known fact in KB then
return True
for each rule r in KB where head unifies with goal do
θ ← UNIFY(head of r, goal)
premises ← (body of r)θ
if FOL-BC-ASK-ALL(KB, premises) then
return True
return False
end
Algorithm: FOL-BC-ASK-ALL(KB, premises)
begin
if premises is empty then
return True
first ← first premise in premises
rest ← remaining premises
for each substitution θ that makes FOL-BC-ASK(KB, first) = True do
if FOL-BC-ASK-ALL(KB, restθ) then
return True
return False
endProperties:
Sound: Only returns True if entailment holds
Complete: For definite clauses
Efficient: Only explores relevant parts of KB
Depth-first: Can loop infinitely (needs cycle detection)
Example: Same as forward chaining, but working backward from
5.8.4 Forward vs Backward Chaining¶
| Aspect | Forward Chaining | Backward Chaining |
|---|---|---|
| Strategy | Data-driven | Goal-driven |
| Direction | Facts → Conclusions | Query → Facts |
| Search | Breadth-first | Depth-first |
| Efficiency | Derives many facts | Focuses on query |
| Best for | Multiple queries | Single query |
| Space | Higher | Lower |
5.9 Resolution in First-Order Logic¶
Resolution is the most powerful inference method for FOL, generalizing propositional resolution.
5.9.1 Conversion to CNF¶
To use resolution, first convert to CNF:
Steps:
Eliminate implications and biconditionals
Move negation inward (De Morgan + quantifiers)
Standardize variables (different names for different quantifiers)
Skolemize (eliminate )
Replace existential variables with Skolem functions
Drop universal quantifiers (all remaining variables are )
Distribute over
Example: Convert to CNF
Original:
Step 1 (eliminate ):
Step 4 (Skolemize inside ):
Step 5 (drop ):
This is now in CNF.
5.9.2 Resolution Rule with Unification¶
Propositional resolution:
First-order resolution (with unification):
where
Example:
Clauses:
Resolution:
Unify with :
Resolve:
5.9.3 Resolution Algorithm¶
Algorithm: FOL-RESOLUTION(KB, α)
begin
clauses ← CNF(KB ∧ ¬α)
new ← {}
loop
for each pair (Ci, Cj) of clauses in clauses do
resolvents ← RESOLVE(Ci, Cj)
if resolvents contains empty clause □ then
return True // Proved by contradiction
new ← new ∪ resolvents
if new ⊆ clauses then
return False // No progress, cannot prove
clauses ← clauses ∪ new
end
Algorithm: RESOLVE(Ci, Cj)
begin
resolvents ← {}
for each literal ℓi in Ci do
for each literal ℓj in Cj do
if θ = UNIFY(ℓi, ¬ℓj) exists then
resolvent ← ((Ci - {ℓi}) ∪ (Cj - {ℓj}))θ
resolvents ← resolvents ∪ {resolvent}
return resolvents
endProperties:
Sound: Only derives true conclusions
Refutation-complete: Can prove any entailed sentence by finding contradiction
Semi-decidable: May not terminate for non-entailed sentences
Complete Example:
Prove: All cats are mammals; Whiskers is a cat; therefore Whiskers is a mammal.
KB:
Query:
CNF of :
Resolution steps:
Resolve (1) and (2) with :
Resolve and (3): (empty clause)
Contradiction found → ✓
5.10 Practical Knowledge Engineering¶
5.10.1 Designing a Knowledge Base¶
Steps:
Identify the domain: What objects and relationships exist?
Choose vocabulary:
Constants for objects
Predicates for properties and relations
Functions for mappings
Encode general knowledge: Universal rules
Encode specific facts: Ground instances
Query and test: Verify inferences are correct
5.10.2 Example: Family Relations¶
Domain: People and family relationships
Vocabulary:
Constants: John, Mary, Bob, Sue
Predicates: Parent(x,y), Male(x), Female(x)
Functions: Father(x), Mother(x)
Rules:
Parent(x,y) ∧ Parent(y,z) → Grandparent(x,z)
Parent(x,y) ∧ Male(x) → Father(x) = x
Parent(x,y) ∧ Parent(x,z) ∧ y≠z → Sibling(y,z)
Parent(x,y) ∧ Parent(z,w) ∧ Sibling(y,w) → Cousin(x,z)Facts:
Parent(John, Mary)
Parent(Mary, Bob)
Male(John)
Female(Mary)Query: Is John a grandparent of Bob?
Inference: Apply first rule with appropriate substitutions → Yes!
5.10.3 Common Patterns¶
Transitive Relations:
P(x,y) → Ancestor(x,y)
P(x,z) ∧ Ancestor(z,y) → Ancestor(x,y)Symmetric Relations:
Sibling(x,y) → Sibling(y,x)Equivalence Classes:
SameSpecies(x,y) ∧ SameSpecies(y,z) → SameSpecies(x,z)5.11 Summary¶
Key Concepts¶
Expressive Power: FOL can represent objects, relations, and quantification
Syntax: Constants, variables, functions, predicates, quantifiers
Semantics: Interpretations, models, truth values
Quantifiers: (universal), (existential)
Unification: Making expressions identical via substitution
Skolemization: Eliminating existential quantifiers
Inference: Generalized Modus Ponens, forward/backward chaining, resolution
Comparison with Propositional Logic¶
| Aspect | Propositional | First-Order |
|---|---|---|
| Objects | No | Yes |
| Quantifiers | No | Yes (, ) |
| Relations | No | Yes |
| Functions | No | Yes |
| Decidability | Decidable | Semi-decidable |
| Expressiveness | Limited | High |
| Complexity | NP-complete | Undecidable (general) |
Decidability¶
First-order logic is semi-decidable:
If , resolution will eventually prove it (complete)
If , resolution may run forever (not decidable)
This contrasts with propositional logic, which is fully decidable.
Why FOL Matters¶
First-order logic is the foundation for:
Knowledge representation: Expert systems, medical diagnosis
Semantic web: RDF, OWL, SPARQL
Databases: SQL query languages
Planning: STRIPS, PDDL
Natural language: Semantic parsing
Theorem proving: Automated reasoning systems
5.12 Implementation¶
For complete Python implementations, see:
Chapter 5: First-Order Logic - Implementation
The implementation notebook includes:
Expression Classes: Variable, Constant, Function, Predicate
Term and Formula Structures: Atomic, compound, quantified
Unification Algorithm: MGU computation with occur check
Substitution: Ground and flat substitution operations
CNF Conversion: Full pipeline with Skolemization
Forward Chaining: FOL-FC-ASK with unification
Backward Chaining: FOL-BC-ASK depth-first search
Resolution Prover: Complete theorem proving
Knowledge Base: Store and query FOL sentences
Real-World Applications:
Family relationship reasoner
Expert system for classification
Natural deduction proofs
Further Reading¶
Textbooks¶
Aggarwal, C. C. (2021). Artificial Intelligence: A Textbook. Springer. [Chapter 5]
Russell, S., & Norvig, P. (2020). Artificial Intelligence: A Modern Approach (4th ed.). Pearson. [Chapter 8-9]
Genesereth, M., & Nilsson, N. (1987). Logical Foundations of Artificial Intelligence. Morgan Kaufmann.
Fitting, M. (1996). First-Order Logic and Automated Theorem Proving. Springer.
Classic Papers¶
Robinson, J. A. (1965). A machine-oriented logic based on the resolution principle. Journal of the ACM, 12(1), 23-41.
Herbrand, J. (1930). Investigations in proof theory. From Frege to Gödel, 525-581.
Robinson, J. A. (1965). Automatic deduction with hyper-resolution. International Journal of Computer Mathematics, 1(3), 227-234.
Automated Reasoning¶
Chang, C. L., & Lee, R. C. T. (1973). Symbolic Logic and Mechanical Theorem Proving. Academic Press.
Wos, L., et al. (1984). Automated Reasoning: Introduction and Applications. Prentice-Hall.
Bundy, A. (1983). The Computer Modelling of Mathematical Reasoning. Academic Press.
Online Resources¶
TPTP Problem Library - Thousands of FOL problems
Prover9/Mace4 - Automated theorem prover