0% found this document useful (0 votes)
61 views

First Order Logic + Inference in First Order Logic

Uploaded by

jamesfds007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

First Order Logic + Inference in First Order Logic

Uploaded by

jamesfds007
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 58

First-Order Logic

Chapter 8
Introduction
• Knowledge is represented in 2 ways:
• Propositional & First-order Logic
• Statements are represented using propositional logic.

• Propositional logic, can only represent the facts,


which are either true or false.

• PL is not sufficient to represent complex sentences or


natural language statements.
First-order logic

•FOL is also called Predicate Logic.


•Predicate Logic consists of:
•Predicates
•Quantifiers
•Propositional logic assumes the world
contains facts.
First-order logic

• First-order logic (like natural language) does not only


assume that the world contains facts like propositional logic
• assumes the following things in the world:
• Objects: cat, dog, John, people, houses, numbers, colors,
baseball games, wars, etc.
• Relations: red, round, prime, brother of, bigger than, part of,
comes between
• Fact: father of, best friend, etc
• Facts can be true or False
Logics in General

Language Ontological Epistemological


Commitment Commitment
Propositional Logic Facts True / False

First-Order Logic Fact, objects, True / False /


relations Unknown
Temporal Logic Facts, objects, True / False /
relations, times Unknown
Probability Theory Facts Degree of belief ∈
[0,1]
Fuzzy Logic Degree of truth ∈ Known interval value
[0,1]
March 2, 2006 AI: Chapter 8: First-Order Logic 5
Constants: KingJohn, Richard
Predicates: person, king, crown
Functions: brother, on_head, left_leg
Syntax of FOL: Basic elements

•Constants - objects
•Predicates - relations; Brother, >,...
•Functions- Sqrt, plus etc
•Atomic sentence state facts:
•Brother(Richard, John)
•predicate and function have arity.
•Eg: Likes(Jane, Pizza)- 2 arity
•Term is a logical expression that refers to an
object.
•Eg:LeftLeg(John)
Atomic sentences
•Atomic sentences are the most basic
sentences of first-order logic. These
sentences are formed from a predicate
symbol followed by a parenthesis with a
sequence of terms.
•We can represent atomic sentences as
Predicate (term1, term2, ......, term n).
•Example: Ravi and Ajay are brothers: =>
Brothers(Ravi, Ajay).
•Brother (Richard, John).
Complex Sentences

• Complex sentences are made by combining atomic


sentences using connectives.
• First-order logic statements can be divided into two
parts:
• Subject: The subject is the main part of the statement.

• Predicate: A predicate is a relation that binds two


atoms together in a statement.
• Consider the statement: "x is an integer.", it consists of
two parts, the first part x is the subject of the statement
and second part "is an integer," is known as a predicate.
Complex sentences

•Complex sentences are made from atomic


sentences using connectives

Brother (Richard, John) ∧ Brother (John,
Richard)
King(Richard ) ∨ King(John)
¬King(Richard) ⇒ King(John)
Sibling( KingJohn,Richard ) ⇒ Sibling(
Richard,KingJohn )
Interpretation
Quantifiers

•These are the symbols that permit determining


or identifying the variable's range and scope in
the logical expression.
•First-order logic contains two standard
quantifiers, called universal and existential

•There are two types of quantifiers:


Universal Quantifier

Universal Quantifier, (for all, for each, for


every)
•Syntax ∀ variables sentence
•“All kings are persons”
•∀ x King(x) ⇒ Person(x) .
•We use the implication symbol.
•“Everyone studying in IIT is smart:
•∀ x (StudiesAt(x,IIT) ⇒ Smart(x))
Existential quantifier
Syntax: ∃ variables sentence
(there exist, for some, for at least one).
• “Someone studying in IIT is smart:
• ∃ x (StudiesAt(x,IIT)∧Smart(x))

• Note ∧ is the main connective with ∃


Some Examples of FOL using
quantifier:

