ArticleslgStudy

mathematics

Occurs check

Occurs check is a mathematics topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Occurs check rather than just read about it. In short: In computer science, the occurs check is a part of algorithms for syntactic unification. It causes unification of a variable V and a structure S to fail if S contains V.

Occurs check — main illustration
Occurs check — illustration

Key takeaways

  • Occurs check belongs to mathematics; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Occurs check to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Occurs check from memory before moving on to harder problems.

Reference excerpt

In computer science, the occurs check is a part of algorithms for syntactic unification. It causes unification of a variable V and a structure S to fail if S contains V.

Application in theorem proving In theorem proving, unification without the occurs check can lead to unsound inference. For example, the Prolog goal X=f(X) will succeed, binding X to a cyclic structure which has no counterpart in the Herbrand universe. As another example, without occurs-check, a resolution proof can be found for the non-theorem (∀x∃y.p(x,y))→(∃y∀x.p(x,y)): the negation of that formula has the conjunctive normal form p(X,f(X))∧¬p(g(Y),Y), with f and g denoting the Skolem function for the first and second existential quantifier, respectively. Without occurs check, the literals p(X,f(X)) and p(g(Y),Y) are unifiable, producing the refuting empty clause.

Rational tree unification Prolog implementations usually omit the occurs check for reasons of efficiency, which can lead to circular data structures and looping. By not performing the occurs check, the worst case complexity of unifying a term t1 with term t2 is reduced in many cases from O(size(t1)+size(t2)) to O(min(size(t1),size(t2))); in the frequent case of variable-term unification, runtime shrinks to O(1).

Implementations, based on Colmerauer's Prolog II,

use rational tree unification to avoid looping. However it is difficult to keep the complexity time linear in the presence of cyclic terms. Examples where Colmerauers algorithm becomes quadratic can be readily constructed. Jaffar’s 1984 work proposed a refinement based on union–find techniques, effectively reducing the worst-case complexity to near-linear time. Modern systems — including SWI-Prolog, SICStus Prolog, Scryer Prolog, and Ciao Prolog — appear to implement variants of this approach. See image for an example run of the unification algorithm given in Unification (computer science)#A unification algorithm, trying to solve the goal cons(x,y)=?cons(1,cons(x,cons(2,y))), however without the occurs check rule (named "check" there); applying rule "eliminate" instead leads to a cyclic graph (i.e. an infinite term) in the last step.

Sound unification ISO Prolog implementations have the built-in predicate unify_with_occurs_check/2 for sound unification but are free to use unsound or even looping algorithms when unification is invoked otherwise, provided the algorithm works correctly for all cases that are "not subject to occurs-check" (NSTO). The built-in acyclic_term/1 serves to check the finiteness of terms. Implementations offering sound unification for all unifications are Qu-Prolog, EyeProlog, and Strawberry Prolog and optionally, via a runtime flag: XSB, SWI-Prolog, CxProlog, Flowlog, Shumway Prolog, Tau Prolog, Trealla Prolog and Scryer Prolog. A variety of optimizations can render sound unification feasible for common cases.

See also W.P. Weijland (1990). "Semantics for Logic Programs without Occur Check". Theoretical Computer Science. 71: 155–174. doi:10.1016/0304-3975(90)90194-m.

Notes

References

Worked examples

Example 1 — a first encounter with Occurs check

Start with the simplest possible case. Write down what Occurs check claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In mathematics, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Occurs check before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Occurs check ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Occurs check

In research
Occurs check appears in mathematics research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Occurs check in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Occurs check is common in secondary-school and first-year university syllabi. It links to neighbouring topics Automated theorem proving, Logic programming, Programming constructs, so understanding it makes those chapters shorter.
In everyday life
Look for Occurs check outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Occurs check” →

How to study Occurs check in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Occurs check means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Occurs check out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Occurs check in simple terms?

In computer science, the occurs check is a part of algorithms for syntactic unification. It causes unification of a variable V and a structure S to fail if S contains V.

Why does Occurs check matter?

Because it connects several mathematics ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Occurs check?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Occurs check.

Tags

  • Automated theorem proving
  • Logic programming
  • Programming constructs
  • Unification (computer science)

Keep exploring