Prolog如何回答该查询? [英] How does Prolog answer this query?

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

问题描述

likes(alice, sports).
likes(alice, music).
likes(carol, music).
likes(david,animals).
likes(david,X) :- likes(X,sports).
likes(alice,X) :- likes(david,X).

?- likes(alice,X).

几天来我一直在尝试学习序言,当我尝试这个问题时,我意识到我不完全了解何时实例化和使用变量.最初的目标是:likes(alice , X).之后,要证明的下一个目标是likes(david , X)?然后是likes(X, sports).那么X会变成alice吗?

I've been trying to learn prolog an few days now, and when I attempted this question, I realised that I don't completely understand when the variables are instantiated and used. The initial goal is : likes(alice , X). After that, the next goal to prove is likes(david , X)? Then is it likes(X, sports). Then does X become alice?

另一条路线:

最初的目标是:likes(alice , X).之后,要证明的下一个目标是likes(david , X)?然后X变为sports.然后目标变为likes(david , sports).那我不知道.

The initial goal is : likes(alice , X). After that, the next goal to prove is likes(david , X)? Then X becomes sports. Then the goal becomes likes(david , sports). Then I don't know.

有人可以指出我的想法存在缺陷的地方.

Could someone please indicate where my thinking is flawed.

推荐答案

让我们分解代码.首先,您有一些事实:

Let's break down the code. First, you have some facts:

likes(alice, sports).    % Alice likes sports
likes(alice, music).     % Alice likes music
likes(carol, music).     % Carol likes music
likes(david, animals).   % David likes animals

仅鉴于这些事实,您就可以进行基本查询:

Just given these facts, you can make basic queries:

?- likes(alice, sports).   % Does Alice like sports?
true ;
false.  % no more solutions

是的,爱丽丝喜欢运动(结果是true).爱丽丝喜欢动物吗?

So, yes, Alice likes sports (the result was true). Does Alice like animals?

?- likes(alice, animals).
false.

显然不是.至少,根据我们得到的数据,我们不能证明爱丽丝喜欢动物. (请记住,到目前为止,我们只有事实,但没有任何规则,如下所示.)

Evidently not. At least, according to the data we have, we cannot prove that Alice likes animals. (Remember, we only have the facts so far, but none of the rules, shown below.)

那么,根据事实,爱丽丝喜欢什么?

Well then, what does Alice like, according to the facts?

?- likes(alice, X).
X = sports ;
X = music.

爱丽丝喜欢运动和音乐.

Alice likes sports and music.

现在让我们添加您的规则:

Now let's add in your rules:

likes(david, X) :- likes(X, sports).

这表示如果某人(X)喜欢运动,那么 David喜欢某人(X).

This says that, David likes someone (X) if that someone (X) likes sports.

让我们看看大卫喜欢谁/什么:

Let's see who/what David likes:

?- likes(david, X).
X = animals ;
X = alice ;
false  % no more solutions

所以大卫喜欢动物(因为事实如此),大卫喜欢爱丽丝(因为我们有一条规则说,如果X喜欢运动,大卫喜欢X,爱丽丝喜欢运动).

So David likes animals (because a fact says so), and David likes Alice (because we have a rule that says David likes X if X likes sports, and Alice likes sports).

您的其他规则:

likes(alice, X) :- likes(david, X).

说,如果David喜欢同一个人(X)爱丽丝喜欢某人(X).

添加新规则后,让我们看看爱丽丝喜欢谁/什么:

With the new rules added, let's see who/what Alice likes:

?- likes(alice, X).
X = sports ;
X = music ;
X = animals ;
X = alice ;
false

爱丽丝喜欢体育和音乐(因为事实如此).爱丽丝喜欢动物是因为大卫喜欢动物,而规则说如果大卫喜欢X,那么爱丽丝就喜欢X.爱丽丝显然也喜欢自己,因为根据第一个规则,我们证明了大卫喜欢爱丽丝.根据第二条规则,爱丽丝喜欢大卫喜欢的任何人.因此,爱丽丝喜欢爱丽丝.

Alice likes sports and music (because the facts say so). Alice likes animals because David likes animals and the rule says that if David likes X, then Alice likes X. Alice also evidently likes herself because, according to the first rule, we showed that David likes Alice. According to the second rule, Alice likes anyone that David likes. Therefore, Alice likes Alice.

您可以通过运行trace.逐步执行,然后执行查询.

You can get the step-by-step execution by running trace. then execute your query.

请注意,使用这些简单的规则和事实,这是相当不错的行为.在更复杂的情况下,您必须小心以相同的方式命名规则和事实,因为这会导致无限的逻辑循环.

Note that this is fairly well behaved with these simple rules and facts. In more complex cases you have to be careful about naming your rules and your facts the same way because it can lead to infinite logical loops.

这篇关于Prolog如何回答该查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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