You are on page 1of 42

Knowledge representation

Part two
First Order Logic
Propositional logic is a weak language
• PL cannot handle even a domain with small worlds. The
problem is that there are just too many propositions to handle

• In PL world consists of just facts. It is hard to :


–Identify individuals: E.g., Mary, 3
–Describe properties of (or relations between) individuals. E.g.
Belete is taller than Gelaw
–Generalize for a given universe. E.g., all triangles have 3 sides

• Example: Prove that “my dog Fido is Nice, given that “all dogs
are Nice.”
–This requires to get at the structure and meanings of
statements (where FOL is useful).
First Order Logic

• The 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
First Order Logic
• First-Order Logic (FOL) is expressive enough to concisely
represent any kind of situation that are expressed in
natural language.
• sufficiently expressive to represent a good deal of our
commonsense knowledge.
–FOL represents objects and relations between objects,
variables, and quantifiers in addition to propositions
Every elephant is gray:
 x (elephant(x) → gray(x))
everyone likes ice-cream
 x likes(X, Ice-cream)
There is a white alligator:
 x (alligator(X) ^ white(X))
Syntax of FOL
• Constants symbol : that stand for objects
–names (like Jonas, Kebede, …), numbers (like 1, 2, … n), ...
• Predicates: which stand for relations
–Predicates used to relate one object with another. E.g. brother,
>,...
• Functions: Returns value (Sqrt, mother-of,...)
• Variables: x, y, a, b,...
–Important to increase generalization capability of KB
• Connectives:
–retains connectives used in PL (, , , , )
• Quantifiers:
–Quantifiers specify whether all or some objects satisfy properties
or relations between objects
Two standard quantifiers called
a) Universal (x ) and
b) Existential ( x)

• Universal Quantifiers: makes statements about every object


<variables> <sentence>
–Everyone at ASTU is smart:
x At(x,ASTU)  Smart(x)
–All cats are mammals:
x cat(x)  mammal(x)

x sentence P is true iff P is true with x being each possible object in
the given universe
–The above statement is equivalent to the conjunction
At(Selam, ASTU)  Smart(Selam) 
At(solomon, ASTU)  Smart(solomon) 
...
Universal quantifier
• A common mistake to avoid
–Typically,  is the main connective with 
–Common mistake: the use of  as the main connective with :
x At(x, AAU)  Smart(x) is true if “Everyone is at AAU & everyone
is smart”

Another example.
• Rules such as "All kings are persons,'' is written in first-
order logic as
• x King(x) => Person(x)
• where  x or .x is pronounced as “ For all ..”
• Thus, the sentence says, "For all x, if x is a king, then z is a
person."
Existential quantification
•Makes statements about some objects in the universe
<variables> <sentence>
–Someone at ASTU is smart:
x At(x,ASTU)  Smart(x)
–Spot has a sister who is a cat:
x sister(spot,x)  cat(x)
x sentence P is true iff P is true with x being some
possible objects
–The above statement is equivalent to the disjunction
At(Selam, ASTU)  Smart(Selam) 
At(Rakeb, ASTU)  Smart(Rakeb) 
…..
•Common mistake to avoid
–Typically,  is the main connective with 
–Common mistake: using  as the main connective with :
x At(x, ASTU)  Smart(x) is true if there is anyone who is not at ASTU
Existential quantification

• The sentence  x P says that P is true for at


least one object x. More precisely,  x P is true
in a given model under a given interpretation if
P is true in at least one extended interpretation
that assigns x to a domain element.
Nested quantifiers
x, y parent(x, y)  child(y, x)
–for all x and y, if x is the parent of y then y is the child of x.

"Brothers are siblings“

 x  y Brother(x, y)  Sibling(x, y) .
"x, y Sibling(x, y)  Sibling(y, z) .

x y Loves(x, y)
–There is a person who loves everyone in the given world
x  y Loves(x, y)
–Everyone in the given universe is loved by at least one person

Exercise give a meaning


1. x  y likes(X, Y)
2.  y  x likes(X, Y)
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

