Subject: Re: Modules

From: Jan Wielemaker <[email-masked]>
Date: Tue, 13 Jun 2000 16:40:10 +0200

On Mon, 12 Jun 2000, Lalit Jain wrote:

>Jan and Richard, Thanks a bunch for your help!
>
>My intention is to have the same set of predicates loaded into two or more
>different modules so that I can treat each module space as a session and
>assert different facts and execute different goals in each of them.
>
>For now, looks like the way I can do it is to create a module file for
>each one and include my original predicate file in it, such as:
>
>file: m1.pro
>:- module(m1, [])
>:- include('mypreds.pro').
>
>file m2.pro
>:- module(m2, [])
>:- include('mypreds.pro').
>
>Because I need to create these modules dynamically at runtime, I will need
>to create these files at runtime (I load the engine from C++).

Hmm.  Why don't you simply assert into the required module?  It seems a
bit odd to write out a file and load it just to create a module.  Just
do

	?- assert(m1:foobar).

and the system will create the module m1 and add a fact foobar.  Using
the predicates import and export you can also import and export to/from
dynamically created modules (not portable).

>Now when I load these files I have two distinct module spaces with the
>same predicates. Also, 'mypreds.pro' has some foreign predicates (loaded
>using load_foreign_library) and I realized that I'll need to call install
>functions seperately from each module with approprirate predicate names
>(such as PL_register_foreign(m1:foo, ..)  from module m1
>         PL_register_foreign(m2:foo, ..)  from module m2)
>
>Please let me know if you think there's a better way to do this.

This is possible, but you might consider to add them to the user module,
which makes them visible from all modules.  You can also add them to
some specific module and use export/import to relay them to the modules
where you need them.

>** I have one more question, which is, how can I destroy these modules at 
>runtime when I no longer need them ?? ** Is these some predicate to
>destroy a module space?

You cannot really destroy modules.  You can use predicate_property/2 to
enumerate all predicates and then abolish to get rid of them.  Note
that abolish on an imported predicates just removes the import, not
the predicate.  This way you can make the module empty.  The module
itself won't disappear.  If that is really needed (empty modules are
rather small), you need to add it to the base system.

	Regards --- Jan