Existential instantiation (EI)

For any sentence α, variable v, and constant symbol k that does not appear elsewhere in the knowledge base: Inference in first-order logic ∃ v α Subst({v/k}, α) E.g., ∃ x Crown(x) ∧ OnHead(x, John) yields

Chapter 9 Crown(C1) ∧ OnHead(C1, John) provided C1 is a new constant symbol, called a Skolem constant Another example: from ∃ x d(xy)/dy = xy we obtain d(ey)/dy = ey provided e is a new constant symbol

Chapter 9 1 Chapter 9 4

Outline Existential instantiation contd.

♦ Reducing first-order inference to propositional inference UI can be applied several times to add new sentences; the new KB is logically equivalent to the old ♦ Unification EI can be applied once to replace the existential sentence; ♦ Generalized the new KB is not equivalent to the old, ♦ Forward and backward chaining but is satisfiable iff the old KB was satisfiable ♦ Resolution

Chapter 9 2 Chapter 9 5

Universal instantiation (UI) Reduction to propositional inference

Every instantiation of a universally quantified sentence is entailed by it: Suppose the KB contains just the following: ∀ v α ∀ x King(x) ∧ Greedy(x) ⇒ Evil(x) Subst({v/g}, α) King(John) Greedy(John) for any variable v and ground term g Brother(Richard,John) E.g., ∀ x King(x) ∧ Greedy(x) ⇒ Evil(x) yields Instantiating the universal sentence in all possible ways, we have King(John) ∧ Greedy(John) ⇒ Evil(John) King(John) ∧ Greedy(John) ⇒ Evil(John) King(Richard) ∧ Greedy(Richard) ⇒ Evil(Richard) King(Richard) ∧ Greedy(Richard) ⇒ Evil(Richard) King(F ather(John)) ∧ Greedy(F ather(John)) ⇒ Evil(F ather(John)) 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 3 Chapter 9 6 Reduction contd. Unification

Claim: a ground sentence∗ is entailed by new KB iff entailed by original KB We can get the inference immediately if we can find a θ such that King(x) and Greedy(x) match King(John) and Greedy(y) Claim: every FOL KB can be propositionalized so as to preserve entailment θ = {x/John, y/John} works Idea: propositionalize KB and query, apply resolution, return result Unify(α, β)= θ if αθ = βθ 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, p q θ it is entailed by a finite subset of the propositional KB Knows(John, x) Knows(John, Jane) {x/Jane} Knows(John, x) Knows(y,OJ) Idea: For n = 0 to ∞ do Knows(John, x) Knows(y, Mother(y)) create a propositional KB by instantiating with depth-n terms Knows(John, x) Knows(x,OJ) 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 7 Chapter 9 10

Problems with propositionalization Unification

Propositionalization seems to generate lots of irrelevant sentences. We can get the inference immediately if we can find a substitution θ E.g., from such that King(x) and Greedy(x) match King(John) and Greedy(y) ∀ x King(x) ∧ Greedy(x) ⇒ Evil(x) θ = {x/John, y/John} works King(John) Unify ∀ y Greedy(y) (α, β)= θ if αθ = βθ Brother(Richard,John) it seems obvious that Evil(John), but propositionalization produces lots of p q θ facts such as Greedy(Richard) that are irrelevant Knows(John, x) Knows(John, Jane) {x/Jane} Knows(John, x) Knows(y,OJ) {x/OJ, y/John} Knows(John, x) Knows(y, Mother(y)) Knows(John, x) Knows(x,OJ)

Chapter 9 8 Chapter 9 11

Unification Unification

We can get the inference immediately if we can find a substitution θ 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) such that King(x) and Greedy(x) match King(John) and Greedy(y) θ = {x/John, y/John} works θ = {x/John, y/John} works Unify(α, β)= θ if αθ = βθ Unify(α, β)= θ if αθ = βθ

