Subject: Re: initialising SWI-Prolog from foreign code

From: Paul Singleton <[email-masked]>
Date: Thu, 10 Feb 2000 16:59:24 +0000

Jan Wielemaker wrote:

> On Wed, 09 Feb 2000, Paul Singleton wrote:

> >Is there a safe, portable way to discover, from foreign code,
> >whether Prolog has already been successfully initialised?

> No.  Of course this would be easy to add.

I think hybrid Prolog+Java applications will need this in general,
so I'd like to put in a request for this.

Just as Prolog apps can (whether they realise it or not) dynamically
load, start and use a JVM (e.g. via my "JSP" Prolog-calls-Java module
of Prolog and foreign predicates), so can Java apps dynamically load,
start and use a SWI-Prolog VM (if I may call it that :-) (e.g. via
Fred Dushin's JPL package of Java and "native methods").

Furthermore, Prolog may start Java via JSP to run a Java app which
calls Prolog via JPL, and vice versa!

Another possibility is that some third party decides to start both
Java and Prolog together.

Whichever, the apps shouldn't have to know or care about the order
in which things were started.

What JSP does currently is to check, on entry to every relevant
foreign predicate, whether a JVM is present, and start it with
default initialisation parameters if necessary.

This is done with a macro which checks whether a crucial pointer
(JVM *jvm :-) is NULL, so I don't begrudge the cost for the nice
properties it brings.

I't would be good to be able to do the corresponding thing from
Java.

> >Is there a way to "clean up the Prolog environment" without
> >exiting from the host process?

> Neither.   In the current implementation this would not be trivial.
> For quite a few things, Prolog has little clue what memory it
> holds.  On the other hand, if you accept some leaking it shouldn't
> be too hard.  Deallocating the stacks is implemented for the
> multi-threaded version and almost all memory is allocated through
> pl-alloc.c.  Only very few places use direct malloc().

This I *don't* need (nor have I figured out how to clean up
and restart a JVM if it aborts or exits).  This just came to mind
while I was thinking about initialisation.  But it could be of value
in the future.

> >If PL_initialise() is called when Prolog is already initialised,
> >does it put Prolog into its normal freshly initialised state?

> I think it might `appear to work', but I wouldn't give any
> guarantee on the consistency of the new environment.

This was another idle question, and given an already-initialised
test, there seems no need for this to ever be defined?

Paul Singleton