公理分辨率
我尝试了解公理解析在序言中的工作原理。
假设我定义了自然数的两个基本运算:
s(term)(代表后继)和
add(term, anotherTerm) .
add 的语义由
add(0, x1) ->; 给出。 x1
add (x1, 0) ->; x1
add(s(x1), y1) -> s(add(x1, y1))
然后,我想求解方程
add (x, add(y, z)) = s(0)
我想一种策略可能是
测试右手是否等式的右边(RHS)等于其左边(LHS)
如果不相等,看看是否可以通过寻找最通用的统一符来找到解决方案
如果没有,那么尝试找到一个可以在这个方程中使用的公理。完成这项工作的策略可能是(对于每个公理):尝试求解方程的 RHS 等于公理的 RHS。如果有解,则尝试解方程的 LHS 等于公理的 LHS。如果成功,那么我们就找到了正确的公理。
最终,如果没有解,并且方程的 LHS 和 RHS 是相同的运算(即相同的签名但操作数不同),则对每个操作数应用该算法,如果为每个操作数找到了解,则找到解
我认为这个(简单的)算法可能有效。 不过,我想知道是否有人有解决此类问题的经验? 有谁知道我在哪里可以找到有关更好算法的文档?
提前致谢
I try to understand how axiom resolution works in prolog.
Let's assume that I define the two basic operations on the natural numbers:
s(term) (stands for successor) and
add(term, anotherTerm).
The semantic of add is given by
add (0, x1) -> x1
add (x1, 0) -> x1
add(s(x1), y1) -> s(add(x1, y1))
Then, I would like to solve the equation
add (x, add(y, z)) = s(0)
I imagine that one strategy could be to
test if the right hand side (RHS) of the equation is equal to its left hand side (LHS)
if not see if a solution can be find by looking for the most general unifier
if not then try to find an axiom which can be used in this equation. A strategy for doing this job could be to (for each axiom): try to solve the RHS of the equation equals to the RHS of the axiom. If there is a solution then try to solve the LHS of the equation equals to the LHS of the axiom. If it succeeds, then we have found the right axiom.
eventually, if there is no solution and the LHS and RHS of the equation are the same operation (i.e. same signature but not same operands), apply the algorithm on each operand and a solution is found if a solution is found for each operand.
I think that this (simple) algorithm may work.
However, I would like to know if anyone has experience solving this kind of problem?
Does anyone know where I can find some documentation about a better algorithm?
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Prolog 程序是谓词的集合。
谓词是子句的集合。
子句的形式
表示“如果
Body
为 true,则Head
为 true”。有一个简写子句形式
相同
,其含义与
true
是始终为 true 的内置函数。回到子句的
Body
部分,Body
是一个目标,可以采用以下形式之一(A
、B
和C
表示任意目标):Prolog 中有一些关于求值顺序(从左到右)和“剪切”(修剪搜索树)的特殊规则,但这只是细节这超出了本简短教程的范围。
现在,要确定
Atom
是否为真,Atom
可以是以下形式之一(X
和Y
表示任意术语):术语本质上是任何语法片段。
这里要注意的关键是 Prolog 没有函数!在函数式语言中,您可以定义一个函数
add(X, Y)
,其计算结果为X
和Y
之和,在Prolog中,您可以定义一个谓词,其头部为add(X, Y, Z)
,如果成功,则将Z
与表示X
之和的项统一起来> 和Y
。鉴于所有这些,我们可以在 Prolog 中定义您的规则,如下所示:
其中我使用
0
表示零 (!),使用s(X)
表示后继X
。考虑评估
add(s(s(0)), s(0), Z)
:将
Z
的所有这些统一放在一起,我们有Z = s (s(s(0)))
。现在,您可能会问“如果子句中有多个头匹配会发生什么”或“如果评估路径失败会发生什么?”,答案是“不确定性”、“回溯”,并且一般来说,阅读Prolog教科书!
希望这有帮助。
A Prolog program is a collection of predicates.
A predicate is a collection of clauses.
A clause has the form
meaning "
Head
is true ifBody
is true".There is a shorthand clause form
which means the same as
where
true
is a builtin that is always true.Going back to the
Body
part of a clause,Body
is a goal which can take one of the following forms (A
,B
, andC
denote arbitrary goals):There are some special rules in Prolog concerning evaluation order (left to right) and "cuts" (which prune the search tree), but that's fine detail which is beyond the scope of this brief tutorial.
Now, to decide whether an
Atom
is true,Atom
can be one of the following forms (X
andY
denote arbitrary terms):A term is, essentially, any piece of syntax.
The key thing to spot here is that Prolog has no functions! Where in a functional language you might define a function
add(X, Y)
which evaluates to the sum ofX
andY
, in Prolog you define a predicate whose head isadd(X, Y, Z)
which, if it succeeds, unifiesZ
with the term denoting the sum ofX
andY
.Given all that, we can define your rules in Prolog as follows:
where I'm using
0
to denote zero (!) ands(X)
to denote the successor ofX
.Consider evaluating
add(s(s(0)), s(0), Z)
:Putting all those unifications for
Z
together, we haveZ = s(s(s(0)))
.Now, you may ask "what happens if more than one head matches in a clause" or "what happens if an evaluation path fails?", to which the answers are "nondeterminism", "backtracking", and, in general, read a Prolog textbook!
Hope this helps.
您正在寻找的内容称为缩小< /a>.它在一些功能逻辑语言中实现,例如 Curry,但在Prolog 本身。
What you are looking for is called narrowing. It is implemented in some functional-logic languages such as Curry, but not in Prolog itself.
Brachmann 和 Levesque 的《知识表示和推理》很好地介绍了这些东西的工作原理。
"Knowledge representation and reasoning" by Brachmann and Levesque gives a fairly good introduction to how these things work.