In general it cannot be done (well) completely at compile-time:
work done by a call often depends on the size
of its input (difficult to approximate at compile time,
even using abstract interpretation):
q([],[]).
q([X|RX],[X1|RX1]) :-
X1 is X +1,
q(RX,RX1).
Approach [DLH PLDI89, DLHL / LH TR94]:
generate at compile-time functions (to be evaluated
at run-time) that efficiently approximate task size,
transform programs to carry out
run-time granularity control.
Note: size computations can be done on-the-fly [ICLP 95].
Example (with q above):
..., q(X,Y) & r(X), ...
Cost is equal to (cost function ). Assuming
threshold is 4 units: