KEMBAR78
Prolog PDF 2 | PDF | Programming Paradigms | Computer Programming
0% found this document useful (0 votes)
25 views30 pages

Prolog PDF 2

Prolog is a declarative programming language used in the logic programming paradigm, where programs consist of facts and rules rather than instructions. It is utilized in various fields such as artificial intelligence, natural language processing, and expert systems, and operates by allowing users to pose queries that the system answers based on its knowledge base of facts and rules. Key features of Prolog include pattern matching, backtracking, and a syntax that distinguishes between facts, rules, and queries.

Uploaded by

SHUBH SINGH
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)
25 views30 pages

Prolog PDF 2

Prolog is a declarative programming language used in the logic programming paradigm, where programs consist of facts and rules rather than instructions. It is utilized in various fields such as artificial intelligence, natural language processing, and expert systems, and operates by allowing users to pose queries that the system answers based on its knowledge base of facts and rules. Key features of Prolog include pattern matching, backtracking, and a syntax that distinguishes between facts, rules, and queries.

Uploaded by

SHUBH SINGH
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/ 30

What is Prolog

 Prolog stands for programming in logic. Prolog is the most


commonly used programming language in the logic programming
paradigm.
 Because Prolog is a declarative language, programmes written in it
don't compute how to solve problems; instead, they are composed
of data founded on facts and rules (logical relationships). The
relationships that apply to the particular application are described
by a logical relationship.
 Instead of using a program, the user poses a question to get the
answer. The run-time system searches through the database of
facts and rules to find the answer when a user poses a question.
 Marseille Prolog, which is built on Colmerauer's work, was the
original Prolog. Prolog was a prominent example of a fourth-
generation computer language. The declarative computing
paradigm is supported by it.
 An announcement of a fifth-generation Japanese computer project was
made in 1981. Following that, Prolog was accepted as a development
language. The programme for this lesson was created using Edinburgh
Prolog's "Standard" syntax. The other category of prologues that are
decedents of Marseille Prolog is PrologII family prologues.
 Prolog features include a pattern-matching facility, a mathematical
variable, and interchangeable input and output. They also operate like
uniform data structures and have a backtracking strategy for searching for
proofs.
 There will be several ways to determine the solution. The run-time system
will be asked to discover an alternative remedy in such a situation. Use the
backtracking strategy to come up with an alternative answer. A weakly
typed language with dynamic type checking and static scope constraints is
Prolog.
 Because Prolog is a declarative language, we can define the issue we want
to solve rather than the method for doing so.
 Prolog is used in some fields, such as databases, NLP, and AI, but it is largely
worthless in others, such as numerical algorithms and instance graphics.
 Prolog is employed in projects involving artificial intelligence. Automated
reasoning systems, user interfaces that use natural language, and expert
systems are some examples of artificial intelligence uses. A database of
information and an interface engine make up the expert system. An interface
mechanism is offered by the prologue run time system.
 There are no literal numbers in an environment for fundamental logic
programming. Variables are denoted by an identifier with capital characters
and other identifiers. Data values are indicated by identifiers that begin with
lowercase characters. Typeless components make up the core of Prolog. The
majority of prologue versions now support integer values, characters, and
operations. The tuples and collections are described in the Prolog Mechanism.
 Prolog and functional computer languages share some characteristics with
Hugs. Relation term is used in a logic programme. A series of function
definitions are used to create a functional computer language. Recursive
definitions are crucial to logical computing and functional programming.
Applications of Prolog
 The applications of prolog are as follows:
 Specification Language
 Robot Planning
 Natural language understanding
 Machine Learning
 Problem Solving
 Intelligent Database retrieval
 Expert System
 Automated Reasoning
Prolog Version
We are using GNU Prolog, Version: 1.5.0

Official Website
 This is the official GNU Prolog website where you can
see all the necessary details about GNU Prolog, and also
get the download link.

 http://www.gprolog.org/
Starting Prolog

 Prolog system is straightforward. From one person to


other person, the precise details of Prolog will vary.
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:
?- write('Welcome to VIT'),nl,write('First class of Prolog'),nl.
nl indicates 'start a new line'. When we press 'return' key, the above
line will show the effect like this:
Welcome to VIT
First class of Prolog
yes
?- prompt shows the sequence of goal which is entered by the user.
The user will not type the prompt. Prolog system will automatically
generate this prompt. It means that it is ready to receive a
sequence of goals.
Consider the following sequence of goals:
write('Welcome to VIT'),nl,write('First class of Prolog'),nl.
The above sequence of goals has to succeed in order to be
succeeded.
 write('Welcome to VIT')
On the screen of the user, Welcome to Javatpoint has to be
displayed
 nl
On the screen of the user, a new line has to be output
 write('First class of Prolog')
On the screen of the user, Example of Prolog has to be displayed
 nl
On the screen of the user, a new line has to be output
 All these goals will simply achieve by the Prolog system by
outputting the line of text to the screen of the user. To show that
the goals have succeeded, we will output yes.
 The Prolog system predefined the meanings of nl and write. Write
