帮助了解 prolog 的子句

发布于 2024-09-24 08:30:46 字数 401 浏览 7 评论 0原文

emissionOf(alpha).
emissionOf(beta).

detected(proton), detected(electron) :- emissionOf(alpha), emissionOf(beta).

我面临的问题是,由于某些(可能是显而易见的)原因,Prolog 不接受上述代码的最后一行。原因与以下有关:

detected(proton), detected(electron)

如果我尝试

detected(proton)

它就会表现正确。

问题是什么?我试图指出,如果同时存在 α 和 β 发射,那么就会同时检测到质子和电子。

谢谢

emissionOf(alpha).
emissionOf(beta).

detected(proton), detected(electron) :- emissionOf(alpha), emissionOf(beta).

I'm facing the problem that for some (probably obvious) reason Prolog doesn't accept the last line of the above code. The reason has something to do with:

detected(proton), detected(electron)

If I try just

detected(proton)

it behaves correctly.

What is the problem? I am trying to state that if there is both emission of alpha and beta, then there there are detections of both protons and electrons.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心在旅行 2024-10-01 08:30:46

你是对的,PROLOG 中的子句是 Horn 子句。此外,碰巧子句开头的连词,例如:

detected(proton), detected(electron) :- emissionOf(alpha), emissionOf(beta).

...(不是 Horn 子句)实际上等价于以下两个单独 Horn 子句:

detected(proton) :- emissionOf(alpha), emissionOf(beta).
detected(electron) :- emissionOf(alpha), emissionOf(beta).

...因为 已检测到(质子)已检测到(电子) 这两个事实都是由身体目标的结合所暗示的。

请注意,可能还有其他几种等效方法可以对您想要程序表示的含义进行编码,例如以下(作为示例):

emissionOf(alpha).
emissionOf(beta).

detected(X) :- 
  emissionOf(alpha), 
  emissionOf(beta), 
  (X = proton; X = electron).

执行目标 Detected(X) 将绑定 X 到原子 质子,然后回溯到 电子

You are correct, clauses in PROLOG are Horn clauses. Furthermore, it so happens that a conjunction in the head of a clause such as:

detected(proton), detected(electron) :- emissionOf(alpha), emissionOf(beta).

...(which is not a Horn clause) is in fact equivalent to the following two separate Horn clauses:

detected(proton) :- emissionOf(alpha), emissionOf(beta).
detected(electron) :- emissionOf(alpha), emissionOf(beta).

...since both facts detected(proton) and detected(electron) are implied by the conjunction of the body goals.

Note that there may be several other equivalent ways to encode what you intend the program to mean, such as the following (as an example):

emissionOf(alpha).
emissionOf(beta).

detected(X) :- 
  emissionOf(alpha), 
  emissionOf(beta), 
  (X = proton; X = electron).

Executing goal detected(X) will bind X to atom proton, then to electron on backtracking.

软糯酥胸 2024-10-01 08:30:46

它相当于

,(detected(proton),detected(electron)) :- emissionOf(alpha), emissionOf(beta).

You are本质上试图重新定义 ,/2 运算符,这是不允许的。您收到什么错误消息?

你想说什么?如果 emissionOf(alpha)emissionOf(beta)Detected(proton)Detected(electron) 均为 true > 是真的吗?在这种情况下,您需要将其分成两个子句,如 @sharky 所说:

detected(proton) :- emissionOf(alpha), emissionOf(beta).
detected(electron) :- emissionOf(alpha), emissionOf(beta).

这将为您提供回溯的两种解决方案。我认为这肯定比他的第二个建议更清楚。

It's equivalent to

,(detected(proton),detected(electron)) :- emissionOf(alpha), emissionOf(beta).

You are essentially trying to redefine the ,/2 operator, which is not allowed. What error message did you get?

What is it you are trying to say? That both detected(proton) and detected(electron) are true if emissionOf(alpha) and emissionOf(beta) are true? In that case you need to split in into two clauses as @sharky said:

detected(proton) :- emissionOf(alpha), emissionOf(beta).
detected(electron) :- emissionOf(alpha), emissionOf(beta).

This will give you both solutions on backtracking. I think this is definitely clearer then his second suggestion.

岁月染过的梦 2024-10-01 08:30:46

据我所知,这是 Prolog 算法固有的。每个陈述的句子必须是Horn 子句

From what was told to me, it is inherent to Prolog's algorithm. Each stated sentence must be a Horn clause.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文