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 sub-modules. It may be as simple as this mypackage.m file:

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

This defines a module ‘mypackage’ containing sub-modules ‘mypackage.foo’, ‘mypackage.bar’, 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.
:- import_module fee, fie, foe, 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’.

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