gpt4 book ai didi

module - 跨多个模块定义谓词的一部分

转载 作者:行者123 更新时间:2023-12-04 21:59:17 25 4
gpt4 key购买 nike

我正在尝试编写谓词 move/3它处理多种术语,每种术语都在单独的文件中定义。我正在尝试为此使用模块,因为这些文件包含应该适当命名空间的其他谓词。

所以,我创建了一个模块 cat.prolog内容:

:- 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 中运行它:
?- ['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/3cat:move/3但不是 move/3 .它适用于 cat案例,但不是 dog案子。

我觉得有一种非常明显的方法可以做到这一点。我已经尝试以多种方式组合模块和导入以及多文件指令,但仍然没有找到它......

最佳答案

multifile/1 语法很简单,但是 documentation缺少一个简单的例子......

我创建了 3 个模块文件: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 :- ...在“依赖”文件中
?- [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.

?-

关于module - 跨多个模块定义谓词的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21692835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com