AI Unit3
AI Unit3
UNIT-3
Logic and Knowledge Representation First-Order Logic: Representation, Syntax
and Semantics of First-Order Logic, Using First-Order Logic, Knowledge
Engineering in First-Order Logic.
First-Order Logic
n the topic of Propositional logic, we have seen that how to represent statements
using propositional logic. But unfortunately, in propositional logic, we can only
represent the facts, which are either true or false. PL is not sufficient to
represent the complex sentences or natural language statements. The
propositional logic has very limited expressive power. Consider the following
sentence, which we cannot represent using PL logic.
First-Order logic:
● First-order logic (like natural language) does not only assume that the
world contains facts like propositional logic but also assumes the
following things in the world:
c. Function: Father of, best friend, third inning of, end of, ......
a. Syntax
b. Semantics
Variables x, y, z, a, b,....
Connectives ∧, ∨, ¬, ⇒, ⇔
Equality ==
Quantifier ∀, ∃
lOMoAR cPSD| 14288805
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.
Complex Sentences:
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.
lOMoAR cPSD| 14288805
● These are the symbols that permit to determine or identify the range and
scope of the variable in the logical expression. There are two types of
quantifier:
Universal Quantifier:
● For all x
● For each x
● For every x.
Example:
It will be read as: There are all x where x is a man who drink coffee.
Existential Quantifier:
Existential quantifiers are the type of quantifiers, which express that the
statement within its scope is true for at least one instance of something.
Example:
It will be read as: There are some x where x is a boy who is intelligent.
Points to remember:
Properties of Quantifiers:
And since there are all birds who fly so it will be represented as follows.
∀x bird(x) →fly(x).
In this question, the predicate is "respect(x, y)," where x=man, and y= parent.
Since there is every man so will use ∀, and it will be represented as follows:
In this question, the predicate is "play(x, y)," where x= boys, and y= game.
Since there are some boys so we will use ∃, and it will be represented as:
In this question, the predicate is "like(x, y)," where x= student, and y= subject.
Since there are not all students, so we will use ∀ with negation, so following
representation for this:
In this question, the predicate is "failed(x, y)," where x= student, and y= subject.
Since there is only one student who failed in Mathematics, so we will use
following representation for this:
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:
● 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:
In the second step, we will assemble the relevant knowledge which is required
for digital circuits. So for digital circuits, we have the following required
knowledge:
● In this logic circuit, there are four types of gates used: AND, OR, XOR,
and NOT.
● All these gates have one output terminal and two input terminals (except
NOT gate, it has one input terminal).
3. Decide on vocabulary:
The next step of the process is to select functions, predicate, and constants to
represent the circuits, terminals, signals, and gates. Firstly we will distinguish
the gates from each other and from other objects. 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 gate input, we will use the function In(1, X1) for denoting the first input
terminal of the gate, and for output terminal we will use Out (1, X1).
The function Arity(c, i, j) is used to denote that circuit c has i input, j output.
We use a unary predicate On (t), which is true if the signal at a terminal is on.
To encode the general knowledge about the logic circuit, we need some following
rules:
● If two terminals are connected then they have the same input signal, it
can be represented as:
1. ∀ t1, t2 Terminal (t1) ∧ Terminal (t2) ∧ Connect (t1, t2) → Signal (t1)
= Signal (2).
● Signal at every terminal will have either value 0 or 1, it will be
represented as:
1. ∀ t Terminal (t) →Signal (t) = 1 ∨Signal (t) = 0.
● Connect predicates are commutative:
1. ∀ t1, t2 Connect(t1, t2) → Connect (t2, t1).
● Representation of types of gates:
1. ∀ g Gate(g) ∧ r = Type(g) → r = OR ∨r = AND ∨r = XOR ∨r =
NOT.
● Output of AND gate will be zero if and only if any of its input is zero.
1. ∀ g Gate(g) ∧ Type(g) = AND →Signal (Out(1, g))= 0 ⇔ ∃n Signal
(In(n, g))= 0.
● Output of OR gate is 1 if and only if any of its input is 1:
1. ∀ g Gate(g) ∧ Type(g) = OR → Signal (Out(1, g))= 1 ⇔ ∃n Signal
(In(n, g))= 1
● Output of XOR gate is 1 if and only if its inputs are different:
1. ∀ g Gate(g) ∧ Type(g) = XOR → Signal (Out(1, g)) = 1 ⇔ Signal
(In(1, g)) ≠ Signal (In(2, g)).
● Output of NOT gate is invert of its input:
1. ∀ g Gate(g) ∧ Type(g) = NOT → Signal (In(1, g)) ≠ Signal (Out(1,
g)).
● All the gates in the above circuit have two inputs and one output (except
NOT gate).
1. ∀ g Gate(g) ∧ Type(g) = NOT → Arity(g, 1, 1)
2. ∀ g Gate(g) ∧ r =Type(g) ∧ (r= AND ∨r= OR ∨r= XOR) → Arity (g,
2, 1).
● All gates are logic circuits:
1. ∀ g Gate(g) → Circuit (g).
Now we encode problem of circuit C1, firstly we categorize the circuit and its gate
components. This step is easy if ontology about the problem is already thought.
This step involves the writing simple atomics sentences of instances of
concepts, which is known as ontology.
For the given circuit C1, we can encode the problem instance in atomic sentences
as below:
Since in the circuit there are two XOR, two AND, and one OR gate so atomic
sentences for these gates will be:
In this step, we will find all the possible set of values of all the terminal for the
adder circuit. The first query will be:
What should be the combination of input which would generate the first output of
circuit C1, as 0 and a second output to be 1?
Substitution:
Equality:
First-Order logic does not only use predicate and terms for making atomic
sentences but also uses another way, which is equality in FOL. For this, we can
use equality symbols which specify that the two terms refer to the same object.
As in the above example, the object referred by the Brother (John) is similar to
the object referred by Smith. The equality symbol can also be used with
negation to represent that two terms are not the same objects.
● Universal Generalization
● Universal Instantiation
● Existential Instantiation
● Existential introduction
1. Universal Generalization:
● This rule can be used if we want to show that every element has a similar
property.
Example: Let's represent, P(c): "A byte contains 8 bits", so for ∀ x P(x) "All
bytes contain 8 bits.", it will also be true.
2. Universal Instantiation:
● The UI rule state that we can infer any sentence P(c) by substituting a
ground term c (a constant within domain x) from ∀ x P(x) for any object
in the universe of discourse.
Example: 2.
"All kings who are greedy are Evil." So let our knowledge base contains this
detail as in the form of FOL:
So from this information, we can infer any of the following statements using
Universal Instantiation:
3. Existential Instantiation:
● This rule states that one can infer P(c) from the formula given in the
form of ∃x P(x) for a new constant symbol c.
● The restriction with this rule is that c used in the rule must be a new term
for which P(c ) is true.
● It can be represented as:
Example:
4. Existential introduction
● This rule states that if there is some element c in the universe of discourse
which has a property P, then we can infer that there exists something in
the universe which has the property P.
For the inference process in FOL, we have a single inference rule which is
called Generalized Modus Ponens. It is lifted version of Modus ponens.
Example:
We will use this rule for Kings are evil, so we will find some x such that x is
king, and x is greedy so we can infer that x is evil.
What is Unification?
● It takes two literals as input and makes them identical using substitution.
● Let Ψ1 and Ψ2 be two atomic sentences and 𝜎 be a unifier such that, Ψ1𝜎
= Ψ2𝜎, then it can be expressed as UNIFY(Ψ1, Ψ2).
● Example: Find the MGU for Unify{King(x),
● The UNIFY algorithm is used for unification, which takes two atomic
sentences and returns a unifier for those sentences (If any exist).
● Unification is a key component of all first-order inference algorithms.
E.g. Let's say there are two different expressions, P(x, y), and P(a, f(z)).
In this example, we need to make both above statements identical to each other.
For this, we will perform the substitution.
P(x, y)...............(i)
P(a, f(z))...............(ii)
● Substitute x with a, and y with f(z) in the first expression, and it will be
represented as a/x and f(z)/y.
● With both the substitutions, the first expression will be identical to the
second expression and the substitution set will be: [a/x, f(z)/y].
● Unification will fail if there are two similar variables present in the same
expression.
Unification Algorithm:
Algorithm: Unify(Ψ1, Ψ2)
c) Else if Ψ2 is a variable,
Step.2: If the initial Predicate symbol in Ψ1 and Ψ2 are not same, then return
FAILURE.
a) Call Unify function with the ith element of Ψ1 and ith element of Ψ2,
and put the result into S.
b. If one expression is a variable vi, and the other is a term ti which does not
contain variable vi, then:
For each pair of the following atomic sentences find the most general unifier (If
exist).
SUBST θ= {f(a) / X}
SUBST θ={b/Z}
SUBST θ= {X/Z}
SUBST θ= {11/y}
Unifier: {11/y}.
5. Find the MGU of Q(a, g(x, a), f(y)), Q(a, g(f(b), a), x)}
SUBST θ= {f(b)/x}
S1 => {Q(a, g(f(b), a), f(y)); Q(a, g(f(b), a), f(b))} SUBST
θ= {b/y}
S1 => {Q(a, g(f(b), a), f(b)); Q(a, g(f(b), a), f(b))}, Successfully Unified.
SUBST θ= {John/x}
Resolution in FOL
Resolution
Resolution is used, if there are various statements are given, and we need to
prove a conclusion of those statements. Unification is a key concept in proofs
by resolutions. Resolution is a single inference rule which can efficiently
operate on the conjunctive normal form or clausal form.
The resolution rule for first-order logic is simply a lifted version of the
propositional rule. Resolution can resolve two clauses if they contain
complementary literals, which are assumed to be standardized apart so that they
share no variables.
Example:
Where two complimentary literals are: Loves (f(x), x) and ¬ Loves (a, b)
These literals can be unified with unifier θ= [a/f(x), and b/x] , and it will
generate a resolvent clause:
To better understand all the above steps, we will take an example in which we
will apply resolution.
Example:
In the first step we will convert all the given statements into its first order logic.
In First order logic resolution, it is required to convert the FOL into CNF as
CNF form makes easier for resolution proofs.
a. ∀x ¬ food(x) V likes(John, x)
b. food(Apple) Λ food(vegetables)
e. ∀x ¬ eats(Anil, x) V eats(Harry, x)
h. likes(John, Peanuts).
a. ∀x ¬ food(x) V likes(John, x)
b. food(Apple) Λ food(vegetables)
e. ∀x ¬ eats(Anil, x) V eats(Harry, x)
f. ∀x ¬killed(x) ] V alive(x)
g. ∀x ¬ alive(x) V ¬ killed(x)
h. likes(John, Peanuts).
a. ∀x ¬ food(x) V likes(John, x)
b. food(Apple) Λ food(vegetables)
f. ∀g ¬killed(g) ] V alive(g)
g. ∀k ¬ alive(k) V ¬ killed(k)
h. likes(John, Peanuts).
a. ¬ food(x) V likes(John, x)
b. food(Apple)
c. food(vegetables)
f. alive(Anil)
g. ¬ eats(Anil, w) V eats(Harry, w)
h. killed(g) V alive(g)
i. ¬ alive(k) V ¬ killed(k)
j. likes(John, Peanuts).
proved
graph:
Now in this step, we will solve the problem by resolution tree using substitution.
For the above problem, it will be given as follows:
Hence the negation of the conclusion has been proved as a complete
contradiction with the given set of statements.
● In the third step of the resolution graph, ¬ eats(y, Peanuts) and eats (Anil,
Peanuts) get resolved by substitution {Anil/y}, and we are left with
Killed(Anil) .
● In the fourth step of the resolution graph, Killed(Anil) and ¬ killed(k) get
resolve by substitution {Anil/k}, and we are left with ¬ alive(Anil) .
● In the last step of the resolution graph ¬ alive(Anil) and alive(Anil) get
resolved.
Inference engine:
a. Forward chaining
b. Backward chaining
Horn clause and definite clause are the forms of sentences, which enables
knowledge base to use a more restricted and efficient inference algorithm.
Logical inference algorithms use forward and backward chaining approaches,
which require KB in the form of the first-order definite clause.Definite clause:
A clause which is a disjunction of literals with exactly one positive literal is
known as a definite clause or strict horn clause.
Horn clause: A clause which is a disjunction of literals with at most one positive
literal is known as horn clause. Hence all the definite clauses are horn clauses.
It is equivalent to p ∧ q → k.
A. Forward Chaining
The Forward-chaining algorithm starts from known facts, triggers all rules
whose premises are satisfied, and add their conclusion to the known facts. This
process repeats until the problem is solved.
Properties of Forward-Chaining:
Consider the following famous example which we will use in both approaches:
Example:
"As per the law, it is a crime for an American to sell weapons to hostile nations.
Country A, an enemy of America, has some missiles, and all the missiles were
sold to it by Robert, who is an American citizen."
To solve the above problem, first, we will convert all the above facts into first-
order definite clauses, and then we will use a forward-chaining algorithm to
reach the goal.
● Robert is American
American(Robert)
.....................................................
(8)
In the first step we will start with the known facts and will choose the sentences
which do not have implications, such as: American(Robert), Enemy(A,
America), Owns(A, T1), and Missile(T1). All these facts will be represented as
below.
Step-2:
At the second step, we will see those facts which infer from available facts and
with satisfied premises.
Rule-(1) does not satisfy premises, so it will not be added in the first iteration.
Rule-(4) satisfy with the substitution {p/T1}, so Sells (Robert, T1, A) is added,
which infers from the conjunction of Rule (2) and (3).
B. Backward Chaining:
Backward-chaining is also known as a backward deduction or backward
reasoning method when using an inference engine. A backward chaining
algorithm is a form of reasoning, which starts with the goal and works
backward, chaining through rules to find known facts that support the goal.
Properties of backward chaining:
Example:
In backward-chaining, we will use the same above example, and will rewrite all
the rules.
● Missile(T1)
Backward-Chaining proof:
Step-1:
At the first step, we will take the goal fact. And from the goal fact, we will infer
other facts, and at last, we will prove those facts true. So our goal fact is
"Robert is Criminal," so following is the predicate of it.
Step-2:
At the second step, we will infer other facts form goal fact which satisfies the rules.
So as we can see in Rule-1, the goal predicate Criminal (Robert) is present with
substitution {Robert/P}. So we will add all the conjunctive facts below the first
level and will replace p with Robert.
At step-4, we can infer facts Missile(T1) and Owns(A, T1) form Sells(Robert,
T1, r) which satisfies the Rule- 4, with the substitution of A in place of r. So
these two statements are proved here.
Step-5:
At step-5, we can infer the fact Enemy(A, America) from Hostile(A) which
satisfies Rule- 6. And hence all the statements are proved true using backward
chaining.
Difference between backward chaining and
forward chaining
Following is the difference between the forward chaining and backward
chaining:
● Forward chaining as the name suggests, start from the known facts and
move forward by applying inference rules to extract more data, and it
continues until it reaches to the goal, whereas backward chaining starts
from the goal, move backward by using inference rules to determine the
facts that satisfy the goal.
● Forward chaining can be used for tasks such as planning, design process
monitoring, diagnosis, and classification, whereas backward chaining can
be used for classification and diagnosis tasks.
● Forward chaining is slow as it checks for all the rules, whereas backward
chaining is fast as it checks few required rules only.
S. Forward Chaining Backward Chaining
No
.
5. Forward chaining tests for all the Backward chaining only tests for few
available rules required rules.
6. Forward chaining is suitable for Backward chaining is suitable for
the planning, monitoring, diagnostic, prescription, and
control, and interpretation debugging application.
application.
Reasoning:
The reasoning is the mental process of deriving logical conclusion and making
predictions from available knowledge, facts, and beliefs. Or we can say,
"Reasoning is a way to infer facts from existing data." It is a general process of
thinking rationally, to find valid conclusions.
In artificial intelligence, the reasoning is essential so that the machine can also
think rationally as a human brain, and can perform like a human.
Types of Reasoning
In artificial intelligence, reasoning can be divided into the following categories:
● Deductive reasoning
● Inductive reasoning
● Abductive reasoning
● Monotonic Reasoning
1. Deductive reasoning:
In deductive reasoning, the truth of the premises guarantees the truth of the
conclusion.
Deductive reasoning mostly starts from the general premises to the specific
conclusion, which can be explained as below example.
Example:
2. Inductive Reasoning:
Example:
Premise: All of the pigeons we have seen in the zoo are white.
3. Abductive reasoning:
Example:
Conclusion It is raining.
Example:
The above two statements are examples of common sense reasoning which a
human mind can easily understand and assume.
5. Monotonic Reasoning:
In monotonic reasoning, once the conclusion is taken, then it will remain the
same even if we add some other information to existing information in our
knowledge base. In monotonic reasoning, adding knowledge does not decrease
the set of prepositions that can be derived.
To solve monotonic problems, we can derive the valid conclusion from the
available facts only, and it will not be affected by new facts.
Monotonic reasoning is not useful for the real-time systems, as in real time,
facts get changed, so we cannot use monotonic reasoning.
Example:
● If we deduce some facts from available facts, then it will remain valid for
always.
● Since we can only derive conclusions from the old proofs, so new
knowledge from the real world cannot be added.
6. Non-monotonic Reasoning
"Human perceptions for various things in daily life, "is a general example of
non-monotonic reasoning.
Example: Let suppose the knowledge base contains the following knowledge:
● Pitty is a bird
So from the above sentences, we can conclude that Pitty can fly.
However, if we add one another sentence into knowledge base "Pitty is a
penguin", which concludes "Pitty cannot fly", so it invalidates the above
conclusion.