1.4 Practical Session 1

Don't be fooled by the fact that the descriptions of the practical sessions are much shorter than the text you have just read --- the practical part of the course is definitely the most important. Yes, you need to read the text and do the exercises, but that's not enough to become a Prolog programmer. To really master the language you need to sit down in front of a computer and play with Prolog --- a lot!

The goal of the first practical session is for you to become familiar with the basics of how to create and run simple Prolog programs. Now, because there are many different implementations of Prolog, and many different operating systems you can run them under, we can't be too specific here. Rather, what we'll do is describe in very general terms what is involved in running Prolog, list the practical skills you will need to master, and make some suggestions for things to do.

The simplest way to run a Prolog program is as follows. You have a file with your Prolog program in it (for example, you may have a file kb2.pl which contains the knowledge base KB2). You then start Prolog running. Prolog will display its prompt, something like

?-

which indicates that it is ready to accept a query.

Now, at this stage, Prolog knows absolutely nothing about KB2 (or indeed anything else). To see this, type in the command listing, followed by a full stop, and hit return. That is, type

?- listing.

and press the return key.

Now, the listing command is a special in-built Prolog predicate that instructs Prolog to display the contents of the current knowledge base. But we haven't yet told Prolog about any knowledge bases, so it will just say

yes

This is a correct answer: as yet Prolog knows nothing --- so it correctly displays all this nothing and says yes. Actually, with more sophisticated Prolog implementations you may get a little more (for example, the names of libraries that have been loaded) but, one way or another, you will receive what is essentially an ``I know nothing about any knowledge bases!'' answer.

So let's tell Prolog about KB2. Assuming you've stored KB2 in the file kb2.pl, and that this file is in the same directory where you're running Prolog, all you have to type is

?- [kb2].

This tells Prolog to consult the file kb2.pl, and load the contents as its new knowledge base. Assuming that the kb2.pl contains no typos, Prolog will read it in, maybe print out a message saying that it is consulting the file kb2.pl, and then answer:

yes

Incidentally, it is quite common to store Prolog code in files with a .pl suffix. It's a useful indication of what the file contains (namely Prolog code) and with many Prolog implementations you don't actually have to type in the .pl suffix when you consult a file.

Ok, so Prolog should now know about all the KB2 predicates. And we can check whether it does by using the listing command again:

?- listing.

If you do this, Prolog will list (something like) the following on the screen:

listensToMusic(mia).
happy(yolanda).
playsAirGuitar(mia)  :-     
        listensToMusic(mia).
playsAirGuitar(yolanda) :-     
        listensToMusic(yolanda).
listensToMusic(yolanda):-  
        happy(yolanda).
 
yes

That is, it will list the facts and rules that make up KB2, and then say yes. Once again, you may get a little more than this, such as the locations of various libraries that have been loaded.

Incidentally, listing can be used in other ways. For example, typing

?- listing(playsAirGuitar).

simply lists all the information in the knowledge base about the playsAirGuitar predicate. So in this case Prolog will display

playsAirGuitar(mia)  :-     
        listensToMusic(mia).
playsAirGuitar(yolanda) :-     
        listensToMusic(yolanda).
 
yes

Well --- now you're ready to go. KB2 is loaded and Prolog is running, so you can (and should!) start making exactly the sort of inquiries we discussed in the text ...

But let's back up a little, and summarize a few of the practical skills you will need to master to get this far:

The sooner you pick up these skills, the better. With them out of the way (which shouldn't take long) you can start concentrating on mastering Prolog (which will take a lot longer).

But assuming you have mastered these skills, what next? Quite simply, play with Prolog! Consult the various knowledge bases discussed today, and check that the queries discussed really do work the way we said they did. In particular, take a look at KB5 and make sure you understand why you get those peculiar ``jealousy'' relations. Try posing new queries. Experiment with the listing predicate (it's a useful tool). Type in the knowledge base used in Exercise 5, and check whether your answers are correct. Best of all, think of some simple domain that interests you, and create a brand-new knowledge base from scratch ...


Patrick Blackburn, Johan Bos and Kristina Striegnitz
Version 1.2.5 (20030212)