Module unify

This module provides a type 1 interface to attributed variables. The trailed state of the at-tributed variable is modelled as key value pairs and can be accessed and modified by the predicates put_attr/3, get_attr/3 and del_attr/2. When an attributed variable with type 1 state gets instantiated a call to the attributed variable unify hooks gets scheduled.

Examples:
?- [user].
foo:attr_unify_hook(L, _) :- write('L='), write(L), nl.
^D
Yes
?- put_attr(X, foo, [X,Y]), put_attr(Y, foo, [X,Y]), [X,Y]=[1,2].
L=[1,2]
L=[1,2]
?- put_attr(X, foo, [X,Y]), put_attr(Y, foo, [X,Y]), X=Y.
L=[_A,_A]

The unify hook attr_unify_hook/2 has to be declared inside the module of the key. The scheduled hook is called after the variable has been instantiated and at the next calls port if the surrounding unification was successful. The hook is allowed to fail or to succeed non-deterministically.

The goals hook attribute_goals/3 has to be optionally declared inside the module of the key. When needed the hook is called only once. If the hook is missing or if it fails a single goal for a put_attr/3 call is generated. The goals are used in the top-level display of answers and they can be retrieved by the call_residue/2 predicate from the module residue.

The following unify predicates are provided.

put_attr(V, K, W):
The predicate assigns the value W to the key K of the variable V. The assignment is automatically undone upon backtracking.
get_attr(V, K, W):
The predicate succeeds for the value W of the key K of the variable V.
del_attr(V, K):
The predicate de-assigns the key K from the variable V. The de-assignment is automatically undone upon backtracking.
K:attr_unify_hook(W, T) (hook):
This predicate has to be implemented as a hook for a key K. It will be called with the value W and the term T after the unification.
K:attribute_goals(V, I, O) (hook):
This predicate has to be optionally implemented as a hook for a key K. It will be called when the goals by the variable V are needed. It should return a list of goals in I. The list uses the end O.

Comments