Feedback Think of Prolog as searching an infinite tree of proofs 

Forum: Com Sci 221, U Chicago, Autumn 2001, Discuss Classwork
Re: Question Stack Order in Prolog
Date: 2001, Nov 29
From: Mike O'Donnell <odonnell@cs.uchicago.edu>

Recall how the history of a recursive procedure computation can be understood as an activation tree. The normal implementation of recursion with a stack never generates the whole tree. Rather, the stack traces paths from the root to other nodes in the tree, and the history of the stack sweeps across the tree from left to right.

Similarly, the computation of a Prolog program traverses a virtual tree representing all possible goal-directed proofs using the clauses in the program. The Prolog stack does not trace paths in the tree, but the computation goes from left to right in the tree, with the stack both increasing and decreasing along a given path from the root. While recursive computations traverse the whole tree, a Prolog computation stops as soon as it finds a leaf of the tree corresponding to success.

If an activation tree is infinite, then the recursive computation always tries to traverse the whole thing, so it gets into an infinite loop. But a Prolog computation only traverses the tree until it finds a leaf representing success, then stops and prints out the solution associated with that successful proof. The computation may halt, in spite of an infinite path in the tree, as long as it finds a successful proof before hitting the infinite path.

The order in which branches leave a node in the proof tree is determined by the order of clauses within the definition of a given relation. So reordering of clauses can determine whether the computation hits an infinite path first or succeeds.

But that's not the phenomenon that you're observing. The order of goals on the right-hand side of a clause affects the order in which edges occur along a path in the proof tree. In the case of the multiplication clause that you mention


mult(s(x),y,z) :- mult(x,y,w), add(w,y,z).
if the mult(x,y,w) goal fails along a particular path, then the add(w,y,z) goal is never tried. Perhaps the add(w,y,z) goal produces an infinite path. Then the order above works fine, but the alternate order

mult(s(x),y,z) :- add(w,y,z), mult(x,y,w).
goes into an infinite attempt to solve add(w,y,z) before noticing that mult(x,y,w) fails.

This is a key way in which Prolog programming isn't quite as clean as the basic logic programming concept suggests. In order to actually make things work, you have to get details of the presentation just right, even though from the point of view of pure logic they make no difference to the meanings of the clauses. The official ideology is expressed by the slogan

Algorithm = Logic + Control
The idea is that you should be able to write clauses just because they express logical truths, then fiddle with the "control" through ordering and some other nastier tricks, to achieve termination and good performance. In practice, nobody has succeeded in separating logic cleanly from control. I predict that it will be done some day, but that day is decades away.




Mike O'D.


to: "Think of Prolog as searching an infinite tree of proofs"