Sunday 17 July 2016

[C][Java][Prolog] Building XSB Prolog and InterProlog on Debian "jessie" 8.0 64-bit

This article is a guide to building XSB Prolog and InterProlog.
XSB extends Prolog with tabled resolution and HiLog (a standard extension of Prolog permitting limited higher-order logic programming).[1]
Tabled resolution is a very useful feature in logic programming. SWI-Prolog has its own implementation which seems to be less mature than XSB's[2]. SWI-Prolog's documentation of the feature states its two uses: memoization[3] and avoiding non-termination. Check http://www.swi-prolog.org/pldoc/man?section=tabling for more details.

All of the following operations are performed on a 64-bit Linux Debian "jessie" 8.0 machine.

(1) Begin by downloading XSB and InterProlog sources. Check these links for more information:
XSB - http://xsb.sourceforge.net/
InterProlog - http://interprolog.com/wiki/index.php?title=Java-Prolog_bridge#Download_and_install
For this article, I have used XSB 3.7 and InterProlog 2.5.x (labeled "95ff1c9, February 26 2015")

(2) Unpack the archives and go to XSB/build in a shell terminal.

(3) Edit configure file. Search for case $CC in, few lines further (at the end of gcc) case), replace this line:
CFLAGS="-Wall -pipe"
with this one:
CFLAGS="-Wall -pipe -fPIC"
In case autoconf is to be used, configure.in should be edited in the same spot as well.

(4) There is no extensive, easily reachable list of all the needed dependencies to build XSB. Since I had all the needed dependencies installed on my system (except for one), I won't make a list of them in this article. Perhaps, I will edit it in the future. I can't promise to do it, though. However, I will point out how to make sure that the key configuration items are present.

Run this command:
./configure --enable-mt

When that command is run, all the dependencies will be displayed along with their respective presence status (e.g. checking for socket... yes). At the end of the configuration process, check the INSTALLATION SUMMARY.
In INSTALLATION SUMMARY, Support for InterProlog's native engine must have the value yes, otherwise edit your JAVA_HOME environment variable (potentially JRE_HOME, as well) or root/sudo-run:
update-alternatives --config javac
and, potentially:
update-alternatives --config java
The other key configuration item is multi-threading. The option --enable-mt is there for that. You can check the value of XSB abstract machine in INSTALLATION SUMMARY, it should be multi-threading.
XSB will be built with displays the prefix compilation command. The option -fPIC must be be in that command, otherwise step (3) should be redone properly.

(5) Compile the default subsystems using this command:
./makexsb --config-tag=mt
The command is kind enough to specify on termination the full path to the script that sould be used to run XSB Prolog. Save that path for later.

(6) Compile the .so library using this command:
./makexsb --config-tag=mt dynmodule
The kindness is still abundant. Save the path to the .so file.

Unless configure file is edited as per step (3), the build will fail with an error message similar to this one:
/usr/bin/ld: biassert.o: relocation R_X86_64_32 against `a local symbol' can not be used when making a shared object; recompile with -fPICbiassert.o: could not read symbols: Bad value

(7) Information about install and cleaning of specific subsystems (or everything for that matter) is specified in INSTALL file under XSB and the manuals under XSB/docs/userman, and this article doesn't cover any of it.

(8) Go to interprolog directory, it should be where build.xml resides.

(9) In build.xml, edit these lines:
<property name="XSB_BIN_DIRECTORY" location="PATH_TO_YOUR_XSB_BIN_DIR" />
<property name="bin.dir" location="/Users/mc/tempCompiled" />
 
Remember that path that we saved in step (5), the directory path to the file should be put in "PATH_TO_YOUR_XSB_BIN_DIR". For the second property, put any path as long as it exists.
For instance:
<property name="XSB_BIN_DIRECTORY" location="/path/to/XSB/bin" /><property name="bin.dir" location="/path/to/interprolog/tempCompiled" />
Then, in this line:
<exec dir="${mybase.dir}" executable="${XSB_BIN_DIRECTORY}/xsb"
replace xsb in executable attribute with xsb-mt which should be the bare file name of the script generated in step (5).

(10) Edit interprolog/src/com/xsb/interprolog/NativeEngine.java:
else  libraryFile = new File(providedBinDirectory, "xsb"); 
to:
else  libraryFile = new File(providedBinDirectory, "libxsb.so");
(11) Time to test how InterProlog mixes with XSB!
For people who are as lazy as I am, just stay in the same directory (interprolog.jar should be there). vim, nano or any other editor Test.java, then paste the following code:

The argument of subprocessEngine is the whole path from step (5) and nativeEngine's is the directory path to libxsb.so from step (6).

Compile using:
javac -cp interprolog.jar:. Test.java
Run with:
java -cp interprolog.jar:. Test

Upon test, XSBSubprocessEngine works properly, however, NativeEngine crashes on initialization. NativeEngine runs as expected with the XSB build that does not support multi-threading, i.e. no --enable-mt specified.

---
External links:

[1] https://en.wikipedia.org/wiki/XSB
[2] http://www.swi-prolog.org/pldoc/man?section=tabling
[3] https://en.wikipedia.org/wiki/Memoization

No comments:

Post a Comment