1

Is there a workaround to make ensure_loaded/1 work in GNU Prolog as it works in many other Prolog systems? The goal is to have a preamble so that the rest of code can use ensure_loaded/1 independent of whether which Prolog system I use.

I tried the following:

:- multifile(term_expansion/2).
term_expansion((:- ensure_loaded(X)),
     (:- atom_concat('<base>\\', X, Y),
     include(Y))).

But the following query doesn't work:

:- ensure_loaded('suite.p').

The path calculation itself is not the issue of the question, but the redefinition of a directive in GNU Prolog. There is another directive that causes problems: meta_predicate/1. The byte code crashes as follows:

GNU Fatal

Bye

4

2 回答 2

2

部分解决方案是:

ensure_loaded(File) :-
    absolute_file_name(File, Path),
    (   predicate_property(_, prolog_file(Path)) ->
        true
    ;   consult(Path)
    ).

它假定文件定义了至少一个谓词,但这是一个合理的假设。但是,似乎没有办法覆盖ensure_loaded/1指令的本机、非功能性定义。一种解决方法是将指令包装在ensure_loaded/1指令中initialization/1。例如:

:- initialization(ensure_loaded('suite.pl')).

因此,这是一个部分解决方案,因为我们实际上是在定义一个ensure_loaded/1 predicate,而不是一个directive

于 2014-06-12T11:48:49.930 回答
0

我目前的猜测是,GNU Prolog 1.4.4 的标准发行版是不可能的。文档说:

GNU Prolog 编译器(第 4.4 节)在每个读入的 Term1 上自动调用 expand_term/2。但是,在当前版本中,编译器仅完成 DCG 转换(即不能使用 term_expansion/2)。要使用 term_expansion/2,必须显式调用 expand_term/2。

我还尝试通过命令行为 term_expansion/2 注入一些 Prolog 代码,但没有成功。尽管工具链具有诸如 -O、-L、-A 之类的选项,它们将选项传递给其他工具。在执行顶层内部发出的咨询/1 的过程中,实际上并没有将 Prolog 文本传递给 pl2wam 的选项。

至少这是我到目前为止的结果。

再见

于 2014-06-12T15:32:50.990 回答