Prolog event: prolog_listen/2, /3

I need the predicate prolog_listen to hook into the asserta/z processes of swi_prolog. However, the manual uses PredicateIndicator(Action, ClauseRef), without defining PredicateIndicator and ClauseRef. And the given example doesn’t work. Can somebody help ?

The example behaves exactly as promised for me. Note that you do need a pretty recent version for this to work. What version do you use?

You can find examples in the file boot/

Without code in the question it is difficult to say, but I suspect OP is confused about the options in prolog_listen/3, thinking that PredicateIndicator is a predicate itself.

Hi Jan, Hi Boris,

I use this: Welcome to SWI-Prolog (threaded, 32 bits, version 8.2.1).

OK, I entered the example and it works, its my error.
I was experimenting with APE with this code:

updated_hook(Pred, Action, Context) :-
   format('Updated ~p: ~p ~p~n', [Pred, Action, Context]).
:- prolog_listen(assertz(noun_sg/3), updated_hook(noun_sg/3)).

?- assertz(noun_sg(a,a,neutr)).

Yes the following text from the manual is very confusing:
PredicateIndicator(Action, ClauseRef)

I found my error:
I need the “dynamic” declaration. Without it, I had to modify the first argument to include assertz to escape a weird message:
ERROR: prolog_listen/2: Domain error: event' expected, found noun_sg/3’.
So here it is corrected:

?- dynamic noun_sg/3.
?- prolog_listen(noun_sg/3, updated_hook(noun_sg/3)).
updated_hook(Pred, Action, Context) :-
	format('Updated ~p: ~p ~p~n', [Pred, Action, Context]).

?- assertz(noun_sg(a,a,neutr)).
Updated noun_sg/3: assertz (072D4268)

Thanks Jan and Boris for the discussion !

This is supposed to be clarified by this text above the available event channels:

Defined channels are described below. The Channel argument is the name of the term listed below. The arguments are added as additional arguments to the given Closure.

Hmm. Doesn’t reproduce. Ideally this should be a permission error. I can set the listen on a static predicate, but of course this is not very useful as I cannot assert to it :slight_smile:

Anyway, there is a reason why this predicate is in the Hackers corner

1 Like