• Places
    • Home
    • Graphs
    • Prefixes
  • Admin
    • Users
    • Settings
    • Plugins
    • Statistics
  • CPACK
    • Home
    • List packs
    • Submit pack
  • Repository
    • Load local file
    • Load from HTTP
    • Load from library
    • Remove triples
    • Clear repository
  • Query
    • YASGUI SPARQL Editor
    • Simple Form
    • SWISH Prolog shell
  • Help
    • Documentation
    • Tutorial
    • Roadmap
    • HTTP Services
  • Login

12.4 The Foreign Include File
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
      • Foreign Language Interface
        • The Foreign Include File
          • Argument Passing and Control
          • Atoms and functors
          • Analysing Terms via the Foreign Interface
          • Constructing Terms
          • Unifying data
          • Convenient functions to generate Prolog exceptions
          • 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
            • Examples for storing foreign data
          • Embedding SWI-Prolog in other applications
    • Packages
ity. Defined keys are:
PL_VERSION_SYSTEM
SWI-Prolog version as 10,000 × major + 100 × minor + patch.
PL_VERSION_FLI
Incremented if the foreign interface defined in this chapter changes in a way that breaks backward compatibility.
PL_VERSION_REC
Incremented if the binary representation of terms as used by PL_record_external() and fast_write/2 changes.
PL_VERSION_QLF
Incremented if the QLF file format changes.
PL_VERSION_QLF_LOAD
Represents the oldest loadable QLF file format version.
PL_VERSION_VM
A hash that represents the VM instructions and their arguments.
PL_VERSION_BUILT_IN
A hash that represents the names, arities and properties of all built-in predicates defined in C. If this function is called before PL_initialise() it returns 0.

12.4.20 Querying Prolog

long PL_query(int)
Obtain status information on the Prolog system. The actual argument type depends on the information required. int describes what information is wanted.223Returning pointers and integers as a long is bad style. The signature of this function should be changed. The options are given in table 9.
PL_QUERY_ARGC Return an integer holding the number of arguments given to Prolog from Unix.
PL_QUERY_ARGV Return a char ** holding the argument vector given to Prolog from Unix.
PL_QUERY_SYMBOLFILE Return a char * holding the current symbol file of the running process.
PL_MAX_INTEGER Return a long, representing the maximal integer value represented by a Prolog integer.
PL_MIN_INTEGER Return a long, representing the minimal integer value.
PL_QUERY_VERSION Return a long, representing the version as 10,000 × M + 100 × m + p, where M is the major, m the minor version number and p the patch level. For example, 20717 means 2.7.17.
PL_QUERY_ENCODING Return the default stream encoding of Prolog (of type IOENC).
PL_QUERY_USER_CPU Get amount of user CPU time of the process in milliseconds.
Table 9 : PL_query() options

12.4.21 Registering Foreign Predicates

int 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 C int) is created in module mod. If mod is NULL, the predicate is created in the module of the calling context, or if no context is present in the module user.

When called in Prolog, Prolog will call function. flags form a bitwise or’ed list of options for the installation. These are:

PL_FA_META Provide meta-predicate info (see below)
PL_FA_TRANSPARENT Predicate is module transparent (deprecated)
PL_FA_NONDETERMINISTIC Predicate is non-deterministic. See also PL_retry().
PL_FA_NOTRACE Predicate cannot be seen in the tracer
PL_FA_VARARGS Use alternative calling convention.

If PL_FA_META is provided, PL_register_foreign_in_module() takes one extra argument. This argument is of type const char*. This string must be exactly as long as the number of arguments of the predicate and filled with characters from the set 0-9:^-+?. See meta_predicate/1 for details. PL_FA_TRANSPARENT is 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.224It is encouraged to pass an additional NULL pointer 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 of term_t arguments as the arity of the predicate and, if the predicate is non-deterministic, an extra argument of type control_t (see section 12.4.1.1). If PL_FA_VARARGS is provided, function is called with three arguments. The first argument is a term_t handle 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 type void*. 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);
}
int PL_register_foreign(const char *name, int arity, foreign_t (*function)(), int fl

ClioPatria (version V3.1.1-42-gd6a756b-DIRTY)