Introduction To Prolog
First Prolog Program
What Is Prolog?
Prolog is a declarative language. Programs in prolog are lists of facts and rules. When you execute a prolog program, you state a goal that you want to achieve. The program searches through all of the facts and rules attempting to reach this goal.
Prolog programs are text files. This means that we can write them in a text editor like Notepad. Launch Notepad a file to your area as family tree.pl You will have to make sure that you select All Files in the save dialog.
Type up the following facts for the family tree program.
/* Family Tree Program */
/* facts */
The lines which begin and end with asterisks and forward slashes are comments - these are ignored when the program is run. All names must be in lower case - Prolog uses upper case for unknown information. All statements in Prolog end with a full stop.
Your program will need some rules if it is going to be used to make decisions. Add the following lines to the bottom of your program.
/* rules */
The first rule states that M is the mother of X if M is a parent of X and M is female. The father rule works in a similar way.
Testing The Program
Save your program. Launch Prolog.
Click on File, Consult and select the program that you have saved.
We will start by getting our program to find out who the parents of evelyn are. Type the following line in the Prolog window.
In our statement, X stands for the information that we don't know. You should be given the answer X = dorothy. Press enter and type the following
You should get the answer X = jack. Use the program to find out the following.
- The father of liz
- The mother of john
- The father of grace
Adding More Rules
Return to your program and add the following rules.
To get your program to use the new rules, first save your program. Now click on File and Reload Modified Files.
Type in the following
You should get the answer X = john.
Now type in the following
After the first answer, press the semi-colon key. You should get the second possible answer.
Using The Anonymous Variable
We can get our program to answer vaguer questions than the ones we have been putting. Suppose we want to find out who all the mothers in the system are. Type the following
The underscore in Prolog is called the anonymous variable. This query should return all of the mothers in the system. Press the semi-colon after each answer.
Creating A User Interface
Expert systems can be made easier to use by creating an interface for the queries that you wish to run. Add the following to the bottom of your program.
write('Whose mother do you want to find? '),
write('The mother of '), write(Name), write(' is '), write(X), nl.
To use this, type findMother. at the prompt. The program should help you with questions. Remember to place a full stop at the end of any line that you enter at the prompt.
Extending The Program
Using the information above, try to write some new rules for your program to find the brother, grandfather, grandmother of a person.
Write interfaces for all standard queries in the system.