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

Meta-predicate that receives a list of goals as input

我已经实现了以下使用 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).

...