I would like to mix Java and Prolog code on a number of occasions. Together with a student of mine (Joachim Pehl), we just convinced ourselves that the JPL library of SWI-Prolog is really cool. Basically JPL allows you to call Prolog from Java and Java from Prolog. But the question is, of course, how easy is it and can you easily go back and forth . For instance, can we call Prolog from Java and call back Java from Prolog and share Java state throughout? Sure it works! PROLOG PORTION FOLLOWS % This library is all we need to call Java from Prolog :- ensure_loaded(library(jpl)). main :- % Create an object jpl_new(class([],['Test']), [], X), % Printing objects is like printing object ids write(X),nl, % Access a field of the object; happens to be static jpl_get(X, state, Y), % Prints whatever the state's value is write(Y),nl. JAVA PORTION FOLLOWS // This jar is all we need to call Prolog from Java import jpl.*; public class Test { public static int state = 0; ...
Interesting that you've removed all Prolog. I seem to be hearing about a lot of use of Datalog in program analysis circles: any thoughts on that?
ReplyDeleteYes, Prolog and Datalog and DCG are great tools in metaprogramming. I just figured that I might get a bit more readers by more consistently relying on Haskell throughout. This nice side effect is that I now have many problems coded up both in Prolog and Haskell. Don't get anywhere interestingly deep into program analysis.
Delete