1. Pizza is a food
2. Tom likes pizza
3. All birds fly.
4. All kings are person
5. No one likes math
1. Every man respects his parent. ∀x man(x) → respects (x,
parent).
2. Some boys play cricket. ∃x boys(x) → play(x, cricket).
3. Brothers are siblings.
4. All indoor games are easy.
5. Caesar was a king
Nested quantifiers
• Nested quantifiers are a concept in predicate logic
where multiple quantifiers (like ∀ for "for all" and ∃
for "there exists") are used together ,often with one
quantifier inside the scope of another.
• Two quantifiers are said to be nested if one is within
the scope of the other.
• ∀x ∀y P(x, y): This means "for all x and for all y, P(x, y)
is true." Here, the proposition P applies universally to
all pairs of x and y.
• For example, “Brothers are siblings” can be written as
• ∀ x ∀ y Brother (x, y) ⇒ Sibling(x, y) .
•∃x ∃y P(x, y): This means "there exists an x and
there exists a y such that P(x, y) is true." Here, there
is at least one pair of x and y for which the
proposition P is true.
•∀x ∃y P(x, y): This means "for all x, there exists a
y such that P(x, y) is true." In other words, for each
x, you can find at least one y that makes P(x, y)
true.
•∀x ∃y (x + y = 0): This means for every real
number x, there exists a real number y (specifically
y = -x) such that x + y = 0.
Equality

• Equality symbol to signify that two terms refer to the


same object.
• For example, Father (John)=Henry.
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:
• Query: We can ask questions of the knowledge base using ASK.
• ASK(KB, King(John))
• Goal: True/False
• 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.
Knowledge engineering in FOL

•The process of constructing a


knowledge-base in first-order logic is
called as knowledge- engineering.
•In knowledge-engineering, someone
who investigates a particular domain,
learns important concept of that domain,
and generates a formal representation of
the objects, is known as knowledge
engineer.
• First-Order Logic for "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.“ Formulate this knowledge in First
order Logic
Knowledge engineering in FOL

1. Identify the task


2. Assemble the relevant knowledge
3. Decide on a vocabulary of predicates, functions,
and constants
4. Encode general knowledge about the domain
5. Encode a description of the specific problem
instance
6. Pose queries to the inference procedure and get
answers
7. Debug the knowledge base
Circuit Domain
Identify the tasks

• The first step of the process is to identify the task, and for the digital
circuit, there are various reasoning tasks.
• At the first level or highest level, we will examine the functionality of
the circuit:
• Does the circuit add properly?
• What will be the output of gate A2, if all the inputs are high?
• At the second level, we will examine the circuit structure details such
as:
• Which gate is connected to the first input terminal?
• Does the circuit have feedback loops?
Assemble the relevant knowledge:

•We need to know gates and how they


transform signals.
•there are four types of gates used: AND, OR,
XOR, and NOT.
Decide on vocabulary:
• The next step of the process is to select functions,
predicate, and constants to represent the circuits,
terminals, signals, and gates.
• Each gate is represented as an object which is named
by a constant, such as, Gate(X1).
• The functionality of each gate is determined by its
type, which is taken as constants such as AND, OR,
XOR, or NOT. Circuits will be identified by a
predicate: Circuit (C1).
• For the terminal, we will use
predicate: Terminal(x).
• function In(1,X1) to denote the first input terminal for gate
X1.

• function Arity(c, i, j)
• 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.
Encode general knowledge about the domain:
• 1. If two terminals are connected, then they have the
same signal:
• ∀ t1, t2 Terminal (t1) ∧ Terminal (t2) ∧ Connected (
t1, t2) → Signal (t1) = Signal (t2).

• Signal at every terminal will have either value 0 or


1, it will be represented as:
• ∀ t Signal (t) = 1 ∨ Signal (t) = 0.

• Connected is commutative:
• ∀ t1, t2 Connected(t1, t2) ⇔ Connected(t2, t1) .
• An OR gate’s output is 1 if and only if any of its inputs is 1:
• ∀ g Gate(g) ∧ Type(g)=OR ⇒
• Signal (Out(1, g))= 1 ⇔ ∃n Signal (In(n, g))=1 .

• An AND gate’s output is 0 if and only if any of its inputs is 0:


• ∀ g Gate(g) ∧ Type(g)=AND ⇒
• Signal (Out(1, g))= 0 ⇔ ∃n Signal (In(n, g))=0 .
Encode a description of the problem instance

• This step involves writing simple atomic sentences of


instances of concepts, which is known as ontology.
• Since in the circuit there are two XOR, two AND, and one
OR gate so atomic sentences for these gates will be:
• For XOR gate: Type(x1)= XOR, Type(X2) = XOR
• For AND gate: Type(A1) = AND, Type(A2)= AND
• For OR gate: Type (O1) = OR.

• 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))
Pose queries to the inference procedure and get
answers

• ∃ i1, i2, i3, o1, o2 Signal (In(1, C1))=i1 ∧ Signal


