Introduction

This section describes how to load and call programs written in C, Pascal, FORTRAN, or Assembly language. This may be desirable in order to:

Examples showing the correct use of the foreign interface are found in the library directory. Examples of incrementally loading C, Pascal and FORTRAN code can be found in fli-p2f-fex.

Foreign functions are loaded directly into the Prolog system by using one of the built-in predicates load_foreign_executable/1 or load_foreign_files/2. These predicates load executable images or object files into the address space of the running Prolog.

Before calling these predicates, you must prepare facts in the database that describe which functions may be called by Prolog, the native language of each function, and the argument types of each function. This information is used to link Prolog predicates and foreign functions when loading the foreign code.

The foreign language interface supports the direct exchange of Prolog's atomic data types (atoms, integers and floating-point numbers). The data is automatically converted between Prolog's internal representation and the internal representation of the foreign language.

The foreign language interface also supports passing any Prolog term to C and receiving any Prolog term from C. A set of C functions is provided to type test and access terms passed to C and to create new Prolog terms in C. For information on these functions see cfu.

Prolog procedures that are attached to foreign functions are determinate, in that they succeed at most once for a given call and are not re-entered on backtracking. This imposes no serious limitation, since it is always possible to divide a foreign function into the part to be done on the first call and the part to be redone on backtracking. Backtracking can then take place at the Prolog level where it is naturally expressed.