1.1.5 Knowledge Base 5

Well, we've introduced variables, but so far we've only used them in queries. In fact, variables not only can be used in knowledge bases, it's only when we start to do so that we can write truly interesting programs. Here's a simple example, the knowledge base KB5:

loves(vincent,mia).
loves(marcellus,mia).
loves(pumpkin,honey_bunny).
loves(honey_bunny,pumpkin).
 
jealous(X,Y) :- loves(X,Z),loves(Y,Z).

KB5 contains four facts about the loves relation and one rule. (Incidentally, the blank line between the facts and the rule has no meaning: it's simply there to increase the readability. As we said earlier, Prolog gives us a great deal of freedom in the way we format knowledge bases.) But this rule is by far the most interesting one we have seen so far: it contains three variables (note that X, Y, and Z are all upper-case letters). What does it say?

In effect, it is defining a concept of jealousy. It says that an individual X will be jealous of an individual Y if there is some individual Z that X loves, and Y loves that same individual Z too. (Ok, so jealously isn't as straightforward as this in the real world ...) The key thing to note is that this is a general statement: it is not stated in terms of mia, or pumpkin, or anyone in particular --- it's a conditional statement about everybody in our little world.

Suppose we pose the query:

?- jealous(marcellus,W).

This query asks: can you find an individual W such that Marcellus is jealous of W? Vincent is such an individual. If you check the definition of jealousy, you'll see that Marcellus must be jealous of Vincent, because they both love the same woman, namely Mia. So Prolog will return the value

W = vincent

Now some questions for you, First, are there any other jealous people in KB5? Furthermore, suppose we wanted Prolog to tell us about all the jealous people: what query would we pose? Do any of the answers surprise you? Do any seem silly?


Patrick Blackburn, Johan Bos and Kristina Striegnitz
Version 1.2.5 (20030212)