Prolog:停止条件? [英] Prolog: stop condition?

查看:13
本文介绍了Prolog:停止条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常简单的 Prolog 知识库:

Here's a very trivial Prolog knowledge base:

spouse(bill,cheryl).
married(X,Y) :- spouse(X,Y).
married(X,Y) :- spouse(Y,X).

我运行了以下查询.请注意,有时答案是正确的名称(仅),但有时答案是正确的名称和假".

I ran the following queries. Note that sometimes the answer is the correct name (only), but other times the answer is the correct name and "false".

1 ?- married(bill,X).
X = cheryl ;
false.

2 ?- married(cheryl,X).
X = bill.

3 ?- married(X,bill).
X = cheryl.

4 ?- married(X,cheryl).
X = bill ;
false.

有人可以解释这种看似不一致的行为吗?提前致谢.

Can someone explain this seemingly inconsistent behavior? Thanks in advance.

推荐答案

来自 Prolog 的 false 响应意味着 Prolog 有一个选择点可以返回以尝试找到进一步的答案,它找到了不再.您的谓词和事实的设置顺序会影响它是否认为它有更多的选择可供探索.

The false response from Prolog means that Prolog had a choice point to go back to in an attempt to find further answers and it found no more. The order in which your predicates and facts are set up can impact whether it thinks it has more choices to explore.

在给定的情况下:

spouse(bill,cheryl).

married(X,Y) :- spouse(X,Y).
married(X,Y) :- spouse(Y,X).

1 ?- married(bill,X).
X = cheryl ;
false.

2 ?- married(cheryl,X).
X = bill.

3 ?- married(X,bill).
X = cheryl.

4 ?- married(X,cheryl).
X = bill ;
false.

在两个 false 情况下,查询 married/2 由两个 married/2 子句中的第一个满足.一旦满足,Prolog 就会意识到它还有另一个选择(第二个 married/2 子句),并提示您寻找更多.您按下 ;,然后 Prolog 探索第二个(也是最后一个)子句,找不到更多解决方案,然后返回 false.

In the two false cases, the query married/2 is satisfied by the first of two married/2 clauses. Once satisified, Prolog realizes it has another choice to make (the second married/2 clause), and prompts for you to look for more. You press ;, then Prolog explores the second (and final) clause, finds no more solutions, and comes back false.

交换 married/2 子句的顺序,看看会发生什么:

Swap the order of your married/2 clauses and see what happens:

spouse(bill,cheryl).

married(X,Y) :- spouse(Y,X).
married(X,Y) :- spouse(X,Y).

?- married(bill,X).
X = cheryl.

?- married(cheryl,X).
X = bill ;
false.

?- married(X,bill).
X = cheryl ;
false.

?- married(X,cheryl).
X = bill.

正如预期的那样,结果是相反的,因为我们更改了第一个子句满足哪些查询.

As expected, the results are reversed since we've changed which queries are satisfied by the first clause.

false 响应对于初级 Prolog 程序员来说可能看起来不一致,并且感觉"像是一个错误或警告,但它实际上是一个完全正常的 Prolog 响应.Prolog 在尝试寻找解决方案的行为上非常一致,当没有更多选择存在时,将返回 false.如果 Prolog 在找到最终解决方案之前已经用尽了所有其他选择,它会显示解决方案并且不返回 false(如上面的情况,第二个子句是唯一的解决方案).

The false response can appear inconsistent to beginning Prolog programmers and "feel" like an error or warning, but it's actually a perfectly normal Prolog response. Prolog is quite consistent in its behavior of attempting to find solutions and, when no more choices exist, will return false. If Prolog has exhausted all the other choices before it finds the final solution, it displays the solution and doesn't return false (as in the case above in which the second clause is the only solution).

有一种尝试通过使用剪切来清理"false 响应的诱惑.虽然这可能会产生理想的短期结果,但这是有风险的,因为您要从谓词中删除选择点,并且在添加数据和逻辑时可能会消除您真正想要的解决方案.

There is a temptation to try and "clean up" the false responses by using cuts. Although this can have the desired short-term result, it is risky since you are removing choice points from the predicate and as you add data and logic may eliminate solutions which you really want.

因此,在修改的情况下:

Thus, in the modified case:

spouse(bill,cheryl).
spouse(emma,nate).

married(X,Y) :- spouse(X,Y), !.  % If we found the spouse, we're done, no more!
married(X,Y) :- spouse(Y,X).

?- married(bill,X).
X = cheryl.

?- married(cheryl,X).
X = bill.

?- married(X,bill).
X = cheryl.

?- married(X, cheryl).
X = bill.

是的,生活是美好的!但是等等,如果我们这样做会怎样:

Yay, life is good! But wait, what if we do this:

?- married(X,Y).
X = bill,
Y = cheryl.

?-

billcheryl 是唯一的已婚夫妇吗?不...它遗漏了 nateemma.剪切消除了其余的解决方案.

Are bill and cheryl the only married couple? No... it left out nate and emma. The cut eliminated the rest of the solutions.

这篇关于Prolog:停止条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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