[mercury-users] RE: top-level loop?

Bartlomiej Szymczak rhywek at gmail.com
Sat Jun 2 20:10:59 AEST 2007


On Dec 05 1998 Andreas Schoter wrote:
> Please excuse the dumb question: is it possible to interact with the system
> in the kind of way that Prolog's top-level loop provides, i.e, the '?-'
> prompt?

As far as I know, there is no such possibility. I, however, find the
top-level feature extremely useful, so I use Prolog's top-level for
testing my Mercury programs. Of course you can't simply load Mercury
program into Prolog. It won't compile. So I have written the M2PL
program (attached), which will convert Mercury file to Prolog.

The conversion is very simple - it removes blocks of lines such that
the first line starts with ":-" or "main(" and the last line ends with
".". This way it removes all type and mode definitions, etc., and the
main predicate. It also replaces double quotes with single quotes, so
that strings become atoms.

To use the program:

Compile it (here I use javac):

javac M2PL.java

Run it, supplying your Mercury program as standard input and saving
the Prolog program from standard output. If you use bash as your
shell, you can do (here I use java):

java M2PL <program.m >program.pl

If you use GCJ as your Java compiler, try:

gcj -o M2PL --main=M2PL M2PL.java
./M2PL <program.m >program.pl

Now you can load your generated Prolog code to your Prolog system and
enjoy top-level loop!

For multi-file projects, just do:
for i in *.m; do java M2PL <$i >${i%m}pl; done

It works only for simple programs, where everything except for main
predicate works in Prolog. So if you have such a program:

:-type yesno ---> yes ; no.
:-pred isu(string::in,yesno::out) is det.
isu(W,X):-(if uselessWord(W) then X=yes else X=no).

You need to rewrite it:

:-type yesno ---> yes ; no.
:-pred isu(string::in,yesno::out) is nondet.
isu(W,yes):-uselessWord(W).
isu(W,no):- \+uselessWord(W).

Now you have nice syntax, which works both in Prolog and Mercury, but
notice that I had to change the determinism declaration to nondet,
because that's what Mercury compiler infers. Probably the golden
middle is:

:-type yesno ---> yes ; no.
:-pred isu(string::in,yesno::out) is det.
isu(W,X):-(uselessWord(W) -> X=yes ; X=no).

Now the syntax is not as nice as the first translation, but you can
keep your determinism declaration as det.

Best regards,
-- 
Bartlomiej Antoni Szymczak

Gmail: rhywek at gmail.com
Work mail: bas at imm.dtu.dk
Student mail: s041702 at student.dtu.dk
DTU office: 321/129
DTU tel.: +4545253892
Mobile: +4520789323
-------------- next part --------------
A non-text attachment was scrubbed...
Name: M2PL.java
Type: text/x-java
Size: 1358 bytes
Desc: not available
URL: <http://lists.mercurylang.org/archives/users/attachments/20070602/0d14bc76/attachment.java>


More information about the users mailing list