Skip to content

2 Prolog Source Code

Ken Bowen edited this page Nov 17, 2015 · 16 revisions

Just like most other computer languages, Prolog allows you to store programs and data in text files. The builtin predicates consult/1, reconsult/1, read/1, and their variants will translate the textual representation for programs and data into the internal representations used by the Prolog system. This section describes what kinds of syntactic objects can appear in source files and how they are interpreted.

###2.1 Source Terms

Every Prolog source file must be a sequence of zero or more Prolog terms, each term followed by a period (.) and a white space character. A period followed by a white space character is called a full stop. Full stops are needed in source files to show where one term ends and another begins. Each term in a file is treated as a closed logical formula. This means that even though two seperate terms have variable names in common, each term’s variables are actually distinct from the variables in any other term. Most variables are quantified once for each term. However, there is a special variable, the anonymous variable (written ‘_ ’), which is quantified for each occurrance. This means that within a single term, every occurrance of ‘_’ is a different variable.

####2.1.1 Rules

A rule is the most common programming construct in Prolog. A rule says that a particular property holds if a conjunction of properties holds. Rules are always written with :-/2 as the principal functor. The first argument of the :- is called the head, and the second argument is called the body. Here are some examples of rules:

a(X) :- b(X).  
blt :- bacon, lettuce, tomato.  
test(A,B,C) :- cond1(A,B), cond2(B,C).  

consult/1 and reconsult/1 load rules (and facts - see below) from a source file into the internal run-time Prolog database in the order they occur in the source file.

####2.1.2 Facts

A fact is any term which is not a rule and which cannot be interpreted as a directive or declaration. For example,

module mymodule.

would not be interpreted as a fact since it is a module declaration - see Chapter 3 Modules.{ADD LINK} More specifically, a fact is any term that cannot be interpreted as a declaration and whose principal functor is not :-/1, ?-/1, or :-/2. One way to understand a fact is to say that it is a rule without a body, or a rule with a trivial body (one that is always true). These are example facts:

mortal(socrates).  
big(ben).  
identical(X,X).  

As with rules, consult/1 and reconsult/1 load facts (and rules)from a source file into the internal database in the order they occur in the source file.

####2.1.3 Commands and Queries

A command or directive is any term whose principal functor is :-/1. Queries are terms whose principal functor is ?-/1. The single argument to a command or query is a conjunction of goals to be executed by the prolog engine. Commands and queries can be submitted via the prolog shell, or can be included in a prolog program, to be executed whenever the program is loaded (consulted) by the engine.

Commands and queries either succeed or fail depending on the program context in which they occur. Commands are silent in all contexts unless they fail, or unless some subsidiary goal inside the command invokes a write on the current output stream. When a command fails (either using the prolog shell or during consulting a file), a warning message is written to standard output, which is usually the screen or console window.

Like commands, when queries fail, a warning message is written to standard output. However, when queries succeed, a success message is also written to standard output. If a query contains unbound prolog variables and it is submitted via the prolog shell, the success message will describe values for those variables which were bound during the successful execution.

Commands are often used in prolog source files to require the consult or reconsult of other prolog source files, or to add operator declarations to the prolog parser.

Here are some examples of commands:

:- op(300, xfx, #).
:- [file1], [file2], write(’Files 1&2 have been loaded’).
:- initializeProgram, topLevelGoal.

The prolog shell accepts queries from a terminal or window, and prints the results. Because the shell assumes that each input is a query, it uses the query symbol ?- as a prompt. Suppose the following program of simple facts has been loaded:

wavelength(red, 620-750).
wavelength(yellow, 570-590).
wavelength(blue, 450-495).

Then the follow are some simple query interations with this program using the shell:

?- wavelength(blue,W).

W=450-495 

yes.
?- wavelength(orange, W).

no.
?- wavelength(C, W).

C=red 
W=620-750 ;

C=yellow 
W=570-590 ;

C=blue 
W=450-495 ;

no.

Notice that when a success answer is printed, the shell waits for input from the user. If the user types a semi-colon (;), the shell attempts to find further solutions for the query, and prints the next one if found, or "no." if no solutions are found. Any input other than a semi-colon is interpreted to mean that no further solutions are required.

####2.1.4 Declarations

Declarations are terms that have a special interpretation when seen by consult or reconsult. Here are some example declarations:

use builtins.
export a/1, b/2, c/3.
module foobar.

###2.2 Program Files Program files are sequences of source terms that are meant to be read in by consult/1 or reconsult/1, which interpret the terms as either clauses, declarations, commands, or queries.

####2.2.1 Consulting Program Files

To consult a file means to read the file, load the file’s clauses into the internal Prolog database, and execute any commands or directives occurring in the file. Reconsulting a file causes part or all of the current definitions in the internal prolog database for procedures which occur in the file to be discarded and the new ones (from the file) to be loaded, as well as executing any commands or directives in the file (again). See Chapter 11 (Prolog Builtins: Non-I/O) consult/1. {ADD LINK}

A file is consulted by the query or goal

?- consult(filename).    or    :-  consult(filename).

or reconsulted by the query or goal

?- reconsult(filename).    or    :- reconsult(filename).

Several files can be consulted or reconsulted at once by enclosing the file names in list brackets, as in

?- [file1,file2,file3].

By default, files listed this way (inside list brackets) are reconsulted.

To insist that one or more files in such a list be consulted (which might cause some of the clauses from the files to be doubled in memory), prefix a ‘+’ to the filename, as in:

?- [file1,+file2,file3].

In this case, file2 will be consulted instead of reconsulted. For consistency and backwards compatibility, one can also prefix a ‘-’ to indicate that the file should be reconsulted, even though this is redundant:

?- [file1,+file2,-file3].

When any of these consult goals are presented, first the terms from file1 are processed, then the terms from file2, and finally the terms from file3. It is permitted that clauses for the same procedure to occur in more than one file being consulted. In this case, clauses from the earlier file are listed in the internal database before clauses from the later file. Thus, if both file1 and file2 contain clauses for procedure p, those from file1 will be listed in the internal database before those from file2.

Clauses in the internal database are ‘tagged’ with the file from which they originated. When a file is reconsulted, only those clauses in memory which are tagged as originating from that file will be discarded at the start of the reconsult operation. Thus, suppose that both file1 and file3 contain clauses for the procedure p, and that we initially perform

?- [file1,file2,file3].

Then suppose that we edit file3, and then perform

?- [file3].

The clauses for p originally loaded from file1 will remain undisturbed. The clauses currently in memory for p originally from file3 will be discarded, and the new clauses from file3 will be loaded.

####2.2.2 Using Filenames in Prolog

Note: Complete path names to files are of course quite variable across operating systems. The discussions below are only intended to describe those aspects of file names and path names which affect how ALS Prolog locates files. Examples are provided for all the operating systems supported by ALS Prolog. File names follow the ordinary naming conventions of the host operating system. Thus all of the following are acceptable file names:

Linux (including Mac OS X):
    fighter  cave.man  hack/cave.man
    /usr/hack/cave.man

Win32:
    fighter  cave.man  hack\cave.man
    C:\usr\hack\cave.man
Clone this wiki locally