1. You have now learnt about: VED commands from Prolog ved prog1.pl showlib traveller.pl teach lesson2 VED commands from inside VED ESC w ENTER x (etc) Prolog questions, with and without variables loves( mary, john ). loves( Who, john ). loves( X, Y ). Questions with "and" loves( X, Y ), drinks( Y, wine ). eats( X, curry ), eats( X, garlic ). Facts loves( mary, john ). Facts with "if" loves( mary, X ) :- drinks( X, wine ). Facts with "if" and "and" loves( mary, X ) :- drinks( X, wine ), eats( X, curry ). You have also learnt, in Supplement 1, the words PREDICATE and ARGUMENT, which I shall use from now on. In "loves(mary,john)", the predicate is "loves" and its arguments are "mary" and "john". I shall also use RULE to mean a conditional fact (one with :- in). I shall write FACT on its own, or sometimes CLAUSE, when I mean either a conditional or an unconditional fact and don't care which. Now, for the matter of Lesson 2. 2. Please start by typing library(relations). This will fill your knowledge base with the facts in a file which I have created for you, called "relations". You can find out what is in this file by typing "showlib relations.pl", and you can print it by then giving the ENTER command "ctcprint". 3. Your knowledge base defines three predicates. They are "is_a", "is_father_of", and "is_mother_of". You will see that "is_a" is used in two ways. One is to say someone's male; the other, to say they're female. So each fact in the knowledge base is saying one of four things: That A is male; that A is female; that A is B's father; or that A is B's mother. 4. In the rest of this lesson, we're going to use Prolog to define what various family relationships mean. We'll use the same kind of rules we saw in Lesson 1 and the following exercises. But there is a difference in the kind of knowledge we're encoding. Previously, we've concentrated (not too seriously :-) on representing ad-hoc rules of thumb about medicine, sport, and other messy topics. But in the rest of the current lesson, we're going to think more about structural relationships; on asking "what does it mean to say A is B's cousin, and how can I formalise this notion?" In terms of what I said in Supplement 1, we will be writing intensional and not extensional definitions. In A.I. terms, we're using Prolog as a KNOWLEDGE REPRESENTATION LANGUAGE. This is an important part of A.I., especially in expert systems work. A.I. programs often mix ad-hoc and systematic knowledge. For example, a system for diagnosing faults in radios might contain some rules of thumb like "if it's a battery set, test for flat batteries", but it could also contain some systematic knowledge about the properties of electronic components, Ohm's law, etc. In medicine and psychology, I'm afraid the rules are mainly ad-hoc... 5. Now, back to relationships. Let's start simply. Some people in the knowledge base are mothers, for example Amanda and Anjali. Now, I would like to be able to ask the question is_mum(amanda). or is_mum(anjali). and receive the answer "Yes". So, can you add a rule for "is_mum" that will do this. That is, can you write a rule which begins is_mum( X ) :- and which ends with a dot, and where there is some text between the ":-" and the dot which states under what conditions X can be a mum? Write this rule in terms of those which you already have in the knowledge base, and then type it into Prolog. If you get it wrong, you can always delete it later, so don't worry about mistakes. 6. When trying these problems, remember that Prolog knows only those facts which are in its knowledge base (i.e. the set of facts which you see on typing "show"). From which of these facts is it possible to infer that someone might be a mother? Well, a fact like "is_a(bill,male)" isn't much use for this, though it does tell you that Bill CAN'T be a mother. How about "is_a(freda,female)"? Not much use either: although Freda is female, she is not automatically going to have children (unless, like an aphid, she's been born with two generations of small but perfectly formed offspring inside her). 7. We've now rejected two kinds of fact as unhelpful: those like is_a(bill,male). is_a(freda,female). The third kind of fact you see on doing "showlib" is exemplified by is_father_of(bill,charles). This is not much help either. So all we are left with is facts like is_mother_of(freda,gertrude). which is what we want. If Freda is the mother of Gertrude, she must be a mum. Indeed, if X is the mother of Y, whoever Y is, then X is necessarily a mum. Can you work out how to express this in Prolog, and type it in? 8. You should have typed is_mum( X ) :- is_mother_of( X, Y ). Following this, how would you define the predicate "is_dad"? 9. Now, how about defining parenthood? Firstly, can you devise a rule or rules to define is_parent_of( X, Y ) :- You will probably need to type in two rules for this. Remember: when doing these problems, examine the facts which Prolog already knows. Ask yourself which facts tell you something positive about parenthood. Ignore ones that don't. You are now left with a few facts from which you can infer parenthood. Must more than one of them be true at the same time? If so, you need an "and" (or perhaps several "and"s) after an "if": is_parent_of( X, Y ) :- ... , ... , ... On the other hand, perhaps there are several facts, any one of which is sufficient in each case. Then you need several definitions of "is_parent_of": is_parent_of( X, Y ) :- ... . is_parent_of( X, Y ) :- ... . 10. In this case, you have two relevant kinds of fact. The facts about maleness and femaleness are (as before) no help. The ones about motherhood and fatherhood are of help. X can be Y's parent if X is Y's mother. X can also be Y's parent if X is Y's father. Unless X is a hermaphrodite (unlikely), X does not have to be both father and mother at the same time to be a parent. We can therefore write: is_parent_of( X, Y ) :- is_mother_of( X, Y ). is_parent_of( X, Y ) :- is_father_of( X, Y ). i.e. X is Y's parent if EITHER X is Y's mother, OR X is Y's father. 11. How could we define the predicate "has_children( X )"? Try it. Again, we want one (or more) rules of the form has_children( X ) :- ... . 12. There are actually three ways you could have done this. This is the first. has_children( X ) :- is_parent_of( X, Y ). This is the second. has_children( X ) :- is_mum( X ). has_children( X ) :- is_dad( X ). And this is the third. has_children( X ) :- is_mother_of( X, Y ). has_children( X ) :- is_father_of( X, Y ). The first two methods use rules you yourself wrote. The third method sticks with what was originally in the knowledge base. In general, there is often more than one method to solve such problems, and some methods may use auxiliary definitions (as the first two do here). 13. Now, let's try reversing the relationships. Can you write some rules which define the predicates is_son_of( X, Y ) is_daughter_of( X, Y ) is_child_of( X, Y ) Remember, first find those facts that contain relevant information. Secondly, work out how to combine them: do you need "and"s? do you need more than one definition, as for parent? Don't worry if there seems to be more than one way to solve the problem - in the last section, there were three. There are often several equally good solutions. 14. Probably the easiest way to deal with these is to write rules which talk about parenthood and sex. A son must be male. A son must also (at the same time) have a parent: is_son_of( X, Y ) :- is_a( X, male ), is_parent_of( Y, X ). Alternatively, you could write is_son_of( X, Y ) :- is_parent_of( Y, X ). is_a( X, male ). Note that this is WRONG: is_son_of( X, Y ) :- is_parent_of( Y, X ). is_son_of( X, Y ) :- is_a( X, male ). because it says that X is Y's son if X is EITHER male, OR if Y is X's parent. 15. Now, can you define what it is to be a sister? Try completing the rule is_sister_of( X, Y ) :- Hints: 1) X must be a female. The sex of Y does not matter. 2) X and Y must have the same parent (if step-sister) or parents (if strict sister). I don't mind whether you take "sister" to include step-sisters, but make sure that you know whether you are doing so. 16. Try asking a few questions about who is a sister of who. What is odd about the answers? You would need to change your definition slightly to exclude some of the answers which you now see. You don't know enough of Prolog to do this yet - what in particular don't you know? 17. Finally, how would you define the predicate "X ancestor_of Y", meaning X is Y's ancestor? You may find this much harder than the other problems: if you can't do it in twenty minutes or so, ask your teacher for advice.