Navigation  without Java Scripts

Improved Global Domain Handling

Note that everything we say here about global domains also counts for domains corresponding to names of global facts sections (i.e. global databases).

From VIP 5.2 the handling of global domains is improved, so that global domains no longer need to be included in all modules of a program.

Now a global domain only needs to be included into:

the main module (which is the module containing the goal)
those modules that actually use the domain

There are three main benefits from this feature:

It is possible to give a program an include structure that more precisely match the structure of the program.
When a global domain is changed, recompilation time is reduced (provided the include structure of the program is changed to take advantage of the feature, of course).
It is possible to create and use pre-compiled libraries (using global domains).

Note that class names are considered to be global domains (in some respects) and, as such, class declarations (prior to VIP 5.2) also had to be included in every module. This constraint has been removed in VIP 5.2 . In fact, you can now declare globally accessible domains in classes (see Domains in classes), and these classes need not even to be included into the main module (if they are not actually used in the main module).

Let us consider include "styles" of programs. There are (of course) many ways to structure a program. Here we shall consider two "old" styles and then a suggestion for a new style.

The VDE has a default structuring mechanism that fits well for beginners, small-scale programming, prototyping, etc. The structuring mechanism is that every module includes a <project>.inc file, which then in turn includes a <module>.dom file and a <module>.pre file for each module. In the dom file you are supposed to declare the global domains and facts of the module. And in the pre file you are supposed to declare the global predicates. Since every module includes the inc file, which includes all dom and pre files, you have access to every global domain and every global predicate in every module. This is nice for "easy programming" (programming in the small).

When you make large programs, there are drawbacks of this style, however:

If you change a global domain declaration or a global predicate declaration, you have to recompile all modules of the project.
When creating larger programs (especially with many programmers) having access to everything from everywhere, tends to lead to spaghetti programs. I.e. programs where every part of a program references to all other parts of the program.

Therefore, it is quite common to structure larger programs differently. When creating larger programs, you would often move the includes of all the pre files out of the inc file, and include them only into those modules that actually use the corresponding pre files. Until VIP v. 5.2, you could not do the same with the dom files, because Visual Prolog used to require that all global domains were declared in all modules (and even in the same order).

The benefit from moving pre file includes from the inc file into the using them pro files are:

When you change the declaration of a global predicate, only those modules that include the corresponding pre file need to be recompiled.
If a module does not include a certain pre file, then you cannot call the predicates declared in that pre file. Subsequently it is easier to maintain a certain usage structure between modules, with reduction in spaghetti code as a consequence. Of course, it is still possible to create spaghetti code, but it is easier to avoid it. Especially it is easier to avoid that new programmers on a project, who do not yet know the program structure, accidentally circumvent a good program structure. New programmers on a project can also learn about the overall structure of a program by investigating the include structure.
Since the overall number of predicates that can be declared in a module is limited; therefore, more global predicates can be used in a project and more local predicates can be declared in each module.

As it was mentioned, it is now possible to move not only pre file inclusions into the pro files, but also the dom file inclusions. Recall however, that all global domains still have to be included into the main module (i.e. the one with the goal). If you also move the dom file inclusions from the inc file into the pro files then you, of course, immediately extend the mentioned above benefits with the following:

When you change the declaration of a global domain, only those modules that include the corresponding dom file need to be recompiled. (This, of course, also includes the main module).
Now it is also possible to use domains only in those files, where you intend its usage.
Since the overall number of domains that can be declared in a module is limited; therefore, more global domains can be used in a project and more local domains can be declared in each module.

Now, when the main module is the only one that has to include all global domains, you might consider making the main module very small (so that recompilation will be fast). An easy way to make the main module small is to replace the goal in its original place with a clause of a new global predicate (lets name it run).

And then create a new module containing a lot of includes and a goal:

goal
    run().

The declaration of run must be visible, of course, both in the old main module and in the new one.

Another thing you might consider is to put the global domains as well as the global predicates of a module into the same file. Typically, you need to use both the predicates and the domains, if you need to use a module at all. Putting both domains and predicates in the same file will, of course, increase the amount of code to compile in the case of the main module, how much that will mean depends on your program. If you put both domain and predicate declarations in the same file then we recommend that you give it extension ph, i.e. <module>.ph. This is also the extension we recommend for files containing class declarations.