Transcript Document

CS 8520: Artificial Intelligence
Logical Agents and First
Order Logic
Paula Matuszek
Spring 2013
CSC 8520 Spring 2013. Paula Matuszek
1
References
• These slides draw from a number of
sources, including:
– Hwee Tou Ng,
http://aima.eecs.berkeley.edu/slides-ppt
– Russell, http://aima.eecs.berkeley.edu/slidespdf
– Diagrams are based on AIMA.
– Matuszek,
http://www.cis.upenn.edu/~matuszek/General/
Logic/logic-for-AI.ppt
CSC 8520 Spring 2013. Paula Matuszek
2
Outline
•
•
•
•
•
•
Knowledge-based agents
Wumpus world
Logic in general - models and entailment
Propositional (Boolean) logic
First Order Logic
Inference rules and theorem proving
– forward chaining
– backward chaining
– resolution
CSC 8520 Spring 2013. Paula Matuszek
3
Knowledge bases
• Knowledge base = set of sentences in a formal language
• Declarative approach to building an agent (or other system):
– Tell it what it needs to know
• Then it can Ask itself what to do - answers should follow
from the KB
• Agents can be viewed at the knowledge level
– i.e., what they know, regardless of how implemented
• Or at the implementation level
– i.e., data structures in KB and algorithms that manipulate them
CSC 8520 Spring 2013. Paula Matuszek
4
A simple knowledge-based agent
CSC 8520 Spring 2013. Paula Matuszek
5
Simple Knowledge-Based Agent
• This agent tells the KB what it sees, asks the KB
what to do, tells the KB what it has done (or is
about to do).
• The agent must be able to:
–
–
–
–
–
Represent states, actions, etc.
Incorporate new percepts
Update internal representations of the world
Deduce hidden properties of the world
Deduce appropriate actions
CSC 8520 Spring 2013. Paula Matuszek
6
Wumpus World PEAS Description
• Performance measure
– gold +1000, death -1000
– -1 per step, -10 for using the arrow
• Environment
–
–
–
–
–
–
–
Squares adjacent to wumpus are smelly
Squares adjacent to pit are breezy
Glitter iff gold is in the same square
Shooting kills wumpus if you are facing it
Shooting uses up the only arrow
Grabbing picks up gold if in same square
Releasing drops the gold in same square
• Sensors: Stench, Breeze, Glitter, Bump, Scream
• Actuators: Left turn, Right turn, Forward, Grab, Release, Shoot
CSC 8520 Spring 2013. Paula Matuszek
7
Wumpus world characterization
• Fully Observable? No – only local perception
• Deterministic? Yes – outcomes exactly
specified
• Static? Yes – Wumpus and Pits do not move
• Discrete? Yes
• Episodic? No – sequential at the level of
actions
• Single-agent? Yes – The wumpus itself is
essentially a natural feature, not another agent
CSC 8520 Spring 2013. Paula Matuszek
8
More Environment
• Some versions of Wumpus have a bat (“You hear
squeaking”.) If you enter the room with a bat, it
carries you off and drops you elsewhere. Does that
change the environment description?
–
–
–
–
–
–
Fully Observable?
Deterministic?
Static?
Discrete?
Episodic?
Single-agent?
• What about the Wumpus game we played?
CSC 8520 Spring 2013. Paula Matuszek
9
Exploring a Wumpus world
Directly observed:
S: stench
B: breeze
G: glitter
A: agent
Inferred (mostly):
OK: safe square
P: pit
W: wumpus
CSC 8520 Spring 2013. Paula Matuszek
10
Exploring a wumpus world
In 1,1 we don’t get
B or S, so we know
1,2 and 2,1 are safe.
Move to 1,2.
In 1,2 we feel a
breeze.
CSC 8520 Spring 2013. Paula Matuszek
11
Exploring a wumpus world
In 1,2 we feel a
breeze. So we
know there is a
pit in 1,3 or 2,2.
CSC 8520 Spring 2013. Paula Matuszek
12
Exploring a wumpus world
So go back to
1,1 then to 2,1
where we smell
a stench.
CSC 8520 Spring 2013. Paula Matuszek
13
Exploring a wumpus world
Stench in 2,1, so
the wumpus is in
3,1 or 2,2.
We don't smell a
stench in 1,2, so
2,2 can't be the
wumpus, so 3,1
must be the
wumpus.
CSC 8520 Spring 2013. Paula Matuszek
S
W
W
14
Exploring a wumpus world
We don't feel a
breeze in 2,1, so 2,2
can't be a pit, so 1,3
must be a pit.
2,2 has neither pit
nor wumpus and is
therefore okay.
CSC 8520 Spring 2013. Paula Matuszek
15
Exploring a wumpus world
We move to 2,2. We
don’t get any
sensory input.
CSC 8520 Spring 2013. Paula Matuszek
16
Exploring a wumpus world
So we know that
2,3 and 3,2 are
ok.
CSC 8520 Spring 2013. Paula Matuszek
17
Exploring a wumpus world
Move to 3,2, where we
observe stench, breeze
and glitter!
We have found the
gold and won.
Do we want to kill the
wumpus? (hint: look at
the scoring function)
CSC 8520 Spring 2013. Paula Matuszek
18
Representing Knowledge
• The agent that solves the wumpus world
can most effectively be implemented by a
knowledge-based approach
• Need to represent states and actions, update
internal representations, deduce hidden
properties and appropriate actions
• Need a formal representation for the KB
• And a way to reason about that
representation
CSC 8520 Spring 2013. Paula Matuszek
19
Logic in general
• Logics are formal languages for representing information
such that conclusions can be drawn
• Syntax defines the sentences in the language
• Semantics define the "meaning" of sentences;
– i.e., define truth of a sentence in a world
• E.g., the language of arithmetic
• x+2 ≥ y is a sentence; x2+y > {} is not a sentence
– x+2 ≥ y is true iff the number x+2 is no less than the number y.
– x+2 ≥ y is true in a world where x = 7, y = 1
– x+2 ≥ y is false in a world where x = 0, y = 6
CSC 8520 Spring 2013. Paula Matuszek
20
What is logic?
• We can also think of logic as an “algebra”
for manipulating only two values: true (T)
and false (F)
• We will cover:
– Propositional logic--the simplest kind
– Predicate logic (a.k.a. predicate calculus)--an
extension of propositional logic
– Resolution theory--a general way of doing
proofs in predicate logic (and the basis for
Prolog’s inference).
CSC 8520 Spring 2013. Paula Matuszek
21
Propositional logic: Syntax
• Propositional logic is the simplest logic – illustrates basic
ideas
• The proposition symbols P1, P2 etc are sentences
– If S is a sentence, ¬S is a sentence (negation, not)
– If S1 and S2 are sentences, S1 ∧ S2 is a sentence (conjunction,
AND)
– If S1 and S2 are sentences, S1 ∨ S2 is a sentence (disjunction,
OR)
– If S1 and S2 are sentences, S1 ⇒ S2 is a sentence (implication,
IMPLIES)
– If S1 and S2 are sentences, S1 ⇔ S2 is a sentence
(biconditional)
CSC 8520 Spring 2013. Paula Matuszek
22
Propositional logic
• Propositional logic consists of:
– The logical values true and false (T and F)
– Propositions: “Sentences,” which
• Are atomic (that is, they must be treated as indivisible
units, with no internal structure), and
• Have a single logical value, either true or false
– Operators, both unary and binary; when applied to
logical values, yield logical values
• The usual operators are and, or, not, and implies
CSC 8520 Spring 2013. Paula Matuszek
23
Truth tables
• Logic, like arithmetic, has operators, which apply to
one, two, or more values (operands)
• A truth table lists the results for each possible
arrangement of operands
– Order is important: x op y may or may not give the
same result as y op x
• The rows in a truth table list all possible sequences
of truth values for n operands, and specify a result
for each sequence
– Hence, there are 2n rows in a truth table for n operands
CSC 8520 Spring 2013. Paula Matuszek
24
Unary operators
• There are four possible unary operators:
X

