A "Hello World" Prolog program

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:
prolog

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 */
parent(sam,mark).
parent(mark,jim).
/* A general rule */
grandparent(GRANDPARENT,CHILD) :-
parent(GRANDPARENT,PARENT),  
parent(PARENT,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 ;
false.

Notes

  • "false." here just means that after the first occurence of a matching item, it didn't find anymore items.
  • words starting with a Capital letter are treated as variables, while all-lowercase words are treated as atoms.

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.

Tags:

Comments

bio and/or chemo example?

Maybe it is interesting to write a (bio)chemical example too?

BTW, is their a Jaunty .deb for swi-prolog?

Yep, sure! Is hoping to get

Yep, sure!

Is hoping to get blipkit up running quite soon. Then hopefully being able to try out an example with RDF data, (but don't know yet how much struggle there will be before getting to that point).

Otherwise I might look closer at the "RNA knot" example rule, which was mentioned as something that is hard/impossible to represent with Descriptive Logic. It is rather illustrative, so I it might be a good candidate for the presentation at least.