First Order Logic in AI: Syntax & Semantics
First Order Logic in AI: Syntax & Semantics
Syllabus:
First Order Logic: Representation Revisited, Syntax and Semantics of First Order logic, Using First
Order logic, Knowledge Engineering In First-Order Logic
Inference in First Order Logic: Propositional Versus First Order Inference, Unification ,Forward
Chaining
Chapter 8- 8.1, 8.2, 8.3, 8.4
Chapter 9- 9.1, 9.2, 9.3
Text book: Stuart J. Russell and Peter Norvig, Artificial Intelligence, 3rd Edition, Pearson, 2015
• The language of thought
• Combining the best of natural and formal languages.
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 .
1
Prof. Salma Itagi,Dept. of CSE,SVIT
MODULE 4 ARTIFICIAL INTELLIGENCE(BCS515B)
2
Prof. Salma Itagi,Dept. of CSE,SVIT
MODULE 4 ARTIFICIAL INTELLIGENCE(BCS515B)
3
Prof. Salma Itagi,Dept. of CSE,SVIT
MODULE 4 ARTIFICIAL INTELLIGENCE(BCS515B)
Atomic sentence
Brother(Richard,John)
Complex sentence
¬Brother (LeftLeg(Richard), John)
Brother (Richard , John) ∧ Brother (John,Richard) King(Richard ) ∨ King(John)
¬King(Richard) ⇒ King(John)
1. First-order logic contains two standard quantifiers, called universal and existential
“All kings are persons”
∀ x King(x) ⇒ Person(x) .
Universal quantification makes statements about every object.
A statement about some object in the universe without naming it, by using an existential quantifier.
King John has a crown on his head ∃ x Crown(x) ∧ OnHead(x, John) .
“For all x, if x is a king, then x is a person.” The symbol x is called a variable.
By convention, variables are lowercase letters.
A variable is a term all by itself, and as such can also serve as the argument of a function—for example,
LeftLeg(x).
A term with no variables is called a ground term.
4
Prof. Salma Itagi,Dept. of CSE,SVIT
MODULE 4 ARTIFICIAL INTELLIGENCE(BCS515B)
•
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.
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, we can assert that John is a king, Richard is a
person, and all kings are persons:
• TELL(KB, King(John)) .
5
Prof. Salma Itagi,Dept. of CSE,SVIT
MODULE 4 ARTIFICIAL INTELLIGENCE(BCS515B)
• TELL(KB, Person(Richard)) .
• TELL(KB, ∀ x King(x) ⇒ Person(x)) .
• We can ask questions of the knowledge base using ASK.
• For example, ASK(KB, King(John)) returns true.
• Questions asked with ASK are called queries or goals.
2. The kinship domain
• It is the domain of family relationships, or kinship.
• This domain includes facts such as “Elizabeth is the mother of Charles” and “Charles is the father
of William” and rules such as “One’s grandmother is the mother of one’s parent.”
• 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.
• For example, one’s mother is one’s female parent:
∀ m, c Mother (c)=m ⇔ Female(m) ∧ Parent(m, c)
Few more examples:
1. Male and female are disjoint categories:
∀ x Male(x) ⇔ ¬Female(x) .
[Link] and child are inverse relations:
∀ p, c Parent(p, c) ⇔ Child (c, p) .
3. A grandparent is a parent of one’s parent:
∀ g, c Grandparent (g, c) ⇔ ∃p Parent(g, p) ∧ Parent(p, c) .
4. A sibling is another child of one’s parents:
∀ x, y Sibling(x, y) ⇔ x = y ∧ ∃p Parent(p, x) ∧ Parent(p, y) .
[Link],Sets and lists
• The theory of natural numbers or non-negative integers.
• Here we need a predicate NatNum that will be true of natural numbers; we need one constant
symbol, 0; and we need one function symbol, S (successor).
• The Peano axioms define natural numbers and addition.
• Natural numbers are defined recursively:
NatNum(0) . ∀ n NatNum(n) ⇒ NatNum(S(n))
• That is, 0 is a natural number, and for every object n, if n is a natural number, then S(n) is a natural
6
Prof. Salma Itagi,Dept. of CSE,SVIT
MODULE 4 ARTIFICIAL INTELLIGENCE(BCS515B)
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:
∀ s1, s2 (s1 =s2) ⇔ (s1 ⊆ s2 ∧ s2 ⊆ s1)
7. An object is in the intersection of two sets if and only if it is a member of both sets:
∀ x, s1, s2 x∈ (s1 ∩ s2) ⇔ (x∈ s1 ∧ x∈s2) .
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) .
• Lists are similar to sets.
• The differences are that lists are ordered and the same element can appear more than once in a list.
• We can use the vocabulary of Lisp for 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. List? is a predicate that is true only of lists.
• The empty list is [ ]. The term Cons(x, y), where y is a nonempty list, is written [x|y].
• The term Cons(x, Nil) (i.e., the list containing the element x) is written as [x].
• A list of several elements, such as [A,B,C], corresponds to the nested term Cons(A, Cons(B,
Cons(C, Nil))).
3. The WUMPUS WORLD
The wumpus agent receives a percept vector with five elements.
Percept ([Stench, Breeze, Glitter , None, None]
• The actions in the wumpus world can be represented by logical terms:
• Turn(Right ), Turn(Left ), Forward , Shoot , Grab, Climb .
• If the agent is at a square and perceives a breeze, then that square is breezy:
∀ s, t At(Agent, s, t) ∧ Breeze(t) ⇒ Breezy(s)
∀ s Breezy(s) ⇔ ∃r Adjacent (r, s) ∧ Pit(r) .
• STEPS in Knowledge Engineering Process
1. Identify the task. The knowledge engineer must delineate the range of questions that the
knowledge base will support and the kinds of facts that will be available for each specific problem
instance.
2. Assemble the relevant knowledge. The knowledge engineer might already be an expert in the domain,
or might need to work with real experts to extract what they know—a process called knowledge
acquisition.
8
Prof. Salma Itagi,Dept. of CSE,SVIT
MODULE 4 ARTIFICIAL INTELLIGENCE(BCS515B)
•
Identify the TASK
10
Prof. Salma Itagi,Dept. of CSE,SVIT
MODULE 4 ARTIFICIAL INTELLIGENCE(BCS515B)
• What do we know about digital circuits? For our purposes, they are composed of wires and gates.
• Signals flow along wires to the input terminals of gates, and each gate produces a signal on the
output terminal that flows along another wire.
• To determine what these signals will be, we need to know how the gates transform their input
signals.
• There are four types of gates: AND, OR, and XOR gates have two input terminals, and NOT gates
have one.
• All gates have one output terminal.
• Circuits, like gates, have input and output terminals.
Decide on Vocabulary
• The next step is to choose functions, predicates, and constants to represent them.
• First, we need to be able to distinguish gates from each other and from other objects.
• Each gate is represented as an object named by a constant, about which we assert that it is a gate
with, say, Gate(X1).
• The behavior of each gate is determined by its type: one of the constants AND,OR, XOR, or NOT.
• Because a gate has exactly one type, a function is appropriate: Type(X1)=XOR.
• Circuits, like gates, are identified by a predicate: Circuit(C1).
• Next we consider terminals, which are identified by the predicate Terminal (x).
• A gate or circuit can have one or more input terminals and one or more output terminals.
• We use the function In(1,X1) to denote the first input terminal for gate X1.
• A similar function Out is used for output terminals.
• The function Arity(c, i, j) says that circuit c has i input and j output terminals.
• The connectivity between gates can be represented by a predicate, Connected, which takes two
terminals as arguments, as in Connected(Out(1,X1), In(1,X2)).
• Finally, we need to know whether a signal is on or off.
• One possibility is to use a unary predicate, On(t), which is true when the signal at a terminal is on.
• This makes it a little difficult, however, to pose questions such as “What are all the possible values
of the signals at the output terminals of circuit C1 ?”
• We therefore introduce as objects two signal values, 1 and 0, and a function Signal (t) that denotes
11
Prof. Salma Itagi,Dept. of CSE,SVIT
MODULE 4 ARTIFICIAL INTELLIGENCE(BCS515B)
• Gate(A1) ∧ Type(A1)=AND
• Gate(A2) ∧ Type(A2)=AND
• Gate(O1) ∧ Type(O1)=OR .
• X1 (XOR): Computes A ⊕ B.
• X2 (XOR): Computes (A ⊕ B) ⊕ C_in to produce the Sum output.
• A1 (AND): Computes A ∧ B.
• A2 (AND): Computes C_in ∧ (A ⊕ B).
• O1 (OR): Computes the Carry-out (C_out) as the OR of the two AND gates' outputs.
• Then, we show the connections between them:
• Connected(Out(1,X1), In(1,X2)) Connected(In(1,C1), In(1,X1))
• Connected(Out(1,X1), In(2,A2)) Connected(In(1,C1), In(1,A1))
• Connected(Out(1,A2), In(1,O1)) Connected(In(2,C1), In(2,X1))
• Connected(Out(1,A1), In(2,O1)) Connected(In(2,C1), In(2,A1))
• Connected(Out(1,X2), Out(1,C1)) Connected(In(3,C1), In(2,X2))
• Connected(Out(1,O1), Out(2,C1)) Connected(In(3,C1), In(1,A2)) .
• 1. Connected(Out(1,X1), In(1,X2))
• Out(1, X1): The output of the first XOR gate X1.
• In(1, X2): The first input of the second XOR gate X2.
• This statement means that the output of X1 is connected to the first input of X2. In the full adder,
X1 calculates A ⊕ B, and this output is used as an input to X2, which calculates (A ⊕ B) ⊕ C_in
to produce the Sum.
• 2. Connected(In(1,C1), In(1,X1))
• In(1, C1): The first input of the circuit C1 (which has 3 inputs and 2 outputs, representing the 1-
bit full adder).
• In(1, X1): The first input of XOR gate X1.
• This connection means that A (from the inputs of the full adder) is connected to the first input of
XOR gate X1.
• 3. Connected(Out(1, X1), In(2, A2))
• Out(1, X1): The output of XOR gate X1.
• In(2, A2): The second input of AND gate A2.
• This means the output of X1 (which is A ⊕ B) is connected to the second input of the AND gate
13
Prof. Salma Itagi,Dept. of CSE,SVIT
MODULE 4 ARTIFICIAL INTELLIGENCE(BCS515B)
• This means that C_in is connected to the second input of X2, as it is used to compute (A ⊕ B) ⊕
C_in for the Sum output.
• 11. Connected(Out(1, O1), Out(2, C1))
• Out(1, O1): The output of OR gate O1 (the Carry-out).
• Out(2, C1): The second output of the full adder circuit C1.
• This means that the Carry-out from O1 is connected to the Carry-out output of the full adder
circuit.
• 12. Connected(In(3, C1), In(1, A2))
• In(3, C1): The third input of the full adder circuit C1.
• In(1, A2): The first input of AND gate A2.
• This means that C_in is connected to the first input of AND gate A2, which is used in the
calculation of C_in ∧ (A ⊕ B) for the carry computation.
Summary:
• The 1-bit full adder circuit involves three types of gates: XOR, AND, and OR.
• The inputs are A, B, and C_in (carry input), and the outputs are Sum and Carry-out.
• The various Connected statements describe how these gates interact with each other, ensuring the
proper calculation of the Sum and Carry-out.
• The Sum is calculated by XORing A, B, and C_in, and the Carry-out is calculated using AND
and OR gates, based on the inputs.
• The connections reflect the standard logic for a 1-bit full adder where:
• Sum = (A ⊕ B) ⊕ C_in.
• Carry-out = (A ∧ B) ∨ (C_in ∧ (A ⊕ B)).
Pose queries to the inference procedure
• What combinations of inputs would cause the first output of C1 (the sum bit) to be 0 and the second
output of C1 (the carry bit) to be 1?
• ∃ i1, i2, i3 Signal (In(1, C1))=i1 ∧ Signal (In(2, C1))=i2 ∧ Signal (In(3, C1))=i3∧ Signal (Out(1,
C1))=0 ∧ Signal (Out(2, C1))=1 .
• The answers are substitutions for the variables i1, i2, and i3 such that the resulting sentence is
entailed by the knowledge base.
• ASKVARS will give us three such substitutions:
• {i1/1, i2/1, i3/0} {i1/1, i2/0, i3/1} {i1/0, i2/1, i3/1} .
15
Prof. Salma Itagi,Dept. of CSE,SVIT
MODULE 4 ARTIFICIAL INTELLIGENCE(BCS515B)
• What are the possible sets of values of all the terminals for the adder circuit?
• ∃ i1, i2, i3, o1, o2 Signal (In(1, C1))=i1 ∧ Signal (In(2, C1))=i2
• ∧ Signal (In(3, C1))=i3 ∧ Signal (Out(1, C1))=o1 ∧ Signal (Out(2, C1))=o2 .
• {i1/1, i2/1, i3/0}: Sum = 1 ⊕ 1 ⊕ 0 = 0 (sum is 0) and carry-out = (1 ∧ 1) ∨ (1 ∧ 0) ∨ (1 ∧ 0) = 1
(carry-out is 1).
• Cout=(A∧B)∨(B∧Cin)∨(A∧Cin)
For example:
{i1=0, i2=0, i3=0, o1=0, o2=0}
Debug the knowledge base
16
Prof. Salma Itagi,Dept. of CSE,SVIT
MODULE 4 ARTIFICIAL INTELLIGENCE(BCS515B)
• The system is unable to infer the correct behavior of the XOR gate for certain input combinations,
like 1 and 0.
• This failure to infer is due to the lack of knowledge about the relationship between those inputs
(i.e., that 1 and 0 are different).
• By examining the axiom for the XOR gate and testing for the output at each gate, it becomes clear
that the system needs to be explicitly told about the condition 1 ≠ 0 in order to deduce the correct
output.
• Once this information is provided, the system can correctly infer that Signal (Out(1, X1)) = 1 when
the inputs are 1 and 0.
In essence, the problem is a missing or forgotten assertion that would allow the system to properly deduce
the XOR gate's output.
INFERENCE IN FIRST ORDER LOGIC
• Propositional vs First order inference
• Inference Rules for Quantifiers
All greedy kings are evil
• ∀ x King(x) ∧ Greedy(x) ⇒ Evil(x) .
• Then it seems quite permissible to infer any of the following sentences:
• King(John) ∧ Greedy(John) ⇒ Evil(John)
• King(Richard ) ∧ Greedy(Richard) ⇒ Evil(Richard)
• King(Father (John)) ∧ Greedy(Father (John)) ⇒ Evil(Father (John)) .
• The rule of Universal Instantiation (UI for short) says that we can infer any sentence obtained by
substituting a ground term (a term without variables) for the variable.
17
Prof. Salma Itagi,Dept. of CSE,SVIT
MODULE 4 ARTIFICIAL INTELLIGENCE(BCS515B)
18
Prof. Salma Itagi,Dept. of CSE,SVIT
MODULE 4 ARTIFICIAL INTELLIGENCE(BCS515B)
Premises
We have a series of n atomic sentences (propositions), denoted by:
• p1,p2,…, pn
• P1is: King(x)
• p2 is: Greedy(x)
Additionally, there is one implication involving these premises:
• (p1∧ p2 ∧⋯∧ p2n⇒q)
• This implication states that if all the premises p1,p2,…, pn are true, then the conclusion q must also
be true.
• (P1∧P2⇒q) where q is: Evil(x)
So, the implication says:
• "If P1 (King(x)) and P1 (Greedy(x)) are true, then q(Evil(x)) must also be true."
Substitution (θ)
The substitution θ is given as:
• θ={x/John,y/John}
This substitution says:
• Replace x with John in all occurrences of x.
• Replace y with John in all occurrences of y.
Thus, when you apply this substitution to a formula, you replace the variables x and y with the constant
"John."
Substitution Applied to the Conclusion (q)
The final part of the rule is applying the substitution θ to the consequent q, which is Evil(x) q is Evil(x)
meaning "x is evil."
19
Prof. Salma Itagi,Dept. of CSE,SVIT
MODULE 4 ARTIFICIAL INTELLIGENCE(BCS515B)
• After applying the substitution θ={x/John,y/John}, you replace the variable x with "John".
Therefore:
• SUBST(θ,q) becomes Evil(John)
So, after substitution, the conclusion becomes Evil(John), which asserts that John is evil.
Here’s a complete breakdown of how this rule works in the context of the given example:
1. Premises:
1. p1:King(x) (John is a King)
2. p2:Greedy (John is Greedy)
3. The rule: If p1and p2 are true, then q (Evil(x)) is true.
2. Implication (Rule):
1. (p1∧p2⇒q) which reads: "If John is a King and greedy, then John is evil."
3. Substitution:
1. θ={x/John,y/John}, meaning replace all instances of x and y with "John."
4. Conclusion after applying the substitution:
1. q=Evil(x), and after applying θ , it becomes Evil(John).
Thus, the result of applying the substitution θ to q is that John is evil.
UNIFICATION
• UNIFY(Knows(John, x), Knows(John, Jane)) = {x/Jane}
• Substitution: x→Jane
• UNIFY(Knows(John, x), Knows(y, Bill )) = {x/Bill, y/John}
• Substitution: x→Bill,y→John
• UNIFY(Knows(John, x), Knows(y,Mother (y))) = {y/John, x/Mother (John)}
• Substitution: y→John,x→Mother(John)
• UNIFY(Knows(John, x), Knows(x, Elizabeth)) = fail .
• Fail: The unification fails because of a contradiction in the substitution for x.
20
Prof. Salma Itagi,Dept. of CSE,SVIT
MODULE 4 ARTIFICIAL INTELLIGENCE(BCS515B)
•
Example 1:
We want to unify the terms
Knows(John, x) and Knows(John, Jane).
• The operator Knows is the same for both terms.
• The first argument is John in both cases, so they are already unified.
• The second argument is x in the first term and Jane in the second term. We need to unify x with
Jane.
The unification is possible, and the substitution becomes {x → Jane}.
FORWARD CHAINING
• The idea is simple: start with the atomic sentences in the knowledge base and apply Modus Ponens
in the forward direction, adding new atomic sentences, until no further inferences can be made.
21
Prof. Salma Itagi,Dept. of CSE,SVIT
MODULE 4 ARTIFICIAL INTELLIGENCE(BCS515B)
Eg:
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 ColonelWest, who is
American.
American(x) ∧Weapon(y) ∧ Sells(x, y, z) ∧ Hostile(z) ⇒ Criminal (x) .
22
Prof. Salma Itagi,Dept. of CSE,SVIT
MODULE 4 ARTIFICIAL INTELLIGENCE(BCS515B)
24
Prof. Salma Itagi,Dept. of CSE,SVIT
MODULE 4 ARTIFICIAL INTELLIGENCE(BCS515B)
•
The idea is simple: start with the atomic sentences in the knowledge base and apply Modus
Ponens in the forward direction, adding new atomic sentences, until no further inferences can be
made.
• This method uses First Order Definite clause .
• A definite clause either is atomic or is an implication whose antecedent is a conjunction of positive
literals and whose consequent is a single positive literal.
For example
• King(x) ∧ Greedy(x) ⇒ Evil(x) .
• King(John) .
• Greedy(y) .
• The law says that it is a crime for an American to sell weapons to hostile nations. The country
25
Prof. Salma Itagi,Dept. of CSE,SVIT
MODULE 4 ARTIFICIAL INTELLIGENCE(BCS515B)
Nono, an enemy of America, has some missiles, and all of its missiles were sold to it by
ColonelWest, who is American.
• We will prove that West is a criminal.
• Representation of this by using first order definite clauses.
• So 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) ----1
• “Nono . . . has some missiles.”
• The sentence ∃ x Owns(Nono, x)∧Missile(x) is transformed into two definite clauses by Existential
Instantiation, introducing a new constant M1:
• Owns(Nono,M1) ---2
• Missile(M1)-----3
• “All of its missiles were sold to it by Colonel West”:
Missile(x) ∧ Owns(Nono, x) ⇒ Sells(West, x, Nono) ---4
• We will also need to know that missiles are weapons:
Missile(x) ⇒ Weapon(x) ----5
• An enemy of America counts as “hostile”:
• Enemy(x,America) ⇒ Hostile(x) ----6
• “West, who is American . . .”:
• American(West) -----7
• “The country Nono, an enemy of America . . .”:
• Enemy(Nono,America) .
• This knowledge base contains no function symbols and is therefore an instance of the class
of Datalog knowledge bases.
• Datalog is a language that is restricted to first-order definite clauses with no function symbols.
• Starting from the known facts, it triggers all the rules whose premises are satisfied, adding their
conclusions to the known facts. The process repeats until the query is answered or no new facts are
added.
• On the first iteration, rule 1 has unsatisfied premises.
• Rule 4 is satisfied with {x/M1}, 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/M1, z/Nono}, and Criminal (West) is
26
Prof. Salma Itagi,Dept. of CSE,SVIT
MODULE 4 ARTIFICIAL INTELLIGENCE(BCS515B)
added.
Steps:
1. Initialization
2. Loop until new is empty
3. Processing each rule
4. Finding applicable premises
5. Checking for duplicates
6. Unifying with the query
7. Adding new facts to KB
8. Termination
27
Prof. Salma Itagi,Dept. of CSE,SVIT
MODULE 4 ARTIFICIAL INTELLIGENCE(BCS515B)
28
Prof. Salma Itagi,Dept. of CSE,SVIT