1. Start up Prolog. Wait until you get the ?- prompt, and then type the QUESTION loves(mary,john). followed by RETURN. You must use only lower-case letters, even though you're talking about people's names. If you use upper-case, then Prolog will treat the names as variables, and they mean something quite different. You should find that Prolog responds 'no'. 2. Now enter a FACT into Prolog's DATABASE. Do this by using the Ved editor, as follows: From the ?- prompt, type ved myfile.pl followed by RETURN. Ved will display the contents of this file (which should be empty) on the screen. Type loves(mary,john). and then hit ENTER (remember, this is the name we use for the + key at the lower right-hand corner of the keypad). Type x and then RETURN. You will get a message saying that your file is being written and then loaded into Prolog, after which you'll see a ?- prompt again. 3. Now ask the question, as before. Once again, make sure that both names start with a lower-case letter. Prolog should have answered "yes" to that question, because you'd told it that Mary, John. Now try asking loves(anne,george). 4. Prolog should have answered "no" to that one. Of course, in the real world, there is almost certainly an Anne somewhere who does love a George. But Prolog (obviously) can't tell what's going on outside its computer. All it has to work from is the fact you've told it about Mary and John. Now, if someone asked us a question we don't know the answer to, we'd say "don't know" or "haven't a clue". But Prolog is different. If it doesn't know something, it ALWAYS assumes the answer is FALSE. So a "no" answer really means "I don't have enough facts to prove that this is true, so I'll assume it is false". 5. You could go on in this way for some time, adding facts and asking Prolog whether it knows them or not. But that wouldn't be very exciting - after all, you typed the facts, so you must know whether they're there. But you can make things more interesting by using VARIABLES in your questions. For example, suppose you wanted to find out WHO Mary loves? You can ask this by putting a variable in your question. You can use any word as a variable, as long as it starts with a capital letter - for example, X, to represent something unknown. Try loves(mary,X). Make sure you put the X in UPPER CASE. Don't forget the dot, and as usual, end with RETURN. ~6. You should have got back an answer saying Yes - X = john or something similar. 7. You can also use two variables in a question. Try this question: loves(X,Y). In general, any word that starts with a capital is a variable. I shall mainly write X and Y for variables, but you could use other names, such as Person or Z or Which or Who or Blurgle or Crun (though it's a good idea to stick to sensible names). 8. Now watch what happens if Mary, more than one person: You will have to type in some more facts first. Use Ved to add this fact: loves( mary, dai ). and then ask the question loves( mary, X ). 9. From the facts you've now put in, Prolog can tell that Mary loves both John and Dai, and so it gives you both as possible values of X. Use Ved to add another romance: loves( juliet, romeo ). and then ask loves( X, Y ). 10. In general, you can use variables in either or both positions in a question. What question would you type to find out who, Dai? 11. You should have typed loves( X, dai ). But any variable names would have done, so you could instead have said loves( Y, dai ). loves( Who, dai ). or even loves( Potrzebie, dai ). though the last is not recommended as you and your teacher won't find it very easy to understand. 12. We shall return to questions a bit later. First, I must tell you about errors. One particularly common mistake is to omit the full stop at the end of a sentence: loves( juliet, romeo ) <---- missing full stop loves( mary, john ). This causes Prolog to take both lines as making up one question. That's because Prolog uses full stops as we do, to mark the end of a sentence. It has no other way to tell when a sentence finishes - it's far too stupid to use meaning as a cue. However, it can tell that there's something wrong with such a fact, because there are too many words and brackets. The kind of fact you've met so far can only contain three words: two to name things, and one to state a relation between those things. 13. You can test this by typing such a fact. Type the above, and watch for the error message. 14. Now we shall return to our romances. So far, we have only seen simple facts which state things that, as far as Prolog knows, are always true. But we can also tell Prolog that the truth of one fact depends on the truth of another. To do this, use the symbol :- , which is Prolog for "if". So, using Ved, enter the fact loves( maggie, denis ) :- drinks( denis, gin ). Then ask the question loves( maggie, X ). NB. You can lay out the fact above in almost any way as long as you don't have spaces inside names. However, the format above is generally accepted, and makes programs easy to read. 15. Prolog should have said "no". Being unable to find any facts that tell it Maggie does love Denis, it assumes the opposite (as we saw in section earlier). Now use Ved to add the fact drinks( denis, gin ). and again ask loves( maggie, X ). You should have got the answer "X = denis". This is an example of INFERENCE. Prolog did not need to be told explicitly that Maggie, Denis - it inferred it from the :- drinks( denis, gin ). part of the new fact. Try another example of inference. Use Ved to add these facts: loves( maggie, nick ) :- hates( nick, europeans ). hates( nick, europeans ). and then ask loves( maggie, X ). Prolog will INFER the answer to the question, by reasoning about the two facts. 16. We call facts that contain "if", CONDITIONAL FACTS. You can write conditional facts with variables in. These are more powerful, because they let you express general rules about things. Type this one in: loves( maggie, X ) :- likes( X, money ). This means "Maggie, anyone who likes money". 17. Now try asking loves( maggie, X ). then adding likes( cecil, money ). and asking again loves( maggie, X ). 18. You can of course still ask questions with variables in any or both of the two positions. Prolog will always answer with all the inferences it can make. So you can also ask loves( X, Y ). loves( X, cecil ). and get answers drawn from all your facts. Notice that it does not matter at all which variable names you use. In particular, it does not matter whether the names are the same as those in any of your facts. The questions loves( X, Y ). loves( Y, X ). loves( Who, SomeoneElse ). loves( Potrzebie, Ftang ). will have exactly the same answers. 19. Reasoning with conditional facts which contain variables can be very powerful. The same conditional fact can be used for lots of inferences. Here is another conditional fact with a variable. Type it in. is_a( X, mortal ) :- is_a( X, human ). then add: is_a( mary, human ). is_a( dai, human ). is_a( john, human ). Finally, see what Prolog can say in answer to: is_a( X, mortal ). This is exactly the same reasoning as in the classical syllogism about Socrates being mortal if he is a man. 20. Before we go on to more complicated inferences, a note on what words we're allowed in facts. Unfortunately, Prolog won't allow the word "is" to be used, so a fact like "is(mary,human)." is illegal. You'll learn why when you come on to the subject of arithmetic in Prolog. For the moment, just accept it as one of those annoying historical accidents that are so common in computing. By the way, don't confuse underline (_) with minus (-). You can't use minus instead of underline in names. 21. The conditional part of a fact can have a comma in it, which is Prolog's way to say "and". This means that its truth depends on more than one condition. For example try this: loves( anne, P ) :- likes( P, money ), likes( P, horses ). likes( mark, horses ). likes( mark, money ). and then ask loves( X, Y ). Notice that although Prolog will tell you that Anne likes Mark, it won't tell you that Maggie likes Mark. Although it knows Cecil likes money, it doesn't know whether he likes horses. So it assumes he doesn't. This means one of the parts of the condition is false, and so the whole condition is false. 22. Here's another example. Enter the facts: dislikes( mary, X ) :- eats( X, garlic ), eats( X, curry ). eats( romeo, garlic ). eats( chris, curry ). eats( dai, garlic ). eats( dai, curry ). and then ask dislikes( mary, X ). 23. So, for a condition which uses and, for it to be true, the parts either side of the "and" (the comma) must both be true. That's what you'd expect. 24. We've seen that rules can use and. So can questions, for example: loves( mary, john ), loves( anne, mark ). This can be understood as Is it true that Mary, John and Anne, Mark? 25. Questions can contain variables and "and"s together. This is more interesting. For example, you can ask the question eats( X, garlic ), eats( X, curry ). Prolog interprets the X to refer to the same thing in both cases. That's sensible: you wouldn't expect one X to be about one person (Mary, say) and the second about a different person (e.g. John). So this question asks Prolog to find one person who both (a) eats garlic, and (b) eats curry. 26. Similarly, you could try loves( X, cecil ), loves( X, nick ). Here, you're asking for one person (call them X) who (a) loves Cecil, and (b) loves Nick. 27. The questions in the last two sections both work exactly like the condition in the rule you saw earlier: dislikes( mary, X ) :- eats( X, garlic ), eats( X, curry ). There, you were saying that Mary dislikes someone (X) if this person (a) eats garlic, and also (b) eats curry. Again, the person must be the same in both cases. In general, "and"s and variables in questions work just like they do after a :- in conditions. We shall have more practice with inference and questions later. But we must now turn to more peripheral matters, like how to print facts, and how to replace one fact by another. 28. Now you're putting in lots of facts, you may like to print them onto paper. You can do this with the Ved command ctcprint which will send the file currently being edited to the lineprinter in the 59 George Street building. Before ending, please read the final sections of this lesson, to clarify what Prolog can't do and how little it understands: 29. "As an able but little known writer has remarked: ''Suppose someone to assert: The gostak distims the doshes. You do not know what this means; nor do I. But if we assume that it is English, we know that 'the doshes are distimmed by the gostak'. We know too that 'one distimmer of doshes is a gostak' . If moreover, the 'doshes are galloons', we know that 'some galloons are distimmed by the gostak'. And so we may go on, and so we often do go on'' ". The quote above is from "The Meaning of Meaning" by C.K.Ogden and I.A. Richards; quoted also by Dr. Miles Breuer in "The gostak and the doshes", an S.F. tale of countries driven to war through the force of incomprehensible slogans. Prolog works at the same level of understanding: manipulating words like "loves" and "joe" without any notion of what they denote. All it does is to apply a few rules of logic. The main one is what's technically called MODUS PONENS: If I have a rule of the form (Q if P) And I know that P is true Then I can deduce that Q is true. 30. This being so, Prolog does not and cannot perform inferences which depend on the meaning of the words it reads. For example, WE find it natural to infer from X is in Y and Y is in Z that X is in Z. However, from X faces opposite Y and Y faces opposite Z we do not infer that X faces opposite Z Knowing nothing of the meaning of "in", Prolog would have no reason to make the first inference rather than the second. We could tell it to, by adding a rule like is_in( X, Z ) :- is_in( X, Y ), is_in( Y, Z ). (Though you'll see later that that will fail for other reasons.) But if we don't, it will not do any such inference for itself. It has NO common sense or general knowledge. 31. This lack of common sense answers a common problem. If you put in a fact such as is_next_to( england, wales ). and ask is_next_to( wales, england ). then Prolog will say "No" - i.e. it isn't. That's because, lacking any knowledge of the meaning of "is_next_to", Prolog doesn't know it's reversible - doesn't know that if A is next to B, then B is next to A. Some relationships aren't: "loves" for example. But Prolog has no way to know which are and which aren't. 32. Finally, a point about ambiguity. If we say "I'm going for lunch in about 10 minutes", the word "about" doesn't mean the same as in "Bill Sykes set about him with a large cudgel", or "the documentary is about the primeval struggle of the limpet for survival in a hostile marine environment". Ambiguities are so common in English that we often fail to notice them. However, when using words in logic programs, we must ensure that each word in our program has one and only one meaning. Otherwise we can get into confusions like is_better_than( nothing, heaven ). is_better_than( ham_sandwich, nothing ). is_much_better_than( X, Y ) :- is_better_than( X, Z ), is_better_than( Z, Y ). from which Prolog could infer that: is_much_better_than( ham_sandwich, heaven ). 33. The incorrect inference above is a well-known joke; it results from the ambiguity of "nothing". Another joke arises from the mis-use of "is": eat( tigers, meat ). is_a( meat, word ). eat( X, Y ) :- is_a( Z, Y ), eat( X, Z ). From these facts, Prolog would deduce that eat( tigers, word ). These are extreme examples, but the same kind of confusion can arise in less extreme cases. So, when programming, make sure that each name in your program refers to one and only one concept.