"I have a mind like a steel... uh... thingy." Patrick Logan's weblog.

Search This Blog

Tuesday, December 06, 2011

Prolog in Java

There are a number of implementations of Prolog in Java. Most of them are interpreters, and a number of those implement a complete Prolog system. Most implementations are a bit dated.

Wanting to get a bit more speed but remain in the JVM, I took a look at Prolog Cafe, which compiles Prolog to Java source. Even better, the source is as recent as 2009.

Then I looked around to see who might be using Prolog Cafe, and how recently. Even better news: The Gerrit code review system uses Prolog Cafe for rule checking. The team has been actively supporting a fork off the last original release of 1.2.5.

I just began trying Prolog Cafe (the new fork) this morning. The most significant difference so far is the Java package namespace. Below are a few notes illustrating the system, based off the original documentation, using the new namespace, etc.

  1. Install SWI Prolog for bootstrapping.
  2. cd to the prolog-cafe source directory.
  3. make

The package namespace has changed from
jp.ac.kobe_u.cs.prolog.*
to: com.googlecode.prolog_cafe.*.

Prolog Cafe has an interpreter. Try it:


java -cp $PLCAFEDIR/plcafe.jar com.googlecode.prolog_cafe.lang.PrologMain com.googlecode.prolog_cafe.builtin:cafeteria
Prolog Cafe 1.2.5 (mantis)
Copyright(C) 1997-2009 M.Banbara and N.Tamura
| ?- [queens].
{consulting /home/patrick/dev/prolog-cafe-ex/queens.pl ...}
{/home/patrick/dev/prolog-cafe-ex/queens.pl consulted 350 msec}
| ?- main.
[4,2,7,3,6,8,5,1]
[5,2,4,7,3,8,6,1]
[3,5,2,8,6,4,7,1]
[3,6,4,2,8,5,7,1]
...

Compile Prolog to Java:


java -cp $PLCAFEDIR/plcafe.jar com.googlecode.prolog_cafe.compiler.Compiler ../queens.pl

Compile the resulting Java:


javac -d . -cp $PLCAFEDIR/plcafe.jar user/*.java

Run the compiled code:


java -cp .:$PLCAFEDIR/plcafe.jar com.googlecode.prolog_cafe.lang.PrologMain com.googlecode.prolog_cafe.builtin:cafeteria

Prolog Cafe 1.2.5 (mantis)
Copyright(C) 1997-2009 M.Banbara and N.Tamura
| ?- main.
[4,2,7,3,6,8,5,1]
[5,2,4,7,3,8,6,1]
[3,5,2,8,6,4,7,1]
[3,6,4,2,8,5,7,1]
[5,7,1,3,8,6,4,2]
[4,6,8,3,1,7,5,2]
[3,6,8,1,4,7,5,2]
[5,3,8,4,7,1,6,2]
[5,7,4,1,3,8,6,2]
[4,1,5,8,6,3,7,2]
...

5 comments:

Emilian Bold said...

I found it a bit funny when I read "as recent as 2009" but, on second though, it's quite refreshing to see such a "recent" Prolog - Java translator.

I'm glad Prolog is still going strong because whenever I start a new project I think all I would need is a mixture language made out of Prolog, Clips and Lisp on top of the JVM. Then I start doing it in pure Java...

Patrick Logan said...

Yeah, 2009 seems somewhat dated at today's pace. I'm happy to see an active handful of people keeping it moving.

re: Prolog, Clips, and Lisp -- funny that you write that. Between 1986 and 1988 I used a system called KnowledgeCraft from a company that grew out of CMU (home of OPS5). KnowledgeCraft cost however many $10k's and ran on Lisp Machines that cost however many more multiples of $10k's.

But KnowledgeCraft combined CommonLisp, OPS5, and Prolog with a declarative knowledge representation system. "Semantic Nets" back then, but "semantic web" today.

That was an ideal programming environment. And now the same is available at a miniscule fraction of the cost.

Emilian Bold said...

>re: Prolog, Clips, and Lisp -- funny that you write that. Between 1986 and 1988 I used a system called KnowledgeCraft [...] KnowledgeCraft combined CommonLisp, OPS5, and Prolog with a declarative knowledge representation system.

Well that's a depressing thought :-) So the industry is actually regressing.

>And now the same is available at a miniscule fraction of the cost.

I assume you are talking about JVM + some Lisp + Prolog Cafe + some Java rule engine?

The problem is they are not very well integrated nor people educated to think such solutions.

Dev{eloper} Stonez said...

Prolog is definitely in use in our days ... Just take a look to this article:
Natural Language Processing With Prolog in the IBM Watson System

Patrick Logan said...

> So the industry is actually regressing.

"Plus ça change, plus c'est la même chose."

Actually most of the changes have been for the better. I'd certainly rather be developing software in todays environment than go back to the 1980s. Been there.

> The problem is they are not very well integrated nor people educated to think such solutions.

The integration is not as bad as one might expect. And languages like Clojure or Scala provide mechanisms to make the integration easier than with Java.

I am encouraged by the wide interest in many kinds of programming languages and paradigms over the last few years. I hope this is making its way back into formal education as well.

I was fortunate to have gone through a CS program before the days of Java or C++ having been established as "the language used by the department". PL/1 was my first language, and Lisp was second or third.

And I was fortunate to have had a couple of very good programming language semantics and implementation courses that covered a wide range.

Blog Archive

About Me

Portland, Oregon, United States
I'm usually writing from my favorite location on the planet, the pacific northwest of the u.s. I write for myself only and unless otherwise specified my posts here should not be taken as representing an official position of my employer. Contact me at my gee mail account, username patrickdlogan.