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. In theorem proving, unification without the occurs check can lead to unsound inference.
will succeed, binding X to a cyclic structure which has no counterpart in the Herbrand universe.
As another example,[1] without occurs-check, a resolution proof can be found for the non-theorem[2]
: the negation of that formula has the conjunctive normal form
denoting the Skolem function for the first and second existential quantifier, respectively.
are unifiable, producing the refuting empty clause.
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
; in the particular, frequent case of variable-term unifications, runtime shrinks to
[nb 1] Modern implementations, based on Colmerauer's Prolog II, [4] [5] [6] [7] 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 [8] can be readily constructed, but refinement proposals exist.
, 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.
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).
[9] The built-in acyclic_term/1 serves to check the finiteness of terms.
Implementations offering sound unification for all unifications are Qu-Prolog and Strawberry Prolog and (optionally, via a runtime flag): XSB, SWI-Prolog, CxProlog, Tau Prolog, Trealla Prolog and Scryer Prolog.
A variety [10][11] of optimizations can render sound unification feasible for common cases.
"Semantics for Logic Programs without Occur Check".