Node:Calling Prolog from C, Next:, Previous:Support Functions, Up:Mixing C and Prolog

Calling Prolog from C

In development and runtime systems alike, Prolog and C code may call each other to arbitrary depths.

Before calling a predicate from C you must look up the predicate definition by module, name, and arity. The function SP_predicate() will return a pointer to this definition or return NULL if the predicate is not visible in the module. This definition could be used in more than one call to the same predicate. The module specification is optional. If NULL or "" (the empty string) is given, then the default type-in module (see Module Spec) is assumed:

SP_pred_ref SP_predicate(char *name_string,
                         long arity,
                         char *module_string)

Note that the first and third arguments point to encoded strings, representing the characters of the predicate and module name.

The function SP_pred() may be used as an alternative to the above. The only difference is that the name and module arguments are passed as Prolog atoms rather than strings, and the module argument is mandatory. This saves the cost of looking up the two arguments in the Prolog symbol table. This cost dominates the cost of SP_predicate():

SP_pred_ref SP_pred(SP_atom name_atom,
                    long arity,
                    SP_atom module_atom)