Prolog request is doable but returns false

I have a simple example below the prologue program that I wrote that contains an executable query that always returns false for the search.

male(william).
male(harry).

% parent(x,y) - the parent of x is y
parent(william, diana).
parent(william, charles).
parent(harry, diana).
parent(harry, charles).

% Brother - the brother of X is Y
brother(X,Y) :- X\=Y, parent(X,A), parent(Y,A), male(Y).

When I ask if the two constants are brothers, this works fine, but if I try to find the brother of a constant prolog, it returns false.

?- brother(william,harry).
true

?- brother(william,X).
false.

What am I doing wrong?

+4
source share
1 answer

The problem here is X\=Ythis part of lucks is logical purity, since it is \=/2nonmonotonic. Just make him change the order:

brother(X,Y) :- X\=Y, parent(X,A), parent(Y,A), male(Y).

at

brother(X,Y) :-  parent(X,A), parent(Y,A), male(Y), X\=Y.

But a much better solution would be to use dif/2that keeps it clean:

brother(X,Y) :- dif(X,Y), parent(X,A), parent(Y,A), male(Y).
+6
source

Source: https://habr.com/ru/post/1695033/


All Articles