Home » Loading clauses in Prolog

Loading clauses in Prolog

by Online Tutorials Library

Loading clauses in Prolog

To load the clauses into the database, there are two built-in predicates like: consult/1 and reconsult/1. In both predicates, clauses available in a text file will be loaded into the database. The following example shows the difference between them. Let’s assume, file1.pl file contains the following details:

The file2.pl file contains the following details:

Now we will enter the two goals as follows:

At the prompt, after succession these goals will put these clauses in the database.

Effectively, the clauses which are loaded from the second file are added to those already loaded from the first file. The clauses are added predicate by predicate. In the above example, we can see that the dog(fido) appears twice in the database. In the Prolog system, there is nothing to prevent this.

By contrast, we will enter the two goals as follows:

At the prompt, after succession these goals will put these clauses in the database.

In file2.pl, the definition of predicate completely replaces any previous clauses in the database. Now in the usual way, the new predicates can load. The above example shows the following things:

  1. The dog/1, cat/1, and large/1 definition will replace those already in the database.
  2. In file1.pl, the small/1 definition will remain in the database.
  3. In file2.pl, the large_animal/1 definition is placed in the database.

The above example shows that reconsult is most unhelpful. But reconsult is routinely used in normal program development. Using various consult goals, some program developers choose several parts to load a large program. But the most common method of developing a program by the programmer is to load an entire program as a single file, test the file, and then make the changes in the file. In a new version of file, the programmer will save the changes using the same name, and then the programmer will reload the clauses from the file. To work it properly, we have to check that each time older versions of clauses are deleted. Using the consult, we can achieve this first time and then reconsult each subsequent time.

In various Prolog versions, consult and reconsult predicates are used so frequently. A simplified notation of Prolog describes that [‘file1.pl’] standing for consult(‘file1.pl’), and [-file.pl] standing for reconsult(‘file1.pl’).


You may also like