Up Next

9.1  Built-Ins to Evaluate Arithmetic Expressions

Unlike other languages, Prolog usually interprets an arithmetic expression like 3 + 4 as a compound term with functor + and two arguments. Therefore a query like 3 + 4 = 7 fails because a compound term does not unify with a number. The evaluation of an arithmetic expression has to be explicitly requested by using one of the built-ins described below.

The basic predicate for evaluating an arithmetic expression is is/2. Apart from that only the 6 arithmetic comparison predicates evaluate arithmetic expressions automatically.

Result is Expression
Expression is a valid arithmetic expression and Result is an uninstantiated variable or a number. The system evaluates Expression which yields a numeric result. This result is then unified with Result. An error occurs if Expression is not a valid arithmetic expression or if the evaluated value and Result are of different types.
Expr1 < Expr2
succeeds if (after evaluation and type coercion) Expr1 is less than Expr2.
Expr1 >= Expr2
succeeds if (after evaluation and type coercion) Expr1 is greater or equal to Expr2.
Expr1 > Expr2
succeeds if (after evaluation and type coercion) Expr1 is greater than Expr2.
Expr1 =< Expr2
succeeds if (after evaluation and type coercion) Expr1 is less or equal to Expr2.
Expr1 =:= Expr2
succeeds if (after evaluation and type coercion) Expr1 is equal to Expr2.
Expr1 =\= Expr2
succeeds if (after evaluation and type coercion) Expr1 is not equal to Expr2.

9.1.1  Arithmetic Evaluation vs Arithmetic Constraint Solving

This chapter deals purely with the evaluation of arithmetic expressions containing numbers. No uninstantiated variables must occur within the expressions at the time they are evaluated. This is exactly like arithmetic evaluation in procedural languages.

As opposed to that, in arithmetic constraint solving one can state equalities and inequalities involving variables, and a constraint solver tries to find values for these variables which satisfy these constraints. Note that ECLiPSe uses the same syntax in both cases, but different implementations providing different solving capabilities. See the chapter Common Solver Interface in the Constraint Library Manual for an overview.


Up Next