1/* @(#)aggreg.pl	24.1 2/23/88 */
    2
    3/* 
    4	Copyright 1986, Fernando C.N. Pereira and David H.D. Warren,
    5
    6			   All Rights Reserved
    7*/
    8:- public aggregate/3, one_of/2, ratio/3, card/2.    9
   10:- mode aggregate(+,+,?),
   11        dimensioned(+),
   12	one_of(+,?),
   13	i_aggr(+,+,?),
   14	u_aggr(+,+,?),
   15	i_total(+,?),
   16	i_maxs(+,?),
   17	i_mins(+,?),
   18	i_maxs0(+,+,+,?,?),
   19	i_mins0(+,+,+,?,?),
   20	u_total(+,?),
   21	u_sum(+,+,?),
   22	u_maxs(+,?),
   23	u_mins(+,?),
   24	i_maxs0(+,+,+,?,?),
   25	i_mins0(+,+,+,?,?),
   26	u_lt(+,+).
   27
   28aggregate(Fn,Set,Val) :-
   29   dimensioned(Set), !,
   30   u_aggr(Fn,Set,Val).
   31aggregate(Fn,Set,Val) :-
   32   i_aggr(Fn,Set,Val).
   33
   34i_aggr(average,Set,Val) :-
   35   i_total(Set,T),
   36   length(Set,N),
   37   Val is T//N.
   38i_aggr(total,Set,Val) :-
   39   i_total(Set,Val).
   40i_aggr(max,Set,Val) :-
   41   i_maxs(Set,List),
   42   one_of(List,Val).
   43i_aggr(min,Set,Val) :-
   44   i_mins(Set,List),
   45   one_of(List,Val).
   46i_aggr(maximum,[V0:O|S],V) :-
   47   i_maxs0(S,V0,[O],_,V).
   48i_aggr(minimum,[V0:O|S],V) :-
   49   i_mins0(S,V0,[O],_,V).
   50
   51u_aggr(average,Set,V--U) :-
   52   u_total(Set,T--U),
   53   length(Set,N),
   54   V is T//N.
   55u_aggr(total,Set,Val) :-
   56   u_total(Set,Val).
   57u_aggr(max,Set,Val) :-
   58   u_maxs(Set,List),
   59   one_of(List,Val).
   60u_aggr(min,Set,Val) :-
   61   u_mins(Set,List),
   62   one_of(List,Val).
   63u_aggr(maximum,[V0:O|S],V) :-
   64   u_maxs0(S,V0,[O],_,V).
   65u_aggr(minimum,[V0:O|S],V) :-
   66   u_mins0(S,V0,[O],_,V).
   67
   68i_total([],0).
   69i_total([V:_|R],T) :-
   70   i_total(R,T0),
   71   T is V+T0.
   72
   73i_maxs([V:X|Set],List) :-
   74   i_maxs0(Set,V,[X],List,_).
   75
   76i_maxs0([],V,L,L,V).
   77i_maxs0([V0:X|R],V0,L0,L,V) :- !,
   78   i_maxs0(R,V0,[X|L0],L,V).
   79i_maxs0([U:X|R],V,_,L,W) :-
   80   U>V, !,
   81   i_maxs0(R,U,[X],L,W).
   82i_maxs0([_|R],V,L0,L,W) :-
   83   i_maxs0(R,V,L0,L,W).
   84
   85i_mins([V:X|Set],List) :-
   86   i_mins0(Set,V,[X],List,_).
   87
   88i_mins0([],V,L,L,V).
   89i_mins0([V:X|R],V,L0,L,W) :- !,
   90   i_mins0(R,V,[X|L0],L,W).
   91i_mins0([U:X|R],V,_,L,W) :-
   92   U<V, !,
   93   i_mins0(R,U,[X],L,W).
   94i_mins0([_|R],V,L0,L,W) :-
   95   i_mins0(R,V,L0,L,W).
   96
   97u_total([],0--U).
   98u_total([V:_|R],T) :-
   99   u_total(R,T0),
  100   u_sum(T0,V,T).
  101
  102u_sum(X--U,Y--U,Z--U) :- !,
  103   Z is X+Y.
  104u_sum(X--U,Y--U1,Z--U) :-
  105   ratio(U,U1,M,M1), M>M1, !,
  106   Z is X + (Y*M1)//M.
  107u_sum(X--U1,Y--U,Z--U) :-
  108   ratio(U,U1,M,M1), M>M1, !,
  109   Z is (X*M1)//M + Y.
  110
  111u_maxs([V:X|Set],List) :-
  112   u_maxs0(Set,V,[X],List,_).
  113
  114u_maxs0([],V,L,L,V).
  115u_maxs0([V0:X|R],V0,L0,L,V) :- !,
  116   u_maxs0(R,V0,[X|L0],L,V).
  117u_maxs0([U:X|R],V,_,L,W) :-
  118   u_lt(V,U), !,
  119   u_maxs0(R,U,[X],L,W).
  120u_maxs0([_|R],V,L0,L,W) :-
  121   u_maxs0(R,V,L0,L,W).
  122
  123u_mins([V:X|Set],List) :-
  124   u_mins0(Set,V,[X],List,_).
  125
  126u_mins0([],V,L,L,V).
  127u_mins0([V:X|R],V,L0,L,W) :- !,
  128   u_mins0(R,V,[X|L0],L,W).
  129u_mins0([U:X|R],V,_,L,W) :-
  130   u_lt(U,V), !,
  131   u_mins0(R,U,[X],L,W).
  132u_mins0([_|R],V,L0,L,W) :-
  133   u_mins0(R,V,L0,L,W).
  134
  135u_lt(A,X--U) :-
  136   Y is -X,
  137   u_sum(A,Y--U,Z--_),
  138   Z<0.
  139
  140dimensioned([(_--_):_|_]).
  141
  142one_of([X|_],X).
  143one_of([_|R],X) :-
  144   one_of(R,X).
  145
  146ratio(N,M,R) :- R is (N*100)//M.
  147
  148card(S,N) :- length(S,N)