Arithmetic: is vs. = vs. ==

Course 1 · Ch 7
Arithmetic: is vs. = vs. ==
The classic beginner trap — foreshadowed since prolog1-3's own tip-box, fully unpacked here

prolog1-3's tip-box promised this chapter directly. Three operators, all doing something with equality-shaped names, all genuinely different — and mixing them up is the single most common early Prolog mistake.

= — Unification, Not Arithmetic

?- X = 2 + 3. X = 2+3. -- NOT 5! X is unified with the unevaluated TERM 2+3

X = 2 + 3 does not compute anything. It unifies X with the actual compound term 2+3 — functor +, two arguments — never evaluated as arithmetic at all. Genuinely surprising for anyone assuming = means "compute and assign."

is — Forcing Arithmetic Evaluation

?- X is 2 + 3. X = 5. ?- X is Y + 1. -- ERROR: Y is not bound to a number — arithmetic cannot proceed

is evaluates its right side as a genuine arithmetic expression, then unifies the left side with the resulting number. A real asymmetry unlike unification's own flexibility: is's right side must already resolve to a number — an unbound variable there is a real error, not something is can work around the way unification handles unbound variables freely.

== — Structural Equality, No Unification Side Effects

?- X = Y. X = Y. -- succeeds — LINKS X and Y together (prolog1-3) ?- X == Y. false. -- fails — two DIFFERENT unbound variables are not IDENTICAL

== checks whether two terms are already identical, with zero unification side effects — nothing gets bound. Two different unbound variables aren't identical, even though = on the same two would succeed by linking them.

The Three Side by Side

?- X = 2+3. X = 2+3. -- unifies with the unevaluated term ?- X is 2+3. X = 5. -- evaluates, then unifies with the number ?- 2+3 == 2+3. true. -- structurally identical terms, no evaluation needed ?- 2+3 == 5. false. -- the compound term 2+3 is NOT the number 5, structurally

Comparison Operators for Numbers

?- 2+2 =:= 4. true. -- forces arithmetic evaluation on BOTH sides, then compares ?- 2+2 == 4. false. -- 2+2 is structurally a compound term, not the atom 4 — no evaluation

=:=, =\=, <, >, =<, >= all force arithmetic evaluation on both sides — the same spirit as is, genuinely different from =='s own pure structural comparison. A second, concrete demonstration of the exact same underlying distinction.

OperatorWhat it doesForces evaluation?Can bind unbound variables?
=attempts unificationnoyes
isevaluates right side, unifies left with the numberyes (right side)yes (left side only)
==checks structural identitynono
=:=evaluates both sides, compares as numbersyes (both sides)no
Ask what you're actually trying to do
Checking if two things are already the same? Use ==. Computing a number? Use is. Binding a variable, or matching a structure? Use =. Comparing two arithmetic expressions numerically? Use =:=. Each real intent maps to exactly one operator.
2+2 == 4 failing is a genuinely common point of confusion
Coming from any language where + is always eagerly evaluated, seeing 2+2 == 4 report false is a real surprise. In Prolog, 2+2 is just an unevaluated compound term until something — is or =:= — explicitly forces it to become a number.

Coding Challenges

Challenge 1

In swipl, run X = 3 * 4 and separately X is 3 * 4, printing what X actually contains in each case, and explain the difference in a comment.

📄 View solution
Challenge 2

Write a query that unifies X = Y, then a separate query that checks X == Y for two fresh, unbound variables, showing the two results differ and explaining why in a comment.

📄 View solution
Challenge 3

Write a query attempting X is Y + 1 where Y is a genuinely unbound variable, show the resulting error, and explain why is has this restriction while = does not.

📄 View solution

Chapter 7 Quick Reference

  • = unifies with the unevaluated term — X = 2+3 gives X = 2+3, not 5
  • is evaluates its right side as arithmetic, then unifies the left side with the resulting number — the right side must already resolve to a number
  • == checks structural identity with zero unification side effects — two different unbound variables are not identical, even though = would link them
  • =:=/=\=/</>/=</>= force arithmetic evaluation on both sides, unlike =='s pure structural comparison
  • 2+2 == 4 is false — 2+2 stays an unevaluated compound term unless is or =:= forces it into a number
  • Next chapter: cut (!) — controlling backtracking, treated honestly