• Quantifier duality: each can be expressed using the other, using negation ()

x Likes(x,icecream) x Likes(x,icecream)

–Everyone likes ice cream means that there is nobody who dislikes ice cream

x Likes(x,cake) x Likes(x,cake)

–There is someone who likes cake means that there is no one who dislikes
cake
Sentence structure
In FOL the basic unit is a predicate (argument/terms) structure called
sentence to represent facts.
• Terms refer to objects
Example: likes(muhe, chocolate); tall(fred)

–Terms or arguments can be any of: Constant symbol, such as


‘muhe’, variable symbol, such as X , functions, such as
motherof(fred), father-of( father-of( john))
• A predicate is the one that says something about the subject. E.g.,
There is a red book
• Subject: color of the book, represented as: x book(x)red(x)
–Predicate also refers to a particular relation between objects
• Example: likes(X, richard)
friends(motherof(jonas), motherof(semu))
–A predicate statement takes the value true or false
Sentences
Atomic sentences: formed from a predicate symbol followed by a
parenthesized list of terms
Atomic sentence = predicate (term1,...,termn)
Example: Brother(John, Richard)
•Atomic sentences can have arguments that are complex
terms (e.g. term = function (term1,...,termn) )
Example: married(fatherof(Richard),motherof(John))

States that Richard the Lionheart's father is married to


King John's mother (again, under a suitable
interpretation).
Sentences

Complex sentences: complex sentences are made by


combining atomic sentences using connectives:
S, S1  S2, S1  S2, S1  S2, S1  S2,
Ex. likes(john, mary)  tall(mary)
tall(john)  handsome(john)
Sibling(John, Richard)  Sibling(Richard, John)
Brother(Richard, John) A Brother( John, Richard)
King (Richard) + King (John)
• Sentences can also be formed using quantifiers to indicate how to
treat variables:
– Universal quantifier: x lovely(x) - Everything is lovely.
– Existential quantifier: x lovely(x) - Something is lovely.
Sentences
•Can have several quantifiers, e.g.,
x y loves(x, y)
x handsome(x)  y loves(y, x)
Represent the following in FOL:
• Everything in the garden is lovely  x in(x, garden)  lovely(x)
• Everyone likes ice cream  x likes(x, icecream)
y friends(y, Peter)
• Peter has some friends 
•John plays the piano or the violin
plays(john, piano) v plays(john, violin)
x(person(x) Λ likes(x, snakes))
•Some people like snakes --
•Winston did not write Hamlet –write( winston, hamlet)
•Nobody wrote Hamlet – x write(x, hamlet)
•Every city has a dogcatcher who has been bitten by every
dog in town
x {city(x)  y{dogcatcher(y, x) Λ z{[dog(z) Λ live- in(z, x)]  bit(z, y)}}}
Semantics
•There is a precise meaning to expressions in predicate logic.
•Like in propositional logic, it is all about determining whether
something is true or false.
x P(x) means that P(x) must be true for every object x in the
domain of interest.
x P(x) means that P(x) must be true for at least one object x in the
domain of interest.
– So if we have a domain of interest consisting of just two people, john and
marry, and we know that tall(marry) and tall(john) are true, we can
say that x tall(x) is true.
•Atomic sentence is true if the relation referred by the predicate
holds b/n the objects referred by the arguments
brother(John, Richard)

•The truth value of complex sentences depends on logical


