How to express inequality in the prologue?

TL DR: sibling(a,X) fails with a response X = a, but sibling(a,a)fails.


I have the following Prolog file:

children(a, c).
children(a, d).
children(b, c).
children(b, d).

sibling(X, Y) :-
   X \== Y, A \== B,
   children(X, A), children(X, B),
   children(Y, A), children(Y, B).

It seems clear enough to me that two people are brothers and sisters if their parents are the same. In addition, a person is not their sibling.

But when I tried to run some queries in GNU Prolog, I get some strange results:

| ?- sibling(a, b).

true ? a

true

true

yes

This is the intended behavior. aand bare brothers and sisters. There are three results that are a bit strange, but I assume that Prolog has a binding A = c, B = dand A = d, B = c.

| ?- sibling(a, a).

no

I think what it means a, and anot brothers and sisters.

| ?- sibling(a, X).

X = a ? a

X = b

X = a

X = b

X = a

X = b

X = a

X = b

(15 ms) yes

: X = a, , sibling(a,a) , sibling(a,a) !

, , \== Prolog.

?

+4
2

. , , .

sibling(X,Y):- children(X, A), children(X, B),
               children(Y, A), children(Y, B),
               X \== Y, A \== B.
+2

TL; DR: - iso_dif/2 ( iso-prolog , )!


, +1!

, , , : , Prolog , :

  • -
  • , ,
  • / -
  • - , nitty.

, Prolog ( , ) ( , , Prolog ).

, : Prolog , . , : (/) , .

+4

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


All Articles