Re: foreign predicate not working

Paulo Moura <[email protected]>
Newsgroups gmane.comp.gnu.prolog.general
Message-ID <[email protected]>
On 19/jul/2004, at 15:25, Saurabh Bhatla wrote:

> Hi
>   I am using a foreign predicate from my logtalk object. I define that
> in a file receiver.lgt (attached). the definition of my predicate is in
> file file_ops.c (attached). I can compile the code in gprolog and am
> able to make an executable out of it too. The problem is that whenever 
> i
> call the foreign predicate it returns false. I have some printf
> statements in my C code but none of them gets printed, which mean that
> the c code is not being called at all.
>   I tried to trace the execution and the call to my foreign predicate
> simply fails without checking anything. My C program works fine when
> executed alone.

Your definition of the Logtalk object receiver seams to be wrong. The 
code you sent us is:

:- foreign(get_event_ob(+string,-string)).

:-object(receiver).

	:-public(receive/1).
	:-public(validate/1).
	:-public(get_event_ob/2).
	
	receive(Event).

	validate(Event):-
		self(Self),
		Self::get_event_ob(Event,Object),
		write(Object),	
		write('<-object found for event->'),
		write(Event),nl.

	validate(Event):-
		write('No object found for event->'),
		write(Event),nl.
		
:-end_object.

The problem is that the predicate get_event_ob/2 is not defined inside 
the object "receiver". You need to write:

	validate(Event):-
		self(Self),
		{get_event_ob(Event,Object)},	% external call; the {}/1 construct 
bypasses the Logtalk pre-processor
		write(Object),	
		write('<-object found for event->'),
		write(Event),nl.

Cheers,

Paulo


-----------------------------------------------------------
Paulo Jorge Lopes de Moura
Dep. of Informatics                   Office 4.3  Ext. 3257
University of Beira Interior          Phone: +351 275319700
6201-001 Covilhã                      Fax:   +351 275319891
Portugal

<mailto:[email protected]>
<http://www.di.ubi.pt/~pmoura>     <http://www.logtalk.org>
-----------------------------------------------------------
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.