Installing swi-prolog with JPL and plUnit support

  • strict warning: Non-static method view::load() should not be called statically in /home/codecomp/public_html/codecompiling.net/mai.www/sites/all/modules/views/views.module on line 906.
  • strict warning: Declaration of views_handler_filter::options_validate() should be compatible with views_handler::options_validate($form, &$form_state) in /home/codecomp/public_html/codecompiling.net/mai.www/sites/all/modules/views/handlers/views_handler_filter.inc on line 607.
  • strict warning: Declaration of views_handler_filter::options_submit() should be compatible with views_handler::options_submit($form, &$form_state) in /home/codecomp/public_html/codecompiling.net/mai.www/sites/all/modules/views/handlers/views_handler_filter.inc on line 607.
  • strict warning: Declaration of views_handler_filter_boolean_operator::value_validate() should be compatible with views_handler_filter::value_validate($form, &$form_state) in /home/codecomp/public_html/codecompiling.net/mai.www/sites/all/modules/views/handlers/views_handler_filter_boolean_operator.inc on line 159.
  • strict warning: Declaration of views_plugin_style_default::options() should be compatible with views_object::options() in /home/codecomp/public_html/codecompiling.net/mai.www/sites/all/modules/views/plugins/views_plugin_style_default.inc on line 24.
  • strict warning: Declaration of views_plugin_row::options_validate() should be compatible with views_plugin::options_validate(&$form, &$form_state) in /home/codecomp/public_html/codecompiling.net/mai.www/sites/all/modules/views/plugins/views_plugin_row.inc on line 134.
  • strict warning: Declaration of views_plugin_row::options_submit() should be compatible with views_plugin::options_submit(&$form, &$form_state) in /home/codecomp/public_html/codecompiling.net/mai.www/sites/all/modules/views/plugins/views_plugin_row.inc on line 134.

DISCARD

I'm currently playing with Prolog, plUnit and JPL (Java Prolog binding) for a postgraduate course I'm taking. I had some trouble putting all together and making it work, so I decided to write this small tutorial to remember how to do it again later and to share it just in case anyone else is also having the same problems.

First, compile swi-prolog. Ubuntu swi-prolog package does not come with plUnit included so I had to compile it from the sources. I grabbed the latest (6.2.6) tarball from:

http://www.swi-prolog.org/download/stable

Untar the sources (I did it on /usr/local/src) and change to the generated directory:

[dmi@cyrano]$ cd /usr/local/src/pl-6.2.6

Configure it, and don't forget to enable Prolog kernel as shared library:

[dmi@cyrano]$ ./configure --prefix=/usr/local/pl-6.2.6 --enable-shared

If you forget to turn on --enable-shared you will get unsatisfied links errors like this: "java: symbol lookup error: libjpl.so: undefined symbol: PL_is_initialised" when trying to call some prolog code from Java. This site helped to identify and solve the problem.

Build Prolog:

[dmi@cyrano]$ make
...
[dmi@cyrano]$ make install
...

Build extra packages. This step will actually add JPL and plUnit to the base prolog installation generated by the previous make install. To build packages:

[dmi@cyrano]$ cd packages/
[dmi@cyrano]$ ./configure
...
[dmi@cyrano]$ make
...
[dmi@cyrano]$ make install
...

Check /usr/local/pl-6.2.6/lib/swipl-6.2.6/lib/i686-linux , you should have a file named libswipl.so.6.2.6 and link named libswipl.so pointing to it. Also check /usr/local/pl-6.2.6/lib/swipl-6.2.6/lib/ , you should have jar named jpl.jar. If those files are in place then JPL is OK.

Then check plUnit, go to /usr/local/pl-6.2.6/lib/swipl-6.2.6/library and you should see a file named plunit.pl.

Now lets give the Prolog/JPL install a test. Given the following prolog program

foo(a).
foo(b).

...and the following Java code:

import jpl.Query;
 
public class MainJPL {
 
  public static void main(String[] args) {
    Query q;
    q = new Query("consult('foo.pl')");
    System.err.println(q.hasSolution());
    q = new Query("foo(a)");
    System.err.println(q.hasSolution());
    q = new Query("foo(b)");
    System.err.println(q.hasSolution());
    q = new Query("foo(c)");
    System.err.println(q.hasSolution());
    q = new Query("foo(X)");
    System.err.println(q.hasSolution());
 
    while (q.hasMoreElements()) {
      System.err.println(q.nextElement());
    }
  }
}

If you run the program this way:

[dmi@cyrano]$ java -cp .:/usr/local/pl-6.2.6/lib/swipl-6.2.6/lib/jpl.jar
-Djava.library.path=/usr/local/pl-6.2.6/lib/swipl-6.2.6/lib/i686-linux MainJPL

You should get the following output:

% foo.pl compiled 0.00 sec, 3 clauses
true
true
true
false
true
{X=a}
{X=b}

You can also export LD_LIBRARY_PATH=/usr/local/pl-6.2.6/lib/swipl-6.2.6/lib/i686-linux instead of using -Djava.library.path and it should also work. If you don't do this you will get an exception like this:

Exception in thread "main" java.lang.UnsatisfiedLinkError: no jpl in java.library.path
        at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1738)
        at java.lang.Runtime.loadLibrary0(Runtime.java:823)
        at java.lang.System.loadLibrary(System.java:1028)
        at jpl.JPL.loadNativeLibrary(JPL.java:100)
        at jpl.fli.Prolog.<clinit>(Prolog.java:85)
        at jpl.Query.open(Query.java:286)
        at jpl.Util.textToTerm(Util.java:162)
        at jpl.Query.<init>(Query.java:198)
        at MainJPL.main(MainJPL.java:9)

Now, to test plUnit, you can use the following code:

:- use_module(library(plunit)).
 
foo(a).
foo(b).
 
:- begin_tests(some_test_suite).
test(some_test_case) :- foo(a).
:-   end_tests(some_test_suite).

And if you load and run it from the swipl console you will get:

[dmi@cyrano]$ /usr/local/pl-6.2.6/bin/swipl
Welcome to SWI-Prolog (Multi-threaded, 32 bits, Version 6.2.6)
Copyright (c) 1990-2012 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit <a href="http://www.swi-prolog.org" title="http://www.swi-prolog.org">http://www.swi-prolog.org</a> for details.
 
For help, use ?- help(Topic). or ?- apropos(Word).
 
?- ['faa.pl'].
%   library(option) compiled into swi_option 0.00 sec, 32 clauses
%   library(pairs) compiled into pairs 0.00 sec, 22 clauses
%  library(plunit) compiled into plunit 0.03 sec, 371 clauses
% faa.pl compiled 0.03 sec, 380 clauses
true.
 
?- run_tests(some_test_suite).
% PL-Unit: some_test_suite . done
% All 1 tests passed
true.

That's all, hope it helps :)

PS: Just remembered that this site: http://onek.posterous.com/how-to-build-plunit was also useful.

Edit: If you are having errors like:

[FATAL ERROR: Not a SWI-Prolog saved state]

The problem can be a conflict between the apt installed swi-prolog and the compiled one. I removed the apt (ubuntu) installed swi-prolog and all worked well again.

Citas...

It is impossible to separate a cube into two cubes, or a fourth power into two fourth powers, or in general, any power higher than the second, into two like powers. I have discovered a truly marvelous proof of this, which this margin is too narrow to contain.

Fermat's marginalia related to his Last Theorem

Cursos