# Arithmetic

Author(s): Daniel Cabeza, Manuel Hermenegildo.

Arithmetic is performed by built-in predicates which take as arguments arithmetic expressions (see arithexpression/1) and evaluate them. Terms representing arithmetic expressions can be created dynamically, but at the time of evaluation, each variable in an arithmetic expression must be bound to a non-variable expression (the term must be ground). For example, given the code in the first line a possible shell interaction follows:

```evaluate(Expression, Answer) :- Answer is Expression.

?- _X=24*9, evaluate(_X+6, Ans).

Ans = 222 ?

yes
```

## Documentation on exports

PREDICATEis/2
ISOVal is Exp

The arithmetic expression Exp is evaluated and the result is unified with Val

Usage 1:A is B

• The following properties should hold at call time:
A is a free variable.
B is currently a term which is not a free variable.
A is a free variable.
B is an arithmetic expression.
• The following properties hold upon exit:
A is a number.
B is an arithmetic expression.
int(B) is the size of argument A, for any approximation.
• The following properties hold globally:
All the calls of the form A is B do not fail.
A is B is evaluable at compile-time.

Usage 2:A is B

• The following properties should hold at call time:
A is a free variable.
B is currently a term which is not a free variable.
A is a free variable.
B is an integer expression.
• The following properties hold upon exit:
A is an integer.
B is an integer expression.
int(B) is the size of argument A, for any approximation.
• The following properties hold globally:
All the calls of the form A is B do not fail.
A is B is evaluable at compile-time.

Usage 3:A is B

• The following properties should hold at call time:
A is currently a term which is not a free variable.
B is currently a term which is not a free variable.
A is a number.
B is an arithmetic expression.
• The following properties hold globally:
Indicates the type of test that a predicate performs. Required by the nonfailure analyisis.

Usage 4:A is B

• The following properties should hold at call time:
A is currently a term which is not a free variable.
B is currently a term which is not a free variable.
A is an integer.
B is an integer expression.
• The following properties hold globally:
Indicates the type of test that a predicate performs. Required by the nonfailure analyisis.
Other properties:
A is B

• The following properties hold globally:
int is the measure used to determine the size of the terms that A is bound to, for any type of approximation.
int is the measure used to determine the size of the terms that B is bound to, for any type of approximation.

• The following properties hold globally:
This predicate is understood natively by CiaoPP.
Val is Exp is side-effect free.
Val is Exp is binding insensitive.
All calls of the form Val is Exp are deterministic.
Goal Val is Exp produces inf solutions.

PREDICATE</2
ISOExp1<Exp2

The numeric value of Exp1 is less than the numeric value of Exp2 when both are evaluated as arithmetic expressions.

Usage:A<B

• The following properties should hold at call time:
A is currently a term which is not a free variable.
B is currently a term which is not a free variable.
A is an arithmetic expression.
B is an arithmetic expression.
• The following properties hold globally:
A<B is evaluable at compile-time.
int is the measure used to determine the size of the terms that A is bound to, for any type of approximation.
int is the measure used to determine the size of the terms that B is bound to, for any type of approximation.
Other properties:

• The following properties hold globally:
This predicate is understood natively by CiaoPP.
Exp1<Exp2 is side-effect free.
Exp1<Exp2 is binding insensitive.
All calls of the form Exp1<Exp2 are deterministic.
Goal Exp1<Exp2 produces inf solutions.
Indicates the type of test that a predicate performs. Required by the nonfailure analyisis.

PREDICATE=</2
ISOExp1=<Exp2

The numeric value of Exp1 is less than or equal to the numeric value of Exp2 when both are evaluated as arithmetic expressions.

Usage:A=<B

• The following properties should hold at call time:
A is currently a term which is not a free variable.
B is currently a term which is not a free variable.
A is an arithmetic expression.
B is an arithmetic expression.
• The following properties hold globally:
A=<B is evaluable at compile-time.
int is the measure used to determine the size of the terms that A is bound to, for any type of approximation.
int is the measure used to determine the size of the terms that B is bound to, for any type of approximation.
Other properties:

