3

我目前正在尝试学习一些 Prolog(使用 ECLiPSe)。有时我会遇到clause/2 谓词,但我不明白它的用途。我阅读了一些参考资料(例如this one),但我仍然不知道在什么情况下它可能有用。有人可以为我提供一个简单的例子或解释吗?

4

2 回答 2

3

这个谓词允许元编程,即推理你的 Prolog 程序。

SWI-Prologclause/2在 ao 中使用explain谓词:

?- explain(member).
"member" is an atom
        Referenced from 12-th clause of pce_meta:pce_to_pl_type/3
lists:member/2 is a predicate defined in
        c:/program files/swi-prolog/library/lists.pl:81
        Referenced from 1-th clause of prolog_dialect:source_exports/2
        Referenced from 1-th clause of pce_config:term_description/2
        Referenced from 1-th clause of pce_config:config_attribute/2
        Referenced from 1-th clause of pce_config:load_config_key/2
        Referenced from 1-th clause of pce_config:term_description/3
        Referenced from 1-th clause of pce_config:current_config_path/1
        Referenced from 4-th clause of persistent_frame:has_specifier/1
true.

并在约束处理规则的实施中。我怀疑它对于进行归纳逻辑编程和各种其他 Prolog 扩展也很有用。

有关 Prolog 中元编程的全面介绍,请参阅Sterling 和 Shapiro的 The Art of Prolog

于 2011-05-10T17:45:53.257 回答
1

一种用途是非常优雅的quine:b

quine :-
    clause(quine, A),
    portray_clause((quine:-A)).

这里找到

这当然是 larsmans 所说的元编程案例

于 2011-05-10T19:08:36.480 回答