Samiksha Jaiswal (Editor)

Occurs check

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
Occurs check

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.

Contents

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; the literals p ( X , f ( X ) ) and p ( g ( Y ) , Y ) are unifiable without occurs check, producing the refuting empty clause.

Prolog implementation

By default, 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 t 1 with term t 2 is reduced in many cases from O ( size ( t 1 ) + size ( t 2 ) ) to O ( min ( size ( t 1 ) , size ( t 2 ) ) ) ; in the particular, frequent case of variable-term unifications, runtime shrinks to O ( 1 ) .

A naive omission of the occurs check leads to the creation of cyclic structures and may cause unification to loop forever. Modern implementations, based on Colmerauer's Prolog II, use rational tree unification to avoid looping. See image for an example run of the unification algorithm given in Unification (computer science)#A unification algorithm, trying to solve the goal c o n s ( x , y ) = ? c o n s ( 1 , c o n s ( x , c o n s ( 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.

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). Implementations offering sound unification for all unifications are Strawberry Prolog and (optionally, via a runtime flag): XSB and SWI-Prolog.

References

Occurs check Wikipedia