Guide to Prolog Programming

© Roman Barták, 1998

Home
Prolog in Examples

Previous | Contents | Next

First Steps in Prolog

In this chapter we will go through few examples to show basic ideas behind Prolog programming. But first, just few notes about using Prolog systems.

A Prolog program is a set of procedures (the order is indifferent), each procedure consists of one or more clauses (the order of clauses is important). There are two types of clauses: facts and rules, you will understand the difference soon. The program must be written in advance in some text processor like Emacs. Only some Prolog systems contain IDE (Integrated Development Environment). When the Prolog system is started, you will see a prompt, usually in the following form:

?-

The system is waiting for your commands/questions. First, the program should be loaded to the Prolog database. You can load a program into the database by using the consult command in the following way (do not forget the dot at the end!):

?-consult('name_of_the_file_with_the_program').

The consult command adds the clauses and facts from the specified text file to the clauses and facts already stored in the Prolog database. Thus you can load more programs into the database but be careful whether the programs do not use the procedures with the same name. Otherwise, because of cumulating of clauses, these procedures could behave incorrectly.

You can also use the reconsult command to load the program

?-reconsult('name_of_file_with_the_program').

that behaves like the consult command (it adds procedures into the database) but if there is a procedure in the database with the same name as any procedure in the reconsulted file, then the former procedure is replaced by the new definition. You will usually use the reconsult command to update the Prolog database after change of the program.

It is possible to find out the actual contents of the Prolog database by using the listing command:

?-listing.

Your Prolog program is started by calling some procedure of the program in the following way:

?-procedure_name(parameters).

Note, that you have already called a procedure when you consulted or reconsulted the file. "Calling a procedure" is called "asking a question" in Prolog.

And how do you stop the Prolog system? Simply use the command halt (again with the dot at the end):

?-halt.


Designed and maintained by Roman Barták

Previous | Contents | Next