Did you know ... | Search Documentation: |
Indexing for body code |
The current SWI-Prolog versions only consider the head for generating clause indexing. This would make it impossible to examine a head argument and pass the argument in the body without copying the argument. Consider the two clauses below. Both have equal semantics under Prolog. The first version would loose clause indexing while the second creates a copy of the f/1 argument. Neither is desirable.
p(X) :- X = f(I), integer(I), q(X). p(f(I)) :- integer(I), q(f(X)).
As of SWI-Prolog 8.3.21, unifications against head arguments that happen before anything else in the body are compiled special. Effectively, the term unified too is moved into the head (providing indexing) and places where this term is used simply use the corresponding argument. The explicit unification is removed. Decompilation (clause/2) reverses this process, but may not produce exactly the same term. The re-inserted unfications are ordered according to the argument position and the variable is always on the left hand of the =/2. Thus,
p(X,Y) :- f(_) = Y, X = g(_), q(X,Y).
Is decompiled into the following equivalent clause.
p(X,Y) :- X = g(_), Y = f(_), q(X,Y).
Additional notes:
true
while preserving this
optimization.p(X) :- X = a.
is fully equivalent to p(a).