Abbreviated module names

I’m using: SWI-Prolog version 8.2.4 on MacOS 14

I was wondering whether it is possible to abbreviate the imported module name when using its exported predicates.

Python and Java have this possibility:

import alibrary as al
import com.twan.metamodel as mm;

To illustrate: Say I’ve in my module the following directive (the Prolog fragments in the example are taken from my current project)

:- module(metamodel, [relationship/3, entity/2, property/3, ... ]).
...

And use that as follows in another module:

:- module(application, [...]).

:- use_module(library(metamodel)).

metamodel:entity(......)
metamodel:relationship( ....)
...

But rather than writing metamodel:entity or metamodel:relationship, I’ld like to be able to write mm:entity and mm:relationship.

Is there a mechanisme in SWI Prolog facilitating this?

For now, thanks for reading!

/Twan

It should be in the FAQ (which I am updating as we speak). The SWI-Prolog module system works differently. This applies to all Prolog systems who’s module system is based on Quintus Prolog’s module system designed in the late 80s.

In Python (and many modern languages), an import just makes the module available and you have to access the functions as module.func() or something similar. In Prolog, importing a module adds the predicates of the module to the current name space. So, after importing the metamodel, you can access its predicates as

entity(...).

To make this usable, use_module/2 allows you to import only the predicates you really use and it also allows for renaming, e.g.

:- use_module(metamodel, [entity/1 as ent]).

t :- ent(X).

The as name is a SWI-Prolog extension also seen in some other systems. The “classical” deal with a conflict is to use use_module/2, not importing the conflicting predicate from multiple places and use module:pred(....) to make the calls.

The normal way to deal with this is

  • Use long unique module names in the :- module(...) declaration.
  • Use somewhat longer names for exported predicates. In many cases the exported predicates have some abbreviation of the library name as part of their name. See the system libraries for many (mostly good, some dubious) examples.
1 Like