Occurs check explained

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,[1] without occurs-check, a resolution proof can be found for the non-theorem[2]

(\forallx\existsy.p(x,y))(\existsy\forallx.p(x,y))

p(X,f(X))\landlnotp(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 particular, frequent case of variable-term unifications, runtime shrinks to

O(1)

.

Modern implementations, based on Colmerauer's Prolog II,[3] [4] [5] [6] 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 [7] can be readily constructed, but refinement proposals exist.

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)\stackrel{?}{=}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).[8] 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, Tau Prolog, Trealla Prolog and Scryer Prolog. A variety[9] [10] of optimizations can render sound unification feasible for common cases.

See also

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

Notes and References

  1. Book: David A. Duffy. Principles of Automated Theorem Proving. 1991. Wiley.
    here: p.143
  2. Informally, and taking

    p(x,y)

    to mean e.g. "x loves y", the formula reads "If everybody loves somebody, then a single person must exist that is loved by everyone."
  3. Book: A. Colmerauer. Alain Colmerauer. Prolog and Infinite Trees. 1982. Academic Press. K.L. Clark . S.-A. Tarnlund .
  4. M.H. van Emden . J.W. Lloyd . A Logical Reconstruction of Prolog II. Journal of Logic Programming. 1984. 2. 143–149.
  5. Joxan Jaffar . Peter J. Stuckey . Semantics of Infinite Tree Logic Programming. Theoretical Computer Science. 1986. 46. 141–158. 10.1016/0304-3975(86)90027-7. free.
  6. B. Courcelle. Bruno Courcelle. Fundamental Properties of Infinite Trees. Theoretical Computer Science. 1983. 25. 2. 95–169. 10.1016/0304-3975(83)90059-2. free.
  7. Albertro Martelli . Gianfranco Rossi . Efficient Unification with Infinite Terms in Logic Programming. The International Conference oj Fifth Generation Computer Systems. 1984.
  8. 7.3.4 Normal unification in Prolog of ISO/IEC 13211-1:1995.
  9. Ritu Chadha . David A. Plaisted . Correctness of unification without occur check in prolog. The Journal of Logic Programming. 1994. 18. 2. 99–122. 10.1016/0743-1066(94)90048-5. free.
  10. Thomas Prokosch . François Bry. Unification on the Run. The 34th International Workshop on Unification. 2020. 13:1–13:5.