【问题标题】:current_predicate/1 does not work with :- dynamic?current_predicate/1 不适用于 :- 动态?
【发布时间】:2015-12-27 22:49:23
【问题描述】:

我在 Prolog 中使用断言定义了一些谓词。

我使用current_predicate/1 是为了知道断言是否已经运行(只需要断言一个值)。

但是,swipl 一直在抱怨:

Warning: The predicates below are not defined. If these are defined
Warning: at runtime using assert/1, use :- dynamic Name/Arity.
Warning: 
Warning: amountOfStudentsInCourseAsserted/2, which is referenced by

所以,我添加了:- dynamic amountOfStudentsInCourseAsserted/2,但不幸的是,这会将谓词添加到current_predicate(谓词)中。因此,如果我使用此动态命名,我将无法再使用current_predicate/1

是否还有另一个谓词,例如 current_predicate/1,但不适用于动态名称?

【问题讨论】:

    标签: prolog swi-prolog prolog-directive-dynamic


    【解决方案1】:

    您可以替代使用 predicate_property/2 内置谓词和 number_of_clauses/1 属性。

    【讨论】:

    • 你确定这应该有效吗?在手册中:Fails for foreign predicates. 确实如此:clause_property(examsListAsserted, number_of_clauses). false
    • 重新阅读我的答案。您的评论引用了不同的内置谓词和不同的属性。
    • 我先尝试使用 predicate_property,然后测试 Clause_property。我复制粘贴了错误的项目。但不幸的是它仍然是相同的结果:predicate_property(examsListAsserted, number_of_clauses). false. 它总是错误的
    • 属性是number_of_clauses/1,而不是number_of_clauses/0。 IE。该属性有一个参数,顾名思义,谓词的子句数。
    【解决方案2】:

    :- dynamic 声明是合适的,因为它会使符号在数据库中为人所知。然后在断言之前尝试匹配(使用适当的参数,在以下示例中被忽略):

    ...
    (  amountOfStudentsInCourseAsserted(_,_)
    -> true
    ;  assert(amountOfStudentsInCourseAsserted(X,Y))
    ),
    ...
    

    【讨论】:

    • s(X)。 ( clause(amountOfStudentsInCourseAsserted(_,_), _) -> ... 会更安全一点
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-21
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 2018-10-31
    • 2015-08-02
    • 2021-02-08
    相关资源
    最近更新 更多