变量列表中的变量出现 [英] Variable occurrence in a list of variables
问题描述
考虑一个(元逻辑)谓词 var_in_vars(Var, Vars)
,它接受一个变量 Var
和一个变量列表 Vars
和如果 Var
出现在 Vars
中,则成功.所以我们不需要保证 Var
是一个变量,也不需要保证 Vars
是一个变量列表.
Consider a (meta-logical) predicate var_in_vars(Var, Vars)
which takes a variable Var
and a list of variables Vars
and succeeds if Var
occurs in Vars
. So we do not need to ensure that Var
is a variable, nor that Vars
is a list of variables.
在 ISO Prolog 中表达这一点的最紧凑和最规范的方式是什么?这是 ISO/IEC 13211-1 中的内置程序概述:1995 包括 Cor.2:2012.
What is the most compact and canonical way to express this in ISO Prolog? Here is an overview of the built-ins in ISO/IEC 13211-1:1995 including Cor.2:2012.
?- var_in_vars(V, [U,V,W]).
true.
?- var_in_vars(V, [X,Y,Z]).
false.
推荐答案
一种可能性:
var_in_vars(V, Vs) :- \+ unify_with_occurs_check(V, Vs).
和更短:
var_in_vars(V, Vs) :- \+ subsumes_term(V, Vs).
编辑:未来的读者,请考虑问题的上下文,这是一个特定的紧凑性挑战,涉及在给定情况下 ISO 谓词的表达性.
EDIT: Future readers, please take into account the context of the question, which is a specific compactness challenge involving the expressivity of ISO predicates under given circumstances.
在其他情况下,您可能会从以下定义中受益更多:
In other circumstances, you will likely benefit more from a definition like:
var_in_vars(V, Vs) :-
must_be(list, Vs),
once((member(X, Vs), V == X)).
这篇关于变量列表中的变量出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!