# 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. 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.

- aggregate vs. aggregate_all
- The aggregate predicates use setof
*3 (aggregate*4) or bagof*3 (aggregate*3), dealing with existential qualified variables (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 and Distinguish (see below) generated using findall/3. - The Distinguish argument
- The versions with 4 arguments provide a Distinguish argument that allow for keeping duplicate bindings of a variable in the result. For example, if we wish to compute the total population of all countries we do not want to lose results because two countries have the same population. Therefore we use:
== aggregate(sum(P), Name, country(Name, P), Total) ==

- count
- Count number of solutions. Same as sum(1).
- sum(Expr)
- Sum of Expr for all solutions.
- min(Expr)
- Minimum of Expr for all solutions.
- min(Expr, Witness)
- A term min(Min, Witness), where Min is the minimal version of Expr over all Solution and Witness is any other template applied to Solution that produced Min. If multiple solutions provide the same minimum, Witness corresponds to the first solution.
- max(Expr)
- Maximum of Expr for all solutions.
- max(Expr, Witness)
- As min(Expr, Witness), but producing the maximum result.
- set(X)
- An ordered set with all solutions for X.
- bag(X)
- A list of all solutions for X.

#### Acknowledgements

_|The development of this library was sponsored by SecuritEase, http:www.securitease.com |_Quintus, SICStus 4. The forall/2 is a SWI-Prolog built-in and term_variables/3 is a SWI-Prolog with a **different definition**. Analysing the aggregation template and compiling a predicate for the list aggregation can be done at compile time. aggregate_all/3 can be rewritten to run in constant space using non-backtrackable assignment on a term.

## Usage and interface

**Library usage:**`:- use_module(library(c_aggregate_test)).`**Exports:***Predicates:*`foreach/2`,`aggregate/3`,`aggregate/4`,`aggregate_all/3`,`aggregate_all/4`,`free_variables/4`,`foo/3`.

## Documentation on exports

`foreach(Generator,Goal)`

True if the conjunction of instances of Goal using the bindings from Generator is true. Unlike forall/2, which runs a failure-driven loop that proves Goal for each solution of Generator, foreach creates a conjunction. Each member of the conjunction is a copy of Goal, where the variables it shares with Generator are filled with the values from the corresponding solution.

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 ==

**Usage:**`aggregate(Template,Goal,Result)`

Aggregate bindings in Goal according to Template. The aggregate*3 version performs bagof*3 on Goal.

*The following properties should hold at call time:*

(`nonvar/1`)Template is currently a term which is not a free variable.

(`var/1`)Result is a free variable.*The following properties should hold globally:*

(undefined property)`nondet(aggregate(Template,Goal,Result))`

**Usage:**`aggregate(Template,Discriminator,Goal,Result)`

Aggregate bindings in Goal according to Template. The aggregate*3 version performs setof*3 on Goal.

*The following properties should hold at call time:*

(`nonvar/1`)Template is currently a term which is not a free variable.

(`nonvar/1`)Discriminator is currently a term which is not a free variable.

(`var/1`)Result is a free variable.*The following properties should hold globally:*

(undefined property)`nondet(aggregate(Template,Discriminator,Goal,Result))`

**Usage:**`aggregate_all(Template,Goal,Result)`

Aggregate bindings in Goal according to Template. The aggregate_all/3 version performs findall/3 on Goal.

*The following properties should hold at call time:*

(`nonvar/1`)Template is currently a term which is not a free variable.

(`var/1`)Result is a free variable.*The following properties should hold globally:*

(undefined property)`semidet(aggregate_all(Template,Goal,Result))`

**Usage:**`aggregate_all(Template,Discriminator,Goal,Result)`

Aggregate bindings in Goal according to Template. The aggregate_all/3 version performs findall*3 followed by sort*2 on Goal.

*The following properties should hold at call time:*

(`nonvar/1`)Template is currently a term which is not a free variable.

(`nonvar/1`)Discriminator is currently a term which is not a free variable.

(`var/1`)Result is a free variable.*The following properties should hold globally:*

(undefined property)`semidet(aggregate_all(Template,Discriminator,Goal,Result))`

**Usage:**`free_variables(Generator,Template,VarList0,VarList)`

In order to handle variables properly, we have to find all the universally quantified variables in the Generator. All variables as yet unbound are universally quantified, unless

1. they occur in the template 2. they are bound by X^P, setof, or bagoffree_variables(Generator, Template, OldList, NewList) finds this set, using OldList as an accumulator.

Richard O'KeefeJan Wielemaker (made some SWI-Prolog enhancements)Public domain (from DEC10 library). Distinguish between control-structures and data terms. Exploit our built-in term_variables/2 at some places?

*The following properties should hold at call time:*

(`nonvar/1`)Template is currently a term which is not a free variable.

(`nonvar/1`)VarList0 is currently a term which is not a free variable.

(`var/1`)VarList is a free variable.*The following properties should hold globally:*

(undefined property)`det(free_variables(Generator,Template,VarList0,VarList))`

**Usage:**`foo(Template,Goal,Result)`

*The following properties should hold at call time:*

(`nonvar/1`)Template is currently a term which is not a free variable.

(`nonvar/1`)Goal is currently a term which is not a free variable.

(`var/1`)Result is a free variable.*The following properties should hold globally:*

(undefined property)`nondet(foo(Template,Goal,Result))`

## Known bugs and planned improvements

- Goal is copied repeatetly, which may cause problems if attributed variables are involved.