Module 4
Module 4
The world is blessed with many objects, some of which are related to other objects, and
in which we endeavor to reason about them.
1
Module 4-Contents
First Order Logic
• Representation Revisited
• Syntax and Semantics of First Order logic
• Using First Order logic.
2
FIRST-ORDER LOGIC
• Propositional logic is too puny a language to represent
knowledge of complex environments in a concise way.
3
REPRESENTATION REVISITED
The development of first-order logic, a much more expressive language
than the propositional logic
1. The language of thought
• Data Structure using programming languages- “There is a pit in [2,2] or [3,1]”.
• Propositional Logic- B1,1 ⇔ (P1,2 ∨ P2,1).
• First Order Logic- CNF - “¬(A ∨ B)” and “¬A ∧ ¬B”.
2. Combining the best of formal and natural languages
Syntax of natural language
• Object- nouns and noun phrases (squares, pits, wumpuses)
• Relation- verbs and verb phrases (is breezy, is adjacent to, shoots)
• Functions- relations in which there is only one “value” for a given “input.”
4
Examples:
• Objects: people, houses, numbers, theories, Ronald McDonald, colors, baseball games,
wars, centuries ...
• Relations: these can be unary relations or properties such as red, round, bogus, prime,
multistoried .. ., or
more general n-ary relations such as brother of, bigger than, inside, part of, has color,
occurred after, owns, comes between, ...
• Functions: father of, best friend, third inning of, one more than, beginning of ...
• “One plus two equals three.”
• “Evil King John ruled England in 1200.”
Objects: one, two, three, one plus two;
Relation: equals; Objects: John, England, 1200;
Function: plus. Relation: ruled;
Properties: evil, king.
• “Squares neighboring the wumpus are smelly.”
Objects: wumpus, squares;
Property: smelly;
Relation: neighboring.
5
Language Ontological Commitment Epistemological Commitment
(What exists in the world) (What an agent believes about facts)
Propositional logic facts true/false/unknown
First-order logic facts, objects, relations true/false/unknown
Temporal logic facts, objects, relations, times true/false/unknown
Probability theory facts degree of belief ∈ [0, 1]
Fuzzy logic facts with degree of truth ∈ [0, 1] known interval value
Figure 8.1 Formal languages and their ontological and epistemological commitments.
6
SYNTAX AND SEMANTICS OF FIRST-ORDER LOGIC
First-order logic reflect the ontological commitment to objects and relations
7
A model with five
objects:
1. Richard the Lionheart,
King of England from
1189 to 1199;
2. His younger brother,
the evil King John,
who ruled from 1199
to 1215;
3. The left legs of
Richard
4. The left legs of John;
and
5. A crown.
8
A relation is just the set of tuples of objects that are related,
the brotherhood relation in this model is the set
{ (Richard the Lionheart, King John), (King John, Richard the Lionheart)}
• The crown is on King John’s head, so the “on head” relation contains just one tuple, (the
crown, King John).
• The “brother” and “on head” relations are binary relations—that is, they relate pairs of
objects.
Unary Relations
• The “person” property is true of both Richard and John;
• The “king” property is true only of John
• The “crown” property is true only of the crown.
A unary function - “left leg”
(Richard the Lionheart)→ Richard’s left leg
(King John)→ John’s left leg .
Strictly speaking, models in first-order logic require total functions, that is, there
must be a value for every input tuple.
9
2. Symbols and interpretations
First-order logic are the symbols that stand for objects, relations, and functions.
Each model includes an interpretation that specifies exactly which objects, relations
and functions are referred to by the constant, predicate, and function symbols.
10
11
12
13
3. Terms
A term is a logical expression that refers to an object.
A term f (t1,... , tn),
• f refers to some function in the model (call it F ) ,
• The argument terms refer to objects in the domain (call them d1,... , dn)
• Example: LeftLeg (John ) refers to King John’s left leg.
14
4. Atomic sentences
15
5. Complex sentences
16
6. Quantifiers
To express properties of entire collections of objects, instead of enumerating
the objects by name.
Two standard quantifiers are:
1. Universal quantification (∀)
2. Existential quantification (∃)
• Nested quantifiers
• Connections between ∀ and ∃
17
i. Universal quantification (∀)
18
The interpretation in five ways:
x → Richard the Lionheart,
x → King John,
x → Richard’s left leg,
x → John’s left leg,
x → the crown.
The universally quantified sentence ∀ x King(x) ⇒ Person(x) is true in
the original model
if the sentence King(x) ⇒ Person(x) is true under each of the five
extended interpretations.
19
The universally quantified sentence is equivalent to asserting the following five
sentences:
• Richard the Lionheart is a king ⇒ Richard the Lionheart is a person.
• King John is a king ⇒ King John is a person.
• Richard’s left leg is a king ⇒ Richard’s left leg is a person.
• John’s left leg is a king ⇒ John’s left leg is a person.
• The crown is a king ⇒ the crown is a person.
⇒ turns out to be perfect for writing general rules with universal quantifiers.
22
Nested quantifiers
23
Some confusion can arise when two quantifiers are used with the same variable
name.
∀ x (Crown (x) ∨ (∃ x Brother (Richard , x)))
Here the x in Brother (Richard , x) is existentially quantified.
24
Connections between ∀ and ∃
The two quantifiers are actually intimately connected with each other, through negation.
• Asserting that everyone dislikes parsnips is the same as asserting there does not exist
someone who likes them, and vice versa:
• ∀ x ¬Likes(x, Parsnips ) is equivalent to ¬∃ x Likes(x, Parsnips )
• “Everyone likes ice cream” means that there is no one who does not like ice cream:
∀ x Likes(x, IceCream) is equivalent to ¬∃ x ¬Likes(x, IceCream)
Because ∀ is really a conjunction over the universe of objects and ∃ is a disjunction
The De Morgan rules for quantified and unquantified sentences are as follows:
∀ x ¬P ≡ ¬∃ x P ¬(P ∨ Q) ≡ ¬P ∧ ¬Q
¬∀ x P ≡ ∃ x ¬P ¬(P ∧ Q) ≡ ¬P ∨ ¬Q
∀ x P ≡ ¬∃ x ¬P P ∧ Q ≡ ¬(¬P ∨ ¬Q)
∃ x P ≡ ¬∀ x ¬P P ∨ Q ≡ ¬(¬P ∧ ¬Q) .
25
Equality
• The equality symbol to signify that two terms refer to the same
object. For example,
Father (John)= Henry
• The object referred to by Father (John) and the object referred to
by Henry are the same
• It can also be used with negation to insist that two terms are not
the same object. To say that Richard has at least two brothers
∃ x, y Brother (x, Richard ) ∧ Brother (y, Richard ) ∧ ¬(x = y)
• The sentence ∃ x, y Brother (x, Richard ) ∧ Brother (y, Richard )
does not have the intended meaning.
26
8. An alternative semantics?
Suppose that we believe that Richard has two brothers, John and Geoffrey
Assert Brother (John, Richard ) ∧ Brother (Geoffrey , Richard ) ? Not quite
1. This assertion is true in a model where Richard has only one brother—we need to
add John != Geoffrey .
2. The sentence doesn’t rule out models in which Richard has many more brothers
besides John and Geoffrey.
3. The correct translation of “Richard’s brothers are John and Geoffrey” is as follows:
Brother (John, Richard ) ∧ Brother (Geoffrey , Richard ) ∧ John ≠ Geoffrey ∧
∀ x Brother (x, Richard ) ⇒ (x = John ∨ x = Geoffrey )
27
Database Semantics
One proposal that is very popular in database systems works as
follows.
• First, we insist that every constant symbol refer to a distinct object—
the so-called unique-names assumption.
• Second, we assume that atomic sentences not known to be true are in
fact false—the closed-world assumption.
• Finally, we invoke domain closure, meaning that each model
contains no more domain elements than those named by the
constant symbols.
28
USING FIRST-ORDER LOGIC
• TELL/ASK interface for first-order knowledge bases
• The domains of family relationships, numbers, sets, and lists, and at the wumpus
world.
1. 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.
For example: Assert that John is a king, Richard is a person, and all kings are
persons:
• TELL(KB, King(John )) .
• TELL(KB, Person(Richard )) .
• TELL(KB, ∀ x King(x) ⇒ Person(x))
Knowledge Bases
Every way of making the query true will bind the variables to specific values.
First-order logic
Not all logical sentences about a domain are axioms. Some are theorems
entailed by the axioms.
∀ x, y Sibling(x, y) ⇔ Sibling(y, x) .
32
Not all axioms are definitions
predicates have no complete definition
∀ x Person(x) ⇔ . . .
33
3. Numbers, sets, and lists
Required
• A predicate NatNum that will be true of natural numbers.
• One constant symbol, 0; and
• One function symbol, S (successor).
• 0 is a natural number, and for every object n,
• if n is a natural number, then S(n) is a natural number.
• The natural numbers are 0, S(0), S(S(0)), and so on
• axioms to constrain the successor function
∀ n 0 ≠ S(n) .
∀ m, n m ≠ n ⇒ S(m) ≠ S(n) .
34
Addition in terms of the successor function: (0-zero)
∀ m NatNum(m) ⇒ + (0, m) = m
∀ m, n NatNum(m) ∧ NatNum(n) ⇒ + (S(m), n) = S(+(m, n))
write S(n) as n + 1
∀ m, n NatNum(m) ∧ NatNum(n) ⇒ (m + 1)+ n = (m + n)+ 1 .
• The use of infix notation is an example of syntactic sugar, that is, an extension to
or abbreviation of the standard syntax that does not change the semantics.
• Addition, it is straightforward to define multiplication as repeated addition,
exponentiation as repeated multiplication, integer division and remainders, prime
numbers, and so on.
35
Sets
The domain of sets is also fundamental to mathematics as well as to commonsense
reasoning.
• Normal vocabulary of set theory as syntactic sugar.
• Empty set { }.
• One unary predicate, Set, which is true of sets.
• The binary predicates are x ∈ s (x is a member of set s) and s1 ⊆ s2 (set s1 is a subset, not
necessarily proper, of set s2).
• The binary functions are s1 ∩ s2 (the intersection of two sets), s1 ∪ s2 (the union of two
sets), and {x|s} (the set resulting from adjoining element x to set s).
36
Possible set of axioms is as follows:
• The only sets are the empty set and those made by adjoining something to a set:
∀ s Set(s) ⇔ (s = { }) ∨ (∃ x, s2 Set (s2) ∧ s = {x|s2})
• The empty set has no elements adjoined into it. In other words, there is no way to
decompose {} into a smaller set and an element:
¬∃ x, s {x|s} = {}
• Adjoining an element already in the set has no effect:
∀ x, s x ∈ s ⇔ s = {x|s}
• Say that x is a member of s if and only if s is equal to some set s2 adjoined with some
element y, where either y is the same as x or x is a member of s2:
∀ x, s x ∈ s ⇔ ∃ y, s2 (s = {y|s2}∧ (x = y ∨ x ∈ s2)) .
37
• A set is a subset of another set if and only if all of the first set’s members are
members of the second set:
∀ s1, s2 s1 ⊆ s2 ⇔ (∀ x x ∈ s1 ⇒ x ∈ s2) .
• Two sets are equal if and only if each is a subset of the other:
∀ s1, s2 (s1 = s2) ⇔ (s1 ⊆ s2 ∧ s2 ⊆ s1) .
38
Lists are similar to sets. The differences are that lists are ordered and the same element
can appear more than once in a list
• List ? is a predicate that is true only of lists.
• lists: Nil is the constant list with no elements;
• Cons , Append , First , and Rest are functions; and
• Find is the predicate that does for lists what Member does for sets.
39
The wumpus world
• Wumpus agent receives a percept vector with five elements:
Percept ([Stench, Breeze, Glitter , None, None], 5)
• The actions in the wumpus world can be represented by logical terms:
Turn(Right), Turn(Left ), Forward , Shoot , Grab, Climb.
• To determine which is best, the agent program executes the query
ASKVARS(∃ a BestAction(a, 5))---------returns a binding list such as {a/Grab }.
• The raw percept data implies certain facts about the current state
example:
∀ t, s, g, m, c Percept ([s, Breeze, g, m, c], t) ⇒ Breeze(t) ,
∀ t, s, b, m, c Percept([s, b, Glitter , m, c], t) ⇒ Glitter (t) ,…….
• These rules exhibit a trivial form of the reasoning process called perception
• Simple “reflex” behavior can also be implemented by quantified implication sentences.
Example: ∀ t Glitter (t) ⇒ BestAction(Grab, t)
40
• To represent the environment itself
Adjacency of any two squares can be defined as
∀ 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))
42
PROPOSITIONAL VS. FIRST-ORDER INFERENCE
First-order inference can be done by converting the knowledge base to propositional
logic and using propositional inference
43
Universal
● Every instantiation ofInstantiation
a universally quantified sentence is entailed by it:
∀v α
SUBST({v/g}, α)
K i n g ( J o h n ) ∧ Greedy(John) =⇒ E v i l ( J o h n )
K i n g ( R i c h a r d ) ∧ Greedy(Richard) =⇒
Ev i l ( R ich a r d )
K i n g ( Fa t h e r ( J o h n ) ) ∧ Greedy(Father (John)) =⇒
Ev i l ( Fa t h e r ( J o h n ) )
⋮ 44
Existential
Instantiation
● For any sentence α, variable v, and constant symbol k
that does not appear elsewhere in the knowledge base:
∃v
α
SUBST(Iv/k}, α)
C r ow n ( C 1 ) ∧ OnHead(C 1 , J o h n )
provided C 1 is a new constant symbol, called a Skolem constant
45
Reduction to Propositional
Inference
● Claim: a ground sentence is entailed by new KB iff entailed by original KB
∗
48
Unification
● UNIFY(p, q) = θ if pθ =
qθ
θ
p s(John,
Know x) K n o w s ( J o h n , J ane )
Knows(John, x) Knows (y, M a r y )
q s(John,
Know x) Knows (y, Mother(y))
Knows(John, x) Knows(x, Mary)
49
Unification
● UNIFY(p, q) = θ if pθ =
qθ
θ
p s(John,
Know x) K n o w s ( J o h n , J ane ) { x/ J ane }
Knows(John, x) Knows (y, M a r y )
q s(John,
Know x) Knows (y, Mother(y))
Knows(John, x) Knows(x, Mary)
50
Unification
● UNIFY(p, q) = θ if pθ =
qθ
θ
p s(John,
Know x) K n o w s ( J o h n , J ane ) {x/ J ane }
Knows(John, x) Knows (y, M a r y ) {x/Mary, y/John}
q s(John,
Know x) Knows (y, Mother(y)) {y/John,
Knows(John, x) Knows(x, Mary) x/Mother(John)}
51
Unification
● UNIFY(p, q) = θ if pθ =
qθ
θ
p s(John,
Know x) K n o w s ( J o h n , J ane ) {x/ J ane }
Knows(John, x) Knows (y, M a r y ) {x/Mary, y/John}
q s(John,
Know x) Knows (y, Mother(y)) {y/John,
Knows(John, x) Knows(x, Mary) x/Mother(John)} fa i l
52
Generalized Modus
● Generalized modus Ponens
ponens used with KB of definite clauses
(exactly one positive literal)
p 1′, p2′, . . . , p n ′ , (p 1 ∧ p2 ∧ . . . ∧ p n ⇒ q)
where p i ′ θ = piθ for all i
qθ
● Rule: K i n g ( x ) ∧ Greedy(x) ⇒ E v i l ( x )
● Precondition of rule: p1 is K i n g ( x ) p2 is Greedy(x)
● Implication: q is E v i l ( x )
● Facts: p1′ is p2′ is Greedy(y)
● Substitution: θK iis
n g{x/John,
( J o h n ) y/John}
55
FORWARD CHAINING
1. First-order definite clauses
56
Example
Knowledge
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.
57
Example Knowledge
American(x) ∧ Base
● . . . it is a crime for an American to sell weapons to hostile nations:
Weapon(y) ∧ Sells (x, y, z ) ∧ Hosti le(z) ⇒ C r i m i n a l ( x )
● Nono . . . has some missiles, i.e., ∃ x Owns(Nono, x ) ∧ Missile (x):
Owns(Nono, M 1 ) and Missile (M 1 )
● . . . all of its missiles were sold to it by Colonel West
∀ x M i s s i l e ( x ) ∧ Owns(Nono, x ) ⇒ Sells(West, x, Nono)
● Missiles are weapons:
M i s s i l e ( x ) ⇒ Weapon(x)
● An enemy of America counts as “hostile”:
En e my ( x , America) ⇒ Hosti le(x)
● West, who is American . . .
American(West)
● The country Nono, an enemy of America . . .
Enemy(Nono, America)
58
59
60
61
62
Forward Chaining
Algorithm
63
Hard Matching
Example
Diff(wa, nt) ∧ Diff(wa, sa) ∧
Diff(nt, q)Diff(nt, sa) ∧
Diff(q, nsw) ∧ Diff(q, sa) ∧
Diff(nsw, v) ∧ Diff(nsw, sa)
∧ Diff(v, sa) =⇒
Colorable()
Diff(Red, Blue) Diff(Red, Green)
Diff(Green, Diff(Green,
Red) Diff(Blue, Blue )
Red) Diff(Blue, Green)
65
66
67
68
69
70
71
72
73
74
Logic programming
• Prolog is by far the most widely used logic programming
language.
• Prolog programs are sets of definite clauses written in a notation
different from standard first-order logic.
• Prolog uses uppercase letters for variables and lowercase for
constants.
• Clauses are written with the head preceding the body; " : -" is
used for left implication, commas separate literals in the body,
and a period marks the end of a sentence
criminal(X) :- american(X), weapon(Y), sells(X,Y,Z), hostile(Z).
75
Prolog program for append (X, Y, Z), which succeeds if list Z is the result of
appending lists x and Y
append([],X,Y).
append([A|X],Y,[A|Z]) :- append(X,Y,Z).
For example, we can ask the query append(X,Y,[1,2]): what two lists can be
appended to give [1,2]? We get back the solutions
X=[] Y=[1,2];
X=[1] Y=[2];
X=[1,2] Y=[]
76
The execution of Prolog programs is done via depth-first backward chaining
• Prolog allows a form of negation called negation as failure. A negated goal not P
is considered proved if the system fails to prove p. Thus, the sentence
• Alive (X) : - not dead(X) can be read as "Everyone is alive if not provably dead."
• Prolog has an equality operator, =, but it lacks the full power of logical equality.
• An equality goal succeeds if the two terms are unifiable and fails otherwise.
• So X+Y=2+3 succeeds with x bound to 2 and Y bound to 3, but
Morningstar=evening star fails.
• The occur check is omitted from Prolog's unification algorithm.
77
Redundant inference and infinite loops:
Consider the following logic program that decides if a path exists between two points on
a directed graph.
path(A,B) :- link(A,B).
path(A,B) :- path(A,B), link(A,B).
78
79
Database semantics of Prolog
80
Constraint logic programming
triangle(X,Y,Z) :-
X>0, Y>0, Z>0, X+Y>=Z, Y+Z>=X, X+Z>=Y.
If we have a query, triangle (3, 4, and 5) works fine but the query like,
triangle (3, 4, Z) no solution.
81
Resolution
As in the propositional case, first-order resolution requires that sentences be in
conjunctive normal form (CNF) that is, a conjunction of clauses, where each
clause is a disjunction of literals.
∀ x [ A n im a l ( F ( x ) ) ∧ ¬Loves(x, F ( x ) ) ] ∨
Loves (G(x), x )
6. Distribute ∧ over ∨:
{x/y}
{y/M1}
{x/M1} {z/Nono}
M i s s i l e ( M 1 ) and
Owns(Nono, M 1 )
{x/Nono}
86
Now we apply the conversion procedure to convert
each sentence to CNF:
A1. Animal(F(x)) ∨ Loves(G(x), x)
A2. ¬ Loves(x, F(x)) ∨ Loves(G(x), x)
B. ¬ Loves(y, x) ∨ ¬ Animal (z) ∨ ¬ Kills(x, z)
C. ¬ Animal(x) ∨ Loves(Jack, x)
D. Kills(Jack, Tuna) ∨ Kills(Curiosity, Tuna)
E. Cat(Tuna)
F. ¬ Cat(x) ∨ Animal (x)
¬ G. ¬ Kills(Curiosity, Tuna)
87
88
Completeness of resolution
89
1. First, we observe that if S is unsatisfiable, then there exists a particular set of ground
instances of the clauses of S such that this set is also unsatisfiable (Herbrand’s theorem).
2. We then appeal to the ground resolution theorem given in Chapter 7, which states that
propositional resolution is complete for ground sentences.
3. We then use a lifting lemma to show that, for any propositional resolution proof using
the set of ground sentences, there is a corresponding first-order resolution proof using
the first-order sentences from which the ground sentences were obtained.
90
Herbrand universe: If S is a set of clauses, then HS, the Herbrand
universe of S, is the set of all ground terms constructable from the
following:
a. The function symbols in S, if any.
b. The constant symbols in S, if any; if none, then the constant
symbol A.
For example, if S contains just the clause ¬ P(x, F(x,A))∨ ¬
Q(x,A)∨R(x,B), then
HS is the following infinite set of ground terms:
{A,B, F(A,A), F(A,B), F(B,A), F(B,B), F(A,F(A,A)), . . .} .
91
Saturation: If S is a set of clauses and P is a set of ground terms, then
P(S), the
saturation of S with respect to P, is the set of all ground clauses
obtained by applying
all possible consistent substitutions of ground terms in P with variables
in S.
92
Herbrand base: The saturation of a set S of clauses with respect to its
Herbrand universe
is called the Herbrand base of S, written as HS(S). For example, if S
contains
solely the clause just given, then HS(S) is the infinite set of clauses
{ ¬ P(A, F(A,A)) ∨ ¬ Q(A,A) ∨ R(A,B),
¬ P(B,F(B,A)) ∨ ¬ Q(B,A) ∨ R(B,B),
¬ P(F(A,A), F(F(A,A),A)) ∨ ¬ Q(F(A,A),A) ∨ R(F(A,A),B),
¬ P(F(A,B), F(F(A,B),A)) ∨ ¬ Q(F(A,B),A) ∨ R(F(A,B),B), . . . }
93
Thank you!
94