2

I've got a rule in my Prolog program called blanket(X,Y) which checks if X is in a certain kind of set of Y which consists of:

  • Y's parents
  • Y's children
  • Y's coparents

I've defined it as follows :

blanket(Y,X) :- different(Y,X),parent(Y,X);child(Y,X);coparent(Y,X).

However, this doesn't work as I expect. It correctly identifies X's parents, children, and coparents (if any) but it lists itself as both a parent and a coparent, which I do not want. Is it possible to set some kind of order, so that the different(Y,X) evaluates first and independently? I tried something like : different(Y,X),(parent(Y,X);child(Y,X);coparent(Y,X))., of course, but that yields a syntax error as I"m still quite unfamiliar with the language.

Any suggestions would be greatly appreciated.

EDIT: Here are the child, different, and coparent relations:

child(X,Y) :- parent(Y,X).

different(X,Y) :- not(X == Y).

coparent(Y,X) :- parent(Y,Z),parent(X,Z).

For completeness.

Marc
  • 3,683
  • 8
  • 34
  • 48
filpa
  • 3,651
  • 8
  • 52
  • 91

2 Answers2

0
blanket(Y,X) :- different(Y,X), parent(Y,X).
blanket(Y,X) :- different(Y,X), child(Y,X).
blanket(Y,X) :- different(Y,X), coparent(Y,X).
NotAUser
  • 1,436
  • 8
  • 12
  • That has the same effect, unfortunately! – filpa Jan 30 '13 at 14:59
  • Not sure what the question is then. Can you give an example of query that returns a wrong answer? – NotAUser Jan 30 '13 at 15:16
  • Actually, your definition of different is wrong. Try different(X,Y) :- X \= Y. – NotAUser Jan 30 '13 at 15:17
  • Although I had to revert to my old definition of `blanket` above, that works! Thank you very much. If it's not too much trouble, could you please clarify why `not(X == Y)` would not work? – filpa Jan 30 '13 at 15:22
  • `not(X==Y)` is true, while `X\=Y` is false. The former checks whether X and Y are equal, the latter checks if they are unifiable. The choice of which one to use pretty much depends on what you want to do, but most of the time I use `X=Y` and `X\=Y`. In your case, `not(X==Y)` is always true because the two variables are not unified with constants. The whole thing should work if you move different as you previously defined it after the other condition. – NotAUser Jan 30 '13 at 15:26
0

The problen is your definition of coparent. It is the statement that allows return itself as a valid result.

I suggest you redefine it, by example as:

coparent(Y,X) :- parent(Y,Z),parent(X,Z), X \= Y.

in this way, you can simply:

blanket(Y,X) :-parent(Y,X);child(Y,X);coparent(Y,X).

Of course, you can keep definition of "coparent" and only modify blanket as:

blanket(Y,X) :-parent(Y,X);child(Y,X);(coparent(Y,X), X\=Y).
pasaba por aqui
  • 3,446
  • 16
  • 40