理解规则 - 错误作为答案 [英] Understanding rules - false as answer

查看:36
本文介绍了理解规则 - 错误作为答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Prolog 的新手,我只是在想为什么这条规则在一个正确的结果之后给我错误的结果.

I am new in Prolog and I was just thinking that why this rule giving me false result after one true.

喜欢(1,香蕉).
likes(1,mango).

test :- likes(1,banana),likes(1,mango).

?- test.  
true;  
false.

我想知道这个错误背后的原因.

I want to know the reason behind this false.

推荐答案

prolog 的工作方式是评估查询直到否定失败.

The way prolog works is by evaluating queries until a fail by negation.

在这里,您已经确定了两个事实:

Here, you've established two facts:

喜欢(1,香蕉). 表示1 喜欢香蕉"

likes(1, mango). 表示1 个喜欢芒果"

likes(1, mango). which says "1 likes mango"

那么你已经建立了一个规则,它基本上评估为:

Then you have established a rule, which basically evaluates as:

left_hand_side :- right_hand_side. left_hand_side if right_hand_side

将规则评估为查询尝试匹配事实,如果可以,则返回 true,如果不能匹配,则返回 false.需要注意的一件重要事情是,如果指定,只要规则评估为 true,prolog 将继续匹配事实.

Evaluation of the rule as a query tries to match facts and returns true if it can, and false if it cannot match. One important thing to note is that, if specified, prolog will continue to match facts as long as rules evaluate to true.

那么让我们逐步完成测试:- likes(1,banana),likes(1,mango).

如果 test 作为查询运行,prolog 首先尝试 likes(1,banana) 这是先前确定的事实,并且是真的.然后,它转到 likes(1,mango) ,这又是一个事实,并且是真的.Prolog 然后到达规则的末尾,并输出 true.

If test is run as a query, prolog first tries likes(1,banana) which is a previously established fact, and is true. Then, it moves on to likes(1,mango) which, again, is a fact, and is true. Prolog has then reached the end of the rule, and outputs true.

此时,如果您不搜索更多匹配项,您可以缩短查询时间,只使用 true.但是,如果您要查找更多(所有)匹配项,请先登录 回溯 并尝试再次评估规则,搜索更多匹配项.

At this point, if you're not searching for more matches you can cut the query short and just have true. However, if you're looking for more (all) matches, prolog backtracks and tries to evaluate the rule again, searching for more matches.

然而,由于您的规则只匹配喜欢香蕉和喜欢芒果",而我们已经匹配了 likes(1,banana),当 prolog 回溯并尝试评估 likes(1,banana)) 再次,因为我们之前已经匹配过它,这次没有另一个事实(换句话说,1 不能喜欢"香蕉不止一次,除非已经定义)匹配.所以这就是 false 的来源.

However, since your rule is only matching "likes banana and likes mango" and we already matched likes(1,banana), when prolog backtracks and tries evaluating likes(1,banana) again, since we already matched it before, this time there is not another fact (in other words, 1 cannot "like" banana more than once, unless that has been defined) to match. So that's where the false comes from.

在您的 prolog 解释器中,您可以通过键入 trace. 然后运行您的查询来跟踪程序的执行.我的跟踪如下:

In your prolog interpreter you may be able to trace the execution of your program by typing trace. then running your query. My trace is given below:

| ?- trace
.
The debugger will first creep -- showing everything (trace)

(1 ms) yes
{trace}
| ?- test.
      1    1  Call: test ? 
      2    2  Call: likes(1,banana) ? 
      2    2  Exit: likes(1,banana) ? 
      3    2  Call: likes(1,mango) ? 
      3    2  Exit: likes(1,mango) ? 
      1    1  Exit: test ? 

true ? ;
      1    1  Redo: test ? 
      2    2  Redo: likes(1,banana) ? 
      2    2  Fail: likes(1,banana) ? 
      1    1  Fail: test ? 

(1 ms) no
{trace}
| ?-

最后一件事要注意:如果我在 true ? 提示下没有按 ;,而是按 ,脚本将仅以 true 结束.

One last thing to note: If, instead of pressing ; at the true ? prompt, had I pressed <ENTER>, the script would have finished with only the true.

很高兴你问了这个问题,因为它让我对 prolog 有了一点点复习,我真的很喜欢它,但很长时间没有使用过.

I'm glad you asked this question 'cause it allowed me a tiny refresher on prolog, which I really like but haven't used in a long time.

这篇关于理解规则 - 错误作为答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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