如何识别 prolog 查询指定的算术表达式中涉及的不等式?
我正在编写序言并面临这种情况 - 在我的查询中,我传递了这样的内容:
?- query( 2*X + 3*Y >= 3*Z )
现在,我想做的是让 prolog 程序捕获不等式表达式,以便我可以在变量中包含上述不等式,如下所示:
variable 'Lhs
' 将有 2*X + 3*Y
变量“Rhs
”将具有3*Z
现在我希望所涉及的不等式也被分配到某个地方(在一个名为 Opr?? 的变量中),这样说 Lhs Opr Rhs 之类的意思就像说“2*X + 3*Y >= 3*” Z
”..
这是我正在处理的场景的一般形式。我以某种方式希望所涉及的“不平等”能够被识别出来,以便我稍后可以在我的代码中使用它。
我正在使用 IC 库开发 Eclipse-CLP。
I am working on prolog and faced this scenario -
In my query, I pass something like this:
?- query( 2*X + 3*Y >= 3*Z )
Now, what I would like to do is have the prolog program capture the inequality expression so that I can have the above inequality in variables like below:
variable 'Lhs
' will have 2*X + 3*Y
variable 'Rhs
' will have 3*Z
Now I want the inequality involved to be also assigned somewhere (in a variable called Opr??), so that saying something like Lhs Opr Rhs would mean exactly like saying "2*X + 3*Y >= 3*Z
"..
This is a general form of the scenario that I am working on. I somehow want the "inequality" involved to be identified, so that I can use it later in my code.
I am working on Eclipse-CLP with IC library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 univ/2 运算符:
You can do it with any prolog system, using the univ/2 operator:
您可以使用 univ 来反汇编您的不等式:
这在两个方向上都有效。
You can use univ to disassemble your inequaliy:
This works in both directions.
这应该能够简单地执行以下操作:
这里您需要关心的是解析器在读取查询时使用的操作顺序。查看 eclipse-clp 的 op/3 运算符和swi-prolog 的 op/3 运算符文档。请注意,不等式的优先级高于运算符。这意味着当解析查询 (2*X +3*Y >=3*Z) 时,>= 运算符将成为函子。尝试使用显示谓词来阐明这一点。
This should be able to simply do:
What you need to be concerned here with is order of operations the Parser uses when reading your query. Take a look at the op/3 operator for eclipse-clp and the op/3 operator doc for swi-prolog. Notice the precedence number for inequalities are higher than operators. This means that when the query (2*X +3*Y >=3*Z) is parsed, the >= operator becomes the functor. Try to use the display predicate to make this clear.