如何设计和加载我的Prolog源文件,以便我不必手动use_module?

时间:2012-06-15 16:07:42

标签: prolog swi-prolog

我有一个源文件openpage.pl,我在其中调用use_module / 1来“导入”SWI-Prolog的http_open / 3:

use_module(library(http/http_open)).

request(URL, In) :- http_open(URL, In, []),
    copy_stream_data(In, user_output),
    close(In).

加载没有抱怨。但是,尽我所能,我无法运行它中的规则。

?- [openpage].
% openpage compiled 0.00 sec, 1,828 bytes
true.

?- request('http://www.google.com', In).
ERROR: request/2: Undefined procedure: http_open/3
?- use_module(library(http/http_open)).
true.

?- request('http://www.google.com', In).
ERROR: request/2: Undefined procedure: http_open/3
?- make.
% Scanning references for 1 possibly undefined predicates
Warning: The predicates below are not defined. If these are defined
Warning: at runtime using assert/1, use :- dynamic Name/Arity.
Warning: 
Warning: http_open/3, which is referenced by
Warning:    status/2 at /home/dale/sesame_test/prolog/openpage.pl:16
Warning:    request/2 at /home/dale/sesame_test/prolog/openpage.pl:3
Warning:    modified/2 at /home/dale/sesame_test/prolog/openpage.pl:7
true.

?- [openpage].
% openpage compiled 0.00 sec, 616 bytes
true.

?- request('http://www.google.com', In).
ERROR: request/2: Undefined procedure: http_open/3
?- 
[forced] Action (h for help) ? exit

所以在我的下一个会话中,我在加载源文件之前调用了use_module / 1,一切都很好:

?- use_module(library(http/http_open)).
%  library(uri) compiled into uri 0.00 sec, 199,772 bytes
%  library(readutil) compiled into read_util 0.00 sec, 10,312 bytes
%  library(socket) compiled into socket 0.00 sec, 6,376 bytes
%  library(option) compiled into swi_option 0.00 sec, 7,748 bytes
%  library(base64) compiled into base64 0.00 sec, 9,776 bytes
%  library(debug) compiled into prolog_debug 0.01 sec, 12,056 bytes
% library(http/http_open) compiled into http_open 0.01 sec, 282,844 bytes
true.

?- [openpage].
% openpage compiled 0.00 sec, 1,380 bytes
true.

?- request('http://www.google.com/', In).
<!doctype html><html itemscope itemtype="http://schema.org/WebPage">
...
In = <stream>(0x9366508).

如何设置和执行我的文件,以便在加载我自己的代码之前不需要这个加载模块的手动步骤?

1 个答案:

答案 0 :(得分:5)

尝试:

:- use_module(library(http/http_open)).

在源文件中。