Reputation: 65
woman(lisa).
woman(maggie).
parents(lisa , homer, marge).
parents(maggie, homer, marge).
sister(X, Y) :-
X \= Y,
woman(X),
parents(X, P, M),
parents(Y, P, M).
When I ran the following query, I did not expect failure (but got it nonetheless).
?- sister(lisa, X).
false. % expected result: X = maggie
Why am I seeing this behavior?
Upvotes: 2
Views: 288
Reputation: 18726
Stay pure! How? Use prolog-dif for expressing term inequality!
sister(X, Y) :- dif(X, Y), woman(X), parents(X, P, M), parents(Y, P, M).
Sample query:
?- sister(lisa, X).
X = maggie.
For more on dif/2
see also:
Upvotes: 1
Reputation: 726579
Assuming that sister(X,Y)
says that Y
is a sister of X
, you want woman(Y)
, not woman(X)
.
Re-arranging the rule makes it work (demo):
sister(X, Y):-
woman(Y),
parents(X, P, M),
parents(Y, P, M),
X \= Y.
The important thing in this re-arrangement is that the X \= Y
term is moved to a point after both X
and Y
have been bound - X
is given to the rule as input, and Y
is bound through participation in woman(Y)
.
Upvotes: 1