Re: mutual rules: if brother(a,b) then brother(b,a)

From: Bart Demoen (bmd_at_cs.kuleuven.ac.be)
Date: 05/21/04


Date: Fri, 21 May 2004 13:31:26 +0200

RMM wrote:
> Two points:
>
> Firstly is the relation symmetric or commutative?

symmetric (I used the word commutative in an earlier post in this
tread - that was wrong as other people pointed out)

>
> To stop infinite looping in search, keep a list of the instantiations
> and guard the above expression as:
>
> brother(X,Y):-
> var(X), % if X is a variable we are doing a search
> brother(Y,X), % is there a solution
> test(brother(Y,X)),!,fail. % we've seen it before so abandon
> brother(X,Y):-brother(Y,X).
>
> when you use the search version of brother/2 e.g. brother(X,fred) make
> sure you assert the solution into the database by writing:
>
> brother(X,Y),assertz(test(brother(X,Y)),...
>
> don't forget to declare
> :-dynamic test/1.
>
> if you might search on Y e.g. brother(fred,Y) then you will need to
> add another suitable clause.

It is not clear to me what you mean: if I add two facts to the clauses
above,

brother(fred,jan).
brother(jan,fons).

and ask the query

?- brother(X,fred),assertz(test(brother(X,fred)).

I get an infinite loop.

Similar for queries like ?- brother(mary, ann).

Could you please elaborate ? Thanks.

Cheers

Bart Demoen