Occurs check

From HandWiki

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 [math]\displaystyle{ X = f(X) }[/math] 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] [math]\displaystyle{ (\forall x \exists y. p(x,y)) \rightarrow (\exists y \forall x. p(x,y)) }[/math]: the negation of that formula has the conjunctive normal form [math]\displaystyle{ p(X,f(X)) \land \lnot p(g(Y),Y) }[/math], with [math]\displaystyle{ f }[/math] and [math]\displaystyle{ g }[/math] denoting the Skolem function for the first and second existential quantifier, respectively; the literals [math]\displaystyle{ p(X,f(X)) }[/math] and [math]\displaystyle{ p(g(Y),Y) }[/math] are unifiable without occurs check, producing the refuting empty clause.

Cycle by omitted occurs check

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 [math]\displaystyle{ t_1 }[/math] with term [math]\displaystyle{ t_2 }[/math] is reduced in many cases from [math]\displaystyle{ O(\text{size}(t_1)+\text{size}(t_2)) }[/math] to [math]\displaystyle{ O(\text{min}(\text{size}(t_1),\text{size}(t_2))) }[/math]; in the particular, frequent case of variable-term unifications, runtime shrinks to [math]\displaystyle{ O(1) }[/math]. [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.

See image for an example run of the unification algorithm given in Unification (computer science), trying to solve the goal [math]\displaystyle{ cons(x,y) \stackrel{?}{=} cons(1,cons(x,cons(2,y))) }[/math], 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).[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, Tau Prolog, Trealla Prolog and Scryer Prolog. A variety [10][11] 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

  1. Some Prolog manuals state that the complexity of unification without occurs check is [math]\displaystyle{ O(\text{min}(\text{size}(t_1),\text{size}(t_2))) }[/math] (in all cases).[3] This is incorrect, as it would imply comparing arbitrary ground terms in constant time (by unifying [math]\displaystyle{ eq(t_1,t_2) }[/math] with [math]\displaystyle{ eq(X,X) }[/math]).

References

  1. David A. Duffy (1991). Principles of Automated Theorem Proving. Wiley. ; here: p.143
  2. Informally, and taking [math]\displaystyle{ p(x,y) }[/math] 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. Template:Cite tech report
  4. A. Colmerauer (1982). Prolog and Infinite Trees. Academic Press. 
  5. M.H. van Emden; J.W. Lloyd (1984). "A Logical Reconstruction of Prolog II". Journal of Logic Programming 2: 143–149. 
  6. Joxan Jaffar; Peter J. Stuckey (1986). "Semantics of Infinite Tree Logic Programming". Theoretical Computer Science 46: 141–158. doi:10.1016/0304-3975(86)90027-7. 
  7. B. Courcelle (1983). "Fundamental Properties of Infinite Trees". Theoretical Computer Science 25 (2): 95–169. doi:10.1016/0304-3975(83)90059-2. 
  8. Albertro Martelli; Gianfranco Rossi (1984). "Efficient Unification with Infinite Terms in Logic Programming". The International Conference oj Fifth Generation Computer Systems. https://www.ueda.info.waseda.ac.jp/AITEC_ICOT_ARCHIVES/ICOT/Museum/FGCS/FGCS84en-proc/84eFLP2-2.pdf. 
  9. 7.3.4 Normal unification in Prolog of ISO/IEC 13211-1:1995.
  10. Ritu Chadha; David A. Plaisted (1994). "Correctness of unification without occur check in prolog". The Journal of Logic Programming 18 (2): 99–122. doi:10.1016/0743-1066(94)90048-5. 
  11. Thomas Prokosch; François Bry (2020). "Unification on the Run". The 34th International Workshop on Unification. pp. 13:1–13:5. http://www3.risc.jku.at/publications/download/risc_6129/proceedings-UNIF2020.pdf.