假定SWI-Prolog(如已标记)。你可以写类似的东西,以我在这个Prolog的编译器Logtalk适配器文件做:
list_of_exports(File, Module, Exports) :-
absolute_file_name(File, Path, [file_type(prolog), access(read), file_errors(fail)]),
module_property(Module, file(Path)), % only succeeds for loaded modules
module_property(Module, exports(Exports)),
!.
list_of_exports(File, Module, Exports) :-
absolute_file_name(File, Path, [file_type(prolog), access(read), file_errors(fail)]),
open(Path, read, In),
( peek_char(In, #) -> % deal with #! script; if not present
skip(In, 10) % assume that the module declaration
; true % is the first directive on the file
),
setup_call_cleanup(true, read(In, ModuleDecl), close(In)),
ModuleDecl = (:- module(Module, Exports)),
( var(Module) ->
file_base_name(Path, Base),
file_name_extension(Module, _, Base)
; true
).
注意这个代码不涉及编码/ 1指令可能存在作为文件的第一项。该代码也是在SWI-Prolog作者的帮助下很久以前编写的。