Prolog "attrvars"

         
/**
* Warranty & Liability
* To the extent permitted by applicable law and unless explicitly
* otherwise agreed upon, XLOG Technologies AG makes no warranties
* regarding the provided information. XLOG Technologies AG assumes
* no liability that any problems might be solved with the information
* provided by XLOG Technologies AG.
*
* Rights & License
* All industrial property rights regarding the information - copyright
* and patent rights in particular - are the sole property of XLOG
* Technologies AG. If the company was not the originator of some
* excerpts, XLOG Technologies AG has at least obtained the right to
* reproduce, change and translate the information.
*
* Reproduction is restricted to the whole unaltered document. Reproduction
* of the information is only allowed for non-commercial uses. Selling,
* giving away or letting of the execution of the library is prohibited.
* The library can be distributed as part of your applications and libraries
* for execution provided this comment remains unchanged.
*
* Restrictions
* Only to be distributed with programs that add significant and primary
* functionality to the library. Not to be distributed with additional
* software intended to replace any components of the library.
*
* Trademarks
* Jekejeke is a registered trademark of XLOG Technologies AG.
*/
/***************************************************************/
/* deref/2 and unify/2 */
/***************************************************************/
/**
* deref(S, T):
* The predicate succeeds in T with the value of the term S.
* A variable is elevated to an attributed variable.
*/
% deref(+Term, -Term)
deref(S, R) :- S = '$ATTR'(V,_), var(V), !, R = S.
deref('$ATTR'(S,_), R) :- !, deref(S, R).
deref(S, S).
/**
* unify(S, T):
* The predicate succeeds if S and T unify
* while maintaining waken goal success.
*/
% unify(+Term, +Term)
unify(V, W) :- var(V), var(W), V == W, !.
unify(V, T) :- var(V), !, V = T.
unify(S, W) :- var(W), !, W = S.
unify(S, T) :-
deref(S, S2),
deref(T, T2),
sys_attr_unify(S2, T2).
% sys_attr_unify(+Term, +Term)
sys_attr_unify('$ATTR'(V,_), '$ATTR'(W,_)) :- V == W, !.
sys_attr_unify(V, T) :- V = '$ATTR'(_,_), !, sys_attr_bind(V, T).
sys_attr_unify(S, W) :- W = '$ATTR'(_,_), !, sys_attr_bind(W, S).
sys_attr_unify(S, T) :- compound(S), compound(T), !,
functor(S,F,N), functor(T,G,M),
F/N == G/M,
S =.. [_|L], T =.. [_|R],
maplist(unify, L, R).
sys_attr_unify(S, T) :- S == T.
% sys_attr_bind(+Term, +Term)
sys_attr_bind('$ATTR'(U,L), '$ATTR'(U,R)) :- !,
U = '$ATTR'(_,_),
sys_attr_list(L, A, B),
sys_attr_list(R, B, []),
'$SEQ'(nothing, A).
sys_attr_bind('$ATTR'(T,L), T) :-
sys_attr_list(L, A, []),
'$SEQ'(nothing, A).
/***************************************************************/
/* dif/2 */
/***************************************************************/
/**
* dif(S, T):
* The predicate posits that S and T are different.
*/
% dif(+Term, +Term)
dif(S, T) :-
sys_attr_solvable(S, T, [], Q), !,
Q \== [],
sys_attr_triage(Q, L, R),
foldl(sys_attr_put, R, L, H),
sys_attr_pack(L, A),
sys_attr_pack(R, B),
sys_attr_latch(H, dif(A,B)).
dif(_,_).
% sys_attr_triage(+Map, -List, -List)
sys_attr_triage([], [], []).
sys_attr_triage([K-V|L], [K|R], [V|S]) :-
sys_attr_triage(L, R, S).
% sys_attr_solvable(+Term, +Term, +Map, -Map)
sys_attr_solvable(S, T, P, Q) :-
deref(S, A), sys_attr_deref(A, P, X),
deref(T, B), sys_attr_deref(B, P, Y),
sys_attr_unifiable(X, Y, P, Q).
% sys_attr_deref(+Term, +Map, -Term)
sys_attr_deref('$ATTR'(V,_), P, R) :- member('$ATTR'(W,_)-S, P), V == W, !,
sys_attr_deref(S, P, R).
sys_attr_deref(X, _, X).
% sys_attr_unifiable(+Term, +Term, +Map, -Map)
sys_attr_unifiable('$ATTR'(V,_), '$ATTR'(W,_), P, Q) :- V == W, !,
Q = P.
sys_attr_unifiable(V, T, P, Q) :- V = '$ATTR'(_,_), !,
Q = [V-T|P].
sys_attr_unifiable(S, W, P, Q) :- W = '$ATTR'(_,_), !,
Q = [W-S|P].
sys_attr_unifiable(S, T, P, Q) :- compound(S), compound(T), !,
functor(S,F,N), functor(T,G,M),
F/N == G/M,
S =.. [_|L], T =.. [_|R],
foldl(sys_attr_solvable, L, R, P, Q).
sys_attr_unifiable(S, T, P, Q) :- S == T,
Q = P.
/***************************************************************/
/* freeze/2 and when/2 */
/***************************************************************/
/**
* freeze(T, G):
* The predicate delays the goal G until nonvar(T).
*/
% freeze(+Term, +Goal)
freeze(S, G) :- S = '$ATTR'(V,_), var(V), !,
sys_attr_add(freeze(S,G), S).
freeze('$ATTR'(S,_), G) :- !,
freeze(S, G).
freeze(_, G) :- G.
/**
* when(C, G):
* The predicate delays the goal G until condition C.
* For the available conditions see the documentation.
*/
% when(+Goal, +Goal)
when(C, G) :-
sys_attr_when(C, D, L), !,
sys_attr_latch(L, when(D, G)).
when(_, G) :- G.
% sys_attr_when(+Goal, -Goal, -List)
sys_attr_when(V, _, _) :- var(V),
throw(error(instantiation_error,_)).
sys_attr_when((A,B), C, L) :- sys_attr_when(A, X, L), !,
C = (X,B).
sys_attr_when((_,B), Y, R) :- !,
sys_attr_when(B, Y, R).
sys_attr_when((A;B), C, M) :- !,
sys_attr_when(A, X, L),
sys_attr_when(B, Y, R),
foldl(sys_attr_put, R, L, M),
C = (X;Y).
sys_attr_when(nonvar(S), C, M) :- !,
deref(S, T), T = '$ATTR'(_,_),
C = nonvar(T), M = [T].
sys_attr_when(ground(S), C, M) :- !,
sys_attr_vars(S, [], P), P \== [],
reverse(P, Q),
sys_attr_pack(Q, T), C = ground(T),
Q = [X|_], M = [X].
sys_attr_when(T, _, _) :-
throw(error(type_error(cond,T),_)).
/***************************************************************/
/* Term Variables */
/***************************************************************/
% sys_attr_vars(+Term, +List, -List)
sys_attr_vars(S, I, O) :-
deref(S, T),
sys_attr_collect(T, I, O).
% sys_attr_collect(+Term, +List, -List)
sys_attr_collect(S, I, O) :- S = '$ATTR'(_,_), !,
sys_attr_put(S, I, O).
sys_attr_collect(S, I, O) :- compound(S), !,
S =.. [_|L],
foldl(sys_attr_vars, L, I, O).
sys_attr_collect(_, I, I).
% sys_attr_put(+Term, +List, -List)
sys_attr_put('$ATTR'(V,_), P, R) :- member('$ATTR'(W,_), P), V == W, !,
R = P.
sys_attr_put(T, P, R) :- T = '$ATTR'(_,_), !,
R = [T|P].
sys_attr_put(_, P, P).
% sys_attr_pack(+List, -Term)
sys_attr_pack([X], T) :- !,
T = X.
sys_attr_pack(L, T) :-
T =.. [f|L].
/***************************************************************/
/* Open Trees */
/***************************************************************/
% '$LATCH'(+Var, +Goal)
'$LATCH'(V, G) :- var(V), !, V = 1, G.
'$LATCH'(_, _).
% sys_attr_latch(+List, +Goal)
sys_attr_latch([S], G) :- !,
sys_attr_add(G, S).
sys_attr_latch(H, G) :-
M = '$LATCH'(_,G),
maplist(sys_attr_add(M), H).
% sys_attr_add(+Goal, +Attr)
sys_attr_add(G, '$ATTR'(_,L)) :-
sys_attr_conc(L, 30, node(_,G,_)), !.
sys_attr_add(_, _) :-
throw(error(resource_error(buffer_overflow),_)).
% sys_attr_conc(+Tree, +Integer, +Tree).
sys_attr_conc(V, _, N) :- var(V), !,
V = N.
sys_attr_conc(node(L, _, R), D, N) :- var(R),
E is D-2, E >= 0,
sys_attr_conc(L, E, N), !.
sys_attr_conc(node(_, _, R), D, N) :-
E is D-1, E >= 0,
sys_attr_conc(R, E, N).
% sys_attr_list(+Tree, -List, +List)
sys_attr_list(V) --> {var(V)}, !.
sys_attr_list(node(L,T,R)) --> [T],
sys_attr_list(L),
sys_attr_list(R).
/***************************************************************/
/* Portray Hook */
/***************************************************************/
% term_portraying(+Term, -Term)
:- multifile(term_portraying/2).
term_portraying(T, R) :- nonvar(T), T = '$ATTR'(R,_).