[top] [up] [next]

External Prolog clauses


A Prolog clause can be declared as an external clause by using the Prolog directive external/2. The name of the Prolog clause must match with the name of an exported function inside the extension library. If test/0 is declared an external clause then there must be a function called 'test' in the exports table of the library.

The arity of a Prolog clause is ignored when a function in a library is searched, For instance: for the Prolog clauses test/0, test/1, test/2, etc. a function with the name 'test' is called. A declaration of a function that implements a Prolog clause must look like:

TPINT TP_API myClause( TPINT32 Version, TPCALL *Call, TPARGUMENT *First, TPEXCEPTION *Exc )
Delphi example

The first argument is a version number, the second argument is a TPCALL structure with the Prolog engine identifier and the number of arguments. The third parameter is the first argument of a linked list of arguments. The fourth parameter can be used when an exception must be thrown by Trinc-Prolog. The result code of the function determines how Trinc-Prolog must continue proving the current goal.

With the version number it is possible to determine if the function can work with the supplied arguments.

By inspecting the TPCALL structure it is possible to determine if for instance test/1 or test/2 is being proven. Usually the first few lines of a C/C++ function that implements an external Prolog clause look like the example below.

//Check if correct version
If( Version != TP_EXTERNAL_VERSION ) {
    return TP_HALT; //Trinc-Prolog stops proving the current goal
    }

//Check if arity is 1 or 2, other values are not allowed
if( ( Call->_Arity != 1 ) && ( Call->_Arity != 2 ) ) {
    return TP_UNKNOWN; //Trinc-Prolog will generate an 'unknown' clause error
    }
Delphi example

[top] [up] [next]

 

info@trinc-prolog.com