为什么Prolog不回溯比较? [英] Why does Prolog does not backtrack on comparison?

查看:127
本文介绍了为什么Prolog不回溯比较?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果比较回溯,我希望以下内容始终正确,对吧?除非它陷入无限循环!

I would expect that the following should always be true if a comparison backtrack, right? unless it goes into an infinite loop!

 ?- Y=2 , random:random(1,3,X), X =\= Y.
 Y = 2,
 X = 1.

 ?- Y=2 , random:random(1,3,X), X =\= Y.
 false. 

但是我弄错了! 总的来说,我的问题是为什么比较不回溯?

but I got false! In general, my question is why doesn't comparison backtrack?

感谢所有答案.我的困惑似乎主要来自于我对随机值不断生成新随机数的期望,因此我混淆了比较不是回溯,相反,原因是随机数只执行一次,然后失败. 我没有意识到某些谓词的半确定性. 但是现在我可以监视了;)这样的情况.再次感谢.

Thanks for all the answers. My confusion seemed to come primarily from my expectation of random keep generating new-random numbers, so I confused that comparison was not backtracking, instead, the reason was that random does its thing only once and fails afterwards. I was unaware of semi-determinate nature of some predicates. But now I can be on a lookout ;) for cases like this. thanks again.

推荐答案

Prolog适用于所谓的Horn-clauses.这意味着每个术语,例如Y=2,都是要回答的问题中的单独目标.每个目标的结果都是是"或否",如果所有目标都回答是",则问题的答案是是".

Prolog works with what are called Horn-clauses. This means that each term individually, for example Y=2, is a separate goal in a question to be answered. The result will be yes or no for each goal, and if all goals answer yes, the question is answered with yes.

您的代码要求如下:

%Is Y equal to 2 or can it be made equal? 
%Yes, Y is a variable and can be assigned the numerical atom 2
Y=2 ,
%Give me a random number between 1 and 3. 
%Is X equal to the random number or can it be made equal? 
%Yes, X is a variable and can be assigned the outcome atom of random:random
random:random(1,3,X), 
%is the term contained within X NOT equivalent to Y?
X =\= Y.

您可以在例如 SWI文档或在立即学习Prolog!/a>.

You can check out existing comparison predicates in for example the SWI documentation or on Learn Prolog Now!.

根据您的实现,您可以使用tracewrite在变量中输出实际的原子,从而使您能够探索程序的实际工作方式.

Depending on your implementation you can use trace and write to output the actual atoms in the variables, allowing you to explore how your program actually works.

?- trace, (Y=2 , random:random(1,3,X), write(X),nl, X =\= Y). %SWI-Prolog

SWI-prolog在线编辑器

无限递归看起来像p(P) :- p(P)..它可以在答案本身内部调用要解决的问题,这意味着要解决p(P),它将检查p(P),并且永远不会结束.

Infinite recursion looks like p(P) :- p(P).. It has a call to the question it is supposed to solve inside the answer itself, meaning to solve for p(P) it will check p(P), which never ends.

仅当Prolog具有选择点时才进行回溯.选择点是决策树中有多种可能的方式满足Prolog当前正在处理的问题的点. Prolog从上到下,然后从左到右.

Backtracking only happens when Prolog has choicepoints. Choicepoints are points where in the decision tree, there are MULTIPLE POSSIBLE WAYS to satisfy the question Prolog is currently processing. Prolog works from top to bottom, then left to right.

想到一个汽车销售员,他被问到哪辆车最适合我?".他有多种可能的汽车可以卖给您,因此他将开始向您展示满足您条件的其他汽车.该车的运输容量需要超过400升吗?所有不满足此条件的汽车都不会作为解决方案.

Think of a cars salesman who gets asked "which car is the best for me?". He has more than one possible car to sell you, so he'll start showing you different cars that meet your criteria. The car needs to have a transport capacity of a volume over 400 liters? All cars that don't satisfy this condition are not presented as a solution.

Prolog进行深度优先搜索,这意味着它会找到所找到的第一个答案,然后检查是否还有其他答案的方法.如果没有结果,答案是否定的.如果至少有一个解决方案,则答案为是,您将获得所有可能的问题答案.这样一来,您只能获得满足您已设定的整个目标链的结果.

Prolog does a depth-first search, meaning it goes down to the first answer it finds, then checks whether there's other ways to answer it. If there is no result, the answer is no. If there is at least one solution, the answer is yes and you get all possible answers for your question. This way you only get results that satisfy a whole chain of goals you've set.

这篇关于为什么Prolog不回溯比较?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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