我已经实现了以下元谓词,它使用 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.
我的问题是:我应该如何声明这个元谓词?