Bottom-up Evaluation
The standard strategy of evaluation of logic programs is top-down and depth-first: from the goal, a number of clauses are identified as being possibly able to prove the goal, and recursion over the literals of their bodies is performed. An alternative strategy is to start from the facts and use clauses to derive new facts; this strategy is called bottom-up. It is considered better than the top-down one when the aim is that of producing all consequences of a given program, rather than proving a single goal. In particular, finding all consequences of a program in the standard top-down and depth-first manner may not terminate while the bottom-up evaluation strategy terminates.
The bottom-up evaluation strategy maintains the set of facts proved so far during evaluation. This set is initially empty. With each step, new facts are derived by applying a program clause to the existing facts, and are added to the set. For example, the bottom up evaluation of the following program requires two steps:
A(q). B(X):-A(X).The set of consequences is initially empty. At the first step, A(q)
is the only clause whose body can be proved (because it is empty), and A(q)
is therefore added to the current set of consequences. At the second step, since A(q)
is proved, the second clause can be used and B(q)
is added to the consequences. Since no other consequence can be proved from {A(q),B(q)}
, execution terminates.
The advantage of the bottom-up evaluation over the top-down one is that cycles of derivations do not produce an infinite loop. This is because adding a consequence to the current set of consequences that already contains it has no effect. As an example, adding a third clause to the above program generates a cycle of derivations in the top-down evaluation:
A(q). B(X):-A(X). A(X):-B(X).For example, while evaluating all answers to the goal A(X)
, the top-down strategy would produce the following derivations:
In other words, the only consequence A(q)
is produced first, but then the algorithm cycles over derivations that do not produce any other answer. More generally, the top-down evaluation strategy may cycle over possible derivations, possibly when other ones exist.
The bottom-up strategy does not have the same drawback, as consequences that were already derived has no effect. On the above program, the bottom-up strategy starts adding A(q)
to the set of consequences; in the second step, B(X):-A(X)
is used to derive B(q)
; in the third step, the only facts that can be derived from the current consequences are A(q)
and B(q)
, which are however already in the set of consequences. As a result, the algorithm stops.
In the above example, the only used facts were ground literals. In general, every clause that only contains constraints in the body is considered a fact. For example, a clause A(X):-X>0,X<10
is considered a fact as well. For this extended definition of facts, some facts may be equivalent while not syntactically equal. For example, A(q)
is equivalent to A(X):-X=q
and both are equivalent to A(X):-X=Y, Y=q
. To solve this problem, facts are translated into a normal form in which the head contains a tuple of all-different variables; two facts are then equivalent if their bodies are equivalent on the variables of the head, that is, their sets of solutions are the same when restricted to these variables.
As described, the bottom-up approach has the advantage of not considering consequences that have already been derived. However, it still may derive consequences that are entailed by those already derived while not being equal to any of them. As an example, the bottom up evaluation of the following program is infinite:
A(0). A(X):-X>0. A(X):-X=Y+1, A(Y).The bottom-up evaluation algorithm first derives that A(X)
is true for X=0
and X>0
. In the second step, the first fact with the third clause allows for the derivation of A(1)
. In the third step, A(2)
is derived, etc. However, these facts are already entailed by the fact that A(X)
is true for any nonnegative X
. This drawback can be overcome by checking for entailment facts that are to be added to the current set of consequences. If the new consequence is already entailed by the set, it is not added to it. Since facts are stored as clauses, possibly with "local variables", entailment is restricted over the variables of their heads.
Read more about this topic: Constraint Logic Programming
Famous quotes containing the word evaluation:
“Good critical writing is measured by the perception and evaluation of the subject; bad critical writing by the necessity of maintaining the professional standing of the critic.”
—Raymond Chandler (18881959)