concurrency - 接收目标列表作为输入的元谓词

标签 concurrency prolog swi-prolog meta-predicate

我已经实现了以下使用 SWI-Prolog 引擎同时枚举两个或多个可回溯谓词的解决方案的元谓词。

enumerate(Gs) :-
   maplist(term_variables, Gs, Xs),
   findall(E,
           ( nth1(I, Gs, G),
             nth1(I, Xs, X),
             engine_create(X, G, E) ),
           Es),
   enumerate_next(Es, Xs).

enumerate_next(Es, Xs) :-
   repeat,
   (   findall(X,
               ( member(E, Es),
                 engine_next(E, X) ),
               Xs)
   ->  true
   ;   !,
       fail ).

使用示例:

?- enumerate([between(1,inf,X), member(Y,[a,b,c,d,e]), append(Z,W,[1,2,3,4])]).
X = 1,
Y = a,
Z = [],
W = [1, 2, 3, 4] ;

X = 2,
Y = b,
Z = [1],
W = [2, 3, 4] ;

X = 3,
Y = c,
Z = [1, 2],
W = [3, 4] ;

X = 4,
Y = d,
Z = [1, 2, 3],
W = [4] ;

X = 5,
Y = e,
Z = [1, 2, 3, 4],
W = [] ;
false.

我的问题是:我应该如何声明这个元谓词?

最佳答案

我觉得应该是

:- meta_predicate enumerate(:).

但是你应该以某种方式处理目标列表的元素:例如

:- module(enumerate,
          [enumerate/1]).

:- meta_predicate enumerate(:).

enumerate(GsDecl) :-
   strip_module(GsDecl,GsM,Gs),
   maplist(term_variables, Gs, Xs),
   findall(E,
           ( nth1(I, Gs, G),
             nth1(I, Xs, X),
             engine_create(X, GsM:G, E) ),
           Es),
   enumerate_next(Es, Xs).

...

关于concurrency - 接收目标列表作为输入的元谓词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67540772/

相关文章:

javascript - 将 Prolog 与 Javascript 结合使用 - Pengines

python - Scrapy并发策略

java - 使用 minimax 和 AB 剪枝同时搜索博弈树。那可能吗?

prolog - Prolog 的序言?

qt - 如何使用 QProcess 获得 SWI-prolog 查询的下一个解决方案?

recursion - 序言中递归的停止条件

prolog - clpfd 中具体化的剩余约束

c++ - 如何将现有的序言文件查阅到 C++ 及其模块中?

scala - Scala 中的并行递归记忆化

java - 什么时候验证连接池中的连接?