Did you know ... | Search Documentation: |
aggregate.pl -- Aggregation operators on backtrackable predicates |
This library provides aggregating operators over the solutions of a predicate. The operations are a generalisation of the bagof/3, setof/3 and findall/3 built-in predicates. Aggregations that can be computed incrementally avoid findall/3 and run in constant memory. The defined aggregation operations are counting, computing the sum, minimum, maximum, a bag of solutions and a set of solutions. We first give a simple example, computing the country with the smallest area:
smallest_country(Name, Area) :- aggregate(min(A, N), country(N, A), min(Area, Name)).
There are four aggregation predicates (aggregate/3, aggregate/4, aggregate_all/3 and aggregate/4), distinguished on two properties.
Var^Goal
) and providing multiple solutions for the remaining free
variables in Goal. The aggregate_all/3 predicate uses findall/3,
implicitly qualifying all free variables and providing exactly one
solution, while aggregate_all/4 uses sort/2 over solutions that
Discriminator (see below) generated using findall/3.country(belgium, 11000000)
may
succeed twice, we can use the following to avoid counting the
population of Belgium twice:
aggregate(sum(P), Name, country(Name, P), Total)
All aggregation predicates support the following operators below in
Template. In addition, they allow for an arbitrary named compound term,
where each of the arguments is a term from the list below. For example,
the term r(min(X), max(X))
computes both the minimum and maximum binding
for X.
sum(1)
.min(Min, Witness)
, where Min is the minimal version
of Expr over all solutions, and Witness is any other template
applied to solutions that produced Min. If multiple
solutions provide the same minimum, Witness corresponds to
the first solution.min(Expr, Witness)
, but producing the maximum result.Acknowledgements
The development of this library was sponsored by SecuritEase, http://www.securitease.com
min(X)
, max(X)
,
min(X,Witness)
or max(X,Witness)
and Goal has no solutions, i.e.,
the minimum and maximum of an empty set is undefined.
The Template values count
, sum(X)
, max(X)
, min(X)
, max(X,W)
and
min(X,W)
are processed incrementally rather than using findall/3 and
run in constant memory.
foreach(Generator, Goal) :- findall(Goal, Generator, Goals), maplist(call, Goals).
The actual implementation uses findall/3 on a template created from the variables shared between Generator and Goal. Subsequently, it uses every instance of this template to instantiate Goal, call Goal and undo only the instantiation of the template and not other instantiations created by running Goal. Here is an example:
?- foreach(between(1,4,X), dif(X,Y)), Y = 5. Y = 5. ?- foreach(between(1,4,X), dif(X,Y)), Y = 3. false.
The predicate foreach/2 is mostly used if Goal performs backtrackable destructive assignment on terms. Attributed variables (underlying constraints) are an example. Another example of a backtrackable data structure is in library(hashtable). If we care only about the side effects (I/O, dynamic database, etc.) or the truth value of Goal, forall/2 is a faster and simpler alternative. If Goal instantiates its arguments it is will often fail as the argument cannot be instantiated to multiple values. It is possible to incrementally grow an argument:
?- foreach(between(1,4,X), member(X, L)). L = [1,2,3,4|_].
Note that SWI-Prolog up to version 8.3.4 created copies of Goal using copy_term/2 for each iteration, this makes the current implementation unable to properly handle compound terms (in Goal's arguments) that share variables with the Generator. As a workaround you can define a goal that does not use compound terms, like in this example:
mem(E,L) :- % mem/2 hides the compound argument from foreach/2 member(r(E),L). ?- foreach( between(1,5,N), mem(N,L)).
free_variables(Generator, Template, OldList, NewList)
finds this
set using OldList as an accumulator.