and nl are called as built-in predicates.
 Halt and statistics are the two other built-in predicates. In almost
all Prolog versions, these predicates are provided as standard.
 ?- halt.
The above command is used to terminate the Prolog system.
 ?- statistics.
This command will cause the Prolog system statistics. This statistics
feature is mainly used to experienced user. In statistics, the
following things will generate:
The above output ends with Yes. Yes, specify that the goal has
succeeded, as halt, statistics, and many other built-in predicates
always do. When they evaluate, their values produce, which lies in the
side-effects.
'Query' is a sequence of one or more goals. These goals are entered
by the user at the prompt.
 Knowledge Base − This is one of the fundamental parts of Logic
Programming.

 Facts, Rules and Queries − These are the building blocks of logic
programming.

 Here, we will discuss about the essential building blocks of logic


programming. These building blocks are Facts, Rules and the
Queries.
Facts

We can define fact as an explicit relationship between objects, and properties


these objects might have. So facts are unconditionally true in nature. Suppose
we have some facts as given below −

Tom is a cat

Kunal loves to eat Pasta

Hair is of black color.

Nawaz loves to play games

Pratyusha is lazy.

So these are some facts, that are unconditionally true. These are actually
statements, that we have to consider as true.
Following are some guidelines to write facts −

Names of properties/relationships begin with lower case letters.

The relationship name appears as the first term.

Objects appear as comma-separated arguments within parentheses.

A period "." must end a fact.

Objects also begin with lower case letters. They also can begin with digits
(like 1234), and can be strings of characters enclosed in quotes e.g.
color(penink, ‘red’).

phoneno(agnibha, 1122334455). is also called a predicate or clause.


Syntax
The syntax for facts is as follows −

 relation(object1,object2...).

Example
Following is an example of the above concept −

 cat(tom).
 loves_to_eat(kunal,pasta).
 of_color(hair,black).
 loves_to_play_games(nawaz).
 lazy(pratyusha).
Rules

We can define rule as an implicit relationship between objects. So facts are


conditionally true. So when one associated condition is true, then the
predicate is also true. Suppose we have some rules as given below −

Lili is happy if she dances.

Tom is hungry if he is searching for food.

Jack and Bili are friends if both of them love to play cricket.

will go to play if school is closed, and he is free.

So these are some rules that are conditionally true, so when the right hand
side is true, then the left hand side is also true.
 Here the symbol ( :- ) will be pronounced as “If”, or “is implied by”. This is
also known as neck symbol, the LHS of this symbol is called the Head, and
right hand side is called Body. Here we can use comma (,) which is known
as conjunction, and we can also use semicolon, that is known as
disjunction.
Syntax
rule_name(object1, object2, ...) :- fact/rule(object1, object2, ...)
Suppose a clause is like :
 P :- Q;R.
This can also be written as
 P :- Q.
 P :- R.

If one clause is like :


 P :- Q,R;S,T,U.

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.
Knowledge Base in Logic
Programming
 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 Neha, Kamini, and Mili are three
girls, among them, Priya can cook. Let’s try to write these facts in a more
generic way as shown below −
 girl(neha).
 girl(kamini).
 girl(mili).
 can_cook(neha).
# 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 neha a
girl?”, it will reply “yes”, “is yamini a girl?” then it will answer “No”, because it
does not know who yamini is. Our next question is “Can neha cook?”, it will
say “yes”, but if we ask the same question for mili, it will say “No”.
Output
| ?- change_directory('C:/Users/Dell/Desktop/prolog examples').

yes
| ?- [kb1].
compiling C:/Users/Dell/Desktop/prolog examples/kb1.pl for byte code...
C:/Users/Dell/Desktop/prolog examples/kb1.pl compiled, 3 lines read - 507
bytes written, 15 ms

yes
| ?- girl(neha).

yes
| ?- girl(yamini).

no
| ?- can_cook(neha).

yes
| ?- can_cook(mili).

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
| ?- change_directory('C:/Users/Dell/Desktop/prolog examples').

yes
| ?- [kb2].
compiling C:/Users/Dell/Desktop/prolog examples/kb2.pl for byte code...
C:/Users/Dell/Desktop/prolog examples/kb2.pl compiled, 6 lines read - 1090 bytes written, 0 ms

yes
| ?- sing_a_song(ananya).

yes
| ?- happy(rohit).
yes
| ?- sing_a_song(rohit).

no
| ?- playes_guitar(ananya).

no
| ?- playes_guitar(rohit).

yes
| ?- playes_guitar(mohit).

no
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
| ?- change_directory('C:/Users/Dell/Desktop/prolog examples').

yes
| ?- [kb3]
.
compiling C:/Users/Dell/Desktop/prolog examples/kb3.pl for byte code...
C:/Users/Dell/Desktop/prolog examples/kb3.pl compiled, 5 lines read - 761 bytes written, 0 ms

yes
| ?- can_cook(X).

X = priya ? ;

X = jaya ? ;

X = tiyasha

(31 ms) yes


| ?-
| ?- likes(priya,B).

B = jaya ? ;

B = tiyasha

(16 ms) yes


| ?-

You might also like