Pawan
Pawan
What is Prolog?
• Prolog stands for programming in logic.
• Prolog is a declarative language, which means that a program consists of data
based on the facts and rules (Logical relationship) rather than computing how
to find a solution.
• A logical relationship describes the relationships which hold for the given
application.
• To obtain the solution, the user asks a question rather than running a program.
When a user asks a question, then to determine the answer, the run time
system searches through the database of facts and rules.
• Prolog is a declarative language that means we can specify what problem we
want to solve rather than how to solve it.
• Prolog is used in some areas like database, natural language processing,
artificial intelligence, but it is pretty useless in some areas like a numerical
algorithm or instance graphics.
• In artificial intelligence applications, prolog is used. The artificial intelligence
applications can be automated reasoning systems, natural language interfaces,
and expert systems. The expert system consists of an interface engine and a
database of facts. The prolog's run time system provides the service of an
interface engine.
Applications of Prolog
• Specification Language
• Robot Planning
• Natural language understanding
• Machine Learning
• Problem Solving
• Intelligent Database retrieval
• Expert System
• Automated Reasoning
Starting Prolog
• Prolog system is straightforward.
• Prolog will produce a number of lines of headings in the starting, which is
followed by a line. It contains just
• ?-
• The above symbol shows the system prompt. The prompt is used to show that
the Prolog system is ready to specify one or more goals of sequence to the
user.
• Using a full stop, we can terminate the sequence of goals For example:
Prolog Programs
• To write a Prolog program, firstly, the user has to write a program which is
written in the Prolog language, load that program, and then specify a sequence
of one or more goals at the prompt.
• To create a program in Prolog, the simple way is to type it into the text editor
and then save it as a text file like prolog1.pl.
• The following example shows a simple program of Prolog. The program
contains three components, which are known as clauses. Each clause is
terminated using a full stop.
dog(rottweiler).
cat(munchkin).
animal(A) :- cat(A).
• Using the built-in predicate 'consult', the above program can be loaded in the
Prolog system.
• ?-consult('prolog1.pl').
• This shows that prolog1.pl file exists, and the prolog program is systemically
correct, which means it has valid clauses, the goal will succeed, and to confirm
that the program has been correctly read, it produces one or more lines of
output. e.g.,
• ?-
# 0.00 seconds to consult prolog1.pl
?-
• The alternative of 'consult' is 'Load', which will exist on the menu option if the
Prolog system has a graphical user interface.
• When the program is loaded, the clause will be placed in a storage area, and
that storage area is known as the Prolog database. In response to the system
prompt, specify a sequence of goals, and it will cause Prolog to search for and
use the clauses necessary to evaluate the goals.
Terminology
In the following program, three lines show the clauses.
Pawan Kumar Mishra 1900321290043 AI Lab (KCS – 751 A) CEIT
dog(rottweiler).
cat(munchkin).
animal(A) :- cat(A).
Using the full stop, each clause will be terminated. Prolog programs have a
sequence of clauses. Facts or rules are described by these clauses.
Experiment-1 (B)
OBJECTIVE: Write simple fact for the statements using PROLOG.
Facts
A fact is like a predicate expression. It is used to provide a declarative statement about
the problem. In a Prolog expression, when a variable occurs, it is assumed to be
universally quantified. Facts are specified in the form of the head. Head is known as
the clause head. It will take in the same way as the goal entered at the prompt by the
user.
cat(bengal). /* bengal is a cat */
dog(rottweiler). /* rottweiler is a dog */
likes(Jolie, Kevin). /* Jolie likes Kevin */
likes(A, Kevin). /* Everyone likes Kevin */
likes(Jolie, B). /* Jolie likes everybody */
likes(B, Jolie), likes(Jolie, B). /* Everybody likes Jolie and Jolie likes everybody *
/
likes(Jolie, Kevin); likes(Jolie, Ray). /* Jolie likes Kevin or Jolie likes Ray */
not(likes(Jolie, pasta)). /* Jolie does not like pasta */
Queries
In Prolog, the query is the action of asking the program about the information which
is available within its database. When a Prolog program is loaded, we will get the
query prompt,
?-
After this, we can ask about the information to the run time system. Using the above
simple database, we can ask a question to the program like
?- 'It is sunny'.
and it will give the answer
yes
?-
The system responds to the query with yes if the database information is consistent to
answer the query. Using the available database information, we can also check that
the program is capable of proving the query true. No indicates that the fact is not
deducible based on the available information.
The system answers no to the query if the database does not have sufficient
information.
?- 'It is cold'.
no
?-
Program:
likes(ram, mango).
girl(seema).
red(rose).
likes(bill, cindy).
owns(john, gold).
Output:
Example 2.
Statements:
1. The Cakes are delicious.
2. The Pickles are delicious.
3. The Pickles are spicy.
4. Priya relishes coffee.
5. Priya likes food if they are delicious.
6. Prakash likes food if they are spicy and delicious.
Statements in Prolog:
1. delicious(cakes).
2. delicious(pickles).
3. spicy( pickles).
4. relishes(priya, coffee).
5. likes(priya, Food) if delicious(Food). %Here Food is a variable
6. likes(prakash,Food) if spicy(Food) and delicious(Food).
Pawan Kumar Mishra 1900321290043 AI Lab (KCS – 751 A) CEIT
Program Clauses:
Screenshot Output-
Pawan Kumar Mishra 1900321290043 AI Lab (KCS – 751 A) CEIT
Experiment – 2(A)
OBJECTIVE: Write predicates One converts centigrade temperatures to
Fahrenheit, the other checks if a temperature is below freezing.
Rule
Centigrade to Fahrenheit (c_to_f)F is C * 9 / 5 + 32
Program-
Output:
Pawan Kumar Mishra 1900321290043 AI Lab (KCS – 751 A) CEIT
Experiment-2 (B)
OBJECTIVE: Write a program to solve the Monkey Banana problem.
Program:
Pawan Kumar Mishra 1900321290043 AI Lab (KCS – 751 A) CEIT
OUTPUT:
Pawan Kumar Mishra 1900321290043 AI Lab (KCS – 751 A) CEIT
Experiment-3
OBJECTIVE: Write a program to design medical diagnosis expert system in SWI
Prolog. The system must work for diagnosis of following diseases: -
a) measles
b) germanmeasles
c) Flu
d) commoncold
e) mumps
f). chickenpox
Program Code:
Pawan Kumar Mishra 1900321290043 AI Lab (KCS – 751 A) CEIT
OUTPUT:
Pawan Kumar Mishra 1900321290043 AI Lab (KCS – 751 A) CEIT