- Documentation
- Reference manual
- Foreign Language Interface
- The Foreign Include File
- Argument Passing and Control
- Atoms and functors
- Input and output
- Analysing Terms via the Foreign Interface
- Constructing Terms
- Unifying data
- Convenient functions to generate Prolog exceptions
- Foreign language wrapper support functions
- Serializing and deserializing Prolog terms
- BLOBS: Using atoms to store arbitrary binary data
- Exchanging GMP numbers
- Calling Prolog from C
- Discarding Data
- String buffering
- Foreign Code and Modules
- Prolog exceptions in foreign code
- Catching Signals (Software Interrupts)
- Miscellaneous
- Errors and warnings
- Environment Control from Foreign Code
- Querying Prolog
- Registering Foreign Predicates
- Foreign Code Hooks
- Storing foreign data
- Embedding SWI-Prolog in other applications
- The Foreign Include File
- Foreign Language Interface
- Packages
- Reference manual
12.4.22 Registering Foreign Predicates
- bool PL_register_foreign_in_module(char *mod, char *name, int arity, foreign_t (*f)(), int flags, ...)
- Register the C function f to implement a Prolog predicate.
After this call returns successfully a predicate with name name
(a
char *) and arity arity (a Cint) is created in module mod. If mod isNULL, the predicate is created in the module of the calling context, or if no context is present in the moduleuser.When called in Prolog, Prolog will call function. flags form a bitwise or’ed list of options for the installation. These are:
PL_FA_METAProvide meta-predicate info (see below) PL_FA_TRANSPARENTPredicate is module transparent (deprecated) PL_FA_NONDETERMINISTICPredicate is non-deterministic. See also PL_retry(). PL_FA_NOTRACEPredicate cannot be seen in the tracer PL_FA_VARARGSUse alternative calling convention. If
PL_FA_METAis provided, PL_register_foreign_in_module() takes one extra argument. This argument is of typeconst char*. This string must be exactly as long as the number of arguments of the predicate and filled with characters from the set0-9:^-+?. See meta_predicate/1 for details.PL_FA_TRANSPARENTis implied if at least one meta-argument is provided (0-9:^). Note that meta-arguments are not always passed as <module>:<term>. Always use PL_strip_module() to extract the module and plain term from a meta-argument.236It is encouraged to pass an additionalNULLpointer for non-meta-predicates.Predicates may be registered either before or after PL_initialise(). When registered before initialisation the registration is recorded and executed after installing the system predicates and before loading the saved state.
Default calling (i.e. without
PL_FA_VARARGS) function is passed the same number ofterm_targuments as the arity of the predicate and, if the predicate is non-deterministic, an extra argument of typecontrol_t(see section 12.4.1.1). IfPL_FA_VARARGSis provided, function is called with three arguments. The first argument is aterm_thandle to the first argument. Further arguments can be reached by adding the offset (see also PL_new_term_refs()). The second argument is the arity, which defines the number of valid term references in the argument vector. The last argument is used for non-deterministic calls. It is currently undocumented and should be defined of typevoid*. Here is an example:static foreign_t atom_checksum(term_t a0, int arity, void* context) { char *s; if ( PL_get_atom_chars(a0, &s) ) { int sum; for(sum=0; *s; s++) sum += *s&0xff; return PL_unify_integer(a0+1, sum&0xff); } return FALSE; } install_t install() { PL_register_foreign("atom_checksum", 2, atom_checksum, PL_FA_VARARGS); } - bool PL_register_foreign(const char *name, int arity, foreign_t (*function)(), int flags, ...)
- Same as PL_register_foreign_in_module(),
passing
NULLfor the module. - void PL_register_extensions_in_module(const char *module, PL_extension *e)
- Register a series of predicates from an array of definitions of the type
PL_extensionin the given module. If module isNULL, the predicate is created in the module of the calling context, or if no context is present in the moduleuser. ThePL_extensiontype is defined astypedef struct PL_extension { char *predicate_name; /* Name of the predicate */ short arity; /* Arity of the predicate */ pl_function_t function; /* Implementing functions */ short flags; /* Or of PL_FA_... */ } PL_extension;For details, see PL_register_foreign_in_module(). Here is an example of its usage:
static PL_extension predicates[] = { { "foo", 1, pl_foo, 0 }, { "bar", 2, pl_bar, PL_FA_NONDETERMINISTIC }, { NULL, 0, NULL, 0 } }; main(int argc, char **argv) { PL_register_extensions_in_module("user", predicates); if ( !PL_initialise(argc, argv) ) PL_halt(1); ... } - void PL_register_extensions( PL_extension *e)
- Same as PL_register_extensions_in_module()
using
NULLfor the module argument.