next up previous index
Next: Index Up: Grace Utility Predicates Previous: Matrix Predicates

Application Predicates

The utility predicates in this section can be used to apply a given predicate iteratively to one or more lists. This is an extension of the apply_macros library which saves a lot of typing for many constraint problems, because the iteration over the lists does not have to be explicitly repeated each time a new operation has to be performed.

 
+Pred apply_to +Lists
Apply the predicate Pred iteratively to all elements of Lists, which must be a list of lists with equal length or of variables. This means that first the heads of all lists in Lists are appended to the callable term Pred, and the resulting term is called. The same is then repeated for next elements of the lists up to their end. Note that in Prolog, one does not need separate applist and maplist predicates - maplist just iterates over one more list which is taken as output. For example, maplist(Pred, ListIn, ListOut) can be written as Pred apply_to [ListIn, ListOut].

 

apply_with_accs(+Pred, +Lists, +AccsIn, ?AccsOut)
Apply the predicate Pred iteratively to all elements of the list of lists Lists, using N accumulators. The initial values of the accumulators are stored in the list AccsIn and the output ones in AccsOut. For example, the sumlist(Pred, List, AccIn, AccOut) predicate from the apply_macros library could be written as apply_with_accs(Pred, [List], [AccIn], [AccOut]).

 

apprest(+Pred, +List)
Apply the predicate Pred iteratively on all elements of the list List, and pass also the list remainder as an argument to the called predicate. A typical use of this predicate is to define predicates like alldifferent/1  , however without using a quadratic number of constraints. alldifferent/1 could be defined as follows:
alldifferent(List) :-
    apprest(diff1, List).

delay diff1(V, _) if var(V).
diff1(Var, Vars) :-
    #\=(Var) apply_to [Vars].
For each list element we call diff1/2 with the second argument being the whole list without this element. diff1/2 delays until its first argument is instantiated, and then it calls Var #\= El for each element of this list.



next up previous index
Next: Index Up: Grace Utility Predicates Previous: Matrix Predicates



Micha Meier
Tue Jul 2 10:07:34 MET DST 1996