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
?
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.