# Adding an evaluable predicate to the interpreter

Arity/Prolog32 includes a large number of evaluable predicates that perform a variety of functions. An evaluable predicate is one which is built into the interpreter. You can also write predicates yourself for performing functions other than those provided by the built-in predicates. You can add these predicates to your interpreter as evaluable predicates, thereby taking advantage of the convenience and increased speed provided by evaluable predicates. Evaluable predicates are available whenever you use the interpreter – you do not have to consult them into the database. And because evaluable predicates are compiled, they run much faster than predicates that reside in the database.

You need to have the Arity/Prolog32 Compiler in order to add Prolog predicates to the Arity/Prolog32 Interpreter. This section describes how to add Prolog predicates to the interpreter using the compiler. You can also add predicates written in assembly, C, Pascal, and Fortran.

If you only have the Arity/Prolog32 Interpreter, you can add predicates to your interpreter if they are written in assembly, C, Pascal, or Fortran. In this case, you need to use the declaration compiler to make the predicate "visible" to interpreted code. Refer to section 4.11 for information on the declaration compiler. Refer to chapter nnn for information on writing embedded C expressions.

The procedures for adding Prolog evaluable predicates to the interpreter are as follows:

1. You write the predicate in Prolog. The predicate can also be written in C, assembly, Pascal or Fortran, but this section describes the procedures for adding evaluable predicates written in Prolog.
2. You provide the appropriate visible declaration for the predicate.
3. You use the apc command to compile the file containing the predicate. The compilation of an evaluable predicate always involves the interpreter database (api.idb).
4. You link all the object modules for the predicate with the interpreter and Arity/Prolog32 libraries. This linking procedure produces a new version of the interpreter incorporating your evaluable predicate.
5. The next time you start the interpreter, your predicate will be incorporated as an evaluable predicate.

Figure 4.1 illustrates these procedures.

IMPORT C:\DOCS\NEW\PIC15.TIF

Figure 4.1: Adding Prolog evaluable predicates

Note that when you add evaluable predicates to the interpreter, you can modify your working version of the interpreter, or you can create a new, separate version of the interpreter. If you want to modify the working version of your interpreter, you use the files supplied with Arity/Prolog32. If you want to create a new, separate version of the interpreter, you copy the necessary interpreter files to a different directory, and then use those files to create the new interpreter. The following files are necessary for performing the procedures for adding evaluable predicates to the interpreter:

• API32.idb
• API32.obj
• arity32.lib
• api32.lib

To illustrate how a predicate is incorporated as an evaluable predicate, you will add the append predicate to your working interpreter. The append predicate appends a list onto the end of another list, thereby creating a single list. The predicate looks like this:

append([],L,L).
append([H|T],L,[H|R]) :- append(T,L,R).

For now, it is not necessary to understand how the predicate works. To use the predicate, you supply two lists and a variable, such as the following:

append([the,boy],[walked,the,dog],X).

The predicate returns the appended lists:

X = [the,boy,walked,the,dog]

This predicate is supplied in the file append.ari, which is included in the Arity/Prolog32 distribution kit. You should have this file in the same directory that contains your other Arity/Prolog32 files. Change to this directory.

Since the predicate is already written, the first thing you need to do is supply the appropriate visible declaration. Start the Arity/Prolog32 Interpreter by typing api and choose the Open... option from the File menu. Choose the file append.ari from the list box and press the Enter key to load the file into the editor.

Each evaluable predicate must include a visible declaration. An evaluable predicate must be made visible so that other clauses in the interpreter database can call the evaluable predicate. The visible declaration makes the evaluable predicate "visible" to clauses in the interpreter database.

The visible declaration must appear before the definition of the evaluable predicate. The format for the visible declaration is:

:- visible name/arity.

The name is the name of the evaluable predicate and the arity is the number of arguments to the evaluable predicate. Add the following visible declaration as the first line in the append.ari file:

:- visible append/3.

Remember to include the period. Then save the file and exit the editor and Arity/Prolog32.

Next, you need to compile the append.ari file. Type the following command at the MS-DOS or OS/2 prompt:

C> apc append -Fdapi

This command will modify the api32.idb database. It also creates the object module append.obj. After compiling the file, you need to link the new interpreter. In addition to the object module append.obj, an evaluable predicate must be linked with the object module api32.obj. The libraries that must be used with linking an evaluable predicate are arity32.lib and api32.lib. Using the Microsoft linker, the link command is as follows:

C> link
Object modules [.OBJ]: api+append
Run file [CODE.EXE]: api
List file [NUL.MAP]: api/map
Libraries: api+arity;

When linking has completed, start the new interpreter by typing:

C> api

To see that the append predicate has been incorporated as an evaluable predicate, type the following:

?- append([apple,orange,pear],[grape,lemon],X).