Making Prolog Procedures Callable by Foreign Functions

Any Prolog predicate can be made callable from foreign functions using the declaration extern/1. This includes built-in predicates, as well as predicates that are currently undefined and dynamic predicates that currently have no clauses.

An extern/1 declaration not only makes the predicate known to foreign languages, but also specifies how arguments will be passed to and from it. When a predicate is declared callable using extern/1 declaration, it becomes available to foreign functions as soon as the declaration is loaded. This is equally true of extern/1 declarations occurring in files that are loaded from source form, pre-compiled QOF files whose source files contained extern/1 declarations, and certain QOF files (e.g. those created using save_program/1) that retain callability information (see ref-sls-sst).

A Prolog predicate that has been made callable from foreign functions is not otherwise changed in any way. It can still be abolished or redefined just like any other predicate. There is no performance penalty associated with making a predicate callable from foreign functions. A predicate can be redeclared by loading a new or modified extern/1 declaration.

When a predicate is made callable from foreign code, a new and closely related Prolog predicate called an interface predicate is created in the module in which extern/1 was declared. The interface predicate has the same arity as the callable predicate, and its name is the name of the declared predicate with an underscore prepended to it. The interface predicate provides the link between foreign languages and its Prolog predicate. It can be abolished or saved just as any other predicate, but because they can only be created using an extern/1 declaration, valid interface predicates cannot be made dynamic or multifile.

The purpose of the interface predicate is to supply an entry point into Prolog for foreign functions, and a handle on the property of callability of Prolog procedures for manipulation in Prolog. It is possible to call an interface predicate from Prolog, but the call will simply fail.