A "Hello World" program in SWI-Prolog

e · l · n
Sep 22, 2009

This is how to create a "Hello World" kind of program in SWI-Prolog, as installed on Ubuntu (Jaunty).

Prolog usage can be divided into two phases, one in which you write the Prolog program, and the other in which you query the program. The first phase is easiest done in a separate file. So create a file, i.e. test.pl, in you home folder, and edit it with a text editor like GEdit or Kate (Kate is much more powerful).

Then you can load the program from inside prolog after you've started it.

So, let's start the prolog interactive GUI:


Then, in the Prolog GUI, load the file test.pl like so:

?- [test].

Now, if you had some prolog clauses in the test.pl file, you will be able to extract that information by querying.

A very simple test program that you could create is:

/* Some facts about parent relationships */
/* A general rule */
grandparent(Grandparent,Child) :-

If you write this in test.pl, and load test.pl according to the instructions above, you'll now be able to query prolog for the grandparent of jim in the following way:

?- grandparent(WHO,jim).
WHO = sam ;


More explanations about how Prolog works can be found elsewhere. This is just how to get going

When you want to exit the Prolog GUI, type:

?- halt.