Since we've just met facts and rules, we want to get some practice with using them.
The program we wrote in the last tutorial was a fairly small one, so we won't be adding many rules.
Test your program by loading it into Prolog after each modification, and running the following queries against it:
(Do this now, before you change anything!)
Open the file in the text editor and try adding in rules to express the following:
Do these one at a time, testing the above queries each time
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 parent, 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 Prolog we represent this as:
male(james1). male(charles1). male(charles2). male(james2). male(george1). female(catherine). female(elizabeth). female(sophia). parent(charles1, james1). parent(elizabeth, james1). parent(charles2, charles1). parent(catherine, charles1). parent(james2, charles1). parent(sophia, elizabeth). parent(george1, sophia).
Start a new file in your text editor (call it "family.pl"), and copy and paste the above program into it.
We can now formulate some queries (try entering these yourself):
Try adding the following rules to the program, and check the results:
Remember that "and" in Prolog is represented using a comma.
If you get this done, can you add rules for:
Written by James Power
Revised: 7th October 1998
Contact:
James.Power@may.ie