2
votes

I'm baffled by the very basics of Prolog.

If I have this knowledge base:

loves(vincent, mia).
loves(marcellus, mia).
loves(pumpkin, honey_bunny).
loves(honey_bunny, pumpkin).

jealous(X, Y) :-
    loves(X, Z),
    loves(Y, Z).

Then I assume

"X is jealous of Y if X loves Z and Y loves Z"

When I run the query jealous(X, Y).

I get

X = Y, Y = vincent
X = vincent,
Y = marcellus
X = marcellus,
Y = vincent
X = Y, Y = marcellus
X = Y, Y = pumpkin
X = Y, Y = honey_bunny

I can see that vincent is jealous of marcellius and marcellius is jealous of vincent, but what do the lines in the form X = Y, Y = vincent tell me please? I'm assuming that when there is a match, the next lines give the values where the query is true, as in

X = Y, Y = vincent
X = vincent,
Y = marcellus
X = marcellus,
Y = vincent

and that having nothing like that below X = Y, Y = marcellus and the others means no match. But X = Y, Y = vincent makes no sense to me. It would make sense if it meant "X is some value Y, let's suppose Y is vincent." But that would not explain X = marcellus, Y = vincent in the result.

Any help understanding this much appreciated.

1

1 Answers

2
votes

what do the lines in the form X = Y, Y = vincent tell me please?

That vincent is jealous of himself :-)

You are right, the output is confusing, it would be clearer if it was X=vincent, Y=vincent, which is equivalent. There is nothing about prolog that forces the strange output form you mentioned, it is an implementation choice of the prolog system, maybe because they want to have very short answers.

If you want to exclude the possibility that someone is jealous of themselves, you need to change your definition of jealousy and demand that X is different from Y, for example:

jealous(X, Y) :-
    loves(X, Z),
    loves(Y, Z),
    X\=Y.