Inference in first-order logic
Chapter 9
Chapter 9 1
Outline
♦ Reducing first-order inference to propositional inference
♦ Unification
♦ Generalized Modus Ponens
♦ Forward and backward chaining
♦ Logic programming
♦ Resolution
Chapter 9 2
A brief history of reasoning
450b.c. Stoics propositional logic, inference (maybe)
322b.c. Aristotle “syllogisms” (inference rules), quantifiers
1565 Cardano probability theory (propositional logic + uncertainty)
1847 Boole propositional logic (again)
1879 Frege first-order logic
1922 Wittgenstein proof by truth tables
1930 Gödel ∃ complete algorithm for FOL
1930 Herbrand complete algorithm for FOL (reduce to propositional)
1931 Gödel ¬∃ complete algorithm for arithmetic
1960 Davis/Putnam “practical” algorithm for propositional logic
1965 Robinson “practical” algorithm for FOL—resolution
Chapter 9 3
Frege’s notation for FOL
Frege’s notation was quite different from ours:
Chapter 9 4
Universal instantiation (UI)
Every instantiation of a universally quantified sentence is entailed by it:
∀v α
Subst({v = g}, α)
for any variable v and ground term g
E.g., ∀ x King(x) ∧ Greedy(x) ⇒ Evil(x) yields
King(John) ∧ Greedy(John) ⇒ Evil(John)
King(Richard) ∧ Greedy(Richard) ⇒ Evil(Richard)
King(F ather(John)) ∧ Greedy(F ather(John)) ⇒ Evil(F ather(John))
..
Chapter 9 5
Existential instantiation (EI)
For any sentence α, variable v, and constant symbol k
that does not appear elsewhere in the knowledge base:
∃v α
Subst({v = k}, α)
E.g., ∃ x Crown(x) ∧ OnHead(x, John) yields
Crown(C1) ∧ OnHead(C1, John)
where C1 is a new constant symbol (i.e., doesn’t already appear somewhere)
In words:
If we know that there is a crown on John’s head,
then we can call that crown C1
C1 is called a Skolem constant
Chapter 9 6
Existential instantiation contd.
UI can be applied several times to add new sentences
the new KB is logically equivalent to the old
EI can be applied once to replace the existential sentence
the new KB is not equivalent to the old,
but is satisfiable iff the old KB was satisfiable
Mathematicians use these techniques informally every day.
Example: proofs involving limits
Given limx→5 f (x) = 2, i.e.,
∀ > 0 ∃δ > 0 ∀x s.t. |x − 5| < δ, |f (x) − 2| < .
Let be any number > 0.
Then ∃δ > 0 ∀x s.t. |x − 5| < δ, |f (x) − 2| < .
Let δ1 > 0 be such that ∀x s.t. |x − 5| < δ, |f (x) − 2| < .
Let x be any number such that |x − 5| < δ1. Then |f (x) − 2| < .
...
Chapter 9 7
Reduction to propositional inference
Suppose the KB contains just the following:
∀ x King(x) ∧ Greedy(x) ⇒ Evil(x)
King(John)
Greedy(John)
Brother(Richard, John)
Instantiating the universal sentence in all possible ways, we have
King(John) ∧ Greedy(John) ⇒ Evil(John)
King(Richard) ∧ Greedy(Richard) ⇒ Evil(Richard)
King(John)
Greedy(John)
Brother(Richard, John)
The new KB is propositionalized: proposition symbols are
King(John), Greedy(John), Evil(John), King(Richard) etc.
Chapter 9 8
Reduction contd.
Claim: a ground sentence∗ is entailed by new KB iff entailed by original KB
Claim: every FOL KB can be propositionalized so as to preserve entailment
Idea: propositionalize KB and query, apply resolution, return result
Problem: with function symbols, there are infinitely many ground terms,
e.g., F ather(F ather(F ather(John)))
Theorem: Herbrand (1930). If a sentence α is entailed by an FOL KB,
it is entailed by a finite subset of the propositional KB
Idea: For n = 0 to ∞ do
create a propositional KB by instantiating with depth-n terms
see if α is entailed by this KB
Problem: works if α is entailed, loops if α is not entailed
Theorem: Turing (1936), Church (1936), entailment in FOL is semidecidable
Chapter 9 9
Problems with propositionalization
Propositionalization can lots of irrelevant sentences.
E.g., suppose we are given
∀ x King(x) ∧ Greedy(x) ⇒ Evil(x)
King(John)
∀ y Greedy(y)
Brother(Richard, John)
Daughter(John, Joanna)
To prove Evil(John), we first use propositionalization to get Greedy(John)
But propositionalization also produces Greedy(Richard) and Greedy(Joanna)
With p k-ary predicates and n constants, there are p · nk instantiations
With function symbols, there are infinitely many instantiations!
c, f (c), f (f (c)), . . .
Chapter 9 10
Unification
We can get the inference immediately if we can find a substitution θ
such that King(x) and Greedy(x) match King(John) and Greedy(y)
θ = {x = John, y = John} works
A unifier for α and β is a substitution θ such that αθ = βθ
α and β are unifiable if such a θ exists
p q θ
Knows(John, x) Knows(John, Jane)
Knows(John, x) Knows(y, Joanna)
Knows(John, x) Knows(y, M other(y))
Knows(John, x) Knows(x, Joanna)
Chapter 9 11
Unification
We can get the inference immediately if we can find a substitution θ
such that King(x) and Greedy(x) match King(John) and Greedy(y)
θ = {x = John, y = John} works
A unifier for α and β is a substitution θ such that αθ = βθ
α and β are unifiable if such a θ exists
p q θ
Knows(John, x) Knows(John, Jane) {x = Jane}
Knows(John, x) Knows(y, Joanna)
Knows(John, x) Knows(y, M other(y))
Knows(John, x) Knows(x, Joanna)
Chapter 9 12
Unification
We can get the inference immediately if we can find a substitution θ
such that King(x) and Greedy(x) match King(John) and Greedy(y)
θ = {x = John, y = John} works
A unifier for α and β is a substitution θ such that αθ = βθ
α and β are unifiable if such a θ exists
p q θ
Knows(John, x) Knows(John, Jane) {x = Jane}
Knows(John, x) Knows(y, Joanna) {x = Joanna, y = John}
Knows(John, x) Knows(y, M other(y))
Knows(John, x) Knows(x, Joanna)
Chapter 9 13
Unification
We can get the inference immediately if we can find a substitution θ
such that King(x) and Greedy(x) match King(John) and Greedy(y)
θ = {x = John, y = John} works
A unifier for α and β is a substitution θ such that αθ = βθ
α and β are unifiable if such a θ exists
p q θ
Knows(John, x) Knows(John, Jane) {x = Jane}
Knows(John, x) Knows(y, Joanna) {x = Joanna, y = John}
Knows(John, x) Knows(y, M other(y)) {y = John, x = M other(John)}
Knows(John, x) Knows(x, Joanna)
Chapter 9 14
Unification
We can get the inference immediately if we can find a substitution θ
such that King(x) and Greedy(x) match King(John) and Greedy(y)
θ = {x = John, y = John} works
A unifier for α and β is a substitution θ such that αθ = βθ
α and β are unifiable if such a θ exists
p q θ
Knows(John, x) Knows(John, Jane) {x = Jane}
Knows(John, x) Knows(y, Joanna) {x = Joanna, y = John}
Knows(John, x) Knows(y, M other(y)) {y = John, x = M other(John)}
Knows(John, x) Knows(x, Joanna) f ail
Standardizing apart eliminates overlap of variables, e.g., Knows(z17, Joanna)
Chapter 9 15
Unification (continued)
A substitution θ1 is more general than a substitution θ2 if for every expression
α, αθ2 is a substitution instance of αθ1
{x = f ather(y)} is more general than {x = f ather(John)}
{x = f ather(y)} is more general than {x = f ather(f ather(z))}
{x = f ather(y)} is more general than {x = f ather(z)}, and vice versa
{x = f ather(y)} is more general than itself
A most general unifier (mgu) for α and β is a substitution θ such that
(1) θ is a unifier for α and β;
(2) for every unifier θ0 of α and β, θ is more general than θ0
e.g., if α = knows(w, f ather(x)) and β = knows(mother(y), y) then
one mgu is θ1 = {w = mother(f ather(x))), y = f ather(x)}
another is θ2 = {w = mother(f ather(v))), y = f ather(v), x = v}
If θ1 and θ2 are mgu’s for α and β, then they are identical except for renaming
of variables
Next slide: an algorithm for finding an mgu
Chapter 9 16
Unification algorithm for expressions in Lisp notation
procedure unify(e1,e2)
if either e1 or e2 is a Lisp atom then return unify1(e1,e2)
h1 := first(e1); t1 := rest(e1)
h2 := first(e2); t2 := rest(e2)
θ1 := unify(h1,h2); if θ1 = FAIL then return FAIL
t01 := t1θ1; t02 := t2θ1
θ2 := unify(t01,t02); if θ2 = FAIL then return FAIL
return θ1θ2
procedure unify1(e1,e2)
if necessary, interchange e1 and e2 so that e1 is an atom
if e1 = e2 then return {}
if e1 is a variable symbol then
if e1 occurs in e2 then return FAIL ;; the “occurs check”
return the substitution {e1 = e2}
if e2 is a variable then return the substitution {e2 = e1}
return FAIL
Chapter 9 17
Generalized Modus Ponens (GMP)
p10, p20, . . . , pn0, (p1 ∧ p2 ∧ . . . ∧ pn ⇒ q)
where pi0θ = piθ for all i
qθ
p10 is King(John) p1 is King(x)
p20 is Greedy(y) p2 is Greedy(x)
θ is {x = John, y = John} q is Evil(x)
qθ is Evil(John)
GMP used with KB of definite clauses (exactly one positive literal)
All variables assumed universally quantified
Chapter 9 18
Soundness of GMP
Need to show that
p10, . . . , pn0, (p1 ∧ . . . ∧ pn ⇒ q) |= qθ
provided that pi0θ = piθ for all i
Lemma: For any definite clause p, we have p |= pθ by UI
1. (p1 ∧ . . . ∧ pn ⇒ q) |= (p1 ∧ . . . ∧ pn ⇒ q)θ = (p1θ ∧ . . . ∧ pnθ ⇒ qθ)
2. p10, . . . , pn0 |= p10 ∧ . . . ∧ pn0 |= p10θ ∧ . . . ∧ pn0θ
3. From 1 and 2, qθ follows by ordinary Modus Ponens
Chapter 9 19
Example knowledge base
The law says that it is a crime for an American to sell weapons to hostile
nations. The country Nono, an enemy of America, has some missiles, and
all of its missiles were sold to it by Colonel West, who is American.
Prove that Col. West is a criminal
Chapter 9 20
Example knowledge base contd.
. . . it is a crime for an American to sell weapons to hostile nations:
Chapter 9 21
Example knowledge base contd.
. . . it is a crime for an American to sell weapons to hostile nations:
American(x)∧W eapon(y)∧Sells(x, y, z)∧Hostile(z) ⇒ Criminal(x)
Nono . . . has some missiles
Chapter 9 22
Example knowledge base contd.
. . . it is a crime for an American to sell weapons to hostile nations:
American(x)∧W eapon(y)∧Sells(x, y, z)∧Hostile(z) ⇒ Criminal(x)
Nono . . . has some missiles, i.e., ∃ x Owns(N ono, x) ∧ M issile(x):
Owns(N ono, M1) and M issile(M1)
. . . all of its missiles were sold to it by Colonel West
Chapter 9 23
Example knowledge base contd.
. . . it is a crime for an American to sell weapons to hostile nations:
American(x)∧W eapon(y)∧Sells(x, y, z)∧Hostile(z) ⇒ Criminal(x)
Nono . . . has some missiles, i.e., ∃ x Owns(N ono, x) ∧ M issile(x):
Owns(N ono, M1) and M issile(M1)
. . . all of its missiles were sold to it by Colonel West
∀ x M issile(x) ∧ Owns(N ono, x) ⇒ Sells(W est, x, N ono)
Missiles are weapons:
Chapter 9 24
Example knowledge base contd.
. . . it is a crime for an American to sell weapons to hostile nations:
American(x)∧W eapon(y)∧Sells(x, y, z)∧Hostile(z) ⇒ Criminal(x)
Nono . . . has some missiles, i.e., ∃ x Owns(N ono, x) ∧ M issile(x):
Owns(N ono, M1) and M issile(M1)
. . . all of its missiles were sold to it by Colonel West
∀ x M issile(x) ∧ Owns(N ono, x) ⇒ Sells(W est, x, N ono)
Missiles are weapons:
M issile(x) ⇒ W eapon(x)
An enemy of America counts as “hostile”:
Chapter 9 25
Example knowledge base contd.
. . . it is a crime for an American to sell weapons to hostile nations:
American(x)∧W eapon(y)∧Sells(x, y, z)∧Hostile(z) ⇒ Criminal(x)
Nono . . . has some missiles, i.e., ∃ x Owns(N ono, x) ∧ M issile(x):
Owns(N ono, M1) and M issile(M1)
. . . all of its missiles were sold to it by Colonel West
∀ x M issile(x) ∧ Owns(N ono, x) ⇒ Sells(W est, x, N ono)
Missiles are weapons:
M issile(x) ⇒ W eapon(x)
An enemy of America counts as “hostile”:
Enemy(x, America) ⇒ Hostile(x)
West, who is American . . .
American(W est)
The country Nono, an enemy of America . . .
Enemy(N ono, America)
Chapter 9 26
Forward chaining algorithm
function FOL-FC-Ask(KB, α) returns a substitution or false
repeat until new is empty
new ← { }
for each sentence r in KB do
( p 1 ∧ . . . ∧ p n ⇒ q) ← Standardize-Apart(r)
for each θ such that (p 1 ∧ . . . ∧ p n)θ = (p 01 ∧ . . . ∧ p 0n)θ
for some p 01, . . . , p 0n in KB
q 0 ← Subst(θ, q)
if q 0 is not a renaming of a sentence already in KB or new then do
add q 0 to new
φ ← Unify(q 0, α)
if φ is not fail then return φ
add new to KB
return false
Chapter 9 27
Forward chaining proof
American(West) Missile(M1) Owns(Nono,M1) Enemy(Nono,America)
Chapter 9 28
Forward chaining proof
Weapon(M1) Sells(West,M1,Nono) Hostile(Nono)
American(West) Missile(M1) Owns(Nono,M1) Enemy(Nono,America)
Chapter 9 29
Forward chaining proof
Criminal(West)
Weapon(M1) Sells(West,M1,Nono) Hostile(Nono)
American(West) Missile(M1) Owns(Nono,M1) Enemy(Nono,America)
Chapter 9 30
Properties of forward chaining
Sound and complete for first-order definite clauses
(proof similar to propositional proof)
Datalog = first-order definite clauses + no functions (e.g., crime KB)
FC terminates for Datalog in poly iterations: at most p · nk literals
May not terminate in general if α is not entailed
This is unavoidable: entailment with definite clauses is semidecidable
Chapter 9 31
Efficiency of forward chaining
Simple observation: no need to match a rule on iteration k
if a premise wasn’t added on iteration k − 1
⇒ match each rule whose premise contains a newly added literal
Matching itself can be expensive
♦ Database indexing allows O(1) retrieval of known facts
e.g., query M issile(x) retrieves M issile(M1)
♦ But matching conjunctive premises against known facts is NP-hard
Forward chaining is widely used in deductive databases and expert systems
Chapter 9 32
Hard matching example
Diff(wa, nt) ∧ Diff(wa, sa) ∧
Diff(nt, q) ∧ Diff(nt, sa) ∧
NT
Q
Diff(q, nsw) ∧ Diff(q, sa) ∧
WA Diff(nsw, v) ∧ Diff(nsw, sa) ∧
SA NSW Diff(v, sa) ⇒ Colorable()
V
Victoria Diff(Red, Blue) Diff(Red, Green)
Diff(Green, Red) Diff(Green, Blue)
T
Diff(Blue, Red) Diff(Blue, Green)
Colorable() is inferred iff the CSP has a solution
CSPs include 3SAT as a special case, hence matching is NP-hard
Chapter 9 33
Backward chaining algorithm
function FOL-BC-Ask(KB, goals, θ) returns a set of substitutions
inputs: KB, a knowledge base
goals, a list of conjuncts forming a query (θ already applied)
θ, the current substitution, initially the empty substitution { }
local variables: answers, a set of substitutions, initially empty
if goals is empty then return {θ}
q 0 ← Subst(θ, First(goals))
for each sentence r in KB
where Standardize-Apart(r) = ( p 1 ∧ . . . ∧ p n ⇒ q)
and θ0 ← Unify(q, q 0) succeeds
new goals ← [ p 1, . . . , p n|Rest(goals)]
answers ← FOL-BC-Ask(KB, new goals, Compose(θ0, θ)) ∪ answers
return answers
Chapter 9 34
Backward chaining example
Criminal(West)
Chapter 9 35
Backward chaining example
Criminal(West) {x/West}
American(x) Weapon(y) Sells(x,y,z) Hostile(z)
Chapter 9 36
Backward chaining example
Criminal(West) {x/West}
American(West) Weapon(y) Sells(x,y,z) Hostile(z)
{}
Chapter 9 37
Backward chaining example
Criminal(West) {x/West}
American(West) Weapon(y) Sells(West,M1,z)
Sells(x,y,z) Hostile(Nono)
Hostile(z)
{}
Missile(y)
Chapter 9 38
Backward chaining example
Criminal(West) {x/West, y/M1}
American(West) Weapon(y) Sells(West,M1,z)
Sells(x,y,z) Hostile(Nono)
Hostile(z)
{}
Missile(y)
{ y/M1 }
Chapter 9 39
Backward chaining example
Criminal(West) {x/West, y/M1, z/Nono}
American(West) Weapon(y) Sells(West,M1,z) Hostile(z)
{} { z/Nono }
Missile(y) Missile(M1) Owns(Nono,M1)
{ y/M1 }
Chapter 9 40
Backward chaining example
Criminal(West) {x/West, y/M1, z/Nono}
American(West) Weapon(y) Sells(West,M1,z) Hostile(Nono)
{} { z/Nono }
Missile(y) Missile(M1) Owns(Nono,M1) Enemy(Nono,America)
{ y/M1 } {} {} {}
Chapter 9 41
Properties of backward chaining
Depth-first recursive proof search: space is linear in size of proof
Incomplete due to infinite loops
♦ partial fix: check current goal against every goal on stack
Inefficient due to repeated subgoals (both success and failure)
♦ fix using caching of previous results (extra space!)
Widely used (without improvements!) for logic programming
Chapter 9 42
Logic programming
Sound bite: computation as inference on logical KBs
Logic programming Ordinary programming
1. Identify problem Identify problem
2. Assemble information Assemble information
3. Tea break Figure out solution
4. Encode information in KB Program solution
5. Encode problem instance as facts Encode problem instance as data
6. Ask queries Apply program to data
7. Find false facts Debug procedural errors
Chapter 9 43
Prolog systems
Basis: backward chaining with Horn clauses + bells & whistles
Widely used in Europe, Japan (basis of 5th Generation project)
Compilation techniques ⇒ approaching a billion LIPS
Program = set of clauses of the form head :- literal1, . . . literaln.
criminal(X) :- american(X), weapon(Y), sells(X,Y,Z), hostile(Z).
Efficient unification by open coding
Efficient retrieval of matching clauses by direct linking
Depth-first, left-to-right backward chaining
Built-in predicates for arithmetic etc., e.g., X is Y*Z+3
Closed-world assumption (“negation as failure”)
e.g., given alive(X) :- not dead(X).
alive(joe) succeeds if dead(joe) fails
Chapter 9 44
Prolog examples
Depth-first search from a start state X:
dfs(X) :- goal(X).
dfs(X) :- successor(X,S),dfs(S).
No need to loop over S: successor succeeds for each
Appending two lists to produce a third:
append([],Y,Y).
append([X|L],Y,[X|Z]) :- append(L,Y,Z).
query: append(A,B,[1,2]) ?
answers: A=[] B=[1,2]
A=[1] B=[2]
A=[1,2] B=[]
Chapter 9 45
Resolution: brief summary
Full first-order version:
`1 ∨ · · · ∨ `k , m 1 ∨ · · · ∨ mn
(`1 ∨ · · · ∨ `i−1 ∨ `i+1 ∨ · · · ∨ `k ∨ m1 ∨ · · · ∨ mj−1 ∨ mj+1 ∨ · · · ∨ mn)θ
where Unify(`i, ¬mj ) = θ.
For example,
¬Rich(x) ∨ U nhappy(x)
Rich(Ken)
U nhappy(Ken)
with θ = {x = Ken}
Apply resolution steps to CN F (KB ∧ ¬α); complete for FOL
Chapter 9 46
Conversion to CNF
Everyone who loves all animals is loved by someone:
∀ x [∀ y Animal(y) ⇒ Loves(x, y)] ⇒ [∃ y Loves(y, x)]
1. Eliminate biconditionals and implications
∀ x [¬∀ y ¬Animal(y) ∨ Loves(x, y)] ∨ [∃ y Loves(y, x)]
2. Move ¬ inwards: ¬∀ x, p ≡ ∃ x ¬p, ¬∃ x, p ≡ ∀ x ¬p:
∀ x [∃ y ¬(¬Animal(y) ∨ Loves(x, y))] ∨ [∃ y Loves(y, x)]
∀ x [∃ y ¬¬Animal(y) ∧ ¬Loves(x, y)] ∨ [∃ y Loves(y, x)]
∀ x [∃ y Animal(y) ∧ ¬Loves(x, y)] ∨ [∃ y Loves(y, x)]
Chapter 9 47
Conversion to CNF contd.
3. Standardize variables: each quantifier should use a different one
∀ x [∃ y Animal(y) ∧ ¬Loves(x, y)] ∨ [∃ z Loves(z, x)]
4. Skolemize: a more general form of existential instantiation.
Each existential variable is replaced by a Skolem function
of the enclosing universally quantified variables:
∀ x [Animal(F (x)) ∧ ¬Loves(x, F (x))] ∨ Loves(G(x), x)
5. Drop universal quantifiers:
[Animal(F (x)) ∧ ¬Loves(x, F (x))] ∨ Loves(G(x), x)
6. Distribute ∧ over ∨:
[Animal(F (x)) ∨ Loves(G(x), x)] ∧ [¬Loves(x, F (x)) ∨ Loves(G(x), x)]
Chapter 9 48
Resolution proof: definite clauses
American(x) Weapon(y) Sells(x,y,z) Hostile(z) Criminal(x) Criminal(West)
>
>
>
>
L
L
American(West) American(West) Weapon(y) Sells(West,y,z) Hostile(z)
>
>
>
L
L
Missile(x) Weapon(x) Weapon(y) Sells(West,y,z) Hostile(z)
>
>
>
L
L
Missile(M1) Missile(y) Sells(West,y,z) Hostile(z)
>
>
L
L
Missile(x) Owns(Nono,x) Sells(West,x,Nono) Sells(West,M1,z) Hostile(z)
>
>
>
L
L
Missile(M1) Missile(M1) Owns(Nono,M1) Hostile(Nono)
>
>
L
L
Owns(Nono,M1) Owns(Nono,M1) Hostile(Nono)
>
L
L
Enemy(x,America) Hostile(x) Hostile(Nono)
>
L
Enemy(Nono,America) Enemy(Nono,America)
Chapter 9 49