
Coral supports aggregation in the head of rules, the functions currently
implemented are, {\bf min}, {\bf max}, {\bf any}, {\bf sum}, {\bf prod},
{\bf count}, {\bf avg}, and simple grouping.  There are also versions
of each (that make sense) with the {\em distinct} suffix, indicating
that duplicates should be eliminated before the aggregation is performed.
There are also operators, so that each of these functions can also be
performed on (multi)sets in the body of rules; note that the
implementation of the operators is (currently) completely distinct
from the aggregation part of the code.

Each form of aggregation is seen as a special type of argument. It is
parsed as a normal functor, then converted to the special form in
{\em create\_functor()}.

When an insert is attempted on a relation that is performing some
type of grouping, {\em insert\_grouping()} is called to perform 
the insert.   If this is the first tuple inserted with unique
values in the non-grouping columns, it is inserted as normal;
otherwise the {\em update\_grouping()} function is called to update
the {\bf stored} tuple {\bf in-place}.  The reason this works
(it was quite a shock), is because this tuple is guarenteed to not
be accessed until this aggregation is complete as long as the
{\em monotonic} annotation is off.  The exact reason for this reason
is not entirely clear to me, even after hearing Raghu explain it a
few times.

If either aggregation or negation is used in a module,
{\em Ordered Search} evaluation is used. There is a paper on the
Coral web site detailing the theory and implementation of ordered
search, but the code within Coral is basically write-only. 
The similarity between aggregation and negation is that all tuples
in a grouping, or of the form being negated,
must be known to have been computed, before the negation/aggregation
can be evaluated. i.e. If the literal `not p(2,Y)' appears in a rule,
we must have already computed enough of p to determine that no tuple
of p with a first argument of 2 exists, or will be still be computed.


\noindent
Files to look at:
{\em
\begin{itemize}
\item CArg.[Chi]    : Definition of Aggregation CArg types.
\item CArgUtil.C    : update\_grouping function.
\item ParseUtils.C  : create\_functor function.
\item StorageRelation.C  : insert\_grouping function.
\end{itemize}
}
