12 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


12.1 Overview of the Interface
12.2 Linking Foreign Modules
12.2.1 What linking is provided?
12.2.2 What kind of loading should I be using?
12.2.3 library(shlib): Utility library for loading foreign objects (DLLs, shared objects)
12.2.4 Low-level operations on shared libraries
12.2.5 Static Linking
12.3 Interface Data Types
12.3.1 Type term_t: a reference to a Prolog term
12.3.1.1 Interaction with the garbage collector and stack-shifter
12.3.2 Other foreign interface types
12.3.2.1 PL_ARITY_AS_SIZE
12.4 The Foreign Include File
12.4.1 Argument Passing and Control
12.4.1.1 Non-deterministic Foreign Predicates
12.4.1.2 Yielding from foreign predicates
12.4.2 Atoms and functors
12.4.2.1 Atoms and atom garbage collection
12.4.3 Analysing Terms via the Foreign Interface
12.4.3.1 Testing the type of a term
12.4.3.2 Reading data from a term
12.4.3.3 Exchanging text using length and string
12.4.3.4 Wide-character versions
12.4.3.5 Reading a list
12.4.3.6 Processing option lists and dicts
12.4.3.7 An example: defining write/1 in C
12.4.4 Constructing Terms
12.4.5 Unifying data
12.4.6 Convenient functions to generate Prolog exceptions
12.4.7 Serializing and deserializing Prolog terms
12.4.8 BLOBS: Using atoms to store arbitrary binary data
12.4.8.1 Defining a BLOB type
12.4.8.2 Accessing blobs
12.4.9 Exchanging GMP numbers
12.4.10 Calling Prolog from C
12.4.10.1 Predicate references
12.4.10.2 Initiating a query from C
12.4.11 Discarding Data
12.4.12 String buffering
12.4.13 Foreign Code and Modules
12.4.14 Prolog exceptions in foreign code
12.4.15 Catching Signals (Software Interrupts)
12.4.16 Miscellaneous
12.4.16.1 Term Comparison
12.4.16.2 Recorded database
12.4.16.3 Database
12.4.16.4 Getting file names
12.4.16.5 Dealing with Prolog flags from C
12.4.17 Errors and warnings
12.4.18 Environment Control from Foreign Code
12.4.19 Querying Prolog
12.4.20 Registering Foreign Predicates
12.4.21 Foreign Code Hooks
12.4.22 Storing foreign data
12.4.22.1 Examples for storing foreign data
12.4.23 Embedding SWI-Prolog in other applications
12.4.23.1 Threading, Signals and embedded Prolog
12.5 Linking embedded applications using swipl-ld
12.5.1 A simple example
12.6 The Prolog‘home' directory
12.7 Example of Using the Foreign Interface
12.8 Notes on Using Foreign Code
12.8.1 Foreign debugging functions
12.8.2 Memory Allocation
12.8.3 Compatibility between Prolog versions
12.8.4 Foreign hash tables
12.8.5 Debugging and profiling foreign code (valgrind, asan)
12.8.6 Name Conflicts in C modules
12.8.7 Compatibility of the Foreign Interface
12.9 Foreign access to Prolog IO streams
12.9.1 Get IO stream handles
12.9.2 Creating an IO stream
12.9.3 Interacting with foreign streams
12.9.4 Foreign stream error handling
12.9.5 Foreign stream encoding
12.9.6 Foreign stream line endings
12.9.7 Foreign stream position information