Logic and Reasoning: Room 1 Room 2
Logic and Reasoning: Room 1 Room 2
R&N 7-9
Room 1 Room 2
Door 1
Prior Knowledge:
Room 4 Room 3
Door1Open AND
Door2Open AND
Knowledge Door3Open
Base (KB) IMPLIES Path
Room1 to Room4
Query: is Free
Path Room1 to Reply:
Room4 is Free Query is satisfied Door3Open
1
Representing Knowledge:
Propositional Logic
• Symbols
• True, False
• Implication: =>
• Equivalence: <=> • Raining => Wet
• And (conjunction): ^
• ¬(Busy ^ Sleeping)
• Or (disjunction): V
• Negation: ¬ • (A ^ B) V ¬C
• Sentences = combination
of the symbols, truth
values, and operators
• Literals = Symbols and
negated symbols (A and
¬A are both literals)
2
Example KB
A B C KB
F F F F
KB:
F F T F
A∨ B F T F T
¬C ∨ A F T T F
T F F T
T F T T
T T F T
T T T T
Logical Entailment
• “KB logically entails S” if all the models
that evaluate KB to True also evaluate S
to True.
• Denoted by: KB |= S
• Note: We do not care about those models
that evaluate KB to False. The result of
evaluating S for these models is irrelevant.
3
Example KB
A B C KB S
F F F F F
KB:
F F T F F KB |= S
A∨ B F T F T F because
KB is true
¬C ∨ A F T T F F but S is
T F F T F false
S: T F T T T
A∧C T T F T F
T T T T T
Example KB
A B C KB S
F F F F F
KB:
F F T F T KB |= S
A∨ B F T F T T because S
is true for all
¬C ∨ A F T T F T the
T F F T T assignments
S: T F T T T for which KB
A∨ B ∨C is true
T T F T T
T T T T T
4
Logical Entailment
Assignments for
which KB is True
KB KB
S
S
KB |= S KB |= S
KB S
KB |= S
Inference
• An inference algorithm is a procedure for
deriving a sentence from the KB
• KB |-i S means that S is inferred from KB
using algorithm i.
• The inference algorithm is sound if it
derives only sentences that are entailed by
KB.
• The inference algorithm is complete if it
can derive any sentence that is entailed by
KB.
5
Examples
• Examples of sound inference rules
Premise
α ∧β And-Elimination. In words: if
two things must be true,
then either of them must be
Conclusion
α true.
α ,β And-Introduction.
α ∧ β
Inference
• Basic problem:
– We have a KB
– We have a sentence S (the “query”)
– We want to check KB |= S
• Informally, “prove” S from KB
• Simplest approach: Model checking =
evaluate all possible settings of the symbols
• Sound and complete (if the space of models
is finite), but 2n
6
Definitions
• Valid: A sentence is valid if it is true for all models.
α ∨ ¬α
• Satisfiable: A sentence is satisfiable if it is true for
some models.
• Unsatisfiable: A sentence is unsatisfiable if it is true for
no models.
α ∧ ¬α
• Proof by contradiction: Given KB and a sentence S,
establishing entailment is equivalent to proving that no
model exists that satisfies KB and ¬S.
KB |= S equivalent to (KB ^ ¬S) unsatisfiable
A∨ B T F F T
T F T T
¬C ∨ A T T F T
T T T T
7
Proof as Search: Inference Rules
• Proof can be viewed as a search problem
The basic search algorithms that we saw before
can be used here
– State: KB
– Successor: Apply inference to KB to obtain new
sentences
– Solution: Sequence of inferences to goal sentence. If
the inference algorithm is sound, then this is
guaranteed to establish entailment
• Questions: Is there an inference algorithm that
guarantees efficient search? Will the search be
complete?
Resolution
• A sentence is in Conjunctive Normal Form
(CNF) if it is a conjunction of clauses, each
clause being a disjunction of literals
• Examples:
( A ∨ B ) ∧ (C ∨ D ∨ J ) ∧ (E ∨ G )
Clause Clause Clause
8
CNF Conversion
1. α ⇔ β (α ⇒ β ) ∧ (β ⇒ α )
2. α ⇒ β
To CNF
¬α ∨ β
3. ¬(α ∧ β ) ¬α ∨ ¬β
4. ¬(α ∨ β ) ¬α ∧ ¬β
( A ∧ B) ⇒ C
1.
¬( A ∧ B ) ∨ C
3.
( ¬A ∨ ¬B ) ∨ C
( ¬A ∨ ¬B ∨ C )
Resolution
A1 ∨ K ∨ Ai ∨ K ∨ An ¬Ai
A1 ∨ K ∨ Ai −1 ∨ Ai +1 ∨ K ∨ An
• In words: If a literal appears in one clause and
its negation in the other one, the two clauses
can be merged and that literal can be discarded.
9
Resolution
A1 ∨ K ∨ Ai ∨ K ∨ An B1 ∨ K ∨ ¬Ai ∨ K ∨ Bm
A1 ∨ K ∨ Ai −1 ∨ Ai +1 ∨ K ∨ An ∨ B1 ∨ K ∨ Bl −1 ∨ Bl +1 ∨ K ∨ Bm
10
Door1Open ^ Door2Open => Room2Reachable
¬ Weekend => Door2Open
¬ Weekend ^ Door1Open Query: Room2Reachable
¬ Door1Open V ¬ Door2Open V Room2Reachable
Weekend V Door2Open
¬ Weekend ^ Door1Open
¬ Door1Open V ¬ Door2Open
¬ Room2Reachable
Door1Open ^ Door2Open
¬ Door2Open ^ Door2Open
EMPTY
11
Resolution Algorithm
• Input: KB and S
• Output: True if KB entails S, False otherwise
• Initialize: Clauses CNF(KB ^ ¬S)
– Repeat:
• For each pair of clauses Ci and Cj in Clauses
– R Resolution(Ci, Cj)
– If R contains the empty clause: Return True
– new new U R
• If Clauses contains new: Return False
• Clauses Clauses U new
Resolution: Property
• Resolution is sound: Always produces
sentences that are entailed by their premise
• Resolution is complete: It is guarantee to
establish entailment of the query in finite time
• Completeness based on the key theorem: If
a set of clauses is unsatisfiable, then the set
of all clauses that can be obtained by
resolution contains the empty clause
• So, conversely, if we cannot find the empty
clause, the query must be satisfiable
12
Resolution can be Used to Check
Consistency of a KB
• Repeat: Resolve pairs of sentences from
the KB until
– No more sentences can be produced KB is
consistent (satisfiable)
– A unsatisfiable sentence is produced KB is
inconsistent
Human => Mortal ¬ Human V Mortal CNF
KB
Peter => Human ¬ Peter V Human
Resolution
¬ Peter V Mortal
False
13
Chaining
• “Special” case: The KB contains only two types
of sentences:
– Symbols
– Sentences of the form:
(conjunction) => symbol
( A1 ∧L ∧ An ) ⇒ B
Sentences of this type are “Horn clauses”
Chaining
• Basic inference mechanism (“Modus Ponens”):
A1 ∧ K ∧ An ⇒ B A1 ∧ K ∧ An
B
• Basic idea: Given KB and a symbol S
– Forward chaining: Repeatedly apply the inference
rule to KB until we get to S
– Backward chaining: Start from S and find
implications whose conclusions are S
Sentences of this type are “Horn clauses”
14
Forward Chaining
A1 ∧ K ∧ An ⇒ B
Counter = number of symbols on left hand-side
If Counter = 0Infer symbol on right-hand side, B
Forward Chaining
• Maintain a current list of symbols from KB
• Initialize a counter for each clause in KB =
number of symbols on the left-hand side
• Repeat:
– Get the next symbol P from the queue
– If P = S
• We are done, KB |= S
– Else
• Decrease the counter of each clause in which P
appear in the left-hand side
• If the counter is 0: Add the right-hand side of the
clause to the list
15
Forward Chaining
P => Q
L ^ M => P
B ^ L => M
A ^ P => L
A ^ B => L
A
B
Forward Chaining
P => Q
L ^ M => P
B ^ L => M
A ^ P => L
A ^ B => L
A
B
16
Forward Chaining
P => Q
L ^ M => P
B ^ L => M
A ^ P => L
A ^ B => L
A
B
Forward Chaining
P => Q
L ^ M => P
B ^ L => M
A ^ P => L
A ^ B => L
A
B
17
Forward Chaining
P => Q
L ^ M => P
B ^ L => M
A ^ P => L
A ^ B => L
A
B
Forward Chaining
P => Q
L ^ M => P
B ^ L => M
A ^ P => L
A ^ B => L
A
B
18
Forward Chaining
P => Q
L ^ M => P
B ^ L => M
A ^ P => L
A ^ B => L
A
B
Forward Chaining
P => Q
L ^ M => P
B ^ L => M
A ^ P => L
A ^ B => L
A
B
19
Forward/Backward Chaining
• Both algorithms are
– Sound (valid inferences)
– Complete (every entailed symbol can be derived)
• Both algorithms are linear in the size of the
knowledge base
• Forward=data-driven: Start with the data (KB)
and draw conclusions (entailed symbol) through
logical inferences
• Backward=goal-driven: Start with the goal
(entailed symbol) and check backwards if it can
be generated by an inference rule
Summary
• Knowledge base (KB) as list of sentences
• Entailment verifies that query sentence is
consistent with KB
• Establishing entailment by direct model checking
is exponential in the size of the KB, but:
– If KB is in CNF form (always possible): Resolution is a
sound and complete procedure
– If KB is composed of Horn clauses:
– Forward and backward checking algorithms are
linear, and are sound and complete
• Shown so far using a restricted representation
(propositional logic)
• What is the problem with using these tools for
reasoning in real-world scenarios?
20