(In(2, C1))=i2 ∧ Signal (In(3, C1))=i3
• ∧ Signal (Out(1, C1))=0i ∧ Signal (Out(2, C1))=02.
• {i1/1, i2/1, i3/0} {i1/1, i2/0, i3/1} {i1/0, i2/1, i3/1} .

• This final query will return a complete input–output


table for the device, which can be used to check that
it does in fact add its inputs correctly. This is a
simple example of circuit verification.
Debug the knowledge base

• In this step, we will try to debug the issues of the knowledge


base.
• System unable to give output in no signals.
• If all inputs are 000 then outputs will also be 000
• Consider a vocabulary with the following symbols.
• Occupation(p, o): Predicate. Person p has occupation o.
• Customer(p1, p2): Predicate. Person p1 is a customer of person p2.
• Boss(p1, p2): Predicate. Person p1 is the boss of person p2.
• Doctor, Surgeon, Lawyer, Actor: Constants denoting occupations.
• Emily, Joe: Constants denoting people.

A. Emily is either a surgeon or a lawyer.


B. Joe is an actor, but he also holds another job.
C. All surgeons are doctors.
D. Joe does not have a lawyer (i.e., is not a customer of any lawyer).
E. Emily has a boss who is a lawyer.
For each of the logical expressions, state whether it (1) correctly
expresses the English sentence; (2) is syntactically invalid and therefore meaningless; or
(3)
is syntactically valid but does not express the meaning of the English sentence.

Paris and Marseilles are both in France.


(i) In(Paris ∧ Marseilles, France ).
(ii) In(Paris, France ) ∧ In(Marseilles, France ).
(iii) In(Paris, France ) ∨ In(Marseilles, France ).

There is a country that borders both Iraq and Pakistan.


(i) ∃ c Country(c) ∧ Border (c, Iraq) ∧ Border (c, Pakistan).
(ii) ∃ c Country(c) ⇒ [Border (c, Iraq) ∧ Border (c, Pakistan)].
(iii) ∃ c Border (Country(c), Iraq ∧ Pakistan).
Backtracking

• Backtracking is finding the solution of a problem whereby the


solution depends on the previous steps taken.
• For example, maze problem.
• In backtracking, we first take a step and then we see if this step taken
is correct or not i.e., whether it will give a correct answer or not. And
if it doesn’t, then we just come back and change our first step.
Horn clauses

• A Horn clause is a sentence of the form:


(∀x) P1(x) ∧ P2(x) ∧ ... ∧ Pn(x) → Q(x)
where
• there are 0 or more Pis and 0 or 1 Q
• the Pis and Q are positive (i.e., non-negated) literals
• Equivalently: P1(x) ∨ P2(x) … ∨ Pn(x) where the Pi are all atomic
and at most one of them is positive
• Prolog is based on Horn clauses
• Horn clauses represent a subset of the set of sentences
representable in FOL

39
Inference Engine

• Inference Engine- components of Expert System.

• It applies logical rules to the KB to infer new information from the


known facts.

• Forward and backward chaining is used by IE to make deductions.

• When the If (condition) part of the rule matches the fact, the rule is
fired, and its THEN (action ) part is executed.

• The matching of the IF parts to the facts produces inference chains.


• These Inference chains determine how the rules are applied to reach a
conclusion.
• Modus Ponens:
• The Modus Ponens rule is one of the most important rules of
inference, and it states that if P and P → Q is true, then we can infer
that Q will be true. It can be represented as:

Forward chaining

43
Properties
Backward chaining

46
Forward vs. backward chaining

• FC is data-driven
• Automatic, unconscious processing
• E.g., object recognition, routine decisions
• May do lots of work that is irrelevant to the goal
• BC is goal-driven, appropriate for problem-solving
• Where are my keys? How do I get to my next class?
• Complexity of BC can be much less than linear in the size of the KB

47
Resolution

• Resolution is a sound and complete inference procedure for FOL


• Reminder: Resolution rule for propositional logic:
• P1 ∨ P2 ∨ ... ∨ Pn
• ¬P1 ∨ Q2 ∨ ... ∨ Qm
• Resolvent: P2 ∨ ... ∨ Pn ∨ Q2 ∨ ... ∨ Qm
• Examples
• P and ¬ P ∨ Q : derive Q (Modus Ponens)
• (¬ P ∨ Q) and (¬ Q ∨ R) : derive ¬ P ∨ R
• P and ¬ P : derive False [contradiction!]
• (P ∨ Q) and (¬ P ∨ ¬ Q) : derive True

