如何在prolog中定义简单规则 [英] How to define simple rule in prolog

查看:68
本文介绍了如何在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:

  1. 一个是should go to the bar"是X的一个属性,在这种情况下,你可以将谓词should_go_to_the_bar定义为一个谓词,使得should_go_to_the_bar(X) 表示 X 应该去酒吧.
  1. One is that "should go to the bar" is a property of X, in which case you can define the predicate should_go_to_the_bar as a predicate so that should_go_to_the_bar(X) means X should go to the bar.

那么你的程序可以是:

thirsty(bob).
should_go_to_the_bar(X) :- thirsty(X).

?- should_go_to_the_bar(bob).
True

  1. 另一个是应该去"可能是Xthe_bar之间的关系,所以should_go_to(X, the_bar)意味着X 应该去 the_bar.
  1. Another is that "should go to" may be a relation between X and the_bar, so that should_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

  1. 另一个是应该"可能是像go_to(X, the_bar)这样的动作的属性,所以should(go_to(the_bar))意味着它应该成为 go_to(X, the_bar).
  1. Another is that "should" may be a property of actions like go_to(X, the_bar), so that should(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))

  1. 最后我要讨论的是,应该"可能是一个人 X 和他们可以做的动作之间的关系,比如 go_to(X, the_bar),所以 should(X, go_to(X, the_bar) 意味着 X 应该 go_to(X, the_bar) 即 X 应该使X 去酒吧"是真的.
  1. Last I’ll go into is that "should" may be a relation between a person X and an action they can do like go_to(X, the_bar), so that should(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屋!

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