帮助了解 prolog 的子句
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你是对的,PROLOG 中的子句是 Horn 子句。此外,碰巧子句开头的连词,例如:
...(不是 Horn 子句)实际上等价于以下两个单独 Horn 子句:
...因为
已检测到(质子)
和已检测到(电子)
这两个事实都是由身体目标的结合所暗示的。请注意,可能还有其他几种等效方法可以对您想要程序表示的含义进行编码,例如以下(作为示例):
执行目标
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:
...(which is not a Horn clause) is in fact equivalent to the following two separate Horn clauses:
...since both facts
detected(proton)
anddetected(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):
Executing goal
detected(X)
will bindX
to atomproton
, then toelectron
on backtracking.它相当于
You are本质上试图重新定义
,/2
运算符,这是不允许的。您收到什么错误消息?你想说什么?如果
emissionOf(alpha)
和emissionOf(beta)
则Detected(proton)
和Detected(electron)
均为 true > 是真的吗?在这种情况下,您需要将其分成两个子句,如 @sharky 所说:这将为您提供回溯的两种解决方案。我认为这肯定比他的第二个建议更清楚。
It's equivalent to
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)
anddetected(electron)
are true ifemissionOf(alpha)
andemissionOf(beta)
are true? In that case you need to split in into two clauses as @sharky said:This will give you both solutions on backtracking. I think this is definitely clearer then his second suggestion.
据我所知,这是 Prolog 算法固有的。每个陈述的句子必须是Horn 子句。
From what was told to me, it is inherent to Prolog's algorithm. Each stated sentence must be a Horn clause.