• The following properties hold globally:
This predicate is understood natively by CiaoPP.
Exp1=<Exp2 is side-effect free.
Exp1=<Exp2 is binding insensitive.
All calls of the form Exp1=<Exp2 are deterministic.
Goal Exp1=<Exp2 produces inf solutions.
Indicates the type of test that a predicate performs. Required by the nonfailure analyisis.

PREDICATE>/2
ISOExp1>Exp2

The numeric value of Exp1 is greater than the numeric value of Exp2 when both are evaluated as arithmetic expressions.

Usage:A>B

• The following properties should hold at call time:
A is currently a term which is not a free variable.
B is currently a term which is not a free variable.
A is an arithmetic expression.
B is an arithmetic expression.
• The following properties hold globally:
A>B is evaluable at compile-time.
int is the measure used to determine the size of the terms that A is bound to, for any type of approximation.
int is the measure used to determine the size of the terms that B is bound to, for any type of approximation.
Other properties:

• The following properties hold globally:
This predicate is understood natively by CiaoPP.
Exp1>Exp2 is side-effect free.
Exp1>Exp2 is binding insensitive.
All calls of the form Exp1>Exp2 are deterministic.
Goal Exp1>Exp2 produces inf solutions.
Indicates the type of test that a predicate performs. Required by the nonfailure analyisis.

PREDICATE>=/2
ISOExp1>=Exp2

The numeric value of Exp1 is greater than or equal to the numeric value of Exp2 when both are evaluated as arithmetic expressions.

Usage:A>=B

• The following properties should hold at call time:
A is currently a term which is not a free variable.
B is currently a term which is not a free variable.
A is an arithmetic expression.
B is an arithmetic expression.
• The following properties hold globally:
A>=B is evaluable at compile-time.
int is the measure used to determine the size of the terms that A is bound to, for any type of approximation.
int is the measure used to determine the size of the terms that B is bound to, for any type of approximation.
Other properties:

• The following properties hold globally:
This predicate is understood natively by CiaoPP.
Exp1>=Exp2 is side-effect free.
Exp1>=Exp2 is binding insensitive.
All calls of the form Exp1>=Exp2 are deterministic.
Goal Exp1>=Exp2 produces inf solutions.
Indicates the type of test that a predicate performs. Required by the nonfailure analyisis.

PREDICATE=:=/2
ISOExp1=:=Exp2

The numeric values of Exp1 and Exp2 are equal when both are evaluated as arithmetic expressions.

Usage:A=:=B

• The following properties should hold at call time:
A is currently a term which is not a free variable.
B is currently a term which is not a free variable.
A is an arithmetic expression.
B is an arithmetic expression.
• The following properties hold globally:
A=:=B is evaluable at compile-time.
int is the measure used to determine the size of the terms that A is bound to, for any type of approximation.
int is the measure used to determine the size of the terms that B is bound to, for any type of approximation.
Other properties:

• The following properties hold globally:
This predicate is understood natively by CiaoPP.
Exp1=:=Exp2 is side-effect free.
Exp1=:=Exp2 is binding insensitive.
All calls of the form Exp1=:=Exp2 are deterministic.
Goal Exp1=:=Exp2 produces inf solutions.
Indicates the type of test that a predicate performs. Required by the nonfailure analyisis.

PREDICATE=\=/2
ISOExp1=\=Exp2

The numeric values of Exp1 and Exp2 are not equal when both are evaluated as arithmetic expressions.

Usage:A=\=B

• The following properties should hold at call time:
A is currently a term which is not a free variable.
B is currently a term which is not a free variable.
A is an arithmetic expression.
B is an arithmetic expression.
• The following properties hold globally:
A=\=B is evaluable at compile-time.
int is the measure used to determine the size of the terms that A is bound to, for any type of approximation.
int is the measure used to determine the size of the terms that B is bound to, for any type of approximation.
Other properties:

