mblitz
mblitz

Reputation: 189

How to create a rule that makes all relations symmetric in Prolog?

What I want is when I define:

marriedTo(martin, annie).

It also makes the following true:

marriedTo(annie, martin).

I have tried the following, but it's (obviously) an infinite loop.

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

How would I do this in Prolog?

Upvotes: 8

Views: 4419

Answers (2)

mblitz
mblitz

Reputation: 189

I figured it out after all:

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

Upvotes: 1

NotAUser
NotAUser

Reputation: 1466

The most simple way to solve it is:

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

Then there are plenty of other ways, implementations and semantics that came up to solve the problem of infinite recursion...

Upvotes: 12

Related Questions