如何在prolog中定义简单规则 [英] How to define simple rule in prolog
问题描述
我想在 prolog 中定义以下规则:
I want to define the following rule in prolog:
当X
渴了,X
应该去酒吧.
之后,我会问 prolog thirsty(X)
并且 prolog 应该还给 go(bar)
after, I would ask prolog thirsty(X)
and prolog should give back go(bar)
我已经尝试过以下方式:
I have tried it on the following way:
go(Y).
thirsty(X) :- go(bar).
但是当我用
thirsty(bob).
结果只会是 true
.有人可以帮助我改变什么才能得到 go(bar)
结果吗?
The result will only be true
. Can someone help me what I have to change to get go(bar)
as the result?
推荐答案
当
X
渴了,X
应该去酒吧.
看这句话的第一部分,when X
is thirsty",显然X
可以拥有一个属性thirsty,所以thirsty
听起来是个不错的谓词(thirsty(X)
表示 X
口渴).
Looking at the first part of this sentence, "when X
is thirsty", there’s clearly a property thirsty that X
can have, so thirsty
sounds like a good predicate (thirsty(X)
means X
is thirsty).
然后有几种方法可以看句子的第二部分,X
应该去吧",下面我会按照复杂度增加的顺序写一些,最后一个是什么我想你想要:
Then there’s a few ways to look at the second part of the sentence, "X
should go to the bar", I’ll write some below in order of increasing complexity with the last being what I think you want:
- 一个是should go to the bar"是
X
的一个属性,在这种情况下,你可以将谓词should_go_to_the_bar
定义为一个谓词,使得should_go_to_the_bar(X)
表示X
应该去酒吧.
- One is that "should go to the bar" is a property of
X
, in which case you can define the predicateshould_go_to_the_bar
as a predicate so thatshould_go_to_the_bar(X)
meansX
should go to the bar.
那么你的程序可以是:
thirsty(bob).
should_go_to_the_bar(X) :- thirsty(X).
?- should_go_to_the_bar(bob).
True
- 另一个是应该去"可能是
X
和the_bar
之间的关系,所以should_go_to(X, the_bar)
意味着X 应该去 the_bar.
- Another is that "should go to" may be a relation between
X
andthe_bar
, so thatshould_go_to(X, the_bar)
means that X should go to the_bar.
那么你的程序可以是:
thirsty(bob).
should_go_to_the_bar(X, the_bar) :- thirsty(X).
?- should_go_to(bob, X).
X = the_bar
- 另一个是应该"可能是像
go_to(X, the_bar)
这样的动作的属性,所以should(go_to(the_bar))
意味着它应该成为 go_to(X, the_bar).
- Another is that "should" may be a property of actions like
go_to(X, the_bar)
, so thatshould(go_to(the_bar))
means that it should be that go_to(X, the_bar).
那么你的程序可以是:
thirsty(bob).
should(go_to(X, the_bar)) :- thirsty(X).
?- should(X).
X = should(go_to(bob, the_bar))
- 最后我要讨论的是,应该"可能是一个人
X
和他们可以做的动作之间的关系,比如go_to(X, the_bar)
,所以should(X, go_to(X, the_bar)
意味着 X 应该 go_to(X, the_bar) 即 X 应该使X 去酒吧"是真的.
- Last I’ll go into is that "should" may be a relation between a person
X
and an action they can do likego_to(X, the_bar)
, so thatshould(X, go_to(X, the_bar)
means that X should go_to(X, the_bar) i.e. X should make it true that "X goes to the bar".
那么你的程序可以是:
thirsty(bob).
should(X, go_to(X, the_bar)) :- thirsty(X).
?- should(bob, Action).
Action = go_to(bob, the_bar)
我认为最后一个解释最接近你想要的.
I think this last interpretation is closest to what you want.
这篇关于如何在prolog中定义简单规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!