5

TL;DR: sibling(a,X) succeeds with the answer 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, two person are siblings if their parents are the same. Also, a person is not their own sibling.

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

| ?- sibling(a, b).

true ? a

true

true

yes

This is the intended behavior. a and b are siblings. There are three results, which is a bit weird, but I assume Prolog is binding A = c, B = d and A = d, B = c.

| ?- sibling(a, a).

no

I think this means a and a are not siblings.

| ?- sibling(a, X).

X = a ? a

X = b

X = a

X = b

X = a

X = b

X = a

X = b

(15 ms) yes

This is where I got stuck: It says X = a, which means sibling(a,a) is true, but sibling(a,a) failed in the previous query!

I feel that I'm not understanding what \== actually does in Prolog.

What is happening, and how do I fix this?

false
  • 10,182
  • 12
  • 93
  • 182
parchment
  • 3,749
  • 1
  • 14
  • 29
  • 3
    See [this answer](http://stackoverflow.com/a/8523825/772868). And should you be using GNU, use [that definition of `dif/2`](http://stackoverflow.com/a/20238931/772868). – false Oct 31 '15 at 13:13

2 Answers2

4

TL;DR: Use —or iso_dif/2 (on conforming systems like )!


Good question, +1!

In fact, it's a question I have asked myself, and the answer has to do with : logical purity is a central aspect of what makes Prolog as a language so special, as it enables you to:

  • describe—not prescribe
  • write code that is relational—not just functional
  • think in terms of problems / solutions—not the individual steps of the search process itself
  • operate on a higher level—not get lost in nitty-gritty details

Unlike many other programming languages, Prolog programs have both procedural semantics (defining the execution steps and their order) and declarative semantics (allowing you to state relations that should hold and let the Prolog processor find a proper way of execution by itself).

But, beware: Prolog has some features that, when used, ruin declarative semantics. To prevent this, try to structure your application into two parts: an impure shell for dealing with side-effects (input/output) and a logically pure base which comprises pure monotonic Prolog code.

Community
  • 1
  • 1
repeat
  • 19,449
  • 4
  • 51
  • 152
2

Try moving the inequality to the end of the predicate. Maybe it gives you true because it's not instantiated already.

sibling(X,Y):- children(X, A), children(X, B),
               children(Y, A), children(Y, B),
               X \== Y, A \== B.
leoxs
  • 425
  • 1
  • 5
  • 15
  • 2
    I thought ordering doesn't matter in prolog, but turns out it does. Thank you! – parchment Oct 31 '15 at 05:05
  • @repeat ooh, the capital letter, I don't see that, sorry haha – leoxs Oct 31 '15 at 19:48
  • @parchment: Ordering typically matters if you use *impure* primitives. I recommend you check out the great answer by @repeat. Use `dif/2` for pure disequality, usable in all directions. – mat Nov 13 '15 at 12:25