我在这里写了一个通用的助手。我认为需要更少的内置函数......
search_facts_by_arg(Functor, Arg, C) :-
current_functor(Functor, Arity),
Arity >= 2, % because of 'value of the other Atom is any value.'
length(Args, Arity),
C =.. [Functor|Args],
clause(C, true),
once((arg(_, C, A), A == Arg)).
呸...
测试:
?- search_facts_by_arg(go, a, C).
C = go(a, t) ;
C = go(g, a) ;
C = go(f, a) ;
false.
编辑:在@false'建议之后,代码可以简化
search_facts_by_arg(Functor, Arg, C) :-
functor(C, Functor, 2),
clause(C, true),
once((arg(_, C, A), A == Arg)).
这是函子/3 文档。基本的内置语句是 Clause/2,这里是另一种简化的方式,使用 univ 构建“模板”头:
search_facts_by_arg(Functor, Arg, C) :-
C =.. [Functor,_,_],
clause(C, true),
once((arg(_, C, A), A == Arg)).