DeAL Tutorial

Arithmetic (.deal | .fac)

Examples of Arithmetic Terms:

Y + Z
X * (Y / Z)
  • All participating variables must be bound to numeric values.
  • The result of the evaluated term replaces the expression.
Precedence

Level Operator Order
0 log (unary)
1 * / right to left
2 mod right to left
3 + - right to left

Example: The term A / B * C + D will be evaluated in the following order:

  1. r1 = B * C
  2. r2 = A / r1
  3. r3 = r2 + D

Parenthesis
Use parenthesis to enforce arithmetic operation execution order. (A / B) * (C + D) will be evaluated in the following order:

  1. r1 = A / B
  2. r2 = C + D
  3. r3 = r1 * r2

Result Types

  • Integer arithmetic returns integers.
  • Mixed (float and integer) arithmetic returns float.

Example:

result(X) <- b(A, B), X = A / B.
b(1, 2).

query result(X) returns result(0) when A,B are declared as integers in the schema, it returns result(0.500) when A,B are declared as float.

Examples

Example: Express the distance from Austin to Bastrop in feet.

Two equivalent formulations:

distance_feet1(X, Z, W) <- distance(X, Z, Y), W = 5280 * Y.
%or
distance_feet(X, Z, 5280 * Y) <- distance(X, Z, Y).
export distance_feet($X, $Z,  Y).

When the user types:

query distance_feet('Austin', 'Bastrop', Z).

the system returns:

distance_feet( 'Austin', 'Bastrop', 158400).
-- 1 solution

Example: "List all cities which are within 100 miles of Austin".

close(X, Y, Z) <- distance(X, Y, W), W < Z.

export close($X, Y, $Z).

query close('Austin', City, 100).

    close('Austin', 'San Antonio', 100).
    close('Austin', 'Bastrop', 100).
    -- 2 solutions

The query form: export close($X, Y, Z). will not compile. Why?

Arithmetic: Safety

Also, the program

close1(X, Y, Z) <- W < Z, distance(X, Y, W).
export close1($X, Y, $Z).

will not compile, since W is unbound when used in the comparison. Predicates must be permuted in the body to render it safe.


Last Updated 2014