Prolog - true if binary relation is symmetric

581 views Asked by At

I have binary relations and I want to get result true if all binary relations are symmetric, otherwise false. So far I have this:

married(mary, tom).
married(sam, linda).
married(linda, sam).

spouse(X, Y) :- married(X, Y), married(Y, X).

Where the result is this:

?- spouse(X, Y).
X = sam,
Y = linda ;
X = linda,
Y = sam ;
false.

But in this case I want to get result false because there no binary relation married(tom, mary) (according formule ∀x,y:xRy⇒yRx). And on the other hand, when I have these binary relations:

married(tom, mary).
married(mary, tom).
married(sam, linda).
married(linda, sam).

I want to get result as true because binary relations are symmetric. How can I do that? I'm using SWI-Prolog.

1

There are 1 answers

0
Grzegorz Adam Kowalski On BEST ANSWER

I want to get result true if all binary relations are symmetric, otherwise false.

all_relations_symmetric :- not(some_relations_not_symmetric).
some_relations_not_symmetric :- married(X, Y), not(married(Y, X)).

For your first example:

?- all_relations_symmetric.
false.

For the second one:

?- all_relations_symmetric.
true.