How to create a rule that makes all relationships symmetrical in Prolog?

What I want when I define:

marriedTo(martin, annie). 

He also does the following true:

 marriedTo(annie, martin). 

I tried the following, but this is an (obviously) infinite loop.

 marriedTo(X,Y) :- marriedTo(Y,X). 

How can I do this in Prolog?

+4
source share
2 answers

I got it:

 marriedTo(X,Y) :- marriedTo(Y,Z), X = Z, !. 
0
source

The easiest way to solve this problem:

 marriedTo(martin, annie). ... married(X,Y) :- marriedTo(X,Y). married(X,Y) :- marriedTo(Y,X). 

Then there are many other methods, implementations, and semantics that have come to the solution of the problem of infinite recursion ...

+8
source

All Articles