Prolog Directives

Amzi! has a number of directives that specify operators, properties of Prolog predicates, goals to be executed and the organization of predicates into modules. The module directives are described in Modules. The rest of the directives are described here:

Dynamic Clauses in Compiled Code

Sometimes it is desireable to have some predicates in an application be static (compiled) and others dynamic (interpreted). The dynamic directives, dynamic, sorted, indexed, let you tell the compiler which clauses are to remain dynamic in the logicbase, so they can be manipulated dynamically by the application.

Multifile Directive

Sometimes it is desireable to have the clauses that define a predicate to be split between several files. If this is the case, you must let the compiler know with the multifile directive.

It's syntax is:

You can have multiple multifile directives if you prefer.

Discontiguous Directive

The discontiguous directive is needed when the clauses of a static (compiled) predicate cannot be compiled as a single unit. This happens when the clause definitions are:

So, if the clauses are discontiguous, or there are a very large number of clauses for a predicate, that predicate needs to be declared as discontiguous.

It's syntax is:

You can have multiple discontiguous directives if you prefer.

Large numbers of clauses (1000s) usually only occur for data/facts, and those clauses might better be handled dynamically, maybe as indexed or sorted if performance is an issue.

Include Directive

The include directive consults a Prolog source file (.pro) at the point where the include is specified. In the interpreter it is consulted. In the compiler, it is read in and compiled.

:- include('myops.pro').

Ensure Loaded Directive

The ensure_loaded directive makes sure that the mentioned file is loaded. In a consulted file, it acts just like a latent expression to load the named .plm file. In compiled code, whether in a .plm or .xpl, it waits until the end of a load, and then checks to see if the named file has been loaded. If not, it loads it.

It is particularly useful for library modules, ensuring that they are there whether the program is being tested in the interpreter or run as a compiled program.

:- ensure_loaded(list).

Typically, you will need an import directive as well, such as:

:- import(list).

for the example above. See modules for details.

Op Directive

Prolog operators are defined with the op/3 directive. This is described fully under Prolog Terms. The op/3 directive establishes operator definitions for the program. It is both a compiler directive and a latent expression, so its result is used both during compilation and running.

Set Prolog Flag

Allows the setting of Prolog flags. It is both a compiler directive and a latent expression, so its result is used both during compilation and running. The example sets the default behavior for processing decimal numbers for both the compiler and the runtime.

:- set_prolog_flag(decimals, real).

Latent Expressions

A latent expression is a term which again represents a goal. However latent expressions are executed as the file is loaded. A latent expression is indicated using the prefix :-. The compiler translates :- body. to the special goal latent_exp :- body. The Prolog loader knows that as it encounters latent_exp :- .. clauses they are to be executed and not added to the logicbase.

For example consider:

This file is compiled. When it is loaded (either as part of an application or as a single Prolog Object file) it will print out three lines at the terminal:

while loading the two clauses. Then the listener will be entered.

Because latent expressions are run as they are encountered, if they refer to any predicates that are NOT built-in predicates, then those predicates must be defined before the latent expression is executed.

In this example, the greeting message will be displayed as the last action when the file is consulted or loaded. If the the latent-expression was put first in the file, it would fail, because greeting/0 would not be defined.

Two goals are both treated as compiler directives and as latent expressions. These are op/3 and set_prolog_flag/2. Both of these are executed by the compiler and the results used in compilation, as well as being included as latent expressions to be executed at runtime as well.

Copyright ©1987-2011 Amzi! inc. All Rights Reserved. Amzi! is a registered trademark and Logic Server is a trademark of Amzi! inc.