gpt4 book ai didi

module - SWI-Prolog 中的跨模块 "interface"调用

转载 作者:行者123 更新时间:2023-12-05 09:35:04 42 4
gpt4 key购买 nike

这可能特定于 SWI Prolog 模块系统。

假设我们有三个 Prolog 模块(在 SWI-Prolog 模块系统中):

  • robin(在文件 robin.pl 中)
  • arthur(在文件 arthur.pl 中)
  • helper(在文件 helper.pl 中)。

谓词 robin:robin/0(即模块 robin 中的谓词 robin_hood/0)和谓词 arthur:arthur/0 调用谓词 helper:helper/2(由模块 helper 导出)。

Predicate helper:helper/2 然后应该调用一个 predicate toolshed/1,它不同取决于调用者模块。我希望 helper/2 调用与调用 helper/2 的谓词关联的 toolshed/1 谓词。

在 Java 中,可以将带有 toolshed() 方法的接口(interface)传递给 helper(),这样 helper() 就可以调用接口(interface)并最终得到正确的实现。

我如何在 Prolog 中做到这一点?

calls

示例代码:

罗宾.pl

:- module(robin,
[
robin/0
,toolshed/1
]).

:- use_module(library('helper.pl')).

robin :- helper(friar_tuck,help).

toolshed('a katana made by mastersmith Masamune').
toolshed('an ancient shovel with a sharpened blade').
toolshed('an Uzi 9mm with Norinco markings').

亚瑟.pl

:- module(arthur,
[
arthur/0
,toolshed/1
]).

:- use_module(library('helper.pl')).

arthur :- helper(merlin,help).

toolshed('a slightly musty grimoire').
toolshed('a jar of mandragore').
toolshed('a fresh toadstool').

helper.pl

:- module(helper,
[
helper/2
]).

helper(friar_tuck,help) :-
format("I will help you rout the Sheriff of Nottingham's men!~n"),
setof(X,toolshed(X),Tools),
format("I found these tools: ~q~n",[Tools]),
format("Have at them!~n").

helper(merlin,help) :-
format("I will help you rout Mordred's army!~n"),
setof(X,toolshed(X),Tools),
format("I found these tools: ~q~n",[Tools]),
format("Have at them!~n").

将它们放入导演测试:

testing
├── arthur.pl
├── helper.pl
└── robin.pl

启动swipl,设置库搜索路径并加载arthur.pl:

?- assertz(file_search_path(library,'/home/me/testing')).
true.

?- use_module(library('arthur.pl')).
true.

?- arthur.
I will help you rout Mordred's army!
I found these tools: ['a fresh toadstool','a jar of mandragore','a slightly musty grimoire']
Have at them!
true.

所以这行得通。 toolshed/1 由模块 arthur 导出,并且由模块 helper 可见(并且可调用 unqalified),即使 helper < strong>不导入 arthur.pl(不太确定它是如何工作的,也许属于当前在堆栈上的谓词的所有模块的导出谓词是可见的和可访问的未限定?)。

但是我也无法加载robin.pl:

?- use_module(library('robin.pl')).
ERROR: import/1: No permission to import robin:toolshed/1 into user (already imported from arthur)
true.

好吧,这并不奇怪。但是我怎样才能得到我想要的结果呢?我想看到这个:

?- use_module(library('robin.pl')).
true.

?- robin.
I will help you rout the Sheriff of Nottingham's men!
I found these tools: ['a katana made by mastersmith Masamune','an Uzi 9mm with Norinco markings','an ancient shovel with a sharpened blade']
Have at them!
true.

最佳答案

使用 SWI-Prolog 专有 module-transparent mechanism确实提供了一个可行的选择。但要注意,这种机制不仅被标记为“不推荐由程序员直接使用”。但也有该文档中未提及的其他问题。

在此解决方案中,我们使 helper/2 谓词模块透明:

:- module(helper, [helper/2]).

:- module_transparent(helper/2).

helper(friar_tuck,help) :-
format("I will help you rout the Sheriff of Nottingham's men!~n"),
setof(X,toolshed(X),Tools),
format("I found these tools: ~q~n",[Tools]),
format("Have at them!~n").

helper(merlin,help) :-
format("I will help you rout Mordred's army!~n"),
setof(X,toolshed(X),Tools),
format("I found these tools: ~q~n",[Tools]),
format("Have at them!~n").

其他模块则简化为:

:- module(arthur, [arthur/0]).

:- use_module('helper.pl').

arthur :- helper(merlin,help).

toolshed('a slightly musty grimoire').
toolshed('a jar of mandragore').
toolshed('a fresh toadstool').

和:

:- module(robin, [robin/0]).

:- use_module('helper.pl').

robin :- helper(friar_tuck,help).

toolshed('a katana made by mastersmith Masamune').
toolshed('an ancient shovel with a sharpened blade').
toolshed('an Uzi 9mm with Norinco markings').

然后我们得到:

?- [helper, arthur, robin].
true.

?- arthur.
I will help you rout Mordred's army!
I found these tools: ['a fresh toadstool','a jar of mandragore','a slightly musty grimoire']
Have at them!
true.

?- robin.
I will help you rout the Sheriff of Nottingham's men!
I found these tools: ['a katana made by mastersmith Masamune','an Uzi 9mm with Norinco markings','an ancient shovel with a sharpened blade']
Have at them!
true.

也就是说,这个和其他模块解决方案有几个问题,并且不能很好地扩展到更复杂的情况,因为您正在寻找的功能(特别是,作为一流构造的接口(interface)/协议(protocol))不存在在 Prolog 模块系统和 hacks 中不足(参见例如 https://logtalk.org/blog.html?tag=half+broken+hacks)。

关于module - SWI-Prolog 中的跨模块 "interface"调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66146716/

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