A procedure can be (and usually is) declared as exported before it is actually defined. Export declarations should occur either at the beginning of a module text, or just before the procedure definition, e.g
:- export double/2. double(X, Y) :- Y is 2*X.
You can only export procedures that are defined in the exporting module. Imported procedures cannot be exported with export/1 (it raises error 94) - use reexport/1 to do this.
Declaring a procedure as exported will make it accessible to other modules. That means that it can either be called with explicit module qualification using :/2, or it can be imported and thus made visible elsewhere.
Procedures can be imported and calls to them compiled before they have been exported, e.g. when an importing module is compiled before the exporting module. This mechanism should be used only in exceptional situations, normally the exporting module should be compiled first. The reason is that the compiler needs some information about the predicate when compiling a call to it. If this information is not available at call time, an incompatibility may occur later when the exported definition is encountered.
Exporting Other Declarations
Exported structure, operator, syntax, macro and portray declarations have the same effect as the corresponding local declarations in the module where they occur. In addition, they are available in every module where they are imported.
The exported initialization directive does not have any effect in the exporting module, but only in the module where it is imported. The initialization goal is called once in the context of every importing module.
All the export (and reexport) directives of a module together form what is called the module's interface. The module interface can be extracted from a module source file using the icompile/2 utility from library(document). The interface can also be retrieved from a loaded module by calling get_module_info/3.
Exporting the same item twice, or exporting something that has previously been declared local, is accepted silently.
The following primitives implicitly export items: The module/3 directive and the create_module/3 predicate export the list of items given in their second argument. The tool/2 declaration implicitly exports the tool body predicate. Event handlers, error handlers and interrupt handlers are implicitly exported by the corresponding set_xxx_handler primitive.
The export/1 primitive can not only occur as a directive but can also be called at runtime.
% A module that exports a predicate and an operator: :- module(m1). :- export before/2, op(700, xfx, before). A before B :- A < B. % Using this module elsewhere: :- module(m2). :- import m1. % or :- use_module(".../m1..."). main :- 3 before 7. % operator and procedure definition are visible! % Using before/2 without import, via explicit qualification: % We can call before/2, but we cannot use the infix syntax! :- module(m3). main :- m1:before(3,7). % Error cases: :- export Q. (Error 4). :- export p/a. (Error 5). :- import p/1 from m. :- export p/1. (Error 94).