Attribute variables and custom debug info


I have an identifier that is passed around in various dynamic structures. To help debug, I’d like to have visible various related information, but without filling my program with various debug related predicates – could i use attribute variables, somehow, to centralize the printing of debug information across the many predicates that process the identifier?

any thoughts are much appreciated,


What’s the nature of the identifier? An atomic term? A compound term?

its an atom

Attributed variables associate attributes with variables.

Maybe use the recorded database to associate terms with the information that you to record with the identifier? See For example:

?- recorda(foo, a(1), _).

?- recorda(foo, b(2), _).

?- recorded(foo, T, _).
T = b(2) ;
T = a(1).

Still, is easy to have debug printing messages that are only active when debugging.

Thank you.

What i have in mind is something like an aspectual capability, where i can define a “point-cut” – a location in the code where, when reached, some predicate is called – without including an explicit call at that location.

Key is that all this is defined outside of the program in a separate aspectual-module.

I thought that attribute variables could be a mechanism for this.


A number of solutions are possible but the most appropriated one depends on the details of the code that you want to instrument. I know a few using Logtalk’s categories for hot patching and using the event-driven programming support. The low level SWI-Prolog machinery that is used for tracing also may provide a solution.

1 Like


I added a simple example of Aspect-Oriented Programming in Logtalk reusing the familiar bank transfer example:

Hot patching via a category is used to limit transfer amounts and events are used to generate logging messages. Note that there are other AOP examples in the distribution (e.g. blocks and searching).

I also failed to mention in my previous message SWI-Prolog support for wrapping predicates:

This is really nice, how easily event broadcast can be used for modularizing aspectual functionality.

Any idea what the overhead is for such a feature – although, as long as its for debugging it would matter – i presume that it can be swiched off (selectively?).


You can turn on/off events dynamically at runtime. See:

There’s a sub-section on performance. See also:

Usually you only generate events for selected objects (senders) and messages so it can be as selective as you want. But, as your main use case is debugging, I would not worry much about performance. After debugging, you can simply comment out the calls to define_events/5 and turn off events globally if not used for anything else (actually, that’s the default). Similar for using categories to implement aspects via hot patching, which is also disallowed by default.