Search code examples
prologiso-prolog

Variable occurrence in a list of variables


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.

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.

Solution

  • One possibility:

    var_in_vars(V, Vs) :- \+ unify_with_occurs_check(V, Vs).
    

    and shorter:

    var_in_vars(V, Vs) :- \+ subsumes_term(V, Vs).
    

    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)).