尝试在 Prolog 中实现交换性 [英] Trying to implement commutativity in Prolog

查看:46
本文介绍了尝试在 Prolog 中实现交换性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个知识库.我的问题有 terminal/1connected/2,我定义了以下规则:

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.

然后,我尝试搜索 SO 并发现了这个问题:表达Commutativity"的替代方法;在序言中?.根据提供的答案,我尝试将上述事实更改为以下内容:

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

然而,这并没有给我想要的结果.如果我定义了一个规则 connected(t1,t7),如果我问问题 connected(t7,t1),我希望得到 yes.

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

推荐答案

我设法解决了这个问题.

I managed to figure this out.

我改变了这个:

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

到:

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

事实证明,我还需要提出更正的问题.我现在不再询问 connected(X,Y),而是询问 is_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).

这篇关于尝试在 Prolog 中实现交换性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