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

Usage and interface

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

Usage 2:A is B

Usage 3:A is B

Usage 4:A is B

Other properties:
A is B

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

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

Other properties:

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

Other properties:

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

Other properties:

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

Other properties:

PREDICATE=:=/2
ISOExp1=:=Exp2

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

Usage:A=:=B

Other properties:

PREDICATE=\=/2
ISOExp1=\=Exp2

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

Usage:A=\=B

Other properties:

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: addition. ISO
  • - /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:
      (basic_props:sideff/2)arithexpression(Arg1) is side-effect free.

    • If the following properties hold at call time:
      (term_typing:nonvar/1)Arg1 is currently a term which is not a free variable.
      then the following properties hold globally:
      (native_props:is_det/1)All calls of the form arithexpression(Arg1) are deterministic.
      (native_props:test_type/2)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:
        (basic_props:sideff/2)intexpression(Arg1) is side-effect free.

      Documentation on imports

      This module has the following direct dependencies: