10 Foreign Language Interface

SWI-Prolog offers a powerful interface to C Kernighan & Ritchie, 1978. The main design objectives of the foreign language interface are flexibility and performance. A foreign predicate is a C function that has the same number of arguments as the predicate represented. C functions are provided to analyse the passed terms, convert them to basic C types as well as to instantiate arguments using unification. Non-deterministic foreign predicates are supported, providing the foreign function with a handle to control backtracking.

C can call Prolog predicates, providing both a query interface and an interface to extract multiple solutions from a non-deterministic Prolog predicate. There is no limit to the nesting of Prolog calling C, calling Prolog, etc. It is also possible to write the `main' in C and use Prolog as an embedded logical engine.


Section Index


10.1 Overview of the Interface
10.2 Linking Foreign Modules
10.2.1 What linking is provided?
10.2.2 What kind of loading should I be using?
10.2.3 library(shlib): Utility library for loading foreign objects (DLLs, shared objects)
10.2.4 Low-level operations on shared libraries
10.2.5 Static Linking
10.3 Interface Data Types
10.3.1 Type term_t: a reference to a Prolog term
10.3.1.1 Interaction with the garbage collector and stack-shifter
10.3.2 Other foreign interface types
10.4 The Foreign Include File
10.4.1 Argument Passing and Control
10.4.1.1 Non-deterministic Foreign Predicates
10.4.2 Atoms and functors
10.4.2.1 Atoms and atom garbage collection
10.4.3 Analysing Terms via the Foreign Interface
10.4.3.1 Testing the type of a term
10.4.3.2 Reading data from a term
10.4.3.3 Exchanging text using length and string
10.4.3.4 Wide-character versions
10.4.3.5 Reading a list
10.4.3.6 An example: defining write/1 in C
10.4.4 Constructing Terms
10.4.5 Unifying data
10.4.6 Convenient functions to generate Prolog exceptions
10.4.7 BLOBS: Using atoms to store arbitrary binary data
10.4.7.1 Defining a BLOB type
10.4.7.2 Accessing blobs
10.4.8 Exchanging GMP numbers
10.4.9 Calling Prolog from C
10.4.9.1 Predicate references
10.4.9.2 Initiating a query from C
10.4.10 Discarding Data
10.4.11 Foreign Code and Modules
10.4.12 Prolog exceptions in foreign code
10.4.13 Catching Signals (Software Interrupts)
10.4.14 Miscellaneous
10.4.14.1 Term Comparison
10.4.14.2 Recorded database
10.4.14.3 Getting file names
10.4.14.4 Dealing with Prolog flags from C
10.4.15 Errors and warnings
10.4.16 Environment Control from Foreign Code
10.4.17 Querying Prolog
10.4.18 Registering Foreign Predicates
10.4.19 Foreign Code Hooks
10.4.20 Storing foreign data
10.4.20.1 Examples for storing foreign data
10.4.21 Embedding SWI-Prolog in other applications
10.4.21.1 Threading, Signals and embedded Prolog
10.5 Linking embedded applications using swipl-ld
10.5.1 A simple example
10.6 The Prolog `home' directory
10.7 Example of Using the Foreign Interface
10.8 Notes on Using Foreign Code
10.8.1 Memory Allocation
10.8.1.1 Boehm-GC support
10.8.2 Compatibility between Prolog versions
10.8.3 Debugging and profiling foreign code (valgrind)
10.8.4 Name Conflicts in C modules
10.8.5 Compatibility of the Foreign Interface