I'm writing a "solver" using the SICStus Prolog attributed variables interface:
:- module(attach2, [attach2/2]). :- use_module(library(atts)). :- attribute att/2. attach2(X,Y) :- put_atts(X,att(X,Y)), put_atts(Y,att(Y,X)). verify_attribute(_,_,[]). attribute_goal(V,(true,G,true,G,true)) :- get_atts(V,att(X,Y)), G = attach2(X,Y).
Sample query:
| ?- attach2(X,Y), copy_term(X,Xc,Xcc), copy_term(Y,Yc,Ycc), copy_term(X+Y,XYc,XYcc). Xcc = attach2:(true,attach2(Xc,_A),true,attach2(Xc,_A),true), Ycc = attach2:(true,attach2(Yc,_B),true,attach2(Yc,_B),true), XYc = _C+_D, XYcc = (attach2:attach2(_C,_D),attach2:attach2(_D,_C)), attach2(X,Y), attach2(Y,X) ? ; no
If I change attribute_goal/2
to ...
attach2(X,Y) :- put_atts(X,att(X,Y)), put_atts(Y,att(X,Y)).
... the answer get better:
| ?- attach2(X,Y), copy_term(X,Xc,Xcc), copy_term(Y,Yc,Ycc), copy_term(X+Y,XYc,XYcc). Xcc = attach2:(true,attach2(Xc,_A),true,attach2(Xc,_A),true), Ycc = attach2:(true,attach2(_B,Yc),true,attach2(_B,Yc),true), XYc = _C+_D, XYcc = attach2:attach2(_C,_D), attach2(X,Y) ? ; no
My conclusion from this:
copy_term/3 collects goals from
attribute_goal/2
and if it encounters more than one attributed variable, then it removes trivialtrue
and duplicate goals.
It this essentially right?