Re: Asserting a fact when triggering a rule

Paulo Moura <[email protected]>
Newsgroups gmane.comp.ai.prolog.swi
Message-ID <[email protected]>
Hi Baltasar,

On 18/12/2013, at 09:31, Baltasar García Perez-Schofield <[email protected]> wrote:

> 
> 	Hi, there,
> 
> 	I'm new to SWI Prolog. I'm using it through pySWIP, a bridge between SWI Prolog and Python.
> 
> 	I need to assert an extra fact when a given rule is triggered. I know how to do this in Pyke, but, I can't figure it out in SWI Prolog.
> 
> 	So, an example would be:
> 
> 	person( X ) :-
> 				human( X ).
> 
> 	So, every human is also a person. But say I want to assert that it is also a mammal. I can do it repeating the rule:
> 
> 	person( X ) :-
> 				human( X ).
> 	mammal( X ) :-
> 				human( X ).
> 
> 	However, I've been trying to accomplish this in swipl, and it seems that the second rule is not triggered unless a query asks for it.
> 
> 	I wonder whether it would be possible to assert both facts at the same time, something like:
> 
> 	person(X) :-
> 				human(X),
> 				if_triggered(assert(mammal(X))).

Not sure the approach you describe is the best but, given that Richard's reply today already discusses that, I will describe a solution along the lines of your post.

It should be straight-forward to accomplish the functionality you describe by running Logtalk with SWI-Prolog and by taking advantage of Logtalk support for event-driven programming. In a nutshell, you can define a monitor for the events corresponding to the predicates being called. The monitor defines event handlers that are automatically called by the Logtalk runtime when the events occur. These handlers can be used for asserting the extra facts you require. Let me know if you would like to explore a solution along these lines.

Cheers,

Paulo

-----------------------------------------------------------------
Paulo Moura
Logtalk developer

Email: <mailto:[email protected]>
Web:   <http://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.