next up previous contents
Next: Controlling the Control Up: Pragmatics Previous: Pragmatics

Programming Tips

Control primitives should be used carefully, at least for a first implementation: they can lead to incorrect programs in CLP more easily than in Prolog. This is so because some implicit assumptions on the classification of cuts for Prolog, which was based on the behavior of some builtins, cannot be extended to CLP. The Prolog-safe code for max/3 in Section 4.7, translated below to Prolog IV, is not safe any more:

max(X,Y,X):- gtlin(X, Y),!.
max(X,Y,Y):- lelin(X, Y).

The fact is that the comparison performed by gtlin/2 can now succeed on two free variables, so on backtracking lelin/2 might be called as well--and this is disallowed by the cut. The following call exhibits a wrong behavior:

?- max(5, X, Y), X = 8.

since the correct answer would have been X = 8, Y = 8. The programmer has to ensure that the proper instantiation mode is used when calling such predicates (which in fact breaks their declarative transparency), or be aware that answers can be lost, depending on the constraint system supported by the language.

% latex2html id marker 3691

One of the initial tasks in CLP is making up a correct model of the problem. When coming to a neat model, people naturally try to be frugal in the use of relationships, and not to set up too many equations. This is a sensible advice in general, but for some cases putting redundant constraints is advantageous: the reason is that it shortens communication paths inside the solver, so that faster reductions are possible. As an example, if we have the constraints X > Y, Y > 0, X + Y = Z, then the constraint Z > 0 is implied by all of them; but trying to assign a negative number to Z and failing takes some propagation steps which would not be needed if the (redundant) constraint Z > 0 were directly added to the system: this is called overconstraining. Its impact in the execution time depends heavily on the actual language and its implementation, but in general it can be tried if the problem to be solved is very complex, or if the constraint solver is weak. Excessive overconstraining can be negative, though: too much equations, many of which are redundant, add up to the amount of information to be processed.

next up previous contents
Next: Controlling the Control Up: Pragmatics Previous: Pragmatics