Package: janus-swi
Janus-swi: a bi-directional interface between SWI-Prolog and PythonThis  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 packagejanus_xsb.   Bothjanus_swiandjanus_xsbimplement extensions
upon  the   agreed  interface.   For  example,janus_swisupports
SWI-Prolog dicts and defines thread synchronization between Prolog and
Python.Bi-directionalThis  GIT repository  is a  GITsubmoduleof the  SWI-Prolog source
repository.  As part of the  SWI-Prolog source distribution it is used
to buildlibrary(janus), a Prolog  library that embeds Python.  This
same  module can  be  used  stand-alone to  build  the Python  packagejanus_swithat 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 packageIf  this  repository   is  used  to  build  the   Python  pip  packagejanus_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    fromPyPiasjanus-swi.      We    currently
provide  a fewwheelsfor  Windows.   The binaries  in the  Windowswheelprobably supports all Python and Prolog versions that are also
supported by  the source.   The package can  be installed  usingpipfrom 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
usingpip install janus-swiSWI-Prolog is  selected by  findingswiplon the  executable search
path.   Ifswipl.exeis not  in%PATH%on Windows  the Windows
registry is examined to find SWI-Prolog.If  you installed  SWI-Prolog from  source, it  is advices  to install
Janus from thepackages/swipydirectory in the  Prolog source.  The
package can be installed from within this directory usingpip install .Embedding Python into Prolog: library(janus)Configuration and installation oflibrary(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.  OnDebianbased Linux systems, this is
achieved usingapt install python3 libpython3-devIf  you need  to  build  Python, the  following  command is  suggested
(assuming you wish to install  it in$HOME/.local/bin). You may also
need the option--enable-shared.CFLAGS='-fPIC' CCSHARED='-fPIC' ./configure --prefix=$HOME/.local --enable-optimizations
make -j8   # change "8" to the number of CPUs on your machine
make installOnMacOS,  these files are  included in the Homebrew  and Macports
versions of PythonOn  Windows,  these  files  are included  in  the  default  installer.
Configuration requires Python to appear in%PATH%.After successful installation, runningpy_version/0should 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.0Using CondaOngoing work  to get SWI-Prolog  working under  Conda can be  found athttps://github.com/SWI-Prolog/swi-prolog-feedstock.   Eventually, this
work shall be merged withhttps://anaconda.org/conda-forge/swi-prologAs  is,https://github.com/SWI-Prolog/swi-prolog-feedstockhas  been
used  to build  the full  SWI-Prolog  system with  Janus interface  on
Linux, MacOS and Windows.DocumentationSeeSWI-Prolog manualAlternativesMQI (Machine Query Interface)SWI-Prolog               comes              bundled               withMQI.  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.pyswipThepyswipinterface  uses   thePython    ctypesto
embed Prolog into Python.  Only relying  onctypes, 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.pyswipcalls Prolog, similarly than janus, using a string.  However,
where  janus allows  passing input  to the  goal as  adictthat is
transferred using the C API  rather than strings,pyswipalso passes
the  input as  a  string.   This is  slower,  sensitive toinjection
attacksand   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  Pythoniteratorsandgeneratorsas
non-deterministic goals using py_iter/2.The  overhead of  Janus is  roughly 5  times less  thanpyswip.   Aspyswipstill sustains over 100K  calls per second this is irrelevant
to many applications.