I'm following a Prolog tutorial that asks to define a symmetric relation.
Say for instance you have the following fact in the knowledge base:
colleague(john,bob).
You also want colleague(bob,john) to be true, and likewise for all colleague facts.
The tutorial says to use the following rule...
colleague(X,Y) :- colleague(Y,X).
But there are issues when I do this when using the following query...
| ? - colleague(john,X).
X = bob ? ;
X = bob ? ;
X = bob ? ;
X = bob ? ;
...
| ? - colleague(bob,X).
X = john ? ;
X = john ? ;
X = john ? ;
X = john ? ;
...
I think I understand why this is happening - the rule is infinitely recursive? But I'm having issues fixing it. This is what I want...
| ? - colleague(john,X).
X = bob ? ;
no
| ? - colleague(bob,X).
X = john ? ;
no
I've had a look at other answers to very similar questions on here, but haven't been able to sort it. I've tried putting using a cut at the end of the rule, which seems to perform better but still repeats itself once for the fact that is included in the knowledge base:
colleague(X,Y) :- colleague(Y,X), !.
_
| ? - colleague(john,X).
X = bob ? ;
X = bob ? ;
no
| ? - colleague(bob,X).
X = john ? ;
no
Any ideas what I'm doing wrong?