Introduction
In class I demonstrated how to use Prolog by connecting to student.cs.uni.edu. In this homework you will first walk through a simple tutorial for using Prolog. Second, you will create a simple knowledge base in Prolog and ask it to prove some simple facts.
To complete this lab, you will either need to log on to student.cs.uni.edu (using the account that I gave you in class) or download a free trial copy of prolog for Windows. I used to use Trinc-Prolog which seems to have disappeared. I make no claims or recommendations regarding any version you may find online.
Firstly, we want to type in a Prolog program and save it in a file, so, using
a Text Editor such as pico or vi
type in the following program:
likes(mary,food).
likes(mary,wine).
likes(john,wine).
likes(john,mary).
Try to get this exactly as it is
Once you have typed this in, save it as intro.pl (Prolog files usually end with ".pl", just as Java files end with ".java")
Start Prolog at the command prompt; by typing 'prolog' After a while, you should get something like the following on screen:
GNU Prolog 1.2.9
By Daniel Diaz
Copyright © 1999-2001 Daniel Diaz
|| ?-
Prolog is now running and waiting for you to type in some commands.
Writing programs in Prolog is a cycle involving
1. Write/Edit the program in a text-editor
2. Save the program in the text editor
3. Tell Prolog to read in the program
4. If Prolog gives you errors, go back to step 1 and fix them
5. Test it - if it doesn't do what you expected, go back to step 1
We've done the first two of these, so now we need to load the program into Prolog. The program has been saved as "intro.pl", so in your Prolog window, type the following and hit the return key:
[intro].
Don't forget the full-stop at the end of this!
This tells Prolog to read in the file called intro.pl - you should do this every time you change your program in the text editor. (If your program was called something else, like "other.pl", you'd type "other" instead of "intro" above).
You should now have something like the following on screen
| ?- [intro].
compiling /user/username/intro.pl for byte code
/user/username/intro.pl compiled, 4 lines read- 554 bytes written, 12 ms
yes
|| ?-
The "yes" at the end indicates that Prolog has checked your code and found no errors. If you get anything else (particularly a "no"), you should check that you have typed in the code correctly.
We can now ask Prolog about some of the information it has just read in. Type in the following (and don't forget the full-stop at the end: Prolog won't do anything until it sees a full-stop)...
? likes(mary,food).
When you do this you should get
true ?
At this point you should hit the semicolon key. This tells the computer you wish to see any additional facts pertaining to your query. Since there aren't any, it quits and returns "yes." Indicating that the fact is true.
Repeat this process for each of the following. What do you observe for
each?
? likes(john,wine).
? likes(john,food).
? likes(john,X).
? likes(mary,X).
? likes(Y,food).
? likes(Y,wine).
The program we wrote in the last tutorial was a fairly small one containing few facts. Thus, it doesn't make much sense to add very many rules, but to get you started consider the following:
? John likes anything that Mary likes
How do you write this rule? In prolog, rules are written BACKWARDS from what you think of in discrete. The conditions go on the right hand side, and the result goes on the left hand side. Since Mary liking something is the condition and John liking it is the result, we write:
likes(john,X) :- likes(mary,X).
Add this rule you your knowledge base using your text editor. Test the
impact of this rule by reloading and resubmitting the
following queries against it:
? likes(john,X).
? likes(mary,X).
? likes(Y,food).
? likes(Y,wine).
Repeat this process with each of the following rules...
? John likes anyone who likes wine
? John likes anyone who likes themselves
Do these one at a time, testing the above queries each time.
No is a good place to take a break if you need to. When you want to leave Prolog you do so by typing
halt.
Suppose that we want to represent a family tree, so that we can ask questions like "is john related to ...", or "list all john's sisters" and so on.
The basic entities will be people; the properties we will want to look at will be father, mother, brother, sister, ..... We choose three basic predicates, male, female and parentOf, which will describe a family by a series of facts.
Take the following family tree as an example:
James I
| | +----------------+-----------------+ | | Charles I Elizabeth | | | | +----------+------------+ | | | | | Catherine Charles II James II Sophia | | | George I
In order to test your knowledge base, submit these queries, and observe if the correct answers come out.
Add the following rules to the program, and check the results:
? M is the mother of X if she is a parent of X and is female
? F is the father of X if he is a parent of X and is male
? X is a sibling of Y if they both have the same parent.
Remember that "and" in Prolog is represented using a comma.
In addition to the above rules, consider the following six relationships. Add rules for these to your knowledge base.
Prior to the deadline, use the homework submission system (http://math-cs.cns.uni.edu/~schafer/submit/which_course.cgi) to upload :
family.pl