通过Prolog中的调用检索从body执行的输出

时间:2015-10-14 11:40:26

标签: lambda prolog meta-predicate

假设我们有一个类似于此的谓词p/2

p('an atom', OutputList) :-
  some_predicate_1,
  some_predicate_2,
  ...
  findall(...,...,OutputList).

p/2做了一些非常复杂的事情,最后将一些结果放在OutputList中。

假设我需要在列表中包含谓词p/2的主体: Body = [some_predicate_1,...,findall(...,...,OutputList)]我希望执行它。

如果我执行call(Body)之类的操作,我该如何检索OutputList

我可以使用其他任何谓词吗?

可能call/1call/2不适合此目的。

1 个答案:

答案 0 :(得分:0)

可能很容易

get_output_list(Predicates, Outputlist) :-
  last(Predicates, findall(_,_,Outputlist)),
  maplist(call, Predicates).

如果findall / 3并不总是最后一个,这应该可行

get_output_list(Predicates, Outputlist) :-
  maplist(call, Predicates),
  member(findall(_,_,Outputlist), Predicates).

如果在Predicates中可能有多个findall,则在maplist / 2之后放置member / 2将获得回溯的所有Outputlist