A Hello World program in SWI-Prolog
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.