next up previous contents index
Next: Delaying Up: B-Prolog-specific Programming Previous: Matching clauses   Contents   Index

Efficient programs

Most of the Prolog programming rules for writing efficient programs for WAM-based systems are still effective for B-Prolog. The only exception is the rule for reducing data movements. In the WAM, as arguments are passed in registers, they can be reused by the first call in the body of a clause. Sometimes, the first call can be executed without rearranging the arguments. Nevertheless, as arguments are passed in stack frames in the ATOAM, they can be reused only by the last call in the body of a clause. To reduce data movements, it is necessary to make the last calls in a predicate as similar to the head as possible. For example, the clause

		  p([X|Xs],Y):-foo(X,Z),q(Xs,Y,Z).
is preferable to

		  p([X|Xs],Y):-foo(X,Z),q(Z,Xs,Y).
because in the first clause the two arguments Xs and Y can be reused by the last call, whereas in the second clause all three arguments need to be rearranged.



Neng-Fa Zhou
1999-11-24