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

一笑奈何 提交于 2019-12-21 16:21:11

问题


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?


回答1:


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




回答2:


I figured it out after all:

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


来源:https://stackoverflow.com/questions/14639317/how-to-create-a-rule-that-makes-all-relations-symmetric-in-prolog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!