Trying to implement commutativity in Prolog

泪湿孤枕 提交于 2019-12-11 15:09:11

问题


I am trying to create a knowledge base. My problem has terminal/1 and connected/2 and I have defined the following rule:

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

For reasons I now understand (I think), this went into an infinite recursion.

Then, I tried search SO and found this question: Alternative to express "Commutativity" in Prolog? . Based on the answers provided, I tried to change my above fact to the following:

connected(X, Y) :- is_connected(Y, X) /\ is_connected(X, Y).
is_connected(X, Y) :- terminal(X) /\ terminal(Y) /\ connected(X , Y).

However, this doesn't give me the results I was hoping for. If I define a rule connected(t1,t7), I am hoping to get yes if I ask the question connected(t7,t1).


回答1:


I managed to figure this out.

I changed this:

connected(X, Y) :- is_connected(Y, X) /\ is_connected(X, Y).
is_connected(X, Y) :- terminal(X) /\ terminal(Y) /\ connected(X , Y).

to:

is_connected(X, Y) :- connected(Y, X); connected(X, Y).

As it turns out, I also need to be asking the corrected question. Instead of asking connected(X,Y), I will now be asking is_connected(X,Y).



来源:https://stackoverflow.com/questions/15328936/trying-to-implement-commutativity-in-prolog

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