在 Prolog 中定义谓词的最佳方法 [英] Best way to define predicate in Prolog

查看:51
本文介绍了在 Prolog 中定义谓词的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Prolog 中定义过程时遇到问题.我有两个源文件,想用它们咨询 Prolog 引擎.这可以通过调用 Prolog 作为 swipl -g ['1.pl','2.pl'] 来完成.

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).

但是,有时规则

predicate2(something):-body

在两个文件中都不存在,并且在对 predicate1 执行某些查询时,我收到错误predicate2"未定义.

does not exist in both of the files and i get a error "predicate2" is undefined, when executing some queries for predicate1.

如果我包含该行

:- dynamic(predicate2/2). 

进入其中一个文件,只有在 predicate/2 未在另一个文件中定义时才会有帮助(否则我会得到类似你真的确定要重新定义 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.

所以,我不知道如何使谓词只是定义".我需要 SWI-Prolog 或 SICStus Prolog 的解决方案.(不幸的是,这些版本没有定义谓词的部分,如visual Prolog)

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)

推荐答案

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

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).

所以你的命令行将是:

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

这篇关于在 Prolog 中定义谓词的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