Attribute variables trigger during unification. From the viewpoint of the interpreter attribute variables are simply variables. The main functionality is that binding an attribute variable triggers the hooks that are associated with the attribute variable. If a hook fails the unification fails. If a hook succeeds the binding effects of the hook are kept.Example:
hook(V, T) :-
write('bind '), write(V),
write(' to '), write(T), nl.
A hook is just a closure that takes the attribute variable and the term that is attempted to unify with the attribute variable. It can be compiled with the predicate sys_compile_hook/3. The resulting reference can be recorded and erased like a clause reference. The hooks of a variable can be enumerated via the predicate sys_clause_hook/3.
?- sys_compile_hook(V, hook, R),
recordz_ref(R), V = 99.
bind _A to 99
V = 99.
In contrast to ordinary variables, attribute variables are reordered during unification so that two attribute variables are only unified as a last resort. As a result attribute variables are less often instantiated than ordinary variables. The predicate sys_ensure_serno/1 can be used to force the assignment of a serial number. The serial number of a variable is used in writing and lexical comparison.
The following attribute variable predicates are provided: