如何处理在序言的目标/查询中传递的方程?
我有这样的场景,其中我在 Prolog 查询中得到一个线性方程,如下所示:
?- myquery( 3X + 5Y = 10, Result).
因此,我的查询有一个方程 3X + 5Y = 10,通常采用 AX + BY = C 的形式,其中 A=3、B=5 和C=10。
现在,在我的序言程序中,我尝试定义一个谓词,该谓词可以接受上面查询中提到的表达式。也就是说,我想以某种方式获取 A、B 和 C 值以及所涉及的运算符(在上述情况下为加运算符),然后将其用于我在程序中定义的逻辑。我想知道如何做到这一点。
更通用地说,问题是如何识别通过目标/查询传递的方程中涉及的常量和运算符?
I have this scenario wherein I get a linear equation in the Prolog query like below:
?- myquery( 3X + 5Y = 10, Result).
So my query has an equation 3X + 5Y = 10, which in general assumes the form AX + BY = C, where A=3, B=5 and C=10.
Now, in my prolog program, I am trying to define a predicate that can take in the expression mentioned in the query above. That is, I somehow want to get A, B and C values and also the operator involved (in the above case the plus operator) stored and then used on the logic that I define withing the program. I am wondering how this can be done.
To be more generic, the question is how do I identify the constants and the operator involved in an equation that is passed on through the goal/query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
SWI-Prolog 有一个约束 库 clp(Q,R) 在符号级别求解这些方程:
Eclipse 肯定会有更多东西 先进的。这些库并不简单、困难...
您感兴趣的是,使用 Prolog 语法作为宿主语言,因此可以应用常用的内置函数来识别变量、常量等。
SWI-Prolog has a constraint library clp(Q,R) that solve at symbolic level these equations:
Eclipse will surely have something more advanced. These libraries aren't simple, tough...
Of interest to you, the Prolog syntax is used, as a host language, so the usual builtins could be applied for identify vars, constants, and the like.
以下文字记录可能具有启发性:
最后一个查询如下:对于给定的
Term
,Term
的第一个arg
是Val1
>,Val1
的函子是F1
,元数为A1
(意思是,它有A1
args - 子部分 - 本身),以及第二个Val1
中的术语的arg
存储在Val12
名称下。澄清一下,Prolog 中的任何符号数据都采用fff(aa,bb,cc,...)
的形式,其中fff
是某个名称,称为函子 (functor) ,并且可以通过arg
调用访问该表达式中的“参数”。这意味着原始表达式
(3*_X + 5*_Y = 10)
实际上在 Prolog 中存储为'='( '+'( '*'(3,_X), '*'(5,_Y)), 10)
。当您到达原子部分(元数为 0 的函子)时,您可以进一步检查它们:编辑:回答您的其他问题(来自评论):
如果您坚持不写出乘法符号
*
明确地,您必须将术语表示为字符串,并分析该字符串。这将是一项更加复杂的任务。编辑:要尝试的另一件事是
=..
谓词,称为“Univ”:The following transcript may prove illuminating:
The last query reads: for
Term
as given, 1starg
ofTerm
isVal1
, the functor ofVal1
isF1
with arityA1
(meaning, it hasA1
args - subparts - itself), and 2ndarg
of the term inVal1
is stored underVal12
name. To clarify, any symbolic data in Prolog is in the form offff(aa,bb,cc,...)
wherefff
is some name, called functor, and the "arguments" in that expression can be accessed through thearg
call.That means that the original expression
(3*_X + 5*_Y = 10)
is actually stored in Prolog as'='( '+'( '*'(3,_X), '*'(5,_Y)), 10)
. When you get to the atomic parts (functors with arity 0), you can check them further:EDIT: to answer your other question (from the comments):
If you insist on not writing out the multiplication sign
*
explicitly, you will have to represent your terms as strings, and to analyze that string. That would be a much more involved task.EDIT: another thing to try is
=..
predicate, called "Univ":例如,您可以使用术语检查谓词:arg/3、functor/3、var/1、(=..)/2 等。
You can for example use term inspection predicates: arg/3, functor/3, var/1, (=..)/2 etc.
您可能想看一下使用术语重写规则实现的符号微分的示例;他们处理这样的表达。
这是《Clause and Effect》一书中的一章(减去 1 页),您可能会觉得有用:
<一href="http://books.google.com/books?id=fbYjjEn6a_4C&lpg=PA69&ots=sphfpOafZc&dq=prolog%20term-rewrite%20symbolic- Differentiation&pg=PA69#v=onepage&q=prolog %20term-rewrite%20symbolic- Differentiation&f=false" rel="nofollow noreferrer">子句和效果 - 第六章:术语重写
另一篇来自《Prolog 的艺术:高级编程技术》
<一href="http://books.google.com/books?id=w-XjuvpOrjMC&lpg=PA439&ots=4Xz3XNF0Mw&dq=programming%20in%20prolog%20s ymbolic%20微分&pg=PA439#v=onepage&q=编程%20in%20prolog%20symbolic%20微分&f=false" rel="nofollow noreferrer">23 方程求解器
在 Prolog 中编程 还有一个关于符号微分的部分 (7.11)。
You might want to take a look at examples of symbolic differentiation implemented using term rewrite rules; they handle such expressions.
Here's a chapter (minus 1 page) from the book Clause and Effect that you might find useful:
Clause and Effect - Chapter Six: Term Rewriting
Another from The art of Prolog: advanced programming techniques
23 An equation solver
Programming in Prolog also has a section (7.11) on symbolic differentiation.