[comp.lang.prolog] NU-Prolog "pure" declarations

lee@mulga.oz (Lee Naish) (05/19/88)

>    tell us more about what declaring a predicate "pure" in
>NU-Prolog means and does?  How much is assumed, how much checked?

Declaring a predicate pure takes away meaning rather than adding it.
Without pure declarations predicates have a procedural meaning and
(possibly) a declarative one.  Predicates which are declared pure have
no well defined procedural meaning (and if they use non-logical predicates
such as cut, var and assert they have no meaning :-).  The compiler
doesnt check for "pure" predicates calling non-logical predicates but
"nit" (a program checker like lint) does so.

Currently, pure declarations do not have much effect.  They result in a
slightly different transformation of clause bodies containing constructs
relating to negation.  An example appears in "Parallelizing NU-Prolog":

        % check list of clauses in cl/4 form can be made deterministic:
        % for all clause heads H, all previous clauses PrevCL whose
        % heads HP unify with H must have cuts
?- pure check_det/1.    % enable optimization
check_det(CL) :-
        all PrevCL.H.HP.GP
        (       append(PrevCL, cl(H, _, _, _)._, CL),
                member(cl(HP, _, GP, _), PrevCL),
                unify(H, HP)
        )
        =>
                GP ~= $nocut.

Without the pure declaration the body would be transformed into
the following (plus some code to delay it until CL is ground):

        \+ (	append(PrevCL, cl(H, _, _, _)._, CL),
		member(cl(HP, _, GP, _), PrevCL),
		unify(H, HP),
		\+ GP ~= $nocut
	).

With the pure declaration it is transformed into

        \+ (	GP = $nocut,
		append(PrevCL, cl(H, _, _, _)._, CL),
		member(cl(HP, _, GP, _), PrevCL),
		unify(H, HP)
	).

Some time is saved by removing the "double negation" and potentially
much more time is saved by reordering the conjuncts so failure occurs
before unify/2 is called.  Unify/2 uses $VAR(N) to represent variables
so it is relatively expensive.
(Astute readers will note that the use of $nocut indicates BAD DATA
STRUCTURE DESIGN.)

When declarations are also used by a preprocessor (nac) which adds
control information to NU-Prolog programs.  It reorders clause bodies
and adds when declarations (which cause suspension of calls).  Since
these changes are not always desirable (especially with non-logical
code), nac only changes predicates with pure declarations.

	Lee Naish