2.4 Loading programs, editing programs

The standard Prolog predicates for loading programs are 'consult', 'reconsult', and the bracket loader notation '[ ...]'. For example, the goal
?- consult('lists.pro').
opens the file lists.pro and loads the clauses in that file into memory.

There are two main ways in which a prolog program can be deficient: either the source code has syntax errors, in which case there will be error messages upon loading, or else there is a logical error of some sort in the program, which the programmer discovers by testing the program. The current version of a prolog program is usually considered to be a prototype for the correct version in the future, and it is a common practice to edit the current version and reload and retest it. This rapid prototyping appraoch works nicely provided that the programmer has devoted sufficient time and effort to analyzing the problem at hand. Interestingly, if the rapid prototyping approach seems to be failing, this is an excellent signal to take up paper and pencil, rethink the requirements, and start over!

We could call our editor from within prolog ...

?- edit('lists.pro'). %% User defined edit, see below ...
and then upon returning from the editor (and assuming that the new version of the file was resaved using the same file name), one could use the goal
? reconsult('lists.pro').
to reload the program clauses into memory, automatically replacing the old definitions. If one had used 'consult' rather than 'reconsult' the old (and possibly incorrect) clauses would have remained in memory along with the new clauses (... this depends upon the Prolog system, actually).

If several files have been loaded into memory, and one needs to be reloaded, use 'reconsult'. If the reloaded file defines predicates which are not defined in the remaining files then the reload will not disturb the clauses that were originally loaded from the other files.

The bracket notation is very handy. For example,

?- ['file1.pro',file2.pro',file3.pro'].
would load (effectively reconsult) all three files into prolog memory.

Many prolog systems rely upon the programmer to hava a favorite text editor for editing prolog programs. Here is a sample prolog program which calls TextEdit on the Mac(OSX). (This is just an illustration; we do not use TextEdit routinely for writing prolog programs.)

edit(File) :- 
   name('open -e ', TextEditString), %% Edit this line for your favorite editor
To use this editor, its source must be loaded (assume it's local to prolog session) ...
?- [edit].

and then an 'edit' goal can be used (again assume that the file to edit is local to the prolog session)...
?- edit('p.pl').

{ TextEdit starts up with file loaded. Edit the program... Save the program using the same filename ...}

Fig. 2.4.1 Calling external editor
Fig. 2.4.1 Calling external editor

After editing and saving the prolog program, we can reconsult the new version in the prolog session ...

?- reconsult('p.pl').

{ Our prolog session reloads the program for further testing ...}

It is possible to modify the little edit program to suit the user's particular circumstances (various prologs, various operating systems, various editors).

To load clauses supplied interactively by the user, use the goals

The user then types in clauses interactively, using stop '.' at the end of clauses, and ^Z to end input.

Exercise 2.4 Analyze how the edit program works. First, try goals ...

?- name(Name,"name").
name/2 is described in Section 4.13
Now is a good time for the reader to jump ahead and give a first reading to the first two sections of Chapter 3, How Prolog Works, and then return for more sample programs. It will be necessary to understand how the Prolog inference engine works in order to understand the construction of many Prolog programs.
Prolog Tutorial Contents

Valid HTML 4.01 Transitional