In section 4.2 we make reference to a predicate,6=/2, that needs further explanation.
6=/2 is the predicate in charge of evaluating inequalities. We start by explaining why the Prolog predefined predicate for evaluating inequalities, “\ ==”, is not suitable for evaluating the inequalities that we have in the dual program, and, just after that, we show in detail how6=/2 is implemented.
The Prolog predefined predicate to evaluate inequalities, “\ ==”, can not be used to evaluate the inequalities in the dual program due to two reasons:
• It does not retain information from the disequalities. For example in pro- gram 4.4.1 if we evaluate the query ?−p(X). the answer will be X = a, al- though we have explicitly coded that X has to be different from a.
The reason for this behavior is that the information of the disequality is not stored in the variable, and once the disequality code has been evaluated the variable can again take the forbidden value(s).
• As “\ ==” is not coded in a library but in the Prolog compiler, there is no way to modify the implementation without removing the possibility of using our tool in other Prolog systems. As “\ ==” is not able to deal with the meta-term f A( ) (see Sec. 4.2 for more details on the meta-term f A( )) because it has no representation for it, we can not use the implementation to evaluate disequalities like s(0) 6=s(f A( )). Program 4.4.1 1 p(X) ← X \== a, q(X). 2 q(a).
In order to solve these problems we present an implementation of the predicate
6=/2 which retains the disequality information by using attributed variables (defined below) and is able to manage disequalities in which one or both terms are composed by using the meta-term f A( ). Besides, our implementation is able to accumulate information from different inequalities and, whenever it is possible, to simplify the resulting inequality. We start defining attributed variables.
Definition 4.4.1 (Attributed variables). Attributed variables provide a technique for
extending the Prolog unification algorithm [Hol92] by hooking the binding of attributed variables.
A suspension variable, or an attributed variable, is a variable to which there are suspended agents (or hooks) and attribute values (its name and a value) attached. Agents are registered onto suspension variables by action rules and each attribute is associated to a module where the hook is executed immediately after a successful unification.
The built-in predicates to make use of attributed variables are usually (in some Prolog systems their names change) the following ones:
• put attr/3 is used to register attribute values, • get attr/3 is used to retrieve attribute values and • del attr/2 is used to remove attribute values.
The way that agents are registered changes from one Prolog system to another one, but in general they offer some kind of predicate such as attr uni f y hook/3 or install constraint portray hook/4, so the programmer can define the agent behavior.
Now we present the core (simplified) of our implementation (program 4.4.2), and, after that, we expose some optimizations to increase the speedup of the evaluation of inequalities.
Remark. As the predicate6= /2 can not be coded because there is no symbol for 6=, in the code its name is dist/2. Besides, dist/2 is not defined as an infix operator, but as a prefix operator, so we have dist(a, b)in spite of a6= b.
Program 4.4.2 dist/2 implementation (simplified)
1 dist(T1, T2) ← var(T1), var(T2), T1 == T2, !, fail.
2 dist(T1, T2) ← var(T1), var(T2), add_attribute(T1/T2),
3 add_attribute(T2/T1).
4 dist(T1, T2) ← var(T1), is_forall(T2), !, fail.
5 dist(T1, T2) ← var(T1), is_functor(T2),
6 add_attr(T1/T2).
7 dist(T1, T2) ← var(T2), is_forall(T1), !, fail.
8 dist(T1, T2) ← var(T2), is_functor(T1),
9 add_attr(T2/T1).
10 11 dist(T1, T2) ← is_forall(T1), !, fail.
12 dist(T1, T2) ← is_forall(T2), !, fail.
13 14 dist(T1, T2) ← functor(T1, Name1, Arity1),
15
functor(T2, Name2, Arity2),
16 (Name1 \== Name2 ; Arity1 \== Arity2).
17 18 dist(T1, T2) ← functor(T1, Name, Arity),
19
functor(T2, Name, Arity),
20 T1=..[Name|Arguments1], 21 T2=..[Name|Arguments2], 22 dist_args(Arguments1, Arguments2). 23 24 dist_args([], []) ← !, fail. 25 dist_args([A1|L1], [A2|L2]) ← 26 dist(A1, A2) ; 27 dist_args(L1, L2).
• accept the inequality because the terms are different at this point and the modi- fications they can suffer do not affect the decision.
• fail the inequality because the terms unify.
• delay the decision until the variables are grounded by unification.
The last task is performed by adding an attribute to the involved variables, which is done by the predicate add attr/1 Its implementation is presented in program 4.4.3. Program 4.4.3 add attribute/1 implementation (simplified)
1 add_attr(T1/T2) ← get_attribute(T1, T1/T3), !, 2 remove_attribute(T1), 3 combine_attributes(T2, T3, T4), 4 put_attribute(T1, T1/T4). 5 add_attr(T1/T2) ← put_attribute(T1, T1/T2).
The agent to be executed just after the unification of an attributed variable is pre- sented in the program below. Basically it removes the variable’s attribute (so the hook is not reevaluated while the agent determines if the inequality holds) and determines if the variable has taken one of the values forbidden by the inequality or not. For doing that we use again the predicate dist/2.
Program 4.4.4 verify attribute/2 implementation (simplified)
1 verify_attribute(Value, T1/T2) ← 2 remove_attribute(T1), 3 dist(Value, T2).
Remark. For the sake of simplicity we have not included the code in charge of join- ing/splitting the list of inequalities that is stored and retrieved from the variable’s attribute.
Apart from trivial optimizations, like the elimination of identical inequalities, a huge optimization is carried out by the dist/2 predicate: the division of this complex inequalities into a disjunction of simpler and smaller inequalities. We introduce it by means of program 4.4.5.
Program 4.4.5 bigger than predicate in Peano numbers (exhaustive)
1 bt(s(X), s(Y)) ← bt(X, Y) 2 bt(s(X), 0). 3 bt(V, W) ← ((V, W) 6= (s(fA(_)), s(fA(_)))), 4 ((V, W) 6= (s(fA(_)), 0)), 5 fail.
Taking a look to the first inequality it can be seen that
((V, W) 6= (s(f A( )), s(f A( ))))
is equivalent to
(V 6=s(f A( )))_(W 6=s(f A( ))).
So, instead of having a big and complex inequality, we now have two small and simple inequalities joined by disjunction.
However, this simplification can not always be made: in program 4.4.6 the exist- ing relationship between the first and the second arguments of the predicate incr/2 produces an unexpected result. Let’s see why:
If we take the program obtained from the negation of the clauses (we omit the code for obtaining WFS results for the sake of clarity), shown in program 4.4.7, and we make the simplification obtained before, we obtain program 4.4.8.
When evaluating the clause, the implementation notices the disjunction and selects the first part of it (and only if it fails will it, through backtracking, look for the second part). Inside the first part, it evaluates the dist/2 and adds two attributes: the first to the variable X and the second one to the variable U. As a result, for the clause to succeed it is enough to have the variable U different from the variable X, and, since X is a free variable, this is always the case. neg incr(0, s(0)) succeeds, which is not the intended meaning. The problem is reproduced in a similar way for the second disjunction of the clause.
Program 4.4.6 incr predicate in Peano numbers
1 incr(X, s(X)).
Program 4.4.7 incr predicate in Peano numbers (original+dual)
1 incr(X, s(X)).
2 neg_incr(U, V) ← (U, V) 6= (X, s(X)).
Program 4.4.8 incr predicate in Peano numbers (erroneous simplification)
1 incr(X, s(X)).
2 neg_incr(U, V) ← (U 6= X); (V 6= s(X)).
The reason for this behavior is that we can not split inequalities when there are dependencies between the components of y used in ci(x, yi). In this case we have to store the whole inequality in each one of the affected variables and test it every time one of the variables is changed by unification.