Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 40 ยท Unification and Backtracking

Prolog Term Comparison: =, ==, =:=, Standard Order and Type Checks

Learn the difference between =, \=, ==, \==, =:= and is in Prolog, the standard order of terms with @< and compare/3, plus type tests like var, atom and ground.

Prolog has more ways to ask "are these two things the same?" than any other language, and each one means something different. Picking the wrong one is one of the most common beginner bugs. This lesson sorts them out and adds the standard order of terms and the type-checking predicates, which let a program ask what kind of thing a variable currently holds.

Prolog is read-only on this site. The runnable blocks use the Python mini-Prolog stand-in to recreate the key behaviors: unification versus identity, and the standard order.

Three kinds of "equal"

Operator Name Meaning Binds variables?
A = B unification make A and B identical if possible yes
A == B identity A and B are already identical no
A =:= B arithmetic equality both sides evaluate to the same number no

Their negations are \= (cannot unify), \== (not identical) and =\= (numerically different).

= versus ==

= can change variables to make a match. == never changes anything; it only looks.

PROLOG
?- X = Y.
X = Y.

?- X == Y.
false.

?- X = Y, X == Y.
X = Y.

?- a == a.
true.

?- f(X) == f(X).
true.

?- f(X) == f(Y).
false.

Two different unbound variables are never identical. After X = Y they are the same variable, so X == Y holds. \== is the opposite check, and it is the right way to say "these are different" when you do not want to bind anything. Compare X \= Y (fails if they can be made equal, so it fails for two unbound variables) with X \== Y (succeeds for two distinct unbound variables).

= versus is versus =:=

Arithmetic is separate from unification:

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

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

?- 2 + 3 = 5.
false.

?- 2 + 3 =:= 5.
true.

?- 1 =:= 1.0.
true.

?- 1 == 1.0.
false.

is evaluates its right side and unifies the result with its left side. =:=, <, >, =<, >= and =\= evaluate both sides and compare. All of them raise an error if a variable on an evaluated side is unbound. Do not use = when you mean to compute; you only get the unevaluated expression.

Variants and the occurs check

A =@= B ("structurally equivalent", also called variant) is true if the terms are equal up to a consistent renaming of variables: f(X, Y) =@= f(A, B) is true, but f(X, X) =@= f(A, B) is not. It is useful for detecting duplicates.

Standard unification does not check whether a variable occurs inside the term it is being bound to. The query X = f(X) succeeds in SWI-Prolog, producing a cyclic term (X = f(X)), which can crash programs that traverse it. The ISO-style safe version is:

PROLOG
?- unify_with_occurs_check(X, f(X)).
false.

It is slower, so Prolog omits the check by default. Pure Prolog programs rarely need it, but if you write theorem provers or type-inference code, you do.

The standard order of terms

Prolog can compare any two terms, not just numbers. Terms are sorted in a fixed standard order:

Variables < Numbers < Atoms < Strings < Compound terms

  • Variables are ordered by age (unreliable, do not depend on it).
  • Numbers are compared by value; if equal, a float comes before an integer.
  • Atoms are compared alphabetically (by character code).
  • Compound terms are compared first by arity, then by name, then by arguments from left to right.

The comparison operators are @<, @>, @=<, @>=, and the three-way compare(Order, A, B), where Order becomes <, = or >.

PROLOG
?- a @< b.
true.

?- 1 @< a.
true.

?- f(a) @< g.
false.

?- compare(Order, 3, 10).
Order = (<).

?- msort([b, 3, f(x), a, 1.5, "str"], L).
L = [1.5, 3, a, b, "str", f(x)].

msort/2 sorts by standard order and keeps duplicates; sort/2 removes duplicates; sort/4 can sort by a chosen argument and direction. Standard order is why sorting a list of mixed things always works.

Asking about a term: type checks

Because variables can hold anything, Prolog provides tests that look at what the term is right now:

Test True when
var(X) X is an unbound variable
nonvar(X) X is anything else
atom(X) X is an atom (but not a number, and not [] in SWI-Prolog 7+)
number(X) integer or float
integer(X), float(X) the specific number type
atomic(X) atom, number, string or []
compound(X) a compound term, for example a list [1] or f(a)
callable(X) an atom or compound, so it could be a goal
is_list(X) a proper list
ground(X) X has no unbound variables inside it
PROLOG
?- var(X).
true.

?- X = 5, var(X).
false.

?- atom(foo), \+ atom(5), \+ atom("text").
true.

?- ground(f(a, b)).
true.

?- ground(f(a, _)).
false.

Type checks are tests, not generators, and their answer depends on the moment they run. var(X), X = 5 is true, but X = 5, var(X) is false. These predicates are how library code behaves differently depending on which arguments you supply. Use them sparingly: relying on them often makes a predicate unusable "backwards", which is much of the point of Prolog.

Try it: unification versus identity

The stand-in has unify (like =) and walk (to follow bindings). We build identical (like ==): terms are identical if, after following bindings, they are equal everywhere and no variable would need to be bound.

Python = versus == (mini-Prolog stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Each row of the table shows whether the pair could unify and whether it already is identical. Note the difference for X and Y: they can unify, but they are not identical until they do.

Try it: the standard order

Python Sorting mixed terms (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Variables sort first, then numbers by value (the float 3.0 before the integer 3), then atoms, then strings, then compounds by arity first, which puts f(x) before g(a, b).

Which operator should I use?

  • Need to match or build a structure? =.
  • Need to know if two terms are already the same, without binding? ==.
  • Need to compare numeric values? =:=, <, >.
  • Need to compute? is.
  • Need to sort or compare arbitrary terms? @<, compare/3, msort/2.
  • Need to know whether two different things matter? \== when you do not want to bind, \= when it is a "cannot be made equal" test.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does X == Y return for two different unbound variables?

    == tests identity without binding. Two different variables are not identical.
  2. 2. What is the result of 2 + 3 = 5?

    2+3 is a compound term and 5 is an integer. They do not unify. Use =:= to compare values.
  3. 3. Which is the standard order of term types?

    This fixed order lets Prolog sort any list of terms.
  4. 4. What does var(X), X = 5 give, compared with X = 5, var(X)?

    var/1 tests the current state, and X is unbound only before the unification.
  5. 5. Which tests whether a term contains no unbound variables?

    ground(T) is true when T has no variables anywhere inside it.

Summary

  • = unifies, == tests identity without binding, =:= compares evaluated numbers, and is computes.
  • \=, \== and =\= are the matching negations.
  • Terms have a standard order (variables, numbers, atoms, strings, compounds) used by @<, compare/3, msort/2 and sort/2.
  • var/1, atom/1, number/1, compound/1, ground/1 and friends test what a term is at that moment.
  • unify_with_occurs_check/2 prevents cyclic terms; =@= checks for variants.

Next: Proof Search Trees