问题描述
我正在尝试编写一个谓词 move/3
来处理多种术语,每种术语都在一个单独的文件中定义.我正在尝试为此使用模块,因为文件包含其他应适当命名空间的谓词.
I'm trying to write a predicate move/3
which handles several kinds of terms, each kind of which is defined in a separate file. I'm trying to use modules for this, because the files contain other predicates which should be namespaced appropriately.
所以,我创建了一个包含内容的模块 cat.prolog
:
So, I've created a module cat.prolog
with contents:
:- module(cat, [move/3]).
:- multifile(move/3).
move(cat(C), P, cat(C2)) :-
...
dog.prolog
类似.
和 main.prolog
与:
:- use_module(['cat.prolog'], [move/3]).
:- use_module(['dog.prolog'], [move/3]).
(various predicates that use move/3 and expecting the clauses from all imported modules to be applicable.)
尝试在 SWI-Prolog 中运行:
Trying to run this in SWI-Prolog:
?- ['main.prolog'].
% cat.prolog compiled into cat 0.00 sec, 4,800 bytes
ERROR: Cannot import dog:move/3 into module user: already imported from cat
Warning: /home/edmund/main.prolog:2:
Goal (directive) failed: user:use_module([dog.prolog],[move/3])
% main.prolog compiled 0.00 sec, 10,176 bytes
true.
此时我可以使用 dog:move/3
和 cat:move/3
但不能使用 move/3
.它适用于 cat
情况,但不适用于 dog
情况.
At this point I can use dog:move/3
and cat:move/3
but not move/3
. It works for the cat
case but not the dog
case.
我觉得有一种非常明显的方法可以做到这一点.我已经尝试以多种方式组合模块和导入以及多文件指令,但仍然没有找到它......
I get the feeling there's a really obvious way to do this. I've tried combining modules and imports and multifile directives in many ways and still not found it yet...
推荐答案
multifile/1 语法很简单,但是 文档 缺少一个简单的例子...
The multifile/1 syntax is simple, but the documentation lacks a simple example...
我创建了 3 个 modules 文件:pets.pl
、cat.pl
、dog.pl
.
I created 3 modules files: pets.pl
, cat.pl
, dog.pl
.
:- module(pets, [test/0, move/3]).
:- multifile move/3.
move(A,B,C) :- writeln(pets-move(A,B,C)).
test :- forall(move(A,B,C), writeln(move(A,B,C))).
:- module(cat, []).
:- use_module(pets).
pets:move(A,B,C) :- writeln(cat-move(A,B,C)).
:- module(dog, []).
:- use_module(pets).
pets:move(A,B,C) :- writeln(dog-move(A,B,C)).
注意相关语法 Module:Pred :- ...
在依赖"文件中
Note the relevant syntax Module:Pred :- ...
in the 'dependent' files
?- [cat,dog].
% pets compiled into pets 0.00 sec, 3 clauses
% cat compiled into cat 0.01 sec, 7 clauses
% dog compiled into dog 0.00 sec, 3 clauses
true.
?- test.
Correct to: "pets:test"? yes
pets-move(_G41,_G42,_G43)
move(_G41,_G42,_G43)
cat-move(_G41,_G42,_G43)
move(_G41,_G42,_G43)
dog-move(_G41,_G42,_G43)
move(_G41,_G42,_G43)
true.
?-
这篇关于跨多个模块定义谓词的部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!