[top] [up] [next]

Public and private clauses


There are public and private clauses inside a file, default is that each clause is private. A private clause is only accessible by clauses which are in the same file, public clauses are accessible by clauses from all other files. This mechanism of hiding clauses is useful for hiding 'internal' predicates that should never by accessible except by other Prolog clauses.

arrow.gif (1632 bytes)The public and private properties cannot be used to (re)use predicates that are in other modules, to (re)use predicates from other modules you should use import and export. Public and private clauses are useful if a single module consists of more than one file.

There are four predicates for changing the scope of a predicate, public/0, private/0, public/1 and private/1. public/0, private/0 are compile directives which can only be placed inside a module, public/1 and private/1 can be placed inside a file as compile directives or entered as a goal to prove.

  private. %All predicates in the file below this predicate become private
  test2( X ) :-
    X is 34*787.

  public. %All predicates in the file below this predicate are public
  test2( I ) :-
    I is value("3*9.").

It is also possible to use a predicate indicator or a list of predicate indicators.

  public( test1 / 1 ). %All 'test1' predicates with 1 argument in this file become public
  private( test2 / 0 ). %All 'test2' predicates with no argument in this file become private
  public( [ test1 / 3, test1 / 4, test2 / 2 ] ).

The predicates, of the example above, are executed automatically after the file has been loaded and compiled. The public and private predicates both fail if they cannot find at least a single clause to change. For instance, if you pass a list with three predicate indicators as arguments and 2 of them fail and one succeeds then the predicate still succeeds.

By using public and private predicates it is possible to control which clauses are accessible for Trinc-Prolog when it starts searching for a goal.

[top] [up] [next]

 

info@trinc-prolog.com