prepare to add project_attributes/2 and attribute_goals/2

This commit is contained in:
Mark Thom
2019-02-13 14:18:41 -07:00
parent 7edc2567a8
commit f616b4ddfd
14 changed files with 163 additions and 36 deletions

57
src/prolog/lib/dif.pl Normal file
View File

@@ -0,0 +1,57 @@
[:- module(dif, [dif/2]).
:- use_module(library(atts)).
:- use_module(library(control), [(\=)/2]).
:- attribute dif/2.
dif_set_variables([], _, _).
dif_set_variables([Var|Vars], X, Y) :-
put_atts(Var, dif(X, Y)),
dif_set_variables(Vars, X, Y).
verify_dif_attrs([dif(X, Y) | Attrs], Var, [X \== Y | Goals]) :-
( get_atts(Var, +dif(X, Y)) -> true
; put_atts(Var, +dif(X, Y))
),
verify_dif_attrs(Attrs, Var, Goals).
verify_dif_attrs([_ | Attrs], Var, Goals) :-
verify_dif_attrs(Attrs, Var, Goals).
verify_dif_attrs([], _, []).
verify_dif_attrs_no_var([dif(X, Y) | Attrs], [X \== Y | Goals]) :-
verify_dif_attrs_no_var(Attrs, Goals).
verify_dif_attrs_no_var([_ | Attrs], Goals) :-
verify_dif_attrs_no_var(Attrs, Goals).
verify_dif_attrs_no_var([], []).
verify_attributes(Var, Value, Goals) :-
( get_atts(Var, Attrs) ->
( var(Value) -> verify_dif_attrs(Attrs, Value, Goals)
; verify_dif_attrs_no_var(Attrs, Goals)
)
).
% Probably the world's worst dif/2 implementation. I'm open to
% suggestions for improvement.
dif(X, Y) :- ( X \= Y -> true
; term_variables(X, XVars), term_variables(Y, YVars),
( XVars == [], YVars == [] -> false
; dif_set_variables(XVars, X, Y),
dif_set_variables(YVars, X, Y)
)
).
gather_dif_goals(Attrs, Goal, Goal) :-
var(Attrs), !.
gather_dif_goals([dif(X, Y)|Attrs], OldGoal, Goal) :-
( var(OldGoal), !, gather_dif_goals(Attrs, dif(X, Y), Goal)
; !, gather_dif_goals(Attrs, (dif(X, Y), OldGoal), Goal)
).
gather_dif_goals([_|Attrs], OldGoal, Goal) :-
gather_dif_goals(Attrs, OldGoal, Goal).
attribute_goals(X, Goal) :-
'$get_attr_list'(X, Attrs),
gather_dif_goals(Attrs, _, Goal).