Saturday, April 11, 2009

Prolog on Linux and a few lessons

I code extensively in Prolog and much of my work was done in an esoteric version of that language; Visual Prolog (earlier PDC Prolog, earlier to that... Turbo Prolog, yes from Borland) Visual Prolog is a fantastic language even though strict Prolog afficionados may scoff at it. It truly has some remarkable features and the PDC people are rightfully not so worried about where their language is placed. Unfortunately from version 6.0 onwards, there is no version for Linux, so I went looking for one. Version 5.2 did have a good Linux compiler but it was still a commercial one, and so not suitable for open source apps; an area I am now getting into.

I tried out two Prolog interpreters/compilers: SWI-Prolog and then Gnu Prolog. SWI-Prolog is extremely well supported but it is not as fast as Gnu Prolog. Gnu Prolog has an extensive range on how it can work: It can act as an interpreter, or it can even compile to various forms of compilation: bytecode (similar in nature to Java) and native code.

One can even create a customized Prolog interpreter with one's own extensions -- this was very appealing for some of my projects. I had written my own version of Prolog called "Web Prolog" which has not seen the light of the day by the outside world but was used quite effectively in my own office. Gnu Prolog interpreter creation feature may make Web Prolog even better and usable by many more. Oh I am digressing...

So here I wanted to try out Gnu Prolog's compilation feature. So I wrote this very simple test, and guess what; called it test.pl

go:- write('Hello world').

It compiled all right to create the executable "test" with the following compiler command:

gplc test.pl

but when I gave the following command

test

Nothing! Nothing happened! After several vexing minutes, I realized that Linux was probably not running test from the same folder where it compiled and there is another executable somewhere which is being executed. So I gave this command to find out

which test

And it returned

/usr/bin/test

Now I definitely was not compiling my program at /usr/bin/test (Nobody should, Work always in your own folder or sub-folders underneath it)

Dumb! What was that version of test doing there? No idea. Who created that? I think it came with some installation. I checked the date and it was created last year, so it was not because of some compilation I did.

Important takeaway lesson for Windows users:

In windows, when you run a command in a current folder, Windows will first execute from the current folder and if it is not existing there it will go checking the environment PATH. In Linux, not so. If you want to run from the current folder then you have to specifically state it thus:

./test

It is an excellent security feature, which is better explained elsewhere.

Now the program was not correctly written or compiled. gprolog was jumping directly into the interpreter. I wanted a squeaky little program of my own. So here is the actual code for gnu prolog:

:- initialization(go).

go:- write('Hello there').

And this is how you compile it:

gplc -o mytest --no-top-level test.pl

The above produces an executable called mytest Aha, the satisfaction of seeing someone on my laptop LCD telling me "Hello there" ....hmmmm

No comments:

Post a Comment