Powered by SmartDoc

Getting Started

June 2008
Mutsunori BANBARA and Naoyuki TAMURA
Kobe University, JAPAN

Table of Contents

Prolog Cafe is a Prolog-to-Java source-to-source translator system. Prolog Cafe consists of three parts:

In this section, we explain how to use the Prolog Cafe interpreter and translator by using N-Queens program as an example. The goal of N-Queens is to place N queens on an N by N board such that no piece attacks another.

queens.pl
main :-
	queens(8, Qs), write(Qs), nl, fail.

queens(N,Qs) :-
	range(1,N,Ns),
	queens(Ns,[],Qs).

queens([],Qs,Qs).
queens(UnplacedQs,SafeQs,Qs) :-
	select(UnplacedQs,UnplacedQs1,Q),
	not_attack(SafeQs,Q),
	queens(UnplacedQs1,[Q|SafeQs],Qs).

not_attack(Xs,X) :-
	not_attack(Xs,X,1).

not_attack([],_,_) :- !.
not_attack([Y|Ys],X,N) :-
	X =\= Y+N, X =\= Y-N,
	N1 is N+1,
	not_attack(Ys,X,N1).

select([X|Xs],Xs,X).
select([Y|Ys],[Y|Zs],X) :- select(Ys,Zs,X).

range(N,N,[N]) :- !.
range(M,N,[M|Ns]) :-
	M < N,
	M1 is M+1,
	range(M1,N,Ns).

How to use the Prolog Cafe interpreter

Prolog Cafe provides a small Prolog interpreter. The interpreter is useful for running and debugging Prolog programs. However, the execution speed is much slower than the translator mentioned in Section 2[How to use the Prolog Cafe translator].

Preliminary

Save the program in List 1[queens.pl] as queens.pl.

Getting Started

To run the Prolog Cafe interpreter, perform the following command. The interpreter shows the prompt '| ?-' as soon as it is ready to accept inputs.

> java -cp $PLCAFEDIR/plcafe.jar \
    jp.ac.kobe_u.cs.prolog.lang.PrologMain \
    jp.ac.kobe_u.cs.prolog.builtin:cafeteria
...
| ?-

The plcafe.jar is the jar archive of Prolog Cafe runtime system and translator. The jp.ac.kobe_u.cs.prolog.lang.PrologMain is the main program. The jp.ac.kobe_u.cs.prolog.builtin:cafeteria is an argument to be passed to the main program, where the jp.ac.kobe_u.cs.prolog.builtin and cafeteria are the package name and predicate name of the interpreter.

Reading in Programs (consult)

Put a file in brackets and type it.

| ?- [queens].
{consulting /.../queens.pl ...}
{/.../queens.pl consulted, 170 msec}
| ?-

This instructs the interpreter to read in (consult) the program.

Executing Goals

Type a goal.

| ?- 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]
...

no
| ?-

Debugging

Prolog Cafe provides a small debugger. The usage is almost same as other Prolog systems, and we omit it here. The following is a sample session of debugging.

| ?- spy(queens/3).
{Small debugger is switch on}
{spypoint user:queens/3 is added}

yes
{debug}
| ?- queens(4,X).
  0 call : user:queens(4,_A2470C) ? l
+ 1 call : user:queens([1,2,3,4],[],_A2470C) ? l
+ 2 call : user:queens([2,3,4],[1],_A2470C) ? l
+ 3 call : user:queens([2,4],[3,1],_A2470C) ? l
+ 3 fail : user:queens([2,4],[3,1],_A2470C) ? l
+ 3 call : user:queens([2,3],[4,1],_A2470C) ? ?
    Debuggin options:
    a      abort
    RET    creep
    c      creep
    l      leap
    +      spy this
    -      nospy this
    ?      help
    h      help
+ 3 call : user:queens([2,3],[4,1],_A2470C) ? -
{spypoint user:queens/3 is removed}
  3 call : user:queens([2,3],[4,1],_A2470C) ? l

X = [3,1,4,2] ? ;

X = [2,4,1,3] ? ;

no
{debug}
| ?- nodebug.
{Small debugger is switch off}

yes
| ?-   

Quit

Type halt. to exit from the interpreter.

| ?- halt.
bye

How to use the Prolog Cafe translator

Basic procedure to use the translator is as follows:

  1. Translate Prolog programs into Java programs.
  2. Compile translated code into Bytecode class files by using a Java compiler.
  3. Execute a goal in the interpreter or command line.

Preliminary

Save the program in List 1[queens.pl] as queens.pl.

Translating Prolog into Java

Create the work directory and move to there.

> ls
queens.pl
> mkdir work
> cd work/

Let us translate a Prolog file queens.pl into Java.

work> java -cp $PLCAFEDIR/plcafe.jar \
    jp.ac.kobe_u.cs.prolog.compiler.Compiler ../queens.pl
...
work> ls
PRED_main_0.java	PRED_not_attack_3.java	PRED_queens_3.java	PRED_select_3.java
PRED_not_attack_2.java	PRED_queens_2.java	PRED_range_3.java

The jp.ac.kobe_u.cs.prolog.compiler.Compiler is the class for translating Prolog into Java. The ../queens.pl is an input program to be passed to that class. After the translation succeeds, for each predicate in queens.pl, a file named PRED_f_n.java is created in the current directory, where f is the predicate name, and n is the arity.

Compiling Translated Code

Compile translated code by using the javac command.

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

Now, all of the predicates of queens.pl are compiled into Java bytecode in the current directory.

work> ls *.class
PRED_main_0.class		PRED_not_attack_3_var_1.class	PRED_range_3.class
PRED_not_attack_2.class		PRED_queens_2.class		PRED_range_3_1.class
PRED_not_attack_3.class		PRED_queens_3.class		PRED_range_3_2.class
PRED_not_attack_3_1.class	PRED_queens_3_1.class		PRED_range_3_sub_1.class
PRED_not_attack_3_2.class	PRED_queens_3_2.class		PRED_range_3_top.class
PRED_not_attack_3_top.class	PRED_queens_3_var.class		PRED_select_3.class
PRED_not_attack_3_var.class	PRED_queens_3_var_1.class	................

Executing a Goal in the Interpreter

Add the current directory to the class path and execute goals on the interpreter.

work> java -cp .:$PLCAFEDIR/plcafe.jar \
    jp.ac.kobe_u.cs.prolog.lang.PrologMain \
    jp.ac.kobe_u.cs.prolog.builtin:cafeteria
...
| ?- 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]
...
no
| ?- findall(X,queens(4,X),L).
L = [[3,1,4,2],[2,4,1,3]] ? 
yes

Executing a Goal in the Commnad Line

By passing a top-level goal as atom to the PrologMain class, it is possible to execute it from command line without the interpreter.

work> java -cp .:$PLCAFEDIR/plcafe.jar \
    jp.ac.kobe_u.cs.prolog.lang.PrologMain 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]
...
work>

Loading Predicates from Jar files

Create a jar file of class files.

work> jar cf ../queens.jar *.class
work> cd ..
> ls
queens.jar	queens.pl	work

Add that jar file to the class path and execute a goal.

> java -cp queens.jar:$PLCAFEDIR/plcafe.jar \
    jp.ac.kobe_u.cs.prolog.lang.PrologMain 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]
...