• The following properties hold globally:
This predicate is understood natively by CiaoPP.
Exp1=\=Exp2 is side-effect free.
Exp1=\=Exp2 is binding insensitive.
All calls of the form Exp1=\=Exp2 are deterministic.
Goal Exp1=\=Exp2 produces inf solutions.
Indicates the type of test that a predicate performs. Required by the nonfailure analyisis.

An arithmetic expression is a term built from numbers and evaluable functors that represent arithmetic functions. An arithmetic expression evaluates to a number, which may be an integer (int/1) or a float (flt/1). The evaluable functors allowed in an arithmetic expression are listed below, together with an indication of the functions they represent. All evaluable functors defined in ISO-Prolog are implemented, as well as some other useful or traditional. Unless stated otherwise, an expression evaluates to a float if any of its arguments is a float, otherwise to an integer.

• - /1: sign reversal. ISO
• + /1: identity.

• -- /1: decrement by one.

• ++ /1: increment by one.

• - /2: subtraction. ISO
• * /2: multiplication. ISO
• // /2: integer division. Float arguments are truncated to integers, result always integer. ISO
• / /2: division. Result always float. ISO
• rem/2: integer remainder. The result is always an integer, its sign is the sign of the first argument. ISO
• mod/2: modulo. The result is always a positive integer. ISO
• abs/1: absolute value. ISO
• sign/1: sign of. ISO
• float_integer_part/1: float integer part. Result always float. ISO
• float_fractional_part/1: float fractional part. Result always float. ISO
• truncate/1: The result is the integer equal to the integer part of the argument. ISO
• integer/1: same as truncate/1.

• float/1: conversion to float. ISO
• floor/1: largest integer not greater than. ISO
• round/1: integer nearest to. ISO
• ceiling/1: smallest integer not smaller than. ISO
• ** /2: exponentiation. Result always float. ISO
• >> /2: integer bitwise right shift. ISO
• << /2: integer bitwise left shift. ISO
• /\ /2: integer bitwise and. ISO
• \/ /2: integer bitwise or. ISO
• \ /1: integer bitwise complement. ISO
• # /2: integer bitwise exclusive or (xor).

• exp/1: exponential (e to the power of). Result always float. ISO
• log/1: natural logarithm (base e). Result always float. ISO
• sqrt/1: square root. Result always float. ISO
• sin/1: sine. Result always float. ISO
• cos/1: cosine. Result always float. ISO
• atan/1: arc tangent. Result always float. ISO

• gcd/2: Greatest common divisor. Arguments must evaluate to integers, result always integer.

In addition to these functors, a list of just a number evaluates to this number. Since a quoted string is just a list of integers, this allows a quoted character to be used in place of its ASCII code; e.g. "A" behaves within arithmetic expressions as the integer 65. Note that this is not ISO-compliant, and that can be achieved by using the ISO notation 0'A.

Arithmetic expressions, as described above, are just data structures. If you want one evaluated you must pass it as an argument to one of the arithmetic predicates defined in this library.

Usage:arithexpression(E)

E is an arithmetic expression.

Other properties:

• The following properties hold globally:
arithexpression(Arg1) is side-effect free.

• If the following properties hold at call time:
Arg1 is currently a term which is not a free variable.
then the following properties hold globally:
All calls of the form arithexpression(Arg1) are deterministic.
Indicates the type of test that a predicate performs. Required by the nonfailure analyisis.

Usage:intexpression(E)

E is an integer expression.

Other properties:

• The following properties hold globally:
intexpression(Arg1) is side-effect free.

## Documentation on multifiles

No further documentation available for this predicate. The predicate is multifile.

## Documentation on imports

This module has the following direct dependencies: