如何设计和加载Prolog源文件,以避免';您不必手动使用_模块吗?

如何设计和加载Prolog源文件,以避免';您不必手动使用_模块吗?,prolog,swi-prolog,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.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).
?-使用\u模块(库(http/http\u-open))。
%库(uri)编译为uri 0.00秒,199772字节
%库(readutil)编译为0.00秒的read_util,10312字节
%库(套接字)编译为套接字0.00秒,6376字节
%库(选项)编译成swi_选项0.00秒,7748字节
%库(base64)编译为base64 0.00秒,9776字节
%库(调试)编译为prolog_调试0.01秒,12056字节
%库(http/http_-open)编译为http_-open 0.01秒,282844字节
对。
?-[openpage]。
%openpage编译0.00秒,1380字节
对。
?-请求('http://www.google.com/",中)。
...
In=(0x9366508)。
如何设置和执行文件,以便在加载自己的代码之前不需要手动加载模块?

尝试:

:- use_module(library(http/http_open)).
在源文件中。请尝试:

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

在您的源文件中。

起初我以为您没有仔细阅读我的问题,但后来我发现您的意思是,在源文件开头的use_模块谓词前面加上“:-”。它奏效了,我想我明白为什么。非常感谢。Kaarel的回答比我的回答早了20秒,这是我检查SWI Prolog IDE对未命中操作符的反应所用的时间。可能您没有使用IDE,这很好地突出了miss指令。起初我以为您没有仔细阅读我的问题,但后来我发现您的意思是,在源文件开头的use_模块谓词前面放“:-”。它奏效了,我想我明白为什么。非常感谢。Kaarel的回答比我的回答早了20秒,这是我检查SWI Prolog IDE对未命中操作符的反应所用的时间。可能您没有使用IDE,因为它很好地突出了miss指令。