Unit-4
Unit-4
In 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.
To represent the above statements, PL logic is not sufficient, so we required some more
powerful logic, such as first-order logic.
First-Order logic:
o First-order logic is another way of knowledge representation in artificial
intelligence. It is an extension to propositional logic.
o FOL is sufficiently expressive to represent the natural language statements in a
concise way.
o First-order logic is also known as Predicate logic or First-order predicate
logic. First-order logic is a powerful language that develops information about
the objects in a more easy way and can also express the relationship between
those objects.
o 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:
o Objects: A, B, people, numbers, colors, wars, theories, squares, pits,
wumpus, ......
o Relations: It can be unary relation such as: red, round, is adjacent, or
n-any relation such as: the sister of, brother of, has color, comes
between
o Function: Father of, best friend, third inning of, end of, ......
o As a natural language, first-order logic also has two main parts:
a. Syntax
b. Semantics
Syntax of First-Order logic:
The syntax of FOL determines which collection of symbols is a logical expression in
first-order logic. The basic syntactic elements of first-order logic are symbols. We write
statements in short-hand notation in FOL.
Variables x, y, z, a, b,....
Connectives ∧, ∨, ¬, ⇒, ⇔
Equality ==
Quantifier ∀, ∃
Atomic sentences:
o 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.
o We can represent atomic sentences as Predicate (term1, term2, ......, term n).
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.
Universal Quantifier:
Universal quantifier is a symbol of logical representation, which specifies that the
statement within its range is true for everything or every instance of a particular thing.
o For all x
o For each x
o For every x.
Example:
All man drink coffee.
Let a variable x which refers to a cat so all x can be represented in UOD as below:
∀x man(x) → drink (x, coffee).
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.
If x is a variable, then existential quantifier will be ∃x or ∃(x). And it will be read as:
Example:
Some boys are intelligent.
∃x: boys(x) ∧ intelligent(x)
It will be read as: There are some x where x is a boy who is intelligent.
Points to remember:
o The main connective for universal quantifier ∀ is implication →.
o The main connective for existential quantifier ∃ is and ∧.
Properties of Quantifiers:
o In universal quantifier, ∀x∀y is similar to ∀y∀x.
o In Existential quantifier, ∃x∃y is similar to ∃y∃x.
o ∃x∀y is not similar to ∀y∃x.
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.
o Universal Generalization
o Universal Instantiation
o Existential Instantiation
o Existential introduction
1. Universal Generalization:
o Universal generalization is a valid inference rule which states that if premise P(c)
is true for any arbitrary element c in the universe of discourse, then we can have
a conclusion as ∀ x P(x).
2. Universal Instantiation:
Example:1.
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:
Example:
So we can infer: Crown(K) ∧ OnHead( K, John), as long as K does not appear in the
knowledge base.
4. Existential introduction
According to Modus Ponens, for atomic sentences pi, pi', q. Where there is a
substitution θ such that SUBST (θ, pi',) = SUBST(θ, pi), it can be represented as:
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.
Inference engine:
The inference engine is the component of the intelligent system in artificial
intelligence, which applies logical rules to the knowledge base to infer new information
from known facts. The first inference engine was part of the expert system. Inference
engine commonly proceeds in two modes, which are:
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
Forward chaining is also known as a forward deduction or forward reasoning method
when using an inference engine. Forward chaining is a form of reasoning which start
with atomic sentences in the knowledge base and applies inference rules (Modus
Ponens) in the forward direction to extract more data until a goal is reached.
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.
Facts Conversion into FOL:
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-(2) and (3) are already added.
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).
Rule-(6) is satisfied with the substitution(p/A), so Hostile(A) is added and which infers
from Rule-(7).
Step-3:
At step-3, as we can check Rule-(1) is satisfied with the substitution {p/Robert, q/T1,
r/A}, so we can add Criminal(Robert) which infers all the available facts. And hence
we reached our goal statement.
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.
Backward-Chaining proof:
In Backward chaining, we will start with our goal predicate, which is Criminal(Robert),
and then infer further rules.
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.
Step-3:t At step-3, we will extract further fact Missile(q) which infer from Weapon(q),
as it satisfies Rule-(5). Weapon (q) is also true with the substitution of a constant T1 at
q.
Step-4:
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.
What is Fuzzy Logic?
The 'Fuzzy' word means the things that are not clear or are vague. Sometimes, we
cannot decide in real life that the given problem or statement is either true or false. At
that time, this concept provides many values between the true and false and gives the
flexibility to find the best solution to that problem.
Fuzzy logic contains the multiple logical values and these values are the truth values
of a variable or problem between 0 and 1. This concept was introduced by Lofti
Zadeh in 1965 based on the Fuzzy Set Theory. This concept provides the possibilities
which are not given by computers, but similar to the range of possibilities generated
by humans.
In the Boolean system, only two possibilities (0 and 1) exist, where 1 denotes the
absolute truth value and 0 denotes the absolute false value. But in the fuzzy system,
there are multiple possibilities present between the 0 and 1, which are partially false
and partially true.
The Fuzzy logic can be implemented in systems such as micro-controllers, workstation-
based or large network-based systems for achieving the definite output. It can also be
implemented in both hardware or software.
1. This concept is flexible and we can easily understand and implement it.
2. It is used for helping the minimization of the logics created by the human.
3. It is the best method for finding the solution of those problems which are
suitable for approximate or uncertain reasoning.
4. It always offers two values, which denote the two possible solutions for a
problem and statement.
5. It allows users to build or create the functions which are non-linear of arbitrary
complexity.
6. In fuzzy logic, everything is a matter of degree.
7. In the Fuzzy logic, any system which is logical can be easily fuzzified.
8. It is based on natural language processing.
9. It is also used by the quantitative analysts for improving their algorithm's
execution.
10. It also allows users to integrate with the programming.
1. Rule Base
Rule Base is a component used for storing the set of rules and the If-Then conditions
given by the experts are used for controlling the decision-making systems. There are
so many updates that come in the Fuzzy theory recently, which offers effective
methods for designing and tuning of fuzzy controllers. These updates or developments
decreases the number of fuzzy set of rules.
2. Fuzzification
Fuzzification is a module or component for transforming the system inputs, i.e., it
converts the crisp number into fuzzy steps. The crisp numbers are those inputs which
are measured by the sensors and then fuzzification passed them into the control
systems for further processing. This component divides the input signals into following
five states in any Fuzzy Logic system:
o Large Positive (LP)
o Medium Positive (MP)
o Small (S)
o Medium Negative (MN)
o Large negative (LN)
3. Inference Engine
This component is a main component in any Fuzzy Logic system (FLS), because all the
information is processed in the Inference Engine. It allows users to find the matching
degree between the current fuzzy input and the rules. After the matching degree, this
system determines which rule is to be added according to the given input field. When
all rules are fired, then they are combined for developing the control actions.
4. Defuzzification
Defuzzification is a module or component, which takes the fuzzy set inputs generated
by the Inference Engine, and then transforms them into a crisp value. It is the last
step in the process of a fuzzy logic system. The crisp value is a type of value which is
acceptable by the user. Various techniques are present to do this, but the user has to
select the best one for reducing the errors.
Membership Function
The membership function is a function which represents the graph of fuzzy sets, and
allows users to quantify the linguistic term. It is a graph which is used for mapping
each element of x to the value between 0 and 1.
This function of Membership was introduced in the first papers of fuzzy set by Zadeh.
For the Fuzzy set B, the membership function for X is defined as: μB:X → [0,1]. In this
function X, each element of set B is mapped to the value between 0 and 1. This is called
a degree of membership or membership value.
Fuzzy Set
The set theory of classical is the subset of Fuzzy set theory. Fuzzy logic is based on this
theory, which is a generalisation of the classical theory of set (i.e., crisp set) introduced
by Zadeh in 1965.
A fuzzy set is a collection of values which exist between 0 and 1. Fuzzy sets are denoted
or represented by the tilde (~) character. The sets of Fuzzy theory were introduced in
1965 by Lofti A. Zadeh and Dieter Klaua. In the fuzzy set, the partial membership also
exists. This theory released as an extension of classical set theory.
This theory is denoted mathematically asA fuzzy set (Ã) is a pair of U and M, where U
is the Universe of discourse and M is the membership function which takes on values
in the interval [ 0, 1 ]. The universe of discourse (U) is also denoted by Ω or X.
Example:
then,
AUB = {( X1, 0.6), (X2, 0.8), (X3, 1), (X4, 0.9)}
For X1
μA∪B(X1)=max(μA(X1)μB(X1))
μA∪B(X1)=max(0.6,0.1)
μA∪B(X1) = 0.6
For X2
μA∪B(X2)=max(μA(X2),μB(X2))
μA∪B(X2)=max(0.2,0.8)
μA∪B(X2) = 0.8
For X3
μA∪B(X3)=max(μA(X3),μB(X3))
μA∪B(X3)=max(1,0)
μA∪B(X3) = 1
For X4
μA∪B(X4)=max(μA(X4),μB(X4))
μA∪B(X4)=max(0.4,0.9)
μA∪B(X4) = 0.9
Example:
For X1
μA∩B(X1)=min(μA(X1),μB(X1))
μA∩B(X1)=min(0.3,0.8)
μA∩B(X1) = 0.3
For X2
μA∩B(X2)=min(μA(X2),μB(X2))
μA∩B(X2)=min(0.7,0.2)
μA∩B(X2) = 0.2
For X3
μA∩B(X3)=min(μA(X3),μB(X3))
μA∩B(X3)=min(0.5,0.4)
μA∩B(X3) = 0.4
For X4
μA∩B(X4)=min(μA(X4),μB(X4))
μA∩B(X4)=min(0.1,0.9)
μA∩B(X4) = 0.1
μĀ(x) = 1-μA(x),
Example:
then,
Ā= {( X1, 0.7 ), (X2, 0.2), (X3, 0.5), (X4, 0.9)}
For X1
μĀ(X1)=1-μA(X1)
μĀ(X1)=1-0.3
μĀ(X1) = 0.7
For X2
μĀ(X2)=1-μA(X2)
μĀ(X2)=1-0.8
μĀ(X2) =0.2
For X3
μĀ(X3)=1-μA(X3)
μĀ(X3)=1-0.5
μĀ(X3) = 0.5
For X4
μĀ(X4)=1-μA(X4)
μĀ(X4)=1-0.1
μĀ(X4) = 0.9
1. The run time of fuzzy logic systems is slow and takes a long time to produce
outputs.
2. Users can understand it easily if they are simple.
3. The possibilities produced by the fuzzy logic system are not always accurate.
4. Many researchers give various ways for solving a given statement using this
technique which leads to ambiguity.
5. Fuzzy logics are not suitable for those problems that require high accuracy.
6. The systems of a Fuzzy logic need a lot of testing for verification and validation.
Bayesian networks are probabilistic, because these networks are built from
a probability distribution, and also use probability theory for prediction and anomaly
detection.
Real world applications are probabilistic in nature, and to represent the relationship
between multiple events, we need a Bayesian network. It can also be used in various
tasks including prediction, anomaly detection, diagnostics, automated insight,
reasoning, time series prediction, and decision making under uncertainty.
Bayesian Network can be used for building models from data and experts opinions,
and it consists of two parts:
The generalized form of Bayesian network that represents and solve decision problems
under uncertain knowledge is known as an Influence diagram.
A Bayesian network graph is made up of nodes and Arcs (directed links), where:
o Causal Component
o Actual numbers
Each node in the Bayesian network has condition probability
distribution P(Xi |Parent(Xi) ), which determines the effect of the parent on that node.
P[x1, x2, x3,....., xn], it can be written as the following way in terms of the joint
probability distribution.
In general for each variable Xi, we can write the equation as:
Example: Harry installed a new burglar alarm at his home to detect burglary. The alarm
reliably responds at detecting a burglary but also responds for minor earthquakes.
Harry has two neighbors David and Sophia, who have taken a responsibility to inform
Harry at work when they hear the alarm. David always calls Harry when he hears the
alarm, but sometimes he got confused with the phone ringing and calls at that time
too. On the other hand, Sophia likes to listen to high music, so sometimes she misses
to hear the alarm. Here we would like to compute the probability of Burglary Alarm.
Problem:
Calculate the probability that alarm has sounded, but there is neither a burglary,
nor an earthquake occurred, and David and Sophia both called the Harry.
Solution:
o The Bayesian network for the above problem is given below. The network
structure is showing that burglary and earthquake is the parent node of the
alarm and directly affecting the probability of alarm's going off, but David and
Sophia's calls depend on alarm probability.
o The network is representing that our assumptions do not directly perceive the
burglary and also do not notice the minor earthquake, and they also not confer
before calling.
o The conditional distributions for each node are given as conditional
probabilities table or CPT.
o Each row in the CPT must be sum to 1 because all the entries in the table
represent an exhaustive set of cases for the variable.
o In CPT, a boolean variable with k boolean parents contains 2 K probabilities.
Hence, if there are two parents, then CPT will contain 4 probability values
o Burglary (B)
o Earthquake(E)
o Alarm(A)
o David Calls(D)
o Sophia calls(S)
We can write the events of problem statement in the form of probability: P[D, S, A, B,
E], can rewrite the above probability statement using joint probability distribution:
P(E= False)= 0.999, Which is the probability that an earthquake not occurred.
The Conditional probability of David that he will call depends on the probability of
Alarm.
The Conditional probability of Sophia that she calls is depending on its Parent Node
"Alarm."
From the formula of joint distribution, we can write the problem statement in the form
of probability distribution:
= 0.00068045.
Hence, a Bayesian network can answer any query about the domain by using
Joint distribution.
The semantics of Bayesian Network:
There are two ways to understand the semantics of the Bayesian network, which is
given below:
1. To understand the network as the representation of the Joint probability
distribution.
It is helpful to understand how to construct the network.
2. To understand the network as an encoding of a collection of conditional
independence statements.
It is helpful in designing inference procedure.
What is Reinforcement Learning?
o Reinforcement Learning is a feedback-based Machine learning technique in
which an agent learns to behave in an environment by performing the actions
and seeing the results of actions. For each good action, the agent gets positive
feedback, and for each bad action, the agent gets negative feedback or penalty.
o In Reinforcement Learning, the agent learns automatically using feedbacks
without any labeled data, unlike supervised learning.
o Since there is no labeled data, so the agent is bound to learn by its experience
only.
o RL solves a specific type of problem where decision making is sequential, and
the goal is long-term, such as game-playing, robotics, etc.
o The agent interacts with the environment and explores it by itself. The primary
goal of an agent in reinforcement learning is to improve the performance by
getting the maximum positive rewards.
o The agent learns with the process of hit and trial, and based on the experience,
it learns to perform the task in a better way. Hence, we can say
that "Reinforcement learning is a type of machine learning method where
an intelligent agent (computer program) interacts with the environment
and learns to act within that." How a Robotic dog learns the movement of
his arms is an example of Reinforcement learning.
o It is a core part of Artificial intelligence, and all AI agent works on the concept
of reinforcement learning. Here we do not need to pre-program the agent, as
it learns from its own experience without any human intervention.
o Example: Suppose there is an AI agent present within a maze environment, and
his goal is to find the diamond. The agent interacts with the environment by
performing some actions, and based on those actions, the state of the agent
gets changed, and it also receives a reward or penalty as feedback.
o The agent continues doing these three things (take action, change
state/remain in the same state, and get feedback), and by doing these
actions, he learns and explores the environment.
o The agent learns that what actions lead to positive feedback or rewards and
what actions lead to negative feedback penalty. As a positive reward, the agent
gets a positive point, and as a penalty, it gets a negative point.
Terms used in Reinforcement Learning
o Agent(): An entity that can perceive/explore the environment and act upon it.
o Environment(): A situation in which an agent is present or surrounded by. In
RL, we assume the stochastic environment, which means it is random in nature.
o Action(): Actions are the moves taken by an agent within the environment.
o State(): State is a situation returned by the environment after each action taken
by the agent.
o Reward(): A feedback returned to the agent from the environment to evaluate
the action of the agent.
o Policy(): Policy is a strategy applied by the agent for the next action based on
the current state.
o Value(): It is expected long-term retuned with the discount factor and opposite
to the short-term reward.
o Q-value(): It is mostly similar to the value, but it takes one additional parameter
as a current action (a).
Key Features of Reinforcement Learning
o In RL, the agent is not instructed about the environment and what actions need
to be taken.
o It is based on the hit and trial process.
o The agent takes the next action and changes states according to the feedback
of the previous action.
o The agent may get a delayed reward.
o The environment is stochastic, and the agent needs to explore it to reach to get
the maximum positive rewards.
1. Value-based:
The value-based approach is about to find the optimal value function, which is
the maximum value at a state under any policy. Therefore, the agent expects
the long-term return at any state(s) under policy π.
2. Policy-based:
Policy-based approach is to find the optimal policy for the maximum future
rewards without using the value function. In this approach, the agent tries to
apply such a policy that the action performed in each step helps to maximize
the future reward.
The policy-based approach has mainly two types of policy:
o Deterministic: The same action is produced by the policy (π) at any
state.
o Stochastic: In this policy, probability determines the produced action.
3. Model-based: In the model-based approach, a virtual model is created for the
environment, and the agent explores that environment to learn it. There is no
particular solution or algorithm for this approach because the model
representation is different for each environment.
Basics of PROLOG:
Prolog or PROgramming in LOGics is a logical and declarative programming
language. It is one major example of the fourth generation language that supports
the declarative programming paradigm. This is particularly suitable for programs
that involve symbolic or non-numeric computation. This is the main reason to
use Prolog as the programming language in Artificial Intelligence,
where symbol manipulation and inference manipulation are the fundamental
tasks.
In Prolog, we need not mention the way how one problem can be solved, we just
need to mention what the problem is, so that Prolog automatically solves it.
However, in Prolog we are supposed to give clues as the solution method.
Prolog language basically has three different elements −
Facts − The fact is predicate that is true, for example, if we say, “Tom is the son
of Jack”, then this is a fact.
Rules − Rules are extinctions of facts that contain conditional clauses. To satisfy
a rule these conditions should be met. For example, if we define a rule as −
grandfather(X, Y) :- father(X, Z), parent(Z, Y)
This implies that for X to be the grandfather of Y, Z should be a parent of Y and
X should be father of Z.
Questions − And to run a prolog program, we need some questions, and those
questions can be answered by the given facts and rules.
Knowledge Base − This is one of the fundamental parts of Logic Programming.
We will see in detail about the Knowledge Base, and how it helps in logic
programming.
Facts, Rules and Queries − These are the building blocks of logic programming.
We will get some detailed knowledge about facts and rules, and also see some
kind of queries that will be used in logic programming.
Here, we will discuss about the essential building blocks of logic programming.
These building blocks are Facts, Rules and the Queries.
Facts
Rules
Is understood as
P :- (Q,R);(S,T,U).
Or can also be written as:
P :- Q,R.
P :- S,T,U.
Example
happy(lili) :- dances(lili).
hungry(tom) :- search_for_food(tom).
friends(jack, bili) :- lovesCricket(jack), lovesCricket(bili).
goToPlay(ryan) :- isClosed(school), free(ryan).
Queries
Queries are some questions on the relationships between objects and object
properties. So question can be anything, as given below −
Is tom a cat?
Does Kunal love to eat pasta?
Is Lili happy?
Will Ryan go to play?
So according to these queries, Logic programming language can find the answer
and return them.
In this section, we will see what knowledge base in logic programming is.
Well, as we know there are three main components in logic programming
− Facts, Rules and Queries. Among these three if we collect the facts and rules
as a whole then that forms a Knowledge Base. So we can say that the knowledge
base is a collection of facts and rules.
Now, we will see how to write some knowledge bases. Suppose we have our very
first knowledge base called KB1. Here in the KB1, we have some facts. The facts
are used to state things, that are unconditionally true of the domain of interest.
Knowledge Base 1
Suppose we have some knowledge, that Priya, Tiyasha, and Jaya are three girls,
among them, Priya can cook. Let’s try to write these facts in a more generic way
as shown below −
girl(priya).
girl(tiyasha).
girl(jaya).
can_cook(priya).
Note − Here we have written the name in lowercase letters, because in Prolog, a
string starting with uppercase letter indicates a variable.
Now we can use this knowledge base by posing some queries. “Is priya a girl?”,
it will reply “yes”, “is jamini a girl?” then it will answer “No”, because it does
not know who jamini is. Our next question is “Can Priya cook?”, it will say “yes”,
but if we ask the same question for Jaya, it will say “No”.
Output
GNU Prolog 1.4.5 (64 bits)
Compiled Jul 14 2018, 13:19:42 with x86_64-w64-mingw32-gcc
By Daniel Diaz
Copyright (C) 1999-2018 Daniel Diaz
| ?- change_directory('D:/TP Prolog/Sample_Codes').
yes
| ?- [kb1]
.
compiling D:/TP Prolog/Sample_Codes/kb1.pl for byte code...
D:/TP Prolog/Sample_Codes/kb1.pl compiled, 3 lines read - 489 bytes written,
10 ms
yes
| ?- girl(priya)
.
yes
| ?- girl(jamini).
no
| ?- can_cook(priya).
yes
| ?- can_cook(jaya).
no
| ?-
Let us see another knowledge base, where we have some rules. Rules contain
some information that are conditionally true about the domain of interest.
Suppose our knowledge base is as follows −
sing_a_song(ananya).
listens_to_music(rohit).
listens_to_music(ananya) :- sing_a_song(ananya).
happy(ananya) :- sing_a_song(ananya).
happy(rohit) :- listens_to_music(rohit).
playes_guitar(rohit) :- listens_to_music(rohit).
So there are some facts and rules given above. The first two are facts, but the rest
are rules. As we know that Ananya sings a song, this implies she also listens to
music. So if we ask “Does Ananya listen to music?”, the answer will be true.
Similarly, “is Rohit happy?”, this will also be true because he listens to music.
But if our question is “does Ananya play guitar?”, then according to the
knowledge base, it will say “No”. So these are some examples of queries based
on this Knowledge base.
Output
| ?- [kb2].
compiling D:/TP Prolog/Sample_Codes/kb2.pl for byte code...
D:/TP Prolog/Sample_Codes/kb2.pl compiled, 6 lines read - 1066 bytes written,
15 ms
yes
| ?- happy(rohit).
yes
| ?- sing_a_song(rohit).
no
| ?- sing_a_song(ananya).
yes
| ?- playes_guitar(rohit).
yes
| ?- playes_guitar(ananya).
no
| ?- listens_to_music(ananya).
yes
| ?-
Knowledge Base 3
The facts and rules of Knowledge Base 3 are as follows −
can_cook(priya).
can_cook(jaya).
can_cook(tiyasha).
likes(priya,jaya) :- can_cook(jaya).
likes(priya,tiyasha) :- can_cook(tiyasha).
Suppose we want to see the members who can cook, we can use one variable in
our query. The variables should start with uppercase letters. In the result, it will
show one by one. If we press enter, then it will come out, otherwise if we press
semicolon (;), then it will show the next result.
Let us see one practical demonstration output to understand how it works.
Output
| ?- [kb3].
compiling D:/TP Prolog/Sample_Codes/kb3.pl for byte code...
D:/TP Prolog/Sample_Codes/kb3.pl compiled, 5 lines read - 737 bytes written,
22 ms
warning: D:/TP Prolog/Sample_Codes/kb3.pl:1: redefining procedure
can_cook/1
D:/TP Prolog/Sample_Codes/kb1.pl:4: previous definition
yes
| ?- can_cook(X).
X = priya ? ;
X = jaya ? ;
X = tiyasha
yes
| ?- likes(priya,X).
X = jaya ? ;
X = tiyasha
yes
| ?-
Structures
Now let us see one example of structures in Prolog. We will define a structure of
points, Segments and Triangle as structures.
To represent a point, a line segment and a triangle using structure in Prolog, we
can consider following statements −
p1 − point(1, 1)
p2 − point(2,3)
S − seg( Pl, P2): seg( point(1,1), point(2,3))
T − triangle( point(4,Z), point(6,4), point(7,1) )
Note − Structures can be naturally pictured as trees. Prolog can be viewed as a
language for processing trees.
Matching in Prolog
Matching is used to check whether two given terms are same (identical) or the
variables in both terms can have the same objects after being instantiated. Let us
see one example.
Suppose date structure is defined as date(D,M,2020) = date(D1,apr, Y1), this
indicates that D = D1, M = feb and Y1 = 2020.
Following rules are to be used to check whether two terms S and T match −
If S and T are constants, S=T if both are same objects.
If S is a variable and T is anything, T=S.
If T is variable and S is anything, S=T.
If S and T are structures, S=T if −
o S and T have same functor.
o All their corresponding arguments components have to match.
Binary Trees
Prolog - Backtracking
Suppose A to G are some rules and facts. We start from A and want to reach G. The
proper path will be A-C-G, but at first, it will go from A to B, then B to D. When it finds
that D is not the destination, it backtracks to B, then go to E, and backtracks again to
B, as there is no other child of B, then it backtracks to A, thus it searches for G, and
finally found G in the path A-C-G. (Dashed lines are indicating the backtracking.) So
when it finds G, it stops.
Knowledge Base
boy(tom).
boy(bob).
girl(alice).
girl(lili).
As X will be a boy, so there are two choices, and for each boy there are two choices
alice and lili. Now let us see the output, how backtracking is working.
Output
| ?- [backtrack].
compiling D:/TP Prolog/Sample_Codes/backtrack.pl for byte code...
D:/TP Prolog/Sample_Codes/backtrack.pl compiled, 5 lines read - 703 bytes written, 22 ms
yes
| ?- pay(X,Y).
X = tom
Y = alice ?
X = tom
Y = alice ? ;
X = tom
Y = lili ? ;
X = bob
Y = alice ? ;
X = bob
Y = lili
yes
| ?- trace.
The debugger will first creep -- showing everything (trace)
X = tom
Y = alice ? ;
1 1 Redo: pay(tom,alice) ?
3 2 Redo: girl(alice) ?
3 2 Exit: girl(lili) ?
1 1 Exit: pay(tom,lili) ?
X = tom
Y = lili ? ;
1 1 Redo: pay(tom,lili) ?
2 2 Redo: boy(tom) ?
2 2 Exit: boy(bob) ?
3 2 Call: girl(_24) ?
3 2 Exit: girl(alice) ?
1 1 Exit: pay(bob,alice) ?
X = bob
Y = alice ? ;
1 1 Redo: pay(bob,alice) ?
3 2 Redo: girl(alice) ?
3 2 Exit: girl(lili) ?
1 1 Exit: pay(bob,lili) ?
X = bob
Y = lili
yes
{trace}
| ?-
Preventing Backtracking
So far we have seen some concepts of backtracking. Now let us see some drawbacks
of backtracking. Sometimes we write the same predicates more than once when our
program demands, for example to write recursive rules or to make some decision
making systems. In such cases uncontrolled backtracking may cause inefficiency in a
program. To resolve this, we will use the Cut in Prolog.
Suppose we have some rules as follows −
Program
f(X,0) :- X < 3. % Rule 1
f(X,2) :- 3 =< X, X < 6. % Rule 2
f(X,4) :- 6 =< X. % Rule 3
Output
| ?- [backtrack].
compiling D:/TP Prolog/Sample_Codes/backtrack.pl for byte code...
D:/TP Prolog/Sample_Codes/backtrack.pl compiled, 10 lines read - 1224 bytes written, 17 ms
yes
| ?- f(1,Y), 2<Y.
no
| ?- trace
.
The debugger will first creep -- showing everything (trace)
yes
{trace}
| ?- f(1,Y), 2<Y.
1 1 Call: f(1,_23) ?
2 2 Call: 1<3 ?
2 2 Exit: 1<3 ?
1 1 Exit: f(1,0) ?
3 1 Call: 2<0 ?
3 1 Fail: 2<0 ?
1 1 Redo: f(1,0) ?
2 2 Call: 3=<1 ?
2 2 Fail: 3=<1 ?
2 2 Call: 6=<1 ?
2 2 Fail: 6=<1 ?
1 1 Fail: f(1,_23) ?
(46 ms) no
{trace}
| ?-
Let us see the same using cut.
Program
f(X,0) :- X < 3,!. % Rule 1
f(X,2) :- 3 =< X, X < 6,!. % Rule 2
f(X,4) :- 6 =< X. % Rule 3
Output
| ?- [backtrack].
1 1 Call: [backtrack] ?
compiling D:/TP Prolog/Sample_Codes/backtrack.pl for byte code...
D:/TP Prolog/Sample_Codes/backtrack.pl compiled, 10 lines read - 1373 bytes written, 15 ms
1 1 Exit: [backtrack] ?
(16 ms) yes
{trace}
| ?- f(1,Y), 2<Y.
1 1 Call: f(1,_23) ?
2 2 Call: 1<3 ?
2 2 Exit: 1<3 ?
1 1 Exit: f(1,0) ?
3 1 Call: 2<0 ?
3 1 Fail: 2<0 ?
no
{trace}
| ?-
Negation as Failure
Here we will perform failure when condition does not satisfy. Suppose we have a
statement, “Mary likes all animals but snakes”, we will express this in Prolog.
It would be very easy and straight forward, if the statement is “Mary likes all animals”.
In that case we can write “Mary likes X if X is an animal”. And in prolog we can write
this statement as, likes(mary, X) := animal(X).
Our actual statement can be expressed as −
If X is snake, then “Mary likes X” is not true
Otherwise if X is an animal, then Mary likes X.
In prolog we can write this as −
likes(mary,X) :- snake(X), !, fail.
likes(mary, X) :- animal(X).
The ‘fail’ statement causes the failure. Now let us see how it works in Prolog.
Program
animal(dog).
animal(cat).
animal(elephant).
animal(tiger).
animal(cobra).
animal(python).
snake(cobra).
snake(python).
yes
| ?- likes(mary,elephant).
yes
| ?- likes(mary,tiger).
yes
| ?- likes(mary,python).
no
| ?- likes(mary,cobra).
no
| ?- trace
.
The debugger will first creep -- showing everything (trace)
yes
{trace}
| ?- likes(mary,dog).
1 1 Call: likes(mary,dog) ?
2 2 Call: snake(dog) ?
2 2 Fail: snake(dog) ?
2 2 Call: animal(dog) ?
2 2 Exit: animal(dog) ?
1 1 Exit: likes(mary,dog) ?
yes
{trace}
| ?- likes(mary,python).
1 1 Call: likes(mary,python) ?
2 2 Call: snake(python) ?
2 2 Exit: snake(python) ?
3 2 Call: fail ?
3 2 Fail: fail ?
1 1 Fail: likes(mary,python) ?
no
{trace}
| ?-