Hi all,
I’m trying to find a way to systematically assign meaningful names to (fresh) variables.
The structures whose variables I want to name are “metarules”. It’s not important what those are exactly (they are second-order definite clauses whose literals are enclosed in first-order terms), but I have a predicate that outputs them and this is what they look like:
?- expanded_metarules([chain], [C]).
C = (m(chain, _460200, _460202, _460204):-m(_460200, _460216, _460218), m(_460202, _460216, _460232), m(_460204, _460232, _460218)). [1]
In [1] above, the first three variables {_460200, _460202, _460204} are second order variables that bind to predicate symbols, so I’d like to give them names that are commonly used for predicate symbols, e.g. {P,Q,R}. Remaining variables in [1] are first-order variables and they should take names that are commonly used for first-order variables, e.g. {X,Y,Z} (never minding the capitalisation).
I would like to find a way to print the metarule bound to C, above, as follows:
m(chain,P,Q,R):-m(P,X,Y),m(Q,X,Z),m(R,Z,Y) [2]
Suppose that I have a sensible way to generate appropriate atomic names for second- and first- order variables, such as {P,Q,R} and {X,Y,Z}.
I know that I can do the following:
?- expanded_metarules([chain], [_C]), term_variables(_C, [A,B,C,D,E,F]), write_term(_C, [nl(true), variable_names(['P'=A,'Q'=B,'R'=C,'X'=D,'Y'=E,'Z'=F])]). [3]
m(chain,P,Q,R):-m(P,X,Y),m(Q,X,Z),m(R,Z,Y)
true.
The query in [3] assigns variable names [A,B,C,D,E,F] to the variables in C and then prints them out replaced by the atomic variable names [‘P’,‘Q’,‘R’,‘X’,‘Y’,‘Z’] thanks to the variable_names/1 option.
However, this will only work for my purposes if I know the number of variables in a metarule, such as C, and that is not always the case. More to the point, I’d like to be able to assign arbitrary variable names to the variables in C, before I call write_term/2. Or in other words, I’d like to generate the list of variable names [A,B,C,D,E,F] “on the fly” and according to the number of variables in C.
I tried to be sneaky and call numbervars/1 on the output of term_variables/2 but that doesn’t quite work. I’m omitting the output to reduce clutter. I had a look in the other predicatesin library(varnumbers) but there doesn’t seem to be anything in there that I can use (I use that library a lot but not this time).
So, is it possible to do what I want?
Cheers,
Stassa