user234159
user234159

Reputation: 928

Don't understand Prolog result

I'm new to Prolog. I have this code:

loves(vincent, mia).
loves(marsellus,mia).
jealous(X,Y):- loves(X,Z), loves(Y,Z).

I queried jealous(vincent,W). But SWI-Prolog gives me W = vincent! Shouldn't it be W = marsellus?

Upvotes: 2

Views: 165

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 727087

This is only the first result that you get. If you you ask Prolog interpreter to give you the next result, you will get marsellus as well.

The problem with your rule is that it does not prohibit X from being jealous of him- or herself. To fix this, add a condition that X must not be equal to Y:

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

Demo.

Upvotes: 3

Related Questions