Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 978
mblitz Avatar asked Feb 01 '13 03:02

mblitz


2 Answers

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...

like image 50
NotAUser Avatar answered Nov 08 '22 07:11

NotAUser


I figured it out after all:

marriedTo(X,Y) :- marriedTo(Y,Z), X = Z, !.
like image 1
mblitz Avatar answered Nov 08 '22 07:11

mblitz