这是类似问题的解决方案。查询不是将答案列表表示为包含[V,term]
每个变量条目的列表,而是goal_answers(Goal, Answerp)
找到一对Vars-Terms
。
goal_answerp(Goal, Answerp) :-
term_variables(Goal, Vars),
findall(Vars, Goal, Substs),
Answerp = Vars-Substs.
?- goal_answerp(a(X,1), Ms).
Ms = [X]-[[0],[1]].
?- goal_answerp(a(X,Y), Ms).
Ms = [X,Y]-[[0,1],[1,1]].
[编辑]要以原始格式获取答案,请使用library(lambda)
:
?- goal_answerp(a(X,1), Vs-Dss),
maplist(Vs+\Ds^VDs^maplist(\V^D^[V,D]^true,Vs,Ds,VDs),Dss,VDss).
Vs = [X],
Dss = [[0],[1]],
VDss = [[[X,0]],[[X,1]]].
?- goal_answerp(a(X,Y), Vs-Dss),
maplist(Vs+\Ds^VDs^maplist(\V^D^[V,D]^true,Vs,Ds,VDs),Dss,VDss).
Vs = [X,Y],
Dss = [[0,1],[1,1]],
VDss = [[[X,0],[Y,1]],[[X,1],[Y,1]]].