Consider the following setup:
:- use_module(library(chr)).
:- chr_constraint
a/1,
b/1.
% really there will be many of these, possibly 100s
% some rules about how to replace as with bs, e.g.,
a(1),a(1) <=> b(2).
% a way to decompose, e.g., b(2) <=> b(1), b(1)
a(X) <=> X #> 1, Y #= X-1 | a(Y), a(1).
b(X) <=> X #> 1, Y #= X-1 | b(Y), b(1).
% except I have to write these for all 100+ variables
I know prolog is capable of meta-programming, and I believe it could be used to generate the x(X)
decompositions above, but I'm not at all sure how to do it. I got close once using =..
to pull apart and put back together calls, but then I had to write something like n(a(2))
everywhere. Ideally, I would write n(a)
once and the correct constraint rule would be added (asserted?):
It would make more sense to be able to do something like
n(X) <=> %... or possibly :-
n(a).
n(b).
% a(X) <=> ... and b(X) <=> ... are added to the "database" of rules
If it were lisp, I could write the macro to do it, I think. And prolog is supposed to be homoiconic like lisp is, so it's theoretically achievable. I just don't know how.
How do I write the decomposer "macro" following something similar to the above style?