Theresa Swift from XSB has convinced me that a bundled Python interface has a lot of value. She presented a paper on Janus, the XSB Python interface. The main goal is to access Python transparently and hassle-free from Prolog and thus allow transparent access to the wealth of Python interfaces. Porting this work is an obvious choice. However, the foreign API differs and, SWI-Prolog provides additional datatypes, notably dicts to make the binding much more intuitive. Further more, I recently created the JavaScript interface and that gave some new insights.
If you check out the latest git source, run gity submodule update --init
and build it, it should add the package swipy
(GitHub) and if Python is installed with the development files, all should configure nicely. After building, you can run Python interactively under Prolog using
?- py_shell.
You can also call e.g. ?- py_call(print("Hello world\n"), _).
See examples in packages/swipy and the very short docs built in builtdir/packages/swipy/janus.html
There are a lot of issues and questions. It is, at least on some platforms, already useful as is though
- Naming and compatibility to the XSB version. Having a shared Python interface would be nice, probably next to the more natural SWI-Prolog interface.
- (How) can we support big integers and rational numbers?
- Code is probably buggy at places. I’m still struggling with the Py_DECREF(), etc.
- Barely any comment in the code yet.
- I wonder whether calling Prolog from Python is what we want. Currently it provides a module
janus
with some functions and a classProlog
that provides an iterator over solutions. - Currently it binds to the Python discovered by CMake. Probably you want to be able to select a Python.
- Eventually there should also be a
pip install swipy
. Little clue what should be in there. - Surely much more.
So, Python programmers, please comment and, if possible, help.