Arithmetic: is vs. = vs. ==
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 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
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
== 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
Comparison Operators for Numbers
=:=, =\=, <, >, =<, >= 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.
| Operator | What it does | Forces evaluation? | Can bind unbound variables? |
|---|---|---|---|
| = | attempts unification | no | yes |
| is | evaluates right side, unifies left with the number | yes (right side) | yes (left side only) |
| == | checks structural identity | no | no |
| =:= | evaluates both sides, compares as numbers | yes (both sides) | no |
==. 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.
+ 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
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 solutionWrite 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 solutionWrite 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 solutionChapter 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