Javatpoint Logo
Javatpoint Logo

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

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.

Using the built-in predicate 'consult', the above program can be loaded in the Prolog system.


This shows that 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

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.


In the following program, three lines show the clauses.

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.

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).

To imply that munchkin is an animal, there is no evidence of this.

?- animal(munchkin).

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 */.

Predicate dog and predicate cat both have four clauses. Assume that in a text file '', 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('').                 System prompt
# 0.01 seconds to consult        loaded using the consult

?- dog(rottweiler).

?- dog(boxer).

?- 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 */


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:


This will give us all possible combinations of a cat and a dog.

A = sphinx,
B = rottweiler;

A = sphinx,
B = poodle;

A = sphinx,
B = bulldog;

A = sphinx,
B = dobermann;


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.


Next TopicTypes of Prolog

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA