100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Programming

Arithmetic in Prolog

Learn how Prolog evaluates arithmetic expressions with is/2, the difference between unification and arithmetic comparison, and common numeric predicates.

Lists & RecursionBeginner8 min readJul 10, 2026
Analogies

Why Prolog Needs is/2

Unlike most languages, = in Prolog is unification, not assignment or evaluation, so X = 2+3 binds X to the unevaluated term +(2,3), not 5. To actually evaluate arithmetic you must use is/2: X is 2+3 evaluates the right-hand side and unifies the result with X. The right-hand side of is/2 must be a fully evaluable arithmetic expression, built from numbers, arithmetic functors, and bound variables.

🏏

Cricket analogy: Writing X = 2+3 is like writing 2+3 on a scorecard without adding it up, just recording the expression, whereas X is 2+3 is like the scorer actually totting up two partnership scores into a final total of 5 runs.

Arithmetic Comparison Operators

Prolog provides =:=, =\=, <, >, =<, >= for numeric comparison, which evaluate both sides arithmetically, distinct from =, \=, ==, \== which compare terms structurally without evaluating anything. For example, 1+1 =:= 2 succeeds because both sides evaluate to 2, but 1+1 == 2 fails, because it compares the term +(1,1) against the term 2, and those are structurally different terms.

🏏

Cricket analogy: =:= is like checking whether two different bowlers' figures actually total the same number of wickets even if recorded differently, 3+2 versus 5, while == is like checking whether the scorecard entries are the exact same literal notation, which they aren't.

prolog
?- X = 2+3.
X = 2+3.

?- X is 2+3.
X = 5.

?- 1+1 =:= 2.
true.

?- 1+1 == 2.
false.

?- X is 7 mod 3, Y is 7 // 2, Z is 7 rem -2.
X = 1,
Y = 3,
Z = 1.

Integer and Float Operations

Beyond +, -, and *, Prolog distinguishes // for integer division from / for true division, which may return a float even when both operands are integers. mod/2 and rem/2 both compute a remainder but differ in sign handling: mod/2's result takes the sign of the divisor while rem/2's result takes the sign of the dividend, and functions like abs/1, sqrt/1, min/2, max/2, and ** are available inside any is/2 expression.

🏏

Cricket analogy: // (integer division) is like calculating how many complete overs fit into a given number of balls, discarding the leftover partial over, e.g. 25 balls // 6 = 4 complete overs, unlike / which would give 4.1666 overs.

mod/2 returns a result with the same sign as the divisor, e.g. -7 mod 3 = 2, while rem/2 returns a result with the same sign as the dividend, e.g. -7 rem 3 = -1. Choosing the wrong one is a common source of off-by-sign bugs when working with negative numbers.

Common Pitfalls: Unbound Variables and Evaluation Order

is/2's right-hand side must be fully evaluable at the moment of the call; if it contains an unbound variable, Prolog raises an instantiation_error instead of guessing. Arithmetic comparisons like < and > require both sides to be evaluable too. A classic beginner bug is writing = where is/2 was intended, which silently builds an unevaluated term instead of raising an error, only causing visible trouble later when that term is compared arithmetically or printed and looks wrong.

🏏

Cricket analogy: Calling X is Y + 1 before Y is known is like a scorer trying to add next ball's runs to a total before the ball has even been bowled; Prolog throws an instantiation_error rather than guessing.

Writing Total = Price + Tax when you meant Total is Price + Tax is a classic bug: Total gets bound to the unevaluated term +(Price,Tax) instead of a number, and the mistake often only surfaces later when Total is compared with =:= or printed and looks wrong.

  • = is unification (no evaluation); is/2 evaluates the right-hand arithmetic expression and unifies it with the left.
  • =:=, =\=, <, >, =<, >= compare evaluated numeric values; ==, \==, =, \= compare terms structurally.
  • // performs integer division; / can return a float; mod takes the divisor's sign, rem takes the dividend's sign.
  • is/2's right-hand side must be fully ground (no unbound variables) or Prolog raises an instantiation_error.
  • Confusing = with is/2 is a classic beginner bug that silently builds an unevaluated term.
  • Arithmetic functions like abs/1, sqrt/1, min/2, max/2, and ** are available inside is/2 expressions.

Practice what you learned

Was this page helpful?

Topics covered

#Programming#PrologStudyNotes#ArithmeticInProlog#Arithmetic#Prolog#Needs#Comparison#StudyNotes#SkillVeris#ExamPrep