Next: , Up: Libraries   [Contents][Index]

6.1 Writing libraries

A Mercury library is identified by a top-level module, which should contain all of the modules in that library as submodules. It may be as simple as this mypackage.m file:

:- module mypackage.
:- interface.
:- include_module foo.
:- include_module bar.
:- include_module baz.

This defines a module ‘mypackage’ containing submodules ‘’, ‘’, and ‘mypackage.baz’.

It is also possible to build libraries of unrelated modules, so long as the top-level module imports all the necessary modules. For example:

:- module blah.
:- implementation.
:- import_module fee.
:- import_module fie.
:- import_module foe.
:- import_module fum.

This example defines a module ‘blah’, which has no functionality of its own, and which is just used for grouping the unrelated modules ‘fee’, ‘fie’, ‘foe’, and ‘fum’. To avoid a warning about the interface of this module being empty, this module would have to be compiled with ‘--no-warn-nothing-exported’. Alternatively, the library could of course just export something, such as a predicate that returns its version number.

Generally it is better style for each library to consist of a single module which encapsulates its submodules, as in the first example, rather than just a group of unrelated modules, as in the second example.