p q θ p q θ Knows(John, x) Knows(John, Jane) Knows(John, x) Knows(John, Jane) {x/Jane} Knows(John, x) Knows(y,OJ) Knows(John, x) Knows(y,OJ) {x/OJ, y/John} Knows(John, x) Knows(y, Mother(y)) Knows(John, x) Knows(y, Mother(y)) {y/John, x/Mother(John)} Knows(John, x) Knows(x,OJ) Knows(John, x) Knows(x,OJ)

Chapter 9 9 Chapter 9 12 Unification Example knowledge base contd.

We can get the inference immediately if we can find a substitution θ ... it is a crime for an American to sell weapons to hostile nations: such that King(x) and Greedy(x) match King(John) and Greedy(y) θ = {x/John, y/John} works Unify(α, β)= θ if αθ = βθ

p q θ Knows(John, x) Knows(John, Jane) {x/Jane} Knows(John, x) Knows(y,OJ) {x/OJ, y/John} Knows(John, x) Knows(y, Mother(y)) {y/John, x/Mother(John)} Knows(John, x) Knows(x,OJ) fail

Standardizing apart eliminates overlap of variables, e.g., Knows(z17, OJ)

Chapter 9 13 Chapter 9 16

Generalized Modus Ponens (GMP) Example knowledge base contd.

... it is a crime for an American to sell weapons to hostile nations: ′ ′ ′ p1 , p2 , ..., p , (p1 ∧ p2 ∧ ... ∧ p ⇒ q) American(x)∧W eapon(y)∧Sells(x,y,z)∧Hostile(z) ⇒ Criminal(x) n n where p ′θ = p θ for all i qθ i i Nono ... has some missiles

′ p1 is King(John) p1 is King(x) ′ p2 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 14 Chapter 9 17

Example knowledge base Example knowledge base contd.

The law says that it is a crime for an American to sell weapons to hostile ... it is a crime for an American to sell weapons to hostile nations: nations. The country Nono, an enemy of America, has some missiles, and American(x)∧W eapon(y)∧Sells(x,y,z)∧Hostile(z) ⇒ Criminal(x) all of its missiles were sold to it by Colonel West, who is American. Nono ... has some missiles, i.e., ∃ x Owns(Nono, x) ∧ Missile(x): Owns(Nono, M1) and Missile(M1) Prove that Col. West is a criminal ... all of its missiles were sold to it by Colonel West

Chapter 9 15 Chapter 9 18 Example knowledge base contd. Forward chaining algorithm

... it is a crime for an American to sell weapons to hostile nations: function FOL-FC-Ask(KB, α) returns a substitution or false American(x)∧W eapon(y)∧Sells(x,y,z)∧Hostile(z) ⇒ Criminal(x) Nono ... has some missiles, i.e., ∃ x Owns(Nono, x) ∧ Missile(x): repeat until new is empty new ←{} Owns(Nono, M1) and Missile(M1) for each sentence r in KB do ... all of its missiles were sold to it by Colonel West ( p1 ∧ . . . ∧ pn ⇒ q) ← Standardize-Apart(r) ′ ′ Missile(x) ∧ Owns(Nono, x) ⇒ Sells(W est, x, Nono) for each θ such that (p1 ∧ . . . ∧ pn)θ = (p1 ∧ . . . ∧ pn)θ ′ ′ Missiles are weapons: for some p1,..., pn in KB q′ ← Subst(θ, q) if q′ is not a renaming of a sentence already in KB or new then do add q′ to new φ ← Unify(q ′, α) if φ is not fail then return φ add new to KB return false

Chapter 9 19 Chapter 9 22

Example knowledge base contd. Forward chaining proof

... 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(Nono, x) ∧ Missile(x): Owns(Nono, M1) and Missile(M1) ... all of its missiles were sold to it by Colonel West Missile(x) ∧ Owns(Nono, x) ⇒ Sells(W est, x, Nono) Missiles are weapons: Missile(x) ⇒ W eapon(x) An enemy of America counts as “hostile”:

American(West) Missile(M1) Owns(Nono,M1) Enemy(Nono,America)

