Getting started with prolog and I am trying to do some simple exercises, however I got stuck pretty early... What I am trying to 'say' is: mike likes anyone if it's a man or a woman and it is not mike. But the X \= mike seems to be ignored:
man(mike).
man(danny).
man(samuel).
man(henry).
woman(samantha).
woman(jane).
woman(betty).
woman(jenny).
likes(mike, X) :-
man(X);
woman(X),
X \= mike.
mike shows up...
?- findall(X, likes(mike, X), L).
L = [mike, danny, samuel, henry, samantha, jane, betty, jenny].
I assume it's because prolog evaluates the rule man(X) first so mike is true. However if I change it to:
likes(mike, X) :-
X \= mike,
man(X);
woman(X).
I will only get the women.
?- findall(X, likes(mike, X), L).
L = [samantha, jane, betty, jenny].
The question is how to do it then? Thanks!
Note: I am using swi-prolog
man(X); woman(X), X \= mike.is interpreted asman(X) ; (woman(X), X \= mike).. You can change that with parentheses, to group the;:(man(X) ; woman(X)), X \= mike.. That's also one of the problems with your second example. See information on operator precedence. - lurkerlisting(likes)! It shows you the problem immediately. - false