- Documentation
- Reference manual
- Packages
- SWI-Prolog Python interface
- Introduction
- Data conversion
- Janus by example
- library(janus): Call Python from Prolog
- Calling Prolog from Python
- Janus and threads
- Janus as a Python package
- Prolog and Python
- Janus performance evaluation
- Python or C/C++ for accessing resources?
- Janus platforms notes
- Compatibility to the XSB Janus implementation
- Status of Janus
- SWI-Prolog Python interface
12 Compatibility to the XSB Janus implementation
We aim to provide an interface that is close enough to allow developing Prolog code that uses Python and visa versa. Differences between the two Prolog implementation make this non-trivial. SWI-Prolog has native support for dicts, strings, unbounded integers and blobs that provide safe pointers to external objects that are subject to (atom) garbage collection.
We try to find a compromise to make the data conversion as close as
possible while supporting both systems as good as possible. For this
reason we support creating a Python dict both from a SWI-Prolog dict and
from the Prolog term py({k1:v1, k2:v2, ...}). With
py defined as a prefix operator, this may be written
without parenthesis and is thus equivalent to the SWI-Prolog dict
syntax. The library(janus) library provides access
predicates that are supported by both systems and where the SWI-Prolog
version supports both SWI-Prolog dicts and the above Prolog
representation. See
items/2, values/3, key/2
and items/2.
Both implementations will provide a low-level and more high level interface. The high level interface is realized by py_call/[2,3] and py_iter/[2,3] from Prolog and janus.once() and janus.Query() from Python. We realize the low level interfaces py_func/[3,4] and py_dot/[4,5] on top of py_call/2 and the Python functions px_cmd(), px_qdet() and px_comp() on top of janus.once(). Emulation of the Prolog predicates is shallow and has little impact on performance. Emulation of the Python functions on top of janus.once() is more expensive. Future versions of the SWI-Prolog implementation may opt for a more low-level implementation.
We are discussing to minimize the differences. The current implementation reflects the almost complete agreement calling Python from Prolog. Discussing calling Prolog from Python is work in progress.
12.1 Writing portable Janus modules
This section will be written after the dust has settled. Topics
- Dealing with Python dicts
- Dealing with Prolog modules
- Dealing with Prolog references to Python objects
- More?