Prolog编译器返回错误 [英] Prolog compiler return error

查看:155
本文介绍了Prolog编译器返回错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个假设的程序来检查是否存在从A点到B点的路径.

I have this hypothetical program to check if a path exists from point A to B.

/*city rules*/

edge(phx, tuc).
edge(tuc, ccg).
edge(ccg, sf).

connected(C1, C2) :-
   edge(C1, C2).
connected(C1, C2) :- 
   edge(C1, X),
   connected(X, C2).

问题在于它返回true,然后返回false.错误来自哪里?

the problem is it returns true, then false. Where is the false coming from?

推荐答案

让我们看看您从Prolog获得的确切答复!首先,您只有一个true,然后按 SPACE ; ,您最终得到:

Let's look at the exact reply you got from Prolog! First you got a single true and on pressing SPACE or ; you eventually got:

?- connected(phx,sf).
true ;
false.

因此,您得到了true ; false.作为完整答案. ;表示或".因此Prolog本质上说:您的查询connected(phx,sf).true ; false.相同,因此您需要查看整个答案以了解其含义.当true.足够好时,当然有些奇怪.

So you got true ; false. as the complete answer. The ; means "or". So Prolog essentially says: Your query connected(phx,sf). is the same as true ; false. So you need to look at the entire answer to understand what it means. Of course that is a bit odd, when true. would be good enough.

但是首先让我们举一个例子:

But first let's have another example:

?- connected(phx,X).
X = tuc ;
X = ccg ;
X = sf ;
false.

这里Prolog的完整答案是:connected(phx,X).描述了与X = tuc ; X = ccg ; X = fg ; false.相同的一组解决方案,如果省略false,则同样会更短.

Here Prolog's complete answer is: connected(phx,X). describes the same set of solutions as X = tuc ; X = ccg ; X = fg ; false. which again would be shorter if false would be omitted.

那为什么Prolog会写出这个false? Prolog逐步计算答案.它首先向您显示X = tuc,然后等待您将要执行的操作.从某种意义上说,不一次向您展示所有内容有点懒.有时候,Prolog确实知道将没有进一步的答案,在这种情况下,它会直接写一个点:

So why does Prolog write out this false? Prolog computes the answers incrementally. It first shows you X = tuc and waits what you would do. In a sense, it is a bit lazy not to show you everything at once. Sometimes, Prolog does know that there will be no further answer, in that case, it writes a dot directly:

?- member(X,[1,2]).
X = 1 ;
X = 2.

在这里Prolog说:dixi!我说了.

Here Prolog says: dixi! I have spoken.

但是有时并不确定:

?- member(1,[1,2]).
true ;
false.

证明1是成员后,它将停止,否则它将不得不进一步浏览该列表.

After proving that 1 is a member, it stops, otherwise it would have to explore the list further.

因此,此; false.的意思是:在最后一个答案/解决方案之后,Prolog尚不确定是否已探究所有内容.这可能是效率低下的情况,可能表明可以进行某些改进.但是,从不,永远以此为借口在程序中插入剪切.另一个答案是完全不正确的.它是许多错误的根源.在开始使用削减功能之前,您确实应该首先学习Prolog的其他纯粹部分.

So this ; false. means: After the last answer/solution, Prolog was not yet sure that everything has been explored. This might be an inefficiency, it might be an indication that something can be improved. However, never, ever take this as pretext to insert a cut into your program. The cut in another answer is completely malaprop. It is the source for many, many errors. Before starting to use cuts you really should learn the other, pure part of Prolog, first.

BTW:这是connected/2的更好定义,它通过使用 closure/3 来避免无限循环(点击获取定义).

BTW: Here is a better definition for connected/2 which avoids infinite loops by using closure/3 (click on it to get the definition).

connected(C0,C) :-
   edge(C0,C1)
   closure0(edge,C1,C).

这篇关于Prolog编译器返回错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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