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