|
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:
Queries Asserting facts Retracting facts
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.
| ?- You're ready to go. | ?- 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.
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 -
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.
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:
To query the database. To assert facts into it. To retract facts from it.
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 |