Chapter 9 20 Chapter 9 23

Example knowledge base contd. Forward chaining proof

... 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(Nono, x) ∧ Missile(x): Owns(Nono, M1) and Missile(M1) ... all of its missiles were sold to it by Colonel West Missile(x) ∧ Owns(Nono, x) ⇒ Sells(W est, x, Nono) Weapon(M1) Sells(West,M1,Nono) Hostile(Nono) Missiles are weapons: Missile(x) ⇒ W eapon(x) An enemy of America counts as “hostile”: Enemy(x, America) ⇒ Hostile(x) West, who is American ... American(West) Missile(M1) Owns(Nono,M1) Enemy(Nono,America) American(W est) The country Nono, an enemy of America ... Enemy(Nono, America)

Chapter 9 21 Chapter 9 24 Forward chaining proof Backward chaining example

Criminal(West) Criminal(West)

Weapon(M1) Sells(West,M1,Nono) Hostile(Nono)

American(West) Missile(M1) Owns(Nono,M1) Enemy(Nono,America)

Chapter 9 25 Chapter 9 28

Properties of forward chaining Backward chaining example

Sound and complete for first-order definite clauses Criminal(West) {x/West} (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 (with functions) if α is not entailed American(x) Weapon(y) Sells(x,y,z) Hostile(z) This is unavoidable: entailment with definite clauses is semidecidable

Chapter 9 26 Chapter 9 29

Backward chaining algorithm Backward chaining example

function FOL-BC-Ask(KB, goals, θ) returns a set of substitutions Criminal(West) {x/West} 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 {θ} American(West) Weapon(y) Sells(x,y,z) Hostile(z) ′ Subst First q ← (θ, (goals)) { } for each sentence r in KB where Standardize-Apart(r) = ( p1 ∧ . . . ∧ pn ⇒ q) and θ′ ← Unify(q, q ′) succeeds new goals ← [ p1,..., pn|Rest(goals)] answers ← FOL-BC-Ask(KB, new goals, Compose(θ′, θ)) ∪ answers return answers

Chapter 9 27 Chapter 9 30 Backward chaining example Backward chaining example

Criminal(West) {x/West} Criminal(West) {x/West, y/M1, z/Nono}

American(West) Weapon(y) Sells(West,M1,z)Sells(x,y,z) Hostile(Nono)Hostile(z) American(West) Weapon(y) Sells(West,M1,z) Hostile(Nono) { } { } {} z/Nono

Missile(y) Missile(y) Missile(M1) Owns(Nono,M1) Enemy(Nono,America) {} y/M1 { } { } { }

Chapter 9 31 Chapter 9 34

Backward chaining example Properties of backward chaining

Criminal(West) {x/West, y/M1} Depth-first recursive proof search: space is linear in size of proof Incomplete due to infinite loops ⇒ fix by checking current goal against every goal on stack Inefficient due to repeated subgoals (both success and failure) American(West) Weapon(y) Sells(West,M1,z)Sells(x,y,z) Hostile(Nono)Hostile(z) ⇒ fix using caching of previous results (extra space!) { } Widely used (without improvements!) for logic programming

Missile(y) {} y/M1

Chapter 9 32 Chapter 9 35

Backward chaining example Logic programming

Criminal(West) {x/West, y/M1, z/Nono} 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 American(West) Weapon(y) Sells(West,M1,z) Hostile(z) 4. Encode information in KB Program solution { } {} z/Nono 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 Missile(y) Missile(M1) Owns(Nono,M1) Should be easier to debug Capital(NewYork,US) than x := x +2 ! {} y/M1

Chapter 9 33 Chapter 9 36 Prolog systems Conversion to CNF

Basis: backward chaining with Horn clauses + bells & whistles Everyone who loves all animals is loved by someone: Widely used in Europe, Japan (basis of 5th Generation project) ∀ x [∀ y Animal(y) ⇒ Loves(x, y)] ⇒ [∃ y Loves(y,x)] Compilation techniques ⇒ approaching a billion LIPS 1. Eliminate biconditionals and implications Program = set of clauses = head :- literal1, ... literaln. ∀ x [¬∀ y ¬Animal(y) ∨ Loves(x, y)] ∨ [∃ y Loves(y,x)] criminal(X) :- american(X), weapon(Y), sells(X,Y,Z), hostile(Z). 2. Move ¬ inwards: ¬∀ x,p ≡∃ x ¬p, ¬∃ x,p ≡∀ x ¬p: Depth-first, left-to-right backward chaining Built-in predicates for arithmetic etc., e.g., X is Y*Z+3 ∀ x [∃ y ¬(¬Animal(y) ∨ Loves(x, y))] ∨ [∃ y Loves(y,x)] Closed-world assumption (“negation as failure”) ∀ x [∃ y ¬¬Animal(y) ∧¬Loves(x, y)] ∨ [∃ y Loves(y,x)] e.g., given alive(X) :- not dead(X). ∀ x [∃ y Animal(y) ∧¬Loves(x, y)] ∨ [∃ y Loves(y,x)] alive(joe) succeeds if dead(joe) fails

Chapter 9 37 Chapter 9 40

Prolog examples Conversion to CNF contd.

Depth-first search from a start state X: 3. Standardize variables: each quantifier should use a different one dfs(X) :- goal(X). ∀ x [∃ y Animal(y) ∧¬Loves(x, y)] ∨ [∃ z Loves(z,x)] dfs(X) :- successor(X,S),dfs(S). 4. Skolemize: a more general form of existential instantiation. No need to loop over S: successor succeeds for each Each existential variable is replaced by a Skolem function of the enclosing universally quantified variables: Appending two lists to produce a third: ∀ x [Animal(F (x)) ∧¬Loves(x, F (x))] ∨ Loves(G(x),x) append([],Y,Y). append([X|L],Y,[X|Z]) :- append(L,Y,Z). 5. Drop universal quantifiers: [Animal(F (x)) ∧¬Loves(x, F (x))] ∨ Loves(G(x),x) query: append(A,B,[1,2]) ? answers: A=[] B=[1,2] 6. Distribute ∧ over ∨: A=[1] B=[2] A=[1,2] B=[] [Animal(F (x)) ∨ Loves(G(x),x)] ∧ [¬Loves(x, F (x)) ∨ Loves(G(x),x)]

Chapter 9 38 Chapter 9 41

Resolution: brief summary Resolution proof: definite clauses

> > > >

L L L L Full first-order version: American(x) Weapon(y) Sells(x,y,z) L Hostile(z) Criminal(x) Criminal(West)

1 1

> >

ℓ ∨···∨ ℓ , m ∨···∨ m >

L L L k n L American(West) American(West) Weapon(y) Sells(West,y,z) Hostile(z)

(ℓ1 ∨···∨ ℓi−1 ∨ ℓi+1 ∨···∨ ℓk ∨ m1 ∨···∨ mj−1 ∨ mj+1 ∨···∨ mn)θ

> > >

L L L L Missile(x) Weapon(x) Weapon(y) Sells(West,y,z) Hostile(z)

where Unify(ℓi, ¬mj)= θ.

> >

L L L

For example, Missile(M1) Missile(y) Sells(West,y,z) Hostile(z)

> > >

L L L ¬Rich(x) ∨ Unhappy(x) L Missile(x) Owns(Nono,x) Sells(West,x,Nono) Sells(West,M1,z) Hostile(z)

Rich(Ken)

> >

L L Missile(M1) Missile(M1) Owns(Nono,M1) L Hostile(Nono)

Unhappy(Ken)

>

L L

with θ = {x/Ken} Owns(Nono,M1) Owns(Nono,M1) Hostile(Nono)

> L Apply resolution steps to CNF (KB ∧¬α); complete for FOL L Enemy(x,America) Hostile(x) Hostile(Nono)

Enemy(Nono,America) Enemy(Nono,America)

Chapter 9 39 Chapter 9 42