connectives used
older(John,30)  younger(John,30)
Assertions and queries in first-order logic
• Sentences are added to a knowledge base using
TELL, exactly as in propositional logic. Such
sentences are called assertions.
• TELL(KB, King (John)) .
• TELL(KB, x King(x) => Person(x))
• We can ask questions of the knowledge base
using ASK. For example,
• ASK(KB, King(John), …
Proof and inference
•We can define inference rules allowing us to say that if certain
things are true, certain other things are sure to be true,
E.g. All men are mortal
Aristotle is a man
using logical inferences we can deduce that: Aristotle is
mortal

x man(x) mortal(x)
man(Aristotle)
so we can conclude that: mortal(Aristotle)
–This involves matching man(x) against man(Aristotle) and
binding the variable x to Aristotle.
Unification
• Unification is an algorithm for determining the substitutions
needed to make two expressions match
– Unification is a "pattern matching" procedure that takes two
atomic sentences as input, and returns the most general unifier,
i.e., a shortest length substitution list that makes the two literals
match.
– E.g: To make, say p(X, X) and p( Y, Z) match, subst(X/ Y) or subst(X/ Z)

• Note: It is possible to substitute two variables with the same


value, but not the same variables with different values.

•Example
Sentence 1 Sentence 2 Unifier
group(x, cat(x), dog(Bill)) group(Bill, cat(Bill), y) {x/Bill, y/dog(Bill)}
group(x, cat(x), dog(Bill)) group(Bill, cat(y), z) {x/Bill, y/Bill, z/dog(Bill)}
group(x, cat(x), dog(Jane)) group(Bill, cat(y), dog(y)) Failure
Unification
• A variable can never be replaced by a term containing that variable. For
example, x/f(x) is illegal.
• Unification and inference rules allows us to make inferences on a set of
logical assertions. To do this, the logical database must be expressed in an
appropriate form
• A substitution α unfies atomic sentences p and q if pα = qα
p q α
Knows(John,x) Knows(John,Jane) {x/Jane}
Knows(John,x) Knows(y, Abe) {y/John, x/Abe}
Knows(John,x) Knows(y,Mother(y))
{y/John, x/Mother(John)}
Knows(John,x) Knows(x,Abe)

Unify rule: Premises with known facts apply unifier to conclusion


Example. if we know q and Knows(John,x) → Likes(John,x)
then we conclude Likes(John,Jane)
Likes(John,Abe)
Likes(John,Mother(John))
Inference rules
• Rules for PL apply to FOL as well. For example,
– Modus Ponens & Tollens
– Resolution and Unit Resolution
– AND-introduction
– AND-elimination,
– OR introduction,
– etc.

• New (sound) inference rules for use with quantifiers:


– Universal Elimination
– Existential Introduction
– Existential Elimination
Sound Inference Rules
•Universal Elimination: If x P(x) is true, then P(c) is true, where
c is a constant in the domain of x.
Example: x eats(x, IceCream).
Using the substitution (x/Ben) we can infer eats(Ben, Icecream).
– The variable symbol can be replaced by any constant symbol or
function symbol.
•Existential Introduction: If P(c) is true, then x P(x) is inferred.
Example: eats(John, Ice-cream) we can infer x eats(x, ice-cream).
– All instances of the given constant symbol are replaced by the new
variable symbol.
•Existential Elimination: From x P(x) infer P(c).
Example: x eats(Sol, x) infer eats(Sol, Cheese)
– Note that the variable x is replaced by a brand new constant (like
Cheese) that does not occur in this or any other sentence in the KB
– If x is within the scope of y replace it by new function f(y)
Sound Inference Rules
Example 1: What can we conclude from the following?
x tall(x)  strong(x)
tall(john)
x strong(x)  loves(mary, x)
Example2: Suppose our knowledge base contains stating that all
greedy kings are evil: x King(x) A Greedy (x) => Evil(x)
From this sentences we can infer the following

Example 2: Every metal is dissolved by sulphuric acid


copper is a metal
can we conclude:
Copper is dissolved by sulphuric acid
Reduction to propositional inference
• Once we have rules for inferring non-quantified sentences from quantified
sentences, it becomes possible to reduce first-order inference to
propositional inference.
•  x King (x) A Greedy(x) => Evil (x)
King (John)
Greedy (John)
Brother(Richard, John)
• Apply UI to the first sentence using all possible ground term by
substitutions.
King( John) A Greedy( John) => Evil (John) ,
King (Richard) A Greedy (Richard) => Evil (Richard)
we can apply any of the complete propositional algorithms to obtain
conclusions such as Evil (John). This techniques is called propositionalization.
Proofs
• Sound inference: find α such that KB |= α
• Proof process is a search, operators are inference rules
–It requires the operation of a series of inference rule to come up
with some conclusion
Example:
Bob is a buffalo. Pat is a pig. Buffaloes outrun pigs
Conclude: Bob outruns Pat
1. Buffalo(Bob)
2. Pig(Pat)
3. x,y Buffalo(x) ^ Pig(y) → outrun(x,y)
4. Buffalo(Bob) ^ Pig(Pat) And Introduction (1, 2)
5. Buffalo(Bob) ^ Pig(Pat) → outrun (Bob, Pat)
Universal Elimination (3, {x/Bob,y/Pat})
6. outrun(Bob,Pat) Modus Ponens (6, 7)
Generalized Modus Ponens
 Is a lifted version of Modus Ponens-it raises Modus Ponens from
propositional to first-order logic.
 We can develop lifted versions of the forward chaining, backward
chaining, and resolution algorithms.
 The key advantage of lifted inference rules over
propositionalization is that, they make only those substitutions
which are required to allow particular inferences to proceed.
 One potentially confusing point is that in one sense Generalized
Modus Ponens is less general than Modus Ponens
 Modus Ponens allows any single a on the left-hand side of the
implication, while Generalized Modus Ponens requires a special
format for this sentence. It is generalized in the sense that it
allows any number of Pi'.
Generalized Modus Ponens (GMP)
• Combines And-Introduction, Universal-Elimination, and Modus Ponens
• Given atomic sentences P1, P2, ..., PN, and implication sentence (Q1 ^ Q2
^ ... ^ QN)  R, where Q1, ..., QN and R are atomic sentences, derive new
sentence R. That is,
• from P(c), Q(c), and x (P(x) ^ Q(x)  R(x)), derive R(c)
• N + 1: there are n atomic sentences pi' and the one implication.
• GMP is sound inference rule, p I= Subs(8,p)

Example: Given Faster(Bob,Pat), Faster(Pat,Steve) and


Faster(x,y) ^ Faster(y,z)  Faster(x,z);
by substituting {x/Bob, y/Pat, z/Steve}
infer in one step the new sentence: Faster(Bob, Steve)
• GMP is not complete for FOL. Natural deduction using GMP is possible
only for KBs containing Horn clauses.
• A Horn clause is a sentence of the form:
x (P1(x) ^ P2(x) ^ ... ^ Pn(x))  Q(x)
where there are zero or more Pi‘s
Inference Mechanisms
• Inference is a means of interpretation of knowledge in
the KB to reason and give advise to users query.
• There are two inference strategies to control and
organize the steps taken to solve problems:
– Forward chaining: also called data-driven chaining
• It starts with facts and rules in the KB and try to draw
conclusions from the data.(how this inference implemented in
prolog)
– Backward chaining: also called goal-driven chaining
• It starts with possible solutions/goals and tries to gather
information that verifies the solution is true. (how this
inference implemented in prolog)
Inference Mechanisms
•Forward chaining
–Proofs start with the given axioms/premises in KB, deriving new
sentences using GMP until the goal/query sentence is derived.
–This defines a forward chaining inference procedure because it
moves "forward" from the KB to the goal.
•Example: All cats like fish, cats eat everything they like, and
Ziggy is a cat. Goal query: Does Ziggy eat fish?
1. x cat(x)  likes(x, Fish)
2. x,y (cat(x) ^ likes(x,y))  eats(x,y)
3. cat(Ziggy)
–Proof: 4. likes(Ziggy, Fish) Using GMP with (1) and (3) &
subst{x/Ziggy}
5. eats(Ziggy, Fish) Using GMP with (3), (4) and (2)
So, Yes, Ziggy eats fish.
Another example

Rule 1: IF Y is true
AND D is true
THEN Z is true
A X
Rule 2: IF X is true
AND B is true
B Y
AND E is true
Z
THEN Y is true
E D
Rule 3: IF A is true
THEN X is true
Forward chaining

• There are three possible sources of complexity.


• First, the "inner loop" of the algorithm involves finding
all possible unifiers such that the premise of a rule
unifies with a suitable set of facts in the knowledge
base. This is often called pattern matching and can be
very expensive.
• Second, the algorithm rechecks every rule on every
iteration to see whether its premises are satisfied,
even if very few additions are made to the knowledge
base on each iteration.
• Finally, the algorithm might generate many facts that
are irrelevant to the goal.
Inference Mechanisms
•Backward chaining:
–Proofs start with the goal query, find implications that would allow
you to prove it, and then prove each of the antecedents in the
implication, continuing to work "backwards" until we get to the
axioms, which we know are true.
–To prove eats(Ziggy, Fish), first see if this is known from one of the
axioms directly. Otherwise, see if there is a Horn clause that has the
consequent (i.e., right-hand side (RHS)) of the implication matching
the goal. Here,
• Goal matches RHS of (2), so prove new sub-goals
cat(Ziggy) and likes(Ziggy, Fish) that correspond to the LHS of (2)
• cat(Ziggy) matches axiom (3), so we've "solved" that sub-goal
• likes(Ziggy, Fish) matches the RHS of (1), so prove cat(Ziggy)
• cat(Ziggy) matches (3), so we've solved this sub-goal
• There are no unsolved sub-goals,
So we conclude. Yes, Ziggy eats fish
Backward chaining

• Thus the inference engine puts aside the rule it


is working with (the rule is said to stack) and
sets up a new goal, a subgoal, to prove the IF
part of this rule. Then the knowledge base is
searched again for rules that can prove the
subgoal. The inference engine repeats the
process of stacking the rules until no rules are
found in the knowledge base to prove the
current subgoal.
….
Pass 1 Pass 2 Pass 3
Database Database Database

A BC DE AB CD E AB CD E

? ?

Z Y X
Knowledge Base Knowledge Base Knowledge Base
Y&D Z Y&D Z Y&D Z
X&B&E Y X & B & EY X & B & EY
A X A X A X
C L C L C L
L&M N L&M N L&M N
Goal: Z Sub-Goal: Y Sub-Goal: X

Pass 4 Pass 5 Pass 6


Database Database Database

AB CD E AC B DE AC B DE
X X Y X Y Z

Match Fire Match Fire Match Fire


Knowledge Base Knowledge Base Knowledge Base
Y&D Z Y&D Z Y&D Z
X & B & EY X&B&E Y X&B&E Y
A X A X A X
C L C L C L
L&M N L&M N L&M N
Sub-Goal: X Sub-Goal: Y Goal: Z
...
Generalized Resolution
• Resolution procedure is a sound and complete inference procedure
for FOL
–It uses a single resolution rule of inference: which is generalization of the
same rule used in PL.
• Resolution Rule for PL: From sentence P1 v P2 v ... v Pn and sentence
P1 v Q2 v ... v Qm derive resolvent sentence:
P2 v ... v Pn v Q2 v ... v Qm
• Resolution Rule for FOL:
• Given sentence P1 v ... v Pn and sentence Q1 v ... v Qm where each Pi
and Qi is a predicate sentences, if Pj and Qk unify with substitution
list θ, then derive the sentence:
P1 v ... v Pj-1 v Pj+1 v ... v Pn v Q1 v ... Qk-1 v Qk+1 v ... V Qm

Examples:
–Given: Rich(ken); Rich(x) v unhappy(x)
using subst {x/ken}
we can conclude that: unhappy(ken)
Generalized Resolution: proof by refutation
• Generalized Resolution inference rule provides a complete
system for proof by refutation.
–Resolution is a generalization of modus ponens
–It requires a normal form, any sentence can be put into CNF
• Conjunctive normal form (CNF)
–Each individual sentence is a disjunction of literals. This form is
CNF
–CNF is more common. For instance,
P(x) v R(x)
Q(y) v S(y)
• Conversion to CNF:
–any FOL sentence can be converted into normal form
Steps to convert a FOL sentence to CNF
• Eliminate  connectives: replace (P  Q) by ((P  Q) ^ (Q  P))
• Eliminate  connectives: replace each instance (P  Q) by (P v Q)
• Reduce the scope of negation:
P to P; (P v Q) to P ^ Q; (P ^ Q) to P v Q; x P(x) to x P(x), and x
P(x) to x P(x)
• Eliminate  by introducing Skolem symbols/function.
x P(x) converted to P(c) where c is a brand new Skolem constant symbol
that is not used in any other sentence.
– If  is within the scope of a universal quantified variable, then introduce a
Skolem function (f). Example, xyP(x,y) is converted to xP(x, f(x)).
E.g.: x y loves(x,y) converted to x loves(x,f(x)) where f(x) specifies the
person that x loves. (If everyone loved their mother, then f could be
mother_of (x) function)
• Drop  x quantifiers. For example, convert x P(x) to P(x).
• Distribute "and" over "or" to get conjunctive normal form.
Convert (P ^ Q) v R to (P v R) ^ (Q v R), & (P v Q) v R to (P v Q v R).
• And Elimination: Split each conjunct into a separate clause.
(P v R) ^ (Q v R) into (P v R), (Q v R)
x (P(x)  (y (P(y)  P(f(x,y))) ^ y(Q(x,y)  P(y)))
• Eliminate :
(x)(P(x) v ((y)(P(y) v P(f(x,y))) ^ (y)(Q(x,y) v P(y))))
• Reduce scope of  :
(x)(P(x) v ((y)(P(y) v P(f(x,y))) ^ (y)(Q(x,y) ^ P(y))))
• Standardize variables:
(x)(P(x) v ((y)(P(y) v P(f(x,y))) ^ (z)(Q(x,z) ^ P(z))))
• Eliminate :
(x)(P(x) v ((y)(P(y) v P(f(x,y))) ^ (Q(x,g(x)) ^ P(g(x)))))
• Drop universal quantification
(P(x) v ((P(y) v P(f(x,y))) ^ (Q(x,g(x)) ^ P(g(x)))))
• Convert to conjunction of disjunctions
(P(x) v P(y) v P(f(x,y))) ^ (P(x) v Q(x,g(x))) ^ (P(x) v P(g(x)))
• Create separate clauses (And Elimination)
P(x) v P(y) v P(f(x,y)), P(x) v Q(x,g(x)), P(x) v P(g(x))
• Standardize variables
P(x) v P(y) v P(f(x,y)), P(z) v Q(z,g(z)), P(w) v P(g(w))
Assignment 1:
Convert the following sentence to CNF
1. x [y animal(y)  loves(x,y)]  [z loves(z,x)]

2. x (P(x)  (y (P(y) < P(f(x,y))) ^ y(Q(x,y)  P(y)))


3. (P∧Q) ↔(R→(P→¬Q))
4. x( y( z(A(x, y, z) ^ B(y)) ---> ( x.C(x, z))))
5.  x y(A(x, y, z) ^  uC(x, u)) --->  vC(x, v))
6.  x(S(x) ^  y(L(y)  A(x, y)))

Note: make sure that you followed all the steps until you come up with
independent sentences
Resolution: Proof by Refutation
Example
• Jack owns a dog. Every dog owner is an animal lover. No
animal lover kills an animal. Either Jack or Curiosity killed
the cat, who is named Tuna. Did Curiosity kill the cat?

FOL representation:
A. (x) Dog(x)  Owns(Jack,x)
B. (x) ((y) Dog(y)  Owns(x, y))  AnimalLover(x)
C. (x) AnimalLover(x)  ((y) Animal(y)  Kills(x,y))
D. Kills(Jack,Tuna)  Kills(Curiosity,Tuna)
E. Cat(Tuna)
F. (x) Cat(x)  Animal(x)
G. Kills(Curiosity, Tuna)
Assignment
• The law sys that it is crime to for Sudanese to sell
weapons to hostile nations. The country Eritrea
an enemy of Sudan, has some bomb and all of its
bombs were sold to it by Hasan.
• Is john garang criminal ? proof

You might also like