answer-set-programmingclingo

Clingo: Can I match multiple variables (sorta like varargs)?


I am looking for something like this:

g(X, ...Y) :- f(X, ...Y) .

which will be a syntactic sugar for:

g(X, Y) :- f(X, Y) .
g(X, Y, Z) :- f(X, Y, Z) .
g(X, Y, Z, Z1) :- f(X, Y, Z, Z1) .
%...and so on

is there any way to provide some such syntactic sugar, just I have to type less?


Solution

  • No, there is no such thing for arguments of predicates. But you could use functions instead.

    f((1)).
    f((1,2)).
    f((1,2,3)).
    f((1,2,3,4)).
    g(X) :- f(X).
    

    The key here is that f has only one parameter which is a tuple. And X refers to the whole tuple. You can then still access single elements of specific tuples using h(X) :- g((,,X)).

    Remarks: I think in general it is a problem with your datastructures that you have f/n with an unknown number of arguments. You can not write rules about these facts as you do not know the meaning or number of arguments. If you want to store e.g. a list of [a,b,c,d,e], use something like:

    f(id1,a).
    f(id1,b).
    f(id1,c).
    f(id1,d).
    f(id1,e).
    

    These facts can then be used with the : operator to generate sets etc...