【问题标题】:Ignore undefined facts in SWI Prolog忽略 SWI Prolog 中未定义的事实
【发布时间】:2018-03-13 15:34:26
【问题描述】:

我正在针对一组生成的事实运行查询,而某些事实可能不存在。发生这种情况时,SWI Prolog 会出错,例如Undefined procedure: 'LongIdent'/4。有没有办法让它简单地让涉及'LongIdent'/4 的目标失败?

【问题讨论】:

  • 如果您只是编写另一个谓词来检查它,如果存在则调用谓词 else 失败,例如:check(LongIdent(....) ). -> false if LongIdent/4 does not exist else call LongIdent/4 这可以吗??
  • 如果这些是动态事实,您应该使用指令:- dynamic('LongIdent'/4).。这告诉 Prolog 它是一个真实的谓词或事实,但可能尚未被断言。
  • @coder 除非我能够将它全局应用到整个文件?
  • @lurker 问题在于事实集可能会改变;对于每个数据文件,它是恒定的,但可能会生成不同的事实。列举它们也有点痛苦......
  • @NathanRingo,假设您按照我的描述定义了一个谓词检查,您唯一需要做的就是首先加载谓词检查定义,然后您将直接调用您的谓词 snot,例如:predicate1(.. .) 但是检查(predicate1(...)) 如果没问题,那么这可以用于谓词文件...

标签: prolog


【解决方案1】:

您可以使用声明为unknown(-Old, +New)unknown/2 更改默认行为,Old 是旧的当前标志,New 是您使用的新标志:

?- unknown(trace,fail).
Warning: Using a non-error value for unknown in the global module
Warning: causes most of the development environment to stop working.
Warning: Please use :- dynamic or limit usage of unknown to a module.
Warning: See http://www.swi-prolog.org/howto/database.html
true.

?- a(1).
false.

但是您会看到警告说明这可能不是一个好主意...

如果您不知道当前/旧标志,您可以像这样使用它:

?- unknown(X,fail).
    Warning: Using a non-error value for unknown in the global module
    Warning: causes most of the development environment to stop working.
    Warning: Please use :- dynamic or limit usage of unknown to a module.
    Warning: See http://www.swi-prolog.org/howto/database.html
    X = trace.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-26
    • 2015-01-13
    • 1970-01-01
    • 2017-02-14
    • 2011-11-11
    • 2011-08-22
    相关资源
    最近更新 更多