Previous Up Next

9.5  The Multi-Directional Arithmetic Predicates

A drawback of arithmetic using is/2 is that the right hand side must be fully instantiated at evaluation time. Often it is desirable to have predicates that define true logic relationships between their arguments like “Z is the sum of X and Y”. For integer addition and multiplication this is provided as:

succ(X, Y)
True if Xand Y are natural numbers, and Y is one greater than X. At most one of X, Y can be a variable.
plus(X, Y, Z)
True if the sum of X and Y is Z. At most one of X, Y, Z can be a variable.
times(X, Y, Z)
True if the product of X and Y is Z. At most one of X, Y, Z can be a variable.

These predicates work only with integer arguments but any single argument can be a variable which is then instantiated so that the relation holds. If more than one argument is uninstantiated, an instantiation fault is produced.

Note that if one of the first two arguments is a variable, a solution doesn’t necessarily exist. For example, the following goal has no integer solution :

[eclipse 1]: times(2, X, 3).

no (more) solution.

Since any one of the arguments of these two predicates can be a variable, it does not make much sense to use them in arithmetic expressions where always the first arguments are taken as input and the last one as output.


Previous Up Next