• Places
    • Home
    • Graphs
    • Prefixes
  • Admin
    • Users
    • Settings
    • Plugins
    • Statistics
  • 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

11 Foreign Language Interface
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
      • Introduction
      • Overview
      • Initialising and Managing a Prolog Project
      • Built-in Predicates
      • SWI-Prolog extensions
      • Modules
      • Constraint Logic Programming
      • CHR: Constraint Handling Rules
      • Multithreaded applications
      • Coroutining using Prolog engines
      • Foreign Language Interface
        • Overview of the Interface
        • Linking Foreign Modules
        • Interface Data Types
        • The Foreign Include File
        • Linking embedded applications using swipl-ld
        • The Prolog `home' directory
        • Example of Using the Foreign Interface
        • Notes on Using Foreign Code
      • Generating Runtime Applications
      • The SWI-Prolog library
      • Hackers corner
      • Compatibility with other Prolog dialects
      • Glossary of Terms
      • SWI-Prolog License Conditions and Tools
      • Summary
      • Bibliography
    • Packages

11 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


11.1 Overview of the Interface
11.2 Linking Foreign Modules
11.2.1 What linking is provided?
11.2.2 What kind of loading should I be using?
11.2.3 library(shlib): Utility library for loading foreign objects (DLLs, shared objects)
11.2.4 Low-level operations on shared libraries
11.2.5 Static Linking
11.3 Interface Data Types
11.3.1 Type term_t: a reference to a Prolog term
11.3.1.1 Interaction with the garbage collector and stack-shifter
11.3.2 Other foreign interface types
11.3.2.1 PL_ARITY_AS_SIZE
11.4 The Foreign Include File
11.4.1 Argument Passing and Control
11.4.1.1 Non-deterministic Foreign Predicates
11.4.2 Atoms and functors
11.4.2.1 Atoms and atom garbage collection
11.4.3 Analysing Terms via the Foreign Interface
11.4.3.1 Testing the type of a term
11.4.3.2 Reading data from a term
11.4.3.3 Exchanging text using length and string
11.4.3.4 Wide-character versions
11.4.3.5 Reading a list
11.4.3.6 An example: defining write/1 in C
11.4.4 Constructing Terms
11.4.5 Unifying data
11.4.6 Convenient functions to generate Prolog exceptions
11.4.7 BLOBS: Using atoms to store arbitrary binary data
11.4.7.1 Defining a BLOB type
11.4.7.2 Accessing blobs
11.4.8 Exchanging GMP numbers
11.4.9 Calling Prolog from C
11.4.9.1 Predicate references
11.4.9.2 Initiating a query from C
11.4.10 Discarding Data
11.4.11 Foreign Code and Modules
11.4.12 Prolog exceptions in foreign code
11.4.13 Catching Signals (Software Interrupts)
11.4.14 Miscellaneous
11.4.14.1 Term Comparison
11.4.14.2 Recorded database
11.4.14.3 Getting file names
11.4.14.4 Dealing with Prolog flags from C
11.4.15 Errors and warnings
11.4.16 Environment Control from Foreign Code
11.4.17 Querying Prolog
11.4.18 Registering Foreign Predicates
11.4.19 Foreign Code Hooks
11.4.20 Storing foreign data
11.4.20.1 Examples for storing foreign data
11.4.21 Embedding SWI-Prolog in other applications
11.4.21.1 Threading, Signals and embedded Prolog
11.5 Linking embedded applications using swipl-ld
11.5.1 A simple example
11.6 The Prolog `home' directory
11.7 Example of Using the Foreign Interface
11.8 Notes on Using Foreign Code
11.8.1 Foreign debugging functions
11.8.2 Memory Allocation
11.8.2.1 Boehm-GC support
11.8.3 Compatibility between Prolog versions
11.8.4 Debugging and profiling foreign code (valgrind)
11.8.5 Name Conflicts in C modules
11.8.6 Compatibility of the Foreign Interface
ClioPatria (version V3.1.1-21-gb8003bb)