在 Prolog 中定义谓词的最佳方法

发布于 2024-12-23 17:08:13 字数 644 浏览 1 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦初启 2024-12-30 17:08:13

您需要使用同名的 ISO 指令声明 predicate2/2 multifile 。因此,在每个文件中,您都可以在顶部或 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 of predicate2/2:

:- multifile(predicate2/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 used predicate2(_). So which arity do you want?

¢好甜 2024-12-30 17:08:13

在 SWI Prolog 中您可以避免该错误。使用 ISO 内置更改系统行为

:- set_prolog_flag(unknown, Choice).

选择是(失败、警告、错误)之一。

所以你的命令行将是:

swipl -g “set_prolog_flag(unknown,fail),['1.pl','2.pl']."

另一种可能性:定义一个假过程

swipl -g “assert(predicate2(_):-fail),['1.pl','2.pl']."

HTH

In SWI Prolog you can avoid the error. Change the system behaviour using the ISO builtin

:- set_prolog_flag(unknown, Choice).

The Choice is one of (fail,warning,error).

So your command line will be:

swipl -g “set_prolog_flag(unknown,fail),['1.pl','2.pl']."

Another possibility: define a fake procedure

swipl -g “assert(predicate2(_):-fail),['1.pl','2.pl']."

HTH

一刻暧昧 2024-12-30 17:08:13

显然,Prolog 无法回答我们提出的任何问题。例如,如果我们
问唐纳德是不是鸭子

?- isDuck(Donald) .
! ----------------------------------------
! Error 20 : Predicate Not Defined
! Goal : isDuck(_17610)

Prolog 会回答说它不知道“某事”
是或不是鸭子。正在发生的是逻辑谓词
isDuck/1 未定义(Predicate Not Defined),因此 Prolog 未定义
能够检查“某物”是否是鸭子。正如我们将看到的
之后,即使 Prolog 不知道鸭子是什么,也可以
教它如何区分什么是鸭子和什么不是鸭子。
正如我们之前所说,Prolog 是一种对话语言。在对话中
Prolog 不仅能够在系统和程序员之间
回答某些问题,但也能够了解它的作用
不知道。为了回答程序员提出的问题,Prolog
检查知识库,其中注册了 Prolog 的所有内容
知道。在 Prolog 会话开始时,知识库保持
基础知识,包括其他事物、概念和
自然数算术的定义。会议期间是
可以增加这个知识库,包括定义和
Prolog 不熟悉的概念(例如定义
鸭子),或者也修改和扩展 Prolog 的定义
知道(例如,包括新的自然算术运算符
数字)。事实和规则表达了知识库。事实和规则
是一阶 Horn 子句的句法表示。所以所有
Prolog 的知识首先(几乎)完全使用
顺序逻辑。这就是为什么Prolog被称为逻辑的原因
语言。 Prolog 中的程序是一组事实和规则,它们表达
一定的知识。


Clearly, Prolog is not able to answer any question we formulate. For instance, if we
ask if Donald is a duck

?- isDuck(Donald) .
! ----------------------------------------
! Error 20 : Predicate Not Defined
! Goal : isDuck(_17610)

Prolog will answer that it does not know anything about if “something”
is or is not a duck. What it is happening is that the logic predicate
isDuck/1 is not defined (Predicate Not Defined), so Prolog is not
able to check if “something” is or is not a duck. As we will see
later, even if Prolog does not know what a duck is, it is possible to
teach it how to distinguish between what is a duck and what it is not.
As we said previously, Prolog is a dialogue language. In the dialogue
between the system and the programmer Prolog is not only able to
answer certain questions, but is also able to learn about what it does
not know. To answer the questions formulated by the programmer, Prolog
checks a knowledge base where everything is registered that Prolog
knows. In the beginning of the Prolog session, the knowledge base keep
a basic knowledge that includes, between other things, concepts and
definitions of natural number’s arithmetic. During the session it is
possible to increase this knowledge base, including definitions and
concepts that Prolog isn't familiar with (for instance the definition
of a duck), or also modifying and extending definitions that Prolog
knows (for instance, including new arithmetic operators for natural
numbers). Facts and rules express the knowledge base. Facts and rules
are syntactic representations of the first order Horn Clauses. So all
the Prolog’s knowledge is expressed using (almost) exclusively first
order logic. This is the reason why Prolog is said to be a logic
language. A program in Prolog is a set of facts and rules that express
certain knowledge.


~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文