48
Resolution in first-order logic
• Given sentences
P1 ∨ ... ∨ Pn
Q1 ∨ ... ∨ Qm
• in conjunctive normal form:
• each Pi and Qi is a literal, i.e., a positive or negated predicate symbol with its terms,

• if Pj and ¬Qk unify with substitution list θ, then derive the resolvent sentence:
subst(θ, P1 ∨... ∨ Pj-1 ∨ Pj+1 ... Pn ∨ Q1 ∨ …Qk-1 ∨ Qk+1 ∨... ∨ Qm)
• Example
• from clause P(x, f(a)) ∨ P(x, f(y)) ∨ Q(y)
• and clause ¬P(z, f(a)) ∨ ¬Q(z)
• derive resolvent P(z, f(y)) ∨ Q(y) ∨ ¬Q(z)
• using θ = {x/z}

49
Unification

• Unification is a “pattern-matching” procedure


• Takes two atomic sentences, called literals, as input
• Returns “Failure” if they do not match and a substitution list, θ, if they do
• That is, unify(p,q) = θ means subst(θ, p) = subst(θ, q) for two atomic
sentences, p and q
• θ is called the most general unifier (mgu)
• All variables in the given two literals are implicitly universally
quantified
• To make literals match, replace (universally quantified) variables by
terms

51
Unification algorithm
procedure unify(p, q, θ)
Scan p and q left-to-right and find the first corresponding
terms where p and q “disagree” (i.e., p and q not equal)
If there is no disagreement, return θ (success!)
Let r and s be the terms in p and q, respectively,
where disagreement first occurs
If variable(r) then {
Let θ = union(θ, {r/s})
Return unify(subst(θ, p), subst(θ, q), θ)
} else if variable(s) then {
Let θ = union(θ, {s/r})
Return unify(subst(θ, p), subst(θ, q), θ)
} else return “Failure”
end
52
Unification examples
• Example:
• parents(x, father(x), mother(Bill))
• parents(Bill, father(Bill), y)
• {x/Bill, y/mother(Bill)}
• Example:
• parents(x, father(x), mother(Bill))
• parents(Bill, father(y), z)
• {x/Bill, y/Bill, z/mother(Bill)}
• Example:
• parents(x, father(x), mother(Jane))
• parents(Bill, father(y), mother(y))
• Failure

53
Resolution example

54
Practice example
Did Curiosity kill the cat
• 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?
• These can be represented as follows:
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)

GOAL

55
• Convert to clause form
A1. (Dog(D))
A2. (Owns(Jack,D))
D is a skolem constant
B. (¬Dog(y), ¬Owns(x, y), AnimalLover(x))
C. (¬AnimalLover(a), ¬Animal(b), ¬Kills(a,b))
D. (Kills(Jack,Tuna), Kills(Curiosity,Tuna))
E. Cat(Tuna)
F. (¬Cat(z), Animal(z))
• Add the negation of query:
¬G: (¬Kills(Curiosity, Tuna))

56
• The resolution refutation proof
R1: ¬G, D, {} (Kills(Jack, Tuna))
R2: R1, C, {a/Jack, b/Tuna} (~AnimalLover(Jack),
~Animal(Tuna))
R3: R2, B, {x/Jack} (~Dog(y), ~Owns(Jack, y),
~Animal(Tuna))
R4: R3, A1, {y/D} (~Owns(Jack, D),
~Animal(Tuna))
R5: R4, A2, {} (~Animal(Tuna))
R6: R5, F, {z/Tuna} (~Cat(Tuna))
R7: R6, E, {} FALSE

57
• The proof tree

¬G D
{}
R1: K(J,T) C
{a/J,b/T}
R2: ¬AL(J) ∨ ¬A(T) B
{x/J}
R3: ¬D(y) ∨ ¬O(J,y) ∨ ¬A(T) A1
{y/D}
R4: ¬O(J,D), ¬A(T) A2
{}
R5: ¬A(T) F

{z/T}
R6: ¬C(T) A
{}
R7: FALSE
58
Example
1. ¬Battery-OK ∨ ¬Bulbs-OK ∨ Headlights-Work
2. ¬Battery-OK ∨ ¬Starter-OK ∨ Empty-Gas-Tank ∨ Engine-Starts
3. ¬Engine-Starts ∨ Flat-Tire ∨ Car-OK
4. Headlights-Work
5. Battery-OK
6. Starter-OK
7. ¬Empty-Gas-Tank
8. ¬Car-OK
9. ¬Flat-Tire negated goal

59

You might also like