6.3 Importing Predicates into a Module
AllApplicationManualNameSummaryHelp

  • Documentation
    • Reference manual
      • Modules
        • Importing Predicates into a Module
          • use_module/1
          • use_module/2
    • Packages
Availability:built-in
Sourceuse_module(+Files)
Load the file(s) specified with Files just like ensure_loaded/1. The files must all be module files. All exported predicates from the loaded files are imported into the module from which this predicate is called. This predicate is equivalent to ensure_loaded/1, except that it raises an error if Files are not module files.

The imported predicates act as weak symbols in the module into which they are imported. This implies that a local definition of a predicate overrides (clobbers) the imported definition. If the flag warn_override_implicit_import is true (default), a warning is printed. Below is an example of a module that uses library(lists), but redefines flatten/2, giving it a totally different meaning:

:- module(shapes, []).
:- use_module(library(lists)).

flatten(cube, square).
flatten(ball, circle).

Loading the above file prints the following message:

Warning: /home/janw/Bugs/Import/t.pl:5:
        Local definition of shapes:flatten/2
        overrides weak import from lists

This warning can be avoided by (1) using use_module/2 to only import the predicates from the lists library that are actually used in the‘shapes' module, (2) using the except([flatten/2]) option of use_module/2, (3) use :- abolish(flatten/2). before the local definition or (4) setting warn_override_implicit_import to false. Globally disabling this warning is only recommended if overriding imported predicates is common as a result of design choices or the program is ported from a system that silently overrides imported predicates.

Note that it is always an error to import two modules with use_module/1 that export the same predicate. Such conflicts must be resolved with use_module/2 as described above.