Subject: RE: Predicate Determining the port in which was called.

From: Jan Wielemaker <[email-masked]>
Date: Wed, 9 Aug 2000 21:48:50 +0200

> Corrections: (actually i am not sure if this is correct eighter)
> 
> I want to write but do not know how is to determine the port in which a
> predicate was called over:
> 
> example:
> 
> test:- member(X,[1,2,3]),mypredicate,X=4.
> mypredicate:- port(call),write('% creating side effects'),nl.
> mypredicate:- port(redo),write('% doing nothing'),nl.
> mypredicate:- port(exit),write('% comitting side effects'),nl.
> mypredicate:- port(fail),write('% undoing side effects'),nl.

Note really sure this is what you mean  but here is a try:

In principle you can wrap a goal into something like:

wrap(G) :-
	at_call,
	G,
	(  at_exit
	;  at_redo, fail
	).
wrap(_) :-
	at_fail,
	fail.

where you insert code at the at_* places to the thing you need done there.
Note however that this is not perfect.  First of all, you need to add some
more tricks to get exceptions working properly (but this can be done) and
second, if the choicepoint is cut you will not be informed:

test :-
	wrap(hello),
	!.

You can deal with this case using foreign code as discussed on the
mailinglist before.

	--- Jan