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. 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, distinguished on two properties.
^
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 and Distinguish (see below) generated using findall/3.
aggregate(sum(P), Name, country(Name, P), Total)
All aggregation predicates support the following operator 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. I.e. the term r(min(X), max(X)) computes both the minimum and maximum binding for X.
The development of this library was sponsored by SecuritEase, http://www.securitease.com
The implementation executes forall/2 if Goal does not contain any variables that are not shared with Generator.
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. No
^
P, setof, or bagof
free_variables(Generator, Template, OldList, NewList) finds this set, using OldList as an accumulator.