[top] [up] [next]

Import and export


If a Prolog program consist of more than one module and if it is necessary to call predicates inside another module then the import and export predicates must be used. Only if a predicate is exported from the module in which it is located and imported by the module which contains the call to the same predicate then will the call to the predicate succeed, else the predicate will not be accessible.

Example

The import/1 and export/1 predicates work just like the public/1 and private/1, the argument is a predicate indicator or a list of predicate indicators. The following example exports two predicates from module A :

  %Module A
  :-  export( [test/1, test/3] ). 

  test( 1 ).
  test( 2, 2 ).   %This predicate is not exported by this module
  test( 3, 3, 3 ).

The import and export predicates are used as compile directives in the example above, they can also be placed inside a goal. Then it is possible to export and import clauses while running a program

In the example below the clauses test/2 and test/3 are imported by module B.

  %Module B
  :-  import( [test/2, test/3] ).

  goal( A ) :- test( A ).     %The predicate test.1 is not imported by this module
  goal( A, B ) :- test( 2, 2 ).
  goal(A, B, C ) :- test( 3, 3, 3 ).

If both modules A and B are compiled and goal/1 (in module B) is called then it will not find test/1 because it is not imported by module B. The call to goal/2 will also not succeed because test/2 is not exported by module A. The call to goal/3 will succeed because it is exported by module A and imported by module B.

There are also import/2 and export/2 predicates, it is possible with these predicates to specify for other modules which clauses are imported and exported.

Modules promote reuse

With the import and export mechanism it is possible to create Prolog programs like building blocks, i.e. each separate module states the predicates it needs (which are imported) and which predicates it exposes to the rest of the program (exports). An example of this is an application which contains separate modules for rules which are different per year and another module that can use the interface of these rules-per-year modules for other calculations.

[top] [up] [next]

 

info@trinc-prolog.com