Arithmetic
The right-hand side of is must be fully instantiated:
8.1 is/2 evaluates expressions
The right-hand side of is must be fully instantiated:
?- X = 2 + 3. % X = 2+3 (just a term! no evaluation)
?- X is 2 + 3. % X = 5
?- X is 7 / 2. % X = 3.5 (SWI: 3.5; with prefer_rationals → 7r2)
?- X is 7 // 2. % X = 3 integer division (truncating)
?- X is 7 mod 2. % X = 1
?- X is -7 rem 2. % X = -1
?- X is 2 ** 10. % X = 1024
?- X is 2 ^ 100. % big integer
?- X is sqrt(16). % X = 4.0
?- X is max(3, 7), Y is abs(-5), Z is sign(-3).
?- X is pi, Y is e, Z is cot(1.0).
?- X is truncate(3.7), Y is round(3.5), Z is ceiling(3.2), W is floor(3.8).
?- X is 5 /\ 3, Y is 5 \/ 3, Z is 5 xor 3, V is \ 5, S is 1 << 4, R is 16 >> 2.
?- X is gcd(12, 18), Y is msb(1000), Z is random(10), F is random_float.
?- X is cputime, Y is integer(2.5), Z is float(3), D is float_integer_part(3.7).?- X is Y + 1.
ERROR: Arguments are not sufficiently instantiated8.2 Arithmetic comparison (evaluates both sides)
arithmetically not equal X < Y, X > Y less / greater less or equal (not <=) greater or equal
?- 1 + 2 =:= 3. % true
?- 1 + 2 = 3. % false (different terms!)
?- 2.0 =:= 2. % true8.3 Examples
is/2 is not reversible: factorial(N, 120) won't find N. For relational arithmetic use CLP(FD) (§28):
factorial(0, 1).
factorial(N, F) :- N > 0, N1 is N - 1, factorial(N1, F1), F is N * F1.
max_of(X, Y, X) :- X >= Y.
max_of(X, Y, Y) :- X < Y.
celsius_fahrenheit(C, F) :- F is C * 9 / 5 + 32.:- use_module(library(clpfd)).
?- X #= 3 + Y, Y #= 2. % X = 5
?- 120 #= N * 5. % N = 248.4 Useful numeric predicates
succ/2 (succ(X, 4) → X = 3, reversible), plus/3 (plus(2, Y, 5) → Y = 3), between/3 (between(1, 3, X) enumerates), numlist/3, sum_list/2, max_list/2, min_list/2.