Download User Manual

Transcript
7.3.2
Qualified Access via :/2
Normally, it is convenient to import predicates which are needed. By importing, they become
visible and can be used within the module in the same way as local definitions. However,
sometimes it is preferable to explicitly specify from which module a definition is meant to be
taken. This is the case for example when multiple versions of the predicate are needed, or when
the presence of a local definition makes it impossible to import a predicate of the same name
from elsewhere. A call with explicit module qualification is done using : /2 and looks like this:
lists:print_list([1,2,3])
Here, the module where the definition of print list/1 is looked up (the lookup module) is
explicitly specified. To call print list/1 like this, it is not necessary to make print list/1 visible.
The only requirement is that it is exported (or reexported) from the module lists.
Note that, if the called predicate is in operator notation, it will often be necessary to use brackets,
e.g. in
..., ria:(X #>= Y), ...
The : /2 primitive can be used to resolve import conflicts, i.e. the case where the same name is
exported from more than one module and both are needed. In this case, none of the conflicting
predicates is imported - an attempt to call the unqualified predicate raises an error. The solution
is to qualify every reference with the module name:
:- lib(ria).
:- lib(eplex).
% exports #>= / 2
% exports #>= / 2
..., ria:(X #>= Y), ...
..., eplex:(X #>= Y), ...
Another case is the situation that a module wants to define a predicate of a given name but
at the same time use a predicate of the same name from another module. It is not possible to
import the predicate because of the name conflict with the local definition. Explicit qualification
must be used instead:
:- lib(lists).
print_list(List) :writeln("This is the list"),
lists:print_list(List).
A more unusual feature, which is however very appropriate for constraint programming, is the
possibility to call several versions of the same predicate by specifying several lookup modules:
..., [ria,eplex]:(X #>= Y), ...
which has exactly the same meaning as
..., ria:(X #>= Y), eplex:(X #>= Y), ...
Note that the modules do not have to be known at compile time, i.e. it is allowed to write code
like
after(X, Y, Solver) :Solver:(X #>= Y).
However, this is likely to be less efficient because it prevents compile-time optimizations.
59