I’m working on code generation that at some point needs to save the outstanding CHR constraints and other attributed goals. copy_term/3 covers most of this need, however CHR constraints are not included (attribute_goals//1 in CHR modules is a noop, I assume because it makes no sense to selectively preserve constraints tied to a particular variable). I tried to use findall/3 with ‘$enumerate_constraints’/1 to collect all constraints currently in store, however findall/3 replaces constrained variables with fresh copies. Is there any way to get all constraints in the store in a way similar to copy_term/3 's Goals
?
For now I settled for manually collecting answers of $enumerate_constraints/1 using memberchk_eq/2 to filter out already collected ones. Quadratic complexity but with <5 constraints each time it doesn’t really matter.
I do not fully understand what you seek. When I answered a StackOverflow question using CLP(FD) with gtrace/0 it did show the constraints.
Please don’t ask me how gtrace accessed the constraints, I have no clue.
HTH
Thing is I don’t want to see the constraints in the debugger, I want my macro to retrieve them and include in the generated code.
The way to get all the CHR constraints of a type are:
a) use enumerate_constraints like you did, though it is slow, linear in the total size of the constraint store.
b) Write a CHR constraint that matches for you, and gives the result in an output variable.
It’s a bit tedious:
:- chr_constraint collect_x/2.
:- chr_constraint collected_x/1.
x(X), collect_x([], _Xs) ==> collected_x(X).
collect_x(X0, Xs) \ collected_x(X) <=> collect_x([X|X0], Xs).
?- x(1), x(2), collect_x([], Xs).
This tedium is less if you stay within the CHR realm. Then all those x/1 can just exist and do their thing, and you don’t need to pull them out into a single Prolog variable.
thanks for the awesome information.