Constant T Constant F
Identity
¬X
T
T
F
T
F
F
T
F
F
T
Only the last of these (negation) is widely used (and has a
symbol,¬ ,for the operation
CSC 8520 Spring 2013. Paula Matuszek
25
Useful binary operators
• Here are the binary operators that are traditionally used:
AND
OR
X Y X Y X Y
T
T
F
F


T
F
T
F
T
F
F
F
T
T
T
F
IMPLIES
X Y
BICONDITIONAL
X Y
T
F
T
T
T
F
F
T
Notice in particular that material implication () only approximately
means the same as the English word “implies”
Any other binary operators can be constructed from a combination of
these (along with unary not, ¬)
CSC 8520 Spring 2013. Paula Matuszek
26
Logical expressions
• All logical expressions can be computed with some combination
of and ( ), or ( ), and not () operators
• For example, logical implication can be computed this way:

X
Y
X
X  Y
X Y
T
T
F
T
T
T
F
F
F
F
F
T
T
T
T
F
F
T
T
T
Notice that X  Y is equivalent to X Y
CSC 8520 Spring 2013. Paula Matuszek
27
Another example
• Exclusive or (xor) is true if exactly one of its operands is true
X Y X  Y
X  Y
(X Y) (X Y)
X
Y
T
T
F
F
F
F
F
F
T
F
F
T
F
T
T
T
F
T
T
F
T
F
T
T
F
F
T
T
F
F
F
F

X xor Y
Notice that (X Y) (X Y) is equivalent to X xor Y
CSC 8520 Spring 2013. Paula Matuszek
28
Worlds
• A world is a collection of prepositions and logical
expressions relating those prepositions
• Example:
– Propositions: JohnLovesMary, MaryIsFemale, MaryIsRich
– Expressions:
MaryIsFemale  MaryIsRich  JohnLovesMary
• A proposition “says something” about the world, but
since it is atomic (you can’t look inside it to see
component parts), propositions tend to be very
specialized and inflexible
CSC 8520 Spring 2013. Paula Matuszek
29
Models
A model is an assignment of a truth value to each proposition, for
example:
– JohnLovesMary: T, MaryIsFemale: T, MaryIsRich: F
• An expression is satisfiable if there is a model for which the expression
is true
– For example, the above model satisfies the expression
MaryIsFemale  MaryIsRich  JohnLovesMary
• An expression is valid if it is satisfied by every model
– This expression is not valid:
MaryIsFemale  MaryIsRich  JohnLovesMary
because it is not satisfied by this model:
JohnLovesMary: F, MaryIsFemale: T, MaryIsRich: T
– But this expression is valid:
MaryIsFemale  MaryIsRich  MaryIsFemale
CSC 8520 Spring 2013. Paula Matuszek
30
Wumpus world sentences
• Let Pi,j be true if there is a pit in [i, j].
• Let Bi,j be true if there is a breeze in [i, j].
• We have
– ¬ P1,1
– ¬B1,1
– B2,1
• "Pits cause breezes in adjacent squares"
– B1,1 ⇔
(P1,2 ∨ P2,1)
– B2,1 ⇔(P1,1 ∨ P2,2 ∨ P3,1)
CSC 8520 Spring 2013. Paula Matuszek
31
Grounding
• We must also be aware of the issue of
grounding: the connection between our KB
and the real world.
– Straightforward for Wumpus or adventure games
– More difficult when reasoning about real
situations
– Real problems seldom perfectly grounded,
because we must ignore details.
• Is the connection good enough to get useful answers?
CSC 8520 Spring 2013. Paula Matuszek
32
Entailment
• Entailment means that one thing follows from
another.
• Knowledge base KB entails sentence S if and
only if S is true in all worlds where KB is true
– E.g., the KB containing “the Wildcats won” and
“the Bearcats won” entails “Either the Wildcats
won or the Bearcats won”
– E.g., x+y = 4 entails 4 = x+y
– Entailment is a relationship between sentences
(i.e., syntax) that is based on semantics
CSC 8520 Spring 2013. Paula Matuszek
33
Entailment in the wumpus world
• Situation after detecting
nothing in [1,1], moving
right, breeze in [2,1]
• Consider possible models
for KB assuming only
pits: there are 3 Boolean
choices ⇒ 8 possible
models (ignoring sensory
data)
CSC 8520 Spring 2013. Paula Matuszek
34
Wumpus models for pits
CSC 8520 Spring 2013. Paula Matuszek
35
What Do We Know?
• We know:
– ¬ P1,1
– ¬B1,1
– B2,1
• There is a breeze in square 2,1
• One of our rules says:
reeze in square i in aaen square
• So wha is enaile by hese fas an his
rule?
CSC 8520 Spring 2013. Paula Matuszek
36
Wumpus models
• KB = wumpus-world rules + observations.
Only the three models in red are consistent
with KB.
CSC 8520 Spring 2013. Paula Matuszek
37
Wumpus Models
Consider S1: pit is not in 1.2.
S1 model is dotted yellow boundary. KB
is contained within S1 model, so KB
entails S1; in every model in which KB is
true, so is S1. We can conclude that the
pit is not in1.2.
Consider S2: pit is not in 2.2 .
S2 is dotted brown boundary. KB is not
within S1, so KB does not entail S2; nor
does S2 entail KB. So we can't conclude
anything about the truth of S2 given KB.
CSC 8520 Spring 2013. Paula Matuszek
38
Pros of propositional logic
•
•
•
•
•
Declarative
Allows partial/disjunctive/negated information
Compositional: meaning of B1,1 ∧ P1,2 is
derived from meaning of B1,1 and of P1,2
Meaning in propositional logic is contextindependent (unlike natural language, where
meaning depends on context)
Has a sound, complete inference procedure
CSC 8520 Spring 2013. Paula Matuszek
39
Cons of Propositional Logic
• Very limited expressive power (unlike
natural language)
• - Rapid proliferation of clauses.
– For instance, Wumpus KB contains "physics"
sentences for every single square E.g., cannot
say "pits cause breezes in adjacent
squares“ except by writing one sentence for
each square
CSC 8520 Spring 2013. Paula Matuszek
40
Inference
• Given some sentences how do we get to
new sentences?
• Inference!
CSC 8520 Spring 2013. Paula Matuszek
41
Inference
• KB entailsi S means sentence S can be derived
from KB by procedure i
• Soundness: i is sound if it derives only sentences
S that are entailed by KB
• Completeness: i is complete if it derives all
sentences S that are entailed by KB.
• Logic:
– Has a sound and complete inference procedure
– Which will answer any question whose answer
follows from what is known by the KB.
CSC 8520 Spring 2013. Paula Matuszek
42
Inference
• Simplest inference method is model-based
inference
– enumerate all possible models
– check to see if a proposed sentence is true in
every KB
– sound, complete, slow
CSC 8520 Spring 2013. Paula Matuszek
43
Inference by enumeration
• Depth-first enumeration of all models is sound and complete
•
n symbols, time complexity is O(2n), space complexity is O(n)
CSC 8520 Spring 2013. Paula Matuszek
44
Inference-based agents in the wumpus world
• A wumpus-world agent using propositional logic:
–
–
–
–
–
–
–
–
¬P1,1
¬W1,1
Bx,y ⇔ (Px,y+1 ∨ Px,y-1 ∨ Px+1,y ∨ Px-1,y)
Sx,y ⇔ (Wx,y+1 ∨ Wx,y-1 ∨ Wx+1,y ∨ Wx-1,y)
W1,1 ∨ W1,2 ∨ … ∨ W4,4
¬W1,1 ∨ ¬W1,2
¬W1,1 ∨ ¬W1,3
…
• ⇒ 64 distinct proposition symbols, 155 sentences
CSC 8520 Spring 2013. Paula Matuszek
45
CSC 8520 Spring 2013. Paula Matuszek
46
Inference by Theorem Proving
• Rather than enumerate all possible models,
we can apply inference rules to the current
sentences in the KB to derive new
sentences
• A proof consists of a chain of rules
beginning with known sentences (premises)
and ending with the sentence we want to
prove (conclusion)
CSC 8520 Spring 2013. Paula Matuszek
47
Inference rules in propositional logic
• Here are just a few of the rules you can apply
when reasoning in propositional logic:
From aima.eecs.berkeley.edu/slides-ppt, chs 7-9
CSC 8520 Spring 2013. Paula Matuszek
48
Implication elimination
• A particularly important rule allows you to get
rid of the implication operator, :
X Y a X  Y
Thurs AI class a AI class  Thurs
• The symbol a means “is logically
equivalent to”
• We will use this later on as a necessary tool for
simplifying logical expressions
CSC 8520 Spring 2013. Paula Matuszek
49
Conjunction elimination
• Another important rule for simplifying logical
expressions allows you to get rid of the
conjunction (and) operator,  :
• This rule simply says that if you have an and
operator at the top level of a fact (logical
expression), you can break the expression up into
two separate facts:
– Breeze2,1  Breeze1,2
• becomes:
– Breeze2,1
– Breeze1,2
CSC 8520 Spring 2013. Paula Matuszek
50
Inference by Theorem Proving
• Given:
– A set of sentences in the KB: the premises
– A sentence to be proved: the conclusion
– A set of sound rules
•
•
•
•
Apply a rule to an appropriate sentence
Add the resulting sentence to the KB
Stop if we have added the conclusion to the KB
Essentially a search problem
– Better than model-based if many models, short proofs
– But b is very large
CSC 8520 Spring 2013. Paula Matuszek
51
Summary
• Logical agents apply inference to a knowledge base to
derive new information and make decisions
• Basic concepts of logic:
–
–
–
–
–
–
syntax: formal structure of sentences
semantics: truth of sentences wrt models
entailment: necessary truth of one sentence given another
inference: deriving sentences from other sentences
soundness: derivations produce only entailed sentences
completeness: derivations can produce all entailed sentences
• Propositional logic lacks expressive power
CSC 8520 Spring 2013. Paula Matuszek
52
First-order logic (Predicate
Calculus)
• Whereas propositional logic assumes the
world contains facts,
• first-order logic (like natural language)
assumes the world contains
– Objects: people, houses, numbers, colors,
baseball games, wars, …
– Relations: red, round, prime, brother of, bigger
than, part of, comes between, …
– Functions: father of, best friend, one more than,
plus, …
CSC 8520 Spring 2013. Paula Matuszek
53
Syntax of FOL: Basic elements
•
•
•
•
•
•
•
•
All of propositional logic
Constants
kingJohn, 2, villanova,...
Predicates
brother, >,...
Functions
sqrt, leftLegOf,...
Variables
X, Y, A, B,...
Connectives ¬, ⇒, ∧, ∨, ⇔
Equality
=
Quantifiers ∀, ∃
CSC 8520 Spring 2013. Paula Matuszek
54
Constants, functions, and predicates
• A constant represents a “thing”--it has no truth value,
and it does not occur “bare” in a logical expression
– Examples: paulaMatuszek, 5, earth, goodIdea
• Given zero or more arguments, a function produces a
constant as its value:
– Examples: studentof(paulaMatuszek), add(2, 2),
thisPlanet()
• A predicate is like a function, but produces a truth
value
– Examples: aiInstructor(paulaMatuszek),
isPlanet(earth), greater(3, add(2, 2))
CSC 8520 Spring 2013. Paula Matuszek
55
Universal quantification
• The universal quantifier, , is read as “for each”
or “for every” or “for all”
– Example: x, x2 e 0 (for all x, x2 is greater than or equal to zero)
• Typically, is the main connective with :
x, at(x,villanova)  smart(x)
means “Everyone at Villanova is smart”
• Common mistake: using  as the main connective with :
x, at(x,villanova)  smart(x)
means “Everyone is at Villanova and everyone is smart”
• If there are no values satisfying the condition, the result is true
– Example: x, isPersonFromMars(x)  smart(x) is true
CSC 8520 Spring 2013. Paula Matuszek
56
Existential quantification
• The existential quantifier, , is read “for some” or
“there exists”
– E.G.:
x, x2 < 0 (there exists an x such that x2 < zero)
• Typically,  is the main connective with :
x, at(x,villanova)  smart(x)
means “There is someone who is at Villanova and is smart”
• Common mistake: using as the main connective with :
x, at(x,villanova)  smart(x)
This is true if there is someone at Villanova who is smart...
...but it is also true if there is someone who is not at Villanova
By the rules of material implication, the result of F T is T
CSC 8520 Spring 2013. Paula Matuszek
57
Properties of quantifiers
•
•
x y is the same as y x
x y is the same as y x
•
•
x y is not the same as y x
x y Loves(x,y)
– “There is a person who loves everyone in the world”
– More exactly: x y (person(x)  person(y)  Loves(x,y))
•
y x Loves(x,y)
– “Everyone in the world is loved by at least one person”
CSC 8520 Spring 2013. Paula Matuszek
58
More rules
• Quantifier duality: each can be expressed
using the other
x Likes(x,IceCream)  x Likes(x,IceCream)
x Likes(x,Broccoli)  x Likes(x,Broccoli)
• Two additional important rules:
 x, p(x)  x, p(x)
“If not every x satisfies p(x), then there exists a x that
does not satisfy p(x)”
 x, p(x)  x, p(x)
“If there does not exist an x that satisfies p(x), then
all x do not satisfy p(x)
CSC 8520 Spring 2013. Paula Matuszek
59
Terms and Atomic sentences
• A term is a logical expression that refers to an
object.
– book(andrew). Andrew’s book.
– textbook(8520). Textbook for 8520.
• An atomic sentence states a fact.
–
–
–
–
student(andrew).
student(andrew, aaula).
atudent(andrew, ai).
Note that the interpretation of these is different; it
depends on how we consider them to be grounded.
CSC 8520 Spring 2013. Paula Matuszek
60
Complex sentences
• Complex sentences are made from atomic
sentences using connectives
• ¬S, S1 ∧ S2, S1 ∨ S2, S1 ⇒ S2, S1 ⇔ S2,
• E.g. sibling(kingJohn,richard) ⇒
sibling(richard,kingJohn)
CSC 8520 Spring 2013. Paula Matuszek
61
Truth in first-order logic
• Sentences are true with respect to a model and an
interpretation
• Model contains objects (domain elements) and relations
among them
• Interpretation specifies referents for
– constant symbols
– predicate symbols
– function symbols
→
→
→
objects
relations
functional relations
• An atomic sentence predicate(term1,...,termn) is true iff
the objects referred to by term1,...,termn are in the
relation referred to by predicate
CSC 8520 Spring 2013. Paula Matuszek
62
Knowledge base for the wumpus world
• Perception
– ∀t,s,b Percept([s,b,Glitter],t) ⇒ Glitter(t)
• Reflex
– ∀t Glitter(t) ⇒ BestAction(Grab,t)
CSC 8520 Spring 2013. Paula Matuszek
63
Deducing hidden properties
• ∀x,y,a,b Adjacent([x,y],[a,b]) ⇔
(x=a ∧ (y=b-1 ∨ y=b+1) ∨ (y=b ∧ (x=a-1 ∨ x=a+1))
• Properties of squares:
– ∀s,t At(Agent,s,t) ∧ Breeze(t) ⇒ Breezy(s)
– Squares are breezy near a pit:
• Diagnostic rule---infer cause from effect
• ∀s Breezy(s) ⇔ ∃ r Adjacent(r,s) ∧ Pit(r)
• Causal rule---infer effect from cause
• ∀r Pit(r) ⇒ [∀s Adjacent(r,s) ⇒ Breezy(s)]
CSC 8520 Spring 2013. Paula Matuszek
64
Knowledge engineering in FOL
• Identify the task
• Assemble the relevant knowledge: knowledge
acquisition
• Decide on a vocabulary of predicates, functions, and
constants: an ontology
• Encode general knowledge about the domain
• Encode a description of the specific problem instance
• Pose queries to the inference procedure and get
answers
• Debug the knowledge base
CSC 8520 Spring 2013. Paula Matuszek
65
Summary
• First-order logic:
– objects and relations are semantic primitives
– syntax: constants, functions, predicates,
equality, quantifiers
• Increased expressive power: sufficient to
define wumpus world compactly
CSC 8520 Spring 2013. Paula Matuszek
66
Inference
• When we have all this knowledge we want to
do something with it
• Typically, we want to infer new knowledge
– An appropriate action to take
– Additional information for the Knowledge Base
• Some typical forms of inference include
– Forward chaining
– Backward chaining
– Resolution
CSC 8520 Spring 2013. Paula Matuszek
67
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
CSC 8520 Spring 2013. Paula Matuszek
68
Example knowledge base contd.
... it is a crime for an American to sell weapons to hostile nations:
American(x) ∧ Weapon(y) ∧ Sells(x,y,z) ∧ Hostile(z) ⇒ Criminal(x)
Nono … has some missiles: Owns(Nono,M1)
Missile(M1)
… all of its missiles were sold to it by Colonel West
Missile(x) ∧ Owns(Nono,x) ⇒ Sells(West,x,Nono)
Missiles are weapons: Missile(x) ⇒ Weapon(x)
An enemy of America counts as "hostile“:
Enemy(x,America) ⇒ Hostile(x)
West, who is American: American(West)
The country Nono, an enemy of America: Enemy(Nono,America)
CSC 8520 Spring 2013. Paula Matuszek
69
Forward chaining proof
CSC 8520 Spring 2013. Paula Matuszek
70
Forward chaining proof
CSC 8520 Spring 2013. Paula Matuszek
71
Forward chaining proof
CSC 8520 Spring 2013. Paula Matuszek
72
Properties of forward chaining
• Sound and complete for first-order definite
clauses
• Datalog = first-order definite clauses + no
functions
• FC terminates for Datalog in finite number of
iterations
• May not terminate in general if α is not entailed
• This is unavoidable: entailment with definite
clauses is semidecidable
CSC 8520 Spring 2013. Paula Matuszek
73
Efficiency of forward chaining
• Incremental forward chaining: no need to match a rule
on iteration k if a premise wasn't added on iteration k1
– ⇒ match each rule whose premise contains a newly
added positive literal
• Matching itself can be expensive:
• Database indexing allows O(1) retrieval of known
facts
– e.g., query Missile(x) retrieves Missile(M1)
• Forward chaining is widely used in deductive
databases
CSC 8520 Spring 2013. Paula Matuszek
74
Backward chaining example
CSC 8520 Spring 2013. Paula Matuszek
75
Backward chaining example
CSC 8520 Spring 2013. Paula Matuszek
76
Backward chaining example
CSC 8520 Spring 2013. Paula Matuszek
77
Backward chaining example
CSC 8520 Spring 2013. Paula Matuszek
78
Backward chaining example
CSC 8520 Spring 2013. Paula Matuszek
79
Backward chaining example
CSC 8520 Spring 2013. Paula Matuszek
80
Backward chaining example
CSC 8520 Spring 2013. Paula Matuszek
81
Backward chaining example
CSC 8520 Spring 2013. Paula Matuszek
82
Properties of backward chaining
• 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)
– ⇒ fix using caching of previous results (extra space)
• Widely used for logic programming
CSC 8520 Spring 2013. Paula Matuszek
83
Forward vs. backward chaining
• FC is data-driven, automatic, unconscious processing,
– e.g., object recognition, routine decisions
• May do lots of work that is irrelevant to the goal
• BC is goal-driven, appropriate for problem-solving,
– e.g., Where are my keys? How do I get into a PhD program?
• Complexity of BC can be much less than linear in size
of KB
• Choice may depend on whether you are likely to have
many goals or lots of data.
CSC 8520 Spring 2013. Paula Matuszek
84
Resolution
CSC 8520 Spring 2013. Paula Matuszek
85
Inference is Expensive!
• You start with a lot of facts (predicates)
• and a lot of possible transformations (rules)
– Some rules apply to a single fact to yield a new fact
– Some rules apply to a pair of facts to yield a new fact
• So at every step you must:
– Choose some rule to apply
– Choose one or two facts to apply it to
• If there are n facts
– There are n potential ways to apply a single-operand rule
– There are n * (n - 1) potential ways to apply a two-operand rule
– Add the new fact to your ever-expanding fact base
• The search space is huge!
CSC 8520 Spring 2013. Paula Matuszek
86
The magic of resolution
• Here’s how resolution works:
– You transform each of your facts into a particular form,
called a clause
– You apply a single rule, the resolution principle, to a pair of
clauses
• Clauses are closed with respect to resolution--that is, when you
resolve two clauses, you get a new clause
• You add the new clause to your fact base
• So the number of facts you have grows linearly
– You still have to choose a pair of facts to resolve
– You never have to choose a rule, because there’s only one
CSC 8520 Spring 2013. Paula Matuszek
87
The fact base
• A fact base is a collection of “facts,” expressed in predicate
calculus, that are presumed to be true (valid)
• These facts are implicitly “anded” together
• Example fact base:
– seafood(X)  likes(John, X)
(where X is a variable)
– seafood(shrimp)
– pasta(X)  likes(Mary, X)
(where X is a different variable)
– pasta(spaghetti)
• That is,
– (seafood(X)  likes(John, X))  seafood(shrimp) 
(pasta(Y)  likes(Mary, Y))  pasta(spaghetti)
– Notice that we had to change some Xs to Ys
– The scope of a variable is the single fact in which it occurs
CSC 8520 Spring 2013. Paula Matuszek
88
Clause form
• A clause is a disjunction ("or") of zero or more literals, some
or all of which may be negated
• Example:
sinks(X)  dissolves(X, water)  ¬denser(X, water)
• Notice that clauses use only “or” and “not”—they do not use
“and,” “implies,” or either of the quantifiers “for all” or
“there exists”
• The impressive part is that any predicate calculus expression
can be put into clause form
– Existential quantifiers, , are the trickiest ones
• And any FOL expression can be expressed as a conjunction
of clauses: Conjunctive Normal Form
CSC 8520 Spring 2013. Paula Matuszek
89
Unification
• From the pair of facts (not yet clauses, just facts):
– seafood(X)  likes(john, X)
– seafood(shrimp)
(where X is a variable)
• We ought to be able to conclude
– likes(john, shrimp)
• We can do this by unifying the variable X with the
constant shrimp
– This is the same “unification” as is done in Prolog
• This unification turns seafood(X)  likes(john, X) into
seafood(shrimp)  likes(john, shrimp)
• Together with the given fact seafood(shrimp), the final
deductive step is easy
CSC 8520 Spring 2013. Paula Matuszek
90
The resolution principle
• Here it is:
– From X  someLiterals
and X  someOtherLiterals
---------------------------------------------conclude: someLiterals  someOtherLiterals
• That’s all there is to it! X and X are complementary literals;
someLiterals  someOtherLiterals is the resolvent clause
• Example:
–
broke(Bob)  well-fed(Bob)
¬broke(Bob)  ¬hungry(Bob)
-------------------------------------well-fed(Bob)  ¬hungry(Bob)
CSC 8520 Spring 2013. Paula Matuszek
91
A common error
• You can only do one resolution at a time
• Example:
– broke(Bob)  well-fed(Bob)  happy(Bob)
¬broke(Bob)  ¬hungry(Bob) ∨ ¬happy(Bob)
• You can resolve on broke to get:
– well-fed(Bob)  happy(Bob)  ¬hungry(Bob)  ¬happy(Bob) a T
• Or you can resolve on happy to get:
– broke(Bob)  well-fed(Bob)  ¬broke(Bob)  ¬hungry(Bob) a T
• Note that both legal resolutions yield a tautology (a trivially true
statement, containing X  ¬X), which is correct but useless
• But you cannot resolve on both at once to get:
–
well-fed(Bob)  ¬hungry(Bob)
CSC 8520 Spring 2013. Paula Matuszek
92
Contradiction
• A special case occurs when the result of a
resolution (the resolvent) is empty, or “NIL”
• Example:
– hungry(Bob)
¬hungry(Bob)
---------------NIL
• In this case, the fact base is inconsistent
• This will turn out to be a very useful observation
in doing resolution theorem proving
CSC 8520 Spring 2013. Paula Matuszek
93
A first example
• “Everywhere that John goes, Rover goes. John is at
school.”
– at(John, X)  at(Rover, X) (not yet in clause form)
– at(John, school)
(already in clause form)
• We use implication elimination to change the first
of these into clause form:
– at(John, X)  at(Rover, X)
– at(John, school)
• We can resolve these on at(-, -), but to do so we
have to unify X with school; this gives:
– at(Rover, school)
CSC 8520 Spring 2013. Paula Matuszek
94
Refutation resolution
• The previous example was easy because it had very few
clauses
• When we have a lot of clauses, we want to focus our
search on the thing we would like to prove
• We can do this as follows:
– Assume that our fact base is consistent (we can’t derive NIL)
– Add the negation of the thing we want to prove to the fact
base
– Show that the fact base is now inconsistent
– Conclude the thing we want to prove
CSC 8520 Spring 2013. Paula Matuszek
95
Example of refutation resolution





“Everywhere that John goes, Rover goes. John is at school.
Prove that Rover is at school.”
• at(John, X)  at(Rover, X)
• at(John, school)
• at(Rover, school) (this is the added clause)
Resolve #1 and #3:
• at(John, X)
Resolve #2 and #4:
• NIL
Conclude the negation of the added clause:
 at(Rover, school)
This seems a roundabout approach for such a simple
example, but it works well for larger problems
CSC 8520 Spring 2013. Paula Matuszek
96
A second example


Start with:

it_is_raining  it_is_sunny

it_is_sunny  I_stay_dry

it_is_raining  I_take_umbrella

I_take_umbrella  I_stay_dry
Convert to clause form:
1.
2.
3.
4.


6.
it_is_raining  it_is_sunny
7.
it_is_sunny  I_stay_dry
8.
it_is_raining  I_take_umbrella 9.
I_take_umbrella  I_stay_dry 10.
Prove that I stay dry:
5. I_stay_dry
CSC 8520 Spring 2013. Paula Matuszek
Proof:
(5, 2) it_is_sunny
(6, 1) it_is_raining
(5, 4) I_take_umbrella
(8, 3) it_is_raining
(9, 7) NIL

Therefore, (I_stay_dry)

I_stay_dry
97
Resolution Summary
• Resolution is a single inference role which is both sound
and complete
• Every sentence in the KB is represented in clause form;
any sentence in FOL can be reduced to this clause form.
• Two sentences can be resolved if one contains a positive
literal and the other contains a matching negative literal;
the result is a new fact which is thedisjunction of the
remaining literals in both.
• Resolution can be used as a relatively efficient theoremprover by adding to the KB the negative of the sentence
to be proved and attempting to derive a contradiction
CSC 8520 Spring 2013. Paula Matuszek
98
Summary
• Inference is the process of adding
information to the knowledge base
• We want inference to be sound: what we
add is true if the KB is true
• And complete: if the KB entails a sentence
we can derive it.
• Forward chaining, backward chaining, and
resolution are typical inference mechanisms
for first order logic.
CSC 8520 Spring 2013. Paula Matuszek
99