假定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作者的幫助下很久以前編寫的。