Module - 4 Notes - AI (BAD402)
Module - 4 Notes - AI (BAD402)
MODULE – 4
SYLLABUS
First Order Logic: Representation Revisited, Syntax and Semantics of First Order logic,
Using First Order logic.
Inference in First Order Logic: Propositional Versus First Order Inference, Unification,
Forward Chaining, Backward Chaining, Resolution
Text book 1: Chapter 8 – 8.1, 8.2, 8.3 Chapter 9 – 9.1, 9.2, 9.3, 9.4, 9.5
ARTIFICIAL INTELLIGENCE
SESSION - 23
1. Representation Revisited
A second drawback of data structures in programs is the lack of any easy way
to say, for example, ―There is a pit in [2,2] or [3,1]‖ or ―If the wumpus is in
[1,1] then he is not in [2,2].‖ Programs can store a single value for each
variable, and some systems allow the value to be ―unknown,‖ but they lack
the expressiveness required to handle partial information.
1
ARTIFICIAL INTELLIGENCE [ BAD402 ]
In a first-order logic reasoning system that uses CNF, we can see that the
linguistic form ―¬(A ∨ B)‖ and ―¬A 𝖠 ¬B‖ are the same because we can
look inside the system and see that the two sentences are stored as the same
canonical CNF form.
When we look at the syntax of natural language, the most obvious elements
are nouns and noun phrases that refer to objects (squares, pits, wumpuses)
and verbs and verb phrases that refer to relations among objects (is breezy, is
adjacent to, shoots). Some of these relations are functions— relations in
which there is only one ―value‖ for a given ―input.‖
2
ARTIFICIAL INTELLIGENCE [ BAD402 ]
•―One plus two equals three.‖ Objects: one, two, three, one plus two; Relation:
equals; Function: plus. (―One plus two‖ is a name for the object that is obtained
by applying the function ―plus‖ to the objects ―one‖ and ―two.‖ ―Three‖ is
another name for this object.)
• ―Evil King John ruled England in 1200.‖ Objects: John, England, 1200;
Relation: ruled; Properties: evil, king.
The primary difference between propositional and first-order logic lies in the
ontological commitment made by each language—that is, what it assumes about
the nature of reality. Mathematically, this commitment is expressed through the
nature of the formal models with respect to which the truth of sentences is
defined.
• For example, propositional logic assumes that there are facts that either hold or
do not hold in the world. Each fact can be in one of two states: true or false, and
each model assigns true or false to each proposition symbol.
• First-order logic assumes more; namely, that the world consists of objects with
certain relations among them that do or do not hold.
• Temporal logic assumes that facts hold at particular times and that those times
(which may be points or intervals) are ordered.
3
ARTIFICIAL INTELLIGENCE [ BAD402 ]
Questions
1
2
3
4
ARTIFICIAL INTELLIGENCE [ BAD402 ]
SESSION - 24
The models of a logical language are the formal structures that constitute the possible
worlds under consideration. Each model links the vocabulary of the logical sentences
to elements of the possible world, so that the truth of any sentence can be determined.
Thus, models for propositional logic link proposition symbols to predefined truth
values.
Models for first-order logic are much more interesting. First, they have objects in
them! The domain of a model is the set of objects. The domain is required to be
nonempty—every possible world must contain at least one object.
The objects in the model may be related in various ways. In the figure, Richard and
John are brothers.
A relation is just the set of tuples of objects that are related. (A tuple is a collection of
objects arranged in a fixed order and is written with angle brackets surrounding the
objects.)
Thus, the brotherhood relation in this model is the set
{ <Richard the Lionheart, King John>, <King John, Richard the Lionheart >} (8.1)
5
ARTIFICIAL INTELLIGENCE [ BAD402 ]
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. The model also contains unary relations, or properties: the
―person‖ property is true of both Richard and John; the ―king‖ property is true
only of John (presumably because Richard is dead at this point); and the
―crown‖ property is true only of the crown.
The basic syntactic elements of first-order logic are the symbols that stand for
objects, relations, and functions. The symbols, therefore, come in three kinds:
We adopt the convention that these symbols will begin with uppercase letters.
For example, we might use
6
ARTIFICIAL INTELLIGENCE [ BAD402 ]
Richard refers to Richard the Lionheart and John refers to the evil King John.
Brother refers to the brotherhood relation, that is, the set of tuples of objects
given in Equation (8.1); OnHead refers to the ―on head‖ relation that holds
between the crown and King John;
Thus, in addition to its 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.
One possible interpretation for our example—which a logician would call the
intended interpretation—is as follows:
LeftLeg refers to the ―left leg‖ function, that is, the mapping given in Equation
(8.2).
There are many other possible interpretations, of course. For example, one
interpretation maps Richard to the crown and John to King John’s left leg. There
are five objects in the model, so there are 25 possible interpretations just for the
constant symbols Richard and John.
In summary, a model in first-order logic consists of a set of objects and an
interpretation that maps constant symbols to objects, predicate symbols to
relations on those objects, and function symbols to functions on those objects.
To get an idea of what the set of all possible models looks like, see Figure 8.4. It
7
ARTIFICIAL INTELLIGENCE [ BAD402 ]
shows that models vary in how many objects they contain—from one up to
infinity— and in the way the constant symbols map to objects. If there are two
constant symbols and one object, then both symbols must refer to the same
object; but this can still happen even with more objects. When there are more
objects than constant symbols, some of the objects will have no names. Because
the number of possible models is unbounded, checking entailment by the
enumeration of all possible models is not feasible for first-order logic.
iii. Terms
This is what function symbols are for: instead of using a constant symbol, we
use LeftLeg(John).
For example, suppose the LeftLeg function symbol refers to the function shown
in Equation (8.2) and John refers to King John, then LeftLeg(John) refers to
King John’s left leg. In this way, the interpretation fixes the referent of every
term.
8
ARTIFICIAL INTELLIGENCE [ BAD402 ]
This states, under the intended interpretation given earlier, that Richard the
Lionheart is the brother of King John. Atomic sentences can have complex
terms as arguments.
Thus,
Married(Father (Richard), Mother (John))
states that Richard the Lionheart’s father is married to King John’s mother
(again, under a suitable interpretation).
v. Complex sentences
We can use logical connectives to construct more complex sentences, with the
same syntax and semantics as in propositional calculus.
Here are four sentences that are true in the model of Figure 8.2 under our
intended interpretation:
¬Brother (LeftLeg(Richard), John)
Brother (Richard, John) 𝖠 Brother (John, Richard) King(Richard) ∨ King(John)
¬King(Richard) ⇒ King(John) .
vi. Quantifiers
9
ARTIFICIAL INTELLIGENCE [ BAD402 ]
The sentence ∀ x P, where P is any logical expression, says that P is true for
every object x. More precisely, ∀ x P is true in a given model if P is true in all
possible extended interpretations constructed from the interpretation given in
the model, where each extended interpretation specifies a domain element to
which x refers.
To say, for example, that King John has a crown on his head, we write
∃ x Crown(x) 𝖠 OnHead(x, John).
10
ARTIFICIAL INTELLIGENCE [ BAD402 ]
Using 𝖠 as the main connective with ∀ led to an overly strong statement in the
example in the previous section; using ⇒ with ∃ usually leads to a very weak
statement, indeed.
C. Nested quantifiers
Consecutive quantifiers of the same type can be written as one quantifier with
several variables. For example, to say that siblinghood is a symmetric
relationship, we can write ∀ x, y Sibling(x, y) ⇔ Sibling(y, x) .
In other cases we will have mixtures. ―Everybody loves somebody‖ means that
for every person, there is someone that person loves: ∀ x ∃ y Loves(x, y).
On the other hand, to say ―There is someone who is loved by everyone,‖ we
write
∃ y ∀ x Loves(x, y) .
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) .
11
ARTIFICIAL INTELLIGENCE [ BAD402 ]
We can go one step further: ―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).
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) .
Thus, we do not really need both ∀ and ∃, just as we do not really need both 𝖠
and ∨.
vii. Equality
First-order logic includes one more way to make atomic sentences, other than
using a predicate and terms as described earlier. We can use the equality symbol
to signify that two terms refer to the same object.
For example,
Father (John) = Henry
says that the object referred to by Father (John) and the object referred to by
Henry are the same. Because an interpretation fixes the referent of any term,
determining the truth of an equality sentence is simply a matter of seeing that
the referents of the two terms are the same object.
The equality symbol can be used to state facts about a given function, as we just
did for the Father symbol. 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, we would write ∃ x, y Brother (x,
Richard) 𝖠 Brother (y, Richard) 𝖠 ¬(x = y) .
12
ARTIFICIAL INTELLIGENCE [ BAD402 ]
Questions
1
2
3
4
5
13
ARTIFICIAL INTELLIGENCE [ BAD402 ]
SESSION - 25
For example, we can 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)) .
Questions asked with ASK are called queries or goals. Generally speaking, any
query that is logically entailed by the knowledge base should be answered
affirmatively.
The answer is true, but this is perhaps not as helpful as we would like. It is
rather like answering ―Can you tell me the time?‖ with ―Yes.‖ If we want to
know what value of x makes the sentence true, we will need a different function,
ASKVARS, which we call with ASKVARS(KB,Person(x)) and which yields a
stream of answers. In this case there will be two answers: {x/John} and
{x/Richard}. Such an answer is called a substitution or binding list.
ASKVARS is usually reserved for knowledge bases consisting solely of Horn
clauses, because in such knowledge bases every way of making the query true
will bind the variables to specific value.
14
ARTIFICIAL INTELLIGENCE [ BAD402 ]
Clearly, the objects in our domain are people. We have two unary predicates,
Male and Female. Kinship relations—parenthood, brotherhood, marriage, and
so on—are represented by binary predicates: Parent, Sibling, Brother, Sister,
Child, Daughter, Son, Spouse, Wife, Husband, Grandparent, Grandchild,
Cousin, Aunt, and Uncle.
We can go through each function and predicate, writing down what we know in
terms of the other symbols.
For example,
one’s mother is one’s female parent:
∀ m, c Mother (c) = m ⇔ Female(m) 𝖠 Parent(m, c) .
Our kinship axioms are also definitions; they have the form ∀ x, y P(x, y) ⇔ ….
The axioms define the Mother function and the Husband, Male, Parent,
Grandparent, and Sibling predicates in terms of other predicates.
Not all logical sentences about a domain are axioms. Some are theorems—that
is, they are entailed by the axioms. For example, consider the assertion that
siblinghood is symmetric:
∀ x, y Sibling(x, y) ⇔ Sibling(y, x)
Not all axioms are definitions. Some provide more general information about
certain predicates without constituting a definition. Indeed, some predicates
have no complete definition because we do not know enough to characterize
them fully.
For example, there is no obvious definitive way to complete the sentence
∀ x Person(x) ⇔ ...
15
ARTIFICIAL INTELLIGENCE [ BAD402 ]
Axioms can also be ―just plain facts,‖ such as Male(Jim) and Spouse(Jim,
Laura). Such facts form the descriptions of specific problem instances, enabling
specific questions to be answered.
Numbers are perhaps the most vivid example of how a large theory can be built
up from a tiny kernel of axioms. We describe here the theory of natural numbers
or non-negative integers.
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.
16
ARTIFICIAL INTELLIGENCE [ BAD402 ]
2. The empty set has no elements adjoined into it. In other words, there is no
way to decompose
∀ x, s x∈ s ⇔ s = {x|s} .
4. The only members of a set are the elements that were adjoined into it.
∀ x, s x∈ s ⇔ ∃ y, s2 (s = {y|s2} 𝖠 (x = y ∨ x∈ s2)) .
5. 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) .
6. Two sets are equal if and only if each is a subset of the other:
8. An object is in the union of two sets if and only if it is a member of either set:
∀ x, s1, s2 x∈ (s1 𝖴 s2) ⇔ (x∈ s1 ∨ x∈s2) .
17
ARTIFICIAL INTELLIGENCE [ BAD402 ]
Lists are similar to sets. The differences are that lists are ordered and the same
element can appear more than once in a list
18
ARTIFICIAL INTELLIGENCE [ BAD402 ]
Questions
1
2
19
ARTIFICIAL INTELLIGENCE [ BAD402 ]
SESSION - 26
INFERENCE IN FIRST-ORDER LOGIC
Thus a universally quantified sentence can be replaced by the set of all possible
instantiations.
20
ARTIFICIAL INTELLIGENCE [ BAD402 ]
Then we apply UI to the first sentence using all possible ground term
substitutions from the vocabulary of the knowledge base-in this case, {xl John)
and {x/Richard). We obtain
21
ARTIFICIAL INTELLIGENCE [ BAD402 ]
Disadvantage:
If the knowledge base includes a function symbol, the set of possible ground
term substitutions is infinite. Propositional algorithms will have difficulty with
an infinitely large set of sentences.
NOTE:
Entailment for first-order logic is semi decidable which means algorithms exist
that say yes to every entailed sentence, but no algorithm exists that also says no
to every non entailed sentence
Questions
1
2
22
ARTIFICIAL INTELLIGENCE [ BAD402 ]
SESSION - 27
5. Unification
Consider the above discussed example, if we add Siblings (Peter, Sharon) to the
knowledge base then it will be
Removing Universal Quantifier will add new sentences to the knowledge base
which are not necessary for the query Evil (John)?
Hence we need to teach the computer to make better inferences. For this
purpose Inference rules were used.
For atomic sentences pi, pi ', and q, where there is a substitution θ such that
SUBST( θ , pi ) = SUBST(θ , pi '), for all i,
p1 ', p2 ', …, pn ', (p1 𝖠 p2 𝖠 … 𝖠 pn ⇒ q)
SUBST (θ, q)
23
ARTIFICIAL INTELLIGENCE [ BAD402 ]
For example,
Unification:
It is the process used to find substitutions that make different logical expressions
look identical.
UNIFY (p, q) = θ where SUBST (θ, p) = SUBST (θ, q) θ is our unifier value (if
one exists). Ex:
―Who does John know?‖
UNIFY (Knows (John, x), Knows (John, Jane)) = {x/ Jane}. UNIFY (Knows
(John, x), Knows (y, Bill)) = {x/Bill, y/ John}.
UNIFY (Knows (John, x), Knows (y, Mother(y))) = {x/Bill, y/ John} UNIFY
(Knows (John, x), Knows (x, Elizabeth)) = FAIL
The last unification fails because both use the same variable, X. X can’t equal
both John and Elizabeth. To avoid this change the variable X to Y (or any other
value) in Knows(X, Elizabeth)
24
ARTIFICIAL INTELLIGENCE [ BAD402 ]
Easy way to implement these functions is Store all sentences in a long list,
browse list one sentence at a time with UNIFY on an ASK query. But this is
inefficient.
To make FETCH more efficient by ensuring that unifications are attempted only
with sentences that have some chance of unifying. (i.e. Knows(John, x) vs.
Brother(Richard, John) are not compatible for unification)
To avoid this, a simple scheme called predicate indexingputs all the Knows
facts in one bucket and all the Brother facts in another.
25
ARTIFICIAL INTELLIGENCE [ BAD402 ]
The buckets can be stored in a hash table for efficient access. Predicate indexing
is useful when there are many predicate symbols but only a few clauses for each
symbol.
But if we have many clauses for a given predicate symbol, facts can be stored
under multiple index keys.
For the fact Employs ([Link], Richard), the queries are Employs (A IMA.
org, Richard)
Does [Link] employ Richard? Employs (x, Richard) who employs Richard?
Employs ([Link], y) whom does [Link] employ?
Employs Y(x), who employs whom?
Questions
1
2
26
ARTIFICIAL INTELLIGENCE [ BAD402 ]
SESSION - 28
6. Forward Chaining
Rule 7. American(West)
• The country Nono, an enemy of America . . .
27
ARTIFICIAL INTELLIGENCE [ BAD402 ]
➢ The process repeats until the query is answered or no new facts are added.
Notice that a fact is not "new" if it is just renamingof a known fact.
We will use our crime problem to illustrate how FOL-FC-ASK works. The
implication sentences are (1), (4), (5), and (6). Two iterations are required:
Rule (4) is satisfied with {x/Ml), and Sells (West, M1, Nono) is added. Rule (5)
is satisfied with
{x/M1) and Weapon (M1) is added.
Rule (6) is satisfied with {x/Nono}, and Hostile (Nono) is added.
➢ On the second iteration, rule (1) is satisfied with {x/West, Y/MI, z /Nono),
and Criminal
(West) is added.
It is sound, because every inference is just an application of Generalized Modus
Ponens, it is completefor definite clause knowledge bases; that is, it answers
every query whose answers are entailed by any knowledge base of definite
clauses
28
ARTIFICIAL INTELLIGENCE [ BAD402 ]
29
ARTIFICIAL INTELLIGENCE [ BAD402 ]
―Every new fact inferred on iteration t must be derived from at leastone new fact
inferred on iteration t – 1‖.
This observation leads naturally to an incremental forward chaining algorithm
where, at iteration t, we check a rule only if its premise includes a conjunct p, that
unifies with a fact p: newly inferred at iteration t - 1. The rule matching step then
fixes p, to match with p’, but allows the other conjuncts of the rule to match with
facts from any previous iteration.
3. Irrelevant facts:
One way to avoid drawing irrelevant conclusions is to use backward chaining.
Another solution is to restrict forward chaining to a selected subset of rules
A third approach, is to rewrite the rule set, using information from the [Link] that
only relevant variable bindings-those belonging to a so-called magic set-are
considered during forward inference.
For example, if the goal is Criminal (West), the rule that concludes Criminal (x)
will be rewritten to include an extra conjunct that constrains the value of x:
Magic(x) A American(z) A Weapon(y)A Sells(x, y, z) A Hostile(z) =>Criminal(x )
The fact Magic (West) is also added to the KB. In this way, even if the knowledge
base contains facts about millions of Americans, only Colonel West will be
considered during the forward inference process.
Questions
1
2
3
4
5
6
30
ARTIFICIAL INTELLIGENCE [ BAD402 ]
SESSION - 29
7. Backward Chaining
This algorithm work backward from the goal, chaining through rules to find known
facts that support the proof. It is called with a list of goals containing the original
query, and returns the set of all substitutions satisfying the query. The algorithm
takes the first goal in the list and finds every clause in the knowledge base whose
head, unifies with the goal. Each such clause creates a new recursive call in which
body, of the clause is added to the goal stack .Remember that facts are clauses with
a head but no body, so when a goal unifies with a known fact, no new sub goals are
added to the stack and the goal is solved. The algorithm for backward chaining and
proof tree for finding criminal (West) using backward chaining are given below.
31
ARTIFICIAL INTELLIGENCE [ BAD402 ]
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
Prolog includes "syntactic sugar" for list notation and arithmetic. Prolog program
for append
(X, Y, Z), which succeeds if list Z is the result of appending lists x and Y
For example, we can ask the query append (A, B, [1, 2]): what two lists can be
appended to give [1, 2]? We get back the solutions
32
ARTIFICIAL INTELLIGENCE [ BAD402 ]
First, instead of constructing the list of all possible answers for each sub goal before
continuing to the next, Prolog interpreters generate one answer and a "promise" to
generate the rest when the current answer has been fully explored. This promise is
called a choice [Link]-BC-
ASK spends a good deal of time in generating and composing substitutions
when a path in search fails. Prolog will backup to previous choice point and unbind
some variables.
33
ARTIFICIAL INTELLIGENCE [ BAD402 ]
that is suitable for prolog and can be either translated or interpreted into
machine language.
A simple three-node graph, described by the facts link (a, b) and link (b, c)
34
ARTIFICIAL INTELLIGENCE [ BAD402 ]
• If we have a query, triangle (3, 4, and 5) works fine but the query like,
triangle (3, 4, Z) no solution.
• The difficulty is variable in prolog can be in one of two states i.e.,
Unbound or bound.
• Binding a variable to a particular term can be viewed as an extreme form
of constraint namely
Questions
1
2
3
4
5
6
35
ARTIFICIAL INTELLIGENCE [ BAD402 ]
SESSION - 30
8. Resolution
Step 2. Move Negation inwards: In addition to the usual rules for negated
connectives, we need rules for negated quantifiers. Thus, we have
Step 3. Standardize variables: For sentences like which use the same variable
name twice, change the name of one of the variables. This avoids confusion
later when we drop the quantifiers. Thus, we have
36
ARTIFICIAL INTELLIGENCE [ BAD402 ]
Which has the wrong meaning entirely: it says that everyone either fails to love
a particular animal A or is loved by some particular entity B. In fact, our original
sentence allows each person to fail to love a different animal or to be loved by a
different person.
Here F and G are Skolem functions. The general rule is that the arguments of
the Skolem function are all the universally quantified variables in whose scope
the existential quantifier appears.
Step 5. Drop universal quantifiers: At this point, all remaining variables must be
universally quantified. Moreover, the sentence is equivalent to one in which all
the universal quantifiers have been moved to the left. We can therefore drop the
universal quantifiers
37
ARTIFICIAL INTELLIGENCE [ BAD402 ]
By eliminating the complementary literals Loves (G(x), x) and ¬Loves (u, v),
with unifier θ =
Questions
1
2
3
4
*****
38