Passing Data to and from Prolog

The foreign function interface automatically converts between C's representation of data and the representation of atomic data types expected by Prolog. Thus the calling function does not need to know how Prolog represents atoms, integers, floats or addresses in order to communicate with Prolog. This feature simplifies the integration of Prolog with foreign code; in particular, it makes it easier to interface directly with already-written functions in libraries and other programs. It also allows for compatibility with later versions of Quintus Prolog and with versions of Quintus Prolog running on other hardware.

Asymmetry note: When C calls Prolog, in contrast to Prolog calling foreign code, there is no Prolog datum passed as the function return value. Instead, the return value supplies the calling function with information as to whether the Prolog call succeeded or failed, or whether there was an exception raised.

Arguments are passed from C functions to Prolog predicates in the same order as they appear in the Prolog call. Prolog assumes that C functions will call Prolog predicates with the number and type of arguments as declared by the extern/1 declarations; if it does not, the results are unpredictable. Certain types of inputs (for example, atoms) can be checked for validity when the query to Prolog is made, and an error value is returned if the type is incorrect. Outputs are passed to Prolog as pointers to storage for results. Prolog will internally create unbound variables with which to calculate the results. The outputs will then be automatically converted and written into the C storage according to the calling specification. If the result Prolog computes is inconsistent with the specified output type, an exception is signaled.

Asymmetry note: When Prolog calls foreign code, outputs are unified with items supplied by the calling function; with C calling Prolog, assignment is used instead.