在 Prolog 中定义谓词的最佳方法
我在 Prolog 中定义过程时遇到问题。 我有两个源文件,想用它们来查询 Prolog 引擎。 这可以通过调用 Prolog 作为 swipl -g “['1.pl','2.pl'] 来完成。
这两个文件都是由用另一种编程语言编写的另一个程序生成的,我无法事先预测文件的确切内容。
问题是,在其中一个文件中始终存在一条规则
predicate1(X):-predicate2(X).
该规则
predicate2(something):-body
,但是,有时两个文件中都不存在
,并且在执行谓词 1 的某些查询时,我会收到错误“谓词 2”未定义。如果我将该行包含
:- dynamic(predicate2/2).
到其中一个文件中,则只有在另一个文件中未定义 predicate/2 时才会有帮助(否则我会得到类似“您真的确定要重新定义 predicate2/2 吗?”的信息。在这里我不这样做不想重新定义某些内容来保存另一个文件中的数据,
所以,我不知道如何使谓词“定义”,我需要一个 SWI-Prolog 或 SICStus Prolog 的解决方案。用于定义谓词的部分,如视觉 Prolog)
I have a problem with defining procedures in Prolog.
I have two source files and want to consult Prolog engine with both of them.
This can be done by invoking Prolog as swipl -g “['1.pl','2.pl'].
Both of the files are generated by another program written in another programming language and i can't predict the exact content of the files beforehand.
The problem is that in one of the files there is always a rule
predicate1(X):-predicate2(X).
But,sometimes the rule
predicate2(something):-body
does not exist in both of the files and i get a error "predicate2" is undefined, when executing some queries for predicate1.
If i include the line
:- dynamic(predicate2/2).
into one of the files it only helps if predicate/2 is not defined in another file (otherwise i get something like "are you really sure you want to redefine the predicate2/2?". And here i don't want to redefine something to save the data from another file.
So,i have no idea how to make the predicate just "defined". I need a solution for SWI-Prolog or SICStus Prolog. (unfortunately the versions do not have a section for defining predicates,like visual Prolog)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要使用同名的 ISO 指令声明
predicate2/2
multifile
。因此,在每个文件中,您都可以在顶部或 predicate2/2 的任何子句之前写入:This,无论您是否有该谓词的子句。
@CapelliC 的建议绝对是一个危险的举动。如果您关闭所有存在错误,您将错过许多合法错误!
例如,您声明了
dynamic(predicate2/2)
但使用了predicate2(_)
。那么你想要哪个数量呢?You need to declare
predicate2/2
multifile
with the ISO directive of same name. So in each of the files, you write at the top, or prior to any clauses ofpredicate2/2
:This, regardless of whether or not you are having clauses for that predicate.
The suggestion by @CapelliC is definitely a dangerous move. If you turn off all existence errors you will miss many legitimate errors!
For example, you declared
dynamic(predicate2/2)
but usedpredicate2(_)
. So which arity do you want?在 SWI Prolog 中您可以避免该错误。使用 ISO 内置更改系统行为
选择是(失败、警告、错误)之一。
所以你的命令行将是:
另一种可能性:定义一个假过程
HTH
In SWI Prolog you can avoid the error. Change the system behaviour using the ISO builtin
The Choice is one of (fail,warning,error).
So your command line will be:
Another possibility: define a fake procedure
HTH
显然,Prolog 无法回答我们提出的任何问题。例如,如果我们
问唐纳德是不是鸭子
Clearly, Prolog is not able to answer any question we formulate. For instance, if we
ask if Donald is a duck