o Prolog stands for programming in logic.
In the logic programming paradigm, prolog
language is most widely available. 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.
o 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.
o Prolog features are 'Logical variable', which means that they behave like uniform
data structure, a backtracking strategy to search for proofs, a pattern-matching
facility, mathematical variable, and input and out are interchangeable.
o To deduce the answer, there will be more than one way. In such case, the run time
system will be asked to find another solution. To generate another solution, use the
backtracking strategy. Prolog is a weakly typed language
o Prolog is a declarative language that means we can specify what problem we want to
solve rather than how to solve it.
o 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.
o 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
The applications of prolog are as follows:
o Specification Language
o Robot Planning
o Natural language understanding
o Machine Learning
o Problem Solving
o Intelligent Database retrieval
o Expert System
o Automated Reasoning
?-
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 Javatpoint'),nl,write('Example 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 Javatpoint
Example 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.
The above example shows a sequence of goals entered by the user like this:
write('Welcome to Javatpoint'), write('Example of Prolog'), nl(twice).
Consider the following sequence of goals:
write('Welcome to Javatpoint'),nl,write('Example of Prolog'),nl.
The above sequence of goals has to succeed in order to be succeeded.
o write('Welcome to Javatpoint')
On the screen of the user, Welcome to Javatpoint has to be displayed
o nl
On the screen of the user, a new line has to be output
o write('Example of Prolog')
On the screen of the user, Example of Prolog has to be displayed
o 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.
o ?- halt.
The above command is used to terminate the Prolog system.
o ?- 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. In this tutorial, we are generally using the 'sequence of goals' term.
Prolog Programs
Using the built-in predicates, the sequence of goals, or specifying a goal at the system prompt
would be of little value in itself. 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.
1. dog(rottweiler).
2. cat(munchkin).
3. 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.
1. dog(rottweiler).
2. cat(munchkin).
3. 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.
Example of facts is dog(rottweiler) and cat(munchkin). They mean that
'rottweiler is a dog' and 'munchkin is a cat'.
Dog is called a predicate. Dog contains one argument. Word 'rottweiler' enclosed in
bracket( ). Rottweiler is called an atom.
The example of rule is the final line of the program.
1. animal(A) :- dog(A).
The colon(:-) character will be read as 'if'. Here A is a variable, and it represents any
value. In a natural way, the rule can be read as "If A is an animal, then A is a dog".
The above clause shows that the rottweiler is an animal. Such deduction can also
make by Prolog:
?-animal(rottweiler).
yes
To imply that munchkin is an animal, there is no evidence of this.
?-animal(munchkin).
no
More Terminology
Evaluating a goal term determines whether or not it is satisfied. It also means that
goal evaluates to true or false.
Note that when a user enters a goal, then sometimes it can be interpreted as a
command. For example,
?- halt. 'It is used to exit from the Prolog system.'
Sometimes it can be regarded as a question like,
?- animal(rottweiler). & 'Is rottweiler an animal?'
The following program shows another example about animals. It comprises eight
clauses. The comment is shown by all the text between the /* and */.
1. /* Another Program of Animal */
2. Dog(rottweiler).
3. cat(sphynx). dog(poodle).
4. dog(bulldog). cat(bengal).
5. dog(dobermann).
6. cat(himalayan). cat(singapura).
7.
/* This Prolog program consists of various clauses. It is always terminated using the f
ull stop.*/
Predicate dog and predicate cat both have four clauses. Assume that in a text file
'animal.pl', the program has been saved, and output is generated by loading the
program and at the system prompt, we are entering a sequence of goals as follows:
?-consult('animals1.pl'). System prompt
# 0.01 seconds to consult animals.pl animals.pl loaded using the
consult
?-dog(rottweiler).
yes
?-dog(boxer).
no
?-dog(A).
A = rottweiler pauses- return key is pressed by the user
?-dog(B).
B = rottweiler; pauses ? user presses ;
B = poodle; pauses ? user presses ;
B = bulldog; pauses ? user presses ;
B = dobermann No pause ? It will go onto next line
?-cat(A). A = sphinx; pause ? user presses;
A = Bengal pauses ? user presses return
?- listening(dog). It will list all the clauses which define predicate dog
/* dog/1 */
dog(rottweiler).
dog(poodle).
dog(bulldog).
dog(dobermann).
yes
?-
In this example, various new features of Prolog are introduced. The query is as
follows:
?- dog(A).
It means that find the A's value, and it will be the name of the dog. The answer of
Prolog is as follows:
A = rottweiler
Other possible answers of A are as follows, poodle, bulldog, dobermann. It will cause
the Prolog pause, and because of this, we have to wait for the user to press the
'return' key before it outputs the system prompt ?-.
We can enter the next query as follows:
?- dog(B).
This query is the same as before. The above query means that 'find the B's value,
and it will be the name of a dog'. The answer of Prolog is as follows:
B = rottweiler
Prolog will again pause. This time semicolon (;) key is pressed by the user. Now
Prolog will find for an alternative value of B that satisfies the goal dog(B). It will reply
as follows:
B = poodle
Prolog will again pause. The semicolon (;) key is again pressed by the user. Prolog
produces a further solution as follows:
B = bulldog
Prolog will again pause. The semicolon (;) key is again pressed by the user. Prolog
produces a further solution as follows:
B = dobermann
Prolog recognizes that there is no more available solution by not pausing, but the
system prompt ?- by immediately going on to the output.
A new built-in predicate is introduced in this example. Specifying the goal
?- listing(dog)
In the above goal, Prolog will list all four clauses which define the predicate dog.
They will define in the same order as they loaded into the database.
The use of variables in the query is shown by the following example. The sequence
of goal is as follows:
?-cat(A),dog(B).
This will give us all possible combinations of a cat and a dog.
?-cat(A),dog(B).
A=sphinx,
B = rottweiler;
A=sphinx,
B = poodle;
A=sphinx,
B = bulldog;
A=sphinx,
B = dobermann;
etc.
In contrast, the sequence of goal is as follow:
?-cat(A), dog(A).
This will give all animals which are both a cat and a dog (in the database, there is no
such animal). Here A is 'any value' in both cat(A) and dog(A), but both must have the
same value.
?-cat(A),dog(A).
no