4.4 Occurs-Check

It is possible, and sometimes useful, to write programs that unify a variable to a term in which that variable occurs, thus creating a cyclic term. The usual mathematical theory behind Logic Programming forbids the creation of cyclic terms, dictating that an occurs-check should be done each time a variable is unified with a term. Unfortunately, an occurs-check would be so expensive as to render Prolog impractical as a programming language. Thus cyclic terms may be created and may cause loops trying to print them.

SICStus Prolog mitigates the problem by its ability to unify, compare (see Term Compare), assert, and copy cyclic terms without looping. The write_term/[2,3] built-in predicate can optionally handle cyclic terms; see Term I/O. Unification with occurs-check is available as a built-in predicate; see Misc Pred. Predicates testing (a)cyclicity are available in a library package; see Term Utilities. Other predicates usually do not handle cyclic terms well.