0

If I wanna get the head of an arbitrary length tuple, I have to code like: head(A) :- tuple(A,B). head(A) :- tuple(A,B,C). head(A) :- tuple(A,B,C,D). .......

Is there a method could represent head(A) :- tuple(A...).

Thanks!

  • Is there a reason why you need to represent tuples with different arity? If not, then you could represent tuples as lists, i.e. the `tuple(d,e,f)` could be represented as `tuple(d, tuple(e, tuple(f, nil))).`. Then, the head predicate could be defined as `head(X) :- tuple(X, _).` – tphilipp May 12 '20 at 22:02

1 Answers1

0

I see two possible solutions:

Avoid terms with different arity

This can be done by changing the representation of tuples:

tuple(a, nil).
tuple(b, tuple(c, nil)).
tuple(d, tuple(e, tuple(f, nil))).

head(X) :- tuple(X, _).

Use External Functions in Clingo

Clingo supports the definition of external functions see Python API and the guide. For instance, we can write functions in Python that can manipulate terms. As a workaround, I suggest using a further constructor for terms, here c.

#script (python)
import clingo
def head(x):
      return x.arguments[0]
#end.

tuple(c(1)).
tuple(c(2,3)).
head(@head(X)) :- tuple(X).
tphilipp
  • 441
  • 2
  • 7