如何生成一个规则来告诉这个简单的事实是双向的? [英] How to generate a rule to tell that this simple facts go both ways?

查看:26
本文介绍了如何生成一个规则来告诉这个简单的事实是双向的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有几个事实:

%bridge(Name,From,To).
bridge(a,1,2).
bridge(b,1,2).
bridge(c,2,3).
bridge(d,3,4).

改为原子

所以这读起来就像桥 A 从区域 1 穿过区域 2".这很简单.然而,反过来也是如此.桥 A 从区域 2 穿过区域 1.这就是我想的原因:

So this reads like "bridge A crosses from zone 1 to zone 2". This is simple. However, the inverse is also true. Bridge A crosses from zone 2 to zone 1. That's why I thought:

bridge(B,S,E):- bridge(B,E,S).

事实是,这弄乱了我的程序,因为每当 swi-prolog 找不到网桥的匹配项时,它就会继续使用网桥规则一遍又一遍地反转它的参数.有什么办法可以阻止这种情况吗?或者有没有其他方法可以创建一个简单的规则?如果我添加所有其他事实(bridge(A,2,1)、bridge(C,3,2) 等),我的程序可以完美运行.

Thing is, this messes up my program because whenever swi-prolog can't find a match for a bridge, it will keep using the bridge rule to invert it's parameters over and over again. Is there any way to stop this? Or is there any other way to create a simple rule? My program works flawlessly if I add every other fact (bridge(A,2,1), bridge(C,3,2), etc).

推荐答案

首先请注意,您似乎在使用变量的地方使用原子来识别桥梁.

First note that you seem to be using variables where you meant to use atoms to identify bridges.

关于您的问题:您可以通过添加其他必需的事实轻松解决此问题,例如:

As to your question: You can easily solve this by adding the additional required facts, for example:

bridge(a, 1, 2).
bridge(a, 2, 1).

bridge(b, 1, 2).
bridge(b, 2, 1).

etc.

然而,正如您直观地注意到的那样,这显然是多余的,您可以使用辅助谓词重构它,例如 bridge_/2,它由两个子句组成:

However, as you already seem to have noticed intuitively, this is clearly redundant and you can refactor it with an auxiliary predicate, say for example bridge_/2, that consists of two clauses:

bridge(B, X, Y) :- bridge_(B, X, Y).
brigde(B, X, Y) :- bridge_(B, Y, X).

bridge_(a, 1, 2).
bridge_(b, 2, 1).
bridge_(c, 2, 3).
etc.

这篇关于如何生成一个规则来告诉这个简单的事实是双向的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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