1/*  Copyright 1986-2020 David H. D. Warren and Fernando C. N. Pereira
    2
    3    Permission is hereby granted, free of charge, to any person obtaining a
    4    copy of this software and associated documentation files (the
    5    "Software"), to deal in the Software without restriction, including
    6    without limitation the rights to use, copy, modify, merge, publish,
    7    distribute, sublicense, and/or sell copies of the Software, and to
    8    permit persons to whom the Software is furnished to do so, subject to
    9    the following conditions:
   10
   11    The above copyright notice and this permission notice shall be included
   12    in all copies or substantial portions of the Software.
   13
   14    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   15    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   16    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   17    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
   18    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
   19    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
   20    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   21*/
   22
   23:- public aggregate/3, one_of/2, ratio/3, card/2.   24
   25:- mode aggregate(+,+,?),
   26        dimensioned(+),
   27	one_of(+,?),
   28	i_aggr(+,+,?),
   29	u_aggr(+,+,?),
   30	i_total(+,?),
   31	i_maxs(+,?),
   32	i_mins(+,?),
   33	i_maxs0(+,+,+,?,?),
   34	i_mins0(+,+,+,?,?),
   35	u_total(+,?),
   36	u_sum(+,+,?),
   37	u_maxs(+,?),
   38	u_mins(+,?),
   39	i_maxs0(+,+,+,?,?),
   40	i_mins0(+,+,+,?,?),
   41	u_lt(+,+).   42
   43aggregate(Fn,Set,Val) :-
   44   dimensioned(Set), !,
   45   u_aggr(Fn,Set,Val).
   46aggregate(Fn,Set,Val) :-
   47   i_aggr(Fn,Set,Val).
   48
   49i_aggr(average,Set,Val) :-
   50   i_total(Set,T),
   51   length(Set,N),
   52   Val is T//N.
   53i_aggr(total,Set,Val) :-
   54   i_total(Set,Val).
   55i_aggr(max,Set,Val) :-
   56   i_maxs(Set,List),
   57   one_of(List,Val).
   58i_aggr(min,Set,Val) :-
   59   i_mins(Set,List),
   60   one_of(List,Val).
   61i_aggr(maximum,[V0:O|S],V) :-
   62   i_maxs0(S,V0,[O],_,V).
   63i_aggr(minimum,[V0:O|S],V) :-
   64   i_mins0(S,V0,[O],_,V).
   65
   66u_aggr(average,Set,V--U) :-
   67   u_total(Set,T--U),
   68   length(Set,N),
   69   V is T//N.
   70u_aggr(total,Set,Val) :-
   71   u_total(Set,Val).
   72u_aggr(max,Set,Val) :-
   73   u_maxs(Set,List),
   74   one_of(List,Val).
   75u_aggr(min,Set,Val) :-
   76   u_mins(Set,List),
   77   one_of(List,Val).
   78u_aggr(maximum,[V0:O|S],V) :-
   79   u_maxs0(S,V0,[O],_,V).
   80u_aggr(minimum,[V0:O|S],V) :-
   81   u_mins0(S,V0,[O],_,V).
   82
   83i_total([],0).
   84i_total([V:_|R],T) :-
   85   i_total(R,T0),
   86   T is V+T0.
   87
   88i_maxs([V:X|Set],List) :-
   89   i_maxs0(Set,V,[X],List,_).
   90
   91i_maxs0([],V,L,L,V).
   92i_maxs0([V0:X|R],V0,L0,L,V) :- !,
   93   i_maxs0(R,V0,[X|L0],L,V).
   94i_maxs0([U:X|R],V,_,L,W) :-
   95   U>V, !,
   96   i_maxs0(R,U,[X],L,W).
   97i_maxs0([_|R],V,L0,L,W) :-
   98   i_maxs0(R,V,L0,L,W).
   99
  100i_mins([V:X|Set],List) :-
  101   i_mins0(Set,V,[X],List,_).
  102
  103i_mins0([],V,L,L,V).
  104i_mins0([V:X|R],V,L0,L,W) :- !,
  105   i_mins0(R,V,[X|L0],L,W).
  106i_mins0([U:X|R],V,_,L,W) :-
  107   U<V, !,
  108   i_mins0(R,U,[X],L,W).
  109i_mins0([_|R],V,L0,L,W) :-
  110   i_mins0(R,V,L0,L,W).
  111
  112u_total([],0--U).
  113u_total([V:_|R],T) :-
  114   u_total(R,T0),
  115   u_sum(T0,V,T).
  116
  117u_sum(X--U,Y--U,Z--U) :- !,
  118   Z is X+Y.
  119u_sum(X--U,Y--U1,Z--U) :-
  120   ratio(U,U1,M,M1), M>M1, !,
  121   Z is X + (Y*M1)//M.
  122u_sum(X--U1,Y--U,Z--U) :-
  123   ratio(U,U1,M,M1), M>M1, !,
  124   Z is (X*M1)//M + Y.
  125
  126u_maxs([V:X|Set],List) :-
  127   u_maxs0(Set,V,[X],List,_).
  128
  129u_maxs0([],V,L,L,V).
  130u_maxs0([V0:X|R],V0,L0,L,V) :- !,
  131   u_maxs0(R,V0,[X|L0],L,V).
  132u_maxs0([U:X|R],V,_,L,W) :-
  133   u_lt(V,U), !,
  134   u_maxs0(R,U,[X],L,W).
  135u_maxs0([_|R],V,L0,L,W) :-
  136   u_maxs0(R,V,L0,L,W).
  137
  138u_mins([V:X|Set],List) :-
  139   u_mins0(Set,V,[X],List,_).
  140
  141u_mins0([],V,L,L,V).
  142u_mins0([V:X|R],V,L0,L,W) :- !,
  143   u_mins0(R,V,[X|L0],L,W).
  144u_mins0([U:X|R],V,_,L,W) :-
  145   u_lt(U,V), !,
  146   u_mins0(R,U,[X],L,W).
  147u_mins0([_|R],V,L0,L,W) :-
  148   u_mins0(R,V,L0,L,W).
  149
  150u_lt(A,X--U) :-
  151   Y is -X,
  152   u_sum(A,Y--U,Z--_),
  153   Z<0.
  154
  155dimensioned([(_--_):_|_]).
  156
  157one_of([X|_],X).
  158one_of([_|R],X) :-
  159   one_of(R,X).
  160
  161ratio(N,M,R) :- R is (N*100)//M.
  162
  163card(S,N) :- length(S,N)