Skip to main content

Janus library to call SWI-Prolog

Project description

Janus-swi: a bi-directional interface between SWI-Prolog and Python

This code implements a ready-to-use bi-directional interface to Python. As motivated by Theresa Swift, Python opens many doors for accessing resources such as graphics, machine learning and many more.

The API defined in this interface has been established as a PIP, Prolog Improvement Proposal. When the PIP is finished and published we will properly reference it. The main predicates and Python functions of this interface are compatible with the XSB Python package janus_xsb. Both janus_swi and janus_xsb implement extensions upon the agreed interface. For example, janus_swi supports SWI-Prolog dicts and defines thread synchronization between Prolog and Python.

Documentation

See SWI-Prolog manual

Bi-directional

This GIT repository is a GIT submodule of the SWI-Prolog source repository. As part of the SWI-Prolog source distribution it is used to build library(janus), a Prolog library that embeds Python. This same module can be used stand-alone to build the Python package janus_swi that embeds Prolog into Python. Loaded either way, Janus is the same and allows for mutually recursive calls between Prolog and Python.

Embedding Prolog into Python: the Python janus_swi package

If this repository is used to build the Python pip package janus_swi, we can load SWI-Prolog into Python and call predicates. For example:

python
>>> import janus_swi as janus
>>> janus.query_once("writeln('Hello world!')")
Hello world!
{'truth': True}
>>>

The Python package is available from PyPi as janus-swi. We currently provide a few wheels for Windows. The binaries in the Windows wheel probably supports all Python and Prolog versions that are also supported by the source. The package can be installed using pip from source on any system with CPython 3.6 or later, SWI-Prolog 9.1.12 or later and a C compiler. For compiling the C code, GCC, Clang and VS2022 have been tested. Thus, normally the package can be installed using

pip install janus-swi

SWI-Prolog is selected from the environment variable SWIPL or by finding swipl on the executable search path. On Windows, if neither of the above work, the Windows registry is examined to find the installed SWI-Prolog system (by default C:\Program Files\swipl\bin\swipl.exe).

If you installed SWI-Prolog from source, it is advised to install Janus from the packages/swipy directory in the Prolog source. The package can be installed from within this directory using

pip install .

Embedding Python into Prolog: library(janus)

Configuration and installation of library(janus) which embeds Python into Prolog is handled by the normal Prolog configuration. Building the interface requires the libraries and C headers for Python embedding to be installed. Below are the commands for installing the embedded Python engine for Ubuntu and Fedora Linux.

apt install python3 libpython3-dev   # Ubuntu
dnf install python3-devel            # Fedora

On MacOS, these files are included in the Homebrew and Macports versions of Python

On Windows, these files are included in the default installer. Configuration requires Python to appear in %PATH%.

After successful installation, running py_version/0 should result in printing relevant information on the embedded Python system.

?- py_version.
% Janus embeds Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0

Using Conda

Ongoing work to get SWI-Prolog working under Conda can be found at https://github.com/SWI-Prolog/swi-prolog-feedstock. Eventually, this work shall be merged with https://anaconda.org/conda-forge/swi-prolog

As is, https://github.com/SWI-Prolog/swi-prolog-feedstock has been used to build the full SWI-Prolog system with Janus interface on Linux, MacOS and Windows.

Alternatives

MQI (Machine Query Interface)

SWI-Prolog comes bundled with MQI. MQI is initiated from Python and starts SWI-Prolog as a server. It allows calling Prolog from Python. Separated using networking, this approach is easy to install and runs with any Python version. It does not allow calling Python from Prolog, the primary reason for the existence of this package. Using networking, the latency is relatively high.

pyswip

The pyswip interface uses the Python ctypes to embed Prolog into Python. Only relying on ctypes, the package is a fully portable Python package that supports a wide range of Python and Prolog versions.

Unlike this package, embedding Python into Prolog is not possible. pyswip calls Prolog, similarly than janus, using a string. However, where janus allows passing input to the goal as a dict that is transferred using the C API rather than strings, pyswip also passes the input as a string. This is slower, sensitive to injection attacks and complicated because the user is responsible for generating valid Prolog syntax. Calls from Prolog to Python are possible by defining a Prolog predicate from Python. This only seems to support deterministic predicates and it cannot pass data back to Prolog. Janus supports calling Python functions and methods directly and supports enumerating Python iterators and generators as non-deterministic goals using py_iter/2.

The overhead of Janus is roughly 5 times less than pyswip. As pyswip still sustains over 100K calls per second this is irrelevant to many applications.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

janus_swi-1.5.1.tar.gz (88.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

janus_swi-1.5.1-cp313-cp313-win_amd64.whl (75.4 kB view details)

Uploaded CPython 3.13Windows x86-64

File details

Details for the file janus_swi-1.5.1.tar.gz.

File metadata

  • Download URL: janus_swi-1.5.1.tar.gz
  • Upload date:
  • Size: 88.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.0

File hashes

Hashes for janus_swi-1.5.1.tar.gz
Algorithm Hash digest
SHA256 f975c73eb0b490e64db782597bf68bb235099da10e45f8e2822721f0a1eb6828
MD5 641a80432fd4926a9191ee71f556e68c
BLAKE2b-256 d72a6572343546ca3c82c940633b18b4baa8dfe602d75f9134884e07649b6ba5

See more details on using hashes here.

File details

Details for the file janus_swi-1.5.1-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: janus_swi-1.5.1-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 75.4 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.13.0

File hashes

Hashes for janus_swi-1.5.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 1eedfb7b6c4bc18657257db42a68446e7ca3d7c120002c568565b676989be018
MD5 c6e8bdd027310307524cc470199a3f30
BLAKE2b-256 5454ae11fbf87b73499d7f2087e72c197da436cde1ef608b1133153ff925ab7e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page