A Pro/3 function is mapping from zero, one or more parameters to a
function value. The parameters and the function value (return
value) belong to a specified data element domain.
A function consists of three parts:
- terminology
- sentence model
declaration
- function
definition
Normally, you have to insert these three elements in the KB in
the sequence over, and remove (delete) from the KB in the reversed over.
Functions are used two ways in Pro/3, in
sentence rules and in certainty rules (i.e. in the query part of
data rules). The general rule of function usage is to use them as
little as possible. Functions should be resorted on only if the
desired rule logic cannot be achieved without functions.
|
Terminological definition - Sentence model declaration
|
The terminological definition defines the function's KB name
and NL name. The sentence model declaration defines the function's
parameters as an ordered list of data element types, and the
domain of the function's return value. The function definition defines
the mapping from parameters to function value (the "program
code").
|
Example:
The faculty function can be defined as follows (in an English
language terminology):
TERMINOLOGY:
the syntagm
with language English, KB - name "fFac", syntax
class FUNCTION, syntax form SINGULAR/UNDETERMINED and token
list "faculty" and "of" is in the
terminology with segment name "MATH"!
SENTENCE MODEL:
the
deterministic function with NL - name faculty of, domain name
INTEGER and data element list integer value is in the
sentence model with segment name "MATH"!
DEFINITION:
the faculty
of is defined by:
integer value<0;
/* invalid parameter - give an error message */
Dummy=error given message=("function faculty called with
negative number:"+integer value);
return value =IN -1!
the faculty of is defined by:
integer value=0;
return value =IN 1!
the faculty of is defined by:
integer value=1;
return value =IN 1!
the faculty of is defined by:
M=(integer value-1);
N=faculty of given integer value M;
return value =IN (N*M)!
|
|
The faculty function example illustrates several aspects of
Pro/3 functions:
- The function definition consists of a header (terminated by
':'), and one or more statements separated by ';' - the
last statement is terminated by '!'.
- There can be several function
definitions defined for a
function each handling e.g. unique ranges of direct or
indirect parameter values. The definitions are processed in
the same sequence as they are defined (there is
accordingly no need to have a condition N>1 in the
last definition in the example above). It is however strongly recommended
to program the function definitions such that the function works correctly
regardless of the sequence of the definitions in the KB.
- One and only one statement assigns a value to return
value. This is usually, but not necessarily, the last
statement. return value is a pre-defined function return
variable. (The return value can also be used in
conditional statements such as function value > 0.
- A function can call other functions and it can call
itself.
- The integer error function is an error handler function
which takes an error message string as an input
parameter. The function opens an error message dialog box
(with the error message) and asks if the execution is to
be continued or canceled. The function will return 0 if
the execution is continued. The integer error function is
an example of a built-in function.
|
Function Window
|
Functions are either inputted as text via the editor window or
by using the function window. The function window can be used to
enter or update all aspects of a function, i.e. its terminology,
sentence model declaration and its definition (definitions):
|
|
Function statements
|
There are three types of function statements: assignment
statements, comparison statements and true/false statements.
Assignment statements have the general format VARIABLE = RIGHT
SIDE, where the variable is a local variable or the function
return value variable. The function return value variable has the
predefined name return value. The right side of the assignment
statement is a local variable, a function call, a data element
reference, a constant from any of the data element domains or an
expression.
The NL syntax describes the expression syntax in detail. Note
that:
- expressions must always be in parentheses
- expressions can not contain function calls
- expressions can not contain data element references
One, and only one, statement must assign a value to the return
value predefined variable. This is usually but not necessarily
the last statement in the function definition.
A comparison statement has the format VARIABLE <comparison
operator> COMPARATOR. The comparator is a local variable or a
constant from any of the data element domains. The execution of
the function definition fails if the comparison is not true.
True/false statements have the general format true(UNARY
PREDICATE CONDITION) or false(UNARY PREDICATE CONDITION). It is
similar to the comparison statement in the way that the function
interpreter simply needs to decide whether or not the statement
is true or false.
|
Deterministic and non-deterministic
functions
|
A function is defined as either deterministic or
non-deterministic. Deterministic functions return one value (or
none if it fails). Non-deterministic functions can return many
values. Refer to non-determinism for further information on the
use of non-determinism.
The built-in function integer series is an example of a
non-deterministic function. The call integer series given 1, 2
and 20 will return, consecutively, the integers 1, 3, 5, ..., 19.
You can define non-deterministic functions by building in at
least on non-deterministic statement, i.e. a call to another
non-deterministic function or a non-deterministic data element
reference. You can also achieve non-determinism by using multiple
function definitions.
The months function returns non-deterministically the name of the
twelve months. The function is not very meaningful, but it
illustrates the three methods of making a defining a
non-deterministic function.
Terminology and structure are defined by
the following sentences:
the syntagm
with language English, KB - name "fMonths", syntax
class FUNCTION, syntax form SINGULAR / UNDETERMINED and token
list "months" is in the terminology with segment name
"EXAMPLES"!
the non-deterministic function with NL - name months and domain
IDENTIFIER is in the sentence model with segment name
"EXAMPLES"!
|
non-determinism through
non-deterministic function call |
the months is defined by:
No =*IN the integer series given start 1, end 12 and increment 1;
Month = the month corresponding to month-number No;
return value =*IF Month!
month corresponding to
and integer series given
are built-in functions. The following function call query will
give the desired results:
what=months?
Jan=months
Feb=months
Mar=months
Apr=months
May=months
Jun=months
Jul=months
Aug=months
Sep=months
Oct=months
Nov=months
Dec=months
|
non-determinism through
non-deterministic data element reference |
the months is defined by:
Month = (the value of name of the existing calendar month);
return value =*IF Month!
This declaration of the months function is based on the assumption
that the following sentences are know (i.e. in the KB):
the calendar month with name
Jan, ... exists!
the calendar month with name Feb, ... exists!
:
the calendar month with name Dec, ... exists!
|
non-determinism through multiple function definitions
|
the months is defined by:
return value =IF Jan!
the months is defined by:
return value =IF Feb!
:
the months is defined by:
return value =IF Dec!
|
|
Function Calls
|
Actual parameters (i.e. the parameters specified in given
call) must match the formal parameters (i.e. the parameters in
the function declaration) in number and domain compatibility. Actual parameters can be
parameters, variables and constants. Note that:
- function calls and
certainty rule calls are not permitted as actual parameters
- expressions are not allowed as actual parameters
|
Function call examples:
function without parameters:
Value = sample function one
function with three non-list
parameters:
Value = sample function two no of notes 23, denomination 100
and currency US$
function with one list parameter:
Value = sample function three observations 120, 3 and 55
function with a mixture of list and
non-list parameters:
Value = sample function four first composer "Dylan",
second composer "Kristoffersen" and third composer
"Peter", "Paul" and "Mary
Equality-sign can be used between
parameter name and value. Brackets can be used to show the
parameter part of the call explicitly. given can be used to precede the parameters:
Value = sample
function four given [first composer="Dylan", second
composer="Kristoffersen" and third
composer="Peter", "Paul" and "Mary]
|
|
Built-in functions
|
Pro/3 has a number of built in functions. Refer to the
following files in the /STD directory for details:
|
FILE |
CONTENTS |
PRO3-B-FNS.3PR |
Function declarations |
PRO3-E-FNT.3PR |
Function terminology (English) including a brief description
of each function. |
|
The file
Q-BI-FUNCS.3NL includes test queries for all built-in functions.
|
Function
libraries (standard Pro/3 functions)
|
Pro/3 has a number of library functions. Refer to the following
files in the /STD directory:
|
FILE |
CONTENTS |
PRO3-F-DATE.3NL |
Date/time function declarations |
PRO3-EF-DATE.3PR |
do. terminology (English) |
PRO3-F-MATH.3NL |
Math function declarations |
PRO3-EF-MATH.3PR |
do. terminology (English) |
PRO3-F-MISC.3NL |
Misc. function declarations |
PRO3-EF-MISC.3PR |
do. terminology (English) |
|
The file Q-LIB-FUNCS.3NL includes
test queries for all library functions.
|