6.13 Dynamic Modules

So far, we discussed modules that were created by loading a module file. These modules have been introduced to facilitate the development of large applications. The modules are fully defined at load-time of the application and normally will not change during execution. Having the notion of a set of predicates as a self-contained world can be attractive for other purposes as well. For example, assume an application that can reason about multiple worlds. It is attractive to store the data of a particular world in a module, so we extract information from a world simply by invoking goals in this world.

Dynamic modules can easily be created. Any built-in predicate that tries to locate a predicate in a specific module will create this module as a side-effect if it did not yet exist. For example:

?- assert(world_a:consistent),
   set_prolog_flag(world_a:unknown, fail).

These calls create a module called ‘world_a’ and make the call ‘world_a:consistent’ succeed. Undefined predicates will not raise an exception for this module (see unknown).

Import and export from a dynamically created world can be achieved using import/1 and export/1 or by specifying the import module as described in section 6.10.

?- world_b:export(solve/2).          % exports solve/2 from world_b
?- world_c:import(world_b:solve/2).  % and import it to world_c

If the Prolog flag gc_code is module, modules created at runtime are removed by garbage collection when they are empty and no longer referenced. The default value of this flag is clause, which disables garbage collection of predicates and modules. Garbage collection of modules is part of clause garbage collection (see garbage_collect_clauses/0). A module is empty if it has no predicates, exports, local operators or flags other than the defaults and imports only from user. Note that calling an undefined predicate creates a predicate, which is in turn removed by garbage collection if nothing refers to it.

A module that is referenced from compiled code, loaded from a file, modified using e.g., set_module/1, add_import_module/3, op/3 or set_prolog_flag/2, or used through the foreign language interface is never removed. As modules are created when they are used, removing an empty module has no visible effect, except that current_module/1 no longer reports it.

A world that is no longer needed can be emptied using abolish_module/1.

[det]abolish_module(+Module)
Reset Module to a new, empty module. All predicates of Module are abolished and their clauses are reclaimed by clause garbage collection (see garbage_collect_clauses/0). Exports and local operators are removed, the import module is reset to user and the module class and flags are reset to their defaults. If Module was loaded from a file, this file is unloaded (see unload_file/1). Predicates imported from Module into other modules become undefined.

The module itself remains as an empty module, so clauses that call predicates of Module keep working if these predicates are defined again. If nothing refers to the module and the flag gc_code is module, it is subsequently removed by garbage collection as described above. This predicate is safe to use while other threads are running code of Module. Succeeds silently if Module does not exist and raises a permission error for the modules user and system and other system modules.