The Prolog Tutorial:

Introducing Prolog

This is the first tutorial in the course. As this is an introduction it is kept simple but having said that there is a lot of material that has to be covered. The input that you are supposed to enter into SICStus is displayed in the following font type -

| ?- write( 'Hello World' ).

After each entry the you should hit return / enter. But do not forget to put in the full stop / period before doing so.

Aims of the first tutorial:

  • To be able to start and exit SICStus.
  • To be able to carry out simple interactions with the SICStus:

Queries

Asserting facts

Retracting facts

  • To understand simply how Prolog works:

The database + search mechanism

A Brief Introduction

Prolog is a programming language based on logic. Unlike other programming languages that you may have come across, it is declarative. This means that programmer attempts to describe what the problem is. This is different from procedural programming languages where the programmer describes how the problem is solved.

The Prolog System

Prolog consists of a database ( as opposed to a set of instructions to work through ) and a search mechanism.

Remember this. Understanding how the Prolog search mechanism works is vital in understanding Prolog.

Running SICStus

Log into Solaris. If the machine you are in is currently running NT. Shutdown the machine and reboot. Do this from within NT - don't hit the restart button!

Starting SICStus

After you have logged in you will be presented with a desktop. There are two basic ways to run SICStus both need a terminal window. So open a terminal window. This done you can either run SICStus directly in this shell or run it in a separate window by using xterm or cmdtool.

  1. To run SICStus directly in the current shell merely type 'sicstus' or 'prolog'. You will then presented with a short burst of information on the version of SICStus you've loaded and a prompt which looks as follows:
  2. | ?-

    You're ready to go.

  3. If you wish to run SICStus in a separate window you can type either of the following - 'xterm -e sicstus &' or 'cmdtool -e sicstus &' ( where I have written 'sicstus' you could also put 'prolog'. A new window should open and you will then presented with a short burst of information on the version of SICStus you've loaded and a prompt which looks as follows:

| ?-

The first advantage of using SICStus this way is that you can still use the original shell to perform whatever that you need to ( e.g copying files etc. ). The second advantage is that both these programs ( xterm & cmdtool ) can be given additional command line arguments that can change the font / font size or the colour of the display.

Exiting SICStus

To quit SICStus type 'halt.' -

| ?- halt.

If you have run SICStus in separate window the window will be disappear ( or killed in UNIX parlance ). On the other hand if you have run it a terminal window, control id returned to the terminal.

Your first interaction with Prolog

Okay. If you've got this far you can start interacting with the Prolog system.

Type the following and hit return.

| ? - write( 'Hello world' ).

Don't forget the full stop and note we're using single quotes. You should see that the following:

| ? - write( 'Hello world' ).

Hello World

Yes

| ?-

There are a few things you should note from this simple interaction.

  • First of all Prolog comes with a number of built-in predicates ( of which write is one ). Over the coming tutorials you should learn many of these. For they will be your building blocks for writing programs. To be able to program in Prolog you'll need to have an understanding of what many of these built-in predicates do.
  • Second of all you'll note that we interact with Prolog system. Generally speaking Prolog is an interpreted programming language ( although you can get Prolog compilers ). This means that we will often load information into the system and then 'query' the system.
  • Thirdly note that the interaction resulted in the printing of a 'yes'. If Prolog can successfully carry out a query it will print yes. If it can't we'll get 'no'.
  • Prolog will redisplay the prompt and wait for further input.

Okay that's probably a lot of information to digest in one sitting. So lets carry on and carry out some more simple interactions. Type the following and see what response you get.

| ? - atom = atom.

| ? - horse = horse.

| ? - atom = horse.

| ? - horse = atom.

How did the Prolog system respond? Remember what we've just learned about how Prolog reacts when it can or can't carry out a query.

You have also just entered one of the basic data types in Prolog - an atom.

Now try the following.

| ? - atom( atom ).

| ? - atom( horse ).

| ? - atom( 'atom' ).

| ? - atom = 'atom'.

| ? - atom = "atom".

| ? - atom( 'horse' ).

| ? - horse = 'horse'.

| ? - atom( "atom" ).

| ? - atom( "horse" ).

Again, what was the response from the Prolog system?

There are a number of things you should note from the responses -

  • There is a built-in predicate atom which tests to see if something is an atom or not.
  • An atom consists of lowercase letters, or characters contained in apostrophes.
  • The '=' is another built-in predicate known as the matching operator. It attempts to match items before it and behind it.
  • The matching process compares items character by character so atom does not equal "atom". Whereas atom will match with 'atom'. This because Prolog will match the characters between the single quote marks.
  • "atom" is not an atom. It is a list. We will examine this data type in detail in tutorial 4 .

Here are the rules for constructing an atom.

Other data types will be discussed in the next tutorial.

In the next section we will in effect give the Prolog system some information and then query about this information.

Putting facts in Database

Prolog is a programming language that is excellent at describing objects and the relations between objects. When we use Prolog we generally load information into what is known as the database. The information in this database is Prolog's world knowledge.

Type the following:

| ?- listing.

This lists information in the database. However, as you should note from the examples above the system also has 'knowledge' ( how they work and what they should do ) of the built-in predicates ( e.g. write, atom, listing, halt ).

There are two ways to enter information into the database: (1) by using assert or (2) by using consult. These are two more built-in predicates. We will only examine the former in this particular tutorial.

Let us say that we have the following set of information.

Harry's is boy.

Tony is a boy.

Rachel is a girl.

Eunice is a girl.

We have to turn this information into facts that Prolog will recognise. This is done as follows:

boy( harry ).

boy( tony ).

girl( rachel ).

girl( eunice ).

The relation comes first, in this case we use 'boy' or 'girl' to mean "is a boy" or "is a girl". The argument to the relation is the object. In this case all the people are objects. Note that the names of the people are all lower-case as is the relation name. Why this is so will become clear in the next tutorial. For the time being just assume that we generally only use atoms to build relations.

Now to enter these facts type the following:

| ?- assert( boy( harry ) ).

| ?- assert( boy( tony ) ).

| ?- assert( girl( rachel ) ).

| ?- assert( girl( eunice ) ).

Now if you type:

| ? - listing.

What do you see?

You should see that the facts have been entered into Prolog's database.

You can now query the database about these facts.

| ?- boy( harry ).

What response did you get? This is the equivalent of asking "Is Harry a boy". Now see what happens if you type the following:

| ?- boy( jer ).

Do you see what the response was? Prolog has what is known as a "closed world assumption". If something is not in its database it is false. Even though in reality it may be true. But you must remember we are not querying the system about reality we merely querying it about the contents of its database.

To remove a fact that we have asserted we use another built-in predicate called retract. So to remove the fact - boy( harry ) - we would type the following:

| ?- retract( boy( harry ) ).

In future tutorials we will see how to load information from a file into the database. Which makes things a lot easier

Exercises - Tutorial 1

Try and create facts from the following information:

Joe is male.

Pat is male.

Sharon is female.

Mary is female.

Joe is the father of Mary.

Sharon is the mother of Mary.

Joe is married to Sharon.

Answers

Peculiarities of using Prolog in D.C.U.

There are actually two versions of SICStus available to the user in DCU. For the purposes of this introductory tutorial this does not make much difference. However, if in the future you are undertaking a large project written in Prolog please be careful as to which version you use. SICStus comes with a huge number of libraries that make it quite a powerful system. Some libraries may not be available with the earlier version of SICStus - so bear that in mind.

What you should know after this first tutorial:

  • You should be able to start and exit SICStus.
  • You should be able to carry out simple interactions with the SICStus:

To query the database.

To assert facts into it.

To retract facts from it.

  • You should have a rough understanding as to how Prolog works:

You should understand that the database is Prolog's storehouse of knowledge.

These are the built-in predicates that are introduced in tutorial 1:

write, atom, =, listing, assert, consult

Author - Jer Hayes - 2000